Skip to content

Prompts ​

The cards for a chat's approvals and questions. The composer draws them for its chat and its delegated chats. The package exports the parts for a stack elsewhere, such as over a canvas of chats.

tsx
import { PromptView, usePromptSession } from '@adecore/agents-react/prompts';
tsx
import type { ChatApprovalItem, ChatQuestionItem } from '@adecore/agent-contracts';
import {
    orderPrompts,
    type PendingPrompt,
    isBlockingSubject,
    promptCreatedAt,
    promptIdOf,
    type PromptSubject,
    usePromptSession,
    PromptView
} from '@adecore/agents-react/prompts';
import { Button } from '@adecore/ui';
import { useState } from 'react';

const approval: ChatApprovalItem = {
    id: 'approval-1',
    requestId: 'approval-1',
    createdAt: 1,
    turnId: null,
    kind: 'approval',
    toolUseId: null,
    toolName: 'Bash',
    input: { command: 'bun run check && bun test', description: 'Run the checks' },
    description: 'Run the checks of the project before reporting back.',
    canAllowAlways: true,
    allowAlways: { label: 'Allow bun run for this session', description: 'Commands that start with bun run need no approval until the session ends.' },
    decision: 'pending'
};

const question: ChatQuestionItem = {
    id: 'question-1',
    requestId: 'question-1',
    createdAt: 2,
    turnId: null,
    kind: 'question',
    questions: [
        {
            id: 'callers',
            header: 'Callers',
            question: 'Which callers should handle a failed request?',
            multiSelect: true,
            choices: [
                { label: 'Barometer', description: 'Keeps its last reading' },
                { label: 'Thermometer', description: 'Keeps its last reading' },
                { label: 'Station', description: 'Lets the error escape today' }
            ]
        },
        { id: 'log', header: 'Logging', question: 'What should a retry write to the log?', multiSelect: false, choices: [] }
    ],
    answers: null,
    state: 'pending'
};

function pick(waiting: readonly PromptSubject[], id: string | null): PromptSubject | null {
    return waiting.find((subject) => promptIdOf(subject) === id) ?? orderPrompts(waiting, isBlockingSubject, promptCreatedAt)[0] ?? null;
}

export default function PromptsDemo() {
    const [pending, setPending] = useState<PendingPrompt[]>([approval, question]);
    const [answered, setAnswered] = useState<string[]>([]);
    const subjects = pending.map((item): PromptSubject => ({ kind: 'chat', nodeId: 'chat-demo', item }));
    const session = usePromptSession({ prompts: subjects, idOf: promptIdOf, pick, disabled: false });
    const active = session.active;

    if (!active) {
        return (
            <div className="flex flex-col items-center gap-2 text-sm text-text-muted">
                {answered.map((line) => (
                    <span key={line}>{line}</span>
                ))}
                <Button
                    size="sm"
                    variant="secondary"
                    onClick={() => {
                        setPending([approval, question]);
                        setAnswered([]);
                    }}
                >
                    Ask again
                </Button>
            </div>
        );
    }

    const id = promptIdOf(active);

    return (
        <div className="w-full max-w-xl" onPointerDownCapture={session.onPointerDownCapture} onClickCapture={session.onClickCapture}>
            <PromptView
                subject={active}
                draft={session.draftOf(id)}
                onDraft={(draft) => session.setDraft(id, draft)}
                onAction={(action) => {
                    void session.act(active, async () => {
                        await new Promise((resolve) => setTimeout(resolve, 300));
                        setAnswered((lines) => [...lines, JSON.stringify(action)]);
                        setPending((items) => items.filter((item) => active.kind !== 'chat' || item.id !== active.item.id));
                    });
                }}
                more={session.waiting.length - 1}
                hasDraft={false}
                denyReason
                disabled={false}
                sending={session.sending}
                error={session.errorOf(id)}
            />
        </div>
    );
}

What a person sees ​

An approval names the tool and shows the command or diff, with Deny and Allow. When the CLI proposes a rule to remember, a third button grants it for good. The keyboard never starts on Allow or that button. A denial can carry a reason when the CLI takes one.

A question shows single or multiple choices, or a field for a written answer. Several questions page before they are answered together. The answer joins multiple picks with commas. A person can dismiss a question marked async.

Blocking requests come first, oldest first, then optional questions. A new request never replaces the card being read. A refusal keeps the card, with the host's message under it.

A delegated request carries a badge with the subagent's task, and its answer goes to the child chat.

A stack of your own ​

ChatPromptFeed from @adecore/agents-react/chat feeds the same subjects to another surface. Construct it with a ChatScope, the main chat id and a callback, call start(), and dispose() when the surface closes. It attaches child chats through ChatClient.observe(chatId), which returns a release function.

