@crawlee/stagehand 4.0.0-beta.33 → 4.0.0-beta.331

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.
@@ -1,13 +1,11 @@
1
1
  import type { Action, ActOptions, ActResult, AgentConfig, ExtractOptions, LLMClient, ModelConfiguration, NonStreamingAgentInstance, ObserveOptions, Stagehand, StreamingAgentInstance } from '@browserbasehq/stagehand';
2
- import type { BrowserCrawlerOptions, BrowserCrawlingContext, BrowserHook, ContextPipeline, CrawlingContext, GetUserDataFromRequest, LoadedContext, RequestHandler, RouterRoutes } from '@crawlee/browser';
2
+ import type { BrowserCrawlerOptions, BrowserCrawlingContext, BrowserHook, GetUserDataFromRequest, RequestHandler, RouterHandler, RouterRoutes, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
3
3
  import { BrowserCrawler } from '@crawlee/browser';
4
4
  import type { Dictionary } from '@crawlee/types';
5
5
  // @ts-ignore optional peer dependency or compatibility with es2022
6
- import type { LaunchOptions, Page, Response } from 'playwright';
7
- import type { z } from 'zod';
8
- import type { StagehandController } from './stagehand-controller';
9
- import type { StagehandLaunchContext } from './stagehand-launcher';
10
- import type { StagehandPlugin } from './stagehand-plugin';
6
+ import type { Page, Response } from 'playwright';
7
+ import { z } from 'zod';
8
+ import type { StagehandLaunchContext } from './stagehand-launcher.js';
11
9
  /**
12
10
  * Stagehand-specific configuration options.
13
11
  */
@@ -133,7 +131,7 @@ export interface StagehandPage extends Page {
133
131
  * );
134
132
  * ```
135
133
  */
136
- extract<T>(instruction: string, schema: z.ZodSchema<T>, options?: Omit<ExtractOptions, 'page'>): Promise<T>;
134
+ extract<T>(instruction: string, schema: z.ZodType<T>, options?: Omit<ExtractOptions, 'page'>): Promise<T>;
137
135
  /**
138
136
  * Observe the page and get AI-suggested actions.
139
137
  *
@@ -169,7 +167,11 @@ export interface StagehandPage extends Page {
169
167
  /**
170
168
  * Crawling context for StagehandCrawler with enhanced page object.
171
169
  */
172
- export interface StagehandCrawlingContext<UserData extends Dictionary = Dictionary> extends BrowserCrawlingContext<StagehandPage, Response, StagehandController, UserData> {
170
+ /**
171
+ * Goto options for StagehandCrawler navigation.
172
+ */
173
+ export type StagehandGotoOptions = NonNullable<Parameters<Page['goto']>[1]>;
174
+ export interface StagehandCrawlingContext<UserData extends Dictionary = any> extends BrowserCrawlingContext<StagehandPage, Response, UserData, StagehandGotoOptions> {
173
175
  /**
174
176
  * Enhanced Playwright page with Stagehand AI methods.
175
177
  * Use page.act(), page.extract(), page.observe(), page.agent() for AI-powered operations.
@@ -184,23 +186,11 @@ export interface StagehandCrawlingContext<UserData extends Dictionary = Dictiona
184
186
  /**
185
187
  * Hook function for StagehandCrawler.
186
188
  */
187
- export interface StagehandHook extends BrowserHook<StagehandCrawlingContext, StagehandGotoOptions> {
188
- }
189
- /**
190
- * Request handler for StagehandCrawler.
191
- */
192
- export interface StagehandRequestHandler extends RequestHandler<LoadedContext<StagehandCrawlingContext>> {
193
- }
194
- /**
195
- * Goto options for StagehandCrawler navigation.
196
- */
197
- export type StagehandGotoOptions = Dictionary & Parameters<Page['goto']>[1];
189
+ export type StagehandHook<UserData extends Dictionary = any> = BrowserHook<StagehandCrawlingContext<UserData>>;
198
190
  /**
199
191
  * Options for StagehandCrawler.
200
192
  */
201
- export interface StagehandCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends StagehandCrawlingContext = StagehandCrawlingContext & ContextExtension> extends BrowserCrawlerOptions<StagehandPage, Response, StagehandController, StagehandCrawlingContext, ContextExtension, ExtendedContext, {
202
- browserPlugins: [StagehandPlugin];
203
- }> {
193
+ export interface StagehandCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends StagehandCrawlingContext = StagehandCrawlingContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<StagehandCrawlingContext['request']>>, StatisticStateExtension extends object = {}> extends Omit<BrowserCrawlerOptions<StagehandPage, Response, StagehandCrawlingContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension>, 'ignoreShadowRoots' | 'ignoreIframes'> {
204
194
  /**
205
195
  * Stagehand-specific configuration options.
206
196
  * These options configure the AI behavior and Browserbase integration.
@@ -210,13 +200,17 @@ export interface StagehandCrawlerOptions<ContextExtension = Dictionary<never>, E
210
200
  * Launch context with Stagehand-specific options.
211
201
  */
212
202
  launchContext?: StagehandLaunchContext;
203
+ /**
204
+ * Whether to run browser in headless mode. Defaults to `true`.
205
+ * Can be also set via {@link Configuration}.
206
+ */
207
+ headless?: boolean;
213
208
  /**
214
209
  * Function that is called to process each request.
215
210
  *
216
211
  * The function receives the {@link StagehandCrawlingContext} as an argument, where:
217
212
  * - `request` is an instance of the {@link Request} object with details about the URL to open, HTTP method etc.
218
213
  * - `page` is an enhanced Playwright [`Page`](https://playwright.dev/docs/api/class-page) with AI methods
219
- * - `browserController` is an instance of {@link StagehandController}
220
214
  * - `response` is the main resource response as returned by `page.goto(request.url)`
221
215
  * - `stagehand` is the Stagehand instance for advanced control
222
216
  *
@@ -255,15 +249,15 @@ export interface StagehandCrawlerOptions<ContextExtension = Dictionary<never>, E
255
249
  * }
256
250
  * ```
257
251
  */
258
- requestHandler?: StagehandRequestHandler;
252
+ requestHandler?: RouterHandler<ExtendedContext, Routes> | RequestHandler<ExtendedContext>;
259
253
  /**
260
254
  * Async functions that are sequentially evaluated before the navigation.
261
255
  */
262
- preNavigationHooks?: StagehandHook[];
256
+ preNavigationHooks?: BrowserHook<StagehandCrawlingContext<GetUserDataFromRequest<ExtendedContext['request']>>, ContextExtension>[];
263
257
  /**
264
258
  * Async functions that are sequentially evaluated after the navigation.
265
259
  */
266
- postNavigationHooks?: StagehandHook[];
260
+ postNavigationHooks?: BrowserHook<StagehandCrawlingContext<GetUserDataFromRequest<ExtendedContext['request']>>, ContextExtension>[];
267
261
  }
