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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/cjs/lib/utils.d.ts +1 -0
  2. package/dist/cjs/lib/utils.js +4 -0
  3. package/dist/cjs/lib/utils.js.map +1 -1
  4. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +4 -6
  5. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  6. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +4 -6
  7. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  8. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +4 -6
  9. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  10. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.d.ts +2 -0
  11. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js +2 -2
  12. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  13. package/dist/cjs/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  14. package/dist/cjs/lib/v3/agent/tools/braveSearch.js.map +1 -0
  15. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  16. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js +70 -0
  17. package/dist/cjs/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  18. package/dist/cjs/lib/v3/agent/tools/index.d.ts +14 -3
  19. package/dist/cjs/lib/v3/agent/tools/index.js +7 -3
  20. package/dist/cjs/lib/v3/agent/tools/index.js.map +1 -1
  21. package/dist/cjs/lib/v3/eventStore.d.ts +41 -0
  22. package/dist/cjs/lib/v3/eventStore.js +375 -0
  23. package/dist/cjs/lib/v3/eventStore.js.map +1 -0
  24. package/dist/cjs/lib/v3/flowLogger.d.ts +62 -103
  25. package/dist/cjs/lib/v3/flowLogger.js +362 -773
  26. package/dist/cjs/lib/v3/flowLogger.js.map +1 -1
  27. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js +21 -33
  28. package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  29. package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +0 -4
  30. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +14 -34
  31. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  32. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +10 -12
  33. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  34. package/dist/cjs/lib/v3/llm/aisdk.js +10 -16
  35. package/dist/cjs/lib/v3/llm/aisdk.js.map +1 -1
  36. package/dist/cjs/lib/v3/types/public/agent.d.ts +16 -2
  37. package/dist/cjs/lib/v3/types/public/agent.js.map +1 -1
  38. package/dist/cjs/lib/v3/types/public/options.d.ts +5 -0
  39. package/dist/cjs/lib/v3/types/public/options.js.map +1 -1
  40. package/dist/cjs/lib/v3/understudy/cdp.d.ts +3 -12
  41. package/dist/cjs/lib/v3/understudy/cdp.js +83 -10
  42. package/dist/cjs/lib/v3/understudy/cdp.js.map +1 -1
  43. package/dist/cjs/lib/v3/understudy/page.js +32 -17
  44. package/dist/cjs/lib/v3/understudy/page.js.map +1 -1
  45. package/dist/cjs/lib/v3/v3.d.ts +10 -0
  46. package/dist/cjs/lib/v3/v3.js +181 -157
  47. package/dist/cjs/lib/v3/v3.js.map +1 -1
  48. package/dist/cjs/tests/unit/public-api/public-types.test.js.map +1 -1
  49. package/dist/esm/lib/utils.d.ts +1 -0
  50. package/dist/esm/lib/utils.js +3 -0
  51. package/dist/esm/lib/utils.js.map +1 -1
  52. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +5 -7
  53. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  54. package/dist/esm/lib/v3/agent/GoogleCUAClient.js +5 -7
  55. package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  56. package/dist/esm/lib/v3/agent/OpenAICUAClient.js +5 -7
  57. package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  58. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.d.ts +2 -0
  59. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js +2 -2
  60. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  61. package/dist/esm/lib/v3/agent/tools/{search.js → braveSearch.js} +1 -1
  62. package/dist/esm/lib/v3/agent/tools/braveSearch.js.map +1 -0
  63. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.d.ts +13 -0
  64. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js +66 -0
  65. package/dist/esm/lib/v3/agent/tools/browserbaseSearch.js.map +1 -0
  66. package/dist/esm/lib/v3/agent/tools/index.d.ts +14 -3
  67. package/dist/esm/lib/v3/agent/tools/index.js +7 -3
  68. package/dist/esm/lib/v3/agent/tools/index.js.map +1 -1
  69. package/dist/esm/lib/v3/eventStore.d.ts +41 -0
  70. package/dist/esm/lib/v3/eventStore.js +363 -0
  71. package/dist/esm/lib/v3/eventStore.js.map +1 -0
  72. package/dist/esm/lib/v3/flowLogger.d.ts +62 -103
  73. package/dist/esm/lib/v3/flowLogger.js +356 -762
  74. package/dist/esm/lib/v3/flowLogger.js.map +1 -1
  75. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js +22 -34
  76. package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
  77. package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +0 -4
  78. package/dist/esm/lib/v3/handlers/v3AgentHandler.js +16 -36
  79. package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  80. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +11 -13
  81. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  82. package/dist/esm/lib/v3/llm/aisdk.js +11 -17
  83. package/dist/esm/lib/v3/llm/aisdk.js.map +1 -1
  84. package/dist/esm/lib/v3/types/public/agent.d.ts +16 -2
  85. package/dist/esm/lib/v3/types/public/agent.js.map +1 -1
  86. package/dist/esm/lib/v3/types/public/options.d.ts +5 -0
  87. package/dist/esm/lib/v3/types/public/options.js.map +1 -1
  88. package/dist/esm/lib/v3/understudy/cdp.d.ts +3 -12
  89. package/dist/esm/lib/v3/understudy/cdp.js +83 -10
  90. package/dist/esm/lib/v3/understudy/cdp.js.map +1 -1
  91. package/dist/esm/lib/v3/understudy/page.js +33 -18
  92. package/dist/esm/lib/v3/understudy/page.js.map +1 -1
  93. package/dist/esm/lib/v3/v3.d.ts +10 -0
  94. package/dist/esm/lib/v3/v3.js +182 -158
  95. package/dist/esm/lib/v3/v3.js.map +1 -1
  96. package/dist/esm/tests/unit/public-api/public-types.test.js.map +1 -1
  97. package/package.json +1 -3
  98. package/dist/cjs/lib/v3/agent/tools/search.js.map +0 -1
  99. package/dist/cjs/tests/unit/rerender-missing-shadows.test.d.ts +0 -1
  100. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js +0 -209
  101. package/dist/cjs/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  102. package/dist/esm/lib/v3/agent/tools/search.js.map +0 -1
  103. package/dist/esm/tests/unit/rerender-missing-shadows.test.d.ts +0 -1
  104. package/dist/esm/tests/unit/rerender-missing-shadows.test.js +0 -207
  105. package/dist/esm/tests/unit/rerender-missing-shadows.test.js.map +0 -1
  106. /package/dist/cjs/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
  107. /package/dist/esm/lib/v3/agent/tools/{search.d.ts → braveSearch.d.ts} +0 -0
