@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;
@@ -63,7 +63,8 @@ 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.js";
67
+ import { getEventStore } from "./eventStore.js";
67
68
  import { createTimeoutGuard } from "./handlers/handlerUtils/timeoutGuard.js";
68
69
  import { ActTimeoutError } from "./types/public/sdkErrors.js";
69
70
  const DEFAULT_MODEL_NAME = "openai/gpt-4.1-mini";
@@ -111,9 +112,18 @@ let V3 = (() => {
111
112
  return class V3 {
112
113
  static {
113
114
  const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
114
- _act_decorators = [logStagehandStep("Stagehand.act", "ACT")];
115
- _extract_decorators = [logStagehandStep("Stagehand.extract", "EXTRACT")];
116
- _observe_decorators = [logStagehandStep("Stagehand.observe", "OBSERVE")];
115
+ _act_decorators = [FlowLogger.wrapWithLogging({
116
+ eventType: "StagehandAct",
117
+ eventIdSuffix: "4",
118
+ })];
119
+ _extract_decorators = [FlowLogger.wrapWithLogging({
120
+ eventType: "StagehandExtract",
121
+ eventIdSuffix: "4",
122
+ })];
123
+ _observe_decorators = [FlowLogger.wrapWithLogging({
124
+ eventType: "StagehandObserve",
125
+ eventIdSuffix: "4",
126
+ })];
117
127
  __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);
118
128
  __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);
119
129
  __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);
@@ -198,6 +208,8 @@ let V3 = (() => {
198
208
  stagehandLogger;
199
209
  _history = [];
200
210
  instanceId;
211
+ sessionId;
212
+ flowLoggerContext;
201
213
  static _processGuardsInstalled = false;
202
214
  static _instances = new Set();
203
215
  cacheStorage;
@@ -206,6 +218,10 @@ let V3 = (() => {
206
218
  apiClient = null;
207
219
  keepAlive;
208
220
  shutdownSupervisor = null;
221
+ detachEventStoreListener = null;
222
+ withLoggingContext(fn) {
223
+ return withInstanceLogContext(this.instanceId, fn);
224
+ }
209
225
  stagehandMetrics = {
210
226
  actPromptTokens: 0,
211
227
  actCompletionTokens: 0,
@@ -237,6 +253,7 @@ let V3 = (() => {
237
253
  this.externalLogger = opts.logger;
238
254
  this.verbose = opts.verbose ?? 1;
239
255
  this.instanceId = uuidv7();
256
+ this.sessionId = opts.sessionId ?? this.instanceId;
240
257
  this.keepAlive =
241
258
  opts.keepAlive ?? opts.browserbaseSessionCreateParams?.keepAlive;
242
259
  // Create per-instance StagehandLogger (handles usePino, verbose, externalLogger)
@@ -329,8 +346,9 @@ let V3 = (() => {
329
346
  act: this.act.bind(this),
330
347
  });
331
348
  this.opts = opts;
332
- // Initialize session file logger
333
- SessionFileLogger.init(this.instanceId, opts);
349
+ void getEventStore().initializeSession(this.sessionId, opts);
350
+ this.flowLoggerContext = FlowLogger.init(this.sessionId, this.bus);
351
+ this.detachEventStoreListener = getEventStore().attachBus(this.sessionId, this.bus);
334
352
  // Track instance for global process guard handling
335
353
  V3._instances.add(this);
336
354
  }
@@ -537,7 +555,7 @@ let V3 = (() => {
537
555
  */
538
556
  async init() {
539
557
  try {
540
- return await withInstanceLogContext(this.instanceId, async () => {
558
+ return await this.withLoggingContext(async () => {
541
559
  this.actHandler = new 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);
542
560
  this.extractHandler = new 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));
543
561
  this.observeHandler = new 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));
@@ -571,9 +589,7 @@ let V3 = (() => {
571
589
  env: "LOCAL",
572
590
  cdpHeaders: lbo.cdpHeaders,
573
591
  });
574
- const logCtx = SessionFileLogger.getContext();
575
- this.ctx.conn.cdpLogger = (info) => SessionFileLogger.logCdpCallEvent(info, logCtx);
576
- this.ctx.conn.cdpEventLogger = (info) => SessionFileLogger.logCdpMessageEvent(info, logCtx);
592
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
577
593
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
578
594
  this.state = {
579
595
  kind: "LOCAL",
@@ -669,9 +685,7 @@ let V3 = (() => {
669
685
  env: "LOCAL",
670
686
  localBrowserLaunchOptions: lbo,
671
687
  });
672
- const logCtx = SessionFileLogger.getContext();
673
- this.ctx.conn.cdpLogger = (info) => SessionFileLogger.logCdpCallEvent(info, logCtx);
674
- this.ctx.conn.cdpEventLogger = (info) => SessionFileLogger.logCdpMessageEvent(info, logCtx);
688
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
675
689
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
676
690
  this.state = {
677
691
  kind: "LOCAL",
@@ -753,9 +767,7 @@ let V3 = (() => {
753
767
  env: "BROWSERBASE",
754
768
  apiClient: this.apiClient,
755
769
  });
756
- const logCtx = SessionFileLogger.getContext();
757
- this.ctx.conn.cdpLogger = (info) => SessionFileLogger.logCdpCallEvent(info, logCtx);
758
- this.ctx.conn.cdpEventLogger = (info) => SessionFileLogger.logCdpMessageEvent(info, logCtx);
770
+ this.ctx.conn.flowLoggerContext = this.flowLoggerContext;
759
771
  this.ctx.conn.onTransportClosed(this._onCdpClosed);
760
772
  this.state = { kind: "BROWSERBASE", sessionId, ws, bb };
761
773
  this.browserbaseSessionId = sessionId;
@@ -817,6 +829,13 @@ let V3 = (() => {
817
829
  // ignore cleanup errors
818
830
  }
819
831
  }
832
+ try {
833
+ this.detachEventStoreListener?.();
834
+ this.detachEventStoreListener = null;
835
+ }
836
+ catch {
837
+ // ignore cleanup errors
838
+ }
820
839
  throw error;
821
840
  }
822
841
  }
@@ -860,7 +879,7 @@ let V3 = (() => {
860
879
  this.browserbaseDebugUrl = undefined;
861
880
  }
862
881
  async act(input, options) {
863
- return await withInstanceLogContext(this.instanceId, async () => {
882
+ return await this.withLoggingContext(async () => {
864
883
  if (!this.actHandler)
865
884
  throw new StagehandNotInitializedError("act()");
866
885
  let actResult;
@@ -944,7 +963,7 @@ let V3 = (() => {
944
963
  });
945
964
  }
946
965
  async extract(a, b, c) {
947
- return await withInstanceLogContext(this.instanceId, async () => {
966
+ return await this.withLoggingContext(async () => {
948
967
  if (!this.extractHandler) {
949
968
  throw new StagehandNotInitializedError("extract()");
950
969
  }
@@ -1012,7 +1031,7 @@ let V3 = (() => {
1012
1031
  });
1013
1032
  }
1014
1033
  async observe(a, b) {
1015
- return await withInstanceLogContext(this.instanceId, async () => {
1034
+ return await this.withLoggingContext(async () => {
1016
1035
  if (!this.observeHandler) {
1017
1036
  throw new StagehandNotInitializedError("observe()");
1018
1037
  }
@@ -1098,7 +1117,7 @@ let V3 = (() => {
1098
1117
  try {
1099
1118
  // Close session file logger
1100
1119
  try {
1101
- await SessionFileLogger.close();
1120
+ await FlowLogger.close(this.flowLoggerContext);
1102
1121
  }
1103
1122
  catch {
1104
1123
  // ignore
@@ -1134,6 +1153,13 @@ let V3 = (() => {
1134
1153
  catch {
1135
1154
  // ignore
1136
1155
  }
1156
+ try {
1157
+ this.detachEventStoreListener?.();
1158
+ this.detachEventStoreListener = null;
1159
+ }
1160
+ catch {
1161
+ // ignore
1162
+ }
1137
1163
  try {
1138
1164
  this.bus.removeAllListeners();
1139
1165
  }
@@ -1147,6 +1173,13 @@ let V3 = (() => {
1147
1173
  V3._instances.delete(this);
1148
1174
  }
1149
1175
  }
1176
+ /**
1177
+ * Resolves the Browserbase API key from options or environment variables.
1178
+ * Returns undefined if no key is found (does not throw).
1179
+ */
1180
+ get browserbaseApiKey() {
1181
+ return this.opts.apiKey || process.env.BROWSERBASE_API_KEY;
1182
+ }
1150
1183
  /** Guard: ensure Browserbase credentials exist in options. */
1151
1184
  requireBrowserbaseCreds() {
1152
1185
  let { apiKey, projectId } = this.opts;
@@ -1393,152 +1426,59 @@ let V3 = (() => {
1393
1426
  throw new CuaModelRequiredError(AVAILABLE_CUA_MODELS);
1394
1427
  }
1395
1428
  const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1396
- return {
1397
- execute: async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1398
- validateExperimentalFeatures({
1399
- isExperimental: this.experimental,
1400
- agentConfig: options,
1401
- executeOptions: typeof instructionOrOptions === "object"
1402
- ? instructionOrOptions
1403
- : null,
1404
- });
1405
- SessionFileLogger.logAgentTaskStarted({
1406
- invocation: "Agent.execute",
1407
- args: [instructionOrOptions],
1408
- });
1409
- const tools = options?.integrations
1410
- ? await resolveTools(options.integrations, options.tools)
1411
- : (options?.tools ?? {});
1412
- const handler = new V3CuaAgentHandler(this, this.logger, {
1413
- modelName,
1414
- clientOptions,
1415
- userProvidedInstructions: options.systemPrompt ??
1416
- `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1417
- }, tools);
1418
- const resolvedOptions = typeof instructionOrOptions === "string"
1419
- ? {
1420
- instruction: instructionOrOptions,
1421
- toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1422
- }
1423
- : {
1424
- ...instructionOrOptions,
1425
- toolTimeout: instructionOrOptions.toolTimeout ??
1426
- DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1427
- };
1428
- if (resolvedOptions.page) {
1429
- const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1430
- this.ctx.setActivePage(normalizedPage);
1431
- }
1432
- const instruction = resolvedOptions.instruction.trim();
1433
- const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1434
- const cacheVariables = flattenVariables(resolvedOptions.variables);
1435
- let cacheContext = null;
1436
- if (this.agentCache.shouldAttemptCache(instruction)) {
1437
- const startPage = await this.ctx.awaitActivePage();
1438
- cacheContext = await this.agentCache.prepareContext({
1439
- instruction,
1440
- options: sanitizedOptions,
1441
- configSignature: agentConfigSignature,
1442
- page: startPage,
1443
- variables: cacheVariables,
1444
- });
1445
- if (cacheContext) {
1446
- const replayed = await this.agentCache.tryReplay(cacheContext);
1447
- if (replayed) {
1448
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1449
- return replayed;
1450
- }
1451
- }
1452
- }
1453
- let agentSteps = [];
1454
- const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1455
- if (shouldRecordLocally) {
1456
- this.beginAgentReplayRecording();
1457
- }
1458
- let result;
1459
- try {
1460
- if (this.apiClient && !this.experimental) {
1461
- const page = await this.ctx.awaitActivePage();
1462
- result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1463
- if (cacheContext) {
1464
- const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1465
- await this.agentCache.storeTransferredEntry(transferredEntry);
1466
- }
1467
- }
1468
- else {
1469
- result = await handler.execute(instructionOrOptions);
1470
- }
1471
- if (shouldRecordLocally) {
1472
- agentSteps = this.endAgentReplayRecording();
1473
- }
1474
- if (shouldRecordLocally &&
1475
- cacheContext &&
1476
- result.success &&
1477
- agentSteps.length > 0) {
1478
- await this.agentCache.store(cacheContext, agentSteps, result);
1479
- }
1480
- return result;
1481
- }
1482
- catch (err) {
1483
- if (shouldRecordLocally)
1484
- this.discardAgentReplayRecording();
1485
- throw err;
1486
- }
1487
- finally {
1488
- if (shouldRecordLocally) {
1489
- this.discardAgentReplayRecording();
1490
- }
1491
- SessionFileLogger.logAgentTaskCompleted();
1492
- }
1493
- }),
1494
- };
1495
- }
1496
- // Default: AISDK tools-based agent
1497
- const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1498
- const isStreaming = options?.stream ?? false;
1499
- return {
1500
- execute: async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
1429
+ const execute = FlowLogger.wrapWithLogging({
1430
+ eventType: "AgentExecute",
1431
+ eventIdSuffix: "3",
1432
+ })(async (instructionOrOptions) => this.withLoggingContext(async () => {
1501
1433
  validateExperimentalFeatures({
1502
1434
  isExperimental: this.experimental,
1503
1435
  agentConfig: options,
1504
1436
  executeOptions: typeof instructionOrOptions === "object"
1505
1437
  ? instructionOrOptions
1506
1438
  : null,
1507
- isStreaming,
1508
1439
  });
1509
- SessionFileLogger.logAgentTaskStarted({
1510
- invocation: "Agent.execute",
1511
- args: [instructionOrOptions],
1512
- });
1513
- // Streaming mode
1514
- if (isStreaming) {
1515
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1440
+ const tools = options?.integrations
1441
+ ? await resolveTools(options.integrations, options.tools)
1442
+ : (options?.tools ?? {});
1443
+ const handler = new V3CuaAgentHandler(this, this.logger, {
1444
+ modelName,
1445
+ clientOptions,
1446
+ userProvidedInstructions: options.systemPrompt ??
1447
+ `You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
1448
+ }, tools);
1449
+ const resolvedOptions = typeof instructionOrOptions === "string"
1450
+ ? {
1451
+ instruction: instructionOrOptions,
1452
+ toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1453
+ }
1454
+ : {
1455
+ ...instructionOrOptions,
1456
+ toolTimeout: instructionOrOptions.toolTimeout ??
1457
+ DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1458
+ };
1459
+ if (resolvedOptions.page) {
1460
+ const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
1461
+ this.ctx.setActivePage(normalizedPage);
1462
+ }
1463
+ const instruction = resolvedOptions.instruction.trim();
1464
+ const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
1465
+ const cacheVariables = flattenVariables(resolvedOptions.variables);
1466
+ let cacheContext = null;
1467
+ if (this.agentCache.shouldAttemptCache(instruction)) {
1468
+ const startPage = await this.ctx.awaitActivePage();
1469
+ cacheContext = await this.agentCache.prepareContext({
1470
+ instruction,
1471
+ options: sanitizedOptions,
1472
+ configSignature: agentConfigSignature,
1473
+ page: startPage,
1474
+ variables: cacheVariables,
1475
+ });
1516
1476
  if (cacheContext) {
1517
- const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1477
+ const replayed = await this.agentCache.tryReplay(cacheContext);
1518
1478
  if (replayed) {
1519
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1520
1479
  return replayed;
1521
1480
  }
1522
1481
  }
1523
- const streamResult = await handler.stream(resolvedOptions);
1524
- if (cacheContext) {
1525
- const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1526
- // Log completion when stream is returned (stream completes asynchronously)
1527
- SessionFileLogger.logAgentTaskCompleted();
1528
- return wrappedStream;
1529
- }
1530
- // Log completion when stream is returned (stream completes asynchronously)
1531
- SessionFileLogger.logAgentTaskCompleted();
1532
- return streamResult;
1533
- }
1534
- // Non-streaming mode (default)
1535
- const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1536
- if (cacheContext) {
1537
- const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1538
- if (replayed) {
1539
- SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
1540
- return replayed;
1541
- }
1542
1482
  }
1543
1483
  let agentSteps = [];
1544
1484
  const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
@@ -1549,14 +1489,14 @@ let V3 = (() => {
1549
1489
  try {
1550
1490
  if (this.apiClient && !this.experimental) {
1551
1491
  const page = await this.ctx.awaitActivePage();
1552
- result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1492
+ result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
1553
1493
  if (cacheContext) {
1554
1494
  const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1555
1495
  await this.agentCache.storeTransferredEntry(transferredEntry);
1556
1496
  }
1557
1497
  }
1558
1498
  else {
1559
- result = await handler.execute(resolvedOptions);
1499
+ result = await handler.execute(instructionOrOptions);
1560
1500
  }
1561
1501
  if (shouldRecordLocally) {
1562
1502
  agentSteps = this.endAgentReplayRecording();
@@ -1578,9 +1518,93 @@ let V3 = (() => {
1578
1518
  if (shouldRecordLocally) {
1579
1519
  this.discardAgentReplayRecording();
1580
1520
  }
1581
- SessionFileLogger.logAgentTaskCompleted();
1582
1521
  }
1583
- }),
1522
+ }));
1523
+ return {
1524
+ execute,
1525
+ };
1526
+ }
1527
+ // Default: AISDK tools-based agent
1528
+ const agentConfigSignature = this.agentCache.buildConfigSignature(options);
1529
+ const isStreaming = options?.stream ?? false;
1530
+ const execute = FlowLogger.wrapWithLogging({
1531
+ eventType: "AgentExecute",
1532
+ eventIdSuffix: "3",
1533
+ })(async (instructionOrOptions) => this.withLoggingContext(async () => {
1534
+ validateExperimentalFeatures({
1535
+ isExperimental: this.experimental,
1536
+ agentConfig: options,
1537
+ executeOptions: typeof instructionOrOptions === "object"
1538
+ ? instructionOrOptions
1539
+ : null,
1540
+ isStreaming,
1541
+ });
1542
+ // Streaming mode
1543
+ if (isStreaming) {
1544
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1545
+ if (cacheContext) {
1546
+ const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
1547
+ if (replayed) {
1548
+ return replayed;
1549
+ }
1550
+ }
1551
+ const streamResult = await handler.stream(resolvedOptions);
1552
+ if (cacheContext) {
1553
+ const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
1554
+ return wrappedStream;
1555
+ }
1556
+ return streamResult;
1557
+ }
1558
+ // Non-streaming mode (default)
1559
+ const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
1560
+ if (cacheContext) {
1561
+ const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
1562
+ if (replayed) {
1563
+ return replayed;
1564
+ }
1565
+ }
1566
+ let agentSteps = [];
1567
+ const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
1568
+ if (shouldRecordLocally) {
1569
+ this.beginAgentReplayRecording();
1570
+ }
1571
+ let result;
1572
+ try {
1573
+ if (this.apiClient && !this.experimental) {
1574
+ const page = await this.ctx.awaitActivePage();
1575
+ result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
1576
+ if (cacheContext) {
1577
+ const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
1578
+ await this.agentCache.storeTransferredEntry(transferredEntry);
1579
+ }
1580
+ }
1581
+ else {
1582
+ result = await handler.execute(resolvedOptions);
1583
+ }
1584
+ if (shouldRecordLocally) {
1585
+ agentSteps = this.endAgentReplayRecording();
1586
+ }
1587
+ if (shouldRecordLocally &&
1588
+ cacheContext &&
1589
+ result.success &&
1590
+ agentSteps.length > 0) {
1591
+ await this.agentCache.store(cacheContext, agentSteps, result);
1592
+ }
1593
+ return result;
1594
+ }
1595
+ catch (err) {
1596
+ if (shouldRecordLocally)
1597
+ this.discardAgentReplayRecording();
1598
+ throw err;
1599
+ }
1600
+ finally {
1601
+ if (shouldRecordLocally) {
1602
+ this.discardAgentReplayRecording();
1603
+ }
1604
+ }
1605
+ }));
1606
+ return {
1607
+ execute,
1584
1608
  };
1585
1609
  }
1586
1610
  };