@browserbasehq/stagehand 3.0.8-alpha-091296e438bb2374c8bb10ef6c08283978145ebf → 3.0.8-alpha-692ffa0346ad3d121686aba503c0a22844293efa

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/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({
@@ -31486,8 +31551,8 @@ var clickTool = (v3, provider) => (0, import_ai11.tool)({
31486
31551
  level: 1,
31487
31552
  auxiliary: {
31488
31553
  arguments: {
31489
- value: JSON.stringify({ describe, coordinates, processed }),
31490
- type: "string"
31554
+ value: JSON.stringify({ describe }),
31555
+ type: "object"
31491
31556
  }
31492
31557
  }
31493
31558
  });
@@ -31495,9 +31560,7 @@ var clickTool = (v3, provider) => (0, import_ai11.tool)({
31495
31560
  const xpath = yield page.click(processed.x, processed.y, {
31496
31561
  returnXpath: shouldCollectXpath
31497
31562
  });
31498
- if (isGoogleProvider(provider)) {
31499
- yield waitForTimeout(1e3);
31500
- }
31563
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page);
31501
31564
  if (shouldCollectXpath) {
31502
31565
  const normalizedXpath = ensureXPath(xpath);
31503
31566
  if (normalizedXpath) {
@@ -31518,7 +31581,8 @@ var clickTool = (v3, provider) => (0, import_ai11.tool)({
31518
31581
  return {
31519
31582
  success: true,
31520
31583
  describe,
31521
- coordinates: [processed.x, processed.y]
31584
+ coordinates: [processed.x, processed.y],
31585
+ screenshotBase64
31522
31586
  };
31523
31587
  } catch (error) {
31524
31588
  return {
@@ -31526,15 +31590,46 @@ var clickTool = (v3, provider) => (0, import_ai11.tool)({
31526
31590
  error: `Error clicking: ${error.message}`
31527
31591
  };
31528
31592
  }
31529
- })
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
+ }
31530
31628
  });
31531
31629
 
31532
31630
  // lib/v3/agent/tools/type.ts
31533
31631
  var import_ai12 = require("ai");
31534
31632
  var import_zod16 = require("zod");
31535
- function waitForTimeout2(ms) {
31536
- return new Promise((resolve3) => setTimeout(resolve3, ms));
31537
- }
31538
31633
  var typeTool = (v3, provider) => (0, import_ai12.tool)({
31539
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)",
31540
31635
  inputSchema: import_zod16.z.object({
@@ -31544,7 +31639,11 @@ var typeTool = (v3, provider) => (0, import_ai12.tool)({
31544
31639
  text: import_zod16.z.string().describe("The text to type into the element"),
31545
31640
  coordinates: import_zod16.z.array(import_zod16.z.number()).describe("The (x, y) coordinates to type into the element")
31546
31641
  }),
31547
- execute: (_0) => __async(null, [_0], function* ({ describe, coordinates, text }) {
31642
+ execute: (_0) => __async(null, [_0], function* ({
31643
+ describe,
31644
+ coordinates,
31645
+ text
31646
+ }) {
31548
31647
  try {
31549
31648
  const page = yield v3.context.awaitActivePage();
31550
31649
  const processed = processCoordinates(
@@ -31558,8 +31657,8 @@ var typeTool = (v3, provider) => (0, import_ai12.tool)({
31558
31657
  level: 1,
31559
31658
  auxiliary: {
31560
31659
  arguments: {
31561
- value: JSON.stringify({ describe, coordinates, processed, text }),
31562
- type: "string"
31660
+ value: JSON.stringify({ describe, text }),
31661
+ type: "object"
31563
31662
  }
31564
31663
  }
31565
31664
  });
@@ -31567,10 +31666,8 @@ var typeTool = (v3, provider) => (0, import_ai12.tool)({
31567
31666
  const xpath = yield page.click(processed.x, processed.y, {
31568
31667
  returnXpath: shouldCollectXpath
31569
31668
  });
31570
- if (isGoogleProvider(provider)) {
31571
- yield waitForTimeout2(1e3);
31572
- }
31573
31669
  yield page.type(text);
31670
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page);
31574
31671
  if (shouldCollectXpath) {
31575
31672
  const normalizedXpath = ensureXPath(xpath);
31576
31673
  if (normalizedXpath) {
@@ -31588,14 +31685,53 @@ var typeTool = (v3, provider) => (0, import_ai12.tool)({
31588
31685
  });
31589
31686
  }
31590
31687
  }
31591
- return { success: true, describe, text };
31688
+ return {
31689
+ success: true,
31690
+ describe,
31691
+ text,
31692
+ screenshotBase64
31693
+ };
31592
31694
  } catch (error) {
31593
31695
  return {
31594
31696
  success: false,
31595
31697
  error: `Error typing: ${error.message}`
31596
31698
  };
31597
31699
  }
31598
- })
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
+ }
31599
31735
  });
31600
31736
 
31601
31737
  // lib/v3/agent/tools/dragAndDrop.ts
@@ -31608,7 +31744,11 @@ var dragAndDropTool = (v3, provider) => (0, import_ai13.tool)({
31608
31744
  startCoordinates: import_zod17.z.array(import_zod17.z.number()).describe("The (x, y) coordinates to start the drag and drop from"),
31609
31745
  endCoordinates: import_zod17.z.array(import_zod17.z.number()).describe("The (x, y) coordinates to end the drag and drop at")
31610
31746
  }),
31611
- execute: (_0) => __async(null, [_0], function* ({ describe, startCoordinates, endCoordinates }) {
31747
+ execute: (_0) => __async(null, [_0], function* ({
31748
+ describe,
31749
+ startCoordinates,
31750
+ endCoordinates
31751
+ }) {
31612
31752
  try {
31613
31753
  const page = yield v3.context.awaitActivePage();
31614
31754
  const processedStart = processCoordinates(
@@ -31628,13 +31768,9 @@ var dragAndDropTool = (v3, provider) => (0, import_ai13.tool)({
31628
31768
  auxiliary: {
31629
31769
  arguments: {
31630
31770
  value: JSON.stringify({
31631
- describe,
31632
- startCoordinates,
31633
- endCoordinates,
31634
- processedStart,
31635
- processedEnd
31771
+ describe
31636
31772
  }),
31637
- type: "string"
31773
+ type: "object"
31638
31774
  }
31639
31775
  }
31640
31776
  });
@@ -31646,6 +31782,7 @@ var dragAndDropTool = (v3, provider) => (0, import_ai13.tool)({
31646
31782
  processedEnd.y,
31647
31783
  { returnXpath: shouldCollectXpath }
31648
31784
  );
31785
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page);
31649
31786
  if (shouldCollectXpath) {
31650
31787
  const normalizedFrom = ensureXPath(fromXpath);
31651
31788
  const normalizedTo = ensureXPath(toXpath);
@@ -31664,14 +31801,51 @@ var dragAndDropTool = (v3, provider) => (0, import_ai13.tool)({
31664
31801
  });
31665
31802
  }
31666
31803
  }
31667
- return { success: true, describe };
31804
+ return {
31805
+ success: true,
31806
+ describe,
31807
+ screenshotBase64
31808
+ };
31668
31809
  } catch (error) {
31669
31810
  return {
31670
31811
  success: false,
31671
31812
  error: `Error dragging: ${error.message}`
31672
31813
  };
31673
31814
  }
31674
- })
31815
+ }),
31816
+ toModelOutput: (result) => {
31817
+ if (result.success) {
31818
+ const content = [
31819
+ {
31820
+ type: "text",
31821
+ text: JSON.stringify({
31822
+ success: result.success,
31823
+ describe: result.describe
31824
+ })
31825
+ }
31826
+ ];
31827
+ if (result.screenshotBase64) {
31828
+ content.push({
31829
+ type: "media",
31830
+ mediaType: "image/png",
31831
+ data: result.screenshotBase64
31832
+ });
31833
+ }
31834
+ return { type: "content", value: content };
31835
+ }
31836
+ return {
31837
+ type: "content",
31838
+ value: [
31839
+ {
31840
+ type: "text",
31841
+ text: JSON.stringify({
31842
+ success: result.success,
31843
+ error: result.error
31844
+ })
31845
+ }
31846
+ ]
31847
+ };
31848
+ }
31675
31849
  });
31676
31850
 
31677
31851
  // lib/v3/agent/tools/clickAndHold.ts
@@ -31702,11 +31876,9 @@ var clickAndHoldTool = (v3, provider) => (0, import_ai14.tool)({
31702
31876
  arguments: {
31703
31877
  value: JSON.stringify({
31704
31878
  describe,
31705
- coordinates,
31706
- processed,
31707
31879
  duration
31708
31880
  }),
31709
- type: "string"
31881
+ type: "object"
31710
31882
  }
31711
31883
  }
31712
31884
  });
@@ -31771,7 +31943,7 @@ Use method="press" for navigation keys (Enter, Tab, Escape, Backspace, arrows) a
31771
31943
  auxiliary: {
31772
31944
  arguments: {
31773
31945
  value: JSON.stringify({ method, value, repeat }),
31774
- type: "string"
31946
+ type: "object"
31775
31947
  }
31776
31948
  }
31777
31949
  });
@@ -31860,7 +32032,7 @@ MANDATORY USE CASES (always use fillFormVision for these):
31860
32032
  auxiliary: {
31861
32033
  arguments: {
31862
32034
  value: JSON.stringify({ fields, processedFields }),
31863
- type: "string"
32035
+ type: "object"
31864
32036
  }
31865
32037
  }
31866
32038
  });
