import React, { useMemo } from 'react'; import { MessageSquare } from 'lucide-react'; import { cn } from '@/lib/utils'; import TranscriptAssistantBlock from './TranscriptAssistantBlock'; import TranscriptItem from './TranscriptItem'; import type { DebugTranscriptRow, DebugTranscriptTextRow, DebugTranscriptToolRow } from './types'; type AssistantRenderBlock = { kind: 'assistant-block'; id: string; message?: DebugTranscriptTextRow; tools: DebugTranscriptToolRow[]; }; type TranscriptRenderItem = | { kind: 'row'; id: string; row: DebugTranscriptRow } | AssistantRenderBlock; const getCorrelationKey = (row: Pick) => { if (row.responseId) return `response:${row.responseId}`; if (row.turnId && row.utteranceId) return `turn:${row.turnId}:utterance:${row.utteranceId}`; if (row.turnId) return `turn:${row.turnId}`; if (row.utteranceId) return `utterance:${row.utteranceId}`; return ''; }; const buildRenderItems = (messages: DebugTranscriptRow[]): TranscriptRenderItem[] => { const items: TranscriptRenderItem[] = []; const assistantBlocks = new Map(); messages.forEach((row) => { if (row.kind === 'text' && row.role === 'assistant') { const correlationKey = getCorrelationKey(row); if (!correlationKey) { items.push({ kind: 'row', id: row.id, row }); return; } const existingBlock = assistantBlocks.get(correlationKey); if (existingBlock) { existingBlock.message = row; return; } const block: AssistantRenderBlock = { kind: 'assistant-block', id: `assistant-block:${correlationKey}`, message: row, tools: [], }; assistantBlocks.set(correlationKey, block); items.push(block); return; } if (row.kind === 'tool') { const correlationKey = getCorrelationKey(row); if (!correlationKey) { items.push({ kind: 'row', id: row.id, row }); return; } const existingBlock = assistantBlocks.get(correlationKey); if (existingBlock) { existingBlock.tools.push(row); return; } const block: AssistantRenderBlock = { kind: 'assistant-block', id: `assistant-block:${correlationKey}`, tools: [row], }; assistantBlocks.set(correlationKey, block); items.push(block); return; } items.push({ kind: 'row', id: row.id, row }); }); return items; }; const TranscriptList: React.FC<{ scrollRef: React.RefObject; messages: DebugTranscriptRow[]; isLoading: boolean; className?: string; }> = ({ scrollRef, messages, isLoading, className = '' }) => { const renderItems = useMemo(() => buildRenderItems(messages), [messages]); return (
{messages.length === 0 && !isLoading ? (

鏆傛棤瀵硅瘽璁板綍

) : (
{renderItems.map((item) => item.kind === 'assistant-block' ? ( ) : ( ) )}
)}
); }; export default React.memo(TranscriptList);