An assistant wraps an invoice fetch in a retry with backoff: an import, a doc comment and the new body land one after another. Keep or undo each change, or all of them from the header.
registry/demos/diff-apply-demo.tsx
"use client";
import { DiffApply } from "@/components/ui/diff-apply";
const before = `
import type { Invoice } from "./types";
const API = "https://api.northwind.dev/v2";
export async function fetchInvoice(id: string): Promise<Invoice> {
const res = await fetch(\`\${API}/invoices/\${id}\`);
if (!res.ok) {
throw new Error("Request failed");
}
return res.json();
}
export function formatTotal(invoice: Invoice) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: invoice.currency,
}).format(invoice.total / 100);
}
`;
const after = `
import type { Invoice } from "./types";
import { retry } from "@/lib/retry";
const API = "https://api.northwind.dev/v2";
/** Retries 5xx responses up to 3 times, backing off from 250ms. */
export async function fetchInvoice(id: string): Promise<Invoice> {
return retry(
async () => {
const res = await fetch(\`\${API}/invoices/\${id}\`);
if (res.status >= 500) throw new Error(\`Northwind \${res.status}\`);
if (!res.ok) throw new Error(\`Invoice \${id} not found\`);
return res.json();
},
{ attempts: 3, backoff: (n) => 250 * 2 ** n },
);
}
export function formatTotal(invoice: Invoice) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: invoice.currency,
}).format(invoice.total / 100);
}
`;
export function DiffApplyDemo() {
return (
<div className="flex w-full max-w-2xl flex-col gap-3">
<div className="flex items-center gap-2.5 px-1 text-[13px] text-zinc-600 dark:text-zinc-400">
<span className="flex size-6 shrink-0 items-center justify-center rounded-full bg-zinc-900 text-white dark:bg-white dark:text-zinc-900">
<svg aria-hidden viewBox="0 0 16 16" className="size-3.5" fill="currentColor">
<path d="M8 1.5c.3 2.9 1.6 4.2 4.5 4.5-2.9.3-4.2 1.6-4.5 4.5-.3-2.9-1.6-4.2-4.5-4.5 2.9-.3 4.2-1.6 4.5-4.5Zm4.25 7.25c.15 1.45.8 2.1 2.25 2.25-1.45.15-2.1.8-2.25 2.25-.15-1.45-.8-2.1-2.25-2.25 1.45-.15 2.1-.8 2.25-2.25Z" />
</svg>
</span>
<p className="min-w-0 truncate">
<span className="font-medium text-zinc-900 dark:text-zinc-100">Retry invoice fetches</span> when Northwind
returns a 5xx, with backoff
</p>
</div>
<DiffApply filename="billing/fetch-invoice.ts" before={before} after={after} />
</div>
);
}
Live preview, examples and props: https://keyframeui.com/components/diff-apply#diff-apply-demo
