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