@aintela/chat 0.4.6 → 0.4.7

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.
@@ -11,6 +11,14 @@ export declare const APPLICATION_CLOSED_CODE = 4001;
11
11
  * close to the consumer instead.
12
12
  */
13
13
  export declare const SESSION_ALREADY_ATTACHED_CODE = 4002;
14
+ /**
15
+ * Client-local close code used when the heartbeat declares the peer dead. It never
16
+ * goes on the wire (there is nobody left to receive it); it reaches `onClose` so a
17
+ * consumer can tell a detected dead peer from a server-initiated close. Retryable:
18
+ * the inner loop resumes the session with its replay cursor when the channel was
19
+ * proven, otherwise the outer state manager takes over as for any other drop.
20
+ */
21
+ export declare const HEARTBEAT_TIMEOUT_CODE = 4100;
14
22
  export declare class WebSocketConnecionV2 implements IWebSocketConnection {
15
23
  ws: WebSocket | null;
16
24
  stats: InteractionStatistics;
@@ -66,7 +74,8 @@ export declare class WebSocketConnecionV2 implements IWebSocketConnection {
66
74
  streamingResponseSerializer: TypedJSON<StreamingResponse>;
67
75
  events: [number, number][];
68
76
  lastActivity: number;
69
- pingPongStack: number[];
77
+ /** When the outstanding heartbeat Ping went out; null while no reply is pending. */
78
+ private pingSentAt;
70
79
  firstTokenInteractions: Set<string>;
71
80
  /** Last sessionId we successfully had — used to resume on reconnect. */
72
81
  private lastKnownSessionId?;
@@ -129,13 +138,20 @@ export declare class WebSocketConnecionV2 implements IWebSocketConnection {
129
138
  * - 1000 normal closure
130
139
  * - 1008 policy violation (auth rejected)
131
140
  * - 4001 our own intentional disconnect
132
- * - 4000-4099 reserved for app-level fatal codes from the server
141
+ * - 4002 the session is attached elsewhere (another tab) — retrying just bounces
142
+ * Retried, on purpose:
143
+ * - 4003 the server's idle close ("reconnect on next interaction"): resuming is
144
+ * cheap and lossless, and while the user is present the outer loop would redial
145
+ * anyway; when they are gone the client's own idle timer closes first.
146
+ * - 4100 HEARTBEAT_TIMEOUT_CODE, our own dead-peer verdict.
133
147
  */
134
148
  private shouldAttemptReconnect;
135
149
  private cancelPendingReconnect;
136
150
  private computeBackoffDelay;
137
151
  private scheduleReconnect;
138
152
  sleep(ms: number): Promise<unknown>;
153
+ /** The session this instance believes it is on — for log lines, never for logic. */
154
+ private get sessionLabel();
139
155
  /**
140
156
  * Handle incoming messages from server
141
157
  */
@@ -177,11 +193,25 @@ export declare class WebSocketConnecionV2 implements IWebSocketConnection {
177
193
  sendMessage(message: IMessage): void;
178
194
  sendBaseMessage(message: IMessageBase): void;
179
195
  sendSuspendMessage(message: IMessage): void;
196
+ /**
197
+ * Heartbeat: keep a quiet socket alive with a Ping every PING_INTERVAL_MS, and
198
+ * declare it dead when a Ping goes PONG_TIMEOUT_MS without ANY inbound frame.
199
+ * Started by `onopen`, stopped by `onclose`/`disconnect()`.
200
+ */
180
201
  monitor(): void;
181
202
  /**
182
- * Send a heartbeat ping
203
+ * Send a heartbeat ping and start the reply clock (unless one is already running).
183
204
  */
184
205
  ping(): void;
206
+ /**
207
+ * Declare the current socket dead and run the close path NOW, without waiting
208
+ * for the browser: for a half-open peer `onclose` can lag by minutes or never
209
+ * come, and the decision has already been made. The dead socket's own onclose
210
+ * handler is invoked with a client-local code, so the reconnect-vs-handoff
211
+ * logic that runs is exactly the one a server close would run. Handlers are
212
+ * detached first so a late real close event cannot run it a second time.
213
+ */
214
+ private failSocket;
185
215
  /**
186
216
  * Disconnect the WebSocket
187
217
  */
@@ -55,5 +55,21 @@ export declare class AgentGatewayWSAsync extends AgentGatewayService {
55
55
  simulateDisconnect(code?: number, reason?: string): void;
56
56
  sleep(ms: number): Promise<unknown>;
57
57
  streamOpened: boolean;
58
+ /**
59
+ * Make sure the socket is OPEN before a send, redialling the SAME session when it is not.
60
+ *
61
+ * The socket can be down at send time for reasons the composer cannot see: the client's own
62
+ * idle close (5 min without input), the server's (6 min without a message, code 4003), a dead
63
+ * peer the heartbeat just detected, or a dial still in flight. Until this existed the send
64
+ * threw `WebSocket is not connected` AFTER the user's turn was already on the transcript — or
65
+ * the composer refused it outright — and the only way back was the 2 s heartbeat noticing
66
+ * mouse movement. A send IS the activity that reconnect was waiting for, so dial here.
67
+ *
68
+ * `resume: true` keeps the replay cursor: if the socket died mid-turn the server replays what
69
+ * we missed. `openConnection` de-duplicates against a dial already in flight (the state
70
+ * manager's retry loop, a host's own bind), so concurrent callers share one socket. A
71
+ * transport without `isConnected` semantics (the demo's fake gateway) is treated as up.
72
+ */
73
+ private ensureConnected;
58
74
  sendMessageInternal(message: ClientMessage, options?: GatewaySendMessageOptions, _openTurn?: boolean): Promise<SendMessageResponse>;
59
75
  }
@@ -15,13 +15,28 @@ export declare class FakeWebSocketConnection implements IWebSocketConnection {
15
15
  onRewindLost?: never;
16
16
  readonly recordedSends: IMessage[];
17
17
  sessionIdToReturn: string;
18
+ /** Every `connect()` call — the id and options the gateway dialled with, in order. */
19
+ readonly connectCalls: {
20
+ sessionId?: string;
21
+ options?: {
22
+ resume?: boolean;
23
+ };
24
+ }[];
18
25
  private connected;
19
- connect(_sessionId?: string): Promise<string>;
26
+ connect(sessionId?: string, options?: {
27
+ resume?: boolean;
28
+ }): Promise<string>;
20
29
  sendMessage(message: IMessage): void;
21
30
  suspendStream(): void;
22
31
  disconnect(): void;
23
32
  closeStream(): void;
24
33
  isConnected(): boolean;
34
+ /**
35
+ * The peer went away and the socket knows it — what the real transport looks like after
36
+ * an idle close, or after the heartbeat's dead-peer verdict. No callback fires: the
37
+ * consumer has (or has not) already reacted; the point is what it does at the next send.
38
+ */
39
+ drop(): void;
25
40
  /**
26
41
  * Drive an event through `onEvent` *and await it*. Use this when the
27
42
  * next assertion depends on the merger having finished (the gateway's
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aintela/chat",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"