okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
4.0 KiB63 linesraw
1/** The card that asks the person to approve or deny one action of the assistant, and the dock that keeps it in view. */
2import { useMemo } from 'react';
3import { tr } from '../lib/i18n';
4import { Btn, Icon } from './ui';
5import { askQuestion, askSub, askIcon } from '../lib/ask';
6import { resolveTool, useStore } from '../lib/store';
7import { pendingApprovals } from '../lib/approvals';
8import { QuestionCard } from './QuestionCard';
9import type { AgentAsk } from '../lib/agent-ask';
10import type { ToolCall } from '../lib/types';
11
12const NO_ASKS: AgentAsk[] = [];
13export const SCOPE: Record<ToolCall['scope'], {
14    label: string;
15    tone: 'plain' | 'info' | 'warn';
16}> = {
17    read: { label: 'Reads', tone: 'plain' },
18    write: { label: 'Writes', tone: 'warn' },
19    shell: { label: 'Runs a command', tone: 'warn' },
20    connected: { label: 'Connected tool', tone: 'info' },
21};
22
23/** The one place the Approve and Deny buttons live. Chat shows it in the dock above the box, not in the middle of a long reply (#4725). */
24export function ApprovalCard({ messageId, tool: t }: {
25    messageId: string;
26    tool: ToolCall;
27}) {
28    const scope = SCOPE[t.scope];
29    if (t.ask)
30        return <section className="askcard" data-state={t.state} aria-label={tr(askQuestion(t.ask))}>
31      <div className="askcard__top"><span className="askcard__icon"><Icon name={askIcon(t.ask)} size={18}/></span>
32        <div className="askcard__text"><p className="askcard__q">{askQuestion(t.ask)}</p>{askSub(t.ask) ? <p className="askcard__why" title={askSub(t.ask)}>{askSub(t.ask)}</p> : null}</div>
33        <div className="askcard__acts"><Btn size="sm" onClick={() => resolveTool(messageId, t.id, false)}>{tr("Deny")}</Btn><Btn variant="primary" size="sm" onClick={() => resolveTool(messageId, t.id, true)}>{tr("Approve")}</Btn></div></div>
34      <details className="askcard__details"><summary><Icon name="chevron" size={11}/>{tr("Details")}</summary><pre>{t.preview}</pre></details>
35    </section>;
36    return <section className="toolcard toolcard--compact" data-state={t.state} aria-label={t.name}>
37    <details className="toolcard__disclosure">
38      <summary><Icon name={t.scope === 'shell' ? 'bolt' : 'tool'} size={17}/><span className="toolcard__title">{tr(t.verb ?? t.name)}</span><span className="toolcard__scope xs muted">{tr(scope.label)}</span><Icon name="chevron" className="toolcard__chevron" size={15}/></summary>
39      <div className="toolcard__body"><code className="toolcard__preview">{t.preview}</code><p className="small muted">{tr(t.effect)}</p>{t.result ? <p className="small">{t.result}</p> : null}</div>
40    </details>
41    {t.target ? <code className="toolcard__what" title={t.target}>{t.target}</code> : null}
42    <div className="toolcard__quick-actions"><span className="xs muted">{tr("Needs your approval")}</span><Btn size="sm" onClick={() => resolveTool(messageId, t.id, false)}>{tr("Deny")}</Btn><Btn variant="primary" size="sm" icon="check" onClick={() => resolveTool(messageId, t.id, true)}>{tr("Approve")}</Btn></div>
43  </section>;
44}
45
46/**
47 * Keeps an approval where the person is looking. A reply can run to many screens; the card used to sit above its text, so a
48 * person reading the end of it never saw that the assistant had stopped to ask. The dock sits directly above the message box and
49 * does not scroll with the conversation. Where the card was, the conversation says that it is waiting.
50 */
51export function ApprovalDock({ sessionId }: {
52    sessionId: string | undefined;
53}) {
54    const session = useStore(s => s.sessions.find(x => x.id === sessionId));
55    const items = useMemo(() => pendingApprovals(session), [session]);
56    const asks = useStore(s => (sessionId ? s.agentAsks[sessionId] : undefined)) ?? NO_ASKS;
57    if (!items.length && !asks.length)
58        return null;
59    return <section className="approvaldock" role="group" aria-label={tr("Waiting for your answer")} data-count={items.length}>
60      {asks.map(a => <QuestionCard key={a.callId} sessionId={sessionId!} ask={a}/>)}
61      {items.map(({ messageId, tool }) => <ApprovalCard key={tool.id} messageId={messageId} tool={tool}/>)}
62    </section>;
63}