@@ -3,46 +3,30 @@ import { toolParams } from '@codebuff/common/tools/list'
33import { getToolCallString } from '@codebuff/common/tools/utils'
44import { buildArray } from '@codebuff/common/util/array'
55import { pluralize } from '@codebuff/common/util/string'
6+ import { cloneDeep } from 'lodash'
67import z from 'zod/v4'
78
89import type { ToolName } from '@codebuff/common/tools/constants'
9- import type { customToolDefinitionsSchema } from '@codebuff/common/util/file'
10- import type { JSONSchema } from 'zod/v4/core'
10+ import type {
11+ CustomToolDefinitions ,
12+ customToolDefinitionsSchema ,
13+ } from '@codebuff/common/util/file'
14+ import type { ToolSet } from 'ai'
1115
12- function paramsSection ( params : {
13- schema :
14- | { type : 'zod' ; value : z . ZodObject }
15- | { type : 'json' ; value : JSONSchema . BaseSchema }
16- endsAgentStep : boolean
17- } ) {
16+ function paramsSection ( params : { schema : z . ZodType ; endsAgentStep : boolean } ) {
1817 const { schema, endsAgentStep } = params
19- const schemaWithEndsAgentStepParam =
20- schema . type === 'zod'
21- ? z . toJSONSchema (
22- endsAgentStep
23- ? schema . value . extend ( {
24- [ endsAgentStepParam ] : z
25- . literal ( endsAgentStep )
26- . describe ( 'Easp flag must be set to true' ) ,
27- } )
28- : schema . value ,
29- { io : 'input' } ,
18+ const schemaWithEndsAgentStepParam = z . toJSONSchema (
19+ endsAgentStep
20+ ? schema . and (
21+ z . object ( {
22+ [ endsAgentStepParam ] : z
23+ . literal ( endsAgentStep )
24+ . describe ( 'Easp flag must be set to true' ) ,
25+ } ) ,
3026 )
31- : JSON . parse ( JSON . stringify ( schema . value ) )
32- if ( schema . type === 'json' ) {
33- if ( ! schemaWithEndsAgentStepParam . properties ) {
34- schemaWithEndsAgentStepParam . properties = { }
35- }
36- schemaWithEndsAgentStepParam . properties [ endsAgentStepParam ] = {
37- const : true ,
38- type : 'boolean' ,
39- description : 'Easp flag must be set to true' ,
40- }
41- if ( ! schemaWithEndsAgentStepParam . required ) {
42- schemaWithEndsAgentStepParam . required = [ ]
43- }
44- schemaWithEndsAgentStepParam . required . push ( endsAgentStepParam )
45- }
27+ : schema ,
28+ { io : 'input' } ,
29+ )
4630
4731 const jsonSchema = schemaWithEndsAgentStepParam
4832 delete jsonSchema . description
@@ -63,9 +47,7 @@ function paramsSection(params: {
6347// Helper function to build the full tool description markdown
6448export function buildToolDescription ( params : {
6549 toolName : string
66- schema :
67- | { type : 'zod' ; value : z . ZodObject }
68- | { type : 'json' ; value : JSONSchema . BaseSchema }
50+ schema : z . ZodType
6951 description ?: string
7052 endsAgentStep : boolean
7153 exampleInputs ?: any [ ]
@@ -88,7 +70,7 @@ export function buildToolDescription(params: {
8870 ) . join ( '\n\n' )
8971 return buildArray ( [
9072 `### ${ toolName } ` ,
91- schema . value . description || '' ,
73+ schema . description || '' ,
9274 paramsSection ( { schema, endsAgentStep } ) ,
9375 descriptionWithExamples ,
9476 ] ) . join ( '\n\n' )
@@ -99,7 +81,7 @@ export const toolDescriptions = Object.fromEntries(
9981 name ,
10082 buildToolDescription ( {
10183 toolName : name ,
102- schema : { type : 'zod' , value : config . inputSchema } ,
84+ schema : config . inputSchema ,
10385 description : config . description ,
10486 endsAgentStep : config . endsAgentStep ,
10587 } ) ,
@@ -108,9 +90,7 @@ export const toolDescriptions = Object.fromEntries(
10890
10991function buildShortToolDescription ( params : {
11092 toolName : string
111- schema :
112- | { type : 'zod' ; value : z . ZodObject }
113- | { type : 'json' ; value : JSONSchema . BaseSchema }
93+ schema : z . ZodType
11494 endsAgentStep : boolean
11595} ) : string {
11696 const { toolName, schema, endsAgentStep } = params
@@ -119,7 +99,9 @@ function buildShortToolDescription(params: {
11999
120100export const getToolsInstructions = (
121101 tools : readonly string [ ] ,
122- additionalToolDefinitions : z . infer < typeof customToolDefinitionsSchema > ,
102+ additionalToolDefinitions : NonNullable <
103+ z . input < typeof customToolDefinitionsSchema >
104+ > ,
123105) => {
124106 if (
125107 tools . length === 0 &&
@@ -201,7 +183,7 @@ ${fullToolList(tools, additionalToolDefinitions)}
201183
202184export const fullToolList = (
203185 toolNames : readonly string [ ] ,
204- additionalToolDefinitions : z . infer < typeof customToolDefinitionsSchema > ,
186+ additionalToolDefinitions : CustomToolDefinitions ,
205187) => {
206188 if (
207189 toolNames . length === 0 &&
224206 const toolDef = additionalToolDefinitions [ toolName ]
225207 return buildToolDescription ( {
226208 toolName,
227- schema : { type : 'json' , value : toolDef . inputJsonSchema } ,
209+ schema : toolDef . inputSchema ,
228210 description : toolDef . description ,
229- endsAgentStep : toolDef . endsAgentStep ,
211+ endsAgentStep : toolDef . endsAgentStep ?? true ,
230212 exampleInputs : toolDef . exampleInputs ,
231213 } )
232214 } ) ,
235217
236218export const getShortToolInstructions = (
237219 toolNames : readonly string [ ] ,
238- additionalToolDefinitions : z . infer < typeof customToolDefinitionsSchema > ,
220+ additionalToolDefinitions : CustomToolDefinitions ,
239221) => {
240222 if (
241223 toolNames . length === 0 &&
@@ -253,16 +235,16 @@ export const getShortToolInstructions = (
253235 const tool = toolParams [ name ]
254236 return buildShortToolDescription ( {
255237 toolName : name ,
256- schema : { type : 'zod' , value : tool . inputSchema } ,
238+ schema : tool . inputSchema ,
257239 endsAgentStep : tool . endsAgentStep ,
258240 } )
259241 } ) ,
260242 ...Object . keys ( additionalToolDefinitions ) . map ( ( name ) => {
261- const { inputJsonSchema , endsAgentStep } = additionalToolDefinitions [ name ]
243+ const { inputSchema , endsAgentStep } = additionalToolDefinitions [ name ]
262244 return buildShortToolDescription ( {
263245 toolName : name ,
264- schema : { type : 'json' , value : inputJsonSchema } ,
265- endsAgentStep,
246+ schema : inputSchema ,
247+ endsAgentStep : endsAgentStep ?? true ,
266248 } )
267249 } ) ,
268250 ]
0 commit comments