web-agent-bridge 1.2.0 → 2.1.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.
Files changed (111) hide show
  1. package/LICENSE +21 -21
  2. package/README.ar.md +572 -446
  3. package/README.md +968 -933
  4. package/bin/agent-runner.js +465 -0
  5. package/bin/cli.js +138 -80
  6. package/bin/wab.js +80 -80
  7. package/examples/bidi-agent.js +119 -119
  8. package/examples/mcp-agent.js +94 -94
  9. package/examples/next-app-router/README.md +44 -0
  10. package/examples/puppeteer-agent.js +108 -108
  11. package/examples/saas-dashboard/README.md +55 -0
  12. package/examples/shopify-hydrogen/README.md +74 -0
  13. package/examples/vision-agent.js +171 -171
  14. package/examples/wordpress-elementor/README.md +77 -0
  15. package/package.json +71 -78
  16. package/public/.well-known/ai-assets.json +59 -0
  17. package/public/admin/login.html +84 -84
  18. package/public/ai.html +196 -0
  19. package/public/cookies.html +208 -208
  20. package/public/css/premium.css +317 -0
  21. package/public/css/styles.css +1235 -1235
  22. package/public/dashboard.html +704 -704
  23. package/public/demo.html +259 -0
  24. package/public/docs.html +585 -585
  25. package/public/feed.xml +89 -0
  26. package/public/index.html +581 -332
  27. package/public/js/auth-nav.js +31 -31
  28. package/public/js/auth-redirect.js +12 -12
  29. package/public/js/cookie-consent.js +56 -56
  30. package/public/js/wab-demo-page.js +721 -0
  31. package/public/js/ws-client.js +74 -74
  32. package/public/llms-full.txt +309 -0
  33. package/public/llms.txt +85 -0
  34. package/public/login.html +83 -83
  35. package/public/openapi.json +580 -0
  36. package/public/premium-dashboard.html +2487 -0
  37. package/public/premium.html +791 -0
  38. package/public/privacy.html +295 -295
  39. package/public/register.html +103 -103
  40. package/public/robots.txt +87 -0
  41. package/public/script/wab-consent.d.ts +36 -0
  42. package/public/script/wab-consent.js +104 -0
  43. package/public/script/wab-schema.js +131 -0
  44. package/public/script/wab.d.ts +108 -0
  45. package/public/script/wab.min.js +405 -0
  46. package/public/sitemap.xml +93 -0
  47. package/public/sovereign.html +660 -0
  48. package/public/terms.html +254 -254
  49. package/public/video/tutorial.mp4 +0 -0
  50. package/script/ai-agent-bridge.js +1558 -1513
  51. package/sdk/README.md +55 -55
  52. package/sdk/index.d.ts +118 -0
  53. package/sdk/index.js +257 -203
  54. package/sdk/package.json +14 -14
  55. package/sdk/schema-discovery.js +83 -0
  56. package/server/config/secrets.js +94 -92
  57. package/server/index.js +2 -9
  58. package/server/middleware/adminAuth.js +30 -30
  59. package/server/middleware/auth.js +41 -41
  60. package/server/middleware/rateLimits.js +24 -24
  61. package/server/migrations/001_add_analytics_indexes.sql +7 -7
  62. package/server/migrations/002_premium_features.sql +418 -0
  63. package/server/models/adapters/index.js +33 -33
  64. package/server/models/adapters/mysql.js +183 -183
  65. package/server/models/adapters/postgresql.js +172 -172
  66. package/server/models/adapters/sqlite.js +7 -7
  67. package/server/models/db.js +561 -561
  68. package/server/routes/admin-premium.js +671 -0
  69. package/server/routes/admin.js +247 -247
  70. package/server/routes/api.js +131 -138
  71. package/server/routes/auth.js +51 -51
  72. package/server/routes/billing.js +45 -45
  73. package/server/routes/discovery.js +406 -329
  74. package/server/routes/license.js +240 -240
  75. package/server/routes/noscript.js +543 -543
  76. package/server/routes/premium-v2.js +686 -0
  77. package/server/routes/premium.js +724 -0
  78. package/server/routes/sovereign.js +307 -0
  79. package/server/routes/wab-api.js +476 -476
  80. package/server/services/agent-memory.js +625 -0
  81. package/server/services/email.js +204 -204
  82. package/server/services/fairness.js +420 -420
  83. package/server/services/negotiation.js +439 -0
  84. package/server/services/plugins.js +747 -0
  85. package/server/services/premium.js +1883 -0
  86. package/server/services/reputation.js +465 -0
  87. package/server/services/self-healing.js +843 -0
  88. package/server/services/stripe.js +192 -192
  89. package/server/services/swarm.js +788 -0
  90. package/server/services/verification.js +481 -0
  91. package/server/services/vision.js +871 -0
  92. package/server/utils/cache.js +125 -125
  93. package/server/utils/migrate.js +81 -81
  94. package/server/utils/secureFields.js +50 -50
  95. package/server/ws.js +101 -101
  96. package/templates/artisan-marketplace.yaml +104 -0
  97. package/templates/book-price-scout.yaml +98 -0
  98. package/templates/electronics-price-tracker.yaml +108 -0
  99. package/templates/flight-deal-hunter.yaml +113 -0
  100. package/templates/freelancer-direct.yaml +116 -0
  101. package/templates/grocery-price-compare.yaml +93 -0
  102. package/templates/hotel-direct-booking.yaml +113 -0
  103. package/templates/local-services.yaml +98 -0
  104. package/templates/olive-oil-tunisia.yaml +88 -0
  105. package/templates/organic-farm-fresh.yaml +101 -0
  106. package/templates/restaurant-direct.yaml +97 -0
  107. package/docs/DEPLOY.md +0 -118
  108. package/docs/SPEC.md +0 -1540
  109. package/wab-mcp-adapter/README.md +0 -136
  110. package/wab-mcp-adapter/index.js +0 -555
  111. package/wab-mcp-adapter/package.json +0 -17
