Spaces:
Sleeping
Sleeping
File size: 7,253 Bytes
1d2a779 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
"use client";
import { useMemo } from "react";
type ToolInvocation = {
tool: string;
startTime: number;
endTime: number;
latency: number;
status: "success" | "error";
resultCount?: number;
};
type ToolTimelineProps = {
toolTraces?: Array<Record<string, any>>;
reasoningTrace?: Array<Record<string, any>>;
};
export function ToolTimeline({ toolTraces = [], reasoningTrace = [] }: ToolTimelineProps) {
const timelineData = useMemo(() => {
if (!toolTraces || toolTraces.length === 0) {
return [];
}
// Extract tool invocations from traces
const invocations: ToolInvocation[] = toolTraces.map((trace, idx) => {
const tool = trace.tool || trace.tool_name || "unknown";
const startTime = trace.start_time || trace.timestamp || idx * 100; // Fallback timing
const latency = trace.latency_ms || trace.latency || 0;
const endTime = startTime + latency;
const status = trace.status === "error" || trace.error ? "error" : "success";
const resultCount = trace.result_count || trace.hits || trace.results?.length || 0;
return {
tool,
startTime,
endTime,
latency,
status,
resultCount,
};
});
// Sort by start time
invocations.sort((a, b) => a.startTime - b.startTime);
// Calculate timeline bounds
const minTime = Math.min(...invocations.map((i) => i.startTime), 0);
const maxTime = Math.max(...invocations.map((i) => i.endTime), minTime + 1000);
// Normalize times to 0-100% for visualization
const timeRange = maxTime - minTime || 1000;
return invocations.map((inv) => ({
...inv,
startPercent: ((inv.startTime - minTime) / timeRange) * 100,
widthPercent: (inv.latency / timeRange) * 100,
}));
}, [toolTraces]);
if (timelineData.length === 0) {
return (
<div className="rounded-2xl border border-white/10 bg-slate-950/40 p-6">
<p className="text-sm text-slate-400 text-center">
No tool invocations yet. Tools will appear here as they are executed.
</p>
</div>
);
}
const totalLatency = timelineData.reduce((sum, inv) => sum + inv.latency, 0);
const maxLatency = Math.max(...timelineData.map((inv) => inv.latency), 0);
return (
<div className="rounded-2xl border border-white/10 bg-slate-950/40 p-6">
<div className="mb-4 flex items-center justify-between">
<h3 className="text-lg font-semibold text-white">Tool Invocation Timeline</h3>
<div className="text-xs text-slate-400">
<span className="text-cyan-300">{timelineData.length}</span> tools ·{" "}
<span className="text-emerald-300">{totalLatency}ms</span> total
</div>
</div>
{/* Timeline visualization */}
<div className="relative mb-6 h-32 rounded-xl border border-white/10 bg-slate-900/50 p-4">
{/* Time axis */}
<div className="absolute inset-x-4 top-2 flex justify-between text-xs text-slate-500">
<span>0ms</span>
<span>{maxLatency}ms</span>
</div>
{/* Tool bars */}
<div className="relative mt-8 h-full">
{timelineData.map((inv, idx) => {
const height = 24;
const top = idx * (height + 8);
const color =
inv.status === "error"
? "bg-red-500/60 border-red-400"
: "bg-cyan-500/60 border-cyan-400";
return (
<div key={idx} className="absolute inset-x-0" style={{ top: `${top}px` }}>
{/* Tool label */}
<div className="absolute -left-24 top-0 flex h-6 w-20 items-center justify-end pr-2 text-xs text-slate-300">
<span className="truncate font-medium">{inv.tool}</span>
</div>
{/* Timeline bar */}
<div
className={`relative h-6 rounded border transition-all hover:opacity-100 ${
inv.status === "error" ? "opacity-80" : "opacity-70"
} ${color}`}
style={{
left: `${inv.startPercent}%`,
width: `${Math.max(inv.widthPercent, 2)}%`,
}}
title={`${inv.tool}: ${inv.latency}ms (${inv.status})`}
>
{/* Latency label */}
{inv.widthPercent > 5 && (
<span className="absolute inset-0 flex items-center justify-center text-[10px] font-semibold text-white">
{inv.latency}ms
</span>
)}
{/* Result count badge */}
{inv.resultCount > 0 && inv.widthPercent > 8 && (
<span className="absolute -right-2 -top-1 flex h-4 w-4 items-center justify-center rounded-full bg-emerald-500 text-[8px] font-bold text-white">
{inv.resultCount > 9 ? "9+" : inv.resultCount}
</span>
)}
{/* Error indicator */}
{inv.status === "error" && (
<span className="absolute -right-1 -top-1 text-red-400">⚠️</span>
)}
</div>
</div>
);
})}
</div>
</div>
{/* Tool list */}
<div className="space-y-2">
{timelineData.map((inv, idx) => (
<div
key={idx}
className="flex items-center justify-between rounded-lg border border-white/5 bg-white/5 px-4 py-2"
>
<div className="flex items-center gap-3">
<div
className={`h-3 w-3 rounded-full ${
inv.status === "error" ? "bg-red-500" : "bg-emerald-500"
}`}
/>
<span className="font-medium text-white">{inv.tool}</span>
{inv.resultCount > 0 && (
<span className="text-xs text-slate-400">
{inv.resultCount} result{inv.resultCount !== 1 ? "s" : ""}
</span>
)}
</div>
<div className="flex items-center gap-4 text-sm">
<span className="text-slate-400">{inv.latency}ms</span>
{inv.status === "error" && (
<span className="text-red-400">Error</span>
)}
</div>
</div>
))}
</div>
{/* Summary stats */}
<div className="mt-4 grid grid-cols-3 gap-3 rounded-lg border border-white/5 bg-white/5 p-3">
<div className="text-center">
<p className="text-xs text-slate-400">Total Tools</p>
<p className="text-lg font-semibold text-white">{timelineData.length}</p>
</div>
<div className="text-center">
<p className="text-xs text-slate-400">Total Time</p>
<p className="text-lg font-semibold text-emerald-300">{totalLatency}ms</p>
</div>
<div className="text-center">
<p className="text-xs text-slate-400">Avg Latency</p>
<p className="text-lg font-semibold text-cyan-300">
{Math.round(totalLatency / timelineData.length)}ms
</p>
</div>
</div>
</div>
);
}
|