// An open question of the session, as a card above the input: an external // agent asking "may I run this?" (docs/external-agents.md). One button per // option; the turn waits until one is clicked or the time runs out (then // the server picks the safest option). The builder keeps its own panel. // A question that does not wait (an agent's ask_user in a chat with you, // Codex's own question) has its own card below: the turn is over, a click // sends the choice as your next message, and writing anything else in // the chat answers it too. import { useState } from 'react'; import { MessageCircleQuestion, ShieldQuestion } from 'lucide-react'; import { api, type SessionQuestion } from '../lib/api'; import { answersOnClick, buildAnswer, questionParts } from '../lib/question'; import { AssistantMarkdown } from './AssistantMarkdown'; /** What the person decides on, in full: the text to approve, the drafts * to pick from. On the card itself, because the agent's reply around it * may not be in view (2026-11-06: posts approved whose text was never * shown). Scrolls on its own when long. */ /** In a small chat window (520x460 by default) a card with details * pushed its options and the input below the window's edge: it shrinks * to the room left and scrolls as a whole instead. The transcript above * is flex 1 and gives way first. */ const FIT_WINDOW: React.CSSProperties = { flexShrink: 1, minHeight: 120, overflowY: 'auto', overscrollBehavior: 'contain' }; function QuestionDetails({ text }: { text: string }) { return (
); } function remaining(expiresAt: number): string { const s = Math.max(0, Math.round((expiresAt - Date.now()) / 1000)); return s >= 60 ? `${Math.round(s / 60)} min` : `${s} s`; } const KINDS = new Set(['allow_once', 'allow_always', 'reject_once', 'reject_always']); /** Tone of a button: an external agent's option carries its kind as * description; an agent's ask_user option carries an explanation. */ function tone(description: string | undefined): 'allow' | 'reject' | 'plain' { if (!description || !KINDS.has(description)) return 'plain'; return description.startsWith('allow') ? 'allow' : 'reject'; } export function QuestionCard({ agent, session, question, onAnswered }: { agent: string; session: string; question: SessionQuestion; onAnswered: () => void }) { if (question.async) return ; return ; } function WaitingQuestionCard({ agent, session, question, onAnswered }: { agent: string; session: string; question: SessionQuestion; onAnswered: () => void }) { const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // A question with several answers allowed: click to pick, then send. const [picked, setPicked] = useState([]); const answer = async (labels: string[]) => { setBusy(true); setError(null); try { await api.answerSessionQuestion(agent, session, question.id, labels); onAnswered(); } catch (err) { setError((err as Error).message); } finally { setBusy(false); } }; return (
{question.header ?? 'Question'} waits {remaining(question.expiresAt)}
{question.question}
{question.details && }
{question.options.map((o) => { const t = tone(o.description); const on = picked.includes(o.label); const note = o.description && !KINDS.has(o.description) ? o.description : null; return ( ); })}
{question.multiple && ( )} {error &&
{error}
}
); } const optionStyle = (on: boolean, busy: boolean): React.CSSProperties => ({ padding: '5px 12px', borderRadius: 6, fontSize: 12, textAlign: 'left', display: 'flex', flexDirection: 'column', gap: 1, cursor: busy ? 'default' : 'pointer', opacity: busy ? 0.6 : 1, border: `1px solid ${on ? 'var(--accent)' : 'var(--line-2)'}`, background: on ? 'color-mix(in srgb, var(--accent) 16%, transparent)' : 'var(--bg-3)', color: 'var(--text-0)', }); /** A question that does not wait: one or several, each with options * (or a line for your own words). The answer goes out as your next * message. */ function AsyncQuestionCard({ agent, session, question, onAnswered }: { agent: string; session: string; question: SessionQuestion; onAnswered: () => void }) { const parts = questionParts(question); const oneClick = answersOnClick(question); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [picks, setPicks] = useState(() => parts.map(() => [])); const [texts, setTexts] = useState(() => parts.map(() => '')); const send = async (body: NonNullable>) => { setBusy(true); setError(null); try { await api.answerSessionQuestion(agent, session, question.id, body); onAnswered(); } catch (err) { setError((err as Error).message); } finally { setBusy(false); } }; const pick = (i: number, label: string, multiple: boolean) => { if (oneClick) return void send({ answers: [label] }); setPicks((all) => all.map((p, j) => (j !== i ? p : multiple ? (p.includes(label) ? p.filter((x) => x !== label) : [...p, label]) : p[0] === label ? [] : [label])), ); }; const body = buildAnswer(question, picks, texts); return (
{question.header ?? (parts.length > 1 ? `${parts.length} questions` : 'Question')} answer any time
{question.details && } {parts.map((p, i) => (
{p.question}
{p.options.length > 0 ? (
{p.options.map((o) => { const on = (picks[i] ?? []).includes(o.label); return ( ); })}
) : ( setTexts((all) => all.map((t, j) => (j === i ? e.target.value : t)))} onKeyDown={(e) => { if (e.key === 'Enter' && body) void send(body); }} style={{ padding: '5px 8px', borderRadius: 6, fontSize: 12, border: '1px solid var(--line-2)', background: 'var(--bg-1)', color: 'var(--text-0)' }} /> )}
))}
{!oneClick && ( )} or write your own answer below
{error &&
{error}
}
); }