@browserbasehq/stagehand 2.5.7 → 2.5.8

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
@@ -978,6 +978,10 @@ declare class InvalidAISDKModelFormatError extends StagehandError {
978
978
  declare class StagehandNotInitializedError extends StagehandError {
979
979
  constructor(prop: string);
980
980
  }
981
+ declare class StagehandTargetClosedError extends StagehandError {
982
+ causedBy?: Error | StagehandError;
983
+ constructor(error?: unknown);
984
+ }
981
985
  declare class BrowserbaseSessionNotFoundError extends StagehandError {
982
986
  constructor();
983
987
  }
@@ -1244,4 +1248,4 @@ declare class Stagehand {
1244
1248
  };
1245
1249
  }
1246
1250
 
1247
- export { AISdkClient, type ActOptions, type ActResult, type ActToolResult, type ActionExecutionResult, type AgentAction$1 as AgentAction, type AgentClientOptions, type AgentConfig, type AgentExecuteOptions$1 as AgentExecuteOptions, type AgentExecuteParams, type AgentExecutionOptions, type AgentHandlerOptions, type AgentInstance, type AgentOptions$1 as AgentOptions, type AgentProviderType, type AgentResult$1 as AgentResult, AgentScreenshotProviderError, type AgentType, AnnotatedScreenshotText, type AnthropicContentBlock, type AnthropicJsonSchemaObject, type AnthropicMessage, type AnthropicTextBlock, type AnthropicToolResult, type AvailableModel, AvailableModelSchema, type Browser, type BrowserContext, type BrowserResult, BrowserbaseSessionNotFoundError, CaptchaTimeoutError, type ChatCompletionOptions, type ChatMessage, type ChatMessageContent, type ChatMessageImageContent, type ChatMessageTextContent, type ClientOptions, type ComputerCallItem, type ConstructorParams, ContentFrameNotFoundError, type CreateChatCompletionOptions, CreateChatCompletionResponseError, ExperimentalApiConflictError, ExperimentalNotConfiguredError, type ExtractOptions, type ExtractResult, type FunctionCallItem, type GotoOptions, HandlerNotInitializedError, type HistoryEntry, type InitResult, InvalidAISDKModelFormatError, LLMClient, type LLMResponse, LLMResponseError, LOG_LEVEL_NAMES, type LocalBrowserLaunchOptions, type LogLevel, type LogLine, type Logger, MCPConnectionError, MissingEnvironmentVariableError, MissingLLMConfigurationError, type ModelProvider, type ObserveOptions, type ObserveResult$1 as ObserveResult, type Page, PlaywrightCommandException, PlaywrightCommandMethodNotSupportedException, type ResponseInputItem, type ResponseItem, Stagehand, StagehandAPIError, StagehandAPIUnauthorizedError, StagehandClickError, StagehandDefaultError, StagehandDomProcessError, StagehandElementNotFoundError, StagehandEnvironmentError, StagehandError, StagehandEvalError, StagehandFunctionName, StagehandHttpError, StagehandIframeError, StagehandInitError, StagehandInvalidArgumentError, type StagehandMetrics, StagehandMissingArgumentError, StagehandNotInitializedError, StagehandResponseBodyError, StagehandResponseParseError, type StagehandScreenshotOptions, StagehandServerError, StagehandShadowRootMissingError, StagehandShadowSegmentEmptyError, StagehandShadowSegmentNotFoundError, type ToolUseItem, type TreeResult, UnsupportedAISDKModelProviderError, UnsupportedModelError, UnsupportedModelProviderError, XPathResolutionError, type ZodPathSegments, ZodSchemaValidationError, connectToMCPServer, defaultExtractSchema, getAccessibilityTree, pageTextSchema };
1251
+ export { AISdkClient, type ActOptions, type ActResult, type ActToolResult, type ActionExecutionResult, type AgentAction$1 as AgentAction, type AgentClientOptions, type AgentConfig, type AgentExecuteOptions$1 as AgentExecuteOptions, type AgentExecuteParams, type AgentExecutionOptions, type AgentHandlerOptions, type AgentInstance, type AgentOptions$1 as AgentOptions, type AgentProviderType, type AgentResult$1 as AgentResult, AgentScreenshotProviderError, type AgentType, AnnotatedScreenshotText, type AnthropicContentBlock, type AnthropicJsonSchemaObject, type AnthropicMessage, type AnthropicTextBlock, type AnthropicToolResult, type AvailableModel, AvailableModelSchema, type Browser, type BrowserContext, type BrowserResult, BrowserbaseSessionNotFoundError, CaptchaTimeoutError, type ChatCompletionOptions, type ChatMessage, type ChatMessageContent, type ChatMessageImageContent, type ChatMessageTextContent, type ClientOptions, type ComputerCallItem, type ConstructorParams, ContentFrameNotFoundError, type CreateChatCompletionOptions, CreateChatCompletionResponseError, ExperimentalApiConflictError, ExperimentalNotConfiguredError, type ExtractOptions, type ExtractResult, type FunctionCallItem, type GotoOptions, HandlerNotInitializedError, type HistoryEntry, type InitResult, InvalidAISDKModelFormatError, LLMClient, type LLMResponse, LLMResponseError, LOG_LEVEL_NAMES, type LocalBrowserLaunchOptions, type LogLevel, type LogLine, type Logger, MCPConnectionError, MissingEnvironmentVariableError, MissingLLMConfigurationError, type ModelProvider, type ObserveOptions, type ObserveResult$1 as ObserveResult, type Page, PlaywrightCommandException, PlaywrightCommandMethodNotSupportedException, type ResponseInputItem, type ResponseItem, Stagehand, StagehandAPIError, StagehandAPIUnauthorizedError, StagehandClickError, StagehandDefaultError, StagehandDomProcessError, StagehandElementNotFoundError, StagehandEnvironmentError, StagehandError, StagehandEvalError, StagehandFunctionName, StagehandHttpError, StagehandIframeError, StagehandInitError, StagehandInvalidArgumentError, type StagehandMetrics, StagehandMissingArgumentError, StagehandNotInitializedError, StagehandResponseBodyError, StagehandResponseParseError, type StagehandScreenshotOptions, StagehandServerError, StagehandShadowRootMissingError, StagehandShadowSegmentEmptyError, StagehandShadowSegmentNotFoundError, StagehandTargetClosedError, type ToolUseItem, type TreeResult, UnsupportedAISDKModelProviderError, UnsupportedModelError, UnsupportedModelProviderError, XPathResolutionError, type ZodPathSegments, ZodSchemaValidationError, connectToMCPServer, defaultExtractSchema, getAccessibilityTree, pageTextSchema };
package/dist/index.js CHANGED
@@ -466,6 +466,7 @@ __export(index_exports, {
466
466
  StagehandShadowRootMissingError: () => StagehandShadowRootMissingError,
467
467
  StagehandShadowSegmentEmptyError: () => StagehandShadowSegmentEmptyError,
468
468
  StagehandShadowSegmentNotFoundError: () => StagehandShadowSegmentNotFoundError,
469
+ StagehandTargetClosedError: () => StagehandTargetClosedError,
469
470
  UnsupportedAISDKModelProviderError: () => UnsupportedAISDKModelProviderError,
470
471
  UnsupportedModelError: () => UnsupportedModelError,
471
472
  UnsupportedModelProviderError: () => UnsupportedModelProviderError,
@@ -494,7 +495,7 @@ var StagehandFunctionName = /* @__PURE__ */ ((StagehandFunctionName2) => {
494
495
  })(StagehandFunctionName || {});
495
496
 
496
497
  // lib/version.ts
497
- var STAGEHAND_VERSION = "2.5.7";
498
+ var STAGEHAND_VERSION = "2.5.8";
498
499
 
499
500
  // types/stagehandErrors.ts
500
501
  var StagehandError = class extends Error {
@@ -568,6 +569,15 @@ var StagehandNotInitializedError = class extends StagehandError {
568
569
  );
569
570
  }
570
571
  };
572
+ var StagehandTargetClosedError = class extends StagehandError {
573
+ constructor(error) {
574
+ const message = error instanceof Error || error instanceof StagehandError ? `Target closed before CDP session could attach: ${error.message}` : "Target closed before CDP session could attach";
575
+ super(message);
576
+ if (error instanceof Error || error instanceof StagehandError) {
577
+ this.causedBy = error;
578
+ }
579
+ }
580
+ };
571
581
  var BrowserbaseSessionNotFoundError = class extends StagehandError {
572
582
  constructor() {
573
583
  super("No Browserbase session ID found");
@@ -2154,6 +2164,18 @@ var ID_PATTERN = /^\d+-\d+$/;
2154
2164
  // lib/utils.ts
2155
2165
  var import_genai = require("@google/genai");
2156
2166
  var import_v33 = require("zod/v3");
2167
+ var TARGET_GONE_ERROR_SNIPPETS = [
2168
+ "No target with given id found",
2169
+ "Target closed"
2170
+ ];
2171
+ function isTargetGoneError(err) {
2172
+ const msg = err instanceof Error ? err.message : String(err);
2173
+ return TARGET_GONE_ERROR_SNIPPETS.some((snippet) => msg.includes(snippet));
2174
+ }
2175
+ function isTargetGone(target) {
2176
+ if (typeof target.isClosed === "function" && target.isClosed()) return true;
2177
+ return typeof target.isDetached === "function" && target.isDetached();
2178
+ }
2157
2179
  function validateZodSchema(schema, data) {
2158
2180
  const result = schema.safeParse(data);
2159
2181
  if (result.success) {
@@ -4624,12 +4646,24 @@ var StagehandPage = class _StagehandPage {
4624
4646
  return __async(this, arguments, function* (target = this.page) {
4625
4647
  var _a15;
4626
4648
  const cached = this.cdpClients.get(target);
4627
- if (cached) return cached;
4649
+ if (cached) {
4650
+ if (isTargetGone(target)) {
4651
+ this.cdpClients.delete(target);
4652
+ throw new StagehandTargetClosedError();
4653
+ }
4654
+ return cached;
4655
+ }
4656
+ if (isTargetGone(target)) {
4657
+ throw new StagehandTargetClosedError();
4658
+ }
4628
4659
  try {
4629
4660
  const session = yield this.context.newCDPSession(target);
4630
4661
  this.cdpClients.set(target, session);
4631
4662
  return session;
4632
4663
  } catch (err) {
4664
+ if (isTargetGone(target) || isTargetGoneError(err)) {
4665
+ throw new StagehandTargetClosedError(err);
4666
+ }
4633
4667
  const msg = (_a15 = err.message) != null ? _a15 : "";
4634
4668
  if (msg.includes("does not have a separate CDP session")) {
4635
4669
  const rootSession = yield this.getCDPClient(this.page);
@@ -4726,6 +4760,9 @@ var _StagehandContext = class _StagehandContext {
4726
4760
  }
4727
4761
  createStagehandPage(page) {
4728
4762
  return __async(this, null, function* () {
4763
+ if (page.isClosed()) {
4764
+ throw new StagehandTargetClosedError();
4765
+ }
4729
4766
  const stagehandPage = yield new StagehandPage(
4730
4767
  page,
4731
4768
  this.stagehand,
@@ -4766,9 +4803,18 @@ var _StagehandContext = class _StagehandContext {
4766
4803
  }));
4767
4804
  const existingPages = context.pages();
4768
4805
  for (const page of existingPages) {
4769
- const stagehandPage = yield instance.createStagehandPage(page);
4806
+ if (page.isClosed()) continue;
4807
+ let stagehandPage;
4808
+ try {
4809
+ stagehandPage = yield instance.createStagehandPage(page);
4810
+ } catch (err) {
4811
+ if (err instanceof StagehandTargetClosedError || isTargetGoneError(err) || page.isClosed()) {
4812
+ continue;
4813
+ }
4814
+ throw err;
4815
+ }
4770
4816
  yield instance.attachFrameNavigatedListener(page);
4771
- if (!instance.activeStagehandPage) {
4817
+ if (!instance.activeStagehandPage && stagehandPage) {
4772
4818
  instance.setActivePage(stagehandPage);
4773
4819
  }
4774
4820
  }
@@ -4817,9 +4863,17 @@ var _StagehandContext = class _StagehandContext {
4817
4863
  }
4818
4864
  handleNewPlaywrightPage(pwPage) {
4819
4865
  return __async(this, null, function* () {
4866
+ if (pwPage.isClosed()) return;
4820
4867
  let stagehandPage = this.pageMap.get(pwPage);
4821
4868
  if (!stagehandPage) {
4822
- stagehandPage = yield this.createStagehandPage(pwPage);
4869
+ try {
4870
+ stagehandPage = yield this.createStagehandPage(pwPage);
4871
+ } catch (err) {
4872
+ if (err instanceof StagehandTargetClosedError || isTargetGoneError(err) || pwPage.isClosed()) {
4873
+ return;
4874
+ }
4875
+ throw err;
4876
+ }
4823
4877
  }
4824
4878
  this.setActivePage(stagehandPage);
4825
4879
  });
@@ -4828,7 +4882,14 @@ var _StagehandContext = class _StagehandContext {
4828
4882
  return __async(this, null, function* () {
4829
4883
  const shPage = this.pageMap.get(pwPage);
4830
4884
  if (!shPage) return;
4831
- const session = yield this.intContext.newCDPSession(pwPage);
4885
+ if (pwPage.isClosed()) return;
4886
+ let session;
4887
+ try {
4888
+ session = yield this.intContext.newCDPSession(pwPage);
4889
+ } catch (err) {
4890
+ if (pwPage.isClosed() || isTargetGoneError(err)) return;
4891
+ throw err;
4892
+ }
4832
4893
  yield session.send("Page.enable");
4833
4894
  pwPage.once("close", () => {
4834
4895
  if (shPage.frameId) this.unregisterFrameId(shPage.frameId);
@@ -24967,6 +25028,7 @@ var Stagehand3 = class {
24967
25028
  StagehandShadowRootMissingError,
24968
25029
  StagehandShadowSegmentEmptyError,
24969
25030
  StagehandShadowSegmentNotFoundError,
25031
+ StagehandTargetClosedError,
24970
25032
  UnsupportedAISDKModelProviderError,
24971
25033
  UnsupportedModelError,
24972
25034
  UnsupportedModelProviderError,
@@ -3,6 +3,12 @@ import { z, ZodTypeAny } from "zod/v3";
3
3
  import { LogLine } from "../types/log";
4
4
  import { ModelProvider } from "../types/model";
5
5
  import { ZodPathSegments } from "../types/stagehand";
6
+ export declare const TARGET_GONE_ERROR_SNIPPETS: string[];
7
+ export declare function isTargetGoneError(err: unknown): boolean;
8
+ export declare function isTargetGone(target: {
9
+ isClosed?: () => boolean;
10
+ isDetached?: () => boolean;
11
+ }): boolean;
6
12
  export declare function validateZodSchema(schema: z.ZodTypeAny, data: unknown): boolean;
7
13
  /**
8
14
  * Detects if the code is running in the Bun runtime environment.
@@ -2,4 +2,4 @@
2
2
  * AUTO-GENERATED — DO NOT EDIT BY HAND
3
3
  * Run `pnpm run gen-version` to refresh.
4
4
  */
5
- export declare const STAGEHAND_VERSION: "2.5.7";
5
+ export declare const STAGEHAND_VERSION: "2.5.8";
@@ -27,6 +27,10 @@ export declare class InvalidAISDKModelFormatError extends StagehandError {
27
27
  export declare class StagehandNotInitializedError extends StagehandError {
28
28
  constructor(prop: string);
29
29
  }
30
+ export declare class StagehandTargetClosedError extends StagehandError {
31
+ causedBy?: Error | StagehandError;
32
+ constructor(error?: unknown);
33
+ }
30
34
  export declare class BrowserbaseSessionNotFoundError extends StagehandError {
31
35
  constructor();
32
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@browserbasehq/stagehand",
3
- "version": "2.5.7",
3
+ "version": "2.5.8",
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",