Open History to see the last commit
1import type { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
2import {
3 resolveSelectableProviderInstanceEntry,
4 type ProviderInstanceEntry,
5} from "../../providerInstances";
6
7function providerSupportsManualCompaction(
8 provider: ProviderInstanceEntry | null | undefined,
9): boolean {
10 return provider?.snapshot.slashCommands.some((command) => command.name === "compact") ?? false;
11}
12
13export function hasAvailableCompactionProvider(input: {
14 readonly providers: ReadonlyArray<ProviderInstanceEntry>;
15 readonly driverKind: ProviderDriverKind;
16 readonly instanceId: ProviderInstanceId | null;
17 readonly lockedInstanceId: ProviderInstanceId | null;
18}): boolean {
19 const driverProviders = input.providers.filter(
20 (provider) => provider.driverKind === input.driverKind,
21 );
22 const lockedContinuationGroupKey = input.lockedInstanceId
23 ? driverProviders.find((provider) => provider.instanceId === input.lockedInstanceId)
24 ?.continuationGroupKey
25 : undefined;
26 const compatibleProviders = lockedContinuationGroupKey
27 ? driverProviders.filter(
28 (provider) => provider.continuationGroupKey === lockedContinuationGroupKey,
29 )
30 : driverProviders;
31
32 return providerSupportsManualCompaction(
33 resolveSelectableProviderInstanceEntry(compatibleProviders, input.instanceId ?? undefined),
34 );
35}
36
37export function formatContextWindowCompactionMessage(autoCompactThreshold?: number | null): string {
38 if (typeof autoCompactThreshold === "number" && autoCompactThreshold > 0) {
39 return `Compacts automatically at about ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
40 }
41 return "Context compacts automatically when needed.";
42}