@browserbasehq/orca 3.2.0-preview.3 → 3.2.0-preview.4

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 (171) hide show
  1. package/dist/cjs/lib/utils.d.ts +1 -0
  2. package/dist/cjs/lib/utils.js +4 -0
  3. package/dist/cjs/lib/utils.js.map +1 -1
  4. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +5 -7
  5. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  6. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +5 -7
  7. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  8. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +5 -7
  9. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  10. package/dist/cjs/lib/v3/agent/tools/act.js +1 -10
  11. package/dist/cjs/lib/v3/agent/tools/act.js.map +1 -1
  12. package/dist/cjs/lib/v3/agent/tools/ariaTree.js +1 -12
  13. package/dist/cjs/lib/v3/agent/tools/ariaTree.js.map +1 -1
  14. package/dist/cjs/lib/v3/agent/tools/braveSearch.js.map +1 -1
  15. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js.map +1 -1
  16. package/dist/cjs/lib/v3/agent/tools/click.js.map +1 -1
  17. package/dist/cjs/lib/v3/agent/tools/clickAndHold.js.map +1 -1
  18. package/dist/cjs/lib/v3/agent/tools/dragAndDrop.js.map +1 -1
  19. package/dist/cjs/lib/v3/agent/tools/extract.js +1 -10
  20. package/dist/cjs/lib/v3/agent/tools/extract.js.map +1 -1
  21. package/dist/cjs/lib/v3/agent/tools/fillFormVision.js.map +1 -1
  22. package/dist/cjs/lib/v3/agent/tools/fillform.js +1 -10
  23. package/dist/cjs/lib/v3/agent/tools/fillform.js.map +1 -1
  24. package/dist/cjs/lib/v3/agent/tools/index.d.ts +2 -2
  25. package/dist/cjs/lib/v3/agent/tools/index.js +53 -5
  26. package/dist/cjs/lib/v3/agent/tools/index.js.map +1 -1
  27. package/dist/cjs/lib/v3/agent/tools/keys.d.ts +1 -1
  28. package/dist/cjs/lib/v3/agent/tools/keys.js.map +1 -1
  29. package/dist/cjs/lib/v3/agent/tools/type.js.map +1 -1
  30. package/dist/cjs/lib/v3/api.js +9 -2
  31. package/dist/cjs/lib/v3/api.js.map +1 -1
  32. package/dist/cjs/lib/v3/flowlogger/EventEmitter.d.ts +7 -0
  33. package/dist/cjs/lib/v3/flowlogger/EventEmitter.js +30 -0
  34. package/dist/cjs/lib/v3/flowlogger/EventEmitter.js.map +1 -0
  35. package/dist/cjs/lib/v3/flowlogger/EventSink.d.ts +44 -0
  36. package/dist/cjs/lib/v3/flowlogger/EventSink.js +217 -0
  37. package/dist/cjs/lib/v3/flowlogger/EventSink.js.map +1 -0
  38. package/dist/cjs/lib/v3/flowlogger/EventStore.d.ts +26 -0
  39. package/dist/cjs/lib/v3/flowlogger/EventStore.js +135 -0
  40. package/dist/cjs/lib/v3/flowlogger/EventStore.js.map +1 -0
  41. package/dist/cjs/lib/v3/flowlogger/FlowLogger.d.ts +99 -0
  42. package/dist/cjs/lib/v3/flowlogger/FlowLogger.js +591 -0
  43. package/dist/cjs/lib/v3/flowlogger/FlowLogger.js.map +1 -0
  44. package/dist/cjs/lib/v3/flowlogger/prettify.d.ts +6 -0
  45. package/dist/cjs/lib/v3/flowlogger/prettify.js +395 -0
  46. package/dist/cjs/lib/v3/flowlogger/prettify.js.map +1 -0
  47. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js +43 -57
  48. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  49. package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +0 -4
  50. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +2 -32
  51. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  52. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +9 -13
  53. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  54. package/dist/cjs/lib/v3/llm/aisdk.js +11 -17
  55. package/dist/cjs/lib/v3/llm/aisdk.js.map +1 -1
  56. package/dist/cjs/lib/v3/types/public/options.d.ts +7 -0
  57. package/dist/cjs/lib/v3/types/public/options.js.map +1 -1
  58. package/dist/cjs/lib/v3/understudy/cdp.d.ts +3 -12
  59. package/dist/cjs/lib/v3/understudy/cdp.js +134 -21
  60. package/dist/cjs/lib/v3/understudy/cdp.js.map +1 -1
  61. package/dist/cjs/lib/v3/understudy/page.js +28 -18
  62. package/dist/cjs/lib/v3/understudy/page.js.map +1 -1
  63. package/dist/cjs/lib/v3/v3.d.ts +7 -2
  64. package/dist/cjs/lib/v3/v3.js +178 -159
  65. package/dist/cjs/lib/v3/v3.js.map +1 -1
  66. package/dist/cjs/tests/integration/flowLogger.spec.d.ts +1 -0
  67. package/dist/cjs/tests/integration/flowLogger.spec.js +714 -0
  68. package/dist/cjs/tests/integration/flowLogger.spec.js.map +1 -0
  69. package/dist/cjs/tests/integration/testUtils.d.ts +33 -0
  70. package/dist/cjs/tests/integration/testUtils.js +144 -0
  71. package/dist/cjs/tests/integration/testUtils.js.map +1 -1
  72. package/dist/cjs/tests/integration/timeouts.spec.js +112 -2
  73. package/dist/cjs/tests/integration/timeouts.spec.js.map +1 -1
  74. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.d.ts +1 -0
  75. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.js +95 -0
  76. package/dist/cjs/tests/unit/flowlogger-capturing-cdp.test.js.map +1 -0
  77. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.d.ts +1 -0
  78. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.js +43 -0
  79. package/dist/cjs/tests/unit/flowlogger-capturing-llm.test.js.map +1 -0
  80. package/dist/cjs/tests/unit/flowlogger-eventstore.test.d.ts +1 -0
  81. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js +250 -0
  82. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js.map +1 -0
  83. package/dist/esm/lib/utils.d.ts +1 -0
  84. package/dist/esm/lib/utils.js +3 -0
  85. package/dist/esm/lib/utils.js.map +1 -1
  86. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +5 -7
  87. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  88. package/dist/esm/lib/v3/agent/GoogleCUAClient.js +5 -7
  89. package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  90. package/dist/esm/lib/v3/agent/OpenAICUAClient.js +5 -7
  91. package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  92. package/dist/esm/lib/v3/agent/tools/act.js +1 -10
  93. package/dist/esm/lib/v3/agent/tools/act.js.map +1 -1
  94. package/dist/esm/lib/v3/agent/tools/ariaTree.js +1 -12
  95. package/dist/esm/lib/v3/agent/tools/ariaTree.js.map +1 -1
  96. package/dist/esm/lib/v3/agent/tools/braveSearch.js.map +1 -1
  97. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js.map +1 -1
  98. package/dist/esm/lib/v3/agent/tools/click.js.map +1 -1
  99. package/dist/esm/lib/v3/agent/tools/clickAndHold.js.map +1 -1
  100. package/dist/esm/lib/v3/agent/tools/dragAndDrop.js.map +1 -1
  101. package/dist/esm/lib/v3/agent/tools/extract.js +1 -10
  102. package/dist/esm/lib/v3/agent/tools/extract.js.map +1 -1
  103. package/dist/esm/lib/v3/agent/tools/fillFormVision.js.map +1 -1
  104. package/dist/esm/lib/v3/agent/tools/fillform.js +1 -10
  105. package/dist/esm/lib/v3/agent/tools/fillform.js.map +1 -1
  106. package/dist/esm/lib/v3/agent/tools/index.d.ts +2 -2
  107. package/dist/esm/lib/v3/agent/tools/index.js +53 -5
  108. package/dist/esm/lib/v3/agent/tools/index.js.map +1 -1
  109. package/dist/esm/lib/v3/agent/tools/keys.d.ts +1 -1
  110. package/dist/esm/lib/v3/agent/tools/keys.js.map +1 -1
  111. package/dist/esm/lib/v3/agent/tools/type.js.map +1 -1
  112. package/dist/esm/lib/v3/api.js +9 -2
  113. package/dist/esm/lib/v3/api.js.map +1 -1
  114. package/dist/esm/lib/v3/flowlogger/EventEmitter.d.ts +7 -0
  115. package/dist/esm/lib/v3/flowlogger/EventEmitter.js +26 -0
  116. package/dist/esm/lib/v3/flowlogger/EventEmitter.js.map +1 -0
  117. package/dist/esm/lib/v3/flowlogger/EventSink.d.ts +44 -0
  118. package/dist/esm/lib/v3/flowlogger/EventSink.js +206 -0
  119. package/dist/esm/lib/v3/flowlogger/EventSink.js.map +1 -0
  120. package/dist/esm/lib/v3/flowlogger/EventStore.d.ts +26 -0
  121. package/dist/esm/lib/v3/flowlogger/EventStore.js +127 -0
  122. package/dist/esm/lib/v3/flowlogger/EventStore.js.map +1 -0
  123. package/dist/esm/lib/v3/flowlogger/FlowLogger.d.ts +99 -0
  124. package/dist/esm/lib/v3/flowlogger/FlowLogger.js +583 -0
  125. package/dist/esm/lib/v3/flowlogger/FlowLogger.js.map +1 -0
  126. package/dist/esm/lib/v3/flowlogger/prettify.d.ts +6 -0
  127. package/dist/esm/lib/v3/flowlogger/prettify.js +389 -0
  128. package/dist/esm/lib/v3/flowlogger/prettify.js.map +1 -0
  129. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js +43 -57
  130. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  131. package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +0 -4
  132. package/dist/esm/lib/v3/handlers/v3AgentHandler.js +2 -32
  133. package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  134. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +9 -13
  135. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  136. package/dist/esm/lib/v3/llm/aisdk.js +11 -17
  137. package/dist/esm/lib/v3/llm/aisdk.js.map +1 -1
  138. package/dist/esm/lib/v3/types/public/options.d.ts +7 -0
  139. package/dist/esm/lib/v3/types/public/options.js.map +1 -1
  140. package/dist/esm/lib/v3/understudy/cdp.d.ts +3 -12
  141. package/dist/esm/lib/v3/understudy/cdp.js +134 -21
  142. package/dist/esm/lib/v3/understudy/cdp.js.map +1 -1
  143. package/dist/esm/lib/v3/understudy/page.js +28 -18
  144. package/dist/esm/lib/v3/understudy/page.js.map +1 -1
  145. package/dist/esm/lib/v3/v3.d.ts +7 -2
  146. package/dist/esm/lib/v3/v3.js +178 -159
  147. package/dist/esm/lib/v3/v3.js.map +1 -1
  148. package/dist/esm/tests/integration/flowLogger.spec.d.ts +1 -0
  149. package/dist/esm/tests/integration/flowLogger.spec.js +712 -0
  150. package/dist/esm/tests/integration/flowLogger.spec.js.map +1 -0
  151. package/dist/esm/tests/integration/testUtils.d.ts +33 -0
  152. package/dist/esm/tests/integration/testUtils.js +138 -0
  153. package/dist/esm/tests/integration/testUtils.js.map +1 -1
  154. package/dist/esm/tests/integration/timeouts.spec.js +112 -2
  155. package/dist/esm/tests/integration/timeouts.spec.js.map +1 -1
  156. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.d.ts +1 -0
  157. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.js +93 -0
  158. package/dist/esm/tests/unit/flowlogger-capturing-cdp.test.js.map +1 -0
  159. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.d.ts +1 -0
  160. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.js +41 -0
  161. package/dist/esm/tests/unit/flowlogger-capturing-llm.test.js.map +1 -0
  162. package/dist/esm/tests/unit/flowlogger-eventstore.test.d.ts +1 -0
  163. package/dist/esm/tests/unit/flowlogger-eventstore.test.js +248 -0
  164. package/dist/esm/tests/unit/flowlogger-eventstore.test.js.map +1 -0
  165. package/package.json +3 -1
  166. package/dist/cjs/lib/v3/flowLogger.d.ts +0 -139
  167. package/dist/cjs/lib/v3/flowLogger.js +0 -881
  168. package/dist/cjs/lib/v3/flowLogger.js.map +0 -1
  169. package/dist/esm/lib/v3/flowLogger.d.ts +0 -139
  170. package/dist/esm/lib/v3/flowLogger.js +0 -868
  171. package/dist/esm/lib/v3/flowLogger.js.map +0 -1