268
262
  /**
269
263
  * StagehandCrawler provides AI-powered web crawling using Browserbase's Stagehand library.
@@ -314,111 +308,139 @@ export interface StagehandCrawlerOptions<ContextExtension = Dictionary<never>, E
314
308
  * await crawler.run(['https://example.com']);
315
309
  * ```
316
310
  */
317
- export declare class StagehandCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends StagehandCrawlingContext = StagehandCrawlingContext & ContextExtension> extends BrowserCrawler<StagehandPage, Response, StagehandController, {
318
- browserPlugins: [StagehandPlugin];
319
- }, LaunchOptions, StagehandCrawlingContext, ContextExtension, ExtendedContext> {
311
+ export declare class StagehandCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends StagehandCrawlingContext = StagehandCrawlingContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<StagehandCrawlingContext['request']>>, StatisticStateExtension extends object = {}> extends BrowserCrawler<StagehandPage, Response, StagehandCrawlingContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension> {
312
+ #private;
313
+ /**
314
+ * @internal
315
+ */
320
316
  protected static optionsShape: {
321
- // @ts-ignore optional peer dependency or compatibility with es2022
322
- stagehandOptions: import("ow", { with: { "resolution-mode": "import" } }).ObjectPredicate<object> & import("ow", { with: { "resolution-mode": "import" } }).BasePredicate<object | undefined>;
323
- // @ts-ignore optional peer dependency or compatibility with es2022
324
- browserPoolOptions: import("ow", { with: { "resolution-mode": "import" } }).ObjectPredicate<object> & import("ow", { with: { "resolution-mode": "import" } }).BasePredicate<object | undefined>;
325
- // @ts-ignore optional peer dependency or compatibility with es2022
326
- navigationTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
327
- // @ts-ignore optional peer dependency or compatibility with es2022
328
- preNavigationHooks: import("ow").ArrayPredicate<unknown> & import("ow").BasePredicate<unknown[] | undefined>;
329
- // @ts-ignore optional peer dependency or compatibility with es2022
330
- postNavigationHooks: import("ow").ArrayPredicate<unknown> & import("ow").BasePredicate<unknown[] | undefined>;
331
- // @ts-ignore optional peer dependency or compatibility with es2022
332
- launchContext: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
333
- // @ts-ignore optional peer dependency or compatibility with es2022
334
- headless: import("ow").AnyPredicate<string | boolean>;
335
- // @ts-ignore optional peer dependency or compatibility with es2022
336
- sessionPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
337
- // @ts-ignore optional peer dependency or compatibility with es2022
338
- persistCookiesPerSession: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
339
- // @ts-ignore optional peer dependency or compatibility with es2022
340
- useSessionPool: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
341
- // @ts-ignore optional peer dependency or compatibility with es2022
342
- proxyConfiguration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
343
- // @ts-ignore optional peer dependency or compatibility with es2022
344
- contextPipelineBuilder: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
345
- // @ts-ignore optional peer dependency or compatibility with es2022
346
- extendContext: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
347
- // @ts-ignore optional peer dependency or compatibility with es2022
348
- requestList: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
349
- // @ts-ignore optional peer dependency or compatibility with es2022
350
- requestQueue: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
351
- // @ts-ignore optional peer dependency or compatibility with es2022
352
- requestHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
353
- // @ts-ignore optional peer dependency or compatibility with es2022
354
- requestHandlerTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
355
- // @ts-ignore optional peer dependency or compatibility with es2022
356
- errorHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
357
- // @ts-ignore optional peer dependency or compatibility with es2022
358
- failedRequestHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
359
- // @ts-ignore optional peer dependency or compatibility with es2022
360
- maxRequestRetries: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
361
- // @ts-ignore optional peer dependency or compatibility with es2022
362
- sameDomainDelaySecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
363
- // @ts-ignore optional peer dependency or compatibility with es2022
364
- maxSessionRotations: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
365
- // @ts-ignore optional peer dependency or compatibility with es2022
366
- maxRequestsPerCrawl: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
367
- // @ts-ignore optional peer dependency or compatibility with es2022
368
- maxCrawlDepth: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
369
- // @ts-ignore optional peer dependency or compatibility with es2022
370
- autoscaledPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
371
- // @ts-ignore optional peer dependency or compatibility with es2022
372
- statusMessageLoggingInterval: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
373
- // @ts-ignore optional peer dependency or compatibility with es2022
374
- statusMessageCallback: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
375
- // @ts-ignore optional peer dependency or compatibility with es2022
376
- retryOnBlocked: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
377
- // @ts-ignore optional peer dependency or compatibility with es2022
378
- respectRobotsTxtFile: import("ow").AnyPredicate<boolean | object>;
379
- // @ts-ignore optional peer dependency or compatibility with es2022
380
- onSkippedRequest: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
381
- // @ts-ignore optional peer dependency or compatibility with es2022
382
- httpClient: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
383
- // @ts-ignore optional peer dependency or compatibility with es2022
384
- configuration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
385
- // @ts-ignore optional peer dependency or compatibility with es2022
386
- storageClient: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
387
- // @ts-ignore optional peer dependency or compatibility with es2022
388
- eventManager: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
389
- // @ts-ignore optional peer dependency or compatibility with es2022
390
- logger: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
391
- // @ts-ignore optional peer dependency or compatibility with es2022
392
- minConcurrency: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
393
- // @ts-ignore optional peer dependency or compatibility with es2022
394
- maxConcurrency: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
395
- // @ts-ignore optional peer dependency or compatibility with es2022
396
- maxRequestsPerMinute: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
397
- // @ts-ignore optional peer dependency or compatibility with es2022
398
- keepAlive: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
399
- // @ts-ignore optional peer dependency or compatibility with es2022
400
- experiments: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
401
- // @ts-ignore optional peer dependency or compatibility with es2022
402
- statisticsOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
403
- // @ts-ignore optional peer dependency or compatibility with es2022
404
- id: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
317
+ contextPipelineBuilder: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
318
+ extendContext: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
319
+ requestList: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
320
+ requestQueue: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
321
+ requestManager: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
322
+ requestHandler: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
323
+ requestHandlerTimeoutSecs: z.ZodOptional<z.ZodCustom<number, number>>;
324
+ errorHandler: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
325
+ failedRequestHandler: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
326
+ maxRequestRetries: z.ZodDefault<z.ZodCustom<number, number>>;
327
+ sameDomainDelaySecs: z.ZodDefault<z.ZodCustom<number, number>>;
328
+ maxRequestsPerCrawl: z.ZodOptional<z.ZodCustom<number, number>>;
329
+ maxCrawlDepth: z.ZodOptional<z.ZodCustom<number, number>>;
330
+ taskLoopOptions: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
331
+ concurrencySystem: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
332
+ sessionPool: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
333
+ statusMessageLoggingInterval: z.ZodDefault<z.ZodCustom<number, number>>;
334
+ statusMessageCallback: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
335
+ additionalHttpErrorStatusCodes: z.ZodDefault<z.ZodArray<z.ZodCustom<number, number>>>;
336
+ ignoreHttpErrorStatusCodes: z.ZodDefault<z.ZodArray<z.ZodCustom<number, number>>>;
337
+ blockedStatusCodes: z.ZodOptional<z.ZodArray<z.ZodCustom<number, number>>>;
338
+ retryOnBlocked: z.ZodDefault<z.ZodBoolean>;
339
+ respectRobotsTxtFile: z.ZodDefault<z.ZodUnion<readonly [z.ZodBoolean, z.ZodCustom<Dictionary, Dictionary>]>>;
340
+ transactionalStorage: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
341
+ requestQueue: z.ZodOptional<z.ZodEnum<{
342
+ deferred: "deferred";
343
+ writeThrough: "writeThrough";
344
+ }>>;
345
+ }, z.core.$strict>]>>;
346
+ onSkippedRequest: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
347
+ // @ts-ignore optional peer dependency or compatibility with es2022
348
+ httpClient: z.ZodOptional<z.ZodInstanceOf<import("@crawlee/http-client").BaseHttpClient>>;
349
+ // @ts-ignore optional peer dependency or compatibility with es2022
350
+ configuration: z.ZodOptional<z.ZodInstanceOf<import("@crawlee/browser").Configuration>>;
351
+ storageBackend: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
352
+ logger: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
353
+ minConcurrency: z.ZodOptional<z.ZodCustom<number, number>>;
354
+ maxConcurrency: z.ZodOptional<z.ZodCustom<number, number>>;
355
+ initialConcurrency: z.ZodOptional<z.ZodCustom<number, number>>;
356
+ maxRequestsPerMinute: z.ZodOptional<z.ZodCustom<number, number>>;
357
+ keepAlive: z.ZodOptional<z.ZodBoolean>;
358
+ statistics: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
359
+ id: z.ZodOptional<z.ZodString>;
360
+ navigationTimeoutSecs: z.ZodDefault<z.ZodCustom<number, number>>;
361
+ preNavigationHooks: z.ZodDefault<z.ZodCustom<unknown[], unknown[]>>;
362
+ postNavigationHooks: z.ZodDefault<z.ZodCustom<unknown[], unknown[]>>;
363
+ browserPool: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
364
+ browserPoolBuilder: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
365
+ remoteBrowser: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
366
+ saveResponseCookies: z.ZodDefault<z.ZodBoolean>;
367
+ proxyConfiguration: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
368
+ ignoreIframes: z.ZodDefault<z.ZodBoolean>;
369
+ ignoreShadowRoots: z.ZodDefault<z.ZodBoolean>;
370
+ launchContext: z.ZodDefault<z.ZodCustom<Dictionary, Dictionary>>;
371
+ stagehandOptions: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
372
+ headless: z.ZodOptional<z.ZodBoolean>;
405
373
  };
