Skip to content

Commit b32f590

Browse files
committed
Better errors / logging for handleSteps & mainPrompt
1 parent 3942952 commit b32f590

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

backend/src/main-prompt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export const mainPrompt = async (
202202
localAgentTemplates,
203203
})
204204

205+
logger.debug({ agentState }, 'Main prompt finished')
206+
205207
return {
206208
sessionState: {
207209
fileContext,

backend/src/run-programmatic-step.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ export async function runProgrammaticStep(
183183
toolCallId: crypto.randomUUID(),
184184
} as CodebuffToolCall
185185

186+
if (!template.toolNames.includes(toolCall.toolName)) {
187+
throw new Error(
188+
`Tool ${toolCall.toolName} is not available for agent ${template.id}. Available tools: ${template.toolNames.join(', ')}`,
189+
)
190+
}
191+
186192
// Add assistant message with the tool call before executing it
187193
// Exception: don't add tool call message for add_message since it adds its own message
188194
if (toolCall.toolName !== 'add_message') {
@@ -239,20 +245,20 @@ export async function runProgrammaticStep(
239245
} catch (error) {
240246
endTurn = true
241247

248+
const errorMessage = `Error executing handleSteps for agent ${template.id}: ${
249+
error instanceof Error ? error.message : 'Unknown error'
250+
}`
242251
logger.error(
243252
{ error: getErrorObject(error), template: template.id },
244-
'Programmatic agent execution failed',
253+
errorMessage,
245254
)
246255

247-
const errorMessage = `Error executing handleSteps for agent ${template.id}: ${
248-
error instanceof Error ? error.message : 'Unknown error'
249-
}`
250256
onResponseChunk(errorMessage)
251257

252258
state.agentState.messageHistory = [
253259
...state.messages,
254260
{
255-
role: 'user' as const,
261+
role: 'assistant' as const,
256262
content: errorMessage,
257263
},
258264
]

backend/src/websockets/websocket-action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131
} from '@codebuff/common/actions'
3232
import type { ClientMessage } from '@codebuff/common/websockets/websocket-schema'
3333
import type { WebSocket } from 'ws'
34+
import { getErrorObject } from '@codebuff/common/util/error'
3435

3536
/**
3637
* Sends an action to the client via WebSocket
@@ -162,7 +163,7 @@ const onPrompt = async (
162163
clientSessionId,
163164
})
164165
} catch (e) {
165-
logger.error(e, 'Error in mainPrompt')
166+
logger.error({ error: getErrorObject(e) }, 'Error in mainPrompt')
166167
let response =
167168
e && typeof e === 'object' && 'message' in e ? `${e.message}` : `${e}`
168169

0 commit comments

Comments
 (0)