Files
archie/ui/src/tasks/timeline-event.ts
T

171 lines
6.2 KiB
TypeScript

/**
* The timeline's vocabulary: one human line per event kind.
*
* The formatters live here rather than inside a component because two surfaces
* read them -- the timeline row and the stage rail's agent reports -- and a
* second copy would eventually disagree with this one.
*/
import { duration } from "../lib/format.ts";
import type { TaskEvent } from "./task-run";
// The one duration formatter lives in lib/format; re-exported so the stage
// rail renders a duration exactly the way the timeline does.
export { duration };
function words(value: unknown): string {
return String(value || "event")
.replaceAll("_", " ")
.replace(/\b\w/g, (letter) => letter.toUpperCase());
}
function count(value: unknown): string {
const n = Number(value);
// Render only for positive counts; zero is treated as "no metric
// reported" so a partially-populated agent_finish (pre-v0.1.24 events,
// or a failed run whose result is the zero Result) does not appear as
// "0 prompt · 0 completion · 0 cached".
if (!Number.isFinite(n) || n <= 0) return "";
return n.toLocaleString("en-US");
}
function text(value: unknown): string {
return typeof value === "string" ? value : "";
}
export interface TimelineLine {
title: string;
detail: string;
/** Set for an event a reader must not skim past. */
tone?: "warn";
/** Records the event names, as dashboard paths. */
links?: { label: string; to: string }[];
}
export function describeTimelineEvent(ev: TaskEvent = {}): TimelineLine {
const data = ev.data || {};
const stage = ev.stage || "unknown stage";
const workflow = ev.workflow ? `${ev.workflow} workflow` : "";
if (ev.kind === "stage_start") {
return { title: `Started stage: ${stage}`, detail: workflow };
}
if (ev.kind === "stage_finish") {
const elapsed = duration(data.duration_ms);
const context = [workflow, elapsed]
.filter(Boolean)
.join(" · ");
return {
title: data.interrupted
? `Stage interrupted: ${stage}`
: data.skipped
? `Skipped stage: ${stage}`
: data.error
? `Stage failed: ${stage}`
: `Finished stage: ${stage}`,
detail: [context, text(data.error), text(data.when)].filter(Boolean).join(" · "),
};
}
if (ev.kind === "alert") {
return {
title: data.state === "resolved" ? "Alert resolved" : "Alert",
detail: ev.detail || "",
};
}
if (ev.kind === "task_retried") {
const retryCount = Number(data.retry_count);
const attempt =
Number.isFinite(retryCount) && retryCount > 0
? `Retry ${retryCount}`
: "Retry requested";
const previous = [
text(data.previous_stage) && `after ${text(data.previous_stage)}`,
text(data.previous_reason),
]
.filter(Boolean)
.join(": ");
return { title: attempt, detail: previous || ev.detail || "" };
}
if (ev.kind === "agent_finish") {
const iterations = count(data.iterations);
const total = count(data.tokens);
const cachedRaw = Number(data.cached_tokens) || 0;
// data.prompt_tokens INCLUDES cache hits -- showing it next to "cached"
// reads as two separate charges when it's really one figure containing
// the other. Show the fresh (full-price) remainder instead, so a
// heavily-cached run doesn't look ~10x more expensive than it was billed.
const fresh = count((Number(data.prompt_tokens) || 0) - cachedRaw);
const completion = count(data.completion_tokens);
const cached = count(data.cached_tokens);
const usage = [
iterations && `${iterations} iterations`,
total && `${total} tokens`,
fresh && `${fresh} fresh prompt`,
completion && `${completion} completion`,
cached && `${cached} cached`,
text(data.model),
].filter(Boolean);
return {
title: `Agent ${text(data.status) || "finished"}: ${stage}`,
detail: usage.join(" · ") || ev.detail || "",
};
}
// Condition (d): the two provenance kinds render as one human line. The
// captured payload is rendered by its own panel, never dumped into the
// timeline, so these cases name what was recorded and nothing more.
if (ev.kind === "remediation_round") {
const of = Number(data.of);
const author = text(data.author);
return {
title: `Remediation ${text(data.round)}${of > 0 ? ` of ${of}` : ""}`,
detail: [author && `answers ${author}'s review`, data.review_id ? `#${text(data.review_id)}` : ""].filter(Boolean).join(" "),
};
}
if (ev.kind === "review_approved") {
const pr = text(data.pr_number);
return { title: "Pull request approved", detail: [text(data.author), pr && `#${pr}`].filter(Boolean).join(" · ") };
}
if (ev.kind === "step_retried") {
return {
title: `Attempt ${text(data.attempt)} of ${text(data.of)} failed: ${ev.stage || "step"}`,
detail: text(data.error),
tone: "warn",
};
}
if (ev.kind === "config_captured") {
return {
title: "Configuration captured",
detail: "The effective task-runtime configuration for this run.",
};
}
if (ev.kind === "binding_started") {
const source = text(data.source);
const capture = text(data.capture_id);
const links = [
{ label: `binding ${text(data.binding_name) || text(data.binding_id)}`, to: "/events/bindings" },
...(capture ? [{ label: "captured event", to: `/events/inspector?capture=${encodeURIComponent(capture)}` }] : []),
];
if (data.unsigned === true)
return { title: "Started by an unsigned event", detail: source ? `source ${source}` : "", tone: "warn", links };
return { title: "Started by an event", detail: source ? `source ${source}` : "", links };
}
if (ev.kind === "changes_captured") {
const totals = (data.totals ?? {}) as { files?: unknown };
const fileCount = Number(totals.files);
const files =
Number.isFinite(fileCount) && fileCount > 0
? `${fileCount} file${fileCount === 1 ? "" : "s"} changed`
: "";
const after = data.captured_after
? `captured after ${text(data.captured_after)}`
: "";
return {
title: "Change capture recorded",
detail: [files, after].filter(Boolean).join(" · "),
};
}
return { title: words(ev.kind || ev.type), detail: ev.detail || "" };
}