@@ -159,72 +159,79 @@ async function handleManualCompaction(
159
159
}
160
160
}
161
161
162
- // Helper function to handle auto-compaction
162
+ // Helper function to handle auto-compaction for headless mode
163
163
async function handleAutoCompaction (
164
164
chatHistory : ChatCompletionMessageParam [ ] ,
165
165
model : ModelConfig ,
166
166
llmApi : any ,
167
167
isHeadless : boolean ,
168
168
format ?: "json" ,
169
169
) : Promise < number | null > {
170
- logger . info ( "Auto-compacting triggered due to context limit" ) ;
171
-
172
- if ( ! isHeadless ) {
173
- console . info (
174
- chalk . yellow (
175
- "\nApproaching context limit. Auto-compacting chat history..." ,
176
- ) ,
177
- ) ;
178
- } else if ( format === "json" ) {
179
- safeStdout (
180
- JSON . stringify ( {
181
- status : "info" ,
182
- message : "Auto-compacting triggered" ,
183
- contextUsage :
184
- calculateContextUsagePercentage (
185
- countChatHistoryTokens ( chatHistory ) ,
186
- model ,
187
- ) + "%" ,
188
- } ) + "\n" ,
189
- ) ;
190
- }
170
+ const { handleAutoCompaction : coreAutoCompaction } = await import (
171
+ "../streamChatResponse.autoCompaction.js"
172
+ ) ;
191
173
192
- try {
193
- const result = await compactChatHistory ( chatHistory , model , llmApi ) ;
194
- chatHistory . length = 0 ;
195
- chatHistory . push ( ...result . compactedHistory ) ;
196
- saveSession ( chatHistory ) ;
174
+ // Custom callbacks for headless mode console output
175
+ const callbacks = {
176
+ onSystemMessage : ( message : string ) => {
177
+ if (
178
+ message . includes ( "Auto-compacting" ) ||
179
+ message . includes ( "Approaching" )
180
+ ) {
181
+ if ( ! isHeadless ) {
182
+ console . info ( chalk . yellow ( `\n${ message } ` ) ) ;
183
+ } else if ( format === "json" ) {
184
+ safeStdout (
185
+ JSON . stringify ( {
186
+ status : "info" ,
187
+ message : "Auto-compacting triggered" ,
188
+ contextUsage :
189
+ calculateContextUsagePercentage (
190
+ countChatHistoryTokens ( chatHistory ) ,
191
+ model ,
192
+ ) + "%" ,
193
+ } ) + "\n" ,
194
+ ) ;
195
+ }
196
+ } else if ( message . includes ( "✓" ) ) {
197
+ if ( ! isHeadless ) {
198
+ console . info ( chalk . green ( message ) ) ;
199
+ } else if ( format === "json" ) {
200
+ safeStdout (
201
+ JSON . stringify ( {
202
+ status : "success" ,
203
+ message : "Auto-compacted successfully" ,
204
+ historyLength : chatHistory . length ,
205
+ } ) + "\n" ,
206
+ ) ;
207
+ }
208
+ } else if ( message . includes ( "Warning:" ) ) {
209
+ if ( ! isHeadless ) {
210
+ console . error ( chalk . red ( message ) ) ;
211
+ console . info ( chalk . yellow ( "Continuing without compaction..." ) ) ;
212
+ } else if ( format === "json" ) {
213
+ safeStdout (
214
+ JSON . stringify ( {
215
+ status : "warning" ,
216
+ message : "Auto-compaction failed, continuing without compaction" ,
217
+ } ) + "\n" ,
218
+ ) ;
219
+ }
220
+ }
221
+ } ,
222
+ } ;
197
223
198
- if ( ! isHeadless ) {
199
- console . info ( chalk . green ( "✓ Chat history auto-compacted successfully." ) ) ;
200
- } else if ( format === "json" ) {
201
- safeStdout (
202
- JSON . stringify ( {
203
- status : "success" ,
204
- message : "Auto-compacted successfully" ,
205
- historyLength : chatHistory . length ,
206
- } ) + "\n" ,
207
- ) ;
208
- }
224
+ const result = await coreAutoCompaction ( chatHistory , model , llmApi , {
225
+ isHeadless,
226
+ format,
227
+ callbacks,
228
+ } ) ;
209
229
210
- return result . compactionIndex ;
211
- } catch ( error ) {
212
- const errorMsg = `Auto-compaction error: ${ formatError ( error ) } ` ;
213
- logger . error ( errorMsg ) ;
230
+ // Update the original array reference for headless mode
231
+ chatHistory . length = 0 ;
232
+ chatHistory . push ( ...result . chatHistory ) ;
214
233
215
- if ( ! isHeadless ) {
216
- console . error ( chalk . red ( `Warning: ${ errorMsg } ` ) ) ;
217
- console . info ( chalk . yellow ( "Continuing without compaction..." ) ) ;
218
- } else if ( format === "json" ) {
219
- safeStdout (
220
- JSON . stringify ( {
221
- status : "warning" ,
222
- message : "Auto-compaction failed, continuing without compaction" ,
223
- } ) + "\n" ,
224
- ) ;
225
- }
226
- return null ;
227
- }
234
+ return result . compactionIndex ;
228
235
}
229
236
230
237
interface ProcessMessageOptions {
0 commit comments