@browserbasehq/orca 3.1.0-patch.4 → 3.2.0-middleware.1

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 (431) hide show
  1. package/dist/cjs/lib/inference.js +1 -4
  2. package/dist/cjs/lib/inference.js.map +1 -1
  3. package/dist/cjs/lib/utils.d.ts +1 -0
  4. package/dist/cjs/lib/utils.js +4 -0
  5. package/dist/cjs/lib/utils.js.map +1 -1
  6. package/dist/cjs/lib/v3/agent/AgentClient.d.ts +8 -0
  7. package/dist/cjs/lib/v3/agent/AgentClient.js +13 -0
  8. package/dist/cjs/lib/v3/agent/AgentClient.js.map +1 -1
  9. package/dist/cjs/lib/v3/agent/AgentProvider.js +0 -1
  10. package/dist/cjs/lib/v3/agent/AgentProvider.js.map +1 -1
  11. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +6 -7
  12. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  13. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +6 -7
  14. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  15. package/dist/cjs/lib/v3/agent/MicrosoftCUAClient.js +1 -0
  16. package/dist/cjs/lib/v3/agent/MicrosoftCUAClient.js.map +1 -1
  17. package/dist/cjs/lib/v3/agent/OpenAICUAClient.d.ts +4 -4
  18. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +67 -8
  19. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  20. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.d.ts +4 -2
  21. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js +7 -7
  22. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  23. package/dist/cjs/lib/v3/agent/tools/act.d.ts +1 -1
  24. package/dist/cjs/lib/v3/agent/tools/act.js +11 -4
  25. package/dist/cjs/lib/v3/agent/tools/act.js.map +1 -1
  26. package/dist/cjs/lib/v3/agent/tools/ariaTree.d.ts +8 -1
  27. package/dist/cjs/lib/v3/agent/tools/ariaTree.js +49 -22
  28. package/dist/cjs/lib/v3/agent/tools/ariaTree.js.map +1 -1
  29. package/dist/cjs/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  30. package/dist/cjs/lib/v3/agent/tools/braveSearch.js.map +1 -0
  31. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  32. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js +70 -0
  33. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  34. package/dist/cjs/lib/v3/agent/tools/click.js +23 -31
  35. package/dist/cjs/lib/v3/agent/tools/click.js.map +1 -1
  36. package/dist/cjs/lib/v3/agent/tools/clickAndHold.js.map +1 -1
  37. package/dist/cjs/lib/v3/agent/tools/dragAndDrop.js +22 -30
  38. package/dist/cjs/lib/v3/agent/tools/dragAndDrop.js.map +1 -1
  39. package/dist/cjs/lib/v3/agent/tools/extract.d.ts +2 -2
  40. package/dist/cjs/lib/v3/agent/tools/extract.js +7 -3
  41. package/dist/cjs/lib/v3/agent/tools/extract.js.map +1 -1
  42. package/dist/cjs/lib/v3/agent/tools/fillFormVision.js +30 -30
  43. package/dist/cjs/lib/v3/agent/tools/fillFormVision.js.map +1 -1
  44. package/dist/cjs/lib/v3/agent/tools/fillform.d.ts +7 -2
  45. package/dist/cjs/lib/v3/agent/tools/fillform.js +56 -45
  46. package/dist/cjs/lib/v3/agent/tools/fillform.js.map +1 -1
  47. package/dist/cjs/lib/v3/agent/tools/index.d.ts +19 -3
  48. package/dist/cjs/lib/v3/agent/tools/index.js +63 -11
  49. package/dist/cjs/lib/v3/agent/tools/index.js.map +1 -1
  50. package/dist/cjs/lib/v3/agent/tools/keys.d.ts +1 -1
  51. package/dist/cjs/lib/v3/agent/tools/keys.js.map +1 -1
  52. package/dist/cjs/lib/v3/agent/tools/screenshot.d.ts +8 -0
  53. package/dist/cjs/lib/v3/agent/tools/screenshot.js +32 -15
  54. package/dist/cjs/lib/v3/agent/tools/screenshot.js.map +1 -1
  55. package/dist/cjs/lib/v3/agent/tools/scroll.js +12 -0
  56. package/dist/cjs/lib/v3/agent/tools/scroll.js.map +1 -1
  57. package/dist/cjs/lib/v3/agent/tools/type.js +23 -31
  58. package/dist/cjs/lib/v3/agent/tools/type.js.map +1 -1
  59. package/dist/cjs/lib/v3/agent/tools/wait.js +6 -0
  60. package/dist/cjs/lib/v3/agent/tools/wait.js.map +1 -1
  61. package/dist/cjs/lib/v3/agent/utils/captchaSolver.d.ts +76 -0
  62. package/dist/cjs/lib/v3/agent/utils/captchaSolver.js +175 -0
  63. package/dist/cjs/lib/v3/agent/utils/captchaSolver.js.map +1 -0
  64. package/dist/cjs/lib/v3/agent/utils/handleDoneToolCall.js +4 -0
  65. package/dist/cjs/lib/v3/agent/utils/handleDoneToolCall.js.map +1 -1
  66. package/dist/cjs/lib/v3/api.d.ts +2 -2
  67. package/dist/cjs/lib/v3/api.js +1 -1
  68. package/dist/cjs/lib/v3/api.js.map +1 -1
  69. package/dist/cjs/lib/v3/cache/ActCache.d.ts +0 -1
  70. package/dist/cjs/lib/v3/cache/ActCache.js +2 -18
  71. package/dist/cjs/lib/v3/cache/ActCache.js.map +1 -1
  72. package/dist/cjs/lib/v3/flowlogger/EventEmitter.d.ts +7 -0
  73. package/dist/cjs/lib/v3/flowlogger/EventEmitter.js +30 -0
  74. package/dist/cjs/lib/v3/flowlogger/EventEmitter.js.map +1 -0
  75. package/dist/cjs/lib/v3/flowlogger/EventSink.d.ts +44 -0
  76. package/dist/cjs/lib/v3/flowlogger/EventSink.js +217 -0
  77. package/dist/cjs/lib/v3/flowlogger/EventSink.js.map +1 -0
  78. package/dist/cjs/lib/v3/flowlogger/EventStore.d.ts +26 -0
  79. package/dist/cjs/lib/v3/flowlogger/EventStore.js +135 -0
  80. package/dist/cjs/lib/v3/flowlogger/EventStore.js.map +1 -0
  81. package/dist/cjs/lib/v3/flowlogger/FlowLogger.d.ts +99 -0
  82. package/dist/cjs/lib/v3/flowlogger/FlowLogger.js +591 -0
  83. package/dist/cjs/lib/v3/flowlogger/FlowLogger.js.map +1 -0
  84. package/dist/cjs/lib/v3/flowlogger/prettify.d.ts +6 -0
  85. package/dist/cjs/lib/v3/flowlogger/prettify.js +395 -0
  86. package/dist/cjs/lib/v3/flowlogger/prettify.js.map +1 -0
  87. package/dist/cjs/lib/v3/handlers/actHandler.js +1 -2
  88. package/dist/cjs/lib/v3/handlers/actHandler.js.map +1 -1
  89. package/dist/cjs/lib/v3/handlers/extractHandler.js +2 -2
  90. package/dist/cjs/lib/v3/handlers/extractHandler.js.map +1 -1
  91. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js +43 -57
  92. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  93. package/dist/cjs/lib/v3/handlers/observeHandler.js +1 -2
  94. package/dist/cjs/lib/v3/handlers/observeHandler.js.map +1 -1
  95. package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +2 -5
  96. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +130 -91
  97. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  98. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.d.ts +5 -0
  99. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +134 -14
  100. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  101. package/dist/cjs/lib/v3/index.d.ts +2 -1
  102. package/dist/cjs/lib/v3/launch/browserbase.d.ts +1 -1
  103. package/dist/cjs/lib/v3/launch/browserbase.js +4 -9
  104. package/dist/cjs/lib/v3/launch/browserbase.js.map +1 -1
  105. package/dist/cjs/lib/v3/llm/LLMProvider.d.ts +5 -2
  106. package/dist/cjs/lib/v3/llm/LLMProvider.js +13 -11
  107. package/dist/cjs/lib/v3/llm/LLMProvider.js.map +1 -1
  108. package/dist/cjs/lib/v3/llm/aisdk.js +11 -17
  109. package/dist/cjs/lib/v3/llm/aisdk.js.map +1 -1
  110. package/dist/cjs/lib/v3/runtimePaths.js +2 -1
  111. package/dist/cjs/lib/v3/runtimePaths.js.map +1 -1
  112. package/dist/cjs/lib/v3/shutdown/supervisor.js +2 -2
  113. package/dist/cjs/lib/v3/shutdown/supervisor.js.map +1 -1
  114. package/dist/cjs/lib/v3/timeoutConfig.d.ts +1 -1
  115. package/dist/cjs/lib/v3/timeoutConfig.js +5 -0
  116. package/dist/cjs/lib/v3/timeoutConfig.js.map +1 -1
  117. package/dist/cjs/lib/v3/types/private/cache.d.ts +0 -1
  118. package/dist/cjs/lib/v3/types/private/cache.js.map +1 -1
  119. package/dist/cjs/lib/v3/types/private/shutdown.d.ts +1 -1
  120. package/dist/cjs/lib/v3/types/private/shutdown.js.map +1 -1
  121. package/dist/cjs/lib/v3/types/public/agent.d.ts +28 -3
  122. package/dist/cjs/lib/v3/types/public/agent.js +0 -1
  123. package/dist/cjs/lib/v3/types/public/agent.js.map +1 -1
  124. package/dist/cjs/lib/v3/types/public/api.d.ts +7 -0
  125. package/dist/cjs/lib/v3/types/public/api.js +9 -0
  126. package/dist/cjs/lib/v3/types/public/api.js.map +1 -1
  127. package/dist/cjs/lib/v3/types/public/model.d.ts +12 -3
  128. package/dist/cjs/lib/v3/types/public/model.js.map +1 -1
  129. package/dist/cjs/lib/v3/types/public/options.d.ts +8 -0
  130. package/dist/cjs/lib/v3/types/public/options.js.map +1 -1
  131. package/dist/cjs/lib/v3/understudy/cdp.d.ts +8 -13
  132. package/dist/cjs/lib/v3/understudy/cdp.js +180 -20
  133. package/dist/cjs/lib/v3/understudy/cdp.js.map +1 -1
  134. package/dist/cjs/lib/v3/understudy/context.d.ts +1 -0
  135. package/dist/cjs/lib/v3/understudy/context.js +142 -60
  136. package/dist/cjs/lib/v3/understudy/context.js.map +1 -1
  137. package/dist/cjs/lib/v3/understudy/frame.js +23 -6
  138. package/dist/cjs/lib/v3/understudy/frame.js.map +1 -1
  139. package/dist/cjs/lib/v3/understudy/page.d.ts +13 -0
  140. package/dist/cjs/lib/v3/understudy/page.js +84 -21
  141. package/dist/cjs/lib/v3/understudy/page.js.map +1 -1
  142. package/dist/cjs/lib/v3/understudy/screenshotUtils.d.ts +0 -1
  143. package/dist/cjs/lib/v3/understudy/screenshotUtils.js +0 -18
  144. package/dist/cjs/lib/v3/understudy/screenshotUtils.js.map +1 -1
  145. package/dist/cjs/lib/v3/v3.d.ts +26 -3
  146. package/dist/cjs/lib/v3/v3.js +242 -180
  147. package/dist/cjs/lib/v3/v3.js.map +1 -1
  148. package/dist/cjs/lib/version.d.ts +1 -1
  149. package/dist/cjs/lib/version.js +1 -1
  150. package/dist/cjs/lib/version.js.map +1 -1
  151. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.js +56 -0
  152. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.js.map +1 -0
  153. package/dist/cjs/tests/integration/agent-hybrid-mode.spec.js +6 -6
  154. package/dist/cjs/tests/integration/agent-hybrid-mode.spec.js.map +1 -1
  155. package/dist/cjs/tests/integration/cdp-session-detached.spec.js +1 -1
  156. package/dist/cjs/tests/integration/cdp-session-detached.spec.js.map +1 -1
  157. package/dist/cjs/tests/integration/context-addInitScript.spec.js +104 -11
  158. package/dist/cjs/tests/integration/context-addInitScript.spec.js.map +1 -1
  159. package/dist/cjs/tests/integration/flowLogger.spec.js +714 -0
  160. package/dist/cjs/tests/integration/flowLogger.spec.js.map +1 -0
  161. package/dist/cjs/tests/integration/iframe-ctx-addInitScript-race.spec.d.ts +1 -0
  162. package/dist/cjs/tests/integration/iframe-ctx-addInitScript-race.spec.js +219 -0
  163. package/dist/cjs/tests/integration/iframe-ctx-addInitScript-race.spec.js.map +1 -0
  164. package/dist/cjs/tests/integration/page-extra-http-headers.spec.d.ts +1 -0
  165. package/dist/cjs/tests/integration/page-extra-http-headers.spec.js +85 -0
  166. package/dist/cjs/tests/integration/page-extra-http-headers.spec.js.map +1 -0
  167. package/dist/cjs/tests/integration/page-screenshot.spec.js +1 -1
  168. package/dist/cjs/tests/integration/page-screenshot.spec.js.map +1 -1
  169. package/dist/cjs/tests/integration/testUtils.d.ts +33 -0
  170. package/dist/cjs/tests/integration/testUtils.js +144 -0
  171. package/dist/cjs/tests/integration/testUtils.js.map +1 -1
  172. package/dist/cjs/tests/integration/timeouts.spec.js +278 -0
  173. package/dist/cjs/tests/integration/timeouts.spec.js.map +1 -1
  174. package/dist/cjs/tests/unit/agent-captcha-hooks.test.d.ts +1 -0
  175. package/dist/cjs/tests/unit/agent-captcha-hooks.test.js +285 -0
  176. package/dist/cjs/tests/unit/agent-captcha-hooks.test.js.map +1 -0
  177. package/dist/cjs/tests/unit/agent-execution-model.test.js +1 -1
  178. package/dist/cjs/tests/unit/agent-execution-model.test.js.map +1 -1
  179. package/dist/cjs/tests/unit/browserbase-session-accessors.test.js +20 -0
  180. package/dist/cjs/tests/unit/browserbase-session-accessors.test.js.map +1 -1
  181. package/dist/cjs/tests/unit/captcha-solver.test.d.ts +1 -0
  182. package/dist/cjs/tests/unit/captcha-solver.test.js +154 -0
  183. package/dist/cjs/tests/unit/captcha-solver.test.js.map +1 -0
  184. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.d.ts +1 -0
  185. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.js +95 -0
  186. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.js.map +1 -0
  187. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.d.ts +1 -0
  188. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.js +43 -0
  189. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.js.map +1 -0
  190. package/dist/cjs/tests/unit/flowlogger-eventstore.test.d.ts +1 -0
  191. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js +250 -0
  192. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js.map +1 -0
  193. package/dist/cjs/tests/unit/llm-middleware.test.d.ts +1 -0
  194. package/dist/cjs/tests/unit/llm-middleware.test.js +495 -0
  195. package/dist/cjs/tests/unit/llm-middleware.test.js.map +1 -0
  196. package/dist/cjs/tests/unit/model-deprecation.test.js +5 -8
  197. package/dist/cjs/tests/unit/model-deprecation.test.js.map +1 -1
  198. package/dist/cjs/tests/unit/openai-cua-client.test.d.ts +1 -0
  199. package/dist/cjs/tests/unit/openai-cua-client.test.js +71 -0
  200. package/dist/cjs/tests/unit/openai-cua-client.test.js.map +1 -0
  201. package/dist/cjs/tests/unit/page-extra-http-headers.test.d.ts +1 -0
  202. package/dist/cjs/tests/unit/page-extra-http-headers.test.js +92 -0
  203. package/dist/cjs/tests/unit/page-extra-http-headers.test.js.map +1 -0
  204. package/dist/cjs/tests/unit/public-api/llm-and-agents.test.js +13 -1
  205. package/dist/cjs/tests/unit/public-api/llm-and-agents.test.js.map +1 -1
  206. package/dist/cjs/tests/unit/public-api/public-types.test.js.map +1 -1
  207. package/dist/esm/lib/inference.js +1 -4
  208. package/dist/esm/lib/inference.js.map +1 -1
  209. package/dist/esm/lib/utils.d.ts +1 -0
  210. package/dist/esm/lib/utils.js +3 -0
  211. package/dist/esm/lib/utils.js.map +1 -1
  212. package/dist/esm/lib/v3/agent/AgentClient.d.ts +8 -0
  213. package/dist/esm/lib/v3/agent/AgentClient.js +13 -0
  214. package/dist/esm/lib/v3/agent/AgentClient.js.map +1 -1
  215. package/dist/esm/lib/v3/agent/AgentProvider.js +0 -1
  216. package/dist/esm/lib/v3/agent/AgentProvider.js.map +1 -1
  217. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +6 -7
  218. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  219. package/dist/esm/lib/v3/agent/GoogleCUAClient.js +6 -7
  220. package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  221. package/dist/esm/lib/v3/agent/MicrosoftCUAClient.js +1 -0
  222. package/dist/esm/lib/v3/agent/MicrosoftCUAClient.js.map +1 -1
  223. package/dist/esm/lib/v3/agent/OpenAICUAClient.d.ts +4 -4
  224. package/dist/esm/lib/v3/agent/OpenAICUAClient.js +67 -8
  225. package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  226. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.d.ts +4 -2
  227. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js +7 -7
  228. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  229. package/dist/esm/lib/v3/agent/tools/act.d.ts +1 -1
  230. package/dist/esm/lib/v3/agent/tools/act.js +11 -4
  231. package/dist/esm/lib/v3/agent/tools/act.js.map +1 -1
  232. package/dist/esm/lib/v3/agent/tools/ariaTree.d.ts +8 -1
  233. package/dist/esm/lib/v3/agent/tools/ariaTree.js +49 -22
  234. package/dist/esm/lib/v3/agent/tools/ariaTree.js.map +1 -1
  235. package/dist/esm/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  236. package/dist/esm/lib/v3/agent/tools/braveSearch.js.map +1 -0
  237. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  238. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js +66 -0
  239. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  240. package/dist/esm/lib/v3/agent/tools/click.js +23 -31
  241. package/dist/esm/lib/v3/agent/tools/click.js.map +1 -1
  242. package/dist/esm/lib/v3/agent/tools/clickAndHold.js.map +1 -1
  243. package/dist/esm/lib/v3/agent/tools/dragAndDrop.js +22 -30
  244. package/dist/esm/lib/v3/agent/tools/dragAndDrop.js.map +1 -1
  245. package/dist/esm/lib/v3/agent/tools/extract.d.ts +2 -2
  246. package/dist/esm/lib/v3/agent/tools/extract.js +7 -3
  247. package/dist/esm/lib/v3/agent/tools/extract.js.map +1 -1
  248. package/dist/esm/lib/v3/agent/tools/fillFormVision.js +30 -30
  249. package/dist/esm/lib/v3/agent/tools/fillFormVision.js.map +1 -1
  250. package/dist/esm/lib/v3/agent/tools/fillform.d.ts +7 -2
  251. package/dist/esm/lib/v3/agent/tools/fillform.js +56 -45
  252. package/dist/esm/lib/v3/agent/tools/fillform.js.map +1 -1
  253. package/dist/esm/lib/v3/agent/tools/index.d.ts +19 -3
  254. package/dist/esm/lib/v3/agent/tools/index.js +63 -11
  255. package/dist/esm/lib/v3/agent/tools/index.js.map +1 -1
  256. package/dist/esm/lib/v3/agent/tools/keys.d.ts +1 -1
  257. package/dist/esm/lib/v3/agent/tools/keys.js.map +1 -1
  258. package/dist/esm/lib/v3/agent/tools/screenshot.d.ts +8 -0
  259. package/dist/esm/lib/v3/agent/tools/screenshot.js +32 -15
  260. package/dist/esm/lib/v3/agent/tools/screenshot.js.map +1 -1
  261. package/dist/esm/lib/v3/agent/tools/scroll.js +12 -0
  262. package/dist/esm/lib/v3/agent/tools/scroll.js.map +1 -1
  263. package/dist/esm/lib/v3/agent/tools/type.js +23 -31
  264. package/dist/esm/lib/v3/agent/tools/type.js.map +1 -1
  265. package/dist/esm/lib/v3/agent/tools/wait.js +6 -0
  266. package/dist/esm/lib/v3/agent/tools/wait.js.map +1 -1
  267. package/dist/esm/lib/v3/agent/utils/captchaSolver.d.ts +76 -0
  268. package/dist/esm/lib/v3/agent/utils/captchaSolver.js +171 -0
  269. package/dist/esm/lib/v3/agent/utils/captchaSolver.js.map +1 -0
  270. package/dist/esm/lib/v3/agent/utils/handleDoneToolCall.js +4 -0
  271. package/dist/esm/lib/v3/agent/utils/handleDoneToolCall.js.map +1 -1
  272. package/dist/esm/lib/v3/api.d.ts +2 -2
  273. package/dist/esm/lib/v3/api.js +1 -1
  274. package/dist/esm/lib/v3/api.js.map +1 -1
  275. package/dist/esm/lib/v3/cache/ActCache.d.ts +0 -1
  276. package/dist/esm/lib/v3/cache/ActCache.js +2 -18
  277. package/dist/esm/lib/v3/cache/ActCache.js.map +1 -1
  278. package/dist/esm/lib/v3/flowlogger/EventEmitter.d.ts +7 -0
  279. package/dist/esm/lib/v3/flowlogger/EventEmitter.js +26 -0
  280. package/dist/esm/lib/v3/flowlogger/EventEmitter.js.map +1 -0
  281. package/dist/esm/lib/v3/flowlogger/EventSink.d.ts +44 -0
  282. package/dist/esm/lib/v3/flowlogger/EventSink.js +206 -0
  283. package/dist/esm/lib/v3/flowlogger/EventSink.js.map +1 -0
  284. package/dist/esm/lib/v3/flowlogger/EventStore.d.ts +26 -0
  285. package/dist/esm/lib/v3/flowlogger/EventStore.js +127 -0
  286. package/dist/esm/lib/v3/flowlogger/EventStore.js.map +1 -0
  287. package/dist/esm/lib/v3/flowlogger/FlowLogger.d.ts +99 -0
  288. package/dist/esm/lib/v3/flowlogger/FlowLogger.js +583 -0
  289. package/dist/esm/lib/v3/flowlogger/FlowLogger.js.map +1 -0
  290. package/dist/esm/lib/v3/flowlogger/prettify.d.ts +6 -0
  291. package/dist/esm/lib/v3/flowlogger/prettify.js +389 -0
  292. package/dist/esm/lib/v3/flowlogger/prettify.js.map +1 -0
  293. package/dist/esm/lib/v3/handlers/actHandler.js +1 -2
  294. package/dist/esm/lib/v3/handlers/actHandler.js.map +1 -1
  295. package/dist/esm/lib/v3/handlers/extractHandler.js +2 -2
  296. package/dist/esm/lib/v3/handlers/extractHandler.js.map +1 -1
  297. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js +43 -57
  298. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  299. package/dist/esm/lib/v3/handlers/observeHandler.js +1 -2
  300. package/dist/esm/lib/v3/handlers/observeHandler.js.map +1 -1
  301. package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +2 -5
  302. package/dist/esm/lib/v3/handlers/v3AgentHandler.js +131 -92
  303. package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  304. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.d.ts +5 -0
  305. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +134 -14
  306. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  307. package/dist/esm/lib/v3/index.d.ts +2 -1
  308. package/dist/esm/lib/v3/launch/browserbase.d.ts +1 -1
  309. package/dist/esm/lib/v3/launch/browserbase.js +4 -9
  310. package/dist/esm/lib/v3/launch/browserbase.js.map +1 -1
  311. package/dist/esm/lib/v3/llm/LLMProvider.d.ts +5 -2
  312. package/dist/esm/lib/v3/llm/LLMProvider.js +14 -12
  313. package/dist/esm/lib/v3/llm/LLMProvider.js.map +1 -1
  314. package/dist/esm/lib/v3/llm/aisdk.js +11 -17
  315. package/dist/esm/lib/v3/llm/aisdk.js.map +1 -1
  316. package/dist/esm/lib/v3/runtimePaths.js +2 -1
  317. package/dist/esm/lib/v3/runtimePaths.js.map +1 -1
  318. package/dist/esm/lib/v3/shutdown/supervisor.js +2 -2
  319. package/dist/esm/lib/v3/shutdown/supervisor.js.map +1 -1
  320. package/dist/esm/lib/v3/timeoutConfig.d.ts +1 -1
  321. package/dist/esm/lib/v3/timeoutConfig.js +5 -0
  322. package/dist/esm/lib/v3/timeoutConfig.js.map +1 -1
  323. package/dist/esm/lib/v3/types/private/cache.d.ts +0 -1
  324. package/dist/esm/lib/v3/types/private/cache.js.map +1 -1
  325. package/dist/esm/lib/v3/types/private/shutdown.d.ts +1 -1
  326. package/dist/esm/lib/v3/types/private/shutdown.js.map +1 -1
  327. package/dist/esm/lib/v3/types/public/agent.d.ts +28 -3
  328. package/dist/esm/lib/v3/types/public/agent.js +0 -1
  329. package/dist/esm/lib/v3/types/public/agent.js.map +1 -1
  330. package/dist/esm/lib/v3/types/public/api.d.ts +7 -0
  331. package/dist/esm/lib/v3/types/public/api.js +9 -0
  332. package/dist/esm/lib/v3/types/public/api.js.map +1 -1
  333. package/dist/esm/lib/v3/types/public/model.d.ts +12 -3
  334. package/dist/esm/lib/v3/types/public/model.js.map +1 -1
  335. package/dist/esm/lib/v3/types/public/options.d.ts +8 -0
  336. package/dist/esm/lib/v3/types/public/options.js.map +1 -1
  337. package/dist/esm/lib/v3/understudy/cdp.d.ts +8 -13
  338. package/dist/esm/lib/v3/understudy/cdp.js +181 -21
  339. package/dist/esm/lib/v3/understudy/cdp.js.map +1 -1
  340. package/dist/esm/lib/v3/understudy/context.d.ts +1 -0
  341. package/dist/esm/lib/v3/understudy/context.js +142 -60
  342. package/dist/esm/lib/v3/understudy/context.js.map +1 -1
  343. package/dist/esm/lib/v3/understudy/frame.js +23 -6
  344. package/dist/esm/lib/v3/understudy/frame.js.map +1 -1
  345. package/dist/esm/lib/v3/understudy/page.d.ts +13 -0
  346. package/dist/esm/lib/v3/understudy/page.js +86 -23
  347. package/dist/esm/lib/v3/understudy/page.js.map +1 -1
  348. package/dist/esm/lib/v3/understudy/screenshotUtils.d.ts +0 -1
  349. package/dist/esm/lib/v3/understudy/screenshotUtils.js +0 -17
  350. package/dist/esm/lib/v3/understudy/screenshotUtils.js.map +1 -1
  351. package/dist/esm/lib/v3/v3.d.ts +26 -3
  352. package/dist/esm/lib/v3/v3.js +242 -181
  353. package/dist/esm/lib/v3/v3.js.map +1 -1
  354. package/dist/esm/lib/version.d.ts +1 -1
  355. package/dist/esm/lib/version.js +1 -1
  356. package/dist/esm/lib/version.js.map +1 -1
  357. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.d.ts +1 -0
  358. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.js +54 -0
  359. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.js.map +1 -0
  360. package/dist/esm/tests/integration/agent-hybrid-mode.spec.js +6 -6
  361. package/dist/esm/tests/integration/agent-hybrid-mode.spec.js.map +1 -1
  362. package/dist/esm/tests/integration/cdp-session-detached.spec.js +1 -1
  363. package/dist/esm/tests/integration/cdp-session-detached.spec.js.map +1 -1
  364. package/dist/esm/tests/integration/context-addInitScript.spec.js +104 -11
  365. package/dist/esm/tests/integration/context-addInitScript.spec.js.map +1 -1
  366. package/dist/esm/tests/integration/flowLogger.spec.d.ts +1 -0
  367. package/dist/esm/tests/integration/flowLogger.spec.js +712 -0
  368. package/dist/esm/tests/integration/flowLogger.spec.js.map +1 -0
  369. package/dist/esm/tests/integration/iframe-ctx-addInitScript-race.spec.d.ts +1 -0
  370. package/dist/esm/tests/integration/iframe-ctx-addInitScript-race.spec.js +217 -0
  371. package/dist/esm/tests/integration/iframe-ctx-addInitScript-race.spec.js.map +1 -0
  372. package/dist/esm/tests/integration/page-extra-http-headers.spec.d.ts +1 -0
  373. package/dist/esm/tests/integration/page-extra-http-headers.spec.js +83 -0
  374. package/dist/esm/tests/integration/page-extra-http-headers.spec.js.map +1 -0
  375. package/dist/esm/tests/integration/page-screenshot.spec.js +1 -1
  376. package/dist/esm/tests/integration/page-screenshot.spec.js.map +1 -1
  377. package/dist/esm/tests/integration/testUtils.d.ts +33 -0
  378. package/dist/esm/tests/integration/testUtils.js +138 -0
  379. package/dist/esm/tests/integration/testUtils.js.map +1 -1
  380. package/dist/esm/tests/integration/timeouts.spec.js +278 -0
  381. package/dist/esm/tests/integration/timeouts.spec.js.map +1 -1
  382. package/dist/esm/tests/unit/agent-captcha-hooks.test.d.ts +1 -0
  383. package/dist/esm/tests/unit/agent-captcha-hooks.test.js +283 -0
  384. package/dist/esm/tests/unit/agent-captcha-hooks.test.js.map +1 -0
  385. package/dist/esm/tests/unit/agent-execution-model.test.js +1 -1
  386. package/dist/esm/tests/unit/agent-execution-model.test.js.map +1 -1
  387. package/dist/esm/tests/unit/browserbase-session-accessors.test.js +20 -0
  388. package/dist/esm/tests/unit/browserbase-session-accessors.test.js.map +1 -1
  389. package/dist/esm/tests/unit/captcha-solver.test.d.ts +1 -0
  390. package/dist/esm/tests/unit/captcha-solver.test.js +152 -0
  391. package/dist/esm/tests/unit/captcha-solver.test.js.map +1 -0
  392. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.d.ts +1 -0
  393. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.js +93 -0
  394. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.js.map +1 -0
  395. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.d.ts +1 -0
  396. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.js +41 -0
  397. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.js.map +1 -0
  398. package/dist/esm/tests/unit/flowlogger-eventstore.test.d.ts +1 -0
  399. package/dist/esm/tests/unit/flowlogger-eventstore.test.js +248 -0
  400. package/dist/esm/tests/unit/flowlogger-eventstore.test.js.map +1 -0
  401. package/dist/esm/tests/unit/llm-middleware.test.d.ts +1 -0
  402. package/dist/esm/tests/unit/llm-middleware.test.js +460 -0
  403. package/dist/esm/tests/unit/llm-middleware.test.js.map +1 -0
  404. package/dist/esm/tests/unit/model-deprecation.test.js +5 -8
  405. package/dist/esm/tests/unit/model-deprecation.test.js.map +1 -1
  406. package/dist/esm/tests/unit/openai-cua-client.test.d.ts +1 -0
  407. package/dist/esm/tests/unit/openai-cua-client.test.js +69 -0
  408. package/dist/esm/tests/unit/openai-cua-client.test.js.map +1 -0
  409. package/dist/esm/tests/unit/page-extra-http-headers.test.d.ts +1 -0
  410. package/dist/esm/tests/unit/page-extra-http-headers.test.js +90 -0
  411. package/dist/esm/tests/unit/page-extra-http-headers.test.js.map +1 -0
  412. package/dist/esm/tests/unit/public-api/llm-and-agents.test.js +13 -1
  413. package/dist/esm/tests/unit/public-api/llm-and-agents.test.js.map +1 -1
  414. package/dist/esm/tests/unit/public-api/public-types.test.js.map +1 -1
  415. package/package.json +5 -3
  416. package/dist/cjs/lib/v3/agent/tools/search.js.map +0 -1
  417. package/dist/cjs/lib/v3/flowLogger.d.ts +0 -139
  418. package/dist/cjs/lib/v3/flowLogger.js +0 -881
  419. package/dist/cjs/lib/v3/flowLogger.js.map +0 -1
  420. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js +0 -209
  421. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  422. package/dist/esm/lib/v3/agent/tools/search.js.map +0 -1
  423. package/dist/esm/lib/v3/flowLogger.d.ts +0 -139
  424. package/dist/esm/lib/v3/flowLogger.js +0 -868
  425. package/dist/esm/lib/v3/flowLogger.js.map +0 -1
  426. package/dist/esm/tests/unit/rerender-missing-shadows.test.js +0 -207
  427. package/dist/esm/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  428. /package/dist/cjs/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
  429. /package/dist/cjs/tests/{unit/rerender-missing-shadows.test.d.ts → integration/agent-captcha-autosolve.spec.d.ts} +0 -0
  430. /package/dist/{esm/tests/unit/rerender-missing-shadows.test.d.ts → cjs/tests/integration/flowLogger.spec.d.ts} +0 -0
  431. /package/dist/esm/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
