@celljs/ta-client 4.16.0 → 4.18.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,3 +1,7 @@
1
+ export * from './session-client-tools.js';
1
2
  export * from './session-reducer.js';
2
3
  export * from './session-store.js';
4
+ export * from './session-store-options.js';
3
5
  export * from './ta-connection.js';
6
+ export * from './ta-connection-options.js';
7
+ export * from './ta-remote-error.js';
@@ -1,4 +1,8 @@
1
+ export * from './session-client-tools.js';
1
2
  export * from './session-reducer.js';
2
3
  export * from './session-store.js';
4
+ export * from './session-store-options.js';
3
5
  export * from './ta-connection.js';
6
+ export * from './ta-connection-options.js';
7
+ export * from './ta-remote-error.js';
4
8
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,32 @@
1
+ import type { TaClientToolDefinition, TaClientToolRequest, TaClientToolResult } from '@celljs/ta-protocol';
2
+ import type { SessionState } from './session-reducer.js';
3
+ import type { SessionConnection } from './session-store-options.js';
4
+ /**
5
+ * A tool this client runs. Open the session with its `definition` in `clientTools`, and give the tool
6
+ * to the store, which runs each call and sends the result. Its output is what the model reads; a
7
+ * thrown error becomes a failed call.
8
+ */
9
+ export interface ClientTool {
10
+ definition: TaClientToolDefinition;
11
+ run(input: unknown, request: TaClientToolRequest): Promise<string> | string;
12
+ }
13
+ export declare abstract class SessionClientTools {
14
+ protected abstract readonly connection: SessionConnection;
15
+ abstract readonly sessionId: string;
16
+ protected abstract readonly clientTools: Map<string, ClientTool>;
17
+ /** Calls of client tools this store is running or answering now, by id. */
18
+ protected readonly running: Set<string>;
19
+ /** Results not yet taken by the server, by call id. */
20
+ protected readonly results: Map<string, TaClientToolResult>;
21
+ /** Calls another client took, by id: tried again only when the request comes again (that client left). */
22
+ protected readonly declined: Map<string, TaClientToolRequest>;
23
+ protected runClientTools(state: SessionState): void;
24
+ /**
25
+ * Runs a call once, if this client takes it: every page of the user gets it. A result that could not be
26
+ * sent is sent again after a reconnect, not made again.
27
+ */
28
+ protected answer(tool: ClientTool, request: TaClientToolRequest): Promise<void>;
29
+ /** Whether this client runs the call; a Ta service from before claims (a computer not updated yet, reached from a phone) lets every client run it, as it did. */
30
+ protected claim(request: TaClientToolRequest): Promise<boolean>;
31
+ protected run(tool: ClientTool, request: TaClientToolRequest): Promise<TaClientToolResult>;
32
+ }
@@ -0,0 +1,72 @@
1
+ import { TaRemoteError } from './ta-remote-error.js';
2
+ export class SessionClientTools {
3
+ constructor() {
4
+ /** Calls of client tools this store is running or answering now, by id. */
5
+ this.running = new Set();
6
+ /** Results not yet taken by the server, by call id. */
7
+ this.results = new Map();
8
+ /** Calls another client took, by id: tried again only when the request comes again (that client left). */
9
+ this.declined = new Map();
10
+ }
11
+ runClientTools(state) {
12
+ for (const request of state.pendingClientTools) {
13
+ const tool = this.clientTools.get(request.toolName);
14
+ if (tool && !this.running.has(request.id) && this.declined.get(request.id) !== request) {
15
+ this.running.add(request.id);
16
+ void this.answer(tool, request);
17
+ }
18
+ }
19
+ for (const calls of [this.results, this.declined]) {
20
+ for (const id of calls.keys()) {
21
+ if (!state.pendingClientTools.some(request => request.id === id)) {
22
+ calls.delete(id);
23
+ }
24
+ }
25
+ }
26
+ }
27
+ /**
28
+ * Runs a call once, if this client takes it: every page of the user gets it. A result that could not be
29
+ * sent is sent again after a reconnect, not made again.
30
+ */
31
+ async answer(tool, request) {
32
+ try {
33
+ let result = this.results.get(request.id);
34
+ if (!result) {
35
+ // Not asked again before the request comes again, also when the connection was lost: the reconnect brings it.
36
+ this.declined.set(request.id, request);
37
+ if (!(await this.claim(request))) {
38
+ return;
39
+ }
40
+ this.declined.delete(request.id);
41
+ result = await this.run(tool, request);
42
+ this.results.set(request.id, result);
43
+ }
44
+ await this.connection.server.respondClientTool(this.sessionId, result);
45
+ this.results.delete(request.id);
46
+ }
47
+ catch (_a) {
48
+ // The connection was lost; the reconnect sends the result if the call still waits.
49
+ }
50
+ finally {
51
+ this.running.delete(request.id);
52
+ }
53
+ }
54
+ /** Whether this client runs the call; a Ta service from before claims (a computer not updated yet, reached from a phone) lets every client run it, as it did. */
55
+ claim(request) {
56
+ return this.connection.server.claimClientTool(this.sessionId, request.id).catch(error => {
57
+ if (TaRemoteError.isMethodNotFound(error)) {
58
+ return true;
59
+ }
60
+ throw error;
61
+ });
62
+ }
63
+ async run(tool, request) {
64
+ try {
65
+ return { id: request.id, output: await tool.run(request.input, request) };
66
+ }
67
+ catch (error) {
68
+ return { id: request.id, output: error instanceof Error ? error.message : String(error), isError: true };
69
+ }
70
+ }
71
+ }
72
+ //# sourceMappingURL=session-client-tools.js.map
@@ -1,4 +1,4 @@
1
- import type { RpcExtensionUIRequest, TaClientToolRequest, TaEvent, TaMessage, TaToolRun } from '@celljs/ta-protocol';
1
+ import type { RpcExtensionUIRequest, TaClientToolRequest, TaEvent, TaMessage, TaToolRun, TaTurnHead } from '@celljs/ta-protocol';
2
2
  export type SessionMessage = TaMessage;
