Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 41 additions & 1 deletion src/summarizer-safe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="@types/dom-chromium-ai" />

import { ResultAsync } from "neverthrow";
import type { SummarizeResult } from "./types";
import type { CheckInputUsageResult, SummarizeResult } from "./types";
import { checkAvailability } from "./utils";

/**
Expand Down Expand Up @@ -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)}`),
),
);
}
18 changes: 18 additions & 0 deletions src/summarizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="@types/dom-chromium-ai" />

import * as Safe from "./summarizer-safe";
import type { InputUsageInfo } from "./types";
import { okOrThrow } from "./utils";

/**
Expand Down Expand Up @@ -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<InputUsageInfo> {
const result = await Safe.checkInputUsage(
input,
createOptions,
summarizeOptions,
);
return okOrThrow(result);
}
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ export type TranslateResult = ResultAsync<string, Error>;
export type DetectResult = ResultAsync<LanguageDetectionResult[], Error>;

export type SummarizeResult = ResultAsync<string, Error>;

/**
* Information about input usage for a summarization request
*/
export interface InputUsageInfo {
inputUsage: number;
inputQuota: number;
willFit: boolean;
}

export type CheckInputUsageResult = ResultAsync<InputUsageInfo, Error>;
Loading