@steventsao/agent-session-core 0.1.11 → 0.1.13

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.js CHANGED
@@ -1,10 +1,226 @@
1
- /**
2
- * @agent-session/core
3
- *
4
- * Shared types and protocol for agent-session.
5
- * Used by both client and server packages.
6
- */
7
- export * from './socket-events';
8
- export * from './types';
9
- export * from './session-config';
10
- //# sourceMappingURL=index.js.map
1
+ // src/socket-events.ts
2
+ import { z } from "zod";
3
+ var ClientEventType = {
4
+ // Session
5
+ INIT: "INIT",
6
+ PING: "PING",
7
+ JOIN_SESSION: "JOIN_SESSION",
8
+ // Sandbox lifecycle
9
+ START_SANDBOX: "START_SANDBOX",
10
+ STOP_SANDBOX: "STOP_SANDBOX",
11
+ // Command execution
12
+ EXEC: "EXEC",
13
+ // Agent
14
+ AGENT_MESSAGE: "AGENT_MESSAGE",
15
+ AGENT_STOP: "AGENT_STOP",
16
+ AGENT_RESET: "AGENT_RESET"
17
+ };
18
+ var ServerEventType = {
19
+ // Connection
20
+ CONNECTED: "CONNECTED",
21
+ READY: "READY",
22
+ PONG: "PONG",
23
+ MESSAGE_ACK: "MESSAGE_ACK",
24
+ MESSAGE_ERROR: "MESSAGE_ERROR",
25
+ // Event resumption (Manus-style)
26
+ EVENTS_BATCH: "EVENTS_BATCH",
27
+ // Sandbox status
28
+ SANDBOX_STATUS: "SANDBOX_STATUS",
29
+ // Lifecycle events (granular phases)
30
+ LIFECYCLE: "LIFECYCLE",
31
+ // Terminal
32
+ TERM_DATA: "TERM_DATA",
33
+ EXEC_COMPLETE: "EXEC_COMPLETE",
34
+ // System messages
35
+ SYSTEM: "SYSTEM",
36
+ ERROR: "ERROR",
37
+ // Agent events
38
+ AGENT_STARTED: "AGENT_STARTED",
39
+ AGENT_MESSAGE: "AGENT_MESSAGE",
40
+ AGENT_ACTION: "AGENT_ACTION",
41
+ AGENT_STEP_COMPLETE: "AGENT_STEP_COMPLETE",
42
+ AGENT_DONE: "AGENT_DONE",
43
+ AGENT_ERROR: "AGENT_ERROR",
44
+ AGENT_EVENT: "AGENT_EVENT",
45
+ FILE_UPLOADED: "FILE_UPLOADED",
46
+ FILES_SYNC: "FILES_SYNC",
47
+ // TOC extraction events
48
+ TOC_REQUEST_RECEIVED: "TOC_REQUEST_RECEIVED",
49
+ TOC_AUTH_COMPLETED: "TOC_AUTH_COMPLETED",
50
+ TOC_DB_QUERY_STARTED: "TOC_DB_QUERY_STARTED",
51
+ TOC_DB_QUERY_COMPLETED: "TOC_DB_QUERY_COMPLETED",
52
+ TOC_GCS_DOWNLOAD_STARTED: "TOC_GCS_DOWNLOAD_STARTED",
53
+ TOC_GCS_DOWNLOAD_COMPLETED: "TOC_GCS_DOWNLOAD_COMPLETED",
54
+ TOC_SANDBOX_CREATED: "TOC_SANDBOX_CREATED",
55
+ TOC_EXTRACTION_STARTED: "TOC_EXTRACTION_STARTED",
56
+ TOC_EXTRACTION_COMPLETED: "TOC_EXTRACTION_COMPLETED",
57
+ TOC_RESPONSE_READY: "TOC_RESPONSE_READY"
58
+ };
59
+ var SandboxStatus = {
60
+ IDLE: "idle",
61
+ BOOTING: "booting",
62
+ READY: "ready",
63
+ ERROR: "error"
64
+ };
65
+ var SandboxLifecycle = {
66
+ IDLE: "idle",
67
+ BOOTING: "booting",
68
+ ENVIRONMENT_READY: "environment_ready",
69
+ FILES_READY: "files_ready",
70
+ INSTALLING: "installing",
71
+ STARTING: "starting",
72
+ INTERACTION_READY: "interaction_ready",
73
+ READY: "ready",
74
+ // alias for backwards compat
75
+ ERROR: "error"
76
+ };
77
+ function parseSystemMessageToLifecycle(msg) {
78
+ const lowerMsg = msg.toLowerCase();
79
+ if (lowerMsg.includes("booting")) return SandboxLifecycle.BOOTING;
80
+ if (lowerMsg.includes("environment ready")) return SandboxLifecycle.ENVIRONMENT_READY;
81
+ if (lowerMsg.includes("files ready") || lowerMsg.includes("files loaded") || lowerMsg.includes("restored")) return SandboxLifecycle.FILES_READY;
82
+ if (lowerMsg.includes("installing")) return SandboxLifecycle.INSTALLING;
83
+ if (lowerMsg.includes("starting")) return SandboxLifecycle.STARTING;
84
+ if (lowerMsg.includes("interaction ready") || lowerMsg.includes("agent ready")) return SandboxLifecycle.INTERACTION_READY;
85
+ if (lowerMsg.includes("ready")) return SandboxLifecycle.READY;
86
+ if (lowerMsg.includes("error")) return SandboxLifecycle.ERROR;
87
+ return null;
88
+ }
89
+ var EPHEMERAL_EVENT_TYPES = /* @__PURE__ */ new Set([
90
+ "CONNECTED",
91
+ "READY",
92
+ "PONG",
93
+ "MESSAGE_ACK",
94
+ "MESSAGE_ERROR",
95
+ "TERM_DATA",
96
+ "EXEC_COMPLETE",
97
+ "EVENTS_BATCH",
98
+ "AGENT_STEP_COMPLETE",
99
+ "AGENT_EVENT",
100
+ "ERROR"
101
+ ]);
102
+ function isPersistableEvent(event) {
103
+ return !EPHEMERAL_EVENT_TYPES.has(event.type);
104
+ }
105
+ var ToolUseBlockSchema = z.object({
106
+ type: z.literal("tool_use"),
107
+ name: z.string(),
108
+ input: z.record(z.unknown()).optional()
109
+ });
110
+ var TextBlockSchema = z.object({
111
+ type: z.literal("text"),
112
+ text: z.string()
113
+ });
114
+ var ContentBlockSchema = z.discriminatedUnion("type", [
115
+ ToolUseBlockSchema,
116
+ TextBlockSchema
117
+ ]);
118
+ var AssistantMessageSchema = z.object({
119
+ type: z.literal("assistant"),
120
+ message: z.object({
121
+ content: z.array(ContentBlockSchema)
122
+ })
123
+ });
124
+ var ResultMessageSchema = z.object({
125
+ type: z.literal("result"),
126
+ session_id: z.string().optional(),
127
+ total_cost_usd: z.number().optional(),
128
+ duration_ms: z.number().optional()
129
+ });
130
+ var ErrorMessageSchema = z.object({
131
+ type: z.literal("error"),
132
+ error: z.string()
133
+ });
134
+ var SystemMessageSchema = z.object({
135
+ type: z.literal("system"),
136
+ message: z.string()
137
+ });
138
+ var AgentOutputMessageSchema = z.discriminatedUnion("type", [
139
+ AssistantMessageSchema,
140
+ ResultMessageSchema,
141
+ ErrorMessageSchema,
142
+ SystemMessageSchema
143
+ ]);
144
+ function parseAgentOutputMessage(line) {
145
+ if (!line.trim()) return null;
146
+ try {
147
+ const json = JSON.parse(line);
148
+ const result = AgentOutputMessageSchema.safeParse(json);
149
+ return result.success ? result.data : null;
150
+ } catch {
151
+ return null;
152
+ }
153
+ }
154
+ var CLIENT_EVENT_TYPES = new Set(Object.values(ClientEventType));
155
+ var SERVER_EVENT_TYPES = new Set(Object.values(ServerEventType));
156
+ function isClientEvent(data) {
157
+ if (!data || typeof data !== "object") return false;
158
+ const maybeEvent = data;
159
+ return typeof maybeEvent.type === "string" && CLIENT_EVENT_TYPES.has(maybeEvent.type);
160
+ }
161
+ function isServerEvent(data) {
162
+ if (!data || typeof data !== "object") return false;
163
+ const maybeEvent = data;
164
+ return typeof maybeEvent.type === "string" && SERVER_EVENT_TYPES.has(maybeEvent.type);
165
+ }
166
+ function parseServerEvent(json) {
167
+ try {
168
+ const parsed = JSON.parse(json);
169
+ if (isServerEvent(parsed)) return parsed;
170
+ return null;
171
+ } catch {
172
+ return null;
173
+ }
174
+ }
175
+ function parseClientEvent(json) {
176
+ try {
177
+ const parsed = JSON.parse(json);
178
+ if (isClientEvent(parsed)) return parsed;
179
+ return null;
180
+ } catch {
181
+ return null;
182
+ }
183
+ }
184
+
185
+ // src/session-config.ts
186
+ var DEFAULT_SESSION_REFRESH_CONFIG = {
187
+ onSessionChange: "reset",
188
+ clearMessagesOnReset: true,
189
+ clearAgentSessionOnReset: true
190
+ };
191
+ var PRESERVE_SESSION_CONFIG = {
192
+ onSessionChange: "preserve",
193
+ clearMessagesOnReset: false,
194
+ clearAgentSessionOnReset: false
195
+ };
196
+ function shouldResetOnSessionChange(config, oldSessionId, newSessionId) {
197
+ if (config.onSessionChange === "preserve") {
198
+ return false;
199
+ }
200
+ return oldSessionId !== newSessionId && oldSessionId !== null && newSessionId !== null;
201
+ }
202
+ export {
203
+ AgentOutputMessageSchema,
204
+ AssistantMessageSchema,
205
+ ClientEventType,
206
+ ContentBlockSchema,
207
+ DEFAULT_SESSION_REFRESH_CONFIG,
208
+ EPHEMERAL_EVENT_TYPES,
209
+ ErrorMessageSchema,
210
+ PRESERVE_SESSION_CONFIG,
211
+ ResultMessageSchema,
212
+ SandboxLifecycle,
213
+ SandboxStatus,
214
+ ServerEventType,
215
+ SystemMessageSchema,
216
+ TextBlockSchema,
217
+ ToolUseBlockSchema,
218
+ isClientEvent,
219
+ isPersistableEvent,
220
+ isServerEvent,
221
+ parseAgentOutputMessage,
222
+ parseClientEvent,
223
+ parseServerEvent,
224
+ parseSystemMessageToLifecycle,
225
+ shouldResetOnSessionChange
226
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steventsao/agent-session-core",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Shared types and protocol for agent-session",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -15,8 +15,8 @@
15
15
  "dist"
16
16
  ],
