web-agent-bridge 2.6.0 → 2.8.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.
@@ -0,0 +1,205 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Hosted Runtime Service
5
+ *
6
+ * Cloud execution abstraction for running agents without local infrastructure.
7
+ * Pay-as-you-go model with auto-scaling, resource tracking, and multi-region support.
8
+ */
9
+
10
+ const crypto = require('crypto');
11
+ const { bus } = require('../runtime/event-bus');
12
+ const metering = require('./metering');
13
+
14
+ class HostedRuntime {
15
+ constructor() {
16
+ this._instances = new Map(); // instanceId → RuntimeInstance
17
+ this._executions = new Map(); // executionId → Execution
18
+ this._maxInstances = 1000;
19
+ }
20
+
21
+ /**
22
+ * Launch a hosted runtime instance for an agent
23
+ */
24
+ launch(config) {
25
+ if (!config.agentId) throw new Error('agentId required');
26
+
27
+ const instanceId = `hrt_${crypto.randomBytes(8).toString('hex')}`;
28
+ const instance = {
29
+ id: instanceId,
30
+ agentId: config.agentId,
31
+ tier: config.tier || 'starter',
32
+ region: config.region || 'auto',
33
+ resources: {
34
+ cpu: config.cpu || '0.5', // vCPU
35
+ memory: config.memory || '512', // MB
36
+ timeout: config.timeout || 300000, // 5 min default
37
+ },
38
+ status: 'starting',
39
+ startedAt: Date.now(),
40
+ lastActivity: Date.now(),
41
+ executionCount: 0,
42
+ computeMinutes: 0,
43
+ errors: 0,
44
+ };
45
+
46
+ this._instances.set(instanceId, instance);
47
+
48
+ // Simulate startup (in real deployment, this would provision container/lambda)
49
+ instance.status = 'running';
50
+ bus.emit('hosted.launched', { instanceId, agentId: config.agentId, region: instance.region });
51
+
52
+ return instance;
53
+ }
54
+
55
+ /**
56
+ * Execute a task on a hosted instance
57
+ */
58
+ async execute(instanceId, task) {
59
+ const instance = this._instances.get(instanceId);
60
+ if (!instance) throw new Error('Instance not found');
61
+ if (instance.status !== 'running') throw new Error(`Instance not running (status: ${instance.status})`);
62
+
63
+ const executionId = `hexe_${crypto.randomBytes(8).toString('hex')}`;
64
+ const execution = {
65
+ id: executionId,
66
+ instanceId,
67
+ agentId: instance.agentId,
68
+ task: {
69
+ type: task.type,
70
+ action: task.action,
71
+ params: task.params || {},
72
+ },
73
+ status: 'running',
74
+ startedAt: Date.now(),
75
+ completedAt: null,
76
+ result: null,
77
+ error: null,
78
+ computeMs: 0,
79
+ resources: {
80
+ cpuUsage: 0,
81
+ memoryUsage: 0,
82
+ networkCalls: 0,
83
+ },
84
+ };
85
+
86
+ this._executions.set(executionId, execution);
87
+ instance.executionCount++;
88
+ instance.lastActivity = Date.now();
89
+
90
+ // Record metering
91
+ metering.record(instance.agentId, 'executionsPerDay', instance.tier, 1);
92
+
93
+ bus.emit('hosted.execution.started', { executionId, instanceId, type: task.type });
94
+
95
+ // Return execution handle (actual execution is async in real deployment)
96
+ return execution;
97
+ }
98
+
99
+ /**
100
+ * Complete an execution (called by worker after task finishes)
101
+ */
102
+ completeExecution(executionId, result, error = null) {
103
+ const execution = this._executions.get(executionId);
104
+ if (!execution) return null;
105
+
106
+ execution.status = error ? 'failed' : 'completed';
107
+ execution.completedAt = Date.now();
108
+ execution.result = result;
109
+ execution.error = error ? { message: error.message || String(error) } : null;
110
+ execution.computeMs = execution.completedAt - execution.startedAt;
111
+
112
+ // Update instance stats
113
+ const instance = this._instances.get(execution.instanceId);
114
+ if (instance) {
115
+ const minutes = execution.computeMs / 60000;
116
+ instance.computeMinutes += minutes;
117
+ metering.record(instance.agentId, 'computeMinutesPerDay', instance.tier, minutes);
118
+ if (error) instance.errors++;
119
+ }
120
+
121
+ bus.emit('hosted.execution.completed', {
122
+ executionId, instanceId: execution.instanceId,
123
+ status: execution.status, computeMs: execution.computeMs,
124
+ });
125
+
126
+ return execution;
127
+ }
128
+
129
+ /**
130
+ * Stop a hosted instance
131
+ */
132
+ stop(instanceId) {
133
+ const instance = this._instances.get(instanceId);
134
+ if (!instance) return false;
135
+ instance.status = 'stopped';
136
+ instance.stoppedAt = Date.now();
137
+ bus.emit('hosted.stopped', { instanceId, agentId: instance.agentId });
138
+ return true;
139
+ }
140
+
141
+ /**
142
+ * Get instance
143
+ */
144
+ getInstance(instanceId) {
145
+ return this._instances.get(instanceId) || null;
146
+ }
147
+
148
+ /**
149
+ * List instances
150
+ */
151
+ listInstances(filters = {}, limit = 50) {
152
+ let instances = Array.from(this._instances.values());
153
+ if (filters.agentId) instances = instances.filter(i => i.agentId === filters.agentId);
154
+ if (filters.status) instances = instances.filter(i => i.status === filters.status);
155
+ if (filters.region) instances = instances.filter(i => i.region === filters.region);
156
+ return instances.slice(0, limit);
157
+ }
158
+
159
+ /**
160
+ * Get execution
161
+ */
162
+ getExecution(executionId) {
163
+ return this._executions.get(executionId) || null;
164
+ }
165
+
166
+ /**
167
+ * List executions for an instance
168
+ */
169
+ listExecutions(instanceId, limit = 50) {
170
+ return Array.from(this._executions.values())
171
+ .filter(e => e.instanceId === instanceId)
172
+ .sort((a, b) => b.startedAt - a.startedAt)
173
+ .slice(0, limit);
174
+ }
175
+
176
+ /**
177
+ * Get compute usage for an agent
178
+ */
179
+ getComputeUsage(agentId) {
180
+ const instances = Array.from(this._instances.values())
181
+ .filter(i => i.agentId === agentId);
182
+
183
+ return {
184
+ activeInstances: instances.filter(i => i.status === 'running').length,
185
+ totalExecutions: instances.reduce((sum, i) => sum + i.executionCount, 0),
186
+ totalComputeMinutes: Math.round(instances.reduce((sum, i) => sum + i.computeMinutes, 0) * 100) / 100,
187
+ totalErrors: instances.reduce((sum, i) => sum + i.errors, 0),
188
+ };
189
+ }
190
+
191
+ getStats() {
192
+ const instances = Array.from(this._instances.values());
193
+ return {
194
+ totalInstances: instances.length,
195
+ running: instances.filter(i => i.status === 'running').length,
196
+ stopped: instances.filter(i => i.status === 'stopped').length,
197
+ totalExecutions: this._executions.size,
198
+ totalComputeMinutes: Math.round(instances.reduce((sum, i) => sum + i.computeMinutes, 0) * 100) / 100,
199
+ };
200
+ }
201
+ }
202
+
203
+ const hostedRuntime = new HostedRuntime();
204
+
205
+ module.exports = { HostedRuntime, hostedRuntime };