Skip to content

Commit c5d202c

Browse files
authored
fix: cn auto compact if necessary before LLM api call (#7865)
1 parent 0e4f216 commit c5d202c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

extensions/cli/src/stream/streamChatResponse.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,31 @@ export async function streamChatResponse(
358358
}
359359
logger.debug("Starting conversation iteration");
360360

361+
// Pre-API auto-compaction checkpoint
362+
const { wasCompacted: preCompacted, chatHistory: preCompactHistory } =
363+
await handleAutoCompaction(chatHistory, model, llmApi, {
364+
isHeadless,
365+
callbacks: {
366+
onSystemMessage: callbacks?.onSystemMessage,
367+
onContent: callbacks?.onContent,
368+
},
369+
});
370+
371+
if (preCompacted) {
372+
logger.debug("Pre-API compaction occurred, updating chat history");
373+
// Update chat history after pre-compaction
374+
const chatHistorySvc2 = services.chatHistory;
375+
if (
376+
typeof chatHistorySvc2?.isReady === "function" &&
377+
chatHistorySvc2.isReady()
378+
) {
379+
chatHistorySvc2.setHistory(preCompactHistory);
380+
chatHistory = chatHistorySvc2.getHistory();
381+
} else {
382+
chatHistory = [...preCompactHistory];
383+
}
384+
}
385+
361386
// Recompute tools on each iteration to handle mode changes during streaming
362387
const tools = await getAllTools();
363388

0 commit comments

Comments
 (0)