AI Inline Edit - React Component | Kokonut UI
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.
Install with
npx shadcn@latest add https://kokonutui.com/r/ai-inline-edit.json
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.
"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();
}
In order to make exports faster, we have basically rewritten the way that reports are generated on the server.
Due to the fact that large files are now split up into parts, you might really notice that downloads start a lot sooner than before.
Installation
AI Inline Edit is a free, open-source React component in the Kokonut UI AI collection. Add it with the shadcn CLI, which copies the source into components/kokonutui/ai-inline-edit.tsx, so the code is yours to change.
npx shadcn@latest add @kokonutui/ai-inline-edit
Or add it by hand: install its packages, then copy the source from above into components/kokonutui/ai-inline-edit.tsx.
npm install lucide-react motion
Props
title
stringDefault: "Billing launch update"
paragraphs
string[]Default: demo data
The document, one string per paragraph. Read once, on mount.
rewrite
function:(text: string, instruction: string, attempt: number, signal: AbortSignal) => RewriteDefault: demo data
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>;
Dependencies
| Needs | Version | Comes from |
|---|---|---|
lucide-react |
^0.556.0 |
npm, installed by the CLI |
motion |
^12.23.25 |
npm, installed by the CLI |
cn from lib/utils.ts |
shadcn init | |
| React | 19 |
Your app |
| Tailwind CSS | 4 |
Your app |
No provider, font or global CSS to set up: the file works as soon as it lands.