MCPcopy Index your code
hub / github.com/codeaashu/claude-code / getAPIContextManagement

Function getAPIContextManagement

src/services/compact/apiMicrocompact.ts:64–153  ·  view source on GitHub ↗
(options?: {
  hasThinking?: boolean
  isRedactThinkingActive?: boolean
  clearAllThinking?: boolean
})

Source from the content-addressed store, hash-verified

62
63// API-based microcompact implementation that uses native context management
64export function getAPIContextManagement(options?: {
65 hasThinking?: boolean
66 isRedactThinkingActive?: boolean
67 clearAllThinking?: boolean
68}): ContextManagementConfig | undefined {
69 const {
70 hasThinking = false,
71 isRedactThinkingActive = false,
72 clearAllThinking = false,
73 } = options ?? {}
74
75 const strategies: ContextEditStrategy[] = []
76
77 // Preserve thinking blocks in previous assistant turns. Skip when
78 // redact-thinking is active — redacted blocks have no model-visible content.
79 // When clearAllThinking is set (>1h idle = cache miss), keep only the last
80 // thinking turn — the API schema requires value >= 1, and omitting the edit
81 // falls back to the model-policy default (often "all"), which wouldn't clear.
82 if (hasThinking && !isRedactThinkingActive) {
83 strategies.push({
84 type: 'clear_thinking_20251015',
85 keep: clearAllThinking ? { type: 'thinking_turns', value: 1 } : 'all',
86 })
87 }
88
89 // Tool clearing strategies are ant-only
90 if (process.env.USER_TYPE !== 'ant') {
91 return strategies.length > 0 ? { edits: strategies } : undefined
92 }
93
94 const useClearToolResults = isEnvTruthy(
95 process.env.USE_API_CLEAR_TOOL_RESULTS,
96 )
97 const useClearToolUses = isEnvTruthy(process.env.USE_API_CLEAR_TOOL_USES)
98
99 // If no tool clearing strategy is enabled, return early
100 if (!useClearToolResults && !useClearToolUses) {
101 return strategies.length > 0 ? { edits: strategies } : undefined
102 }
103
104 if (useClearToolResults) {
105 const triggerThreshold = process.env.API_MAX_INPUT_TOKENS
106 ? parseInt(process.env.API_MAX_INPUT_TOKENS)
107 : DEFAULT_MAX_INPUT_TOKENS
108 const keepTarget = process.env.API_TARGET_INPUT_TOKENS
109 ? parseInt(process.env.API_TARGET_INPUT_TOKENS)
110 : DEFAULT_TARGET_INPUT_TOKENS
111
112 const strategy: ContextEditStrategy = {
113 type: 'clear_tool_uses_20250919',
114 trigger: {
115 type: 'input_tokens',
116 value: triggerThreshold,
117 },
118 clear_at_least: {
119 type: 'input_tokens',
120 value: triggerThreshold - keepTarget,
121 },

Callers 1

paramsFromContextFunction · 0.85

Calls 2

isEnvTruthyFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected