From 5224e083701de908f31c238653a02b71ad3bc195 Mon Sep 17 00:00:00 2001 From: Victor Huang Date: Mon, 6 Apr 2026 23:48:39 -0700 Subject: [PATCH] Add checkInputUsage to Summarizer API Add input measurement helper that checks if text fits within the model's limits before summarizing, mirroring the Prompt API's checkTokenUsage. Co-Authored-By: Claude Opus 4.6 --- README.md | 13 +++++++++++++ src/index.ts | 2 ++ src/summarizer-safe.ts | 42 +++++++++++++++++++++++++++++++++++++++++- src/summarizer.ts | 18 ++++++++++++++++++ src/types.ts | 11 +++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) 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;