@@ -153,6 +153,35 @@ export class ChatAgent extends AIChatAgent<Env> {
153153 }
154154 } ) ,
155155
156+ // Math calculation — runs inside the sandbox alongside other tools
157+ calculate : tool ( {
158+ description :
159+ "Perform a math calculation with two numbers." ,
160+ inputSchema : z . object ( {
161+ a : z . number ( ) . describe ( "First number" ) ,
162+ b : z . number ( ) . describe ( "Second number" ) ,
163+ operator : z
164+ . enum ( [ "+" , "-" , "*" , "/" , "%" ] )
165+ . describe ( "Arithmetic operator" )
166+ } ) ,
167+ execute : async ( { a, b, operator } ) => {
168+ const ops : Record < string , ( x : number , y : number ) => number > = {
169+ "+" : ( x , y ) => x + y ,
170+ "-" : ( x , y ) => x - y ,
171+ "*" : ( x , y ) => x * y ,
172+ "/" : ( x , y ) => x / y ,
173+ "%" : ( x , y ) => x % y
174+ } ;
175+ if ( operator === "/" && b === 0 ) {
176+ return { error : "Division by zero" } ;
177+ }
178+ return {
179+ expression : `${ a } ${ operator } ${ b } ` ,
180+ result : ops [ operator ] ( a , b )
181+ } ;
182+ }
183+ } ) ,
184+
156185 // MCP tools from connected servers
157186 ...mcpTools
158187 } ;
@@ -180,39 +209,8 @@ If the user asks to schedule a task, use the schedule tool to schedule the task.
180209 toolCalls : "before-last-2-messages"
181210 } ) ,
182211 tools : {
183- // Codemode tool: LLM writes orchestration code for multi-step tool use
184- codemode,
185-
186- // Approval tool: requires user confirmation — not supported inside codemode yet
187- calculate : tool ( {
188- description :
189- "Perform a math calculation with two numbers. Requires user approval for large numbers." ,
190- inputSchema : z . object ( {
191- a : z . number ( ) . describe ( "First number" ) ,
192- b : z . number ( ) . describe ( "Second number" ) ,
193- operator : z
194- . enum ( [ "+" , "-" , "*" , "/" , "%" ] )
195- . describe ( "Arithmetic operator" )
196- } ) ,
197- needsApproval : async ( { a, b } ) =>
198- Math . abs ( a ) > 1000 || Math . abs ( b ) > 1000 ,
199- execute : async ( { a, b, operator } ) => {
200- const ops : Record < string , ( x : number , y : number ) => number > = {
201- "+" : ( x , y ) => x + y ,
202- "-" : ( x , y ) => x - y ,
203- "*" : ( x , y ) => x * y ,
204- "/" : ( x , y ) => x / y ,
205- "%" : ( x , y ) => x % y
206- } ;
207- if ( operator === "/" && b === 0 ) {
208- return { error : "Division by zero" } ;
209- }
210- return {
211- expression : `${ a } ${ operator } ${ b } ` ,
212- result : ops [ operator ] ( a , b )
213- } ;
214- }
215- } )
212+ // Codemode: LLM writes one JS script that orchestrates all tools in parallel
213+ codemode
216214 } ,
217215 stopWhen : stepCountIs ( 5 ) ,
218216 abortSignal : options ?. abortSignal
0 commit comments