/** * Adapter over the global `Summarizer` API. 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 model-download progress when creation requires one. */ monitor?: (m: CreateMonitor) => void; } type SummarizerAvailability = "unavailable" | "downloadable" | "downloading" | "available"; /** * Subset of `SummarizerCreateOptions` that the spec accepts when probing * availability. Pass the relevant creation shape so the result applies to the * configuration you intend to create. */ 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; interface ConfigureSummarizerCacheOptions { /** Soft cap on cached summarizer sessions. Default: `8`. */ max?: number; } /** * Bound the internal summarizer session cache. Excess entries are evicted in * LRU order (their `destroy?()` is invoked when present). Lowering `max` * immediately evicts down to the new ceiling. */ declare const configureSummarizerCache: (options?: ConfigureSummarizerCacheOptions) => void; /** * Drop every cached summarizer session. Sessions live for the tab lifetime by * default; call this to free them eagerly when navigating away from a feature * that won't be revisited. Sessions pinned by a lease or an in-flight call * leave the cache now and are destroyed once the last pin drops. */ declare const clearSummarizerSessions: () => void; /** * Drop the cached summarizer whose create-options match `options`. */ declare const clearSummarizerSession: (options: SummarizerCreateOptions) => void; /** * `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; /** * Default time-to-live for entries written by the built-in `"session"` / * `"local"` storage shortcuts: one hour. On-device model output changes as * the browser updates the model, so built-in entries expire instead of * persisting indefinitely. Override per call with `cacheTtl`. Custom * `{ get, set }` caches own their expiry policy. */ declare const DEFAULT_CACHE_TTL_MS: number; /** * @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 model-download progress when creation requires one. */ 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; /** * Time-to-live in milliseconds for entries written by the built-in * `"session"` / `"local"` storage shortcuts. Default: one hour * (`DEFAULT_CACHE_TTL_MS`). Ignored for custom `{ get, set }` caches, which * own their expiry policy. */ cacheTtl?: number; /** * Force a fresh summary. Skips the cache read, runs the model, and * replaces the cached value after a successful run. Applies to built-in * and custom caches. */ cacheRefresh?: boolean; /** * 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"; } /** * Session-affecting subset of `SummarizeOptions`. `prepareSummarizer` and * `summarize` derive the same native create options from these fields, so a * prepared session is reused by the matching call. * `monitor` observes creation only; it never affects the cache key. */ type PrepareSummarizerOptions = Pick; interface SummarizerLease { /** * Resolves when the native session is created. Rejects with * `SummarizerUnavailableError` when the API is missing or creation fails. */ ready: Promise; /** * Idempotent. The final release destroys the session once no other lease * or in-flight call uses it. */ release(): void; } /** * Start native session creation as soon as user intent is clear, before the * input exists. The matching `summarize` call reuses the prepared session * without a second create. Never throws synchronously; unavailability and * creation failures reject `ready`. Failed preparations leave the cache so a * later call can retry. */ declare const prepareSummarizer: (options: PrepareSummarizerOptions) => SummarizerLease; /** * 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 ConfigureSummarizerCacheOptions, type CreateMonitor, DEFAULT_CACHE_TTL_MS, type PrepareSummarizerOptions, type SummarizeOptions, type SummarizeResult, type SummarizerApi, type SummarizerAvailability, type SummarizerAvailabilityOptions, type SummarizerCreateOptions, type SummarizerInstance, type SummarizerLease, SummarizerUnavailableError, type SummaryCache, checkAvailability, clearSummarizerSession, clearSummarizerSessions, configureSummarizerCache, isAvailable, prepareSummarizer, summarize };