ai inline edit.md
AI Inline Edit
Select text in a document and ask AI to rewrite it. The edit streams in place as a word-level diff you accept, discard or try again.
Installation
npx shadcn@latest add @kokonutui/ai-inline-edit
Props
| Prop | Type | Default | Description |
|---|---|---|---|
title? |
string |
"Billing launch update" |
|
paragraphs? |
string[] |
demo |
The document, one string per paragraph. Read once, on mount. |
rewrite? |
(text: string, instruction: string, attempt: number, signal: AbortSignal) => Rewrite |
demo |
Rewrites the selected text. Return the text, a Promise of it, or a stream of deltas (like the AI SDK's textStream); the bar shows it working until the text is complete, then writes the changes in. attempt counts up with each Try again, so you can ask for a different take. signal aborts when the person presses Stop. Throw and the bar shows the error's message. |
onChange? |
(paragraphs: string[]) => void |
Called with every paragraph once a rewrite is accepted. | |
className? |
string |
Extra classes for the root element. |
Rewrite
A rewrite: the text, a Promise of it, or a stream of text deltas.
type Rewrite = string | Promise<string> | AsyncIterable<string>;
Source
"use client";
/**
* @author: @kokonutui
* @description: AI Inline Edit
* @version: 1.1.0
* @date: 2026-10-21
* @license: MIT
* @website: https://kokonutui.com
* @github: https://github.com/kokonut-labs/kokonutui
*/
import {
ArrowUp,
ChevronLeft,
ChevronRight,
PenLine,
RotateCw,
Square,
} from "lucide-react";
import {
AnimatePresence,
MotionConfig,
motion,
useReducedMotion,
} from "motion/react";
import {
type ReactNode,
type RefObject,
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import { cn } from "@/lib/utils";
/** A rewrite: the text, a Promise of it, or a stream of text deltas. */
export type Rewrite = string | Promise<string> | AsyncIterable<string>;
export interface AIInlineEditProps {
title?: string;
/** The document, one string per paragraph. Read once, on mount. */
paragraphs?: string[];
/**
* Rewrites the selected text. Return the text, a Promise of it, or a stream
* of deltas (like the AI SDK's `textStream`); the bar shows it working until
* the text is complete, then writes the changes in. `attempt` counts up with
* each Try again, so you can ask for a different take. `signal` aborts when
* the person presses Stop. Throw and the bar shows the error's message.
*/
rewrite?: (
text: string,
instruction: string,
attempt: number,
signal: AbortSignal
) => Rewrite;
/** Called with every paragraph once a rewrite is accepted. */
onChange?: (paragraphs: string[]) => void;
className?: string;
}
...
## Usage
Pass your document as `paragraphs` and a `rewrite` that calls your model. It gets the selected text and the instruction, and returns the new text, a Promise of it, or a stream of deltas such as the AI SDK's `textStream`. The bar works until the text is complete, then writes the changes into the sentence. Stop aborts the `signal`, and a throw shows the error's message in the bar. `onChange` gets every paragraph once a rewrite is accepted.
```tsx
"use client";
import AIInlineEdit from "@/components/kokonutui/ai-inline-edit";
export function Editor({ doc, save }: { doc: string[]; save: (doc: string[]) => void }) {
return (
<AIInlineEdit
onChange={save}
paragraphs={doc}
rewrite={async function* (text, instruction, attempt, signal) {
const res = await fetch("/api/rewrite", {
method: "POST",
body: JSON.stringify({ text, instruction, attempt }),
signal,
});
if (!(res.ok && res.body)) {
throw new Error("Couldn't reach the model. Try again.");
}
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
while (true) {
const { done, value } = await reader.read();
if (done) return;
yield value;
}
}}
title="Billing launch update"
/>
);
}
// app/api/rewrite/route.ts
import { anthropic } from "@ai-sdk/anthropic";
import { streamText } from "ai";
export async function POST(req: Request) {
const { text, instruction, attempt } = await req.json();
const result = streamText({
model: anthropic("claude-sonnet-5-5"),
temperature: attempt > 0 ? 1 : 0.3,
system: "Rewrite the text as asked. Reply with the new text only.",
prompt: `${instruction}:\n\n${text}`,
abortSignal: req.signal,
});
return result.toTextStreamResponse();
}