@celljs/ta-client 4.9.1 → 4.11.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.
@@ -1,9 +1,36 @@
1
- import { type TaAccessServer, type TaAuthEvent, type TaAuthServer, TaErrorCode, type TaEventEnvelope, type TaGatewayServer, type TaPackageServer, type TaRelayServer, type TaRemoteEvent, type TaRemoteServer, type TaServer, type TaSettingsServer, type TaTerminalServer, type TaWorkspaceServer } from '@celljs/ta-protocol';
2
- /** A failed call. `code` is set for failures the server names. */
1
+ import { type TaAccessServer, type TaAuthEvent, type TaAuthServer, TaErrorCode, type TaErrorData, type TaEventEnvelope, type TaGatewayServer, type TaPackageServer, type TaRelayServer, type TaRemoteEvent, type TaRemoteServer, type TaRoomEvent, type TaRoomServer, type TaServer, type TaSessionInfo, type TaSettingsServer, type TaTerminalServer, type TaWorkspaceServer } from '@celljs/ta-protocol';
2
+ /**
3
+ * A failed call. `code` is set for failures the server names; `rpcCode` is the code of JSON-RPC's error,
4
+ * by which a client tells a method an older ta serve does not have (`TaRemoteError.METHOD_NOT_FOUND`).
5
+ */
3
6
  export declare class TaRemoteError extends Error {
4
7
  readonly code?: TaErrorCode | undefined;
5
8
  readonly hint?: string | undefined;
6
- constructor(message: string, code?: TaErrorCode | undefined, hint?: string | undefined);
9
+ readonly rpcCode?: number | undefined;
10
+ /** JSON-RPC's code for a method the server does not have. */
11
+ static readonly METHOD_NOT_FOUND = -32601;
12
+ constructor(message: string, code?: TaErrorCode | undefined, hint?: string | undefined, rpcCode?: number | undefined);
13
+ /** The error of a JSON-RPC answer: its code, and the code and hint of ta serve when it named the failure. */
14
+ static of(error: {
15
+ message: string;
16
+ code?: number;
17
+ data?: {
18
+ data?: TaErrorData;
19
+ };
20
+ }): TaRemoteError;
21
+ /** What a call says that waited for a connection that did not come back. */
22
+ static readonly NOT_CONNECTED = "Not connected to ta serve.";
23
+ /** What a call says that was sent on a connection lost before its answer. */
24
+ static readonly LOST = "The connection to ta serve was lost.";
25
+ /** A call that waited for a connection that did not come back. */
26
+ static notConnected(): TaRemoteError;
27
+ /**
28
+ * Whether a failure is only that the connection was lost, which mends itself: by its code, or by its
29
+ * words in a text (a web extension shows `error.message`, often inside its own words).
30
+ */
31
+ static isConnectionLost(cause: unknown): boolean;
32
+ /** Whether the server has no such method: a ta serve from before it was added. */
33
+ static isMethodNotFound(error: unknown): boolean;
7
34
  }
