Prodular UI

AI action

Pro

The shared state model for AI actions: start, stream, stop, retry, accept, reject and undo, with any provider. useAiAction powers the field actions and form fill, and builds your own AI features in a few lines.

npx prodular-ui add ai-action

Part of Prodular UI Pro. Preview and read the docs freely, install with a license. See pricing

Your own AI feature

A summary written by a simulated model: it streams, can be stopped, saved or discarded, and the save can be undone.

October summaryNot written yet

Anatomy

const ai = useAiAction({ run: summarize, onAccept: (text) => save(text) })
<Button onClick={() => ai.start({ period })}>Write summary</Button><AiActionStatusLine status={ai.status} error={ai.error} onCancel={ai.cancel} onRetry={ai.retry} />

Guidance

Use it when

  • Any button that asks a model for text and lets people review it before it is used.
  • AiActionStatusLine under the feature for the working and error states.
  • readAiResult when you only need to read a stream or a promise to the end.

Avoid

  • Chat. Use AI chat, which keeps a conversation.
  • Applying results without review for anything people will send or publish.

Rules for AI agents

Published in llms-full.txt, the registry and the MCP server, so coding agents follow them when they build with this component.

  • Call useAiAction({ run, onAccept }). run(input, signal) returns a string, a promise or an async iterable of text chunks; honor the signal.
  • In onAccept apply the result and return a function that reverts it; that is what undo calls.
  • Render output while status is streaming or proposed, and offer accept and reject when proposed.
  • busy is true while running or streaming. Disable the trigger then, and show AiActionStatusLine with onCancel.
  • Errors thrown by run show their message; give people a next step in it.

API

useAiAction

PropTypeDefaultDescription
run(input, signal) => AiResultCalls the model.
onAccept(output, input) => void | (() => void)Applies the result; return a revert function for undo.
onReject(input) => voidCalled on reject and cancel.
returns{ status, output, error, busy, start, cancel, retry, accept, reject, undo, canUndo, clearUndo }State and controls.

AiActionStatusLine

PropTypeDefaultDescription
status / error / label / onCancel / onRetryAiActionStatus / string / ReactNode / () => voidWorking with Stop, or the error with Try again.

readAiResult

PropTypeDefaultDescription
(result, signal, onChunk?)Promise<string | null>Reads any result to the end; null when aborted.

Installation

The Prodular UI CLI adds the source to components/ui with everything it depends on. In a project that already uses shadcn/ui, the shadcn CLI installs it from the Prodular registry too. See Installation for the first setup.

npx prodular-ui add ai-action# or, with the registry in components.jsonnpx shadcn@latest add @prodular/ai-action