ComponentsAI Command

AI Command

A ⌘K palette that doubles as “Ask AI”, plus a Notion-style inline toolbar that rewrites selected text with a streamed word diff.

Preview

Command palette

Try “invite”, or type a question and press Tab.

Inline Ask AI

Installation

  1. 1

    Run the following command

    npx shadcn@latest add https://localhostui.com/r/ai-command.json
    

Also copy ai-command.tsx, inline-ai.tsx and word-diff.ts from the same folder. The answer view reuses the markdown renderer from Streaming Message, and the diff uses the success token (see Agent Plan).

Command palette

Typing filters commands by group. When nothing matches — or you press Tab — the first row becomes Ask AI: “your question”. Choosing it switches the palette to answer mode: the query morphs into the header and the answer streams below, with Copy, Insert and Ask follow-up in the footer. Backspace on the empty follow-up input returns to commands.

Connecting onAsk to an API route

// app/api/ask/route.ts
export async function POST(req: Request) {
  const { query } = await req.json();
  const stream = await model.stream(query); // any SDK that yields text
  return new Response(stream.toReadableStream());
}
async function* streamAnswer(query: string) {
  const res = await fetch("/api/ask", { method: "POST", body: JSON.stringify({ query }) });
  const reader = res.body!.pipeThrough(new TextDecoderStream()).getReader();
  for (;;) {
    const { value, done } = await reader.read();
    if (done) return;
    yield value;
  }
}

Inline Ask AI

Select text in the target and a small Ask AI pill pops up above the selection (or below, near the top of the screen). It expands into a menu of rewrite actions plus a custom instruction field. The result panel shows a live word diff — removed words struck through, added words underlined. Accept replaces the selection with document.execCommand("insertText") so Ctrl+Z still works, and the new text flashes briefly in place.

Using it with Tiptap

Tiptap's DOM is a contenteditable, so InlineAI can position itself from the native selection. Writing into it directly would bypass ProseMirror, so pass onReplace — it replaces the built-in insertion — and apply the change through the editor's command chain:

function TiptapInlineAI({ editor }: { editor: Editor }) {
  const targetRef = useMemo(() => ({ current: editor.view.dom as HTMLElement }), [editor]);
  return (
    <InlineAI
      targetRef={targetRef}
      onRun={(action, text, instruction) => streamRewrite(action, text, instruction)}
      onReplace={(result) => {
        const { from, to } = editor.state.selection;
        editor.chain().focus().insertContentAt({ from, to }, result).run();
      }}
    />
  );
}

ProseMirror records the change in its own history, so undo keeps working.

Props

AICommandPalette

PropTypeDefaultDescription
commands*PaletteCommand[]—{ id, label, icon?, shortcut?, group?, keywords?, onSelect }. Fuzzy-filtered with cmdk's scorer.
onAsk(query: string) => AsyncIterable<string>—Streams an answer. Enables the “Ask AI” row.
onInsert(answer: string) => void—Adds an Insert action to the answer footer.
open / defaultOpen / onOpenChangeboolean / boolean / (open) => void—Open state, controlled or not.
placeholderstring"Search commands or ask AI…"
hotkeystring | false"k"Letter used with ⌘ / Ctrl to toggle.
recentLimitnumber3Recent commands shown when the input is empty.

InlineAI

PropTypeDefaultDescription
targetRef*RefObject<HTMLTextAreaElement | HTMLElement>—The textarea or contenteditable to watch.
onRun*(action, selectedText, instruction?) => AsyncIterable<string>—Streams the rewritten text.
actions("improve" | "shorter" | "longer" | "fix-grammar" | "tone" | "translate" | "custom")[]allMenu items, in order. custom adds an instruction input.
onAccept(result, original) => void—Called after the replacement is inserted.
onReplace(result, original) => void—Take over insertion (Tiptap, ProseMirror, Lexical…). Skips the built-in execCommand replacement.

Keyboard

KeyAction
⌘ / Ctrl + KOpen or close the palette
↑ / ↓, EnterMove and select
TabPromote “Ask AI” to the top
Backspace (empty, answer mode)Back to commands
EscClose
⌘ / Ctrl + JOpen inline AI for the current selection
Enter / EscAccept / discard the inline suggestion

The kbd chips in the palette press down while you actually hold the key.

Accessibility

  • The palette is built on cmdk and Radix Dialog: combobox/listbox semantics, aria-activedescendant, focus trapped while open.
  • Inline actions are a keyboard-navigable menu; “Suggestion ready” is announced when the rewrite completes.
  • With reduced motion the highlight and query morph become simple fades.