diff --git a/src/ui/App.tsx b/src/ui/App.tsx index 5bef2d5..539e627 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -1,13 +1,6 @@ import type { KeyEvent, MouseEvent, ScrollBoxRenderable, Selection, TextareaRenderable } from "@opentui/core"; import { useKeyboard, useRenderer, useTerminalDimensions } from "@opentui/react"; import { resolveComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch"; -import { - buildPendingUserInputAnswers, - resolvePendingUserInputAnswer, - setPendingUserInputCustomAnswer, - togglePendingUserInputOptionSelection, - type PendingUserInputDraftAnswer, -} from "@t3tools/client-runtime/state/pending-user-input"; import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; import { type DiscoveredLocalServer, type OrchestrationV2ThreadShell, type ProviderApprovalDecision, type ScheduledTask, type ThreadId } from "@t3tools/contracts"; import { homedir } from "node:os"; @@ -33,6 +26,7 @@ import { DiffView, type DiffState } from "./DiffView.tsx"; import { DraftView } from "./DraftView.tsx"; import { useDraft } from "./useDraft.ts"; import { useModelPicker } from "./useModelPicker.ts"; +import { useQuestion } from "./useQuestion.ts"; import { useThreadMenu } from "./useThreadMenu.ts"; import { editInEditor } from "./editor.ts"; import { Palette } from "./Palette.tsx"; @@ -98,11 +92,6 @@ const COMPOSER_KEYS = { /** The composer's key while a new thread is being drafted. */ const DRAFT_KEY = "draft"; -interface QuestionDraft { - readonly index: number; - readonly answers: Record; -} - export function App(props: { readonly session: T3Session; readonly config: Tui3Config; readonly attachThreadId?: ThreadId }) { const { session, attachThreadId } = props; const attached = attachThreadId !== undefined; @@ -147,7 +136,6 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C const [pathResults, setPathResults] = useState<{ key: string; entries: readonly ProjectEntry[] } | null>(null); /** Mentions per composer (threads, pull requests), sent as the message's context. */ const mentions = useRef(new Map>()); - const [questionDrafts, setQuestionDrafts] = useState>(new Map()); const [now, setNow] = useState(Date.now()); const scrollRef = useRef(null); const composerRef = useRef(null); @@ -268,7 +256,6 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C const openFind = () => { setDiff(null); setSettingsIndex(null); setMode("find"); }; const pendingApproval = current?.pending.approvals[0]; const pendingInput = current?.pending.userInputs[0]; - const questionDraft = pendingInput ? (questionDrafts.get(pendingInput.requestId) ?? { index: 0, answers: {} }) : undefined; const working = current?.isWorking ?? false; // A thread opens at its latest message: once its first snapshot renders, jump to the bottom. @@ -399,6 +386,7 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C run, say, }); + const questions = useQuestion({ session, threadId, request: pendingInput, run, say }); const queue = useQueue({ session, threadId: draft ? undefined : threadId, @@ -643,35 +631,6 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C setComposerEmpty(true); }; - /** Moves the question flow on: the next question, or the answers once all are in. */ - const advanceQuestion = (draft: QuestionDraft) => { - if (!threadId || !pendingInput) return; - const question = pendingInput.questions[draft.index]; - if (!question || resolvePendingUserInputAnswer(question, draft.answers[question.id]) === null) { - setQuestionDrafts((map) => new Map(map).set(pendingInput.requestId, draft)); - return say("Pick an option or type an answer.", true); - } - if (draft.index + 1 < pendingInput.questions.length) { - setQuestionDrafts((map) => new Map(map).set(pendingInput.requestId, { ...draft, index: draft.index + 1 })); - return; - } - const answers = buildPendingUserInputAnswers(pendingInput.questions, draft.answers); - if (!answers) return say("Some questions are unanswered.", true); - setQuestionDrafts((map) => new Map(map).set(pendingInput.requestId, draft)); - run("Answered", () => session.answer(threadId, pendingInput.requestId, answers)); - }; - - const pickOption = (optionIndex: number) => { - if (!pendingInput || !questionDraft) return; - const question = pendingInput.questions[questionDraft.index]; - const option = question?.options[optionIndex]; - if (!question || !option) return; - const draft = { ...questionDraft, answers: { ...questionDraft.answers, [question.id]: togglePendingUserInputOptionSelection(question, questionDraft.answers[question.id], option.value ?? option.label) } }; - setQuestionDrafts((map) => new Map(map).set(pendingInput.requestId, draft)); - // Single choice moves on by itself, after a beat to show the pick. - if (!question.multiSelect) setTimeout(() => advanceQuestion(draft), 200); - }; - /** Enter in the composer; `alternate` is alt+enter, which flips steer and queue. */ const submit = (alternate = false) => { const composer = composerRef.current; @@ -693,13 +652,11 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C return newThread.launch(text || ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, context, taken); } if (!threadId) return; - if (pendingInput && questionDraft) { + if (questions.request) { if (attachedFiles.length > 0) return say("Attachments can't go with a question answer here; remove them first from the palette.", true); - const question = pendingInput.questions[questionDraft.index]; - if (!question) return; - const answer = text ? setPendingUserInputCustomAnswer(question, questionDraft.answers[question.id], text) : questionDraft.answers[question.id]; + if (!questions.question) return; clearComposer(); - return advanceQuestion({ ...questionDraft, answers: { ...questionDraft.answers, ...(answer ? { [question.id]: answer } : {}) } }); + return questions.answer(text); } if ((queue.editing || attachedFiles.length === 0) && queue.submit(text)) return; if (!text && attachedFiles.length === 0) return; @@ -860,7 +817,6 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C const toInput = () => { if (hasComposer) setMode("input"); }; - const dismissQuestion = threadId && pendingInput?.dismissible ? () => run("Dismissed", () => session.dismissQuestion(threadId, pendingInput.requestId)) : null; const leaveDetails = () => { setDetailsSelected(null); setMode("nav"); @@ -887,9 +843,6 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C !attached && previousThreadKey, ); - const digitKeys = pendingInput ? [1, 2, 3, 4, 5, 6, 7, 8, 9].map((digit): Binding => ({ keys: [String(digit)], run: () => pickOption(digit - 1) })) : []; - const pickHint: Binding = { keys: [], label: "1-9", hint: "pick" }; - // Navigation: single keys move, open and act on the thread. const scrollDiffDown: Binding = { keys: ["j", "↓"], label: "j/k", run: () => scrollBy(1), hint: "scroll" }; const closeDiff: Binding = { keys: ["esc", "q"], run: () => setDiff(null), hint: "close diff" }; @@ -908,9 +861,9 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C { keys: ["n"], run: () => respond("decline"), hint: "decline", command: { id: "decline", title: "Decline request" } }, ], !diff && secret.bindings, - pendingInput && pickHint, - digitKeys, - dismissQuestion && { keys: ["x"], run: dismissQuestion, hint: "dismiss", command: { id: "dismiss", title: "Dismiss question" } }, + questions.keys?.pick, + questions.keys?.digits, + questions.keys?.dismiss, queue.keys.nav, library.commands(threadId).map((command) => fromCommand(command)), workspace.commands.map((command) => fromCommand(command)), @@ -980,8 +933,8 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C const sendKey = config.sendShortcut === "enter" ? "enter" : "ctrl+enter"; const inputKeys = keys( pendingApproval && { keys: [], label: "approval waiting:", hint: "esc, then y/n" }, - pendingInput && pickHint, - pendingInput && composerEmpty && digitKeys, + questions.keys?.pick, + composerEmpty && questions.keys?.digits, { keys: [], label: sendKey, hint: working ? config.followUpBehavior : "send" }, working && { keys: [], label: "alt+enter", hint: config.followUpBehavior === "steer" ? "queue" : "steer" }, { keys: ["alt+enter"], run: () => submit(true) }, @@ -1352,13 +1305,13 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C {find.open ? setMode("nav")} /> : null} {mode !== "confirm" && secret.request ? : null} {queue.queued.length > 0 ? : null} - {mode !== "confirm" && pendingInput && questionDraft ? advanceQuestion(questionDraft)} - onDismiss={threadId && pendingInput.dismissible ? () => run("Dismissed", () => session.dismissQuestion(threadId, pendingInput.requestId)) : undefined} + {mode !== "confirm" && questions.request && questions.draft ? : null} {mode === "confirm" && confirmation ? ( ; +} + +/** + * The web composer's question flow: one question at a time, picked by digit or click or typed + * in the composer, answered together once all are in. + */ +export function useQuestion(deps: { + readonly session: T3Session; + readonly threadId: ThreadId | undefined; + /** The question waiting in the open thread. */ + readonly request: ThreadPendingUserInput | undefined; + readonly run: (label: string, action: () => Promise) => void; + readonly say: (text: string, bad?: boolean) => void; +}) { + const { session, threadId, request, run, say } = deps; + const [drafts, setDrafts] = useState>(new Map()); + const draft = request ? (drafts.get(request.requestId) ?? { index: 0, answers: {} }) : undefined; + const question = request && draft ? request.questions[draft.index] : undefined; + const save = (requestId: string, next: QuestionDraft) => setDrafts((map) => new Map(map).set(requestId, next)); + + /** Moves on: the next question, or the answers once all are in. */ + const advance = (next: QuestionDraft) => { + if (!threadId || !request) return; + const asked = request.questions[next.index]; + if (!asked || resolvePendingUserInputAnswer(asked, next.answers[asked.id]) === null) { + save(request.requestId, next); + return say("Pick an option or type an answer.", true); + } + if (next.index + 1 < request.questions.length) return save(request.requestId, { ...next, index: next.index + 1 }); + const answers = buildPendingUserInputAnswers(request.questions, next.answers); + if (!answers) return say("Some questions are unanswered.", true); + save(request.requestId, next); + run("Answered", () => session.answer(threadId, request.requestId, answers)); + }; + + const pick = (optionIndex: number) => { + if (!request || !draft || !question) return; + const option = question.options[optionIndex]; + if (!option) return; + const next = { ...draft, answers: { ...draft.answers, [question.id]: togglePendingUserInputOptionSelection(question, draft.answers[question.id], option.value ?? option.label) } }; + save(request.requestId, next); + // Single choice moves on by itself, after a beat to show the pick. + if (!question.multiSelect) setTimeout(() => advance(next), 200); + }; + + /** A typed answer from the composer; empty sends what's picked. */ + const answer = (text: string) => { + if (!draft || !question) return; + const typed = text ? setPendingUserInputCustomAnswer(question, draft.answers[question.id], text) : draft.answers[question.id]; + advance({ ...draft, answers: { ...draft.answers, ...(typed ? { [question.id]: typed } : {}) } }); + }; + + const next = () => draft && advance(draft); + + const dismiss = threadId && request?.dismissible ? () => run("Dismissed", () => session.dismissQuestion(threadId, request.requestId)) : undefined; + + const keys = request + ? { + pick: { keys: [], label: "1-9", hint: "pick" } satisfies Binding, + digits: [1, 2, 3, 4, 5, 6, 7, 8, 9].map((digit): Binding => ({ keys: [String(digit)], run: () => pick(digit - 1) })), + dismiss: dismiss ? ({ keys: ["x"], run: dismiss, hint: "dismiss", command: { id: "dismiss", title: "Dismiss question" } } satisfies Binding) : null, + } + : null; + + return { request, draft, question, pick, answer, next, dismiss, keys }; +}