'use client' import { useState } from 'react' import { IconArrowRight as ArrowRight, IconCheck as Check, IconPlayerTrackNext as SkipForward } from '@tabler/icons-react' import type { ToolPart } from '@/lib/types/ai' import { Button } from '@/components/ui/button' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Checkbox } from '@/components/ui/checkbox' import { Input } from '@/components/ui/input' interface QuestionConfirmationProps { toolInvocation: ToolPart<'askQuestion'> onConfirm: (toolCallId: string, approved: boolean, response?: any) => void isCompleted?: boolean } interface QuestionOption { value: string label: string } interface QuestionInput { question: string options: QuestionOption[] allowsInput?: boolean inputLabel?: string inputPlaceholder?: string } interface QuestionOutput { selectedOptions?: string[] inputText?: string skipped?: boolean } export function QuestionConfirmation({ toolInvocation, onConfirm, isCompleted = false }: QuestionConfirmationProps) { const input = (toolInvocation.input || {}) as QuestionInput const { question = '', options = [], allowsInput = false, inputLabel = '', inputPlaceholder = '' } = input // Get result data if available const resultData = toolInvocation.state === 'output-available' && toolInvocation.output ? toolInvocation.output : null const [selectedOptions, setSelectedOptions] = useState([]) const [inputText, setInputText] = useState('') const [completed, setCompleted] = useState(isCompleted) const [skipped, setSkipped] = useState(false) const isButtonDisabled = selectedOptions.length === 0 && (!allowsInput || inputText.trim() === '') const handleOptionChange = (label: string) => { setSelectedOptions(prev => { if (prev.includes(label)) { return prev.filter(item => item !== label) } else { return [...prev, label] } }) } const handleInputChange = (e: React.ChangeEvent) => { setInputText(e.target.value) } const handleSkip = () => { setSkipped(true) setCompleted(true) onConfirm(toolInvocation.toolCallId, false, { skipped: true }) } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() const response = { selectedOptions, inputText: inputText.trim(), question } onConfirm(toolInvocation.toolCallId, true, response) setCompleted(true) } // Get options to display (from result or local state) const getDisplayedOptions = (): string[] => { const result = resultData as QuestionOutput | null if (result && Array.isArray(result.selectedOptions)) { return result.selectedOptions } return selectedOptions } // Get input text to display (from result or local state) const getDisplayedInputText = (): string => { const result = resultData as QuestionOutput | null if (result && result.inputText) { return result.inputText } return inputText } // Check if question was skipped const wasSkipped = (): boolean => { const result = resultData as QuestionOutput | null if (result && result.skipped) { return true } return skipped } const updatedQuery = () => { // If skipped, show skipped message if (wasSkipped()) { return 'Question skipped' } const displayOptions = getDisplayedOptions() const displayInputText = getDisplayedInputText() const optionsText = displayOptions.length > 0 ? `Selected: ${displayOptions.join(', ')}` : '' const inputTextDisplay = displayInputText.trim() !== '' ? `Input: ${displayInputText}` : '' return [optionsText, inputTextDisplay].filter(Boolean).join(' | ') } // Show result view if completed or if tool has result state if (completed || toolInvocation.state === 'output-available') { const isSkipped = wasSkipped() return ( {question}
{isSkipped ? ( ) : ( )}
{updatedQuery()}
) } return ( {question}
{options && options.map((option: QuestionOption, index: number) => (
handleOptionChange(option.label)} />
))}
{allowsInput && (
)}
) }