@browserbasehq/stagehand 3.0.8-alpha-3d5af07f66d6d26d1f5ac4bd9be7183c3381dd92 → 3.0.8-alpha-16d72fb4c4081dd33bf45605d75c27644ea4c00e

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 (3) hide show
  1. package/dist/index.d.ts +65 -58
  2. package/dist/index.js +390 -108
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -3403,6 +3403,63 @@ interface StreamingAgentInstance {
3403
3403
  interface NonStreamingAgentInstance {
3404
3404
  execute: (instructionOrOptions: string | AgentExecuteOptions) => Promise<AgentResult>;
3405
3405
  }
3406
+ /**
3407
+ * Content item type for toModelOutput return values.
3408
+ * Used in tool definitions to return text and/or media to the model.
3409
+ */
3410
+ type ModelOutputContentItem = {
3411
+ type: "text";
3412
+ text: string;
3413
+ } | {
3414
+ type: "media";
3415
+ mediaType: string;
3416
+ data: string;
3417
+ };
3418
+ interface ClickToolResult {
3419
+ success: boolean;
3420
+ describe?: string;
3421
+ coordinates?: number[];
3422
+ error?: string;
3423
+ screenshotBase64?: string;
3424
+ }
3425
+ interface TypeToolResult {
3426
+ success: boolean;
3427
+ describe?: string;
3428
+ text?: string;
3429
+ error?: string;
3430
+ screenshotBase64?: string;
3431
+ }
3432
+ interface DragAndDropToolResult {
3433
+ success: boolean;
3434
+ describe?: string;
3435
+ error?: string;
3436
+ screenshotBase64?: string;
3437
+ }
3438
+ interface FillFormField {
3439
+ action: string;
3440
+ value: string;
3441
+ coordinates: {
3442
+ x: number;
3443
+ y: number;
3444
+ };
3445
+ }
3446
+ interface FillFormVisionToolResult {
3447
+ success: boolean;
3448
+ playwrightArguments?: FillFormField[];
3449
+ error?: string;
3450
+ screenshotBase64?: string;
3451
+ }
3452
+ interface ScrollVisionToolResult {
3453
+ success: boolean;
3454
+ message: string;
3455
+ scrolledPixels: number;
3456
+ screenshotBase64?: string;
3457
+ }
3458
+ interface WaitToolResult {
3459
+ success: boolean;
3460
+ waited: number;
3461
+ screenshotBase64?: string;
3462
+ }
3406
3463
 
3407
3464
  type OpenAIClientOptions = Pick<ClientOptions$1, "baseURL" | "apiKey">;
3408
3465
  type AnthropicClientOptions = Pick<ClientOptions$2, "baseURL" | "apiKey">;
@@ -3815,12 +3872,9 @@ declare const screenshotTool: (v3: V3) => ai.Tool<Record<string, never>, {
3815
3872
  pageUrl: string;
3816
3873
  }>;
3817
3874
 
3818
- declare const waitTool: (v3: V3) => ai.Tool<{
3875
+ declare const waitTool: (v3: V3, mode?: AgentToolMode) => ai.Tool<{
3819
3876
  timeMs: number;
3820
- }, {
3821
- success: boolean;
3822
- waited: number;
3823
- }>;
3877
+ }, WaitToolResult>;
3824
3878
 
3825
3879
  declare const navBackTool: (v3: V3) => ai.Tool<{
3826
3880
  reasoningText: string;
@@ -3873,11 +3927,7 @@ declare const scrollVisionTool: (v3: V3, provider?: string) => ai.Tool<{
3873
3927
  direction: "up" | "down";
3874
3928
  coordinates?: number[];
3875
3929
  percentage?: number;
3876
- }, {
3877
- success: boolean;
3878
- message: string;
3879
- scrolledPixels: number;
3880
- }>;
3930
+ }, ScrollVisionToolResult>;
3881
3931
 
3882
3932
  declare const extractTool: (v3: V3, executionModel?: string, logger?: (message: LogLine) => void) => ai.Tool<{
3883
3933
  instruction: string;
@@ -3895,47 +3945,19 @@ declare const extractTool: (v3: V3, executionModel?: string, logger?: (message:
3895
3945
  declare const clickTool: (v3: V3, provider?: string) => ai.Tool<{
3896
3946
  describe: string;
3897
3947
  coordinates: number[];
3898
- }, {
3899
- success: boolean;
3900
- describe: string;
3901
- coordinates: number[];
3902
- error?: undefined;
3903
- } | {
3904
- success: boolean;
3905
- error: string;
3906
- describe?: undefined;
3907
- coordinates?: undefined;
3908
- }>;
3948
+ }, ClickToolResult>;
3909
3949
 
3910
3950
  declare const typeTool: (v3: V3, provider?: string) => ai.Tool<{
3911
3951
  describe: string;
3912
3952
  text: string;
3913
3953
  coordinates: number[];
3914
- }, {
3915
- success: boolean;
3916
- describe: string;
3917
- text: string;
3918
- error?: undefined;
3919
- } | {
3920
- success: boolean;
3921
- error: string;
3922
- describe?: undefined;
3923
- text?: undefined;
3924
- }>;
3954
+ }, TypeToolResult>;
3925
3955
 
3926
3956
  declare const dragAndDropTool: (v3: V3, provider?: string) => ai.Tool<{
3927
3957
  describe: string;
3928
3958
  startCoordinates: number[];
3929
3959
  endCoordinates: number[];
3930
- }, {
3931
- success: boolean;
3932
- describe: string;
3933
- error?: undefined;
3934
- } | {
3935
- success: boolean;
3936
- error: string;
3937
- describe?: undefined;
3938
- }>;
3960
+ }, DragAndDropToolResult>;
3939
3961
 
3940
3962
  declare const clickAndHoldTool: (v3: V3, provider?: string) => ai.Tool<{
3941
3963
  describe: string;
@@ -3984,22 +4006,7 @@ declare const fillFormVisionTool: (v3: V3, provider?: string) => ai.Tool<{
3984
4006
  y: number;
3985
4007
  };
3986
4008
  }[];
3987
- }, {
3988
- success: boolean;
3989
- playwrightArguments: {
3990
- coordinates: {
3991
- x: number;
3992
- y: number;
3993
- };
3994
- action: string;
3995
- value: string;
3996
- }[];
3997
- error?: undefined;
3998
- } | {
3999
- success: boolean;
4000
- error: string;
4001
- playwrightArguments?: undefined;
4002
- }>;
4009
+ }, FillFormVisionToolResult>;
4003
4010
 
