The platform for continuously improving AI agents.
import { useMemo, useState } from "react";
import type { JsonObject } from "@bufbuild/protobuf";
import { SpanNavigator } from "@/components/otel/span-navigator";
import { orderedSpans } from "@/components/otel/span-waterfall";
import { SplitPane } from "@/components/otel/split-pane";
import { StatusBadge } from "@/components/otel/status-badge";
import {
formatCost,
formatDuration,
formatTime,
formatTokens,
listJson,
spanKind,
} from "@/lib/otel-rpc/format";
import type { Span } from "@/lib/otel-rpc/gen/v1/trace_pb";
export function TraceView({ spans }: { spans: Span[] }) {
const ordered = useMemo(() => orderedSpans(spans), [spans]);
const [selectedId, setSelectedId] = useState(ordered[0]?.spanId);
const selected = spans.find((span) => span.spanId === selectedId) ?? ordered[0];
return (
<SplitPane
className="h-full min-h-0 border-t border-border"
defaultWidth={280}
end={selected ? <SpanDetail span={selected} /> : null}
maxWidth={480}
minWidth={200}
start={<SpanNavigator onSelect={setSelectedId} selectedId={selected?.spanId} spans={spans} />}
storageKey="trace-sheet-panes"
/>
);
}
function SpanDetail({ span }: { span: Span }) {
const events = listJson(span.events);
return (
<div className="h-full overflow-y-auto bg-background">
<div className="flex items-start justify-between gap-3 border-b border-border px-4 py-3">
<div className="min-w-0">
<h2 className="truncate text-sm font-medium text-foreground">{span.name}</h2>
<p className="mt-1 truncate font-mono text-xs text-muted-foreground">{span.spanId}</p>
</div>
<StatusBadge code={span.statusCode} />
</div>
<div className="grid gap-4 px-4 py-4">
<dl className="grid gap-3 text-sm sm:grid-cols-4">
<Fact label="Service" value={span.serviceName || "—"} />
<Fact label="Start" value={formatTime(span.startTimeNs)} />
<Fact label="Duration" value={formatDuration(span.durationNs)} />
<Fact label="Kind" value={spanKind(span.kind)} />
</dl>
{span.statusMessage ? (
<p className="text-sm text-destructive">{span.statusMessage}</p>
) : null}
<AttributeList title="Attributes" value={span.attributes} />
<AttributeList title="Resource" value={span.resourceAttributes} />
{events.length > 0 ? (
<section>
<h3 className="pixel-label mb-2 text-xs text-muted-foreground">Events</h3>
<pre className="overflow-auto rounded-sm border border-border/70 bg-code-surface p-3 font-mono text-xs text-code-fg">
{JSON.stringify(events, null, 2)}
</pre>
</section>
) : null}
</div>
</div>
);
}
function Fact({ label, value }: { label: string; value: string }) {
return (
<div>
<dt className="pixel-label text-xs text-muted-foreground">{label}</dt>
<dd className="mt-1 truncate text-foreground">{value}</dd>
</div>
);
}
function AttributeList({ title, value }: { title: string; value: JsonObject | undefined }) {
const entries = Object.entries(value ?? {}).sort(([a], [b]) => a.localeCompare(b));
if (entries.length === 0) return null;
return (
<section>
<h3 className="pixel-label mb-2 text-xs text-muted-foreground">{title}</h3>
<dl className="grid gap-2 text-sm">
{entries.map(([key, entry]) => (
<div className="grid grid-cols-[minmax(8rem,16rem)_1fr] gap-3" key={key}>
<dt className="truncate font-mono text-xs text-muted-foreground">{key}</dt>
<dd className="font-mono text-xs break-all text-foreground">
{formatAttribute(entry)}
</dd>
</div>
))}
</dl>
</section>
);
}
function formatAttribute(value: unknown): string {
if (value === null || value === undefined) return "null";
if (typeof value === "string") return value;
return JSON.stringify(value);
}
export function usageLine(
tokens: bigint | undefined,
cost: number | undefined,
model: string | undefined
) {
return `${formatTokens(tokens)} tokens · ${formatCost(cost)}${model ? ` · ${model}` : ""}`;
}