17
17
  "scripts": {
18
- "build": "tsc",
19
- "dev": "tsc --watch"
18
+ "build": "tsup",
19
+ "dev": "tsup --watch"
20
20
  },
21
21
  "dependencies": {
22
22
  "zod": "^3.23.0"
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC"}
@@ -1,55 +0,0 @@
1
- /**
2
- * Session Refresh Configuration
3
- *
4
- * Defines how the client should behave when the sessionId changes.
5
- * This is critical for document isolation in multi-document applications.
6
- */
7
- /**
8
- * Refresh strategies for session changes
9
- *
10
- * - 'reset': Clear all client state and connect fresh (recommended for document isolation)
11
- * - 'preserve': Attempt to reconnect to existing sandbox (for tab persistence scenarios)
12
- */
13
- export type SessionRefreshStrategy = 'reset' | 'preserve';
14
- export interface SessionRefreshConfig {
15
- /**
16
- * What to do when sessionId changes:
17
- *
18
- * 'reset' (default, recommended):
19
- * - Clear sandboxId, sandboxUrl, messages, agentSessionId
20
- * - Disconnect from old socket
21
- * - Connect to new session with clean slate
22
- * - Use this when sessionId represents a document UUID
23
- *
24
- * 'preserve':
25
- * - Keep existing state during reconnection
26
- * - Useful for browser tab persistence where same user returns
27
- * - NOT recommended when switching between documents
28
- */
29
- onSessionChange: SessionRefreshStrategy;
30
- /**
31
- * Whether to clear chat messages when session changes (only applies to 'reset' strategy)
32
- * Default: true
33
- */
34
- clearMessagesOnReset?: boolean;
35
- /**
36
- * Whether to clear agent session ID when session changes (only applies to 'reset' strategy)
37
- * Default: true
38
- */
39
- clearAgentSessionOnReset?: boolean;
40
- }
41
- /**
42
- * Default configuration - full reset on session change
43
- * This is the safest option for document isolation
44
- */
45
- export declare const DEFAULT_SESSION_REFRESH_CONFIG: SessionRefreshConfig;
46
- /**
47
- * Preserve configuration - for tab persistence scenarios
48
- * Use with caution - only when you know the user is returning to the same document
49
- */
50
- export declare const PRESERVE_SESSION_CONFIG: SessionRefreshConfig;
51
- /**
52
- * Helper to determine if state should be reset based on config
53
- */
54
- export declare function shouldResetOnSessionChange(config: SessionRefreshConfig, oldSessionId: string | null, newSessionId: string | null): boolean;
55
- //# sourceMappingURL=session-config.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-config.d.ts","sourceRoot":"","sources":["../src/session-config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,sBAAsB,GAAG,OAAO,GAAG,UAAU,CAAC;AAE1D,MAAM,WAAW,oBAAoB;IACnC;;;;;;;;;;;;;OAaG;IACH,eAAe,EAAE,sBAAsB,CAAC;IAExC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAE/B;;;OAGG;IACH,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACpC;AAED;;;GAGG;AACH,eAAO,MAAM,8BAA8B,EAAE,oBAI5C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,oBAIrC,CAAC;AAEF;;GAEG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,oBAAoB,EAC5B,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,YAAY,EAAE,MAAM,GAAG,IAAI,GAC1B,OAAO,CAOT"}
@@ -1,35 +0,0 @@
1
- /**
2
- * Session Refresh Configuration
3
- *
4
- * Defines how the client should behave when the sessionId changes.
5
- * This is critical for document isolation in multi-document applications.
6
- */
7
- /**
8
- * Default configuration - full reset on session change
9
- * This is the safest option for document isolation
10
- */
11
- export const DEFAULT_SESSION_REFRESH_CONFIG = {
12
- onSessionChange: 'reset',
13
- clearMessagesOnReset: true,
14
- clearAgentSessionOnReset: true,
15
- };
16
- /**
17
- * Preserve configuration - for tab persistence scenarios
18
- * Use with caution - only when you know the user is returning to the same document
19
- */
20
- export const PRESERVE_SESSION_CONFIG = {
21
- onSessionChange: 'preserve',
22
- clearMessagesOnReset: false,
23
- clearAgentSessionOnReset: false,
24
- };
25
- /**
26
- * Helper to determine if state should be reset based on config
27
- */
28
- export function shouldResetOnSessionChange(config, oldSessionId, newSessionId) {
29
- if (config.onSessionChange === 'preserve') {
30
- return false;
31
- }
32
- // Reset if sessions are different
33
- return oldSessionId !== newSessionId && oldSessionId !== null && newSessionId !== null;
34
- }
35
- //# sourceMappingURL=session-config.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-config.js","sourceRoot":"","sources":["../src/session-config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAwCH;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAyB;IAClE,eAAe,EAAE,OAAO;IACxB,oBAAoB,EAAE,IAAI;IAC1B,wBAAwB,EAAE,IAAI;CAC/B,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAyB;IAC3D,eAAe,EAAE,UAAU;IAC3B,oBAAoB,EAAE,KAAK;IAC3B,wBAAwB,EAAE,KAAK;CAChC,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACxC,MAA4B,EAC5B,YAA2B,EAC3B,YAA2B;IAE3B,IAAI,MAAM,CAAC,eAAe,KAAK,UAAU,EAAE,CAAC;QAC1C,OAAO,KAAK,CAAC;IACf,CAAC;IAED,kCAAkC;IAClC,OAAO,YAAY,KAAK,YAAY,IAAI,YAAY,KAAK,IAAI,IAAI,YAAY,KAAK,IAAI,CAAC;AACzF,CAAC"}