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

orchestrationTiming.ts

51 lines1.7 KB

1type LatestTurnTiming = {
2 readonly turnId: string | null;
3 readonly startedAt: string | null;
4 readonly completedAt: string | null;
5};
6​
7type SessionActivityState = {
8 readonly orchestrationStatus: string;
9 readonly activeTurnId?: string | null;
10};
11​
12export function formatDuration(durationMs: number): string {
13 if (!Number.isFinite(durationMs) || durationMs < 0) return "0ms";
14 if (durationMs < 1_000) return `${Math.max(1, Math.round(durationMs))}ms`;
15 if (durationMs < 10_000) {
16 const tenths = Math.round(durationMs / 100) / 10;
17 return tenths >= 10 ? "10s" : `${tenths.toFixed(1)}s`;
18 }
19 if (durationMs < 60_000) return `${Math.round(durationMs / 1_000)}s`;
20 const totalSeconds = Math.round(durationMs / 1_000);
21 const hours = Math.floor(totalSeconds / 3_600);
22 const minutes = Math.floor((totalSeconds % 3_600) / 60);
23 const seconds = totalSeconds % 60;
24 const parts: string[] = [];
25 if (hours > 0) parts.push(`${hours}h`);
26 if (minutes > 0) parts.push(`${minutes}m`);
27 if (seconds > 0) parts.push(`${seconds}s`);
28 return parts.join(" ");
30​
31function isLatestTurnSettled(
32 latestTurn: LatestTurnTiming | null,
33 session: SessionActivityState | null,
34): boolean {
35 if (!latestTurn?.startedAt) return false;
36 if (!latestTurn.completedAt) return false;
37 if (!session) return true;
38 if (session.orchestrationStatus === "running") return false;
39 return true;
41​
42export function deriveActiveWorkStartedAt(
43 latestTurn: LatestTurnTiming | null,
44 session: SessionActivityState | null,
45 sendStartedAt: string | null,
46): string | null {
47 if (!isLatestTurnSettled(latestTurn, session)) {
48 return latestTurn?.startedAt ?? sendStartedAt;
49 }
50 return sendStartedAt;