@@ -13,6 +13,7 @@ import { LogLine } from "@/types/log";
1313import { AgentScreenshotProviderError } from "@/types/stagehandErrors" ;
1414import Anthropic from "@anthropic-ai/sdk" ;
1515import { ToolSet } from "ai" ;
16+ import { zodToJsonSchema } from "zod-to-json-schema" ;
1617import { AgentClient } from "./AgentClient" ;
1718import { mapKeyToPlaywright } from "./utils/cuaKeyMapping" ;
1819import { compressConversationImages } from "./utils/imageCompression" ;
@@ -275,6 +276,12 @@ export class AnthropicCUAClient extends AgentClient {
275276 level : 2 ,
276277 } ) ;
277278 stepActions . push ( action ) ;
279+ } else if ( this . tools && toolUseItem . name in this . tools ) {
280+ stepActions . push ( {
281+ type : "custom_tool" ,
282+ tool : toolUseItem . name ,
283+ input : toolUseItem . input ,
284+ } as AgentAction ) ;
278285 }
279286 } else if ( block . type === "text" ) {
280287 // Safe to cast here since we've verified it's a text block
@@ -436,17 +443,16 @@ export class AnthropicCUAClient extends AgentClient {
436443 if ( this . tools && Object . keys ( this . tools ) . length > 0 ) {
437444 const customTools = Object . entries ( this . tools ) . map ( ( [ name , tool ] ) => {
438445 // Convert Zod schema to proper JSON schema format for Anthropic
439- let inputSchema = tool . parameters ;
440-
441- // Ensure the schema has the required 'type' field at root level
442- if ( typeof inputSchema === "object" && inputSchema !== null ) {
443- if ( ! ( "type" in inputSchema ) ) {
444- inputSchema = {
445- type : "object" ,
446- ...inputSchema ,
447- } ;
448- }
449- }
446+ const jsonSchema = zodToJsonSchema ( tool . parameters ) as {
447+ properties ?: Record < string , unknown > ;
448+ required ?: string [ ] ;
449+ } ;
450+
451+ const inputSchema = {
452+ type : "object" ,
453+ properties : jsonSchema . properties || { } ,
454+ required : jsonSchema . required || [ ] ,
455+ } ;
450456
451457 return {
452458 name,
@@ -890,6 +896,8 @@ export class AnthropicCUAClient extends AgentClient {
890896 type : name ,
891897 params : input ,
892898 } ;
899+ } else if ( this . tools && name in this . tools ) {
900+ return null ;
893901 }
894902
895903 console . warn ( `Unknown tool name: ${ name } ` ) ;
0 commit comments