web-agent-bridge 1.0.0 → 1.1.1

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 (53) hide show
  1. package/README.ar.md +1 -1
  2. package/README.md +336 -36
  3. package/docs/DEPLOY.md +118 -0
  4. package/docs/SPEC.md +1540 -0
  5. package/examples/mcp-agent.js +94 -0
  6. package/examples/vision-agent.js +12 -0
  7. package/package.json +14 -3
  8. package/public/admin/dashboard.html +848 -0
  9. package/public/admin/login.html +84 -0
  10. package/public/cookies.html +208 -0
  11. package/public/css/premium.css +317 -0
  12. package/public/dashboard.html +138 -0
  13. package/public/docs.html +5 -2
  14. package/public/index.html +54 -28
  15. package/public/js/auth-nav.js +31 -0
  16. package/public/js/auth-redirect.js +12 -0
  17. package/public/js/cookie-consent.js +56 -0
  18. package/public/js/ws-client.js +74 -0
  19. package/public/login.html +4 -2
  20. package/public/premium-dashboard.html +2075 -0
  21. package/public/premium.html +791 -0
  22. package/public/privacy.html +295 -0
  23. package/public/register.html +11 -2
  24. package/public/terms.html +254 -0
  25. package/script/ai-agent-bridge.js +253 -22
  26. package/sdk/index.js +36 -0
  27. package/server/config/secrets.js +92 -0
  28. package/server/index.js +102 -26
  29. package/server/middleware/adminAuth.js +30 -0
  30. package/server/middleware/auth.js +4 -7
  31. package/server/middleware/rateLimits.js +24 -0
  32. package/server/migrations/001_add_analytics_indexes.sql +7 -0
  33. package/server/migrations/002_premium_features.sql +418 -0
  34. package/server/models/db.js +360 -4
  35. package/server/routes/admin.js +247 -0
  36. package/server/routes/api.js +26 -9
  37. package/server/routes/billing.js +45 -0
  38. package/server/routes/discovery.js +329 -0
  39. package/server/routes/license.js +200 -11
  40. package/server/routes/noscript.js +543 -0
  41. package/server/routes/premium.js +724 -0
  42. package/server/routes/wab-api.js +476 -0
  43. package/server/services/email.js +204 -0
  44. package/server/services/fairness.js +420 -0
  45. package/server/services/premium.js +1680 -0
  46. package/server/services/stripe.js +192 -0
  47. package/server/utils/cache.js +125 -0
  48. package/server/utils/migrate.js +81 -0
  49. package/server/utils/secureFields.js +50 -0
  50. package/server/ws.js +33 -13
  51. package/wab-mcp-adapter/README.md +136 -0
  52. package/wab-mcp-adapter/index.js +555 -0
  53. package/wab-mcp-adapter/package.json +17 -0