@@ -1,10 +1,12 @@
1
- import { EventEmitter } from "events";
2
1
  import { z } from "zod";
3
2
  import { InferStagehandSchema, StagehandZodSchema } from "./zodCompat.js";
4
3
  import { LLMClient } from "./llm/LLMClient.js";
5
4
  import { AgentReplayStep } from "./types/private/index.js";
6
5
  import { AgentConfig, AgentExecuteOptions, AgentStreamExecuteOptions, AgentResult, LogLine, StagehandMetrics, Action, ActOptions, ActResult, defaultExtractSchema, ExtractOptions, HistoryEntry, ObserveOptions, pageTextSchema, V3FunctionName, V3Options, AgentStreamResult } from "./types/public/index.js";
7
6
  import { V3Context } from "./understudy/context.js";
7
+ import { type FlowLoggerContext } from "./flowlogger/FlowLogger.js";
8
+ import { EventEmitterWithWildcardSupport } from "./flowlogger/EventEmitter.js";
9
+ import { EventStore } from "./flowlogger/EventStore.js";
8
10
  /**
9
11
  * V3
10
12
  *
@@ -32,7 +34,7 @@ export declare class V3 {
32
34
  * Event bus for internal communication.
33
35
  * Emits events like 'screenshot' when screenshots are captured during agent execution.
34
36
  */
