@celljs/ta-client 4.16.0 → 4.17.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.
- package/esm/common/index.d.ts +4 -0
- package/esm/common/index.js +4 -0
- package/esm/common/session-client-tools.d.ts +32 -0
- package/esm/common/session-client-tools.js +72 -0
- package/esm/common/session-reducer.d.ts +6 -1
- package/esm/common/session-store-options.d.ts +26 -0
- package/esm/common/session-store-options.js +3 -0
- package/esm/common/session-store.d.ts +4 -50
- package/esm/common/session-store.js +12 -74
- package/esm/common/ta-connection-options.d.ts +44 -0
- package/esm/common/ta-connection-options.js +2 -0
- package/esm/common/ta-connection.d.ts +29 -82
- package/esm/common/ta-connection.js +49 -49
- package/esm/common/ta-remote-error.d.ts +38 -0
- package/esm/common/ta-remote-error.js +48 -0
- package/lib/common/index.d.ts +4 -0
- package/lib/common/index.js +4 -0
- package/lib/common/session-client-tools.d.ts +33 -0
- package/lib/common/session-client-tools.js +76 -0
- package/lib/common/session-reducer.d.ts +6 -1
- package/lib/common/session-store-options.d.ts +27 -0
- package/lib/common/session-store-options.js +6 -0
- package/lib/common/session-store.d.ts +4 -50
- package/lib/common/session-store.js +16 -78
- package/lib/common/ta-connection-options.d.ts +45 -0
- package/lib/common/ta-connection-options.js +3 -0
- package/lib/common/ta-connection.d.ts +29 -82
- package/lib/common/ta-connection.js +54 -55
- package/lib/common/ta-remote-error.d.ts +39 -0
- package/lib/common/ta-remote-error.js +52 -0
- package/package.json +2 -2
|
@@ -1,50 +1,5 @@
|
|
|
1
|
-
import { TA_ACCESS_SERVICE, TA_AUTH_SERVICE, TA_CLIENT_SERVICE, TA_GATEWAY_SERVICE, TA_PACKAGE_SERVICE, TA_PRESENCE_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
|
-
*/
|
|
6
|
-
export class TaRemoteError extends Error {
|
|
7
|
-
constructor(message, code, hint, rpcCode,
|
|
8
|
-
/** All ta serve said of the failure: for a refused call, the action it needs (TaForbiddenData). */
|
|
9
|
-
data) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.code = code;
|
|
12
|
-
this.hint = hint;
|
|
13
|
-
this.rpcCode = rpcCode;
|
|
14
|
-
this.data = data;
|
|
15
|
-
this.name = 'TaRemoteError';
|
|
16
|
-
}
|
|
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
|
-
var _a;
|
|
20
|
-
const data = (_a = error.data) === null || _a === void 0 ? void 0 : _a.data;
|
|
21
|
-
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, data);
|
|
22
|
-
}
|
|
23
|
-
/** A call that waited for a connection that did not come back. */
|
|
24
|
-
static notConnected() {
|
|
25
|
-
return new TaRemoteError(TaRemoteError.NOT_CONNECTED, TaErrorCode.CONNECTION_LOST, 'Try again once the connection is back.');
|
|
26
|
-
}
|
|
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) {
|
|
32
|
-
if (typeof cause === 'string') {
|
|
33
|
-
return cause.includes(TaRemoteError.NOT_CONNECTED) || cause.includes(TaRemoteError.LOST);
|
|
34
|
-
}
|
|
35
|
-
return (cause === null || cause === void 0 ? void 0 : cause.code) === TaErrorCode.CONNECTION_LOST;
|
|
36
|
-
}
|
|
37
|
-
/** Whether the server has no such method: a ta serve from before it was added. */
|
|
38
|
-
static isMethodNotFound(error) {
|
|
39
|
-
return error instanceof TaRemoteError && error.rpcCode === TaRemoteError.METHOD_NOT_FOUND;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/** JSON-RPC's code for a method the server does not have. */
|
|
43
|
-
TaRemoteError.METHOD_NOT_FOUND = -32601;
|
|
44
|
-
/** What a call says that waited for a connection that did not come back. */
|
|
45
|
-
TaRemoteError.NOT_CONNECTED = 'Not connected to ta serve.';
|
|
46
|
-
/** What a call says that was sent on a connection lost before its answer. */
|
|
47
|
-
TaRemoteError.LOST = 'The connection to ta serve was lost.';
|
|
1
|
+
import { TA_ACCESS_SERVICE, TA_AUTH_SERVICE, TA_CLIENT_SERVICE, TA_GATEWAY_SERVICE, TA_PACKAGE_SERVICE, TA_PAGE_SERVICE, TA_PRESENCE_SERVICE, TA_RELAY_SERVICE, TA_REMOTE_SERVICE, TA_ROOM_SERVICE, TA_SERVER_SERVICE, TA_SETTINGS_SERVICE, TA_TERMINAL_SERVICE, TA_VOICE_SERVICE, TA_WORKSPACE_SERVICE, TaErrorCode, } from '@celljs/ta-protocol';
|
|
2
|
+
import { TaRemoteError } from './ta-remote-error.js';
|
|
48
3
|
/** Methods that only read: sent again after a lost connection. */
|
|
49
4
|
const READ_METHOD = /^(get|list|read|find)[A-Z]/;
|
|
50
5
|
/** Methods whose input carries a clientMessageId, by which ta serve takes a message once however often it comes. */
|
|
@@ -79,6 +34,10 @@ export class TaConnection {
|
|
|
79
34
|
this.rooms = this.service(TA_ROOM_SERVICE);
|
|
80
35
|
/** What this page is at, for the others in the same Ta (docs/design/ta-team.md 7). */
|
|
81
36
|
this.presence = this.service(TA_PRESENCE_SERVICE);
|
|
37
|
+
/** The voice services of calls (docs/design/ta-voice.md). */
|
|
38
|
+
this.voice = this.service(TA_VOICE_SERVICE);
|
|
39
|
+
/** Which requests of ta serve this page answers (onPageAsk, docs/design/ta-app-use.md 4.1). */
|
|
40
|
+
this.pages = this.service(TA_PAGE_SERVICE);
|
|
82
41
|
this.closed = false;
|
|
83
42
|
this.attempts = 0;
|
|
84
43
|
this.nextId = 0;
|
|
@@ -89,6 +48,7 @@ export class TaConnection {
|
|
|
89
48
|
this.authListeners = new Set();
|
|
90
49
|
this.remoteListeners = new Set();
|
|
91
50
|
this.roomListeners = new Set();
|
|
51
|
+
this.voiceListeners = new Set();
|
|
92
52
|
this.sessionsListeners = new Set();
|
|
93
53
|
this.openListeners = new Set();
|
|
94
54
|
this.closeListeners = new Set();
|
|
@@ -142,6 +102,15 @@ export class TaConnection {
|
|
|
142
102
|
this.roomListeners.add(listener);
|
|
143
103
|
return () => this.roomListeners.delete(listener);
|
|
144
104
|
}
|
|
105
|
+
/** What the voices this connection opened tell (TaVoiceServer.openVoice). */
|
|
106
|
+
/** Answers what ta serve asks this page (TaClient.onPageAsk): one handler, the last set; without one, an error. */
|
|
107
|
+
onPageAsk(handler) {
|
|
108
|
+
this.pageAsk = handler;
|
|
109
|
+
}
|
|
110
|
+
onVoiceEvent(listener) {
|
|
111
|
+
this.voiceListeners.add(listener);
|
|
112
|
+
return () => this.voiceListeners.delete(listener);
|
|
113
|
+
}
|
|
145
114
|
/** The open sessions as they change (TaClient.onSessions), once this connection called TaServer.watchSessions. */
|
|
146
115
|
onSessions(listener) {
|
|
147
116
|
this.sessionsListeners.add(listener);
|
|
@@ -386,8 +355,26 @@ export class TaConnection {
|
|
|
386
355
|
}
|
|
387
356
|
}, Math.min(1000, stallMs));
|
|
388
357
|
}
|
|
358
|
+
/** What the page answers to a request of ta serve, on the socket it came on. */
|
|
359
|
+
async answerPage(id, kind, payload) {
|
|
360
|
+
var _a;
|
|
361
|
+
const socket = this.socket;
|
|
362
|
+
let reply;
|
|
363
|
+
try {
|
|
364
|
+
if (!this.pageAsk) {
|
|
365
|
+
throw new Error('This page answers nothing.');
|
|
366
|
+
}
|
|
367
|
+
reply = { result: (_a = (await this.pageAsk(kind, payload))) !== null && _a !== void 0 ? _a : null };
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
reply = { error: { code: -32000, message: error instanceof Error ? error.message : String(error) } };
|
|
371
|
+
}
|
|
372
|
+
if (socket === this.socket) {
|
|
373
|
+
socket === null || socket === void 0 ? void 0 : socket.send(JSON.stringify({ jsonrpc: '2.0', id, ...reply }));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
389
376
|
receive(text) {
|
|
390
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
377
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
391
378
|
this.lastReceivedAt = Date.now();
|
|
392
379
|
const message = JSON.parse(text);
|
|
393
380
|
if (message.method === `${TA_CLIENT_SERVICE}/onEvents`) {
|
|
@@ -439,12 +426,25 @@ export class TaConnection {
|
|
|
439
426
|
(_f = this.socket) === null || _f === void 0 ? void 0 : _f.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
|
|
440
427
|
return;
|
|
441
428
|
}
|
|
429
|
+
if (message.method === `${TA_CLIENT_SERVICE}/onVoiceEvent`) {
|
|
430
|
+
const [event] = message.params;
|
|
431
|
+
for (const listener of this.voiceListeners) {
|
|
432
|
+
listener(event);
|
|
433
|
+
}
|
|
434
|
+
(_g = this.socket) === null || _g === void 0 ? void 0 : _g.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (message.method === `${TA_CLIENT_SERVICE}/onPageAsk`) {
|
|
438
|
+
const [kind, payload] = message.params;
|
|
439
|
+
void this.answerPage(message.id, kind, payload);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
442
|
if (message.method === `${TA_CLIENT_SERVICE}/onExtensionData`) {
|
|
443
443
|
const [name, keys, cwd] = message.params;
|
|
444
444
|
for (const listener of this.extensionDataListeners) {
|
|
445
445
|
listener(name, keys, cwd !== null && cwd !== void 0 ? cwd : undefined);
|
|
446
446
|
}
|
|
447
|
-
(
|
|
447
|
+
(_h = this.socket) === null || _h === void 0 ? void 0 : _h.send(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: null }));
|
|
448
448
|
return;
|
|
449
449
|
}
|
|
450
450
|
const call = this.pending.get(message.id);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { TaErrorCode, type TaErrorData } 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
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { 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
|
+
*/
|
|
6
|
+
export class TaRemoteError extends Error {
|
|
7
|
+
constructor(message, code, hint, rpcCode,
|
|
8
|
+
/** All ta serve said of the failure: for a refused call, the action it needs (TaForbiddenData). */
|
|
9
|
+
data) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.hint = hint;
|
|
13
|
+
this.rpcCode = rpcCode;
|
|
14
|
+
this.data = data;
|
|
15
|
+
this.name = 'TaRemoteError';
|
|
16
|
+
}
|
|
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
|
+
var _a;
|
|
20
|
+
const data = (_a = error.data) === null || _a === void 0 ? void 0 : _a.data;
|
|
21
|
+
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, data);
|
|
22
|
+
}
|
|
23
|
+
/** A call that waited for a connection that did not come back. */
|
|
24
|
+
static notConnected() {
|
|
25
|
+
return new TaRemoteError(TaRemoteError.NOT_CONNECTED, TaErrorCode.CONNECTION_LOST, 'Try again once the connection is back.');
|
|
26
|
+
}
|
|
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) {
|
|
32
|
+
if (typeof cause === 'string') {
|
|
33
|
+
return cause.includes(TaRemoteError.NOT_CONNECTED) || cause.includes(TaRemoteError.LOST);
|
|
34
|
+
}
|
|
35
|
+
return (cause === null || cause === void 0 ? void 0 : cause.code) === TaErrorCode.CONNECTION_LOST;
|
|
36
|
+
}
|
|
37
|
+
/** Whether the server has no such method: a ta serve from before it was added. */
|
|
38
|
+
static isMethodNotFound(error) {
|
|
39
|
+
return error instanceof TaRemoteError && error.rpcCode === TaRemoteError.METHOD_NOT_FOUND;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** JSON-RPC's code for a method the server does not have. */
|
|
43
|
+
TaRemoteError.METHOD_NOT_FOUND = -32601;
|
|
44
|
+
/** What a call says that waited for a connection that did not come back. */
|
|
45
|
+
TaRemoteError.NOT_CONNECTED = 'Not connected to ta serve.';
|
|
46
|
+
/** What a call says that was sent on a connection lost before its answer. */
|
|
47
|
+
TaRemoteError.LOST = 'The connection to ta serve was lost.';
|
|
48
|
+
//# sourceMappingURL=ta-remote-error.js.map
|
package/lib/common/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
export * from './session-client-tools';
|
|
1
2
|
export * from './session-reducer';
|
|
2
3
|
export * from './session-store';
|
|
4
|
+
export * from './session-store-options';
|
|
3
5
|
export * from './ta-connection';
|
|
6
|
+
export * from './ta-connection-options';
|
|
7
|
+
export * from './ta-remote-error';
|
|
4
8
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/common/index.js
CHANGED
|
@@ -14,7 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./session-client-tools"), exports);
|
|
17
18
|
__exportStar(require("./session-reducer"), exports);
|
|
18
19
|
__exportStar(require("./session-store"), exports);
|
|
20
|
+
__exportStar(require("./session-store-options"), exports);
|
|
19
21
|
__exportStar(require("./ta-connection"), exports);
|
|
22
|
+
__exportStar(require("./ta-connection-options"), exports);
|
|
23
|
+
__exportStar(require("./ta-remote-error"), exports);
|
|
20
24
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { TaClientToolDefinition, TaClientToolRequest, TaClientToolResult } from '@celljs/ta-protocol';
|
|
2
|
+
import type { SessionState } from './session-reducer';
|
|
3
|
+
import type { SessionConnection } from './session-store-options';
|
|
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
|
+
}
|
|
33
|
+
//# sourceMappingURL=session-client-tools.d.ts.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SessionClientTools = void 0;
|
|
4
|
+
const ta_remote_error_1 = require("./ta-remote-error");
|
|
5
|
+
class SessionClientTools {
|
|
6
|
+
constructor() {
|
|
7
|
+
/** Calls of client tools this store is running or answering now, by id. */
|
|
8
|
+
this.running = new Set();
|
|
9
|
+
/** Results not yet taken by the server, by call id. */
|
|
10
|
+
this.results = new Map();
|
|
11
|
+
/** Calls another client took, by id: tried again only when the request comes again (that client left). */
|
|
12
|
+
this.declined = new Map();
|
|
13
|
+
}
|
|
14
|
+
runClientTools(state) {
|
|
15
|
+
for (const request of state.pendingClientTools) {
|
|
16
|
+
const tool = this.clientTools.get(request.toolName);
|
|
17
|
+
if (tool && !this.running.has(request.id) && this.declined.get(request.id) !== request) {
|
|
18
|
+
this.running.add(request.id);
|
|
19
|
+
void this.answer(tool, request);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
for (const calls of [this.results, this.declined]) {
|
|
23
|
+
for (const id of calls.keys()) {
|
|
24
|
+
if (!state.pendingClientTools.some(request => request.id === id)) {
|
|
25
|
+
calls.delete(id);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Runs a call once, if this client takes it: every page of the user gets it. A result that could not be
|
|
32
|
+
* sent is sent again after a reconnect, not made again.
|
|
33
|
+
*/
|
|
34
|
+
async answer(tool, request) {
|
|
35
|
+
try {
|
|
36
|
+
let result = this.results.get(request.id);
|
|
37
|
+
if (!result) {
|
|
38
|
+
// Not asked again before the request comes again, also when the connection was lost: the reconnect brings it.
|
|
39
|
+
this.declined.set(request.id, request);
|
|
40
|
+
if (!(await this.claim(request))) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.declined.delete(request.id);
|
|
44
|
+
result = await this.run(tool, request);
|
|
45
|
+
this.results.set(request.id, result);
|
|
46
|
+
}
|
|
47
|
+
await this.connection.server.respondClientTool(this.sessionId, result);
|
|
48
|
+
this.results.delete(request.id);
|
|
49
|
+
}
|
|
50
|
+
catch (_a) {
|
|
51
|
+
// The connection was lost; the reconnect sends the result if the call still waits.
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
this.running.delete(request.id);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** 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. */
|
|
58
|
+
claim(request) {
|
|
59
|
+
return this.connection.server.claimClientTool(this.sessionId, request.id).catch(error => {
|
|
60
|
+
if (ta_remote_error_1.TaRemoteError.isMethodNotFound(error)) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
throw error;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async run(tool, request) {
|
|
67
|
+
try {
|
|
68
|
+
return { id: request.id, output: await tool.run(request.input, request) };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
return { id: request.id, output: error instanceof Error ? error.message : String(error), isError: true };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.SessionClientTools = SessionClientTools;
|
|
76
|
+
//# 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. */
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ClientTool } from './session-client-tools';
|
|
2
|
+
import type { SessionState } from './session-reducer';
|
|
3
|
+
import type { TaConnection } from './ta-connection';
|
|
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
|
+
}
|
|
27
|
+
//# sourceMappingURL=session-store-options.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HOLD_MS = void 0;
|
|
4
|
+
/** How long a message the connection could not take waits for it, on its way, before it shows as failed. */
|
|
5
|
+
exports.HOLD_MS = 60000;
|
|
6
|
+
//# sourceMappingURL=session-store-options.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type RpcExtensionUIResponse, type TaAttachResult, type
|
|
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';
|
|
2
3
|
import { type OutboxMessage, type SessionMessage, type SessionState } from './session-reducer';
|
|
3
|
-
import { type
|
|
4
|
+
import { type SessionConnection, type SessionSnapshot, type SessionStoreOptions } from './session-store-options';
|
|
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,15 +133,6 @@ 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 {};
|
|
184
138
|
//# sourceMappingURL=session-store.d.ts.map
|