@browserbasehq/orca 3.1.0-patch.4 → 3.2.0-middleware.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 (495) hide show
  1. package/dist/cjs/lib/inference.d.ts +3 -1
  2. package/dist/cjs/lib/inference.js +4 -7
  3. package/dist/cjs/lib/inference.js.map +1 -1
  4. package/dist/cjs/lib/prompt.d.ts +1 -1
  5. package/dist/cjs/lib/prompt.js +24 -18
  6. package/dist/cjs/lib/prompt.js.map +1 -1
  7. package/dist/cjs/lib/utils.d.ts +1 -0
  8. package/dist/cjs/lib/utils.js +4 -0
  9. package/dist/cjs/lib/utils.js.map +1 -1
  10. package/dist/cjs/lib/v3/agent/AgentClient.d.ts +8 -0
  11. package/dist/cjs/lib/v3/agent/AgentClient.js +13 -0
  12. package/dist/cjs/lib/v3/agent/AgentClient.js.map +1 -1
  13. package/dist/cjs/lib/v3/agent/AgentProvider.js +0 -1
  14. package/dist/cjs/lib/v3/agent/AgentProvider.js.map +1 -1
  15. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +6 -7
  16. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  17. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +6 -7
  18. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  19. package/dist/cjs/lib/v3/agent/MicrosoftCUAClient.js +1 -0
  20. package/dist/cjs/lib/v3/agent/MicrosoftCUAClient.js.map +1 -1
  21. package/dist/cjs/lib/v3/agent/OpenAICUAClient.d.ts +10 -6
  22. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +107 -18
  23. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  24. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.d.ts +4 -2
  25. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js +11 -12
  26. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  27. package/dist/cjs/lib/v3/agent/tools/act.d.ts +1 -1
  28. package/dist/cjs/lib/v3/agent/tools/act.js +11 -4
  29. package/dist/cjs/lib/v3/agent/tools/act.js.map +1 -1
  30. package/dist/cjs/lib/v3/agent/tools/ariaTree.d.ts +8 -1
  31. package/dist/cjs/lib/v3/agent/tools/ariaTree.js +49 -22
  32. package/dist/cjs/lib/v3/agent/tools/ariaTree.js.map +1 -1
  33. package/dist/cjs/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  34. package/dist/cjs/lib/v3/agent/tools/braveSearch.js.map +1 -0
  35. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  36. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js +70 -0
  37. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  38. package/dist/cjs/lib/v3/agent/tools/click.js +23 -31
  39. package/dist/cjs/lib/v3/agent/tools/click.js.map +1 -1
  40. package/dist/cjs/lib/v3/agent/tools/clickAndHold.js.map +1 -1
  41. package/dist/cjs/lib/v3/agent/tools/dragAndDrop.js +22 -30
  42. package/dist/cjs/lib/v3/agent/tools/dragAndDrop.js.map +1 -1
  43. package/dist/cjs/lib/v3/agent/tools/extract.d.ts +2 -2
  44. package/dist/cjs/lib/v3/agent/tools/extract.js +7 -3
  45. package/dist/cjs/lib/v3/agent/tools/extract.js.map +1 -1
  46. package/dist/cjs/lib/v3/agent/tools/fillFormVision.js +30 -30
  47. package/dist/cjs/lib/v3/agent/tools/fillFormVision.js.map +1 -1
  48. package/dist/cjs/lib/v3/agent/tools/fillform.d.ts +7 -2
  49. package/dist/cjs/lib/v3/agent/tools/fillform.js +56 -45
  50. package/dist/cjs/lib/v3/agent/tools/fillform.js.map +1 -1
  51. package/dist/cjs/lib/v3/agent/tools/index.d.ts +19 -3
  52. package/dist/cjs/lib/v3/agent/tools/index.js +63 -11
  53. package/dist/cjs/lib/v3/agent/tools/index.js.map +1 -1
  54. package/dist/cjs/lib/v3/agent/tools/keys.d.ts +1 -1
  55. package/dist/cjs/lib/v3/agent/tools/keys.js.map +1 -1
  56. package/dist/cjs/lib/v3/agent/tools/screenshot.d.ts +8 -0
  57. package/dist/cjs/lib/v3/agent/tools/screenshot.js +32 -15
  58. package/dist/cjs/lib/v3/agent/tools/screenshot.js.map +1 -1
  59. package/dist/cjs/lib/v3/agent/tools/scroll.js +12 -0
  60. package/dist/cjs/lib/v3/agent/tools/scroll.js.map +1 -1
  61. package/dist/cjs/lib/v3/agent/tools/type.js +23 -31
  62. package/dist/cjs/lib/v3/agent/tools/type.js.map +1 -1
  63. package/dist/cjs/lib/v3/agent/tools/wait.js +6 -0
  64. package/dist/cjs/lib/v3/agent/tools/wait.js.map +1 -1
  65. package/dist/cjs/lib/v3/agent/utils/captchaSolver.d.ts +76 -0
  66. package/dist/cjs/lib/v3/agent/utils/captchaSolver.js +175 -0
  67. package/dist/cjs/lib/v3/agent/utils/captchaSolver.js.map +1 -0
  68. package/dist/cjs/lib/v3/agent/utils/handleDoneToolCall.js +4 -0
  69. package/dist/cjs/lib/v3/agent/utils/handleDoneToolCall.js.map +1 -1
  70. package/dist/cjs/lib/v3/agent/utils/variables.d.ts +5 -0
  71. package/dist/cjs/lib/v3/agent/utils/variables.js +9 -0
  72. package/dist/cjs/lib/v3/agent/utils/variables.js.map +1 -1
  73. package/dist/cjs/lib/v3/api.d.ts +2 -2
  74. package/dist/cjs/lib/v3/api.js +1 -1
  75. package/dist/cjs/lib/v3/api.js.map +1 -1
  76. package/dist/cjs/lib/v3/cache/ActCache.d.ts +0 -1
  77. package/dist/cjs/lib/v3/cache/ActCache.js +2 -18
  78. package/dist/cjs/lib/v3/cache/ActCache.js.map +1 -1
  79. package/dist/cjs/lib/v3/flowlogger/EventEmitter.d.ts +7 -0
  80. package/dist/cjs/lib/v3/flowlogger/EventEmitter.js +30 -0
  81. package/dist/cjs/lib/v3/flowlogger/EventEmitter.js.map +1 -0
  82. package/dist/cjs/lib/v3/flowlogger/EventSink.d.ts +44 -0
  83. package/dist/cjs/lib/v3/flowlogger/EventSink.js +217 -0
  84. package/dist/cjs/lib/v3/flowlogger/EventSink.js.map +1 -0
  85. package/dist/cjs/lib/v3/flowlogger/EventStore.d.ts +26 -0
  86. package/dist/cjs/lib/v3/flowlogger/EventStore.js +135 -0
  87. package/dist/cjs/lib/v3/flowlogger/EventStore.js.map +1 -0
  88. package/dist/cjs/lib/v3/flowlogger/FlowLogger.d.ts +99 -0
  89. package/dist/cjs/lib/v3/flowlogger/FlowLogger.js +591 -0
  90. package/dist/cjs/lib/v3/flowlogger/FlowLogger.js.map +1 -0
  91. package/dist/cjs/lib/v3/flowlogger/prettify.d.ts +6 -0
  92. package/dist/cjs/lib/v3/flowlogger/prettify.js +395 -0
  93. package/dist/cjs/lib/v3/flowlogger/prettify.js.map +1 -0
  94. package/dist/cjs/lib/v3/handlers/actHandler.js +1 -2
  95. package/dist/cjs/lib/v3/handlers/actHandler.js.map +1 -1
  96. package/dist/cjs/lib/v3/handlers/extractHandler.js +2 -2
  97. package/dist/cjs/lib/v3/handlers/extractHandler.js.map +1 -1
  98. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js +43 -57
  99. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  100. package/dist/cjs/lib/v3/handlers/observeHandler.js +3 -3
  101. package/dist/cjs/lib/v3/handlers/observeHandler.js.map +1 -1
  102. package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +2 -5
  103. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +130 -91
  104. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  105. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.d.ts +5 -0
  106. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +134 -14
  107. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  108. package/dist/cjs/lib/v3/index.d.ts +2 -1
  109. package/dist/cjs/lib/v3/launch/browserbase.d.ts +1 -1
  110. package/dist/cjs/lib/v3/launch/browserbase.js +4 -9
  111. package/dist/cjs/lib/v3/launch/browserbase.js.map +1 -1
  112. package/dist/cjs/lib/v3/llm/LLMProvider.d.ts +5 -2
  113. package/dist/cjs/lib/v3/llm/LLMProvider.js +14 -11
  114. package/dist/cjs/lib/v3/llm/LLMProvider.js.map +1 -1
  115. package/dist/cjs/lib/v3/llm/aisdk.d.ts +3 -1
  116. package/dist/cjs/lib/v3/llm/aisdk.js +24 -28
  117. package/dist/cjs/lib/v3/llm/aisdk.js.map +1 -1
  118. package/dist/cjs/lib/v3/runtimePaths.js +2 -1
  119. package/dist/cjs/lib/v3/runtimePaths.js.map +1 -1
  120. package/dist/cjs/lib/v3/shutdown/supervisor.js +2 -2
  121. package/dist/cjs/lib/v3/shutdown/supervisor.js.map +1 -1
  122. package/dist/cjs/lib/v3/timeoutConfig.d.ts +1 -1
  123. package/dist/cjs/lib/v3/timeoutConfig.js +5 -0
  124. package/dist/cjs/lib/v3/timeoutConfig.js.map +1 -1
  125. package/dist/cjs/lib/v3/types/private/cache.d.ts +0 -1
  126. package/dist/cjs/lib/v3/types/private/cache.js.map +1 -1
  127. package/dist/cjs/lib/v3/types/private/handlers.d.ts +1 -0
  128. package/dist/cjs/lib/v3/types/private/handlers.js.map +1 -1
  129. package/dist/cjs/lib/v3/types/private/shutdown.d.ts +1 -1
  130. package/dist/cjs/lib/v3/types/private/shutdown.js.map +1 -1
  131. package/dist/cjs/lib/v3/types/public/agent.d.ts +28 -3
  132. package/dist/cjs/lib/v3/types/public/agent.js +0 -1
  133. package/dist/cjs/lib/v3/types/public/agent.js.map +1 -1
  134. package/dist/cjs/lib/v3/types/public/api.d.ts +27 -7
  135. package/dist/cjs/lib/v3/types/public/api.js +42 -14
  136. package/dist/cjs/lib/v3/types/public/api.js.map +1 -1
  137. package/dist/cjs/lib/v3/types/public/methods.d.ts +1 -0
  138. package/dist/cjs/lib/v3/types/public/methods.js.map +1 -1
  139. package/dist/cjs/lib/v3/types/public/model.d.ts +14 -3
  140. package/dist/cjs/lib/v3/types/public/model.js.map +1 -1
  141. package/dist/cjs/lib/v3/types/public/options.d.ts +8 -0
  142. package/dist/cjs/lib/v3/types/public/options.js.map +1 -1
  143. package/dist/cjs/lib/v3/types/public/variables.d.ts +7 -0
  144. package/dist/cjs/lib/v3/types/public/variables.js +22 -0
  145. package/dist/cjs/lib/v3/types/public/variables.js.map +1 -0
  146. package/dist/cjs/lib/v3/understudy/cdp.d.ts +8 -13
  147. package/dist/cjs/lib/v3/understudy/cdp.js +180 -20
  148. package/dist/cjs/lib/v3/understudy/cdp.js.map +1 -1
  149. package/dist/cjs/lib/v3/understudy/context.d.ts +1 -0
  150. package/dist/cjs/lib/v3/understudy/context.js +148 -62
  151. package/dist/cjs/lib/v3/understudy/context.js.map +1 -1
  152. package/dist/cjs/lib/v3/understudy/frame.js +23 -6
  153. package/dist/cjs/lib/v3/understudy/frame.js.map +1 -1
  154. package/dist/cjs/lib/v3/understudy/page.d.ts +13 -0
  155. package/dist/cjs/lib/v3/understudy/page.js +85 -22
  156. package/dist/cjs/lib/v3/understudy/page.js.map +1 -1
  157. package/dist/cjs/lib/v3/understudy/screenshotUtils.d.ts +0 -1
  158. package/dist/cjs/lib/v3/understudy/screenshotUtils.js +0 -18
  159. package/dist/cjs/lib/v3/understudy/screenshotUtils.js.map +1 -1
  160. package/dist/cjs/lib/v3/v3.d.ts +26 -3
  161. package/dist/cjs/lib/v3/v3.js +250 -180
  162. package/dist/cjs/lib/v3/v3.js.map +1 -1
  163. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.js +56 -0
  164. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.js.map +1 -0
  165. package/dist/cjs/tests/integration/agent-hybrid-mode.spec.js +6 -6
  166. package/dist/cjs/tests/integration/agent-hybrid-mode.spec.js.map +1 -1
  167. package/dist/cjs/tests/integration/cdp-session-detached.spec.js +1 -1
  168. package/dist/cjs/tests/integration/cdp-session-detached.spec.js.map +1 -1
  169. package/dist/cjs/tests/integration/chrome-newtab-page-tracking.spec.js +56 -0
  170. package/dist/cjs/tests/integration/chrome-newtab-page-tracking.spec.js.map +1 -0
  171. package/dist/cjs/tests/integration/context-addInitScript.spec.js +104 -11
  172. package/dist/cjs/tests/integration/context-addInitScript.spec.js.map +1 -1
  173. package/dist/cjs/tests/integration/flowLogger.spec.d.ts +1 -0
  174. package/dist/cjs/tests/integration/flowLogger.spec.js +714 -0
  175. package/dist/cjs/tests/integration/flowLogger.spec.js.map +1 -0
  176. package/dist/cjs/tests/integration/iframe-ctx-addInitScript-race.spec.d.ts +1 -0
  177. package/dist/cjs/tests/integration/iframe-ctx-addInitScript-race.spec.js +219 -0
  178. package/dist/cjs/tests/integration/iframe-ctx-addInitScript-race.spec.js.map +1 -0
  179. package/dist/cjs/tests/integration/page-extra-http-headers.spec.d.ts +1 -0
  180. package/dist/cjs/tests/integration/page-extra-http-headers.spec.js +85 -0
  181. package/dist/cjs/tests/integration/page-extra-http-headers.spec.js.map +1 -0
  182. package/dist/cjs/tests/integration/page-screenshot.spec.js +1 -1
  183. package/dist/cjs/tests/integration/page-screenshot.spec.js.map +1 -1
  184. package/dist/cjs/tests/integration/testUtils.d.ts +33 -0
  185. package/dist/cjs/tests/integration/testUtils.js +144 -0
  186. package/dist/cjs/tests/integration/testUtils.js.map +1 -1
  187. package/dist/cjs/tests/integration/timeouts.spec.js +278 -0
  188. package/dist/cjs/tests/integration/timeouts.spec.js.map +1 -1
  189. package/dist/cjs/tests/unit/agent-captcha-hooks.test.d.ts +1 -0
  190. package/dist/cjs/tests/unit/agent-captcha-hooks.test.js +285 -0
  191. package/dist/cjs/tests/unit/agent-captcha-hooks.test.js.map +1 -0
  192. package/dist/cjs/tests/unit/agent-execution-model.test.js +25 -3
  193. package/dist/cjs/tests/unit/agent-execution-model.test.js.map +1 -1
  194. package/dist/cjs/tests/unit/agent-system-prompt-variables.test.d.ts +1 -0
  195. package/dist/cjs/tests/unit/agent-system-prompt-variables.test.js +23 -0
  196. package/dist/cjs/tests/unit/agent-system-prompt-variables.test.js.map +1 -0
  197. package/dist/cjs/tests/unit/aisdk-reasoning-effort.test.d.ts +1 -0
  198. package/dist/cjs/tests/unit/aisdk-reasoning-effort.test.js +153 -0
  199. package/dist/cjs/tests/unit/aisdk-reasoning-effort.test.js.map +1 -0
  200. package/dist/cjs/tests/unit/api-client-observe-variables.test.d.ts +1 -0
  201. package/dist/cjs/tests/unit/api-client-observe-variables.test.js +86 -0
  202. package/dist/cjs/tests/unit/api-client-observe-variables.test.js.map +1 -0
  203. package/dist/cjs/tests/unit/api-variables-schema.test.d.ts +1 -0
  204. package/dist/cjs/tests/unit/api-variables-schema.test.js +37 -0
  205. package/dist/cjs/tests/unit/api-variables-schema.test.js.map +1 -0
  206. package/dist/cjs/tests/unit/browserbase-session-accessors.test.js +20 -0
  207. package/dist/cjs/tests/unit/browserbase-session-accessors.test.js.map +1 -1
  208. package/dist/cjs/tests/unit/captcha-solver.test.d.ts +1 -0
  209. package/dist/cjs/tests/unit/captcha-solver.test.js +154 -0
  210. package/dist/cjs/tests/unit/captcha-solver.test.js.map +1 -0
  211. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.d.ts +1 -0
  212. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.js +95 -0
  213. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.js.map +1 -0
  214. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.d.ts +1 -0
  215. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.js +43 -0
  216. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.js.map +1 -0
  217. package/dist/cjs/tests/unit/flowlogger-eventstore.test.d.ts +1 -0
  218. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js +250 -0
  219. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js.map +1 -0
  220. package/dist/cjs/tests/unit/llm-middleware.test.d.ts +1 -0
  221. package/dist/cjs/tests/unit/llm-middleware.test.js +495 -0
  222. package/dist/cjs/tests/unit/llm-middleware.test.js.map +1 -0
  223. package/dist/cjs/tests/unit/model-deprecation.test.js +5 -8
  224. package/dist/cjs/tests/unit/model-deprecation.test.js.map +1 -1
  225. package/dist/cjs/tests/unit/openai-cua-client.test.d.ts +1 -0
  226. package/dist/cjs/tests/unit/openai-cua-client.test.js +71 -0
  227. package/dist/cjs/tests/unit/openai-cua-client.test.js.map +1 -0
  228. package/dist/cjs/tests/unit/page-extra-http-headers.test.d.ts +1 -0
  229. package/dist/cjs/tests/unit/page-extra-http-headers.test.js +92 -0
  230. package/dist/cjs/tests/unit/page-extra-http-headers.test.js.map +1 -0
  231. package/dist/cjs/tests/unit/prompt-observe-variables.test.d.ts +1 -0
  232. package/dist/cjs/tests/unit/prompt-observe-variables.test.js +19 -0
  233. package/dist/cjs/tests/unit/prompt-observe-variables.test.js.map +1 -0
  234. package/dist/cjs/tests/unit/public-api/llm-and-agents.test.js +13 -1
  235. package/dist/cjs/tests/unit/public-api/llm-and-agents.test.js.map +1 -1
  236. package/dist/cjs/tests/unit/public-api/public-types.test.js.map +1 -1
  237. package/dist/cjs/tests/unit/timeout-handlers.test.js +50 -0
  238. package/dist/cjs/tests/unit/timeout-handlers.test.js.map +1 -1
  239. package/dist/esm/lib/inference.d.ts +3 -1
  240. package/dist/esm/lib/inference.js +4 -7
  241. package/dist/esm/lib/inference.js.map +1 -1
  242. package/dist/esm/lib/prompt.d.ts +1 -1
  243. package/dist/esm/lib/prompt.js +24 -18
  244. package/dist/esm/lib/prompt.js.map +1 -1
  245. package/dist/esm/lib/utils.d.ts +1 -0
  246. package/dist/esm/lib/utils.js +3 -0
  247. package/dist/esm/lib/utils.js.map +1 -1
  248. package/dist/esm/lib/v3/agent/AgentClient.d.ts +8 -0
  249. package/dist/esm/lib/v3/agent/AgentClient.js +13 -0
  250. package/dist/esm/lib/v3/agent/AgentClient.js.map +1 -1
  251. package/dist/esm/lib/v3/agent/AgentProvider.js +0 -1
  252. package/dist/esm/lib/v3/agent/AgentProvider.js.map +1 -1
  253. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +6 -7
  254. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  255. package/dist/esm/lib/v3/agent/GoogleCUAClient.js +6 -7
  256. package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  257. package/dist/esm/lib/v3/agent/MicrosoftCUAClient.js +1 -0
  258. package/dist/esm/lib/v3/agent/MicrosoftCUAClient.js.map +1 -1
  259. package/dist/esm/lib/v3/agent/OpenAICUAClient.d.ts +10 -6
  260. package/dist/esm/lib/v3/agent/OpenAICUAClient.js +107 -18
  261. package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  262. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.d.ts +4 -2
  263. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js +11 -12
  264. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  265. package/dist/esm/lib/v3/agent/tools/act.d.ts +1 -1
  266. package/dist/esm/lib/v3/agent/tools/act.js +11 -4
  267. package/dist/esm/lib/v3/agent/tools/act.js.map +1 -1
  268. package/dist/esm/lib/v3/agent/tools/ariaTree.d.ts +8 -1
  269. package/dist/esm/lib/v3/agent/tools/ariaTree.js +49 -22
  270. package/dist/esm/lib/v3/agent/tools/ariaTree.js.map +1 -1
  271. package/dist/esm/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  272. package/dist/esm/lib/v3/agent/tools/braveSearch.js.map +1 -0
  273. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  274. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js +66 -0
  275. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  276. package/dist/esm/lib/v3/agent/tools/click.js +23 -31
  277. package/dist/esm/lib/v3/agent/tools/click.js.map +1 -1
  278. package/dist/esm/lib/v3/agent/tools/clickAndHold.js.map +1 -1
  279. package/dist/esm/lib/v3/agent/tools/dragAndDrop.js +22 -30
  280. package/dist/esm/lib/v3/agent/tools/dragAndDrop.js.map +1 -1
  281. package/dist/esm/lib/v3/agent/tools/extract.d.ts +2 -2
  282. package/dist/esm/lib/v3/agent/tools/extract.js +7 -3
  283. package/dist/esm/lib/v3/agent/tools/extract.js.map +1 -1
  284. package/dist/esm/lib/v3/agent/tools/fillFormVision.js +30 -30
  285. package/dist/esm/lib/v3/agent/tools/fillFormVision.js.map +1 -1
  286. package/dist/esm/lib/v3/agent/tools/fillform.d.ts +7 -2
  287. package/dist/esm/lib/v3/agent/tools/fillform.js +56 -45
  288. package/dist/esm/lib/v3/agent/tools/fillform.js.map +1 -1
  289. package/dist/esm/lib/v3/agent/tools/index.d.ts +19 -3
  290. package/dist/esm/lib/v3/agent/tools/index.js +63 -11
  291. package/dist/esm/lib/v3/agent/tools/index.js.map +1 -1
  292. package/dist/esm/lib/v3/agent/tools/keys.d.ts +1 -1
  293. package/dist/esm/lib/v3/agent/tools/keys.js.map +1 -1
  294. package/dist/esm/lib/v3/agent/tools/screenshot.d.ts +8 -0
  295. package/dist/esm/lib/v3/agent/tools/screenshot.js +32 -15
  296. package/dist/esm/lib/v3/agent/tools/screenshot.js.map +1 -1
  297. package/dist/esm/lib/v3/agent/tools/scroll.js +12 -0
  298. package/dist/esm/lib/v3/agent/tools/scroll.js.map +1 -1
  299. package/dist/esm/lib/v3/agent/tools/type.js +23 -31
  300. package/dist/esm/lib/v3/agent/tools/type.js.map +1 -1
  301. package/dist/esm/lib/v3/agent/tools/wait.js +6 -0
  302. package/dist/esm/lib/v3/agent/tools/wait.js.map +1 -1
  303. package/dist/esm/lib/v3/agent/utils/captchaSolver.d.ts +76 -0
  304. package/dist/esm/lib/v3/agent/utils/captchaSolver.js +171 -0
  305. package/dist/esm/lib/v3/agent/utils/captchaSolver.js.map +1 -0
  306. package/dist/esm/lib/v3/agent/utils/handleDoneToolCall.js +4 -0
  307. package/dist/esm/lib/v3/agent/utils/handleDoneToolCall.js.map +1 -1
  308. package/dist/esm/lib/v3/agent/utils/variables.d.ts +5 -0
  309. package/dist/esm/lib/v3/agent/utils/variables.js +8 -0
  310. package/dist/esm/lib/v3/agent/utils/variables.js.map +1 -1
  311. package/dist/esm/lib/v3/api.d.ts +2 -2
  312. package/dist/esm/lib/v3/api.js +1 -1
  313. package/dist/esm/lib/v3/api.js.map +1 -1
  314. package/dist/esm/lib/v3/cache/ActCache.d.ts +0 -1
  315. package/dist/esm/lib/v3/cache/ActCache.js +2 -18
  316. package/dist/esm/lib/v3/cache/ActCache.js.map +1 -1
  317. package/dist/esm/lib/v3/flowlogger/EventEmitter.d.ts +7 -0
  318. package/dist/esm/lib/v3/flowlogger/EventEmitter.js +26 -0
  319. package/dist/esm/lib/v3/flowlogger/EventEmitter.js.map +1 -0
  320. package/dist/esm/lib/v3/flowlogger/EventSink.d.ts +44 -0
  321. package/dist/esm/lib/v3/flowlogger/EventSink.js +206 -0
  322. package/dist/esm/lib/v3/flowlogger/EventSink.js.map +1 -0
  323. package/dist/esm/lib/v3/flowlogger/EventStore.d.ts +26 -0
  324. package/dist/esm/lib/v3/flowlogger/EventStore.js +127 -0
  325. package/dist/esm/lib/v3/flowlogger/EventStore.js.map +1 -0
  326. package/dist/esm/lib/v3/flowlogger/FlowLogger.d.ts +99 -0
  327. package/dist/esm/lib/v3/flowlogger/FlowLogger.js +583 -0
  328. package/dist/esm/lib/v3/flowlogger/FlowLogger.js.map +1 -0
  329. package/dist/esm/lib/v3/flowlogger/prettify.d.ts +6 -0
  330. package/dist/esm/lib/v3/flowlogger/prettify.js +389 -0
  331. package/dist/esm/lib/v3/flowlogger/prettify.js.map +1 -0
  332. package/dist/esm/lib/v3/handlers/actHandler.js +1 -2
  333. package/dist/esm/lib/v3/handlers/actHandler.js.map +1 -1
  334. package/dist/esm/lib/v3/handlers/extractHandler.js +2 -2
  335. package/dist/esm/lib/v3/handlers/extractHandler.js.map +1 -1
  336. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js +43 -57
  337. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  338. package/dist/esm/lib/v3/handlers/observeHandler.js +3 -3
  339. package/dist/esm/lib/v3/handlers/observeHandler.js.map +1 -1
  340. package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +2 -5
  341. package/dist/esm/lib/v3/handlers/v3AgentHandler.js +131 -92
  342. package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  343. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.d.ts +5 -0
  344. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +134 -14
  345. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  346. package/dist/esm/lib/v3/index.d.ts +2 -1
  347. package/dist/esm/lib/v3/launch/browserbase.d.ts +1 -1
  348. package/dist/esm/lib/v3/launch/browserbase.js +4 -9
  349. package/dist/esm/lib/v3/launch/browserbase.js.map +1 -1
  350. package/dist/esm/lib/v3/llm/LLMProvider.d.ts +5 -2
  351. package/dist/esm/lib/v3/llm/LLMProvider.js +15 -12
  352. package/dist/esm/lib/v3/llm/LLMProvider.js.map +1 -1
  353. package/dist/esm/lib/v3/llm/aisdk.d.ts +3 -1
  354. package/dist/esm/lib/v3/llm/aisdk.js +24 -28
  355. package/dist/esm/lib/v3/llm/aisdk.js.map +1 -1
  356. package/dist/esm/lib/v3/runtimePaths.js +2 -1
  357. package/dist/esm/lib/v3/runtimePaths.js.map +1 -1
  358. package/dist/esm/lib/v3/shutdown/supervisor.js +2 -2
  359. package/dist/esm/lib/v3/shutdown/supervisor.js.map +1 -1
  360. package/dist/esm/lib/v3/timeoutConfig.d.ts +1 -1
  361. package/dist/esm/lib/v3/timeoutConfig.js +5 -0
  362. package/dist/esm/lib/v3/timeoutConfig.js.map +1 -1
  363. package/dist/esm/lib/v3/types/private/cache.d.ts +0 -1
  364. package/dist/esm/lib/v3/types/private/cache.js.map +1 -1
  365. package/dist/esm/lib/v3/types/private/handlers.d.ts +1 -0
  366. package/dist/esm/lib/v3/types/private/handlers.js.map +1 -1
  367. package/dist/esm/lib/v3/types/private/shutdown.d.ts +1 -1
  368. package/dist/esm/lib/v3/types/private/shutdown.js.map +1 -1
  369. package/dist/esm/lib/v3/types/public/agent.d.ts +28 -3
  370. package/dist/esm/lib/v3/types/public/agent.js +0 -1
  371. package/dist/esm/lib/v3/types/public/agent.js.map +1 -1
  372. package/dist/esm/lib/v3/types/public/api.d.ts +27 -7
  373. package/dist/esm/lib/v3/types/public/api.js +37 -12
  374. package/dist/esm/lib/v3/types/public/api.js.map +1 -1
  375. package/dist/esm/lib/v3/types/public/methods.d.ts +1 -0
  376. package/dist/esm/lib/v3/types/public/methods.js.map +1 -1
  377. package/dist/esm/lib/v3/types/public/model.d.ts +14 -3
  378. package/dist/esm/lib/v3/types/public/model.js.map +1 -1
  379. package/dist/esm/lib/v3/types/public/options.d.ts +8 -0
  380. package/dist/esm/lib/v3/types/public/options.js.map +1 -1
  381. package/dist/esm/lib/v3/types/public/variables.d.ts +7 -0
  382. package/dist/esm/lib/v3/types/public/variables.js +19 -0
  383. package/dist/esm/lib/v3/types/public/variables.js.map +1 -0
  384. package/dist/esm/lib/v3/understudy/cdp.d.ts +8 -13
  385. package/dist/esm/lib/v3/understudy/cdp.js +181 -21
  386. package/dist/esm/lib/v3/understudy/cdp.js.map +1 -1
  387. package/dist/esm/lib/v3/understudy/context.d.ts +1 -0
  388. package/dist/esm/lib/v3/understudy/context.js +148 -62
  389. package/dist/esm/lib/v3/understudy/context.js.map +1 -1
  390. package/dist/esm/lib/v3/understudy/frame.js +23 -6
  391. package/dist/esm/lib/v3/understudy/frame.js.map +1 -1
  392. package/dist/esm/lib/v3/understudy/page.d.ts +13 -0
  393. package/dist/esm/lib/v3/understudy/page.js +87 -24
  394. package/dist/esm/lib/v3/understudy/page.js.map +1 -1
  395. package/dist/esm/lib/v3/understudy/screenshotUtils.d.ts +0 -1
  396. package/dist/esm/lib/v3/understudy/screenshotUtils.js +0 -17
  397. package/dist/esm/lib/v3/understudy/screenshotUtils.js.map +1 -1
  398. package/dist/esm/lib/v3/v3.d.ts +26 -3
  399. package/dist/esm/lib/v3/v3.js +250 -181
  400. package/dist/esm/lib/v3/v3.js.map +1 -1
  401. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.d.ts +1 -0
  402. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.js +54 -0
  403. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.js.map +1 -0
  404. package/dist/esm/tests/integration/agent-hybrid-mode.spec.js +6 -6
  405. package/dist/esm/tests/integration/agent-hybrid-mode.spec.js.map +1 -1
  406. package/dist/esm/tests/integration/cdp-session-detached.spec.js +1 -1
  407. package/dist/esm/tests/integration/cdp-session-detached.spec.js.map +1 -1
  408. package/dist/esm/tests/integration/chrome-newtab-page-tracking.spec.d.ts +1 -0
  409. package/dist/esm/tests/integration/chrome-newtab-page-tracking.spec.js +54 -0
  410. package/dist/esm/tests/integration/chrome-newtab-page-tracking.spec.js.map +1 -0
  411. package/dist/esm/tests/integration/context-addInitScript.spec.js +104 -11
  412. package/dist/esm/tests/integration/context-addInitScript.spec.js.map +1 -1
  413. package/dist/esm/tests/integration/flowLogger.spec.d.ts +1 -0
  414. package/dist/esm/tests/integration/flowLogger.spec.js +712 -0
  415. package/dist/esm/tests/integration/flowLogger.spec.js.map +1 -0
  416. package/dist/esm/tests/integration/iframe-ctx-addInitScript-race.spec.d.ts +1 -0
  417. package/dist/esm/tests/integration/iframe-ctx-addInitScript-race.spec.js +217 -0
  418. package/dist/esm/tests/integration/iframe-ctx-addInitScript-race.spec.js.map +1 -0
  419. package/dist/esm/tests/integration/page-extra-http-headers.spec.d.ts +1 -0
  420. package/dist/esm/tests/integration/page-extra-http-headers.spec.js +83 -0
  421. package/dist/esm/tests/integration/page-extra-http-headers.spec.js.map +1 -0
  422. package/dist/esm/tests/integration/page-screenshot.spec.js +1 -1
  423. package/dist/esm/tests/integration/page-screenshot.spec.js.map +1 -1
  424. package/dist/esm/tests/integration/testUtils.d.ts +33 -0
  425. package/dist/esm/tests/integration/testUtils.js +138 -0
  426. package/dist/esm/tests/integration/testUtils.js.map +1 -1
  427. package/dist/esm/tests/integration/timeouts.spec.js +278 -0
  428. package/dist/esm/tests/integration/timeouts.spec.js.map +1 -1
  429. package/dist/esm/tests/unit/agent-captcha-hooks.test.d.ts +1 -0
  430. package/dist/esm/tests/unit/agent-captcha-hooks.test.js +283 -0
  431. package/dist/esm/tests/unit/agent-captcha-hooks.test.js.map +1 -0
  432. package/dist/esm/tests/unit/agent-execution-model.test.js +25 -3
  433. package/dist/esm/tests/unit/agent-execution-model.test.js.map +1 -1
  434. package/dist/esm/tests/unit/agent-system-prompt-variables.test.d.ts +1 -0
  435. package/dist/esm/tests/unit/agent-system-prompt-variables.test.js +21 -0
  436. package/dist/esm/tests/unit/agent-system-prompt-variables.test.js.map +1 -0
  437. package/dist/esm/tests/unit/aisdk-reasoning-effort.test.d.ts +1 -0
  438. package/dist/esm/tests/unit/aisdk-reasoning-effort.test.js +118 -0
  439. package/dist/esm/tests/unit/aisdk-reasoning-effort.test.js.map +1 -0
  440. package/dist/esm/tests/unit/api-client-observe-variables.test.d.ts +1 -0
  441. package/dist/esm/tests/unit/api-client-observe-variables.test.js +84 -0
  442. package/dist/esm/tests/unit/api-client-observe-variables.test.js.map +1 -0
  443. package/dist/esm/tests/unit/api-variables-schema.test.d.ts +1 -0
  444. package/dist/esm/tests/unit/api-variables-schema.test.js +35 -0
  445. package/dist/esm/tests/unit/api-variables-schema.test.js.map +1 -0
  446. package/dist/esm/tests/unit/browserbase-session-accessors.test.js +20 -0
  447. package/dist/esm/tests/unit/browserbase-session-accessors.test.js.map +1 -1
  448. package/dist/esm/tests/unit/captcha-solver.test.d.ts +1 -0
  449. package/dist/esm/tests/unit/captcha-solver.test.js +152 -0
  450. package/dist/esm/tests/unit/captcha-solver.test.js.map +1 -0
  451. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.d.ts +1 -0
  452. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.js +93 -0
  453. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.js.map +1 -0
  454. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.d.ts +1 -0
  455. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.js +41 -0
  456. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.js.map +1 -0
  457. package/dist/esm/tests/unit/flowlogger-eventstore.test.d.ts +1 -0
  458. package/dist/esm/tests/unit/flowlogger-eventstore.test.js +248 -0
  459. package/dist/esm/tests/unit/flowlogger-eventstore.test.js.map +1 -0
  460. package/dist/esm/tests/unit/llm-middleware.test.d.ts +1 -0
  461. package/dist/esm/tests/unit/llm-middleware.test.js +460 -0
  462. package/dist/esm/tests/unit/llm-middleware.test.js.map +1 -0
  463. package/dist/esm/tests/unit/model-deprecation.test.js +5 -8
  464. package/dist/esm/tests/unit/model-deprecation.test.js.map +1 -1
  465. package/dist/esm/tests/unit/openai-cua-client.test.d.ts +1 -0
  466. package/dist/esm/tests/unit/openai-cua-client.test.js +69 -0
  467. package/dist/esm/tests/unit/openai-cua-client.test.js.map +1 -0
  468. package/dist/esm/tests/unit/page-extra-http-headers.test.d.ts +1 -0
  469. package/dist/esm/tests/unit/page-extra-http-headers.test.js +90 -0
  470. package/dist/esm/tests/unit/page-extra-http-headers.test.js.map +1 -0
  471. package/dist/esm/tests/unit/prompt-observe-variables.test.d.ts +1 -0
  472. package/dist/esm/tests/unit/prompt-observe-variables.test.js +17 -0
  473. package/dist/esm/tests/unit/prompt-observe-variables.test.js.map +1 -0
  474. package/dist/esm/tests/unit/public-api/llm-and-agents.test.js +13 -1
  475. package/dist/esm/tests/unit/public-api/llm-and-agents.test.js.map +1 -1
  476. package/dist/esm/tests/unit/public-api/public-types.test.js.map +1 -1
  477. package/dist/esm/tests/unit/timeout-handlers.test.js +50 -0
  478. package/dist/esm/tests/unit/timeout-handlers.test.js.map +1 -1
  479. package/package.json +6 -4
  480. package/dist/cjs/lib/v3/agent/tools/search.js.map +0 -1
  481. package/dist/cjs/lib/v3/flowLogger.d.ts +0 -139
  482. package/dist/cjs/lib/v3/flowLogger.js +0 -881
  483. package/dist/cjs/lib/v3/flowLogger.js.map +0 -1
  484. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js +0 -209
  485. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  486. package/dist/esm/lib/v3/agent/tools/search.js.map +0 -1
  487. package/dist/esm/lib/v3/flowLogger.d.ts +0 -139
  488. package/dist/esm/lib/v3/flowLogger.js +0 -868
  489. package/dist/esm/lib/v3/flowLogger.js.map +0 -1
  490. package/dist/esm/tests/unit/rerender-missing-shadows.test.js +0 -207
  491. package/dist/esm/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  492. /package/dist/cjs/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
  493. /package/dist/cjs/tests/{unit/rerender-missing-shadows.test.d.ts → integration/agent-captcha-autosolve.spec.d.ts} +0 -0
  494. /package/dist/{esm/tests/unit/rerender-missing-shadows.test.d.ts → cjs/tests/integration/chrome-newtab-page-tracking.spec.d.ts} +0 -0
  495. /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
  }
