fix: use model cache context flag for runtime context limits
This commit is contained in:
@@ -2,13 +2,17 @@ import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import { createSystemDirective, SystemDirectiveTypes } from "../shared/system-directive"
|
||||
|
||||
const ANTHROPIC_DISPLAY_LIMIT = 1_000_000
|
||||
const ANTHROPIC_ACTUAL_LIMIT =
|
||||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
||||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
||||
? 1_000_000
|
||||
: 200_000
|
||||
const DEFAULT_ANTHROPIC_ACTUAL_LIMIT = 200_000
|
||||
const CONTEXT_WARNING_THRESHOLD = 0.70
|
||||
|
||||
function getAnthropicActualLimit(anthropicContext1MEnabled: boolean): number {
|
||||
return anthropicContext1MEnabled ||
|
||||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
||||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
||||
? 1_000_000
|
||||
: DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
||||
}
|
||||
|
||||
const CONTEXT_REMINDER = `${createSystemDirective(SystemDirectiveTypes.CONTEXT_WINDOW_MONITOR)}
|
||||
|
||||
You are using Anthropic Claude with 1M context window.
|
||||
@@ -31,7 +35,10 @@ function isAnthropicProvider(providerID: string): boolean {
|
||||
return providerID === "anthropic" || providerID === "google-vertex-anthropic"
|
||||
}
|
||||
|
||||
export function createContextWindowMonitorHook(_ctx: PluginInput) {
|
||||
export function createContextWindowMonitorHook(
|
||||
_ctx: PluginInput,
|
||||
anthropicContext1MEnabled = false,
|
||||
) {
|
||||
const remindedSessions = new Set<string>()
|
||||
const tokenCache = new Map<string, CachedTokenState>()
|
||||
|
||||
@@ -51,7 +58,8 @@ export function createContextWindowMonitorHook(_ctx: PluginInput) {
|
||||
const lastTokens = cached.tokens
|
||||
const totalInputTokens = (lastTokens?.input ?? 0) + (lastTokens?.cache?.read ?? 0)
|
||||
|
||||
const actualUsagePercentage = totalInputTokens / ANTHROPIC_ACTUAL_LIMIT
|
||||
const actualUsagePercentage =
|
||||
totalInputTokens / getAnthropicActualLimit(anthropicContext1MEnabled)
|
||||
|
||||
if (actualUsagePercentage < CONTEXT_WARNING_THRESHOLD) return
|
||||
|
||||
|
||||
@@ -27,9 +27,12 @@ interface EventInput {
|
||||
};
|
||||
}
|
||||
|
||||
export function createDirectoryAgentsInjectorHook(ctx: PluginInput) {
|
||||
export function createDirectoryAgentsInjectorHook(
|
||||
ctx: PluginInput,
|
||||
anthropicContext1MEnabled?: boolean,
|
||||
) {
|
||||
const sessionCaches = new Map<string, Set<string>>();
|
||||
const truncator = createDynamicTruncator(ctx);
|
||||
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
|
||||
|
||||
const toolExecuteAfter = async (input: ToolExecuteInput, output: ToolExecuteOutput) => {
|
||||
const toolName = input.tool.toLowerCase();
|
||||
|
||||
@@ -27,9 +27,12 @@ interface EventInput {
|
||||
};
|
||||
}
|
||||
|
||||
export function createDirectoryReadmeInjectorHook(ctx: PluginInput) {
|
||||
export function createDirectoryReadmeInjectorHook(
|
||||
ctx: PluginInput,
|
||||
anthropicContext1MEnabled?: boolean,
|
||||
) {
|
||||
const sessionCaches = new Map<string, Set<string>>();
|
||||
const truncator = createDynamicTruncator(ctx);
|
||||
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
|
||||
|
||||
const toolExecuteAfter = async (input: ToolExecuteInput, output: ToolExecuteOutput) => {
|
||||
const toolName = input.tool.toLowerCase();
|
||||
|
||||
@@ -2,11 +2,13 @@ import { log } from "../shared/logger"
|
||||
|
||||
const DEFAULT_ACTUAL_LIMIT = 200_000
|
||||
|
||||
const ANTHROPIC_ACTUAL_LIMIT =
|
||||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
||||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
||||
function getAnthropicActualLimit(anthropicContext1MEnabled: boolean): number {
|
||||
return anthropicContext1MEnabled ||
|
||||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
||||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
||||
? 1_000_000
|
||||
: DEFAULT_ACTUAL_LIMIT
|
||||
}
|
||||
|
||||
const PREEMPTIVE_COMPACTION_THRESHOLD = 0.78
|
||||
|
||||
@@ -43,7 +45,10 @@ type PluginInput = {
|
||||
directory: string
|
||||
}
|
||||
|
||||
export function createPreemptiveCompactionHook(ctx: PluginInput) {
|
||||
export function createPreemptiveCompactionHook(
|
||||
ctx: PluginInput,
|
||||
anthropicContext1MEnabled = false,
|
||||
) {
|
||||
const compactionInProgress = new Set<string>()
|
||||
const compactedSessions = new Set<string>()
|
||||
const tokenCache = new Map<string, CachedCompactionState>()
|
||||
@@ -60,7 +65,7 @@ export function createPreemptiveCompactionHook(ctx: PluginInput) {
|
||||
|
||||
const actualLimit =
|
||||
isAnthropicProvider(cached.providerID)
|
||||
? ANTHROPIC_ACTUAL_LIMIT
|
||||
? getAnthropicActualLimit(anthropicContext1MEnabled)
|
||||
: DEFAULT_ACTUAL_LIMIT
|
||||
|
||||
const lastTokens = cached.tokens
|
||||
|
||||
@@ -29,8 +29,11 @@ interface EventInput {
|
||||
|
||||
const TRACKED_TOOLS = ["read", "write", "edit", "multiedit"];
|
||||
|
||||
export function createRulesInjectorHook(ctx: PluginInput) {
|
||||
const truncator = createDynamicTruncator(ctx);
|
||||
export function createRulesInjectorHook(
|
||||
ctx: PluginInput,
|
||||
anthropicContext1MEnabled?: boolean,
|
||||
) {
|
||||
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
|
||||
const { getSessionCache, clearSessionCache } = createSessionCacheStore();
|
||||
const { processFilePathForInjection } = createRuleInjectionProcessor({
|
||||
workspaceDirectory: ctx.directory,
|
||||
|
||||
@@ -27,11 +27,12 @@ const TOOL_SPECIFIC_MAX_TOKENS: Record<string, number> = {
|
||||
}
|
||||
|
||||
interface ToolOutputTruncatorOptions {
|
||||
anthropicContext1MEnabled?: boolean
|
||||
experimental?: ExperimentalConfig
|
||||
}
|
||||
|
||||
export function createToolOutputTruncatorHook(ctx: PluginInput, options?: ToolOutputTruncatorOptions) {
|
||||
const truncator = createDynamicTruncator(ctx)
|
||||
const truncator = createDynamicTruncator(ctx, options?.anthropicContext1MEnabled)
|
||||
const truncateAll = options?.experimental?.truncate_all_tool_outputs ?? false
|
||||
|
||||
const toolExecuteAfter = async (
|
||||
|
||||
Reference in New Issue
Block a user