Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
99 lines
3.4 KiB
TypeScript
99 lines
3.4 KiB
TypeScript
import type { ModelMessage } from 'ai';
|
|
import {
|
|
estimateModelMessagesTokensWithKind,
|
|
estimateTextTokens,
|
|
estimateUnknownTokens,
|
|
} from './tokenEstimator';
|
|
|
|
export const COMPACTION_PROMPT_RESERVE = 150;
|
|
export const AUTO_COMPACT_BUFFER_CAP = 15_000;
|
|
export const AUTO_COMPACT_BUFFER_RATIO = 0.8;
|
|
export const COMPACTION_SUMMARY_MAX_OUTPUT_TOKENS = 1600;
|
|
export const DEFAULT_MAX_OUTPUT_TOKENS = 4096;
|
|
const MIN_OUTPUT_RESERVE = 256;
|
|
const MAX_OUTPUT_SHARE_OF_WINDOW = 0.25;
|
|
|
|
export function resolveEffectiveMaxOutputTokens(
|
|
contextWindow: number,
|
|
maxOutputTokens: number = DEFAULT_MAX_OUTPUT_TOKENS,
|
|
): number {
|
|
if (contextWindow <= 0) return maxOutputTokens;
|
|
const cappedByWindow = Math.max(
|
|
MIN_OUTPUT_RESERVE,
|
|
Math.floor(contextWindow * MAX_OUTPUT_SHARE_OF_WINDOW),
|
|
);
|
|
return Math.min(maxOutputTokens, cappedByWindow);
|
|
}
|
|
|
|
export interface ComputeCompactionThresholdInput {
|
|
contextWindow: number;
|
|
maxOutputTokens?: number;
|
|
compactionPromptTokens?: number;
|
|
}
|
|
|
|
export function computeCompactionBuffer(contextWindow: number, maxOutputTokens: number): number {
|
|
const remaining = Math.max(0, contextWindow - maxOutputTokens);
|
|
const ratioCompactionBuffer = Math.ceil((1 - AUTO_COMPACT_BUFFER_RATIO) * remaining);
|
|
const safeCompactionBuffer = Math.max(maxOutputTokens, ratioCompactionBuffer);
|
|
return Math.min(safeCompactionBuffer, AUTO_COMPACT_BUFFER_CAP);
|
|
}
|
|
|
|
/** Continue-style threshold: compact before the next turn would exceed the window. */
|
|
export function computeCompactionThreshold(input: ComputeCompactionThresholdInput): number {
|
|
const maxOutputTokens = resolveEffectiveMaxOutputTokens(
|
|
input.contextWindow,
|
|
input.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS,
|
|
);
|
|
const compactionPromptTokens = input.compactionPromptTokens ?? COMPACTION_PROMPT_RESERVE;
|
|
const buffer = computeCompactionBuffer(input.contextWindow, maxOutputTokens);
|
|
const threshold = input.contextWindow - maxOutputTokens - buffer - compactionPromptTokens;
|
|
return Math.max(1, threshold);
|
|
}
|
|
|
|
export interface ComputeTotalInputTokensInput {
|
|
messages: ModelMessage[];
|
|
providerId?: string | null;
|
|
systemPrompt?: string;
|
|
toolNames?: string[];
|
|
reservedTokens?: number;
|
|
}
|
|
|
|
export function computeTotalInputTokens(input: ComputeTotalInputTokensInput): number {
|
|
const messageTokens = estimateModelMessagesTokensWithKind({
|
|
messages: input.messages,
|
|
providerId: input.providerId,
|
|
}).tokens;
|
|
const systemTokens = input.systemPrompt
|
|
? estimateTextTokens(input.systemPrompt, input.providerId)
|
|
: 0;
|
|
const toolTokens = input.toolNames?.length
|
|
? estimateUnknownTokens({ tools: input.toolNames }, input.providerId)
|
|
: 0;
|
|
const reserved = Math.max(0, Math.ceil(input.reservedTokens ?? 0));
|
|
return messageTokens + systemTokens + toolTokens + reserved;
|
|
}
|
|
|
|
export function shouldCompactByBudget(input: {
|
|
messages: ModelMessage[];
|
|
contextWindow: number;
|
|
maxOutputTokens?: number;
|
|
providerId?: string | null;
|
|
systemPrompt?: string;
|
|
toolNames?: string[];
|
|
reservedTokens?: number;
|
|
forceThreshold?: number;
|
|
}): boolean {
|
|
const total = computeTotalInputTokens({
|
|
messages: input.messages,
|
|
providerId: input.providerId,
|
|
systemPrompt: input.systemPrompt,
|
|
toolNames: input.toolNames,
|
|
reservedTokens: input.reservedTokens,
|
|
});
|
|
const threshold = input.forceThreshold ?? computeCompactionThreshold({
|
|
contextWindow: input.contextWindow,
|
|
maxOutputTokens: input.maxOutputTokens,
|
|
});
|
|
return total >= threshold;
|
|
}
|