package/README.md CHANGED
@@ -1,933 +1,968 @@
1
- # Web Agent Bridge (WAB)
2
-
3
- [![npm](https://img.shields.io/npm/v/web-agent-bridge)](https://www.npmjs.com/package/web-agent-bridge)
4
- [![CI](https://github.com/abokenan444/web-agent-bridge/actions/workflows/ci.yml/badge.svg)](https://github.com/abokenan444/web-agent-bridge/actions/workflows/ci.yml)
5
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
- [![Node.js](https://img.shields.io/badge/node-%3E%3D18-brightgreen.svg)](https://nodejs.org/)
7
- [![Docker](https://img.shields.io/badge/docker-ready-blue.svg)](https://hub.docker.com/)
8
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
9
- [![Socket](https://img.shields.io/badge/Socket-Verified-brightgreen.svg)](https://socket.dev/npm/package/web-agent-bridge)
10
-
11
- > **robots.txt told bots what NOT to do. WAB tells AI agents what they CAN do.**
12
-
13
- **English** | **[العربية](README.ar.md)** | **[Protocol Spec](docs/SPEC.md)** | **[Socket Report](https://socket.dev/npm/package/web-agent-bridge)**
14
-
15
- WAB is an **open protocol + runtime** for AI agents to interact with websites — the **OpenAPI for human-facing pages**. It provides a standardized discovery format (`agent-bridge.json`), a command protocol, and a fairness layer that ensures small businesses get equal visibility alongside large platforms.
16
-
17
- WAB transforms websites from opaque HTML into **agent-readable endpoints** with declared capabilities, permissions, and actions. AI agents discover what a site offers, authenticate, and execute commands through a uniform protocol — no DOM scraping, no guesswork, no bias toward big brands.
18
-
19
- ### Architecture: Protocol + Runtime + Ecosystem
20
-
21
- ```
22
- ┌──────────────────────────────────────────────────────────┐
23
- │ WAB Protocol (Spec)
24
- │ agent-bridge.json · Commands · Lifecycle · Fairness │
25
- ├────────────┬───────────────────┬─────────────────────────┤
26
- │ JS Runtime │ HTTP Transport │ MCP Adapter │
27
- AICommands │ REST + WebSocket │ WAB → MCP Tools │
28
- ├────────────┴───────────────────┴─────────────────────────┤
29
- Discovery Registry + Fairness Engine │
30
- └──────────────────────────────────────────────────────────┘
31
- ```
32
-
33
- ### Three Paths to WAB
34
-
35
- | Path | For | How |
36
- |---|---|---|
37
- | **🏢 Website Owner** | Control how AI interacts with your site | Embed the script, publish `agent-bridge.json` |
38
- | **🤖 Agent Developer** | Build reliable agents that work on any WAB-enabled site | Use `window.AICommands`, the Agent SDK, or the MCP Adapter |
39
- | **🔧 Self-Hosting** | Run the full WAB platform for your organization | Clone, deploy, manage licenses & analytics |
40
- | **🔌 MCP Integration** | Connect WAB to Claude, GPT, or any MCP-compatible agent | Use `wab-mcp-adapter` |
41
- | **WordPress** | Sites powered by WP | Use the **[Web Agent Bridge WordPress plugin](web-agent-bridge-wordpress/README.md)** |
42
-
43
- ---
44
-
45
- ## What's New in v1.1.0
46
-
47
- - **WAB Protocol Specification v1.0** — Formal protocol spec (`docs/SPEC.md`) with discovery format, command protocol, lifecycle, and fairness layer
48
- - **Discovery Protocol** — Sites publish `agent-bridge.json` or `/.well-known/wab.json` for agent discovery
49
- - **MCP Adapter** — `wab-mcp-adapter` converts WAB actions into MCP tools for Claude/GPT integration
50
- - **Fairness Engine** — Neutrality layer ensures equal visibility for small businesses and independent sites
51
- - **Discovery Registry** — Public searchable directory of WAB-enabled sites with fairness-weighted results
52
- - **9 Protocol Commands** — `wab.discover`, `wab.getContext`, `wab.getActions`, `wab.executeAction`, `wab.readContent`, `wab.getPageInfo`, `wab.authenticate`, `wab.subscribe`, `wab.ping`
53
-
54
- ---
55
-
56
- ## Features
57
-
58
- ### Protocol Layer
59
- - **WAB Specification v1.0** — Formal protocol spec defining discovery, commands, lifecycle, and fairness ([docs/SPEC.md](docs/SPEC.md))
60
- - **Discovery Protocol** Sites declare capabilities via `agent-bridge.json` or `/.well-known/wab.json`
61
- - **Command Protocol** — 9 standard methods with request/response format (transport-agnostic)
62
- - **Lifecycle Protocol** — Discover Authenticate Plan Execute Confirm
63
- - **MCP Compatibility** — Full adapter for Model Context Protocol (Claude, GPT, LangChain)
64
-
65
- ### Fairness & Neutrality
66
- - **Fairness Engine** — Neutrality scoring ensures small businesses get equal agent visibility
67
- - **Discovery Registry** — Public directory of WAB-enabled sites with anti-bias ranking
68
- - **Commission Transparency** — Sites declare commission rates; agents can favor direct providers
69
- - **Independent Business Priority** — Self-declared independent sites get fairness scoring boost
70
-
71
- ### Runtime
72
- - **Auto-Discovery** — Automatically detects buttons, forms, and navigation on the page
73
- - **Permission System** Granular control over what AI agents can do (click, fill forms, API access, etc.)
74
- - **Standardized Interface** — Unified `window.AICommands` object any agent can consume
75
- - **Self-Healing Selectors** — Resilient element resolution with 7-strategy fuzzy matching for SPAs
76
- - **Security Sandbox** Origin validation, session tokens, command signing, audit logging, auto-lockdown
77
- - **Stealth Mode** — Human-like interaction patterns (requires explicit consent)
78
- - **NoJS Fallback** — CSS tracking, pixel tracking, and SSR bridge for JavaScript-disabled environments
79
-
80
- ### Infrastructure
81
- - **Secure License Exchange** — Embed uses `siteId` + token exchange; keys stay in the dashboard
82
- - **Rate Limiting** — Multi-dimensional abuse protection (IP + license key + site)
83
- - **Analytics Dashboard** Track how AI agents interact with your site
84
- - **Real-Time Analytics** — WebSocket-based live event streaming with auto-reconnection
85
- - **WebDriver BiDi Compatible** — Standard protocol support via `window.__wab_bidi`
86
- - **CDN Versioning** — Serve scripts via versioned URLs (`/v1/ai-agent-bridge.js`, `/latest/ai-agent-bridge.js`)
87
- - **Docker Ready** — One-command deployment with Docker Compose
88
- - **Multi-Database** — SQLite (default), PostgreSQL, MySQL via pluggable adapters
89
- - **Agent SDK** — Built-in SDK for building AI agents with Puppeteer/Playwright
90
- - **Admin Dashboard** User management, tier grants, system analytics
91
- - **Stripe Integration** — Payment processing with customer portal
92
-
93
- ---
94
-
95
- ## Premium Services (webagentbridge.com)
96
-
97
- The open-source core is free forever. For teams and businesses that need more, **[webagentbridge.com](https://webagentbridge.com/premium)** offers paid add-ons and higher-tier plans:
98
-
99
- | # | Service | Plans |
100
- |---|---------|-------|
101
- | 1 | **Agent Traffic Intelligence** — Deep analytics: agent type, platform, country, behavioral classification, anomaly alerts | Starter+ |
102
- | 2 | **Advanced Exploit Shield** — Behavioral fingerprint blocking, unauthorized command detection, periodic security reports | Pro+ |
103
- | 3 | **Pre-built Smart Actions Library** — Ready-made action packs for WooCommerce, Shopify, WordPress, Salesforce with auto-updates | Starter+ |
104
- | 4 | **Custom AI Agents as a Service** — Visual agent builder, task scheduling, cloud-based execution | Pro+ |
105
- | 5 | **CRM & Cloud Integrations** — Salesforce, HubSpot, Zoho; export to BigQuery/Snowflake/Datadog; custom webhooks | Pro+ |
106
- | 6 | **Multi-Tenant Permission Management** — Sub-users, per-user quotas, central control panel for agencies | Enterprise |
107
- | 7 | **AI-Powered Priority Support** — Smart chatbot, live video sessions, SLA from 15 min (Enterprise) to same-day (Starter) | Starter+ |
108
- | 8 | **Custom Bridge Script** — Plugin-based custom actions, performance optimization, automatic zero-day patches | Pro+ |
109
- | 9 | **Stealth Mode Pro** — Customizable human-like behavior profiles, anti-detection bypass, monthly fingerprint updates | Pro+ |
110
- | 10 | **Private CDN** — Global edge network, custom domain (`bridge.yoursite.com`), geo performance stats | Pro+ |
111
- | 11 | **Extended Audit Logs & Compliance** — 7-year retention, HIPAA/GDPR/SOC2 settings, signed PDF/CSV exports | Enterprise |
112
- | 12 | **Virtual Sandbox Environment** — Isolated test environment, simulated agent traffic, before/after benchmarks | Enterprise |
113
-
114
- Plans: **Free** (open source) → **Starter $9/mo** → **Pro $29/mo** → **Enterprise (custom)**. Visit [webagentbridge.com/premium](https://webagentbridge.com/premium) for details.
115
-
116
- ---
117
-
118
- ## Quick Start
119
-
120
- ### 1. Install & Run the Server
121
-
122
- ```bash
123
- # Option A: Clone and run
124
- git clone https://github.com/abokenan444/web-agent-bridge.git
125
- cd web-agent-bridge
126
- npm install
127
- cp .env.example .env
128
- npm start
129
-
130
- # Option B: npx (one command)
131
- npx web-agent-bridge start
132
-
133
- # Option C: Docker
134
- docker compose up -d
135
- ```
136
-
137
- ### 2. Create an Account
138
-
139
- Visit `http://localhost:3000/register` and create an account, then add your site from the dashboard.
140
-
141
- ### 3. Add the Script to Your Website
142
-
143
- ```html
144
- <!-- Recommended: copy the snippet from your dashboard (uses siteId only) -->
145
- <script>
146
- window.AIBridgeConfig = {
147
- siteId: "your-site-uuid-from-dashboard",
148
- configEndpoint: "https://yourserver.com/api/license/token",
149
- agentPermissions: {
150
- readContent: true,
151
- click: true,
152
- fillForms: true,
153
- scroll: true
154
- }
155
- };
156
- </script>
157
- <script src="https://yourserver.com/script/ai-agent-bridge.js"></script>
158
- ```
159
-
160
- The server matches **Origin** to your registered site domain, then returns a short-lived **session token**. Analytics (`/api/license/track`) require that session — not the long-lived license key. Keep the license key in the dashboard only.
161
-
162
- ### 4. AI Agents Can Now Interact
163
-
164
- ```javascript
165
- // From the AI agent's side
166
- const bridge = window.AICommands;
167
- const actions = bridge.getActions(); // discover actions
168
- await bridge.execute("signup"); // execute an action
169
- const info = bridge.getPageInfo(); // get page metadata
170
- ```
171
-
172
- ---
173
-
174
- ## Discovery Protocol (`agent-bridge.json`)
175
-
176
- Any website can declare its WAB capabilities by serving a discovery document at `/agent-bridge.json` or `/.well-known/wab.json`. AI agents fetch this file to understand what a site offers before interacting.
177
-
178
- ```json
179
- {
180
- "wab_version": "1.0",
181
- "provider": {
182
- "name": "Local Bookshop",
183
- "domain": "localbookshop.com",
184
- "category": "e-commerce",
185
- "description": "Independent bookshop"
186
- },
187
- "capabilities": {
188
- "commands": ["readContent", "click", "fillForms"],
189
- "permissions": { "readContent": true, "click": true, "fillForms": true },
190
- "tier": "starter",
191
- "transport": ["js_global", "http"]
192
- },
193
- "agent_access": {
194
- "bridge_script": "/script/ai-agent-bridge.js",
195
- "api_base": "/api/license",
196
- "selectors": { "search": "#search-input", "cart": ".add-to-cart" }
197
- },
198
- "fairness": {
199
- "is_independent": true,
200
- "commission_rate": 0,
201
- "direct_benefit": "Owner is the producer",
202
- "neutrality_score": 85
203
- },
204
- "security": {
205
- "session_required": true,
206
- "origin_validation": true,
207
- "rate_limit": 60,
208
- "sandbox": true
209
- }
210
- }
211
- ```
212
-
213
- WAB servers auto-generate this document from each site's configuration. No manual file creation needed — register your site and the discovery endpoint is live.
214
-
215
- ### Discovery API Endpoints
216
-
217
- | Endpoint | Method | Description |
218
- |---|---|---|
219
- | `/.well-known/wab.json` | GET | Discovery document (domain-matched) |
220
- | `/agent-bridge.json` | GET | Alternative discovery location |
221
- | `/api/discovery/:siteId` | GET | Discovery document for a specific site |
222
- | `/api/discovery/registry` | GET | Public directory of WAB-enabled sites |
223
- | `/api/discovery/search` | GET | Fairness-weighted site search |
224
- | `/api/discovery/register` | POST | Register site in directory (authenticated) |
225
-
226
- ---
227
-
228
- ## MCP Adapter (Model Context Protocol)
229
-
230
- The `wab-mcp-adapter` converts WAB capabilities into MCP tools, so Claude, GPT, LangChain, or any MCP-compatible agent can interact with WAB-enabled sites.
231
-
232
- ```javascript
233
- const { WABMCPAdapter } = require('web-agent-bridge/wab-mcp-adapter');
234
-
235
- const adapter = new WABMCPAdapter({
236
- siteUrl: 'https://example.com',
237
- transport: 'http'
238
- });
239
-
240
- // Discover site capabilities
241
- const discovery = await adapter.discover();
242
-
243
- // Get all available MCP tools
244
- const tools = await adapter.getTools();
245
- // [{ name: 'wab_discover', ... }, { name: 'wab_click_signup', ... }, ...]
246
-
247
- // Execute a tool (just like any MCP tool call)
248
- const result = await adapter.executeTool('wab_execute_action', {
249
- name: 'signup',
250
- data: { email: 'user@test.com' }
251
- });
252
- ```
253
-
254
- ### Built-in MCP Tools
255
-
256
- | Tool | Description |
257
- |---|---|
258
- | `wab_discover` | Discover site capabilities and fairness data |
259
- | `wab_get_actions` | List all available actions |
260
- | `wab_execute_action` | Execute any action by name |
261
- | `wab_read_content` | Read page content by CSS selector |
262
- | `wab_get_page_info` | Get page metadata and bridge status |
263
- | `wab_fairness_search` | Search WAB registry with fairness-weighted results |
264
- | `wab_authenticate` | Authenticate with a WAB site |
265
-
266
- See [`wab-mcp-adapter/README.md`](wab-mcp-adapter/README.md) for the full API reference.
267
-
268
- ---
269
-
270
- ## Fairness Engine
271
-
272
- WAB includes a **Neutrality Layer** — a fairness engine that prevents AI agents from systematically favoring large platforms over small businesses.
273
-
274
- ### How It Works
275
-
276
- 1. **Neutrality Scoring** — Each site gets a score (0-100) based on config quality, trust signatures, and commission transparency — not brand recognition
277
- 2. **Anti-Bias Ranking** Search results are fairness-weighted: independent businesses get +15%, transparent commission sites get +10%
278
- 3. **Position Rotation** — Top results are shuffled to prevent position lock-in
279
- 4. **Monopoly Prevention** — No single provider can dominate more than 30% of top results
280
-
281
- ### Register Your Site
282
-
283
- ```bash
284
- # Register in the WAB discovery directory
285
- curl -X POST https://yourserver.com/api/discovery/register \
286
- -H "Authorization: Bearer YOUR_JWT" \
287
- -H "Content-Type: application/json" \
288
- -d '{
289
- "siteId": "your-site-uuid",
290
- "category": "e-commerce",
291
- "is_independent": true,
292
- "commission_rate": 0,
293
- "direct_benefit": "Products from local artisans",
294
- "tags": ["handmade", "local", "organic"]
295
- }'
296
- ```
297
-
298
- ---
299
-
300
- ## Advanced Premium Features
301
-
302
- The following capabilities extend WAB with AI-powered intelligence layers. They require a **premium subscription** — see [webagentbridge.com/premium](https://webagentbridge.com/premium) for plans.
303
-
304
- ### Agent Memory System
305
-
306
- Long-term memory for AI agents using vector embeddings. Agents remember user preferences, past interactions, and successful navigation paths across sessions.
307
-
308
- | Capability | Description |
309
- |---|---|
310
- | **Vector Embeddings** | TF-IDF based similarity search for intelligent recall |
311
- | **Memory Consolidation** | Auto-merges duplicate memories and decays stale ones |
312
- | **Session Tracking** | Maintains context across multiple agent sessions |
313
- | **Preference Management** | Stores and retrieves user preferences for personalized interactions |
314
-
315
- ### Self-Healing Selectors (Premium)
316
-
317
- Automatic CSS/XPath selector repair when websites change their DOM structure. Goes beyond the built-in 7-strategy resolution with community-powered healing.
318
-
319
- | Strategy | Description |
320
- |---|---|
321
- | **Attribute Match** | Finds elements by matching known attributes |
322
- | **ID Match** | Resolves elements by ID similarity |
323
- | **Text Similarity** | Levenshtein-based fuzzy text matching |
324
- | **Structural Match** | Compares DOM tree position and hierarchy |
325
- | **Class Match** | Identifies elements by CSS class patterns |
326
- | **Community Corrections** | Shared selector fixes across the WAB ecosystem |
327
-
328
- Includes DOM drift detection and element snapshot comparison for proactive repair before selectors break.
329
-
330
- ### Multimodal Vision
331
-
332
- Integrates local and cloud vision models so AI agents can "see" web pages as images instead of parsing raw DOM.
333
-
334
- | Model | Type |
335
- |---|---|
336
- | **Moondream** | Local (lightweight) |
337
- | **LLaVA** | Local (high quality) |
338
- | **GPT-4V** | Cloud (OpenAI) |
339
- | **Claude Vision** | Cloud (Anthropic) |
340
-
341
- - Automatic UI element extraction with bounding boxes and suggested selectors
342
- - Screenshot comparison for visual regression detection
343
- - Encrypted API key storage (AES-256-GCM)
344
-
345
- ### Agent Swarm
346
-
347
- Multi-agent orchestration for complex tasks that benefit from parallel or collaborative execution.
348
-
349
- | Strategy | Description |
350
- |---|---|
351
- | **Parallel** | Run multiple agents simultaneously on independent subtasks |
352
- | **Sequential** | Chain agents in order, passing results forward |
353
- | **Competitive** | Race agents against each other, take the fastest result |
354
- | **Collaborative** | Agents share findings and build on each other's work |
355
-
356
- - Built-in fairness weighting to surface small/indie sites in swarm results
357
- - Real-time task monitoring and result merging
358
- - Automatic price/content extraction from target URLs
359
- - Consensus-based result validation across multiple agents
360
-
361
- ### Plugin Marketplace
362
-
363
- Extensible hook system with 16 integration points for customizing WAB behavior.
364
-
365
- | Official Plugin | Description |
366
- |---|---|
367
- | `fairness-boost` | Amplifies fairness scoring for indie sites |
368
- | `security-monitor` | Real-time threat detection and alerts |
369
- | `analytics-enhanced` | Extended analytics with behavioral classification |
370
- | `auto-healer` | Proactive selector repair using DOM monitoring |
371
- | `memory-optimizer` | Memory consolidation and cleanup scheduling |
372
-
373
- - JSON Schema config validation for all plugins
374
- - Community plugin ratings and download tracking
375
- - Sandboxed handler execution via `Function()` constructor
376
-
377
- ### Premium API Endpoints
378
-
379
- | Route Group | Feature | Endpoints |
380
- |---|---|---|
381
- | `/api/premium/memory/*` | Agent Memory | 13 |
382
- | `/api/premium/healing/*` | Self-Healing | 9 |
383
- | `/api/premium/vision/*` | Vision Inference | 9 |
384
- | `/api/premium/swarm/*` | Agent Swarm | 10 |
385
- | `/api/premium/plugins/*` | Plugin Marketplace | 12 |
386
-
387
- ---
388
-
389
- ## Project Structure
390
-
391
- ```
392
- web-agent-bridge/
393
- ├── docs/
394
- │ └── SPEC.md # WAB Protocol Specification v1.0
395
- ├── server/ # Express.js backend
396
- │ ├── index.js # Server entry point
397
- │ ├── routes/
398
- │ │ ├── auth.js # Authentication (register/login)
399
- │ │ ├── api.js # Sites, config, analytics API
400
- │ │ ├── license.js # License verification & token exchange
401
- │ │ ├── discovery.js # Discovery protocol endpoints
402
- │ │ ├── noscript.js # NoJS fallback (pixel, CSS, SSR)
403
- │ │ ├── admin.js # Admin dashboard API
404
- │ │ └── billing.js # Stripe billing integration
405
- │ ├── services/
406
- │ │ └── fairness.js # Fairness engine & neutrality layer
407
- │ ├── middleware/
408
- │ │ └── auth.js # JWT authentication middleware
409
- │ ├── models/
410
- │ │ └── db.js # SQLite database & operations
411
- │ ├── migrations/ # Numbered SQL migrations
412
- │ └── utils/
413
- │ ├── cache.js # In-memory cache + analytics queue
414
- │ └── migrate.js # Migration runner
415
- ├── wab-mcp-adapter/ # MCP adapter for WAB → Claude/GPT
416
- │ ├── index.js # WABMCPAdapter class
417
- │ ├── package.json
418
- │ └── README.md
419
- ├── public/ # Frontend
420
- │ ├── index.html # Landing page
421
- │ ├── dashboard.html # Management dashboard
422
- │ ├── docs.html # Documentation
423
- │ ├── admin/ # Admin panel
424
- │ ├── js/ # Client-side utilities
425
- │ └── css/styles.css # Design system
426
- ├── script/
427
- │ └── ai-agent-bridge.js # The bridge script (embed in websites)
428
- ├── examples/ # Agent examples (Puppeteer, BiDi, MCP, Vision)
429
- ├── sdk/ # Agent SDK for Puppeteer/Playwright
430
- ├── .env # Environment variables
431
- └── package.json
432
- ```
433
-
434
- ---
435
-
436
- ## API Endpoints
437
-
438
- ### Authentication
439
- | Endpoint | Method | Description |
440
- |---|---|---|
441
- | `/api/auth/register` | POST | Create account |
442
- | `/api/auth/login` | POST | Sign in, receive JWT |
443
- | `/api/auth/me` | GET | Get current user |
444
-
445
- ### Sites
446
- | Endpoint | Method | Description |
447
- |---|---|---|
448
- | `/api/sites` | GET | List your sites |
449
- | `/api/sites` | POST | Add a new site |
450
- | `/api/sites/:id` | GET | Get site details |
451
- | `/api/sites/:id/config` | PUT | Update configuration |
452
- | `/api/sites/:id/tier` | PUT | Change subscription tier |
453
- | `/api/sites/:id` | DELETE | Delete a site |
454
- | `/api/sites/:id/snippet` | GET | Get install code snippet |
455
- | `/api/sites/:id/analytics` | GET | Get analytics data |
456
-
457
- ### License (Public)
458
- | Endpoint | Method | Description |
459
- |---|---|---|
460
- | `/api/license/verify` | POST | Verify license key for domain (cached) |
461
- | `/api/license/token` | POST | Exchange `siteId` (Origin must match domain) or `licenseKey` for session token |
462
- | `/api/license/session` | POST | Validate session token (domain-locked) |
463
- | `/api/license/track` | POST | Record analytics (`sessionToken` + Origin; legacy `licenseKey` only if `ALLOW_LEGACY_LICENSE_TRACK`) |
464
-
465
- ### Discovery Protocol (Public)
466
- | Endpoint | Method | Description |
467
- |---|---|---|
468
- | `/.well-known/wab.json` | GET | Discovery document for the requesting domain |
469
- | `/agent-bridge.json` | GET | Alternative discovery location |
470
- | `/api/discovery/:siteId` | GET | Discovery document for a specific site |
471
- | `/api/discovery/registry` | GET | Public directory of WAB-enabled sites |
472
- | `/api/discovery/search?q=&category=` | GET | Fairness-weighted site search |
473
- | `/api/discovery/register` | POST | Register site in directory (authenticated) |
474
-
475
- ---
476
-
477
- ## Bridge Script API
478
-
479
- Once loaded, `window.AICommands` exposes:
480
-
481
- | Method | Description |
482
- |---|---|
483
- | `discover()` | Get full discovery document with protocol info and fairness data |
484
- | `ping()` | Health check — returns version, protocol, ready state |
485
- | `getActions(category?)` | List available actions |
486
- | `getAction(name)` | Get a specific action |
487
- | `execute(name, params?)` | Execute an action |
488
- | `readContent(selector)` | Read element content |
489
- | `getPageInfo()` | Get page and bridge metadata |
490
- | `subscribe(event, callback)` | Subscribe to events with subscription ID |
491
- | `unsubscribe(subscriptionId)` | Unsubscribe from events |
492
- | `waitForElement(selector, timeout?)` | Wait for DOM element |
493
- | `waitForNavigation(timeout?)` | Wait for URL change |
494
- | `registerAction(def)` | Register a custom action |
495
- | `authenticate(key, meta?)` | Authenticate an agent |
496
- | `refresh()` | Re-scan the page |
497
- | `onReady(callback)` | Callback when bridge is ready |
498
- | `events.on(event, cb)` | Subscribe to raw events |
499
-
500
- ---
501
-
502
- ## Configuration
503
-
504
- ```javascript
505
- window.AIBridgeConfig = {
506
- // Recommended — copy siteId from dashboard snippet (no license key in HTML)
507
- siteId: "uuid-from-dashboard",
508
- configEndpoint: "/api/license/token",
509
-
510
- // Legacy: token exchange via license key (avoid embedding in public pages)
511
- // licenseKey: "WAB-...",
512
-
513
- agentPermissions: {
514
- readContent: true, // Read page text
515
- click: true, // Click elements
516
- fillForms: false, // Fill/submit forms
517
- scroll: true, // Scroll page
518
- navigate: false, // Navigate pages
519
- apiAccess: false, // Internal API calls (Pro+)
520
- automatedLogin: false, // Auto login (Starter+)
521
- extractData: false // Data extraction (Pro+)
522
- },
523
- restrictions: {
524
- allowedSelectors: [],
525
- blockedSelectors: [".private", "[data-private]"],
526
- requireLoginForActions: ["apiAccess"],
527
- rateLimit: { maxCallsPerMinute: 60 }
528
- },
529
- logging: { enabled: false, level: "basic" }
530
- };
531
- ```
532
-
533
- ---
534
-
535
- ## Subscription Tiers
536
-
537
- | Feature | Free | Starter | Pro | Enterprise |
538
- |---|:---:|:---:|:---:|:---:|
539
- | Auto-discovery | ✓ | ✓ | ✓ | ✓ |
540
- | Click/Scroll | | ✓ | ✓ | ✓ |
541
- | Form filling | | ✓ | ✓ | ✓ |
542
- | Basic logging | | ✓ | ✓ | ✓ |
543
- | Automated login | ✗ | ✓ | ✓ | ✓ |
544
- | Analytics dashboard | | | | ✓ |
545
- | API access | | | ✓ | ✓ |
546
- | Data extraction | ✗ | ✗ | ✓ | ✓ |
547
- | Custom rate limits | ✗ | ✗ | ✗ | ✓ |
548
- | Webhooks | | | ✗ | ✓ |
549
-
550
- ---
551
-
552
- ## Tech Stack
553
-
554
- - **Protocol**: WAB Specification v1.0 with RFC 2119 conformance levels
555
- - **Backend**: Node.js + Express + WebSocket (ws)
556
- - **Database**: SQLite (via better-sqlite3) with migration runner
557
- - **Auth**: JWT + bcrypt + session tokens (domain-locked)
558
- - **MCP**: WAB-to-MCP adapter for Claude, GPT, LangChain integration
559
- - **Fairness**: Neutrality engine with anti-bias ranking and monopoly prevention
560
- - **Discovery**: Auto-generated `agent-bridge.json` + public registry
561
- - **Caching**: In-memory TTL cache + batched analytics queue
562
- - **Payments**: Stripe integration with billing portal
563
- - **Frontend**: Vanilla HTML/CSS/JS (no framework dependencies)
564
- - **Security**: Helmet, CORS, CSP, multi-layer rate limiting, sandbox
565
- - **Containers**: Docker + Docker Compose
566
- - **CI/CD**: GitHub Actions (test + auto-publish to npm)
567
- - **Testing**: Jest + Supertest
568
-
569
- ---
570
-
571
- ## WebDriver BiDi Compatibility
572
-
573
- WAB exposes a `window.__wab_bidi` interface for agents using standardized WebDriver BiDi protocol:
574
-
575
- ```javascript
576
- // Get BiDi context
577
- const context = window.__wab_bidi.getContext();
578
-
579
- // Send BiDi command
580
- const result = await window.__wab_bidi.send({
581
- id: 1,
582
- method: 'wab.executeAction',
583
- params: { name: 'signup', data: {} }
584
- });
585
-
586
- // Supported methods:
587
- // wab.discover, wab.getContext, wab.getActions, wab.executeAction,
588
- // wab.readContent, wab.getPageInfo, wab.authenticate, wab.subscribe, wab.ping
589
- ```
590
-
591
- ---
592
-
593
- ## Real-Time Analytics (WebSocket)
594
-
595
- Connect to `ws://localhost:3000/ws/analytics` for live analytics. Use the built-in `WABWebSocket` client for automatic reconnection with exponential backoff:
596
-
597
- ```javascript
598
- // Recommended: use the auto-reconnecting client
599
- import { WABWebSocket } from './js/ws-client.js';
600
-
601
- const ws = new WABWebSocket('jwt-token', 'site-id');
602
- ws.on('analytic', (data) => console.log(data));
603
- ws.on('reconnecting', ({ attempt, delay }) => console.log(`Reconnecting #${attempt}...`));
604
- ws.connect();
605
- ```
606
-
607
- ```javascript
608
- // Or connect manually
609
- const ws = new WebSocket('ws://localhost:3000/ws/analytics');
610
- ws.onopen = () => ws.send(JSON.stringify({ type: 'auth', token: 'jwt-token', siteId: 'site-id' }));
611
- ws.onmessage = (e) => console.log(JSON.parse(e.data));
612
- ```
613
-
614
- ### WebSocket Message Protocol
615
-
616
- **Client → Server Messages:**
617
-
618
- | Message | Fields | Description |
619
- |---|---|---|
620
- | `auth` | `type`, `token`, `siteId` | Authenticate and subscribe to a site's events |
621
-
622
- ```json
623
- { "type": "auth", "token": "eyJhbGciOi...", "siteId": "uuid-of-site" }
624
- ```
625
-
626
- **Server → Client Messages:**
627
-
628
- | Message Type | Fields | Description |
629
- |---|---|---|
630
- | `auth:success` | `type`, `siteId` | Authentication succeeded |
631
- | `analytic` | `type`, `timestamp`, `actionName`, `agentId`, `success` | Real-time analytics event |
632
- | `error` | `type`, `message` | Error (invalid auth, malformed message) |
633
-
634
- ```json
635
- // Success response
636
- { "type": "auth:success", "siteId": "uuid-of-site" }
637
-
638
- // Analytics event
639
- {
640
- "type": "analytic",
641
- "timestamp": "2024-01-15T10:30:00.000Z",
642
- "actionName": "click-signup",
643
- "agentId": "agent-123",
644
- "triggerType": "click",
645
- "success": true
646
- }
647
-
648
- // Error
649
- { "type": "error", "message": "Invalid message or auth failed" }
650
- ```
651
-
652
- **Connection Lifecycle:**
653
- 1. Connect to `ws://host:port/ws/analytics`
654
- 2. Send `auth` message with valid JWT and site ID
655
- 3. Receive `auth:success` confirmation
656
- 4. Receive `analytic` events as they occur
657
- 5. Server sends heartbeat pings every 30 seconds dead connections are cleaned up automatically
658
-
659
- ---
660
-
661
- ## CDN & Versioning
662
-
663
- Scripts are served at versioned URLs for cache-safe deployments:
664
-
665
- | URL | Description |
666
- |---|---|
667
- | `/script/ai-agent-bridge.js` | Default path |
668
- | `/v1/ai-agent-bridge.js` | Version-pinned (recommended) |
669
- | `/latest/ai-agent-bridge.js` | Always latest (use with caution) |
670
-
671
- ---
672
-
673
- ## Docker
674
-
675
- ```bash
676
- # Quick start
677
- docker compose up -d
678
-
679
- # Or build manually
680
- docker build -t web-agent-bridge .
681
- docker run -p 3000:3000 -e JWT_SECRET=your-secret -e JWT_SECRET_ADMIN=your-admin-secret web-agent-bridge
682
- ```
683
-
684
- ---
685
-
686
- ## Testing
687
-
688
- ```bash
689
- npm test
690
- ```
691
-
692
- Tests cover: authentication, site CRUD, config management, license verification, analytics tracking, and static pages.
693
-
694
- ---
695
-
696
- ## Agent SDK
697
-
698
- WAB includes a built-in SDK for building AI agents. See [`sdk/README.md`](sdk/README.md) for full documentation.
699
-
700
- ```javascript
701
- const puppeteer = require('puppeteer');
702
- const { WABAgent } = require('web-agent-bridge/sdk');
703
-
704
- const browser = await puppeteer.launch();
705
- const page = await browser.newPage();
706
- const agent = new WABAgent(page);
707
-
708
- await agent.navigateAndWait('https://example.com');
709
- const actions = await agent.getActions();
710
- await agent.execute('signup', { email: 'user@test.com' });
711
- await browser.close();
712
- ```
713
-
714
- ---
715
-
716
- ## Agent Examples
717
-
718
- Ready-to-run agent examples in the [`examples/`](examples/) directory:
719
-
720
- | File | Description |
721
- |---|---|
722
- | `puppeteer-agent.js` | Basic agent using Puppeteer + `window.AICommands` |
723
- | `bidi-agent.js` | Agent using WebDriver BiDi protocol via `window.__wab_bidi` |
724
- | `mcp-agent.js` | MCP adapter demo — WAB actions as MCP tools for Claude/GPT |
725
- | `vision-agent.js` | Vision/NLP agent — resolves natural language intents to actions using a local keyword-based resolver (no external API) |
726
-
727
- ```bash
728
- node examples/puppeteer-agent.js http://localhost:3000
729
- node examples/bidi-agent.js http://localhost:3000
730
- node examples/mcp-agent.js http://localhost:3000
731
- node examples/vision-agent.js http://localhost:3000
732
- ```
733
-
734
- ---
735
-
736
- ## Multi-Database Support
737
-
738
- WAB defaults to SQLite but supports PostgreSQL and MySQL via database adapters.
739
-
740
- ```bash
741
- # SQLite (default — no setup needed)
742
- npm start
743
-
744
- # PostgreSQL
745
- npm install pg
746
- DB_ADAPTER=postgresql DATABASE_URL=postgres://user:pass@localhost:5432/wab npm start
747
-
748
- # MySQL
749
- npm install mysql2
750
- DB_ADAPTER=mysql DATABASE_URL=mysql://user:pass@localhost:3306/wab npm start
751
- ```
752
-
753
- ### When to Choose Which Database
754
-
755
- | Scenario | Recommended DB | Why |
756
- |---|---|---|
757
- | Local dev / prototyping | SQLite | Zero setup, single file, instant |
758
- | Small production (< 100 sites) | SQLite | Fast, no external dependencies |
759
- | Medium production (100-10K sites) | PostgreSQL | Better concurrency, JSONB support |
760
- | Large / enterprise production | PostgreSQL | Replication, backups, scalability |
761
- | Existing MySQL infrastructure | MySQL | Integrate with what you already use |
762
-
763
- See [`server/models/adapters/`](server/models/adapters/) for adapter implementations.
764
-
765
- ---
766
-
767
- ## Security Architecture
768
-
769
- WAB implements defense-in-depth to protect the bridge from misuse:
770
-
771
- ### Secure License Exchange
772
-
773
- 1. **Dashboard snippet (recommended):** `siteId` + `configEndpoint`. The browser sends `POST /api/license/token` with `{ siteId }`; the server checks **Origin** against the site’s registered domain and issues a session token.
774
- 2. **Legacy:** `licenseKey` + `configEndpoint` (or deprecated `_licenseKey`) still works for token exchange but should not be embedded in public HTML.
775
- 3. **Session** is domain-locked (1h TTL); **analytics** use `sessionToken` on `POST /api/license/track` (not the license key).
776
- 4. **WebSocket** `/ws/analytics`: user JWT must **own** the `siteId`; admin JWT may observe any site.
777
-
778
- ```
779
- Client Server
780
- │── POST /api/license/token ──→│ { siteId } + Origin header
781
- │ │ domain match → sessionToken
782
- │←── { sessionToken, tier } ──│
783
- │── POST /api/license/track ─→│ { sessionToken, actionName } + Origin
784
- ```
785
-
786
- **Production:** set `JWT_SECRET`, `JWT_SECRET_ADMIN`, `STRIPE_WEBHOOK_SECRET`, `ALLOWED_ORIGINS`, and create the first admin via `BOOTSTRAP_ADMIN_*` or `node scripts/create-admin.js`.
787
-
788
- ### Security Sandbox
789
-
790
- Every bridge instance runs inside a `SecuritySandbox` that provides:
791
-
792
- - **Session tokens** — Unique cryptographic token per session prevents replay attacks
793
- - **Origin validation** — Only whitelisted origins can interact with the bridge
794
- - **Command validation** — All commands are validated for format, length, and blocklist
795
- - **Audit logging** — Every action is logged with timestamp, agent fingerprint, and status
796
- - **Escalation protection** — Attempts to access higher-tier features trigger automatic lockdown after 5 violations
797
- - **Auto-lockdown** — Bridge becomes read-only when security violations are detected
798
-
799
- ```javascript
800
- // Get security status
801
- const info = bridge.getPageInfo();
802
- console.log(info.security);
803
- // { sandboxActive: true, locked: false, sessionToken: "a3f2..." }
804
-
805
- // View audit log
806
- const audit = bridge.security.getAuditLog(20);
807
- ```
808
-
809
- ### Selector Restrictions
810
-
811
- Block sensitive page sections from agent access:
812
-
813
- ```javascript
814
- window.AIBridgeConfig = {
815
- restrictions: {
816
- blockedSelectors: [".private", "[data-private]", "#payment-form"],
817
- allowedSelectors: [".public-content"]
818
- }
819
- };
820
- ```
821
-
822
- ---
823
-
824
- ## Self-Healing Selectors
825
-
826
- Modern SPAs frequently change their DOM structure. WAB's self-healing system ensures selectors keep working even when the page changes:
827
-
828
- ### How It Works
829
-
830
- 1. **Fingerprinting** — When actions are discovered, WAB stores a rich fingerprint of each element (tag, id, classes, text, ARIA attributes, position)
831
- 2. **7-Strategy Resolution** — When a selector breaks, WAB tries these strategies in order:
832
- - `data-wab-id` attribute (most stable — add to your HTML)
833
- - `data-testid` attribute
834
- - Element ID
835
- - `aria-label` (semantic, usually survives redesigns)
836
- - `name` attribute
837
- - Fuzzy text matching (bigram similarity > 70%)
838
- - Role + position heuristic
839
- 3. **SPA Observer** — A `MutationObserver` watches for DOM changes and automatically re-discovers actions with a 500ms debounce
840
-
841
- ```javascript
842
- // Check healing stats
843
- const info = bridge.getPageInfo();
844
- console.log(info.selfHealing);
845
- // { tracked: 12, healed: 3, failed: 0 }
846
-
847
- // Listen for healing events
848
- bridge.events.on('selector:healed', (data) => {
849
- console.log(`Healed: ${data.action} via ${data.strategy}`);
850
- });
851
- ```
852
-
853
- ### Best Practices for Site Owners
854
-
855
- Add `data-wab-id` attributes to critical elements for maximum stability:
856
-
857
- ```html
858
- <button data-wab-id="signup-btn">Sign Up</button>
859
- <form data-wab-id="login-form">...</form>
860
- ```
861
-
862
- ---
863
-
864
- ## Stealth Mode
865
-
866
- For sites with anti-bot protection, WAB can simulate human-like interaction patterns. **Stealth mode requires explicit consent** to ensure ethical use.
867
-
868
- ```javascript
869
- window.AIBridgeConfig = {
870
- stealth: {
871
- enabled: true,
872
- consent: true // Required — confirms site owner authorizes human-like patterns
873
- }
874
- };
875
- ```
876
-
877
- > **⚠️ Ethical Use Policy:** Stealth mode is designed for accessibility and testing on your own websites. Using it to bypass security controls on sites you do not own may violate terms of service and applicable laws.
878
-
879
- When enabled, all interactions use:
880
-
881
- | Feature | Description |
882
- |---|---|
883
- | **Mouse event chain** | `mouseover → mouseenter → mousemove → mousedown → mouseup → click` with natural coordinates |
884
- | **Typing simulation** | Character-by-character input with 30-120ms delays per keystroke |
885
- | **Scroll easing** | Multi-step scrolling with variable speed |
886
- | **Random delays** | 50-400ms natural pauses between actions |
887
-
888
- ```javascript
889
- // Enable/disable at runtime (consent required)
890
- bridge.stealth.enable(true); // true = consent granted
891
- bridge.stealth.disable();
892
- ```
893
-
894
- ---
895
-
896
- ## CLI
897
-
898
- Install globally or use via npx:
899
-
900
- ```bash
901
- # Run the server
902
- npx web-agent-bridge start
903
- npx web-agent-bridge start --port 8080
904
-
905
- # Initialize a new project
906
- npx web-agent-bridge init
907
- ```
908
-
909
- ---
910
-
911
- ## Environment Variables
912
-
913
- See `.env.example`. Important:
914
-
915
- ```
916
- PORT=3000
917
- NODE_ENV=development
918
- JWT_SECRET=long-random-user-signing-secret
919
- JWT_SECRET_ADMIN=long-random-admin-signing-secret # required in production
920
- ALLOWED_ORIGINS=http://localhost:3000,https://your-app.com
921
- STRIPE_WEBHOOK_SECRET=whsec_... # Stripe webhook verify
922
- CREDENTIALS_ENCRYPTION_KEY=... # optional SMTP password encryption
923
- DB_ADAPTER=sqlite
924
- DATABASE_URL=
925
- ```
926
-
927
- First admin: set `BOOTSTRAP_ADMIN_EMAIL` / `BOOTSTRAP_ADMIN_PASSWORD` when the `admins` table is empty, or run `node scripts/create-admin.js <email> <password>`.
928
-
929
- ---
930
-
931
- ## License
932
-
933
- MIT Free to use, modify, and distribute.
1
+ # Web Agent Bridge (WAB)
2
+
3
+ [![npm](https://img.shields.io/npm/v/web-agent-bridge)](https://www.npmjs.com/package/web-agent-bridge)
4
+ [![CI](https://github.com/abokenan444/web-agent-bridge/actions/workflows/ci.yml/badge.svg)](https://github.com/abokenan444/web-agent-bridge/actions/workflows/ci.yml)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D18-brightgreen.svg)](https://nodejs.org/)
7
+ [![Docker](https://img.shields.io/badge/docker-ready-blue.svg)](https://hub.docker.com/)
8
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
9
+
10
+ > **robots.txt told bots what NOT to do. WAB tells AI agents what they CAN do.**
11
+
12
+ **English** | **[العربية](README.ar.md)**
13
+
14
+ WAB is an open-source middleware layer that bridges AI agents and websites — like **OpenAPI for human-facing pages**. Website owners embed a script that exposes a standardized `window.AICommands` interface. AI agents discover available actions, execute commands, and interact with sites accurately — no DOM parsing, no scraping, no guesswork.
15
+
16
+ ### Three Paths to WAB
17
+
18
+ | Path | For | How |
19
+ |---|---|---|
20
+ | **🏢 Website Owner** | Control how AI interacts with your site | Embed the script, configure permissions |
21
+ | **🤖 Agent Developer** | Build reliable agents that work on any WAB-enabled site | Use `window.AICommands` or the Agent SDK |
22
+ | **🔧 Self-Hosting** | Run the full WAB platform for your organization | Clone, deploy, manage licenses & analytics |
23
+ | **WordPress** | Sites powered by WP | Use the **[Web Agent Bridge WordPress plugin](web-agent-bridge-wordpress/README.md)** (settings, shortcode, per-page disable, hooks) |
24
+
25
+ ---
26
+
27
+ ## Features
28
+
29
+ - **Auto-Discovery** Automatically detects buttons, forms, and navigation on the page
30
+ - **Structured Auto-Discovery** — Detects schema.org JSON-LD + microdata products/offers and exposes read actions
31
+ - **Commerce + Booking Intents** — Detects common actions like add-to-cart, checkout, and booking/reservation flows
32
+ - **Permission System** — Granular control over what AI agents can do (click, fill forms, API access, etc.)
33
+ - **Standardized Interface** Unified `window.AICommands` object any agent can consume
34
+ - **Secure License Exchange** — Embed uses public `siteId` + `/api/license/token`; long-lived license keys stay in the owner dashboard, not in HTML
35
+ - **Rate Limiting** Multi-dimensional abuse protection (IP + license key + site)
36
+ - **Analytics Dashboard** — Track how AI agents interact with your site
37
+ - **Real-Time Analytics** WebSocket-based live event streaming with auto-reconnection
38
+ - **In-Memory Caching** TTL-based cache layer reduces DB reads on hot paths
39
+ - **Analytics Queue** Batched writes with transaction support for high-throughput tracking
40
+ - **WebDriver BiDi Compatible** Standard protocol support via `window.__wab_bidi`
41
+ - **CDN Versioning** Serve scripts via versioned URLs (`/v1/ai-agent-bridge.js`, `/latest/ai-agent-bridge.js`)
42
+ - **Docker Ready** — One-command deployment with Docker Compose
43
+ - **DB Migrations** — Numbered SQL migration runner with tracking table
44
+ - **Custom Actions** — Register your own actions with custom handlers
45
+ - **Subscription Tiers** Free core + paid premium features (API access, analytics, automated login)
46
+ - **Event System** — Subscribe to bridge events for monitoring
47
+ - **Security Sandbox** — Origin validation, session tokens, command signing, audit logging, auto-lockdown
48
+ - **Self-Healing Selectors** — Resilient element resolution with fuzzy matching for dynamic SPAs
49
+ - **Stealth Mode** — Human-like interaction patterns (requires explicit consent)
50
+ - **Multi-Database** — SQLite (default), PostgreSQL, MySQL via pluggable adapters
51
+ - **Agent SDK** — Built-in SDK for building AI agents with Puppeteer/Playwright
52
+ - **React Package** — `@web-agent-bridge/react` with `WABProvider`, `useWAB`, `useWABAction`, and `useWABActions`
53
+ - **Vue Package** — `@web-agent-bridge/vue` composables (`useWAB`, `useWABAction`, `useWABActions`) for Vue 3+
54
+ - **Svelte Package** — `@web-agent-bridge/svelte` stores (`createWAB`, `createWABAction`) for Svelte 3+
55
+ - **LangChain Adapter** — `@web-agent-bridge/langchain` wraps WAB actions as LangChain tools for LLM agents
56
+ - **GDPR/CCPA Consent** — Optional `wab-consent.js` banner with `WABConsent.showBanner()` and `hasConsent()` gate
57
+ - **Admin Dashboard** — User management, tier grants, system analytics
58
+ - **Stripe Integration** — Payment processing with customer portal
59
+
60
+ ### v2.0Digital Fortress Features
61
+
62
+ - **Real-time Negotiation Engine** — AI agents negotiate prices directly with WAB-enabled sites using multi-round sessions, 8 condition types, and 4 discount types
63
+ - **Anti-Hallucination Shield** — Cross-verification engine comparing DOM vs vision screenshots, market benchmark validation, temporal consistency checks, and Levenshtein text similarity scoring
64
+ - **Decentralized Reputation System** — Cryptographic trust attestations from the agent network with weighted scoring, trust levels (emerging → verified → exemplary), and global leaderboard
65
+ - **Sovereign Dashboard** — Real-time command center with fairness radar, privacy shield, negotiation logs, verification checks, and AI model switcher
66
+ - **Community Agent Hub** — 11 pre-built YAML agent templates (hotel booking, grocery comparison, artisan marketplace, flight deals, etc.) with CLI runner: `npx wab-agent run template.yaml`
67
+ - **AI Brain Swapping** — Switch between Llama 3, GPT-4, Claude, Gemini, Mistral, or Ollama (local) without reconfiguration
68
+
69
+ ---
70
+
71
+ ## Quick Start
72
+
73
+ ### 1. Install & Run the Server
74
+
75
+ ```bash
76
+ # Option A: Clone and run
77
+ git clone https://github.com/abokenan444/web-agent-bridge.git
78
+ cd web-agent-bridge
79
+ npm install
80
+ cp .env.example .env
81
+ npm start
82
+
83
+ # Option B: npx (one command)
84
+ npx web-agent-bridge start
85
+
86
+ # Option C: Docker
87
+ docker compose up -d
88
+ ```
89
+
90
+ ### 2. Create an Account
91
+
92
+ Visit `http://localhost:3000/register` and create an account, then add your site from the dashboard.
93
+
94
+ ### 3. Add the Script to Your Website
95
+
96
+ ```html
97
+ <!-- Recommended: copy the snippet from your dashboard (uses siteId only) -->
98
+ <script>
99
+ window.AIBridgeConfig = {
100
+ siteId: "your-site-uuid-from-dashboard",
101
+ configEndpoint: "https://yourserver.com/api/license/token",
102
+ agentPermissions: {
103
+ readContent: true,
104
+ click: true,
105
+ fillForms: true,
106
+ scroll: true
107
+ }
108
+ };
109
+ </script>
110
+ <script src="https://yourserver.com/script/ai-agent-bridge.js"></script>
111
+ ```
112
+
113
+ The server matches **Origin** to your registered site domain, then returns a short-lived **session token**. Analytics (`/api/license/track`) require that session — not the long-lived license key. Keep the license key in the dashboard only.
114
+
115
+ ### 4. AI Agents Can Now Interact
116
+
117
+ ```javascript
118
+ // From the AI agent's side
119
+ const bridge = window.AICommands;
120
+ const actions = bridge.getActions(); // discover actions
121
+ await bridge.execute("signup"); // execute an action
122
+ const info = bridge.getPageInfo(); // get page metadata
123
+ ```
124
+
125
+ ---
126
+
127
+ ## Project Structure
128
+
129
+ ```
130
+ web-agent-bridge/
131
+ ├── server/ # Express.js backend
132
+ │ ├── index.js # Server entry point
133
+ │ ├── routes/
134
+ │ │ ├── auth.js # Authentication (register/login)
135
+ │ │ ├── api.js # Sites, config, analytics API
136
+ │ │ ├── license.js # License verification, token exchange & tracking
137
+ │ │ ├── admin.js # Admin dashboard API
138
+ │ │ ├── billing.js # Stripe billing integration
139
+ │ │ └── sovereign.js # v2.0: negotiation, reputation, verification
140
+ │ ├── services/
141
+ │ │ ├── negotiation.js # Real-time negotiation engine
142
+ │ │ ├── verification.js # Anti-hallucination shield
143
+ │ │ └── reputation.js # Decentralized reputation system
144
+ │ ├── middleware/
145
+ │ │ └── auth.js # JWT authentication middleware
146
+ │ ├── models/
147
+ │ │ └── db.js # SQLite database & operations
148
+ │ ├── migrations/ # Numbered SQL migrations
149
+ │ └── utils/
150
+ │ ├── cache.js # In-memory cache + analytics queue
151
+ │ └── migrate.js # Migration runner
152
+ ├── public/ # Frontend
153
+ │ ├── index.html # Landing page
154
+ │ ├── dashboard.html # Management dashboard
155
+ │ ├── docs.html # Documentation
156
+ │ ├── login.html # Sign in
157
+ │ ├── register.html # Sign up
158
+ │ ├── admin/ # Admin panel
159
+ │ ├── js/
160
+ │ │ └── ws-client.js # WebSocket client with auto-reconnect
161
+ │ └── css/styles.css # Design system
162
+ ├── script/
163
+ │ └── ai-agent-bridge.js # The bridge script (embed in websites)
164
+ ├── examples/ # Agent examples (Puppeteer, BiDi, Vision)
165
+ ├── packages/ # Framework wrappers
166
+ │ ├── react/ # @web-agent-bridge/react
167
+ │ ├── vue/ # @web-agent-bridge/vue
168
+ │ ├── svelte/ # @web-agent-bridge/svelte
169
+ │ └── langchain/ # @web-agent-bridge/langchain
170
+ ├── sdk/ # Agent SDK for Puppeteer/Playwright
171
+ ├── bin/
172
+ │ ├── cli.js # CLI entry point (wab-agent)
173
+ │ └── agent-runner.js # YAML template runner
174
+ ├── templates/ # Community Agent Hub YAML templates
175
+ ├── .env # Environment variables
176
+ └── package.json
177
+ ```
178
+
179
+ ---
180
+
181
+ ## API Endpoints
182
+
183
+ ### Authentication
184
+ | Endpoint | Method | Description |
185
+ |---|---|---|
186
+ | `/api/auth/register` | POST | Create account |
187
+ | `/api/auth/login` | POST | Sign in, receive JWT |
188
+ | `/api/auth/me` | GET | Get current user |
189
+
190
+ ### Sites
191
+ | Endpoint | Method | Description |
192
+ |---|---|---|
193
+ | `/api/sites` | GET | List your sites |
194
+ | `/api/sites` | POST | Add a new site |
195
+ | `/api/sites/:id` | GET | Get site details |
196
+ | `/api/sites/:id/config` | PUT | Update configuration |
197
+ | `/api/sites/:id/tier` | PUT | Change subscription tier |
198
+ | `/api/sites/:id` | DELETE | Delete a site |
199
+ | `/api/sites/:id/snippet` | GET | Get install code snippet |
200
+ | `/api/sites/:id/analytics` | GET | Get analytics data |
201
+
202
+ ### License (Public)
203
+ | Endpoint | Method | Description |
204
+ |---|---|---|
205
+ | `/api/license/verify` | POST | Verify license key for domain (cached) |
206
+ | `/api/license/token` | POST | Exchange `siteId` (Origin must match domain) or `licenseKey` for session token |
207
+ | `/api/license/session` | POST | Validate session token (domain-locked) |
208
+ | `/api/license/track` | POST | Record analytics (`sessionToken` + Origin; legacy `licenseKey` only if `ALLOW_LEGACY_LICENSE_TRACK`) |
209
+
210
+ ### Sovereign (v2.0)
211
+ | Endpoint | Method | Description |
212
+ |---|---|---|
213
+ | `/api/sovereign/reputation/agents` | POST | Register a new agent |
214
+ | `/api/sovereign/reputation/attestations` | POST | Submit a trust attestation |
215
+ | `/api/sovereign/reputation/sites/:siteId` | GET | Get site reputation |
216
+ | `/api/sovereign/reputation/leaderboard` | GET | Get reputation leaderboard |
217
+ | `/api/sovereign/negotiation/rules` | POST | Create negotiation rule |
218
+ | `/api/sovereign/negotiation/rules/:siteId` | GET | Get rules for a site |
219
+ | `/api/sovereign/negotiation/sessions` | POST | Open negotiation session |
220
+ | `/api/sovereign/negotiation/sessions/:id/propose` | POST | Submit counter-offer |
221
+ | `/api/sovereign/negotiation/sessions/:id/confirm` | POST | Confirm a deal |
222
+ | `/api/sovereign/verify/price` | POST | Verify price (DOM vs vision) |
223
+ | `/api/sovereign/verify/text` | POST | Verify text accuracy |
224
+ | `/api/sovereign/verify/page` | POST | Full page verification |
225
+ | `/api/sovereign/dashboard/sovereign` | GET | Dashboard aggregate data |
226
+
227
+ ---
228
+
229
+ ## Bridge Script API
230
+
231
+ Once loaded, `window.AICommands` exposes:
232
+
233
+ | Method | Description |
234
+ |---|---|
235
+ | `getActions(category?)` | List available actions |
236
+ | `getAction(name)` | Get a specific action |
237
+ | `execute(name, params?)` | Execute an action |
238
+ | `readContent(selector)` | Read element content |
239
+ | `getPageInfo()` | Get page and bridge metadata |
240
+ | `waitForElement(selector, timeout?)` | Wait for DOM element |
241
+ | `waitForNavigation(timeout?)` | Wait for URL change |
242
+ | `registerAction(def)` | Register a custom action |
243
+ | `authenticate(key, meta?)` | Authenticate an agent |
244
+ | `refresh()` | Re-scan the page |
245
+ | `onReady(callback)` | Callback when bridge is ready |
246
+ | `events.on(event, cb)` | Subscribe to events |
247
+
248
+ ---
249
+
250
+ ## Configuration
251
+
252
+ ```javascript
253
+ window.AIBridgeConfig = {
254
+ // Recommended — copy siteId from dashboard snippet (no license key in HTML)
255
+ siteId: "uuid-from-dashboard",
256
+ configEndpoint: "/api/license/token",
257
+
258
+ // Legacy: token exchange via license key (avoid embedding in public pages)
259
+ // licenseKey: "WAB-...",
260
+
261
+ agentPermissions: {
262
+ readContent: true, // Read page text
263
+ click: true, // Click elements
264
+ fillForms: false, // Fill/submit forms
265
+ scroll: true, // Scroll page
266
+ navigate: false, // Navigate pages
267
+ apiAccess: false, // Internal API calls (Pro+)
268
+ automatedLogin: false, // Auto login (Starter+)
269
+ extractData: false // Data extraction (Pro+)
270
+ },
271
+ restrictions: {
272
+ allowedSelectors: [],
273
+ blockedSelectors: [".private", "[data-private]"],
274
+ requireLoginForActions: ["apiAccess"],
275
+ rateLimit: { maxCallsPerMinute: 60 }
276
+ },
277
+ logging: { enabled: false, level: "basic" }
278
+ };
279
+ ```
280
+
281
+ ---
282
+
283
+ ## Subscription Tiers
284
+
285
+ | Feature | Free | Starter | Pro | Enterprise |
286
+ |---|:---:|:---:|:---:|:---:|
287
+ | Auto-discovery | ✓ | ✓ | ✓ | ✓ |
288
+ | Click/Scroll | ✓ | ✓ | ✓ | ✓ |
289
+ | Form filling | ✓ | ✓ | ✓ | ✓ |
290
+ | Basic logging | ✓ | ✓ | ✓ | ✓ |
291
+ | Automated login | ✗ | ✓ | ✓ | ✓ |
292
+ | Analytics dashboard | ✗ | ✓ | ✓ | ✓ |
293
+ | API access | ✗ | ✗ | ✓ | ✓ |
294
+ | Data extraction | ✗ | ✗ | ✓ | ✓ |
295
+ | Custom rate limits | ✗ | ✗ | ✗ | ✓ |
296
+ | Webhooks | ✗ | ✗ | ✗ | ✓ |
297
+
298
+ ---
299
+
300
+ ## Tech Stack
301
+
302
+ - **Backend**: Node.js + Express + WebSocket (ws)
303
+ - **Database**: SQLite (via better-sqlite3) with migration runner
304
+ - **Auth**: JWT + bcrypt + session tokens (domain-locked)
305
+ - **Caching**: In-memory TTL cache + batched analytics queue
306
+ - **Payments**: Stripe integration with billing portal
307
+ - **Frontend**: Vanilla HTML/CSS/JS (no framework dependencies)
308
+ - **Framework Wrappers**: React, Vue 3, Svelte (optional)
309
+ - **LLM Integration**: LangChain adapter, MCP adapter
310
+ - **Security**: Helmet, CORS, CSP, multi-layer rate limiting
311
+ - **Containers**: Docker + Docker Compose
312
+ - **CI/CD**: GitHub Actions (test + auto-publish to npm)
313
+ - **Testing**: Jest + Supertest
314
+
315
+ ---
316
+
317
+ ## WebDriver BiDi Compatibility
318
+
319
+ WAB exposes a `window.__wab_bidi` interface for agents using standardized WebDriver BiDi protocol:
320
+
321
+ ```javascript
322
+ // Get BiDi context
323
+ const context = window.__wab_bidi.getContext();
324
+
325
+ // Send BiDi command
326
+ const result = await window.__wab_bidi.send({
327
+ id: 1,
328
+ method: 'wab.executeAction',
329
+ params: { name: 'signup', data: {} }
330
+ });
331
+
332
+ // Supported methods:
333
+ // wab.getContext, wab.getActions, wab.executeAction, wab.readContent, wab.getPageInfo
334
+ ```
335
+
336
+ ---
337
+
338
+ ## Real-Time Analytics (WebSocket)
339
+
340
+ Connect to `ws://localhost:3000/ws/analytics` for live analytics. Use the built-in `WABWebSocket` client for automatic reconnection with exponential backoff:
341
+
342
+ ```javascript
343
+ // Recommended: use the auto-reconnecting client
344
+ import { WABWebSocket } from './js/ws-client.js';
345
+
346
+ const ws = new WABWebSocket('jwt-token', 'site-id');
347
+ ws.on('analytic', (data) => console.log(data));
348
+ ws.on('reconnecting', ({ attempt, delay }) => console.log(`Reconnecting #${attempt}...`));
349
+ ws.connect();
350
+ ```
351
+
352
+ ```javascript
353
+ // Or connect manually
354
+ const ws = new WebSocket('ws://localhost:3000/ws/analytics');
355
+ ws.onopen = () => ws.send(JSON.stringify({ type: 'auth', token: 'jwt-token', siteId: 'site-id' }));
356
+ ws.onmessage = (e) => console.log(JSON.parse(e.data));
357
+ ```
358
+
359
+ ### WebSocket Message Protocol
360
+
361
+ **Client Server Messages:**
362
+
363
+ | Message | Fields | Description |
364
+ |---|---|---|
365
+ | `auth` | `type`, `token`, `siteId` | Authenticate and subscribe to a site's events |
366
+
367
+ ```json
368
+ { "type": "auth", "token": "eyJhbGciOi...", "siteId": "uuid-of-site" }
369
+ ```
370
+
371
+ **Server Client Messages:**
372
+
373
+ | Message Type | Fields | Description |
374
+ |---|---|---|
375
+ | `auth:success` | `type`, `siteId` | Authentication succeeded |
376
+ | `analytic` | `type`, `timestamp`, `actionName`, `agentId`, `success` | Real-time analytics event |
377
+ | `error` | `type`, `message` | Error (invalid auth, malformed message) |
378
+
379
+ ```json
380
+ // Success response
381
+ { "type": "auth:success", "siteId": "uuid-of-site" }
382
+
383
+ // Analytics event
384
+ {
385
+ "type": "analytic",
386
+ "timestamp": "2024-01-15T10:30:00.000Z",
387
+ "actionName": "click-signup",
388
+ "agentId": "agent-123",
389
+ "triggerType": "click",
390
+ "success": true
391
+ }
392
+
393
+ // Error
394
+ { "type": "error", "message": "Invalid message or auth failed" }
395
+ ```
396
+
397
+ **Connection Lifecycle:**
398
+ 1. Connect to `ws://host:port/ws/analytics`
399
+ 2. Send `auth` message with valid JWT and site ID
400
+ 3. Receive `auth:success` confirmation
401
+ 4. Receive `analytic` events as they occur
402
+ 5. Server sends heartbeat pings every 30 seconds — dead connections are cleaned up automatically
403
+
404
+ ---
405
+
406
+ ## CDN & Versioning
407
+
408
+ Scripts are served at versioned URLs for cache-safe deployments:
409
+
410
+ | URL | Description |
411
+ |---|---|
412
+ | `/script/ai-agent-bridge.js` | Default path |
413
+ | `/v1/ai-agent-bridge.js` | Version-pinned (recommended) |
414
+ | `/latest/ai-agent-bridge.js` | Always latest (use with caution) |
415
+
416
+ ---
417
+
418
+ ## Docker
419
+
420
+ ```bash
421
+ # Quick start
422
+ docker compose up -d
423
+
424
+ # Or build manually
425
+ docker build -t web-agent-bridge .
426
+ docker run -p 3000:3000 -e JWT_SECRET=your-secret -e JWT_SECRET_ADMIN=your-admin-secret web-agent-bridge
427
+ ```
428
+
429
+ ---
430
+
431
+ ## Testing
432
+
433
+ ```bash
434
+ npm test
435
+ ```
436
+
437
+ Tests cover: authentication, site CRUD, config management, license verification, analytics tracking, and static pages.
438
+
439
+ ---
440
+
441
+ ## Agent SDK
442
+
443
+ WAB includes a built-in SDK for building AI agents. See [`sdk/README.md`](sdk/README.md) for full documentation.
444
+
445
+ ```javascript
446
+ const puppeteer = require('puppeteer');
447
+ const { WABAgent } = require('web-agent-bridge/sdk');
448
+
449
+ const browser = await puppeteer.launch();
450
+ const page = await browser.newPage();
451
+ const agent = new WABAgent(page);
452
+
453
+ await agent.navigateAndWait('https://example.com');
454
+ const actions = await agent.getActions();
455
+ await agent.execute('signup', { email: 'user@test.com' });
456
+ await browser.close();
457
+ ```
458
+
459
+ ### SDK Extras
460
+
461
+ The SDK now includes additional helpers for advanced agent workflows:
462
+
463
+ ```javascript
464
+ // Wait for GDPR consent before proceeding
465
+ await agent.waitForConsent();
466
+
467
+ // Discover all actions + page meta
468
+ const disc = await agent.discover();
469
+ console.log(disc.actions, disc.meta);
470
+
471
+ // Run a sequence of actions (stops on first failure by default)
472
+ const results = await agent.runPipeline([
473
+ { name: 'login', params: { email: 'a@b.com', pass: 'secret' } },
474
+ { name: 'addToCart', params: { sku: 'ABC123' } },
475
+ { name: 'checkout' }
476
+ ]);
477
+
478
+ // Run actions in parallel
479
+ const parallel = await agent.executeParallel([
480
+ { name: 'getCartCount' },
481
+ { name: 'getWishlistCount' }
482
+ ]);
483
+
484
+ // Capture screenshot (base64) for vision agents
485
+ const b64 = await agent.screenshot({ fullPage: true });
486
+ ```
487
+
488
+ ---
489
+
490
+ ## Framework Packages
491
+
492
+ ### Vue 3
493
+
494
+ ```bash
495
+ npm install @web-agent-bridge/vue
496
+ ```
497
+
498
+ ```javascript
499
+ import { useWAB, useWABAction } from '@web-agent-bridge/vue';
500
+
501
+ // In setup()
502
+ const { ready, execute } = useWAB({ siteUrl: 'https://example.com' });
503
+ const cart = useWABAction('addToCart');
504
+
505
+ // In template handler
506
+ await cart.run({ sku: 'ABC123' });
507
+ console.log(cart.result.value);
508
+ ```
509
+
510
+ ### Svelte
511
+
512
+ ```bash
513
+ npm install @web-agent-bridge/svelte
514
+ ```
515
+
516
+ ```svelte
517
+ <script>
518
+ import { createWAB, createWABAction } from '@web-agent-bridge/svelte';
519
+
520
+ const wab = createWAB();
521
+ const cart = createWABAction('addToCart');
522
+
523
+ async function add() {
524
+ await cart.run({ sku: 'ABC123' });
525
+ }
526
+ </script>
527
+
528
+ {#if $cart.loading}Adding...{/if}
529
+ {#if $cart.result}Added!{/if}
530
+ <button on:click={add}>Add to Cart</button>
531
+ ```
532
+
533
+ ### LangChain / LangGraph
534
+
535
+ ```bash
536
+ npm install @web-agent-bridge/langchain
537
+ ```
538
+
539
+ ```javascript
540
+ const { WABToolkit } = require('@web-agent-bridge/langchain');
541
+ const { ChatOpenAI } = require('@langchain/openai');
542
+ const { AgentExecutor, createOpenAIToolsAgent } = require('langchain/agents');
543
+
544
+ // HTTP mode discover + execute via the WAB server
545
+ const toolkit = new WABToolkit({ siteUrl: 'https://shop.example.com' });
546
+ const tools = await toolkit.getTools();
547
+
548
+ // Browser mode use with Puppeteer/Playwright
549
+ const { WABAgent } = require('web-agent-bridge/sdk');
550
+ const toolkit2 = new WABToolkit({ agent: new WABAgent(page) });
551
+ const tools2 = await toolkit2.getTools();
552
+
553
+ // Pass tools to any LangChain agent
554
+ const llm = new ChatOpenAI({ model: 'gpt-4o' });
555
+ const agent = await createOpenAIToolsAgent({ llm, tools, prompt });
556
+ const executor = new AgentExecutor({ agent, tools });
557
+ await executor.invoke({ input: 'Add the first product to my cart' });
558
+ ```
559
+
560
+ ---
561
+
562
+ ## GDPR / CCPA Consent
563
+
564
+ Load the consent script after `wab.min.js` to gate agent actions behind user consent:
565
+
566
+ ```html
567
+ <script src="/script/wab.min.js"></script>
568
+ <script src="/script/wab-consent.js"></script>
569
+ <script>
570
+ WABConsent.showBanner({
571
+ policyUrl: '/privacy',
572
+ message: 'Allow AI agents to interact with this page?',
573
+ onAccept: () => WAB.init({ siteUrl: location.origin }),
574
+ onDecline: () => console.log('Agent access declined')
575
+ });
576
+ </script>
577
+ ```
578
+
579
+ SDK agents can check consent programmatically:
580
+
581
+ ```javascript
582
+ const agent = new WABAgent(page);
583
+ const ok = await agent.hasConsent(); // true | false
584
+ await agent.waitForConsent(); // blocks until Allow is clicked
585
+ ```
586
+
587
+ ---
588
+
589
+ ## Agent Examples
590
+
591
+ Ready-to-run agent examples in the [`examples/`](examples/) directory:
592
+
593
+ | File | Description |
594
+ |---|---|
595
+ | `puppeteer-agent.js` | Basic agent using Puppeteer + `window.AICommands` |
596
+ | `bidi-agent.js` | Agent using WebDriver BiDi protocol via `window.__wab_bidi` |
597
+ | `vision-agent.js` | Vision/NLP agent — resolves natural language intents to actions using a local keyword-based resolver (no external API) |
598
+
599
+ ## Framework + CMS Examples
600
+
601
+ Additional integration examples are available in:
602
+
603
+ | Path | Description |
604
+ |---|---|
605
+ | `examples/next-app-router/` | Next.js App Router integration with `@web-agent-bridge/react` |
606
+ | `examples/shopify-hydrogen/` | Hydrogen storefront integration with practical cart actions |
607
+ | `examples/wordpress-elementor/` | WordPress + Elementor setup with schema-assisted actions |
608
+ | `examples/saas-dashboard/` | Notion-style SaaS dashboard actions for KPI read + workflow triggers |
609
+
610
+ ```bash
611
+ node examples/puppeteer-agent.js http://localhost:3000
612
+ node examples/bidi-agent.js http://localhost:3000
613
+ node examples/vision-agent.js http://localhost:3000
614
+ ```
615
+
616
+ ---
617
+
618
+ ## Multi-Database Support
619
+
620
+ WAB defaults to SQLite but supports PostgreSQL and MySQL via database adapters.
621
+
622
+ ```bash
623
+ # SQLite (default no setup needed)
624
+ npm start
625
+
626
+ # PostgreSQL
627
+ npm install pg
628
+ DB_ADAPTER=postgresql DATABASE_URL=postgres://user:pass@localhost:5432/wab npm start
629
+
630
+ # MySQL
631
+ npm install mysql2
632
+ DB_ADAPTER=mysql DATABASE_URL=mysql://user:pass@localhost:3306/wab npm start
633
+ ```
634
+
635
+ ### When to Choose Which Database
636
+
637
+ | Scenario | Recommended DB | Why |
638
+ |---|---|---|
639
+ | Local dev / prototyping | SQLite | Zero setup, single file, instant |
640
+ | Small production (< 100 sites) | SQLite | Fast, no external dependencies |
641
+ | Medium production (100-10K sites) | PostgreSQL | Better concurrency, JSONB support |
642
+ | Large / enterprise production | PostgreSQL | Replication, backups, scalability |
643
+ | Existing MySQL infrastructure | MySQL | Integrate with what you already use |
644
+
645
+ See [`server/models/adapters/`](server/models/adapters/) for adapter implementations.
646
+
647
+ ---
648
+
649
+ ## Security Architecture
650
+
651
+ WAB implements defense-in-depth to protect the bridge from misuse:
652
+
653
+ ### Secure License Exchange
654
+
655
+ 1. **Dashboard snippet (recommended):** `siteId` + `configEndpoint`. The browser sends `POST /api/license/token` with `{ siteId }`; the server checks **Origin** against the site’s registered domain and issues a session token.
656
+ 2. **Legacy:** `licenseKey` + `configEndpoint` (or deprecated `_licenseKey`) still works for token exchange but should not be embedded in public HTML.
657
+ 3. **Session** is domain-locked (1h TTL); **analytics** use `sessionToken` on `POST /api/license/track` (not the license key).
658
+ 4. **WebSocket** `/ws/analytics`: user JWT must **own** the `siteId`; admin JWT may observe any site.
659
+
660
+ ```
661
+ Client Server
662
+ │── POST /api/license/token ──→│ { siteId } + Origin header
663
+ │ │ domain match sessionToken
664
+ │←── { sessionToken, tier } ──│
665
+ │── POST /api/license/track ─→│ { sessionToken, actionName } + Origin
666
+ ```
667
+
668
+ **Production:** set `JWT_SECRET`, `JWT_SECRET_ADMIN`, `STRIPE_WEBHOOK_SECRET`, `ALLOWED_ORIGINS`, and create the first admin via `BOOTSTRAP_ADMIN_*` or `node scripts/create-admin.js`.
669
+
670
+ ### Security Sandbox
671
+
672
+ Every bridge instance runs inside a `SecuritySandbox` that provides:
673
+
674
+ - **Session tokens** — Unique cryptographic token per session prevents replay attacks
675
+ - **Origin validation** — Only whitelisted origins can interact with the bridge
676
+ - **Command validation** — All commands are validated for format, length, and blocklist
677
+ - **Audit logging** — Every action is logged with timestamp, agent fingerprint, and status
678
+ - **Escalation protection** — Attempts to access higher-tier features trigger automatic lockdown after 5 violations
679
+ - **Auto-lockdown** Bridge becomes read-only when security violations are detected
680
+
681
+ ```javascript
682
+ // Get security status
683
+ const info = bridge.getPageInfo();
684
+ console.log(info.security);
685
+ // { sandboxActive: true, locked: false, sessionToken: "a3f2..." }
686
+
687
+ // View audit log
688
+ const audit = bridge.security.getAuditLog(20);
689
+ ```
690
+
691
+ ### Selector Restrictions
692
+
693
+ Block sensitive page sections from agent access:
694
+
695
+ ```javascript
696
+ window.AIBridgeConfig = {
697
+ restrictions: {
698
+ blockedSelectors: [".private", "[data-private]", "#payment-form"],
699
+ allowedSelectors: [".public-content"]
700
+ }
701
+ };
702
+ ```
703
+
704
+ ---
705
+
706
+ ## Self-Healing Selectors
707
+
708
+ Modern SPAs frequently change their DOM structure. WAB's self-healing system ensures selectors keep working even when the page changes:
709
+
710
+ ### How It Works
711
+
712
+ 1. **Fingerprinting** — When actions are discovered, WAB stores a rich fingerprint of each element (tag, id, classes, text, ARIA attributes, position)
713
+ 2. **7-Strategy Resolution** — When a selector breaks, WAB tries these strategies in order:
714
+ - `data-wab-id` attribute (most stable — add to your HTML)
715
+ - `data-testid` attribute
716
+ - Element ID
717
+ - `aria-label` (semantic, usually survives redesigns)
718
+ - `name` attribute
719
+ - Fuzzy text matching (bigram similarity > 70%)
720
+ - Role + position heuristic
721
+ 3. **SPA Observer** — A `MutationObserver` watches for DOM changes and automatically re-discovers actions with a 500ms debounce
722
+
723
+ ```javascript
724
+ // Check healing stats
725
+ const info = bridge.getPageInfo();
726
+ console.log(info.selfHealing);
727
+ // { tracked: 12, healed: 3, failed: 0 }
728
+
729
+ // Listen for healing events
730
+ bridge.events.on('selector:healed', (data) => {
731
+ console.log(`Healed: ${data.action} via ${data.strategy}`);
732
+ });
733
+ ```
734
+
735
+ ### Best Practices for Site Owners
736
+
737
+ Add `data-wab-id` attributes to critical elements for maximum stability:
738
+
739
+ ```html
740
+ <button data-wab-id="signup-btn">Sign Up</button>
741
+ <form data-wab-id="login-form">...</form>
742
+ ```
743
+
744
+ ---
745
+
746
+ ## Stealth Mode
747
+
748
+ For sites with anti-bot protection, WAB can simulate human-like interaction patterns. **Stealth mode requires explicit consent** to ensure ethical use.
749
+
750
+ ```javascript
751
+ window.AIBridgeConfig = {
752
+ stealth: {
753
+ enabled: true,
754
+ consent: true // Required — confirms site owner authorizes human-like patterns
755
+ }
756
+ };
757
+ ```
758
+
759
+ > **⚠️ Ethical Use Policy:** Stealth mode is designed for accessibility and testing on your own websites. Using it to bypass security controls on sites you do not own may violate terms of service and applicable laws.
760
+
761
+ When enabled, all interactions use:
762
+
763
+ | Feature | Description |
764
+ |---|---|
765
+ | **Mouse event chain** | `mouseover → mouseenter → mousemove → mousedown → mouseup → click` with natural coordinates |
766
+ | **Typing simulation** | Character-by-character input with 30-120ms delays per keystroke |
767
+ | **Scroll easing** | Multi-step scrolling with variable speed |
768
+ | **Random delays** | 50-400ms natural pauses between actions |
769
+
770
+ ```javascript
771
+ // Enable/disable at runtime (consent required)
772
+ bridge.stealth.enable(true); // true = consent granted
773
+ bridge.stealth.disable();
774
+ ```
775
+
776
+ ---
777
+
778
+ ## CLI
779
+
780
+ Install globally or use via npx:
781
+
782
+ ```bash
783
+ # Run the server
784
+ npx web-agent-bridge start
785
+ npx web-agent-bridge start --port 8080
786
+
787
+ # Initialize a new project
788
+ npx web-agent-bridge init
789
+ ```
790
+
791
+ ---
792
+
793
+ ## Environment Variables
794
+
795
+ See `.env.example`. Important:
796
+
797
+ ```
798
+ PORT=3000
799
+ NODE_ENV=development
800
+ JWT_SECRET=long-random-user-signing-secret
801
+ JWT_SECRET_ADMIN=long-random-admin-signing-secret # required in production
802
+ ALLOWED_ORIGINS=http://localhost:3000,https://your-app.com
803
+ STRIPE_WEBHOOK_SECRET=whsec_... # Stripe webhook verify
804
+ CREDENTIALS_ENCRYPTION_KEY=... # optional SMTP password encryption
805
+ DB_ADAPTER=sqlite
806
+ DATABASE_URL=
807
+ ```
808
+
809
+ First admin: set `BOOTSTRAP_ADMIN_EMAIL` / `BOOTSTRAP_ADMIN_PASSWORD` when the `admins` table is empty, or run `node scripts/create-admin.js <email> <password>`.
810
+
811
+ ---
812
+
813
+ ## Real-time Negotiation Engine
814
+
815
+ Site owners define negotiation rules. AI agents negotiate prices in multi-round sessions:
816
+
817
+ ```javascript
818
+ // Agent opens a negotiation session
819
+ const session = await fetch('/api/sovereign/negotiation/sessions', {
820
+ method: 'POST',
821
+ headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
822
+ body: JSON.stringify({
823
+ siteId: 'site-uuid',
824
+ agentId: 'agent-id',
825
+ originalPrice: 49.99,
826
+ itemId: 'product-123',
827
+ itemName: 'Olive Oil 1L'
828
+ })
829
+ }).then(r => r.json());
830
+
831
+ // Agent makes a counter-offer
832
+ const counter = await fetch(`/api/sovereign/negotiation/sessions/${session.sessionId}/propose`, {
833
+ method: 'POST',
834
+ headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
835
+ body: JSON.stringify({
836
+ agentId: 'agent-id',
837
+ proposedPrice: 39.99
838
+ })
839
+ }).then(r => r.json());
840
+ // → { status: 'accepted', finalPrice: 42.49, message: 'Deal! ...' }
841
+ ```
842
+
843
+ ### Condition Types
844
+ | Condition | Description |
845
+ |---|---|
846
+ | `bulk_quantity` | Discounts based on order quantity |
847
+ | `loyalty` | Rewards for repeat customers |
848
+ | `time_based` | Happy hour / flash sale windows |
849
+ | `first_purchase` | Welcome discount for new buyers |
850
+ | `cart_value` | Minimum cart value threshold |
851
+ | `seasonal` | Date-range seasonal promotions |
852
+ | `membership` | Member-only pricing |
853
+ | `referral` | Referral-based discounts |
854
+
855
+ ---
856
+
857
+ ## Anti-Hallucination Shield
858
+
859
+ Cross-verification engine that catches AI hallucinations before they reach users:
860
+
861
+ ```javascript
862
+ // Verify a price
863
+ const result = await fetch('/api/sovereign/verify/price', {
864
+ method: 'POST',
865
+ headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
866
+ body: JSON.stringify({
867
+ siteId: 'site-uuid',
868
+ domValue: 29.99,
869
+ visionValue: 29.99,
870
+ category: 'electronics',
871
+ itemName: 'USB Cable'
872
+ })
873
+ }).then(r => r.json());
874
+ // → { verified: true, confidence: 0.98, severity: 'none', layers: { dom_vision: { match: true }, ... } }
875
+
876
+ // Verify text content
877
+ const textResult = await fetch('/api/sovereign/verify/text', {
878
+ method: 'POST',
879
+ headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
880
+ body: JSON.stringify({
881
+ siteId: 'site-uuid',
882
+ source: 'dom',
883
+ value: 'Free shipping on orders over $50',
884
+ expected: 'Free shipping on orders over $50'
885
+ })
886
+ }).then(r => r.json());
887
+ // → { verified: true, similarity: 1.0 }
888
+ ```
889
+
890
+ ### Verification Layers
891
+ 1. **DOM vs Vision** — Compares DOM-extracted price with screenshot OCR value
892
+ 2. **Market Benchmark** — Validates against historical price benchmarks for the category
893
+ 3. **Temporal Consistency** — Checks if price changed suspiciously since last verification
894
+ 4. **Composite Score** — Weighted combination of all layers with severity classification
895
+
896
+ ---
897
+
898
+ ## Community Agent Hub
899
+
900
+ Pre-built YAML agent templates for common use cases. Run any template from the CLI:
901
+
902
+ ```bash
903
+ # List available templates
904
+ npx wab-agent templates
905
+
906
+ # Run a template
907
+ npx wab-agent run olive-oil-tunisia --budget 50 --region tunis
908
+
909
+ # Run with custom server
910
+ npx wab-agent run hotel-direct-booking --server https://yourserver.com --checkin 2025-01-15
911
+ ```
912
+
913
+ ### Available Templates
914
+ | Template | Description |
915
+ |---|---|
916
+ | `olive-oil-tunisia` | Find olive oil from small Tunisian farms |
917
+ | `hotel-direct-booking` | Book hotels directly, bypass aggregators |
918
+ | `artisan-marketplace` | Handmade products from independent artisans |
919
+ | `grocery-price-compare` | Compare grocery prices across local stores |
920
+ | `freelancer-direct` | Find freelancers without platform fees |
921
+ | `restaurant-direct` | Order from restaurants without delivery apps |
922
+ | `book-price-scout` | Find books from indie bookstores |
923
+ | `flight-deal-hunter` | Find flights direct from airlines |
924
+ | `electronics-price-tracker` | Track electronics prices with history |
925
+ | `local-services` | Find local service providers |
926
+ | `organic-farm-fresh` | Organic produce direct from farms |
927
+
928
+ ### Create Your Own Template
929
+
930
+ ```yaml
931
+ name: my-custom-agent
932
+ description: My custom agent template
933
+ goal: Find the best deals on custom products
934
+ version: "1.0"
935
+ target_sites:
936
+ - https://example.com
937
+ parameters:
938
+ budget:
939
+ type: number
940
+ default: 100
941
+ description: Maximum budget
942
+ actions:
943
+ - name: discover
944
+ wab_action: discover
945
+ - name: search
946
+ wab_action: execute
947
+ action_name: search
948
+ params:
949
+ query: "{{keyword}}"
950
+ - name: negotiate
951
+ wab_action: negotiate
952
+ params:
953
+ item_id: "{{item_id}}"
954
+ max_price: "{{budget}}"
955
+ negotiation:
956
+ enabled: true
957
+ max_rounds: 3
958
+ accept_threshold: 0.85
959
+ fairness_rules:
960
+ - Prefer independent sellers over large platforms
961
+ - Verify all prices before purchase
962
+ ```
963
+
964
+ ---
965
+
966
+ ## License
967
+
968
+ MIT — Free to use, modify, and distribute.