/** * Adapter over the global `Summarizer` API exposed by Chrome 138+. Feature- * detected; on browsers without it, every entry point returns `null` so * callers can stay declarative. * * Spec: https://developer.chrome.com/docs/ai/summarizer-api */ interface SummarizerInstance { summarize(text: string): Promise; summarizeStreaming?(text: string): AsyncIterable; destroy?(): void; } interface DownloadProgressEvent extends Event { readonly loaded: number; } interface CreateMonitor { addEventListener(type: "downloadprogress", listener: (event: DownloadProgressEvent) => void): void; } interface SummarizerCreateOptions { type?: "tldr" | "key-points" | "teaser" | "headline"; format?: "markdown" | "plain-text"; length?: "short" | "medium" | "long"; preference?: "auto" | "speed" | "capability"; sharedContext?: string; expectedInputLanguages?: string[]; expectedContextLanguages?: string[]; outputLanguage?: string; /** Observe the first-call model download (~1.7 GB on a fresh profile). */ monitor?: (m: CreateMonitor) => void; } type SummarizerAvailability = "unavailable" | "downloadable" | "downloading" | "available"; /** * Subset of `SummarizerCreateOptions` that the spec accepts when probing * availability. Edge enforces these strictly (e.g. fires a warning when * `outputLanguage` is missing); Chrome is more lenient. Pass the same * shape you'd pass to `create()` to get accurate, warning-free results * across browsers. */ interface SummarizerAvailabilityOptions { type?: SummarizerCreateOptions["type"]; format?: SummarizerCreateOptions["format"]; length?: SummarizerCreateOptions["length"]; preference?: SummarizerCreateOptions["preference"]; expectedInputLanguages?: string[]; expectedContextLanguages?: string[]; outputLanguage?: string; } interface SummarizerApi { availability(options?: SummarizerAvailabilityOptions): Promise; create(options?: SummarizerCreateOptions): Promise; } /** Whether the current environment exposes the Summarizer API. */ declare const isAvailable: () => boolean; declare const checkAvailability: (options?: SummarizerAvailabilityOptions) => Promise; /** * `sessionStorage`-backed cache so a successful summary renders instantly on * revisit, skipping the model entirely. The cache is best-effort: storage * disabled / quota exceeded falls through silently; the summary still * renders. */ interface SummaryCache { get(key: string): string | null; set(key: string, value: string): void; } /** * Public-facing `cache` option. Pass `"session"` / `"local"` for storage * shortcuts, or any `{ get, set }`-shaped object for a custom backend. */ type CacheOption = "session" | "local" | SummaryCache; /** * @web-ai-sdk/summarizer; building block for the Web's Built-in Summarizer API. * * Vanilla TypeScript / DOM core. The React adapter at `@web-ai-sdk/summarizer/react` is a * thin hook around this module. * * Spec: https://developer.chrome.com/docs/ai/summarizer-api */ interface SummarizeOptions { /** Text to summarize. Empty / whitespace input resolves to `{ output: null }`. */ input: string; /** BCP-47 language for input + output hints. Falls back to omitting hints if unsupported. */ language: string; /** Languages the model supports for input/output hints. Default: `["en", "es", "ja"]`. */ supportedLanguages?: readonly string[]; /** Summary shape. Default: `"tldr"`. */ type?: "tldr" | "key-points" | "teaser" | "headline"; /** Length preset. Default: `"medium"`. */ length?: "short" | "medium" | "long"; /** Output format. Default: `"plain-text"`. */ format?: "plain-text" | "markdown"; /** * Performance preference hint. `"speed"` biases toward a faster, lighter * model; `"capability"` toward a more comprehensive one; `"auto"` lets the * browser balance the two. The browser may override the hint when a * functional requirement (e.g. the requested language) needs a more capable * model. Default: `"auto"` (matches the platform default). */ preference?: "auto" | "speed" | "capability"; /** Native `sharedContext` string (a hint about who/what the summary is for). */ sharedContext?: string; /** Observe the first-call model download (~1.7 GB on a fresh profile). */ monitor?: (m: CreateMonitor) => void; /** * Result cache. Off by default; every call hits the model. Pass * `"session"` / `"local"` for the matching web-storage shortcut, or any * `{ get, set }`-shaped object for a custom backend. */ cache?: CacheOption; /** Cache key. Default: JSON string of route, input, and summary options. */ cacheKey?: string; /** * Streaming update callback (cleaned text, monotonically growing). * Receives the **cumulative** buffer, not deltas. */ onUpdate?: (text: string) => void; /** Abort signal. */ signal?: AbortSignal; } interface SummarizeResult { /** Final summary text (cleaned), or `null` if the input was empty. */ output: string | null; /** Whether the result came from the cache (no model call). */ cached: boolean; } declare class SummarizerUnavailableError extends Error { readonly name = "SummarizerUnavailableError"; } /** * Generate a summary. Uses streaming when the underlying instance supports * it, one-shot otherwise. Returns `{ output: null }` for empty input. * Throws `SummarizerUnavailableError` when the API isn't present in the * environment. * * Output is normalized via an internal cleaner (wrapping quotes/whitespace * stripped, internal whitespace collapsed). Anything beyond that — e.g. * trimming terminal punctuation for headline-style use cases — is the * consumer's concern. */ declare const summarize: (options: SummarizeOptions) => Promise; export { type CacheOption, type CreateMonitor, type SummarizeOptions, type SummarizeResult, type SummarizerApi, type SummarizerAvailability, type SummarizerAvailabilityOptions, type SummarizerCreateOptions, type SummarizerInstance, SummarizerUnavailableError, type SummaryCache, checkAvailability, isAvailable, summarize };