'use client'; import { useCallback, useState } from 'react'; import { IconX, IconMessageQuestion } from '@tabler/icons-react'; import { cn } from '@/lib/utils'; import { getBackendConfig } from '@/lib/config'; import type { Message, ClarificationRequestMetadata, ClarificationAnswer } from '@/lib/types/http'; type ClarificationInputOverlayProps = { message: Message; onResolved: () => void; }; /** * Inline clarification UI - simple numbered text options like Conductor. */ export function ClarificationInputOverlay({ message, onResolved }: ClarificationInputOverlayProps) { const metadata = message.metadata as ClarificationRequestMetadata | undefined; const [customText, setCustomText] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const handleSubmitOption = useCallback(async (optionId: string) => { if (!metadata?.pending_id || isSubmitting) return; setIsSubmitting(true); try { const answer: ClarificationAnswer = { question_id: metadata.question.id, selected_options: [optionId], }; const { apiBaseUrl } = getBackendConfig(); const response = await fetch(`${apiBaseUrl}/api/v1/clarification/${metadata.pending_id}/respond`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ answers: [answer], rejected: false }), }); if (response.ok) { onResolved(); } else if (response.status === 410) { // Clarification expired (agent timed out) // Backend has marked the message as expired, so reload to clear the UI const data = await response.json().catch(() => ({})); console.warn('Clarification expired:', data); // Force reload to fetch updated messages (clarification no longer pending) window.location.reload(); } else { console.error('Failed to submit clarification response:', response.status, response.statusText); } } catch (error) { console.error('Failed to submit clarification response:', error); } finally { setIsSubmitting(false); } }, [metadata, isSubmitting, onResolved]); const handleSubmitCustom = useCallback(async () => { if (!metadata?.pending_id || isSubmitting || !customText.trim()) return; setIsSubmitting(true); try { const answer: ClarificationAnswer = { question_id: metadata.question.id, selected_options: [], custom_text: customText.trim(), }; const { apiBaseUrl } = getBackendConfig(); const response = await fetch(`${apiBaseUrl}/api/v1/clarification/${metadata.pending_id}/respond`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ answers: [answer], rejected: false }), }); if (response.ok) { onResolved(); } else if (response.status === 410) { // Clarification expired (agent timed out) // Backend has marked the message as expired, so reload to clear the UI const data = await response.json().catch(() => ({})); console.warn('Clarification expired:', data); // Force reload to fetch updated messages (clarification no longer pending) window.location.reload(); } else { console.error('Failed to submit clarification response:', response.status, response.statusText); } } catch (error) { console.error('Failed to submit clarification response:', error); } finally { setIsSubmitting(false); } }, [metadata, isSubmitting, customText, onResolved]); const handleSkip = useCallback(async () => { if (!metadata?.pending_id || isSubmitting) return; setIsSubmitting(true); try { const { apiBaseUrl } = getBackendConfig(); const response = await fetch(`${apiBaseUrl}/api/v1/clarification/${metadata.pending_id}/respond`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ answers: [], rejected: true, reject_reason: 'User skipped' }), }); if (response.ok) { onResolved(); } else if (response.status === 410) { // Clarification expired (agent timed out) const data = await response.json().catch(() => ({})); console.warn('Clarification expired:', data); alert('This question has timed out. The agent has already moved on. Please refresh the page.'); onResolved(); // Clear the UI even though it failed } else { console.error('Failed to skip clarification:', response.status, response.statusText); } } catch (error) { console.error('Failed to skip clarification:', error); } finally { setIsSubmitting(false); } }, [metadata, isSubmitting, onResolved]); if (!metadata?.question) { return null; } const question = metadata.question; return (
{question.prompt}