add quiz prompt component
This commit is contained in:
parent
6d082f2ff7
commit
dd9177fe76
6 changed files with 252 additions and 3 deletions
|
|
@ -184,6 +184,7 @@ function useChatHandlers(
|
|||
};
|
||||
}
|
||||
|
||||
// eslint-disable-next-line complexity
|
||||
const TUIChat: React.FC<TUIChatProps> = ({
|
||||
remoteUrl,
|
||||
configPath,
|
||||
|
|
@ -261,6 +262,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
compactionStartTime,
|
||||
inputMode,
|
||||
activePermissionRequest,
|
||||
activeQuizQuestion,
|
||||
wasInterrupted,
|
||||
queuedMessages,
|
||||
handleUserMessage,
|
||||
|
|
@ -269,6 +271,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
resetChatHistory,
|
||||
handleEditMessage,
|
||||
handleToolPermissionResponse,
|
||||
handleQuizAnswer,
|
||||
} = useChat({
|
||||
assistant: services.config?.config || undefined,
|
||||
model: services.model?.model || undefined,
|
||||
|
|
@ -332,7 +335,9 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
// 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<TUIChatProps> = ({
|
|||
handleReload={handleReload}
|
||||
closeCurrentScreen={closeCurrentScreen}
|
||||
activePermissionRequest={activePermissionRequest}
|
||||
activeQuizQuestion={activeQuizQuestion}
|
||||
handleToolPermissionResponse={handleToolPermissionResponse}
|
||||
handleQuizAnswer={handleQuizAnswer}
|
||||
handleUserMessage={handleUserMessage}
|
||||
isWaitingForResponse={isWaitingForResponse}
|
||||
isCompacting={isCompacting}
|
||||
|
|
|
|||
|
|
@ -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<string, Buffer>) => void;
|
||||
isWaitingForResponse: boolean;
|
||||
isCompacting: boolean;
|
||||
|
|
@ -30,7 +36,9 @@ interface ChatScreenContentProps {
|
|||
|
||||
export const ChatScreenContent: React.FC<ChatScreenContentProps> = ({
|
||||
activePermissionRequest,
|
||||
activeQuizQuestion,
|
||||
handleToolPermissionResponse,
|
||||
handleQuizAnswer,
|
||||
handleUserMessage,
|
||||
isWaitingForResponse,
|
||||
isCompacting,
|
||||
|
|
@ -44,6 +52,19 @@ export const ChatScreenContent: React.FC<ChatScreenContentProps> = ({
|
|||
onImageInClipboardChange,
|
||||
onShowEditSelector,
|
||||
}) => {
|
||||
if (activeQuizQuestion) {
|
||||
return (
|
||||
<QuizPrompt
|
||||
question={activeQuizQuestion.question.question}
|
||||
options={activeQuizQuestion.question.options}
|
||||
allowCustomAnswer={activeQuizQuestion.question.allowCustomAnswer}
|
||||
defaultAnswer={activeQuizQuestion.question.defaultAnswer}
|
||||
requestId={activeQuizQuestion.requestId}
|
||||
onAnswer={handleQuizAnswer}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (activePermissionRequest) {
|
||||
return (
|
||||
<ToolPermissionSelector
|
||||
|
|
|
|||
178
extensions/cli/src/ui/components/QuizPrompt.tsx
Normal file
178
extensions/cli/src/ui/components/QuizPrompt.tsx
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
import { Box, Text, useInput } from "ink";
|
||||
import React, { useState } from "react";
|
||||
|
||||
import { defaultBoxStyles } from "../styles.js";
|
||||
|
||||
interface QuizPromptProps {
|
||||
question: string;
|
||||
options?: string[];
|
||||
allowCustomAnswer?: boolean;
|
||||
defaultAnswer?: string;
|
||||
requestId: string;
|
||||
onAnswer: (
|
||||
requestId: string,
|
||||
answer: string,
|
||||
isCustomAnswer: boolean,
|
||||
) => void;
|
||||
}
|
||||
|
||||
/**Quiz prompt component for answering for the AskQuestion tool and QuizService */
|
||||
export const QuizPrompt: React.FC<QuizPromptProps> = ({
|
||||
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 (
|
||||
<Box {...defaultBoxStyles("magenta")} marginBottom={1}>
|
||||
<Box marginBottom={1}>
|
||||
<Text color="magenta" bold>
|
||||
?{" "}
|
||||
</Text>
|
||||
<Text bold>{question}</Text>
|
||||
</Box>
|
||||
|
||||
{hasOptions && !isCustomMode && (
|
||||
<Box flexDirection="column" marginLeft={2}>
|
||||
{options.map((option, index) => {
|
||||
const isSelected = index === selectedIndex;
|
||||
return (
|
||||
<Box key={index}>
|
||||
<Text color={isSelected ? "cyan" : "white"} bold={isSelected}>
|
||||
{isSelected ? "❯ " : " "}
|
||||
{option}
|
||||
</Text>
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
{allowCustomAnswer && (
|
||||
<Box>
|
||||
<Text color="gray" dimColor>
|
||||
{" "}(or start typing for custom answer)
|
||||
</Text>
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{isCustomMode && (
|
||||
<Box marginLeft={2} flexDirection="column">
|
||||
<Box>
|
||||
<Text color="cyan">❯ </Text>
|
||||
<Text>{customInput}</Text>
|
||||
<Text color="gray">▊</Text>
|
||||
</Box>
|
||||
{defaultAnswer && !customInput && (
|
||||
<Box marginTop={1}>
|
||||
<Text color="gray" dimColor>
|
||||
(Press Enter for default: {defaultAnswer})
|
||||
</Text>
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
|
||||
<Box marginTop={1}>
|
||||
<Text color="gray" dimColor>
|
||||
{hasOptions && !isCustomMode
|
||||
? "↑/↓ navigate, Enter select"
|
||||
: "Type answer, Enter submit"}
|
||||
</Text>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
|
@ -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<void>;
|
||||
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<string, Buffer>) => void;
|
||||
isWaitingForResponse: boolean;
|
||||
isCompacting: boolean;
|
||||
|
|
@ -72,7 +77,9 @@ export const ScreenContent: React.FC<ScreenContentProps> = ({
|
|||
handleReload,
|
||||
closeCurrentScreen,
|
||||
activePermissionRequest,
|
||||
activeQuizQuestion,
|
||||
handleToolPermissionResponse,
|
||||
handleQuizAnswer,
|
||||
handleUserMessage,
|
||||
isWaitingForResponse,
|
||||
isCompacting,
|
||||
|
|
@ -198,7 +205,9 @@ export const ScreenContent: React.FC<ScreenContentProps> = ({
|
|||
return (
|
||||
<ChatScreenContent
|
||||
activePermissionRequest={activePermissionRequest}
|
||||
activeQuizQuestion={activeQuizQuestion}
|
||||
handleToolPermissionResponse={handleToolPermissionResponse}
|
||||
handleQuizAnswer={handleQuizAnswer}
|
||||
handleUserMessage={handleUserMessage}
|
||||
isWaitingForResponse={isWaitingForResponse}
|
||||
isCompacting={isCompacting}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ import {
|
|||
} from "./useChat.stream.helpers.js";
|
||||
import {
|
||||
ActivePermissionRequest,
|
||||
ActiveQuizQuestion,
|
||||
AttachedFile,
|
||||
UseChatProps,
|
||||
} from "./useChat.types.js";
|
||||
|
|
@ -168,6 +169,8 @@ export function useChat({
|
|||
const [attachedFiles, setAttachedFiles] = useState<AttachedFile[]>([]);
|
||||
const [activePermissionRequest, setActivePermissionRequest] =
|
||||
useState<ActivePermissionRequest | null>(null);
|
||||
const [activeQuizQuestion, setActiveQuizQuestion] =
|
||||
useState<ActiveQuizQuestion | null>(null);
|
||||
const [compactionIndex, setCompactionIndex] = useState<number | null>(() => {
|
||||
// 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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue