Skip to content

Rewriter

Building block for the Web’s Built-in Rewriter API. Revises and restructures existing text with tone / length adjustments, session reuse, streaming, and pluggable result caching. The React adapter lives at @web-ai-sdk/rewriter/react; see useRewriter.

Usage

import { rewrite } from "@web-ai-sdk/rewriter";
const result = await rewrite({
input: "hey, can u send me that doc when u get a sec? thx",
tone: "more-formal",
length: "as-is",
onUpdate: (text) => render(text),
});
console.log(result.output, result.cached);

result.output is the rewritten text (trimmed), or null when the input is empty. result.cached tells you whether the response came from the cache without invoking the model.

Options

interface RewriteOptions {
input: string; // text to rewrite
context?: string; // per-call background info
language?: string; // BCP-47; drives input/output hints when supported
supportedLanguages?: readonly string[];
tone?: "as-is" | "more-formal" | "more-casual";
format?: "as-is" | "markdown" | "plain-text";
length?: "as-is" | "shorter" | "longer";
sharedContext?: string;
monitor?: (m: CreateMonitor) => void;
cache?: "session" | "local" | { get, set };
cacheKey?: string;
onUpdate?: (text: string) => void;
signal?: AbortSignal;
}
NameTypeDescription
input requiredstringText to rewrite. Empty / whitespace resolves to { output: null }.
contextstringOptional per-call background information for the model.
languagestringBCP-47 language for input + output hints. Falls back to omitting hints if unsupported.
supportedLanguagesreadonly string[]Languages the model supports for hints. Defaults to ["en", "es", "ja"].
tone"as-is" | "more-formal" | "more-casual"Tone adjustment. Defaults to "as-is".
format"as-is" | "markdown" | "plain-text"Output format. Defaults to "as-is".
length"as-is" | "shorter" | "longer"Length adjustment. Defaults to "as-is".
sharedContextstringA hint shared across multiple rewrite tasks.
monitor(m) => voidObserve the first-call model download.
cache"session" | "local" | { get, set }Opt-in result cache.
cacheKeystringOverride the default cache key.
onUpdate(text: string) => voidStreaming update callback. Receives the cumulative buffer, not deltas.
signalAbortSignalAbort signal.

Returns

interface RewriteResult {
output: string | null;
cached: boolean;
}

Writer vs. Rewriter

The Writer generates new content from a task description; the Rewriter transforms text you already have. They share the same lifecycle (session reuse, streaming, caching) and the same tone / format / length ergonomics, but the Rewriter’s enums are relative adjustments (more-formal, shorter, as-is) rather than absolute targets.

Errors and unavailability

The vanilla rewrite() throws RewriterUnavailableError when the API is missing:

import { rewrite, RewriterUnavailableError } from "@web-ai-sdk/rewriter";
try {
const result = await rewrite({ input: draft, tone: "more-formal" });
} catch (err) {
if (err instanceof RewriterUnavailableError) return;
throw err;
}

AbortSignal is supported. Aborting mid-stream resolves cleanly; an opt-in result cache is not written for aborted runs.