web-agent-bridge 1.1.1 → 1.1.2

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 (66) hide show
  1. package/LICENSE +21 -21
  2. package/README.ar.md +446 -446
  3. package/README.md +844 -844
  4. package/bin/cli.js +80 -80
  5. package/bin/wab.js +80 -80
  6. package/docs/DEPLOY.md +118 -118
  7. package/docs/SPEC.md +1540 -1540
  8. package/examples/bidi-agent.js +119 -119
  9. package/examples/mcp-agent.js +94 -94
  10. package/examples/puppeteer-agent.js +108 -108
  11. package/examples/vision-agent.js +171 -171
  12. package/package.json +78 -78
  13. package/public/admin/dashboard.html +848 -848
  14. package/public/admin/login.html +84 -84
  15. package/public/cookies.html +208 -208
  16. package/public/css/styles.css +1235 -1235
  17. package/public/dashboard.html +704 -704
  18. package/public/docs.html +585 -585
  19. package/public/index.html +332 -332
  20. package/public/js/auth-nav.js +31 -31
  21. package/public/js/auth-redirect.js +12 -12
  22. package/public/js/cookie-consent.js +56 -56
  23. package/public/js/ws-client.js +74 -74
  24. package/public/login.html +83 -83
  25. package/public/privacy.html +295 -295
  26. package/public/register.html +103 -103
  27. package/public/terms.html +254 -254
  28. package/script/ai-agent-bridge.js +1513 -1513
  29. package/sdk/README.md +55 -55
  30. package/sdk/index.js +203 -203
  31. package/sdk/package.json +14 -14
  32. package/server/config/secrets.js +92 -92
  33. package/server/index.js +181 -181
  34. package/server/middleware/adminAuth.js +30 -30
  35. package/server/middleware/auth.js +41 -41
  36. package/server/middleware/rateLimits.js +24 -24
  37. package/server/migrations/001_add_analytics_indexes.sql +7 -7
  38. package/server/models/adapters/index.js +33 -33
  39. package/server/models/adapters/mysql.js +183 -183
  40. package/server/models/adapters/postgresql.js +172 -172
  41. package/server/models/adapters/sqlite.js +7 -7
  42. package/server/models/db.js +561 -561
  43. package/server/routes/admin.js +247 -247
  44. package/server/routes/api.js +138 -138
  45. package/server/routes/auth.js +51 -51
  46. package/server/routes/billing.js +45 -45
  47. package/server/routes/discovery.js +329 -329
  48. package/server/routes/license.js +240 -240
  49. package/server/routes/noscript.js +543 -543
  50. package/server/routes/wab-api.js +476 -476
  51. package/server/services/email.js +204 -204
  52. package/server/services/fairness.js +420 -420
  53. package/server/services/stripe.js +192 -192
  54. package/server/utils/cache.js +125 -125
  55. package/server/utils/migrate.js +81 -81
  56. package/server/utils/secureFields.js +50 -50
  57. package/server/ws.js +101 -101
  58. package/wab-mcp-adapter/README.md +136 -136
  59. package/wab-mcp-adapter/index.js +555 -555
  60. package/wab-mcp-adapter/package.json +17 -17
  61. package/public/css/premium.css +0 -317
  62. package/public/premium-dashboard.html +0 -2075
  63. package/public/premium.html +0 -791
  64. package/server/migrations/002_premium_features.sql +0 -418
  65. package/server/routes/premium.js +0 -724
  66. package/server/services/premium.js +0 -1680