35
- readonly bus: EventEmitter;
37
+ readonly bus: EventEmitterWithWildcardSupport;
36
38
  private modelName;
37
39
  private modelClientOptions;
38
40
  private llmProvider;
@@ -70,6 +72,9 @@ export declare class V3 {
70
72
  private stagehandLogger;
71
73
  private _history;
72
74
  private readonly instanceId;
75
+ private readonly sessionId;
76
+ readonly eventStore: EventStore;
77
+ readonly flowLoggerContext: FlowLoggerContext;
73
78
  private static _processGuardsInstalled;
74
79
  private static _instances;
75
80
  private cacheStorage;
@@ -71,7 +71,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
71
71
  };
72
72
  Object.defineProperty(exports, "__esModule", { value: true });
73
73
  exports.V3 = void 0;
74
- const events_1 = require("events");
75
74
  const fs_1 = __importDefault(require("fs"));
76
75
  const os_1 = __importDefault(require("os"));
77
76
  const path_1 = __importDefault(require("path"));
@@ -102,7 +101,9 @@ const modelUtils_js_2 = require("../modelUtils.js");
102
101
  const api_js_1 = require("./api.js");
103
102
  const validateExperimentalFeatures_js_1 = require("./agent/utils/validateExperimentalFeatures.js");
104
103
  const variables_js_1 = require("./agent/utils/variables.js");
