@browserbasehq/orca 3.2.0-preview.1 → 3.2.0-preview.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/cjs/lib/utils.d.ts +1 -0
  2. package/dist/cjs/lib/utils.js +4 -0
  3. package/dist/cjs/lib/utils.js.map +1 -1
  4. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +4 -6
  5. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  6. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +4 -6
  7. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  8. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +4 -6
  9. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  10. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.d.ts +2 -0
  11. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js +2 -2
  12. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  13. package/dist/cjs/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  14. package/dist/cjs/lib/v3/agent/tools/braveSearch.js.map +1 -0
  15. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  16. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js +70 -0
  17. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  18. package/dist/cjs/lib/v3/agent/tools/index.d.ts +14 -3
  19. package/dist/cjs/lib/v3/agent/tools/index.js +7 -3
  20. package/dist/cjs/lib/v3/agent/tools/index.js.map +1 -1
  21. package/dist/cjs/lib/v3/eventStore.d.ts +41 -0
  22. package/dist/cjs/lib/v3/eventStore.js +375 -0
  23. package/dist/cjs/lib/v3/eventStore.js.map +1 -0
  24. package/dist/cjs/lib/v3/flowLogger.d.ts +62 -103
  25. package/dist/cjs/lib/v3/flowLogger.js +362 -773
  26. package/dist/cjs/lib/v3/flowLogger.js.map +1 -1
  27. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js +21 -33
  28. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  29. package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +0 -4
  30. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +14 -34
  31. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  32. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +10 -12
  33. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  34. package/dist/cjs/lib/v3/llm/aisdk.js +10 -16
  35. package/dist/cjs/lib/v3/llm/aisdk.js.map +1 -1
  36. package/dist/cjs/lib/v3/types/public/agent.d.ts +16 -2
  37. package/dist/cjs/lib/v3/types/public/agent.js.map +1 -1
  38. package/dist/cjs/lib/v3/types/public/options.d.ts +5 -0
  39. package/dist/cjs/lib/v3/types/public/options.js.map +1 -1
  40. package/dist/cjs/lib/v3/understudy/cdp.d.ts +3 -12
  41. package/dist/cjs/lib/v3/understudy/cdp.js +83 -10
  42. package/dist/cjs/lib/v3/understudy/cdp.js.map +1 -1
  43. package/dist/cjs/lib/v3/understudy/page.js +32 -17
  44. package/dist/cjs/lib/v3/understudy/page.js.map +1 -1
  45. package/dist/cjs/lib/v3/v3.d.ts +10 -0
  46. package/dist/cjs/lib/v3/v3.js +181 -157
  47. package/dist/cjs/lib/v3/v3.js.map +1 -1
  48. package/dist/cjs/tests/unit/public-api/public-types.test.js.map +1 -1
  49. package/dist/esm/lib/utils.d.ts +1 -0
  50. package/dist/esm/lib/utils.js +3 -0
  51. package/dist/esm/lib/utils.js.map +1 -1
  52. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +5 -7
  53. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  54. package/dist/esm/lib/v3/agent/GoogleCUAClient.js +5 -7
  55. package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  56. package/dist/esm/lib/v3/agent/OpenAICUAClient.js +5 -7
  57. package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  58. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.d.ts +2 -0
  59. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js +2 -2
  60. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  61. package/dist/esm/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  62. package/dist/esm/lib/v3/agent/tools/braveSearch.js.map +1 -0
  63. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  64. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js +66 -0
  65. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  66. package/dist/esm/lib/v3/agent/tools/index.d.ts +14 -3
  67. package/dist/esm/lib/v3/agent/tools/index.js +7 -3
  68. package/dist/esm/lib/v3/agent/tools/index.js.map +1 -1
  69. package/dist/esm/lib/v3/eventStore.d.ts +41 -0
  70. package/dist/esm/lib/v3/eventStore.js +363 -0
  71. package/dist/esm/lib/v3/eventStore.js.map +1 -0
  72. package/dist/esm/lib/v3/flowLogger.d.ts +62 -103
  73. package/dist/esm/lib/v3/flowLogger.js +356 -762
  74. package/dist/esm/lib/v3/flowLogger.js.map +1 -1
  75. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js +22 -34
  76. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  77. package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +0 -4
  78. package/dist/esm/lib/v3/handlers/v3AgentHandler.js +16 -36
  79. package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  80. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +11 -13
  81. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  82. package/dist/esm/lib/v3/llm/aisdk.js +11 -17
  83. package/dist/esm/lib/v3/llm/aisdk.js.map +1 -1
  84. package/dist/esm/lib/v3/types/public/agent.d.ts +16 -2
  85. package/dist/esm/lib/v3/types/public/agent.js.map +1 -1
  86. package/dist/esm/lib/v3/types/public/options.d.ts +5 -0
  87. package/dist/esm/lib/v3/types/public/options.js.map +1 -1
  88. package/dist/esm/lib/v3/understudy/cdp.d.ts +3 -12
  89. package/dist/esm/lib/v3/understudy/cdp.js +83 -10
  90. package/dist/esm/lib/v3/understudy/cdp.js.map +1 -1
  91. package/dist/esm/lib/v3/understudy/page.js +33 -18
  92. package/dist/esm/lib/v3/understudy/page.js.map +1 -1
  93. package/dist/esm/lib/v3/v3.d.ts +10 -0
  94. package/dist/esm/lib/v3/v3.js +182 -158
  95. package/dist/esm/lib/v3/v3.js.map +1 -1
  96. package/dist/esm/tests/unit/public-api/public-types.test.js.map +1 -1
  97. package/package.json +1 -3
  98. package/dist/cjs/lib/v3/agent/tools/search.js.map +0 -1
  99. package/dist/cjs/tests/unit/rerender-missing-shadows.test.d.ts +0 -1
  100. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js +0 -209
  101. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  102. package/dist/esm/lib/v3/agent/tools/search.js.map +0 -1
  103. package/dist/esm/tests/unit/rerender-missing-shadows.test.d.ts +0 -1
  104. package/dist/esm/tests/unit/rerender-missing-shadows.test.js +0 -207
  105. package/dist/esm/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  106. /package/dist/cjs/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
  107. /package/dist/esm/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"aisdk.js","sourceRoot":"","sources":["../../../../../lib/v3/llm/aisdk.ts"],"names":[],"mappings":";;;AAAA,2BAYY;AAGZ,+BAAoC;AAGpC,iDAAwE;AACxE,o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AAE,YAAY,CAAC,KAAK,CAAC,WAAW;YAC3C,YAAY,EAAE,YAAY,CAAC,KAAK,CAAC,YAAY;SAC9C,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,QAAQ,EAAE,OAAO;YACjB,OAAO,EAAE,UAAU;YACnB,KAAK,EAAE,CAAC;YACR,SAAS,EAAE;gBACT,QAAQ,EAAE;oBACR,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC;wBACpB,IAAI,EAAE,YAAY,CAAC,IAAI;wBACvB,KAAK,EAAE,YAAY,CAAC,KAAK;wBACzB,YAAY,EAAE,YAAY,CAAC,YAAY;wBACvC,iEAAiE;qBAClE,CAAC;oBACF,IAAI,EAAE,QAAQ;iBACf;gBACD,SAAS,EAAE;oBACT,KAAK,EAAE,OAAO,CAAC,SAAS;oBACxB,IAAI,EAAE,QAAQ;iBACf;aACF;SACF,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AArYD,kCAqYC","sourcesContent":["import {\n CoreAssistantMessage,\n ModelMessage,\n CoreSystemMessage,\n CoreUserMessage,\n generateObject,\n generateText,\n ImagePart,\n NoObjectGeneratedError,\n TextPart,\n ToolSet,\n Tool,\n} from \"ai\";\nimport type { LanguageModelV2 } from \"@ai-sdk/provider\";\nimport { ChatCompletion } from \"openai/resources\";\nimport { v7 as uuidv7 } from \"uuid\";\nimport { LogLine } from \"../types/public/logs.js\";\nimport { AvailableModel } from \"../types/public/model.js\";\nimport { CreateChatCompletionOptions, LLMClient } from \"./LLMClient.js\";\nimport { SessionFileLogger, formatLlmPromptPreview } from \"../flowLogger.js\";\nimport { toJsonSchema } from \"../zodCompat.js\";\n\nexport class AISdkClient extends LLMClient {\n public type = \"aisdk\" as const;\n private model: LanguageModelV2;\n private logger?: (message: LogLine) => void;\n\n constructor({\n model,\n logger,\n }: {\n model: LanguageModelV2;\n logger?: (message: LogLine) => void;\n }) {\n super(model.modelId as AvailableModel);\n this.model = model;\n this.logger = logger;\n }\n\n public getLanguageModel(): LanguageModelV2 {\n return this.model;\n }\n\n async createChatCompletion<T = ChatCompletion>({\n options,\n }: CreateChatCompletionOptions): Promise<T> {\n this.logger?.({\n category: \"aisdk\",\n message: \"creating chat completion\",\n level: 2,\n auxiliary: {\n options: {\n value: JSON.stringify({\n ...options,\n image: undefined,\n messages: options.messages.map((msg) => ({\n ...msg,\n content: Array.isArray(msg.content)\n ? msg.content.map((c) =>\n \"image_url\" in c\n ? { ...c, image_url: { url: \"[IMAGE_REDACTED]\" } }\n : c,\n )\n : msg.content,\n })),\n }),\n type: \"object\",\n },\n modelName: {\n value: this.model.modelId,\n type: \"string\",\n },\n },\n });\n\n const formattedMessages: ModelMessage[] = options.messages.map(\n (message) => {\n if (Array.isArray(message.content)) {\n if (message.role === \"system\") {\n const systemMessage: CoreSystemMessage = {\n role: \"system\",\n content: message.content\n .map((c) => (\"text\" in c ? c.text : \"\"))\n .join(\"\\n\"),\n };\n return systemMessage;\n }\n\n const contentParts = message.content.map((content) => {\n if (\"image_url\" in content) {\n const imageContent: ImagePart = {\n type: \"image\",\n image: content.image_url.url,\n };\n return imageContent;\n } else {\n const textContent: TextPart = {\n type: \"text\",\n text: content.text,\n };\n return textContent;\n }\n });\n\n if (message.role === \"user\") {\n const userMessage: CoreUserMessage = {\n role: \"user\",\n content: contentParts,\n };\n return userMessage;\n } else {\n const textOnlyParts = contentParts.map((part) => ({\n type: \"text\" as const,\n text: part.type === \"image\" ? \"[Image]\" : part.text,\n }));\n const assistantMessage: CoreAssistantMessage = {\n role: \"assistant\",\n content: textOnlyParts,\n };\n return assistantMessage;\n }\n }\n\n return {\n role: message.role,\n content: message.content,\n };\n },\n );\n\n let objectResponse: Awaited<ReturnType<typeof generateObject>>;\n const isGPT5 = this.model.modelId.includes(\"gpt-5\");\n const isCodex = this.model.modelId.includes(\"codex\");\n const usesLowReasoningEffort =\n (this.model.modelId.includes(\"gpt-5.1\") ||\n this.model.modelId.includes(\"gpt-5.2\")) &&\n !isCodex;\n // Kimi models only support temperature=1\n const isKimi = this.model.modelId.includes(\"kimi\");\n const temperature = isKimi ? 1 : options.temperature;\n\n // Models that lack native structured-output support need a prompt-based\n // JSON fallback instead of response_format: { type: \"json_schema\" }.\n const PROMPT_JSON_FALLBACK_PATTERNS = [\"deepseek\", \"kimi\", \"glm\"];\n const needsPromptJsonFallback = PROMPT_JSON_FALLBACK_PATTERNS.some((p) =>\n this.model.modelId.includes(p),\n );\n\n if (options.response_model) {\n // Log LLM request for generateObject (extract)\n const llmRequestId = uuidv7();\n const promptPreview = formatLlmPromptPreview(options.messages, {\n hasSchema: true,\n });\n SessionFileLogger.logLlmRequest({\n requestId: llmRequestId,\n model: this.model.modelId,\n operation: \"generateObject\",\n prompt: promptPreview,\n });\n\n // For models that don't support native structured outputs, add a prompt instruction\n if (needsPromptJsonFallback) {\n const parsedSchema = JSON.stringify(\n toJsonSchema(options.response_model.schema),\n );\n\n formattedMessages.push({\n role: \"user\",\n content: `Respond in this zod schema format:\\n${parsedSchema}\\n\nYou must respond in JSON format. respond WITH JSON. Do not include any other text, formatting or markdown in your output. Do not include \\`\\`\\` or \\`\\`\\`json in your response. Only the JSON object itself.`,\n });\n }\n\n try {\n objectResponse = await generateObject({\n model: this.model,\n messages: formattedMessages,\n schema: options.response_model.schema,\n temperature,\n providerOptions: isGPT5\n ? {\n openai: {\n textVerbosity: isCodex ? \"medium\" : \"low\", // codex models only support 'medium'\n reasoningEffort: isCodex\n ? \"medium\"\n : usesLowReasoningEffort\n ? \"low\"\n : \"minimal\",\n },\n }\n : undefined,\n });\n } catch (err) {\n // Log error response to maintain request/response pairing\n SessionFileLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n operation: \"generateObject\",\n output: `[error: ${err instanceof Error ? err.message : \"unknown\"}]`,\n });\n\n if (NoObjectGeneratedError.isInstance(err)) {\n this.logger?.({\n category: \"AISDK error\",\n message: err.message,\n level: 0,\n auxiliary: {\n cause: {\n value: JSON.stringify(err.cause ?? {}),\n type: \"object\",\n },\n text: {\n value: err.text ?? \"\",\n type: \"string\",\n },\n response: {\n value: JSON.stringify(err.response ?? {}),\n type: \"object\",\n },\n usage: {\n value: JSON.stringify(err.usage ?? {}),\n type: \"object\",\n },\n finishReason: {\n value: err.finishReason ?? \"unknown\",\n type: \"string\",\n },\n requestId: {\n value: options.requestId,\n type: \"string\",\n },\n },\n });\n\n throw err;\n }\n throw err;\n }\n\n const result = {\n data: objectResponse.object,\n usage: {\n prompt_tokens: objectResponse.usage.inputTokens ?? 0,\n completion_tokens: objectResponse.usage.outputTokens ?? 0,\n reasoning_tokens: objectResponse.usage.reasoningTokens ?? 0,\n cached_input_tokens: objectResponse.usage.cachedInputTokens ?? 0,\n total_tokens: objectResponse.usage.totalTokens ?? 0,\n },\n } as T;\n\n // Log LLM response for generateObject\n SessionFileLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n operation: \"generateObject\",\n output: JSON.stringify(objectResponse.object),\n inputTokens: objectResponse.usage.inputTokens,\n outputTokens: objectResponse.usage.outputTokens,\n });\n\n this.logger?.({\n category: \"aisdk\",\n message: \"response\",\n level: 1,\n auxiliary: {\n response: {\n value: JSON.stringify({\n object: objectResponse.object,\n usage: objectResponse.usage,\n finishReason: objectResponse.finishReason,\n // Omit request and response properties that might contain images\n }),\n type: \"object\",\n },\n requestId: {\n value: options.requestId,\n type: \"string\",\n },\n },\n });\n\n return result;\n }\n\n const tools: ToolSet = {};\n if (options.tools && options.tools.length > 0) {\n for (const tool of options.tools) {\n tools[tool.name] = {\n description: tool.description,\n inputSchema: tool.parameters,\n } as Tool;\n }\n }\n\n // Log LLM request for generateText (act/observe)\n const llmRequestId = uuidv7();\n const toolCount = Object.keys(tools).length;\n const promptPreview = formatLlmPromptPreview(options.messages, {\n toolCount,\n });\n SessionFileLogger.logLlmRequest({\n requestId: llmRequestId,\n model: this.model.modelId,\n operation: \"generateText\",\n prompt: promptPreview,\n });\n\n let textResponse: Awaited<ReturnType<typeof generateText>>;\n try {\n textResponse = await generateText({\n model: this.model,\n messages: formattedMessages,\n tools: Object.keys(tools).length > 0 ? tools : undefined,\n toolChoice:\n Object.keys(tools).length > 0\n ? options.tool_choice === \"required\"\n ? \"required\"\n : options.tool_choice === \"none\"\n ? \"none\"\n : \"auto\"\n : undefined,\n temperature,\n });\n } catch (err) {\n // Log error response to maintain request/response pairing\n SessionFileLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n operation: \"generateText\",\n output: `[error: ${err instanceof Error ? err.message : \"unknown\"}]`,\n });\n throw err;\n }\n\n // Transform AI SDK response to match LLMResponse format expected by operator handler\n const transformedToolCalls = (textResponse.toolCalls || []).map(\n (toolCall) => ({\n id:\n toolCall.toolCallId ||\n `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,\n type: \"function\",\n function: {\n name: toolCall.toolName,\n arguments: JSON.stringify(toolCall.input),\n },\n }),\n );\n\n const result = {\n id: `chatcmpl_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,\n object: \"chat.completion\",\n created: Math.floor(Date.now() / 1000),\n model: this.model.modelId,\n choices: [\n {\n index: 0,\n message: {\n role: \"assistant\",\n content: textResponse.text || null,\n tool_calls: transformedToolCalls,\n },\n finish_reason: textResponse.finishReason || \"stop\",\n },\n ],\n usage: {\n prompt_tokens: textResponse.usage.inputTokens ?? 0,\n completion_tokens: textResponse.usage.outputTokens ?? 0,\n reasoning_tokens: textResponse.usage.reasoningTokens ?? 0,\n cached_input_tokens: textResponse.usage.cachedInputTokens ?? 0,\n total_tokens: textResponse.usage.totalTokens ?? 0,\n },\n } as T;\n\n // Log LLM response for generateText\n SessionFileLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n operation: \"generateText\",\n output:\n textResponse.text ||\n (transformedToolCalls.length > 0\n ? `[${transformedToolCalls.length} tool calls]`\n : \"\"),\n inputTokens: textResponse.usage.inputTokens,\n outputTokens: textResponse.usage.outputTokens,\n });\n\n this.logger?.({\n category: \"aisdk\",\n message: \"response\",\n level: 2,\n auxiliary: {\n response: {\n value: JSON.stringify({\n text: textResponse.text,\n usage: textResponse.usage,\n finishReason: textResponse.finishReason,\n // Omit request and response properties that might contain images\n }),\n type: \"object\",\n },\n requestId: {\n value: options.requestId,\n type: \"string\",\n },\n },\n });\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"file":"aisdk.js","sourceRoot":"","sources":["../../../../../lib/v3/llm/aisdk.ts"],"names":[],"mappings":";;;AAAA,2BAYY;AAGZ,+BAAoC;AAGpC,iDAAwE;AACxE,oDAAuE;AACvE,kDAA+C;AAE/C,MAAa,WAAY,SAAQ,wBAAS;IACjC,IAAI,GAAG,OAAgB,CAAC;IACvB,KAAK,CAAkB;IACvB,MAAM,CAA8B;IAE5C,YAAY,EACV,KAAK,EACL,MAAM,GAIP;QACC,KAAK,CAAC,KAAK,CAAC,OAAyB,CAAC,CAAC;QACvC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,gBAAgB;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAqB,EAC7C,OAAO,GACqB;QAC5B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,QAAQ,EAAE,OAAO;YACjB,OAAO,EAAE,0BAA0B;YACnC,KAAK,EAAE,CAAC;YACR,SAAS,EAAE;gBACT,OAAO,EAAE;oBACP,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC;wBACpB,GAAG,OAAO;wBACV,KAAK,EAAE,SAAS;wBAChB,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;4BACvC,GAAG,GAAG;4BACN,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;gCACjC,CAAC,CAAC,GAAG,C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,CAAC,OAAO;YACzB,MAAM,EACJ,YAAY,CAAC,IAAI;gBACjB,CAAC,oBAAoB,CAAC,MAAM,GAAG,CAAC;oBAC9B,CAAC,CAAC,IAAI,oBAAoB,CAAC,MAAM,cAAc;oBAC/C,CAAC,CAAC,EAAE,CAAC;YACT,WAAW,EAAE,YAAY,CAAC,KAAK,CAAC,WAAW;YAC3C,YAAY,EAAE,YAAY,CAAC,KAAK,CAAC,YAAY;SAC9C,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,QAAQ,EAAE,OAAO;YACjB,OAAO,EAAE,UAAU;YACnB,KAAK,EAAE,CAAC;YACR,SAAS,EAAE;gBACT,QAAQ,EAAE;oBACR,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC;wBACpB,IAAI,EAAE,YAAY,CAAC,IAAI;wBACvB,KAAK,EAAE,YAAY,CAAC,KAAK;wBACzB,YAAY,EAAE,YAAY,CAAC,YAAY;wBACvC,iEAAiE;qBAClE,CAAC;oBACF,IAAI,EAAE,QAAQ;iBACf;gBACD,SAAS,EAAE;oBACT,KAAK,EAAE,OAAO,CAAC,SAAS;oBACxB,IAAI,EAAE,QAAQ;iBACf;aACF;SACF,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AA/XD,kCA+XC","sourcesContent":["import {\n CoreAssistantMessage,\n ModelMessage,\n CoreSystemMessage,\n CoreUserMessage,\n generateObject,\n generateText,\n ImagePart,\n NoObjectGeneratedError,\n TextPart,\n ToolSet,\n Tool,\n} from \"ai\";\nimport type { LanguageModelV2 } from \"@ai-sdk/provider\";\nimport { ChatCompletion } from \"openai/resources\";\nimport { v7 as uuidv7 } from \"uuid\";\nimport { LogLine } from \"../types/public/logs.js\";\nimport { AvailableModel } from \"../types/public/model.js\";\nimport { CreateChatCompletionOptions, LLMClient } from \"./LLMClient.js\";\nimport { FlowLogger, extractLlmPromptSummary } from \"../flowLogger.js\";\nimport { toJsonSchema } from \"../zodCompat.js\";\n\nexport class AISdkClient extends LLMClient {\n public type = \"aisdk\" as const;\n private model: LanguageModelV2;\n private logger?: (message: LogLine) => void;\n\n constructor({\n model,\n logger,\n }: {\n model: LanguageModelV2;\n logger?: (message: LogLine) => void;\n }) {\n super(model.modelId as AvailableModel);\n this.model = model;\n this.logger = logger;\n }\n\n public getLanguageModel(): LanguageModelV2 {\n return this.model;\n }\n\n async createChatCompletion<T = ChatCompletion>({\n options,\n }: CreateChatCompletionOptions): Promise<T> {\n this.logger?.({\n category: \"aisdk\",\n message: \"creating chat completion\",\n level: 2,\n auxiliary: {\n options: {\n value: JSON.stringify({\n ...options,\n image: undefined,\n messages: options.messages.map((msg) => ({\n ...msg,\n content: Array.isArray(msg.content)\n ? msg.content.map((c) =>\n \"image_url\" in c\n ? { ...c, image_url: { url: \"[IMAGE_REDACTED]\" } }\n : c,\n )\n : msg.content,\n })),\n }),\n type: \"object\",\n },\n modelName: {\n value: this.model.modelId,\n type: \"string\",\n },\n },\n });\n\n const formattedMessages: ModelMessage[] = options.messages.map(\n (message) => {\n if (Array.isArray(message.content)) {\n if (message.role === \"system\") {\n const systemMessage: CoreSystemMessage = {\n role: \"system\",\n content: message.content\n .map((c) => (\"text\" in c ? c.text : \"\"))\n .join(\"\\n\"),\n };\n return systemMessage;\n }\n\n const contentParts = message.content.map((content) => {\n if (\"image_url\" in content) {\n const imageContent: ImagePart = {\n type: \"image\",\n image: content.image_url.url,\n };\n return imageContent;\n } else {\n const textContent: TextPart = {\n type: \"text\",\n text: content.text,\n };\n return textContent;\n }\n });\n\n if (message.role === \"user\") {\n const userMessage: CoreUserMessage = {\n role: \"user\",\n content: contentParts,\n };\n return userMessage;\n } else {\n const textOnlyParts = contentParts.map((part) => ({\n type: \"text\" as const,\n text: part.type === \"image\" ? \"[Image]\" : part.text,\n }));\n const assistantMessage: CoreAssistantMessage = {\n role: \"assistant\",\n content: textOnlyParts,\n };\n return assistantMessage;\n }\n }\n\n return {\n role: message.role,\n content: message.content,\n };\n },\n );\n\n let objectResponse: Awaited<ReturnType<typeof generateObject>>;\n const isGPT5 = this.model.modelId.includes(\"gpt-5\");\n const isCodex = this.model.modelId.includes(\"codex\");\n const usesLowReasoningEffort =\n (this.model.modelId.includes(\"gpt-5.1\") ||\n this.model.modelId.includes(\"gpt-5.2\")) &&\n !isCodex;\n // Kimi models only support temperature=1\n const isKimi = this.model.modelId.includes(\"kimi\");\n const temperature = isKimi ? 1 : options.temperature;\n\n // Models that lack native structured-output support need a prompt-based\n // JSON fallback instead of response_format: { type: \"json_schema\" }.\n const PROMPT_JSON_FALLBACK_PATTERNS = [\"deepseek\", \"kimi\", \"glm\"];\n const needsPromptJsonFallback = PROMPT_JSON_FALLBACK_PATTERNS.some((p) =>\n this.model.modelId.includes(p),\n );\n\n if (options.response_model) {\n // Log LLM request for generateObject (extract)\n const llmRequestId = uuidv7();\n const promptSummary = extractLlmPromptSummary(options.messages, {\n hasSchema: true,\n });\n FlowLogger.logLlmRequest({\n requestId: llmRequestId,\n model: this.model.modelId,\n prompt: promptSummary,\n });\n\n // For models that don't support native structured outputs, add a prompt instruction\n if (needsPromptJsonFallback) {\n const parsedSchema = JSON.stringify(\n toJsonSchema(options.response_model.schema),\n );\n\n formattedMessages.push({\n role: \"user\",\n content: `Respond in this zod schema format:\\n${parsedSchema}\\n\nYou must respond in JSON format. respond WITH JSON. Do not include any other text, formatting or markdown in your output. Do not include \\`\\`\\` or \\`\\`\\`json in your response. Only the JSON object itself.`,\n });\n }\n\n try {\n objectResponse = await generateObject({\n model: this.model,\n messages: formattedMessages,\n schema: options.response_model.schema,\n temperature,\n providerOptions: isGPT5\n ? {\n openai: {\n textVerbosity: isCodex ? \"medium\" : \"low\", // codex models only support 'medium'\n reasoningEffort: isCodex\n ? \"medium\"\n : usesLowReasoningEffort\n ? \"low\"\n : \"minimal\",\n },\n }\n : undefined,\n });\n } catch (err) {\n // Log error response to maintain request/response pairing\n FlowLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n output: `[error: ${err instanceof Error ? err.message : \"unknown\"}]`,\n });\n\n if (NoObjectGeneratedError.isInstance(err)) {\n this.logger?.({\n category: \"AISDK error\",\n message: err.message,\n level: 0,\n auxiliary: {\n cause: {\n value: JSON.stringify(err.cause ?? {}),\n type: \"object\",\n },\n text: {\n value: err.text ?? \"\",\n type: \"string\",\n },\n response: {\n value: JSON.stringify(err.response ?? {}),\n type: \"object\",\n },\n usage: {\n value: JSON.stringify(err.usage ?? {}),\n type: \"object\",\n },\n finishReason: {\n value: err.finishReason ?? \"unknown\",\n type: \"string\",\n },\n requestId: {\n value: options.requestId,\n type: \"string\",\n },\n },\n });\n\n throw err;\n }\n throw err;\n }\n\n const result = {\n data: objectResponse.object,\n usage: {\n prompt_tokens: objectResponse.usage.inputTokens ?? 0,\n completion_tokens: objectResponse.usage.outputTokens ?? 0,\n reasoning_tokens: objectResponse.usage.reasoningTokens ?? 0,\n cached_input_tokens: objectResponse.usage.cachedInputTokens ?? 0,\n total_tokens: objectResponse.usage.totalTokens ?? 0,\n },\n } as T;\n\n // Log LLM response for generateObject\n FlowLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n output: JSON.stringify(objectResponse.object),\n inputTokens: objectResponse.usage.inputTokens,\n outputTokens: objectResponse.usage.outputTokens,\n });\n\n this.logger?.({\n category: \"aisdk\",\n message: \"response\",\n level: 1,\n auxiliary: {\n response: {\n value: JSON.stringify({\n object: objectResponse.object,\n usage: objectResponse.usage,\n finishReason: objectResponse.finishReason,\n // Omit request and response properties that might contain images\n }),\n type: \"object\",\n },\n requestId: {\n value: options.requestId,\n type: \"string\",\n },\n },\n });\n\n return result;\n }\n\n const tools: ToolSet = {};\n if (options.tools && options.tools.length > 0) {\n for (const tool of options.tools) {\n tools[tool.name] = {\n description: tool.description,\n inputSchema: tool.parameters,\n } as Tool;\n }\n }\n\n // Log LLM request for generateText (act/observe)\n const llmRequestId = uuidv7();\n const toolCount = Object.keys(tools).length;\n const promptSummary = extractLlmPromptSummary(options.messages, {\n toolCount,\n });\n FlowLogger.logLlmRequest({\n requestId: llmRequestId,\n model: this.model.modelId,\n prompt: promptSummary,\n });\n\n let textResponse: Awaited<ReturnType<typeof generateText>>;\n try {\n textResponse = await generateText({\n model: this.model,\n messages: formattedMessages,\n tools: Object.keys(tools).length > 0 ? tools : undefined,\n toolChoice:\n Object.keys(tools).length > 0\n ? options.tool_choice === \"required\"\n ? \"required\"\n : options.tool_choice === \"none\"\n ? \"none\"\n : \"auto\"\n : undefined,\n temperature,\n });\n } catch (err) {\n // Log error response to maintain request/response pairing\n FlowLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n output: `[error: ${err instanceof Error ? err.message : \"unknown\"}]`,\n });\n throw err;\n }\n\n // Transform AI SDK response to match LLMResponse format expected by operator handler\n const transformedToolCalls = (textResponse.toolCalls || []).map(\n (toolCall) => ({\n id:\n toolCall.toolCallId ||\n `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,\n type: \"function\",\n function: {\n name: toolCall.toolName,\n arguments: JSON.stringify(toolCall.input),\n },\n }),\n );\n\n const result = {\n id: `chatcmpl_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,\n object: \"chat.completion\",\n created: Math.floor(Date.now() / 1000),\n model: this.model.modelId,\n choices: [\n {\n index: 0,\n message: {\n role: \"assistant\",\n content: textResponse.text || null,\n tool_calls: transformedToolCalls,\n },\n finish_reason: textResponse.finishReason || \"stop\",\n },\n ],\n usage: {\n prompt_tokens: textResponse.usage.inputTokens ?? 0,\n completion_tokens: textResponse.usage.outputTokens ?? 0,\n reasoning_tokens: textResponse.usage.reasoningTokens ?? 0,\n cached_input_tokens: textResponse.usage.cachedInputTokens ?? 0,\n total_tokens: textResponse.usage.totalTokens ?? 0,\n },\n } as T;\n\n // Log LLM response for generateText\n FlowLogger.logLlmResponse({\n requestId: llmRequestId,\n model: this.model.modelId,\n output:\n textResponse.text ||\n (transformedToolCalls.length > 0\n ? `[${transformedToolCalls.length} tool calls]`\n : \"\"),\n inputTokens: textResponse.usage.inputTokens,\n outputTokens: textResponse.usage.outputTokens,\n });\n\n this.logger?.({\n category: \"aisdk\",\n message: \"response\",\n level: 2,\n auxiliary: {\n response: {\n value: JSON.stringify({\n text: textResponse.text,\n usage: textResponse.usage,\n finishReason: textResponse.finishReason,\n // Omit request and response properties that might contain images\n }),\n type: \"object\",\n },\n requestId: {\n value: options.requestId,\n type: \"string\",\n },\n },\n });\n\n return result;\n }\n}\n"]}
