The loop runner hooks every model call and tool call and reports ordered,
redacted, clipped entries. Each is persisted as an agent_transcript task
event through InsertEvent, left out of the timeline read, and served per
attempt at /api/tasks/{id}/transcript. The step run view shows it with Trace.
219 lines
9.6 KiB
Vue
219 lines
9.6 KiB
Vue
<script setup lang="ts">
|
|
import { CircleAlertIcon, CircleCheckIcon, FoldVerticalIcon, ScrollTextIcon, SparklesIcon, TargetIcon } from "@lucide/vue";
|
|
import { computed, onUnmounted, ref, watch } from "vue";
|
|
|
|
import { Bubble, BubbleContent } from "@/components/ui/bubble";
|
|
import { CodeBlock, CodeBlockContent } from "@/components/ui/code-block";
|
|
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
|
import { Message, MessageContent } from "@/components/ui/message";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import {
|
|
Trace,
|
|
TraceCollapsible,
|
|
TraceCollapsibleContent,
|
|
TraceCollapsibleTrigger,
|
|
TraceDescription,
|
|
TraceIcon,
|
|
TraceLine,
|
|
TraceTitle,
|
|
} from "@/components/ui/trace";
|
|
import ChatMarkdown from "@/chat/ChatMarkdown.vue";
|
|
import { api } from "@/lib/api";
|
|
import { useLiveResource } from "@/stores/session";
|
|
import { duration } from "@/tasks/timeline-event";
|
|
import type { TaskEvent } from "@/tasks/task-run";
|
|
|
|
/** One step's agent conversations for one attempt, oldest first. */
|
|
const props = defineProps<{ taskId: number | string; attempt: number; match: (event: TaskEvent) => boolean; running: boolean }>();
|
|
|
|
interface Usage { prompt_tokens?: number; completion_tokens?: number; cached_tokens?: number }
|
|
interface Entry {
|
|
seq: number;
|
|
kind: string;
|
|
text?: string;
|
|
text_bytes?: number;
|
|
tool_calls?: { id?: string; name: string }[];
|
|
call_id?: string;
|
|
tool?: string;
|
|
args?: string;
|
|
args_bytes?: number;
|
|
output?: string;
|
|
output_bytes?: number;
|
|
error?: string;
|
|
finish_reason?: string;
|
|
usage?: Usage;
|
|
latency_ms?: number;
|
|
}
|
|
interface Step { model: Entry; tools: Entry[] }
|
|
type Row =
|
|
| { kind: "prompt"; title: string; entry: Entry }
|
|
| { kind: "user"; entry: Entry }
|
|
| { kind: "compaction"; entry: Entry }
|
|
| { kind: "step"; number: number; step: Step };
|
|
|
|
const events = ref<TaskEvent[]>();
|
|
const failed = ref(false);
|
|
let generation = 0;
|
|
let timer: ReturnType<typeof setInterval> | undefined;
|
|
|
|
async function load(): Promise<void> {
|
|
const current = ++generation;
|
|
try {
|
|
const view = await api.taskTranscript<{ entries?: TaskEvent[] }>(String(props.taskId), { attempt: props.attempt });
|
|
if (current !== generation) return;
|
|
events.value = view.entries ?? [];
|
|
failed.value = false;
|
|
} catch {
|
|
if (current === generation) failed.value = true;
|
|
}
|
|
}
|
|
|
|
watch(() => [props.taskId, props.attempt], () => { events.value = undefined; void load(); }, { immediate: true });
|
|
watch(() => props.running, (running) => {
|
|
clearInterval(timer);
|
|
if (running) timer = setInterval(() => void load(), 3000);
|
|
}, { immediate: true });
|
|
useLiveResource("tasks", () => void load(), 1000);
|
|
onUnmounted(() => { generation++; clearInterval(timer); });
|
|
|
|
function withClip(text: string | undefined, bytes: number | undefined): string {
|
|
if (!bytes) return text ?? "";
|
|
return `${text ?? ""}\n… clipped, ${(bytes / 1024).toFixed(1)} KB in full`;
|
|
}
|
|
|
|
function usage(entry: Entry): string {
|
|
const u = entry.usage;
|
|
const parts = [];
|
|
if (u?.prompt_tokens) parts.push(`${u.prompt_tokens.toLocaleString()} in`);
|
|
if (u?.cached_tokens) parts.push(`${u.cached_tokens.toLocaleString()} cached`);
|
|
if (u?.completion_tokens) parts.push(`${u.completion_tokens.toLocaleString()} out`);
|
|
if (entry.latency_ms) parts.push(duration(entry.latency_ms));
|
|
return parts.join(" · ");
|
|
}
|
|
|
|
// The row names the call by its main value, as "read src/main.go" rather than
|
|
// its JSON: the first string argument, else nothing.
|
|
function argument(entry: Entry): string {
|
|
try {
|
|
const args: unknown = JSON.parse(entry.args ?? "");
|
|
if (args && typeof args === "object" && !Array.isArray(args)) {
|
|
const first = Object.values(args).find((v) => typeof v === "string" && v);
|
|
if (typeof first === "string") return first.split("\n", 1)[0] ?? "";
|
|
}
|
|
} catch {
|
|
// Clipped or not JSON.
|
|
}
|
|
return "";
|
|
}
|
|
|
|
// Tool results sit under the step whose response asked for them.
|
|
function rows(entries: Entry[]): Row[] {
|
|
const out: Row[] = [];
|
|
const owner = new Map<string, Step>();
|
|
let prompted = false;
|
|
let steps = 0;
|
|
for (const entry of entries) {
|
|
if (entry.kind === "system") out.push({ kind: "prompt", title: "System prompt", entry });
|
|
else if (entry.kind === "user") {
|
|
out.push(prompted ? { kind: "user", entry } : { kind: "prompt", title: "Mission", entry });
|
|
prompted = true;
|
|
} else if (entry.kind === "compaction") out.push({ kind: "compaction", entry });
|
|
else if (entry.kind === "model") {
|
|
const step: Step = { model: entry, tools: [] };
|
|
for (const call of entry.tool_calls ?? []) if (call.id) owner.set(call.id, step);
|
|
out.push({ kind: "step", number: ++steps, step });
|
|
} else if (entry.kind === "tool") {
|
|
const step = entry.call_id ? owner.get(entry.call_id) : undefined;
|
|
if (step) step.tools.push(entry);
|
|
else out.push({ kind: "step", number: ++steps, step: { model: { seq: entry.seq, kind: "model" }, tools: [entry] } });
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// A step can hold several agents (parallel branches, retries); each keeps its
|
|
// own conversation, numbered from 1.
|
|
const conversations = computed(() => {
|
|
const byAgent = new Map<string, Entry[][]>();
|
|
for (const event of (events.value ?? []).filter(props.match)) {
|
|
const key = [event.data?.branch, event.stage].filter(Boolean).join("/");
|
|
const entry = event.data as unknown as Entry;
|
|
const runs = byAgent.get(key) ?? [];
|
|
if (!runs.length || entry.seq === 1) runs.push([]);
|
|
runs.at(-1)!.push(entry);
|
|
byAgent.set(key, runs);
|
|
}
|
|
return [...byAgent.entries()].flatMap(([label, runs]) => runs.map((entries, i) => ({ key: `${label}#${i}`, label, rows: rows(entries) })));
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<p v-if="failed" role="alert" class="text-danger">Could not load the transcript</p>
|
|
<Spinner v-else-if="!events" />
|
|
<Empty v-else-if="!conversations.length"><EmptyHeader><EmptyTitle>No transcript recorded</EmptyTitle></EmptyHeader></Empty>
|
|
<div v-for="conversation in conversations" :key="conversation.key" class="flex flex-col gap-4">
|
|
<h4 v-if="conversations.length > 1" class="font-mono text-xs">{{ conversation.label }}</h4>
|
|
<template v-for="row in conversation.rows" :key="`${row.kind}-${row.kind === 'step' ? row.number : row.entry.seq}`">
|
|
<Trace v-if="row.kind === 'prompt' || row.kind === 'compaction'">
|
|
<TraceCollapsible>
|
|
<TraceCollapsibleTrigger>
|
|
<TraceLine>
|
|
<TraceIcon>
|
|
<FoldVerticalIcon v-if="row.kind === 'compaction'" />
|
|
<TargetIcon v-else-if="row.title === 'Mission'" />
|
|
<ScrollTextIcon v-else />
|
|
</TraceIcon>
|
|
<TraceTitle>{{ row.kind === 'compaction' ? 'Compacted the history' : row.title }}</TraceTitle>
|
|
<TraceDescription v-if="row.kind === 'compaction' && usage(row.entry)">{{ usage(row.entry) }}</TraceDescription>
|
|
</TraceLine>
|
|
</TraceCollapsibleTrigger>
|
|
<TraceCollapsibleContent>
|
|
<CodeBlock size="sm">
|
|
<CodeBlockContent wrap class="max-h-96">{{ withClip(row.entry.text || row.entry.error, row.entry.text_bytes) }}</CodeBlockContent>
|
|
</CodeBlock>
|
|
</TraceCollapsibleContent>
|
|
</TraceCollapsible>
|
|
</Trace>
|
|
<Message v-else-if="row.kind === 'user'" align="end">
|
|
<MessageContent>
|
|
<Bubble align="end" variant="muted">
|
|
<BubbleContent class="whitespace-pre-wrap [overflow-wrap:anywhere]">{{ withClip(row.entry.text, row.entry.text_bytes) }}</BubbleContent>
|
|
</Bubble>
|
|
</MessageContent>
|
|
</Message>
|
|
<Message v-else>
|
|
<MessageContent class="gap-2">
|
|
<ChatMarkdown v-if="row.step.model.text" :text="withClip(row.step.model.text, row.step.model.text_bytes)" />
|
|
<Trace>
|
|
<TraceLine :status="row.step.model.error ? 'error' : undefined">
|
|
<TraceIcon><CircleAlertIcon v-if="row.step.model.error" /><SparklesIcon v-else /></TraceIcon>
|
|
<TraceTitle>{{ row.step.model.error ? `Step ${row.number} failed: ${row.step.model.error}` : `Step ${row.number}` }}</TraceTitle>
|
|
<TraceDescription v-if="usage(row.step.model)">{{ usage(row.step.model) }}</TraceDescription>
|
|
</TraceLine>
|
|
<TraceCollapsible v-for="tool in row.step.tools" :key="tool.seq" :default-open="!!tool.error">
|
|
<TraceCollapsibleTrigger>
|
|
<TraceLine :status="tool.error ? 'error' : undefined">
|
|
<TraceIcon><CircleAlertIcon v-if="tool.error" /><CircleCheckIcon v-else /></TraceIcon>
|
|
<TraceTitle>{{ tool.error ? 'Failed to run' : 'Ran' }} <span class="font-mono">{{ tool.tool }}</span> {{ argument(tool) }}</TraceTitle>
|
|
<TraceDescription v-if="tool.latency_ms">{{ duration(tool.latency_ms) }}</TraceDescription>
|
|
</TraceLine>
|
|
</TraceCollapsibleTrigger>
|
|
<TraceCollapsibleContent class="flex flex-col gap-2">
|
|
<CodeBlock size="sm">
|
|
<CodeBlockContent wrap class="max-h-48">{{ withClip(tool.args, tool.args_bytes) }}</CodeBlockContent>
|
|
</CodeBlock>
|
|
<CodeBlock v-if="tool.error" size="sm" variant="destructive">
|
|
<CodeBlockContent wrap class="max-h-64">{{ tool.error }}</CodeBlockContent>
|
|
</CodeBlock>
|
|
<CodeBlock v-if="tool.output || !tool.error" size="sm">
|
|
<CodeBlockContent wrap class="max-h-64">{{ withClip(tool.output, tool.output_bytes) }}</CodeBlockContent>
|
|
</CodeBlock>
|
|
</TraceCollapsibleContent>
|
|
</TraceCollapsible>
|
|
</Trace>
|
|
</MessageContent>
|
|
</Message>
|
|
</template>
|
|
</div>
|
|
</template>
|