374
+ /** @internal */
375
+ protected static optionsSchema: z.ZodObject<{
376
+ contextPipelineBuilder: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
377
+ extendContext: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
378
+ requestList: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
379
+ requestQueue: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
380
+ requestManager: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
381
+ requestHandler: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
382
+ requestHandlerTimeoutSecs: z.ZodOptional<z.ZodCustom<number, number>>;
383
+ errorHandler: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
384
+ failedRequestHandler: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
385
+ maxRequestRetries: z.ZodDefault<z.ZodCustom<number, number>>;
386
+ sameDomainDelaySecs: z.ZodDefault<z.ZodCustom<number, number>>;
387
+ maxRequestsPerCrawl: z.ZodOptional<z.ZodCustom<number, number>>;
388
+ maxCrawlDepth: z.ZodOptional<z.ZodCustom<number, number>>;
389
+ taskLoopOptions: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
390
+ concurrencySystem: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
391
+ sessionPool: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
392
+ statusMessageLoggingInterval: z.ZodDefault<z.ZodCustom<number, number>>;
393
+ statusMessageCallback: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
394
+ additionalHttpErrorStatusCodes: z.ZodDefault<z.ZodArray<z.ZodCustom<number, number>>>;
395
+ ignoreHttpErrorStatusCodes: z.ZodDefault<z.ZodArray<z.ZodCustom<number, number>>>;
396
+ blockedStatusCodes: z.ZodOptional<z.ZodArray<z.ZodCustom<number, number>>>;
397
+ retryOnBlocked: z.ZodDefault<z.ZodBoolean>;
398
+ respectRobotsTxtFile: z.ZodDefault<z.ZodUnion<readonly [z.ZodBoolean, z.ZodCustom<Dictionary, Dictionary>]>>;
399
+ transactionalStorage: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
400
+ requestQueue: z.ZodOptional<z.ZodEnum<{
401
+ deferred: "deferred";
402
+ writeThrough: "writeThrough";
403
+ }>>;
404
+ }, z.core.$strict>]>>;
405
+ onSkippedRequest: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
406
+ // @ts-ignore optional peer dependency or compatibility with es2022
407
+ httpClient: z.ZodOptional<z.ZodInstanceOf<import("@crawlee/http-client").BaseHttpClient>>;
408
+ // @ts-ignore optional peer dependency or compatibility with es2022
409
+ configuration: z.ZodOptional<z.ZodInstanceOf<import("@crawlee/browser").Configuration>>;
410
+ storageBackend: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
411
+ logger: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
412
+ minConcurrency: z.ZodOptional<z.ZodCustom<number, number>>;
413
+ maxConcurrency: z.ZodOptional<z.ZodCustom<number, number>>;
414
+ initialConcurrency: z.ZodOptional<z.ZodCustom<number, number>>;
415
+ maxRequestsPerMinute: z.ZodOptional<z.ZodCustom<number, number>>;
416
+ keepAlive: z.ZodOptional<z.ZodBoolean>;
417
+ statistics: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
418
+ id: z.ZodOptional<z.ZodString>;
419
+ navigationTimeoutSecs: z.ZodDefault<z.ZodCustom<number, number>>;
420
+ preNavigationHooks: z.ZodDefault<z.ZodCustom<unknown[], unknown[]>>;
421
+ postNavigationHooks: z.ZodDefault<z.ZodCustom<unknown[], unknown[]>>;
422
+ browserPool: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
423
+ browserPoolBuilder: z.ZodOptional<z.ZodCustom<(...args: any[]) => unknown, (...args: any[]) => unknown>>;
424
+ remoteBrowser: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
425
+ saveResponseCookies: z.ZodDefault<z.ZodBoolean>;
426
+ proxyConfiguration: z.ZodOptional<z.ZodType<Dictionary<any>, unknown, z.core.$ZodTypeInternals<Dictionary<any>, unknown>>>;
427
+ ignoreIframes: z.ZodDefault<z.ZodBoolean>;
428
+ ignoreShadowRoots: z.ZodDefault<z.ZodBoolean>;
429
+ launchContext: z.ZodDefault<z.ZodCustom<Dictionary, Dictionary>>;
430
+ stagehandOptions: z.ZodOptional<z.ZodCustom<Dictionary, Dictionary>>;
431
+ headless: z.ZodOptional<z.ZodBoolean>;
432
+ }, z.core.$strict>;
406
433
  /**
407
434
  * Creates a new instance of StagehandCrawler.
408
435
  *
409
436
  * @param options - Crawler configuration options
410
437
  */