@@ -5,6 +5,7 @@ import { LLMClient } from "./llm/LLMClient.js";
5
5
  import { AgentReplayStep } from "./types/private/index.js";
6
6
  import { AgentConfig, AgentExecuteOptions, AgentStreamExecuteOptions, AgentResult, LogLine, StagehandMetrics, Action, ActOptions, ActResult, defaultExtractSchema, ExtractOptions, HistoryEntry, ObserveOptions, pageTextSchema, V3FunctionName, V3Options, AgentStreamResult } from "./types/public/index.js";
7
7
  import { V3Context } from "./understudy/context.js";
8
+ import { type FlowLoggerContext } from "./flowLogger.js";
8
9
  /**
9
10
  * V3
10
11
  *
@@ -70,6 +71,8 @@ export declare class V3 {
70
71
  private stagehandLogger;
71
72
  private _history;
72
73
  private readonly instanceId;
74
+ private readonly sessionId;
75
+ readonly flowLoggerContext: FlowLoggerContext;
73
76
  private static _processGuardsInstalled;
74
77
  private static _instances;
75
78
  private cacheStorage;
@@ -78,6 +81,8 @@ export declare class V3 {
78
81
  private apiClient;
79
82
  private keepAlive?;
80
83
  private shutdownSupervisor;
84
+ private detachEventStoreListener;
85
+ private withLoggingContext;
81
86
  stagehandMetrics: StagehandMetrics;
82
87
  constructor(opts: V3Options);
83
88
  /**
@@ -151,6 +156,11 @@ export declare class V3 {
151
156
  close(opts?: {
152
157
  force?: boolean;
153
158
  }): Promise<void>;
159
+ /**
160
+ * Resolves the Browserbase API key from options or environment variables.
161
+ * Returns undefined if no key is found (does not throw).
162
+ */
163
+ get browserbaseApiKey(): string | undefined;
154
164
  /** Guard: ensure Browserbase credentials exist in options. */
