@enclave-vm/runtime 2.19.0 → 2.19.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.
- package/README.md +85 -257
- package/channels/websocket-channel.d.ts +10 -0
- package/cli.d.ts +7 -5
- package/cli.js +34 -351
- package/esm/cli.mjs +34 -351
- package/esm/index.mjs +72 -27
- package/index.d.ts +12 -11
- package/index.js +73 -27
- package/package.json +5 -5
- package/runtime-worker.d.ts +19 -6
- package/session-executor.d.ts +25 -6
- package/types.d.ts +8 -5
package/cli.js
CHANGED
|
@@ -1,321 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
|
-
// libs/runtime/src/
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
debug;
|
|
12
|
-
unsubscribe = null;
|
|
13
|
-
executing = false;
|
|
14
|
-
constructor(options) {
|
|
15
|
-
const { code, channel, limits, debug = false } = options;
|
|
16
|
-
this.code = code;
|
|
17
|
-
this.channel = channel;
|
|
18
|
-
this.debug = debug;
|
|
19
|
-
this.session = (0, import_core.createSession)({
|
|
20
|
-
sessionId: options.sessionId,
|
|
21
|
-
ttlMs: limits?.sessionTtlMs,
|
|
22
|
-
config: {
|
|
23
|
-
limits
|
|
24
|
-
},
|
|
25
|
-
toolHandler: async (callId, toolName, args) => {
|
|
26
|
-
this.log(`Tool call: ${toolName}(${JSON.stringify(args)})`);
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
this.sessionId = this.session.sessionId;
|
|
30
|
-
this.unsubscribe = this.session.events.on((event) => {
|
|
31
|
-
if (this.channel.isOpen) {
|
|
32
|
-
this.channel.send(event);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Get session info
|
|
38
|
-
*/
|
|
39
|
-
getInfo() {
|
|
40
|
-
return {
|
|
41
|
-
sessionId: this.sessionId,
|
|
42
|
-
state: this.session.state,
|
|
43
|
-
createdAt: this.session.createdAt,
|
|
44
|
-
expiresAt: this.session.expiresAt
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Get current state
|
|
49
|
-
*/
|
|
50
|
-
get state() {
|
|
51
|
-
return this.session.state;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Check if session is in terminal state
|
|
55
|
-
*/
|
|
56
|
-
get isTerminal() {
|
|
57
|
-
const state = this.session.state;
|
|
58
|
-
return state === "completed" || state === "cancelled" || state === "failed";
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Execute the code
|
|
62
|
-
*/
|
|
63
|
-
async execute() {
|
|
64
|
-
if (this.executing) {
|
|
65
|
-
throw new Error("Session is already executing");
|
|
66
|
-
}
|
|
67
|
-
this.executing = true;
|
|
68
|
-
try {
|
|
69
|
-
this.log(`Starting execution for session ${this.sessionId}`);
|
|
70
|
-
this.session.start();
|
|
71
|
-
const result = await this.session.wait();
|
|
72
|
-
this.log(`Session ${this.sessionId} completed: ${result.finalState}`);
|
|
73
|
-
} catch (error) {
|
|
74
|
-
this.log(`Session ${this.sessionId} error: ${error}`);
|
|
75
|
-
throw error;
|
|
76
|
-
} finally {
|
|
77
|
-
this.executing = false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Submit a tool result
|
|
82
|
-
*/
|
|
83
|
-
async submitToolResult(callId, success, value, error) {
|
|
84
|
-
await this.session.submitToolResult({
|
|
85
|
-
callId,
|
|
86
|
-
success,
|
|
87
|
-
value,
|
|
88
|
-
error
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Cancel the session
|
|
93
|
-
*/
|
|
94
|
-
async cancel(reason) {
|
|
95
|
-
await this.session.cancel(reason);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Dispose of the executor
|
|
99
|
-
*/
|
|
100
|
-
dispose() {
|
|
101
|
-
if (this.unsubscribe) {
|
|
102
|
-
this.unsubscribe();
|
|
103
|
-
this.unsubscribe = null;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Log debug message
|
|
108
|
-
*/
|
|
109
|
-
log(message) {
|
|
110
|
-
if (this.debug) {
|
|
111
|
-
console.log(`[SessionExecutor] ${message}`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
function createSessionExecutor(options) {
|
|
116
|
-
return new SessionExecutor(options);
|
|
4
|
+
// libs/runtime/src/cli.ts
|
|
5
|
+
var EXIT_OK = 0;
|
|
6
|
+
var EXIT_ERROR = 1;
|
|
7
|
+
var NOT_IMPLEMENTED = "enclave-runtime: not implemented. @enclave-vm/runtime does not execute code and does not listen on a port, so there is no worker to start.\nRun scripts with @enclave-vm/broker (sessions, tools and streaming over HTTP) or @enclave-vm/core (the sandbox).";
|
|
8
|
+
function fail(message) {
|
|
9
|
+
console.error(message);
|
|
10
|
+
process.exit(EXIT_ERROR);
|
|
117
11
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
port: 3001,
|
|
122
|
-
host: "0.0.0.0",
|
|
123
|
-
wsPath: "/ws",
|
|
124
|
-
maxSessions: 10,
|
|
125
|
-
defaultLimits: {},
|
|
126
|
-
debug: false,
|
|
127
|
-
heartbeatIntervalMs: 15e3,
|
|
128
|
-
connectionTimeoutMs: 3e4
|
|
129
|
-
};
|
|
130
|
-
var RuntimeWorker = class {
|
|
131
|
-
config;
|
|
132
|
-
sessions;
|
|
133
|
-
state = "idle";
|
|
134
|
-
startedAt = 0;
|
|
135
|
-
totalSessions = 0;
|
|
136
|
-
constructor(config = {}) {
|
|
137
|
-
this.config = {
|
|
138
|
-
...DEFAULT_CONFIG,
|
|
139
|
-
...config
|
|
140
|
-
};
|
|
141
|
-
this.sessions = /* @__PURE__ */ new Map();
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Check if runtime is running
|
|
145
|
-
*/
|
|
146
|
-
get isRunning() {
|
|
147
|
-
return this.state === "running";
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Start the runtime worker
|
|
151
|
-
*/
|
|
152
|
-
async start() {
|
|
153
|
-
if (this.state !== "idle" && this.state !== "stopped") {
|
|
154
|
-
throw new Error(`Cannot start runtime in state: ${this.state}`);
|
|
155
|
-
}
|
|
156
|
-
this.state = "starting";
|
|
157
|
-
this.startedAt = Date.now();
|
|
158
|
-
this.state = "running";
|
|
159
|
-
this.log("Runtime worker started");
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Stop the runtime worker
|
|
163
|
-
*/
|
|
164
|
-
async stop() {
|
|
165
|
-
if (this.state === "stopped" || this.state === "stopping") {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
this.state = "stopping";
|
|
169
|
-
const cancelPromises = [];
|
|
170
|
-
for (const [_sessionId, executor] of this.sessions) {
|
|
171
|
-
cancelPromises.push(
|
|
172
|
-
executor.cancel("Runtime stopping").catch(() => {
|
|
173
|
-
})
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
await Promise.all(cancelPromises);
|
|
177
|
-
for (const executor of this.sessions.values()) {
|
|
178
|
-
executor.dispose();
|
|
179
|
-
}
|
|
180
|
-
this.sessions.clear();
|
|
181
|
-
this.state = "stopped";
|
|
182
|
-
this.log("Runtime worker stopped");
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Get runtime statistics
|
|
186
|
-
*/
|
|
187
|
-
getStats() {
|
|
188
|
-
const now = Date.now();
|
|
189
|
-
const memoryUsage = process.memoryUsage();
|
|
190
|
-
return {
|
|
191
|
-
state: this.state,
|
|
192
|
-
activeSessions: this.sessions.size,
|
|
193
|
-
totalSessions: this.totalSessions,
|
|
194
|
-
startedAt: this.startedAt,
|
|
195
|
-
uptimeMs: this.startedAt > 0 ? now - this.startedAt : 0,
|
|
196
|
-
memoryUsage: {
|
|
197
|
-
heapUsed: memoryUsage.heapUsed,
|
|
198
|
-
heapTotal: memoryUsage.heapTotal,
|
|
199
|
-
rss: memoryUsage.rss
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Get active sessions
|
|
205
|
-
*/
|
|
206
|
-
getSessions() {
|
|
207
|
-
return Array.from(this.sessions.values()).map((executor) => executor.getInfo());
|
|
12
|
+
function valueOf(option, value) {
|
|
13
|
+
if (value === void 0 || value.startsWith("--")) {
|
|
14
|
+
fail(`Option ${option} needs a value`);
|
|
208
15
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
break;
|
|
217
|
-
case "cancel":
|
|
218
|
-
await this.handleCancel(request);
|
|
219
|
-
break;
|
|
220
|
-
case "tool_result":
|
|
221
|
-
await this.handleToolResult(request);
|
|
222
|
-
break;
|
|
223
|
-
case "ping":
|
|
224
|
-
this.log("Received ping");
|
|
225
|
-
break;
|
|
226
|
-
default:
|
|
227
|
-
this.log(`Unknown request type: ${request.type}`);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Handle execute request
|
|
232
|
-
*/
|
|
233
|
-
async handleExecute(request, channel) {
|
|
234
|
-
if (!this.isRunning) {
|
|
235
|
-
throw new Error("Runtime is not running");
|
|
236
|
-
}
|
|
237
|
-
if (this.sessions.size >= this.config.maxSessions) {
|
|
238
|
-
throw new Error(`Maximum sessions reached (${this.config.maxSessions})`);
|
|
239
|
-
}
|
|
240
|
-
const { sessionId, code, limits } = request;
|
|
241
|
-
if (this.sessions.has(sessionId)) {
|
|
242
|
-
throw new Error(`Session ${sessionId} already exists`);
|
|
243
|
-
}
|
|
244
|
-
const executor = createSessionExecutor({
|
|
245
|
-
sessionId,
|
|
246
|
-
code,
|
|
247
|
-
limits,
|
|
248
|
-
channel,
|
|
249
|
-
debug: this.config.debug
|
|
250
|
-
});
|
|
251
|
-
this.sessions.set(executor.sessionId, executor);
|
|
252
|
-
this.totalSessions++;
|
|
253
|
-
executor.execute().catch((error) => {
|
|
254
|
-
this.log(`Session ${executor.sessionId} execution error: ${error}`);
|
|
255
|
-
}).finally(() => {
|
|
256
|
-
this.cleanupSession(executor.sessionId);
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Handle cancel request
|
|
261
|
-
*/
|
|
262
|
-
async handleCancel(request) {
|
|
263
|
-
const executor = this.sessions.get(request.sessionId);
|
|
264
|
-
if (!executor) {
|
|
265
|
-
this.log(`Cancel request for unknown session: ${request.sessionId}`);
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
await executor.cancel(request.reason);
|
|
269
|
-
this.cleanupSession(request.sessionId);
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Handle tool result
|
|
273
|
-
*/
|
|
274
|
-
async handleToolResult(request) {
|
|
275
|
-
const executor = this.sessions.get(request.sessionId);
|
|
276
|
-
if (!executor) {
|
|
277
|
-
this.log(`Tool result for unknown session: ${request.sessionId}`);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
await executor.submitToolResult(request.callId, request.success, request.value, request.error);
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Clean up a session
|
|
284
|
-
*/
|
|
285
|
-
cleanupSession(sessionId) {
|
|
286
|
-
const executor = this.sessions.get(sessionId);
|
|
287
|
-
if (executor) {
|
|
288
|
-
executor.dispose();
|
|
289
|
-
this.sessions.delete(sessionId);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Log debug message
|
|
294
|
-
*/
|
|
295
|
-
log(message) {
|
|
296
|
-
if (this.config.debug) {
|
|
297
|
-
console.log(`[RuntimeWorker] ${message}`);
|
|
298
|
-
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function integerOf(option, value, minimum) {
|
|
19
|
+
const text = valueOf(option, value);
|
|
20
|
+
const parsed = Number(text);
|
|
21
|
+
if (!/^\d+$/.test(text) || !Number.isSafeInteger(parsed) || parsed < minimum) {
|
|
22
|
+
fail(`Option ${option} needs a whole number of ${minimum} or more, got "${text}"`);
|
|
299
23
|
}
|
|
300
|
-
|
|
301
|
-
function createRuntimeWorker(config) {
|
|
302
|
-
return new RuntimeWorker(config);
|
|
24
|
+
return parsed;
|
|
303
25
|
}
|
|
304
|
-
|
|
305
|
-
// libs/runtime/src/cli.ts
|
|
306
26
|
function parseArgs(args) {
|
|
307
27
|
const config = {};
|
|
308
28
|
for (let i = 0; i < args.length; i++) {
|
|
309
29
|
const arg = args[i];
|
|
310
30
|
switch (arg) {
|
|
311
31
|
case "--port":
|
|
312
|
-
config.port =
|
|
32
|
+
config.port = integerOf(arg, args[++i], 0);
|
|
313
33
|
break;
|
|
314
34
|
case "--host":
|
|
315
|
-
config.host = args[++i];
|
|
35
|
+
config.host = valueOf(arg, args[++i]);
|
|
316
36
|
break;
|
|
317
37
|
case "--max-sessions":
|
|
318
|
-
config.maxSessions =
|
|
38
|
+
config.maxSessions = integerOf(arg, args[++i], 1);
|
|
319
39
|
break;
|
|
320
40
|
case "--debug":
|
|
321
41
|
config.debug = true;
|
|
@@ -323,13 +43,10 @@ function parseArgs(args) {
|
|
|
323
43
|
case "--help":
|
|
324
44
|
case "-h":
|
|
325
45
|
printHelp();
|
|
326
|
-
process.exit(
|
|
46
|
+
process.exit(EXIT_OK);
|
|
327
47
|
break;
|
|
328
48
|
default:
|
|
329
|
-
|
|
330
|
-
console.error(`Unknown option: ${arg}`);
|
|
331
|
-
process.exit(1);
|
|
332
|
-
}
|
|
49
|
+
fail(`Unknown option: ${arg}`);
|
|
333
50
|
}
|
|
334
51
|
}
|
|
335
52
|
return config;
|
|
@@ -338,55 +55,21 @@ function printHelp() {
|
|
|
338
55
|
console.log(`
|
|
339
56
|
Enclave Runtime Worker
|
|
340
57
|
|
|
58
|
+
${NOT_IMPLEMENTED}
|
|
59
|
+
|
|
341
60
|
Usage:
|
|
342
61
|
npx enclave-runtime [options]
|
|
343
62
|
|
|
344
|
-
Options:
|
|
345
|
-
--port <port>
|
|
346
|
-
--host <host>
|
|
347
|
-
--max-sessions <n>
|
|
348
|
-
--debug
|
|
63
|
+
Options (accepted and checked; none of them starts anything):
|
|
64
|
+
--port <port> Reserved (nothing listens)
|
|
65
|
+
--host <host> Reserved (nothing listens)
|
|
66
|
+
--max-sessions <n> Reserved
|
|
67
|
+
--debug Reserved
|
|
349
68
|
--help, -h Show this help message
|
|
350
|
-
|
|
351
|
-
Examples:
|
|
352
|
-
npx enclave-runtime --port 3001
|
|
353
|
-
npx enclave-runtime --port 8080 --max-sessions 20 --debug
|
|
354
69
|
`);
|
|
355
70
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
console.log("Starting Enclave Runtime Worker...");
|
|
360
|
-
console.log(` Port: ${config.port ?? 3001}`);
|
|
361
|
-
console.log(` Host: ${config.host ?? "0.0.0.0"}`);
|
|
362
|
-
console.log(` Max Sessions: ${config.maxSessions ?? 10}`);
|
|
363
|
-
console.log(` Debug: ${config.debug ?? false}`);
|
|
364
|
-
const worker = createRuntimeWorker(config);
|
|
365
|
-
const shutdown = async (signal) => {
|
|
366
|
-
console.log(`
|
|
367
|
-
Received ${signal}, shutting down...`);
|
|
368
|
-
await worker.stop();
|
|
369
|
-
console.log("Runtime stopped");
|
|
370
|
-
process.exit(0);
|
|
371
|
-
};
|
|
372
|
-
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
373
|
-
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
374
|
-
await worker.start();
|
|
375
|
-
console.log("Runtime worker is running");
|
|
376
|
-
console.log(` PID: ${process.pid}`);
|
|
377
|
-
if (config.debug) {
|
|
378
|
-
setInterval(() => {
|
|
379
|
-
const stats = worker.getStats();
|
|
380
|
-
console.log("Stats:", {
|
|
381
|
-
activeSessions: stats.activeSessions,
|
|
382
|
-
totalSessions: stats.totalSessions,
|
|
383
|
-
uptimeMs: stats.uptimeMs,
|
|
384
|
-
memoryMB: Math.round(stats.memoryUsage.heapUsed / 1024 / 1024)
|
|
385
|
-
});
|
|
386
|
-
}, 3e4);
|
|
387
|
-
}
|
|
71
|
+
function main() {
|
|
72
|
+
parseArgs(process.argv.slice(2));
|
|
73
|
+
fail(NOT_IMPLEMENTED);
|
|
388
74
|
}
|
|
389
|
-
main()
|
|
390
|
-
console.error("Failed to start runtime:", error);
|
|
391
|
-
process.exit(1);
|
|
392
|
-
});
|
|
75
|
+
main();
|