import { useState } from 'react' import { useChatStore } from '../../stores/chatStore' import { Button } from '../shared/Button' type QuestionOption = { label: string description?: string } type Question = { question: string options?: QuestionOption[] } type AskUserInput = { questions?: Question[] question?: string options?: QuestionOption[] } type Props = { toolUseId: string input: unknown } /** * Parse the AskUserQuestion input which may come in different shapes. */ function parseInput(input: unknown): Question[] { if (!input || typeof input !== 'object') return [] const obj = input as AskUserInput // Shape 1: { questions: [...] } if (Array.isArray(obj.questions)) { return obj.questions } // Shape 2: { question: "...", options: [...] } if (typeof obj.question === 'string') { return [{ question: obj.question, options: obj.options }] } return [] } export function AskUserQuestion({ toolUseId: _toolUseId, input }: Props) { const { sendMessage } = useChatStore() const questions = parseInput(input) const [selections, setSelections] = useState>({}) const [freeText, setFreeText] = useState('') const [submitted, setSubmitted] = useState(false) if (questions.length === 0) return null const handleSelect = (qIndex: number, label: string) => { if (submitted) return setSelections((prev) => ({ ...prev, [qIndex]: label })) // Clear free text when an option is selected setFreeText('') } const handleSubmit = () => { if (submitted) return // Build the response text const parts: string[] = [] for (let i = 0; i < questions.length; i++) { const selected = selections[i] if (selected) { parts.push(selected) } } // Free text overrides if provided const response = freeText.trim() || parts.join('; ') || '' if (!response) return setSubmitted(true) // Send the response as a user message -- the server routes it as a tool_result sendMessage(response) } const hasSelection = Object.keys(selections).length > 0 || freeText.trim().length > 0 return (
{/* Header */}
help
Claude needs your input {submitted && ( Answered )}
{/* Questions */}
{questions.map((q, qIndex) => (

{q.question}

{/* Option cards */} {q.options && q.options.length > 0 && (
{q.options.map((opt, optIndex) => { const isSelected = selections[qIndex] === opt.label return ( ) })}
)}
))} {/* Free text "Other" input */} {!submitted && (
{ setFreeText(e.target.value) // Clear radio selections when typing if (e.target.value.trim()) { setSelections({}) } }} onKeyDown={(e) => { if (e.key === 'Enter' && hasSelection) { handleSubmit() } }} placeholder="Type your answer..." className="flex-1 px-3 py-2 text-sm bg-[var(--color-surface)] border border-[var(--color-outline-variant)]/40 rounded-[var(--radius-md)] text-[var(--color-text-primary)] placeholder:text-[var(--color-text-tertiary)] focus:outline-none focus:border-[var(--color-secondary)] focus:ring-1 focus:ring-[var(--color-secondary)]/30" />
)} {/* Submitted answer display */} {submitted && (
check_circle Answered: {freeText.trim() || Object.values(selections).join(', ')}
)}
{/* Submit button */} {!submitted && (
)}
) }