@tangle-network/sandbox 0.11.1 → 0.12.0-develop.20260724231304.911a687

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.
@@ -40,16 +40,24 @@ interface ReplayOptions {
40
40
  lastEventId?: string;
41
41
  /** Execution ID to replay events for */
42
42
  executionId?: string;
43
- /** Session ID context for replay */
43
+ /** Actual runtime session ID used to scope replayed agent events */
44
44
  sessionId?: string;
45
45
  }
46
+ interface SubscriptionDenial {
47
+ channel: string;
48
+ reason: string;
49
+ }
50
+ interface SubscriptionAcknowledgement {
51
+ channels: string[];
52
+ denied?: SubscriptionDenial[];
53
+ }
46
54
  /**
47
55
  * Base fields present on all server messages.
48
56
  */
49
57
  interface ServerMessageBase {
50
58
  id?: string;
51
59
  timestamp?: number;
52
- /** Monotonic sequence number for gap detection (per session) */
60
+ /** Monotonic per-session sequence number for consumers to detect gaps. */
53
61
  seq?: number;
54
62
  }
55
63
  /**
@@ -57,15 +65,13 @@ interface ServerMessageBase {
57
65
  */
58
66
  type ServerMessage = (ServerMessageBase & {
59
67
  type: "connection.established";
60
- messageType?: string;
61
68
  data: {
62
69
  sessionId: string;
70
+ hydratedCount?: number;
63
71
  };
64
72
  }) | (ServerMessageBase & {
65
73
  type: "subscribed";
66
- data: {
67
- channels: string[];
68
- };
74
+ data: SubscriptionAcknowledgement;
69
75
  }) | (ServerMessageBase & {
70
76
  type: "unsubscribed";
71
77
  data: {
@@ -85,18 +91,27 @@ type ServerMessage = (ServerMessageBase & {
85
91
  data: {
86
92
  sandboxId: string;
87
93
  error?: string;
94
+ reason?: string;
88
95
  };
89
96
  }) | (ServerMessageBase & {
90
- type: "runtime.not_found";
97
+ type: "runtime.reconnecting";
91
98
  data: {
92
- sessionId: string;
93
- containerId?: string;
99
+ sandboxId: string;
100
+ attempt: number;
101
+ delayMs: number;
102
+ };
103
+ }) | (ServerMessageBase & {
104
+ type: "runtime.removed";
105
+ data: {
106
+ sandboxId: string;
94
107
  reason: string;
95
108
  };
96
109
  }) | (ServerMessageBase & {
97
- type: "container.ready";
110
+ type: "runtime.not_found";
98
111
  data: {
99
- sandboxId?: string;
112
+ sessionId: string;
113
+ containerId?: string;
114
+ reason: string;
100
115
  };
101
116
  }) | (ServerMessageBase & {
102
117
  type: "runtime.ready";
@@ -123,7 +138,6 @@ type ServerMessage = (ServerMessageBase & {
123
138
  type: "agent.event";
124
139
  channel: string;
125
140
  data: unknown;
126
- sequenceId?: number;
127
141
  }) | (ServerMessageBase & {
128
142
  type: "replay.start";
129
143
  total: number;
@@ -156,6 +170,20 @@ type ServerMessage = (ServerMessageBase & {
156
170
  totalDropped: number; /** Hint that client should request a replay */
157
171
  suggestReplay: boolean;
158
172
  };
173
+ }) | (ServerMessageBase & {
174
+ type: "terminal.input.error";
175
+ data: {
176
+ terminalId: string;
177
+ code: string;
178
+ message: string;
179
+ };
180
+ }) | (ServerMessageBase & {
181
+ type: `project.${string}`;
182
+ channel?: string;
183
+ data: unknown;
184
+ }) | (ServerMessageBase & {
185
+ type: "heartbeat";
186
+ data?: unknown;
159
187
  });
160
188
  interface PortEventData {
161
189
  type: "opened" | "closed";
@@ -163,6 +191,25 @@ interface PortEventData {
163
191
  protocol: string;
164
192
  processName?: string;
165
193
  }
194
+ /**
195
+ * Inbound WebSocket frame. `handleMessage` validates its message type before
196
+ * dispatching it to the typed session protocol.
197
+ */
198
+ interface RawWireMessage {
199
+ type?: string;
200
+ channel?: string;
201
+ data?: unknown;
202
+ id?: string;
203
+ message?: string;
204
+ code?: string;
205
+ timestamp?: number;
206
+ seq?: number;
207
+ [key: string]: unknown;
208
+ }
209
+ /** Every inbound frame after its message type has been validated. */
210
+ interface NormalizedServerMessage extends RawWireMessage {
211
+ type: string;
212
+ }
166
213
  /**
167
214
  * Connection state for the WebSocket client.
168
215
  */
@@ -215,13 +262,14 @@ interface TokenRefreshResult {
215
262
  * Event handlers for session gateway events.
216
263
  */
217
264
  interface SessionGatewayEventHandlers {
265
+ /** Called after internal processing for every non-duplicate normalized frame. */
266
+ onMessage?: (message: NormalizedServerMessage) => void;
218
267
  onConnect?: (sessionId: string) => void;
219
268
  onDisconnect?: (code: number, reason: string) => void;
220
269
  onReconnect?: () => void;
221
270
  onRuntimeConnected?: (sandboxId: string, baseUrl: string) => void;
222
271
  onRuntimeDisconnected?: (sandboxId: string, error?: string) => void;
223
272
  onRuntimeNotFound?: (sessionId: string, containerId: string | undefined, reason: string) => void;
224
- onContainerReady?: (sandboxId: string) => void;
225
273
  onRuntimeReady?: (data: {
226
274
  sandboxId: string;
227
275
  sessionId: string;
@@ -237,7 +285,7 @@ interface SessionGatewayEventHandlers {
237
285
  }) => void;
238
286
  onPortOpened?: (port: number, protocol: string, processName?: string) => void;
239
287
  onPortClosed?: (port: number) => void;
240
- onAgentEvent?: (channel: string, data: unknown, sequenceId?: number) => void;
288
+ onAgentEvent?: (channel: string, data: unknown, seq?: number) => void;
241
289
  onError?: (message: string, code?: string) => void;
242
290
  onStateChange?: (state: ConnectionState) => void;
243
291
  onMetricsChange?: (metrics: ConnectionMetrics) => void;
@@ -270,11 +318,11 @@ interface SessionGatewayClientConfig {
270
318
  token: string;
271
319
  /** Callback to refresh the token when it's about to expire */
272
320
  onTokenRefresh?: () => Promise<TokenRefreshResult>;
273
- /** Session ID to connect to */
321
+ /** Browser session ID embedded in the session-scoped read token */
274
322
  sessionId: string;
275
323
  /** Transport mode (default: "websocket") */
276
324
  transport?: TransportMode;
277
- /** Channels to subscribe to (default: ["*"]) */
325
+ /** Additional channels to subscribe to; server-authorized channels are used by default. */
278
326
  channels?: string[];
279
327
  /** Auto-reconnect on disconnect (default: true) */
280
328
  autoReconnect?: boolean;
@@ -302,6 +350,8 @@ interface SessionGatewayClientConfig {
302
350
  replayStorageKeyPrefix?: string;
303
351
  /** Number of latency samples to track for averaging (default: 10) */
304
352
  latencyHistorySize?: number;
353
+ /** Maximum wait for `connection.established` (default: 10000 ms) */
354
+ connectionEstablishmentTimeoutMs?: number;
305
355
  /** Event handlers */
306
356
  handlers?: SessionGatewayEventHandlers;
307
357
  }
@@ -335,11 +385,13 @@ declare class SessionGatewayClient {
335
385
  private ws;
336
386
  private readonly config;
337
387
  private readonly handlers;
388
+ private readonly replayStateStore;
389
+ private readonly tokenRefresher;
338
390
  private currentToken;
339
- private isRefreshingToken;
340
391
  private state;
341
392
  private connectedAt;
342
393
  private hasConnectedBefore;
394
+ private connectionEstablishedForSocket;
343
395
  private lastPingAt;
344
396
  private lastPongAt;
345
397
  private lastPingSentAt;
@@ -353,6 +405,7 @@ declare class SessionGatewayClient {
353
405
  private pingInterval;
354
406
  private pongTimeout;
355
407
  private pendingCallbacks;
408
+ private readonly connectionWaiters;
356
409
  private messageIdCounter;
357
410
  private processedEventIds;
358
411
  private pingLatencyHistory;
@@ -387,7 +440,7 @@ declare class SessionGatewayClient {
387
440
  /**
388
441
  * Request replay of events since a timestamp.
389
442
  */
390
- replay(since: number): void;
443
+ replay(since: number): Promise<void>;
391
444
  /**
392
445
  * Send terminal input via WebSocket.
393
446
  */
@@ -423,14 +476,18 @@ declare class SessionGatewayClient {
423
476
  getToken(): string;
424
477
  private setState;
425
478
  private handleOpen;
479
+ private handleConnectionEstablished;
426
480
  private handleClose;
481
+ private handleError;
482
+ private closeBeforeEstablishment;
427
483
  private handleMessage;
428
484
  private handlePong;
429
485
  private dispatchMessage;
486
+ private deliverMessage;
430
487
  private trackExecutionContext;
431
- private handleTokenExpiring;
432
488
  private send;
433
489
  private sendWithResponse;
490
+ private waitForConnectionEstablished;
434
491
  private restoreSubscriptions;
435
492
  private startPingInterval;
436
493
  private stopPingInterval;
@@ -445,4 +502,4 @@ declare class SessionGatewayClient {
445
502
  private saveReplayState;
446
503
  }
447
504
  //#endregion
448
- export { type ClientMessage, type ConnectionMetrics, type ConnectionQuality, type ConnectionState, INITIAL_METRICS, type PortEventData, type ReplayOptions, type ReplayState, type ReplayStateStorage, type ServerMessage, SessionGatewayClient, type SessionGatewayClientConfig, type SessionGatewayEventHandlers, type SessionGatewayStats, type TokenRefreshResult, type TransportMode, calculateConnectionQuality };
505
+ export { type ClientMessage, type ConnectionMetrics, type ConnectionQuality, type ConnectionState, INITIAL_METRICS, type NormalizedServerMessage, type PortEventData, type ReplayOptions, type ReplayState, type ReplayStateStorage, type ServerMessage, SessionGatewayClient, type SessionGatewayClientConfig, type SessionGatewayEventHandlers, type SessionGatewayStats, type SubscriptionAcknowledgement, type SubscriptionDenial, type TokenRefreshResult, type TransportMode, calculateConnectionQuality };