@enclave-vm/runtime 0.0.1
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 +48 -0
- package/src/channels/index.d.ts +7 -0
- package/src/channels/index.js +16 -0
- package/src/channels/index.js.map +1 -0
- package/src/channels/memory-channel.d.ts +62 -0
- package/src/channels/memory-channel.js +111 -0
- package/src/channels/memory-channel.js.map +1 -0
- package/src/channels/websocket-channel.d.ts +99 -0
- package/src/channels/websocket-channel.js +208 -0
- package/src/channels/websocket-channel.js.map +1 -0
- package/src/cli.d.ts +19 -0
- package/src/cli.js +121 -0
- package/src/cli.js.map +1 -0
- package/src/index.d.ts +26 -0
- package/src/index.js +41 -0
- package/src/index.js.map +1 -0
- package/src/runtime-worker.d.ts +71 -0
- package/src/runtime-worker.js +222 -0
- package/src/runtime-worker.js.map +1 -0
- package/src/session-executor.d.ts +89 -0
- package/src/session-executor.js +149 -0
- package/src/session-executor.js.map +1 -0
- package/src/types.d.ts +214 -0
- package/src/types.js +10 -0
- package/src/types.js.map +1 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Session Executor
|
|
4
|
+
*
|
|
5
|
+
* Wraps enclave-vm session for runtime execution.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.SessionExecutor = void 0;
|
|
11
|
+
exports.createSessionExecutor = createSessionExecutor;
|
|
12
|
+
const core_1 = require("@enclave-vm/core");
|
|
13
|
+
/**
|
|
14
|
+
* Session executor
|
|
15
|
+
*
|
|
16
|
+
* Manages execution of a single session, handling communication
|
|
17
|
+
* between the enclave-vm session and the runtime channel.
|
|
18
|
+
*/
|
|
19
|
+
class SessionExecutor {
|
|
20
|
+
sessionId;
|
|
21
|
+
session;
|
|
22
|
+
channel;
|
|
23
|
+
code;
|
|
24
|
+
debug;
|
|
25
|
+
unsubscribe = null;
|
|
26
|
+
executing = false;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
const { code, channel, limits, debug = false } = options;
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.channel = channel;
|
|
31
|
+
this.debug = debug;
|
|
32
|
+
// Create session with provided ID
|
|
33
|
+
this.session = (0, core_1.createSession)({
|
|
34
|
+
sessionId: options.sessionId,
|
|
35
|
+
ttlMs: limits?.sessionTtlMs,
|
|
36
|
+
config: {
|
|
37
|
+
limits,
|
|
38
|
+
},
|
|
39
|
+
toolHandler: async (callId, toolName, args) => {
|
|
40
|
+
// Tool calls are forwarded via events
|
|
41
|
+
// The broker will handle them and send back results
|
|
42
|
+
this.log(`Tool call: ${toolName}(${JSON.stringify(args)})`);
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
this.sessionId = this.session.sessionId;
|
|
46
|
+
// Forward session events to channel
|
|
47
|
+
this.unsubscribe = this.session.events.on((event) => {
|
|
48
|
+
if (this.channel.isOpen) {
|
|
49
|
+
this.channel.send(event);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get session info
|
|
55
|
+
*/
|
|
56
|
+
getInfo() {
|
|
57
|
+
return {
|
|
58
|
+
sessionId: this.sessionId,
|
|
59
|
+
state: this.session.state,
|
|
60
|
+
createdAt: this.session.createdAt,
|
|
61
|
+
expiresAt: this.session.expiresAt,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get current state
|
|
66
|
+
*/
|
|
67
|
+
get state() {
|
|
68
|
+
return this.session.state;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Check if session is in terminal state
|
|
72
|
+
*/
|
|
73
|
+
get isTerminal() {
|
|
74
|
+
const state = this.session.state;
|
|
75
|
+
return state === 'completed' || state === 'cancelled' || state === 'failed';
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Execute the code
|
|
79
|
+
*/
|
|
80
|
+
async execute() {
|
|
81
|
+
if (this.executing) {
|
|
82
|
+
throw new Error('Session is already executing');
|
|
83
|
+
}
|
|
84
|
+
this.executing = true;
|
|
85
|
+
try {
|
|
86
|
+
this.log(`Starting execution for session ${this.sessionId}`);
|
|
87
|
+
// Start the session
|
|
88
|
+
this.session.start();
|
|
89
|
+
// Note: In a real implementation, we would need to:
|
|
90
|
+
// 1. Parse and execute the code in the enclave
|
|
91
|
+
// 2. Handle tool calls by waiting for tool_result messages
|
|
92
|
+
//
|
|
93
|
+
// For now, we simulate basic execution
|
|
94
|
+
// The actual execution is handled by enclave-vm internally
|
|
95
|
+
// Wait for completion
|
|
96
|
+
const result = await this.session.wait();
|
|
97
|
+
this.log(`Session ${this.sessionId} completed: ${result.finalState}`);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
this.log(`Session ${this.sessionId} error: ${error}`);
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
this.executing = false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Submit a tool result
|
|
109
|
+
*/
|
|
110
|
+
async submitToolResult(callId, success, value, error) {
|
|
111
|
+
await this.session.submitToolResult({
|
|
112
|
+
callId,
|
|
113
|
+
success,
|
|
114
|
+
value,
|
|
115
|
+
error,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Cancel the session
|
|
120
|
+
*/
|
|
121
|
+
async cancel(reason) {
|
|
122
|
+
await this.session.cancel(reason);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Dispose of the executor
|
|
126
|
+
*/
|
|
127
|
+
dispose() {
|
|
128
|
+
if (this.unsubscribe) {
|
|
129
|
+
this.unsubscribe();
|
|
130
|
+
this.unsubscribe = null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Log debug message
|
|
135
|
+
*/
|
|
136
|
+
log(message) {
|
|
137
|
+
if (this.debug) {
|
|
138
|
+
console.log(`[SessionExecutor] ${message}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.SessionExecutor = SessionExecutor;
|
|
143
|
+
/**
|
|
144
|
+
* Create a session executor
|
|
145
|
+
*/
|
|
146
|
+
function createSessionExecutor(options) {
|
|
147
|
+
return new SessionExecutor(options);
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=session-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-executor.js","sourceRoot":"","sources":["../../src/session-executor.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AA+LH,sDAEC;AA9LD,2CAA0D;AAiC1D;;;;;GAKG;AACH,MAAa,eAAe;IACjB,SAAS,CAAY;IACb,OAAO,CAAU;IACjB,OAAO,CAAiB;IACxB,IAAI,CAAS;IACb,KAAK,CAAU;IACxB,WAAW,GAAwB,IAAI,CAAC;IACxC,SAAS,GAAG,KAAK,CAAC;IAE1B,YAAY,OAA+B;QACzC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC;QAEzD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QAEnB,kCAAkC;QAClC,IAAI,CAAC,OAAO,GAAG,IAAA,oBAAa,EAAC;YAC3B,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,KAAK,EAAE,MAAM,EAAE,YAAY;YAC3B,MAAM,EAAE;gBACN,MAAM;aACP;YACD,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;gBAC5C,sCAAsC;gBACtC,oDAAoD;gBACpD,IAAI,CAAC,GAAG,CAAC,cAAc,QAA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;AAhJD,0CAgJC;AAED;;GAEG;AACH,SAAgB,qBAAqB,CAAC,OAA+B;IACnE,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC","sourcesContent":["/**\n * Session Executor\n *\n * Wraps enclave-vm session for runtime execution.\n *\n * @packageDocumentation\n */\n\nimport type { SessionId, CallId, SessionLimits } from '@enclave-vm/types';\nimport { createSession, Session } from '@enclave-vm/core';\nimport type { RuntimeChannel, RuntimeSession } from './types';\n\n/**\n * Session executor options\n */\nexport interface SessionExecutorOptions {\n /**\n * Session ID\n */\n sessionId?: SessionId;\n\n /**\n * Code to execute\n */\n code: string;\n\n /**\n * Session limits\n */\n limits?: Partial<SessionLimits>;\n\n /**\n * Communication channel\n */\n channel: RuntimeChannel;\n\n /**\n * Debug mode\n */\n debug?: boolean;\n}\n\n/**\n * Session executor\n *\n * Manages execution of a single session, handling communication\n * between the enclave-vm session and the runtime channel.\n */\nexport class SessionExecutor {\n readonly sessionId: SessionId;\n private readonly session: Session;\n private readonly channel: RuntimeChannel;\n private readonly code: string;\n private readonly debug: boolean;\n private unsubscribe: (() => void) | null = null;\n private executing = false;\n\n constructor(options: SessionExecutorOptions) {\n const { code, channel, limits, debug = false } = options;\n\n this.code = code;\n this.channel = channel;\n this.debug = debug;\n\n // Create session with provided ID\n this.session = createSession({\n sessionId: options.sessionId,\n ttlMs: limits?.sessionTtlMs,\n config: {\n limits,\n },\n toolHandler: async (callId, toolName, args) => {\n // Tool calls are forwarded via events\n // The broker will handle them and send back results\n this.log(`Tool call: ${toolName}(${JSON.stringify(args)})`);\n },\n });\n\n this.sessionId = this.session.sessionId;\n\n // Forward session events to channel\n this.unsubscribe = this.session.events.on((event) => {\n if (this.channel.isOpen) {\n this.channel.send(event);\n }\n });\n }\n\n /**\n * Get session info\n */\n getInfo(): RuntimeSession {\n return {\n sessionId: this.sessionId,\n state: this.session.state,\n createdAt: this.session.createdAt,\n expiresAt: this.session.expiresAt,\n };\n }\n\n /**\n * Get current state\n */\n get state(): string {\n return this.session.state;\n }\n\n /**\n * Check if session is in terminal state\n */\n get isTerminal(): boolean {\n const state = this.session.state;\n return state === 'completed' || state === 'cancelled' || state === 'failed';\n }\n\n /**\n * Execute the code\n */\n async execute(): Promise<void> {\n if (this.executing) {\n throw new Error('Session is already executing');\n }\n\n this.executing = true;\n\n try {\n this.log(`Starting execution for session ${this.sessionId}`);\n\n // Start the session\n this.session.start();\n\n // Note: In a real implementation, we would need to:\n // 1. Parse and execute the code in the enclave\n // 2. Handle tool calls by waiting for tool_result messages\n //\n // For now, we simulate basic execution\n // The actual execution is handled by enclave-vm internally\n\n // Wait for completion\n const result = await this.session.wait();\n\n this.log(`Session ${this.sessionId} completed: ${result.finalState}`);\n } catch (error) {\n this.log(`Session ${this.sessionId} error: ${error}`);\n throw error;\n } finally {\n this.executing = false;\n }\n }\n\n /**\n * Submit a tool result\n */\n async submitToolResult(\n callId: CallId,\n success: boolean,\n value?: unknown,\n error?: { code?: string; message: string },\n ): Promise<void> {\n await this.session.submitToolResult({\n callId,\n success,\n value,\n error,\n });\n }\n\n /**\n * Cancel the session\n */\n async cancel(reason?: string): Promise<void> {\n await this.session.cancel(reason);\n }\n\n /**\n * Dispose of the executor\n */\n dispose(): void {\n if (this.unsubscribe) {\n this.unsubscribe();\n this.unsubscribe = null;\n }\n }\n\n /**\n * Log debug message\n */\n private log(message: string): void {\n if (this.debug) {\n console.log(`[SessionExecutor] ${message}`);\n }\n }\n}\n\n/**\n * Create a session executor\n */\nexport function createSessionExecutor(options: SessionExecutorOptions): SessionExecutor {\n return new SessionExecutor(options);\n}\n"]}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime Types
|
|
3
|
+
*
|
|
4
|
+
* Types for the standalone runtime worker.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import type { SessionId, SessionLimits, StreamEvent, RuntimeChannelMessage } from '@enclave-vm/types';
|
|
9
|
+
/**
|
|
10
|
+
* Runtime configuration
|
|
11
|
+
*/
|
|
12
|
+
export interface RuntimeConfig {
|
|
13
|
+
/**
|
|
14
|
+
* Port to listen on
|
|
15
|
+
* @default 3001
|
|
16
|
+
*/
|
|
17
|
+
port?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Host to bind to
|
|
20
|
+
* @default '0.0.0.0'
|
|
21
|
+
*/
|
|
22
|
+
host?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Path for WebSocket connections
|
|
25
|
+
* @default '/ws'
|
|
26
|
+
*/
|
|
27
|
+
wsPath?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum concurrent sessions per runtime
|
|
30
|
+
* @default 10
|
|
31
|
+
*/
|
|
32
|
+
maxSessions?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Default session limits
|
|
35
|
+
*/
|
|
36
|
+
defaultLimits?: Partial<SessionLimits>;
|
|
37
|
+
/**
|
|
38
|
+
* Enable debug logging
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
debug?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Heartbeat interval in milliseconds
|
|
44
|
+
* @default 15000
|
|
45
|
+
*/
|
|
46
|
+
heartbeatIntervalMs?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Connection timeout in milliseconds
|
|
49
|
+
* @default 30000
|
|
50
|
+
*/
|
|
51
|
+
connectionTimeoutMs?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Execution request from broker
|
|
55
|
+
*/
|
|
56
|
+
export interface ExecuteRequest {
|
|
57
|
+
type: 'execute';
|
|
58
|
+
sessionId: SessionId;
|
|
59
|
+
code: string;
|
|
60
|
+
limits?: Partial<SessionLimits>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Cancel request from broker
|
|
64
|
+
*/
|
|
65
|
+
export interface CancelRequest {
|
|
66
|
+
type: 'cancel';
|
|
67
|
+
sessionId: SessionId;
|
|
68
|
+
reason?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Tool result from broker
|
|
72
|
+
*/
|
|
73
|
+
export interface ToolResultRequest {
|
|
74
|
+
type: 'tool_result';
|
|
75
|
+
sessionId: SessionId;
|
|
76
|
+
callId: string;
|
|
77
|
+
success: boolean;
|
|
78
|
+
value?: unknown;
|
|
79
|
+
error?: {
|
|
80
|
+
code?: string;
|
|
81
|
+
message: string;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Ping request for health checks
|
|
86
|
+
*/
|
|
87
|
+
export interface PingRequest {
|
|
88
|
+
type: 'ping';
|
|
89
|
+
timestamp: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* All possible runtime channel messages
|
|
93
|
+
*/
|
|
94
|
+
export type RuntimeRequest = ExecuteRequest | CancelRequest | ToolResultRequest | PingRequest;
|
|
95
|
+
/**
|
|
96
|
+
* Runtime worker state
|
|
97
|
+
*/
|
|
98
|
+
export type RuntimeState = 'idle' | 'starting' | 'running' | 'stopping' | 'stopped';
|
|
99
|
+
/**
|
|
100
|
+
* Session state within runtime
|
|
101
|
+
*/
|
|
102
|
+
export interface RuntimeSession {
|
|
103
|
+
sessionId: SessionId;
|
|
104
|
+
state: string;
|
|
105
|
+
createdAt: number;
|
|
106
|
+
expiresAt: number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Runtime statistics
|
|
110
|
+
*/
|
|
111
|
+
export interface RuntimeStats {
|
|
112
|
+
/**
|
|
113
|
+
* Runtime state
|
|
114
|
+
*/
|
|
115
|
+
state: RuntimeState;
|
|
116
|
+
/**
|
|
117
|
+
* Number of active sessions
|
|
118
|
+
*/
|
|
119
|
+
activeSessions: number;
|
|
120
|
+
/**
|
|
121
|
+
* Total sessions executed since start
|
|
122
|
+
*/
|
|
123
|
+
totalSessions: number;
|
|
124
|
+
/**
|
|
125
|
+
* Runtime start time
|
|
126
|
+
*/
|
|
127
|
+
startedAt: number;
|
|
128
|
+
/**
|
|
129
|
+
* Uptime in milliseconds
|
|
130
|
+
*/
|
|
131
|
+
uptimeMs: number;
|
|
132
|
+
/**
|
|
133
|
+
* Memory usage
|
|
134
|
+
*/
|
|
135
|
+
memoryUsage: {
|
|
136
|
+
heapUsed: number;
|
|
137
|
+
heapTotal: number;
|
|
138
|
+
rss: number;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Event handler for runtime events
|
|
143
|
+
*/
|
|
144
|
+
export type RuntimeEventHandler = (event: StreamEvent) => void;
|
|
145
|
+
/**
|
|
146
|
+
* Connection handler for WebSocket connections
|
|
147
|
+
*/
|
|
148
|
+
export type ConnectionHandler = (sessionId: SessionId, sendEvent: (event: StreamEvent) => void) => {
|
|
149
|
+
onMessage: (message: RuntimeChannelMessage) => void;
|
|
150
|
+
onClose: () => void;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Runtime worker interface
|
|
154
|
+
*/
|
|
155
|
+
export interface RuntimeWorker {
|
|
156
|
+
/**
|
|
157
|
+
* Start the runtime worker
|
|
158
|
+
*/
|
|
159
|
+
start(): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* Stop the runtime worker
|
|
162
|
+
*/
|
|
163
|
+
stop(): Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Get runtime statistics
|
|
166
|
+
*/
|
|
167
|
+
getStats(): RuntimeStats;
|
|
168
|
+
/**
|
|
169
|
+
* Get active sessions
|
|
170
|
+
*/
|
|
171
|
+
getSessions(): RuntimeSession[];
|
|
172
|
+
/**
|
|
173
|
+
* Check if runtime is running
|
|
174
|
+
*/
|
|
175
|
+
readonly isRunning: boolean;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* WebSocket server interface (platform-agnostic)
|
|
179
|
+
*/
|
|
180
|
+
export interface WebSocketServer {
|
|
181
|
+
/**
|
|
182
|
+
* Start listening
|
|
183
|
+
*/
|
|
184
|
+
listen(): Promise<void>;
|
|
185
|
+
/**
|
|
186
|
+
* Stop the server
|
|
187
|
+
*/
|
|
188
|
+
close(): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Handle new connections
|
|
191
|
+
*/
|
|
192
|
+
onConnection(handler: ConnectionHandler): void;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Channel abstraction for communication
|
|
196
|
+
*/
|
|
197
|
+
export interface RuntimeChannel {
|
|
198
|
+
/**
|
|
199
|
+
* Send a stream event
|
|
200
|
+
*/
|
|
201
|
+
send(event: StreamEvent): void;
|
|
202
|
+
/**
|
|
203
|
+
* Receive messages
|
|
204
|
+
*/
|
|
205
|
+
onMessage(handler: (message: RuntimeChannelMessage) => void): () => void;
|
|
206
|
+
/**
|
|
207
|
+
* Close the channel
|
|
208
|
+
*/
|
|
209
|
+
close(): void;
|
|
210
|
+
/**
|
|
211
|
+
* Whether the channel is open
|
|
212
|
+
*/
|
|
213
|
+
readonly isOpen: boolean;
|
|
214
|
+
}
|
package/src/types.js
ADDED
package/src/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * Runtime Types\n *\n * Types for the standalone runtime worker.\n *\n * @packageDocumentation\n */\n\nimport type { SessionId, SessionLimits, StreamEvent, RuntimeChannelMessage } from '@enclave-vm/types';\n\n/**\n * Runtime configuration\n */\nexport interface RuntimeConfig {\n /**\n * Port to listen on\n * @default 3001\n */\n port?: number;\n\n /**\n * Host to bind to\n * @default '0.0.0.0'\n */\n host?: string;\n\n /**\n * Path for WebSocket connections\n * @default '/ws'\n */\n wsPath?: string;\n\n /**\n * Maximum concurrent sessions per runtime\n * @default 10\n */\n maxSessions?: number;\n\n /**\n * Default session limits\n */\n defaultLimits?: Partial<SessionLimits>;\n\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n\n /**\n * Heartbeat interval in milliseconds\n * @default 15000\n */\n heartbeatIntervalMs?: number;\n\n /**\n * Connection timeout in milliseconds\n * @default 30000\n */\n connectionTimeoutMs?: number;\n}\n\n/**\n * Execution request from broker\n */\nexport interface ExecuteRequest {\n type: 'execute';\n sessionId: SessionId;\n code: string;\n limits?: Partial<SessionLimits>;\n}\n\n/**\n * Cancel request from broker\n */\nexport interface CancelRequest {\n type: 'cancel';\n sessionId: SessionId;\n reason?: string;\n}\n\n/**\n * Tool result from broker\n */\nexport interface ToolResultRequest {\n type: 'tool_result';\n sessionId: SessionId;\n callId: string;\n success: boolean;\n value?: unknown;\n error?: {\n code?: string;\n message: string;\n };\n}\n\n/**\n * Ping request for health checks\n */\nexport interface PingRequest {\n type: 'ping';\n timestamp: number;\n}\n\n/**\n * All possible runtime channel messages\n */\nexport type RuntimeRequest = ExecuteRequest | CancelRequest | ToolResultRequest | PingRequest;\n\n/**\n * Runtime worker state\n */\nexport type RuntimeState = 'idle' | 'starting' | 'running' | 'stopping' | 'stopped';\n\n/**\n * Session state within runtime\n */\nexport interface RuntimeSession {\n sessionId: SessionId;\n state: string;\n createdAt: number;\n expiresAt: number;\n}\n\n/**\n * Runtime statistics\n */\nexport interface RuntimeStats {\n /**\n * Runtime state\n */\n state: RuntimeState;\n\n /**\n * Number of active sessions\n */\n activeSessions: number;\n\n /**\n * Total sessions executed since start\n */\n totalSessions: number;\n\n /**\n * Runtime start time\n */\n startedAt: number;\n\n /**\n * Uptime in milliseconds\n */\n uptimeMs: number;\n\n /**\n * Memory usage\n */\n memoryUsage: {\n heapUsed: number;\n heapTotal: number;\n rss: number;\n };\n}\n\n/**\n * Event handler for runtime events\n */\nexport type RuntimeEventHandler = (event: StreamEvent) => void;\n\n/**\n * Connection handler for WebSocket connections\n */\nexport type ConnectionHandler = (\n sessionId: SessionId,\n sendEvent: (event: StreamEvent) => void,\n) => {\n onMessage: (message: RuntimeChannelMessage) => void;\n onClose: () => void;\n};\n\n/**\n * Runtime worker interface\n */\nexport interface RuntimeWorker {\n /**\n * Start the runtime worker\n */\n start(): Promise<void>;\n\n /**\n * Stop the runtime worker\n */\n stop(): Promise<void>;\n\n /**\n * Get runtime statistics\n */\n getStats(): RuntimeStats;\n\n /**\n * Get active sessions\n */\n getSessions(): RuntimeSession[];\n\n /**\n * Check if runtime is running\n */\n readonly isRunning: boolean;\n}\n\n/**\n * WebSocket server interface (platform-agnostic)\n */\nexport interface WebSocketServer {\n /**\n * Start listening\n */\n listen(): Promise<void>;\n\n /**\n * Stop the server\n */\n close(): Promise<void>;\n\n /**\n * Handle new connections\n */\n onConnection(handler: ConnectionHandler): void;\n}\n\n/**\n * Channel abstraction for communication\n */\nexport interface RuntimeChannel {\n /**\n * Send a stream event\n */\n send(event: StreamEvent): void;\n\n /**\n * Receive messages\n */\n onMessage(handler: (message: RuntimeChannelMessage) => void): () => void;\n\n /**\n * Close the channel\n */\n close(): void;\n\n /**\n * Whether the channel is open\n */\n readonly isOpen: boolean;\n}\n"]}
|