@steventsao/agent-session-core 0.1.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/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/socket-events.d.ts +493 -0
- package/dist/socket-events.d.ts.map +1 -0
- package/dist/socket-events.js +203 -0
- package/dist/socket-events.js.map +1 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +26 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized WebSocket Events
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for all socket event types used across:
|
|
5
|
+
* - SessionSocket Durable Object (Cloudflare Worker)
|
|
6
|
+
* - Client applications (React, etc.)
|
|
7
|
+
* - E2B sandbox communication
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
export declare const ClientEventType: {
|
|
11
|
+
readonly INIT: "INIT";
|
|
12
|
+
readonly PING: "PING";
|
|
13
|
+
readonly START_SANDBOX: "START_SANDBOX";
|
|
14
|
+
readonly STOP_SANDBOX: "STOP_SANDBOX";
|
|
15
|
+
readonly EXEC: "EXEC";
|
|
16
|
+
readonly AGENT_MESSAGE: "AGENT_MESSAGE";
|
|
17
|
+
readonly AGENT_STOP: "AGENT_STOP";
|
|
18
|
+
readonly AGENT_RESET: "AGENT_RESET";
|
|
19
|
+
};
|
|
20
|
+
export type ClientEventType = (typeof ClientEventType)[keyof typeof ClientEventType];
|
|
21
|
+
export declare const ServerEventType: {
|
|
22
|
+
readonly CONNECTED: "CONNECTED";
|
|
23
|
+
readonly READY: "READY";
|
|
24
|
+
readonly PONG: "PONG";
|
|
25
|
+
readonly SANDBOX_STATUS: "SANDBOX_STATUS";
|
|
26
|
+
readonly LIFECYCLE: "LIFECYCLE";
|
|
27
|
+
readonly TERM_DATA: "TERM_DATA";
|
|
28
|
+
readonly EXEC_COMPLETE: "EXEC_COMPLETE";
|
|
29
|
+
readonly SYSTEM: "SYSTEM";
|
|
30
|
+
readonly ERROR: "ERROR";
|
|
31
|
+
readonly AGENT_STARTED: "AGENT_STARTED";
|
|
32
|
+
readonly AGENT_MESSAGE: "AGENT_MESSAGE";
|
|
33
|
+
readonly AGENT_ACTION: "AGENT_ACTION";
|
|
34
|
+
readonly AGENT_STEP_COMPLETE: "AGENT_STEP_COMPLETE";
|
|
35
|
+
readonly AGENT_DONE: "AGENT_DONE";
|
|
36
|
+
readonly AGENT_ERROR: "AGENT_ERROR";
|
|
37
|
+
readonly AGENT_EVENT: "AGENT_EVENT";
|
|
38
|
+
};
|
|
39
|
+
export type ServerEventType = (typeof ServerEventType)[keyof typeof ServerEventType];
|
|
40
|
+
export declare const SandboxStatus: {
|
|
41
|
+
readonly IDLE: "idle";
|
|
42
|
+
readonly BOOTING: "booting";
|
|
43
|
+
readonly READY: "ready";
|
|
44
|
+
readonly ERROR: "error";
|
|
45
|
+
};
|
|
46
|
+
export type SandboxStatus = (typeof SandboxStatus)[keyof typeof SandboxStatus];
|
|
47
|
+
/**
|
|
48
|
+
* Granular lifecycle phases for UI loading states
|
|
49
|
+
* Common lifecycle events across different app types:
|
|
50
|
+
* - idle: No sandbox running
|
|
51
|
+
* - booting: Creating sandbox instance
|
|
52
|
+
* - environment_ready: Sandbox created, basic environment up
|
|
53
|
+
* - files_ready: App files synced/loaded (for app builders)
|
|
54
|
+
* - installing: Installing dependencies
|
|
55
|
+
* - starting: Starting services (dev server, etc.)
|
|
56
|
+
* - interaction_ready: Ready for user interaction (agent can respond)
|
|
57
|
+
* - ready: Fully ready (deprecated alias for interaction_ready)
|
|
58
|
+
* - error: Something went wrong
|
|
59
|
+
*/
|
|
60
|
+
export declare const SandboxLifecycle: {
|
|
61
|
+
readonly IDLE: "idle";
|
|
62
|
+
readonly BOOTING: "booting";
|
|
63
|
+
readonly ENVIRONMENT_READY: "environment_ready";
|
|
64
|
+
readonly FILES_READY: "files_ready";
|
|
65
|
+
readonly INSTALLING: "installing";
|
|
66
|
+
readonly STARTING: "starting";
|
|
67
|
+
readonly INTERACTION_READY: "interaction_ready";
|
|
68
|
+
readonly READY: "ready";
|
|
69
|
+
readonly ERROR: "error";
|
|
70
|
+
};
|
|
71
|
+
export type SandboxLifecycle = (typeof SandboxLifecycle)[keyof typeof SandboxLifecycle];
|
|
72
|
+
/**
|
|
73
|
+
* Parse SYSTEM message to SandboxLifecycle phase
|
|
74
|
+
*/
|
|
75
|
+
export declare function parseSystemMessageToLifecycle(msg: string): SandboxLifecycle | null;
|
|
76
|
+
export interface InitEvent {
|
|
77
|
+
type: 'INIT';
|
|
78
|
+
userId?: string;
|
|
79
|
+
metadata?: Record<string, unknown>;
|
|
80
|
+
}
|
|
81
|
+
export interface PingEvent {
|
|
82
|
+
type: 'PING';
|
|
83
|
+
}
|
|
84
|
+
export interface StartSandboxEvent {
|
|
85
|
+
type: 'START_SANDBOX';
|
|
86
|
+
template?: string;
|
|
87
|
+
}
|
|
88
|
+
export interface StopSandboxEvent {
|
|
89
|
+
type: 'STOP_SANDBOX';
|
|
90
|
+
}
|
|
91
|
+
export interface ExecEvent {
|
|
92
|
+
type: 'EXEC';
|
|
93
|
+
cmd: string;
|
|
94
|
+
cwd?: string;
|
|
95
|
+
}
|
|
96
|
+
export interface AgentMessageClientEvent {
|
|
97
|
+
type: 'AGENT_MESSAGE';
|
|
98
|
+
content: string;
|
|
99
|
+
model?: {
|
|
100
|
+
id: string;
|
|
101
|
+
provider?: string;
|
|
102
|
+
};
|
|
103
|
+
config?: {
|
|
104
|
+
temperature?: number;
|
|
105
|
+
maxTokens?: number;
|
|
106
|
+
};
|
|
107
|
+
systemPrompt?: string;
|
|
108
|
+
agentType?: string;
|
|
109
|
+
}
|
|
110
|
+
export interface AgentStopEvent {
|
|
111
|
+
type: 'AGENT_STOP';
|
|
112
|
+
}
|
|
113
|
+
export interface AgentResetEvent {
|
|
114
|
+
type: 'AGENT_RESET';
|
|
115
|
+
}
|
|
116
|
+
export type ClientEvent = InitEvent | PingEvent | StartSandboxEvent | StopSandboxEvent | ExecEvent | AgentMessageClientEvent | AgentStopEvent | AgentResetEvent;
|
|
117
|
+
export interface ConnectedServerEvent {
|
|
118
|
+
type: 'CONNECTED';
|
|
119
|
+
clientId: string;
|
|
120
|
+
sessionId: string;
|
|
121
|
+
}
|
|
122
|
+
export interface ReadyServerEvent {
|
|
123
|
+
type: 'READY';
|
|
124
|
+
sandboxId: string | null;
|
|
125
|
+
sandboxUrl: string | null;
|
|
126
|
+
}
|
|
127
|
+
export interface PongServerEvent {
|
|
128
|
+
type: 'PONG';
|
|
129
|
+
timestamp: number;
|
|
130
|
+
}
|
|
131
|
+
export interface SandboxStatusServerEvent {
|
|
132
|
+
type: 'SANDBOX_STATUS';
|
|
133
|
+
status: 'idle' | 'booting' | 'ready' | 'error';
|
|
134
|
+
sandboxId?: string;
|
|
135
|
+
sandboxUrl?: string;
|
|
136
|
+
error?: string;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Lifecycle event - granular phase transitions
|
|
140
|
+
* Use for loading UI states and progress indicators
|
|
141
|
+
*/
|
|
142
|
+
export interface LifecycleServerEvent {
|
|
143
|
+
type: 'LIFECYCLE';
|
|
144
|
+
phase: SandboxLifecycle;
|
|
145
|
+
message?: string;
|
|
146
|
+
progress?: number;
|
|
147
|
+
metadata?: Record<string, unknown>;
|
|
148
|
+
}
|
|
149
|
+
export interface TermDataServerEvent {
|
|
150
|
+
type: 'TERM_DATA';
|
|
151
|
+
data: string;
|
|
152
|
+
stream: 'stdout' | 'stderr';
|
|
153
|
+
}
|
|
154
|
+
export interface ExecCompleteServerEvent {
|
|
155
|
+
type: 'EXEC_COMPLETE';
|
|
156
|
+
exitCode: number;
|
|
157
|
+
}
|
|
158
|
+
export interface SystemServerEvent {
|
|
159
|
+
type: 'SYSTEM';
|
|
160
|
+
msg: string;
|
|
161
|
+
}
|
|
162
|
+
export interface ErrorServerEvent {
|
|
163
|
+
type: 'ERROR';
|
|
164
|
+
msg: string;
|
|
165
|
+
code?: string;
|
|
166
|
+
}
|
|
167
|
+
export interface AgentStartedServerEvent {
|
|
168
|
+
type: 'AGENT_STARTED';
|
|
169
|
+
prompt?: string;
|
|
170
|
+
}
|
|
171
|
+
export interface TextContentBlock {
|
|
172
|
+
type: 'text';
|
|
173
|
+
text: string;
|
|
174
|
+
}
|
|
175
|
+
export interface ToolUseContentBlock {
|
|
176
|
+
type: 'tool_use';
|
|
177
|
+
id: string;
|
|
178
|
+
name: string;
|
|
179
|
+
input: Record<string, unknown>;
|
|
180
|
+
}
|
|
181
|
+
export interface ToolResultContentBlock {
|
|
182
|
+
type: 'tool_result';
|
|
183
|
+
tool_use_id: string;
|
|
184
|
+
content: string | null;
|
|
185
|
+
is_error?: boolean;
|
|
186
|
+
}
|
|
187
|
+
export type ContentBlock = TextContentBlock | ToolUseContentBlock | ToolResultContentBlock;
|
|
188
|
+
export interface AgentMessageServerEvent {
|
|
189
|
+
type: 'AGENT_MESSAGE';
|
|
190
|
+
message?: {
|
|
191
|
+
content: ContentBlock[];
|
|
192
|
+
};
|
|
193
|
+
subtype?: string;
|
|
194
|
+
total_cost_usd?: number | null;
|
|
195
|
+
duration_ms?: number | null;
|
|
196
|
+
result?: string | null;
|
|
197
|
+
}
|
|
198
|
+
export interface AgentActionServerEvent {
|
|
199
|
+
type: 'AGENT_ACTION';
|
|
200
|
+
action: string;
|
|
201
|
+
id?: string;
|
|
202
|
+
input?: unknown;
|
|
203
|
+
status: 'executing' | 'complete' | 'error';
|
|
204
|
+
result?: string;
|
|
205
|
+
error?: string;
|
|
206
|
+
}
|
|
207
|
+
export interface AgentStepCompleteServerEvent {
|
|
208
|
+
type: 'AGENT_STEP_COMPLETE';
|
|
209
|
+
success?: boolean;
|
|
210
|
+
cost?: number;
|
|
211
|
+
duration?: number;
|
|
212
|
+
}
|
|
213
|
+
export interface AgentDoneServerEvent {
|
|
214
|
+
type: 'AGENT_DONE';
|
|
215
|
+
exitCode?: number;
|
|
216
|
+
sessionId?: string;
|
|
217
|
+
}
|
|
218
|
+
export interface AgentErrorServerEvent {
|
|
219
|
+
type: 'AGENT_ERROR';
|
|
220
|
+
error: string;
|
|
221
|
+
}
|
|
222
|
+
export interface AgentGenericServerEvent {
|
|
223
|
+
type: 'AGENT_EVENT';
|
|
224
|
+
[key: string]: unknown;
|
|
225
|
+
}
|
|
226
|
+
export type ServerEvent = ConnectedServerEvent | ReadyServerEvent | PongServerEvent | SandboxStatusServerEvent | LifecycleServerEvent | TermDataServerEvent | ExecCompleteServerEvent | SystemServerEvent | ErrorServerEvent | AgentStartedServerEvent | AgentMessageServerEvent | AgentActionServerEvent | AgentStepCompleteServerEvent | AgentDoneServerEvent | AgentErrorServerEvent | AgentGenericServerEvent;
|
|
227
|
+
export declare const ToolUseBlockSchema: z.ZodObject<{
|
|
228
|
+
type: z.ZodLiteral<"tool_use">;
|
|
229
|
+
name: z.ZodString;
|
|
230
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
231
|
+
}, "strip", z.ZodTypeAny, {
|
|
232
|
+
type: "tool_use";
|
|
233
|
+
name: string;
|
|
234
|
+
input?: Record<string, unknown> | undefined;
|
|
235
|
+
}, {
|
|
236
|
+
type: "tool_use";
|
|
237
|
+
name: string;
|
|
238
|
+
input?: Record<string, unknown> | undefined;
|
|
239
|
+
}>;
|
|
240
|
+
export declare const TextBlockSchema: z.ZodObject<{
|
|
241
|
+
type: z.ZodLiteral<"text">;
|
|
242
|
+
text: z.ZodString;
|
|
243
|
+
}, "strip", z.ZodTypeAny, {
|
|
244
|
+
text: string;
|
|
245
|
+
type: "text";
|
|
246
|
+
}, {
|
|
247
|
+
text: string;
|
|
248
|
+
type: "text";
|
|
249
|
+
}>;
|
|
250
|
+
export declare const ContentBlockSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
251
|
+
type: z.ZodLiteral<"tool_use">;
|
|
252
|
+
name: z.ZodString;
|
|
253
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
254
|
+
}, "strip", z.ZodTypeAny, {
|
|
255
|
+
type: "tool_use";
|
|
256
|
+
name: string;
|
|
257
|
+
input?: Record<string, unknown> | undefined;
|
|
258
|
+
}, {
|
|
259
|
+
type: "tool_use";
|
|
260
|
+
name: string;
|
|
261
|
+
input?: Record<string, unknown> | undefined;
|
|
262
|
+
}>, z.ZodObject<{
|
|
263
|
+
type: z.ZodLiteral<"text">;
|
|
264
|
+
text: z.ZodString;
|
|
265
|
+
}, "strip", z.ZodTypeAny, {
|
|
266
|
+
text: string;
|
|
267
|
+
type: "text";
|
|
268
|
+
}, {
|
|
269
|
+
text: string;
|
|
270
|
+
type: "text";
|
|
271
|
+
}>]>;
|
|
272
|
+
export declare const AssistantMessageSchema: z.ZodObject<{
|
|
273
|
+
type: z.ZodLiteral<"assistant">;
|
|
274
|
+
message: z.ZodObject<{
|
|
275
|
+
content: z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
276
|
+
type: z.ZodLiteral<"tool_use">;
|
|
277
|
+
name: z.ZodString;
|
|
278
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
279
|
+
}, "strip", z.ZodTypeAny, {
|
|
280
|
+
type: "tool_use";
|
|
281
|
+
name: string;
|
|
282
|
+
input?: Record<string, unknown> | undefined;
|
|
283
|
+
}, {
|
|
284
|
+
type: "tool_use";
|
|
285
|
+
name: string;
|
|
286
|
+
input?: Record<string, unknown> | undefined;
|
|
287
|
+
}>, z.ZodObject<{
|
|
288
|
+
type: z.ZodLiteral<"text">;
|
|
289
|
+
text: z.ZodString;
|
|
290
|
+
}, "strip", z.ZodTypeAny, {
|
|
291
|
+
text: string;
|
|
292
|
+
type: "text";
|
|
293
|
+
}, {
|
|
294
|
+
text: string;
|
|
295
|
+
type: "text";
|
|
296
|
+
}>]>, "many">;
|
|
297
|
+
}, "strip", z.ZodTypeAny, {
|
|
298
|
+
content: ({
|
|
299
|
+
type: "tool_use";
|
|
300
|
+
name: string;
|
|
301
|
+
input?: Record<string, unknown> | undefined;
|
|
302
|
+
} | {
|
|
303
|
+
text: string;
|
|
304
|
+
type: "text";
|
|
305
|
+
})[];
|
|
306
|
+
}, {
|
|
307
|
+
content: ({
|
|
308
|
+
type: "tool_use";
|
|
309
|
+
name: string;
|
|
310
|
+
input?: Record<string, unknown> | undefined;
|
|
311
|
+
} | {
|
|
312
|
+
text: string;
|
|
313
|
+
type: "text";
|
|
314
|
+
})[];
|
|
315
|
+
}>;
|
|
316
|
+
}, "strip", z.ZodTypeAny, {
|
|
317
|
+
type: "assistant";
|
|
318
|
+
message: {
|
|
319
|
+
content: ({
|
|
320
|
+
type: "tool_use";
|
|
321
|
+
name: string;
|
|
322
|
+
input?: Record<string, unknown> | undefined;
|
|
323
|
+
} | {
|
|
324
|
+
text: string;
|
|
325
|
+
type: "text";
|
|
326
|
+
})[];
|
|
327
|
+
};
|
|
328
|
+
}, {
|
|
329
|
+
type: "assistant";
|
|
330
|
+
message: {
|
|
331
|
+
content: ({
|
|
332
|
+
type: "tool_use";
|
|
333
|
+
name: string;
|
|
334
|
+
input?: Record<string, unknown> | undefined;
|
|
335
|
+
} | {
|
|
336
|
+
text: string;
|
|
337
|
+
type: "text";
|
|
338
|
+
})[];
|
|
339
|
+
};
|
|
340
|
+
}>;
|
|
341
|
+
export declare const ResultMessageSchema: z.ZodObject<{
|
|
342
|
+
type: z.ZodLiteral<"result">;
|
|
343
|
+
session_id: z.ZodOptional<z.ZodString>;
|
|
344
|
+
total_cost_usd: z.ZodOptional<z.ZodNumber>;
|
|
345
|
+
duration_ms: z.ZodOptional<z.ZodNumber>;
|
|
346
|
+
}, "strip", z.ZodTypeAny, {
|
|
347
|
+
type: "result";
|
|
348
|
+
session_id?: string | undefined;
|
|
349
|
+
total_cost_usd?: number | undefined;
|
|
350
|
+
duration_ms?: number | undefined;
|
|
351
|
+
}, {
|
|
352
|
+
type: "result";
|
|
353
|
+
session_id?: string | undefined;
|
|
354
|
+
total_cost_usd?: number | undefined;
|
|
355
|
+
duration_ms?: number | undefined;
|
|
356
|
+
}>;
|
|
357
|
+
export declare const ErrorMessageSchema: z.ZodObject<{
|
|
358
|
+
type: z.ZodLiteral<"error">;
|
|
359
|
+
error: z.ZodString;
|
|
360
|
+
}, "strip", z.ZodTypeAny, {
|
|
361
|
+
error: string;
|
|
362
|
+
type: "error";
|
|
363
|
+
}, {
|
|
364
|
+
error: string;
|
|
365
|
+
type: "error";
|
|
366
|
+
}>;
|
|
367
|
+
export declare const SystemMessageSchema: z.ZodObject<{
|
|
368
|
+
type: z.ZodLiteral<"system">;
|
|
369
|
+
message: z.ZodString;
|
|
370
|
+
}, "strip", z.ZodTypeAny, {
|
|
371
|
+
type: "system";
|
|
372
|
+
message: string;
|
|
373
|
+
}, {
|
|
374
|
+
type: "system";
|
|
375
|
+
message: string;
|
|
376
|
+
}>;
|
|
377
|
+
export declare const AgentOutputMessageSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
378
|
+
type: z.ZodLiteral<"assistant">;
|
|
379
|
+
message: z.ZodObject<{
|
|
380
|
+
content: z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
381
|
+
type: z.ZodLiteral<"tool_use">;
|
|
382
|
+
name: z.ZodString;
|
|
383
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
384
|
+
}, "strip", z.ZodTypeAny, {
|
|
385
|
+
type: "tool_use";
|
|
386
|
+
name: string;
|
|
387
|
+
input?: Record<string, unknown> | undefined;
|
|
388
|
+
}, {
|
|
389
|
+
type: "tool_use";
|
|
390
|
+
name: string;
|
|
391
|
+
input?: Record<string, unknown> | undefined;
|
|
392
|
+
}>, z.ZodObject<{
|
|
393
|
+
type: z.ZodLiteral<"text">;
|
|
394
|
+
text: z.ZodString;
|
|
395
|
+
}, "strip", z.ZodTypeAny, {
|
|
396
|
+
text: string;
|
|
397
|
+
type: "text";
|
|
398
|
+
}, {
|
|
399
|
+
text: string;
|
|
400
|
+
type: "text";
|
|
401
|
+
}>]>, "many">;
|
|
402
|
+
}, "strip", z.ZodTypeAny, {
|
|
403
|
+
content: ({
|
|
404
|
+
type: "tool_use";
|
|
405
|
+
name: string;
|
|
406
|
+
input?: Record<string, unknown> | undefined;
|
|
407
|
+
} | {
|
|
408
|
+
text: string;
|
|
409
|
+
type: "text";
|
|
410
|
+
})[];
|
|
411
|
+
}, {
|
|
412
|
+
content: ({
|
|
413
|
+
type: "tool_use";
|
|
414
|
+
name: string;
|
|
415
|
+
input?: Record<string, unknown> | undefined;
|
|
416
|
+
} | {
|
|
417
|
+
text: string;
|
|
418
|
+
type: "text";
|
|
419
|
+
})[];
|
|
420
|
+
}>;
|
|
421
|
+
}, "strip", z.ZodTypeAny, {
|
|
422
|
+
type: "assistant";
|
|
423
|
+
message: {
|
|
424
|
+
content: ({
|
|
425
|
+
type: "tool_use";
|
|
426
|
+
name: string;
|
|
427
|
+
input?: Record<string, unknown> | undefined;
|
|
428
|
+
} | {
|
|
429
|
+
text: string;
|
|
430
|
+
type: "text";
|
|
431
|
+
})[];
|
|
432
|
+
};
|
|
433
|
+
}, {
|
|
434
|
+
type: "assistant";
|
|
435
|
+
message: {
|
|
436
|
+
content: ({
|
|
437
|
+
type: "tool_use";
|
|
438
|
+
name: string;
|
|
439
|
+
input?: Record<string, unknown> | undefined;
|
|
440
|
+
} | {
|
|
441
|
+
text: string;
|
|
442
|
+
type: "text";
|
|
443
|
+
})[];
|
|
444
|
+
};
|
|
445
|
+
}>, z.ZodObject<{
|
|
446
|
+
type: z.ZodLiteral<"result">;
|
|
447
|
+
session_id: z.ZodOptional<z.ZodString>;
|
|
448
|
+
total_cost_usd: z.ZodOptional<z.ZodNumber>;
|
|
449
|
+
duration_ms: z.ZodOptional<z.ZodNumber>;
|
|
450
|
+
}, "strip", z.ZodTypeAny, {
|
|
451
|
+
type: "result";
|
|
452
|
+
session_id?: string | undefined;
|
|
453
|
+
total_cost_usd?: number | undefined;
|
|
454
|
+
duration_ms?: number | undefined;
|
|
455
|
+
}, {
|
|
456
|
+
type: "result";
|
|
457
|
+
session_id?: string | undefined;
|
|
458
|
+
total_cost_usd?: number | undefined;
|
|
459
|
+
duration_ms?: number | undefined;
|
|
460
|
+
}>, z.ZodObject<{
|
|
461
|
+
type: z.ZodLiteral<"error">;
|
|
462
|
+
error: z.ZodString;
|
|
463
|
+
}, "strip", z.ZodTypeAny, {
|
|
464
|
+
error: string;
|
|
465
|
+
type: "error";
|
|
466
|
+
}, {
|
|
467
|
+
error: string;
|
|
468
|
+
type: "error";
|
|
469
|
+
}>, z.ZodObject<{
|
|
470
|
+
type: z.ZodLiteral<"system">;
|
|
471
|
+
message: z.ZodString;
|
|
472
|
+
}, "strip", z.ZodTypeAny, {
|
|
473
|
+
type: "system";
|
|
474
|
+
message: string;
|
|
475
|
+
}, {
|
|
476
|
+
type: "system";
|
|
477
|
+
message: string;
|
|
478
|
+
}>]>;
|
|
479
|
+
export type ToolUseBlock = z.infer<typeof ToolUseBlockSchema>;
|
|
480
|
+
export type TextBlock = z.infer<typeof TextBlockSchema>;
|
|
481
|
+
export type AgentContentBlock = z.infer<typeof ContentBlockSchema>;
|
|
482
|
+
export type AssistantMessage = z.infer<typeof AssistantMessageSchema>;
|
|
483
|
+
export type ResultMessage = z.infer<typeof ResultMessageSchema>;
|
|
484
|
+
export type ErrorMessage = z.infer<typeof ErrorMessageSchema>;
|
|
485
|
+
export type SystemMessage = z.infer<typeof SystemMessageSchema>;
|
|
486
|
+
export type AgentOutputMessage = z.infer<typeof AgentOutputMessageSchema>;
|
|
487
|
+
export declare function parseAgentOutputMessage(line: string): AgentOutputMessage | null;
|
|
488
|
+
export declare function isClientEvent(data: unknown): data is ClientEvent;
|
|
489
|
+
export declare function isServerEvent(data: unknown): data is ServerEvent;
|
|
490
|
+
export declare function parseServerEvent(json: string): ServerEvent | null;
|
|
491
|
+
export declare function parseClientEvent(json: string): ClientEvent | null;
|
|
492
|
+
export type ClientMessage = ClientEvent;
|
|
493
|
+
//# sourceMappingURL=socket-events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socket-events.d.ts","sourceRoot":"","sources":["../src/socket-events.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB,eAAO,MAAM,eAAe;;;;;;;;;CAgBlB,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,OAAO,eAAe,CAAC,CAAC;AAMrF,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;CA4BlB,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,OAAO,eAAe,CAAC,CAAC;AAMrF,eAAO,MAAM,aAAa;;;;;CAKhB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,OAAO,aAAa,CAAC,CAAC;AAE/E;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,gBAAgB;;;;;;;;;;CAUnB,CAAC;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,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAQjE;AAGD,MAAM,MAAM,aAAa,GAAG,WAAW,CAAC"}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized WebSocket Events
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for all socket event types used across:
|
|
5
|
+
* - SessionSocket Durable Object (Cloudflare Worker)
|
|
6
|
+
* - Client applications (React, etc.)
|
|
7
|
+
* - E2B sandbox communication
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// Client → Server Events (Commands)
|
|
12
|
+
// =============================================================================
|
|
13
|
+
export const ClientEventType = {
|
|
14
|
+
// Session
|
|
15
|
+
INIT: 'INIT',
|
|
16
|
+
PING: 'PING',
|
|
17
|
+
// Sandbox lifecycle
|
|
18
|
+
START_SANDBOX: 'START_SANDBOX',
|
|
19
|
+
STOP_SANDBOX: 'STOP_SANDBOX',
|
|
20
|
+
// Command execution
|
|
21
|
+
EXEC: 'EXEC',
|
|
22
|
+
// Agent
|
|
23
|
+
AGENT_MESSAGE: 'AGENT_MESSAGE',
|
|
24
|
+
AGENT_STOP: 'AGENT_STOP',
|
|
25
|
+
AGENT_RESET: 'AGENT_RESET',
|
|
26
|
+
};
|
|
27
|
+
// =============================================================================
|
|
28
|
+
// Server → Client Events
|
|
29
|
+
// =============================================================================
|
|
30
|
+
export const ServerEventType = {
|
|
31
|
+
// Connection
|
|
32
|
+
CONNECTED: 'CONNECTED',
|
|
33
|
+
READY: 'READY',
|
|
34
|
+
PONG: 'PONG',
|
|
35
|
+
// Sandbox status
|
|
36
|
+
SANDBOX_STATUS: 'SANDBOX_STATUS',
|
|
37
|
+
// Lifecycle events (granular phases)
|
|
38
|
+
LIFECYCLE: 'LIFECYCLE',
|
|
39
|
+
// Terminal
|
|
40
|
+
TERM_DATA: 'TERM_DATA',
|
|
41
|
+
EXEC_COMPLETE: 'EXEC_COMPLETE',
|
|
42
|
+
// System messages
|
|
43
|
+
SYSTEM: 'SYSTEM',
|
|
44
|
+
ERROR: 'ERROR',
|
|
45
|
+
// Agent events
|
|
46
|
+
AGENT_STARTED: 'AGENT_STARTED',
|
|
47
|
+
AGENT_MESSAGE: 'AGENT_MESSAGE',
|
|
48
|
+
AGENT_ACTION: 'AGENT_ACTION',
|
|
49
|
+
AGENT_STEP_COMPLETE: 'AGENT_STEP_COMPLETE',
|
|
50
|
+
AGENT_DONE: 'AGENT_DONE',
|
|
51
|
+
AGENT_ERROR: 'AGENT_ERROR',
|
|
52
|
+
AGENT_EVENT: 'AGENT_EVENT',
|
|
53
|
+
};
|
|
54
|
+
// =============================================================================
|
|
55
|
+
// Common Enums
|
|
56
|
+
// =============================================================================
|
|
57
|
+
export const SandboxStatus = {
|
|
58
|
+
IDLE: 'idle',
|
|
59
|
+
BOOTING: 'booting',
|
|
60
|
+
READY: 'ready',
|
|
61
|
+
ERROR: 'error',
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Granular lifecycle phases for UI loading states
|
|
65
|
+
* Common lifecycle events across different app types:
|
|
66
|
+
* - idle: No sandbox running
|
|
67
|
+
* - booting: Creating sandbox instance
|
|
68
|
+
* - environment_ready: Sandbox created, basic environment up
|
|
69
|
+
* - files_ready: App files synced/loaded (for app builders)
|
|
70
|
+
* - installing: Installing dependencies
|
|
71
|
+
* - starting: Starting services (dev server, etc.)
|
|
72
|
+
* - interaction_ready: Ready for user interaction (agent can respond)
|
|
73
|
+
* - ready: Fully ready (deprecated alias for interaction_ready)
|
|
74
|
+
* - error: Something went wrong
|
|
75
|
+
*/
|
|
76
|
+
export const SandboxLifecycle = {
|
|
77
|
+
IDLE: 'idle',
|
|
78
|
+
BOOTING: 'booting',
|
|
79
|
+
ENVIRONMENT_READY: 'environment_ready',
|
|
80
|
+
FILES_READY: 'files_ready',
|
|
81
|
+
INSTALLING: 'installing',
|
|
82
|
+
STARTING: 'starting',
|
|
83
|
+
INTERACTION_READY: 'interaction_ready',
|
|
84
|
+
READY: 'ready', // alias for backwards compat
|
|
85
|
+
ERROR: 'error',
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Parse SYSTEM message to SandboxLifecycle phase
|
|
89
|
+
*/
|
|
90
|
+
export function parseSystemMessageToLifecycle(msg) {
|
|
91
|
+
const lowerMsg = msg.toLowerCase();
|
|
92
|
+
if (lowerMsg.includes('booting'))
|
|
93
|
+
return SandboxLifecycle.BOOTING;
|
|
94
|
+
if (lowerMsg.includes('environment ready'))
|
|
95
|
+
return SandboxLifecycle.ENVIRONMENT_READY;
|
|
96
|
+
if (lowerMsg.includes('files ready') || lowerMsg.includes('files loaded') || lowerMsg.includes('restored'))
|
|
97
|
+
return SandboxLifecycle.FILES_READY;
|
|
98
|
+
if (lowerMsg.includes('installing'))
|
|
99
|
+
return SandboxLifecycle.INSTALLING;
|
|
100
|
+
if (lowerMsg.includes('starting'))
|
|
101
|
+
return SandboxLifecycle.STARTING;
|
|
102
|
+
if (lowerMsg.includes('interaction ready') || lowerMsg.includes('agent ready'))
|
|
103
|
+
return SandboxLifecycle.INTERACTION_READY;
|
|
104
|
+
if (lowerMsg.includes('ready'))
|
|
105
|
+
return SandboxLifecycle.READY;
|
|
106
|
+
if (lowerMsg.includes('error'))
|
|
107
|
+
return SandboxLifecycle.ERROR;
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
// =============================================================================
|
|
111
|
+
// Zod Schemas (Runtime Validation)
|
|
112
|
+
// =============================================================================
|
|
113
|
+
export const ToolUseBlockSchema = z.object({
|
|
114
|
+
type: z.literal('tool_use'),
|
|
115
|
+
name: z.string(),
|
|
116
|
+
input: z.record(z.unknown()).optional(),
|
|
117
|
+
});
|
|
118
|
+
export const TextBlockSchema = z.object({
|
|
119
|
+
type: z.literal('text'),
|
|
120
|
+
text: z.string(),
|
|
121
|
+
});
|
|
122
|
+
export const ContentBlockSchema = z.discriminatedUnion('type', [
|
|
123
|
+
ToolUseBlockSchema,
|
|
124
|
+
TextBlockSchema,
|
|
125
|
+
]);
|
|
126
|
+
export const AssistantMessageSchema = z.object({
|
|
127
|
+
type: z.literal('assistant'),
|
|
128
|
+
message: z.object({
|
|
129
|
+
content: z.array(ContentBlockSchema),
|
|
130
|
+
}),
|
|
131
|
+
});
|
|
132
|
+
export const ResultMessageSchema = z.object({
|
|
133
|
+
type: z.literal('result'),
|
|
134
|
+
session_id: z.string().optional(),
|
|
135
|
+
total_cost_usd: z.number().optional(),
|
|
136
|
+
duration_ms: z.number().optional(),
|
|
137
|
+
});
|
|
138
|
+
export const ErrorMessageSchema = z.object({
|
|
139
|
+
type: z.literal('error'),
|
|
140
|
+
error: z.string(),
|
|
141
|
+
});
|
|
142
|
+
export const SystemMessageSchema = z.object({
|
|
143
|
+
type: z.literal('system'),
|
|
144
|
+
message: z.string(),
|
|
145
|
+
});
|
|
146
|
+
export const AgentOutputMessageSchema = z.discriminatedUnion('type', [
|
|
147
|
+
AssistantMessageSchema,
|
|
148
|
+
ResultMessageSchema,
|
|
149
|
+
ErrorMessageSchema,
|
|
150
|
+
SystemMessageSchema,
|
|
151
|
+
]);
|
|
152
|
+
export function parseAgentOutputMessage(line) {
|
|
153
|
+
if (!line.trim())
|
|
154
|
+
return null;
|
|
155
|
+
try {
|
|
156
|
+
const json = JSON.parse(line);
|
|
157
|
+
const result = AgentOutputMessageSchema.safeParse(json);
|
|
158
|
+
return result.success ? result.data : null;
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// =============================================================================
|
|
165
|
+
// Type Guards
|
|
166
|
+
// =============================================================================
|
|
167
|
+
const CLIENT_EVENT_TYPES = new Set(Object.values(ClientEventType));
|
|
168
|
+
const SERVER_EVENT_TYPES = new Set(Object.values(ServerEventType));
|
|
169
|
+
export function isClientEvent(data) {
|
|
170
|
+
if (!data || typeof data !== 'object')
|
|
171
|
+
return false;
|
|
172
|
+
const maybeEvent = data;
|
|
173
|
+
return typeof maybeEvent.type === 'string' && CLIENT_EVENT_TYPES.has(maybeEvent.type);
|
|
174
|
+
}
|
|
175
|
+
export function isServerEvent(data) {
|
|
176
|
+
if (!data || typeof data !== 'object')
|
|
177
|
+
return false;
|
|
178
|
+
const maybeEvent = data;
|
|
179
|
+
return typeof maybeEvent.type === 'string' && SERVER_EVENT_TYPES.has(maybeEvent.type);
|
|
180
|
+
}
|
|
181
|
+
export function parseServerEvent(json) {
|
|
182
|
+
try {
|
|
183
|
+
const parsed = JSON.parse(json);
|
|
184
|
+
if (isServerEvent(parsed))
|
|
185
|
+
return parsed;
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
export function parseClientEvent(json) {
|
|
193
|
+
try {
|
|
194
|
+
const parsed = JSON.parse(json);
|
|
195
|
+
if (isClientEvent(parsed))
|
|
196
|
+
return parsed;
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=socket-events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socket-events.js","sourceRoot":"","sources":["../src/socket-events.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,gFAAgF;AAChF,oCAAoC;AACpC,gFAAgF;AAEhF,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,UAAU;IACV,IAAI,EAAE,MAAM;IACZ,IAAI,EAAE,MAAM;IAEZ,oBAAoB;IACpB,aAAa,EAAE,eAAe;IAC9B,YAAY,EAAE,cAAc;IAE5B,oBAAoB;IACpB,IAAI,EAAE,MAAM;IAEZ,QAAQ;IACR,aAAa,EAAE,eAAe;IAC9B,UAAU,EA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AC,IAAI,KAAK,QAAQ,IAAI,kBAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,IAAuB,CAAC,CAAC;AAC3G,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAChC,IAAI,aAAa,CAAC,MAAM,CAAC;YAAE,OAAO,MAAM,CAAC;QACzC,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAChC,IAAI,aAAa,CAAC,MAAM,CAAC;YAAE,OAAO,MAAM,CAAC;QACzC,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for agent-session
|
|
3
|
+
*/
|
|
4
|
+
export interface SocketAttachment {
|
|
5
|
+
clientId: string;
|
|
6
|
+
userId?: string;
|
|
7
|
+
connectedAt: number;
|
|
8
|
+
}
|
|
9
|
+
export interface SessionState {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
sandboxId?: string;
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Configuration for connecting to agent-session
|
|
16
|
+
*/
|
|
17
|
+
export interface AgentSessionConfig {
|
|
18
|
+
/** WebSocket URL of the deployed worker */
|
|
19
|
+
url: string;
|
|
20
|
+
/** Session ID for this connection */
|
|
21
|
+
sessionId: string;
|
|
22
|
+
/** Optional user ID for tracking */
|
|
23
|
+
userId?: string;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,2CAA2C;IAC3C,GAAG,EAAE,MAAM,CAAC;IACZ,qCAAqC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,oCAAoC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@steventsao/agent-session-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared types and protocol for agent-session",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"dev": "tsc --watch"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"zod": "^3.23.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.0.0"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT"
|
|
26
|
+
}
|