@@ -31888,6 +32060,7 @@ MANDATORY USE CASES (always use fillFormVision for these):
31888
32060
  }
31889
32061
  yield new Promise((resolve3) => setTimeout(resolve3, 100));
31890
32062
  }
32063
+ const screenshotBase64 = yield waitAndCaptureScreenshot(page, 100);
31891
32064
  if (shouldCollectXpath && actions.length > 0) {
31892
32065
  v3.recordAgentReplayStep({
31893
32066
  type: "act",
@@ -31898,7 +32071,8 @@ MANDATORY USE CASES (always use fillFormVision for these):
31898
32071
  }
31899
32072
  return {
31900
32073
  success: true,
31901
- playwrightArguments: processedFields
32074
+ playwrightArguments: processedFields,
32075
+ screenshotBase64
31902
32076
  };
31903
32077
  } catch (error) {
31904
32078
  return {
@@ -31906,7 +32080,41 @@ MANDATORY USE CASES (always use fillFormVision for these):
31906
32080
  error: `Error filling form: ${error.message}`
31907
32081
  };
31908
32082
  }
31909
- })
32083
+ }),
32084
+ toModelOutput: (result) => {
32085
+ var _a4, _b;
32086
+ if (result.success) {
32087
+ const content = [
32088
+ {
32089
+ type: "text",
32090
+ text: JSON.stringify({
32091
+ success: result.success,
32092
+ fieldsCount: (_b = (_a4 = result.playwrightArguments) == null ? void 0 : _a4.length) != null ? _b : 0
32093
+ })
32094
+ }
32095
+ ];
32096
+ if (result.screenshotBase64) {
32097
+ content.push({
32098
+ type: "media",
32099
+ mediaType: "image/png",
32100
+ data: result.screenshotBase64
32101
+ });
32102
+ }
32103
+ return { type: "content", value: content };
32104
+ }
32105
+ return {
32106
+ type: "content",
32107
+ value: [
32108
+ {
32109
+ type: "text",
32110
+ text: JSON.stringify({
32111
+ success: result.success,
32112
+ error: result.error
32113
+ })
32114
+ }
32115
+ ]
32116
+ };
32117
+ }
31910
32118
  });