@@ -32,7 +32,6 @@ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn,
32
32
  if (target) Object.defineProperty(target, contextIn.name, descriptor);
33
33
  done = true;
34
34
  };
35
- import { EventEmitter } from "events";
36
35
  import fs from "fs";
37
36
  import os from "os";
38
37
  import path from "path";
@@ -50,6 +49,7 @@ import { ExtractHandler } from "./handlers/extractHandler.js";
50
49
  import { ObserveHandler } from "./handlers/observeHandler.js";
51
50
  import { V3AgentHandler } from "./handlers/v3AgentHandler.js";
52
51
  import { V3CuaAgentHandler } from "./handlers/v3CuaAgentHandler.js";
52
+ import { CAPTCHA_CUA_SYSTEM_PROMPT_NOTE } from "./agent/utils/captchaSolver.js";
53
53
  import { createBrowserbaseSession } from "./launch/browserbase.js";
54
54
  import { launchLocalChrome } from "./launch/local.js";
55
55
  import { LLMProvider } from "./llm/LLMProvider.js";
@@ -63,12 +63,15 @@ import { resolveModel } from "../modelUtils.js";
63
63
  import { StagehandAPIClient } from "./api.js";
64
64
  import { validateExperimentalFeatures } from "./agent/utils/validateExperimentalFeatures.js";