4004
4011
  declare const thinkTool: () => ai.Tool<{
4005
4012
  reasoning: string;
@@ -4187,4 +4194,4 @@ declare class V3Evaluator {
4187
4194
  private _evaluateWithMultipleScreenshots;
4188
4195
  }
4189
4196
 
4190
- export { type AISDKCustomProvider, type AISDKProvider, AISdkClient, AVAILABLE_CUA_MODELS, type ActOptions, type ActResult, ActTimeoutError, type Action, type ActionExecutionResult, AgentAbortError, type AgentAction, type AgentCallbacks, type AgentConfig, type AgentContext, type AgentExecuteCallbacks, type AgentExecuteOptions, type AgentExecuteOptionsBase, type AgentExecutionOptions, type AgentHandlerOptions, type AgentInstance, type AgentModelConfig, AgentProvider, type AgentProviderType, type AgentResult, AgentScreenshotProviderError, type AgentState, type AgentStreamCallbacks, type AgentStreamExecuteOptions, type AgentStreamResult, type AgentToolCall, type AgentToolMode, type AgentToolResult, type AgentToolTypesMap, type AgentTools, type AgentType, type AgentUITools, AnnotatedScreenshotText, type AnthropicClientOptions, type AnthropicContentBlock, type AnthropicJsonSchemaObject, type AnthropicMessage, type AnthropicTextBlock, type AnthropicToolResult, type AnyPage, api as Api, type AvailableCuaModel, type AvailableModel, BrowserbaseSessionNotFoundError, CaptchaTimeoutError, type ChatCompletionOptions, type ChatMessage, type ChatMessageContent, type ChatMessageImageContent, type ChatMessageTextContent, type ClientOptions, type ComputerCallItem, ConnectionTimeoutError, type ConsoleListener, ConsoleMessage, ContentFrameNotFoundError, type CreateChatCompletionOptions, CreateChatCompletionResponseError, CuaModelRequiredError, ElementNotVisibleError, ExperimentalApiConflictError, ExperimentalNotConfiguredError, type ExtractOptions, type ExtractResult, ExtractTimeoutError, type FunctionCallItem, type GoogleServiceAccountCredentials, type GoogleVertexProviderSettings, HandlerNotInitializedError, type HistoryEntry, type InferStagehandSchema, InvalidAISDKModelFormatError, type JsonSchema, type JsonSchemaDocument, type JsonSchemaProperty, LLMClient, type LLMParsedResponse, type LLMResponse, LLMResponseError, type LLMTool, type LLMUsage, LOG_LEVEL_NAMES, type LoadState, type LocalBrowserLaunchOptions, type LogLevel, type LogLine, type Logger, MCPConnectionError, MissingEnvironmentVariableError, MissingLLMConfigurationError, type ModelConfiguration, type ModelProvider, type NonStreamingAgentInstance, type ObserveOptions, ObserveTimeoutError, type OpenAIClientOptions, Page, PageNotFoundError, Response$1 as Response, ResponseBodyError, type ResponseInputItem, type ResponseItem, ResponseParseError, type SafetyCheck, type SafetyConfirmationHandler, type SafetyConfirmationResponse, V3 as Stagehand, StagehandAPIError, StagehandAPIUnauthorizedError, StagehandClickError, StagehandClosedError, StagehandDefaultError, StagehandDomProcessError, StagehandElementNotFoundError, StagehandEnvironmentError, StagehandError, StagehandEvalError, StagehandHttpError, StagehandIframeError, StagehandInitError, StagehandInvalidArgumentError, type StagehandMetrics, StagehandMissingArgumentError, StagehandNotInitializedError, StagehandResponseBodyError, StagehandResponseParseError, StagehandServerError, StagehandShadowRootMissingError, StagehandShadowSegmentEmptyError, StagehandShadowSegmentNotFoundError, type StagehandZodObject, type StagehandZodSchema, type StreamingAgentInstance, StreamingCallbacksInNonStreamingModeError, TimeoutError, type ToolUseItem, UnsupportedAISDKModelProviderError, UnsupportedModelError, UnsupportedModelProviderError, V3, type V3Env, V3Evaluator, V3FunctionName, type V3Options, XPathResolutionError, ZodSchemaValidationError, connectToMCPServer, defaultExtractSchema, getZodType, injectUrls, isRunningInBun, isZod3Schema, isZod4Schema, jsonSchemaToZod, loadApiKeyFromEnv, localBrowserLaunchOptionsSchema, modelToAgentProviderMap, pageTextSchema, providerEnvVarMap, toGeminiSchema, toJsonSchema, transformSchema, trimTrailingTextNode, validateZodSchema };
4197
+ export { type AISDKCustomProvider, type AISDKProvider, AISdkClient, AVAILABLE_CUA_MODELS, type ActOptions, type ActResult, ActTimeoutError, type Action, type ActionExecutionResult, AgentAbortError, type AgentAction, type AgentCallbacks, type AgentConfig, type AgentContext, type AgentExecuteCallbacks, type AgentExecuteOptions, type AgentExecuteOptionsBase, type AgentExecutionOptions, type AgentHandlerOptions, type AgentInstance, type AgentModelConfig, AgentProvider, type AgentProviderType, type AgentResult, AgentScreenshotProviderError, type AgentState, type AgentStreamCallbacks, type AgentStreamExecuteOptions, type AgentStreamResult, type AgentToolCall, type AgentToolMode, type AgentToolResult, type AgentToolTypesMap, type AgentTools, type AgentType, type AgentUITools, AnnotatedScreenshotText, type AnthropicClientOptions, type AnthropicContentBlock, type AnthropicJsonSchemaObject, type AnthropicMessage, type AnthropicTextBlock, type AnthropicToolResult, type AnyPage, api as Api, type AvailableCuaModel, type AvailableModel, BrowserbaseSessionNotFoundError, CaptchaTimeoutError, type ChatCompletionOptions, type ChatMessage, type ChatMessageContent, type ChatMessageImageContent, type ChatMessageTextContent, type ClickToolResult, type ClientOptions, type ComputerCallItem, ConnectionTimeoutError, type ConsoleListener, ConsoleMessage, ContentFrameNotFoundError, type CreateChatCompletionOptions, CreateChatCompletionResponseError, CuaModelRequiredError, type DragAndDropToolResult, ElementNotVisibleError, ExperimentalApiConflictError, ExperimentalNotConfiguredError, type ExtractOptions, type ExtractResult, ExtractTimeoutError, type FillFormField, type FillFormVisionToolResult, type FunctionCallItem, type GoogleServiceAccountCredentials, type GoogleVertexProviderSettings, HandlerNotInitializedError, type HistoryEntry, type InferStagehandSchema, InvalidAISDKModelFormatError, type JsonSchema, type JsonSchemaDocument, type JsonSchemaProperty, LLMClient, type LLMParsedResponse, type LLMResponse, LLMResponseError, type LLMTool, type LLMUsage, LOG_LEVEL_NAMES, type LoadState, type LocalBrowserLaunchOptions, type LogLevel, type LogLine, type Logger, MCPConnectionError, MissingEnvironmentVariableError, MissingLLMConfigurationError, type ModelConfiguration, type ModelOutputContentItem, type ModelProvider, type NonStreamingAgentInstance, type ObserveOptions, ObserveTimeoutError, type OpenAIClientOptions, Page, PageNotFoundError, Response$1 as Response, ResponseBodyError, type ResponseInputItem, type ResponseItem, ResponseParseError, type SafetyCheck, type SafetyConfirmationHandler, type SafetyConfirmationResponse, type ScrollVisionToolResult, V3 as Stagehand, StagehandAPIError, StagehandAPIUnauthorizedError, StagehandClickError, StagehandClosedError, StagehandDefaultError, StagehandDomProcessError, StagehandElementNotFoundError, StagehandEnvironmentError, StagehandError, StagehandEvalError, StagehandHttpError, StagehandIframeError, StagehandInitError, StagehandInvalidArgumentError, type StagehandMetrics, StagehandMissingArgumentError, StagehandNotInitializedError, StagehandResponseBodyError, StagehandResponseParseError, StagehandServerError, StagehandShadowRootMissingError, StagehandShadowSegmentEmptyError, StagehandShadowSegmentNotFoundError, type StagehandZodObject, type StagehandZodSchema, type StreamingAgentInstance, StreamingCallbacksInNonStreamingModeError, TimeoutError, type ToolUseItem, type TypeToolResult, UnsupportedAISDKModelProviderError, UnsupportedModelError, UnsupportedModelProviderError, V3, type V3Env, V3Evaluator, V3FunctionName, type V3Options, type WaitToolResult, XPathResolutionError, ZodSchemaValidationError, connectToMCPServer, defaultExtractSchema, getZodType, injectUrls, isRunningInBun, isZod3Schema, isZod4Schema, jsonSchemaToZod, loadApiKeyFromEnv, localBrowserLaunchOptionsSchema, modelToAgentProviderMap, pageTextSchema, providerEnvVarMap, toGeminiSchema, toJsonSchema, transformSchema, trimTrailingTextNode, validateZodSchema };
package/dist/index.js CHANGED
@@ -31125,7 +31125,25 @@ var screenshotTool = (v3) => (0, import_ai3.tool)({
31125
31125
  // lib/v3/agent/tools/wait.ts
31126
31126
  var import_ai4 = require("ai");
31127
31127
  var import_zod8 = require("zod");
31128
- var waitTool = (v3) => (0, import_ai4.tool)({
31128
+
31129
+ // lib/v3/agent/utils/screenshotHandler.ts
31130
+ var DEFAULT_DELAY_MS = 500;
31131
+ function waitAndCaptureScreenshot(_0) {
31132
+ return __async(this, arguments, function* (page, delayMs = DEFAULT_DELAY_MS) {
31133
+ if (delayMs > 0) {
31134
+ yield page.waitForTimeout(delayMs);
31135
+ }
31136
+ try {
31137
+ const buffer = yield page.screenshot({ fullPage: false });
31138
+ return buffer.toString("base64");
31139
+ } catch (e2) {
31140
+ return void 0;
31141
+ }
31142
+ });
31143
+ }
31144
+
31145
+ // lib/v3/agent/tools/wait.ts
31146
+ var waitTool = (v3, mode) => (0, import_ai4.tool)({
31129
31147
  description: "Wait for a specified time",
31130
31148
  inputSchema: import_zod8.z.object({
31131
31149
  timeMs: import_zod8.z.number().describe("Time in milliseconds")
@@ -31146,8 +31164,32 @@ var waitTool = (v3) => (0, import_ai4.tool)({
31146
31164
  if (timeMs > 0) {
31147
31165
  v3.recordAgentReplayStep({ type: "wait", timeMs });
31148
31166
  }
31167
+ if (mode === "hybrid") {
31168
+ const page = yield v3.context.awaitActivePage();
31169
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page, 0);
31170
+ return { success: true, waited: timeMs, screenshotBase64 };
31171
+ }
31149
31172
  return { success: true, waited: timeMs };
31150
- })
31173
+ }),
31174
+ toModelOutput: (result) => {
31175
+ const content = [
31176
+ {
31177
+ type: "text",
31178
+ text: JSON.stringify({
31179
+ success: result.success,
31180
+ waited: result.waited
31181
+ })
31182
+ }
31183
+ ];
31184
+ if (result.screenshotBase64) {
31185
+ content.push({
31186
+ type: "media",
31187
+ mediaType: "image/png",
31188
+ data: result.screenshotBase64
31189
+ });
31190
+ }
31191
+ return { type: "content", value: content };
31192
+ }
31151
31193
  });
31152
31194
 
31153
31195
  // lib/v3/agent/tools/navback.ts
@@ -31346,7 +31388,11 @@ var scrollVisionTool = (v3, provider) => (0, import_ai9.tool)({
31346
31388
  ),
31347
31389
  percentage: import_zod13.z.number().min(1).max(200).optional()
31348
31390
  }),
31349
- execute: (_0) => __async(null, [_0], function* ({ direction, coordinates, percentage = 80 }) {
31391
+ execute: (_0) => __async(null, [_0], function* ({
31392
+ direction,
31393
+ coordinates,
31394
+ percentage = 80
31395
+ }) {
31350
31396
  const page = yield v3.context.awaitActivePage();
31351
31397
  const { w, h: h2 } = yield page.mainFrame().evaluate("({ w: window.innerWidth, h: window.innerHeight })");
31352
31398
  let cx;
@@ -31382,6 +31428,7 @@ var scrollVisionTool = (v3, provider) => (0, import_ai9.tool)({
31382
31428
  const scrollDistance = Math.round(h2 * percentage / 100);
31383
31429
  const deltaY = direction === "up" ? -scrollDistance : scrollDistance;
31384
31430
  yield page.scroll(cx, cy, 0, deltaY);
31431
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page, 100);
31385
31432
  v3.recordAgentReplayStep({
31386
31433
  type: "scroll",
31387
31434
  deltaX: 0,
@@ -31391,9 +31438,30 @@ var scrollVisionTool = (v3, provider) => (0, import_ai9.tool)({
31391
31438
  return {
31392
31439
  success: true,
31393
31440
  message: coordinates ? `Scrolled ${percentage}% ${direction} at (${cx}, ${cy})` : `Scrolled ${percentage}% ${direction}`,
31394
- scrolledPixels: scrollDistance
31441
+ scrolledPixels: scrollDistance,
31442
+ screenshotBase64
31395
31443
  };
31396
- })
31444
+ }),
31445
+ toModelOutput: (result) => {
31446
+ const content = [
31447
+ {
31448
+ type: "text",
31449
+ text: JSON.stringify({
31450
+ success: result.success,
31451
+ message: result.message,
31452
+ scrolledPixels: result.scrolledPixels
31453
+ })
31454
+ }
31455
+ ];
31456
+ if (result.screenshotBase64) {
31457
+ content.push({
31458
+ type: "media",
31459
+ mediaType: "image/png",
31460
+ data: result.screenshotBase64
31461
+ });
31462
+ }
31463
+ return { type: "content", value: content };
31464
+ }
31397
31465
  });
31398
31466
 
31399
31467
  // lib/v3/agent/tools/extract.ts
@@ -31461,9 +31529,6 @@ function ensureXPath(value) {
31461
31529
  }
31462
31530
 
31463
31531
  // lib/v3/agent/tools/click.ts
31464
- function waitForTimeout(ms) {
31465
- return new Promise((resolve3) => setTimeout(resolve3, ms));
31466
- }
31467
31532
  var clickTool = (v3, provider) => (0, import_ai11.tool)({
31468
31533
  description: "Click on an element using its coordinates (this is the most reliable way to click on an element, always use this over act, unless the element is not visible in the screenshot, but shown in ariaTree)",
31469
31534
  inputSchema: import_zod15.z.object({
@@ -31491,31 +31556,33 @@ var clickTool = (v3, provider) => (0, import_ai11.tool)({
31491
31556
  }
31492
31557
  }
31493
31558
  });
31559
+ const shouldCollectXpath = v3.isAgentReplayActive();
31494
31560
  const xpath = yield page.click(processed.x, processed.y, {
31495
- returnXpath: true
31561
+ returnXpath: shouldCollectXpath
31496
31562
  });
31497
- if (isGoogleProvider(provider)) {
31498
- yield waitForTimeout(1e3);
31499
- }
31500
- const normalizedXpath = ensureXPath(xpath);
31501
- if (normalizedXpath) {
31502
- const action = {
31503
- selector: normalizedXpath,
31504
- description: describe,
31505
- method: "click",
31506
- arguments: []
31507
- };
31508
- v3.recordAgentReplayStep({
31509
- type: "act",
31510
- instruction: describe,
31511
- actions: [action],
31512
- actionDescription: describe
31513
- });
31563
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page);
31564
+ if (shouldCollectXpath) {
31565
+ const normalizedXpath = ensureXPath(xpath);
31566
+ if (normalizedXpath) {
31567
+ const action = {
31568
+ selector: normalizedXpath,
31569
+ description: describe,
31570
+ method: "click",
31571
+ arguments: []
31572
+ };
31573
+ v3.recordAgentReplayStep({
31574
+ type: "act",
31575
+ instruction: describe,
31576
+ actions: [action],
31577
+ actionDescription: describe
31578
+ });
31579
+ }
31514
31580
  }
31515
31581
  return {
31516
31582
  success: true,
31517
31583
  describe,
31518
- coordinates: [processed.x, processed.y]
31584
+ coordinates: [processed.x, processed.y],
31585
+ screenshotBase64
31519
31586
  };
31520
31587
  } catch (error) {
31521
31588
  return {
@@ -31523,15 +31590,46 @@ var clickTool = (v3, provider) => (0, import_ai11.tool)({
31523
31590
  error: `Error clicking: ${error.message}`
31524
31591
  };
31525
31592
  }
31526
- })
31593
+ }),
31594
+ toModelOutput: (result) => {
31595
+ if (result.success) {
31596
+ const content = [
31597
+ {
31598
+ type: "text",
31599
+ text: JSON.stringify({
31600
+ success: result.success,
31601
+ describe: result.describe,
31602
+ coordinates: result.coordinates
31603
+ })
31604
+ }
31605
+ ];
31606
+ if (result.screenshotBase64) {
31607
+ content.push({
31608
+ type: "media",
31609
+ mediaType: "image/png",
31610
+ data: result.screenshotBase64
31611
+ });
31612
+ }
31613
+ return { type: "content", value: content };
31614
+ }
31615
+ return {
31616
+ type: "content",
31617
+ value: [
31618
+ {
31619
+ type: "text",
31620
+ text: JSON.stringify({
31621
+ success: result.success,
31622
+ error: result.error
31623
+ })
31624
+ }
31625
+ ]
31626
+ };
31627
+ }
31527
31628
  });
31528
31629
 
31529
31630
  // lib/v3/agent/tools/type.ts
31530
31631
  var import_ai12 = require("ai");
31531
31632
  var import_zod16 = require("zod");
31532
- function waitForTimeout2(ms) {
31533
- return new Promise((resolve3) => setTimeout(resolve3, ms));
31534
- }
31535
31633
  var typeTool = (v3, provider) => (0, import_ai12.tool)({
31536
31634
  description: "Type text into an element using its coordinates. This will click the element and then type the text into it (this is the most reliable way to type into an element, always use this over act, unless the element is not visible in the screenshot, but shown in ariaTree)",
31537
31635
  inputSchema: import_zod16.z.object({
@@ -31541,7 +31639,11 @@ var typeTool = (v3, provider) => (0, import_ai12.tool)({
31541
31639
  text: import_zod16.z.string().describe("The text to type into the element"),
31542
31640
  coordinates: import_zod16.z.array(import_zod16.z.number()).describe("The (x, y) coordinates to type into the element")
31543
31641
  }),
31544
- execute: (_0) => __async(null, [_0], function* ({ describe, coordinates, text }) {
31642
+ execute: (_0) => __async(null, [_0], function* ({
31643
+ describe,
31644
+ coordinates,
31645
+ text
31646
+ }) {
31545
31647
  try {
31546
31648
  const page = yield v3.context.awaitActivePage();
31547
31649
  const processed = processCoordinates(
@@ -31560,36 +31662,76 @@ var typeTool = (v3, provider) => (0, import_ai12.tool)({
31560
31662
  }
31561
31663
  }
31562
31664
  });
31665
+ const shouldCollectXpath = v3.isAgentReplayActive();
31563
31666
  const xpath = yield page.click(processed.x, processed.y, {
31564
- returnXpath: true
31667
+ returnXpath: shouldCollectXpath
31565
31668
  });
31566
- if (isGoogleProvider(provider)) {
31567
- yield waitForTimeout2(1e3);
31568
- }
31569
31669
  yield page.type(text);
31570
- const normalizedXpath = ensureXPath(xpath);
31571
- if (normalizedXpath) {
31572
- const action = {
31573
- selector: normalizedXpath,
31574
- description: describe,
31575
- method: "type",
31576
- arguments: [text]
31577
- };
31578
- v3.recordAgentReplayStep({
31579
- type: "act",
31580
- instruction: describe,
31581
- actions: [action],
31582
- actionDescription: describe
31583
- });
31670
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page);
31671
+ if (shouldCollectXpath) {
31672
+ const normalizedXpath = ensureXPath(xpath);
31673
+ if (normalizedXpath) {
31674
+ const action = {
31675
+ selector: normalizedXpath,
31676
+ description: describe,
31677
+ method: "type",
31678
+ arguments: [text]
31679
+ };
31680
+ v3.recordAgentReplayStep({
31681
+ type: "act",
31682
+ instruction: describe,
31683
+ actions: [action],
31684
+ actionDescription: describe
31685
+ });
31686
+ }
31584
31687
  }
31585
- return { success: true, describe, text };
31688
+ return {
31689
+ success: true,
31690
+ describe,
31691
+ text,
31692
+ screenshotBase64
31693
+ };
31586
31694
  } catch (error) {
31587
31695
  return {
31588
31696
  success: false,
31589
31697
  error: `Error typing: ${error.message}`
31590
31698
  };
31591
31699
  }
31592
- })
31700
+ }),
31701
+ toModelOutput: (result) => {
31702
+ if (result.success) {
31703
+ const content = [
31704
+ {
31705
+ type: "text",
31706
+ text: JSON.stringify({
31707
+ success: result.success,
31708
+ describe: result.describe,
31709
+ text: result.text
31710
+ })
31711
+ }
31712
+ ];
31713
+ if (result.screenshotBase64) {
31714
+ content.push({
31715
+ type: "media",
31716
+ mediaType: "image/png",
31717
+ data: result.screenshotBase64
31718
+ });
31719
+ }
31720
+ return { type: "content", value: content };
31721
+ }
31722
+ return {
31723
+ type: "content",
31724
+ value: [
31725
+ {
31726
+ type: "text",
31727
+ text: JSON.stringify({
31728
+ success: result.success,
31729
+ error: result.error
31730
+ })
31731
+ }
31732
+ ]
31733
+ };
31734
+ }
31593
31735
  });
31594
31736
 
31595
31737
  // lib/v3/agent/tools/dragAndDrop.ts
@@ -31602,7 +31744,11 @@ var dragAndDropTool = (v3, provider) => (0, import_ai13.tool)({
31602
31744
  startCoordinates: import_zod17.z.array(import_zod17.z.number()).describe("The (x, y) coordinates to start the drag and drop from"),
31603
31745
  endCoordinates: import_zod17.z.array(import_zod17.z.number()).describe("The (x, y) coordinates to end the drag and drop at")
31604
31746
  }),
31605
- execute: (_0) => __async(null, [_0], function* ({ describe, startCoordinates, endCoordinates }) {
31747
+ execute: (_0) => __async(null, [_0], function* ({
31748
+ describe,
31749
+ startCoordinates,
31750
+ endCoordinates
31751
+ }) {
31606
31752
  try {
31607
31753
  const page = yield v3.context.awaitActivePage();
31608
31754
  const processedStart = processCoordinates(
@@ -31632,37 +31778,78 @@ var dragAndDropTool = (v3, provider) => (0, import_ai13.tool)({
31632
31778
  }
31633
31779
  }
31634
31780
  });
31781
+ const shouldCollectXpath = v3.isAgentReplayActive();
31635
31782
  const [fromXpath, toXpath] = yield page.dragAndDrop(
31636
31783
  processedStart.x,
31637
31784
  processedStart.y,
31638
31785
  processedEnd.x,
31639
31786
  processedEnd.y,
31640
- { returnXpath: true }
31787
+ { returnXpath: shouldCollectXpath }
31641
31788
  );
31642
- const normalizedFrom = ensureXPath(fromXpath);
31643
- const normalizedTo = ensureXPath(toXpath);
31644
- if (normalizedFrom && normalizedTo) {
31645
- const action = {
31646
- selector: normalizedFrom,
31647
- description: describe,
31648
- method: "dragAndDrop",
31649
- arguments: [normalizedTo]
31650
- };
31651
- v3.recordAgentReplayStep({
31652
- type: "act",
31653
- instruction: describe,
31654
- actions: [action],
31655
- actionDescription: describe
31656
- });
31789
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page);
31790
+ if (shouldCollectXpath) {
31791
+ const normalizedFrom = ensureXPath(fromXpath);
31792
+ const normalizedTo = ensureXPath(toXpath);
31793
+ if (normalizedFrom && normalizedTo) {
31794
+ const action = {
31795
+ selector: normalizedFrom,
31796
+ description: describe,
31797
+ method: "dragAndDrop",
31798
+ arguments: [normalizedTo]
31799
+ };
31800
+ v3.recordAgentReplayStep({
31801
+ type: "act",
31802
+ instruction: describe,
31803
+ actions: [action],
31804
+ actionDescription: describe
31805
+ });
31806
+ }
31657
31807
  }
31658
- return { success: true, describe };
31808
+ return {
31809
+ success: true,
31810
+ describe,
31811
+ screenshotBase64
31812
+ };
31659
31813
  } catch (error) {
31660
31814
  return {
31661
31815
  success: false,
31662
31816
  error: `Error dragging: ${error.message}`
31663
31817
  };
31664
31818
  }
31665
- })
31819
+ }),
31820
+ toModelOutput: (result) => {
31821
+ if (result.success) {
31822
+ const content = [
31823
+ {
31824
+ type: "text",
31825
+ text: JSON.stringify({
31826
+ success: result.success,
31827
+ describe: result.describe
31828
+ })
31829
+ }
31830
+ ];
31831
+ if (result.screenshotBase64) {
31832
+ content.push({
31833
+ type: "media",
31834
+ mediaType: "image/png",
31835
+ data: result.screenshotBase64
31836
+ });
31837
+ }
31838
+ return { type: "content", value: content };
31839
+ }
31840
+ return {
31841
+ type: "content",
31842
+ value: [
31843
+ {
31844
+ type: "text",
31845
+ text: JSON.stringify({
31846
+ success: result.success,
31847
+ error: result.error
31848
+ })
31849
+ }
31850
+ ]
31851
+ };
31852
+ }
31666
31853
  });
31667
31854
 
31668
31855
  // lib/v3/agent/tools/clickAndHold.ts
@@ -31701,27 +31888,30 @@ var clickAndHoldTool = (v3, provider) => (0, import_ai14.tool)({
31701
31888
  }
31702
31889
  }
31703
31890
  });
