Skip to content

Commit bae9a1c

Browse files
committed
Refactor helper for surfacing original tool call in error message
1 parent 237aba1 commit bae9a1c

4 files changed

Lines changed: 20 additions & 25 deletions

File tree

packages/agent-runtime/src/tools/handlers/tool/set-output.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { jsonToolResult } from '@codebuff/common/util/messages'
22

33
import { getAgentTemplate } from '../../../templates/agent-registry'
4+
import { formatValueForError } from '../../../util/format-value'
45

56
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
67
import type {
@@ -61,11 +62,7 @@ export const handleSetOutput = (async (params: {
6162
const prefix = usedData
6263
? 'Output validation error: Your output was found inside the `data` field but still failed validation. Please fix the issues and try again without wrapping in `data`. Issues: '
6364
: 'Output validation error: Output failed to match the output schema and was ignored. You might want to try again! Issues: '
64-
const outputStr = JSON.stringify(output, null, 2)
65-
const truncatedOutput = outputStr.length > 500
66-
? outputStr.slice(0, 500) + '...(truncated)'
67-
: outputStr
68-
const errorMessage = `${prefix}${bestError}\n\nOriginal output value:\n${truncatedOutput}`
65+
const errorMessage = `${prefix}${bestError}\n\nOriginal output value:\n${formatValueForError(output)}`
6966
logger.error(
7067
{
7168
output,

packages/agent-runtime/src/tools/handlers/tool/spawn-agent-utils.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateCompactId } from '@codebuff/common/util/string'
55

66
import { loopAgentSteps } from '../../../run-agent-step'
77
import { getAgentTemplate } from '../../../templates/agent-registry'
8+
import { formatValueForError } from '../../../util/format-value'
89
import {
910
filterUnfinishedToolCalls,
1011
withSystemTags,
@@ -221,12 +222,8 @@ export function validateAgentInput(
221222
if (inputSchema.prompt) {
222223
const result = inputSchema.prompt.safeParse(prompt ?? '')
223224
if (!result.success) {
224-
const promptStr = JSON.stringify(prompt ?? '', null, 2)
225-
const truncatedPrompt = promptStr.length > 500
226-
? promptStr.slice(0, 500) + '...(truncated)'
227-
: promptStr
228225
throw new Error(
229-
`Invalid prompt for agent ${agentType}: ${JSON.stringify(result.error.issues, null, 2)}\n\nOriginal prompt value:\n${truncatedPrompt}`,
226+
`Invalid prompt for agent ${agentType}: ${JSON.stringify(result.error.issues, null, 2)}\n\nOriginal prompt value:\n${formatValueForError(prompt ?? '')}`,
230227
)
231228
}
232229
}
@@ -235,12 +232,8 @@ export function validateAgentInput(
235232
if (inputSchema.params) {
236233
const result = inputSchema.params.safeParse(params ?? {})
237234
if (!result.success) {
238-
const paramsStr = JSON.stringify(params ?? {}, null, 2)
239-
const truncatedParams = paramsStr.length > 500
240-
? paramsStr.slice(0, 500) + '...(truncated)'
241-
: paramsStr
242235
throw new Error(
243-
`Invalid params for agent ${agentType}: ${JSON.stringify(result.error.issues, null, 2)}\n\nOriginal params value:\n${truncatedParams}`,
236+
`Invalid params for agent ${agentType}: ${JSON.stringify(result.error.issues, null, 2)}\n\nOriginal params value:\n${formatValueForError(params ?? {})}`,
244237
)
245238
}
246239
}

packages/agent-runtime/src/tools/tool-executor.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { cloneDeep } from 'lodash'
66
import { getMCPToolData } from '../mcp'
77
import { MCP_TOOL_SEPARATOR } from '../mcp-constants'
88
import { getAgentShortName } from '../templates/prompts'
9+
import { formatValueForError } from '../util/format-value'
910
import { codebuffToolHandlers } from './handlers/list'
1011
import {
1112
getMatchingSpawn,
@@ -180,13 +181,10 @@ export async function executeToolCall<T extends ToolName>(
180181
}
181182

182183
if ('error' in toolCall) {
183-
const inputStr = JSON.stringify(input, null, 2)
184-
const truncatedInput = inputStr.length > 500
185-
? inputStr.slice(0, 500) + '...(truncated)'
186-
: inputStr
184+
const formattedInput = formatValueForError(input)
187185
onResponseChunk({
188186
type: 'error',
189-
message: `${toolCall.error}\n\nOriginal tool call input:\n${truncatedInput}`,
187+
message: `${toolCall.error}\n\nOriginal tool call input:\n${formattedInput}`,
190188
})
191189
logger.debug(
192190
{ toolCall, error: toolCall.error },
@@ -491,13 +489,10 @@ export async function executeCustomToolCall(
491489
}
492490

493491
if ('error' in toolCall) {
494-
const inputStr = JSON.stringify(input, null, 2)
495-
const truncatedInput = inputStr.length > 500
496-
? inputStr.slice(0, 500) + '...(truncated)'
497-
: inputStr
492+
const formattedInput = formatValueForError(input)
498493
onResponseChunk({
499494
type: 'error',
500-
message: `${toolCall.error}\n\nOriginal tool call input:\n${truncatedInput}`,
495+
message: `${toolCall.error}\n\nOriginal tool call input:\n${formattedInput}`,
501496
})
502497
logger.debug(
503498
{ toolCall, error: toolCall.error },
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function formatValueForError(value: unknown, maxLength = 500): string {
2+
const jsonStr = JSON.stringify(value, null, 2) ?? 'undefined'
3+
const truncated = jsonStr.length > maxLength
4+
? jsonStr.slice(0, maxLength) + '...(truncated)'
5+
: jsonStr
6+
if (value === null || value === undefined || typeof value !== 'object') {
7+
return `${truncated} (type: ${value === null ? 'null' : typeof value})`
8+
}
9+
return truncated
10+
}

0 commit comments

Comments
 (0)