105
- const flowLogger_js_1 = require("./flowLogger.js");
104
+ const FlowLogger_js_1 = require("./flowlogger/FlowLogger.js");
105
+ const EventEmitter_js_1 = require("./flowlogger/EventEmitter.js");
106
+ const EventStore_js_1 = require("./flowlogger/EventStore.js");
106
107
  const timeoutGuard_js_1 = require("./handlers/handlerUtils/timeoutGuard.js");
107
108
  const sdkErrors_js_1 = require("./types/public/sdkErrors.js");
108
109
  const DEFAULT_MODEL_NAME = "openai/gpt-4.1-mini";
@@ -150,9 +151,15 @@ let V3 = (() => {
150
151
  return class V3 {
151
152
  static {
152
153
  const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
153
- _act_decorators = [(0, flowLogger_js_1.logStagehandStep)("Stagehand.act", "ACT")];
154
- _extract_decorators = [(0, flowLogger_js_1.logStagehandStep)("Stagehand.extract", "EXTRACT")];
155
- _observe_decorators = [(0, flowLogger_js_1.logStagehandStep)("Stagehand.observe", "OBSERVE")];
154
+ _act_decorators = [FlowLogger_js_1.FlowLogger.wrapWithLogging({
155
+ eventType: "StagehandAct",
156
+ })];
157
+ _extract_decorators = [FlowLogger_js_1.FlowLogger.wrapWithLogging({
158
+ eventType: "StagehandExtract",
159
+ })];
160
+ _observe_decorators = [FlowLogger_js_1.FlowLogger.wrapWithLogging({
161
+ eventType: "StagehandObserve",
162
+ })];
156
163
  __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);
157
164
  __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);
158
165
  __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);
@@ -169,7 +176,7 @@ let V3 = (() => {
169
176
  * Event bus for internal communication.
170
177
  * Emits events like 'screenshot' when screenshots are captured during agent execution.
171
178
  */
172
- bus = new events_1.EventEmitter();
179
+ bus = new EventEmitter_js_1.EventEmitterWithWildcardSupport();
173
180
  modelName;
174
181
  modelClientOptions;
175
182
  llmProvider;
@@ -237,6 +244,9 @@ let V3 = (() => {
237
244
  stagehandLogger;
238
245
  _history = [];
239
246
  instanceId;
247
+ sessionId;
248
+ eventStore;
249
+ flowLoggerContext;
240
250
  static _processGuardsInstalled = false;
241
251
  static _instances = new Set();
242
252
  cacheStorage;
@@ -276,6 +286,7 @@ let V3 = (() => {
276
286
  this.externalLogger = opts.logger;
277
287
  this.verbose = opts.verbose ?? 1;
278
288
  this.instanceId = (0, uuid_1.v7)();
289
+ this.sessionId = opts.sessionId ?? this.instanceId;
279
290
  this.keepAlive =
280
291
  opts.keepAlive ?? opts.browserbaseSessionCreateParams?.keepAlive;
281
292
  // Create per-instance StagehandLogger (handles usePino, verbose, externalLogger)
@@ -368,8 +379,21 @@ let V3 = (() => {
368
379
  act: this.act.bind(this),
369
380
  });
370
381
  this.opts = opts;
371
- // Initialize session file logger
372
- flowLogger_js_1.SessionFileLogger.init(this.instanceId, opts);
382
+ // FlowLogger always gets a per-instance session context and shared event
383
+ // bus. The attached EventStore decides which sinks are active:
384
+ // `verbose: 2` or `BROWSERBASE_FLOW_LOGS=1` enables pretty stderr output,
385
+ // and `BROWSERBASE_CONFIG_DIR` enables the pretty/jsonl file sinks for this session.
386
+ this.eventStore = new EventStore_js_1.EventStore(this.sessionId, opts);
387
+ this.flowLoggerContext = FlowLogger_js_1.FlowLogger.init(this.sessionId, this.bus);
388
+ // Flow event pipeline:
389
+ // FlowLogger -> this.bus -> this.eventStore -> configured sinks/query history.
390
+ // V3 owns the bus for this session. EventStore is not another bus; it just
391
+ // receives already-emitted FlowEvents here, then fans them out to sinks and
392
+ // keeps the queryable per-session history used by /v4/log, parent/ancestor lookups, and tests.
393
+ // `on()` stores a strong reference to the handler, so the EventStore
394
+ // stays alive until this bus is garbage-collected with the rest of the V3
395
+ // object graph.
396
+ this.bus.on("*", this.eventStore.emit);
373
397
  // Track instance for global process guard handling
374
398
  V3._instances.add(this);
375
399
  }
@@ -610,9 +634,7 @@ let V3 = (() => {
610
634
  env: "LOCAL",
611
635
  cdpHeaders: lbo.cdpHeaders,
612
636
  });
613
- const logCtx = flowLogger_js_1.SessionFileLogger.getContext();
614
- this.ctx.conn.cdpLogger = (info) => flowLogger_js_1.SessionFileLogger.logCdpCallEvent(info, logCtx);
615
- this.ctx.conn.cdpEventLogger = (info) => flowLogger_js_1.SessionFileLogger.logCdpMessageEvent(info, logCtx);
637
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
616
638
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
617
639
  this.state = {
618
640
  kind: "LOCAL",
@@ -708,9 +730,7 @@ let V3 = (() => {
708
730
  env: "LOCAL",
709
731
  localBrowserLaunchOptions: lbo,
710
732
  });
711
- const logCtx = flowLogger_js_1.SessionFileLogger.getContext();
712
- this.ctx.conn.cdpLogger = (info) => flowLogger_js_1.SessionFileLogger.logCdpCallEvent(info, logCtx);
713
- this.ctx.conn.cdpEventLogger = (info) => flowLogger_js_1.SessionFileLogger.logCdpMessageEvent(info, logCtx);
733
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
714
734
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
715
735
  this.state = {
716
736
  kind: "LOCAL",
@@ -745,9 +765,13 @@ let V3 = (() => {
745
765
  const baseSessionParams = this.opts.browserbaseSessionCreateParams ?? {};
746
766
  const resolvedKeepAlive = this.keepAlive;
747
767
  const keepAlive = this.keepAlive === true;
748
- const effectiveSessionParams = resolvedKeepAlive !== undefined
749
- ? { ...baseSessionParams, keepAlive: resolvedKeepAlive }
750
- : baseSessionParams;
768
+ let effectiveSessionParams = baseSessionParams;
769
+ if (resolvedKeepAlive !== undefined) {
770
+ effectiveSessionParams = {
771
+ ...baseSessionParams,
772
+ keepAlive: resolvedKeepAlive,
773
+ };
774
+ }
751
775
  if (!this.disableAPI && !this.experimental) {
752
776
  this.apiClient = new api_js_1.StagehandAPIClient({
753
777
  apiKey,
@@ -792,9 +816,7 @@ let V3 = (() => {
792
816
  env: "BROWSERBASE",
793
817
  apiClient: this.apiClient,
794
818
  });
795
- const logCtx = flowLogger_js_1.SessionFileLogger.getContext();
796
- this.ctx.conn.cdpLogger = (info) => flowLogger_js_1.SessionFileLogger.logCdpCallEvent(info, logCtx);
797
- this.ctx.conn.cdpEventLogger = (info) => flowLogger_js_1.SessionFileLogger.logCdpMessageEvent(info, logCtx);
819
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
798
820
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
799
821
  this.state = { kind: "BROWSERBASE", sessionId, ws, bb };
800
822
  this.browserbaseSessionId = sessionId;
@@ -1137,7 +1159,7 @@ let V3 = (() => {
1137
1159
  try {
1138
1160
  // Close session file logger
1139
1161
  try {
1140
- await flowLogger_js_1.SessionFileLogger.close();
1162
+ await FlowLogger_js_1.FlowLogger.close(this.flowLoggerContext);
1141
1163
  }
1142
1164
  catch {
1143
1165
  // ignore
@@ -1173,6 +1195,12 @@ let V3 = (() => {
1173
1195
  catch {
1174
1196
  // ignore
1175
1197
  }
1198
+ try {
1199
+ await this.eventStore.destroy();
1200
+ }
1201
+ catch {
1202
+ // ignore
1203
+ }
1176
1204
  try {
1177
1205
  this.bus.removeAllListeners();
1178
1206
  }
@@ -1439,152 +1467,56 @@ let V3 = (() => {
1439
1467
  throw new index_js_1.CuaModelRequiredError(index_js_1.AVAILABLE_CUA_MODELS);
1440
1468
  }
1441
1469
  const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1442
- return {
1443
- execute: async (instructionOrOptions) => (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1444
- (0, validateExperimentalFeatures_js_1.validateExperimentalFeatures)({
1445
- isExperimental: this.experimental,
1446
- agentConfig: options,
1447
- executeOptions: typeof instructionOrOptions === "object"
1448
- ? instructionOrOptions
1449
- : null,
1450
- });
1451
- flowLogger_js_1.SessionFileLogger.logAgentTaskStarted({
1452
- invocation: "Agent.execute",
1453
- args: [instructionOrOptions],
1454
- });
1455
- const tools = options?.integrations
1456
- ? await (0, utils_js_2.resolveTools)(options.integrations, options.tools)
1457
- : (options?.tools ?? {});
1458
- const handler = new v3CuaAgentHandler_js_1.V3CuaAgentHandler(this, this.logger, {
1459
- modelName,
1460
- clientOptions,
1461
- userProvidedInstructions: options.systemPrompt ??
1462
- `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1463
- }, tools);
1464
- const resolvedOptions = typeof instructionOrOptions === "string"
1465
- ? {
1466
- instruction: instructionOrOptions,
1467
- toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1468
- }
1469
- : {
1470
- ...instructionOrOptions,
1471
- toolTimeout: instructionOrOptions.toolTimeout ??
1472
- DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1473
- };
1474
- if (resolvedOptions.page) {
1475
- const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1476
- this.ctx.setActivePage(normalizedPage);
1477
- }
1478
- const instruction = resolvedOptions.instruction.trim();
1479
- const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1480
- const cacheVariables = (0, variables_js_1.flattenVariables)(resolvedOptions.variables);
1481
- let cacheContext = null;
1482
- if (this.agentCache.shouldAttemptCache(instruction)) {
1483
- const startPage = await this.ctx.awaitActivePage();
1484
- cacheContext = await this.agentCache.prepareContext({
1485
- instruction,
1486
- options: sanitizedOptions,
1487
- configSignature: agentConfigSignature,
1488
- page: startPage,
1489
- variables: cacheVariables,
1490
- });
1491
- if (cacheContext) {
1492
- const replayed = await this.agentCache.tryReplay(cacheContext);
1493
- if (replayed) {
1494
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1495
- return replayed;
1496
- }
1497
- }
1498
- }
1499
- let agentSteps = [];
1500
- const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1501
- if (shouldRecordLocally) {
1502
- this.beginAgentReplayRecording();
1503
- }
1504
- let result;
1505
- try {
1506
- if (this.apiClient && !this.experimental) {
1507
- const page = await this.ctx.awaitActivePage();
1508
- result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1509
- if (cacheContext) {
1510
- const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1511
- await this.agentCache.storeTransferredEntry(transferredEntry);
1512
- }
1513
- }
1514
- else {
1515
- result = await handler.execute(instructionOrOptions);
1516
- }
1517
- if (shouldRecordLocally) {
1518
- agentSteps = this.endAgentReplayRecording();
1519
- }
1520
- if (shouldRecordLocally &&
1521
- cacheContext &&
1522
- result.success &&
1523
- agentSteps.length > 0) {
1524
- await this.agentCache.store(cacheContext, agentSteps, result);
1525
- }
1526
- return result;
1527
- }
1528
- catch (err) {
1529
- if (shouldRecordLocally)
1530
- this.discardAgentReplayRecording();
1531
- throw err;
1532
- }
1533
- finally {
1534
- if (shouldRecordLocally) {
1535
- this.discardAgentReplayRecording();
1536
- }
1537
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1538
- }
1539
- }),
1540
- };
1541
- }
1542
- // Default: AISDK tools-based agent
1543
- const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1544
- const isStreaming = options?.stream ?? false;
1545
- return {
1546
- execute: async (instructionOrOptions) => (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1470
+ const execute = async (instructionOrOptions) => (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1547
1471
  (0, validateExperimentalFeatures_js_1.validateExperimentalFeatures)({
1548
1472
  isExperimental: this.experimental,
1549
1473
  agentConfig: options,
1550
1474
  executeOptions: typeof instructionOrOptions === "object"
1551
1475
  ? instructionOrOptions
1552
1476
  : null,
1553
- isStreaming,
1554
- });
1555
- flowLogger_js_1.SessionFileLogger.logAgentTaskStarted({
1556
- invocation: "Agent.execute",
1557
- args: [instructionOrOptions],
1558
1477
  });
1559
- // Streaming mode
1560
- if (isStreaming) {
1561
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1478
+ const tools = options?.integrations
1479
+ ? await (0, utils_js_2.resolveTools)(options.integrations, options.tools)
1480
+ : (options?.tools ?? {});
1481
+ const handler = new v3CuaAgentHandler_js_1.V3CuaAgentHandler(this, this.logger, {
1482
+ modelName,
1483
+ clientOptions,
1484
+ userProvidedInstructions: options.systemPrompt ??
1485
+ `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1486
+ }, tools);
1487
+ const resolvedOptions = typeof instructionOrOptions === "string"
1488
+ ? {
1489
+ instruction: instructionOrOptions,
1490
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1491
+ }
1492
+ : {
1493
+ ...instructionOrOptions,
1494
+ toolTimeout: instructionOrOptions.toolTimeout ??
1495
+ DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1496
+ };
1497
+ if (resolvedOptions.page) {
1498
+ const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1499
+ this.ctx.setActivePage(normalizedPage);
1500
+ }
1501
+ const instruction = resolvedOptions.instruction.trim();
1502
+ const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1503
+ const cacheVariables = (0, variables_js_1.flattenVariables)(resolvedOptions.variables);
1504
+ let cacheContext = null;
1505
+ if (this.agentCache.shouldAttemptCache(instruction)) {
1506
+ const startPage = await this.ctx.awaitActivePage();
1507
+ cacheContext = await this.agentCache.prepareContext({
1508
+ instruction,
1509
+ options: sanitizedOptions,
1510
+ configSignature: agentConfigSignature,
1511
+ page: startPage,
1512
+ variables: cacheVariables,
1513
+ });
1562
1514
  if (cacheContext) {
1563
- const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1515
+ const replayed = await this.agentCache.tryReplay(cacheContext);
1564
1516
  if (replayed) {
1565
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1566
1517
  return replayed;
1567
1518
  }
1568
1519
  }
1569
- const streamResult = await handler.stream(resolvedOptions);
1570
- if (cacheContext) {
1571
- const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1572
- // Log completion when stream is returned (stream completes asynchronously)
1573
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1574
- return wrappedStream;
1575
- }
1576
- // Log completion when stream is returned (stream completes asynchronously)
1577
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1578
- return streamResult;
1579
- }
1580
- // Non-streaming mode (default)
1581
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1582
- if (cacheContext) {
1583
- const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1584
- if (replayed) {
1585
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1586
- return replayed;
1587
- }
1588
1520
  }
1589
1521
  let agentSteps = [];
1590
1522
  const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
@@ -1595,14 +1527,14 @@ let V3 = (() => {
1595
1527
  try {
1596
1528
  if (this.apiClient && !this.experimental) {
1597
1529
  const page = await this.ctx.awaitActivePage();
1598
- result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1530
+ result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1599
1531
  if (cacheContext) {
1600
1532
  const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1601
1533
  await this.agentCache.storeTransferredEntry(transferredEntry);
1602
1534
  }
1603
1535
  }
1604
1536
  else {
1605
- result = await handler.execute(resolvedOptions);
1537
+ result = await handler.execute(instructionOrOptions);
1606
1538
  }
1607
1539
  if (shouldRecordLocally) {
1608
1540
  agentSteps = this.endAgentReplayRecording();
@@ -1624,9 +1556,96 @@ let V3 = (() => {
1624
1556
  if (shouldRecordLocally) {
1625
1557
  this.discardAgentReplayRecording();
1626
1558
  }
1627
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1628
1559
  }
1629
- }),
1560
+ });
1561
+ return {
1562
+ execute: FlowLogger_js_1.FlowLogger.wrapWithLogging({
1563
+ eventType: "AgentExecute",
1564
+ context: this.flowLoggerContext,
1565
+ })(execute),
1566
+ };
1567
+ }
1568
+ // Default: AISDK tools-based agent
1569
+ const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1570
+ const isStreaming = options?.stream ?? false;
1571
+ const execute = async (instructionOrOptions) => (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1572
+ (0, validateExperimentalFeatures_js_1.validateExperimentalFeatures)({
1573
+ isExperimental: this.experimental,
1574
+ agentConfig: options,
1575
+ executeOptions: typeof instructionOrOptions === "object"
1576
+ ? instructionOrOptions
1577
+ : null,
1578
+ isStreaming,
1579
+ });
1580
+ // Streaming mode
1581
+ if (isStreaming) {
1582
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1583
+ if (cacheContext) {
1584
+ const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1585
+ if (replayed) {
1586
+ return replayed;
1587
+ }
1588
+ }
1589
+ const streamResult = await handler.stream(resolvedOptions);
1590
+ if (cacheContext) {
1591
+ const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1592
+ return wrappedStream;
1593
+ }
1594
+ return streamResult;
1595
+ }
1596
+ // Non-streaming mode (default)
1597
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1598
+ if (cacheContext) {
1599
+ const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1600
+ if (replayed) {
1601
+ return replayed;
1602
+ }
1603
+ }
1604
+ let agentSteps = [];
1605
+ const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1606
+ if (shouldRecordLocally) {
1607
+ this.beginAgentReplayRecording();
1608
+ }
1609
+ let result;
1610
+ try {
1611
+ if (this.apiClient && !this.experimental) {
1612
+ const page = await this.ctx.awaitActivePage();
1613
+ result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1614
+ if (cacheContext) {
1615
+ const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1616
+ await this.agentCache.storeTransferredEntry(transferredEntry);
1617
+ }
1618
+ }
1619
+ else {
1620
+ result = await handler.execute(resolvedOptions);
1621
+ }
1622
+ if (shouldRecordLocally) {
1623
+ agentSteps = this.endAgentReplayRecording();
1624
+ }
1625
+ if (shouldRecordLocally &&
1626
+ cacheContext &&
1627
+ result.success &&
1628
+ agentSteps.length > 0) {
1629
+ await this.agentCache.store(cacheContext, agentSteps, result);
1630
+ }
1631
+ return result;
1632
+ }
1633
+ catch (err) {
1634
+ if (shouldRecordLocally)
1635
+ this.discardAgentReplayRecording();
1636
+ throw err;
1637
+ }
1638
+ finally {
1639
+ if (shouldRecordLocally) {
1640
+ this.discardAgentReplayRecording();
1641
+ }
1642
+ }
1643
+ });
1644
+ return {
1645
+ execute: FlowLogger_js_1.FlowLogger.wrapWithLogging({
1646
+ eventType: "AgentExecute",
1647
+ context: this.flowLoggerContext,
1648
+ })(execute),
1630
1649
  };
1631
1650
  }
1632
1651
  };