411
- constructor(options?: StagehandCrawlerOptions<ContextExtension, ExtendedContext>);
412
- protected buildContextPipeline(): ContextPipeline<CrawlingContext, StagehandCrawlingContext>;
413
- /**
414
- * Enhance the page with Stagehand AI methods.
415
- */
416
- private setUpStagehand;
438
+ constructor(options?: StagehandCrawlerOptions<ContextExtension, ExtendedContext, Routes, StatisticStateExtension>);
417
439
  /**
418
440
  * Navigation handler for Stagehand crawler.
419
441
  * Uses standard Playwright navigation.
420
442
  */
421
- protected _navigationHandler(crawlingContext: StagehandCrawlingContext, gotoOptions: StagehandGotoOptions): Promise<Response | null>;
443
+ protected navigationHandler(crawlingContext: StagehandCrawlingContext, gotoOptions: StagehandGotoOptions): Promise<Response | null>;
422
444
  }
423
445
  /**
424
446
  * Creates a new router for StagehandCrawler with type-safe route handlers.
@@ -436,7 +458,7 @@ export declare class StagehandCrawler<ContextExtension = Dictionary<never>, Exte
436
458
  * });
437
459
  *
438
460
  * router.addDefaultHandler(async ({ page, enqueueLinks }) => {
439
- * await enqueueLinks({ globs: ['https://example.com/products/*'] });
461
+ * await enqueueLinks({ include: ['https://example.com/products/*'] });
440
462
  * });
441
463
  *
442
464
  * const crawler = new StagehandCrawler({
@@ -444,6 +466,6 @@ export declare class StagehandCrawler<ContextExtension = Dictionary<never>, Exte
444
466
  * });
445
467
  * ```
446
468
  */
