|
1 | 1 | import type { CDPSession, Page as PlaywrightPage, Frame } from "playwright"; |
2 | 2 | import { selectors } from "playwright"; |
3 | 3 | import { z } from "zod/v3"; |
4 | | -import { Page, defaultExtractSchema } from "../types/page"; |
| 4 | +import { |
| 5 | + Page, |
| 6 | + defaultExtractSchema, |
| 7 | + StagehandScreenshotOptions, |
| 8 | +} from "../types/page"; |
5 | 9 | import { |
6 | 10 | ExtractOptions, |
7 | 11 | ExtractResult, |
@@ -415,37 +419,41 @@ ${scriptContent} \ |
415 | 419 | } |
416 | 420 |
|
417 | 421 | // Handle screenshots with CDP |
418 | | - if (prop === "screenshot" && this.stagehand.env === "BROWSERBASE") { |
419 | | - return async ( |
420 | | - options: { |
421 | | - type?: "png" | "jpeg"; |
422 | | - quality?: number; |
423 | | - fullPage?: boolean; |
424 | | - clip?: { x: number; y: number; width: number; height: number }; |
425 | | - omitBackground?: boolean; |
426 | | - } = {}, |
427 | | - ) => { |
428 | | - const cdpOptions: Record<string, unknown> = { |
429 | | - format: options.type === "jpeg" ? "jpeg" : "png", |
430 | | - quality: options.quality, |
431 | | - clip: options.clip, |
432 | | - omitBackground: options.omitBackground, |
433 | | - fromSurface: true, |
434 | | - }; |
435 | | - |
436 | | - if (options.fullPage) { |
437 | | - cdpOptions.captureBeyondViewport = true; |
438 | | - } |
| 422 | + if (prop === "screenshot") { |
| 423 | + return async (options: StagehandScreenshotOptions = {}) => { |
| 424 | + const rawScreenshot: typeof target.screenshot = |
| 425 | + Object.getPrototypeOf(target).screenshot.bind(target); |
| 426 | + |
| 427 | + const { |
| 428 | + useCDP = this.stagehand.env === "BROWSERBASE", |
| 429 | + ...playwrightOptions |
| 430 | + } = options; |
| 431 | + |
| 432 | + if (useCDP && this.stagehand.env === "BROWSERBASE") { |
| 433 | + const cdpOptions: Record<string, unknown> = { |
| 434 | + format: options.type === "jpeg" ? "jpeg" : "png", |
| 435 | + quality: options.quality, |
| 436 | + clip: options.clip, |
| 437 | + omitBackground: options.omitBackground, |
| 438 | + fromSurface: true, |
| 439 | + }; |
| 440 | + |
| 441 | + if (options.fullPage) { |
| 442 | + cdpOptions.captureBeyondViewport = true; |
| 443 | + } |
439 | 444 |
|
440 | | - const data = await this.sendCDP<{ data: string }>( |
441 | | - "Page.captureScreenshot", |
442 | | - cdpOptions, |
443 | | - ); |
| 445 | + const data = await this.sendCDP<{ data: string }>( |
| 446 | + "Page.captureScreenshot", |
| 447 | + cdpOptions, |
| 448 | + ); |
444 | 449 |
|
445 | | - // Convert base64 to buffer |
446 | | - const buffer = Buffer.from(data.data, "base64"); |
| 450 | + // Convert base64 to buffer |
| 451 | + const buffer = Buffer.from(data.data, "base64"); |
447 | 452 |
|
448 | | - return buffer; |
| 453 | + return buffer; |
| 454 | + } else { |
| 455 | + return await rawScreenshot(playwrightOptions); |
| 456 | + } |
449 | 457 | }; |
450 | 458 | } |
451 | 459 |
|
@@ -739,7 +747,6 @@ ${scriptContent} \ |
739 | 747 | const result = await this.api.act({ |
740 | 748 | ...observeResult, |
741 | 749 | frameId: this.rootFrameId, |
742 | | - modelClientOptions: this.stagehand["modelClientOptions"], |
743 | 750 | }); |
744 | 751 | this.stagehand.addToHistory("act", observeResult, result); |
745 | 752 | return result; |
@@ -837,10 +844,7 @@ ${scriptContent} \ |
837 | 844 | if (!instructionOrOptions) { |
838 | 845 | let result: ExtractResult<T>; |
839 | 846 | if (this.api) { |
840 | | - result = await this.api.extract<T>({ |
841 | | - frameId: this.rootFrameId, |
842 | | - modelClientOptions: this.stagehand["modelClientOptions"], |
843 | | - }); |
| 847 | + result = await this.api.extract<T>({ frameId: this.rootFrameId }); |
844 | 848 | } else { |
845 | 849 | result = await this.extractHandler.extract(); |
846 | 850 | } |
|
0 commit comments