155
165
  private requireBrowserbaseCreds;
156
166
  get logger(): (logLine: LogLine) => void;
@@ -103,6 +103,7 @@ const api_js_1 = require("./api.js");
103
103
  const validateExperimentalFeatures_js_1 = require("./agent/utils/validateExperimentalFeatures.js");
104
104
  const variables_js_1 = require("./agent/utils/variables.js");
105
105
  const flowLogger_js_1 = require("./flowLogger.js");
106
+ const eventStore_js_1 = require("./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,18 @@ 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
+ eventIdSuffix: "4",
157
+ })];
158
+ _extract_decorators = [flowLogger_js_1.FlowLogger.wrapWithLogging({
159
+ eventType: "StagehandExtract",
160
+ eventIdSuffix: "4",
161
+ })];
162
+ _observe_decorators = [flowLogger_js_1.FlowLogger.wrapWithLogging({
163
+ eventType: "StagehandObserve",
164
+ eventIdSuffix: "4",
165
+ })];
156
166
  __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
167
  __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
168
  __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);
@@ -237,6 +247,8 @@ let V3 = (() => {
237
247
  stagehandLogger;
238
248
  _history = [];
239
249
  instanceId;
250
+ sessionId;
251
+ flowLoggerContext;
240
252
  static _processGuardsInstalled = false;
241
253
  static _instances = new Set();
242
254
  cacheStorage;
@@ -245,6 +257,10 @@ let V3 = (() => {
245
257
  apiClient = null;
246
258
  keepAlive;
247
259
  shutdownSupervisor = null;
260
+ detachEventStoreListener = null;
261
+ withLoggingContext(fn) {
262
+ return (0, logger_js_2.withInstanceLogContext)(this.instanceId, fn);
263
+ }
248
264
  stagehandMetrics = {
249
265
  actPromptTokens: 0,
250
266
  actCompletionTokens: 0,
@@ -276,6 +292,7 @@ let V3 = (() => {
276
292
  this.externalLogger = opts.logger;
277
293
  this.verbose = opts.verbose ?? 1;
278
294
  this.instanceId = (0, uuid_1.v7)();
295
+ this.sessionId = opts.sessionId ?? this.instanceId;
279
296
  this.keepAlive =
280
297
  opts.keepAlive ?? opts.browserbaseSessionCreateParams?.keepAlive;
281
298
  // Create per-instance StagehandLogger (handles usePino, verbose, externalLogger)
@@ -368,8 +385,9 @@ let V3 = (() => {
368
385
  act: this.act.bind(this),
369
386
  });
370
387
  this.opts = opts;
371
- // Initialize session file logger
372
- flowLogger_js_1.SessionFileLogger.init(this.instanceId, opts);
388
+ void (0, eventStore_js_1.getEventStore)().initializeSession(this.sessionId, opts);
389
+ this.flowLoggerContext = flowLogger_js_1.FlowLogger.init(this.sessionId, this.bus);
390
+ this.detachEventStoreListener = (0, eventStore_js_1.getEventStore)().attachBus(this.sessionId, this.bus);
373
391
  // Track instance for global process guard handling
374
392
  V3._instances.add(this);
375
393
  }
@@ -576,7 +594,7 @@ let V3 = (() => {
576
594
  */
577
595
  async init() {
578
596
  try {
579
- return await (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
597
+ return await this.withLoggingContext(async () => {
580
598
  this.actHandler = new actHandler_js_1.ActHandler(this.llmClient, this.modelName, this.modelClientOptions, (model) => this.resolveLlmClient(model), this.opts.systemPrompt ?? "", this.logInferenceToFile, this.opts.selfHeal ?? true, (functionName, promptTokens, completionTokens, reasoningTokens, cachedInputTokens, inferenceTimeMs) => this.updateMetrics(functionName, promptTokens, completionTokens, reasoningTokens, cachedInputTokens, inferenceTimeMs), this.domSettleTimeoutMs);
581
599
  this.extractHandler = new extractHandler_js_1.ExtractHandler(this.llmClient, this.modelName, this.modelClientOptions, (model) => this.resolveLlmClient(model), this.opts.systemPrompt ?? "", this.logInferenceToFile, this.experimental, (functionName, promptTokens, completionTokens, reasoningTokens, cachedInputTokens, inferenceTimeMs) => this.updateMetrics(functionName, promptTokens, completionTokens, reasoningTokens, cachedInputTokens, inferenceTimeMs));
582
600
  this.observeHandler = new observeHandler_js_1.ObserveHandler(this.llmClient, this.modelName, this.modelClientOptions, (model) => this.resolveLlmClient(model), this.opts.systemPrompt ?? "", this.logInferenceToFile, this.experimental, (functionName, promptTokens, completionTokens, reasoningTokens, cachedInputTokens, inferenceTimeMs) => this.updateMetrics(functionName, promptTokens, completionTokens, reasoningTokens, cachedInputTokens, inferenceTimeMs));
@@ -610,9 +628,7 @@ let V3 = (() => {
610
628
  env: "LOCAL",
611
629
  cdpHeaders: lbo.cdpHeaders,
612
630
  });
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);
631
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
616
632
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
617
633
  this.state = {
618
634
  kind: "LOCAL",
@@ -708,9 +724,7 @@ let V3 = (() => {
708
724
  env: "LOCAL",
709
725
  localBrowserLaunchOptions: lbo,
710
726
  });
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);
727
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
714
728
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
715
729
  this.state = {
716
730
  kind: "LOCAL",
@@ -792,9 +806,7 @@ let V3 = (() => {
792
806
  env: "BROWSERBASE",
793
807
  apiClient: this.apiClient,
794
808
  });
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);
809
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
798
810
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
799
811
  this.state = { kind: "BROWSERBASE", sessionId, ws, bb };
800
812
  this.browserbaseSessionId = sessionId;
@@ -856,6 +868,13 @@ let V3 = (() => {
856
868
  // ignore cleanup errors
857
869
  }
858
870
  }
871
+ try {
872
+ this.detachEventStoreListener?.();
873
+ this.detachEventStoreListener = null;
874
+ }
875
+ catch {
876
+ // ignore cleanup errors
877
+ }
859
878
  throw error;
860
879
  }
861
880
  }
@@ -899,7 +918,7 @@ let V3 = (() => {
899
918
  this.browserbaseDebugUrl = undefined;
900
919
  }
901
920
  async act(input, options) {
902
- return await (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
921
+ return await this.withLoggingContext(async () => {
903
922
  if (!this.actHandler)
904
923
  throw new index_js_1.StagehandNotInitializedError("act()");
905
924
  let actResult;
@@ -983,7 +1002,7 @@ let V3 = (() => {
983
1002
  });
984
1003
  }
985
1004
  async extract(a, b, c) {
986
- return await (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1005
+ return await this.withLoggingContext(async () => {
987
1006
  if (!this.extractHandler) {
988
1007
  throw new index_js_1.StagehandNotInitializedError("extract()");
989
1008
  }
@@ -1051,7 +1070,7 @@ let V3 = (() => {
1051
1070
  });
1052
1071
  }
1053
1072
  async observe(a, b) {
1054
- return await (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1073
+ return await this.withLoggingContext(async () => {
1055
1074
  if (!this.observeHandler) {
1056
1075
  throw new index_js_1.StagehandNotInitializedError("observe()");
1057
1076
  }
@@ -1137,7 +1156,7 @@ let V3 = (() => {
1137
1156
  try {
1138
1157
  // Close session file logger
1139
1158
  try {
1140
- await flowLogger_js_1.SessionFileLogger.close();
1159
+ await flowLogger_js_1.FlowLogger.close(this.flowLoggerContext);
1141
1160
  }
1142
1161
  catch {
1143
1162
  // ignore
@@ -1173,6 +1192,13 @@ let V3 = (() => {
1173
1192
  catch {
1174
1193
  // ignore
1175
1194
  }
1195
+ try {
1196
+ this.detachEventStoreListener?.();
1197
+ this.detachEventStoreListener = null;
1198
+ }
1199
+ catch {
1200
+ // ignore
1201
+ }
1176
1202
  try {
1177
1203
  this.bus.removeAllListeners();
1178
1204
  }
@@ -1186,6 +1212,13 @@ let V3 = (() => {
1186
1212
  V3._instances.delete(this);
1187
1213
  }
1188
1214
  }
1215
+ /**
1216
+ * Resolves the Browserbase API key from options or environment variables.
1217
+ * Returns undefined if no key is found (does not throw).
1218
+ */
1219
+ get browserbaseApiKey() {
1220
+ return this.opts.apiKey || process_1.default.env.BROWSERBASE_API_KEY;
1221
+ }
1189
1222
  /** Guard: ensure Browserbase credentials exist in options. */
1190
1223
  requireBrowserbaseCreds() {
1191
1224
  let { apiKey, projectId } = this.opts;
@@ -1432,152 +1465,59 @@ let V3 = (() => {
1432
1465
  throw new index_js_1.CuaModelRequiredError(index_js_1.AVAILABLE_CUA_MODELS);
1433
1466
  }
1434
1467
  const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1435
- return {
1436
- execute: async (instructionOrOptions) => (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1437
- (0, validateExperimentalFeatures_js_1.validateExperimentalFeatures)({
1438
- isExperimental: this.experimental,
1439
- agentConfig: options,
1440
- executeOptions: typeof instructionOrOptions === "object"
1441
- ? instructionOrOptions
1442
- : null,
1443
- });
1444
- flowLogger_js_1.SessionFileLogger.logAgentTaskStarted({
1445
- invocation: "Agent.execute",
1446
- args: [instructionOrOptions],
1447
- });
1448
- const tools = options?.integrations
1449
- ? await (0, utils_js_2.resolveTools)(options.integrations, options.tools)
1450
- : (options?.tools ?? {});
1451
- const handler = new v3CuaAgentHandler_js_1.V3CuaAgentHandler(this, this.logger, {
1452
- modelName,
1453
- clientOptions,
1454
- userProvidedInstructions: options.systemPrompt ??
1455
- `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1456
- }, tools);
1457
- const resolvedOptions = typeof instructionOrOptions === "string"
1458
- ? {
1459
- instruction: instructionOrOptions,
1460
- toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1461
- }
1462
- : {
1463
- ...instructionOrOptions,
1464
- toolTimeout: instructionOrOptions.toolTimeout ??
1465
- DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1466
- };
1467
- if (resolvedOptions.page) {
1468
- const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1469
- this.ctx.setActivePage(normalizedPage);
1470
- }
1471
- const instruction = resolvedOptions.instruction.trim();
1472
- const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1473
- const cacheVariables = (0, variables_js_1.flattenVariables)(resolvedOptions.variables);
1474
- let cacheContext = null;
1475
- if (this.agentCache.shouldAttemptCache(instruction)) {
1476
- const startPage = await this.ctx.awaitActivePage();
1477
- cacheContext = await this.agentCache.prepareContext({
1478
- instruction,
1479
- options: sanitizedOptions,
1480
- configSignature: agentConfigSignature,
1481
- page: startPage,
1482
- variables: cacheVariables,
1483
- });
1484
- if (cacheContext) {
1485
- const replayed = await this.agentCache.tryReplay(cacheContext);
1486
- if (replayed) {
1487
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1488
- return replayed;
1489
- }
1490
- }
1491
- }
1492
- let agentSteps = [];
1493
- const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1494
- if (shouldRecordLocally) {
1495
- this.beginAgentReplayRecording();
1496
- }
1497
- let result;
1498
- try {
1499
- if (this.apiClient && !this.experimental) {
1500
- const page = await this.ctx.awaitActivePage();
1501
- result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1502
- if (cacheContext) {
1503
- const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1504
- await this.agentCache.storeTransferredEntry(transferredEntry);
1505
- }
1506
- }
1507
- else {
1508
- result = await handler.execute(instructionOrOptions);
1509
- }
1510
- if (shouldRecordLocally) {
1511
- agentSteps = this.endAgentReplayRecording();
1512
- }
1513
- if (shouldRecordLocally &&
1514
- cacheContext &&
1515
- result.success &&
1516
- agentSteps.length > 0) {
1517
- await this.agentCache.store(cacheContext, agentSteps, result);
1518
- }
1519
- return result;
1520
- }
1521
- catch (err) {
1522
- if (shouldRecordLocally)
1523
- this.discardAgentReplayRecording();
1524
- throw err;
1525
- }
1526
- finally {
1527
- if (shouldRecordLocally) {
1528
- this.discardAgentReplayRecording();
1529
- }
1530
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1531
- }
1532
- }),
1533
- };
1534
- }
1535
- // Default: AISDK tools-based agent
1536
- const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1537
- const isStreaming = options?.stream ?? false;
1538
- return {
1539
- execute: async (instructionOrOptions) => (0, logger_js_2.withInstanceLogContext)(this.instanceId, async () => {
1468
+ const execute = flowLogger_js_1.FlowLogger.wrapWithLogging({
1469
+ eventType: "AgentExecute",
1470
+ eventIdSuffix: "3",
1471
+ })(async (instructionOrOptions) => this.withLoggingContext(async () => {
1540
1472
  (0, validateExperimentalFeatures_js_1.validateExperimentalFeatures)({
1541
1473
  isExperimental: this.experimental,
1542
1474
  agentConfig: options,
1543
1475
  executeOptions: typeof instructionOrOptions === "object"
1544
1476
  ? instructionOrOptions
1545
1477
  : null,
1546
- isStreaming,
1547
1478
  });
1548
- flowLogger_js_1.SessionFileLogger.logAgentTaskStarted({
1549
- invocation: "Agent.execute",
1550
- args: [instructionOrOptions],
1551
- });
1552
- // Streaming mode
1553
- if (isStreaming) {
1554
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1479
+ const tools = options?.integrations
1480
+ ? await (0, utils_js_2.resolveTools)(options.integrations, options.tools)
1481
+ : (options?.tools ?? {});
1482
+ const handler = new v3CuaAgentHandler_js_1.V3CuaAgentHandler(this, this.logger, {
1483
+ modelName,
1484
+ clientOptions,
1485
+ userProvidedInstructions: options.systemPrompt ??
1486
+ `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1487
+ }, tools);
1488
+ const resolvedOptions = typeof instructionOrOptions === "string"
1489
+ ? {
1490
+ instruction: instructionOrOptions,
1491
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1492
+ }
1493
+ : {
1494
+ ...instructionOrOptions,
1495
+ toolTimeout: instructionOrOptions.toolTimeout ??
1496
+ DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1497
+ };
1498
+ if (resolvedOptions.page) {
1499
+ const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1500
+ this.ctx.setActivePage(normalizedPage);
1501
+ }
1502
+ const instruction = resolvedOptions.instruction.trim();
1503
+ const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1504
+ const cacheVariables = (0, variables_js_1.flattenVariables)(resolvedOptions.variables);
1505
+ let cacheContext = null;
1506
+ if (this.agentCache.shouldAttemptCache(instruction)) {
1507
+ const startPage = await this.ctx.awaitActivePage();
1508
+ cacheContext = await this.agentCache.prepareContext({
1509
+ instruction,
1510
+ options: sanitizedOptions,
1511
+ configSignature: agentConfigSignature,
1512
+ page: startPage,
1513
+ variables: cacheVariables,
1514
+ });
1555
1515
  if (cacheContext) {
1556
- const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1516
+ const replayed = await this.agentCache.tryReplay(cacheContext);
1557
1517
  if (replayed) {
1558
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1559
1518
  return replayed;
1560
1519
  }
1561
1520
  }
1562
- const streamResult = await handler.stream(resolvedOptions);
1563
- if (cacheContext) {
1564
- const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1565
- // Log completion when stream is returned (stream completes asynchronously)
1566
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1567
- return wrappedStream;
1568
- }
1569
- // Log completion when stream is returned (stream completes asynchronously)
1570
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1571
- return streamResult;
1572
- }
1573
- // Non-streaming mode (default)
1574
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1575
- if (cacheContext) {
1576
- const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1577
- if (replayed) {
1578
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1579
- return replayed;
1580
- }
1581
1521
  }
1582
1522
  let agentSteps = [];
1583
1523
  const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
@@ -1588,14 +1528,14 @@ let V3 = (() => {
1588
1528
  try {
1589
1529
  if (this.apiClient && !this.experimental) {
1590
1530
  const page = await this.ctx.awaitActivePage();
1591
- result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1531
+ result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1592
1532
  if (cacheContext) {
1593
1533
  const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1594
1534
  await this.agentCache.storeTransferredEntry(transferredEntry);
1595
1535
  }
1596
1536
  }
1597
1537
  else {
1598
- result = await handler.execute(resolvedOptions);
1538
+ result = await handler.execute(instructionOrOptions);
1599
1539
  }
1600
1540
  if (shouldRecordLocally) {
1601
1541
  agentSteps = this.endAgentReplayRecording();
@@ -1617,9 +1557,93 @@ let V3 = (() => {
1617
1557
  if (shouldRecordLocally) {
1618
1558
  this.discardAgentReplayRecording();
1619
1559
  }
1620
- flowLogger_js_1.SessionFileLogger.logAgentTaskCompleted();
1621
1560
  }
1622
- }),
1561
+ }));
1562
+ return {
1563
+ execute,
1564
+ };
1565
+ }
1566
+ // Default: AISDK tools-based agent
1567
+ const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1568
+ const isStreaming = options?.stream ?? false;
1569
+ const execute = flowLogger_js_1.FlowLogger.wrapWithLogging({
1570
+ eventType: "AgentExecute",
1571
+ eventIdSuffix: "3",
1572
+ })(async (instructionOrOptions) => this.withLoggingContext(async () => {
1573
+ (0, validateExperimentalFeatures_js_1.validateExperimentalFeatures)({
1574
+ isExperimental: this.experimental,
1575
+ agentConfig: options,
1576
+ executeOptions: typeof instructionOrOptions === "object"
1577
+ ? instructionOrOptions
1578
+ : null,
1579
+ isStreaming,
1580
+ });
1581
+ // Streaming mode
1582
+ if (isStreaming) {
1583
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1584
+ if (cacheContext) {
1585
+ const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1586
+ if (replayed) {
1587
+ return replayed;
1588
+ }
1589
+ }
1590
+ const streamResult = await handler.stream(resolvedOptions);
1591
+ if (cacheContext) {
1592
+ const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1593
+ return wrappedStream;
1594
+ }
1595
+ return streamResult;
1596
+ }
1597
+ // Non-streaming mode (default)
1598
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1599
+ if (cacheContext) {
1600
+ const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1601
+ if (replayed) {
1602
+ return replayed;
1603
+ }
1604
+ }
1605
+ let agentSteps = [];
1606
+ const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1607
+ if (shouldRecordLocally) {
1608
+ this.beginAgentReplayRecording();
1609
+ }
1610
+ let result;
1611
+ try {
1612
+ if (this.apiClient && !this.experimental) {
1613
+ const page = await this.ctx.awaitActivePage();
1614
+ result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1615
+ if (cacheContext) {
1616
+ const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1617
+ await this.agentCache.storeTransferredEntry(transferredEntry);
1618
+ }
1619
+ }
1620
+ else {
1621
+ result = await handler.execute(resolvedOptions);
1622
+ }
1623
+ if (shouldRecordLocally) {
1624
+ agentSteps = this.endAgentReplayRecording();
1625
+ }
1626
+ if (shouldRecordLocally &&
1627
+ cacheContext &&
1628
+ result.success &&
1629
+ agentSteps.length > 0) {
1630
+ await this.agentCache.store(cacheContext, agentSteps, result);
1631
+ }
1632
+ return result;
1633
+ }
1634
+ catch (err) {
1635
+ if (shouldRecordLocally)
1636
+ this.discardAgentReplayRecording();
1637
+ throw err;
1638
+ }
1639
+ finally {
1640
+ if (shouldRecordLocally) {
1641
+ this.discardAgentReplayRecording();
1642
+ }
1643
+ }
1644
+ }));
1645
+ return {
1646
+ execute,
1623
1647
  };
1624
1648
  }
1625
1649
  };