MCPcopy Create free account
hub / github.com/claude-code-best/claude-code / compactViaReactive

Function compactViaReactive

src/commands/compact/compact.ts:140–229  ·  view source on GitHub ↗
(
  messages: Message[],
  context: ToolUseContext,
  customInstructions: string,
  reactive: NonNullable<typeof reactiveCompact>,
)

Source from the content-addressed store, hash-verified

138}
139
140async function compactViaReactive(
141 messages: Message[],
142 context: ToolUseContext,
143 customInstructions: string,
144 reactive: NonNullable<typeof reactiveCompact>,
145): Promise<{
146 type: 'compact'
147 compactionResult: CompactionResult
148 displayText: string
149}> {
150 context.onCompactProgress?.({
151 type: 'hooks_start',
152 hookType: 'pre_compact',
153 })
154 context.setSDKStatus?.('compacting')
155
156 try {
157 // Hooks and cache-param build are independent — run concurrently.
158 // getCacheSharingParams walks all tools to build the system prompt;
159 // pre-compact hooks spawn subprocesses. Neither depends on the other.
160 const [hookResult, cacheSafeParams] = await Promise.all([
161 executePreCompactHooks(
162 { trigger: 'manual', customInstructions: customInstructions || null },
163 context.abortController.signal,
164 ),
165 getCacheSharingParams(context, messages),
166 ])
167 const mergedInstructions = mergeHookInstructions(
168 customInstructions,
169 hookResult.newCustomInstructions,
170 )
171
172 context.setStreamMode?.('requesting')
173 context.setResponseLength?.(() => 0)
174 context.onCompactProgress?.({ type: 'compact_start' })
175
176 const outcome = await reactive.reactiveCompactOnPromptTooLong(
177 messages,
178 cacheSafeParams,
179 { customInstructions: mergedInstructions, trigger: 'manual' },
180 )
181
182 if (!outcome.ok) {
183 // The outer catch in `call` translates these: aborted → "Compaction
184 // canceled." (via abortController.signal.aborted check), NOT_ENOUGH →
185 // re-thrown as-is, everything else → "Error during compaction: …".
186 switch (outcome.reason) {
187 case 'too_few_groups':
188 throw new Error(ERROR_MESSAGE_NOT_ENOUGH_MESSAGES)
189 case 'aborted':
190 throw new Error(ERROR_MESSAGE_USER_ABORT)
191 case 'exhausted':
192 case 'error':
193 case 'media_unstrippable':
194 throw new Error(ERROR_MESSAGE_INCOMPLETE_RESPONSE)
195 }
196 }
197

Callers 1

callFunction · 0.85

Calls 8

executePreCompactHooksFunction · 0.85
getCacheSharingParamsFunction · 0.85
mergeHookInstructionsFunction · 0.85
runPostCompactCleanupFunction · 0.85
suppressCompactWarningFunction · 0.85
buildDisplayTextFunction · 0.85
clearMethod · 0.45

Tested by

no test coverage detected