diff --git a/README.md b/README.md
index 82c0d40..e66e629 100644
--- a/README.md
+++ b/README.md
@@ -183,6 +183,19 @@ const summary2 = await summarizer.summarize("Second article...");
summarizer.destroy();
```
+### Input Measurement
+
+Check if input fits within the model's limits before summarizing:
+
+```typescript
+import { Summarizer } from 'simple-chromium-ai';
+
+const usage = await Summarizer.checkInputUsage("Long article...", { type: "tldr" });
+if (usage.willFit) {
+ const summary = await Summarizer.summarize("Long article...", { type: "tldr" });
+}
+```
+
## Safe API
Every function has a Safe variant that returns Result types instead of throwing:
diff --git a/src/index.ts b/src/index.ts
index 19c078e..4ed9a76 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -23,8 +23,10 @@ export { translate } from "./translator";
export { translate as safeTranslate } from "./translator-safe";
// Re-export types for users
export type {
+ CheckInputUsageResult,
ChromiumAIInstance,
DetectResult,
+ InputUsageInfo,
PromptResult,
SummarizeResult,
TokenUsageInfo,
diff --git a/src/summarizer-safe.ts b/src/summarizer-safe.ts
index deb60e6..f61e6c9 100644
--- a/src/summarizer-safe.ts
+++ b/src/summarizer-safe.ts
@@ -1,7 +1,7 @@
///
import { ResultAsync } from "neverthrow";
-import type { SummarizeResult } from "./types";
+import type { CheckInputUsageResult, SummarizeResult } from "./types";
import { checkAvailability } from "./utils";
/**
@@ -76,3 +76,43 @@ export function summarize(
),
);
}
+
+/**
+ * Checks input usage for a summarization request without performing it.
+ * Creates a temporary Summarizer instance to measure the input, then destroys it.
+ *
+ * @param input The text to measure
+ * @param createOptions Optional creation options (type, format, length, sharedContext)
+ * @param summarizeOptions Optional options for the measurement (context, signal)
+ * @returns A Result containing input usage information or an Error
+ */
+export function checkInputUsage(
+ input: string,
+ createOptions?: SummarizerCreateOptions,
+ summarizeOptions?: SummarizerSummarizeOptions,
+): CheckInputUsageResult {
+ return checkAvailability(
+ () => Summarizer.availability(createOptions),
+ "Summarizer",
+ ).andThen(() =>
+ ResultAsync.fromPromise(
+ (async () => {
+ const summarizer = await Summarizer.create(createOptions);
+ try {
+ const inputUsage = await summarizer.measureInputUsage(
+ input,
+ summarizeOptions,
+ );
+ const inputQuota = summarizer.inputQuota || 0;
+ return { inputUsage, inputQuota, willFit: inputUsage <= inputQuota };
+ } finally {
+ summarizer.destroy();
+ }
+ })(),
+ (error) =>
+ error instanceof Error
+ ? error
+ : new Error(`Failed to check input usage: ${String(error)}`),
+ ),
+ );
+}
diff --git a/src/summarizer.ts b/src/summarizer.ts
index 1da2019..38926aa 100644
--- a/src/summarizer.ts
+++ b/src/summarizer.ts
@@ -1,6 +1,7 @@
///
import * as Safe from "./summarizer-safe";
+import type { InputUsageInfo } from "./types";
import { okOrThrow } from "./utils";
/**
@@ -38,3 +39,20 @@ export async function summarize(
const result = await Safe.summarize(text, createOptions, summarizeOptions);
return okOrThrow(result);
}
+
+/**
+ * Checks input usage for a summarization request without performing it.
+ * @throws {Error} If input usage check fails
+ */
+export async function checkInputUsage(
+ input: string,
+ createOptions?: SummarizerCreateOptions,
+ summarizeOptions?: SummarizerSummarizeOptions,
+): Promise {
+ const result = await Safe.checkInputUsage(
+ input,
+ createOptions,
+ summarizeOptions,
+ );
+ return okOrThrow(result);
+}
diff --git a/src/types.ts b/src/types.ts
index 602c77f..47f59e7 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -33,3 +33,14 @@ export type TranslateResult = ResultAsync;
export type DetectResult = ResultAsync;
export type SummarizeResult = ResultAsync;
+
+/**
+ * Information about input usage for a summarization request
+ */
+export interface InputUsageInfo {
+ inputUsage: number;
+ inputQuota: number;
+ willFit: boolean;
+}
+
+export type CheckInputUsageResult = ResultAsync;