Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestrationTiming.ts

83 lines2.8 KB

1import { isOrchestrationV2WorkActive, type OrchestrationV2Subagent } from "@t3tools/contracts";
2​
3export function deriveSubagentElapsedMs(
4 agent: {
5 readonly status: OrchestrationV2Subagent["status"];
6 readonly startedAt: string | null;
7 readonly completedAt: string | null;
8 },
9 nowMs: number,
10): number | null {
11 if (agent.startedAt === null) return null;
12 const end = isOrchestrationV2WorkActive(agent.status)
13 ? nowMs
14 : agent.completedAt === null
15 ? null
16 : Date.parse(agent.completedAt);
17 if (end === null) return null;
18 const start = Date.parse(agent.startedAt);
19 return Number.isFinite(start) && Number.isFinite(end) ? Math.max(0, end - start) : null;
21​
22type LatestRunTiming = {
23 readonly runId: string | null;
24 readonly requestedAt?: string | null;
25 readonly startedAt: string | null;
26 readonly completedAt: string | null;
27};
28​
29type RuntimeActivityState = {
30 readonly orchestrationStatus: string;
31 readonly activeRunId?: string | null;
32};
33​
34export function formatDuration(durationMs: number): string {
35 if (!Number.isFinite(durationMs) || durationMs < 0) return "0ms";
36 if (durationMs < 1_000) return `${Math.max(1, Math.round(durationMs))}ms`;
37 if (durationMs < 10_000) {
38 const tenths = Math.round(durationMs / 100) / 10;
39 return tenths >= 10 ? "10s" : `${tenths.toFixed(1)}s`;
40 }
41 const totalSeconds = Math.round(durationMs / 1_000);
42 // Choose the unit after rounding so 59.5 seconds never renders as "60s".
43 if (totalSeconds < 60) return `${totalSeconds}s`;
44 const hours = Math.floor(totalSeconds / 3_600);
45 const minutes = Math.floor((totalSeconds % 3_600) / 60);
46 const seconds = totalSeconds % 60;
47 const parts: string[] = [];
48 if (hours > 0) parts.push(`${hours}h`);
49 if (minutes > 0) parts.push(`${minutes}m`);
50 if (seconds > 0) parts.push(`${seconds}s`);
51 return parts.join(" ");
53​
54function isLatestRunSettled(
55 latestRun: LatestRunTiming | null,
56 runtime: RuntimeActivityState | null,
57): boolean {
58 if (!latestRun) return false;
59 if (!latestRun.completedAt) return false;
60 if (!runtime) return true;
61 if (runtime.orchestrationStatus === "running") return false;
62 return true;
64​
65/**
66 * The projector only stamps `startedAt` in the same update that moves the
67 * session to "running", so while the provider spins up (session "starting") a
68 * requested turn has no `startedAt` at all — and returning null there blinks
69 * the indicator out for the whole spin-up.
70 */
71export function deriveActiveWorkStartedAt(
72 latestRun: LatestRunTiming | null,
73 runtime: RuntimeActivityState | null,
74 sendStartedAt: string | null,
75): string | null {
76 if (runtime?.activeRunId && runtime.activeRunId !== latestRun?.runId) {
77 return sendStartedAt;
78 }
79 if (!isLatestRunSettled(latestRun, runtime)) {
80 return latestRun?.startedAt ?? latestRun?.requestedAt ?? sendStartedAt;
81 }
82 return sendStartedAt;