Skip to content

useRewriter

React adapter for @web-ai-sdk/rewriter. Auto-runs on mount, re-runs when input / config options change, and streams chunks through state updates. For the conceptual overview see Rewriter.

Before shipping, review the Production checklist for safe streaming, progress, Accept/Reject/Undo flows, and cache freshness.

To warm the session before the hook first runs, call prepareRewriter from the core package on user intent. See the Session lifecycle guide.

Edit the source text, then click Rewrite. The rewrite streams in as the model produces it and never replaces your original text. Click Stop to cancel a run. Edits after a rewrite mark it stale until you run again or dismiss it.

import { useRewriter } from "@web-ai-sdk/rewriter/react";
export function Polish({ draft }: { draft: string }) {
const { status, output } = useRewriter({
input: draft,
tone: "more-formal",
});
if (status === "unavailable") return null;
return <p>{output}</p>;
}

The hook auto-runs whenever its meaningful inputs change. Empty / whitespace-only input keeps the hook in "idle" without invoking the model.

idle ───► loading ───► streaming ───► done
unavailable ◄─── (no API) │
dismiss()
  • idle: hook mounted, waiting for input to be non-empty.
  • loading: warming the Rewriter.create() session.
  • streaming: chunks arriving. output grows as they land.
  • done: final text in output. fromCache is true if the response came from the opt-in result cache.
  • unavailable: API missing. Render nothing.
import type { UseRewriterOptions, UseRewriterReturn, RewriterStatus } from "@web-ai-sdk/rewriter/react";
type RewriterStatus = "idle" | "loading" | "streaming" | "done" | "unavailable";
interface UseRewriterOptions extends Omit<RewriteOptions, "onUpdate" | "signal"> {
enabled?: boolean; // default: true
}
interface UseRewriterReturn {
status: RewriterStatus;
output: string | null;
error: Error | null;
fromCache: boolean;
dismiss(): void;
}
declare const useRewriter: (options: UseRewriterOptions) => UseRewriterReturn;

Source: packages/rewriter/src/react/index.ts.