31891
+ const shouldCollectXpath = v3.isAgentReplayActive();
31704
31892
  const [xpath] = yield page.dragAndDrop(
31705
31893
  processed.x,
31706
31894
  processed.y,
31707
31895
  processed.x,
31708
31896
  processed.y,
31709
- { delay: duration, returnXpath: true }
31897
+ { delay: duration, returnXpath: shouldCollectXpath }
31710
31898
  );
31711
- const normalizedXpath = ensureXPath(xpath);
31712
- if (normalizedXpath) {
31713
- const action = {
31714
- selector: normalizedXpath,
31715
- description: describe,
31716
- method: "clickAndHold",
31717
- arguments: [String(duration)]
31718
- };
31719
- v3.recordAgentReplayStep({
31720
- type: "act",
31721
- instruction: describe,
31722
- actions: [action],
31723
- actionDescription: describe
31724
- });
31899
+ if (shouldCollectXpath) {
31900
+ const normalizedXpath = ensureXPath(xpath);
31901
+ if (normalizedXpath) {
31902
+ const action = {
31903
+ selector: normalizedXpath,
31904
+ description: describe,
31905
+ method: "clickAndHold",
31906
+ arguments: [String(duration)]
31907
+ };
31908
+ v3.recordAgentReplayStep({
31909
+ type: "act",
31910
+ instruction: describe,
31911
+ actions: [action],
31912
+ actionDescription: describe
31913
+ });
31914
+ }
31725
31915
  }
31726
31916
  return { success: true, describe };
31727
31917
  } catch (error) {
@@ -31852,28 +32042,32 @@ MANDATORY USE CASES (always use fillFormVision for these):
31852
32042
  }
