@celljs/ta-client 4.15.2 → 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
package/esm/common/index.d.ts
CHANGED
|
@@ -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';
|
package/esm/common/index.js
CHANGED
|
@@ -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. */
|
|
@@ -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
|
+
}
|
|
@@ -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.js';
|
|
2
3
|
import { type OutboxMessage, type SessionMessage, type SessionState } from './session-reducer.js';
|
|
3
|
-
import { type
|
|
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 {
|
|
4
|
-
|
|
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]));
|
|
@@ -270,7 +265,7 @@ export class SessionStore {
|
|
|
270
265
|
return this.connection.server.respondUi(this.sessionId, response);
|
|
271
266
|
}
|
|
272
267
|
async attachAgain(attached) {
|
|
273
|
-
var _a, _b;
|
|
268
|
+
var _a, _b, _c;
|
|
274
269
|
const result = attached !== null && attached !== void 0 ? attached : (await this.connection.server.attach(this.sessionId, this.lastSeq, this.pageBytes ? { recentBytes: this.pageBytes } : undefined));
|
|
275
270
|
let state = this.current;
|
|
276
271
|
// A conversation given (one attached as it opened) is the session's from scratch, whatever this store had.
|
|
@@ -284,6 +279,8 @@ export class SessionStore {
|
|
|
284
279
|
messages: kept,
|
|
285
280
|
tools: conversation.tools,
|
|
286
281
|
start,
|
|
282
|
+
// Those loaded further up kept: the head they start in, as it was.
|
|
283
|
+
head: start === ((_c = conversation.start) !== null && _c !== void 0 ? _c : 0) ? conversation.head : this.current.head,
|
|
287
284
|
runStart: kept === this.current.messages ? this.current.runStart : undefined,
|
|
288
285
|
};
|
|
289
286
|
this.lastSeq = result.lastSeq;
|
|
@@ -370,15 +367,16 @@ export class SessionStore {
|
|
|
370
367
|
...this.current,
|
|
371
368
|
messages: [...page.messages, ...this.current.messages],
|
|
372
369
|
start: page.start,
|
|
370
|
+
head: page.head,
|
|
373
371
|
loadingEarlier: false,
|
|
374
372
|
runStart: runStart === undefined ? undefined : runStart + page.messages.length,
|
|
375
373
|
});
|
|
376
374
|
}
|
|
377
375
|
async reload() {
|
|
378
|
-
const { messages, start } = this.pageBytes
|
|
376
|
+
const { messages, start, head } = this.pageBytes
|
|
379
377
|
? 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 });
|
|
378
|
+
: { ...(await this.connection.server.getMessages(this.sessionId)), start: 0, head: undefined };
|
|
379
|
+
this.update({ ...this.current, messages, start, head, tools: {}, runStart: undefined });
|
|
382
380
|
}
|
|
383
381
|
apply(events) {
|
|
384
382
|
const taken = this.take(this.current, events);
|
|
@@ -436,65 +434,5 @@ export class SessionStore {
|
|
|
436
434
|
listener(shown);
|
|
437
435
|
}
|
|
438
436
|
}
|
|
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
437
|
}
|
|
500
438
|
//# 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
|
+
}
|
|
@@ -1,97 +1,32 @@
|
|
|
1
|
-
import { type TaAccessServer, type TaAuthEvent, type TaAuthServer,
|
|
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 TaErrorData, type TaEventEnvelope, type TaGatewayServer, type TaPackageServer, type TaPageServer, type TaPresenceServer, type TaRelayServer, type TaRemoteEvent, type TaRemoteServer, type TaRoomEvent, type TaRoomServer, 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;
|
|
@@ -125,6 +60,10 @@ export declare class TaConnection {
|
|
|
125
60
|
readonly rooms: TaRoomServer;
|
|
126
61
|
/** What this page is at, for the others in the same Ta (docs/design/ta-team.md 7). */
|
|
127
62
|
readonly presence: TaPresenceServer;
|
|
63
|
+
/** The voice services of calls (docs/design/ta-voice.md). */
|
|
64
|
+
readonly voice: TaVoiceServer;
|
|
65
|
+
/** Which requests of ta serve this page answers (onPageAsk, docs/design/ta-app-use.md 4.1). */
|
|
66
|
+
readonly pages: TaPageServer;
|
|
128
67
|
protected socket?: TaSocket;
|
|
129
68
|
protected closed: boolean;
|
|
130
69
|
protected attempts: number;
|
|
@@ -137,6 +76,8 @@ export declare class TaConnection {
|
|
|
137
76
|
protected readonly authListeners: Set<AuthEventListener>;
|
|
138
77
|
protected readonly remoteListeners: Set<RemoteEventListener>;
|
|
139
78
|
protected readonly roomListeners: Set<RoomEventListener>;
|
|
79
|
+
protected readonly voiceListeners: Set<VoiceEventListener>;
|
|
80
|
+
protected pageAsk?: PageAskHandler;
|
|
140
81
|
protected readonly sessionsListeners: Set<SessionsListener>;
|
|
141
82
|
protected readonly openListeners: Set<OpenListener>;
|
|
142
83
|
protected readonly closeListeners: Set<CloseListener>;
|
|
@@ -166,6 +107,10 @@ export declare class TaConnection {
|
|
|
166
107
|
onRemoteEvent(listener: RemoteEventListener): () => void;
|
|
167
108
|
/** What happens in the rooms, once this connection called TaRoomServer.watch. */
|
|
168
109
|
onRoomEvent(listener: RoomEventListener): () => void;
|
|
110
|
+
/** What the voices this connection opened tell (TaVoiceServer.openVoice). */
|
|
111
|
+
/** Answers what ta serve asks this page (TaClient.onPageAsk): one handler, the last set; without one, an error. */
|
|
112
|
+
onPageAsk(handler: PageAskHandler | undefined): void;
|
|
113
|
+
onVoiceEvent(listener: VoiceEventListener): () => void;
|
|
169
114
|
/** The open sessions as they change (TaClient.onSessions), once this connection called TaServer.watchSessions. */
|
|
170
115
|
onSessions(listener: SessionsListener): () => void;
|
|
171
116
|
onOpen(listener: OpenListener): () => void;
|
|
@@ -201,6 +146,8 @@ export declare class TaConnection {
|
|
|
201
146
|
protected send(service: string, method: string, params: unknown[]): Promise<unknown>;
|
|
202
147
|
/** While calls wait for answers: one that waits past `stallMs` with nothing come since makes it check the connection. */
|
|
203
148
|
protected watchStalls(): void;
|
|
149
|
+
/** What the page answers to a request of ta serve, on the socket it came on. */
|
|
150
|
+
protected answerPage(id: RpcMessage['id'], kind: string, payload: unknown): Promise<void>;
|
|
204
151
|
protected receive(text: string): void;
|
|
205
152
|
}
|
|
206
153
|
export {};
|