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

ContextSavings.tsx

297 lines9.1 KB

1'use client'
2​
3import { useMemo, useState } from 'react'
4import { Info, Zap } from 'lucide-react'
5import {
6 calculateEfficiency,
7 estimateCostFromBytes,
8 estimateRoundTripTimeMs,
9 formatBytes,
10 formatCurrency,
11 formatDuration,
12} from '../lib/efficiency'
13​
14interface ContextSavingsProps {
15 actual: number
16 theoretical: number
17 model?: string
19​
20interface MetricRowProps {
21 label: string
22 actual: string
23 theoretical: string
24 percent: number
25 description?: string
26 savedLabel?: string
28​
29function MetricRow({
30 label,
31 actual,
32 theoretical,
33 percent,
34 description,
35 savedLabel,
36}: MetricRowProps) {
37 // Show the "used" portion (what Code Mode actually uses)
38 const usedPercent = 100 - percent
39​
40 return (
41 <div className="space-y-1.5">
42 <div className="flex justify-between text-sm">
43 <span className="text-gray-400">{label}</span>
44 <span className="text-gray-300 font-mono text-xs">
45 {actual} / {theoretical}
46 </span>
47 </div>
48 <div className="flex items-center gap-2">
49 <div className="flex-1 h-2 bg-gray-700 rounded-full overflow-hidden">
50 <div
51 className="h-full bg-cyan-500 rounded-full transition-all duration-300"
52 style={{ width: `${usedPercent}%` }}
53 />
54 </div>
55 <span className="text-xs text-cyan-400 w-10 text-right">
56 {percent}%
57 </span>
58 </div>
59 <p className="text-xs text-gray-500">
60 {savedLabel ?? `${percent}% ${description}`}
61 </p>
62 </div>
63 )
65​
66interface ValueRowProps {
67 label: string
68 value: string
69 description?: string
71​
72function ValueRow({ label, value, description }: ValueRowProps) {
73 return (
74 <div className="space-y-1.5">
75 <div className="flex justify-between text-sm">
76 <span className="text-gray-400">{label}</span>
77 <span className="text-gray-300 font-mono text-xs">{value}</span>
78 </div>
79 {description && <p className="text-xs text-gray-500">{description}</p>}
80 </div>
81 )
83​
84export function ContextSavings({
85 actual,
86 theoretical,
87 model,
88}: ContextSavingsProps) {
89 const [isOpen, setIsOpen] = useState(false)
90​
91 const metrics = useMemo(
92 () =>
93 calculateEfficiency({
94 actualBytes: actual,
95 theoreticalBytes: theoretical,
96 model,
97 }),
98 [actual, theoretical, model],
99 )
100​
101 // Don't render if no savings
102 if (metrics.contextSavedBytes <= 0) return null
103​
104 // For the collapsed button, show time savings (most tangible per spec)
105 const usedPercentage = 100 - metrics.timeSavedPercent
106​
107 return (
108 <div className="relative">
109 <button
110 onClick={() => setIsOpen(!isOpen)}
111 className="flex items-center gap-2 px-2.5 py-1.5 rounded-lg hover:bg-gray-700/50 transition-colors"
112 title="Code Mode Efficiency"
113 >
114 <Zap className="w-3.5 h-3.5 text-cyan-500" />
115 <div className="w-14 h-2 bg-gray-700 rounded-full overflow-hidden">
116 <div
117 className="h-full bg-cyan-500 rounded-full transition-all duration-300"
118 style={{ width: `${usedPercentage}%` }}
119 />
120 </div>
121 <span className="text-xs text-gray-400 font-medium">
122 {metrics.timeSavedPercent}% faster
123 </span>
124 </button>
125​
126 {isOpen && (
127 <>
128 {/* Backdrop */}
129 <div
130 className="fixed inset-0 z-40"
131 onClick={() => setIsOpen(false)}
132 />
133​
134 {/* Popover */}
135 <div className="absolute right-0 top-full mt-2 w-80 bg-gray-900 border border-gray-700 rounded-xl shadow-xl z-50 overflow-hidden">
136 <div className="px-4 py-3 border-b border-gray-700/50">
137 <h3 className="font-medium text-gray-200">
138 Code Mode Efficiency
139 </h3>
140 </div>
141​
142 <div className="p-4 space-y-4">
143 {/* Context */}
144 <MetricRow
145 label="Context"
146 actual={formatBytes(metrics.contextActual)}
147 theoretical={formatBytes(metrics.contextTheoretical)}
148 percent={metrics.contextSavedPercent}
149 description="smaller context window"
150 />
151​
152 {/* Round-trips */}
153 <MetricRow
154 label="LLM Calls"
155 actual={`${metrics.roundTripsActual}`}
156 theoretical={`${metrics.roundTripsTheoretical}`}
157 percent={metrics.roundTripsSavedPercent}
158 description="fewer round-trips"
159 />
160​
161 {/* Time */}
162 <MetricRow
163 label="Time"
164 actual={`~${formatDuration(metrics.timeActualMs)}`}
165 theoretical={`~${formatDuration(metrics.timeTheoreticalMs)}`}
166 percent={metrics.timeSavedPercent}
167 savedLabel={`~${formatDuration(metrics.timeSavedMs)} saved`}
168 />
169​
170 {/* Cost */}
171 <MetricRow
172 label="Est. Cost"
173 actual={formatCurrency(metrics.costActual)}
174 theoretical={formatCurrency(metrics.costTheoretical)}
175 percent={metrics.costSavedPercent}
176 savedLabel={`~${formatCurrency(metrics.costSaved)} saved`}
177 />
178​
179 {/* Disclaimer */}
180 <div className="pt-3 border-t border-gray-700/50 flex items-start gap-2">
181 <Info className="w-3.5 h-3.5 text-gray-500 mt-0.5 flex-shrink-0" />
182 <p className="text-xs text-gray-500">
183 Estimates based on typical tool-calling patterns. Actual
184 savings may vary.
185 </p>
186 </div>
187 </div>
188 </div>
189 </>
190 )}
191 </div>
192 )
194​
195interface NoCodeMetricsProps {
196 totalBytes: number
197 llmCalls: number
198 totalContextBytes?: number
199 averageContextBytes?: number
200 totalTimeMs?: number
201 model?: string
203​
204export function NoCodeMetrics({
205 totalBytes,
206 llmCalls,
207 totalContextBytes,
208 averageContextBytes,
209 totalTimeMs,
210 model,
211}: NoCodeMetricsProps) {
212 const [isOpen, setIsOpen] = useState(false)
213 const estimatedTimeMs = estimateRoundTripTimeMs(llmCalls)
214 const resolvedTimeMs = totalTimeMs ?? estimatedTimeMs
215 const estimatedCost = estimateCostFromBytes(totalBytes, model)
216​
217 if (totalBytes <= 0 || llmCalls <= 0) return null
218​
219 return (
220 <div className="relative">
221 <button
222 onClick={() => setIsOpen(!isOpen)}
223 className="flex items-center gap-2 px-2.5 py-1.5 rounded-lg hover:bg-gray-700/50 transition-colors"
224 title="Chat Metrics"
225 >
226 <Info className="w-3.5 h-3.5 text-cyan-500" />
227 <span className="text-xs text-gray-400 font-medium">Chat metrics</span>
228 </button>
229​
230 {isOpen && (
231 <>
232 <div
233 className="fixed inset-0 z-40"
234 onClick={() => setIsOpen(false)}
235 />
236​
237 <div className="absolute right-0 top-full mt-2 w-80 bg-gray-900 border border-gray-700 rounded-xl shadow-xl z-50 overflow-hidden">
238 <div className="px-4 py-3 border-b border-gray-700/50">
239 <h3 className="font-medium text-gray-200">Chat Metrics</h3>
240 </div>
241​
242 <div className="p-4 space-y-4">
243 <ValueRow
244 label="Message Size"
245 value={formatBytes(totalBytes)}
246 description="Final serialized size of the messages array."
247 />
248 {typeof totalContextBytes === 'number' &&
249 totalContextBytes > 0 && (
250 <ValueRow
251 label="Context Bytes (Total)"
252 value={formatBytes(totalContextBytes)}
253 description="Accumulated context size across LLM calls."
254 />
255 )}
256 {typeof averageContextBytes === 'number' &&
257 averageContextBytes > 0 && (
258 <ValueRow
259 label="Context Bytes (Avg)"
260 value={formatBytes(averageContextBytes)}
261 description="Average context size per LLM call."
262 />
263 )}
264 <ValueRow
265 label="LLM Calls"
266 value={`${llmCalls}`}
267 description="Count of assistant responses."
268 />
269 <ValueRow
270 label="Time"
271 value={`${totalTimeMs ? '' : '~'}${formatDuration(resolvedTimeMs)}`}
272 description={
273 totalTimeMs
274 ? 'Measured from server events.'
275 : 'Estimated from round-trip latency.'
276 }
277 />
278 <ValueRow
279 label="Est. Cost"
280 value={formatCurrency(estimatedCost)}
281 description="Estimated from bytes and model pricing."
282 />
283​
284 <div className="pt-3 border-t border-gray-700/50 flex items-start gap-2">
285 <Info className="w-3.5 h-3.5 text-gray-500 mt-0.5 flex-shrink-0" />
286 <p className="text-xs text-gray-500">
287 Estimates based on typical LLM latency and pricing. Actual
288 usage may vary.
289 </p>
290 </div>
291 </div>
292 </div>
293 </>
294 )}
295 </div>
296 )