import type { ToolCallBlock } from '@agentscope-ai/agentscope/message'; import { ChevronRight } from 'lucide-react'; import { useEffect, useState } from 'react'; import { getDisplayName, renderConfirmBody } from './tool-renderers'; import { Button } from '@/components/ui/button'; import { Kbd } from '@/components/ui/kbd'; import { useTranslation } from '@/i18n/useI18n'; import { cn } from '@/lib/utils'; type SelectOption = 'yes' | 'yes_with_rule' | 'no'; export function ConfirmCard({ toolCall, onUserConfirm, }: { toolCall: ToolCallBlock; onUserConfirm: (confirm: boolean, rules?: ToolCallBlock['suggested_rules']) => void; }) { const { t } = useTranslation(); const hasSuggestedRules = !!toolCall.suggested_rules?.length; const options: SelectOption[] = hasSuggestedRules ? ['yes', 'yes_with_rule', 'no'] : ['yes', 'no']; const [selected, setSelected] = useState('yes'); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { const currentIndex = options.indexOf(selected); switch (e.key) { case 'ArrowUp': e.preventDefault(); setSelected(options[(currentIndex - 1 + options.length) % options.length]); break; case 'ArrowDown': e.preventDefault(); setSelected(options[(currentIndex + 1) % options.length]); break; case 'Enter': e.preventDefault(); if (selected === 'yes_with_rule') { onUserConfirm(true, [toolCall.suggested_rules![0]]); } else { onUserConfirm(selected === 'yes'); } break; } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [onUserConfirm, selected, options]); return (
{getDisplayName(toolCall, t)}
{renderConfirmBody(toolCall, t)}
{t('chat.confirmToolCall')} {hasSuggestedRules && ( )}
); }