usePromptSession holds a stack's state: the subject in front, a draft per request, sending and the last error. PromptView draws one subject. answerPrompt(subject, action, actions, host) sends a card's action, or hands it to the host's prompts.answer for a prompt of your app.

tsx
import { useChatActions } from '@adecore/agents-react/chat';
import { useChatHost } from '@adecore/agents-react';
import { orderPrompts, answerPrompt, isBlockingSubject, promptCreatedAt, promptIdOf, type PromptSubject } from '@adecore/agents-react/prompts';

const pick = (waiting: readonly PromptSubject[], id: string | null): PromptSubject | null =>
    waiting.find((subject) => promptIdOf(subject) === id) ?? orderPrompts(waiting, isBlockingSubject, promptCreatedAt)[0] ?? null;

function Stack({ subjects }: { subjects: PromptSubject[] }) {
    const actions = useChatActions();
    const host = useChatHost();
    const session = usePromptSession({ prompts: subjects, idOf: promptIdOf, pick, disabled: false });
    const active = session.active;

    if (!active) {
        return null;
    }

    const id = promptIdOf(active);

    return (
        <div onPointerDownCapture={session.onPointerDownCapture} onClickCapture={session.onClickCapture}>
            <PromptView
                subject={active}
                draft={session.draftOf(id)}
                onDraft={(draft) => session.setDraft(id, draft)}
                onAction={(action) => void session.act(active, () => answerPrompt(active, action, actions, host))}
                more={session.waiting.length - 1}
                hasDraft={false}
                denyReason
                disabled={false}
                sending={session.sending}
                error={session.errorOf(id)}
            />
        </div>
    );
}

A chat's PromptSubject is { kind: 'chat', nodeId, item }, where nodeId is the chat that gets the answer. An optional source: { label, provider? } names the delegated agent for the badge. A prompt of your app is { kind: 'host', nodeId, prompt }. Keep both capture handlers on the element around the card, so a press never answers a card that replaced the one it started on. act returns sent, failed or busy, sends one answer at a time and hides an answered card until the host settles it.

Keys ​

Arrows, Home and End move through the choices and Space picks; Enter on a single choice picks and answers. Mod+Enter is the card's main action, and Mod+Shift+Left and Right page through a stack (PROMPT_SHORTCUTS). A request that arrives while a person types puts the focus on its heading, not a button. focusPromptStart and useFocusAfterAnswer do the same for a stack of your own. The hook returns hold(), called before the front card changes, and release(), for a card that stays after all. pageKey(event, apple, typing) reads a keyboard event and returns -1 or 1 for a key that pages through the stack, or null.

Prompts of your own ​

The host's prompts adds prompts of your app beside a chat's, answered in the same place:

tsx
const CHAT_HOST: Partial<ChatHost> = {
    prompts: {
        useExtra: (scopeId, chatId) => usePermissionRequests(scopeId, chatId),
        render: (prompt, props) => <PermissionCard prompt={prompt} {...props} />,
        answer: (prompt, action) => permissions.answer(prompt.id, action)
    }
};

A HostPrompt is { id, createdAt, blocking, asks, data }: a stable id, whether it blocks, approval or question for its mark, and your own data. Return the same objects from useExtra while nothing changed. PromptCard, PromptPrimary and ApprovalActions build a matching card, and a choose action answers with a choice of your own.

Parts ​

PromptView is made of PromptCard (the frame with heading, meta, notice and error), the approval and question bodies, and the action rows. PROMPT_SURFACE is the card's surface class, for a matching frame. For a stack of your own, emptyPromptDraft() returns a blank draft, and orderPrompts(prompts, blocking, createdAt) sorts blocking prompts first, each group oldest first.

The types of those parts:

  • PromptViewProps is what a card gets to draw, the chat's and the app's alike: the subject, its draft, onDraft, onAction and the rest of the props in the example above.
  • PromptAction is what a card asks for: approve with a decision and an optional message, answer with the answers, dismiss, or choose for a terminal's permission request. The draft of one question holds the picked choices, the written text and whether it is a custom answer.
  • usePromptSession returns the session of a chat's prompts. PendingPrompt is a waiting approval or question item, and ChatPromptClients the requests a chat's own prompts are answered with: approve, answer and dismiss.
  • A PromptCard is marked approval, question or waiting.
  • approvalButtons returns an ApprovalButtonSpec per button of a chat's permission request, and ApprovalActions draws them.

PROMPT_SAMPLES from @adecore/agents-react/testing holds prompts in every state, labeled, for a preview or a test. They never enter the chat store or the transport.

Licenses and third-party notices are listed in each package.