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

contextWindow.ts

144 lines4.7 KB

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