MCPcopy Create free account
hub / github.com/Noumena-Network/code / getAPIContextManagement

Function getAPIContextManagement

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

Source from the content-addressed store, hash-verified

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

Callers 2

paramsFromContextFunction · 0.85

Calls 1

isEnvTruthyFunction · 0.90

Tested by 1