@@ -275,7 +275,7 @@ export interface AgentExecuteOptionsBase {
275
275
  * - `think` - Agent reasoning/planning step
276
276
  * - `wait` - Wait for time or condition
277
277
  * - `done` - Mark task as complete
278
- * - `search` - Web search (requires BRAVE_API_KEY)
278
+ * - `search` - Web search (requires useSearch: true and BROWSERBASE_API_KEY)
279
279
  *
280
280
  * **Hybrid mode:**
281
281
  * - `click` - Click at specific coordinates
@@ -294,7 +294,7 @@ export interface AgentExecuteOptionsBase {
294
294
  * - `think` - Agent reasoning step
295
295
  * - `wait` - Wait for time/condition
296
296
  * - `done` - Mark task complete
297
- * - `search` - Web search (requires BRAVE_API_KEY)
297
+ * - `search` - Web search (requires useSearch: true and BROWSERBASE_API_KEY)
298
298
  *
299
299
  * @experimental
300
300
  * @example
@@ -359,6 +359,20 @@ export interface AgentExecuteOptionsBase {
359
359
  * @default 45000 (45 seconds)
360
360
  */
361
361
  toolTimeout?: number;
362
+ /**
363
+ * Enable the web search tool powered by Browserbase Search API.
364
+ * Requires a valid Browserbase API key (BROWSERBASE_API_KEY).
365
+ * When set to true, the agent gains access to a `search` tool for web searches.
366
+ *
367
+ * @example
368
+ * ```typescript
369
+ * const result = await agent.execute({
370
+ * instruction: "Find the latest news about AI",
371
+ * useSearch: true,
372
+ * });
373
+ * ```
374
+ */
375
+ useSearch?: boolean;
362
376
  }
363
377
  /**
364
378
  * Options for non-streaming agent execution.
@@ -1 +1 @@
1
- {"version":3,"file":"agent.js","sourceRoot":"","sources":["../../../../../../lib/v3/types/public/agent.ts"],"names":[],"mappings":";;;AAoba,QAAA,oBAAoB,GAAG;IAClC,6BAA6B;IAC7B,wCAAwC;IACxC,oCAAoC;IACpC,2BAA2B;IAC3B,6BAA6B;IAC7B,qCAAqC;IACrC,oCAAoC;IACpC,sCAAsC;IACtC,gDAAgD;IAChD,+BAA+B;IAC/B,6BAA6B;IAC7B,mBAAmB;CACX,CAAC","sourcesContent":["import type { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport {\n ToolSet,\n ModelMessage,\n wrapLanguageModel,\n StreamTextResult,\n StepResult,\n PrepareStepFunction,\n GenerateTextOnStepFinishCallback,\n StreamTextOnStepFinishCallback,\n StreamTextOnErrorCallback,\n StreamTextOnChunkCallback,\n StreamTextOnFinishCallback,\n} from \"ai\";\nimport { LogLine } from \"./logs.js\";\nimport { ClientOptions } from \"./model.js\";\nimport { StagehandZodObject } from \"../../zodCompat.js\";\n\n// Re-export ModelMessage for consumers who want to use it for conversation continuation\nexport type { ModelMessage } from \"ai\";\n\n// Re-export Tool type for consumers who want to define custom tools\nexport type { Tool } from \"ai\";\nimport { Page as PlaywrightPage } from \"playwright-core\";\nimport { Page as PuppeteerPage } from \"puppeteer-core\";\nimport { Page as PatchrightPage } from \"patchright-core\";\nimport { Page } from \"../../understudy/page.js\";\n\n// =============================================================================\n// Variable Types\n// =============================================================================\n\n/**\n * A variable value can be a simple primitive or a rich object with an optional description.\n * This unified type is shared across `act`, `agent.execute`, and other methods.\n *\n * @example Simple (backward-compatible):\n * ```typescript\n * variables: { username: \"john@example.com\" }\n * ```\n *\n * @example Rich with description (useful for agents):\n * ```typescript\n * variables: {\n * username: { value: \"john@example.com\", description: \"The login email\" }\n * }\n * ```\n */\nexport type VariableValue =\n | string\n | number\n | boolean\n | { value: string | number | boolean; description?: string };\n\n/**\n * A collection of named variables for use in act, agent, and other methods.\n */\nexport type Variables = Record<string, VariableValue>;\n\nexport interface AgentContext {\n options: AgentExecuteOptionsBase;\n maxSteps: number;\n systemPrompt: string;\n allTools: ToolSet;\n messages: ModelMessage[];\n wrappedModel: ReturnType<typeof wrapLanguageModel>;\n initialPageUrl: string;\n}\n\nexport interface AgentState {\n collectedReasoning: string[];\n actions: AgentAction[];\n finalMessage: string;\n completed: boolean;\n currentPageUrl: string;\n}\n\nexport interface AgentAction {\n type: string;\n reasoning?: string;\n taskCompleted?: boolean;\n action?: string;\n // Tool-specific fields\n timeMs?: number; // wait tool\n pageText?: string; // ariaTree tool\n pageUrl?: string; // ariaTree tool\n instruction?: string; // various tools\n [key: string]: unknown;\n}\n\nexport interface AgentResult {\n success: boolean;\n message: string;\n actions: AgentAction[];\n completed: boolean;\n metadata?: Record<string, unknown>;\n usage?: {\n input_tokens: number;\n output_tokens: number;\n reasoning_tokens?: number;\n cached_input_tokens?: number;\n inference_time_ms: number;\n };\n /**\n * The conversation messages from this execution.\n * Pass these to a subsequent execute() call via the `messages` option to continue the conversation.\n * @experimental\n */\n messages?: ModelMessage[];\n /**\n * Custom output data extracted based on the `output` schema provided in execute options.\n * Only populated if an `output` schema was provided.\n * @experimental\n */\n output?: Record<string, unknown>;\n}\n\nexport type AgentStreamResult = StreamTextResult<ToolSet, never> & {\n result: Promise<AgentResult>;\n};\n\n/**\n * Base callbacks shared between execute (non-streaming) and streaming modes.\n */\nexport interface AgentCallbacks {\n /**\n * Optional function called before each step to modify settings.\n * You can change the model, tool choices, active tools, system prompt,\n * and input messages for each step.\n */\n prepareStep?: PrepareStepFunction<ToolSet>;\n /**\n * Callback called when each step (LLM call) is finished.\n * This is called for intermediate steps as well as the final step.\n */\n onStepFinish?:\n | GenerateTextOnStepFinishCallback<ToolSet>\n | StreamTextOnStepFinishCallback<ToolSet>;\n}\n\n/**\n * Error message type for streaming-only callbacks used in non-streaming mode.\n * This provides a clear error message when users try to use streaming callbacks without stream: true.\n */\ntype StreamingCallbackNotAvailable =\n \"This callback requires 'stream: true' in AgentConfig. Set stream: true to use streaming callbacks like onChunk, onFinish, onError, and onAbort.\";\n\n/**\n * Error message for safety confirmation callback misuse.\n * Safety confirmations are only available for non-streaming CUA agent executions.\n */\ntype SafetyConfirmationCallbackNotAvailable =\n \"Safety confirmation callbacks are only available via non-streaming AgentExecuteOptions.callbacks when using mode: 'cua'.\";\n\n/**\n * Callbacks specific to the non-streaming execute method.\n */\nexport interface AgentExecuteCallbacks extends AgentCallbacks {\n /**\n * Callback called when each step (LLM call) is finished.\n */\n onStepFinish?: GenerateTextOnStepFinishCallback<ToolSet>;\n /**\n * Callback for handling safety confirmation requests from CUA providers.\n * Only available when running an agent configured with mode: \"cua\".\n */\n onSafetyConfirmation?: SafetyConfirmationHandler;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onChunk:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onChunk: async (chunk) => console.log(chunk) }\n * });\n * ```\n */\n onChunk?: StreamingCallbackNotAvailable;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onFinish:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onFinish: (event) => console.log(\"Done!\", event) }\n * });\n * ```\n */\n onFinish?: StreamingCallbackNotAvailable;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onError:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onError: ({ error }) => console.error(error) }\n * });\n * ```\n */\n onError?: StreamingCallbackNotAvailable;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onAbort:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onAbort: (event) => console.log(\"Aborted\", event.steps) }\n * });\n * ```\n */\n onAbort?: StreamingCallbackNotAvailable;\n}\n\n/**\n * Callbacks specific to the streaming mode.\n */\nexport interface AgentStreamCallbacks extends AgentCallbacks {\n /**\n * Callback called when each step (LLM call) is finished during streaming.\n */\n onStepFinish?: StreamTextOnStepFinishCallback<ToolSet>;\n /**\n * Callback called when an error occurs during streaming.\n * Use this to log errors or handle error states.\n */\n onError?: StreamTextOnErrorCallback;\n /**\n * Callback called for each chunk of the stream.\n * Stream processing will pause until the callback promise resolves.\n */\n onChunk?: StreamTextOnChunkCallback<ToolSet>;\n /**\n * Callback called when the stream finishes.\n */\n onFinish?: StreamTextOnFinishCallback<ToolSet>;\n /**\n * Callback called when the stream is aborted.\n */\n onAbort?: (event: {\n steps: Array<StepResult<ToolSet>>;\n }) => PromiseLike<void> | void;\n /**\n * NOT AVAILABLE in streaming mode.\n * Safety confirmations currently require non-streaming execute() on CUA agents.\n */\n onSafetyConfirmation?: SafetyConfirmationCallbackNotAvailable;\n}\n\n/**\n * Base options for agent execution (without callbacks).\n */\nexport interface AgentExecuteOptionsBase {\n instruction: string;\n maxSteps?: number;\n page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;\n highlightCursor?: boolean;\n /**\n * Previous conversation messages to continue from.\n * Pass the `messages` from a previous AgentResult to continue that conversation.\n * @experimental\n */\n messages?: ModelMessage[];\n /**\n * An AbortSignal that can be used to cancel the agent execution.\n * When aborted, the agent will stop and return a partial result.\n * @experimental\n *\n * @example\n * ```typescript\n * const controller = new AbortController();\n * setTimeout(() => controller.abort(), 30000); // 30 second timeout\n *\n * const result = await agent.execute({\n * instruction: \"...\",\n * signal: controller.signal\n * });\n * ```\n */\n signal?: AbortSignal;\n /**\n * Tools to exclude from this execution.\n * Pass an array of tool names to prevent the agent from using those tools.\n *\n * **Note:** Not supported in CUA mode (`mode: \"cua\"`).\n *\n * **Available tools by mode:**\n *\n * **DOM mode (default):**\n * - `act` - Perform semantic actions (click, type, etc.)\n * - `fillForm` - Fill form fields using DOM selectors\n * - `ariaTree` - Get accessibility tree of the page\n * - `extract` - Extract structured data from page\n * - `goto` - Navigate to a URL\n * - `scroll` - Scroll using semantic directions (up/down/left/right)\n * - `keys` - Press keyboard keys\n * - `navback` - Navigate back in history\n * - `screenshot` - Take a screenshot\n * - `think` - Agent reasoning/planning step\n * - `wait` - Wait for time or condition\n * - `done` - Mark task as complete\n * - `search` - Web search (requires BRAVE_API_KEY)\n *\n * **Hybrid mode:**\n * - `click` - Click at specific coordinates\n * - `type` - Type text at coordinates\n * - `dragAndDrop` - Drag from one point to another\n * - `clickAndHold` - Click and hold at coordinates\n * - `fillFormVision` - Fill forms using vision/coordinates\n * - `act` - Perform semantic actions\n * - `ariaTree` - Get accessibility tree\n * - `extract` - Extract data from page\n * - `goto` - Navigate to URL\n * - `scroll` - Scroll using coordinates\n * - `keys` - Press keyboard keys\n * - `navback` - Navigate back\n * - `screenshot` - Take screenshot\n * - `think` - Agent reasoning step\n * - `wait` - Wait for time/condition\n * - `done` - Mark task complete\n * - `search` - Web search (requires BRAVE_API_KEY)\n *\n * @experimental\n * @example\n * ```typescript\n * // Exclude screenshot and extract tools\n * const result = await agent.execute({\n * instruction: \"Click the submit button\",\n * excludeTools: [\"screenshot\", \"extract\"]\n * });\n * ```\n */\n excludeTools?: string[];\n /**\n * A Zod schema defining custom output data to return when the task completes.\n * The agent will populate this data in the final done tool call.\n *\n * @experimental\n * @example\n * ```typescript\n * const result = await agent.execute({\n * instruction: \"Find the cheapest flight from NYC to LA\",\n * output: z.object({\n * price: z.string().describe(\"The price of the flight\"),\n * airline: z.string().describe(\"The airline name\"),\n * departureTime: z.string().describe(\"Departure time\"),\n * }),\n * });\n *\n * console.log(result.output); // { price: \"$199\", airline: \"Delta\", departureTime: \"8:00 AM\" }\n * ```\n */\n output?: StagehandZodObject;\n /**\n * Variables that the agent can use when filling forms or typing text.\n * The agent will see variable names and descriptions in the system prompt,\n * and can use them via `%variableName%` syntax in act/type/fillForm tool calls.\n *\n * Accepts both simple values and rich objects with descriptions (same type as `act`).\n *\n * **Note:** Not supported in CUA mode (`mode: \"cua\"`). Requires `experimental: true`.\n *\n * @experimental\n * @example\n * ```typescript\n * // Simple values\n * variables: { username: \"john@example.com\", password: \"secret123\" }\n *\n * // Rich values with descriptions (helps the agent understand context)\n * variables: {\n * username: { value: \"john@example.com\", description: \"The login email\" },\n * password: { value: \"secret123\", description: \"The login password\" },\n * }\n * ```\n */\n variables?: Variables;\n /**\n * Timeout in milliseconds for each agent tool call.\n * If a tool call exceeds this duration, it will be aborted and\n * reported back to the LLM as a timeout error so it can retry or adjust.\n * For tools that call v3 methods (act, extract, fillForm, ariaTree), the\n * timeout is also forwarded to the underlying v3 call for true cancellation.\n * @default 45000 (45 seconds)\n */\n toolTimeout?: number;\n}\n\n/**\n * Options for non-streaming agent execution.\n * Only accepts AgentExecuteCallbacks (no streaming-specific callbacks like onChunk, onFinish).\n */\nexport interface AgentExecuteOptions extends AgentExecuteOptionsBase {\n /**\n * Callbacks for non-streaming agent execution.\n * For streaming callbacks (onChunk, onFinish, onError, onAbort), use stream: true in AgentConfig.\n */\n callbacks?: AgentExecuteCallbacks;\n}\n\n/**\n * Options for streaming agent execution.\n * Accepts AgentStreamCallbacks including onChunk, onFinish, onError, and onAbort.\n */\nexport interface AgentStreamExecuteOptions extends AgentExecuteOptionsBase {\n /**\n * Callbacks for streaming agent execution.\n * Includes streaming-specific callbacks: onChunk, onFinish, onError, onAbort.\n */\n callbacks?: AgentStreamCallbacks;\n}\nexport type AgentType =\n | \"openai\"\n | \"anthropic\"\n | \"google\"\n | \"microsoft\"\n | \"bedrock\";\n\nexport const AVAILABLE_CUA_MODELS = [\n \"openai/computer-use-preview\",\n \"openai/computer-use-preview-2025-03-11\",\n \"anthropic/claude-opus-4-5-20251101\",\n \"anthropic/claude-opus-4-6\",\n \"anthropic/claude-sonnet-4-6\",\n \"anthropic/claude-haiku-4-5-20251001\",\n \"anthropic/claude-sonnet-4-20250514\",\n \"anthropic/claude-sonnet-4-5-20250929\",\n \"google/gemini-2.5-computer-use-preview-10-2025\",\n \"google/gemini-3-flash-preview\",\n \"google/gemini-3-pro-preview\",\n \"microsoft/fara-7b\",\n] as const;\nexport type AvailableCuaModel = (typeof AVAILABLE_CUA_MODELS)[number];\n\nexport interface AgentExecutionOptions<\n TOptions extends AgentExecuteOptions = AgentExecuteOptions,\n> {\n options: TOptions;\n logger: (message: LogLine) => void;\n retries?: number;\n}\n\nexport interface AgentHandlerOptions {\n modelName: string;\n clientOptions?: ClientOptions;\n userProvidedInstructions?: string;\n experimental?: boolean;\n}\n\nexport interface ActionExecutionResult {\n success: boolean;\n error?: string;\n data?: unknown;\n}\n\n/**\n * Represents a safety check that requires user confirmation before proceeding.\n * These are issued by CUA providers (OpenAI, Google) when the agent attempts\n * potentially risky actions.\n */\nexport interface SafetyCheck {\n /** Unique identifier for this safety check */\n id: string;\n /** Code identifying the type of safety concern */\n code: string;\n /** Human-readable description of the safety concern */\n message: string;\n}\n\n/**\n * Response from the user for a safety confirmation request.\n */\nexport interface SafetyConfirmationResponse {\n /** Whether the user acknowledged/approved the safety checks */\n acknowledged: boolean;\n}\n\n/**\n * Callback for handling safety confirmation requests.\n * Called when the CUA provider issues safety checks that require user confirmation.\n * The callback should return a promise that resolves when the user has made a decision.\n *\n * @param safetyChecks - Array of safety checks requiring confirmation\n * @returns Promise resolving to the user's response\n *\n * @example\n * ```typescript\n * const agent = stagehand.agent({\n * mode: \"cua\",\n * });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: {\n * onSafetyConfirmation: async (checks) => {\n * console.log(\"Safety checks:\", checks);\n * const userApproved = await showConfirmationDialog(checks);\n * return { acknowledged: userApproved };\n * },\n * },\n * });\n * ```\n */\nexport type SafetyConfirmationHandler = (\n safetyChecks: SafetyCheck[],\n) => Promise<SafetyConfirmationResponse>;\n\n// Anthropic types:\n\nexport interface ToolUseItem extends ResponseItem {\n type: \"tool_use\";\n id: string; // This is the correct property name from Anthropic's API\n name: string; // Name of the tool being used\n input: Record<string, unknown>;\n}\n\nexport interface AnthropicMessage {\n role: string;\n content: string | Array<AnthropicContentBlock>;\n}\n\nexport interface AnthropicContentBlock {\n type: string;\n [key: string]: unknown;\n}\n\nexport interface AnthropicTextBlock extends AnthropicContentBlock {\n type: \"text\";\n text: string;\n}\n\nexport interface AnthropicToolResult {\n type: \"tool_result\";\n tool_use_id: string;\n content: string | Array<AnthropicContentBlock>;\n}\n\n// OpenAI types:\n\nexport interface ResponseItem {\n type: string;\n id: string;\n [key: string]: unknown;\n}\n\nexport interface ComputerCallItem extends ResponseItem {\n type: \"computer_call\";\n call_id: string;\n action: {\n type: string;\n [key: string]: unknown;\n };\n pending_safety_checks?: Array<{\n id: string;\n code: string;\n message: string;\n }>;\n}\n\nexport interface FunctionCallItem extends ResponseItem {\n type: \"function_call\";\n call_id: string;\n name: string;\n arguments: string;\n}\n\nexport type ResponseInputItem =\n | { role: string; content: string }\n | {\n type: \"computer_call_output\";\n call_id: string;\n output:\n | {\n type: \"input_image\";\n image_url: string;\n current_url?: string;\n error?: string;\n [key: string]: unknown;\n }\n | string;\n acknowledged_safety_checks?: Array<{\n id: string;\n code: string;\n message: string;\n }>;\n }\n | {\n type: \"function_call_output\";\n call_id: string;\n output: string;\n };\n\nexport interface AgentInstance {\n execute: (\n instructionOrOptions: string | AgentExecuteOptions,\n ) => Promise<AgentResult>;\n}\n\nexport type AgentProviderType = AgentType;\n\nexport type AgentModelConfig<TModelName extends string = string> = {\n modelName: TModelName;\n} & Record<string, unknown>;\n\n/**\n * Agent tool mode determines which set of tools are available to the agent.\n * - 'dom': Uses DOM-based tools (act, fillForm) - better for structured page interactions\n * - 'hybrid': Uses coordinate-based tools (click, type, dragAndDrop, etc.) - better for visual/screenshot-based interactions\n * - 'cua': Uses Computer Use Agent (CUA) providers like Anthropic Claude or Google Gemini for screenshot-based automation\n */\nexport type AgentToolMode = \"dom\" | \"hybrid\" | \"cua\";\n\nexport type AgentConfig = {\n /**\n * Custom system prompt to provide to the agent. Overrides the default system prompt.\n */\n systemPrompt?: string;\n /**\n * MCP integrations - Array of Client objects\n */\n integrations?: (Client | string)[];\n /**\n * Tools passed to the agent client\n */\n tools?: ToolSet;\n /**\n * @deprecated Use `mode: \"cua\"` instead. This option will be removed in a future version.\n * Enables Computer Use Agent (CUA) mode.\n */\n cua?: boolean;\n /**\n * The model to use for agent functionality\n */\n model?: string | AgentModelConfig<string>;\n /**\n * The model to use for tool execution (observe/act calls within agent tools).\n * If not specified, inherits from the main model configuration.\n * Format: \"provider/model\" (e.g., \"openai/gpt-4o-mini\", \"google/gemini-2.0-flash-exp\")\n */\n executionModel?: string | AgentModelConfig<string>;\n /**\n * Enable streaming mode for the agent.\n * When true, execute() returns AgentStreamResult with textStream for incremental output.\n * When false (default), execute() returns AgentResult after completion.\n */\n stream?: boolean;\n /**\n * Tool mode for the agent. Determines which set of tools are available.\n * - 'dom' (default): Uses DOM-based tools (act, fillForm) for structured interactions\n * - 'hybrid': Uses coordinate-based tools (click, type, dragAndDrop, clickAndHold, fillFormVision)\n * for visual/screenshot-based interactions\n * - 'cua': Uses Computer Use Agent (CUA) providers for screenshot-based automation\n */\n mode?: AgentToolMode;\n};\n\n/**\n * Agent instance returned when stream: true is set in AgentConfig.\n * execute() returns a streaming result that can be consumed incrementally.\n * Accepts AgentStreamExecuteOptions with streaming-specific callbacks.\n */\nexport interface StreamingAgentInstance {\n execute: (\n instructionOrOptions: string | AgentStreamExecuteOptions,\n ) => Promise<AgentStreamResult>;\n}\n\n/**\n * Agent instance returned when stream is false or not set in AgentConfig.\n * execute() returns a result after the agent completes.\n * Accepts AgentExecuteOptions with non-streaming callbacks only.\n */\nexport interface NonStreamingAgentInstance {\n execute: (\n instructionOrOptions: string | AgentExecuteOptions,\n ) => Promise<AgentResult>;\n}\n\n// =============================================================================\n// Vision Action Tool Result Types\n// =============================================================================\n\n/**\n * Content item type for toModelOutput return values.\n * Used in tool definitions to return text and/or media to the model.\n */\nexport type ModelOutputContentItem =\n | { type: \"text\"; text: string }\n | { type: \"media\"; mediaType: string; data: string };\n\nexport interface ClickToolResult {\n success: boolean;\n describe?: string;\n coordinates?: number[];\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface TypeToolResult {\n success: boolean;\n describe?: string;\n text?: string;\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface DragAndDropToolResult {\n success: boolean;\n describe?: string;\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface FillFormField {\n action: string;\n value: string;\n coordinates: { x: number; y: number };\n}\n\nexport interface FillFormVisionToolResult {\n success: boolean;\n playwrightArguments?: FillFormField[];\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface ScrollToolResult {\n success: boolean;\n message: string;\n scrolledPixels: number;\n error?: string;\n}\n\nexport interface ScrollVisionToolResult extends ScrollToolResult {\n screenshotBase64?: string;\n}\n\nexport interface WaitToolResult {\n success: boolean;\n waited: number;\n screenshotBase64?: string;\n error?: string;\n}\n"]}
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../../../../../../lib/v3/types/public/agent.ts"],"names":[],"mappings":";;;AAkca,QAAA,oBAAoB,GAAG;IAClC,6BAA6B;IAC7B,wCAAwC;IACxC,oCAAoC;IACpC,2BAA2B;IAC3B,6BAA6B;IAC7B,qCAAqC;IACrC,oCAAoC;IACpC,sCAAsC;IACtC,gDAAgD;IAChD,+BAA+B;IAC/B,6BAA6B;IAC7B,mBAAmB;CACX,CAAC","sourcesContent":["import type { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport {\n ToolSet,\n ModelMessage,\n wrapLanguageModel,\n StreamTextResult,\n StepResult,\n PrepareStepFunction,\n GenerateTextOnStepFinishCallback,\n StreamTextOnStepFinishCallback,\n StreamTextOnErrorCallback,\n StreamTextOnChunkCallback,\n StreamTextOnFinishCallback,\n} from \"ai\";\nimport { LogLine } from \"./logs.js\";\nimport { ClientOptions } from \"./model.js\";\nimport { StagehandZodObject } from \"../../zodCompat.js\";\n\n// Re-export ModelMessage for consumers who want to use it for conversation continuation\nexport type { ModelMessage } from \"ai\";\n\n// Re-export Tool type for consumers who want to define custom tools\nexport type { Tool } from \"ai\";\nimport { Page as PlaywrightPage } from \"playwright-core\";\nimport { Page as PuppeteerPage } from \"puppeteer-core\";\nimport { Page as PatchrightPage } from \"patchright-core\";\nimport { Page } from \"../../understudy/page.js\";\n\n// =============================================================================\n// Variable Types\n// =============================================================================\n\n/**\n * A variable value can be a simple primitive or a rich object with an optional description.\n * This unified type is shared across `act`, `agent.execute`, and other methods.\n *\n * @example Simple (backward-compatible):\n * ```typescript\n * variables: { username: \"john@example.com\" }\n * ```\n *\n * @example Rich with description (useful for agents):\n * ```typescript\n * variables: {\n * username: { value: \"john@example.com\", description: \"The login email\" }\n * }\n * ```\n */\nexport type VariableValue =\n | string\n | number\n | boolean\n | { value: string | number | boolean; description?: string };\n\n/**\n * A collection of named variables for use in act, agent, and other methods.\n */\nexport type Variables = Record<string, VariableValue>;\n\nexport interface AgentContext {\n options: AgentExecuteOptionsBase;\n maxSteps: number;\n systemPrompt: string;\n allTools: ToolSet;\n messages: ModelMessage[];\n wrappedModel: ReturnType<typeof wrapLanguageModel>;\n initialPageUrl: string;\n}\n\nexport interface AgentState {\n collectedReasoning: string[];\n actions: AgentAction[];\n finalMessage: string;\n completed: boolean;\n currentPageUrl: string;\n}\n\nexport interface AgentAction {\n type: string;\n reasoning?: string;\n taskCompleted?: boolean;\n action?: string;\n // Tool-specific fields\n timeMs?: number; // wait tool\n pageText?: string; // ariaTree tool\n pageUrl?: string; // ariaTree tool\n instruction?: string; // various tools\n [key: string]: unknown;\n}\n\nexport interface AgentResult {\n success: boolean;\n message: string;\n actions: AgentAction[];\n completed: boolean;\n metadata?: Record<string, unknown>;\n usage?: {\n input_tokens: number;\n output_tokens: number;\n reasoning_tokens?: number;\n cached_input_tokens?: number;\n inference_time_ms: number;\n };\n /**\n * The conversation messages from this execution.\n * Pass these to a subsequent execute() call via the `messages` option to continue the conversation.\n * @experimental\n */\n messages?: ModelMessage[];\n /**\n * Custom output data extracted based on the `output` schema provided in execute options.\n * Only populated if an `output` schema was provided.\n * @experimental\n */\n output?: Record<string, unknown>;\n}\n\nexport type AgentStreamResult = StreamTextResult<ToolSet, never> & {\n result: Promise<AgentResult>;\n};\n\n/**\n * Base callbacks shared between execute (non-streaming) and streaming modes.\n */\nexport interface AgentCallbacks {\n /**\n * Optional function called before each step to modify settings.\n * You can change the model, tool choices, active tools, system prompt,\n * and input messages for each step.\n */\n prepareStep?: PrepareStepFunction<ToolSet>;\n /**\n * Callback called when each step (LLM call) is finished.\n * This is called for intermediate steps as well as the final step.\n */\n onStepFinish?:\n | GenerateTextOnStepFinishCallback<ToolSet>\n | StreamTextOnStepFinishCallback<ToolSet>;\n}\n\n/**\n * Error message type for streaming-only callbacks used in non-streaming mode.\n * This provides a clear error message when users try to use streaming callbacks without stream: true.\n */\ntype StreamingCallbackNotAvailable =\n \"This callback requires 'stream: true' in AgentConfig. Set stream: true to use streaming callbacks like onChunk, onFinish, onError, and onAbort.\";\n\n/**\n * Error message for safety confirmation callback misuse.\n * Safety confirmations are only available for non-streaming CUA agent executions.\n */\ntype SafetyConfirmationCallbackNotAvailable =\n \"Safety confirmation callbacks are only available via non-streaming AgentExecuteOptions.callbacks when using mode: 'cua'.\";\n\n/**\n * Callbacks specific to the non-streaming execute method.\n */\nexport interface AgentExecuteCallbacks extends AgentCallbacks {\n /**\n * Callback called when each step (LLM call) is finished.\n */\n onStepFinish?: GenerateTextOnStepFinishCallback<ToolSet>;\n /**\n * Callback for handling safety confirmation requests from CUA providers.\n * Only available when running an agent configured with mode: \"cua\".\n */\n onSafetyConfirmation?: SafetyConfirmationHandler;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onChunk:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onChunk: async (chunk) => console.log(chunk) }\n * });\n * ```\n */\n onChunk?: StreamingCallbackNotAvailable;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onFinish:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onFinish: (event) => console.log(\"Done!\", event) }\n * });\n * ```\n */\n onFinish?: StreamingCallbackNotAvailable;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onError:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onError: ({ error }) => console.error(error) }\n * });\n * ```\n */\n onError?: StreamingCallbackNotAvailable;\n\n /**\n * NOT AVAILABLE in non-streaming mode.\n * This callback requires `stream: true` in AgentConfig.\n *\n * @example\n * ```typescript\n * // Enable streaming to use onAbort:\n * const agent = stagehand.agent({ stream: true });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: { onAbort: (event) => console.log(\"Aborted\", event.steps) }\n * });\n * ```\n */\n onAbort?: StreamingCallbackNotAvailable;\n}\n\n/**\n * Callbacks specific to the streaming mode.\n */\nexport interface AgentStreamCallbacks extends AgentCallbacks {\n /**\n * Callback called when each step (LLM call) is finished during streaming.\n */\n onStepFinish?: StreamTextOnStepFinishCallback<ToolSet>;\n /**\n * Callback called when an error occurs during streaming.\n * Use this to log errors or handle error states.\n */\n onError?: StreamTextOnErrorCallback;\n /**\n * Callback called for each chunk of the stream.\n * Stream processing will pause until the callback promise resolves.\n */\n onChunk?: StreamTextOnChunkCallback<ToolSet>;\n /**\n * Callback called when the stream finishes.\n */\n onFinish?: StreamTextOnFinishCallback<ToolSet>;\n /**\n * Callback called when the stream is aborted.\n */\n onAbort?: (event: {\n steps: Array<StepResult<ToolSet>>;\n }) => PromiseLike<void> | void;\n /**\n * NOT AVAILABLE in streaming mode.\n * Safety confirmations currently require non-streaming execute() on CUA agents.\n */\n onSafetyConfirmation?: SafetyConfirmationCallbackNotAvailable;\n}\n\n/**\n * Base options for agent execution (without callbacks).\n */\nexport interface AgentExecuteOptionsBase {\n instruction: string;\n maxSteps?: number;\n page?: PlaywrightPage | PuppeteerPage | PatchrightPage | Page;\n highlightCursor?: boolean;\n /**\n * Previous conversation messages to continue from.\n * Pass the `messages` from a previous AgentResult to continue that conversation.\n * @experimental\n */\n messages?: ModelMessage[];\n /**\n * An AbortSignal that can be used to cancel the agent execution.\n * When aborted, the agent will stop and return a partial result.\n * @experimental\n *\n * @example\n * ```typescript\n * const controller = new AbortController();\n * setTimeout(() => controller.abort(), 30000); // 30 second timeout\n *\n * const result = await agent.execute({\n * instruction: \"...\",\n * signal: controller.signal\n * });\n * ```\n */\n signal?: AbortSignal;\n /**\n * Tools to exclude from this execution.\n * Pass an array of tool names to prevent the agent from using those tools.\n *\n * **Note:** Not supported in CUA mode (`mode: \"cua\"`).\n *\n * **Available tools by mode:**\n *\n * **DOM mode (default):**\n * - `act` - Perform semantic actions (click, type, etc.)\n * - `fillForm` - Fill form fields using DOM selectors\n * - `ariaTree` - Get accessibility tree of the page\n * - `extract` - Extract structured data from page\n * - `goto` - Navigate to a URL\n * - `scroll` - Scroll using semantic directions (up/down/left/right)\n * - `keys` - Press keyboard keys\n * - `navback` - Navigate back in history\n * - `screenshot` - Take a screenshot\n * - `think` - Agent reasoning/planning step\n * - `wait` - Wait for time or condition\n * - `done` - Mark task as complete\n * - `search` - Web search (requires useSearch: true and BROWSERBASE_API_KEY)\n *\n * **Hybrid mode:**\n * - `click` - Click at specific coordinates\n * - `type` - Type text at coordinates\n * - `dragAndDrop` - Drag from one point to another\n * - `clickAndHold` - Click and hold at coordinates\n * - `fillFormVision` - Fill forms using vision/coordinates\n * - `act` - Perform semantic actions\n * - `ariaTree` - Get accessibility tree\n * - `extract` - Extract data from page\n * - `goto` - Navigate to URL\n * - `scroll` - Scroll using coordinates\n * - `keys` - Press keyboard keys\n * - `navback` - Navigate back\n * - `screenshot` - Take screenshot\n * - `think` - Agent reasoning step\n * - `wait` - Wait for time/condition\n * - `done` - Mark task complete\n * - `search` - Web search (requires useSearch: true and BROWSERBASE_API_KEY)\n *\n * @experimental\n * @example\n * ```typescript\n * // Exclude screenshot and extract tools\n * const result = await agent.execute({\n * instruction: \"Click the submit button\",\n * excludeTools: [\"screenshot\", \"extract\"]\n * });\n * ```\n */\n excludeTools?: string[];\n /**\n * A Zod schema defining custom output data to return when the task completes.\n * The agent will populate this data in the final done tool call.\n *\n * @experimental\n * @example\n * ```typescript\n * const result = await agent.execute({\n * instruction: \"Find the cheapest flight from NYC to LA\",\n * output: z.object({\n * price: z.string().describe(\"The price of the flight\"),\n * airline: z.string().describe(\"The airline name\"),\n * departureTime: z.string().describe(\"Departure time\"),\n * }),\n * });\n *\n * console.log(result.output); // { price: \"$199\", airline: \"Delta\", departureTime: \"8:00 AM\" }\n * ```\n */\n output?: StagehandZodObject;\n /**\n * Variables that the agent can use when filling forms or typing text.\n * The agent will see variable names and descriptions in the system prompt,\n * and can use them via `%variableName%` syntax in act/type/fillForm tool calls.\n *\n * Accepts both simple values and rich objects with descriptions (same type as `act`).\n *\n * **Note:** Not supported in CUA mode (`mode: \"cua\"`). Requires `experimental: true`.\n *\n * @experimental\n * @example\n * ```typescript\n * // Simple values\n * variables: { username: \"john@example.com\", password: \"secret123\" }\n *\n * // Rich values with descriptions (helps the agent understand context)\n * variables: {\n * username: { value: \"john@example.com\", description: \"The login email\" },\n * password: { value: \"secret123\", description: \"The login password\" },\n * }\n * ```\n */\n variables?: Variables;\n /**\n * Timeout in milliseconds for each agent tool call.\n * If a tool call exceeds this duration, it will be aborted and\n * reported back to the LLM as a timeout error so it can retry or adjust.\n * For tools that call v3 methods (act, extract, fillForm, ariaTree), the\n * timeout is also forwarded to the underlying v3 call for true cancellation.\n * @default 45000 (45 seconds)\n */\n toolTimeout?: number;\n /**\n * Enable the web search tool powered by Browserbase Search API.\n * Requires a valid Browserbase API key (BROWSERBASE_API_KEY).\n * When set to true, the agent gains access to a `search` tool for web searches.\n *\n * @example\n * ```typescript\n * const result = await agent.execute({\n * instruction: \"Find the latest news about AI\",\n * useSearch: true,\n * });\n * ```\n */\n useSearch?: boolean;\n}\n\n/**\n * Options for non-streaming agent execution.\n * Only accepts AgentExecuteCallbacks (no streaming-specific callbacks like onChunk, onFinish).\n */\nexport interface AgentExecuteOptions extends AgentExecuteOptionsBase {\n /**\n * Callbacks for non-streaming agent execution.\n * For streaming callbacks (onChunk, onFinish, onError, onAbort), use stream: true in AgentConfig.\n */\n callbacks?: AgentExecuteCallbacks;\n}\n\n/**\n * Options for streaming agent execution.\n * Accepts AgentStreamCallbacks including onChunk, onFinish, onError, and onAbort.\n */\nexport interface AgentStreamExecuteOptions extends AgentExecuteOptionsBase {\n /**\n * Callbacks for streaming agent execution.\n * Includes streaming-specific callbacks: onChunk, onFinish, onError, onAbort.\n */\n callbacks?: AgentStreamCallbacks;\n}\nexport type AgentType =\n | \"openai\"\n | \"anthropic\"\n | \"google\"\n | \"microsoft\"\n | \"bedrock\";\n\nexport const AVAILABLE_CUA_MODELS = [\n \"openai/computer-use-preview\",\n \"openai/computer-use-preview-2025-03-11\",\n \"anthropic/claude-opus-4-5-20251101\",\n \"anthropic/claude-opus-4-6\",\n \"anthropic/claude-sonnet-4-6\",\n \"anthropic/claude-haiku-4-5-20251001\",\n \"anthropic/claude-sonnet-4-20250514\",\n \"anthropic/claude-sonnet-4-5-20250929\",\n \"google/gemini-2.5-computer-use-preview-10-2025\",\n \"google/gemini-3-flash-preview\",\n \"google/gemini-3-pro-preview\",\n \"microsoft/fara-7b\",\n] as const;\nexport type AvailableCuaModel = (typeof AVAILABLE_CUA_MODELS)[number];\n\nexport interface AgentExecutionOptions<\n TOptions extends AgentExecuteOptions = AgentExecuteOptions,\n> {\n options: TOptions;\n logger: (message: LogLine) => void;\n retries?: number;\n}\n\nexport interface AgentHandlerOptions {\n modelName: string;\n clientOptions?: ClientOptions;\n userProvidedInstructions?: string;\n experimental?: boolean;\n}\n\nexport interface ActionExecutionResult {\n success: boolean;\n error?: string;\n data?: unknown;\n}\n\n/**\n * Represents a safety check that requires user confirmation before proceeding.\n * These are issued by CUA providers (OpenAI, Google) when the agent attempts\n * potentially risky actions.\n */\nexport interface SafetyCheck {\n /** Unique identifier for this safety check */\n id: string;\n /** Code identifying the type of safety concern */\n code: string;\n /** Human-readable description of the safety concern */\n message: string;\n}\n\n/**\n * Response from the user for a safety confirmation request.\n */\nexport interface SafetyConfirmationResponse {\n /** Whether the user acknowledged/approved the safety checks */\n acknowledged: boolean;\n}\n\n/**\n * Callback for handling safety confirmation requests.\n * Called when the CUA provider issues safety checks that require user confirmation.\n * The callback should return a promise that resolves when the user has made a decision.\n *\n * @param safetyChecks - Array of safety checks requiring confirmation\n * @returns Promise resolving to the user's response\n *\n * @example\n * ```typescript\n * const agent = stagehand.agent({\n * mode: \"cua\",\n * });\n * await agent.execute({\n * instruction: \"...\",\n * callbacks: {\n * onSafetyConfirmation: async (checks) => {\n * console.log(\"Safety checks:\", checks);\n * const userApproved = await showConfirmationDialog(checks);\n * return { acknowledged: userApproved };\n * },\n * },\n * });\n * ```\n */\nexport type SafetyConfirmationHandler = (\n safetyChecks: SafetyCheck[],\n) => Promise<SafetyConfirmationResponse>;\n\n// Anthropic types:\n\nexport interface ToolUseItem extends ResponseItem {\n type: \"tool_use\";\n id: string; // This is the correct property name from Anthropic's API\n name: string; // Name of the tool being used\n input: Record<string, unknown>;\n}\n\nexport interface AnthropicMessage {\n role: string;\n content: string | Array<AnthropicContentBlock>;\n}\n\nexport interface AnthropicContentBlock {\n type: string;\n [key: string]: unknown;\n}\n\nexport interface AnthropicTextBlock extends AnthropicContentBlock {\n type: \"text\";\n text: string;\n}\n\nexport interface AnthropicToolResult {\n type: \"tool_result\";\n tool_use_id: string;\n content: string | Array<AnthropicContentBlock>;\n}\n\n// OpenAI types:\n\nexport interface ResponseItem {\n type: string;\n id: string;\n [key: string]: unknown;\n}\n\nexport interface ComputerCallItem extends ResponseItem {\n type: \"computer_call\";\n call_id: string;\n action: {\n type: string;\n [key: string]: unknown;\n };\n pending_safety_checks?: Array<{\n id: string;\n code: string;\n message: string;\n }>;\n}\n\nexport interface FunctionCallItem extends ResponseItem {\n type: \"function_call\";\n call_id: string;\n name: string;\n arguments: string;\n}\n\nexport type ResponseInputItem =\n | { role: string; content: string }\n | {\n type: \"computer_call_output\";\n call_id: string;\n output:\n | {\n type: \"input_image\";\n image_url: string;\n current_url?: string;\n error?: string;\n [key: string]: unknown;\n }\n | string;\n acknowledged_safety_checks?: Array<{\n id: string;\n code: string;\n message: string;\n }>;\n }\n | {\n type: \"function_call_output\";\n call_id: string;\n output: string;\n };\n\nexport interface AgentInstance {\n execute: (\n instructionOrOptions: string | AgentExecuteOptions,\n ) => Promise<AgentResult>;\n}\n\nexport type AgentProviderType = AgentType;\n\nexport type AgentModelConfig<TModelName extends string = string> = {\n modelName: TModelName;\n} & Record<string, unknown>;\n\n/**\n * Agent tool mode determines which set of tools are available to the agent.\n * - 'dom': Uses DOM-based tools (act, fillForm) - better for structured page interactions\n * - 'hybrid': Uses coordinate-based tools (click, type, dragAndDrop, etc.) - better for visual/screenshot-based interactions\n * - 'cua': Uses Computer Use Agent (CUA) providers like Anthropic Claude or Google Gemini for screenshot-based automation\n */\nexport type AgentToolMode = \"dom\" | \"hybrid\" | \"cua\";\n\nexport type AgentConfig = {\n /**\n * Custom system prompt to provide to the agent. Overrides the default system prompt.\n */\n systemPrompt?: string;\n /**\n * MCP integrations - Array of Client objects\n */\n integrations?: (Client | string)[];\n /**\n * Tools passed to the agent client\n */\n tools?: ToolSet;\n /**\n * @deprecated Use `mode: \"cua\"` instead. This option will be removed in a future version.\n * Enables Computer Use Agent (CUA) mode.\n */\n cua?: boolean;\n /**\n * The model to use for agent functionality\n */\n model?: string | AgentModelConfig<string>;\n /**\n * The model to use for tool execution (observe/act calls within agent tools).\n * If not specified, inherits from the main model configuration.\n * Format: \"provider/model\" (e.g., \"openai/gpt-4o-mini\", \"google/gemini-2.0-flash-exp\")\n */\n executionModel?: string | AgentModelConfig<string>;\n /**\n * Enable streaming mode for the agent.\n * When true, execute() returns AgentStreamResult with textStream for incremental output.\n * When false (default), execute() returns AgentResult after completion.\n */\n stream?: boolean;\n /**\n * Tool mode for the agent. Determines which set of tools are available.\n * - 'dom' (default): Uses DOM-based tools (act, fillForm) for structured interactions\n * - 'hybrid': Uses coordinate-based tools (click, type, dragAndDrop, clickAndHold, fillFormVision)\n * for visual/screenshot-based interactions\n * - 'cua': Uses Computer Use Agent (CUA) providers for screenshot-based automation\n */\n mode?: AgentToolMode;\n};\n\n/**\n * Agent instance returned when stream: true is set in AgentConfig.\n * execute() returns a streaming result that can be consumed incrementally.\n * Accepts AgentStreamExecuteOptions with streaming-specific callbacks.\n */\nexport interface StreamingAgentInstance {\n execute: (\n instructionOrOptions: string | AgentStreamExecuteOptions,\n ) => Promise<AgentStreamResult>;\n}\n\n/**\n * Agent instance returned when stream is false or not set in AgentConfig.\n * execute() returns a result after the agent completes.\n * Accepts AgentExecuteOptions with non-streaming callbacks only.\n */\nexport interface NonStreamingAgentInstance {\n execute: (\n instructionOrOptions: string | AgentExecuteOptions,\n ) => Promise<AgentResult>;\n}\n\n// =============================================================================\n// Vision Action Tool Result Types\n// =============================================================================\n\n/**\n * Content item type for toModelOutput return values.\n * Used in tool definitions to return text and/or media to the model.\n */\nexport type ModelOutputContentItem =\n | { type: \"text\"; text: string }\n | { type: \"media\"; mediaType: string; data: string };\n\nexport interface ClickToolResult {\n success: boolean;\n describe?: string;\n coordinates?: number[];\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface TypeToolResult {\n success: boolean;\n describe?: string;\n text?: string;\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface DragAndDropToolResult {\n success: boolean;\n describe?: string;\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface FillFormField {\n action: string;\n value: string;\n coordinates: { x: number; y: number };\n}\n\nexport interface FillFormVisionToolResult {\n success: boolean;\n playwrightArguments?: FillFormField[];\n error?: string;\n screenshotBase64?: string;\n}\n\nexport interface ScrollToolResult {\n success: boolean;\n message: string;\n scrolledPixels: number;\n error?: string;\n}\n\nexport interface ScrollVisionToolResult extends ScrollToolResult {\n screenshotBase64?: string;\n}\n\nexport interface WaitToolResult {\n success: boolean;\n waited: number;\n screenshotBase64?: string;\n error?: string;\n}\n"]}
@@ -38,6 +38,11 @@ export type LocalBrowserLaunchOptions = z.infer<typeof LocalBrowserLaunchOptions
38
38
  /** Constructor options for V3 */
39
39
  export interface V3Options {
40
40
  env: V3Env;
41
+ /**
42
+ * Optional external session identifier to use for flow logging/event storage.
43
+ * When omitted, Stagehand falls back to its internal instance id.
44
+ */
45
+ sessionId?: string;
41
46
  apiKey?: string;
42
47
  projectId?: string;
43
48
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"options.js","sourceRoot":"","sources":["../../../../../../lib/v3/types/public/options.ts"],"names":[],"mappings":";;;AAIA,qCAGkB;AAIlB,0DAA0D;AAC7C,QAAA,+BAA+B,GAAG,wCAA+B,CAAC","sourcesContent":["import { z } from \"zod\";\nimport { LLMClient } from \"../../llm/LLMClient.js\";\nimport { ModelConfiguration } from \"./model.js\";\nimport { LogLine } from \"./logs.js\";\nimport {\n type BrowserbaseSessionCreateParams,\n LocalBrowserLaunchOptionsSchema,\n} from \"./api.js\";\n\nexport type V3Env = \"LOCAL\" | \"BROWSERBASE\";\n\n// Re-export for backwards compatibility (camelCase alias)\nexport const localBrowserLaunchOptionsSchema = LocalBrowserLaunchOptionsSchema;\n\nexport type LocalBrowserLaunchOptions = z.infer<\n typeof LocalBrowserLaunchOptionsSchema\n>;\n\n/** Constructor options for V3 */\nexport interface V3Options {\n env: V3Env;\n // Browserbase (required when env = \"BROWSERBASE\")\n apiKey?: string;\n projectId?: string;\n /**\n * Optional: fine-tune Browserbase session creation or resume an existing session.\n */\n browserbaseSessionCreateParams?: BrowserbaseSessionCreateParams;\n browserbaseSessionID?: string;\n /**\n * Controls browser keepalive behavior. When set, it overrides any value in\n * browserbaseSessionCreateParams.keepAlive.\n */\n keepAlive?: boolean;\n\n // Local Chromium (optional)\n localBrowserLaunchOptions?: LocalBrowserLaunchOptions;\n\n model?: ModelConfiguration;\n llmClient?: LLMClient; // allow user to pass their own\n systemPrompt?: string;\n logInferenceToFile?: boolean;\n experimental?: boolean;\n verbose?: 0 | 1 | 2;\n selfHeal?: boolean;\n // V2 compatibility fields - only included because the server imports this type and supports V2\n waitForCaptchaSolves?: boolean;\n actTimeoutMs?: number;\n /** Disable pino logging backend (useful for tests or minimal environments). */\n disablePino?: boolean;\n /** Optional external logger hook for integrating with host apps. */\n logger?: (line: LogLine) => void;\n /** Directory used to persist cached actions for act(). */\n cacheDir?: string;\n domSettleTimeout?: number;\n disableAPI?: boolean;\n /**\n * When true, enables server-side caching for API requests.\n * When false, disables server-side caching.\n * Defaults to true (caching enabled).\n * Can be overridden per-method in act(), extract(), and observe() options.\n */\n serverCache?: boolean;\n}\n"]}
1
+ {"version":3,"file":"options.js","sourceRoot":"","sources":["../../../../../../lib/v3/types/public/options.ts"],"names":[],"mappings":";;;AAIA,qCAGkB;AAIlB,0DAA0D;AAC7C,QAAA,+BAA+B,GAAG,wCAA+B,CAAC","sourcesContent":["import { z } from \"zod\";\nimport { LLMClient } from \"../../llm/LLMClient.js\";\nimport { ModelConfiguration } from \"./model.js\";\nimport { LogLine } from \"./logs.js\";\nimport {\n type BrowserbaseSessionCreateParams,\n LocalBrowserLaunchOptionsSchema,\n} from \"./api.js\";\n\nexport type V3Env = \"LOCAL\" | \"BROWSERBASE\";\n\n// Re-export for backwards compatibility (camelCase alias)\nexport const localBrowserLaunchOptionsSchema = LocalBrowserLaunchOptionsSchema;\n\nexport type LocalBrowserLaunchOptions = z.infer<\n typeof LocalBrowserLaunchOptionsSchema\n>;\n\n/** Constructor options for V3 */\nexport interface V3Options {\n env: V3Env;\n /**\n * Optional external session identifier to use for flow logging/event storage.\n * When omitted, Stagehand falls back to its internal instance id.\n */\n sessionId?: string;\n // Browserbase (required when env = \"BROWSERBASE\")\n apiKey?: string;\n projectId?: string;\n /**\n * Optional: fine-tune Browserbase session creation or resume an existing session.\n */\n browserbaseSessionCreateParams?: BrowserbaseSessionCreateParams;\n browserbaseSessionID?: string;\n /**\n * Controls browser keepalive behavior. When set, it overrides any value in\n * browserbaseSessionCreateParams.keepAlive.\n */\n keepAlive?: boolean;\n\n // Local Chromium (optional)\n localBrowserLaunchOptions?: LocalBrowserLaunchOptions;\n\n model?: ModelConfiguration;\n llmClient?: LLMClient; // allow user to pass their own\n systemPrompt?: string;\n logInferenceToFile?: boolean;\n experimental?: boolean;\n verbose?: 0 | 1 | 2;\n selfHeal?: boolean;\n // V2 compatibility fields - only included because the server imports this type and supports V2\n waitForCaptchaSolves?: boolean;\n actTimeoutMs?: number;\n /** Disable pino logging backend (useful for tests or minimal environments). */\n disablePino?: boolean;\n /** Optional external logger hook for integrating with host apps. */\n logger?: (line: LogLine) => void;\n /** Directory used to persist cached actions for act(). */\n cacheDir?: string;\n domSettleTimeout?: number;\n disableAPI?: boolean;\n /**\n * When true, enables server-side caching for API requests.\n * When false, disables server-side caching.\n * Defaults to true (caching enabled).\n * Can be overridden per-method in act(), extract(), and observe() options.\n */\n serverCache?: boolean;\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  import type { Protocol } from "devtools-protocol";
2
+ import { type FlowLoggerContext } from "../flowLogger.js";
2
3
  /**
3
4
  * CDP transport & session multiplexer
4
5
  *
@@ -19,6 +20,7 @@ export declare class CdpConnection implements CDPSessionLike {
19
20
  private ws;
20
21
  private nextId;
21
22
  private inflight;
23
+ private latestCdpCallEvent;
22
24
  private eventHandlers;
23
25
  private sessions;
24
26
  /** Maps sessionId -> targetId (1:1 mapping) */
@@ -26,18 +28,7 @@ export declare class CdpConnection implements CDPSessionLike {
26
28
  private sessionDispatchWaiters;
27
29
  readonly id: string | null;
28
30
  private transportCloseHandlers;
29
- /** Optional CDP logger - set this to receive all outgoing CDP method calls */
30
- cdpLogger?: (info: {
31
- method: string;
32
- params?: object;
33
- targetId?: string | null;
34
- }) => void;
35
- /** Optional CDP event logger - set this to receive all incoming CDP events */
36
- cdpEventLogger?: (info: {
37
- method: string;
38
- params?: unknown;
39
- targetId?: string | null;
40
- }) => void;
31
+ flowLoggerContext?: FlowLoggerContext;
41
32
  onTransportClosed(handler: (why: string) => void): void;
42
33
  offTransportClosed(handler: (why: string) => void): void;
43
34
  private emitTransportClosed;
@@ -7,11 +7,13 @@ exports.CdpSession = exports.CdpConnection = void 0;
7
7
  // lib/v3/understudy/cdp.ts
8
8
  const ws_1 = __importDefault(require("ws"));
9
9
  const version_js_1 = require("../../version.js");
10
+ const flowLogger_js_1 = require("../flowLogger.js");
10
11
  const sdkErrors_js_1 = require("../types/public/sdkErrors.js");
11
12
  class CdpConnection {
12
13
  ws;
13
14
  nextId = 1;
14
15
  inflight = new Map();
16
+ latestCdpCallEvent = new Map();
15
17
  eventHandlers = new Map();
16
18
  sessions = new Map();
17
19
  /** Maps sessionId -> targetId (1:1 mapping) */
@@ -19,10 +21,7 @@ class CdpConnection {
19
21
  sessionDispatchWaiters = new Set();
20
22
  id = null; // root
21
23
  transportCloseHandlers = new Set();
22
- /** Optional CDP logger - set this to receive all outgoing CDP method calls */
23
- cdpLogger;
24
- /** Optional CDP event logger - set this to receive all incoming CDP events */
25
- cdpEventLogger;
24
+ flowLoggerContext;
26
25
  onTransportClosed(handler) {
27
26
  this.transportCloseHandlers.add(handler);
28
27
  }
@@ -80,6 +79,26 @@ class CdpConnection {
80
79
  const id = this.nextId++;
81
80
  const payload = { id, method, params };
82
81
  const stack = new Error().stack?.split("\n").slice(1, 4).join("\n");
82
+ let flowLoggerContext = null;
83
+ try {
84
+ flowLoggerContext = flowLogger_js_1.FlowLogger.currentContext;
85
+ }
86
+ catch {
87
+ flowLoggerContext = this.flowLoggerContext ?? null;
88
+ }
89
+ const cdpCallEvent = flowLoggerContext
90
+ ? flowLogger_js_1.FlowLogger.logCdpCallEvent(flowLoggerContext, {
91
+ method,
92
+ params,
93
+ targetId: null,
94
+ })
95
+ : null;
96
+ if (flowLoggerContext && cdpCallEvent) {
97
+ this.latestCdpCallEvent.set(null, {
98
+ flowLoggerContext,
99
+ cdpCallEvent,
100
+ });
101
+ }
83
102
  const p = new Promise((resolve, reject) => {
84
103
  this.inflight.set(id, {
85
104
  resolve,
@@ -89,11 +108,12 @@ class CdpConnection {
89
108
  params,
90
109
  stack,
91
110
  ts: Date.now(),
111
+ flowLoggerContext,
112
+ cdpCallEvent,
92
113
  });
93
114
  });
94
115
  // Prevent unhandledRejection if a session detaches before the caller awaits.
95
116
  void p.catch(() => { });
96
- this.cdpLogger?.({ method, params, targetId: null });
97
117
  this.ws.send(JSON.stringify(payload));
98
118
  return p;
99
119
  }
@@ -120,6 +140,7 @@ class CdpConnection {
120
140
  entry.reject(new sdkErrors_js_1.CdpConnectionClosedError(why));
121
141
  this.inflight.delete(id);
122
142
  }
143
+ this.latestCdpCallEvent.clear();
123
144
  for (const waiter of Array.from(this.sessionDispatchWaiters)) {
124
145
  waiter.reject(new sdkErrors_js_1.CdpConnectionClosedError(why));
125
146
  }
@@ -167,9 +188,29 @@ class CdpConnection {
167
188
  return;
168
189
  this.inflight.delete(msg.id);
169
190
  if ("error" in msg && msg.error) {
191
+ if (rec.flowLoggerContext && rec.cdpCallEvent) {
192
+ const targetId = rec.sessionId != null
193
+ ? (this.sessionToTarget.get(rec.sessionId) ?? rec.sessionId)
194
+ : null;
195
+ flowLogger_js_1.FlowLogger.logCdpResponseEvent(rec.flowLoggerContext, rec.cdpCallEvent, {
196
+ method: rec.method,
197
+ error: `${msg.error.code} ${msg.error.message}`,
198
+ targetId,
199
+ });
200
+ }
170
201
  rec.reject(new Error(`${msg.error.code} ${msg.error.message}`));
171
202
  }
172
203
  else {
204
+ if (rec.flowLoggerContext && rec.cdpCallEvent) {
205
+ const targetId = rec.sessionId != null
206
+ ? (this.sessionToTarget.get(rec.sessionId) ?? rec.sessionId)
207
+ : null;
208
+ flowLogger_js_1.FlowLogger.logCdpResponseEvent(rec.flowLoggerContext, rec.cdpCallEvent, {
209
+ method: rec.method,
210
+ result: msg.result,
211
+ targetId,
212
+ });
213
+ }
173
214
  rec.resolve(msg.result);
174
215
  }
175
216
  return;
@@ -199,6 +240,7 @@ class CdpConnection {
199
240
  }
200
241
  this.sessions.delete(p.sessionId);
201
242
  this.sessionToTarget.delete(p.sessionId);
243
+ this.latestCdpCallEvent.delete(p.sessionId);
202
244
  }
203
245
  else if (msg.method === "Target.targetDestroyed") {
204
246
  const p = msg.params;
@@ -206,14 +248,24 @@ class CdpConnection {
206
248
  for (const [sessionId, targetId] of this.sessionToTarget.entries()) {
207
249
  if (targetId === p.targetId) {
208
250
  this.sessionToTarget.delete(sessionId);
251
+ this.latestCdpCallEvent.delete(sessionId);
209
252
  break;
210
253
  }
211
254
  }
212
255
  }
213
256
  const { method, params, sessionId } = msg;
214
- // Log incoming CDP events
215
- const targetId = this.sessionToTarget.get(sessionId) || sessionId;
216
- this.cdpEventLogger?.({ method, params, targetId });
257
+ const latestCdpCallEvent = this.latestCdpCallEvent.get(sessionId ?? null) ??
258
+ (sessionId ? this.latestCdpCallEvent.get(null) : null);
259
+ const targetId = sessionId != null
260
+ ? (this.sessionToTarget.get(sessionId) ?? sessionId)
261
+ : null;
262
+ if (latestCdpCallEvent) {
263
+ flowLogger_js_1.FlowLogger.logCdpMessageEvent(latestCdpCallEvent.flowLoggerContext, latestCdpCallEvent.cdpCallEvent, {
264
+ method,
265
+ params,
266
+ targetId,
267
+ });
268
+ }
217
269
  if (sessionId) {
218
270
  const session = this.sessions.get(sessionId);
219
271
  session?.dispatch(method, params);
@@ -239,6 +291,27 @@ class CdpConnection {
239
291
  const id = this.nextId++;
240
292
  const payload = { id, method, params, sessionId };
241
293
  const stack = new Error().stack?.split("\n").slice(1, 4).join("\n");
294
+ let flowLoggerContext = null;
295
+ try {
296
+ flowLoggerContext = flowLogger_js_1.FlowLogger.currentContext;
297
+ }
298
+ catch {
299
+ flowLoggerContext = this.flowLoggerContext ?? null;
300
+ }
301
+ const targetId = this.sessionToTarget.get(sessionId) ?? null;
302
+ const cdpCallEvent = flowLoggerContext
303
+ ? flowLogger_js_1.FlowLogger.logCdpCallEvent(flowLoggerContext, {
304
+ method,
305
+ params,
306
+ targetId,
307
+ })
308
+ : null;
309
+ if (flowLoggerContext && cdpCallEvent) {
310
+ this.latestCdpCallEvent.set(sessionId, {
311
+ flowLoggerContext,
312
+ cdpCallEvent,
313
+ });
314
+ }
242
315
  const p = new Promise((resolve, reject) => {
243
316
  this.inflight.set(id, {
244
317
  resolve,
@@ -248,12 +321,12 @@ class CdpConnection {
248
321
  params,
249
322
  stack,
250
323
  ts: Date.now(),
324
+ flowLoggerContext,
325
+ cdpCallEvent,
251
326
  });
252
327
  });
253
328
  // Prevent unhandledRejection if a session detaches before the caller awaits.
254
329
  void p.catch(() => { });
255
- const targetId = this.sessionToTarget.get(sessionId) ?? null;
256
- this.cdpLogger?.({ method, params, targetId });
257
330
  for (const waiter of Array.from(this.sessionDispatchWaiters)) {
258
331
  if (waiter.sessionId !== sessionId)
259
332
  continue;