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

contextWindow.ts

108 lines3.4 KB

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 case "pi":
35 return "Pi";
36 default: {
37 // Title-case unknown driver kinds so they read reasonably.
38 const trimmed = provider.replace(/Agent$/i, "").trim();
39 if (trimmed.length === 0) return provider;
40 return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
41 }
42 }
44​
45export function deriveLatestContextWindowSnapshot(
46 entries: ReadonlyArray<{
47 readonly item: OrchestrationV2TurnItem;
48 }>,
49): ContextWindowSnapshot | null {
50 for (let index = entries.length - 1; index >= 0; index -= 1) {
51 const entry = entries[index];
52 if (!entry || entry.item.type !== "compaction") {
53 continue;
54 }
55 const payload = entry.item;
56 const usedTokens = asFiniteNumber(payload.afterTokenCount);
57 if (usedTokens === null || usedTokens < 0) {
58 continue;
59 }
60​
61 const maxTokens = null;
62 const usedPercentage =
63 maxTokens !== null && maxTokens > 0 ? Math.min(100, (usedTokens / maxTokens) * 100) : null;
64 const remainingTokens =
65 maxTokens !== null ? Math.max(0, Math.round(maxTokens - usedTokens)) : null;
66 const remainingPercentage = usedPercentage !== null ? Math.max(0, 100 - usedPercentage) : null;
67​
68 return {
69 usedTokens,
70 totalProcessedTokens: asFiniteNumber(payload.beforeTokenCount),
71 maxTokens,
72 remainingTokens,
73 usedPercentage,
74 remainingPercentage,
75 inputTokens: null,
76 cachedInputTokens: null,
77 outputTokens: null,
78 reasoningOutputTokens: null,
79 lastUsedTokens: null,
80 lastInputTokens: null,
81 lastCachedInputTokens: null,
82 lastOutputTokens: null,
83 lastReasoningOutputTokens: null,
84 toolUses: null,
85 durationMs: null,
86 compactsAutomatically: true,
87 updatedAt: DateTime.formatIso(payload.startedAt ?? payload.updatedAt),
88 };
89 }
90​
91 return null;
93​
94export function formatContextWindowTokens(value: number | null): string {
95 if (value === null || !Number.isFinite(value)) {
96 return "0";
97 }
98 if (value < 1_000) {
99 return `${Math.round(value)}`;
100 }
101 if (value < 10_000) {
102 return `${(value / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
103 }
104 if (value < 1_000_000) {
105 return `${Math.round(value / 1_000)}k`;
106 }
107 return `${(value / 1_000_000).toFixed(1).replace(/\.0$/, "")}m`;