@@ -0,0 +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.1.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;
@@ -0,0 +1,204 @@
1
+ /**
2
+ * SMTP Email Notification Service
3
+ * Templates: welcome, registration, password_reset, contact
4
+ */
5
+
6
+ const nodemailer = require('nodemailer');
7
+ const { getSmtpSettings, logNotification } = require('../models/db');
8
+
9
+ function escapeHtml(s) {
10
+ if (s == null) return '';
11
+ return String(s)
12
+ .replace(/&/g, '&')
13
+ .replace(/</g, '&lt;')
14
+ .replace(/>/g, '&gt;')
15
+ .replace(/"/g, '&quot;')
16
+ .replace(/'/g, '&#39;');
17
+ }
18
+
19
+ /** Plain-text email subject lines (no HTML entities) */
20
+ function sanitizeSubjectPart(s) {
21
+ if (s == null) return '';
22
+ return String(s).replace(/[\r\n]/g, ' ').slice(0, 300);
23
+ }
24
+
25
+ let transporter = null;
26
+
27
+ function getTransporter() {
28
+ const settings = getSmtpSettings();
29
+ if (!settings || !settings.enabled || !settings.host) return null;
30
+
31
+ transporter = nodemailer.createTransport({
32
+ host: settings.host,
33
+ port: settings.port || 587,
34
+ secure: !!settings.secure,
35
+ auth: {
36
+ user: settings.username,
37
+ pass: settings.password
38
+ }
39
+ });
40
+
41
+ return transporter;
42
+ }
43
+
44
+ const templates = {
45
+ welcome: (data) => ({
46
+ subject: `Welcome to Web Agent Bridge, ${sanitizeSubjectPart(data.name)}!`,
47
+ html: `
48
+ <div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0e1a;color:#f0f4ff;padding:40px;border-radius:12px;">
49
+ <div style="text-align:center;margin-bottom:30px;">
50
+ <div style="font-size:40px;">⚡</div>
51
+ <h1 style="color:#3b82f6;margin:10px 0;">Web Agent Bridge</h1>
52
+ </div>
53
+ <h2 style="color:#f0f4ff;">Welcome aboard, ${escapeHtml(data.name)}!</h2>
54
+ <p style="color:#94a3b8;line-height:1.8;">
55
+ Your account has been successfully created. You're now ready to bridge AI agents with your websites.
56
+ </p>
57
+ <div style="background:#1a2236;border-radius:8px;padding:20px;margin:20px 0;">
58
+ <h3 style="color:#3b82f6;margin-bottom:12px;">Quick Start:</h3>
59
+ <ol style="color:#94a3b8;line-height:2;">
60
+ <li>Add your website in the Dashboard</li>
61
+ <li>Copy the installation snippet</li>
62
+ <li>Paste it into your website's &lt;head&gt; tag</li>
63
+ <li>AI agents can now interact with your site!</li>
64
+ </ol>
65
+ </div>
66
+ <div style="text-align:center;margin-top:30px;">
67
+ <a href="${data.dashboardUrl || 'https://webagentbridge.com/dashboard'}" style="background:linear-gradient(135deg,#3b82f6,#8b5cf6);color:#fff;padding:14px 32px;border-radius:8px;text-decoration:none;font-weight:600;">Go to Dashboard</a>
68
+ </div>
69
+ <p style="color:#64748b;font-size:12px;text-align:center;margin-top:30px;">
70
+ &copy; ${new Date().getFullYear()} Web Agent Bridge. All rights reserved.
71
+ </p>
72
+ </div>
73
+ `
74
+ }),
75
+
76
+ registration: (data) => ({
77
+ subject: 'Account Registration Confirmed — Web Agent Bridge',
78
+ html: `
79
+ <div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0e1a;color:#f0f4ff;padding:40px;border-radius:12px;">
80
+ <div style="text-align:center;margin-bottom:30px;">
81
+ <div style="font-size:40px;">⚡</div>
82
+ <h1 style="color:#3b82f6;margin:10px 0;">Web Agent Bridge</h1>
83
+ </div>
84
+ <h2>Registration Successful</h2>
85
+ <p style="color:#94a3b8;">Hello ${escapeHtml(data.name)},</p>
86
+ <p style="color:#94a3b8;line-height:1.8;">
87
+ Your account has been registered successfully. Here are your account details:
88
+ </p>
89
+ <div style="background:#1a2236;border-radius:8px;padding:20px;margin:20px 0;">
90
+ <p style="color:#94a3b8;"><strong style="color:#f0f4ff;">Email:</strong> ${escapeHtml(data.email)}</p>
91
+ <p style="color:#94a3b8;"><strong style="color:#f0f4ff;">Name:</strong> ${escapeHtml(data.name)}</p>
92
+ ${data.company ? `<p style="color:#94a3b8;"><strong style="color:#f0f4ff;">Company:</strong> ${escapeHtml(data.company)}</p>` : ''}
93
+ </div>
94
+ <p style="color:#64748b;font-size:12px;text-align:center;margin-top:30px;">
95
+ &copy; ${new Date().getFullYear()} Web Agent Bridge
96
+ </p>
97
+ </div>
98
+ `
99
+ }),
100
+
101
+ password_reset: (data) => ({
102
+ subject: 'Password Reset — Web Agent Bridge',
103
+ html: `
104
+ <div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0e1a;color:#f0f4ff;padding:40px;border-radius:12px;">
105
+ <div style="text-align:center;margin-bottom:30px;">
106
+ <div style="font-size:40px;">🔐</div>
107
+ <h1 style="color:#3b82f6;margin:10px 0;">Password Reset</h1>
108
+ </div>
109
+ <p style="color:#94a3b8;">Hello ${escapeHtml(data.name)},</p>
110
+ <p style="color:#94a3b8;line-height:1.8;">
111
+ We received a request to reset your password. Click the button below to set a new password.
112
+ </p>
113
+ <div style="text-align:center;margin:30px 0;">
114
+ <a href="${data.resetUrl}" style="background:linear-gradient(135deg,#3b82f6,#8b5cf6);color:#fff;padding:14px 32px;border-radius:8px;text-decoration:none;font-weight:600;">Reset Password</a>
115
+ </div>
116
+ <p style="color:#64748b;font-size:13px;">
117
+ This link expires in 1 hour. If you didn't request this, ignore this email.
118
+ </p>
119
+ <p style="color:#64748b;font-size:12px;text-align:center;margin-top:30px;">
120
+ &copy; ${new Date().getFullYear()} Web Agent Bridge
121
+ </p>
122
+ </div>
123
+ `
124
+ }),
125
+
126
+ contact: (data) => ({
127
+ subject: `New Contact Message: ${sanitizeSubjectPart(data.subject || 'No Subject')}`,
128
+ html: `
129
+ <div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0e1a;color:#f0f4ff;padding:40px;border-radius:12px;">
130
+ <div style="text-align:center;margin-bottom:30px;">
131
+ <div style="font-size:40px;">📬</div>
132
+ <h1 style="color:#3b82f6;margin:10px 0;">New Contact Message</h1>
133
+ </div>
134
+ <div style="background:#1a2236;border-radius:8px;padding:20px;margin:20px 0;">
135
+ <p style="color:#94a3b8;"><strong style="color:#f0f4ff;">From:</strong> ${escapeHtml(data.fromName)} (${escapeHtml(data.fromEmail)})</p>
136
+ <p style="color:#94a3b8;"><strong style="color:#f0f4ff;">Subject:</strong> ${escapeHtml(data.subject || 'N/A')}</p>
137
+ <div style="margin-top:16px;padding-top:16px;border-top:1px solid #334155;">
138
+ <p style="color:#f0f4ff;line-height:1.8;">${escapeHtml(data.message)}</p>
139
+ </div>
140
+ </div>
141
+ <p style="color:#64748b;font-size:12px;text-align:center;margin-top:30px;">
142
+ Sent via Web Agent Bridge contact form
143
+ </p>
144
+ </div>
145
+ `
146
+ }),
147
+
148
+ tier_upgrade: (data) => ({
149
+ subject: `Your plan has been upgraded to ${sanitizeSubjectPart(data.tier)} — Web Agent Bridge`,
150
+ html: `
151
+ <div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0e1a;color:#f0f4ff;padding:40px;border-radius:12px;">
152
+ <div style="text-align:center;margin-bottom:30px;">
153
+ <div style="font-size:40px;">🎉</div>
154
+ <h1 style="color:#3b82f6;margin:10px 0;">Plan Upgraded!</h1>
155
+ </div>
156
+ <p style="color:#94a3b8;">Hello ${escapeHtml(data.name)},</p>
157
+ <p style="color:#94a3b8;line-height:1.8;">
158
+ Great news! Your plan has been upgraded to <strong style="color:#10b981;">${escapeHtml(String(data.tier).toUpperCase())}</strong>.
159
+ ${data.reason ? `<br><br>Reason: ${escapeHtml(data.reason)}` : ''}
160
+ </p>
161
+ <div style="text-align:center;margin-top:30px;">
162
+ <a href="${data.dashboardUrl || 'https://webagentbridge.com/dashboard'}" style="background:linear-gradient(135deg,#10b981,#3b82f6);color:#fff;padding:14px 32px;border-radius:8px;text-decoration:none;font-weight:600;">View Dashboard</a>
163
+ </div>
164
+ <p style="color:#64748b;font-size:12px;text-align:center;margin-top:30px;">
165
+ &copy; ${new Date().getFullYear()} Web Agent Bridge
166
+ </p>
167
+ </div>
168
+ `
169
+ })
170
+ };
171
+
172
+ async function sendEmail({ to, template, data, userId }) {
173
+ const transport = getTransporter();
174
+ const settings = getSmtpSettings();
175
+
176
+ if (!transport || !settings) {
177
+ logNotification({ userId, emailTo: to, template, subject: `[${template}]`, status: 'failed', errorMessage: 'SMTP not configured' });
178
+ return { success: false, error: 'SMTP not configured' };
179
+ }
180
+
181
+ const tmpl = templates[template];
182
+ if (!tmpl) {
183
+ logNotification({ userId, emailTo: to, template, subject: `[${template}]`, status: 'failed', errorMessage: 'Unknown template' });
184
+ return { success: false, error: 'Unknown template' };
185
+ }
186
+
187
+ const { subject, html } = tmpl(data);
188
+
189
+ try {
190
+ await transport.sendMail({
191
+ from: `"${settings.from_name}" <${settings.from_email}>`,
192
+ to,
193
+ subject,
194
+ html
195
+ });
196
+ logNotification({ userId, emailTo: to, template, subject, status: 'sent' });
197
+ return { success: true };
198
+ } catch (err) {
199
+ logNotification({ userId, emailTo: to, template, subject, status: 'failed', errorMessage: err.message });
200
+ return { success: false, error: err.message };
201
+ }
202
+ }
203
+
204
+ module.exports = { sendEmail, templates };