31853
32043
  }
31854
32044
  });
32045
+ const shouldCollectXpath = v3.isAgentReplayActive();
31855
32046
  const actions = [];
31856
32047
  for (const field of processedFields) {
31857
32048
  const xpath = yield page.click(
31858
32049
  field.coordinates.x,
31859
32050
  field.coordinates.y,
31860
32051
  {
31861
- returnXpath: true
32052
+ returnXpath: shouldCollectXpath
31862
32053
  }
31863
32054
  );
31864
32055
  yield page.type(field.value);
31865
- const normalizedXpath = ensureXPath(xpath);
31866
- if (normalizedXpath) {
31867
- actions.push({
31868
- selector: normalizedXpath,
31869
- description: field.action,
31870
- method: "type",
31871
- arguments: [field.value]
31872
- });
32056
+ if (shouldCollectXpath) {
32057
+ const normalizedXpath = ensureXPath(xpath);
32058
+ if (normalizedXpath) {
32059
+ actions.push({
32060
+ selector: normalizedXpath,
32061
+ description: field.action,
32062
+ method: "type",
32063
+ arguments: [field.value]
32064
+ });
32065
+ }
31873
32066
  }
31874
32067
  yield new Promise((resolve3) => setTimeout(resolve3, 100));
31875
32068
  }
31876
- if (actions.length > 0) {
32069
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page, 100);
32070
+ if (shouldCollectXpath && actions.length > 0) {
31877
32071
  v3.recordAgentReplayStep({
31878
32072
  type: "act",
31879
32073
  instruction: `Fill ${fields.length} form fields`,
@@ -31883,7 +32077,8 @@ MANDATORY USE CASES (always use fillFormVision for these):
31883
32077
  }
31884
32078
  return {
31885
32079
  success: true,
31886
- playwrightArguments: processedFields
32080
+ playwrightArguments: processedFields,
32081
+ screenshotBase64
31887
32082
  };
31888
32083
  } catch (error) {
31889
32084
  return {
@@ -31891,7 +32086,41 @@ MANDATORY USE CASES (always use fillFormVision for these):
31891
32086
  error: `Error filling form: ${error.message}`
31892
32087
  };
31893
32088
  }
31894
- })
32089
+ }),
32090
+ toModelOutput: (result) => {
32091
+ var _a4, _b;
32092
+ if (result.success) {
32093
+ const content = [
32094
+ {
32095
+ type: "text",
32096
+ text: JSON.stringify({
32097
+ success: result.success,
32098
+ fieldsCount: (_b = (_a4 = result.playwrightArguments) == null ? void 0 : _a4.length) != null ? _b : 0
32099
+ })
32100
+ }
32101
+ ];
32102
+ if (result.screenshotBase64) {
32103
+ content.push({
32104
+ type: "media",
32105
+ mediaType: "image/png",
32106
+ data: result.screenshotBase64
32107
+ });
32108
+ }
32109
+ return { type: "content", value: content };
32110
+ }
32111
+ return {
32112
+ type: "content",
32113
+ value: [
32114
+ {
32115
+ type: "text",
32116
+ text: JSON.stringify({
32117
+ success: result.success,
32118
+ error: result.error
32119
+ })
32120
+ }
32121
+ ]
32122
+ };
32123
+ }
31895
32124
  });
