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.

Live demo

Edit the source text; the rewrite streams in as the model produces it.

Usage

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.

State machine

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.

Reference

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.