@crawlee/stagehand 3.15.4-beta.39

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.
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.enhancePageWithStagehand = enhancePageWithStagehand;
4
+ const PROVIDER_ENV_VARS = {
5
+ OpenAI: 'OPENAI_API_KEY',
6
+ Anthropic: 'ANTHROPIC_API_KEY',
7
+ Google: 'GOOGLE_API_KEY',
8
+ };
9
+ /**
10
+ * Improves error messages for common Stagehand errors.
11
+ * Replaces confusing Stagehand API key error messages with ones that reference our options.
12
+ */
13
+ function improveErrorMessage(error) {
14
+ const message = error instanceof Error ? error.message : String(error);
15
+ // Improve API key error messages to reference our options
16
+ if (message.includes('API key is missing')) {
17
+ let provider = 'LLM provider';
18
+ for (const name of Object.keys(PROVIDER_ENV_VARS)) {
19
+ if (message.includes(name)) {
20
+ provider = name;
21
+ break;
22
+ }
23
+ }
24
+ const envVar = PROVIDER_ENV_VARS[provider] ?? '<PROVIDER>_API_KEY';
25
+ return `${provider} API key is missing. Pass it via 'stagehandOptions.apiKey' or set the ${envVar} environment variable.`;
26
+ }
27
+ return message;
28
+ }
29
+ /**
30
+ * Enhances a Playwright Page with Stagehand AI methods.
31
+ * Adds page.act(), page.extract(), page.observe(), and page.agent() methods.
32
+ *
33
+ * The key feature is that each AI method passes the specific page to Stagehand,
34
+ * allowing multiple pages to use AI operations concurrently without interference.
35
+ *
36
+ * @param page - The Playwright page to enhance
37
+ * @param stagehand - The Stagehand instance to bind methods from
38
+ * @returns The enhanced page with AI methods
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const enhancedPage = enhancePageWithStagehand(page, stagehand);
43
+ * await enhancedPage.act('Click the button');
44
+ * const data = await enhancedPage.extract('Get title', schema);
45
+ * ```
46
+ *
47
+ * @ignore
48
+ */
49
+ function enhancePageWithStagehand(page, stagehand) {
50
+ // Cast to StagehandPage to add properties
51
+ const enhancedPage = page;
52
+ /**
53
+ * Perform an action on the page using natural language.
54
+ * Passes this specific page to Stagehand so it operates on the correct page.
55
+ */
56
+ enhancedPage.act = async (instruction, options) => {
57
+ try {
58
+ // Pass the page option to ensure Stagehand operates on this specific page
59
+ return await stagehand.act(instruction, { ...options, page });
60
+ }
61
+ catch (error) {
62
+ throw new Error(`Stagehand act() failed: ${improveErrorMessage(error)}`, {
63
+ cause: error,
64
+ });
65
+ }
66
+ };
67
+ /**
68
+ * Extract structured data from the page using natural language and a Zod schema.
69
+ * Passes this specific page to Stagehand so it operates on the correct page.
70
+ */
71
+ enhancedPage.extract = async (instruction, schema, options) => {
72
+ try {
73
+ // Pass the page option to ensure Stagehand operates on this specific page
74
+ return await stagehand.extract(instruction, schema, { ...options, page });
75
+ }
76
+ catch (error) {
77
+ throw new Error(`Stagehand extract() failed: ${improveErrorMessage(error)}`, {
78
+ cause: error,
79
+ });
80
+ }
81
+ };
82
+ /**
83
+ * Observe the page and get AI-suggested actions.
84
+ * Passes this specific page to Stagehand so it operates on the correct page.
85
+ */
86
+ enhancedPage.observe = async (options) => {
87
+ try {
88
+ // Pass the page option to ensure Stagehand operates on this specific page
89
+ return await stagehand.observe({ ...options, page });
90
+ }
91
+ catch (error) {
92
+ throw new Error(`Stagehand observe() failed: ${improveErrorMessage(error)}`, {
93
+ cause: error,
94
+ });
95
+ }
96
+ };
97
+ /**
98
+ * Create an autonomous agent for multi-step workflows.
99
+ * Note: Agent operates on the page context.
100
+ *
101
+ * The `as any` cast is needed because stagehand.agent() has two overloaded signatures
102
+ * (streaming vs non-streaming) that TypeScript struggles to reconcile when assigning
103
+ * to a property.
104
+ */
105
+ enhancedPage.agent = (config) => {
106
+ try {
107
+ if (config?.stream === true) {
108
+ return stagehand.agent(config);
109
+ }
110
+ return stagehand.agent(config);
111
+ }
112
+ catch (error) {
113
+ throw new Error(`Stagehand agent() failed: ${improveErrorMessage(error)}`, {
114
+ cause: error,
115
+ });
116
+ }
117
+ };
118
+ return enhancedPage;
119
+ }
120
+ //# sourceMappingURL=stagehand-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stagehand-utils.js","sourceRoot":"","sources":["../../../src/internals/utils/stagehand-utils.ts"],"names":[],"mappings":";;AAwDA,4DA2EC;AA7HD,MAAM,iBAAiB,GAA2B;IAC9C,MAAM,EAAE,gBAAgB;IACxB,SAAS,EAAE,mBAAmB;IAC9B,MAAM,EAAE,gBAAgB;CAC3B,CAAC;AAEF;;;GAGG;AACH,SAAS,mBAAmB,CAAC,KAAc;IACvC,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAEvE,0DAA0D;IAC1D,IAAI,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAC,EAAE,CAAC;QACzC,IAAI,QAAQ,GAAG,cAAc,CAAC;QAC9B,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,EAAE,CAAC;YAChD,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;gBACzB,QAAQ,GAAG,IAAI,CAAC;gBAChB,MAAM;YACV,CAAC;QACL,CAAC;QACD,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,IAAI,oBAAoB,CAAC;QAEnE,OAAO,GAAG,QAAQ,yEAAyE,MAAM,wBAAwB,CAAC;IAC9H,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,wBAAwB,CAAC,IAAU,EAAE,SAAoB;IACrE,0CAA0C;IAC1C,MAAM,YAAY,GAAG,IAAqB,CAAC;IAE3C;;;OAGG;IACH,YAAY,CAAC,GAAG,GAAG,KAAK,EAAE,WAAmB,EAAE,OAAkC,EAAE,EAAE;QACjF,IAAI,CAAC;YACD,0E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AE,MAAM,KAAK,IAAI,EAAE,CAAC;gBAC1B,OAAO,SAAS,CAAC,KAAK,CAAC,MAAwC,CAAC,CAAC;YACrE,CAAC;YACD,OAAO,SAAS,CAAC,KAAK,CAAC,MAA0C,CAAC,CAAC;QACvE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,6BAA6B,mBAAmB,CAAC,KAAK,CAAC,EAAE,EAAE;gBACvE,KAAK,EAAE,KAAK;aACf,CAAC,CAAC;QACP,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,YAAY,CAAC;AACxB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "@crawlee/stagehand",
3
+ "version": "3.15.4-beta.39",
4
+ "description": "AI-powered web crawling with Stagehand integration for Crawlee - enables natural language browser automation with act(), extract(), and observe() methods.",
5
+ "engines": {
6
+ "node": ">=16.0.0"
7
+ },
8
+ "main": "./index.js",
9
+ "module": "./index.mjs",
10
+ "types": "./index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./index.mjs",
14
+ "require": "./index.js",
15
+ "types": "./index.d.ts"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "keywords": [
20
+ "apify",
21
+ "crawlee",
22
+ "stagehand",
23
+ "ai",
24
+ "crawler",
25
+ "scraper",
26
+ "automation",
27
+ "playwright",
28
+ "browserbase"
29
+ ],
30
+ "author": {
31
+ "name": "Apify",
32
+ "email": "support@apify.com",
33
+ "url": "https://apify.com"
34
+ },
35
+ "contributors": [
36
+ "Jan Curn <jan@apify.com>",
37
+ "Marek Trunkat <marek@apify.com>",
38
+ "Ondra Urban <ondra@apify.com>"
39
+ ],
40
+ "license": "Apache-2.0",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/apify/crawlee"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/apify/crawlee/issues"
47
+ },
48
+ "homepage": "https://crawlee.dev",
49
+ "scripts": {
50
+ "build": "yarn clean && yarn compile && yarn copy",
51
+ "clean": "rimraf ./dist",
52
+ "compile": "tsc -p tsconfig.build.json && gen-esm-wrapper ./index.js ./index.mjs",
53
+ "copy": "tsx ../../scripts/copy.ts"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "dependencies": {
59
+ "@apify/log": "^2.4.0",
60
+ "@apify/timeout": "^0.3.1",
61
+ "@crawlee/browser": "3.15.4-beta.39",
62
+ "@crawlee/browser-pool": "3.15.4-beta.39",
63
+ "@crawlee/core": "3.15.4-beta.39",
64
+ "@crawlee/types": "3.15.4-beta.39",
65
+ "@crawlee/utils": "3.15.4-beta.39",
66
+ "ow": "^0.28.1",
67
+ "tslib": "^2.4.0"
68
+ },
69
+ "peerDependencies": {
70
+ "@browserbasehq/stagehand": "^3.0.0",
71
+ "playwright": "*",
72
+ "zod": "^3.0.0 || ^4.0.0"
73
+ },
74
+ "peerDependenciesMeta": {
75
+ "@browserbasehq/stagehand": {
76
+ "optional": false
77
+ },
78
+ "playwright": {
79
+ "optional": true
80
+ },
81
+ "zod": {
82
+ "optional": true
83
+ }
84
+ },
85
+ "devDependencies": {
86
+ "@browserbasehq/stagehand": "^3.0.7",
87
+ "playwright": "^1.57.0",
88
+ "zod": "^4.3.5"
89
+ }
90
+ }