web-agent-bridge 2.9.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-agent-bridge",
3
- "version": "2.9.0",
3
+ "version": "3.0.0",
4
4
  "description": "Open-source middleware that bridges AI agents and websites — providing a standardized command interface for intelligent automation",
5
5
  "main": "server/index.js",
6
6
  "bin": {
package/sdk/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-agent-bridge-sdk",
3
- "version": "2.9.0",
3
+ "version": "3.0.0",
4
4
  "description": "SDK for building AI agents that interact with Web Agent Bridge (WAB)",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
@@ -1940,4 +1940,208 @@ router.get('/cluster/events', (req, res) => {
1940
1940
  res.json({ events });
1941
1941
  });
1942
1942
 
1943
+ // ═══════════════════════════════════════════════════════════════════════════
1944
+ // CONTAINER ISOLATION
1945
+ // ═══════════════════════════════════════════════════════════════════════════
1946
+
1947
+ let containerRunner;
1948
+ try { containerRunner = require('../runtime/container').containerRunner; } catch {}
1949
+
1950
+ /**
1951
+ * Run a task in an isolated container
1952
+ */
1953
+ router.post('/containers/run', async (req, res) => {
1954
+ if (!containerRunner) return res.status(501).json({ error: 'Container isolation not available' });
1955
+ try {
1956
+ const result = await containerRunner.runInProcess(
1957
+ req.body.taskId || `ctr_task_${Date.now()}`,
1958
+ req.body.code || '',
1959
+ {
1960
+ params: req.body.params || {},
1961
+ timeout: req.body.timeout || 60000,
1962
+ maxMemory: req.body.maxMemory || 256 * 1024 * 1024,
1963
+ allowNetwork: req.body.allowNetwork !== false,
1964
+ }
1965
+ );
1966
+ res.json(result);
1967
+ } catch (err) {
1968
+ res.status(500).json({ error: err.message });
1969
+ }
1970
+ });
1971
+
1972
+ /**
1973
+ * List active containers
1974
+ */
1975
+ router.get('/containers', (req, res) => {
1976
+ if (!containerRunner) return res.json({ containers: [] });
1977
+ res.json({ containers: containerRunner.listContainers() });
1978
+ });
1979
+
1980
+ /**
1981
+ * Get container details
1982
+ */
1983
+ router.get('/containers/:containerId', (req, res) => {
1984
+ if (!containerRunner) return res.status(404).json({ error: 'Not found' });
1985
+ const c = containerRunner.getContainer(req.params.containerId);
1986
+ if (!c) return res.status(404).json({ error: 'Container not found' });
1987
+ res.json(c);
1988
+ });
1989
+
1990
+ /**
1991
+ * Kill a container
1992
+ */
1993
+ router.post('/containers/:containerId/kill', (req, res) => {
1994
+ if (!containerRunner) return res.status(404).json({ error: 'Not found' });
1995
+ const ok = containerRunner.kill(req.params.containerId);
1996
+ res.json({ success: ok });
1997
+ });
1998
+
1999
+ /**
2000
+ * Container stats
2001
+ */
2002
+ router.get('/containers/stats/summary', (req, res) => {
2003
+ if (!containerRunner) return res.json({ active: 0 });
2004
+ res.json(containerRunner.getStats());
2005
+ });
2006
+
2007
+ /**
2008
+ * Check Docker availability
2009
+ */
2010
+ router.get('/containers/docker/status', (req, res) => {
2011
+ if (!containerRunner) return res.json({ available: false });
2012
+ res.json({ available: containerRunner.isDockerAvailable() });
2013
+ });
2014
+
2015
+ // ═══════════════════════════════════════════════════════════════════════════
2016
+ // EXTERNAL QUEUE MANAGEMENT
2017
+ // ═══════════════════════════════════════════════════════════════════════════
2018
+
2019
+ let queueModule;
2020
+ try { queueModule = require('../runtime/queue'); } catch {}
2021
+
2022
+ /**
2023
+ * Queue stats
2024
+ */
2025
+ router.get('/queue/stats', (req, res) => {
2026
+ if (!queueModule) return res.json({ backend: 'memory' });
2027
+ const q = queueModule.createQueue('scheduler');
2028
+ res.json(q.getStats());
2029
+ });
2030
+
2031
+ /**
2032
+ * Purge completed items from queue
2033
+ */
2034
+ router.post('/queue/purge', (req, res) => {
2035
+ if (!queueModule) return res.json({ purged: 0 });
2036
+ const q = queueModule.createQueue('scheduler');
2037
+ const purged = q.purgeCompleted(parseInt(req.body.maxAge) || 3600_000);
2038
+ res.json({ purged });
2039
+ });
2040
+
2041
+ // ═══════════════════════════════════════════════════════════════════════════
2042
+ // ENHANCED REPLAY
2043
+ // ═══════════════════════════════════════════════════════════════════════════
2044
+
2045
+ /**
2046
+ * Export a recording (full data for download)
2047
+ */
2048
+ router.get('/replay/recordings/:taskId/export', (req, res) => {
2049
+ const data = replayEngine.exportRecording(req.params.taskId);
2050
+ if (!data) return res.status(404).json({ error: 'Recording not found' });
2051
+ res.json(data);
2052
+ });
2053
+
2054
+ /**
2055
+ * Import a recording
2056
+ */
2057
+ router.post('/replay/recordings/import', (req, res) => {
2058
+ try {
2059
+ const id = replayEngine.importRecording(req.body);
2060
+ res.json({ success: true, recordingId: id });
2061
+ } catch (err) {
2062
+ res.status(400).json({ error: err.message });
2063
+ }
2064
+ });
2065
+
2066
+ /**
2067
+ * Delete a recording
2068
+ */
2069
+ router.delete('/replay/recordings/:taskId', (req, res) => {
2070
+ replayEngine.deleteRecording(req.params.taskId);
2071
+ res.json({ success: true });
2072
+ });
2073
+
2074
+ /**
2075
+ * Replay from a specific checkpoint
2076
+ */
2077
+ router.post('/replay/:taskId/from-checkpoint', async (req, res) => {
2078
+ try {
2079
+ const result = await replayEngine.replay(req.params.taskId, {
2080
+ verify: req.body.verify !== false,
2081
+ continueOnMismatch: !!req.body.continueOnMismatch,
2082
+ fromCheckpoint: req.body.checkpoint,
2083
+ });
2084
+ res.json(result);
2085
+ } catch (err) {
2086
+ res.status(400).json({ error: err.message });
2087
+ }
2088
+ });
2089
+
2090
+ /**
2091
+ * Purge old recordings
2092
+ */
2093
+ router.post('/replay/purge', (req, res) => {
2094
+ const maxAge = parseInt(req.body.maxAge) || 7 * 24 * 3600_000;
2095
+ replayEngine.purgeOld(maxAge);
2096
+ res.json({ success: true });
2097
+ });
2098
+
2099
+ // ═══════════════════════════════════════════════════════════════════════════
2100
+ // WORKER PULL ENDPOINT (for distributed workers)
2101
+ // ═══════════════════════════════════════════════════════════════════════════
2102
+
2103
+ /**
2104
+ * Workers pull tasks from here
2105
+ */
2106
+ router.post('/cluster/nodes/:nodeId/pull', (req, res) => {
2107
+ const limit = parseInt(req.body.limit) || 5;
2108
+ // Fetch pending tasks from the cluster task distributor
2109
+ const tasks = [];
2110
+ try {
2111
+ const pending = distributor.getPendingTasks ? distributor.getPendingTasks(req.params.nodeId, limit) : [];
2112
+ tasks.push(...pending);
2113
+ } catch {}
2114
+ res.json({ tasks });
2115
+ });
2116
+
2117
+ /**
2118
+ * Worker reports task started
2119
+ */
2120
+ router.post('/cluster/tasks/:taskId/started', (req, res) => {
2121
+ bus.emit('cluster.task.started', { taskId: req.params.taskId, nodeId: req.body.nodeId });
2122
+ res.json({ ok: true });
2123
+ });
2124
+
2125
+ /**
2126
+ * Worker reports task completed
2127
+ */
2128
+ router.post('/cluster/tasks/:taskId/completed', (req, res) => {
2129
+ bus.emit('cluster.task.completed', {
2130
+ taskId: req.params.taskId,
2131
+ result: req.body.result,
2132
+ });
2133
+ res.json({ ok: true });
2134
+ });
2135
+
2136
+ /**
2137
+ * Worker reports task failed
2138
+ */
2139
+ router.post('/cluster/tasks/:taskId/failed', (req, res) => {
2140
+ bus.emit('cluster.task.failed', {
2141
+ taskId: req.params.taskId,
2142
+ error: req.body.error,
2143
+ });
2144
+ res.json({ ok: true });
2145
+ });
2146
+
1943
2147
  module.exports = router;
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * WAB Container Worker — Runs inside a forked child process
5
+ *
6
+ * This script is the entry point for process-isolated task execution.
7
+ * It reads the task definition from a JSON file, executes it,
8
+ * and sends results back via IPC.
9
+ *
10
+ * Security:
11
+ * - Runs in a separate process with memory limits (--max-old-space-size)
12
+ * - Limited filesystem access (only its tmp directory)
13
+ * - Can disable network via environment
14
+ * - Timeout enforced by parent
15
+ */
16
+
17
+ const fs = require('fs');
18
+ const path = require('path');
19
+
20
+ const taskFile = process.argv[2];
21
+ if (!taskFile) {
22
+ process.stderr.write('No task file specified\n');
23
+ process.exit(1);
24
+ }
25
+
26
+ let taskData;
27
+ try {
28
+ taskData = JSON.parse(fs.readFileSync(taskFile, 'utf8'));
29
+ } catch (err) {
30
+ process.stderr.write(`Failed to read task file: ${err.message}\n`);
31
+ process.exit(1);
32
+ }
33
+
34
+ // ─── Sandbox Utilities (available to task code) ──────────────────────
35
+
36
+ const sandbox = {
37
+ taskId: taskData.taskId,
38
+ containerId: taskData.containerId,
39
+ params: taskData.params || {},
40
+
41
+ // Send progress updates
42
+ progress(pct) {
43
+ if (process.send) process.send({ type: 'progress', progress: pct });
44
+ },
45
+
46
+ // Send log messages
47
+ log(message) {
48
+ if (process.send) process.send({ type: 'log', message: String(message).slice(0, 1000) });
49
+ },
50
+
51
+ // Read a param
52
+ param(key, defaultValue) {
53
+ return taskData.params[key] !== undefined ? taskData.params[key] : defaultValue;
54
+ },
55
+
56
+ // Filesystem is restricted to tmpDir
57
+ tmpDir: path.dirname(taskFile),
58
+
59
+ readFile(name) {
60
+ const p = path.join(sandbox.tmpDir, path.basename(name));
61
+ return fs.readFileSync(p, 'utf8');
62
+ },
63
+
64
+ writeFile(name, content) {
65
+ const p = path.join(sandbox.tmpDir, path.basename(name));
66
+ fs.writeFileSync(p, content);
67
+ },
68
+ };
69
+
70
+ // ─── Execute Task ────────────────────────────────────────────────────
71
+
72
+ async function execute() {
73
+ try {
74
+ let result;
75
+
76
+ if (taskData.code) {
77
+ // Execute provided code string in a restricted scope
78
+ const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
79
+ const fn = new AsyncFunction('sandbox', 'params', taskData.code);
80
+ result = await fn(sandbox, taskData.params);
81
+ } else if (taskData.module) {
82
+ // Execute a module (for trusted internal tasks)
83
+ const mod = require(taskData.module);
84
+ if (typeof mod.execute === 'function') {
85
+ result = await mod.execute(taskData.params, sandbox);
86
+ } else {
87
+ result = { error: 'Module has no execute() function' };
88
+ }
89
+ } else {
90
+ result = { echo: taskData.params, message: 'No code or module specified' };
91
+ }
92
+
93
+ // Send result back via IPC
94
+ if (process.send) {
95
+ process.send({ type: 'result', data: result });
96
+ }
97
+
98
+ // Give IPC time to flush
99
+ setTimeout(() => process.exit(0), 100);
100
+ } catch (err) {
101
+ process.stderr.write(`Task error: ${err.message}\n${err.stack}\n`);
102
+
103
+ if (process.send) {
104
+ process.send({ type: 'result', data: null });
105
+ }
106
+
107
+ setTimeout(() => process.exit(1), 100);
108
+ }
109
+ }
110
+
111
+ execute();