8
35
  export interface TaConnectionOptions {
9
36
  /** The `url` of serve.json. */
@@ -19,6 +46,18 @@ export interface TaConnectionOptions {
19
46
  * that a short drop goes unnoticed. Default 5000; 0 fails at once.
20
47
  */
21
48
  waitForOpenMs?: number;
49
+ /**
50
+ * How long a call that is safe to send again (`TaConnection.resendable`: a read, a message with its
51
+ * clientMessageId) keeps trying across lost connections, waiting for each to come back, before it
52
+ * fails: a drop mended within it goes unseen. Default `waitForOpenMs`.
53
+ */
54
+ retryForMs?: number;
55
+ /**
56
+ * A call unanswered this long while nothing at all came over the connection makes it check that the
57
+ * connection is alive (`wake`): one gone without a word is made again in seconds, not when the system
58
+ * notices; the check waits as long for its answer. Default 5000; 0 never checks.
59
+ */
60
+ stallMs?: number;
22
61
  /**
23
62
  * Another way to ta serve than a WebSocket to `url`, such as a relay (RelaySocket of @celljs/ta-relay):
24
63
  * a new socket for each connection, which takes care of access itself.
@@ -40,12 +79,16 @@ export interface TaSocket {
40
79
  interface PendingCall {
41
80
  resolve(result: unknown): void;
42
81
  reject(error: Error): void;
82
+ /** When it was sent, on the clock of Date.now(). */
83
+ sentAt: number;
43
84
  }
44
85
  type EventsListener = (events: TaEventEnvelope[]) => void;
45
86
  type TerminalListener = (terminalId: string, data: string, exitCode?: number) => void;
46
87
  type ExtensionDataListener = (name: string, keys: string[], cwd?: string) => void;
47
88
  type AuthEventListener = (loginId: string, event: TaAuthEvent) => void;
48
89
  type RemoteEventListener = (event: TaRemoteEvent) => void;
90
+ type RoomEventListener = (event: TaRoomEvent) => void;
91
+ type SessionsListener = (sessions: TaSessionInfo[]) => void;
49
92
  type OpenListener = () => void;
50
93
  type CloseListener = () => void;
51
94
  type ReconnectingListener = (delayMs: number, attempt: number) => void;
@@ -56,8 +99,11 @@ type ReconnectingListener = (delayMs: number, attempt: number) => void;
56
99
  * over the one WebSocket.
57
100
  *
58
101
  * A call made while the connection is down waits a little for it to come back (`waitForOpenMs`): it was
59
- * not sent. Calls that were sent when the connection was lost fail and are not sent again, because only
60
- * the caller knows whether to resend.
102
+ * not sent. Calls that were sent when the connection was lost fail, unless they are safe to send again
103
+ * (`resendable`: reads, and messages ta serve takes once by their clientMessageId), which wait for the
104
+ * next connection and go again for up to `retryForMs`: on a network that drops now and then, a read or a
105
+ * message is late rather than failed. A call that stalls while nothing comes makes it check the
106
+ * connection (`stallMs`), so that one gone without a word is not waited on for long.
61
107
  */
62
108
  export declare class TaConnection {
63
109
  protected readonly options: TaConnectionOptions;
@@ -72,6 +118,7 @@ export declare class TaConnection {
72
118
  readonly gateway: TaGatewayServer;
73
119
  readonly relay: TaRelayServer;
74
120
  readonly remote: TaRemoteServer;
121
+ readonly rooms: TaRoomServer;
75
122
  protected socket?: TaSocket;
76
123
  protected closed: boolean;
77
124
  protected attempts: number;
@@ -83,6 +130,8 @@ export declare class TaConnection {
83
130
  protected readonly extensionDataListeners: Set<ExtensionDataListener>;
84
131
  protected readonly authListeners: Set<AuthEventListener>;
85
132
  protected readonly remoteListeners: Set<RemoteEventListener>;
133
+ protected readonly roomListeners: Set<RoomEventListener>;
134
+ protected readonly sessionsListeners: Set<SessionsListener>;
86
135
  protected readonly openListeners: Set<OpenListener>;
87
136
  protected readonly closeListeners: Set<CloseListener>;
88
137
  protected readonly reconnectingListeners: Set<ReconnectingListener>;
@@ -91,6 +140,11 @@ export declare class TaConnection {
91
140
  go(): void;
92
141
  fail(): void;
93
142
  }>;
143
+ /** When the last message came, on the clock of Date.now(). */
144
+ protected lastReceivedAt: number;
145
+ /** Checks the calls for a stall while some wait for answers. */
146
+ protected stallTimer?: ReturnType<typeof setInterval>;
147
+ protected probing: boolean;
94
148
  constructor(options: TaConnectionOptions);
95
149
  /** Resolves when the first connection is open, or rejects if it cannot be made; later losses reconnect. */
96
150
  connect(): Promise<void>;
@@ -104,6 +158,10 @@ export declare class TaConnection {
104
158
  onAuthEvent(listener: AuthEventListener): () => void;
105
159
  /** What happens with the remote machines, once this connection called TaRemoteServer.watch. */
106
160
  onRemoteEvent(listener: RemoteEventListener): () => void;
161
+ /** What happens in the rooms, once this connection called TaRoomServer.watch. */
162
+ onRoomEvent(listener: RoomEventListener): () => void;
163
+ /** The open sessions as they change (TaClient.onSessions), once this connection called TaServer.watchSessions. */
164
+ onSessions(listener: SessionsListener): () => void;
107
165
  onOpen(listener: OpenListener): () => void;
108
166
  /** Runs each time an open connection is lost; it reconnects unless closed. */
109
167
  onClose(listener: CloseListener): () => void;
@@ -123,11 +181,20 @@ export declare class TaConnection {
123
181
  protected lost(socket: TaSocket, opened: boolean, atOnce?: boolean): void;
124
182
  protected createWebSocket(): TaSocket;
125
183
  protected scheduleReconnect(): void;
126
- /** Resolves once the connection is open, or rejects after `waitForOpenMs`, or at once when it is closed. */
127
- protected opened(): Promise<void>;
184
+ /** Resolves once the connection is open, or rejects after `waitForOpenMs` (or `waitMs`), or at once when it is closed. */
185
+ protected opened(waitMs?: number): Promise<void>;
128
186
  protected failPending(): void;
129
187
  protected service<T extends object>(name: string): T;
188
+ /**
189
+ * Whether a call may go again after a lost connection, as doing it twice does what doing it once does:
190
+ * a read, or a message with its clientMessageId (TaPromptInput), which ta serve takes once.
191
+ */
192
+ static resendable(service: string, method: string, params: readonly unknown[]): boolean;
130
193
  protected call(service: string, method: string, params: unknown[]): Promise<unknown>;
194
+ /** Sends a call on the open connection; its answer, or its failure when the connection is lost first. */
195
+ protected send(service: string, method: string, params: unknown[]): Promise<unknown>;
196
+ /** While calls wait for answers: one that waits past `stallMs` with nothing come since makes it check the connection. */
197
+ protected watchStalls(): void;
131
198
  protected receive(text: string): void;
132
199
  }
133
200
  export {};
@@ -1,13 +1,51 @@
1
- import { TA_ACCESS_SERVICE, TA_AUTH_SERVICE, TA_CLIENT_SERVICE, TA_GATEWAY_SERVICE, TA_PACKAGE_SERVICE, TA_RELAY_SERVICE, TA_REMOTE_SERVICE, TA_SERVER_SERVICE, TA_SETTINGS_SERVICE, TA_TERMINAL_SERVICE, TA_WORKSPACE_SERVICE, TaErrorCode, } from '@celljs/ta-protocol';
2
- /** A failed call. `code` is set for failures the server names. */
1
+ import { TA_ACCESS_SERVICE, TA_AUTH_SERVICE, TA_CLIENT_SERVICE, TA_GATEWAY_SERVICE, TA_PACKAGE_SERVICE, TA_RELAY_SERVICE, TA_REMOTE_SERVICE, TA_ROOM_SERVICE, TA_SERVER_SERVICE, TA_SETTINGS_SERVICE, TA_TERMINAL_SERVICE, TA_WORKSPACE_SERVICE, TaErrorCode, } from '@celljs/ta-protocol';
2
+ /**
3
+ * A failed call. `code` is set for failures the server names; `rpcCode` is the code of JSON-RPC's error,
4
+ * by which a client tells a method an older ta serve does not have (`TaRemoteError.METHOD_NOT_FOUND`).
5
+ */
3
6
  export class TaRemoteError extends Error {
4
- constructor(message, code, hint) {
7
+ constructor(message, code, hint, rpcCode) {
5
8
  super(message);
6
9
  this.code = code;
7
10
  this.hint = hint;
11
+ this.rpcCode = rpcCode;
8
12
  this.name = 'TaRemoteError';
9
13
  }
14
+ /** The error of a JSON-RPC answer: its code, and the code and hint of ta serve when it named the failure. */
15
+ static of(error) {
16
+ var _a;
17
+ const data = (_a = error.data) === null || _a === void 0 ? void 0 : _a.data;
18
+ return new TaRemoteError(error.message, data === null || data === void 0 ? void 0 : data.code, data === null || data === void 0 ? void 0 : data.hint, error.code);
19
+ }
20
+ /** A call that waited for a connection that did not come back. */
21
+ static notConnected() {
22
+ return new TaRemoteError(TaRemoteError.NOT_CONNECTED, TaErrorCode.CONNECTION_LOST, 'Try again once the connection is back.');
23
+ }
24
+ /**
25
+ * Whether a failure is only that the connection was lost, which mends itself: by its code, or by its
26
+ * words in a text (a web extension shows `error.message`, often inside its own words).
27
+ */
28
+ static isConnectionLost(cause) {
29
+ if (typeof cause === 'string') {
30
+ return cause.includes(TaRemoteError.NOT_CONNECTED) || cause.includes(TaRemoteError.LOST);
31
+ }
32
+ return (cause === null || cause === void 0 ? void 0 : cause.code) === TaErrorCode.CONNECTION_LOST;
33
+ }
34
+ /** Whether the server has no such method: a ta serve from before it was added. */
35
+ static isMethodNotFound(error) {
36
+ return error instanceof TaRemoteError && error.rpcCode === TaRemoteError.METHOD_NOT_FOUND;
37
+ }
10
38
  }
39
+ /** JSON-RPC's code for a method the server does not have. */
40
+ TaRemoteError.METHOD_NOT_FOUND = -32601;
41
+ /** What a call says that waited for a connection that did not come back. */
42
+ TaRemoteError.NOT_CONNECTED = 'Not connected to ta serve.';
43
+ /** What a call says that was sent on a connection lost before its answer. */
44
+ TaRemoteError.LOST = 'The connection to ta serve was lost.';
45
+ /** Methods that only read: sent again after a lost connection. */
46
+ const READ_METHOD = /^(get|list|read|find)[A-Z]/;
47
+ /** Methods whose input carries a clientMessageId, by which ta serve takes a message once however often it comes. */
48
+ const MESSAGE_METHODS = new Set(['prompt', 'steer', 'followUp']);
11
49
  /**
12
50
  * A connection to `ta serve` on the `WebSocket` of the platform, with no other dependency, so that it
13
51
  * fits a browser, Electron or a mobile app. It reconnects until closed; `onOpen` runs each time, for
@@ -15,8 +53,11 @@ export class TaRemoteError extends Error {
15
53
  * over the one WebSocket.
16
54
  *
17
55
  * A call made while the connection is down waits a little for it to come back (`waitForOpenMs`): it was
18
- * not sent. Calls that were sent when the connection was lost fail and are not sent again, because only
19
- * the caller knows whether to resend.
56
+ * not sent. Calls that were sent when the connection was lost fail, unless they are safe to send again
57
+ * (`resendable`: reads, and messages ta serve takes once by their clientMessageId), which wait for the
58
+ * next connection and go again for up to `retryForMs`: on a network that drops now and then, a read or a
59
+ * message is late rather than failed. A call that stalls while nothing comes makes it check the
60
+ * connection (`stallMs`), so that one gone without a word is not waited on for long.
20
61
  */
21
62
  export class TaConnection {
22
63
  constructor(options) {
@@ -32,6 +73,7 @@ export class TaConnection {
32
73
  this.gateway = this.service(TA_GATEWAY_SERVICE);
33
74
  this.relay = this.service(TA_RELAY_SERVICE);
34
75
  this.remote = this.service(TA_REMOTE_SERVICE);
76
+ this.rooms = this.service(TA_ROOM_SERVICE);
35
77
  this.closed = false;
36
78
  this.attempts = 0;
37
79
  this.nextId = 0;
@@ -41,11 +83,16 @@ export class TaConnection {
41
83
  this.extensionDataListeners = new Set();
42
84
  this.authListeners = new Set();
43
85
  this.remoteListeners = new Set();
86
+ this.roomListeners = new Set();
87
+ this.sessionsListeners = new Set();
44
88
  this.openListeners = new Set();
45
89
  this.closeListeners = new Set();
46
90
  this.reconnectingListeners = new Set();
47
91
  /** The calls waiting for the connection to open: `go` once it is, `fail` when it is closed. */
48
92
  this.waiting = new Set();
93
+ /** When the last message came, on the clock of Date.now(). */
94
+ this.lastReceivedAt = 0;
95
+ this.probing = false;
49
96
  }
50
97
  /** Resolves when the first connection is open, or rejects if it cannot be made; later losses reconnect. */
51
98
  connect() {
@@ -55,6 +102,7 @@ export class TaConnection {
55
102
  var _a;
56
103
  this.closed = true;
57
104
  clearTimeout(this.reconnectTimer);
105
+ clearInterval(this.stallTimer);
58
106
  for (const call of [...this.waiting]) {
59
107
  call.fail();
60
108
  }
@@ -84,6 +132,16 @@ export class TaConnection {
84
132
  this.remoteListeners.add(listener);
85
133
  return () => this.remoteListeners.delete(listener);
86
134
  }
135
+ /** What happens in the rooms, once this connection called TaRoomServer.watch. */
136
+ onRoomEvent(listener) {
137
+ this.roomListeners.add(listener);
138
+ return () => this.roomListeners.delete(listener);
139
+ }
140
+ /** The open sessions as they change (TaClient.onSessions), once this connection called TaServer.watchSessions. */
141
+ onSessions(listener) {
142
+ this.sessionsListeners.add(listener);
143
+ return () => this.sessionsListeners.delete(listener);
144
+ }
87
145
  onOpen(listener) {
88
146
  this.openListeners.add(listener);
89
147
  return () => this.openListeners.delete(listener);
@@ -124,8 +182,12 @@ export class TaConnection {
124
182
  return;
125
183
  }
126
184
  let answered = false;
185
+ const since = Date.now();
186
+ this.probing = true;
127
187
  const timer = setTimeout(() => {
128
- if (!answered && this.socket === socket) {
188
+ this.probing = false;
189
+ // Anything that came meanwhile says it is alive, only slow: the answer waits behind it.
190
+ if (!answered && this.socket === socket && this.lastReceivedAt <= since) {
129
191
  socket.onclose = null;
130
192
  socket.onmessage = null;
131
193
  socket.close();
@@ -133,10 +195,19 @@ export class TaConnection {
133
195
  this.lost(socket, true, true);
134
196
  }
135
197
  }, timeoutMs);
136
- this.server.getServerInfo().then(() => {
198
+ const done = () => {
137
199
  answered = true;
200
+ this.probing = false;
138
201
  clearTimeout(timer);
139
- }, () => clearTimeout(timer));
202
+ };
203
+ // Any answer says the connection is alive, an error too. Asked of what answers at the front: `ta gateway`
204
+ // answers its own service while a Ta behind it is slow, and a Ta service says it has no such service.
205
+ // Sent once, not again after a loss as a read would be: it asks about this connection.
206
+ this.send(TA_GATEWAY_SERVICE, 'getGateway', []).then(done, error => {
207
+ if (error.code !== TaErrorCode.CONNECTION_LOST) {
208
+ done();
209
+ }
210
+ });
140
211
  }
141
212
  /** With `resolve` and `reject`: the first connection, whose failure is the caller's to handle. */
142
213
  open(resolve, reject) {
@@ -202,10 +273,11 @@ export class TaConnection {
202
273
  listener(delay, this.attempts);
203
274
  }
204
275
  }
205
- /** Resolves once the connection is open, or rejects after `waitForOpenMs`, or at once when it is closed. */
206
- opened() {
207
- const { waitForOpenMs = 5000 } = this.options;
208
- const notConnected = () => new TaRemoteError('Not connected to ta serve.', TaErrorCode.CONNECTION_LOST, 'Try again once the connection is back.');
276
+ /** Resolves once the connection is open, or rejects after `waitForOpenMs` (or `waitMs`), or at once when it is closed. */
277
+ opened(waitMs) {
278
+ const { waitForOpenMs: configured = 5000 } = this.options;
279
+ const waitForOpenMs = configured <= 0 ? 0 : (waitMs !== null && waitMs !== void 0 ? waitMs : configured);
280
+ const notConnected = () => TaRemoteError.notConnected();
209
281
  if (this.closed || waitForOpenMs <= 0) {
210
282
  return Promise.reject(notConnected());
211
283
  }
@@ -228,7 +300,7 @@ export class TaConnection {
228
300
  }
229
301
  failPending() {
230
302
  for (const call of this.pending.values()) {
231
- call.reject(new TaRemoteError('The connection to ta serve was lost.', TaErrorCode.CONNECTION_LOST, 'Send it again once the connection is back.'));
303
+ call.reject(new TaRemoteError(TaRemoteError.LOST, TaErrorCode.CONNECTION_LOST, 'Send it again once the connection is back.'));
232
304
  }
233
305
  this.pending.clear();
234
306
  }
@@ -237,10 +309,46 @@ export class TaConnection {
237
309
  get: (_target, method) => (...params) => this.call(name, method, params),
238
310
  });
239
311
  }
312
+ /**
313
+ * Whether a call may go again after a lost connection, as doing it twice does what doing it once does:
314
+ * a read, or a message with its clientMessageId (TaPromptInput), which ta serve takes once.
315
+ */
316
+ static resendable(service, method, params) {
317
+ var _a;
318
+ if (READ_METHOD.test(method)) {
319
+ return true;
320
+ }
321
+ return service === TA_SERVER_SERVICE && MESSAGE_METHODS.has(method) && !!((_a = params[1]) === null || _a === void 0 ? void 0 : _a.clientMessageId);
322
+ }
240
323
  async call(service, method, params) {
324
+ var _a, _b;
325
+ if (!TaConnection.resendable(service, method, params)) {
326
+ if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== WebSocket.OPEN) {
327
+ await this.opened();
328
+ }
329
+ return this.send(service, method, params);
330
+ }
331
+ const { waitForOpenMs = 5000, retryForMs = waitForOpenMs } = this.options;
332
+ const deadline = Date.now() + Math.max(retryForMs, 1);
333
+ for (;;) {
334
+ if (((_b = this.socket) === null || _b === void 0 ? void 0 : _b.readyState) !== WebSocket.OPEN) {
335
+ await this.opened(Math.max(1, deadline - Date.now()));
336
+ }
337
+ try {
338
+ return await this.send(service, method, params);
339
+ }
340
+ catch (error) {
341
+ if (error.code !== TaErrorCode.CONNECTION_LOST || this.closed || Date.now() >= deadline) {
342
+ throw error;
343
+ }
344
+ }
345
+ }
346
+ }
347
+ /** Sends a call on the open connection; its answer, or its failure when the connection is lost first. */
348
+ send(service, method, params) {
241
349
  var _a;
242
350
  if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== WebSocket.OPEN) {
243
- await this.opened();
351
+ return Promise.reject(TaRemoteError.notConnected());
244
352
  }
245
353
  const socket = this.socket;
246
354
  const id = ++this.nextId;
@@ -250,12 +358,32 @@ export class TaConnection {
250
358
  length--;
251
359
  }
252
360
  return new Promise((resolve, reject) => {
253
- this.pending.set(id, { resolve, reject });
361
+ this.pending.set(id, { resolve, reject, sentAt: Date.now() });
362
+ this.watchStalls();
254
363
  socket.send(JSON.stringify({ jsonrpc: '2.0', id, method: `${service}/${method}`, params: params.slice(0, length) }));
255
364
  });
256
365
  }
366
+ /** While calls wait for answers: one that waits past `stallMs` with nothing come since makes it check the connection. */
367
+ watchStalls() {
368
+ const { stallMs = 5000 } = this.options;
369
+ if (this.stallTimer !== undefined || stallMs <= 0) {
370
+ return;
371
+ }
372
+ this.stallTimer = setInterval(() => {
373
+ const [oldest] = this.pending.values();
374
+ if (!oldest) {
375
+ clearInterval(this.stallTimer);
376
+ this.stallTimer = undefined;
377
+ return;
378
+ }
379
+ if (!this.probing && Date.now() - Math.max(oldest.sentAt, this.lastReceivedAt) >= stallMs) {
380
+ this.wake(stallMs);
381
+ }
382
+ }, Math.min(1000, stallMs));
383
+ }
257
384
  receive(text) {
258
- var _a, _b, _c, _d, _e, _f;
385
+ var _a, _b, _c, _d, _e, _f, _g;
386
+ this.lastReceivedAt = Date.now();
259
387
  const message = JSON.parse(text);
260
388
  if (message.method === `${TA_CLIENT_SERVICE}/onEvents`) {
261
389
  const [events] = message.params;
@@ -265,13 +393,21 @@ export class TaConnection {
265
393
  (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
266
394
  return;
267
395
  }
396
+ if (message.method === `${TA_CLIENT_SERVICE}/onSessions`) {
397
+ const [sessions] = message.params;
398
+ for (const listener of this.sessionsListeners) {
399
+ listener(sessions);
400
+ }
401
+ (_b = this.socket) === null || _b === void 0 ? void 0 : _b.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
402
+ return;
403
+ }
268
404
  if (message.method === `${TA_CLIENT_SERVICE}/onTerminalData`) {
269
405
  const [terminalId, data, exitCode] = message.params;
270
406
  for (const listener of this.terminalListeners) {
271
407
  // JSON has no undefined: a terminal still running may send null.
272
408
  listener(terminalId, data, exitCode !== null && exitCode !== void 0 ? exitCode : undefined);
273
409
  }
274
- (_b = this.socket) === null || _b === void 0 ? void 0 : _b.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
410
+ (_c = this.socket) === null || _c === void 0 ? void 0 : _c.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
275
411
  return;
276
412
  }
277
413
  if (message.method === `${TA_CLIENT_SERVICE}/onAuthEvent`) {
@@ -279,7 +415,7 @@ export class TaConnection {
279
415
  for (const listener of this.authListeners) {
280
416
  listener(loginId, event);
281
417
  }
282
- (_c = this.socket) === null || _c === void 0 ? void 0 : _c.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
418
+ (_d = this.socket) === null || _d === void 0 ? void 0 : _d.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
283
419
  return;
284
420
  }
285
421
  if (message.method === `${TA_CLIENT_SERVICE}/onRemoteEvent`) {
@@ -287,7 +423,15 @@ export class TaConnection {
287
423
  for (const listener of this.remoteListeners) {
288
424
  listener(event);
289
425
  }
290
- (_d = this.socket) === null || _d === void 0 ? void 0 : _d.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
426
+ (_e = this.socket) === null || _e === void 0 ? void 0 : _e.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
427
+ return;
428
+ }
429
+ if (message.method === `${TA_CLIENT_SERVICE}/onRoomEvent`) {
430
+ const [event] = message.params;
431
+ for (const listener of this.roomListeners) {
432
+ listener(event);
433
+ }
434
+ (_f = this.socket) === null || _f === void 0 ? void 0 : _f.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
291
435
  return;
292
436
  }
293
437
  if (message.method === `${TA_CLIENT_SERVICE}/onExtensionData`) {
@@ -295,7 +439,7 @@ export class TaConnection {
295
439
  for (const listener of this.extensionDataListeners) {
296
440
  listener(name, keys, cwd !== null && cwd !== void 0 ? cwd : undefined);
297
441
  }
298
- (_e = this.socket) === null || _e === void 0 ? void 0 : _e.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
442
+ (_g = this.socket) === null || _g === void 0 ? void 0 : _g.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
299
443
  return;
300
444
  }
301
445
  const call = this.pending.get(message.id);
@@ -304,8 +448,7 @@ export class TaConnection {
304
448
  }
305
449
  this.pending.delete(message.id);
306
450
  if (message.error) {
307
- const data = (_f = message.error.data) === null || _f === void 0 ? void 0 : _f.data;
308
- call.reject(new TaRemoteError(message.error.message, data === null || data === void 0 ? void 0 : data.code, data === null || data === void 0 ? void 0 : data.hint));
451
+ call.reject(TaRemoteError.of(message.error));
309
452
  }
310
453
  else {
311
454
  call.resolve(message.result);
@@ -19,6 +19,14 @@ export interface SessionWidget {
19
19
  lines: string[];
20
20
  placement: 'aboveEditor' | 'belowEditor';
21
21
  }
22
+ /** A message sent from this client that the session does not have yet: on its way, or `failed` to go. */
23
+ export interface OutboxMessage {
24
+ /** Its clientMessageId. */
25
+ id: string;
26
+ message: SessionMessage;
27
+ /** Why it did not go. */
28
+ failed?: string;
29
+ }
22
30
  export interface SessionState {
23
31
  /** The agent is working, from the start of a run until it has settled. */
24
32
  working: boolean;
@@ -49,6 +57,10 @@ export interface SessionState {
49
57
  compacting?: 'manual' | 'threshold' | 'overflow';
50
58
  /** pi waits to retry a failed request of the model. */
51
59
  retry?: RetryState;
60
+ /** What an extension says the agent does while it works (pi's `setWorkingMessage`). */
61
+ workingMessage?: string;
62
+ /** Which of the last `messages` this client sent that the session does not have yet (SessionStore.send). */
63
+ outbox?: readonly OutboxMessage[];
52
64
  }
53
65
  export declare namespace SessionReducer {
54
66
  function initial(): SessionState;
@@ -57,7 +69,9 @@ export declare namespace SessionReducer {
57
69
  /**
58
70
  * The messages of `next`, with each that is the same as the one of `previous` at its place kept as it
59
71
  * was: a conversation read again after a reconnect has new objects, which a UI would all draw again.
72
+ * `offset`: where `next` starts in `previous` (the difference of their `start`), for a part of the
73
+ * conversation that begins elsewhere, such as one kept on the device that was cut shorter.
60
74
  */
61
- function keep(previous: readonly SessionMessage[], next: SessionMessage[]): SessionMessage[];
75
+ function keep(previous: readonly SessionMessage[], next: SessionMessage[], offset?: number): SessionMessage[];
62
76
  }
63
77
  //# sourceMappingURL=session-reducer.d.ts.map
@@ -26,7 +26,9 @@ var SessionReducer;
26
26
  case 'agent_start':
27
27
  return { ...state, working: true, runStart: state.messages.length };
28
28
  case 'agent_settled':
29
- return { ...state, working: false, retry: undefined };
29
+ return { ...state, working: false, retry: undefined, workingMessage: undefined };
30
+ case 'working_message':
31
+ return { ...state, workingMessage: event.message };
30
32
  case 'message_start':
31
33
  return { ...state, messages: [...state.messages, event.message] };
32
34
  case 'message_update': {
@@ -158,11 +160,13 @@ var SessionReducer;
158
160
  /**
159
161
  * The messages of `next`, with each that is the same as the one of `previous` at its place kept as it
160
162
  * was: a conversation read again after a reconnect has new objects, which a UI would all draw again.
163
+ * `offset`: where `next` starts in `previous` (the difference of their `start`), for a part of the
164
+ * conversation that begins elsewhere, such as one kept on the device that was cut shorter.
161
165
  */
162
- function keep(previous, next) {
163
- let same = previous.length === next.length;
166
+ function keep(previous, next, offset = 0) {
167
+ let same = offset === 0 && previous.length === next.length;
164
168
  const kept = next.map((message, index) => {
165
- const before = previous[index];
169
+ const before = previous[index + offset];
166
170
  if (before && before.role === message.role && before.timestamp === message.timestamp && JSON.stringify(before) === JSON.stringify(message)) {
167
171
  return before;
168
172
  }