Ask AI to rewrite, shorten or explain a selection, in place.
Due to the fact that the migration is taking longer than we originally anticipated, the release of the new billing page will be pushed out to next week.
Edit
Tone
Uses 1 credit
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { InlineAi, type InlineAiPreset, InlineAiStatus } from "./ui/uicontext/components/kit/InlineAi.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ page: { display: "grid", gap: space.md, inlineSize: "min(100%, 38rem)", paddingBlock: space.xl, paddingInline: space.xl, borderRadius: radius.lg, borderWidth: 1, borderStyle: "solid", borderColor: color.line, backgroundColor: color.panel, }, text: { margin: 0, fontSize: font.lg, lineHeight: 1.6, color: color.fg }, selected: { backgroundColor: `color-mix(in srgb, ${color.accent} 18%, transparent)`, borderRadius: radius.sm }, ask: { justifySelf: "start" },})const presets: ReadonlyArray<InlineAiPreset> = [ { id: "improve", label: "Improve writing", prompt: "Improve the writing", group: "Edit" }, { id: "shorter", label: "Make shorter", prompt: "Make it shorter", group: "Edit" }, { id: "friendly", label: "Make it friendlier", prompt: "Make the tone friendlier", group: "Tone" },]const original = "Due to the fact that the migration is taking longer than we originally anticipated, the release of the new billing page will be pushed out to next week."// what the stand-in model answers, by prompt; anything typed gets the "improve" answerconst answers: Record<string, string> = { improve: "The migration is taking longer than planned, so the new billing page ships next week.", shorter: "The new billing page ships next week.", friendly: "Quick update: the migration needs a little more time, so the new billing page is coming next week instead.",}const Message = UiSchema.TaggedEnum({ ClickedAskAi: {}, RequestedRewrite: { prompt: Schema.String, preset: Schema.NullOr(Schema.String) }, ReceivedWords: { run: Schema.Number }, StoppedRewrite: {}, KeptRewrite: { option: Schema.String }, DiscardedRewrite: {},})// stands in for the model: a first token after 500ms, then a few words every 50msconst WaitForWords = Command.make("Example.WaitForRewriteWords", { args: { run: Schema.Number, first: Schema.Boolean } }, ({ run, first }) => Effect.sleep(first ? "500 millis" : "50 millis").pipe(Effect.as(Message.ReceivedWords({ run }))),)export const RewriteSelection = Html.component({ name: "RewriteSelection", model: Schema.Struct({ text: Schema.String, open: Schema.Boolean, status: InlineAiStatus, prompt: Schema.String, answer: Schema.String, shown: Schema.Number, run: Schema.Number, }), message: Message,}).make({ init: () => ({ text: original, open: true, status: "idle", prompt: "", answer: "", shown: 0, run: 0 }), update: { ClickedAskAi: ($) => $.evolve({ open: true, status: "idle", answer: "", shown: 0 }), RequestedRewrite: ($, { prompt, preset }) => $.evolve({ status: "waiting", prompt, answer: answers[preset ?? "improve"] ?? answers["improve"]!, shown: 0, run: (n) => n + 1, }).run(WaitForWords.make({ run: $.model.run + 1, first: true })), ReceivedWords: ($, { run }) => { if (run !== $.model.run || ($.model.status !== "waiting" && $.model.status !== "streaming")) return $.model const total = $.model.answer.split(" ").length const shown = Math.min(total, $.model.shown + 2) return shown === total ? $.evolve({ status: "proposed", shown }) : $.evolve({ status: "streaming", shown }).run(WaitForWords.make({ run, first: false })) }, StoppedRewrite: ($) => $.evolve({ status: "proposed" }), // nothing lands in the document until it's kept KeptRewrite: ($, { option }) => { const answer = $.model.answer.split(" ").slice(0, $.model.shown).join(" ") return $.evolve({ text: option === "below" ? `${$.model.text} ${answer}` : answer, open: false, status: "kept" }) }, DiscardedRewrite: ($) => $.evolve({ open: false, status: "discarded" }), }, view: (h, model) => h.div.attrs(sx(styles.page)).children( h.p .attrs(sx(styles.text)) .children(h.span.attrs(sx(model.open && styles.selected)).children(model.text)), model.open ? InlineAi(h, { props: { id: "billing-update", agentId: "inline-ai-rewrite", status: model.status, result: model.answer.split(" ").slice(0, model.shown).join(" "), original: model.text, prompt: model.prompt, presets, keepOptions: [ { id: "replace", label: "Replace selection" }, { id: "below", label: "Insert below" }, ], usage: "Uses 1 credit", }, onEmit: { RequestedGenerate: ({ prompt, preset }) => Message.RequestedRewrite({ prompt, preset }), RequestedRetry: ({ prompt }) => Message.RequestedRewrite({ prompt, preset: null }), RequestedKeep: ({ option }) => Message.KeptRewrite({ option }), RequestedDiscard: () => Message.DiscardedRewrite(), RequestedStop: () => Message.StoppedRewrite(), }, }) : Button(h, { size: "sm", style: styles.ask }) .on("click", Message.ClickedAskAi()) .children(model.status === "kept" ? "Kept. Ask AI again" : "Discarded. Ask AI again"), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/inline-aiUsage
import { InlineAi, type InlineAiStatus } from "./ui/uicontext/components/kit/InlineAi.ts"const Message = UiSchema.TaggedEnum({ RequestedRewrite: { prompt: Schema.String }, KeptRewrite: { option: Schema.String }, DiscardedRewrite: {}, StoppedRewrite: {},})// your app runs the model and owns the document: pass the run's status and text, and write the// answer into the document only on Keepexport const RewriteSelection = ( h: Html.H<typeof Message.Type>, selection: string, run: { readonly status: InlineAiStatus; readonly result: string },) => InlineAi(h, { props: { id: "rewrite", original: selection, ...run }, onEmit: { RequestedGenerate: ({ prompt }) => Message.RequestedRewrite({ prompt }), RequestedRetry: ({ prompt }) => Message.RequestedRewrite({ prompt }), RequestedKeep: ({ option }) => Message.KeptRewrite({ option }), RequestedDiscard: () => Message.DiscardedRewrite(), RequestedStop: () => Message.StoppedRewrite(), }, })Examples
AI cells
A column AI fills in: generate, generating with Stop, generated, out of date, failed with retry, refused, and edited by a person, which runs skip.
| Company | Industry ✦ |
|---|---|
| Northwind Freight | ✦Logistics, freight forwarding |
| Halcyon Health | Uses 1 credit |
| Bluebird Bakery | Food and drinkInputs changed |
| Cobalt Labs | The website didn't load |
| Fjord Outfitters | Out of AI credits this month |
| Elm Studio | Design agency (set by Mira)Edited, runs skip it |
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { InlineAi, InlineAiStatus } from "./ui/uicontext/components/kit/InlineAi.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { inlineSize: "min(100%, 38rem)", overflowX: "auto" }, table: { borderCollapse: "collapse", inlineSize: "100%", fontSize: font.base }, th: { textAlign: "start", fontWeight: 500, fontSize: font.sm, color: color.faint, paddingBlock: space.xs, paddingInline: space.md, borderBottomWidth: 1, borderBottomStyle: "solid", borderBottomColor: color.line, }, td: { paddingBlock: space.xs, paddingInline: space.md, borderBottomWidth: 1, borderBottomStyle: "solid", borderBottomColor: color.line, maxInlineSize: 260, },})const Lead = Schema.Struct({ company: Schema.String, status: InlineAiStatus, result: Schema.String, notice: Schema.optional(Schema.String),})// each lead's "Industry" is filled in by AI from the company's websiteconst leads: ReadonlyArray<typeof Lead.Type> = [ { company: "Northwind Freight", status: "succeeded", result: "Logistics, freight forwarding" }, { company: "Halcyon Health", status: "idle", result: "" }, { company: "Bluebird Bakery", status: "stale", result: "Food and drink" }, { company: "Cobalt Labs", status: "failed", result: "", notice: "The website didn't load" }, { company: "Fjord Outfitters", status: "refused", result: "", notice: "Out of AI credits this month" }, { company: "Elm Studio", status: "edited", result: "Design agency (set by Mira)" },]const answers: Record<string, string> = { "Halcyon Health": "Healthcare, clinics", "Bluebird Bakery": "Food and drink, wholesale", "Cobalt Labs": "Software, developer tools", "Northwind Freight": "Logistics, freight forwarding",}const Message = UiSchema.TaggedEnum({ RequestedCell: { company: Schema.String }, StoppedCell: { company: Schema.String }, CompletedCell: { company: Schema.String },})// stands in for the model: an answer a second laterconst FillCell = Command.make("Example.FillCell", { args: { company: Schema.String } }, ({ company }) => Effect.sleep("1 second").pipe(Effect.as(Message.CompletedCell({ company }))),)const set = (leads: ReadonlyArray<typeof Lead.Type>, company: string, change: Partial<typeof Lead.Type>) => leads.map((l) => (l.company === company ? { ...l, ...change } : l))export const LeadIndustries = Html.component({ name: "LeadIndustries", model: Schema.Struct({ leads: Schema.Array(Lead) }), message: Message,}).make({ init: () => ({ leads }), update: { RequestedCell: ($, { company }) => $.evolve({ leads: (all) => set(all, company, { status: "waiting", notice: undefined }) }).run(FillCell.make({ company })), StoppedCell: ($, { company }) => $.evolve({ leads: (all) => set(all, company, { status: "idle", result: "" }) }), // a stopped run's late answer is dropped CompletedCell: ($, { company }) => $.model.leads.find((l) => l.company === company)?.status !== "waiting" ? $.model : $.evolve({ leads: (all) => set(all, company, { status: "succeeded", result: answers[company] ?? "Other" }) }), }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( h.table.attrs(sx(styles.table)).children( h.thead.children( h.tr.children( h.th.attrs({ scope: "col", ...sx(styles.th) }).children("Company"), h.th.attrs({ scope: "col", ...sx(styles.th) }).children("Industry ✦"), ), ), h.tbody.children( ...model.leads.map((lead) => h.tr.key(lead.company).children( h.td.attrs(sx(styles.td)).children(lead.company), h.td.attrs(sx(styles.td)).children( InlineAi(h, { key: lead.company, props: { id: lead.company, agentId: `inline-ai-cell:${lead.company}`, variant: "cell", label: `Industry for ${lead.company}`, status: lead.status, result: lead.result, prompt: "The company's industry, in a few words", generatedAt: "2026-12-01T10:00:00Z", timeZone: "UTC", usage: "Uses 1 credit", notice: lead.notice, }, onEmit: { RequestedGenerate: () => Message.RequestedCell({ company: lead.company }), RequestedRetry: () => Message.RequestedCell({ company: lead.company }), RequestedStop: () => Message.StoppedCell({ company: lead.company }), RequestedKeep: () => undefined, RequestedDiscard: () => undefined, }, }), ), ), ), ), ), ),})Failed and refused
A run that failed says what happened, with Try again. One the workspace doesn't allow says why, and offers no run.
The model took too long to answer. Try again, or ask for something shorter.
Uses 1 credit
AI is off for this workspace. An admin can turn it on in Settings.
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { InlineAi } from "./ui/uicontext/components/kit/InlineAi.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ column: { display: "grid", gap: space.lg, inlineSize: "min(100%, 38rem)" },})const silent = { RequestedGenerate: () => undefined, RequestedRetry: () => undefined, RequestedKeep: () => undefined, RequestedDiscard: () => undefined, RequestedStop: () => undefined,}export const InlineAiProblems = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.column)).children( InlineAi(h, { key: "failed", props: { id: "release-notes", agentId: "inline-ai-failed", status: "failed", result: "", prompt: "Write release notes from the merged pull requests", notice: "The model took too long to answer. Try again, or ask for something shorter.", usage: "Uses 1 credit", }, onEmit: silent, }), InlineAi(h, { key: "refused", props: { id: "contract-summary", agentId: "inline-ai-refused", status: "refused", result: "", notice: "AI is off for this workspace. An admin can turn it on in Settings.", }, onEmit: silent, }), )API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
agentId | string | — | The id agents see this by; inline-ai:<id> when left out |
idrequired | string | — | Identifies the run in the events it sends |
variant | "block" | "cell" | "block" | A prompt and answer under a selection, or a table cell that generates its value; "block" by default |
statusrequired | InlineAiStatus | — | Where the run stands: idle, waiting, streaming, proposed, succeeded, kept, discarded, failed or refused |
resultrequired | string | — | The answer so far, as Markdown |
original | string | — | The text being rewritten: the answer shows as a diff against it |
prompt | string | — | The prompt this answer came from |
presets | InlineAiPreset[] | — | Quick actions offered before anything is typed: "Improve writing", "Make shorter" |
keepOptions | KeepOption[] | — | How to keep it; the first is the main one (default: "Keep") |
placeholder | string | — | Hint text in the empty prompt; "Ask AI to edit or write…" when left out |
notice | string | — | For failed and refused: what went wrong and what to do, as a sentence |
usage | string | — | What a run costs, said before it runs: "Uses 1 credit" |
generatedAt | string | — | When it was generated, as an ISO timestamp: "Generated Nov 29, 10:00 AM" |
locale | string | — | Formats generatedAt; the reader's own when left out |
timeZone | string | — | The time zone for generatedAt; the reader's own when left out |
attribution | AttributionData | — | Who made it, for whom, and how |
can | Can(["run"]) | — | What the viewer may do; without "run" it shows the answer but can't run, keep or retry (a viewer, a read-only cell). Everything when left out. |
label | string | — | The cell's field, for its accessible name |
Events
| Event | Payload | Description |
|---|---|---|
RequestedGenerate | { id: string; prompt: string; preset: string | null } | A new run; on a proposal, the prompt refines it |
RequestedKeep | { id: string; option: string } | The person kept the answer, with one of keepOptions |
RequestedRetry | { id: string; prompt: string } | A new run with the same prompt |
RequestedDiscard | { id: string } | The person discarded the answer |
RequestedStop | { id: string } | The person stopped the run |
Accessibility
- The prompt is a labelled field with a Send button, and the quick actions are a named group of buttons.
- The answer is announced as it's written. Escape stops it while it runs, and discards the proposal after.
- Keeping text in the document is consequential, so an agent must name what it keeps.