447
- // @ts-ignore optional peer dependency or compatibility with es2022
448
- export declare function createStagehandRouter<Context extends StagehandCrawlingContext = StagehandCrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, UserData>): import("packages/browser-crawler/dist/index.js", { with: { "resolution-mode": "import" } }).RouterHandler<Context>;
449
- //# sourceMappingURL=stagehand-crawler.d.ts.map
469
+ export declare function createStagehandRouter<Context extends StagehandCrawlingContext = StagehandCrawlingContext, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<Context['request']>>>(routes?: RouterRoutes<Context, Routes>): RouterHandler<Context, Routes>;
470
+ export declare function createStagehandRouter<Context extends StagehandCrawlingContext = StagehandCrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, Record<string, UserData>>): RouterHandler<Context, Record<string, UserData>>;
471
+ export declare function createStagehandRouter<Context extends StagehandCrawlingContext = StagehandCrawlingContext, const Schemas extends RouteSchemas = RouteSchemas>(schemas: Schemas): RouterHandler<Context, RoutesFromSchemas<Schemas>>;
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StagehandCrawler = void 0;
4
4
  exports.createStagehandRouter = createStagehandRouter;
5
- const tslib_1 = require("tslib");
6
5
  const browser_1 = require("@crawlee/browser");
7
- const ow_1 = tslib_1.__importDefault(require("ow"));
8
- const stagehand_launcher_1 = require("./stagehand-launcher");
9
- const stagehand_utils_1 = require("./utils/stagehand-utils");
6
+ const internal_1 = require("@crawlee/utils/internal");
7
+ const zod_1 = require("zod");
8
+ const stagehand_browser_pool_js_1 = require("./stagehand-browser-pool.js");
9
+ const stagehand_utils_js_1 = require("./utils/stagehand-utils.js");
10
10
  /**
11
11
  * StagehandCrawler provides AI-powered web crawling using Browserbase's Stagehand library.
12
12
  *
@@ -57,84 +57,101 @@ const stagehand_utils_1 = require("./utils/stagehand-utils");
57
57
  * ```
58
58
  */
