debug: 添加详细调试日志定位工具调用卡死问题
在 agent-engine.ts 和 ollama.ts 的关键节点添加 console.log: - 流式调用开始/结束/错误 - 工具调用列表和参数 - 确认框弹出和用户确认结果 - 工具执行开始/结果 - messages 数组长度变化 - 每个 Loop 迭代的完整状态 - Ollama API 请求/响应/流结束
This commit is contained in:
@@ -89,7 +89,9 @@ export class OllamaAPI {
|
|||||||
fetchOptions.signal = abortController.signal;
|
fetchOptions.signal = abortController.signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('[OllamaAPI] chatStream 请求:', `${this.baseUrl}/api/chat`, 'messages:', params.messages.length, 'tools:', params.tools?.length || 0);
|
||||||
const response = await this._request('/api/chat', fetchOptions);
|
const response = await this._request('/api/chat', fetchOptions);
|
||||||
|
console.log('[OllamaAPI] chatStream 响应状态:', response.status);
|
||||||
const reader = response.body!.getReader();
|
const reader = response.body!.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
@@ -102,7 +104,10 @@ export class OllamaAPI {
|
|||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
if (done) break;
|
if (done) {
|
||||||
|
console.log('[OllamaAPI] 流读取结束 (reader done)');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
buffer += decoder.decode(value, { stream: true });
|
buffer += decoder.decode(value, { stream: true });
|
||||||
const lines = buffer.split('\n');
|
const lines = buffer.split('\n');
|
||||||
@@ -114,6 +119,7 @@ export class OllamaAPI {
|
|||||||
const chunk: OllamaStreamChunk = JSON.parse(line);
|
const chunk: OllamaStreamChunk = JSON.parse(line);
|
||||||
if (onChunk) onChunk(chunk);
|
if (onChunk) onChunk(chunk);
|
||||||
if (chunk.done) {
|
if (chunk.done) {
|
||||||
|
console.log('[OllamaAPI] 收到 done:true, eval_count:', chunk.eval_count);
|
||||||
reader.cancel();
|
reader.cancel();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ export async function runAgentLoop(
|
|||||||
}
|
}
|
||||||
|
|
||||||
logAgentLoop(loopCount, MAX_LOOPS);
|
logAgentLoop(loopCount, MAX_LOOPS);
|
||||||
|
console.log(`[AgentEngine] ===== Loop #${loopCount} 开始, messages: ${messages.length}, tools: ${useTools} =====`);
|
||||||
|
|
||||||
let thinking = '';
|
let thinking = '';
|
||||||
content = '';
|
content = '';
|
||||||
@@ -132,6 +133,7 @@ export async function runAgentLoop(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 带超时保护的流式调用
|
// 带超时保护的流式调用
|
||||||
|
console.log('[AgentEngine] 开始流式调用, model:', model, 'think:', state.get<boolean>('thinkEnabled', false));
|
||||||
await Promise.race([
|
await Promise.race([
|
||||||
api.chatStream(
|
api.chatStream(
|
||||||
{
|
{
|
||||||
@@ -184,6 +186,7 @@ export async function runAgentLoop(
|
|||||||
)
|
)
|
||||||
]);
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('[AgentEngine] 流式调用错误:', (err as Error).message, 'aborted:', abortController.signal.aborted);
|
||||||
if (abortController.signal.aborted) {
|
if (abortController.signal.aborted) {
|
||||||
logInfo('流式调用已中止');
|
logInfo('流式调用已中止');
|
||||||
if (content || thinking) {
|
if (content || thinking) {
|
||||||
@@ -224,26 +227,35 @@ export async function runAgentLoop(
|
|||||||
messages.push(assistantMsg);
|
messages.push(assistantMsg);
|
||||||
|
|
||||||
logModelResponse(content.length, toolCalls.length);
|
logModelResponse(content.length, toolCalls.length);
|
||||||
|
console.log('[AgentEngine] 流式完成, toolCalls:', toolCalls.length, 'content:', content.length);
|
||||||
|
|
||||||
if (toolCalls.length === 0) {
|
if (toolCalls.length === 0) {
|
||||||
|
logInfo('无工具调用,Agent Loop 结束');
|
||||||
callbacks.onDone(content, allToolRecords.length > 0 ? allToolRecords : undefined);
|
callbacks.onDone(content, allToolRecords.length > 0 ? allToolRecords : undefined);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('[AgentEngine] 开始处理工具调用:', toolCalls.map(c => c.function.name));
|
||||||
|
|
||||||
for (const call of toolCalls) {
|
for (const call of toolCalls) {
|
||||||
// 检查是否已中止
|
// 检查是否已中止
|
||||||
if (abortController.signal.aborted) {
|
if (abortController.signal.aborted) {
|
||||||
|
console.log('[AgentEngine] Abort 信号已设置,退出');
|
||||||
callbacks.onDone(content, allToolRecords.length > 0 ? allToolRecords : undefined);
|
callbacks.onDone(content, allToolRecords.length > 0 ? allToolRecords : undefined);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
callbacks.onToolCallStart(call);
|
callbacks.onToolCallStart(call);
|
||||||
logToolStart(call.function.name, JSON.stringify(call.function.arguments));
|
logToolStart(call.function.name, JSON.stringify(call.function.arguments));
|
||||||
|
console.log('[AgentEngine] 工具调用:', call.function.name, call.function.arguments);
|
||||||
|
|
||||||
if (needsConfirmation(call.function.name)) {
|
if (needsConfirmation(call.function.name)) {
|
||||||
|
console.log('[AgentEngine] 需要确认,弹出确认框:', call.function.name);
|
||||||
const confirmed = await callbacks.onConfirmTool(call);
|
const confirmed = await callbacks.onConfirmTool(call);
|
||||||
|
console.log('[AgentEngine] 用户确认结果:', confirmed, 'abort:', abortController.signal.aborted);
|
||||||
// 确认后再次检查是否已中止
|
// 确认后再次检查是否已中止
|
||||||
if (abortController.signal.aborted) {
|
if (abortController.signal.aborted) {
|
||||||
|
console.log('[AgentEngine] 确认后 Abort 信号已设置,退出');
|
||||||
callbacks.onDone(content, allToolRecords.length > 0 ? allToolRecords : undefined);
|
callbacks.onDone(content, allToolRecords.length > 0 ? allToolRecords : undefined);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -270,6 +282,7 @@ export async function runAgentLoop(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 工具执行超时保护
|
// 工具执行超时保护
|
||||||
|
console.log('[AgentEngine] 执行工具:', call.function.name);
|
||||||
const result = await Promise.race([
|
const result = await Promise.race([
|
||||||
executeTool(call.function.name, call.function.arguments),
|
executeTool(call.function.name, call.function.arguments),
|
||||||
new Promise<never>((_, reject) =>
|
new Promise<never>((_, reject) =>
|
||||||
@@ -285,11 +298,13 @@ export async function runAgentLoop(
|
|||||||
};
|
};
|
||||||
allToolRecords.push(record);
|
allToolRecords.push(record);
|
||||||
|
|
||||||
|
console.log('[AgentEngine] 工具结果:', call.function.name, result.success, result);
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'tool',
|
role: 'tool',
|
||||||
tool_name: call.function.name,
|
tool_name: call.function.name,
|
||||||
content: JSON.stringify(result)
|
content: JSON.stringify(result)
|
||||||
});
|
});
|
||||||
|
console.log('[AgentEngine] 当前 messages 数量:', messages.length);
|
||||||
callbacks.onToolCallResult(call.function.name, result, call);
|
callbacks.onToolCallResult(call.function.name, result, call);
|
||||||
logToolResult(call.function.name, result.success, result.success ? undefined : result.error);
|
logToolResult(call.function.name, result.success, result.success ? undefined : result.error);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user