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

MessageSizeOverlay.tsx

172 lines5.8 KB

1import { useMemo } from 'react'
2import { Database, Zap } from 'lucide-react'
3import type { UIMessage } from '@tanstack/ai-react'
4import type { VMEvent } from './JavaScriptVM'
5​
6interface MessageSizeOverlayProps {
7 messages: Array<UIMessage>
8 toolCallEvents: Map<string, Array<VMEvent>>
9}
10​
11/**
12 * Format bytes to a human-readable string (KB, MB, etc.)
13 */
14function formatBytes(bytes: number): string {
15 if (bytes < 1024) return `${bytes} B`
16 if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
17 return `${(bytes / (1024 * 1024)).toFixed(2)} MB`
19​
20/**
21 * Calculate the actual byte size of the messages array
22 */
23function calculateActualSize(messages: Array<UIMessage>): number {
24 return new TextEncoder().encode(JSON.stringify(messages)).length
26​
27/**
28 * Calculate the theoretical byte size if all external tool calls were stored
29 * as individual tool call parts in the messages array.
30 */
31function calculateTheoreticalSize(
32 messages: Array<UIMessage>,
33 toolCallEvents: Map<string, Array<VMEvent>>,
34): number {
35 // Start with the actual messages size
36 let theoreticalSize = calculateActualSize(messages)
37​
38 // For each tool call's events, calculate the size of synthetic tool calls
39 for (const events of toolCallEvents.values()) {
40 // Group external_call events with their corresponding external_result events
41 const externalCalls: Array<{ call: VMEvent; result?: VMEvent }> = []
42​
43 for (const event of events) {
44 if (event.eventType === 'code_mode:external_call') {
45 externalCalls.push({ call: event })
46 } else if (event.eventType === 'code_mode:external_result') {
47 // Find the matching call (most recent one for this function)
48 const callData = event.data as { function?: string }
49 const matchingCall = [...externalCalls]
50 .reverse()
51 .find(
52 (c) =>
53 !c.result &&
54 (c.call.data as { function?: string })?.function ===
55 callData?.function,
56 )
57 if (matchingCall) {
58 matchingCall.result = event
59 }
60 }
61 }
62​
63 // Calculate size of synthetic tool call parts for each external call
64 for (const { call, result } of externalCalls) {
65 const callData = call.data as {
66 function?: string
67 args?: unknown
68 }
69 const resultData = result?.data as { result?: unknown }
70​
71 // Create a synthetic tool call structure similar to what would be in messages
72 const syntheticToolCall = {
73 type: 'tool-call',
74 id: `synthetic-${call.id}`,
75 name: callData?.function || 'unknown',
76 arguments: JSON.stringify(callData?.args || {}),
77 state: 'input-complete',
78 output: resultData?.result,
79 }
80​
81 // Add the size of this synthetic tool call
82 theoreticalSize += new TextEncoder().encode(
83 JSON.stringify(syntheticToolCall),
84 ).length
85 }
86 }
87​
88 return theoreticalSize
90​
91/**
92 * A fixed overlay that shows the comparison between actual message size
93 * and theoretical size if code mode external calls were stored in messages.
94 */
95export default function MessageSizeOverlay({
96 messages,
97 toolCallEvents,
98}: MessageSizeOverlayProps) {
99 const { actualSize, theoreticalSize, savings, savingsPercent } =
100 useMemo(() => {
101 const actual = calculateActualSize(messages)
102 const theoretical = calculateTheoreticalSize(messages, toolCallEvents)
103 const saved = theoretical - actual
104 const percent = theoretical > 0 ? (saved / theoretical) * 100 : 0
105​
106 return {
107 actualSize: actual,
108 theoreticalSize: theoretical,
109 savings: saved,
110 savingsPercent: percent,
111 }
112 }, [messages, toolCallEvents])
113​
114 // Don't show if there are no messages
115 if (messages.length === 0) {
116 return null
117 }
118​
119 // Don't show savings if there are no external calls
120 const hasExternalCalls = toolCallEvents.size > 0
121​
122 return (
123 <div className="fixed bottom-4 right-4 z-50 bg-gray-900/95 backdrop-blur-sm border border-cyan-500/30 rounded-lg shadow-xl p-3 min-w-[200px]">
124 <div className="flex items-center gap-2 mb-2 text-xs font-medium text-gray-400 uppercase tracking-wide">
125 <Database className="w-3.5 h-3.5" />
126 <span>Message Size</span>
127 </div>
128​
129 <div className="space-y-2">
130 {/* Actual size */}
131 <div className="flex items-center justify-between gap-4">
132 <span className="text-xs text-gray-400">Actual:</span>
133 <span className="text-sm font-mono text-cyan-400">
134 {formatBytes(actualSize)}
135 </span>
136 </div>
137​
138 {/* Theoretical size - only show if there are external calls */}
139 {hasExternalCalls && (
140 <>
141 <div className="flex items-center justify-between gap-4">
142 <span className="text-xs text-gray-400">Without Code Mode:</span>
143 <span className="text-sm font-mono text-gray-500">
144 {formatBytes(theoreticalSize)}
145 </span>
146 </div>
147​
148 {/* Savings indicator */}
149 {savings > 0 && (
150 <div className="pt-2 border-t border-gray-700/50">
151 <div className="flex items-center justify-between gap-2">
152 <div className="flex items-center gap-1.5 text-xs text-emerald-400">
153 <Zap className="w-3.5 h-3.5" />
154 <span>Saved:</span>
155 </div>
156 <div className="text-right">
157 <span className="text-sm font-mono text-emerald-400">
158 {formatBytes(savings)}
159 </span>
160 <span className="text-xs text-emerald-500/70 ml-1">
161 ({savingsPercent.toFixed(0)}%)
162 </span>
163 </div>
164 </div>
165 </div>
166 )}
167 </>
168 )}
169 </div>
170 </div>
171 )