From dd9177fe76cf038feea0c2666a899ebcee98b6f0 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 26 Feb 2026 11:52:40 +0530 Subject: [PATCH] add quiz prompt component --- extensions/cli/src/ui/TUIChat.tsx | 9 +- .../src/ui/components/ChatScreenContent.tsx | 23 ++- .../cli/src/ui/components/QuizPrompt.tsx | 178 ++++++++++++++++++ .../cli/src/ui/components/ScreenContent.tsx | 11 +- extensions/cli/src/ui/hooks/useChat.ts | 31 +++ extensions/cli/src/ui/hooks/useChat.types.ts | 3 + 6 files changed, 252 insertions(+), 3 deletions(-) create mode 100644 extensions/cli/src/ui/components/QuizPrompt.tsx diff --git a/extensions/cli/src/ui/TUIChat.tsx b/extensions/cli/src/ui/TUIChat.tsx index bedad8b38..62174cead 100644 --- a/extensions/cli/src/ui/TUIChat.tsx +++ b/extensions/cli/src/ui/TUIChat.tsx @@ -184,6 +184,7 @@ function useChatHandlers( }; } +// eslint-disable-next-line complexity const TUIChat: React.FC = ({ remoteUrl, configPath, @@ -261,6 +262,7 @@ const TUIChat: React.FC = ({ compactionStartTime, inputMode, activePermissionRequest, + activeQuizQuestion, wasInterrupted, queuedMessages, handleUserMessage, @@ -269,6 +271,7 @@ const TUIChat: React.FC = ({ resetChatHistory, handleEditMessage, handleToolPermissionResponse, + handleQuizAnswer, } = useChat({ assistant: services.config?.config || undefined, model: services.model?.model || undefined, @@ -332,7 +335,9 @@ const TUIChat: React.FC = ({ // Determine if input should be disabled // Allow input even when services are loading, but disable for UI overlays const isInputDisabled = - navState.currentScreen !== "chat" || !!activePermissionRequest; + navState.currentScreen !== "chat" || + !!activePermissionRequest || + !!activeQuizQuestion; // Check if verbose mode is enabled for resource debugging const isVerboseMode = useMemo(() => process.argv.includes("--verbose"), []); @@ -430,7 +435,9 @@ const TUIChat: React.FC = ({ handleReload={handleReload} closeCurrentScreen={closeCurrentScreen} activePermissionRequest={activePermissionRequest} + activeQuizQuestion={activeQuizQuestion} handleToolPermissionResponse={handleToolPermissionResponse} + handleQuizAnswer={handleQuizAnswer} handleUserMessage={handleUserMessage} isWaitingForResponse={isWaitingForResponse} isCompacting={isCompacting} diff --git a/extensions/cli/src/ui/components/ChatScreenContent.tsx b/extensions/cli/src/ui/components/ChatScreenContent.tsx index 7c3225da6..44f0a38f7 100644 --- a/extensions/cli/src/ui/components/ChatScreenContent.tsx +++ b/extensions/cli/src/ui/components/ChatScreenContent.tsx @@ -1,19 +1,25 @@ import type { AssistantConfig } from "@continuedev/sdk"; import React from "react"; -import type { ActivePermissionRequest } from "../hooks/useChat.types.js"; +import type { + ActivePermissionRequest, + ActiveQuizQuestion, +} from "../hooks/useChat.types.js"; import { UserInput } from "../UserInput.js"; +import { QuizPrompt } from "./QuizPrompt.js"; import { ToolPermissionSelector } from "./ToolPermissionSelector.js"; interface ChatScreenContentProps { activePermissionRequest: ActivePermissionRequest | null; + activeQuizQuestion: ActiveQuizQuestion | null; handleToolPermissionResponse: ( requestId: string, approved: boolean, createPolicy?: boolean, stopStream?: boolean, ) => void; + handleQuizAnswer: (requestId: string, answer: string) => void; handleUserMessage: (message: string, imageMap?: Map) => void; isWaitingForResponse: boolean; isCompacting: boolean; @@ -30,7 +36,9 @@ interface ChatScreenContentProps { export const ChatScreenContent: React.FC = ({ activePermissionRequest, + activeQuizQuestion, handleToolPermissionResponse, + handleQuizAnswer, handleUserMessage, isWaitingForResponse, isCompacting, @@ -44,6 +52,19 @@ export const ChatScreenContent: React.FC = ({ onImageInClipboardChange, onShowEditSelector, }) => { + if (activeQuizQuestion) { + return ( + + ); + } + if (activePermissionRequest) { return ( void; +} + +/**Quiz prompt component for answering for the AskQuestion tool and QuizService */ +export const QuizPrompt: React.FC = ({ + question, + options: rawOptions, + allowCustomAnswer = false, + defaultAnswer, + requestId, + onAnswer, +}) => { + const options = Array.isArray(rawOptions) ? rawOptions : undefined; + const [selectedIndex, setSelectedIndex] = useState(0); + const [isCustomMode, setIsCustomMode] = useState( + !options || options.length === 0, + ); // allows typing out an answer even when there are options + const [customInput, setCustomInput] = useState(""); + const [submitted, setSubmitted] = useState(false); + + const hasOptions = options && options.length > 0; + + useInput((input, key) => { + if (submitted) return; + + if (isCustomMode) { + // submit the typed answer + if (key.return) { + const answer = customInput.trim() || defaultAnswer || ""; + if (answer) { + setSubmitted(true); + onAnswer(requestId, answer, true); + } + return; + } + + if (key.backspace || key.delete) { + setCustomInput((prev) => prev.slice(0, -1)); + return; + } + + if ( + hasOptions && + allowCustomAnswer && + key.upArrow && + customInput === "" + ) { + // exit out of typing a custom answer + setIsCustomMode(false); + setSelectedIndex(options.length - 1); + return; + } + + if (input && !key.ctrl && !key.meta) { + // handle typing a custom answer + setCustomInput((prev) => prev + input); + } + return; + } + + if (key.return) { + // submit the selected option + if (hasOptions && options[selectedIndex]) { + setSubmitted(true); + onAnswer(requestId, options[selectedIndex], false); + } + return; + } + + // navigate between options + if (key.upArrow) { + setSelectedIndex((prev) => + prev === 0 ? (hasOptions ? options.length - 1 : 0) : prev - 1, + ); + } else if (key.downArrow) { + if (hasOptions) { + if (selectedIndex === options.length - 1 && allowCustomAnswer) { + setIsCustomMode(true); + setSelectedIndex(0); + } else { + setSelectedIndex((prev) => + prev === options.length - 1 ? 0 : prev + 1, + ); + } + } + } + + if ( + allowCustomAnswer && + input && + !key.ctrl && + !key.meta && + !key.upArrow && + !key.downArrow + ) { + setIsCustomMode(true); + setCustomInput(input); + } + }); + + if (submitted) { + return null; + } + + return ( + + + + ?{" "} + + {question} + + + {hasOptions && !isCustomMode && ( + + {options.map((option, index) => { + const isSelected = index === selectedIndex; + return ( + + + {isSelected ? "❯ " : " "} + {option} + + + ); + })} + {allowCustomAnswer && ( + + + {" "}(or start typing for custom answer) + + + )} + + )} + + {isCustomMode && ( + + + ❯ + {customInput} + ▊ + + {defaultAnswer && !customInput && ( + + + (Press Enter for default: {defaultAnswer}) + + + )} + + )} + + + + {hasOptions && !isCustomMode + ? "↑/↓ navigate, Enter select" + : "Type answer, Enter submit"} + + + + ); +}; diff --git a/extensions/cli/src/ui/components/ScreenContent.tsx b/extensions/cli/src/ui/components/ScreenContent.tsx index aff68f1ce..516cb7811 100644 --- a/extensions/cli/src/ui/components/ScreenContent.tsx +++ b/extensions/cli/src/ui/components/ScreenContent.tsx @@ -9,7 +9,10 @@ import type { NavigationScreen } from "../context/NavigationContext.js"; import { DiffViewer } from "../DiffViewer.js"; import { EditMessageSelector } from "../EditMessageSelector.js"; import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js"; -import type { ActivePermissionRequest } from "../hooks/useChat.types.js"; +import type { + ActivePermissionRequest, + ActiveQuizQuestion, +} from "../hooks/useChat.types.js"; import { JobsSelector } from "../JobsSelector.js"; import { MCPSelector } from "../MCPSelector.js"; import { ModelSelector } from "../ModelSelector.js"; @@ -31,12 +34,14 @@ interface ScreenContentProps { handleReload: () => Promise; closeCurrentScreen: () => void; activePermissionRequest: ActivePermissionRequest | null; + activeQuizQuestion: ActiveQuizQuestion | null; handleToolPermissionResponse: ( requestId: string, approved: boolean, createPolicy?: boolean, stopStream?: boolean, ) => void; + handleQuizAnswer: (requestId: string, answer: string) => void; handleUserMessage: (message: string, imageMap?: Map) => void; isWaitingForResponse: boolean; isCompacting: boolean; @@ -72,7 +77,9 @@ export const ScreenContent: React.FC = ({ handleReload, closeCurrentScreen, activePermissionRequest, + activeQuizQuestion, handleToolPermissionResponse, + handleQuizAnswer, handleUserMessage, isWaitingForResponse, isCompacting, @@ -198,7 +205,9 @@ export const ScreenContent: React.FC = ({ return ( ([]); const [activePermissionRequest, setActivePermissionRequest] = useState(null); + const [activeQuizQuestion, setActiveQuizQuestion] = + useState(null); const [compactionIndex, setCompactionIndex] = useState(() => { // When resuming or forking, check for compaction markers in the loaded history if ((resume || fork) && currentSession.history.length > 0) { @@ -195,6 +198,27 @@ export function useChat({ }; }, []); + // Set up quiz service listeners + useEffect(() => { + const quizSvc = services.quiz; + + const onQuestionRequested = (pending: ActiveQuizQuestion) => { + setActiveQuizQuestion(pending); + }; + + const onQuestionAnswered = () => { + setActiveQuizQuestion(null); + }; + + quizSvc.on("questionRequested", onQuestionRequested); + quizSvc.on("questionAnswered", onQuestionAnswered); + + return () => { + quizSvc.off("questionRequested", onQuestionRequested); + quizSvc.off("questionAnswered", onQuestionAnswered); + }; + }, []); + // Clean up abort controller on unmount useEffect(() => { return () => { @@ -829,6 +853,11 @@ export function useChat({ } }; + const handleQuizAnswer = (requestId: string, answer: string) => { + setActiveQuizQuestion(null); + services.quiz.answerQuestion(requestId, answer); + }; + return { chatHistory, setChatHistory: setChatHistory, @@ -839,6 +868,7 @@ export function useChat({ inputMode, attachedFiles, activePermissionRequest, + activeQuizQuestion, wasInterrupted, queuedMessages, handleUserMessage, @@ -847,5 +877,6 @@ export function useChat({ resetChatHistory, handleEditMessage, handleToolPermissionResponse, + handleQuizAnswer, }; } diff --git a/extensions/cli/src/ui/hooks/useChat.types.ts b/extensions/cli/src/ui/hooks/useChat.types.ts index ac2ecc5c6..f344b7f8a 100644 --- a/extensions/cli/src/ui/hooks/useChat.types.ts +++ b/extensions/cli/src/ui/hooks/useChat.types.ts @@ -2,6 +2,7 @@ import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; import { BaseLlmApi } from "@continuedev/openai-adapters"; import type { Session } from "core/index.js"; +import { QuestionRequestedEvent } from "../../quiz/types.js"; import { ToolCallPreview } from "../../tools/types.js"; export interface UseChatProps { @@ -42,6 +43,8 @@ export interface ActivePermissionRequest { hasDynamicEvaluation?: boolean; } +export type ActiveQuizQuestion = QuestionRequestedEvent; + export interface RemoteServerState { session: Session; isProcessing: boolean;