@tangle-network/sandbox 0.58.4 → 0.58.5

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.
@@ -1,9 +1,9 @@
1
- //#region src/session-gateway/types.d.ts
1
+ //#region ../../../packages/runtime-contracts/dist/session-gateway.d.ts
2
2
  /**
3
- * SessionGatewayClient Types
3
+ * Shared session-gateway protocol and connection metrics
4
4
  *
5
- * Type definitions for WebSocket-based session gateway communication.
6
- * Browser-safe: no Node.js APIs.
5
+ * Browser-safe wire contracts. SDK-specific extensions and connection defaults
6
+ * remain with their clients; this module imports no runtime or Node services.
7
7
  */
8
8
  /**
9
9
  * Message types sent by the client.
@@ -45,24 +45,16 @@ interface ReplayOptions {
45
45
  lastEventId?: string;
46
46
  /** Execution ID to replay events for */
47
47
  executionId?: string;
48
- /** Actual runtime session ID used to scope replayed agent events */
48
+ /** Session ID context for replay */
49
49
  sessionId?: string;
50
50
  }
51
- interface SubscriptionDenial {
52
- channel: string;
53
- reason: string;
54
- }
55
- interface SubscriptionAcknowledgement {
56
- channels: string[];
57
- denied?: SubscriptionDenial[];
58
- }
59
51
  /**
60
52
  * Base fields present on all server messages.
61
53
  */
62
54
  interface ServerMessageBase {
63
55
  id?: string;
64
56
  timestamp?: number;
65
- /** Monotonic per-session sequence number for consumers to detect gaps. */
57
+ /** Monotonic sequence number for gap detection (per session) */
66
58
  seq?: number;
67
59
  }
68
60
  type RuntimeUnavailableReason = "container_mismatch" | "incarnation_transitioning" | "project_provisioning" | "project_degraded" | "project_suspended" | "project_failed" | "project_deprovisioning" | "project_restarting" | "sidecar_starting" | "sidecar_stopped" | "sidecar_suspended";
