Open History to see the last commit
1import type { ModelSelection, ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
2import {
3 CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
4 isClaudeResumeCompactionQuestion,
5} from "@t3tools/shared/claudeCompaction";
6import {
7 resolveSelectableProviderInstanceEntry,
8 type ProviderInstanceEntry,
9} from "../../providerInstances";
10import { getTriggerDisplayModelName, type ModelEsque } from "./providerIconUtils";
11
12const CLAUDE_RESUME_COMPACTION_MINUTES = 70;
13const CLAUDE_RESUME_COMPACTION_TOKENS = 100_000;
14
15export function providerSupportsManualCompaction(
16 provider: ProviderInstanceEntry | null | undefined,
17): boolean {
18 return provider?.snapshot.slashCommands.some((command) => command.name === "compact") ?? false;
19}
20
21export function hasAvailableCompactionProvider(input: {
22 readonly providers: ReadonlyArray<ProviderInstanceEntry>;
23 readonly driverKind: ProviderDriverKind;
24 readonly instanceId: ProviderInstanceId | null;
25 readonly lockedInstanceId: ProviderInstanceId | null;
26}): boolean {
27 const driverProviders = input.providers.filter(
28 (provider) => provider.driverKind === input.driverKind,
29 );
30 const lockedContinuationGroupKey = input.lockedInstanceId
31 ? driverProviders.find((provider) => provider.instanceId === input.lockedInstanceId)
32 ?.continuationGroupKey
33 : undefined;
34 const compatibleProviders = lockedContinuationGroupKey
35 ? driverProviders.filter(
36 (provider) => provider.continuationGroupKey === lockedContinuationGroupKey,
37 )
38 : driverProviders;
39
40 return providerSupportsManualCompaction(
41 resolveSelectableProviderInstanceEntry(compatibleProviders, input.instanceId ?? undefined),
42 );
43}
44
45export function hasDismissedResumeCompaction(
46 activities: ReadonlyArray<{ readonly kind: string; readonly payload: unknown }>,
47): boolean {
48 return activities.some((activity) => {
49 if (activity.kind !== "user-input.resolved") return false;
50 const payload = activity.payload;
51 if (!payload || typeof payload !== "object") return false;
52 const answers = (payload as { readonly answers?: unknown }).answers;
53 if (!answers || typeof answers !== "object" || Array.isArray(answers)) return false;
54
55 return Object.entries(answers).some(
56 ([question, answer]) =>
57 isClaudeResumeCompactionQuestion(question) &&
58 answer === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
59 );
60 });
61}
62
63export function shouldOfferResumeCompaction(input: {
64 readonly provider: string | null | undefined;
65 readonly usedTokens: number | null | undefined;
66 readonly updatedAt: string | null | undefined;
67 readonly now: string;
68}): boolean {
69 if (
70 input.provider !== "claudeAgent" ||
71 (input.usedTokens ?? 0) < CLAUDE_RESUME_COMPACTION_TOKENS
72 ) {
73 return false;
74 }
75
76 const updatedAt = Date.parse(input.updatedAt ?? "");
77 const now = Date.parse(input.now);
78 return (
79 Number.isFinite(updatedAt) &&
80 Number.isFinite(now) &&
81 now - updatedAt >= CLAUDE_RESUME_COMPACTION_MINUTES * 60_000
82 );
83}
84
85export function resolveContextWindowModelDisplayName(
86 selection: ModelSelection | null | undefined,
87 modelOptionsByInstance: ReadonlyMap<ProviderInstanceId, ReadonlyArray<ModelEsque>>,
88): string | null {
89 if (!selection) {
90 return null;
91 }
92
93 const selectedModel = modelOptionsByInstance
94 .get(selection.instanceId)
95 ?.find((model) => model.slug === selection.model);
96
97 return selectedModel ? getTriggerDisplayModelName(selectedModel) : selection.model;
98}
99
100export function formatContextWindowCompactionMessage(
101 modelDisplayName: string | null | undefined,
102 autoCompactThreshold?: number | null,
103): string {
104 if (typeof autoCompactThreshold === "number" && autoCompactThreshold > 0) {
105 return `Compacts automatically at ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
106 }
107 return modelDisplayName
108 ? `Context for ${modelDisplayName} compacts automatically when needed.`
109 : "Context compacts automatically when needed.";
110}
111
112/**
113 * Whether the footer should hold the meter's slot before a snapshot exists.
114 *
115 * The snapshot comes from thread activities, which load after the shell.
116 * Reserving the slot while the detail loads, for a started thread, keeps the
117 * attach button still until the meter mounts. Once the detail is in, a
118 * missing snapshot means there is no usage to show and nothing is reserved.
119 *
120 * The meter renders from stored activities whatever the provider's state, so
121 * only a provider known not to stream usage skips the reservation. An unknown
122 * provider (catalog still loading, or the thread's provider disabled) reserves.
123 */
124export function shouldReserveContextWindowMeter(input: {
125 readonly meterEnabled: boolean;
126 readonly detailLoading: boolean;
127 readonly threadStarted: boolean;
128 /** `null` while the thread's provider is not in the catalog. */
129 readonly providerReportsContextWindow: boolean | null;
130}): boolean {
131 return (
132 input.meterEnabled &&
133 input.detailLoading &&
134 input.threadStarted &&
135 input.providerReportsContextWindow !== false
136 );
137}