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