File size: 6,883 Bytes
5710dd0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /**
* Pending interactions card (approvals / questions) of one session — fetched
* on demand over the public REST surface (`/api/v1/sessions/{id}/approvals`
* and `.../questions`): the engine's interaction kernel is a process-global
* singleton with no debug channel, and the session `interactions` push
* stream went away with `/api/v2/ws`, so the card refreshes only when Load
* is clicked.
*/
import { useState } from 'react';
import { useConnection } from '../connection';
import {
answerQuestion,
decideApproval,
dismissQuestion,
listPendingApprovals,
listPendingQuestions,
type QuestionAnswerWire,
type QuestionWire,
} from '../interactions/api';
import { ActionButton, Badge, ErrorLine, JsonView } from '../ui';
interface PendingInteraction {
readonly id: string;
/** Known kinds: 'approval' | 'question'. */
readonly kind: string;
readonly payload: Record<string, unknown>;
}
export function InteractionsCard({ sessionId }: { sessionId: string }) {
const { config, baseUrl } = useConnection();
const [pending, setPending] = useState<readonly PendingInteraction[]>([]);
const [error, setError] = useState<unknown>(null);
const api = { baseUrl, token: config.token, sessionId };
const reload = async () => {
try {
setError(null);
const [approvals, questions] = await Promise.all([
listPendingApprovals(api),
listPendingQuestions(api),
]);
setPending([
...approvals.map((p) => ({
id: p.approval_id,
kind: 'approval',
payload: {
toolName: p.tool_name,
action: p.action,
display: p.tool_input_display,
},
})),
...questions.map((p) => ({
id: p.question_id,
kind: 'question',
payload: p as unknown as Record<string, unknown>,
})),
]);
} catch (error) {
setError(error);
}
};
const decide = async (id: string, decision: 'approved' | 'rejected') => {
try {
await decideApproval(api, id, decision);
await reload();
} catch (error) {
setError(error);
}
};
const answer = async (id: string, answers: Readonly<Record<string, QuestionAnswerWire>>) => {
try {
await answerQuestion(api, id, answers, 'click');
await reload();
} catch (error) {
setError(error);
}
};
const dismiss = async (id: string) => {
try {
await dismissQuestion(api, id);
await reload();
} catch (error) {
setError(error);
}
};
return (
<div className="mb-3 rounded-lg border border-amber-900/50 bg-amber-950/20">
<div className="flex items-center justify-between border-b border-amber-900/40 px-3 py-2">
<span className="text-[12px] font-medium text-amber-200">
Pending interactions {pending.length > 0 ? `(${pending.length})` : ''}
</span>
<ActionButton onClick={() => void reload()}>Load</ActionButton>
</div>
<div className="px-3 py-2">
{error !== null ? (
<div className="mb-2">
<ErrorLine error={error} />
</div>
) : null}
{pending.length === 0 ? (
<div className="text-[11px] text-neutral-600 italic">
nothing pending (click Load to check)
</div>
) : (
pending.map((item) => (
<div
key={item.id}
className="mb-2 rounded border border-neutral-800 bg-neutral-950/60 p-2"
>
<div className="mb-1 flex items-center gap-2">
<Badge tone="amber">{item.kind}</Badge>
<span className="font-mono text-[10px] text-neutral-500">{item.id}</span>
</div>
{item.kind === 'approval' ? (
<>
<div className="mb-1.5 text-[11px] text-neutral-300">
<span className="text-neutral-500">tool </span>
{payloadField(item.payload, 'toolName', '?')}
<span className="text-neutral-500"> · </span>
{payloadField(item.payload, 'action', '')}
</div>
<JsonView data={item.payload['display'] ?? item.payload} />
<div className="mt-2 flex gap-1.5">
<ActionButton onClick={() => void decide(item.id, 'approved')}>
Approve
</ActionButton>
<ActionButton danger onClick={() => void decide(item.id, 'rejected')}>
Reject
</ActionButton>
</div>
</>
) : item.kind === 'question' ? (
<QuestionView
wire={item.payload as unknown as QuestionWire}
onAnswer={(answers) => void answer(item.id, answers)}
onDismiss={() => void dismiss(item.id)}
/>
) : (
<JsonView data={item.payload} />
)}
</div>
))
)}
</div>
</div>
);
}
function QuestionView({
wire,
onAnswer,
onDismiss,
}: {
wire: QuestionWire;
onAnswer: (answers: Readonly<Record<string, QuestionAnswerWire>>) => void;
onDismiss: () => void;
}) {
return (
<>
{wire.questions.map((q) => (
<div key={q.id} className="mb-1.5">
<div className="mb-1 text-[11px] text-neutral-300">{q.question}</div>
<div className="flex flex-wrap gap-1.5">
{q.options.map((opt) => (
<ActionButton
key={opt.id}
onClick={() => onAnswer({ [q.id]: { kind: 'single', option_id: opt.id } })}
>
{opt.label}
</ActionButton>
))}
{q.allow_other === false ? null : (
<ActionButton
onClick={() => {
const raw = window.prompt(q.question);
if (raw !== null) onAnswer({ [q.id]: { kind: 'other', text: raw } });
}}
>
Other…
</ActionButton>
)}
</div>
</div>
))}
{wire.questions.length === 0 ? <JsonView data={wire} /> : null}
<div className="mt-1.5">
<ActionButton danger onClick={onDismiss}>
Dismiss
</ActionButton>
</div>
</>
);
}
/**
* Render a wire payload field as display text: strings pass through,
* numbers/booleans are stringified, anything else (or missing) falls back —
* never "[object Object]".
*/
function payloadField(payload: Record<string, unknown>, key: string, fallback: string): string {
const value = payload[key];
if (typeof value === 'string') return value;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
return fallback;
}
|