Skip to content

Commit 30dc486

Browse files
committed
apply zod schema to custom tool input
1 parent 212590d commit 30dc486

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

backend/src/tools/tool-executor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,13 @@ export function parseRawCustomToolCall(
335335
}
336336
}
337337

338-
if (endsAgentStepParam in result.data) {
339-
delete result.data[endsAgentStepParam]
338+
const input = JSON.parse(JSON.stringify(rawToolCall.input))
339+
if (endsAgentStepParam in input) {
340+
delete input[endsAgentStepParam]
340341
}
341-
342342
return {
343343
toolName: toolName,
344-
input: result.data,
344+
input,
345345
toolCallId: rawToolCall.toolCallId,
346346
}
347347
}

sdk/src/client.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class CodebuffClient {
116116
*
117117
* @returns A Promise that resolves to a RunState JSON object which you can pass to a subsequent run() call to continue the run.
118118
*/
119-
public async run({
119+
public async run<A extends string = string, B = any, C = any>({
120120
agent,
121121
prompt,
122122
params,
@@ -136,7 +136,7 @@ export class CodebuffClient {
136136
projectFiles?: Record<string, string>
137137
knowledgeFiles?: Record<string, string>
138138
agentDefinitions?: AgentDefinition[]
139-
customToolDefinitions?: CustomToolDefinition[]
139+
customToolDefinitions?: CustomToolDefinition<A, B, C>[]
140140
maxAgentSteps?: number
141141
}): Promise<RunState> {
142142
await this.websocketHandler.connect()
@@ -169,13 +169,15 @@ export class CodebuffClient {
169169
`Implementation for custom tool ${toolName} not found.`,
170170
)
171171
}
172-
const handler = toolDefs[toolDefs.length - 1].handler
172+
const toolDef = toolDefs[toolDefs.length - 1]
173+
const handler = toolDef.handler
173174
try {
174175
return {
175176
success: true,
176177
output: {
177178
type: 'text',
178-
value: (await handler(input)).toolResultMessage,
179+
value: (await handler(toolDef.zodSchema.parse(input)))
180+
.toolResultMessage,
179181
},
180182
}
181183
} catch (error) {

sdk/src/custom-tool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type CustomToolDefinition<
1313
description?: string
1414
endsAgentStep: boolean
1515
exampleInputs: Input[]
16-
handler: (params: Input) => Promise<{
16+
handler: (params: Output) => Promise<{
1717
toolResultMessage: string
1818
}>
1919
}
@@ -35,7 +35,7 @@ export function getCustomToolDefinintion<
3535
description?: string
3636
endsAgentStep?: boolean
3737
exampleInputs?: Input[]
38-
handler: (params: Input) => Promise<{
38+
handler: (params: Output) => Promise<{
3939
toolResultMessage: string
4040
}>
4141
}): CustomToolDefinition<ToolName, Output, Input> {

0 commit comments

Comments
 (0)