fix: 修复 agent-engine 中 abortController 在自动压缩前未定义导致 ReferenceError 的严重 bug

compressWithLLM() 调用时引用的 abortController 变量在 while 循环内才定义,
但自动压缩逻辑在 while 循环之前执行,导致运行时 ReferenceError。
修复方式:使用 state 中已有的 AbortController 或创建新的实例。
This commit is contained in:
Metona Dev
2026-04-24 12:43:30 +08:00
parent 8e53a411d6
commit 9f38d35b1e
+2 -1
View File
@@ -500,8 +500,9 @@ export async function runAgentLoop(
// 自动压缩:当上下文 token 超过 context window 的 50% 时,调用 LLM 摘要压缩 // 自动压缩:当上下文 token 超过 context window 的 50% 时,调用 LLM 摘要压缩
if (shouldAutoCompress(messages, numCtx)) { if (shouldAutoCompress(messages, numCtx)) {
logInfo(`自动上下文压缩触发: tokens≈${estimateTokens(messages.map(m => m.content || '').join(''))} > ${Math.floor(numCtx * AUTO_COMPRESS_THRESHOLD)} (50% of ${numCtx})`); logInfo(`自动上下文压缩触发: tokens≈${estimateTokens(messages.map(m => m.content || '').join(''))} > ${Math.floor(numCtx * AUTO_COMPRESS_THRESHOLD)} (50% of ${numCtx})`);
const compressAC = state.get<AbortController | null>(KEYS.ABORT_CONTROLLER) || new AbortController();
try { try {
const compressed = await compressWithLLM(messages, api, model, { abortController }); const compressed = await compressWithLLM(messages, api, model, { abortController: compressAC });
if (compressed.length < messages.length || estimateTokens(compressed.map(m => m.content || '').join('')) < estimateTokens(messages.map(m => m.content || '').join(''))) { if (compressed.length < messages.length || estimateTokens(compressed.map(m => m.content || '').join('')) < estimateTokens(messages.map(m => m.content || '').join(''))) {
messages.length = 0; messages.length = 0;
messages.push(...compressed); messages.push(...compressed);