3
3
  export type ToolRun = TaToolRun;
4
4
  /** A message of `notify`. */
@@ -33,6 +33,11 @@ export interface SessionState {
33
33
  messages: SessionMessage[];
34
34
  /** Where `messages` start in the whole conversation: those before are loaded with `SessionStore.loadEarlier`. */
35
35
  start: number;
36
+ /**
37
+ * When `messages` start in the middle of a turn: the message of the user that asked for it, shown above the
38
+ * steps; it counts while its index is before `start`.
39
+ */
40
+ head?: TaTurnHead;
36
41
  /** Earlier messages are on their way (`SessionStore.loadEarlier`): `start` above 0 alone does not say so. */
37
42
  loadingEarlier?: boolean;
38
43
  /** By tool call id. */
@@ -61,6 +66,11 @@ export interface SessionState {
61
66
  retry?: RetryState;
62
67
  /** What an extension says the agent does while it works (pi's `setWorkingMessage`). */
63
68
  workingMessage?: string;
69
+ /**
70
+ * False while the state is not the session's of now: read from what this device kept, or of a store detached. How
71
+ * its last turn ended may be over (the agent went on since); undefined for a state that never was the session's.
72
+ */
73
+ synced?: boolean;
64
74
  /** Which of the last `messages` this client sent that the session does not have yet (SessionStore.send). */
65
75
  outbox?: readonly OutboxMessage[];
66
76
  }
@@ -0,0 +1,26 @@
1
+ import type { ClientTool } from './session-client-tools.js';
2
+ import type { SessionState } from './session-reducer.js';
3
+ import type { TaConnection } from './ta-connection.js';
4
+ /** How long a message the connection could not take waits for it, on its way, before it shows as failed. */
5
+ export declare const HOLD_MS = 60000;
6
+ /** What a store uses of its connection: a TaConnection, or something that stands for one before there is one. */
7
+ export type SessionConnection = Pick<TaConnection, 'server' | 'onEvents' | 'onOpen'>;
8
+ /** What a store shows before it attaches: the conversation as a client kept it (its messages from `start`, and its tool calls). */
9
+ export type SessionSnapshot = Pick<SessionState, 'messages' | 'start' | 'tools'>;
10
+ export interface SessionStoreOptions {
11
+ /** The tools of the session this client runs. Other clients may run them too: the first result counts. */
12
+ clientTools?: ClientTool[];
13
+ /**
14
+ * Only about this many bytes of the latest messages come when it attaches, and as many more each time
15
+ * `loadEarlier` is called: a long conversation shows at once. None: all of it.
16
+ */
17
+ pageBytes?: number;
18
+ /**
19
+ * Shown from the start, before it attaches (and without a connection): the conversation as it was last
20
+ * seen. Attached, the messages of the session that are the same keep their objects, so that a UI draws
21
+ * again only what changed.
22
+ */
23
+ initial?: SessionSnapshot;
24
+ /** How long a message the user sent waits for a lost connection to come back (HOLD_MS). */
25
+ holdMs?: number;
26
+ }
@@ -0,0 +1,3 @@
1
+ /** How long a message the connection could not take waits for it, on its way, before it shows as failed. */
2
+ export const HOLD_MS = 60000;
3
+ //# sourceMappingURL=session-store-options.js.map
@@ -1,6 +1,7 @@
1
- import { type RpcExtensionUIResponse, type TaAttachResult, type TaClientToolDefinition, type TaClientToolRequest, type TaClientToolResult, type TaEvent, type TaEventEnvelope, type TaPromptInput, type TaPromptReceipt, type TaSessionInfo } from '@celljs/ta-protocol';
1
+ import { type RpcExtensionUIResponse, type TaAttachResult, type TaEvent, type TaEventEnvelope, type TaPromptInput, type TaPromptReceipt, type TaSessionInfo } from '@celljs/ta-protocol';
2
+ import { type ClientTool, SessionClientTools } from './session-client-tools.js';
2
3
  import { type OutboxMessage, type SessionMessage, type SessionState } from './session-reducer.js';
3
- import { type TaConnection } from './ta-connection.js';
4
+ import { type SessionConnection, type SessionSnapshot, type SessionStoreOptions } from './session-store-options.js';
4
5
  type Listener = (state: SessionState) => void;
5
6
  interface Taken {
6
7
  state: SessionState;
@@ -17,43 +18,11 @@ interface Sent extends OutboxMessage {
17
18
  /** When the connection first could not take it: it waits for the next, shown as on its way, until `holdMs`. */
18
19
  heldAt?: number;
19
20
  }
20
- /** How long a message the connection could not take waits for it, on its way, before it shows as failed. */
21
- export declare const HOLD_MS = 60000;
22
- /**
23
- * A tool this client runs. Open the session with its `definition` in `clientTools`, and give the tool
24
- * to the store, which runs each call and sends the result. Its output is what the model reads; a
25
- * thrown error becomes a failed call.
26
- */
27
- export interface ClientTool {
28
- definition: TaClientToolDefinition;
29
- run(input: unknown, request: TaClientToolRequest): Promise<string> | string;
30
- }
31
- /** What a store uses of its connection: a TaConnection, or something that stands for one before there is one. */
32
- export type SessionConnection = Pick<TaConnection, 'server' | 'onEvents' | 'onOpen'>;
33
- /** What a store shows before it attaches: the conversation as a client kept it (its messages from `start`, and its tool calls). */
34
- export type SessionSnapshot = Pick<SessionState, 'messages' | 'start' | 'tools'>;
35
- export interface SessionStoreOptions {
36
- /** The tools of the session this client runs. Other clients may run them too: the first result counts. */
37
- clientTools?: ClientTool[];
38
- /**
39
- * Only about this many bytes of the latest messages come when it attaches, and as many more each time
40
- * `loadEarlier` is called: a long conversation shows at once. None: all of it.
41
- */
42
- pageBytes?: number;
43
- /**
44
- * Shown from the start, before it attaches (and without a connection): the conversation as it was last
45
- * seen. Attached, the messages of the session that are the same keep their objects, so that a UI draws
46
- * again only what changed.
47
- */
48
- initial?: SessionSnapshot;
49
- /** How long a message the user sent waits for a lost connection to come back (HOLD_MS). */
50
- holdMs?: number;
51
- }
52
21
  /**
53
22
  * The state of one open session on a client: for any UI to render and subscribe to. It catches up by
54
23
  * itself after a reconnect, from the events it missed or, if those are gone, from the session.
55
24
  */
56
- export declare class SessionStore {
25
+ export declare class SessionStore extends SessionClientTools {
57
26
  protected readonly connection: SessionConnection;
58
27
  readonly sessionId: string;
59
28
  /** The session as the service told it. */
@@ -67,12 +36,6 @@ export declare class SessionStore {
67
36
  protected readonly goneListeners: Set<() => void>;
68
37
  protected readonly stop: (() => void)[];
69
38
  protected readonly clientTools: Map<string, ClientTool>;
70
- /** Calls of client tools this store is running or answering now, by id. */
71
- protected readonly running: Set<string>;
72
- /** Results not yet taken by the server, by call id. */
73
- protected readonly results: Map<string, TaClientToolResult>;
74
- /** Calls another client took, by id: tried again only when the request comes again (that client left). */
75
- protected readonly declined: Map<string, TaClientToolRequest>;
76
39
  protected readonly pageBytes?: number;
77
40
  /** The earlier messages on their way. */
78
41
  protected loadingEarlier?: Promise<void>;
@@ -170,14 +133,5 @@ export declare class SessionStore {
170
133
  protected update(state: SessionState): void;
171
134
  /** UIs get the state with the messages sent, when either changed. */
172
135
  protected show(): void;
173
- protected runClientTools(state: SessionState): void;
174
- /**
175
- * Runs a call once, if this client takes it: every page of the user gets it. A result that could not be
176
- * sent is sent again after a reconnect, not made again.
177
- */
178
- protected answer(tool: ClientTool, request: TaClientToolRequest): Promise<void>;
179
- /** Whether this client runs the call; a Ta service from before claims (a computer not updated yet, reached from a phone) lets every client run it, as it did. */
180
- protected claim(request: TaClientToolRequest): Promise<boolean>;
181
- protected run(tool: ClientTool, request: TaClientToolRequest): Promise<TaClientToolResult>;
182
136
  }
183
137
  export {};
@@ -1,15 +1,16 @@
1
1
  import { TaErrorCode, } from '@celljs/ta-protocol';
2
+ import { SessionClientTools } from './session-client-tools.js';
2
3
  import { SessionReducer } from './session-reducer.js';
3
- import { TaRemoteError } from './ta-connection.js';
4
- /** How long a message the connection could not take waits for it, on its way, before it shows as failed. */
5
- export const HOLD_MS = 60000;
4
+ import { HOLD_MS } from './session-store-options.js';
5
+ import { TaRemoteError } from './ta-remote-error.js';
6
6
  /**
7
7
  * The state of one open session on a client: for any UI to render and subscribe to. It catches up by
8
8
  * itself after a reconnect, from the events it missed or, if those are gone, from the session.
9
9
  */
10
- export class SessionStore {
10
+ export class SessionStore extends SessionClientTools {
11
11
  constructor(connection, sessionId, options = {}) {
12
12
  var _a, _b;
13
+ super();
13
14
  this.connection = connection;
14
15
  this.sessionId = sessionId;
15
16
  /** The session as the service told it. */
@@ -21,12 +22,6 @@ export class SessionStore {
21
22
  this.eventListeners = new Set();
22
23
  this.goneListeners = new Set();
23
24
  this.stop = [];
24
- /** Calls of client tools this store is running or answering now, by id. */
25
- this.running = new Set();
26
- /** Results not yet taken by the server, by call id. */
27
- this.results = new Map();
28
- /** Calls another client took, by id: tried again only when the request comes again (that client left). */
29
- this.declined = new Map();
30
25
  /** The messages held that are being sent again now. */
31
26
  this.resending = new Set();
32
27
  this.clientTools = new Map(((_a = options.clientTools) !== null && _a !== void 0 ? _a : []).map(tool => [tool.definition.name, tool]));
@@ -34,7 +29,7 @@ export class SessionStore {
34
29
  this.holdMs = (_b = options.holdMs) !== null && _b !== void 0 ? _b : HOLD_MS;
35
30
  if (options.initial) {
36
31
  const { messages, start, tools } = options.initial;
37
- this.current = this.shown = { ...this.current, messages, start, tools };
32
+ this.current = this.shown = { ...this.current, messages, start, tools, synced: false };
38
33
  }
39
34
  }
40
35
  get state() {
@@ -58,6 +53,9 @@ export class SessionStore {
58
53
  for (const stop of this.stop.splice(0)) {
59
54
  stop();
60
55
  }
56
+ // Not through `update`: what it shows changes, the calls it runs do not.
57
+ this.current = { ...this.current, synced: false };
58
+ this.show();
61
59
  await this.connection.server.detach(this.sessionId);
62
60
  }
63
61
  subscribe(listener) {
@@ -270,7 +268,7 @@ export class SessionStore {
270
268
  return this.connection.server.respondUi(this.sessionId, response);
271
269
  }
272
270
  async attachAgain(attached) {
273
- var _a, _b;
271
+ var _a, _b, _c;
274
272
  const result = attached !== null && attached !== void 0 ? attached : (await this.connection.server.attach(this.sessionId, this.lastSeq, this.pageBytes ? { recentBytes: this.pageBytes } : undefined));
275
273
  let state = this.current;
276
274
  // A conversation given (one attached as it opened) is the session's from scratch, whatever this store had.
@@ -284,6 +282,8 @@ export class SessionStore {
284
282
  messages: kept,
285
283
  tools: conversation.tools,
286
284
  start,
285
+ // Those loaded further up kept: the head they start in, as it was.
286
+ head: start === ((_c = conversation.start) !== null && _c !== void 0 ? _c : 0) ? conversation.head : this.current.head,
287
287
  runStart: kept === this.current.messages ? this.current.runStart : undefined,
288
288
  };
289
289
  this.lastSeq = result.lastSeq;
@@ -296,6 +296,7 @@ export class SessionStore {
296
296
  pendingUi: result.pendingUi,
297
297
  pendingClientTools: result.pendingClientTools,
298
298
  working: result.info.isStreaming,
299
+ synced: true,
299
300
  };
300
301
  if (result.shownUi) {
301
302
  // What the extensions show now, whatever events were missed.
@@ -370,15 +371,16 @@ export class SessionStore {
370
371
  ...this.current,
371
372
  messages: [...page.messages, ...this.current.messages],
372
373
  start: page.start,
374
+ head: page.head,
373
375
  loadingEarlier: false,
374
376
  runStart: runStart === undefined ? undefined : runStart + page.messages.length,
375
377
  });
376
378
  }
377
379
  async reload() {
378
- const { messages, start } = this.pageBytes
380
+ const { messages, start, head } = this.pageBytes
379
381
  ? await this.connection.server.getEarlierMessages(this.sessionId, undefined, undefined, this.pageBytes)
380
- : { ...(await this.connection.server.getMessages(this.sessionId)), start: 0 };
381
- this.update({ ...this.current, messages, start, tools: {}, runStart: undefined });
382
+ : { ...(await this.connection.server.getMessages(this.sessionId)), start: 0, head: undefined };
383
+ this.update({ ...this.current, messages, start, head, tools: {}, runStart: undefined });
382
384
  }
383
385
  apply(events) {
384
386
  const taken = this.take(this.current, events);
@@ -436,65 +438,5 @@ export class SessionStore {
436
438
  listener(shown);
437
439
  }
438
440
  }
439
- runClientTools(state) {
440
- for (const request of state.pendingClientTools) {
441
- const tool = this.clientTools.get(request.toolName);
442
- if (tool && !this.running.has(request.id) && this.declined.get(request.id) !== request) {
443
- this.running.add(request.id);
444
- void this.answer(tool, request);
445
- }
446
- }
447
- for (const calls of [this.results, this.declined]) {
448
- for (const id of calls.keys()) {
449
- if (!state.pendingClientTools.some(request => request.id === id)) {
450
- calls.delete(id);
451
- }
452
- }
453
- }
454
- }
455
- /**
456
- * Runs a call once, if this client takes it: every page of the user gets it. A result that could not be
457
- * sent is sent again after a reconnect, not made again.
458
- */
459
- async answer(tool, request) {
460
- try {
461
- let result = this.results.get(request.id);
462
- if (!result) {
463
- // Not asked again before the request comes again, also when the connection was lost: the reconnect brings it.
464
- this.declined.set(request.id, request);
465
- if (!(await this.claim(request))) {
466
- return;
467
- }
468
- this.declined.delete(request.id);
469
- result = await this.run(tool, request);
470
- this.results.set(request.id, result);
471
- }
472
- await this.connection.server.respondClientTool(this.sessionId, result);
473
- this.results.delete(request.id);
474
- }
475
- catch (_a) {
476
- // The connection was lost; the reconnect sends the result if the call still waits.
477
- }
478
- finally {
479
- this.running.delete(request.id);
480
- }
481
- }
482
- /** Whether this client runs the call; a Ta service from before claims (a computer not updated yet, reached from a phone) lets every client run it, as it did. */
483
- claim(request) {
484
- return this.connection.server.claimClientTool(this.sessionId, request.id).catch(error => {
485
- if (TaRemoteError.isMethodNotFound(error)) {
486
- return true;
487
- }
488
- throw error;
489
- });
490
- }
491
- async run(tool, request) {
492
- try {
493
- return { id: request.id, output: await tool.run(request.input, request) };
494
- }
495
- catch (error) {
496
- return { id: request.id, output: error instanceof Error ? error.message : String(error), isError: true };
497
- }
498
- }
499
441
  }
500
442
  //# sourceMappingURL=session-store.js.map
@@ -0,0 +1,44 @@
1
+ export interface TaConnectionOptions {
2
+ /** The `url` of serve.json. */
3
+ url: string;
4
+ /** The `token` of serve.json. */
5
+ token: string;
6
+ /** First wait before reconnecting; it doubles up to `maxReconnectDelayMs`. Default 250. */
7
+ reconnectDelayMs?: number;
8
+ /** Default 10000. */
9
+ maxReconnectDelayMs?: number;
10
+ /**
11
+ * How long a call made while the connection is down waits for it to come back before it fails, so
12
+ * that a short drop goes unnoticed. Default 5000; 0 fails at once.
13
+ */
14
+ waitForOpenMs?: number;
15
+ /**
16
+ * How long a call that is safe to send again (`TaConnection.resendable`: a read, a message with its
17
+ * clientMessageId) keeps trying across lost connections, waiting for each to come back, before it
18
+ * fails: a drop mended within it goes unseen. Default `waitForOpenMs`.
19
+ */
20
+ retryForMs?: number;
21
+ /**
22
+ * A call unanswered this long while nothing at all came over the connection makes it check that the
23
+ * connection is alive (`wake`): one gone without a word is made again in seconds, not when the system
24
+ * notices; the check waits as long for its answer. Default 5000; 0 never checks.
25
+ */
26
+ stallMs?: number;
27
+ /**
28
+ * Another way to ta serve than a WebSocket to `url`, such as a relay (RelaySocket of @celljs/ta-relay):
29
+ * a new socket for each connection, which takes care of access itself.
30
+ */
31
+ createSocket?: () => TaSocket;
32
+ }
33
+ /** What TaConnection uses of a WebSocket. */
34
+ export interface TaSocket {
35
+ readonly readyState: number;
36
+ onopen: ((event?: unknown) => void) | null;
37
+ onmessage: ((message: {
38
+ data: unknown;
39
+ }) => void) | null;
40
+ onclose: ((event?: unknown) => void) | null;
41
+ onerror?: ((event?: unknown) => void) | null;
42
+ send(text: string): void;
43
+ close(): void;
44
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ta-connection-options.js.map
@@ -1,97 +1,32 @@
1
- import { type TaAccessServer, type TaAuthEvent, type TaAuthServer, TaErrorCode, type TaErrorData, type TaEventEnvelope, type TaGatewayServer, type TaPackageServer, type TaPresenceServer, 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
- */
6
- export declare class TaRemoteError extends Error {
7
- readonly code?: TaErrorCode | undefined;
8
- readonly hint?: string | undefined;
9
- readonly rpcCode?: number | undefined;
10
- /** All ta serve said of the failure: for a refused call, the action it needs (TaForbiddenData). */
11
- readonly data?: Readonly<Record<string, unknown>> | undefined;
12
- /** JSON-RPC's code for a method the server does not have. */
13
- static readonly METHOD_NOT_FOUND = -32601;
14
- constructor(message: string, code?: TaErrorCode | undefined, hint?: string | undefined, rpcCode?: number | undefined,
15
- /** All ta serve said of the failure: for a refused call, the action it needs (TaForbiddenData). */
16
- data?: Readonly<Record<string, unknown>> | undefined);
17
- /** The error of a JSON-RPC answer: its code, and the code and hint of ta serve when it named the failure. */
18
- static of(error: {
19
- message: string;
20
- code?: number;
21
- data?: {
22
- data?: TaErrorData;
23
- };
24
- }): TaRemoteError;
25
- /** What a call says that waited for a connection that did not come back. */
26
- static readonly NOT_CONNECTED = "Not connected to ta serve.";
27
- /** What a call says that was sent on a connection lost before its answer. */
28
- static readonly LOST = "The connection to ta serve was lost.";
29
- /** A call that waited for a connection that did not come back. */
30
- static notConnected(): TaRemoteError;
31
- /**
32
- * Whether a failure is only that the connection was lost, which mends itself: by its code, or by its
33
- * words in a text (a web extension shows `error.message`, often inside its own words).
34
- */
35
- static isConnectionLost(cause: unknown): boolean;
36
- /** Whether the server has no such method: a ta serve from before it was added. */
37
- static isMethodNotFound(error: unknown): boolean;
38
- }
39
- export interface TaConnectionOptions {
40
- /** The `url` of serve.json. */
41
- url: string;
42
- /** The `token` of serve.json. */
43
- token: string;
44
- /** First wait before reconnecting; it doubles up to `maxReconnectDelayMs`. Default 250. */
45
- reconnectDelayMs?: number;
46
- /** Default 10000. */
47
- maxReconnectDelayMs?: number;
48
- /**
49
- * How long a call made while the connection is down waits for it to come back before it fails, so
50
- * that a short drop goes unnoticed. Default 5000; 0 fails at once.
51
- */
52
- waitForOpenMs?: number;
53
- /**
54
- * How long a call that is safe to send again (`TaConnection.resendable`: a read, a message with its
55
- * clientMessageId) keeps trying across lost connections, waiting for each to come back, before it
56
- * fails: a drop mended within it goes unseen. Default `waitForOpenMs`.
57
- */
58
- retryForMs?: number;
59
- /**
60
- * A call unanswered this long while nothing at all came over the connection makes it check that the
61
- * connection is alive (`wake`): one gone without a word is made again in seconds, not when the system
62
- * notices; the check waits as long for its answer. Default 5000; 0 never checks.
63
- */
64
- stallMs?: number;
65
- /**
66
- * Another way to ta serve than a WebSocket to `url`, such as a relay (RelaySocket of @celljs/ta-relay):
67
- * a new socket for each connection, which takes care of access itself.
68
- */
69
- createSocket?: () => TaSocket;
70
- }
71
- /** What TaConnection uses of a WebSocket. */
72
- export interface TaSocket {
73
- readonly readyState: number;
74
- onopen: ((event?: unknown) => void) | null;
75
- onmessage: ((message: {
76
- data: unknown;
77
- }) => void) | null;
78
- onclose: ((event?: unknown) => void) | null;
79
- onerror?: ((event?: unknown) => void) | null;
80
- send(text: string): void;
81
- close(): void;
82
- }
1
+ import { type TaAccessServer, type TaAuthEvent, type TaAuthServer, type TaDesktopEvent, type TaErrorData, type TaEventEnvelope, type TaGatewayServer, type TaPackageServer, type TaPageServer, type TaPresenceServer, type TaRelayServer, type TaRemoteEvent, type TaRemoteServer, type TaRoomEvent, type TaRoomServer, type TaScreenShareServer, type TaServer, type TaSessionInfo, type TaSettingsServer, type TaTerminalServer, type TaVoiceEvent, type TaVoiceServer, type TaWorkspaceServer } from '@celljs/ta-protocol';
2
+ import type { TaConnectionOptions, TaSocket } from './ta-connection-options.js';
83
3
  interface PendingCall {
84
4
  resolve(result: unknown): void;
85
5
  reject(error: Error): void;
86
6
  /** When it was sent, on the clock of Date.now(). */
87
7
  sentAt: number;
88
8
  }
9
+ interface RpcMessage {
10
+ id?: number | string | null;
11
+ method?: string;
12
+ params?: unknown[];
13
+ result?: unknown;
14
+ error?: {
15
+ message: string;
16
+ code?: number;
17
+ data?: {
18
+ data?: TaErrorData;
19
+ };
20
+ };
21
+ }
89
22
  type EventsListener = (events: TaEventEnvelope[]) => void;
90
23
  type TerminalListener = (terminalId: string, data: string, exitCode?: number) => void;
91
24
  type ExtensionDataListener = (name: string, keys: string[], cwd?: string) => void;
92
25
  type AuthEventListener = (loginId: string, event: TaAuthEvent) => void;
93
26
  type RemoteEventListener = (event: TaRemoteEvent) => void;
94
27
  type RoomEventListener = (event: TaRoomEvent) => void;
28
+ type VoiceEventListener = (event: TaVoiceEvent) => void;
29
+ type PageAskHandler = (kind: string, payload: unknown) => Promise<unknown>;
95
30
  type SessionsListener = (sessions: TaSessionInfo[]) => void;
96
31
  type OpenListener = () => void;
97
32
  type CloseListener = () => void;
@@ -122,9 +57,15 @@ export declare class TaConnection {
122
57
  readonly gateway: TaGatewayServer;
123
58
  readonly relay: TaRelayServer;
124
59
  readonly remote: TaRemoteServer;
60
+ /** Desktops of other computers, seen and worked (docs/design/ta-desktop-remote.md). */
61
+ readonly screenShare: TaScreenShareServer;
125
62
  readonly rooms: TaRoomServer;
126
63
  /** What this page is at, for the others in the same Ta (docs/design/ta-team.md 7). */
127
64
  readonly presence: TaPresenceServer;
65
+ /** The voice services of calls (docs/design/ta-voice.md). */
66
+ readonly voice: TaVoiceServer;
67
+ /** Which requests of ta serve this page answers (onPageAsk, docs/design/ta-app-use.md 4.1). */
68
+ readonly pages: TaPageServer;
128
69
  protected socket?: TaSocket;
129
70
  protected closed: boolean;
130
71
  protected attempts: number;
@@ -137,6 +78,9 @@ export declare class TaConnection {
137
78
  protected readonly authListeners: Set<AuthEventListener>;
138
79
  protected readonly remoteListeners: Set<RemoteEventListener>;
139
80
  protected readonly roomListeners: Set<RoomEventListener>;
81
+ protected readonly voiceListeners: Set<VoiceEventListener>;
82
+ protected readonly desktopListeners: Set<(event: TaDesktopEvent) => void>;
83
+ protected pageAsk?: PageAskHandler;
140
84
  protected readonly sessionsListeners: Set<SessionsListener>;
141
85
  protected readonly openListeners: Set<OpenListener>;
142
86
  protected readonly closeListeners: Set<CloseListener>;
@@ -166,6 +110,12 @@ export declare class TaConnection {
166
110
  onRemoteEvent(listener: RemoteEventListener): () => void;
167
111
  /** What happens in the rooms, once this connection called TaRoomServer.watch. */
168
112
  onRoomEvent(listener: RoomEventListener): () => void;
113
+ /** What the voices this connection opened tell (TaVoiceServer.openVoice). */
114
+ /** Answers what ta serve asks this page (TaClient.onPageAsk): one handler, the last set; without one, an error. */
115
+ onPageAsk(handler: PageAskHandler | undefined): void;
116
+ /** What the desktops ask (a password), once this connection called TaScreenShareServer.watchDesktops. */
117
+ onScreenShareEvent(listener: (event: TaDesktopEvent) => void): () => void;
118
+ onVoiceEvent(listener: VoiceEventListener): () => void;
169
119
  /** The open sessions as they change (TaClient.onSessions), once this connection called TaServer.watchSessions. */
170
120
  onSessions(listener: SessionsListener): () => void;
171
121
  onOpen(listener: OpenListener): () => void;
@@ -193,7 +143,8 @@ export declare class TaConnection {
193
143
  protected service<T extends object>(name: string): T;
194
144
  /**
195
145
  * Whether a call may go again after a lost connection, as doing it twice does what doing it once does:
196
- * a read, or a message with its clientMessageId (TaPromptInput), which ta serve takes once.
146
+ * a read, or a message with its clientMessageId (TaPromptInput), which ta serve takes once; of a desktop, a view
147
+ * opened or text pasted by its id, input by its seq, a view by its revision (docs/design/ta-desktop-remote.md 5.3).
197
148
  */
198
149
  static resendable(service: string, method: string, params: readonly unknown[]): boolean;
199
150
  protected call(service: string, method: string, params: unknown[]): Promise<unknown>;
@@ -201,6 +152,8 @@ export declare class TaConnection {
201
152
  protected send(service: string, method: string, params: unknown[]): Promise<unknown>;
202
153
  /** While calls wait for answers: one that waits past `stallMs` with nothing come since makes it check the connection. */
203
154
  protected watchStalls(): void;
155
+ /** What the page answers to a request of ta serve, on the socket it came on. */
156
+ protected answerPage(id: RpcMessage['id'], kind: string, payload: unknown): Promise<void>;
204
157
  protected receive(text: string): void;
205
158
  }
206
159
  export {};