Open History to see the last commit
1import type { RuntimeSubagent } from "@t3tools/client-runtime/state/subagentRuntime";
2import { isOrchestrationV2WorkActive } from "@t3tools/contracts";
3import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming";
4import { useEffect, useRef } from "react";
5
6function formatElapsedSeconds(totalSeconds: number): string {
7 const seconds = Math.max(0, Math.floor(totalSeconds));
8 const minutes = Math.floor(seconds / 60);
9 if (minutes === 0) {
10 return `${seconds}s`;
11 }
12 const hours = Math.floor(minutes / 60);
13 if (hours === 0) {
14 return `${minutes}m ${String(seconds % 60).padStart(2, "0")}s`;
15 }
16 return `${hours}h ${String(minutes % 60).padStart(2, "0")}m`;
17}
18
19/**
20 * Elapsed time for the current activation. Live agents self-tick via DOM
21 * writes (zero React commits per tick); settled agents freeze at completedAt.
22 */
23export function AgentElapsed({
24 agent,
25}: {
26 agent: Pick<RuntimeSubagent, "status" | "startedAt" | "completedAt">;
27}) {
28 const textRef = useRef<HTMLSpanElement>(null);
29 const live = isOrchestrationV2WorkActive(agent.status);
30 const startedAt = agent.startedAt;
31 const completedAt = agent.completedAt;
32
33 useEffect(() => {
34 if (!startedAt) {
35 return;
36 }
37 const update = () => {
38 if (textRef.current) {
39 const elapsedMs = deriveSubagentElapsedMs(
40 { status: agent.status, startedAt, completedAt },
41 Date.now(),
42 );
43 textRef.current.textContent =
44 elapsedMs === null ? "" : formatElapsedSeconds(elapsedMs / 1000);
45 }
46 };
47 update();
48 if (!live) return;
49 const id = setInterval(update, 1000);
50 return () => clearInterval(id);
51 }, [live, startedAt, completedAt, agent.status]);
52
53 const elapsedMs = deriveSubagentElapsedMs(agent, 0);
54 if (elapsedMs === null) {
55 return null;
56 }
57 return (
58 <span ref={textRef} className="tabular-nums">
59 {formatElapsedSeconds(elapsedMs / 1000)}
60 </span>
61 );
62}