web-ai-sdk
The TypeScript SDK for the Web AI surface.
Why use a wrapper?
You can use the browser APIs directly.
web-ai-sdk handles capability checks, session reuse, stream normalization, abort signals, cleanup, and optional result caching.
See package boundariesWhy one package per capability.
Prompt.
Prompt wraps the general LanguageModel API.
- Prompt / LanguageModel only
- Text, tools, structured output
Other capabilities.
The other APIs use different options and result types. Each has its own package.
- Translator (source + target language, pair-cached sessions)
- Summarizer (type and length)
- Writer (tone, format, length)
- Rewriter (relative tone/length shifts)
- Proofreader (per-issue offsets)
- Detector (confidence scores, alternates)
- WebMCP (an agent surface, not a model)
Lifecycle features by package.
Each package implements only the lifecycle features supported by its browser API.
| Package | Streaming | Session reuse | Result cache | AbortSignal |
|---|---|---|---|---|
| @web-ai-sdk/prompt | ✓ | ✓ | ✓ | ✓ |
| @web-ai-sdk/webmcp | – | – | – | ✓ |
| @web-ai-sdk/summarizer | ✓ | ✓ | ✓ | ✓ |
| @web-ai-sdk/translator | – | ✓ | ✓ | ✓ |
| @web-ai-sdk/detector | – | ✓ | ✓ | ✓ |
| @web-ai-sdk/writer | ✓ | ✓ | ✓ | ✓ |
| @web-ai-sdk/rewriter | ✓ | ✓ | ✓ | ✓ |
| @web-ai-sdk/proofreader | – | ✓ | ✓ | ✓ |
Packages.
- Session reuse. Compatible calls reuse a cached base session.
- Streaming first. Sessions yield deltas. ask() exposes cumulative text.
- Clean lifecycle. Abort signals cancel runs. Leases release prepared sessions.
- Declarative tools. Register typed tools and clean up with an AbortSignal.
- Last writer wins. Re-registers on hot reload without duplicate errors.
- SDK-first surface. Register, discover, and execute through typed functions.
This demo detects the page's native WebMCP API and routes tools in this page. It cannot show whether an external agent will discover or invoke a tool.
- Four shapes. key-points, tl;dr, teaser, headline.
- Three lengths. Choose short, medium, or long output.
- Streaming summaries. Receive cumulative text as the model responds.
- Pair caching. Sessions are cached by source and target language.
- Streaming first. Receive cumulative text as the model responds.
- String input. The SDK does not walk or mutate the DOM.
- Top candidate or full list. Use the top result or inspect all results above a threshold.
- Bias hints. Pass expectedInputLanguages when you know the likely languages.
- Wire the others. Application code can pass the result to another capability.
- Task in, prose out. Pass a writing task and receive generated text.
- Tone and length. Choose a tone and output length.
- Output cleanup. The wrapper trims outer whitespace and preserves internal formatting.
- Relative adjustments. more-formal, more-casual, shorter, longer, as-is.
- Streaming first. Receive cumulative text as the model responds.
- Same lifecycle. Reuse sessions, abort work, and opt in to result caching.
- Corrected text and diffs. Get the clean string and the list of edits with offsets.
- Highlight inline. Offsets index the original input, so you can mark each error in place.
- One-shot. No streaming; resolves once with the full result and optional caching.
Browser support.
Support differs by capability and browser. Each status links to a vendor source. The wrappers expose an unavailable state for fallback UI.
| Package | Chrome | Edge | Safari / Firefox |
|---|---|---|---|
| @web-ai-sdk/prompt | 148+ stable | 138+ Canary/Dev flag; OT 150 | no-op fallback |
| @web-ai-sdk/summarizer | 138+ stable | 138+ stable | no-op fallback |
| @web-ai-sdk/translator | 138+ stable | 148+ stable | no-op fallback |
| @web-ai-sdk/detector | 138+ stable | 148+ stable | no-op fallback |
| @web-ai-sdk/writer | Developer trial, local flag | 138+ Canary/Dev flag | no-op fallback |
| @web-ai-sdk/rewriter | Developer trial, local flag | 138+ Canary/Dev flag | no-op fallback |
| @web-ai-sdk/proofreader | Developer trial, local flag | 142+ Canary/Dev flag | no-op fallback |
| @web-ai-sdk/webmcp | OT 149+, local flag | OT 150 | no-op fallback |
Compare three API levels.
Each tab detects an article's language and creates a key-point summary. Compare the raw API, vanilla SDK, and React code.
// summarize.tsimport { detect } from "@web-ai-sdk/detector"import { summarize } from "@web-ai-sdk/summarizer"const article = document.querySelector("article")const text = article?.innerText ?? ""const detection = await detect({ input: text })const { output: summary } = await summarize({input: text,language: detection.output?.language ?? "en",type: "key-points", length: "short",onUpdate: render,})
Responsibilities.
SDK responsibilities.
The SDK handles browser API lifecycles. This includes support checks, sessions, streams, abort signals, and cleanup.
- Feature detection per package
- Session cache keyed by config
- AsyncIterable streams plus AbortSignal cleanup
- Hot-reload and StrictMode safe
- Zero-runtime fallback when API missing
Application responsibilities.
Application code owns UI, persistence, DOM traversal, fallbacks, and cross-capability workflows.
- Framework-agnostic core
- Optional
/reactsubpath - No global state, no providers required
- Tree-shakable per package
- Pick the layers you need