65
65
  import { flattenVariables } from "./agent/utils/variables.js";
66
- import { SessionFileLogger, logStagehandStep } from "./flowLogger.js";
66
+ import { FlowLogger } from "./flowlogger/FlowLogger.js";
67
+ import { EventEmitterWithWildcardSupport } from "./flowlogger/EventEmitter.js";
68
+ import { EventStore } from "./flowlogger/EventStore.js";
67
69
  import { createTimeoutGuard } from "./handlers/handlerUtils/timeoutGuard.js";
68
70
  import { ActTimeoutError } from "./types/public/sdkErrors.js";
69
71
  const DEFAULT_MODEL_NAME = "openai/gpt-4.1-mini";
70
72
  const DEFAULT_VIEWPORT = { width: 1288, height: 711 };
71
- function resolveModelConfiguration(model) {
73
+ const DEFAULT_AGENT_TOOL_TIMEOUT_MS = 45000;
74
+ export function resolveModelConfiguration(model) {
72
75
  if (!model) {
73
76
  return { modelName: DEFAULT_MODEL_NAME };
74
77
  }
@@ -76,13 +79,14 @@ function resolveModelConfiguration(model) {
76
79
  return { modelName: model };
77
80
  }
78
81
  if (model && typeof model === "object") {
79
- const { modelName, ...clientOptions } = model;
82
+ const { modelName, middleware, ...clientOptions } = model;
80
83
  if (!modelName) {
81
84
  throw new StagehandInvalidArgumentError("model.modelName is required when providing client options.");
82
85
  }
83
86
  return {
84
87
  modelName,
85
88
  clientOptions: clientOptions,
89
+ middleware,
86
90
  };
87
91
  }
88
92
  return { modelName: DEFAULT_MODEL_NAME };
@@ -110,9 +114,15 @@ let V3 = (() => {
110
114
  return class V3 {
111
115
  static {
112
116
  const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
113
- _act_decorators = [logStagehandStep("Stagehand.act", "ACT")];
114
- _extract_decorators = [logStagehandStep("Stagehand.extract", "EXTRACT")];
115
- _observe_decorators = [logStagehandStep("Stagehand.observe", "OBSERVE")];
117
+ _act_decorators = [FlowLogger.wrapWithLogging({
118
+ eventType: "StagehandAct",
119
+ })];
120
+ _extract_decorators = [FlowLogger.wrapWithLogging({
121
+ eventType: "StagehandExtract",
122
+ })];
123
+ _observe_decorators = [FlowLogger.wrapWithLogging({
124
+ eventType: "StagehandObserve",
125
+ })];
116
126
  __esDecorate(this, null, _act_decorators, { kind: "method", name: "act", static: false, private: false, access: { has: obj => "act" in obj, get: obj => obj.act }, metadata: _metadata }, null, _instanceExtraInitializers);
117
127
  __esDecorate(this, null, _extract_decorators, { kind: "method", name: "extract", static: false, private: false, access: { has: obj => "extract" in obj, get: obj => obj.extract }, metadata: _metadata }, null, _instanceExtraInitializers);
118
128
  __esDecorate(this, null, _observe_decorators, { kind: "method", name: "observe", static: false, private: false, access: { has: obj => "observe" in obj, get: obj => obj.observe }, metadata: _metadata }, null, _instanceExtraInitializers);
@@ -129,7 +139,7 @@ let V3 = (() => {
129
139
  * Event bus for internal communication.
130
140
  * Emits events like 'screenshot' when screenshots are captured during agent execution.
131
141
  */
132
- bus = new EventEmitter();
142
+ bus = new EventEmitterWithWildcardSupport();
133
143
  modelName;
134
144
  modelClientOptions;
135
145
  llmProvider;
@@ -154,6 +164,15 @@ let V3 = (() => {
154
164
  get isBrowserbase() {
155
165
  return this.state.kind === "BROWSERBASE";
156
166
  }
167
+ /**
168
+ * Returns true if captcha auto-solving is enabled on Browserbase.
169
+ * Defaults to true when not explicitly set to false.
170
+ */
171
+ get isCaptchaAutoSolveEnabled() {
172
+ return (this.isBrowserbase &&
173
+ this.opts.browserbaseSessionCreateParams?.browserSettings
174
+ ?.solveCaptchas !== false);
175
+ }
157
176
  /**
158
177
  * Returns true if advancedStealth is enabled in Browserbase settings.
159
178
  */
@@ -197,6 +216,9 @@ let V3 = (() => {
197
216
  stagehandLogger;
198
217
  _history = [];
199
218
  instanceId;
219
+ sessionId;
220
+ eventStore;
221
+ flowLoggerContext;
200
222
  static _processGuardsInstalled = false;
201
223
  static _instances = new Set();
202
224
  cacheStorage;
@@ -236,6 +258,7 @@ let V3 = (() => {
236
258
  this.externalLogger = opts.logger;
237
259
  this.verbose = opts.verbose ?? 1;
238
260
  this.instanceId = uuidv7();
261
+ this.sessionId = opts.sessionId ?? this.instanceId;
239
262
  this.keepAlive =
240
263
  opts.keepAlive ?? opts.browserbaseSessionCreateParams?.keepAlive;
241
264
  // Create per-instance StagehandLogger (handles usePino, verbose, externalLogger)
@@ -267,11 +290,11 @@ let V3 = (() => {
267
290
  catch {
268
291
  // ignore
269
292
  }
270
- const { modelName, clientOptions } = resolveModelConfiguration(opts.model);
293
+ const { modelName, clientOptions, middleware } = resolveModelConfiguration(opts.model);
271
294
  this.modelName = modelName;
272
295
  this.experimental = opts.experimental ?? false;
273
296
  this.logInferenceToFile = opts.logInferenceToFile ?? false;
274
- this.llmProvider = new LLMProvider(this.logger);
297
+ this.llmProvider = new LLMProvider(this.logger, middleware);
275
298
  this.domSettleTimeoutMs = opts.domSettleTimeout;
276
299
  this.disableAPI = opts.disableAPI ?? false;
277
300
  const baseClientOptions = clientOptions
@@ -328,8 +351,21 @@ let V3 = (() => {
328
351
  act: this.act.bind(this),
329
352
  });
330
353
  this.opts = opts;
331
- // Initialize session file logger
332
- SessionFileLogger.init(this.instanceId, opts);
354
+ // FlowLogger always gets a per-instance session context and shared event
355
+ // bus. The attached EventStore decides which sinks are active:
356
+ // `BROWSERBASE_FLOW_LOGS=1` enables pretty stderr output,
357
+ // and `BROWSERBASE_CONFIG_DIR` enables the pretty/jsonl file sinks for this session.
358
+ this.eventStore = new EventStore(this.sessionId, opts);
359
+ this.flowLoggerContext = FlowLogger.init(this.sessionId, this.bus);
360
+ // Flow event pipeline:
361
+ // FlowLogger -> this.bus -> this.eventStore -> configured sinks/query history.
362
+ // V3 owns the bus for this session. EventStore is not another bus; it just
363
+ // receives already-emitted FlowEvents here, then fans them out to sinks and
364
+ // keeps the queryable per-session history used by /v4/log, parent/ancestor lookups, and tests.
365
+ // `on()` stores a strong reference to the handler, so the EventStore
366
+ // stays alive until this bus is garbage-collected with the rest of the V3
367
+ // object graph.
368
+ this.bus.on("*", this.eventStore.emit);
333
369
  // Track instance for global process guard handling
334
370
  V3._instances.add(this);
335
371
  }
@@ -359,15 +395,18 @@ let V3 = (() => {
359
395
  }
360
396
  let modelName;
361
397
  let clientOptions;
398
+ let perCallMiddleware;
362
399
  if (typeof model === "string") {
363
400
  modelName = model;
364
401
  }
365
402
  else {
366
- const { modelName: overrideModelName, ...rest } = model;
403
+ const { modelName: overrideModelName, middleware, ...rest } = model;
367
404
  modelName = overrideModelName;
368
405
  clientOptions = rest;
406
+ perCallMiddleware = middleware;
369
407
  }
370
408
  if (modelName === this.modelName &&
409
+ !perCallMiddleware &&
371
410
  (!clientOptions || Object.keys(clientOptions).length === 0)) {
372
411
  return this.llmClient;
373
412
  }
@@ -392,7 +431,11 @@ let V3 = (() => {
392
431
  if (cached) {
393
432
  return cached;
394
433
  }
395
- const client = this.llmProvider.getClient(modelName, mergedOptions, { experimental: this.experimental, disableAPI: this.disableAPI });
434
+ const client = this.llmProvider.getClient(modelName, mergedOptions, {
435
+ experimental: this.experimental,
436
+ disableAPI: this.disableAPI,
437
+ middleware: perCallMiddleware,
438
+ });
396
439
  this.overrideLlmClients.set(cacheKey, client);
397
440
  return client;
398
441
  }
@@ -552,6 +595,13 @@ let V3 = (() => {
552
595
  }
553
596
  }
554
597
  const lbo = this.opts.localBrowserLaunchOptions ?? {};
598
+ if (lbo.cdpHeaders && !lbo.cdpUrl) {
599
+ this.logger({
600
+ category: "init",
601
+ message: "`cdpHeaders` was provided but `cdpUrl` is not set — cdpHeaders will be ignored. Set `cdpUrl` to connect to an existing browser via CDP.",
602
+ level: 2,
603
+ });
604
+ }
555
605
  // If a CDP URL is provided, attach instead of launching.
556
606
  if (lbo.cdpUrl) {
557
607
  this.logger({
@@ -561,10 +611,9 @@ let V3 = (() => {
561
611
  });
562
612
  this.ctx = await V3Context.create(lbo.cdpUrl, {
563
613
  env: "LOCAL",
614
+ cdpHeaders: lbo.cdpHeaders,
564
615
  });
565
- const logCtx = SessionFileLogger.getContext();
566
- this.ctx.conn.cdpLogger = (info) => SessionFileLogger.logCdpCallEvent(info, logCtx);
567
- this.ctx.conn.cdpEventLogger = (info) => SessionFileLogger.logCdpMessageEvent(info, logCtx);
616
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
568
617
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
569
618
  this.state = {
570
619
  kind: "LOCAL",
@@ -660,9 +709,7 @@ let V3 = (() => {
660
709
  env: "LOCAL",
661
710
  localBrowserLaunchOptions: lbo,
662
711
  });
663
- const logCtx = SessionFileLogger.getContext();
664
- this.ctx.conn.cdpLogger = (info) => SessionFileLogger.logCdpCallEvent(info, logCtx);
665
- this.ctx.conn.cdpEventLogger = (info) => SessionFileLogger.logCdpMessageEvent(info, logCtx);
712
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
666
713
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
667
714
  this.state = {
668
715
  kind: "LOCAL",
@@ -689,9 +736,6 @@ let V3 = (() => {
689
736
  }
690
737
  if (this.opts.env === "BROWSERBASE") {
691
738
  const { apiKey, projectId } = this.requireBrowserbaseCreds();
692
- if (!apiKey || !projectId) {
693
- throw new MissingEnvironmentVariableError("BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID", "Browserbase environment");
694
- }
695
739
  this.logger({
696
740
  category: "init",
697
741
  message: "Starting browserbase session",
@@ -700,9 +744,13 @@ let V3 = (() => {
700
744
  const baseSessionParams = this.opts.browserbaseSessionCreateParams ?? {};
701
745
  const resolvedKeepAlive = this.keepAlive;
702
746
  const keepAlive = this.keepAlive === true;
703
- const effectiveSessionParams = resolvedKeepAlive !== undefined
704
- ? { ...baseSessionParams, keepAlive: resolvedKeepAlive }
705
- : baseSessionParams;
747
+ let effectiveSessionParams = baseSessionParams;
748
+ if (resolvedKeepAlive !== undefined) {
749
+ effectiveSessionParams = {
750
+ ...baseSessionParams,
751
+ keepAlive: resolvedKeepAlive,
752
+ };
753
+ }
706
754
  if (!this.disableAPI && !this.experimental) {
707
755
  this.apiClient = new StagehandAPIClient({
708
756
  apiKey,
@@ -710,18 +758,20 @@ let V3 = (() => {
710
758
  logger: this.logger,
711
759
  serverCache: this.opts.serverCache,
712
760
  });
761
+ const { projectId: overrideProjectId, browserSettings, userMetadata, ...restSessionParams } = effectiveSessionParams;
762
+ const resolvedProjectId = overrideProjectId ?? projectId;
713
763
  const createSessionPayload = {
714
- projectId: effectiveSessionParams.projectId ?? projectId,
715
- ...effectiveSessionParams,
764
+ ...(resolvedProjectId ? { projectId: resolvedProjectId } : {}),
765
+ ...restSessionParams,
716
766
  browserSettings: {
717
- ...(effectiveSessionParams.browserSettings ?? {}),
718
- viewport: effectiveSessionParams.browserSettings?.viewport ?? {
767
+ ...(browserSettings ?? {}),
768
+ viewport: browserSettings?.viewport ?? {
719
769
  width: 1288,
720
770
  height: 711,
721
771
  },
722
772
  },
723
773
  userMetadata: {
724
- ...(effectiveSessionParams.userMetadata ?? {}),
774
+ ...(userMetadata ?? {}),
725
775
  stagehand: "true",
726
776
  },
727
777
  };
@@ -745,9 +795,7 @@ let V3 = (() => {
745
795
  env: "BROWSERBASE",
746
796
  apiClient: this.apiClient,
747
797
  });
748
- const logCtx = SessionFileLogger.getContext();
749
- this.ctx.conn.cdpLogger = (info) => SessionFileLogger.logCdpCallEvent(info, logCtx);
750
- this.ctx.conn.cdpEventLogger = (info) => SessionFileLogger.logCdpMessageEvent(info, logCtx);
798
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
751
799
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
752
800
  this.state = { kind: "BROWSERBASE", sessionId, ws, bb };
753
801
  this.browserbaseSessionId = sessionId;
@@ -869,10 +917,7 @@ let V3 = (() => {
869
917
  });
870
918
  }
871
919
  else {
872
- const effectiveTimeoutMs = typeof options?.timeout === "number" && options.timeout > 0
873
- ? options.timeout
874
- : undefined;
875
- const ensureTimeRemaining = createTimeoutGuard(effectiveTimeoutMs, (ms) => new ActTimeoutError(ms));
920
+ const ensureTimeRemaining = createTimeoutGuard(options?.timeout, (ms) => new ActTimeoutError(ms));
876
921
  actResult = await this.actHandler.takeDeterministicAction({ ...input, selector }, v3Page, this.domSettleTimeoutMs, this.resolveLlmClient(options?.model), ensureTimeRemaining, options?.variables);
877
922
  }
878
923
  // history: record ObserveResult-based act call
@@ -1093,7 +1138,7 @@ let V3 = (() => {
1093
1138
  try {
1094
1139
  // Close session file logger
1095
1140
  try {
1096
- await SessionFileLogger.close();
1141
+ await FlowLogger.close(this.flowLoggerContext);
1097
1142
  }
1098
1143
  catch {
1099
1144
  // ignore
@@ -1129,6 +1174,12 @@ let V3 = (() => {
1129
1174
  catch {
1130
1175
  // ignore
1131
1176
  }
1177
+ try {
1178
+ await this.eventStore.destroy();
1179
+ }
1180
+ catch {
1181
+ // ignore
1182
+ }
1132
1183
  try {
1133
1184
  this.bus.removeAllListeners();
1134
1185
  }
@@ -1142,6 +1193,13 @@ let V3 = (() => {
1142
1193
  V3._instances.delete(this);
1143
1194
  }
1144
1195
  }
1196
+ /**
1197
+ * Resolves the Browserbase API key from options or environment variables.
1198
+ * Returns undefined if no key is found (does not throw).
1199
+ */
1200
+ get browserbaseApiKey() {
1201
+ return this.opts.apiKey || process.env.BROWSERBASE_API_KEY;
1202
+ }
1145
1203
  /** Guard: ensure Browserbase credentials exist in options. */
1146
1204
  requireBrowserbaseCreds() {
1147
1205
  let { apiKey, projectId } = this.opts;
@@ -1151,17 +1209,13 @@ let V3 = (() => {
1151
1209
  if (!projectId)
1152
1210
  projectId =
1153
1211
  process.env.BROWSERBASE_PROJECT_ID ?? process.env.BB_PROJECT_ID;
1154
- if (!apiKey || !projectId) {
1155
- const missing = [];
1156
- if (!apiKey)
1157
- missing.push("BROWSERBASE_API_KEY");
1158
- if (!projectId)
1159
- missing.push("BROWSERBASE_PROJECT_ID");
1160
- throw new MissingEnvironmentVariableError(missing.join(", "), "Browserbase");
1212
+ if (!apiKey) {
1213
+ throw new MissingEnvironmentVariableError("BROWSERBASE_API_KEY", "Browserbase");
1161
1214
  }
1162
1215
  // Cache resolved values back into opts for consistency
1163
1216
  this.opts.apiKey = apiKey;
1164
- this.opts.projectId = projectId;
1217
+ if (projectId)
1218
+ this.opts.projectId = projectId;
1165
1219
  // Informational log
1166
1220
  this.logger({
1167
1221
  category: "init",
@@ -1303,10 +1357,16 @@ let V3 = (() => {
1303
1357
  ? this.resolveLlmClient(options.model)
1304
1358
  : this.llmClient;
1305
1359
  const resolvedExecutionModel = options?.executionModel ?? options?.model;
1306
- const handler = new V3AgentHandler(this, this.logger, agentLlmClient, resolvedExecutionModel, options?.systemPrompt, tools, options?.mode);
1360
+ const handler = new V3AgentHandler(this, this.logger, agentLlmClient, resolvedExecutionModel, options?.systemPrompt, tools, options?.mode, this.isCaptchaAutoSolveEnabled);
1307
1361
  const resolvedOptions = typeof instructionOrOptions === "string"
1308
- ? { instruction: instructionOrOptions }
1309
- : instructionOrOptions;
1362
+ ? {
1363
+ instruction: instructionOrOptions,
1364
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1365
+ }
1366
+ : {
1367
+ ...instructionOrOptions,
1368
+ toolTimeout: instructionOrOptions.toolTimeout ?? DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1369
+ };
1310
1370
  const callbacksWithSafety = resolvedOptions.callbacks;
1311
1371
  if (callbacksWithSafety?.onSafetyConfirmation) {
1312
1372
  throw new StagehandInvalidArgumentError('onSafetyConfirmation callback is only supported when using mode: "cua" agents.');
@@ -1386,145 +1446,59 @@ let V3 = (() => {
1386
1446
  throw new CuaModelRequiredError(AVAILABLE_CUA_MODELS);
1387
1447
  }
1388
1448
  const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1389
- return {
1390
- execute: async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1391
- validateExperimentalFeatures({
1392
- isExperimental: this.experimental,
1393
- agentConfig: options,
1394
- executeOptions: typeof instructionOrOptions === "object"
1395
- ? instructionOrOptions
1396
- : null,
1397
- });
1398
- SessionFileLogger.logAgentTaskStarted({
1399
- invocation: "Agent.execute",
1400
- args: [instructionOrOptions],
1401
- });
1402
- const tools = options?.integrations
1403
- ? await resolveTools(options.integrations, options.tools)
1404
- : (options?.tools ?? {});
1405
- const handler = new V3CuaAgentHandler(this, this.logger, {
1406
- modelName,
1407
- clientOptions,
1408
- userProvidedInstructions: options.systemPrompt ??
1409
- `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1410
- }, tools);
1411
- const resolvedOptions = typeof instructionOrOptions === "string"
1412
- ? { instruction: instructionOrOptions }
1413
- : instructionOrOptions;
1414
- if (resolvedOptions.page) {
1415
- const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1416
- this.ctx.setActivePage(normalizedPage);
1417
- }
1418
- const instruction = resolvedOptions.instruction.trim();
1419
- const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1420
- const cacheVariables = flattenVariables(resolvedOptions.variables);
1421
- let cacheContext = null;
1422
- if (this.agentCache.shouldAttemptCache(instruction)) {
1423
- const startPage = await this.ctx.awaitActivePage();
1424
- cacheContext = await this.agentCache.prepareContext({
1425
- instruction,
1426
- options: sanitizedOptions,
1427
- configSignature: agentConfigSignature,
1428
- page: startPage,
1429
- variables: cacheVariables,
1430
- });
1431
- if (cacheContext) {
1432
- const replayed = await this.agentCache.tryReplay(cacheContext);
1433
- if (replayed) {
1434
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1435
- return replayed;
1436
- }
1437
- }
1438
- }
1439
- let agentSteps = [];
1440
- const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1441
- if (shouldRecordLocally) {
1442
- this.beginAgentReplayRecording();
1443
- }
1444
- let result;
1445
- try {
1446
- if (this.apiClient && !this.experimental) {
1447
- const page = await this.ctx.awaitActivePage();
1448
- result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1449
- if (cacheContext) {
1450
- const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1451
- await this.agentCache.storeTransferredEntry(transferredEntry);
1452
- }
1453
- }
1454
- else {
1455
- result = await handler.execute(instructionOrOptions);
1456
- }
1457
- if (shouldRecordLocally) {
1458
- agentSteps = this.endAgentReplayRecording();
1459
- }
1460
- if (shouldRecordLocally &&
1461
- cacheContext &&
1462
- result.success &&
1463
- agentSteps.length > 0) {
1464
- await this.agentCache.store(cacheContext, agentSteps, result);
1465
- }
1466
- return result;
1467
- }
1468
- catch (err) {
1469
- if (shouldRecordLocally)
1470
- this.discardAgentReplayRecording();
1471
- throw err;
1472
- }
1473
- finally {
1474
- if (shouldRecordLocally) {
1475
- this.discardAgentReplayRecording();
1476
- }
1477
- SessionFileLogger.logAgentTaskCompleted();
1478
- }
1479
- }),
1480
- };
1481
- }
1482
- // Default: AISDK tools-based agent
1483
- const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1484
- const isStreaming = options?.stream ?? false;
1485
- return {
1486
- execute: async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1449
+ const execute = async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1487
1450
  validateExperimentalFeatures({
1488
1451
  isExperimental: this.experimental,
1489
1452
  agentConfig: options,
1490
1453
  executeOptions: typeof instructionOrOptions === "object"
1491
1454
  ? instructionOrOptions
1492
1455
  : null,
1493
- isStreaming,
1494
1456
  });
1495
- SessionFileLogger.logAgentTaskStarted({
1496
- invocation: "Agent.execute",
1497
- args: [instructionOrOptions],
1498
- });
1499
- // Streaming mode
1500
- if (isStreaming) {
1501
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1457
+ const tools = options?.integrations
1458
+ ? await resolveTools(options.integrations, options.tools)
1459
+ : (options?.tools ?? {});
1460
+ const handler = new V3CuaAgentHandler(this, this.logger, {
1461
+ modelName,
1462
+ clientOptions,
1463
+ userProvidedInstructions: (options.systemPrompt ??
1464
+ `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`) +
1465
+ (this.isCaptchaAutoSolveEnabled
1466
+ ? CAPTCHA_CUA_SYSTEM_PROMPT_NOTE
1467
+ : ""),
1468
+ }, tools);
1469
+ const resolvedOptions = typeof instructionOrOptions === "string"
1470
+ ? {
1471
+ instruction: instructionOrOptions,
1472
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1473
+ }
1474
+ : {
1475
+ ...instructionOrOptions,
1476
+ toolTimeout: instructionOrOptions.toolTimeout ??
1477
+ DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1478
+ };
1479
+ if (resolvedOptions.page) {
1480
+ const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1481
+ this.ctx.setActivePage(normalizedPage);
1482
+ }
1483
+ const instruction = resolvedOptions.instruction.trim();
1484
+ const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1485
+ const cacheVariables = flattenVariables(resolvedOptions.variables);
1486
+ let cacheContext = null;
1487
+ if (this.agentCache.shouldAttemptCache(instruction)) {
1488
+ const startPage = await this.ctx.awaitActivePage();
1489
+ cacheContext = await this.agentCache.prepareContext({
1490
+ instruction,
1491
+ options: sanitizedOptions,
1492
+ configSignature: agentConfigSignature,
1493
+ page: startPage,
1494
+ variables: cacheVariables,
1495
+ });
1502
1496
  if (cacheContext) {
1503
- const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1497
+ const replayed = await this.agentCache.tryReplay(cacheContext);
1504
1498
  if (replayed) {
1505
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1506
1499
  return replayed;
1507
1500
  }
1508
1501
  }
1509
- const streamResult = await handler.stream(resolvedOptions);
1510
- if (cacheContext) {
1511
- const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1512
- // Log completion when stream is returned (stream completes asynchronously)
1513
- SessionFileLogger.logAgentTaskCompleted();
1514
- return wrappedStream;
1515
- }
1516
- // Log completion when stream is returned (stream completes asynchronously)
1517
- SessionFileLogger.logAgentTaskCompleted();
1518
- return streamResult;
1519
- }
1520
- // Non-streaming mode (default)
1521
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1522
- if (cacheContext) {
1523
- const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1524
- if (replayed) {
1525
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1526
- return replayed;
1527
- }
1528
1502
  }
1529
1503
  let agentSteps = [];
1530
1504
  const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
@@ -1535,14 +1509,14 @@ let V3 = (() => {
1535
1509
  try {
1536
1510
  if (this.apiClient && !this.experimental) {
1537
1511
  const page = await this.ctx.awaitActivePage();
1538
- result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1512
+ result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1539
1513
  if (cacheContext) {
1540
1514
  const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1541
1515
  await this.agentCache.storeTransferredEntry(transferredEntry);
1542
1516
  }
1543
1517
  }
1544
1518
  else {
1545
- result = await handler.execute(resolvedOptions);
1519
+ result = await handler.execute(instructionOrOptions);
1546
1520
  }
1547
1521
  if (shouldRecordLocally) {
1548
1522
  agentSteps = this.endAgentReplayRecording();
@@ -1564,9 +1538,96 @@ let V3 = (() => {
1564
1538
  if (shouldRecordLocally) {
1565
1539
  this.discardAgentReplayRecording();
1566
1540
  }
1567
- SessionFileLogger.logAgentTaskCompleted();
1568
1541
  }
1569
- }),
1542
+ });
1543
+ return {
1544
+ execute: FlowLogger.wrapWithLogging({
1545
+ eventType: "AgentExecute",
1546
+ context: this.flowLoggerContext,
1547
+ })(execute),
1548
+ };
1549
+ }
1550
+ // Default: AISDK tools-based agent
1551
+ const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1552
+ const isStreaming = options?.stream ?? false;
1553
+ const execute = async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1554
+ validateExperimentalFeatures({
1555
+ isExperimental: this.experimental,
1556
+ agentConfig: options,
1557
+ executeOptions: typeof instructionOrOptions === "object"
1558
+ ? instructionOrOptions
1559
+ : null,
1560
+ isStreaming,
1561
+ });
1562
+ // Streaming mode
1563
+ if (isStreaming) {
1564
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1565
+ if (cacheContext) {
1566
+ const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1567
+ if (replayed) {
1568
+ return replayed;
1569
+ }
1570
+ }
1571
+ const streamResult = await handler.stream(resolvedOptions);
1572
+ if (cacheContext) {
1573
+ const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1574
+ return wrappedStream;
1575
+ }
1576
+ return streamResult;
1577
+ }
1578
+ // Non-streaming mode (default)
1579
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1580
+ if (cacheContext) {
1581
+ const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1582
+ if (replayed) {
1583
+ return replayed;
1584
+ }
1585
+ }
1586
+ let agentSteps = [];
1587
+ const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1588
+ if (shouldRecordLocally) {
1589
+ this.beginAgentReplayRecording();
1590
+ }
1591
+ let result;
1592
+ try {
1593
+ if (this.apiClient && !this.experimental) {
1594
+ const page = await this.ctx.awaitActivePage();
1595
+ result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1596
+ if (cacheContext) {
1597
+ const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1598
+ await this.agentCache.storeTransferredEntry(transferredEntry);
1599
+ }
1600
+ }
1601
+ else {
1602
+ result = await handler.execute(resolvedOptions);
1603
+ }
1604
+ if (shouldRecordLocally) {
1605
+ agentSteps = this.endAgentReplayRecording();
1606
+ }
1607
+ if (shouldRecordLocally &&
1608
+ cacheContext &&
1609
+ result.success &&
1610
+ agentSteps.length > 0) {
1611
+ await this.agentCache.store(cacheContext, agentSteps, result);
1612
+ }
1613
+ return result;
1614
+ }
1615
+ catch (err) {
1616
+ if (shouldRecordLocally)
1617
+ this.discardAgentReplayRecording();
1618
+ throw err;
1619
+ }
1620
+ finally {
1621
+ if (shouldRecordLocally) {
1622
+ this.discardAgentReplayRecording();
1623
+ }
1624
+ }
1625
+ });
1626
+ return {
1627
+ execute: FlowLogger.wrapWithLogging({
1628
+ eventType: "AgentExecute",
1629
+ context: this.flowLoggerContext,
1630
+ })(execute),
1570
1631
  };
1571
1632
  }
1572
1633
  };