fix: improve error visibility for agent tool execution
- Add structured logging for tool calls, results, and errors - Log tool call initiation with input parameters - Distinguish between success and error tool results - Include isError flag in SSE tool_result events - Improve error logging with stack traces and context 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
938a382aab
commit
8bbde047b5
2 changed files with 45 additions and 2 deletions
|
|
@ -287,6 +287,14 @@ export async function POST(request: NextRequest, context: RouteContext): Promise
|
||||||
// Stream tool calls as they come in and capture their inputs
|
// Stream tool calls as they come in and capture their inputs
|
||||||
if (chunk.type === 'tool-call') {
|
if (chunk.type === 'tool-call') {
|
||||||
const input = 'args' in chunk ? chunk.args : chunk.input;
|
const input = 'args' in chunk ? chunk.args : chunk.input;
|
||||||
|
|
||||||
|
// Log tool call for debugging
|
||||||
|
console.log('[Agent] Tool call initiated:', {
|
||||||
|
toolName: chunk.toolName,
|
||||||
|
toolCallId: chunk.toolCallId,
|
||||||
|
input: JSON.stringify(input).slice(0, 500),
|
||||||
|
});
|
||||||
|
|
||||||
// Store tool call with input for later persistence
|
// Store tool call with input for later persistence
|
||||||
toolCallsMap.set(chunk.toolCallId, {
|
toolCallsMap.set(chunk.toolCallId, {
|
||||||
toolCallId: chunk.toolCallId,
|
toolCallId: chunk.toolCallId,
|
||||||
|
|
@ -320,9 +328,29 @@ export async function POST(request: NextRequest, context: RouteContext): Promise
|
||||||
// Send tool results via SSE but don't save yet (save after assistant message for correct order)
|
// Send tool results via SSE but don't save yet (save after assistant message for correct order)
|
||||||
if (toolResults && toolResults.length > 0) {
|
if (toolResults && toolResults.length > 0) {
|
||||||
for (const tr of toolResults) {
|
for (const tr of toolResults) {
|
||||||
|
// Check if this is an error result (AI SDK wraps errors in the output)
|
||||||
|
const isError =
|
||||||
|
tr.output && typeof tr.output === 'object' && 'error' in tr.output;
|
||||||
|
|
||||||
|
// Log tool results for debugging
|
||||||
|
if (isError) {
|
||||||
|
console.error('[Agent] Tool execution failed:', {
|
||||||
|
toolName: tr.toolName,
|
||||||
|
toolCallId: tr.toolCallId,
|
||||||
|
error: tr.output,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log('[Agent] Tool execution success:', {
|
||||||
|
toolName: tr.toolName,
|
||||||
|
toolCallId: tr.toolCallId,
|
||||||
|
outputPreview: JSON.stringify(tr.output).slice(0, 200),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
sendEvent('tool_result', {
|
sendEvent('tool_result', {
|
||||||
toolCallId: tr.toolCallId,
|
toolCallId: tr.toolCallId,
|
||||||
output: tr.output,
|
output: tr.output,
|
||||||
|
isError,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Collect tool results to save after assistant message
|
// Collect tool results to save after assistant message
|
||||||
|
|
@ -412,7 +440,13 @@ export async function POST(request: NextRequest, context: RouteContext): Promise
|
||||||
executionTimeMs,
|
executionTimeMs,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Agent conversation error:', error);
|
// Log detailed error for debugging
|
||||||
|
console.error('[Agent] Conversation stream error:', {
|
||||||
|
error: error instanceof Error ? error.message : String(error),
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
agentId: agent.id,
|
||||||
|
conversationId: conversation.id,
|
||||||
|
});
|
||||||
sendEvent('error', {
|
sendEvent('error', {
|
||||||
message: error instanceof Error ? error.message : 'Unknown error',
|
message: error instanceof Error ? error.message : 'Unknown error',
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -144,10 +144,19 @@ export function createToolDefinition(
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
// Log detailed error for debugging
|
||||||
|
console.error('[Tool execute] FAILED:', sanitizedName, {
|
||||||
|
error: result.error,
|
||||||
|
executionTimeMs: result.executionTimeMs,
|
||||||
|
params: JSON.stringify(params).slice(0, 200),
|
||||||
|
});
|
||||||
throw new Error(result.error || 'Package execution failed');
|
throw new Error(result.error || 'Package execution failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[Tool execute] Result:', result.output);
|
console.log('[Tool execute] Success:', sanitizedName, {
|
||||||
|
executionTimeMs: result.executionTimeMs,
|
||||||
|
outputPreview: JSON.stringify(result.output).slice(0, 200),
|
||||||
|
});
|
||||||
return result.output;
|
return result.output;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue