@browserbasehq/orca 3.2.0-preview.2 → 3.2.0-preview.3
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.
- package/dist/cjs/lib/utils.d.ts +0 -1
- package/dist/cjs/lib/utils.js +0 -4
- package/dist/cjs/lib/utils.js.map +1 -1
- package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +6 -4
- package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
- package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +6 -4
- package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
- package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +6 -4
- package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
- package/dist/cjs/lib/v3/flowLogger.d.ts +103 -62
- package/dist/cjs/lib/v3/flowLogger.js +773 -362
- package/dist/cjs/lib/v3/flowLogger.js.map +1 -1
- package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js +33 -21
- package/dist/cjs/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
- package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +4 -0
- package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +31 -1
- package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
- package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +12 -10
- package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
- package/dist/cjs/lib/v3/llm/aisdk.js +16 -10
- package/dist/cjs/lib/v3/llm/aisdk.js.map +1 -1
- package/dist/cjs/lib/v3/types/public/options.d.ts +0 -5
- package/dist/cjs/lib/v3/types/public/options.js.map +1 -1
- package/dist/cjs/lib/v3/understudy/cdp.d.ts +12 -3
- package/dist/cjs/lib/v3/understudy/cdp.js +10 -83
- package/dist/cjs/lib/v3/understudy/cdp.js.map +1 -1
- package/dist/cjs/lib/v3/understudy/page.js +17 -32
- package/dist/cjs/lib/v3/understudy/page.js.map +1 -1
- package/dist/cjs/lib/v3/v3.d.ts +0 -5
- package/dist/cjs/lib/v3/v3.js +157 -174
- package/dist/cjs/lib/v3/v3.js.map +1 -1
- package/dist/esm/lib/utils.d.ts +0 -1
- package/dist/esm/lib/utils.js +0 -3
- package/dist/esm/lib/utils.js.map +1 -1
- package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +7 -5
- package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
- package/dist/esm/lib/v3/agent/GoogleCUAClient.js +7 -5
- package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
- package/dist/esm/lib/v3/agent/OpenAICUAClient.js +7 -5
- package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
- package/dist/esm/lib/v3/flowLogger.d.ts +103 -62
- package/dist/esm/lib/v3/flowLogger.js +762 -356
- package/dist/esm/lib/v3/flowLogger.js.map +1 -1
- package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js +34 -22
- package/dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js.map +1 -1
- package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +4 -0
- package/dist/esm/lib/v3/handlers/v3AgentHandler.js +32 -2
- package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
- package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +13 -11
- package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
- package/dist/esm/lib/v3/llm/aisdk.js +17 -11
- package/dist/esm/lib/v3/llm/aisdk.js.map +1 -1
- package/dist/esm/lib/v3/types/public/options.d.ts +0 -5
- package/dist/esm/lib/v3/types/public/options.js.map +1 -1
- package/dist/esm/lib/v3/understudy/cdp.d.ts +12 -3
- package/dist/esm/lib/v3/understudy/cdp.js +10 -83
- package/dist/esm/lib/v3/understudy/cdp.js.map +1 -1
- package/dist/esm/lib/v3/understudy/page.js +18 -33
- package/dist/esm/lib/v3/understudy/page.js.map +1 -1
- package/dist/esm/lib/v3/v3.d.ts +0 -5
- package/dist/esm/lib/v3/v3.js +158 -175
- package/dist/esm/lib/v3/v3.js.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/lib/v3/eventStore.d.ts +0 -41
- package/dist/cjs/lib/v3/eventStore.js +0 -375
- package/dist/cjs/lib/v3/eventStore.js.map +0 -1
- package/dist/esm/lib/v3/eventStore.d.ts +0 -41
- package/dist/esm/lib/v3/eventStore.js +0 -363
- package/dist/esm/lib/v3/eventStore.js.map +0 -1
package/dist/esm/lib/v3/v3.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ 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";
|
|
9
8
|
/**
|
|
10
9
|
* V3
|
|
11
10
|
*
|
|
@@ -71,8 +70,6 @@ export declare class V3 {
|
|
|
71
70
|
private stagehandLogger;
|
|
72
71
|
private _history;
|
|
73
72
|
private readonly instanceId;
|
|
74
|
-
private readonly sessionId;
|
|
75
|
-
readonly flowLoggerContext: FlowLoggerContext;
|
|
76
73
|
private static _processGuardsInstalled;
|
|
77
74
|
private static _instances;
|
|
78
75
|
private cacheStorage;
|
|
@@ -81,8 +78,6 @@ export declare class V3 {
|
|
|
81
78
|
private apiClient;
|
|
82
79
|
private keepAlive?;
|
|
83
80
|
private shutdownSupervisor;
|
|
84
|
-
private detachEventStoreListener;
|
|
85
|
-
private withLoggingContext;
|
|
86
81
|
stagehandMetrics: StagehandMetrics;
|
|
87
82
|
constructor(opts: V3Options);
|
|
88
83
|
/**
|
package/dist/esm/lib/v3/v3.js
CHANGED
|
@@ -63,8 +63,7 @@ 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 {
|
|
67
|
-
import { getEventStore } from "./eventStore.js";
|
|
66
|
+
import { SessionFileLogger, logStagehandStep } from "./flowLogger.js";
|
|
68
67
|
import { createTimeoutGuard } from "./handlers/handlerUtils/timeoutGuard.js";
|
|
69
68
|
import { ActTimeoutError } from "./types/public/sdkErrors.js";
|
|
70
69
|
const DEFAULT_MODEL_NAME = "openai/gpt-4.1-mini";
|
|
@@ -112,18 +111,9 @@ let V3 = (() => {
|
|
|
112
111
|
return class V3 {
|
|
113
112
|
static {
|
|
114
113
|
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
115
|
-
_act_decorators = [
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
})];
|
|
114
|
+
_act_decorators = [logStagehandStep("Stagehand.act", "ACT")];
|
|
115
|
+
_extract_decorators = [logStagehandStep("Stagehand.extract", "EXTRACT")];
|
|
116
|
+
_observe_decorators = [logStagehandStep("Stagehand.observe", "OBSERVE")];
|
|
127
117
|
__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);
|
|
128
118
|
__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);
|
|
129
119
|
__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);
|
|
@@ -208,8 +198,6 @@ let V3 = (() => {
|
|
|
208
198
|
stagehandLogger;
|
|
209
199
|
_history = [];
|
|
210
200
|
instanceId;
|
|
211
|
-
sessionId;
|
|
212
|
-
flowLoggerContext;
|
|
213
201
|
static _processGuardsInstalled = false;
|
|
214
202
|
static _instances = new Set();
|
|
215
203
|
cacheStorage;
|
|
@@ -218,10 +206,6 @@ let V3 = (() => {
|
|
|
218
206
|
apiClient = null;
|
|
219
207
|
keepAlive;
|
|
220
208
|
shutdownSupervisor = null;
|
|
221
|
-
detachEventStoreListener = null;
|
|
222
|
-
withLoggingContext(fn) {
|
|
223
|
-
return withInstanceLogContext(this.instanceId, fn);
|
|
224
|
-
}
|
|
225
209
|
stagehandMetrics = {
|
|
226
210
|
actPromptTokens: 0,
|
|
227
211
|
actCompletionTokens: 0,
|
|
@@ -253,7 +237,6 @@ let V3 = (() => {
|
|
|
253
237
|
this.externalLogger = opts.logger;
|
|
254
238
|
this.verbose = opts.verbose ?? 1;
|
|
255
239
|
this.instanceId = uuidv7();
|
|
256
|
-
this.sessionId = opts.sessionId ?? this.instanceId;
|
|
257
240
|
this.keepAlive =
|
|
258
241
|
opts.keepAlive ?? opts.browserbaseSessionCreateParams?.keepAlive;
|
|
259
242
|
// Create per-instance StagehandLogger (handles usePino, verbose, externalLogger)
|
|
@@ -346,9 +329,8 @@ let V3 = (() => {
|
|
|
346
329
|
act: this.act.bind(this),
|
|
347
330
|
});
|
|
348
331
|
this.opts = opts;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
this.detachEventStoreListener = getEventStore().attachBus(this.sessionId, this.bus);
|
|
332
|
+
// Initialize session file logger
|
|
333
|
+
SessionFileLogger.init(this.instanceId, opts);
|
|
352
334
|
// Track instance for global process guard handling
|
|
353
335
|
V3._instances.add(this);
|
|
354
336
|
}
|
|
@@ -555,7 +537,7 @@ let V3 = (() => {
|
|
|
555
537
|
*/
|
|
556
538
|
async init() {
|
|
557
539
|
try {
|
|
558
|
-
return await this.
|
|
540
|
+
return await withInstanceLogContext(this.instanceId, async () => {
|
|
559
541
|
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);
|
|
560
542
|
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));
|
|
561
543
|
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));
|
|
@@ -589,7 +571,9 @@ let V3 = (() => {
|
|
|
589
571
|
env: "LOCAL",
|
|
590
572
|
cdpHeaders: lbo.cdpHeaders,
|
|
591
573
|
});
|
|
592
|
-
|
|
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);
|
|
593
577
|
this.ctx.conn.onTransportClosed(this._onCdpClosed);
|
|
594
578
|
this.state = {
|
|
595
579
|
kind: "LOCAL",
|
|
@@ -685,7 +669,9 @@ let V3 = (() => {
|
|
|
685
669
|
env: "LOCAL",
|
|
686
670
|
localBrowserLaunchOptions: lbo,
|
|
687
671
|
});
|
|
688
|
-
|
|
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);
|
|
689
675
|
this.ctx.conn.onTransportClosed(this._onCdpClosed);
|
|
690
676
|
this.state = {
|
|
691
677
|
kind: "LOCAL",
|
|
@@ -767,7 +753,9 @@ let V3 = (() => {
|
|
|
767
753
|
env: "BROWSERBASE",
|
|
768
754
|
apiClient: this.apiClient,
|
|
769
755
|
});
|
|
770
|
-
|
|
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);
|
|
771
759
|
this.ctx.conn.onTransportClosed(this._onCdpClosed);
|
|
772
760
|
this.state = { kind: "BROWSERBASE", sessionId, ws, bb };
|
|
773
761
|
this.browserbaseSessionId = sessionId;
|
|
@@ -829,13 +817,6 @@ let V3 = (() => {
|
|
|
829
817
|
// ignore cleanup errors
|
|
830
818
|
}
|
|
831
819
|
}
|
|
832
|
-
try {
|
|
833
|
-
this.detachEventStoreListener?.();
|
|
834
|
-
this.detachEventStoreListener = null;
|
|
835
|
-
}
|
|
836
|
-
catch {
|
|
837
|
-
// ignore cleanup errors
|
|
838
|
-
}
|
|
839
820
|
throw error;
|
|
840
821
|
}
|
|
841
822
|
}
|
|
@@ -879,7 +860,7 @@ let V3 = (() => {
|
|
|
879
860
|
this.browserbaseDebugUrl = undefined;
|
|
880
861
|
}
|
|
881
862
|
async act(input, options) {
|
|
882
|
-
return await this.
|
|
863
|
+
return await withInstanceLogContext(this.instanceId, async () => {
|
|
883
864
|
if (!this.actHandler)
|
|
884
865
|
throw new StagehandNotInitializedError("act()");
|
|
885
866
|
let actResult;
|
|
@@ -963,7 +944,7 @@ let V3 = (() => {
|
|
|
963
944
|
});
|
|
964
945
|
}
|
|
965
946
|
async extract(a, b, c) {
|
|
966
|
-
return await this.
|
|
947
|
+
return await withInstanceLogContext(this.instanceId, async () => {
|
|
967
948
|
if (!this.extractHandler) {
|
|
968
949
|
throw new StagehandNotInitializedError("extract()");
|
|
969
950
|
}
|
|
@@ -1031,7 +1012,7 @@ let V3 = (() => {
|
|
|
1031
1012
|
});
|
|
1032
1013
|
}
|
|
1033
1014
|
async observe(a, b) {
|
|
1034
|
-
return await this.
|
|
1015
|
+
return await withInstanceLogContext(this.instanceId, async () => {
|
|
1035
1016
|
if (!this.observeHandler) {
|
|
1036
1017
|
throw new StagehandNotInitializedError("observe()");
|
|
1037
1018
|
}
|
|
@@ -1117,7 +1098,7 @@ let V3 = (() => {
|
|
|
1117
1098
|
try {
|
|
1118
1099
|
// Close session file logger
|
|
1119
1100
|
try {
|
|
1120
|
-
await
|
|
1101
|
+
await SessionFileLogger.close();
|
|
1121
1102
|
}
|
|
1122
1103
|
catch {
|
|
1123
1104
|
// ignore
|
|
@@ -1153,13 +1134,6 @@ let V3 = (() => {
|
|
|
1153
1134
|
catch {
|
|
1154
1135
|
// ignore
|
|
1155
1136
|
}
|
|
1156
|
-
try {
|
|
1157
|
-
this.detachEventStoreListener?.();
|
|
1158
|
-
this.detachEventStoreListener = null;
|
|
1159
|
-
}
|
|
1160
|
-
catch {
|
|
1161
|
-
// ignore
|
|
1162
|
-
}
|
|
1163
1137
|
try {
|
|
1164
1138
|
this.bus.removeAllListeners();
|
|
1165
1139
|
}
|
|
@@ -1426,59 +1400,152 @@ let V3 = (() => {
|
|
|
1426
1400
|
throw new CuaModelRequiredError(AVAILABLE_CUA_MODELS);
|
|
1427
1401
|
}
|
|
1428
1402
|
const agentConfigSignature = this.agentCache.buildConfigSignature(options);
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1403
|
+
return {
|
|
1404
|
+
execute: async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
|
|
1405
|
+
validateExperimentalFeatures({
|
|
1406
|
+
isExperimental: this.experimental,
|
|
1407
|
+
agentConfig: options,
|
|
1408
|
+
executeOptions: typeof instructionOrOptions === "object"
|
|
1409
|
+
? instructionOrOptions
|
|
1410
|
+
: null,
|
|
1411
|
+
});
|
|
1412
|
+
SessionFileLogger.logAgentTaskStarted({
|
|
1413
|
+
invocation: "Agent.execute",
|
|
1414
|
+
args: [instructionOrOptions],
|
|
1415
|
+
});
|
|
1416
|
+
const tools = options?.integrations
|
|
1417
|
+
? await resolveTools(options.integrations, options.tools)
|
|
1418
|
+
: (options?.tools ?? {});
|
|
1419
|
+
const handler = new V3CuaAgentHandler(this, this.logger, {
|
|
1420
|
+
modelName,
|
|
1421
|
+
clientOptions,
|
|
1422
|
+
userProvidedInstructions: options.systemPrompt ??
|
|
1423
|
+
`You are a helpful assistant that can use a web browser.\nDo not ask follow up questions, the user will trust your judgement.`,
|
|
1424
|
+
}, tools);
|
|
1425
|
+
const resolvedOptions = typeof instructionOrOptions === "string"
|
|
1426
|
+
? {
|
|
1427
|
+
instruction: instructionOrOptions,
|
|
1428
|
+
toolTimeout: DEFAULT_AGENT_TOOL_TIMEOUT_MS,
|
|
1429
|
+
}
|
|
1430
|
+
: {
|
|
1431
|
+
...instructionOrOptions,
|
|
1432
|
+
toolTimeout: instructionOrOptions.toolTimeout ??
|
|
1433
|
+
DEFAULT_AGENT_TOOL_TIMEOUT_MS,
|
|
1434
|
+
};
|
|
1435
|
+
if (resolvedOptions.page) {
|
|
1436
|
+
const normalizedPage = await this.normalizeToV3Page(resolvedOptions.page);
|
|
1437
|
+
this.ctx.setActivePage(normalizedPage);
|
|
1438
|
+
}
|
|
1439
|
+
const instruction = resolvedOptions.instruction.trim();
|
|
1440
|
+
const sanitizedOptions = this.agentCache.sanitizeExecuteOptions(resolvedOptions);
|
|
1441
|
+
const cacheVariables = flattenVariables(resolvedOptions.variables);
|
|
1442
|
+
let cacheContext = null;
|
|
1443
|
+
if (this.agentCache.shouldAttemptCache(instruction)) {
|
|
1444
|
+
const startPage = await this.ctx.awaitActivePage();
|
|
1445
|
+
cacheContext = await this.agentCache.prepareContext({
|
|
1446
|
+
instruction,
|
|
1447
|
+
options: sanitizedOptions,
|
|
1448
|
+
configSignature: agentConfigSignature,
|
|
1449
|
+
page: startPage,
|
|
1450
|
+
variables: cacheVariables,
|
|
1451
|
+
});
|
|
1452
|
+
if (cacheContext) {
|
|
1453
|
+
const replayed = await this.agentCache.tryReplay(cacheContext);
|
|
1454
|
+
if (replayed) {
|
|
1455
|
+
SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
|
|
1456
|
+
return replayed;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
let agentSteps = [];
|
|
1461
|
+
const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
|
|
1462
|
+
if (shouldRecordLocally) {
|
|
1463
|
+
this.beginAgentReplayRecording();
|
|
1464
|
+
}
|
|
1465
|
+
let result;
|
|
1466
|
+
try {
|
|
1467
|
+
if (this.apiClient && !this.experimental) {
|
|
1468
|
+
const page = await this.ctx.awaitActivePage();
|
|
1469
|
+
result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
|
|
1470
|
+
if (cacheContext) {
|
|
1471
|
+
const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
|
|
1472
|
+
await this.agentCache.storeTransferredEntry(transferredEntry);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
else {
|
|
1476
|
+
result = await handler.execute(instructionOrOptions);
|
|
1477
|
+
}
|
|
1478
|
+
if (shouldRecordLocally) {
|
|
1479
|
+
agentSteps = this.endAgentReplayRecording();
|
|
1480
|
+
}
|
|
1481
|
+
if (shouldRecordLocally &&
|
|
1482
|
+
cacheContext &&
|
|
1483
|
+
result.success &&
|
|
1484
|
+
agentSteps.length > 0) {
|
|
1485
|
+
await this.agentCache.store(cacheContext, agentSteps, result);
|
|
1486
|
+
}
|
|
1487
|
+
return result;
|
|
1488
|
+
}
|
|
1489
|
+
catch (err) {
|
|
1490
|
+
if (shouldRecordLocally)
|
|
1491
|
+
this.discardAgentReplayRecording();
|
|
1492
|
+
throw err;
|
|
1493
|
+
}
|
|
1494
|
+
finally {
|
|
1495
|
+
if (shouldRecordLocally) {
|
|
1496
|
+
this.discardAgentReplayRecording();
|
|
1497
|
+
}
|
|
1498
|
+
SessionFileLogger.logAgentTaskCompleted();
|
|
1499
|
+
}
|
|
1500
|
+
}),
|
|
1501
|
+
};
|
|
1502
|
+
}
|
|
1503
|
+
// Default: AISDK tools-based agent
|
|
1504
|
+
const agentConfigSignature = this.agentCache.buildConfigSignature(options);
|
|
1505
|
+
const isStreaming = options?.stream ?? false;
|
|
1506
|
+
return {
|
|
1507
|
+
execute: async (instructionOrOptions) => withInstanceLogContext(this.instanceId, async () => {
|
|
1433
1508
|
validateExperimentalFeatures({
|
|
1434
1509
|
isExperimental: this.experimental,
|
|
1435
1510
|
agentConfig: options,
|
|
1436
1511
|
executeOptions: typeof instructionOrOptions === "object"
|
|
1437
1512
|
? instructionOrOptions
|
|
1438
1513
|
: null,
|
|
1514
|
+
isStreaming,
|
|
1439
1515
|
});
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
:
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
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
|
+
SessionFileLogger.logAgentTaskStarted({
|
|
1517
|
+
invocation: "Agent.execute",
|
|
1518
|
+
args: [instructionOrOptions],
|
|
1519
|
+
});
|
|
1520
|
+
// Streaming mode
|
|
1521
|
+
if (isStreaming) {
|
|
1522
|
+
const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
|
|
1476
1523
|
if (cacheContext) {
|
|
1477
|
-
const replayed = await this.agentCache.
|
|
1524
|
+
const replayed = await this.agentCache.tryReplayAsStream(cacheContext, llmClient);
|
|
1478
1525
|
if (replayed) {
|
|
1526
|
+
SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
|
|
1479
1527
|
return replayed;
|
|
1480
1528
|
}
|
|
1481
1529
|
}
|
|
1530
|
+
const streamResult = await handler.stream(resolvedOptions);
|
|
1531
|
+
if (cacheContext) {
|
|
1532
|
+
const wrappedStream = this.agentCache.wrapStreamForCaching(cacheContext, streamResult, () => this.beginAgentReplayRecording(), () => this.endAgentReplayRecording(), () => this.discardAgentReplayRecording());
|
|
1533
|
+
// Log completion when stream is returned (stream completes asynchronously)
|
|
1534
|
+
SessionFileLogger.logAgentTaskCompleted();
|
|
1535
|
+
return wrappedStream;
|
|
1536
|
+
}
|
|
1537
|
+
// Log completion when stream is returned (stream completes asynchronously)
|
|
1538
|
+
SessionFileLogger.logAgentTaskCompleted();
|
|
1539
|
+
return streamResult;
|
|
1540
|
+
}
|
|
1541
|
+
// Non-streaming mode (default)
|
|
1542
|
+
const { handler, resolvedOptions, cacheContext, llmClient } = await this.prepareAgentExecution(options, instructionOrOptions, agentConfigSignature);
|
|
1543
|
+
if (cacheContext) {
|
|
1544
|
+
const replayed = await this.agentCache.tryReplay(cacheContext, llmClient);
|
|
1545
|
+
if (replayed) {
|
|
1546
|
+
SessionFileLogger.logAgentTaskCompleted({ cacheHit: true });
|
|
1547
|
+
return replayed;
|
|
1548
|
+
}
|
|
1482
1549
|
}
|
|
1483
1550
|
let agentSteps = [];
|
|
1484
1551
|
const shouldRecordLocally = !!cacheContext && (!this.apiClient || this.experimental);
|
|
@@ -1489,14 +1556,14 @@ let V3 = (() => {
|
|
|
1489
1556
|
try {
|
|
1490
1557
|
if (this.apiClient && !this.experimental) {
|
|
1491
1558
|
const page = await this.ctx.awaitActivePage();
|
|
1492
|
-
result = await this.apiClient.agentExecute(options, resolvedOptions, page.mainFrameId(), !!cacheContext);
|
|
1559
|
+
result = await this.apiClient.agentExecute(options ?? {}, resolvedOptions, page.mainFrameId(), !!cacheContext);
|
|
1493
1560
|
if (cacheContext) {
|
|
1494
1561
|
const transferredEntry = this.apiClient.consumeLatestAgentCacheEntry();
|
|
1495
1562
|
await this.agentCache.storeTransferredEntry(transferredEntry);
|
|
1496
1563
|
}
|
|
1497
1564
|
}
|
|
1498
1565
|
else {
|
|
1499
|
-
result = await handler.execute(
|
|
1566
|
+
result = await handler.execute(resolvedOptions);
|
|
1500
1567
|
}
|
|
1501
1568
|
if (shouldRecordLocally) {
|
|
1502
1569
|
agentSteps = this.endAgentReplayRecording();
|
|
@@ -1518,93 +1585,9 @@ let V3 = (() => {
|
|
|
1518
1585
|
if (shouldRecordLocally) {
|
|
1519
1586
|
this.discardAgentReplayRecording();
|
|
1520
1587
|
}
|
|
1588
|
+
SessionFileLogger.logAgentTaskCompleted();
|
|
1521
1589
|
}
|
|
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,
|
|
1590
|
+
}),
|
|
1608
1591
|
};
|
|
1609
1592
|
}
|
|
1610
1593
|
};
|