@@ -384,6 +423,13 @@ let V3 = (() => {
384
423
  mergedOptions.apiKey = apiKey;
385
424
  }
386
425
  }
426
+ if (perCallMiddleware) {
427
+ return this.llmProvider.getClient(modelName, mergedOptions, {
428
+ experimental: this.experimental,
429
+ disableAPI: this.disableAPI,
430
+ middleware: perCallMiddleware,
431
+ });
432
+ }
387
433
  const cacheKey = JSON.stringify({
388
434
  modelName,
389
435
  clientOptions: mergedOptions,
@@ -392,7 +438,10 @@ let V3 = (() => {
392
438
  if (cached) {
393
439
  return cached;
394
440
  }
395
- const client = this.llmProvider.getClient(modelName, mergedOptions, { experimental: this.experimental, disableAPI: this.disableAPI });
441
+ const client = this.llmProvider.getClient(modelName, mergedOptions, {
442
+ experimental: this.experimental,
443
+ disableAPI: this.disableAPI,
444
+ });
396
445
  this.overrideLlmClients.set(cacheKey, client);
397
446
  return client;
398
447
  }
@@ -552,6 +601,13 @@ let V3 = (() => {
552
601
  }
553
602
  }
554
603
  const lbo = this.opts.localBrowserLaunchOptions ?? {};
604
+ if (lbo.cdpHeaders && !lbo.cdpUrl) {
605
+ this.logger({
606
+ category: "init",
607
+ message: "`cdpHeaders` was provided but `cdpUrl` is not set — cdpHeaders will be ignored. Set `cdpUrl` to connect to an existing browser via CDP.",
608
+ level: 2,
609
+ });
610
+ }
555
611
  // If a CDP URL is provided, attach instead of launching.
556
612
  if (lbo.cdpUrl) {
557
613
  this.logger({
@@ -561,10 +617,9 @@ let V3 = (() => {
561
617
  });
562
618
  this.ctx = await V3Context.create(lbo.cdpUrl, {
563
619
  env: "LOCAL",
620
+ cdpHeaders: lbo.cdpHeaders,
564
621
  });
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);
622
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
568
623
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
569
624
  this.state = {
570
625
  kind: "LOCAL",
@@ -660,9 +715,7 @@ let V3 = (() => {
660
715
  env: "LOCAL",
661
716
  localBrowserLaunchOptions: lbo,
662
717
  });
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);
718
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
666
719
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
667
720
  this.state = {
668
721
  kind: "LOCAL",
@@ -689,9 +742,6 @@ let V3 = (() => {
689
742
  }
690
743
  if (this.opts.env === "BROWSERBASE") {
691
744
  const { apiKey, projectId } = this.requireBrowserbaseCreds();
692
- if (!apiKey || !projectId) {
693
- throw new MissingEnvironmentVariableError("BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID", "Browserbase environment");
694
- }
695
745
  this.logger({
696
746
  category: "init",
697
747
  message: "Starting browserbase session",
@@ -700,9 +750,13 @@ let V3 = (() => {
700
750
  const baseSessionParams = this.opts.browserbaseSessionCreateParams ?? {};
701
751
  const resolvedKeepAlive = this.keepAlive;
702
752
  const keepAlive = this.keepAlive === true;
703
- const effectiveSessionParams = resolvedKeepAlive !== undefined
704
- ? { ...baseSessionParams, keepAlive: resolvedKeepAlive }
705
- : baseSessionParams;
753
+ let effectiveSessionParams = baseSessionParams;
754
+ if (resolvedKeepAlive !== undefined) {
755
+ effectiveSessionParams = {
756
+ ...baseSessionParams,
757
+ keepAlive: resolvedKeepAlive,
758
+ };
759
+ }
706
760
  if (!this.disableAPI && !this.experimental) {
707
761
  this.apiClient = new StagehandAPIClient({
708
762
  apiKey,
@@ -710,18 +764,20 @@ let V3 = (() => {
710
764
  logger: this.logger,
711
765
  serverCache: this.opts.serverCache,
712
766
  });
767
+ const { projectId: overrideProjectId, browserSettings, userMetadata, ...restSessionParams } = effectiveSessionParams;
768
+ const resolvedProjectId = overrideProjectId ?? projectId;
713
769
  const createSessionPayload = {
714
- projectId: effectiveSessionParams.projectId ?? projectId,
715
- ...effectiveSessionParams,
770
+ ...(resolvedProjectId ? { projectId: resolvedProjectId } : {}),
771
+ ...restSessionParams,
716
772
  browserSettings: {
717
- ...(effectiveSessionParams.browserSettings ?? {}),
718
- viewport: effectiveSessionParams.browserSettings?.viewport ?? {
773
+ ...(browserSettings ?? {}),
774
+ viewport: browserSettings?.viewport ?? {
719
775
  width: 1288,
720
776
  height: 711,
721
777
  },
722
778
  },
723
779
  userMetadata: {
724
- ...(effectiveSessionParams.userMetadata ?? {}),
780
+ ...(userMetadata ?? {}),
725
781
  stagehand: "true",
726
782
  },
727
783
  };
@@ -745,9 +801,7 @@ let V3 = (() => {
745
801
  env: "BROWSERBASE",
746
802
  apiClient: this.apiClient,
747
803
  });
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);
804
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
751
805
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
752
806
  this.state = { kind: "BROWSERBASE", sessionId, ws, bb };
753
807
  this.browserbaseSessionId = sessionId;
@@ -869,10 +923,7 @@ let V3 = (() => {
869
923
  });
870
924
  }
871
925
  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));
926
+ const ensureTimeRemaining = createTimeoutGuard(options?.timeout, (ms) => new ActTimeoutError(ms));
876
927
  actResult = await this.actHandler.takeDeterministicAction({ ...input, selector }, v3Page, this.domSettleTimeoutMs, this.resolveLlmClient(options?.model), ensureTimeRemaining, options?.variables);
877
928
  }
878
929
  // history: record ObserveResult-based act call
@@ -1026,6 +1077,7 @@ let V3 = (() => {
1026
1077
  const handlerParams = {
1027
1078
  instruction,
1028
1079
  model: options?.model,
1080
+ variables: options?.variables,
1029
1081
  timeout: options?.timeout,
1030
1082
  selector: options?.selector,
1031
1083
  page: page,
@@ -1045,6 +1097,7 @@ let V3 = (() => {
1045
1097
  // history: record observe call (omit page object)
1046
1098
  this.addToHistory("observe", {
1047
1099
  instruction,
1100
+ variables: options?.variables,
1048
1101
  timeout: options?.timeout,
1049
1102
  }, results);
1050
1103
  return results;
@@ -1093,7 +1146,7 @@ let V3 = (() => {
1093
1146
  try {
1094
1147
  // Close session file logger
1095
1148
  try {
1096
- await SessionFileLogger.close();
1149
+ await FlowLogger.close(this.flowLoggerContext);
1097
1150
  }
1098
1151
  catch {
1099
1152
  // ignore
@@ -1129,6 +1182,12 @@ let V3 = (() => {
1129
1182
  catch {
1130
1183
  // ignore
1131
1184
  }
1185
+ try {
1186
+ await this.eventStore.destroy();
1187
+ }
1188
+ catch {
1189
+ // ignore
1190
+ }
1132
1191
  try {
1133
1192
  this.bus.removeAllListeners();
1134
1193
  }
@@ -1142,6 +1201,13 @@ let V3 = (() => {
1142
1201
  V3._instances.delete(this);
1143
1202
  }
1144
1203
  }
1204
+ /**
1205
+ * Resolves the Browserbase API key from options or environment variables.
1206
+ * Returns undefined if no key is found (does not throw).
1207
+ */
1208
+ get browserbaseApiKey() {
1209
+ return this.opts.apiKey || process.env.BROWSERBASE_API_KEY;
1210
+ }
1145
1211
  /** Guard: ensure Browserbase credentials exist in options. */
1146
1212
  requireBrowserbaseCreds() {
1147
1213
  let { apiKey, projectId } = this.opts;
@@ -1151,17 +1217,13 @@ let V3 = (() => {
1151
1217
  if (!projectId)
1152
1218
  projectId =
1153
1219
  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");
1220
+ if (!apiKey) {
1221
+ throw new MissingEnvironmentVariableError("BROWSERBASE_API_KEY", "Browserbase");
1161
1222
  }
1162
1223
  // Cache resolved values back into opts for consistency
1163
1224
  this.opts.apiKey = apiKey;
1164
- this.opts.projectId = projectId;
1225
+ if (projectId)
1226
+ this.opts.projectId = projectId;
1165
1227
  // Informational log
1166
1228
  this.logger({
1167
1229
  category: "init",
@@ -1303,10 +1365,16 @@ let V3 = (() => {
1303
1365
  ? this.resolveLlmClient(options.model)
1304
1366
  : this.llmClient;
1305
1367
  const resolvedExecutionModel = options?.executionModel ?? options?.model;
1306
- const handler = new V3AgentHandler(this, this.logger, agentLlmClient, resolvedExecutionModel, options?.systemPrompt, tools, options?.mode);
1368
+ const handler = new V3AgentHandler(this, this.logger, agentLlmClient, resolvedExecutionModel, options?.systemPrompt, tools, options?.mode, this.isCaptchaAutoSolveEnabled);
1307
1369
  const resolvedOptions = typeof instructionOrOptions === "string"
1308
- ? { instruction: instructionOrOptions }
1309
- : instructionOrOptions;
1370
+ ? {
1371
+ instruction: instructionOrOptions,
1372
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1373
+ }
1374
+ : {
1375
+ ...instructionOrOptions,
1376
+ toolTimeout: instructionOrOptions.toolTimeout ?? DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1377
+ };
1310
1378
  const callbacksWithSafety = resolvedOptions.callbacks;
1311
1379
  if (callbacksWithSafety?.onSafetyConfirmation) {
1312
1380
  throw new StagehandInvalidArgumentError('onSafetyConfirmation callback is only supported when using mode: "cua" agents.');
@@ -1386,145 +1454,59 @@ let V3 = (() => {
1386
1454
  throw new CuaModelRequiredError(AVAILABLE_CUA_MODELS);
1387
1455
  }
1388
1456
  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 () => {
1457
+ const execute = async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1487
1458
  validateExperimentalFeatures({
1488
1459
  isExperimental: this.experimental,
1489
1460
  agentConfig: options,
1490
1461
  executeOptions: typeof instructionOrOptions === "object"
1491
1462
  ? instructionOrOptions
1492
1463
  : null,
1493
- isStreaming,
1494
- });
1495
- SessionFileLogger.logAgentTaskStarted({
1496
- invocation: "Agent.execute",
1497
- args: [instructionOrOptions],
1498
1464
  });
1499
- // Streaming mode
1500
- if (isStreaming) {
1501
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1465
+ const tools = options?.integrations
1466
+ ? await resolveTools(options.integrations, options.tools)
1467
+ : (options?.tools ?? {});
1468
+ const handler = new V3CuaAgentHandler(this, this.logger, {
1469
+ modelName,
1470
+ clientOptions,
1471
+ userProvidedInstructions: (options.systemPrompt ??
1472
+ `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`) +
1473
+ (this.isCaptchaAutoSolveEnabled
1474
+ ? CAPTCHA_CUA_SYSTEM_PROMPT_NOTE
1475
+ : ""),
1476
+ }, tools);
1477
+ const resolvedOptions = typeof instructionOrOptions === "string"
1478
+ ? {
1479
+ instruction: instructionOrOptions,
1480
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1481
+ }
1482
+ : {
1483
+ ...instructionOrOptions,
1484
+ toolTimeout: instructionOrOptions.toolTimeout ??
1485
+ DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1486
+ };
1487
+ if (resolvedOptions.page) {
1488
+ const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1489
+ this.ctx.setActivePage(normalizedPage);
1490
+ }
1491
+ const instruction = resolvedOptions.instruction.trim();
1492
+ const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1493
+ const cacheVariables = flattenVariables(resolvedOptions.variables);
1494
+ let cacheContext = null;
1495
+ if (this.agentCache.shouldAttemptCache(instruction)) {
1496
+ const startPage = await this.ctx.awaitActivePage();
1497
+ cacheContext = await this.agentCache.prepareContext({
1498
+ instruction,
1499
+ options: sanitizedOptions,
1500
+ configSignature: agentConfigSignature,
1501
+ page: startPage,
1502
+ variables: cacheVariables,
1503
+ });
1502
1504
  if (cacheContext) {
1503
- const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1505
+ const replayed = await this.agentCache.tryReplay(cacheContext);
1504
1506
  if (replayed) {
1505
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1506
1507
  return replayed;
1507
1508
  }
1508
1509
  }
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
1510
  }
1529
1511
  let agentSteps = [];
1530
1512
  const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
@@ -1535,14 +1517,14 @@ let V3 = (() => {
1535
1517
  try {
1536
1518
  if (this.apiClient && !this.experimental) {
1537
1519
  const page = await this.ctx.awaitActivePage();
1538
- result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1520
+ result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1539
1521
  if (cacheContext) {
1540
1522
  const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1541
1523
  await this.agentCache.storeTransferredEntry(transferredEntry);
1542
1524
  }
1543
1525
  }
1544
1526
  else {
1545
- result = await handler.execute(resolvedOptions);
1527
+ result = await handler.execute(instructionOrOptions);
1546
1528
  }
1547
1529
  if (shouldRecordLocally) {
1548
1530
  agentSteps = this.endAgentReplayRecording();
@@ -1564,9 +1546,96 @@ let V3 = (() => {
1564
1546
  if (shouldRecordLocally) {
1565
1547
  this.discardAgentReplayRecording();
1566
1548
  }
1567
- SessionFileLogger.logAgentTaskCompleted();
1568
1549
  }
1569
- }),
1550
+ });
1551
+ return {
1552
+ execute: FlowLogger.wrapWithLogging({
1553
+ eventType: "AgentExecute",
1554
+ context: this.flowLoggerContext,
1555
+ })(execute),
1556
+ };
1557
+ }
1558
+ // Default: AISDK tools-based agent
1559
+ const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1560
+ const isStreaming = options?.stream ?? false;
1561
+ const execute = async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1562
+ validateExperimentalFeatures({
1563
+ isExperimental: this.experimental,
1564
+ agentConfig: options,
1565
+ executeOptions: typeof instructionOrOptions === "object"
1566
+ ? instructionOrOptions
1567
+ : null,
1568
+ isStreaming,
1569
+ });
1570
+ // Streaming mode
1571
+ if (isStreaming) {
1572
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1573
+ if (cacheContext) {
1574
+ const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1575
+ if (replayed) {
1576
+ return replayed;
1577
+ }
1578
+ }
1579
+ const streamResult = await handler.stream(resolvedOptions);
1580
+ if (cacheContext) {
1581
+ const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1582
+ return wrappedStream;
1583
+ }
1584
+ return streamResult;
1585
+ }
1586
+ // Non-streaming mode (default)
1587
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1588
+ if (cacheContext) {
1589
+ const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1590
+ if (replayed) {
1591
+ return replayed;
1592
+ }
1593
+ }
1594
+ let agentSteps = [];
1595
+ const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1596
+ if (shouldRecordLocally) {
1597
+ this.beginAgentReplayRecording();
1598
+ }
1599
+ let result;
1600
+ try {
1601
+ if (this.apiClient && !this.experimental) {
1602
+ const page = await this.ctx.awaitActivePage();
1603
+ result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1604
+ if (cacheContext) {
1605
+ const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1606
+ await this.agentCache.storeTransferredEntry(transferredEntry);
1607
+ }
1608
+ }
1609
+ else {
1610
+ result = await handler.execute(resolvedOptions);
1611
+ }
1612
+ if (shouldRecordLocally) {
1613
+ agentSteps = this.endAgentReplayRecording();
1614
+ }
1615
+ if (shouldRecordLocally &&
1616
+ cacheContext &&
1617
+ result.success &&
1618
+ agentSteps.length > 0) {
1619
+ await this.agentCache.store(cacheContext, agentSteps, result);
1620
+ }
1621
+ return result;
1622
+ }
1623
+ catch (err) {
1624
+ if (shouldRecordLocally)
1625
+ this.discardAgentReplayRecording();
1626
+ throw err;
1627
+ }
1628
+ finally {
1629
+ if (shouldRecordLocally) {
1630
+ this.discardAgentReplayRecording();
1631
+ }
1632
+ }
1633
+ });
1634
+ return {
1635
+ execute: FlowLogger.wrapWithLogging({
1636
+ eventType: "AgentExecute",
1637
+ context: this.flowLoggerContext,
1638
+ })(execute),
1570
1639
  };
1571
1640
  }
1572
1641
  };