31911
32119
 
31912
32120
  // lib/v3/agent/tools/think.ts
@@ -31996,7 +32204,7 @@ var searchTool = (v3) => (0, import_ai18.tool)({
31996
32204
  auxiliary: {
31997
32205
  arguments: {
31998
32206
  value: JSON.stringify({ query }),
31999
- type: "string"
32207
+ type: "object"
32000
32208
  }
32001
32209
  }
32002
32210
  });
@@ -32055,7 +32263,7 @@ function createAgentTools(v3, options) {
32055
32263
  scroll: mode === "hybrid" ? scrollVisionTool(v3, provider) : scrollTool(v3),
32056
32264
  think: thinkTool(),
32057
32265
  type: typeTool(v3, provider),
32058
- wait: waitTool(v3)
32266
+ wait: waitTool(v3, mode)
32059
32267
  };
32060
32268
  if (process.env.BRAVE_API_KEY) {
32061
32269
  allTools.search = searchTool(v3);
@@ -32253,37 +32461,54 @@ function buildAgentSystemPrompt(options) {
32253
32461
  var import_ai19 = require("ai");
32254
32462
 
32255
32463
  // lib/v3/agent/utils/messageProcessing.ts
32464
+ var VISION_ACTION_TOOLS = [
32465
+ "click",
32466
+ "type",
32467
+ "dragAndDrop",
32468
+ "wait",
32469
+ "fillFormVision",
32470
+ "scroll"
32471
+ ];
32256
32472
  function isToolMessage(message) {
32257
32473
  return !!message && typeof message === "object" && message.role === "tool" && Array.isArray(message.content);
32258
32474
  }
32259
32475
  function isScreenshotPart(part) {
32260
32476
  return !!part && typeof part === "object" && part.toolName === "screenshot";
32261
32477
  }
32478
+ function isVisionActionPart(part) {
32479
+ if (!part || typeof part !== "object") return false;
32480
+ const toolName = part.toolName;
32481
+ return typeof toolName === "string" && VISION_ACTION_TOOLS.includes(toolName);
32482
+ }
32483
+ function isVisionPart(part) {
32484
+ return isScreenshotPart(part) || isVisionActionPart(part);
32485
+ }
32262
32486
  function isAriaTreePart(part) {
32263
32487
  return !!part && typeof part === "object" && part.toolName === "ariaTree";
32264
32488
  }
32265
32489
  function processMessages(messages) {
32266
32490
  let compressedCount = 0;
32267
- const screenshotIndices = [];
32491
+ const visionIndices = [];
32268
32492
  const ariaTreeIndices = [];
32269
32493
  for (let i2 = 0; i2 < messages.length; i2++) {
32270
32494
  const message = messages[i2];
32271
32495
  if (isToolMessage(message)) {
32272
32496
  const content = message.content;
32273
- if (content.some(isScreenshotPart)) {
32274
- screenshotIndices.push(i2);
32497
+ if (content.some(isVisionPart)) {
32498
+ visionIndices.push(i2);
32275
32499
  }
32276
32500
  if (content.some(isAriaTreePart)) {
32277
32501
  ariaTreeIndices.push(i2);
32278
32502
  }
32279
32503
  }
32280
32504
  }
32281
- if (screenshotIndices.length > 2) {
32282
- const toCompress = screenshotIndices.slice(0, screenshotIndices.length - 2);
32283
- for (const idx of toCompress) {
32284
- const message = messages[idx];
32505
+ if (visionIndices.length > 2) {
32506
+ const toCompress = visionIndices.slice(0, visionIndices.length - 2);
32507
+ for (const index of toCompress) {
32508
+ const message = messages[index];
32285
32509
  if (isToolMessage(message)) {
32286
32510
  compressScreenshotMessage(message);
32511
+ compressVisionActionMessage(message);
32287
32512
  compressedCount++;
32288
32513
  }
32289
32514
  }
@@ -32315,6 +32540,24 @@ function compressScreenshotMessage(message) {
32315
32540
  }
32316
32541
  }
32317
32542
  }
32543
+ function compressVisionActionMessage(message) {
32544
+ var _a4;
32545
+ for (const part of message.content) {
32546
+ if (isVisionActionPart(part)) {
32547
+ const typedPart = part;
32548
+ if (((_a4 = typedPart.output) == null ? void 0 : _a4.value) && Array.isArray(typedPart.output.value)) {
32549
+ typedPart.output.value = typedPart.output.value.filter(
32550
+ (item) => item && typeof item === "object" && item.type !== "media"
32551
+ );
32552
+ }
32553
+ if (typedPart.result && Array.isArray(typedPart.result)) {
32554
+ typedPart.result = typedPart.result.filter(
32555
+ (item) => item && typeof item === "object" && item.type !== "media"
32556
+ );
32557
+ }
32558
+ }
32559
+ }
32560
+ }
32318
32561
  function compressAriaTreeMessage(message) {
32319
32562
  var _a4;
32320
32563
  for (const part of message.content) {
@@ -32340,6 +32583,18 @@ function compressAriaTreeMessage(message) {
32340
32583
  init_flowLogger();
32341
32584
 
32342
32585
  // lib/v3/agent/utils/actionMapping.ts
32586
+ var EXCLUDED_OUTPUT_KEYS = ["screenshotBase64"];
32587
+ function stripExcludedKeys(output) {
32588
+ const result = {};
32589
+ for (const [key, value] of Object.entries(output)) {
32590
+ if (!EXCLUDED_OUTPUT_KEYS.includes(
32591
+ key
32592
+ )) {
32593
+ result[key] = value;
32594
+ }
32595
+ }
32596
+ return result;
32597
+ }
32343
32598
  function mapToolResultToActions({
32344
32599
  toolCallName,
32345
32600
  toolResult,
@@ -32405,8 +32660,14 @@ function createStandardAction(toolCallName, toolResult, args, reasoning) {
32405
32660
  return action;
32406
32661
  }
32407
32662
  if (toolCallName !== "ariaTree" && toolResult) {
32408
- const { output } = toolResult;
32409
- Object.assign(action, output);
32663
+ const result = toolResult;
32664
+ const output = result.output;
32665
+ if (output && typeof output === "object" && !Array.isArray(output)) {
32666
+ const cleanedOutput = stripExcludedKeys(
32667
+ output
32668
+ );
32669
+ Object.assign(action, cleanedOutput);
32670
+ }
32410
32671
  }
32411
32672
  return action;
32412
32673
  }
@@ -32607,7 +32868,8 @@ var V3AgentHandler = class {
32607
32868
  startTime,
32608
32869
  state,
32609
32870
  messages,
32610
- result
32871
+ result,
32872
+ maxSteps
32611
32873
  );
32612
32874
  } catch (error) {
32613
32875
  if (error instanceof StreamingCallbacksInNonStreamingModeError) {
@@ -32701,7 +32963,8 @@ var V3AgentHandler = class {
32701
32963
  startTime,
32702
32964
  state,
32703
32965
  messages,
32704
- event
32966
+ event,
32967
+ maxSteps
32705
32968
  );
32706
32969
  resolveResult(result);
32707
32970
  },
@@ -32725,11 +32988,20 @@ var V3AgentHandler = class {
32725
32988
  return agentStreamResult;
32726
32989
  });
32727
32990
  }
32728
- consolidateMetricsAndResult(startTime, state, inputMessages, result) {
32729
- var _a4;
32991
+ consolidateMetricsAndResult(startTime, state, inputMessages, result, maxSteps) {
32992
+ var _a4, _b;
32730
32993
  if (!state.finalMessage) {
32731
32994
  const allReasoning = state.collectedReasoning.join(" ").trim();
32732
- state.finalMessage = allReasoning || result.text || "";
32995
+ if (!state.completed && maxSteps && ((_a4 = result.steps) == null ? void 0 : _a4.length) >= maxSteps) {
32996
+ this.logger({
32997
+ category: "agent",
32998
+ message: `Agent stopped: reached maximum steps (${maxSteps})`,
32999
+ level: 1
33000
+ });
33001
+ state.finalMessage = `Agent stopped: reached maximum steps (${maxSteps})`;
33002
+ } else {
33003
+ state.finalMessage = allReasoning || result.text || "";
33004
+ }
32733
33005
  }
32734
33006
  const endTime = Date.now();
32735
33007
  const inferenceTimeMs = endTime - startTime;
@@ -32743,7 +33015,7 @@ var V3AgentHandler = class {
32743
33015
  inferenceTimeMs
32744
33016
  );
32745
33017
  }
32746
- const responseMessages = ((_a4 = result.response) == null ? void 0 : _a4.messages) || [];
33018
+ const responseMessages = ((_b = result.response) == null ? void 0 : _b.messages) || [];
32747
33019
  const fullMessages = [
32748
33020
  ...inputMessages,
32749
33021
  ...responseMessages
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@browserbasehq/stagehand",
3
- "version": "3.0.8-alpha-091296e438bb2374c8bb10ef6c08283978145ebf",
3
+ "version": "3.0.8-alpha-692ffa0346ad3d121686aba503c0a22844293efa",
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",