Open History to see the last commit
1import type { OrchestrationV2TurnItem, ThreadTokenUsageSnapshot } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3
4function asFiniteNumber(value: unknown): number | null {
5 return typeof value === "number" && Number.isFinite(value) ? value : null;
6}
7
8type NullableContextWindowUsage = {
9 readonly [Key in keyof ThreadTokenUsageSnapshot]: undefined extends ThreadTokenUsageSnapshot[Key]
10 ? Exclude<ThreadTokenUsageSnapshot[Key], undefined> | null
11 : ThreadTokenUsageSnapshot[Key];
12};
13
14export type ContextWindowSnapshot = NullableContextWindowUsage & {
15 readonly remainingTokens: number | null;
16 readonly usedPercentage: number | null;
17 readonly remainingPercentage: number | null;
18 readonly updatedAt: string;
19};
20
21/** Map a provider driver kind to a user-facing display name. */
22export function formatProviderDisplayName(provider: string | null | undefined): string {
23 if (!provider) return "This agent";
24 switch (provider) {
25 case "claudeAgent":
26 case "claude":
27 return "Claude";
28 case "codex":
29 return "Codex";
30 case "cursor":
31 return "Cursor";
32 case "opencode":
33 return "OpenCode";
34 default: {
35 // Title-case unknown driver kinds so they read reasonably.
36 const trimmed = provider.replace(/Agent$/i, "").trim();
37 if (trimmed.length === 0) return provider;
38 return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
39 }
40 }
41}
42
43export function deriveLatestContextWindowSnapshot(
44 entries: ReadonlyArray<{
45 readonly item: OrchestrationV2TurnItem;
46 }>,
47): ContextWindowSnapshot | null {
48 for (let index = entries.length - 1; index >= 0; index -= 1) {
49 const entry = entries[index];
50 if (!entry || entry.item.type !== "compaction") {
51 continue;
52 }
53 const payload = entry.item;
54 const usedTokens = asFiniteNumber(payload.afterTokenCount);
55 if (usedTokens === null || usedTokens < 0) {
56 continue;
57 }
58
59 const maxTokens = null;
60 const usedPercentage =
61 maxTokens !== null && maxTokens > 0 ? Math.min(100, (usedTokens / maxTokens) * 100) : null;
62 const remainingTokens =
63 maxTokens !== null ? Math.max(0, Math.round(maxTokens - usedTokens)) : null;
64 const remainingPercentage = usedPercentage !== null ? Math.max(0, 100 - usedPercentage) : null;
65
66 return {
67 usedTokens,
68 totalProcessedTokens: asFiniteNumber(payload.beforeTokenCount),
69 maxTokens,
70 remainingTokens,
71 usedPercentage,
72 remainingPercentage,
73 inputTokens: null,
74 cachedInputTokens: null,
75 outputTokens: null,
76 reasoningOutputTokens: null,
77 lastUsedTokens: null,
78 lastInputTokens: null,
79 lastCachedInputTokens: null,
80 lastOutputTokens: null,
81 lastReasoningOutputTokens: null,
82 toolUses: null,
83 durationMs: null,
84 compactsAutomatically: true,
85 updatedAt: DateTime.formatIso(payload.startedAt ?? payload.updatedAt),
86 };
87 }
88
89 return null;
90}
91
92export function formatContextWindowTokens(value: number | null): string {
93 if (value === null || !Number.isFinite(value)) {
94 return "0";
95 }
96 if (value < 1_000) {
97 return `${Math.round(value)}`;
98 }
99 if (value < 10_000) {
100 return `${(value / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
101 }
102 if (value < 1_000_000) {
103 return `${Math.round(value / 1_000)}k`;
104 }
105 return `${(value / 1_000_000).toFixed(1).replace(/\.0$/, "")}m`;
106}