web-agent-bridge 1.1.2 → 2.0.0
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/LICENSE +21 -21
- package/README.ar.md +446 -446
- package/README.md +780 -844
- package/bin/cli.js +80 -80
- package/bin/wab.js +80 -80
- package/examples/bidi-agent.js +119 -119
- package/examples/mcp-agent.js +94 -94
- package/examples/next-app-router/README.md +44 -0
- package/examples/puppeteer-agent.js +108 -108
- package/examples/saas-dashboard/README.md +55 -0
- package/examples/shopify-hydrogen/README.md +74 -0
- package/examples/vision-agent.js +171 -171
- package/examples/wordpress-elementor/README.md +77 -0
- package/package.json +69 -78
- package/public/.well-known/ai-assets.json +59 -0
- package/public/admin/login.html +84 -84
- package/public/ai.html +196 -0
- package/public/cookies.html +208 -208
- package/public/css/premium.css +317 -0
- package/public/css/styles.css +1235 -1235
- package/public/dashboard.html +704 -704
- package/public/demo.html +259 -0
- package/public/docs.html +585 -585
- package/public/feed.xml +89 -0
- package/public/index.html +495 -332
- package/public/js/auth-nav.js +31 -31
- package/public/js/auth-redirect.js +12 -12
- package/public/js/cookie-consent.js +56 -56
- package/public/js/wab-demo-page.js +721 -0
- package/public/js/ws-client.js +74 -74
- package/public/llms-full.txt +309 -0
- package/public/llms.txt +85 -0
- package/public/login.html +83 -83
- package/public/openapi.json +580 -0
- package/public/premium-dashboard.html +2487 -0
- package/public/premium.html +791 -0
- package/public/privacy.html +295 -295
- package/public/register.html +103 -103
- package/public/robots.txt +87 -0
- package/public/script/wab-consent.d.ts +36 -0
- package/public/script/wab-consent.js +104 -0
- package/public/script/wab-schema.js +131 -0
- package/public/script/wab.d.ts +108 -0
- package/public/script/wab.min.js +234 -0
- package/public/sitemap.xml +93 -0
- package/public/terms.html +254 -254
- package/public/video/tutorial.mp4 +0 -0
- package/script/ai-agent-bridge.js +1558 -1513
- package/sdk/README.md +55 -55
- package/sdk/index.d.ts +118 -0
- package/sdk/index.js +257 -203
- package/sdk/package.json +14 -14
- package/sdk/schema-discovery.js +83 -0
- package/server/config/secrets.js +94 -92
- package/server/index.js +0 -9
- package/server/middleware/adminAuth.js +30 -30
- package/server/middleware/auth.js +41 -41
- package/server/middleware/rateLimits.js +24 -24
- package/server/migrations/001_add_analytics_indexes.sql +7 -7
- package/server/migrations/002_premium_features.sql +418 -0
- package/server/models/adapters/index.js +33 -33
- package/server/models/adapters/mysql.js +183 -183
- package/server/models/adapters/postgresql.js +172 -172
- package/server/models/adapters/sqlite.js +7 -7
- package/server/models/db.js +561 -561
- package/server/routes/admin-premium.js +671 -0
- package/server/routes/admin.js +247 -247
- package/server/routes/api.js +131 -138
- package/server/routes/auth.js +51 -51
- package/server/routes/billing.js +45 -45
- package/server/routes/discovery.js +406 -329
- package/server/routes/license.js +240 -240
- package/server/routes/noscript.js +543 -543
- package/server/routes/premium-v2.js +686 -0
- package/server/routes/premium.js +724 -0
- package/server/routes/wab-api.js +476 -476
- package/server/services/agent-memory.js +625 -0
- package/server/services/email.js +204 -204
- package/server/services/fairness.js +420 -420
- package/server/services/plugins.js +747 -0
- package/server/services/premium.js +1883 -0
- package/server/services/self-healing.js +843 -0
- package/server/services/stripe.js +192 -192
- package/server/services/swarm.js +788 -0
- package/server/services/vision.js +871 -0
- package/server/utils/cache.js +125 -125
- package/server/utils/migrate.js +81 -81
- package/server/utils/secureFields.js +50 -50
- package/server/ws.js +101 -101
- package/docs/DEPLOY.md +0 -118
- package/docs/SPEC.md +0 -1540
- package/wab-mcp-adapter/README.md +0 -136
- package/wab-mcp-adapter/index.js +0 -555
- package/wab-mcp-adapter/package.json +0 -17
package/sdk/index.js
CHANGED
|
@@ -1,203 +1,257 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WAB Agent SDK
|
|
3
|
-
*
|
|
4
|
-
* Helpers for building AI agents that interact with Web Agent Bridge.
|
|
5
|
-
* Works with Puppeteer, Playwright, or any browser automation tool.
|
|
6
|
-
*
|
|
7
|
-
* Usage:
|
|
8
|
-
* const { WABAgent } = require('./sdk');
|
|
9
|
-
* const agent = new WABAgent(page);
|
|
10
|
-
* await agent.waitForBridge();
|
|
11
|
-
* const actions = await agent.getActions();
|
|
12
|
-
* await agent.execute('signup', { email: 'test@example.com' });
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
class WABAgent {
|
|
16
|
-
/**
|
|
17
|
-
* @param {object} page — A Puppeteer or Playwright page object
|
|
18
|
-
* @param {object} [options]
|
|
19
|
-
* @param {number} [options.timeout=10000] — Default timeout in ms
|
|
20
|
-
* @param {boolean} [options.useBiDi=false] — Use BiDi interface instead of AICommands
|
|
21
|
-
*/
|
|
22
|
-
constructor(page, options = {}) {
|
|
23
|
-
this.page = page;
|
|
24
|
-
this.timeout = options.timeout || 10000;
|
|
25
|
-
this.useBiDi = options.useBiDi || false;
|
|
26
|
-
this._biDiId = 0;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Wait for the WAB bridge to be ready on the page.
|
|
31
|
-
* @returns {Promise<boolean>}
|
|
32
|
-
*/
|
|
33
|
-
async waitForBridge() {
|
|
34
|
-
const iface = this.useBiDi ? '__wab_bidi' : 'AICommands';
|
|
35
|
-
await this.page.waitForFunction(
|
|
36
|
-
(name) => typeof window[name] !== 'undefined',
|
|
37
|
-
{ timeout: this.timeout },
|
|
38
|
-
iface
|
|
39
|
-
);
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Check if the bridge is loaded on the current page.
|
|
45
|
-
* @returns {Promise<boolean>}
|
|
46
|
-
*/
|
|
47
|
-
async hasBridge() {
|
|
48
|
-
const iface = this.useBiDi ? '__wab_bidi' : 'AICommands';
|
|
49
|
-
return this.page.evaluate((name) => typeof window[name] !== 'undefined', iface);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Get all available actions.
|
|
54
|
-
* @param {string} [category] — Optional category filter
|
|
55
|
-
* @returns {Promise<Array>}
|
|
56
|
-
*/
|
|
57
|
-
async getActions(category) {
|
|
58
|
-
if (this.useBiDi) {
|
|
59
|
-
const result = await this._bidiSend('wab.getActions', category ? { category } : {});
|
|
60
|
-
return result.result || [];
|
|
61
|
-
}
|
|
62
|
-
return this.page.evaluate((cat) => window.AICommands.getActions(cat), category);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Get a single action by name.
|
|
67
|
-
* @param {string} name
|
|
68
|
-
* @returns {Promise<object|null>}
|
|
69
|
-
*/
|
|
70
|
-
async getAction(name) {
|
|
71
|
-
return this.page.evaluate((n) => window.AICommands.getAction(n), name);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Execute an action by name.
|
|
76
|
-
* @param {string} name — Action name
|
|
77
|
-
* @param {object} [params] — Action parameters
|
|
78
|
-
* @returns {Promise<object>}
|
|
79
|
-
*/
|
|
80
|
-
async execute(name, params) {
|
|
81
|
-
if (this.useBiDi) {
|
|
82
|
-
const result = await this._bidiSend('wab.executeAction', { name, data: params || {} });
|
|
83
|
-
return result.result || result;
|
|
84
|
-
}
|
|
85
|
-
return this.page.evaluate(
|
|
86
|
-
(n, p) => window.AICommands.execute(n, p),
|
|
87
|
-
name, params
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Read text content of an element.
|
|
93
|
-
* @param {string} selector — CSS selector
|
|
94
|
-
* @returns {Promise<object>}
|
|
95
|
-
*/
|
|
96
|
-
async readContent(selector) {
|
|
97
|
-
if (this.useBiDi) {
|
|
98
|
-
const result = await this._bidiSend('wab.readContent', { selector });
|
|
99
|
-
return result.result || result;
|
|
100
|
-
}
|
|
101
|
-
return this.page.evaluate((sel) => window.AICommands.readContent(sel), selector);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Get page info and bridge metadata.
|
|
106
|
-
* @returns {Promise<object>}
|
|
107
|
-
*/
|
|
108
|
-
async getPageInfo() {
|
|
109
|
-
if (this.useBiDi) {
|
|
110
|
-
const result = await this._bidiSend('wab.getPageInfo');
|
|
111
|
-
return result.result || result;
|
|
112
|
-
}
|
|
113
|
-
return this.page.evaluate(() => window.AICommands.getPageInfo());
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Authenticate an agent with the bridge.
|
|
118
|
-
* @param {string} apiKey
|
|
119
|
-
* @param {object} [meta] — Agent metadata
|
|
120
|
-
* @returns {Promise<object>}
|
|
121
|
-
*/
|
|
122
|
-
async authenticate(apiKey, meta) {
|
|
123
|
-
return this.page.evaluate(
|
|
124
|
-
(key, m) => window.AICommands.authenticate(key, m),
|
|
125
|
-
apiKey, meta
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Navigate to a URL and wait for the bridge.
|
|
131
|
-
* @param {string} url
|
|
132
|
-
* @returns {Promise<void>}
|
|
133
|
-
*/
|
|
134
|
-
async navigateAndWait(url) {
|
|
135
|
-
await this.page.goto(url, { waitUntil: 'networkidle2' });
|
|
136
|
-
await this.waitForBridge();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Execute multiple actions in sequence.
|
|
141
|
-
* @param {Array<{name: string, params?: object}>} steps
|
|
142
|
-
* @returns {Promise<Array>}
|
|
143
|
-
*/
|
|
144
|
-
async executeSteps(steps) {
|
|
145
|
-
const results = [];
|
|
146
|
-
for (const step of steps) {
|
|
147
|
-
results.push(await this.execute(step.name, step.params));
|
|
148
|
-
}
|
|
149
|
-
return results;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Get
|
|
154
|
-
* @returns {Promise<object>}
|
|
155
|
-
*/
|
|
156
|
-
async
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
1
|
+
/**
|
|
2
|
+
* WAB Agent SDK
|
|
3
|
+
*
|
|
4
|
+
* Helpers for building AI agents that interact with Web Agent Bridge.
|
|
5
|
+
* Works with Puppeteer, Playwright, or any browser automation tool.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const { WABAgent } = require('./sdk');
|
|
9
|
+
* const agent = new WABAgent(page);
|
|
10
|
+
* await agent.waitForBridge();
|
|
11
|
+
* const actions = await agent.getActions();
|
|
12
|
+
* await agent.execute('signup', { email: 'test@example.com' });
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
class WABAgent {
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} page — A Puppeteer or Playwright page object
|
|
18
|
+
* @param {object} [options]
|
|
19
|
+
* @param {number} [options.timeout=10000] — Default timeout in ms
|
|
20
|
+
* @param {boolean} [options.useBiDi=false] — Use BiDi interface instead of AICommands
|
|
21
|
+
*/
|
|
22
|
+
constructor(page, options = {}) {
|
|
23
|
+
this.page = page;
|
|
24
|
+
this.timeout = options.timeout || 10000;
|
|
25
|
+
this.useBiDi = options.useBiDi || false;
|
|
26
|
+
this._biDiId = 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Wait for the WAB bridge to be ready on the page.
|
|
31
|
+
* @returns {Promise<boolean>}
|
|
32
|
+
*/
|
|
33
|
+
async waitForBridge() {
|
|
34
|
+
const iface = this.useBiDi ? '__wab_bidi' : 'AICommands';
|
|
35
|
+
await this.page.waitForFunction(
|
|
36
|
+
(name) => typeof window[name] !== 'undefined',
|
|
37
|
+
{ timeout: this.timeout },
|
|
38
|
+
iface
|
|
39
|
+
);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Check if the bridge is loaded on the current page.
|
|
45
|
+
* @returns {Promise<boolean>}
|
|
46
|
+
*/
|
|
47
|
+
async hasBridge() {
|
|
48
|
+
const iface = this.useBiDi ? '__wab_bidi' : 'AICommands';
|
|
49
|
+
return this.page.evaluate((name) => typeof window[name] !== 'undefined', iface);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Get all available actions.
|
|
54
|
+
* @param {string} [category] — Optional category filter
|
|
55
|
+
* @returns {Promise<Array>}
|
|
56
|
+
*/
|
|
57
|
+
async getActions(category) {
|
|
58
|
+
if (this.useBiDi) {
|
|
59
|
+
const result = await this._bidiSend('wab.getActions', category ? { category } : {});
|
|
60
|
+
return result.result || [];
|
|
61
|
+
}
|
|
62
|
+
return this.page.evaluate((cat) => window.AICommands.getActions(cat), category);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get a single action by name.
|
|
67
|
+
* @param {string} name
|
|
68
|
+
* @returns {Promise<object|null>}
|
|
69
|
+
*/
|
|
70
|
+
async getAction(name) {
|
|
71
|
+
return this.page.evaluate((n) => window.AICommands.getAction(n), name);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Execute an action by name.
|
|
76
|
+
* @param {string} name — Action name
|
|
77
|
+
* @param {object} [params] — Action parameters
|
|
78
|
+
* @returns {Promise<object>}
|
|
79
|
+
*/
|
|
80
|
+
async execute(name, params) {
|
|
81
|
+
if (this.useBiDi) {
|
|
82
|
+
const result = await this._bidiSend('wab.executeAction', { name, data: params || {} });
|
|
83
|
+
return result.result || result;
|
|
84
|
+
}
|
|
85
|
+
return this.page.evaluate(
|
|
86
|
+
(n, p) => window.AICommands.execute(n, p),
|
|
87
|
+
name, params
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Read text content of an element.
|
|
93
|
+
* @param {string} selector — CSS selector
|
|
94
|
+
* @returns {Promise<object>}
|
|
95
|
+
*/
|
|
96
|
+
async readContent(selector) {
|
|
97
|
+
if (this.useBiDi) {
|
|
98
|
+
const result = await this._bidiSend('wab.readContent', { selector });
|
|
99
|
+
return result.result || result;
|
|
100
|
+
}
|
|
101
|
+
return this.page.evaluate((sel) => window.AICommands.readContent(sel), selector);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get page info and bridge metadata.
|
|
106
|
+
* @returns {Promise<object>}
|
|
107
|
+
*/
|
|
108
|
+
async getPageInfo() {
|
|
109
|
+
if (this.useBiDi) {
|
|
110
|
+
const result = await this._bidiSend('wab.getPageInfo');
|
|
111
|
+
return result.result || result;
|
|
112
|
+
}
|
|
113
|
+
return this.page.evaluate(() => window.AICommands.getPageInfo());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Authenticate an agent with the bridge.
|
|
118
|
+
* @param {string} apiKey
|
|
119
|
+
* @param {object} [meta] — Agent metadata
|
|
120
|
+
* @returns {Promise<object>}
|
|
121
|
+
*/
|
|
122
|
+
async authenticate(apiKey, meta) {
|
|
123
|
+
return this.page.evaluate(
|
|
124
|
+
(key, m) => window.AICommands.authenticate(key, m),
|
|
125
|
+
apiKey, meta
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Navigate to a URL and wait for the bridge.
|
|
131
|
+
* @param {string} url
|
|
132
|
+
* @returns {Promise<void>}
|
|
133
|
+
*/
|
|
134
|
+
async navigateAndWait(url) {
|
|
135
|
+
await this.page.goto(url, { waitUntil: 'networkidle2' });
|
|
136
|
+
await this.waitForBridge();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Execute multiple actions in sequence.
|
|
141
|
+
* @param {Array<{name: string, params?: object}>} steps
|
|
142
|
+
* @returns {Promise<Array>}
|
|
143
|
+
*/
|
|
144
|
+
async executeSteps(steps) {
|
|
145
|
+
const results = [];
|
|
146
|
+
for (const step of steps) {
|
|
147
|
+
results.push(await this.execute(step.name, step.params));
|
|
148
|
+
}
|
|
149
|
+
return results;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Get BiDi context (only available when useBiDi is true).
|
|
154
|
+
* @returns {Promise<object>}
|
|
155
|
+
*/
|
|
156
|
+
async getBiDiContext() {
|
|
157
|
+
return this.page.evaluate(() => window.__wab_bidi.getContext());
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Check if the page has granted consent for agent interactions.
|
|
162
|
+
* @returns {Promise<boolean>}
|
|
163
|
+
*/
|
|
164
|
+
async hasConsent() {
|
|
165
|
+
return this.page.evaluate(() => {
|
|
166
|
+
if (typeof window.WABConsent !== 'undefined') return window.WABConsent.hasConsent();
|
|
167
|
+
// If no consent script, treat as allowed
|
|
168
|
+
return true;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Wait until consent is granted (blocks until user clicks Allow).
|
|
174
|
+
* @param {number} [pollMs=500]
|
|
175
|
+
* @returns {Promise<boolean>}
|
|
176
|
+
*/
|
|
177
|
+
async waitForConsent(pollMs = 500) {
|
|
178
|
+
return this.page.waitForFunction(
|
|
179
|
+
() => {
|
|
180
|
+
if (typeof window.WABConsent === 'undefined') return true;
|
|
181
|
+
return window.WABConsent.hasConsent();
|
|
182
|
+
},
|
|
183
|
+
{ timeout: this.timeout, polling: pollMs }
|
|
184
|
+
).then(() => true);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Discover the page and return the list of actions.
|
|
189
|
+
* Combines bridge discovery with runtime getActions().
|
|
190
|
+
* @returns {Promise<object>}
|
|
191
|
+
*/
|
|
192
|
+
async discover() {
|
|
193
|
+
return this.page.evaluate(() => {
|
|
194
|
+
if (window.WAB && typeof window.WAB.discover === 'function') return window.WAB.discover();
|
|
195
|
+
if (window.AICommands && typeof window.AICommands.getActions === 'function') {
|
|
196
|
+
return { actions: window.AICommands.getActions(), meta: window.AICommands.getPageInfo ? window.AICommands.getPageInfo() : {} };
|
|
197
|
+
}
|
|
198
|
+
return { actions: [] };
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Run a sequence of actions, stopping on the first failure.
|
|
204
|
+
* @param {Array<{name: string, params?: object}>} steps
|
|
205
|
+
* @param {{ stopOnError?: boolean }} [options]
|
|
206
|
+
* @returns {Promise<Array<{ name: string, ok: boolean, result?: any, error?: string }>>}
|
|
207
|
+
*/
|
|
208
|
+
async runPipeline(steps, options = {}) {
|
|
209
|
+
const stopOnError = options.stopOnError !== false;
|
|
210
|
+
const results = [];
|
|
211
|
+
for (const step of steps) {
|
|
212
|
+
try {
|
|
213
|
+
const res = await this.execute(step.name, step.params);
|
|
214
|
+
results.push({ name: step.name, ok: true, result: res });
|
|
215
|
+
} catch (err) {
|
|
216
|
+
results.push({ name: step.name, ok: false, error: err.message || String(err) });
|
|
217
|
+
if (stopOnError) break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return results;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Execute multiple actions in parallel.
|
|
225
|
+
* @param {Array<{name: string, params?: object}>} actions
|
|
226
|
+
* @returns {Promise<Array<{ name: string, status: string, value?: any, reason?: string }>>}
|
|
227
|
+
*/
|
|
228
|
+
async executeParallel(actions) {
|
|
229
|
+
const promises = actions.map((a) =>
|
|
230
|
+
this.execute(a.name, a.params)
|
|
231
|
+
.then((value) => ({ name: a.name, status: 'fulfilled', value }))
|
|
232
|
+
.catch((err) => ({ name: a.name, status: 'rejected', reason: err.message || String(err) }))
|
|
233
|
+
);
|
|
234
|
+
return Promise.all(promises);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Take a screenshot and return as base64 (useful for vision agents).
|
|
239
|
+
* @param {{ fullPage?: boolean }} [opts]
|
|
240
|
+
* @returns {Promise<string>}
|
|
241
|
+
*/
|
|
242
|
+
async screenshot(opts = {}) {
|
|
243
|
+
const buf = await this.page.screenshot({
|
|
244
|
+
encoding: 'base64',
|
|
245
|
+
fullPage: opts.fullPage || false
|
|
246
|
+
});
|
|
247
|
+
return buf;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** @private */
|
|
251
|
+
async _bidiSend(method, params = {}) {
|
|
252
|
+
const cmd = { id: ++this._biDiId, method, params };
|
|
253
|
+
return this.page.evaluate((c) => window.__wab_bidi.send(c), cmd);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
module.exports = { WABAgent };
|
package/sdk/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@anthropic-wab/agent-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "SDK for building AI agents that interact with Web Agent Bridge (WAB)",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"keywords": ["wab", "ai-agent", "sdk", "web-automation", "bridge"],
|
|
8
|
-
"peerDependencies": {
|
|
9
|
-
"puppeteer": ">=20.0.0"
|
|
10
|
-
},
|
|
11
|
-
"peerDependenciesMeta": {
|
|
12
|
-
"puppeteer": { "optional": true }
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@anthropic-wab/agent-sdk",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "SDK for building AI agents that interact with Web Agent Bridge (WAB)",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": ["wab", "ai-agent", "sdk", "web-automation", "bridge"],
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"puppeteer": ">=20.0.0"
|
|
10
|
+
},
|
|
11
|
+
"peerDependenciesMeta": {
|
|
12
|
+
"puppeteer": { "optional": true }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side / Node: extract schema.org Product nodes from HTML (JSON-LD blocks).
|
|
3
|
+
* No extra dependencies — regex-based script extraction (same semantics as browser WABSchema).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const { extractProductsFromHtml, suggestWabActionsFromProducts } = require('./schema-discovery');
|
|
7
|
+
* const products = extractProductsFromHtml(htmlString);
|
|
8
|
+
* const hints = suggestWabActionsFromProducts(products);
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function extractJsonLdBlocks(html) {
|
|
12
|
+
if (!html || typeof html !== 'string') return [];
|
|
13
|
+
const re = /<script[^>]*type=["']application\/ld\+json["'][^>]*>([\s\S]*?)<\/script>/gi;
|
|
14
|
+
const blocks = [];
|
|
15
|
+
let m;
|
|
16
|
+
while ((m = re.exec(html)) !== null) {
|
|
17
|
+
blocks.push(m[1].trim());
|
|
18
|
+
}
|
|
19
|
+
return blocks;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function flattenGraph(data) {
|
|
23
|
+
if (Array.isArray(data)) return data;
|
|
24
|
+
if (data && Array.isArray(data['@graph'])) return data['@graph'];
|
|
25
|
+
return [data];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} html
|
|
30
|
+
* @returns {Array<{ type: string, name?: string, sku?: string, offers?: unknown }>}
|
|
31
|
+
*/
|
|
32
|
+
function extractProductsFromHtml(html) {
|
|
33
|
+
const out = [];
|
|
34
|
+
const blocks = extractJsonLdBlocks(html);
|
|
35
|
+
for (const text of blocks) {
|
|
36
|
+
let data;
|
|
37
|
+
try {
|
|
38
|
+
data = JSON.parse(text);
|
|
39
|
+
} catch {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const items = flattenGraph(data);
|
|
43
|
+
for (const node of items) {
|
|
44
|
+
if (!node || typeof node !== 'object') continue;
|
|
45
|
+
let types = node['@type'];
|
|
46
|
+
if (typeof types === 'string') types = [types];
|
|
47
|
+
if (!Array.isArray(types)) types = [];
|
|
48
|
+
if (!types.includes('Product')) continue;
|
|
49
|
+
out.push({
|
|
50
|
+
type: 'Product',
|
|
51
|
+
name: node.name,
|
|
52
|
+
sku: node.sku,
|
|
53
|
+
offers: node.offers
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function suggestWabActionsFromProducts(products) {
|
|
61
|
+
const actions = [];
|
|
62
|
+
if (products.length) {
|
|
63
|
+
actions.push({
|
|
64
|
+
name: 'getProductFromSchema',
|
|
65
|
+
description: 'Structured products from schema.org JSON-LD',
|
|
66
|
+
source: 'schema.org'
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (products.some((p) => p.offers)) {
|
|
70
|
+
actions.push({
|
|
71
|
+
name: 'getOfferPrice',
|
|
72
|
+
description: 'Prices from schema.org Offer',
|
|
73
|
+
source: 'schema.org'
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return actions;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
extractJsonLdBlocks,
|
|
81
|
+
extractProductsFromHtml,
|
|
82
|
+
suggestWabActionsFromProducts
|
|
83
|
+
};
|