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/server/routes/wab-api.js
CHANGED
|
@@ -1,476 +1,476 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WAB Protocol HTTP Transport — RESTful endpoints that implement the
|
|
3
|
-
* WAB command protocol over HTTP for remote agents and the MCP adapter.
|
|
4
|
-
*
|
|
5
|
-
* Every command from the WAB spec (docs/SPEC.md §5) is accessible here
|
|
6
|
-
* so agents that cannot run JavaScript in a browser can still interact
|
|
7
|
-
* with WAB-enabled sites via standard HTTP requests.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
const express = require('express');
|
|
11
|
-
const router = express.Router();
|
|
12
|
-
const { findSiteById, findSiteByLicense, recordAnalytic, db } = require('../models/db');
|
|
13
|
-
const { broadcastAnalytic } = require('../ws');
|
|
14
|
-
const {
|
|
15
|
-
calculateNeutralityScore,
|
|
16
|
-
fairnessWeightedSearch,
|
|
17
|
-
getDirectoryListings,
|
|
18
|
-
generateFairnessReport
|
|
19
|
-
} = require('../services/fairness');
|
|
20
|
-
|
|
21
|
-
const WAB_VERSION = '1.
|
|
22
|
-
const PROTOCOL_VERSION = '1.0';
|
|
23
|
-
|
|
24
|
-
// ─── Session management ──────────────────────────────────────────────
|
|
25
|
-
const sessions = new Map();
|
|
26
|
-
const SESSION_TTL = 3600_000;
|
|
27
|
-
|
|
28
|
-
setInterval(() => {
|
|
29
|
-
const now = Date.now();
|
|
30
|
-
for (const [token, data] of sessions) {
|
|
31
|
-
if (now > data.expiresAt) sessions.delete(token);
|
|
32
|
-
}
|
|
33
|
-
}, 300_000);
|
|
34
|
-
|
|
35
|
-
function generateSessionToken() {
|
|
36
|
-
const bytes = require('crypto').randomBytes(32);
|
|
37
|
-
return bytes.toString('hex');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function requireSession(req, res, next) {
|
|
41
|
-
const auth = req.get('Authorization');
|
|
42
|
-
if (!auth || !auth.startsWith('Bearer ')) {
|
|
43
|
-
return res.status(401).json({
|
|
44
|
-
type: 'error',
|
|
45
|
-
error: { code: 'auth_required', message: 'Bearer token required in Authorization header' }
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
const token = auth.slice(7);
|
|
49
|
-
const session = sessions.get(token);
|
|
50
|
-
if (!session || Date.now() > session.expiresAt) {
|
|
51
|
-
sessions.delete(token);
|
|
52
|
-
return res.status(401).json({
|
|
53
|
-
type: 'error',
|
|
54
|
-
error: { code: 'session_expired', message: 'Session expired or invalid' }
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
req.wabSession = session;
|
|
58
|
-
next();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// ─── Helper: resolve site from request ───────────────────────────────
|
|
62
|
-
function resolveSite(req) {
|
|
63
|
-
if (req.wabSession) return findSiteById.get(req.wabSession.siteId);
|
|
64
|
-
const siteId = req.query.siteId || req.body?.siteId;
|
|
65
|
-
if (siteId) return findSiteById.get(siteId);
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function parseSiteConfig(site) {
|
|
70
|
-
try { return JSON.parse(site.config || '{}'); } catch (_) { return {}; }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function buildCommandResponse(id, result) {
|
|
74
|
-
return { id: id || null, type: 'success', protocol: PROTOCOL_VERSION, result };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function buildErrorResponse(id, code, message) {
|
|
78
|
-
return { id: id || null, type: 'error', protocol: PROTOCOL_VERSION, error: { code, message } };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
82
|
-
// POST /api/wab/authenticate — session token exchange
|
|
83
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
84
|
-
|
|
85
|
-
router.post('/authenticate', (req, res) => {
|
|
86
|
-
try {
|
|
87
|
-
const { siteId, apiKey, meta } = req.body;
|
|
88
|
-
if (!siteId && !apiKey) {
|
|
89
|
-
return res.status(400).json(buildErrorResponse(null, 'invalid_argument', 'siteId or apiKey required'));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
let site;
|
|
93
|
-
if (apiKey) {
|
|
94
|
-
site = db.prepare('SELECT * FROM sites WHERE api_key = ? AND active = 1').get(apiKey);
|
|
95
|
-
} else {
|
|
96
|
-
site = findSiteById.get(siteId);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (!site) {
|
|
100
|
-
return res.status(404).json(buildErrorResponse(null, 'not_found', 'Site not found or invalid credentials'));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const origin = req.get('origin') || '';
|
|
104
|
-
if (origin) {
|
|
105
|
-
try {
|
|
106
|
-
const reqDomain = new URL(origin).hostname.replace(/^www\./, '');
|
|
107
|
-
const siteDomain = site.domain.replace(/^www\./, '');
|
|
108
|
-
if (reqDomain !== siteDomain && reqDomain !== 'localhost' && reqDomain !== '127.0.0.1') {
|
|
109
|
-
return res.status(403).json(buildErrorResponse(null, 'origin_mismatch', 'Origin does not match site domain'));
|
|
110
|
-
}
|
|
111
|
-
} catch (_) {}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const token = generateSessionToken();
|
|
115
|
-
sessions.set(token, {
|
|
116
|
-
siteId: site.id,
|
|
117
|
-
tier: site.tier,
|
|
118
|
-
domain: site.domain,
|
|
119
|
-
agentMeta: meta || {},
|
|
120
|
-
createdAt: Date.now(),
|
|
121
|
-
expiresAt: Date.now() + SESSION_TTL
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
res.json(buildCommandResponse(null, {
|
|
125
|
-
authenticated: true,
|
|
126
|
-
token,
|
|
127
|
-
siteId: site.id,
|
|
128
|
-
tier: site.tier,
|
|
129
|
-
expiresIn: SESSION_TTL / 1000,
|
|
130
|
-
permissions: parseSiteConfig(site).agentPermissions || {}
|
|
131
|
-
}));
|
|
132
|
-
} catch (err) {
|
|
133
|
-
res.status(500).json(buildErrorResponse(null, 'internal', 'Authentication failed'));
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
138
|
-
// GET /api/wab/discover — full discovery document
|
|
139
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
140
|
-
|
|
141
|
-
router.get('/discover', (req, res) => {
|
|
142
|
-
try {
|
|
143
|
-
const site = resolveSite(req);
|
|
144
|
-
if (!site || !site.active) {
|
|
145
|
-
const domain = (req.get('origin') ? new URL(req.get('origin')).hostname : req.get('host')?.split(':')[0]) || '';
|
|
146
|
-
const byDomain = db.prepare(
|
|
147
|
-
'SELECT * FROM sites WHERE LOWER(REPLACE(domain, "www.", "")) = ? AND active = 1 LIMIT 1'
|
|
148
|
-
).get(domain.toLowerCase().replace(/^www\./, ''));
|
|
149
|
-
|
|
150
|
-
if (!byDomain) {
|
|
151
|
-
return res.status(404).json(buildErrorResponse(null, 'not_found', 'No WAB site found'));
|
|
152
|
-
}
|
|
153
|
-
return res.json(buildCommandResponse(null, buildDiscovery(byDomain)));
|
|
154
|
-
}
|
|
155
|
-
res.json(buildCommandResponse(null, buildDiscovery(site)));
|
|
156
|
-
} catch (err) {
|
|
157
|
-
res.status(500).json(buildErrorResponse(null, 'internal', 'Discovery failed'));
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
162
|
-
// GET /api/wab/actions — list actions
|
|
163
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
164
|
-
|
|
165
|
-
router.get('/actions', (req, res) => {
|
|
166
|
-
try {
|
|
167
|
-
const site = resolveSite(req);
|
|
168
|
-
if (!site) return res.status(400).json(buildErrorResponse(null, 'invalid_argument', 'siteId required'));
|
|
169
|
-
|
|
170
|
-
const config = parseSiteConfig(site);
|
|
171
|
-
const perms = config.agentPermissions || {};
|
|
172
|
-
const category = req.query.category;
|
|
173
|
-
|
|
174
|
-
const actions = Object.entries(perms)
|
|
175
|
-
.filter(([, v]) => v)
|
|
176
|
-
.map(([name]) => ({
|
|
177
|
-
name,
|
|
178
|
-
description: `Permission: ${name}`,
|
|
179
|
-
trigger: name === 'click' ? 'click' : name === 'fillForms' ? 'fill_and_submit' : name === 'scroll' ? 'scroll' : 'api',
|
|
180
|
-
category: name === 'navigate' ? 'navigation' : 'general',
|
|
181
|
-
requiresAuth: ['apiAccess', 'automatedLogin', 'extractData'].includes(name)
|
|
182
|
-
}));
|
|
183
|
-
|
|
184
|
-
const filtered = category ? actions.filter(a => a.category === category) : actions;
|
|
185
|
-
|
|
186
|
-
res.json(buildCommandResponse(req.query.id || null, { actions: filtered, total: filtered.length }));
|
|
187
|
-
} catch (err) {
|
|
188
|
-
res.status(500).json(buildErrorResponse(null, 'internal', 'Failed to list actions'));
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
193
|
-
// POST /api/wab/actions/:name — execute action (with tracking)
|
|
194
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
195
|
-
|
|
196
|
-
router.post('/actions/:name', requireSession, (req, res) => {
|
|
197
|
-
try {
|
|
198
|
-
const actionName = req.params.name;
|
|
199
|
-
const site = findSiteById.get(req.wabSession.siteId);
|
|
200
|
-
if (!site) return res.status(404).json(buildErrorResponse(req.body?.id, 'not_found', 'Site not found'));
|
|
201
|
-
|
|
202
|
-
const config = parseSiteConfig(site);
|
|
203
|
-
const perms = config.agentPermissions || {};
|
|
204
|
-
|
|
205
|
-
const permMap = {
|
|
206
|
-
click: 'click', fill_and_submit: 'fillForms', scroll: 'scroll',
|
|
207
|
-
navigate: 'navigate', api: 'apiAccess', read: 'readContent', extract: 'extractData'
|
|
208
|
-
};
|
|
209
|
-
const requiredPerm = permMap[actionName] || actionName;
|
|
210
|
-
|
|
211
|
-
if (!perms[requiredPerm] && !perms[actionName]) {
|
|
212
|
-
return res.status(403).json(buildErrorResponse(req.body?.id, 'permission_denied',
|
|
213
|
-
`Action "${actionName}" is not permitted by site configuration`));
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
recordAnalytic({
|
|
217
|
-
siteId: site.id,
|
|
218
|
-
actionName,
|
|
219
|
-
agentId: req.wabSession.agentMeta?.name || 'mcp-agent',
|
|
220
|
-
triggerType: 'wab_api',
|
|
221
|
-
success: true,
|
|
222
|
-
metadata: { params: req.body?.params || {}, transport: 'http' }
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
broadcastAnalytic(site.id, {
|
|
226
|
-
actionName,
|
|
227
|
-
agentId: req.wabSession.agentMeta?.name || 'mcp-agent',
|
|
228
|
-
triggerType: 'wab_api',
|
|
229
|
-
success: true
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
res.json(buildCommandResponse(req.body?.id, {
|
|
233
|
-
success: true,
|
|
234
|
-
action: actionName,
|
|
235
|
-
siteId: site.id,
|
|
236
|
-
executed_at: new Date().toISOString(),
|
|
237
|
-
note: 'Server-side action recorded. For DOM interactions, use the bridge script in-browser.'
|
|
238
|
-
}));
|
|
239
|
-
} catch (err) {
|
|
240
|
-
res.status(500).json(buildErrorResponse(req.body?.id, 'internal', 'Action execution failed'));
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
245
|
-
// POST /api/wab/read — read content (selector-based, requires in-browser)
|
|
246
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
247
|
-
|
|
248
|
-
router.post('/read', requireSession, (req, res) => {
|
|
249
|
-
try {
|
|
250
|
-
const { selector, id } = req.body;
|
|
251
|
-
if (!selector) {
|
|
252
|
-
return res.status(400).json(buildErrorResponse(id, 'invalid_argument', 'selector is required'));
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const site = findSiteById.get(req.wabSession.siteId);
|
|
256
|
-
if (!site) return res.status(404).json(buildErrorResponse(id, 'not_found', 'Site not found'));
|
|
257
|
-
|
|
258
|
-
const config = parseSiteConfig(site);
|
|
259
|
-
if (!config.agentPermissions?.readContent) {
|
|
260
|
-
return res.status(403).json(buildErrorResponse(id, 'permission_denied', 'readContent not enabled'));
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
recordAnalytic({
|
|
264
|
-
siteId: site.id,
|
|
265
|
-
actionName: 'readContent',
|
|
266
|
-
agentId: req.wabSession.agentMeta?.name || 'mcp-agent',
|
|
267
|
-
triggerType: 'wab_api',
|
|
268
|
-
success: true,
|
|
269
|
-
metadata: { selector, transport: 'http' }
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
res.json(buildCommandResponse(id, {
|
|
273
|
-
success: true,
|
|
274
|
-
selector,
|
|
275
|
-
note: 'Content reading via HTTP returns metadata only. Use the bridge script in-browser or the noscript bridge for rendered content.',
|
|
276
|
-
bridge_page: `/api/noscript/bridge/${site.id}`,
|
|
277
|
-
noscript_endpoints: {
|
|
278
|
-
pixel: `/api/noscript/pixel/${site.id}`,
|
|
279
|
-
css: `/api/noscript/css/${site.id}`,
|
|
280
|
-
bridge: `/api/noscript/bridge/${site.id}`
|
|
281
|
-
}
|
|
282
|
-
}));
|
|
283
|
-
} catch (err) {
|
|
284
|
-
res.status(500).json(buildErrorResponse(null, 'internal', 'Read failed'));
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
289
|
-
// GET /api/wab/page-info — get page/site metadata
|
|
290
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
291
|
-
|
|
292
|
-
router.get('/page-info', (req, res) => {
|
|
293
|
-
try {
|
|
294
|
-
const site = resolveSite(req);
|
|
295
|
-
if (!site) return res.status(400).json(buildErrorResponse(null, 'invalid_argument', 'siteId required'));
|
|
296
|
-
|
|
297
|
-
const config = parseSiteConfig(site);
|
|
298
|
-
const neutralityScore = calculateNeutralityScore(site);
|
|
299
|
-
|
|
300
|
-
res.json(buildCommandResponse(req.query.id || null, {
|
|
301
|
-
title: site.name,
|
|
302
|
-
domain: site.domain,
|
|
303
|
-
url: `https://${site.domain}`,
|
|
304
|
-
tier: site.tier,
|
|
305
|
-
bridgeVersion: WAB_VERSION,
|
|
306
|
-
protocol: PROTOCOL_VERSION,
|
|
307
|
-
permissions: config.agentPermissions || {},
|
|
308
|
-
restrictions: config.restrictions || {},
|
|
309
|
-
security: {
|
|
310
|
-
sandboxActive: true,
|
|
311
|
-
sessionRequired: true,
|
|
312
|
-
originValidation: true,
|
|
313
|
-
rateLimit: config.restrictions?.rateLimit?.maxCallsPerMinute || 60
|
|
314
|
-
},
|
|
315
|
-
fairness: {
|
|
316
|
-
neutralityScore,
|
|
317
|
-
isIndependent: false
|
|
318
|
-
},
|
|
319
|
-
endpoints: {
|
|
320
|
-
discover: `/api/wab/discover?siteId=${site.id}`,
|
|
321
|
-
actions: `/api/wab/actions?siteId=${site.id}`,
|
|
322
|
-
authenticate: '/api/wab/authenticate',
|
|
323
|
-
bridge: `/api/noscript/bridge/${site.id}`,
|
|
324
|
-
discovery: `/api/discovery/${site.id}`
|
|
325
|
-
}
|
|
326
|
-
}));
|
|
327
|
-
} catch (err) {
|
|
328
|
-
res.status(500).json(buildErrorResponse(null, 'internal', 'Failed to get page info'));
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
333
|
-
// GET /api/wab/search — fairness-weighted search (MCP adapter uses this)
|
|
334
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
335
|
-
|
|
336
|
-
router.get('/search', (req, res) => {
|
|
337
|
-
try {
|
|
338
|
-
const query = req.query.q || '';
|
|
339
|
-
const category = req.query.category || null;
|
|
340
|
-
const limit = Math.min(parseInt(req.query.limit) || 10, 100);
|
|
341
|
-
|
|
342
|
-
let sql = `
|
|
343
|
-
SELECT s.*, d.category, d.tags, d.is_independent, d.commission_rate,
|
|
344
|
-
d.direct_benefit, d.neutrality_score, d.trust_signature
|
|
345
|
-
FROM wab_directory d
|
|
346
|
-
JOIN sites s ON d.site_id = s.id AND s.active = 1
|
|
347
|
-
WHERE d.listed = 1
|
|
348
|
-
`;
|
|
349
|
-
const params = [];
|
|
350
|
-
|
|
351
|
-
if (category) {
|
|
352
|
-
sql += ' AND d.category = ?';
|
|
353
|
-
params.push(category);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
sql += ' ORDER BY d.neutrality_score DESC LIMIT ?';
|
|
357
|
-
params.push(limit * 3);
|
|
358
|
-
|
|
359
|
-
const candidates = db.prepare(sql).all(...params);
|
|
360
|
-
const results = fairnessWeightedSearch(query, candidates).slice(0, limit);
|
|
361
|
-
|
|
362
|
-
res.json(buildCommandResponse(req.query.id || null, {
|
|
363
|
-
query,
|
|
364
|
-
total: results.length,
|
|
365
|
-
fairness_applied: true,
|
|
366
|
-
results: results.map(r => ({
|
|
367
|
-
siteId: r.id,
|
|
368
|
-
name: r.name,
|
|
369
|
-
domain: r.domain,
|
|
370
|
-
description: r.description || '',
|
|
371
|
-
category: r.category || 'general',
|
|
372
|
-
tier: r.tier,
|
|
373
|
-
neutrality_score: r._neutralityScore,
|
|
374
|
-
is_independent: r._isIndependent,
|
|
375
|
-
relevance_score: r._relevance,
|
|
376
|
-
fairness_boost: r._fairnessBoost,
|
|
377
|
-
final_score: r._finalScore,
|
|
378
|
-
endpoints: {
|
|
379
|
-
discover: `/api/wab/discover?siteId=${r.id}`,
|
|
380
|
-
actions: `/api/wab/actions?siteId=${r.id}`,
|
|
381
|
-
bridge: `/api/noscript/bridge/${r.id}`
|
|
382
|
-
}
|
|
383
|
-
}))
|
|
384
|
-
}));
|
|
385
|
-
} catch (err) {
|
|
386
|
-
res.status(500).json(buildErrorResponse(null, 'internal', 'Search failed'));
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
391
|
-
// GET /api/wab/ping — health check
|
|
392
|
-
// ═════════════════════════════════════════════════════════════════════
|
|
393
|
-
|
|
394
|
-
router.get('/ping', (_req, res) => {
|
|
395
|
-
res.json(buildCommandResponse(null, {
|
|
396
|
-
pong: true,
|
|
397
|
-
version: WAB_VERSION,
|
|
398
|
-
protocol: PROTOCOL_VERSION,
|
|
399
|
-
timestamp: Date.now(),
|
|
400
|
-
status: 'healthy'
|
|
401
|
-
}));
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
// ─── Discovery document builder ──────────────────────────────────────
|
|
405
|
-
|
|
406
|
-
function buildDiscovery(site) {
|
|
407
|
-
const config = parseSiteConfig(site);
|
|
408
|
-
const perms = config.agentPermissions || {};
|
|
409
|
-
const features = config.features || {};
|
|
410
|
-
|
|
411
|
-
const commands = Object.entries(perms)
|
|
412
|
-
.filter(([, v]) => v)
|
|
413
|
-
.map(([name]) => ({
|
|
414
|
-
name,
|
|
415
|
-
trigger: name === 'click' ? 'click' : name === 'fillForms' ? 'fill_and_submit' : name === 'scroll' ? 'scroll' : 'api',
|
|
416
|
-
requiresAuth: ['apiAccess', 'automatedLogin', 'extractData'].includes(name)
|
|
417
|
-
}));
|
|
418
|
-
|
|
419
|
-
const featureList = ['auto_discovery', 'noscript_fallback', 'wab_protocol_api'];
|
|
420
|
-
if (features.advancedAnalytics) featureList.push('advanced_analytics');
|
|
421
|
-
if (features.realTimeUpdates) featureList.push('real_time_updates');
|
|
422
|
-
|
|
423
|
-
const dirEntry = db.prepare('SELECT * FROM wab_directory WHERE site_id = ?').get(site.id);
|
|
424
|
-
|
|
425
|
-
return {
|
|
426
|
-
wab_version: WAB_VERSION,
|
|
427
|
-
protocol: PROTOCOL_VERSION,
|
|
428
|
-
generated_at: new Date().toISOString(),
|
|
429
|
-
provider: {
|
|
430
|
-
name: site.name,
|
|
431
|
-
domain: site.domain,
|
|
432
|
-
category: dirEntry?.category || 'general',
|
|
433
|
-
description: site.description || ''
|
|
434
|
-
},
|
|
435
|
-
capabilities: {
|
|
436
|
-
commands,
|
|
437
|
-
permissions: perms,
|
|
438
|
-
tier: site.tier,
|
|
439
|
-
transport: ['js_global', 'http', 'websocket'],
|
|
440
|
-
features: featureList
|
|
441
|
-
},
|
|
442
|
-
agent_access: {
|
|
443
|
-
bridge_script: '/script/ai-agent-bridge.js',
|
|
444
|
-
api_base: '/api/wab',
|
|
445
|
-
websocket: '/ws/analytics',
|
|
446
|
-
noscript: `/api/noscript/bridge/${site.id}`,
|
|
447
|
-
discovery: `/api/discovery/${site.id}`
|
|
448
|
-
},
|
|
449
|
-
fairness: {
|
|
450
|
-
is_independent: dirEntry ? !!dirEntry.is_independent : false,
|
|
451
|
-
commission_rate: dirEntry ? dirEntry.commission_rate : 0,
|
|
452
|
-
direct_benefit: dirEntry ? (dirEntry.direct_benefit || '') : '',
|
|
453
|
-
neutrality_score: calculateNeutralityScore(site)
|
|
454
|
-
},
|
|
455
|
-
security: {
|
|
456
|
-
session_required: true,
|
|
457
|
-
origin_validation: true,
|
|
458
|
-
rate_limit: config.restrictions?.rateLimit?.maxCallsPerMinute || 60,
|
|
459
|
-
sandbox: true
|
|
460
|
-
},
|
|
461
|
-
endpoints: {
|
|
462
|
-
authenticate: '/api/wab/authenticate',
|
|
463
|
-
discover: `/api/wab/discover?siteId=${site.id}`,
|
|
464
|
-
actions: `/api/wab/actions?siteId=${site.id}`,
|
|
465
|
-
execute: '/api/wab/actions/{actionName}',
|
|
466
|
-
read: '/api/wab/read',
|
|
467
|
-
page_info: `/api/wab/page-info?siteId=${site.id}`,
|
|
468
|
-
search: '/api/wab/search',
|
|
469
|
-
ping: '/api/wab/ping',
|
|
470
|
-
token_exchange: '/api/license/token',
|
|
471
|
-
bridge_page: `/api/noscript/bridge/${site.id}`
|
|
472
|
-
}
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
module.exports = router;
|
|
1
|
+
/**
|
|
2
|
+
* WAB Protocol HTTP Transport — RESTful endpoints that implement the
|
|
3
|
+
* WAB command protocol over HTTP for remote agents and the MCP adapter.
|
|
4
|
+
*
|
|
5
|
+
* Every command from the WAB spec (docs/SPEC.md §5) is accessible here
|
|
6
|
+
* so agents that cannot run JavaScript in a browser can still interact
|
|
7
|
+
* with WAB-enabled sites via standard HTTP requests.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const express = require('express');
|
|
11
|
+
const router = express.Router();
|
|
12
|
+
const { findSiteById, findSiteByLicense, recordAnalytic, db } = require('../models/db');
|
|
13
|
+
const { broadcastAnalytic } = require('../ws');
|
|
14
|
+
const {
|
|
15
|
+
calculateNeutralityScore,
|
|
16
|
+
fairnessWeightedSearch,
|
|
17
|
+
getDirectoryListings,
|
|
18
|
+
generateFairnessReport
|
|
19
|
+
} = require('../services/fairness');
|
|
20
|
+
|
|
21
|
+
const WAB_VERSION = '1.2.0';
|
|
22
|
+
const PROTOCOL_VERSION = '1.0';
|
|
23
|
+
|
|
24
|
+
// ─── Session management ──────────────────────────────────────────────
|
|
25
|
+
const sessions = new Map();
|
|
26
|
+
const SESSION_TTL = 3600_000;
|
|
27
|
+
|
|
28
|
+
setInterval(() => {
|
|
29
|
+
const now = Date.now();
|
|
30
|
+
for (const [token, data] of sessions) {
|
|
31
|
+
if (now > data.expiresAt) sessions.delete(token);
|
|
32
|
+
}
|
|
33
|
+
}, 300_000);
|
|
34
|
+
|
|
35
|
+
function generateSessionToken() {
|
|
36
|
+
const bytes = require('crypto').randomBytes(32);
|
|
37
|
+
return bytes.toString('hex');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function requireSession(req, res, next) {
|
|
41
|
+
const auth = req.get('Authorization');
|
|
42
|
+
if (!auth || !auth.startsWith('Bearer ')) {
|
|
43
|
+
return res.status(401).json({
|
|
44
|
+
type: 'error',
|
|
45
|
+
error: { code: 'auth_required', message: 'Bearer token required in Authorization header' }
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const token = auth.slice(7);
|
|
49
|
+
const session = sessions.get(token);
|
|
50
|
+
if (!session || Date.now() > session.expiresAt) {
|
|
51
|
+
sessions.delete(token);
|
|
52
|
+
return res.status(401).json({
|
|
53
|
+
type: 'error',
|
|
54
|
+
error: { code: 'session_expired', message: 'Session expired or invalid' }
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
req.wabSession = session;
|
|
58
|
+
next();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ─── Helper: resolve site from request ───────────────────────────────
|
|
62
|
+
function resolveSite(req) {
|
|
63
|
+
if (req.wabSession) return findSiteById.get(req.wabSession.siteId);
|
|
64
|
+
const siteId = req.query.siteId || req.body?.siteId;
|
|
65
|
+
if (siteId) return findSiteById.get(siteId);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function parseSiteConfig(site) {
|
|
70
|
+
try { return JSON.parse(site.config || '{}'); } catch (_) { return {}; }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildCommandResponse(id, result) {
|
|
74
|
+
return { id: id || null, type: 'success', protocol: PROTOCOL_VERSION, result };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function buildErrorResponse(id, code, message) {
|
|
78
|
+
return { id: id || null, type: 'error', protocol: PROTOCOL_VERSION, error: { code, message } };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
82
|
+
// POST /api/wab/authenticate — session token exchange
|
|
83
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
84
|
+
|
|
85
|
+
router.post('/authenticate', (req, res) => {
|
|
86
|
+
try {
|
|
87
|
+
const { siteId, apiKey, meta } = req.body;
|
|
88
|
+
if (!siteId && !apiKey) {
|
|
89
|
+
return res.status(400).json(buildErrorResponse(null, 'invalid_argument', 'siteId or apiKey required'));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let site;
|
|
93
|
+
if (apiKey) {
|
|
94
|
+
site = db.prepare('SELECT * FROM sites WHERE api_key = ? AND active = 1').get(apiKey);
|
|
95
|
+
} else {
|
|
96
|
+
site = findSiteById.get(siteId);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (!site) {
|
|
100
|
+
return res.status(404).json(buildErrorResponse(null, 'not_found', 'Site not found or invalid credentials'));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const origin = req.get('origin') || '';
|
|
104
|
+
if (origin) {
|
|
105
|
+
try {
|
|
106
|
+
const reqDomain = new URL(origin).hostname.replace(/^www\./, '');
|
|
107
|
+
const siteDomain = site.domain.replace(/^www\./, '');
|
|
108
|
+
if (reqDomain !== siteDomain && reqDomain !== 'localhost' && reqDomain !== '127.0.0.1') {
|
|
109
|
+
return res.status(403).json(buildErrorResponse(null, 'origin_mismatch', 'Origin does not match site domain'));
|
|
110
|
+
}
|
|
111
|
+
} catch (_) {}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const token = generateSessionToken();
|
|
115
|
+
sessions.set(token, {
|
|
116
|
+
siteId: site.id,
|
|
117
|
+
tier: site.tier,
|
|
118
|
+
domain: site.domain,
|
|
119
|
+
agentMeta: meta || {},
|
|
120
|
+
createdAt: Date.now(),
|
|
121
|
+
expiresAt: Date.now() + SESSION_TTL
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
res.json(buildCommandResponse(null, {
|
|
125
|
+
authenticated: true,
|
|
126
|
+
token,
|
|
127
|
+
siteId: site.id,
|
|
128
|
+
tier: site.tier,
|
|
129
|
+
expiresIn: SESSION_TTL / 1000,
|
|
130
|
+
permissions: parseSiteConfig(site).agentPermissions || {}
|
|
131
|
+
}));
|
|
132
|
+
} catch (err) {
|
|
133
|
+
res.status(500).json(buildErrorResponse(null, 'internal', 'Authentication failed'));
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
138
|
+
// GET /api/wab/discover — full discovery document
|
|
139
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
140
|
+
|
|
141
|
+
router.get('/discover', (req, res) => {
|
|
142
|
+
try {
|
|
143
|
+
const site = resolveSite(req);
|
|
144
|
+
if (!site || !site.active) {
|
|
145
|
+
const domain = (req.get('origin') ? new URL(req.get('origin')).hostname : req.get('host')?.split(':')[0]) || '';
|
|
146
|
+
const byDomain = db.prepare(
|
|
147
|
+
'SELECT * FROM sites WHERE LOWER(REPLACE(domain, "www.", "")) = ? AND active = 1 LIMIT 1'
|
|
148
|
+
).get(domain.toLowerCase().replace(/^www\./, ''));
|
|
149
|
+
|
|
150
|
+
if (!byDomain) {
|
|
151
|
+
return res.status(404).json(buildErrorResponse(null, 'not_found', 'No WAB site found'));
|
|
152
|
+
}
|
|
153
|
+
return res.json(buildCommandResponse(null, buildDiscovery(byDomain)));
|
|
154
|
+
}
|
|
155
|
+
res.json(buildCommandResponse(null, buildDiscovery(site)));
|
|
156
|
+
} catch (err) {
|
|
157
|
+
res.status(500).json(buildErrorResponse(null, 'internal', 'Discovery failed'));
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
162
|
+
// GET /api/wab/actions — list actions
|
|
163
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
164
|
+
|
|
165
|
+
router.get('/actions', (req, res) => {
|
|
166
|
+
try {
|
|
167
|
+
const site = resolveSite(req);
|
|
168
|
+
if (!site) return res.status(400).json(buildErrorResponse(null, 'invalid_argument', 'siteId required'));
|
|
169
|
+
|
|
170
|
+
const config = parseSiteConfig(site);
|
|
171
|
+
const perms = config.agentPermissions || {};
|
|
172
|
+
const category = req.query.category;
|
|
173
|
+
|
|
174
|
+
const actions = Object.entries(perms)
|
|
175
|
+
.filter(([, v]) => v)
|
|
176
|
+
.map(([name]) => ({
|
|
177
|
+
name,
|
|
178
|
+
description: `Permission: ${name}`,
|
|
179
|
+
trigger: name === 'click' ? 'click' : name === 'fillForms' ? 'fill_and_submit' : name === 'scroll' ? 'scroll' : 'api',
|
|
180
|
+
category: name === 'navigate' ? 'navigation' : 'general',
|
|
181
|
+
requiresAuth: ['apiAccess', 'automatedLogin', 'extractData'].includes(name)
|
|
182
|
+
}));
|
|
183
|
+
|
|
184
|
+
const filtered = category ? actions.filter(a => a.category === category) : actions;
|
|
185
|
+
|
|
186
|
+
res.json(buildCommandResponse(req.query.id || null, { actions: filtered, total: filtered.length }));
|
|
187
|
+
} catch (err) {
|
|
188
|
+
res.status(500).json(buildErrorResponse(null, 'internal', 'Failed to list actions'));
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
193
|
+
// POST /api/wab/actions/:name — execute action (with tracking)
|
|
194
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
195
|
+
|
|
196
|
+
router.post('/actions/:name', requireSession, (req, res) => {
|
|
197
|
+
try {
|
|
198
|
+
const actionName = req.params.name;
|
|
199
|
+
const site = findSiteById.get(req.wabSession.siteId);
|
|
200
|
+
if (!site) return res.status(404).json(buildErrorResponse(req.body?.id, 'not_found', 'Site not found'));
|
|
201
|
+
|
|
202
|
+
const config = parseSiteConfig(site);
|
|
203
|
+
const perms = config.agentPermissions || {};
|
|
204
|
+
|
|
205
|
+
const permMap = {
|
|
206
|
+
click: 'click', fill_and_submit: 'fillForms', scroll: 'scroll',
|
|
207
|
+
navigate: 'navigate', api: 'apiAccess', read: 'readContent', extract: 'extractData'
|
|
208
|
+
};
|
|
209
|
+
const requiredPerm = permMap[actionName] || actionName;
|
|
210
|
+
|
|
211
|
+
if (!perms[requiredPerm] && !perms[actionName]) {
|
|
212
|
+
return res.status(403).json(buildErrorResponse(req.body?.id, 'permission_denied',
|
|
213
|
+
`Action "${actionName}" is not permitted by site configuration`));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
recordAnalytic({
|
|
217
|
+
siteId: site.id,
|
|
218
|
+
actionName,
|
|
219
|
+
agentId: req.wabSession.agentMeta?.name || 'mcp-agent',
|
|
220
|
+
triggerType: 'wab_api',
|
|
221
|
+
success: true,
|
|
222
|
+
metadata: { params: req.body?.params || {}, transport: 'http' }
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
broadcastAnalytic(site.id, {
|
|
226
|
+
actionName,
|
|
227
|
+
agentId: req.wabSession.agentMeta?.name || 'mcp-agent',
|
|
228
|
+
triggerType: 'wab_api',
|
|
229
|
+
success: true
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
res.json(buildCommandResponse(req.body?.id, {
|
|
233
|
+
success: true,
|
|
234
|
+
action: actionName,
|
|
235
|
+
siteId: site.id,
|
|
236
|
+
executed_at: new Date().toISOString(),
|
|
237
|
+
note: 'Server-side action recorded. For DOM interactions, use the bridge script in-browser.'
|
|
238
|
+
}));
|
|
239
|
+
} catch (err) {
|
|
240
|
+
res.status(500).json(buildErrorResponse(req.body?.id, 'internal', 'Action execution failed'));
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
245
|
+
// POST /api/wab/read — read content (selector-based, requires in-browser)
|
|
246
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
247
|
+
|
|
248
|
+
router.post('/read', requireSession, (req, res) => {
|
|
249
|
+
try {
|
|
250
|
+
const { selector, id } = req.body;
|
|
251
|
+
if (!selector) {
|
|
252
|
+
return res.status(400).json(buildErrorResponse(id, 'invalid_argument', 'selector is required'));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const site = findSiteById.get(req.wabSession.siteId);
|
|
256
|
+
if (!site) return res.status(404).json(buildErrorResponse(id, 'not_found', 'Site not found'));
|
|
257
|
+
|
|
258
|
+
const config = parseSiteConfig(site);
|
|
259
|
+
if (!config.agentPermissions?.readContent) {
|
|
260
|
+
return res.status(403).json(buildErrorResponse(id, 'permission_denied', 'readContent not enabled'));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
recordAnalytic({
|
|
264
|
+
siteId: site.id,
|
|
265
|
+
actionName: 'readContent',
|
|
266
|
+
agentId: req.wabSession.agentMeta?.name || 'mcp-agent',
|
|
267
|
+
triggerType: 'wab_api',
|
|
268
|
+
success: true,
|
|
269
|
+
metadata: { selector, transport: 'http' }
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
res.json(buildCommandResponse(id, {
|
|
273
|
+
success: true,
|
|
274
|
+
selector,
|
|
275
|
+
note: 'Content reading via HTTP returns metadata only. Use the bridge script in-browser or the noscript bridge for rendered content.',
|
|
276
|
+
bridge_page: `/api/noscript/bridge/${site.id}`,
|
|
277
|
+
noscript_endpoints: {
|
|
278
|
+
pixel: `/api/noscript/pixel/${site.id}`,
|
|
279
|
+
css: `/api/noscript/css/${site.id}`,
|
|
280
|
+
bridge: `/api/noscript/bridge/${site.id}`
|
|
281
|
+
}
|
|
282
|
+
}));
|
|
283
|
+
} catch (err) {
|
|
284
|
+
res.status(500).json(buildErrorResponse(null, 'internal', 'Read failed'));
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
289
|
+
// GET /api/wab/page-info — get page/site metadata
|
|
290
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
291
|
+
|
|
292
|
+
router.get('/page-info', (req, res) => {
|
|
293
|
+
try {
|
|
294
|
+
const site = resolveSite(req);
|
|
295
|
+
if (!site) return res.status(400).json(buildErrorResponse(null, 'invalid_argument', 'siteId required'));
|
|
296
|
+
|
|
297
|
+
const config = parseSiteConfig(site);
|
|
298
|
+
const neutralityScore = calculateNeutralityScore(site);
|
|
299
|
+
|
|
300
|
+
res.json(buildCommandResponse(req.query.id || null, {
|
|
301
|
+
title: site.name,
|
|
302
|
+
domain: site.domain,
|
|
303
|
+
url: `https://${site.domain}`,
|
|
304
|
+
tier: site.tier,
|
|
305
|
+
bridgeVersion: WAB_VERSION,
|
|
306
|
+
protocol: PROTOCOL_VERSION,
|
|
307
|
+
permissions: config.agentPermissions || {},
|
|
308
|
+
restrictions: config.restrictions || {},
|
|
309
|
+
security: {
|
|
310
|
+
sandboxActive: true,
|
|
311
|
+
sessionRequired: true,
|
|
312
|
+
originValidation: true,
|
|
313
|
+
rateLimit: config.restrictions?.rateLimit?.maxCallsPerMinute || 60
|
|
314
|
+
},
|
|
315
|
+
fairness: {
|
|
316
|
+
neutralityScore,
|
|
317
|
+
isIndependent: false
|
|
318
|
+
},
|
|
319
|
+
endpoints: {
|
|
320
|
+
discover: `/api/wab/discover?siteId=${site.id}`,
|
|
321
|
+
actions: `/api/wab/actions?siteId=${site.id}`,
|
|
322
|
+
authenticate: '/api/wab/authenticate',
|
|
323
|
+
bridge: `/api/noscript/bridge/${site.id}`,
|
|
324
|
+
discovery: `/api/discovery/${site.id}`
|
|
325
|
+
}
|
|
326
|
+
}));
|
|
327
|
+
} catch (err) {
|
|
328
|
+
res.status(500).json(buildErrorResponse(null, 'internal', 'Failed to get page info'));
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
333
|
+
// GET /api/wab/search — fairness-weighted search (MCP adapter uses this)
|
|
334
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
335
|
+
|
|
336
|
+
router.get('/search', (req, res) => {
|
|
337
|
+
try {
|
|
338
|
+
const query = req.query.q || '';
|
|
339
|
+
const category = req.query.category || null;
|
|
340
|
+
const limit = Math.min(parseInt(req.query.limit) || 10, 100);
|
|
341
|
+
|
|
342
|
+
let sql = `
|
|
343
|
+
SELECT s.*, d.category, d.tags, d.is_independent, d.commission_rate,
|
|
344
|
+
d.direct_benefit, d.neutrality_score, d.trust_signature
|
|
345
|
+
FROM wab_directory d
|
|
346
|
+
JOIN sites s ON d.site_id = s.id AND s.active = 1
|
|
347
|
+
WHERE d.listed = 1
|
|
348
|
+
`;
|
|
349
|
+
const params = [];
|
|
350
|
+
|
|
351
|
+
if (category) {
|
|
352
|
+
sql += ' AND d.category = ?';
|
|
353
|
+
params.push(category);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
sql += ' ORDER BY d.neutrality_score DESC LIMIT ?';
|
|
357
|
+
params.push(limit * 3);
|
|
358
|
+
|
|
359
|
+
const candidates = db.prepare(sql).all(...params);
|
|
360
|
+
const results = fairnessWeightedSearch(query, candidates).slice(0, limit);
|
|
361
|
+
|
|
362
|
+
res.json(buildCommandResponse(req.query.id || null, {
|
|
363
|
+
query,
|
|
364
|
+
total: results.length,
|
|
365
|
+
fairness_applied: true,
|
|
366
|
+
results: results.map(r => ({
|
|
367
|
+
siteId: r.id,
|
|
368
|
+
name: r.name,
|
|
369
|
+
domain: r.domain,
|
|
370
|
+
description: r.description || '',
|
|
371
|
+
category: r.category || 'general',
|
|
372
|
+
tier: r.tier,
|
|
373
|
+
neutrality_score: r._neutralityScore,
|
|
374
|
+
is_independent: r._isIndependent,
|
|
375
|
+
relevance_score: r._relevance,
|
|
376
|
+
fairness_boost: r._fairnessBoost,
|
|
377
|
+
final_score: r._finalScore,
|
|
378
|
+
endpoints: {
|
|
379
|
+
discover: `/api/wab/discover?siteId=${r.id}`,
|
|
380
|
+
actions: `/api/wab/actions?siteId=${r.id}`,
|
|
381
|
+
bridge: `/api/noscript/bridge/${r.id}`
|
|
382
|
+
}
|
|
383
|
+
}))
|
|
384
|
+
}));
|
|
385
|
+
} catch (err) {
|
|
386
|
+
res.status(500).json(buildErrorResponse(null, 'internal', 'Search failed'));
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
391
|
+
// GET /api/wab/ping — health check
|
|
392
|
+
// ═════════════════════════════════════════════════════════════════════
|
|
393
|
+
|
|
394
|
+
router.get('/ping', (_req, res) => {
|
|
395
|
+
res.json(buildCommandResponse(null, {
|
|
396
|
+
pong: true,
|
|
397
|
+
version: WAB_VERSION,
|
|
398
|
+
protocol: PROTOCOL_VERSION,
|
|
399
|
+
timestamp: Date.now(),
|
|
400
|
+
status: 'healthy'
|
|
401
|
+
}));
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// ─── Discovery document builder ──────────────────────────────────────
|
|
405
|
+
|
|
406
|
+
function buildDiscovery(site) {
|
|
407
|
+
const config = parseSiteConfig(site);
|
|
408
|
+
const perms = config.agentPermissions || {};
|
|
409
|
+
const features = config.features || {};
|
|
410
|
+
|
|
411
|
+
const commands = Object.entries(perms)
|
|
412
|
+
.filter(([, v]) => v)
|
|
413
|
+
.map(([name]) => ({
|
|
414
|
+
name,
|
|
415
|
+
trigger: name === 'click' ? 'click' : name === 'fillForms' ? 'fill_and_submit' : name === 'scroll' ? 'scroll' : 'api',
|
|
416
|
+
requiresAuth: ['apiAccess', 'automatedLogin', 'extractData'].includes(name)
|
|
417
|
+
}));
|
|
418
|
+
|
|
419
|
+
const featureList = ['auto_discovery', 'noscript_fallback', 'wab_protocol_api'];
|
|
420
|
+
if (features.advancedAnalytics) featureList.push('advanced_analytics');
|
|
421
|
+
if (features.realTimeUpdates) featureList.push('real_time_updates');
|
|
422
|
+
|
|
423
|
+
const dirEntry = db.prepare('SELECT * FROM wab_directory WHERE site_id = ?').get(site.id);
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
wab_version: WAB_VERSION,
|
|
427
|
+
protocol: PROTOCOL_VERSION,
|
|
428
|
+
generated_at: new Date().toISOString(),
|
|
429
|
+
provider: {
|
|
430
|
+
name: site.name,
|
|
431
|
+
domain: site.domain,
|
|
432
|
+
category: dirEntry?.category || 'general',
|
|
433
|
+
description: site.description || ''
|
|
434
|
+
},
|
|
435
|
+
capabilities: {
|
|
436
|
+
commands,
|
|
437
|
+
permissions: perms,
|
|
438
|
+
tier: site.tier,
|
|
439
|
+
transport: ['js_global', 'http', 'websocket'],
|
|
440
|
+
features: featureList
|
|
441
|
+
},
|
|
442
|
+
agent_access: {
|
|
443
|
+
bridge_script: '/script/ai-agent-bridge.js',
|
|
444
|
+
api_base: '/api/wab',
|
|
445
|
+
websocket: '/ws/analytics',
|
|
446
|
+
noscript: `/api/noscript/bridge/${site.id}`,
|
|
447
|
+
discovery: `/api/discovery/${site.id}`
|
|
448
|
+
},
|
|
449
|
+
fairness: {
|
|
450
|
+
is_independent: dirEntry ? !!dirEntry.is_independent : false,
|
|
451
|
+
commission_rate: dirEntry ? dirEntry.commission_rate : 0,
|
|
452
|
+
direct_benefit: dirEntry ? (dirEntry.direct_benefit || '') : '',
|
|
453
|
+
neutrality_score: calculateNeutralityScore(site)
|
|
454
|
+
},
|
|
455
|
+
security: {
|
|
456
|
+
session_required: true,
|
|
457
|
+
origin_validation: true,
|
|
458
|
+
rate_limit: config.restrictions?.rateLimit?.maxCallsPerMinute || 60,
|
|
459
|
+
sandbox: true
|
|
460
|
+
},
|
|
461
|
+
endpoints: {
|
|
462
|
+
authenticate: '/api/wab/authenticate',
|
|
463
|
+
discover: `/api/wab/discover?siteId=${site.id}`,
|
|
464
|
+
actions: `/api/wab/actions?siteId=${site.id}`,
|
|
465
|
+
execute: '/api/wab/actions/{actionName}',
|
|
466
|
+
read: '/api/wab/read',
|
|
467
|
+
page_info: `/api/wab/page-info?siteId=${site.id}`,
|
|
468
|
+
search: '/api/wab/search',
|
|
469
|
+
ping: '/api/wab/ping',
|
|
470
|
+
token_exchange: '/api/license/token',
|
|
471
|
+
bridge_page: `/api/noscript/bridge/${site.id}`
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
module.exports = router;
|