31896
32125
 
31897
32126
  // lib/v3/agent/tools/think.ts
@@ -32040,7 +32269,7 @@ function createAgentTools(v3, options) {
32040
32269
  scroll: mode === "hybrid" ? scrollVisionTool(v3, provider) : scrollTool(v3),
32041
32270
  think: thinkTool(),
32042
32271
  type: typeTool(v3, provider),
32043
- wait: waitTool(v3)
32272
+ wait: waitTool(v3, mode)
32044
32273
  };
32045
32274
  if (process.env.BRAVE_API_KEY) {
32046
32275
  allTools.search = searchTool(v3);
@@ -32238,37 +32467,54 @@ function buildAgentSystemPrompt(options) {
32238
32467
  var import_ai19 = require("ai");
32239
32468
 
32240
32469
  // lib/v3/agent/utils/messageProcessing.ts
32470
+ var VISION_ACTION_TOOLS = [
32471
+ "click",
32472
+ "type",
32473
+ "dragAndDrop",
32474
+ "wait",
32475
+ "fillFormVision",
32476
+ "scroll"
32477
+ ];
32241
32478
  function isToolMessage(message) {
32242
32479
  return !!message && typeof message === "object" && message.role === "tool" && Array.isArray(message.content);
32243
32480
  }
32244
32481
  function isScreenshotPart(part) {
32245
32482
  return !!part && typeof part === "object" && part.toolName === "screenshot";
32246
32483
  }
32484
+ function isVisionActionPart(part) {
32485
+ if (!part || typeof part !== "object") return false;
32486
+ const toolName = part.toolName;
32487
+ return typeof toolName === "string" && VISION_ACTION_TOOLS.includes(toolName);
32488
+ }
32489
+ function isVisionPart(part) {
32490
+ return isScreenshotPart(part) || isVisionActionPart(part);
32491
+ }
32247
32492
  function isAriaTreePart(part) {
32248
32493
  return !!part && typeof part === "object" && part.toolName === "ariaTree";
32249
32494
  }
32250
32495
  function processMessages(messages) {
32251
32496
  let compressedCount = 0;
32252
- const screenshotIndices = [];
32497
+ const visionIndices = [];
32253
32498
  const ariaTreeIndices = [];
32254
32499
  for (let i2 = 0; i2 < messages.length; i2++) {
32255
32500
  const message = messages[i2];
32256
32501
  if (isToolMessage(message)) {
32257
32502
  const content = message.content;
32258
- if (content.some(isScreenshotPart)) {
32259
- screenshotIndices.push(i2);
32503
+ if (content.some(isVisionPart)) {
32504
+ visionIndices.push(i2);
32260
32505
  }
32261
32506
  if (content.some(isAriaTreePart)) {
32262
32507
  ariaTreeIndices.push(i2);
32263
32508
  }
32264
32509
  }
32265
32510
  }
32266
- if (screenshotIndices.length > 2) {
32267
- const toCompress = screenshotIndices.slice(0, screenshotIndices.length - 2);
32268
- for (const idx of toCompress) {
32269
- const message = messages[idx];
32511
+ if (visionIndices.length > 2) {
32512
+ const toCompress = visionIndices.slice(0, visionIndices.length - 2);
32513
+ for (const index of toCompress) {
32514
+ const message = messages[index];
32270
32515
  if (isToolMessage(message)) {
32271
32516
  compressScreenshotMessage(message);
32517
+ compressVisionActionMessage(message);
32272
32518
  compressedCount++;
32273
32519
  }
32274
32520
  }
@@ -32300,6 +32546,24 @@ function compressScreenshotMessage(message) {
32300
32546
  }
32301
32547
  }
32302
32548
  }
32549
+ function compressVisionActionMessage(message) {
32550
+ var _a4;
32551
+ for (const part of message.content) {
32552
+ if (isVisionActionPart(part)) {
32553
+ const typedPart = part;
32554
+ if (((_a4 = typedPart.output) == null ? void 0 : _a4.value) && Array.isArray(typedPart.output.value)) {
32555
+ typedPart.output.value = typedPart.output.value.filter(
32556
+ (item) => item && typeof item === "object" && item.type !== "media"
32557
+ );
32558
+ }
32559
+ if (typedPart.result && Array.isArray(typedPart.result)) {
32560
+ typedPart.result = typedPart.result.filter(
32561
+ (item) => item && typeof item === "object" && item.type !== "media"
32562
+ );
32563
+ }
32564
+ }
32565
+ }
32566
+ }
32303
32567
  function compressAriaTreeMessage(message) {
32304
32568
  var _a4;
32305
32569
  for (const part of message.content) {
@@ -32325,6 +32589,18 @@ function compressAriaTreeMessage(message) {
32325
32589
  init_flowLogger();
32326
32590
 
32327
32591
  // lib/v3/agent/utils/actionMapping.ts
32592
+ var EXCLUDED_OUTPUT_KEYS = ["screenshotBase64"];
32593
+ function stripExcludedKeys(output) {
32594
+ const result = {};
32595
+ for (const [key, value] of Object.entries(output)) {
32596
+ if (!EXCLUDED_OUTPUT_KEYS.includes(
32597
+ key
32598
+ )) {
32599
+ result[key] = value;
32600
+ }
32601
+ }
32602
+ return result;
32603
+ }
32328
32604
  function mapToolResultToActions({
32329
32605
  toolCallName,
32330
32606
  toolResult,
@@ -32390,8 +32666,14 @@ function createStandardAction(toolCallName, toolResult, args, reasoning) {
32390
32666
  return action;
32391
32667
  }
32392
32668
  if (toolCallName !== "ariaTree" && toolResult) {
32393
- const { output } = toolResult;
32394
- Object.assign(action, output);
32669
+ const result = toolResult;
32670
+ const output = result.output;
32671
+ if (output && typeof output === "object" && !Array.isArray(output)) {
32672
+ const cleanedOutput = stripExcludedKeys(
32673
+ output
32674
+ );
32675
+ Object.assign(action, cleanedOutput);
32676
+ }
32395
32677
  }
32396
32678
  return action;
32397
32679
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@browserbasehq/stagehand",
3
- "version": "3.0.8-alpha-3d5af07f66d6d26d1f5ac4bd9be7183c3381dd92",
3
+ "version": "3.0.8-alpha-16d72fb4c4081dd33bf45605d75c27644ea4c00e",
4
4
  "description": "An AI web browsing framework focused on simplicity and extensibility.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",