59
59
  class StagehandCrawler extends browser_1.BrowserCrawler {
60
+ /**
61
+ * @internal
62
+ */
60
63
  static optionsShape = {
61
64
  ...browser_1.BrowserCrawler.optionsShape,
62
- stagehandOptions: ow_1.default.optional.object,
63
- browserPoolOptions: ow_1.default.optional.object,
65
+ launchContext: internal_1.schemas.anyObject.default(() => ({})),
66
+ stagehandOptions: internal_1.schemas.anyObject.optional(),
67
+ headless: zod_1.z.boolean().optional(),
64
68
  };
69
+ /** @internal */
70
+ static optionsSchema = zod_1.z.strictObject(StagehandCrawler.optionsShape);
71
+ /** Only `parseWithCheerio` reads the omitted options, and the Stagehand context has no such helper. */
72
+ static #constructorOptionsSchema = StagehandCrawler.optionsSchema.omit({
73
+ ignoreShadowRoots: true,
74
+ ignoreIframes: true,
75
+ });
65
76
  /**
66
77
  * Creates a new instance of StagehandCrawler.
67
78
  *
68
79
  * @param options - Crawler configuration options
69
80
  */
70
81
  constructor(options = {}) {
71
- (0, ow_1.default)(options, 'StagehandCrawlerOptions', ow_1.default.object.exactShape(StagehandCrawler.optionsShape));
72
- const { stagehandOptions = {}, launchContext = {}, contextPipelineBuilder, ...browserCrawlerOptions } = options;
73
- const browserPoolOptions = {
74
- ...options.browserPoolOptions,
75
- };
76
- // Create launcher with Stagehand plugin
77
- const launcher = new stagehand_launcher_1.StagehandLauncher({
78
- ...launchContext,
79
- stagehandOptions,
80
- });
81
- browserPoolOptions.browserPlugins = [launcher.createBrowserPlugin()];
82
- // Initialize BrowserCrawler with Stagehand plugin and fingerprinting enabled
82
+ const parsedOptions = (0, internal_1.parseArgument)(options, StagehandCrawler.#constructorOptionsSchema, 'StagehandCrawlerOptions');
83
+ const { stagehandOptions, launchContext, headless, configuration, ...browserCrawlerOptions } = parsedOptions;
84
+ if (options.browserPool) {
85
+ // The raw options, not the parsed ones: `launchContext` has a default, so by now it is always set.
86
+ (0, internal_1.assertBrowserPoolNotConfigured)(new.target.name, {
87
+ launchContext: options.launchContext,
88
+ stagehandOptions: options.stagehandOptions,
89
+ headless: options.headless,
90
+ });
91
+ }
83
92
  super({
84
93
  ...browserCrawlerOptions,
85
- launchContext,
86
- browserPoolOptions,
87
- contextPipelineBuilder: contextPipelineBuilder ?? (() => this.buildContextPipeline()),
94
+ configuration,
95
+ // The pool serves plain Playwright pages - a page only becomes a `StagehandPage` further down the
96
+ // pipeline, once `setUpStagehand` enhances it - so its page type is narrower than the crawler's.
97
+ browserPoolBuilder: (remoteBrowser) => {
98
+ // Stagehand manages its own browser (local or Browserbase), so a Crawlee-level remote connection
99
+ // would be silently ignored.
100
+ if (remoteBrowser) {
101
+ throw new Error('StagehandCrawler does not support `remoteBrowser`. Use `stagehandOptions.env: "BROWSERBASE"` instead.');
102
+ }
103
+ return (0, stagehand_browser_pool_js_1.stagehandBrowserPool)({
104
+ launchContext,
105
+ stagehandOptions,
106
+ headless,
107
+ configuration,
108
+ });
109
+ },
110
+ contextPipelineBuilder: () => this.#buildContextPipeline(),
88
111
  });
89
112
  }
90
- buildContextPipeline() {
91
- return super.buildContextPipeline().compose({ action: this.setUpStagehand.bind(this) });
113
+ #buildContextPipeline() {
114
+ return this.buildContextPipeline().compose(this.#setUpStagehand.bind(this));
115
+ }
116
+ /**
117
+ * Resolves the {@link StagehandController} that owns the given page, or
118
+ * `undefined` when the pool does not expose controllers (e.g. a custom
119
+ * {@link IBrowserPool} implementation).
120
+ *
121
+ * Stagehand needs direct controller access to reach the `Stagehand`
122
+ * instance bound to the page's browser, which is why it reaches past the
123
+ * {@link IBrowserPool} abstraction here.
124
+ */
125
+ #getBrowserControllerByPage(page) {
126
+ if ('getBrowserControllerByPage' in this.browserPool) {
127
+ return this.browserPool.getBrowserControllerByPage(page);
128
+ }
129
+ return undefined;
92
130
  }
93
131
  /**
94
132
  * Enhance the page with Stagehand AI methods.
95
133
  */
96
- async setUpStagehand(crawlingContext) {
97
- const stagehand = crawlingContext.browserController.getStagehand();
134
+ async #setUpStagehand(crawlingContext) {
135
+ const controller = this.#getBrowserControllerByPage(crawlingContext.page);
136
+ if (!controller) {
137
+ throw new Error('Could not resolve StagehandController for page — is the browser pool configured correctly?');
138
+ }
139
+ const stagehand = controller.getStagehand();
98
140
  return {
99
141
  stagehand,
100
- page: (0, stagehand_utils_1.enhancePageWithStagehand)(crawlingContext.page, stagehand),
142
+ page: (0, stagehand_utils_js_1.enhancePageWithStagehand)(crawlingContext.page, stagehand),
101
143
  };
102
144
  }
103
145
  /**
104
146
  * Navigation handler for Stagehand crawler.
105
147
  * Uses standard Playwright navigation.
106
148
  */
107
- async _navigationHandler(crawlingContext, gotoOptions) {
149
+ async navigationHandler(crawlingContext, gotoOptions) {
108
150
  // Use standard page.goto for navigation
109
151
  return crawlingContext.page.goto(crawlingContext.request.url, gotoOptions);
110
152
  }
111
153
  }
112
154
  exports.StagehandCrawler = StagehandCrawler;
113
- /**
114
- * Creates a new router for StagehandCrawler with type-safe route handlers.
115
- *
116
- * @param options - Router options
117
- * @returns Configured router instance
118
- *
119
- * @example
120
- * ```typescript
121
- * const router = createStagehandRouter();
122
- *
123
- * router.addHandler('product', async ({ page, request, log }) => {
124
- * log.info(`Processing product: ${request.url}`);
125
- * const data = await page.extract('Get product info', schema);
126
- * });
127
- *
128
- * router.addDefaultHandler(async ({ page, enqueueLinks }) => {
129
- * await enqueueLinks({ globs: ['https://example.com/products/*'] });
130
- * });
131
- *
132
- * const crawler = new StagehandCrawler({
133
- * requestHandler: router,
134
- * });
135
- * ```
136
- */
137
- function createStagehandRouter(routes) {
138
- return browser_1.Router.create(routes);
155
+ function createStagehandRouter(routesOrSchemas) {
156
+ return browser_1.Router.create(routesOrSchemas);
139
157
  }
140
- //# sourceMappingURL=stagehand-crawler.js.map