Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

stream-debugger.tsx

760 lines23.1 KB

1import { createFileRoute, useSearch } from '@tanstack/react-router'
2import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
3import {
4 Check,
5 Copy,
6 FastForward,
7 RefreshCw,
8 RotateCcw,
9 SkipBack,
10 SkipForward,
11 Upload,
12} from 'lucide-react'
13import { StreamProcessor, uiMessageToModelMessages } from '@tanstack/ai'
14​
15import type {
16 ChunkRecording,
17 ProcessorResult,
18 StreamChunk,
19 UIMessage,
20} from '@tanstack/ai'
21​
22// Import sample traces
23import * as sampleTraces from '@/traces'
24​
25export const Route = createFileRoute('/stream-debugger')({
26 component: TestPanel,
27 validateSearch: (search: Record<string, unknown>) => {
28 return {
29 trace: (search.trace as string) || undefined,
30 }
31 },
32})
33​
34interface RecordedTrace {
35 id: string
36 filename: string
37 timestamp: string
38 provider: string
39 model: string
40 size: number
41 chunkCount: number
43​
44function TestPanel() {
45 const searchParams = useSearch({ from: '/stream-debugger' })
46 const [recording, setRecording] = useState<ChunkRecording | null>(null)
47 const [currentChunkIndex, setCurrentChunkIndex] = useState(-1)
48 const [uiMessage, setUIMessage] = useState<UIMessage | null>(null)
49 const [result, setResult] = useState<ProcessorResult | null>(null)
50 const [isDragging, setIsDragging] = useState(false)
51 const [selectedSample, setSelectedSample] = useState<string>('')
52 const [copied, setCopied] = useState(false)
53 const [recordedTraces, setRecordedTraces] = useState<Array<RecordedTrace>>([])
54 const fileInputRef = useRef<HTMLInputElement>(null)
55​
56 // Processor ref for step-through mode
57 const processorRef = useRef<StreamProcessor | null>(null)
58​
59 const sampleOptions = useMemo(() => Object.keys(sampleTraces), [])
60​
61 // Function to fetch recorded traces
62 const fetchRecordedTraces = useCallback(() => {
63 fetch('/api/list-traces')
64 .then((res) => res.json())
65 .then((data) => {
66 if (data.traces) {
67 setRecordedTraces(data.traces)
68 }
69 })
70 .catch((error) => {
71 console.error('[TestPanel] Failed to load trace list:', error)
72 })
73 }, [])
74​
75 // Fetch recorded traces on mount
76 useEffect(() => {
77 fetchRecordedTraces()
78 }, [fetchRecordedTraces])
79​
80 const resetState = useCallback(() => {
81 setCurrentChunkIndex(-1)
82 setUIMessage(null)
83 setResult(null)
84 processorRef.current = null
85 }, [])
86​
87 const createProcessor = useCallback(() => {
88 const processor = new StreamProcessor({
89 events: {
90 onMessagesChange: (messages: Array<UIMessage>) => {
91 // Get the assistant message (should be the last one)
92 const assistantMessage = messages.find((m) => m.role === 'assistant')
93 if (assistantMessage) {
94 setUIMessage(assistantMessage)
95 }
96 },
97 onStreamEnd: (message: UIMessage) => {
98 // Get text content from parts
99 const textParts = message.parts.filter((p) => p.type === 'text')
100 const content = textParts.map((p) => (p as any).content).join('')
101​
102 // Get tool calls from parts
103 const toolCallParts = message.parts.filter(
104 (p) => p.type === 'tool-call',
105 )
106 const toolCalls =
107 toolCallParts.length > 0
108 ? toolCallParts.map((p: any) => ({
109 id: p.id,
110 type: 'function' as const,
111 function: {
112 name: p.name,
113 arguments: p.arguments,
114 },
115 }))
116 : undefined
117​
118 setResult({
119 content,
120 toolCalls,
121 finishReason: null,
122 })
123 },
124 },
125 })
126​
127 // Start an assistant message using the new API
128 processor.startAssistantMessage()
129​
130 processorRef.current = processor
131 return processor
132 }, [])
133​
134 const loadRecording = useCallback(
135 (rec: ChunkRecording) => {
136 setRecording(rec)
137 resetState()
138 },
139 [resetState],
140 )
141​
142 const handleFileUpload = useCallback(
143 (file: File) => {
144 const reader = new FileReader()
145 reader.onload = (e) => {
146 try {
147 const data = JSON.parse(e.target?.result as string) as ChunkRecording
148 loadRecording(data)
149 setSelectedSample('')
150 } catch (err) {
151 console.error('Failed to parse JSON:', err)
152 alert('Invalid JSON file')
153 }
154 }
155 reader.readAsText(file)
156 },
157 [loadRecording],
158 )
159​
160 const handleDrop = useCallback(
161 (e: React.DragEvent) => {
162 e.preventDefault()
163 setIsDragging(false)
164 const file = e.dataTransfer.files[0]
165 if (file && file.type === 'application/json') {
166 handleFileUpload(file)
167 }
168 },
169 [handleFileUpload],
170 )
171​
172 const handleSampleSelect = useCallback(
173 (value: string) => {
174 setSelectedSample(value)
175​
176 if (!value) return
177​
178 // Check if it's a recorded trace (trace:id) or sample trace (sample:name)
179 if (value.startsWith('trace:')) {
180 const traceId = value.substring(6) // Remove "trace:" prefix
181​
182 // Load the trace from API
183 fetch(`/api/load-trace?id=${traceId}`)
184 .then((res) => {
185 if (!res.ok) {
186 throw new Error(`Failed to load trace: ${res.statusText}`)
187 }
188 return res.json()
189 })
190 .then((traceData) => {
191 const chunkRecording: ChunkRecording = {
192 id: traceData.id,
193 timestamp: traceData.timestamp,
194 metadata: {
195 provider: traceData.provider,
196 model: traceData.model,
197 messages: traceData.messages,
198 },
199 chunks: traceData.chunks,
200 }
201 loadRecording(chunkRecording)
202 })
203 .catch((error) => {
204 console.error('[TestPanel] Failed to load trace:', error)
205 alert(`Failed to load trace: ${error.message}`)
206 })
207 } else if (value.startsWith('sample:')) {
208 const sampleName = value.substring(7) // Remove "sample:" prefix
209 if (sampleTraces[sampleName as keyof typeof sampleTraces]) {
210 loadRecording(
211 sampleTraces[
212 sampleName as keyof typeof sampleTraces
213 ] as ChunkRecording,
214 )
215 }
216 }
217 },
218 [loadRecording],
219 )
220​
221 const stepForward = useCallback(() => {
222 if (!recording) return
223​
224 const nextIndex = currentChunkIndex + 1
225 if (nextIndex >= recording.chunks.length) return
226​
227 if (!processorRef.current) {
228 createProcessor()
229 }
230​
231 const chunk = recording.chunks[nextIndex]?.chunk
232 if (chunk) {
233 processorRef.current?.processChunk(chunk)
234 setCurrentChunkIndex(nextIndex)
235​
236 // Update result with current processor state
237 const state = processorRef.current.getState()
238​
239 // Convert toolCalls Map to array
240 const toolCallsArray = Array.from(state.toolCalls.values()).map((tc) => ({
241 id: tc.id,
242 type: 'function' as const,
243 function: {
244 name: tc.name,
245 arguments: tc.arguments,
246 },
247 }))
248​
249 setResult({
250 content: state.content,
251 toolCalls: toolCallsArray.length > 0 ? toolCallsArray : undefined,
252 finishReason: null,
253 })
254 }
255 }, [recording, currentChunkIndex, createProcessor])
256​
257 const stepBackward = useCallback(() => {
258 if (!recording || currentChunkIndex < 0) return
259​
260 // Replay from start to currentChunkIndex - 1
261 const targetIndex = currentChunkIndex - 1
262 resetState()
263​
264 if (targetIndex < 0) return
265​
266 const processor = createProcessor()
267 for (let i = 0; i <= targetIndex; i++) {
268 const chunk = recording.chunks[i]?.chunk
269 if (chunk) {
270 processor.processChunk(chunk)
271 }
272 }
273 setCurrentChunkIndex(targetIndex)
274​
275 // Update result with current processor state
276 const state = processor.getState()
277​
278 // Convert toolCalls Map to array
279 const toolCallsArray = Array.from(state.toolCalls.values()).map((tc) => ({
280 id: tc.id,
281 type: 'function' as const,
282 function: {
283 name: tc.name,
284 arguments: tc.arguments,
285 },
286 }))
287​
288 setResult({
289 content: state.content,
290 toolCalls: toolCallsArray.length > 0 ? toolCallsArray : undefined,
291 finishReason: null,
292 })
293 }, [recording, currentChunkIndex, createProcessor, resetState])
294​
295 const runAll = useCallback(async () => {
296 if (!recording) return
297​
298 resetState()
299 const processor = createProcessor()
300​
301 for (let i = 0; i < recording.chunks.length; i++) {
302 const chunk = recording.chunks[i]?.chunk
303 if (chunk) {
304 processor.processChunk(chunk)
305 setCurrentChunkIndex(i)
306​
307 // Update result with current processor state
308 const state = processor.getState()
309​
310 // Convert toolCalls Map to array
311 const toolCallsArray = Array.from(state.toolCalls.values()).map(
312 (tc) => ({
313 id: tc.id,
314 type: 'function' as const,
315 function: {
316 name: tc.name,
317 arguments: tc.arguments,
318 },
319 }),
320 )
321​
322 setResult({
323 content: state.content,
324 toolCalls: toolCallsArray.length > 0 ? toolCallsArray : undefined,
325 finishReason: null,
326 })
327​
328 // Small delay for visual effect
329 await new Promise((resolve) => setTimeout(resolve, 50))
330 }
331 }
332 }, [recording, createProcessor, resetState])
333​
334 const reset = useCallback(() => {
335 resetState()
336 }, [resetState])
337​
338 const copyForIDE = useCallback(() => {
339 if (!recording) return
340​
341 const report = {
342 sample: selectedSample || 'custom',
343 currentChunkIndex,
344 totalChunks: recording.chunks.length,
345 chunks: recording.chunks
346 .slice(0, currentChunkIndex + 1)
347 .map(({ chunk, index }) => ({
348 index,
349 type: chunk.type,
350 chunk,
351 })),
352 uiMessage,
353 modelMessage: uiMessage ? convertToModelMessage(uiMessage) : null,
354 processorState: processorRef.current?.getState(),
355 }
356​
357 const formatted = `## Stream Processing Debug Report
358​
359**Sample:** ${report.sample}
360**Progress:** ${currentChunkIndex + 1}/${report.totalChunks} chunks processed
361​
362### Chunks Processed
363\`\`\`json
364${JSON.stringify(report.chunks, null, 2)}
365\`\`\`
366​
367### UIMessage (Parsed)
368\`\`\`json
369${JSON.stringify(report.uiMessage, null, 2)}
370\`\`\`
371​
372### ModelMessage (for server)
373\`\`\`json
374${JSON.stringify(report.modelMessage, null, 2)}
375\`\`\`
376​
377### Raw Processor State
378\`\`\`json
379${JSON.stringify(report.processorState, null, 2)}
380\`\`\`
382​
383 navigator.clipboard.writeText(formatted).then(() => {
384 setCopied(true)
385 setTimeout(() => setCopied(false), 2000)
386 })
387 }, [recording, selectedSample, currentChunkIndex, uiMessage, result])
388​
389 // Load trace from query param
390 useEffect(() => {
391 if (searchParams.trace) {
392 // Reset state first
393 setCurrentChunkIndex(-1)
394 setUIMessage(null)
395 setResult(null)
396 processorRef.current = null
397​
398 // Set the dropdown to show this trace
399 setSelectedSample(`trace:${searchParams.trace}`)
400​
401 // Then load the trace
402 fetch(`/api/load-trace?id=${searchParams.trace}`)
403 .then((res) => {
404 if (!res.ok) {
405 throw new Error(`Failed to load trace: ${res.statusText}`)
406 }
407 return res.json()
408 })
409 .then((traceData) => {
410 // Convert trace data to ChunkRecording format
411 const chunkRecording: ChunkRecording = {
412 id: traceData.id,
413 timestamp: traceData.timestamp,
414 metadata: {
415 provider: traceData.provider,
416 model: traceData.model,
417 messages: traceData.messages,
418 },
419 chunks: traceData.chunks,
420 }
421 setRecording(chunkRecording)
422 })
423 .catch((error) => {
424 console.error('[TestPanel] Failed to load trace:', error)
425 alert(`Failed to load trace: ${error.message}`)
426 })
427 }
428 }, [searchParams.trace])
429​
430 return (
431 <div className="p-6 flex flex-col gap-6 h-[calc(100vh-88px)] bg-gray-900">
432 {/* Controls Row */}
433 <div className="flex gap-4 items-center">
434 {/* File Upload / Drop Zone */}
435 <div
436 className={`flex-1 border-2 border-dashed rounded-lg p-4 transition-all cursor-pointer
437 ${
438 isDragging
439 ? 'border-orange-500 bg-orange-500/20'
440 : 'border-gray-700 hover:border-gray-500'
441 }`}
442 onDragOver={(e) => {
443 e.preventDefault()
444 setIsDragging(true)
445 }}
446 onDragLeave={() => setIsDragging(false)}
447 onDrop={handleDrop}
448 onClick={() => fileInputRef.current?.click()}
449 >
450 <div className="flex items-center gap-3 text-gray-400">
451 <Upload className="w-5 h-5" />
452 <span>Drop trace JSON file or click to upload</span>
453 </div>
454 <input
455 ref={fileInputRef}
456 type="file"
457 accept=".json"
458 className="hidden"
459 onChange={(e) =>
460 e.target.files?.[0] && handleFileUpload(e.target.files[0])
461 }
462 />
463 </div>
464​
465 {/* Sample Selector */}
466 <div className="flex items-center gap-2">
467 <label className="text-sm text-gray-400">Load Trace:</label>
468 <select
469 value={selectedSample}
470 onChange={(e) => handleSampleSelect(e.target.value)}
471 className="bg-gray-800 border border-gray-700 rounded-lg px-3 py-2 text-sm text-white min-w-[280px] focus:outline-none focus:ring-2 focus:ring-orange-500/50 hover:border-gray-600 transition-colors"
472 >
473 <option value="">Select a trace...</option>
474 {recordedTraces.length > 0 && (
475 <optgroup label="Recorded Traces">
476 {recordedTraces.map((trace) => (
477 <option key={trace.id} value={`trace:${trace.id}`}>
478 {trace.id} - {trace.provider}/{trace.model} (
479 {trace.chunkCount} chunks)
480 </option>
481 ))}
482 </optgroup>
483 )}
484 {sampleOptions.length > 0 && (
485 <optgroup label="Sample Traces">
486 {sampleOptions.map((name) => (
487 <option key={name} value={`sample:${name}`}>
488 {name}
489 </option>
490 ))}
491 </optgroup>
492 )}
493 </select>
494 <button
495 onClick={fetchRecordedTraces}
496 className="p-2 bg-gray-800 hover:bg-gray-700 border border-gray-700 rounded-lg text-white transition-colors"
497 title="Refresh trace list"
498 >
499 <RefreshCw className="w-4 h-4" />
500 </button>
501 </div>
502​
503 {/* Copy For IDE Button */}
504 <button
505 onClick={copyForIDE}
506 disabled={!recording || currentChunkIndex < 0}
507 className="flex items-center gap-2 px-4 py-2 bg-orange-600 hover:bg-orange-700 text-white rounded-lg disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
508 >
509 {copied ? (
510 <>
511 <Check className="w-4 h-4" />
512 <span>Copied!</span>
513 </>
514 ) : (
515 <>
516 <Copy className="w-4 h-4" />
517 <span>Copy For IDE</span>
518 </>
519 )}
520 </button>
521 </div>
522​
523 {/* Main Content */}
524 <div className="flex-1 grid grid-cols-2 gap-6 min-h-0">
525 {/* Left Panel - Raw Chunks */}
526 <div className="flex flex-col bg-gray-800 rounded-lg border border-gray-700 overflow-hidden">
527 <div className="px-4 py-3 border-b border-gray-700 flex items-center justify-between">
528 <h2 className="font-semibold text-white">
529 Raw Chunks {recording && `(${recording.chunks.length})`}
530 </h2>
531 <div className="flex items-center gap-1">
532 <button
533 onClick={stepBackward}
534 disabled={!recording || currentChunkIndex < 0}
535 className="p-2 rounded-lg hover:bg-gray-700 disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 hover:text-white transition-colors"
536 title="Step Back"
537 >
538 <SkipBack className="w-4 h-4" />
539 </button>
540 <button
541 onClick={stepForward}
542 disabled={
543 !recording ||
544 currentChunkIndex >= (recording?.chunks.length ?? 0) - 1
545 }
546 className="p-2 rounded-lg hover:bg-gray-700 disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 hover:text-white transition-colors"
547 title="Step Forward"
548 >
549 <SkipForward className="w-4 h-4" />
550 </button>
551 <button
552 onClick={runAll}
553 disabled={!recording}
554 className="p-2 rounded-lg hover:bg-gray-700 disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 hover:text-white transition-colors"
555 title="Run All"
556 >
557 <FastForward className="w-4 h-4" />
558 </button>
559 <button
560 onClick={reset}
561 disabled={!recording}
562 className="p-2 rounded-lg hover:bg-gray-700 disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 hover:text-white transition-colors"
563 title="Reset"
564 >
565 <RotateCcw className="w-4 h-4" />
566 </button>
567 </div>
568 </div>
569 <div className="flex-1 overflow-auto p-4 scrollbar-thin">
570 {recording ? (
571 <div className="space-y-2">
572 {recording.chunks.map(({ chunk }, arrayIndex) => (
573 <ChunkItem
574 key={arrayIndex}
575 chunk={chunk}
576 index={arrayIndex}
577 isActive={arrayIndex === currentChunkIndex}
578 isProcessed={arrayIndex <= currentChunkIndex}
579 />
580 ))}
581 </div>
582 ) : (
583 <div className="text-center text-gray-500 py-12">
584 Load a trace file to see chunks
585 </div>
586 )}
587 </div>
588 </div>
589​
590 {/* Right Panel - Parsed Output */}
591 <div className="flex flex-col gap-4 min-h-0">
592 {/* UIMessage */}
593 <div className="flex-1 bg-gray-800 rounded-lg border border-gray-700 overflow-hidden flex flex-col">
594 <div className="px-4 py-3 border-b border-gray-700">
595 <h2 className="font-semibold text-white">UIMessage (Parsed)</h2>
596 </div>
597 <div className="flex-1 overflow-auto p-4 scrollbar-thin">
598 {uiMessage ? (
599 <JsonView data={uiMessage} />
600 ) : (
601 <div className="text-center text-gray-500 py-12">
602 Process chunks to see UIMessage
603 </div>
604 )}
605 </div>
606 </div>
607​
608 {/* ModelMessage */}
609 <div className="flex-1 bg-gray-800 rounded-lg border border-gray-700 overflow-hidden flex flex-col">
610 <div className="px-4 py-3 border-b border-gray-700">
611 <h2 className="font-semibold text-white">
612 ModelMessage (for server)
613 </h2>
614 </div>
615 <div className="flex-1 overflow-auto p-4 scrollbar-thin">
616 {uiMessage ? (
617 <JsonView data={convertToModelMessage(uiMessage)} />
618 ) : (
619 <div className="text-center text-gray-500 py-12">
620 Process chunks to see ModelMessage
621 </div>
622 )}
623 </div>
624 </div>
625 </div>
626 </div>
627 </div>
628 )
630​
631function ChunkItem({
632 chunk,
633 index,
634 isActive,
635 isProcessed,
636}: {
637 chunk: StreamChunk
638 index: number
639 isActive: boolean
640 isProcessed: boolean
641}) {
642 const typeColors: Record<string, string> = {
643 content: 'text-green-400',
644 tool_call: 'text-blue-400',
645 tool_result: 'text-purple-400',
646 done: 'text-yellow-400',
647 error: 'text-red-400',
648 thinking: 'text-cyan-400',
649 'approval-requested': 'text-orange-400',
650 'tool-input-available': 'text-pink-400',
651 }
652​
653 const getSummary = (chunk: StreamChunk): string => {
654 switch (chunk.type) {
655 case 'content':
656 return `δ="${chunk.delta?.slice(0, 30) ?? ''}${(chunk.delta?.length ?? 0) > 30 ? '...' : ''}"`
657 case 'tool_call':
658 return `${chunk.toolCall.function.name}[${chunk.index}]`
659 case 'tool_result':
660 return `${chunk.toolCallId}`
661 case 'done':
662 return `${chunk.finishReason}`
663 case 'thinking':
664 return `δ="${chunk.delta?.slice(0, 20) ?? ''}..."`
665 case 'error':
666 return chunk.error.message.slice(0, 30)
667 default:
668 return ''
669 }
670 }
671​
672 return (
673 <div
674 className={`p-2 rounded-lg text-sm font-mono transition-all ${
675 isActive
676 ? 'bg-orange-500/20 border border-orange-500'
677 : isProcessed
678 ? 'bg-gray-700 opacity-60'
679 : 'bg-gray-900'
680 }`}
681 >
682 <div className="flex items-center gap-2">
683 <span className="text-gray-500 w-6">{index}</span>
684 <span className={typeColors[chunk.type] || 'text-gray-400'}>
685 {chunk.type}
686 </span>
687 <span className="text-gray-400 truncate">{getSummary(chunk)}</span>
688 </div>
689 </div>
690 )
692​
693function JsonView({ data }: { data: any }) {
694 const formatValue = (value: any, indent: number = 0): React.ReactNode => {
695 const spaces = ' '.repeat(indent)
696​
697 if (value === null) {
698 return <span className="json-null">null</span>
699 }
700 if (typeof value === 'boolean') {
701 return <span className="json-boolean">{String(value)}</span>
702 }
703 if (typeof value === 'number') {
704 return <span className="json-number">{value}</span>
705 }
706 if (typeof value === 'string') {
707 const escaped = value.replace(/"/g, '\\"').replace(/\n/g, '\\n')
708 return <span className="json-string">"{escaped}"</span>
709 }
710 if (Array.isArray(value)) {
711 if (value.length === 0) return '[]'
712 return (
713 <>
714 {'[\n'}
715 {value.map((item, i) => (
716 <span key={i}>
717 {spaces} {formatValue(item, indent + 1)}
718 {i < value.length - 1 ? ',' : ''}
719 {'\n'}
720 </span>
721 ))}
722 {spaces}]
723 </>
724 )
725 }
726 if (typeof value === 'object') {
727 const keys = Object.keys(value)
728 if (keys.length === 0) return '{}'
729 return (
730 <>
731 {'{\n'}
732 {keys.map((key, i) => (
733 <span key={key}>
734 {spaces} <span className="json-key">"{key}"</span>:{' '}
735 {formatValue(value[key], indent + 1)}
736 {i < keys.length - 1 ? ',' : ''}
737 {'\n'}
738 </span>
739 ))}
740 {spaces}
741 {'}'}
742 </>
743 )
744 }
745 return String(value)
746 }
747​
748 return (
749 <pre className="text-sm whitespace-pre-wrap break-all text-gray-100 font-mono">
750 {formatValue(data)}
751 </pre>
752 )
754​
755function convertToModelMessage(uiMessage: UIMessage): any {
756 // Use the actual uiMessageToModelMessages utility from @tanstack/ai-client
757 const modelMessages = uiMessageToModelMessages(uiMessage)
758 // Return the first message (the assistant message)
759 return modelMessages[0] || null