@@ -1,247 +1,247 @@
1
- /**
2
- * Admin API Routes
3
- * Full admin panel backend: users, sites, analytics, Stripe, SMTP, grants
4
- */
5
-
6
- const express = require('express');
7
- const router = express.Router();
8
- const { authenticateAdmin, generateAdminToken } = require('../middleware/adminAuth');
9
- const {
10
- loginAdmin, findAdminById, createAdmin,
11
- getAllUsers, getAllSites, getAdminStats, getPlatformAnalytics,
12
- getUserFullDetails, adminUpdateUserTier, adminUpdateSite, adminDeleteUser,
13
- grantFreeTier, revokeGrant, getActiveGrants,
14
- getSmtpSettings, updateSmtpSettings, getNotificationLogs,
15
- getPayments, getPlatformSetting, setPlatformSetting,
16
- findUserByEmail,
17
- findSiteById,
18
- getAnalyticsBySite,
19
- getAnalyticsTimeline
20
- } = require('../models/db');
21
- const { sendEmail } = require('../services/email');
22
- const { createCheckoutSession, createPortalSession, isStripeConfigured, getStripePrices } = require('../services/stripe');
23
-
24
- // ─── Auth ──────────────────────────────────────────────────────────────
25
-
26
- router.post('/login', (req, res) => {
27
- const { email, password } = req.body;
28
- if (!email || !password) return res.status(400).json({ error: 'Email and password required' });
29
-
30
- const admin = loginAdmin({ email, password });
31
- if (!admin) return res.status(401).json({ error: 'Invalid credentials' });
32
-
33
- const token = generateAdminToken(admin);
34
- res.json({ admin, token });
35
- });
36
-
37
- router.get('/me', authenticateAdmin, (req, res) => {
38
- const admin = findAdminById(req.admin.id);
39
- if (!admin) return res.status(404).json({ error: 'Admin not found' });
40
- res.json({ admin });
41
- });
42
-
43
- // ─── Dashboard Stats ──────────────────────────────────────────────────
44
-
45
- router.get('/stats', authenticateAdmin, (req, res) => {
46
- const stats = getAdminStats();
47
- stats.stripeConfigured = isStripeConfigured();
48
- res.json(stats);
49
- });
50
-
51
- router.get('/analytics', authenticateAdmin, (req, res) => {
52
- const days = parseInt(req.query.days) || 30;
53
- const data = getPlatformAnalytics(days);
54
- res.json(data);
55
- });
56
-
57
- // ─── Users Management ─────────────────────────────────────────────────
58
-
59
- router.get('/users', authenticateAdmin, (req, res) => {
60
- const users = getAllUsers();
61
- res.json({ users });
62
- });
63
-
64
- router.get('/users/:id', authenticateAdmin, (req, res) => {
65
- const user = getUserFullDetails(req.params.id);
66
- if (!user) return res.status(404).json({ error: 'User not found' });
67
- res.json({ user });
68
- });
69
-
70
- router.put('/users/:id/tier', authenticateAdmin, (req, res) => {
71
- const { tier, siteId } = req.body;
72
- if (!['free', 'starter', 'pro', 'enterprise'].includes(tier)) {
73
- return res.status(400).json({ error: 'Invalid tier' });
74
- }
75
- adminUpdateUserTier(req.params.id, siteId, tier);
76
- res.json({ success: true });
77
- });
78
-
79
- router.delete('/users/:id', authenticateAdmin, (req, res) => {
80
- adminDeleteUser(req.params.id);
81
- res.json({ success: true });
82
- });
83
-
84
- // ─── Sites Management ─────────────────────────────────────────────────
85
-
86
- router.get('/sites', authenticateAdmin, (req, res) => {
87
- const sites = getAllSites();
88
- res.json({ sites });
89
- });
90
-
91
- router.put('/sites/:id', authenticateAdmin, (req, res) => {
92
- const { tier, active } = req.body;
93
- const ok = adminUpdateSite(req.params.id, { tier, active });
94
- if (!ok) return res.status(404).json({ error: 'Site not found or invalid tier' });
95
- res.json({ success: true });
96
- });
97
-
98
- router.get('/sites/:id/analytics', authenticateAdmin, (req, res) => {
99
- const site = findSiteById.get(req.params.id);
100
- if (!site) return res.status(404).json({ error: 'Site not found' });
101
- const days = parseInt(req.query.days, 10) || 30;
102
- const since = new Date(Date.now() - days * 86400000).toISOString();
103
- const summary = getAnalyticsBySite.all(site.id, since);
104
- const timeline = getAnalyticsTimeline.all(site.id, since);
105
- res.json({
106
- site: {
107
- id: site.id,
108
- name: site.name,
109
- domain: site.domain,
110
- tier: site.tier,
111
- license_key: site.license_key
112
- },
113
- summary,
114
- timeline,
115
- period: `${days} days`
116
- });
117
- });
118
-
119
- // ─── Free Grants ──────────────────────────────────────────────────────
120
-
121
- router.get('/grants', authenticateAdmin, (req, res) => {
122
- const grants = getActiveGrants();
123
- res.json({ grants });
124
- });
125
-
126
- router.post('/grants', authenticateAdmin, (req, res) => {
127
- const { userId, siteId, tier, reason, expiresAt } = req.body;
128
- if (!userId || !tier) return res.status(400).json({ error: 'userId and tier required' });
129
- if (!['starter', 'pro', 'enterprise'].includes(tier)) return res.status(400).json({ error: 'Invalid tier' });
130
-
131
- const grant = grantFreeTier({ userId, siteId, tier, reason, grantedBy: req.admin.id, expiresAt });
132
-
133
- // Send notification email
134
- const user = getUserFullDetails(userId);
135
- if (user) {
136
- sendEmail({
137
- to: user.email,
138
- template: 'tier_upgrade',
139
- data: { name: user.name, tier, reason: reason || 'Complimentary upgrade' },
140
- userId
141
- }).catch(() => {});
142
- }
143
-
144
- res.status(201).json({ grant });
145
- });
146
-
147
- router.delete('/grants/:id', authenticateAdmin, (req, res) => {
148
- const ok = revokeGrant(req.params.id);
149
- if (!ok) return res.status(404).json({ error: 'Grant not found' });
150
- res.json({ success: true });
151
- });
152
-
153
- // ─── Stripe Settings ─────────────────────────────────────────────────
154
-
155
- router.get('/stripe/config', authenticateAdmin, (req, res) => {
156
- const secretKey = getPlatformSetting('stripe_secret_key');
157
- const publishableKey = getPlatformSetting('stripe_publishable_key');
158
- const webhookSecret = getPlatformSetting('stripe_webhook_secret');
159
- const prices = getStripePrices();
160
-
161
- res.json({
162
- configured: isStripeConfigured(),
163
- hasSecretKey: !!secretKey,
164
- publishableKey: publishableKey || '',
165
- webhookSecret: webhookSecret ? '••••' + webhookSecret.slice(-4) : '',
166
- prices
167
- });
168
- });
169
-
170
- router.put('/stripe/config', authenticateAdmin, (req, res) => {
171
- const { secretKey, publishableKey, webhookSecret, priceStarter, pricePro, priceEnterprise } = req.body;
172
-
173
- if (secretKey) setPlatformSetting('stripe_secret_key', secretKey);
174
- if (publishableKey) setPlatformSetting('stripe_publishable_key', publishableKey);
175
- if (webhookSecret) setPlatformSetting('stripe_webhook_secret', webhookSecret);
176
- if (priceStarter) setPlatformSetting('stripe_price_starter', priceStarter);
177
- if (pricePro) setPlatformSetting('stripe_price_pro', pricePro);
178
- if (priceEnterprise) setPlatformSetting('stripe_price_enterprise', priceEnterprise);
179
-
180
- res.json({ success: true });
181
- });
182
-
183
- // ─── Payments ──────────────────────────────────────────────────────────
184
-
185
- router.get('/payments', authenticateAdmin, (req, res) => {
186
- const limit = parseInt(req.query.limit) || 50;
187
- const payments = getPayments(limit);
188
- res.json({ payments });
189
- });
190
-
191
- // ─── SMTP Settings ────────────────────────────────────────────────────
192
-
193
- router.get('/smtp', authenticateAdmin, (req, res) => {
194
- const settings = getSmtpSettings();
195
- // Mask password
196
- if (settings && settings.password) {
197
- settings.password = '••••••••';
198
- }
199
- res.json({ settings });
200
- });
201
-
202
- router.put('/smtp', authenticateAdmin, (req, res) => {
203
- const { host, port, secure, username, password, fromName, fromEmail, enabled } = req.body;
204
- if (!host || !username || !fromEmail) {
205
- return res.status(400).json({ error: 'Host, username, and fromEmail are required' });
206
- }
207
- updateSmtpSettings({ host, port, secure, username, password, fromName, fromEmail, enabled });
208
- res.json({ success: true });
209
- });
210
-
211
- router.post('/smtp/test', authenticateAdmin, (req, res) => {
212
- const { testEmail } = req.body;
213
- if (!testEmail) return res.status(400).json({ error: 'testEmail required' });
214
-
215
- sendEmail({
216
- to: testEmail,
217
- template: 'welcome',
218
- data: { name: 'Test User', dashboardUrl: 'https://webagentbridge.com/dashboard' }
219
- }).then(result => {
220
- res.json(result);
221
- }).catch(err => {
222
- res.status(500).json({ success: false, error: err.message });
223
- });
224
- });
225
-
226
- // ─── Notification Logs ────────────────────────────────────────────────
227
-
228
- router.get('/notifications', authenticateAdmin, (req, res) => {
229
- const limit = parseInt(req.query.limit) || 100;
230
- const logs = getNotificationLogs(limit);
231
- res.json({ logs });
232
- });
233
-
234
- // ─── Send Custom Notification ─────────────────────────────────────────
235
-
236
- router.post('/notifications/send', authenticateAdmin, (req, res) => {
237
- const { userId, email, template, data } = req.body;
238
- if (!email || !template) return res.status(400).json({ error: 'email and template required' });
239
-
240
- sendEmail({ to: email, template, data: data || {}, userId }).then(result => {
241
- res.json(result);
242
- }).catch(err => {
243
- res.status(500).json({ success: false, error: err.message });
244
- });
245
- });
246
-
247
- module.exports = router;
1
+ /**
2
+ * Admin API Routes
3
+ * Full admin panel backend: users, sites, analytics, Stripe, SMTP, grants
4
+ */
5
+
6
+ const express = require('express');
7
+ const router = express.Router();
8
+ const { authenticateAdmin, generateAdminToken } = require('../middleware/adminAuth');
9
+ const {
10
+ loginAdmin, findAdminById, createAdmin,
11
+ getAllUsers, getAllSites, getAdminStats, getPlatformAnalytics,
12
+ getUserFullDetails, adminUpdateUserTier, adminUpdateSite, adminDeleteUser,
13
+ grantFreeTier, revokeGrant, getActiveGrants,
14
+ getSmtpSettings, updateSmtpSettings, getNotificationLogs,
15
+ getPayments, getPlatformSetting, setPlatformSetting,
16
+ findUserByEmail,
17
+ findSiteById,
18
+ getAnalyticsBySite,
19
+ getAnalyticsTimeline
20
+ } = require('../models/db');
21
+ const { sendEmail } = require('../services/email');
22
+ const { createCheckoutSession, createPortalSession, isStripeConfigured, getStripePrices } = require('../services/stripe');
23
+
24
+ // ─── Auth ──────────────────────────────────────────────────────────────
25
+
26
+ router.post('/login', (req, res) => {
27
+ const { email, password } = req.body;
28
+ if (!email || !password) return res.status(400).json({ error: 'Email and password required' });
29
+
30
+ const admin = loginAdmin({ email, password });
31
+ if (!admin) return res.status(401).json({ error: 'Invalid credentials' });
32
+
33
+ const token = generateAdminToken(admin);
34
+ res.json({ admin, token });
35
+ });
36
+
37
+ router.get('/me', authenticateAdmin, (req, res) => {
38
+ const admin = findAdminById(req.admin.id);
39
+ if (!admin) return res.status(404).json({ error: 'Admin not found' });
40
+ res.json({ admin });
41
+ });
42
+
43
+ // ─── Dashboard Stats ──────────────────────────────────────────────────
44
+
45
+ router.get('/stats', authenticateAdmin, (req, res) => {
46
+ const stats = getAdminStats();
47
+ stats.stripeConfigured = isStripeConfigured();
48
+ res.json(stats);
49
+ });
50
+
51
+ router.get('/analytics', authenticateAdmin, (req, res) => {
52
+ const days = parseInt(req.query.days) || 30;
53
+ const data = getPlatformAnalytics(days);
54
+ res.json(data);
55
+ });
56
+
57
+ // ─── Users Management ─────────────────────────────────────────────────
58
+
59
+ router.get('/users', authenticateAdmin, (req, res) => {
60
+ const users = getAllUsers();
61
+ res.json({ users });
62
+ });
63
+
64
+ router.get('/users/:id', authenticateAdmin, (req, res) => {
65
+ const user = getUserFullDetails(req.params.id);
66
+ if (!user) return res.status(404).json({ error: 'User not found' });
67
+ res.json({ user });
68
+ });
69
+
70
+ router.put('/users/:id/tier', authenticateAdmin, (req, res) => {
71
+ const { tier, siteId } = req.body;
72
+ if (!['free', 'starter', 'pro', 'enterprise'].includes(tier)) {
73
+ return res.status(400).json({ error: 'Invalid tier' });
74
+ }
75
+ adminUpdateUserTier(req.params.id, siteId, tier);
76
+ res.json({ success: true });
77
+ });
78
+
79
+ router.delete('/users/:id', authenticateAdmin, (req, res) => {
80
+ adminDeleteUser(req.params.id);
81
+ res.json({ success: true });
82
+ });
83
+
84
+ // ─── Sites Management ─────────────────────────────────────────────────
85
+
86
+ router.get('/sites', authenticateAdmin, (req, res) => {
87
+ const sites = getAllSites();
88
+ res.json({ sites });
89
+ });
90
+
91
+ router.put('/sites/:id', authenticateAdmin, (req, res) => {
92
+ const { tier, active } = req.body;
93
+ const ok = adminUpdateSite(req.params.id, { tier, active });
94
+ if (!ok) return res.status(404).json({ error: 'Site not found or invalid tier' });
95
+ res.json({ success: true });
96
+ });
97
+
98
+ router.get('/sites/:id/analytics', authenticateAdmin, (req, res) => {
99
+ const site = findSiteById.get(req.params.id);
100
+ if (!site) return res.status(404).json({ error: 'Site not found' });
101
+ const days = parseInt(req.query.days, 10) || 30;
102
+ const since = new Date(Date.now() - days * 86400000).toISOString();
103
+ const summary = getAnalyticsBySite.all(site.id, since);
104
+ const timeline = getAnalyticsTimeline.all(site.id, since);
105
+ res.json({
106
+ site: {
107
+ id: site.id,
108
+ name: site.name,
109
+ domain: site.domain,
110
+ tier: site.tier,
111
+ license_key: site.license_key
112
+ },
113
+ summary,
114
+ timeline,
115
+ period: `${days} days`
116
+ });
117
+ });
118
+
119
+ // ─── Free Grants ──────────────────────────────────────────────────────
120
+
121
+ router.get('/grants', authenticateAdmin, (req, res) => {
122
+ const grants = getActiveGrants();
123
+ res.json({ grants });
124
+ });
125
+
126
+ router.post('/grants', authenticateAdmin, (req, res) => {
127
+ const { userId, siteId, tier, reason, expiresAt } = req.body;
128
+ if (!userId || !tier) return res.status(400).json({ error: 'userId and tier required' });
129
+ if (!['starter', 'pro', 'enterprise'].includes(tier)) return res.status(400).json({ error: 'Invalid tier' });
130
+
131
+ const grant = grantFreeTier({ userId, siteId, tier, reason, grantedBy: req.admin.id, expiresAt });
132
+
133
+ // Send notification email
134
+ const user = getUserFullDetails(userId);
135
+ if (user) {
136
+ sendEmail({
137
+ to: user.email,
138
+ template: 'tier_upgrade',
139
+ data: { name: user.name, tier, reason: reason || 'Complimentary upgrade' },
140
+ userId
141
+ }).catch(() => {});
142
+ }
143
+
144
+ res.status(201).json({ grant });
145
+ });
146
+
147
+ router.delete('/grants/:id', authenticateAdmin, (req, res) => {
148
+ const ok = revokeGrant(req.params.id);
149
+ if (!ok) return res.status(404).json({ error: 'Grant not found' });
150
+ res.json({ success: true });
151
+ });
152
+
153
+ // ─── Stripe Settings ─────────────────────────────────────────────────
154
+
155
+ router.get('/stripe/config', authenticateAdmin, (req, res) => {
156
+ const secretKey = getPlatformSetting('stripe_secret_key');
157
+ const publishableKey = getPlatformSetting('stripe_publishable_key');
158
+ const webhookSecret = getPlatformSetting('stripe_webhook_secret');
159
+ const prices = getStripePrices();
160
+
161
+ res.json({
162
+ configured: isStripeConfigured(),
163
+ hasSecretKey: !!secretKey,
164
+ publishableKey: publishableKey || '',
165
+ webhookSecret: webhookSecret ? '••••' + webhookSecret.slice(-4) : '',
166
+ prices
167
+ });
168
+ });
169
+
170
+ router.put('/stripe/config', authenticateAdmin, (req, res) => {
171
+ const { secretKey, publishableKey, webhookSecret, priceStarter, pricePro, priceEnterprise } = req.body;
172
+
173
+ if (secretKey) setPlatformSetting('stripe_secret_key', secretKey);
174
+ if (publishableKey) setPlatformSetting('stripe_publishable_key', publishableKey);
175
+ if (webhookSecret) setPlatformSetting('stripe_webhook_secret', webhookSecret);
176
+ if (priceStarter) setPlatformSetting('stripe_price_starter', priceStarter);
177
+ if (pricePro) setPlatformSetting('stripe_price_pro', pricePro);
178
+ if (priceEnterprise) setPlatformSetting('stripe_price_enterprise', priceEnterprise);
179
+
180
+ res.json({ success: true });
181
+ });
182
+
183
+ // ─── Payments ──────────────────────────────────────────────────────────
184
+
185
+ router.get('/payments', authenticateAdmin, (req, res) => {
186
+ const limit = parseInt(req.query.limit) || 50;
187
+ const payments = getPayments(limit);
188
+ res.json({ payments });
189
+ });
190
+
191
+ // ─── SMTP Settings ────────────────────────────────────────────────────
192
+
193
+ router.get('/smtp', authenticateAdmin, (req, res) => {
194
+ const settings = getSmtpSettings();
195
+ // Mask password
196
+ if (settings && settings.password) {
197
+ settings.password = '••••••••';
198
+ }
199
+ res.json({ settings });
200
+ });
201
+
202
+ router.put('/smtp', authenticateAdmin, (req, res) => {
203
+ const { host, port, secure, username, password, fromName, fromEmail, enabled } = req.body;
204
+ if (!host || !username || !fromEmail) {
205
+ return res.status(400).json({ error: 'Host, username, and fromEmail are required' });
206
+ }
207
+ updateSmtpSettings({ host, port, secure, username, password, fromName, fromEmail, enabled });
208
+ res.json({ success: true });
209
+ });
210
+
211
+ router.post('/smtp/test', authenticateAdmin, (req, res) => {
212
+ const { testEmail } = req.body;
213
+ if (!testEmail) return res.status(400).json({ error: 'testEmail required' });
214
+
215
+ sendEmail({
216
+ to: testEmail,
217
+ template: 'welcome',
218
+ data: { name: 'Test User', dashboardUrl: 'https://webagentbridge.com/dashboard' }
219
+ }).then(result => {
220
+ res.json(result);
221
+ }).catch(err => {
222
+ res.status(500).json({ success: false, error: err.message });
223
+ });
224
+ });
225
+
226
+ // ─── Notification Logs ────────────────────────────────────────────────
227
+
228
+ router.get('/notifications', authenticateAdmin, (req, res) => {
229
+ const limit = parseInt(req.query.limit) || 100;
230
+ const logs = getNotificationLogs(limit);
231
+ res.json({ logs });
232
+ });
233
+
234
+ // ─── Send Custom Notification ─────────────────────────────────────────
235
+
236
+ router.post('/notifications/send', authenticateAdmin, (req, res) => {
237
+ const { userId, email, template, data } = req.body;
238
+ if (!email || !template) return res.status(400).json({ error: 'email and template required' });
239
+
240
+ sendEmail({ to: email, template, data: data || {}, userId }).then(result => {
241
+ res.json(result);
242
+ }).catch(err => {
243
+ res.status(500).json({ success: false, error: err.message });
244
+ });
245
+ });
246
+
247
+ module.exports = router;