@@ -74,15 +66,16 @@ interface RuntimeUnavailableData {
74
66
  /**
75
67
  * Message types sent by the server.
76
68
  */
77
- type ServerMessage = (ServerMessageBase & {
69
+ type ServerMessage$1 = (ServerMessageBase & {
78
70
  type: "connection.established";
79
71
  data: {
80
72
  sessionId: string;
81
- hydratedCount?: number;
82
73
  };
83
74
  }) | (ServerMessageBase & {
84
75
  type: "subscribed";
85
- data: SubscriptionAcknowledgement;
76
+ data: {
77
+ channels: string[];
78
+ };
86
79
  }) | (ServerMessageBase & {
87
80
  type: "unsubscribed";
88
81
  data: {
@@ -203,20 +196,6 @@ type ServerMessage = (ServerMessageBase & {
203
196
  totalDropped: number; /** Hint that client should request a replay */
204
197
  suggestReplay: boolean;
205
198
  };
206
- }) | (ServerMessageBase & {
207
- type: "terminal.input.error";
208
- data: {
209
- terminalId: string;
210
- code: string;
211
- message: string;
212
- };
213
- }) | (ServerMessageBase & {
214
- type: `project.${string}`;
215
- channel?: string;
216
- data: unknown;
217
- }) | (ServerMessageBase & {
218
- type: "heartbeat";
219
- data?: unknown;
220
199
  });
221
200
  interface PortEventData {
222
201
  type: "opened" | "closed";
@@ -224,31 +203,16 @@ interface PortEventData {
224
203
  protocol: string;
225
204
  processName?: string;
226
205
  }
227
- /**
228
- * Inbound WebSocket frame. `handleMessage` validates its message type before
229
- * dispatching it to the typed session protocol.
230
- */
231
- interface RawWireMessage {
232
- type?: string;
233
- channel?: string;
234
- data?: unknown;
235
- id?: string;
236
- message?: string;
237
- code?: string;
238
- timestamp?: number;
239
- seq?: number;
240
- [key: string]: unknown;
241
- }
242
- /** Every inbound frame after its message type has been validated. */
243
- interface NormalizedServerMessage extends RawWireMessage {
244
- type: string;
245
- }
246
206
  /**
247
207
  * Connection state for the WebSocket client.
248
208
  */
249
209
  type ConnectionState = "disconnected" | "connecting" | "connected" | "reconnecting";
250
210
  /**
251
211
  * Connection quality levels based on latency, missed pongs, and reconnection frequency.
212
+ *
213
+ * Current policy: disconnected first; repeated misses/reconnects are poor;
214
+ * one missed pong, >1000 ms latency, or a recent reconnect are degraded.
215
+ * A fresh <100 ms connection is excellent; remaining connected cases are good.
252
216
  */
253
217
  type ConnectionQuality = "excellent" | "good" | "degraded" | "poor" | "disconnected";
254
218
  /**
@@ -294,16 +258,22 @@ interface TokenRefreshResult {
294
258
  /**
295
259
  * Event handlers for session gateway events.
296
260
  */
297
- interface SessionGatewayEventHandlers {
298
- /** Called after internal processing for every non-duplicate normalized frame. */
299
- onMessage?: (message: NormalizedServerMessage) => void;
261
+ interface SessionGatewayEventHandlers$1 {
300
262
  onConnect?: (sessionId: string) => void;
301
263
  onDisconnect?: (code: number, reason: string) => void;
302
264
  onReconnect?: () => void;
303
265
  onRuntimeConnected?: (sandboxId: string, baseUrl: string) => void;
304
266
  onRuntimeDisconnected?: (sandboxId: string, error?: string) => void;
305
267
  onRuntimeUnavailable?: (data: RuntimeUnavailableData) => void;
268
+ /**
269
+ * Called when no running container is found for the session.
270
+ * This typically happens when a user returns after their container has expired.
271
+ * The client should trigger reprovisioning or prompt the user.
272
+ */
306
273
  onRuntimeNotFound?: (sessionId: string, containerId: string | undefined, reason: string) => void;
274
+ /**
275
+ * Called when a runtime is fully ready with rich metadata.
276
+ */
307
277
  onRuntimeReady?: (data: {
308
278
  sandboxId: string;
309
279
  sessionId: string;
@@ -351,12 +321,29 @@ interface SessionGatewayEventHandlers {
351
321
  sandboxId?: string;
352
322
  error?: string;
353
323
  }) => void;
324
+ /**
325
+ * Called when the token is about to expire.
326
+ * The client should call refreshToken() or disconnect.
327
+ * @param secondsRemaining - Seconds until token expires
328
+ */
354
329
  onTokenExpiring?: (secondsRemaining: number) => void;
330
+ /**
331
+ * Called when the token has expired.
332
+ * The client will be disconnected shortly after this event.
333
+ */
355
334
  onTokenExpired?: () => void;
335
+ /**
336
+ * Called when events have been dropped due to WebSocket backpressure.
337
+ * The client should consider requesting a replay to catch up on missed events.
338
+ * @param droppedCount - Number of events dropped since last notification
339
+ * @param since - Timestamp when the notification window started
340
+ * @param totalDropped - Total events dropped in this session
341
+ */
356
342
  onBackpressureWarning?: (droppedCount: number, since: number, totalDropped: number) => void;
357
343
  }
358
344
  /**
359
345
  * Storage interface for replay state persistence.
346
+ * Allows custom storage implementations (localStorage, sessionStorage, memory, etc.)
360
347
  */
361
348
  interface ReplayStateStorage {
362
349
  getItem(key: string): string | null;
@@ -365,23 +352,34 @@ interface ReplayStateStorage {
365
352
  }
366
353
  /**
367
354
  * Transport mode for event delivery.
355
+ *
356
+ * - "websocket": WebSocket mode - all events via WebSocket (default)
368
357
  */
369
358
  type TransportMode = "websocket";
370
359
  /**
371
360
  * Configuration for SessionGatewayClient.
372
361
  */
373
- interface SessionGatewayClientConfig {
362
+ interface SessionGatewayClientConfig$1 {
374
363
  /** WebSocket URL for the session gateway */
375
364
  url: string;
376
- /** JWT read token for authentication (required) */
365
+ /**
366
+ * JWT read token for authentication (required).
367
+ * Obtain from product backend's session creation endpoint.
368
+ */
377
369
  token: string;
378
- /** Callback to refresh the token when it's about to expire */
370
+ /**
371
+ * Callback to refresh the token when it's about to expire.
372
+ * Called when server sends token.expiring event.
373
+ * Return new token and expiry, or throw to disconnect.
374
+ */
379
375
  onTokenRefresh?: () => Promise<TokenRefreshResult>;
380
- /** Browser session ID embedded in the session-scoped read token */
376
+ /** Session ID to connect to */
381
377
  sessionId: string;
382
- /** Transport mode (default: "websocket") */
378
+ /**
379
+ * Transport mode for event delivery (default: "websocket").
380
+ */
383
381
  transport?: TransportMode;
384
- /** Additional channels to subscribe to; server-authorized channels are used by default. */
382
+ /** Channels to subscribe to (default: ["*"]) */
385
383
  channels?: string[];
386
384
  /** Auto-reconnect on disconnect (default: true) */
387
385
  autoReconnect?: boolean;
@@ -403,16 +401,14 @@ interface SessionGatewayClientConfig {
403
401
  maxDeduplicationSize?: number;
404
402
  /** Enable replay state persistence (default: false) */
405
403
  enableReplayPersistence?: boolean;
406
- /** Storage for replay state persistence */
404
+ /** Storage for replay state persistence (default: localStorage if available) */
407
405
  replayStorage?: ReplayStateStorage;
408
406
  /** Storage key prefix for replay state (default: "session_gateway_") */
409
407
  replayStorageKeyPrefix?: string;
410
408
  /** Number of latency samples to track for averaging (default: 10) */
411
409
  latencyHistorySize?: number;
412
- /** Maximum wait for `connection.established` (default: 10000 ms) */
413
- connectionEstablishmentTimeoutMs?: number;
414
410
  /** Event handlers */
415
- handlers?: SessionGatewayEventHandlers;
411
+ handlers?: SessionGatewayEventHandlers$1;
416
412
  }
417
413
  /**
418
414
  * Statistics for the session gateway connection.
@@ -430,15 +426,77 @@ interface SessionGatewayStats {
430
426
  metrics: ConnectionMetrics;
431
427
  replay: ReplayState;
432
428
  }
433
- /**
434
- * Default connection metrics.
435
- */
436
- declare const INITIAL_METRICS: ConnectionMetrics;
437
429
  /**
438
430
  * Calculate connection quality from metrics.
439
431
  */
440
432
  declare function calculateConnectionQuality(metrics: ConnectionMetrics, isConnected: boolean): ConnectionQuality;
441
433
  //#endregion
434
+ //#region src/session-gateway/types.d.ts
435
+ interface SubscriptionDenial {
436
+ channel: string;
437
+ reason: string;
438
+ }
439
+ interface SubscriptionAcknowledgement {
440
+ channels: string[];
441
+ denied?: SubscriptionDenial[];
442
+ }
443
+ type ServerMessage = Exclude<ServerMessage$1, {
444
+ type: "connection.established" | "subscribed";
445
+ }> | (ServerMessageBase & {
446
+ type: "connection.established";
447
+ data: {
448
+ sessionId: string;
449
+ hydratedCount?: number;
450
+ };
451
+ }) | (ServerMessageBase & {
452
+ type: "subscribed";
453
+ data: SubscriptionAcknowledgement;
454
+ }) | (ServerMessageBase & {
455
+ type: "terminal.input.error";
456
+ data: {
457
+ terminalId: string;
458
+ code: string;
459
+ message: string;
460
+ };
461
+ }) | (ServerMessageBase & {
462
+ type: `project.${string}`;
463
+ channel?: string;
464
+ data: unknown;
465
+ }) | (ServerMessageBase & {
466
+ type: "heartbeat";
467
+ data?: unknown;
468
+ });
469
+ /** Untrusted inbound frame; the client validates its type before dispatch. */
470
+ interface RawWireMessage {
471
+ type?: string;
472
+ channel?: string;
473
+ data?: unknown;
474
+ id?: string;
475
+ message?: string;
476
+ code?: string;
477
+ timestamp?: number;
478
+ seq?: number;
479
+ [key: string]: unknown;
480
+ }
481
+ /** Every inbound frame after its message type has been validated. */
482
+ interface NormalizedServerMessage extends RawWireMessage {
483
+ type: string;
484
+ }
485
+ interface SessionGatewayEventHandlers extends SessionGatewayEventHandlers$1 {
486
+ /** Called after internal processing for every non-duplicate normalized frame. */
487
+ onMessage?: (message: NormalizedServerMessage) => void;
488
+ }
489
+ interface SessionGatewayClientConfig extends Omit<SessionGatewayClientConfig$1, "handlers"> {
490
+ /** Browser session ID embedded in the session-scoped read token. */
491
+ sessionId: string;
492
+ /** Additional subscriptions; server-authorized channels are used by default. */
493
+ channels?: string[];
494
+ /** Maximum wait for `connection.established` (default: 10000 ms). */
495
+ connectionEstablishmentTimeoutMs?: number;
496
+ handlers?: SessionGatewayEventHandlers;
497
+ }
498
+ declare const INITIAL_METRICS: ConnectionMetrics;
499
+ //#endregion
442
500
  //#region src/session-gateway/client.d.ts
443
501
  declare class SessionGatewayClient {
444
502
  private ws;
@@ -209,19 +209,27 @@ var TokenRefresher = class {
209
209
  }
210
210
  };
211
211
  //#endregion
212
- //#region src/session-gateway/types.ts
212
+ //#region ../../../packages/runtime-contracts/dist/session-gateway.js
213
+ /**
214
+ * Shared session-gateway protocol and connection metrics
215
+ *
216
+ * Browser-safe wire contracts. SDK-specific extensions and connection defaults
217
+ * remain with their clients; this module imports no runtime or Node services.
218
+ */
213
219
  /**
214
220
  * Default connection metrics.
215
221
  */
216
- const INITIAL_METRICS = {
217
- lastPingLatencyMs: null,
218
- avgPingLatencyMs: null,
219
- missedPongCount: 0,
220
- reconnectCount: 0,
221
- lastPongAt: null,
222
- lastReconnectAt: null,
223
- quality: "disconnected"
224
- };
222
+ function createConnectionMetrics() {
223
+ return {
224
+ lastPingLatencyMs: null,
225
+ avgPingLatencyMs: null,
226
+ missedPongCount: 0,
227
+ reconnectCount: 0,
228
+ lastPongAt: null,
229
+ lastReconnectAt: null,
230
+ quality: "disconnected"
231
+ };
232
+ }
225
233
  /**
226
234
  * Calculate connection quality from metrics.
227
235
  */
@@ -236,6 +244,10 @@ function calculateConnectionQuality(metrics, isConnected) {
236
244
  return "good";
237
245
  }
238
246
  //#endregion
247
+ //#region src/session-gateway/types.ts
248
+ /** Browser-safe shared contracts plus the public Sandbox gateway extensions. */
249
+ const INITIAL_METRICS = createConnectionMetrics();
250
+ //#endregion
239
251
  //#region src/session-gateway/client.ts
240
252
  const CONNECTION_ESTABLISHMENT_TIMEOUT_CODE = 4008;
241
253
  const CONNECTION_ERROR_CODE = 4009;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/sandbox",
3
- "version": "0.58.4",
3
+ "version": "0.58.5",
4
4
  "description": "Client SDK for the Tangle Sandbox platform - build AI agent applications with dev containers",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -147,10 +147,10 @@
147
147
  "vitest": "^4.1.5",
148
148
  "ws": "^8.20.0",
149
149
  "yjs": "13.6.30",
150
- "@tangle-network/agent-profile-materialize": "0.20.5",
151
150
  "@repo/shared": "0.0.0",
152
- "@tangle-network/runtime-contracts": "0.10.1",
151
+ "@tangle-network/agent-profile-materialize": "0.20.5",
153
152
  "@tangle-network/cli-agent-registry": "0.4.15",
153
+ "@tangle-network/runtime-contracts": "0.10.1",
154
154
  "@tangle-network/sse-core": "0.0.0"
155
155
  },
156
156
  "scripts": {