|
| 1 | +import { Header3 } from "~/components/primitives/Headers"; |
| 2 | +import { CodeBlock } from "~/components/code/CodeBlock"; |
| 3 | +import { TruncatedCopyableValue } from "~/components/primitives/TruncatedCopyableValue"; |
| 4 | + |
| 5 | +export type AIToolCallData = { |
| 6 | + toolName: string; |
| 7 | + toolCallId: string; |
| 8 | + args?: string; |
| 9 | + durationMs: number; |
| 10 | +}; |
| 11 | + |
| 12 | +export function extractAIToolCallData( |
| 13 | + properties: Record<string, unknown>, |
| 14 | + durationMs: number |
| 15 | +): AIToolCallData | undefined { |
| 16 | + const ai = properties.ai; |
| 17 | + if (!ai || typeof ai !== "object") return undefined; |
| 18 | + |
| 19 | + const a = ai as Record<string, unknown>; |
| 20 | + if (a.operationId !== "ai.toolCall") return undefined; |
| 21 | + |
| 22 | + const toolCall = a.toolCall; |
| 23 | + if (!toolCall || typeof toolCall !== "object") return undefined; |
| 24 | + |
| 25 | + const tc = toolCall as Record<string, unknown>; |
| 26 | + const toolName = typeof tc.name === "string" ? tc.name : undefined; |
| 27 | + if (!toolName) return undefined; |
| 28 | + |
| 29 | + return { |
| 30 | + toolName, |
| 31 | + toolCallId: typeof tc.id === "string" ? tc.id : "", |
| 32 | + args: typeof tc.args === "string" ? tc.args : undefined, |
| 33 | + durationMs, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +export function AIToolCallSpanDetails({ data }: { data: AIToolCallData }) { |
| 38 | + return ( |
| 39 | + <div className="flex h-full flex-col overflow-hidden"> |
| 40 | + <div className="flex-1 overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600"> |
| 41 | + <div className="flex flex-col px-3"> |
| 42 | + {/* Tool info */} |
| 43 | + <div className="flex flex-col gap-1 py-2.5"> |
| 44 | + <div className="flex flex-col text-xs @container"> |
| 45 | + <MetricRow label="Tool" value={data.toolName} /> |
| 46 | + {data.toolCallId && ( |
| 47 | + <MetricRow |
| 48 | + label="Call ID" |
| 49 | + value={<TruncatedCopyableValue value={data.toolCallId} />} |
| 50 | + /> |
| 51 | + )} |
| 52 | + <MetricRow label="Duration" value={formatDuration(data.durationMs)} /> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + |
| 56 | + {/* Input args */} |
| 57 | + {data.args && ( |
| 58 | + <div className="flex flex-col gap-1.5 py-2.5"> |
| 59 | + <Header3>Input</Header3> |
| 60 | + <CodeBlock |
| 61 | + code={tryPrettyJson(data.args)} |
| 62 | + maxLines={20} |
| 63 | + showLineNumbers={false} |
| 64 | + showCopyButton |
| 65 | + language="json" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +function MetricRow({ label, value }: { label: string; value: React.ReactNode }) { |
| 76 | + return ( |
| 77 | + <div className="grid h-7 grid-cols-[1fr_auto] items-center gap-4 rounded-sm px-1.5 transition odd:bg-charcoal-750/40 @[28rem]:grid-cols-[8rem_1fr] hover:bg-white/[0.04]"> |
| 78 | + <span className="text-text-dimmed">{label}</span> |
| 79 | + <span className="text-right text-text-bright @[28rem]:text-left">{value}</span> |
| 80 | + </div> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +function formatDuration(ms: number): string { |
| 85 | + if (ms < 1000) return `${Math.round(ms)}ms`; |
| 86 | + if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`; |
| 87 | + const mins = Math.floor(ms / 60_000); |
| 88 | + const secs = ((ms % 60_000) / 1000).toFixed(0); |
| 89 | + return `${mins}m ${secs}s`; |
| 90 | +} |
| 91 | + |
| 92 | +function tryPrettyJson(value: string): string { |
| 93 | + try { |
| 94 | + return JSON.stringify(JSON.parse(value), null, 2); |
| 95 | + } catch { |
| 96 | + return value; |
| 97 | + } |
| 98 | +} |
0 commit comments