Files
t3code/apps/web/src/components/chat/ContextWindowMeter.logic.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

146 lines
5.2 KiB
TypeScript

import type { ModelSelection, ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
import {
CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
isClaudeResumeCompactionQuestion,
} from "@t3tools/shared/claudeCompaction";
import {
resolveSelectableProviderInstanceEntry,
type ProviderInstanceEntry,
} from "../../providerInstances";
import { getTriggerDisplayModelName, type ModelEsque } from "./providerIconUtils";
const CLAUDE_RESUME_COMPACTION_MINUTES = 70;
const CLAUDE_RESUME_COMPACTION_TOKENS = 100_000;
export function providerSupportsManualCompaction(
provider: ProviderInstanceEntry | null | undefined,
): boolean {
return provider?.snapshot.slashCommands.some((command) => command.name === "compact") ?? false;
}
export function hasAvailableCompactionProvider(input: {
readonly providers: ReadonlyArray<ProviderInstanceEntry>;
readonly driverKind: ProviderDriverKind;
readonly instanceId: ProviderInstanceId | null;
readonly lockedInstanceId: ProviderInstanceId | null;
}): boolean {
const driverProviders = input.providers.filter(
(provider) => provider.driverKind === input.driverKind,
);
const lockedContinuationGroupKey = input.lockedInstanceId
? driverProviders.find((provider) => provider.instanceId === input.lockedInstanceId)
?.continuationGroupKey
: undefined;
const compatibleProviders = lockedContinuationGroupKey
? driverProviders.filter(
(provider) => provider.continuationGroupKey === lockedContinuationGroupKey,
)
: driverProviders;
return providerSupportsManualCompaction(
resolveSelectableProviderInstanceEntry(compatibleProviders, input.instanceId ?? undefined),
);
}
export function hasDismissedResumeCompaction(
activities: ReadonlyArray<{ readonly kind: string; readonly payload: unknown }>,
): boolean {
return activities.some((activity) => {
if (activity.kind !== "user-input.resolved") return false;
const payload = activity.payload;
if (!payload || typeof payload !== "object") return false;
const answers = (payload as { readonly answers?: unknown }).answers;
if (!answers || typeof answers !== "object" || Array.isArray(answers)) return false;
return Object.entries(answers).some(
([question, answer]) =>
isClaudeResumeCompactionQuestion(question) &&
answer === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
);
});
}
export function shouldOfferResumeCompaction(input: {
readonly provider: string | null | undefined;
readonly usedTokens: number | null | undefined;
readonly updatedAt: string | null | undefined;
readonly now: string;
}): boolean {
if (
input.provider !== "claudeAgent" ||
(input.usedTokens ?? 0) < CLAUDE_RESUME_COMPACTION_TOKENS
) {
return false;
}
const updatedAt = Date.parse(input.updatedAt ?? "");
const now = Date.parse(input.now);
return (
Number.isFinite(updatedAt) &&
Number.isFinite(now) &&
now - updatedAt >= CLAUDE_RESUME_COMPACTION_MINUTES * 60_000
);
}
export function resolveContextWindowModelDisplayName(
selection: ModelSelection | null | undefined,
modelOptionsByInstance: ReadonlyMap<ProviderInstanceId, ReadonlyArray<ModelEsque>>,
): string | null {
if (!selection) {
return null;
}
const selectedModel = modelOptionsByInstance
.get(selection.instanceId)
?.find((model) => model.slug === selection.model);
return selectedModel ? getTriggerDisplayModelName(selectedModel) : selection.model;
}
export function formatContextWindowCompactionMessage(
modelDisplayName: string | null | undefined,
autoCompactThreshold?: number | null,
): string {
if (typeof autoCompactThreshold === "number" && autoCompactThreshold > 0) {
return `Compacts automatically at ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
}
return modelDisplayName
? `Context for ${modelDisplayName} compacts automatically when needed.`
: "Context compacts automatically when needed.";
}
/**
* Whether the footer should hold the meter's slot before a snapshot exists.
*
* The snapshot comes from thread activities, which load after the shell.
* Reserving the slot while the detail loads, for a started thread, keeps the
* attach button still until the meter mounts. Once the detail is in, a
* missing snapshot means there is no usage to show and nothing is reserved.
*
* The meter renders from stored activities whatever the provider's state, so
* only a provider known not to stream usage skips the reservation. An unknown
* provider (catalog still loading, or the thread's provider disabled) reserves.
*/
export function shouldReserveContextWindowMeter(input: {
readonly meterEnabled: boolean;
readonly detailLoading: boolean;
readonly threadStarted: boolean;
/** `null` while the thread's provider is not in the catalog. */
readonly providerReportsContextWindow: boolean | null;
}): boolean {
return (
input.meterEnabled &&
input.detailLoading &&
input.threadStarted &&
input.providerReportsContextWindow !== false
);
}
export function formatContextWindowCost(cost: {
readonly amount: number;
readonly currency: string;
}): string {
const fractionDigits = Math.abs(cost.amount) > 0 && Math.abs(cost.amount) < 0.01 ? 4 : 2;
return `${cost.currency} ${cost.amount.toFixed(fractionDigits)}`;
}