@celljs/ta-client 4.5.1 → 4.6.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/README.md +16 -3
- package/esm/common/session-reducer.d.ts +26 -1
- package/esm/common/session-reducer.js +44 -9
- package/esm/common/session-reducer.js.map +1 -1
- package/esm/common/session-store.d.ts +27 -2
- package/esm/common/session-store.js +62 -2
- package/esm/common/session-store.js.map +1 -1
- package/esm/common/ta-connection.d.ts +54 -5
- package/esm/common/ta-connection.js +104 -17
- package/esm/common/ta-connection.js.map +1 -1
- package/lib/common/session-reducer.d.ts +26 -1
- package/lib/common/session-reducer.d.ts.map +1 -1
- package/lib/common/session-reducer.js +44 -9
- package/lib/common/session-reducer.js.map +1 -1
- package/lib/common/session-reducer.spec.js +37 -0
- package/lib/common/session-reducer.spec.js.map +1 -1
- package/lib/common/session-store.d.ts +27 -2
- package/lib/common/session-store.d.ts.map +1 -1
- package/lib/common/session-store.js +62 -2
- package/lib/common/session-store.js.map +1 -1
- package/lib/common/ta-connection.d.ts +54 -5
- package/lib/common/ta-connection.d.ts.map +1 -1
- package/lib/common/ta-connection.js +103 -16
- package/lib/common/ta-connection.js.map +1 -1
- package/package.json +2 -2
- package/src/common/session-reducer.spec.ts +42 -0
- package/src/common/session-reducer.ts +72 -9
- package/src/common/session-store.ts +92 -3
- package/src/common/ta-connection.ts +156 -20
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Cell -
|
|
1
|
+
# Cell - Ta Client
|
|
2
2
|
|
|
3
3
|
`ta serve` 的客户端:只依赖平台自带的 `WebSocket`,可用于浏览器、Electron、移动端和 Node.js >= 22.4。
|
|
4
4
|
|
|
@@ -15,6 +15,19 @@ await store.attach();
|
|
|
15
15
|
await store.prompt('修复失败的测试');
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
- `TaConnection`:断线后自动重连(指数退避);`connection.server`
|
|
18
|
+
- `TaConnection`:断线后自动重连(指数退避);`onReconnecting(delayMs, attempt)` 在每次等待前通知,`reconnect()` 跳过等待立即重试。`connection.server`(会话)、`workspace`、`terminals`、`settings`、`packages` 是 ta serve 各服务的带类型代理,共用这一条连接。断线期间的调用直接失败(`TaRemoteError`,`code` 为 `CELL_TA_CONNECTION_LOST`),不排队,是否重发由调用方决定(重发请复用 `clientMessageId`)。
|
|
19
19
|
- `SessionStore`:一个会话在客户端的状态(消息、工具执行、待回答的对话框、扩展状态栏)。重连后按 `seq` 补发错过的事件;补不上时(`gap`)自动用 `getMessages` 重新同步。
|
|
20
|
-
- `
|
|
20
|
+
- 客户端工具:模型可以调用由客户端执行的工具(例如读剪贴板、让用户在界面里选择)。打开会话时声明,`SessionStore` 负责执行并回传结果:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const pickColor: ClientTool = {
|
|
24
|
+
definition: { name: 'pick_color', description: '请用户选一个颜色', parameters: { type: 'object', properties: {} } },
|
|
25
|
+
run: async () => await showColorPicker(), // 返回给模型的文本;抛错即工具失败
|
|
26
|
+
};
|
|
27
|
+
const { sessionId } = await connection.server.openSession({ cwd, clientTools: [pickColor.definition] });
|
|
28
|
+
const store = new SessionStore(connection, sessionId, { clientTools: [pickColor] });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
调用请求广播给所有已连接的客户端,第一个结果生效(与对话框相同);同一调用在一个客户端只执行一次,断线后重发已算好的结果而不是再执行一次。run 被中止或会话关闭时调用以失败结束。codemode 脚本里同样可以 `tools.pick_color()`。
|
|
32
|
+
- `openSession` 的 `appendSystemPrompt` 给会话追加一段 system prompt(同 pi 的 `--append-system-prompt`),客户端可以借此给 Agent 一个角色。
|
|
33
|
+
- `SessionReducer`:纯函数 `(state, event) => state`,不改动旧状态,UI 可按引用比较。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RpcExtensionUIRequest, TaCommandResult, TaEvent } from '@celljs/ta-protocol';
|
|
1
|
+
import type { RpcExtensionUIRequest, TaClientToolRequest, TaCommandResult, TaEvent } from '@celljs/ta-protocol';
|
|
2
2
|
export type SessionMessage = TaCommandResult<'get_messages'>['messages'][number];
|
|
3
3
|
export interface ToolRun {
|
|
4
4
|
toolName: string;
|
|
@@ -6,6 +6,21 @@ export interface ToolRun {
|
|
|
6
6
|
/** The latest partial result while it runs, then the result. */
|
|
7
7
|
result?: unknown;
|
|
8
8
|
status: 'running' | 'done' | 'error';
|
|
9
|
+
/** Set for a call another tool made, such as a codemode script. */
|
|
10
|
+
parentToolCallId?: string;
|
|
11
|
+
}
|
|
12
|
+
/** A message of `notify`. */
|
|
13
|
+
export interface Notice {
|
|
14
|
+
id: string;
|
|
15
|
+
message: string;
|
|
16
|
+
type: 'info' | 'warning' | 'error';
|
|
17
|
+
}
|
|
18
|
+
/** pi retries a failed request of the model by itself: `auto_retry_start`. */
|
|
19
|
+
export interface RetryState {
|
|
20
|
+
attempt: number;
|
|
21
|
+
maxAttempts: number;
|
|
22
|
+
delayMs: number;
|
|
23
|
+
errorMessage: string;
|
|
9
24
|
}
|
|
10
25
|
/** What a UI shows of a session. */
|
|
11
26
|
export interface SessionState {
|
|
@@ -16,9 +31,19 @@ export interface SessionState {
|
|
|
16
31
|
tools: Record<string, ToolRun>;
|
|
17
32
|
/** Dialogs waiting for an answer. */
|
|
18
33
|
pendingUi: RpcExtensionUIRequest[];
|
|
34
|
+
/** Calls of client tools waiting for a result. */
|
|
35
|
+
pendingClientTools: TaClientToolRequest[];
|
|
36
|
+
/** The latest messages of `notify`, oldest first. */
|
|
37
|
+
notices: Notice[];
|
|
19
38
|
/** What extensions show with `setStatus`. */
|
|
20
39
|
status: Record<string, string>;
|
|
21
40
|
closed: boolean;
|
|
41
|
+
/** The index in `messages` where the run of now starts: unknown for a run that started before this client attached. */
|
|
42
|
+
runStart?: number;
|
|
43
|
+
/** pi compacts the conversation, for this reason. */
|
|
44
|
+
compacting?: 'manual' | 'threshold' | 'overflow';
|
|
45
|
+
/** pi waits to retry a failed request of the model. */
|
|
46
|
+
retry?: RetryState;
|
|
22
47
|
}
|
|
23
48
|
export declare namespace SessionReducer {
|
|
24
49
|
function initial(): SessionState;
|
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
export var SessionReducer;
|
|
2
2
|
(function (SessionReducer) {
|
|
3
3
|
const DIALOGS = new Set(['select', 'confirm', 'input', 'editor']);
|
|
4
|
+
const MAX_NOTICES = 20;
|
|
4
5
|
function initial() {
|
|
5
|
-
return { working: false, messages: [], tools: {}, pendingUi: [], status: {}, closed: false };
|
|
6
|
+
return { working: false, messages: [], tools: {}, pendingUi: [], pendingClientTools: [], notices: [], status: {}, closed: false };
|
|
6
7
|
}
|
|
7
8
|
SessionReducer.initial = initial;
|
|
8
9
|
/** The next state; the previous one stays valid, so that UIs can compare by identity. */
|
|
9
10
|
function reduce(state, event) {
|
|
10
11
|
switch (event.type) {
|
|
11
12
|
case 'agent_start':
|
|
12
|
-
return { ...state, working: true };
|
|
13
|
+
return { ...state, working: true, runStart: state.messages.length };
|
|
13
14
|
case 'agent_settled':
|
|
14
|
-
return { ...state, working: false };
|
|
15
|
+
return { ...state, working: false, retry: undefined };
|
|
15
16
|
case 'message_start':
|
|
16
17
|
return { ...state, messages: [...state.messages, event.message] };
|
|
17
|
-
case 'message_update':
|
|
18
|
-
|
|
18
|
+
case 'message_update': {
|
|
19
|
+
// The usage so far: some APIs count while they stream, others only at the end.
|
|
20
|
+
const { usage } = event;
|
|
21
|
+
return withLastMessage(state, message => withUsage(applyDelta(message, event.assistantMessageEvent), usage));
|
|
22
|
+
}
|
|
19
23
|
case 'message_end':
|
|
20
24
|
return withLastMessage(state, () => event.message);
|
|
21
|
-
case 'tool_execution_start':
|
|
22
|
-
|
|
25
|
+
case 'tool_execution_start': {
|
|
26
|
+
const { parentToolCallId } = event;
|
|
27
|
+
const run = { toolName: event.toolName, args: event.args, status: 'running' };
|
|
28
|
+
return withTool(state, event.toolCallId, parentToolCallId ? { ...run, parentToolCallId } : run);
|
|
29
|
+
}
|
|
23
30
|
case 'tool_execution_update':
|
|
24
31
|
return withTool(state, event.toolCallId, { ...state.tools[event.toolCallId], result: event.partialResult });
|
|
25
32
|
case 'tool_execution_end':
|
|
@@ -28,8 +35,22 @@ export var SessionReducer;
|
|
|
28
35
|
return applyUiRequest(state, event);
|
|
29
36
|
case 'ui_request_resolved':
|
|
30
37
|
return { ...state, pendingUi: state.pendingUi.filter(request => request.id !== event.id) };
|
|
38
|
+
case 'client_tool_request':
|
|
39
|
+
return { ...state, pendingClientTools: [...state.pendingClientTools, event] };
|
|
40
|
+
case 'client_tool_resolved':
|
|
41
|
+
return { ...state, pendingClientTools: state.pendingClientTools.filter(request => request.id !== event.id) };
|
|
42
|
+
case 'compaction_start':
|
|
43
|
+
return { ...state, compacting: event.reason };
|
|
44
|
+
case 'compaction_end':
|
|
45
|
+
return { ...state, compacting: undefined };
|
|
46
|
+
case 'auto_retry_start': {
|
|
47
|
+
const { attempt, maxAttempts, delayMs, errorMessage } = event;
|
|
48
|
+
return { ...state, retry: { attempt, maxAttempts, delayMs, errorMessage } };
|
|
49
|
+
}
|
|
50
|
+
case 'auto_retry_end':
|
|
51
|
+
return { ...state, retry: undefined };
|
|
31
52
|
case 'session_closed':
|
|
32
|
-
return { ...state, closed: true, working: false, pendingUi: [] };
|
|
53
|
+
return { ...state, closed: true, working: false, pendingUi: [], pendingClientTools: [] };
|
|
33
54
|
default:
|
|
34
55
|
return state;
|
|
35
56
|
}
|
|
@@ -42,13 +63,24 @@ export var SessionReducer;
|
|
|
42
63
|
const last = state.messages.length - 1;
|
|
43
64
|
return { ...state, messages: [...state.messages.slice(0, last), update(state.messages[last])] };
|
|
44
65
|
}
|
|
66
|
+
function withUsage(message, usage) {
|
|
67
|
+
if (!usage || message.role !== 'assistant' || message.usage === usage) {
|
|
68
|
+
return message;
|
|
69
|
+
}
|
|
70
|
+
return { ...message, usage };
|
|
71
|
+
}
|
|
45
72
|
function withTool(state, toolCallId, run) {
|
|
46
73
|
return { ...state, tools: { ...state.tools, [toolCallId]: run } };
|
|
47
74
|
}
|
|
48
75
|
function applyUiRequest(state, request) {
|
|
76
|
+
var _a;
|
|
49
77
|
if (DIALOGS.has(request.method)) {
|
|
50
78
|
return { ...state, pendingUi: [...state.pendingUi, request] };
|
|
51
79
|
}
|
|
80
|
+
if (request.method === 'notify') {
|
|
81
|
+
const notice = { id: request.id, message: request.message, type: (_a = request.notifyType) !== null && _a !== void 0 ? _a : 'info' };
|
|
82
|
+
return { ...state, notices: [...state.notices, notice].slice(-MAX_NOTICES) };
|
|
83
|
+
}
|
|
52
84
|
if (request.method === 'setStatus') {
|
|
53
85
|
const { [request.statusKey]: _old, ...status } = state.status;
|
|
54
86
|
return { ...state, status: request.statusText === undefined ? status : { ...status, [request.statusKey]: request.statusText } };
|
|
@@ -67,7 +99,7 @@ export var SessionReducer;
|
|
|
67
99
|
return { ...message, content };
|
|
68
100
|
}
|
|
69
101
|
function nextBlock(block, delta) {
|
|
70
|
-
var _a, _b;
|
|
102
|
+
var _a, _b, _c;
|
|
71
103
|
switch (delta.type) {
|
|
72
104
|
case 'text_start':
|
|
73
105
|
return { type: 'text', text: '' };
|
|
@@ -79,6 +111,9 @@ export var SessionReducer;
|
|
|
79
111
|
return block && { ...block, text: `${(_a = block.text) !== null && _a !== void 0 ? _a : ''}${delta.delta}` };
|
|
80
112
|
case 'thinking_delta':
|
|
81
113
|
return block && { ...block, thinking: `${(_b = block.thinking) !== null && _b !== void 0 ? _b : ''}${delta.delta}` };
|
|
114
|
+
case 'toolcall_delta':
|
|
115
|
+
// The arguments as they come, JSON not yet whole: what the model has written so far.
|
|
116
|
+
return block && { ...block, partialJson: `${(_c = block.partialJson) !== null && _c !== void 0 ? _c : ''}${delta.delta}` };
|
|
82
117
|
default:
|
|
83
118
|
return undefined;
|
|
84
119
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-reducer.js","sourceRoot":"","sources":["../../src/common/session-reducer.ts"],"names":[],"mappings":"
|
|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AC,IAAI,EAAE,CAAC;YACjB,KAAK,YAAY;gBACb,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;YACtC,KAAK,gBAAgB;gBACjB,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;YAC9C,KAAK,gBAAgB;gBACjB,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;YACnF,KAAK,YAAY;gBACb,OAAO,KAAK,IAAI,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,GAAG,MAAA,KAAK,CAAC,IAAI,mCAAI,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;YAC5E,KAAK,gBAAgB;gBACjB,OAAO,KAAK,IAAI,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,GAAG,MAAA,KAAK,CAAC,QAAQ,mCAAI,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;YACpF,KAAK,gBAAgB;gBACjB,qFAAqF;gBACrF,OAAO,KAAK,IAAI,EAAE,GAAG,KAAK,EAAE,WAAW,EAAE,GAAG,MAAA,KAAK,CAAC,WAAW,mCAAI,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;YAC1F;gBACI,OAAO,SAAS,CAAC;QACzB,CAAC;IACL,CAAC;AACL,CAAC,EA3HgB,cAAc,KAAd,cAAc,QA2H9B"}
|
|
@@ -1,7 +1,20 @@
|
|
|
1
|
-
import type { RpcExtensionUIResponse, TaAttachResult, TaEventEnvelope, TaPromptInput, TaPromptReceipt, TaSessionInfo } from '@celljs/ta-protocol';
|
|
1
|
+
import type { RpcExtensionUIResponse, TaAttachResult, TaClientToolDefinition, TaClientToolRequest, TaClientToolResult, TaEvent, TaEventEnvelope, TaPromptInput, TaPromptReceipt, TaSessionInfo } from '@celljs/ta-protocol';
|
|
2
2
|
import { type SessionState } from './session-reducer.js';
|
|
3
3
|
import type { TaConnection } from './ta-connection.js';
|
|
4
4
|
type Listener = (state: SessionState) => void;
|
|
5
|
+
/**
|
|
6
|
+
* A tool this client runs. Open the session with its `definition` in `clientTools`, and give the tool
|
|
7
|
+
* to the store, which runs each call and sends the result. Its output is what the model reads; a
|
|
8
|
+
* thrown error becomes a failed call.
|
|
9
|
+
*/
|
|
10
|
+
export interface ClientTool {
|
|
11
|
+
definition: TaClientToolDefinition;
|
|
12
|
+
run(input: unknown, request: TaClientToolRequest): Promise<string> | string;
|
|
13
|
+
}
|
|
14
|
+
export interface SessionStoreOptions {
|
|
15
|
+
/** The tools of the session this client runs. Other clients may run them too: the first result counts. */
|
|
16
|
+
clientTools?: ClientTool[];
|
|
17
|
+
}
|
|
5
18
|
/**
|
|
6
19
|
* The state of one open session on a client: for any UI to render and subscribe to. It catches up by
|
|
7
20
|
* itself after a reconnect, from the events it missed or, if those are gone, from the session.
|
|
@@ -12,14 +25,22 @@ export declare class SessionStore {
|
|
|
12
25
|
protected current: SessionState;
|
|
13
26
|
protected lastSeq?: number;
|
|
14
27
|
protected readonly listeners: Set<Listener>;
|
|
28
|
+
protected readonly eventListeners: Set<(event: TaEvent) => void>;
|
|
15
29
|
protected readonly stop: (() => void)[];
|
|
16
|
-
|
|
30
|
+
protected readonly clientTools: Map<string, ClientTool>;
|
|
31
|
+
/** Calls of client tools this store is running or answering now, by id. */
|
|
32
|
+
protected readonly running: Set<string>;
|
|
33
|
+
/** Results not yet taken by the server, by call id. */
|
|
34
|
+
protected readonly results: Map<string, TaClientToolResult>;
|
|
35
|
+
constructor(connection: TaConnection, sessionId: string, options?: SessionStoreOptions);
|
|
17
36
|
get state(): SessionState;
|
|
18
37
|
/** Starts following the session. */
|
|
19
38
|
attach(): Promise<TaSessionInfo>;
|
|
20
39
|
/** Stops following it. The session goes on. */
|
|
21
40
|
detach(): Promise<void>;
|
|
22
41
|
subscribe(listener: Listener): () => void;
|
|
42
|
+
/** Each new event of the session, once the state has taken it in. */
|
|
43
|
+
onEvent(listener: (event: TaEvent) => void): () => void;
|
|
23
44
|
/** Sends a message with a new id; a resend after a lost connection must reuse the id. */
|
|
24
45
|
prompt(message: string, input?: Partial<TaPromptInput>): Promise<TaPromptReceipt>;
|
|
25
46
|
respondUi(response: RpcExtensionUIResponse): Promise<boolean>;
|
|
@@ -27,5 +48,9 @@ export declare class SessionStore {
|
|
|
27
48
|
protected reload(): Promise<void>;
|
|
28
49
|
protected apply(events: TaEventEnvelope[]): void;
|
|
29
50
|
protected update(state: SessionState): void;
|
|
51
|
+
protected runClientTools(state: SessionState): void;
|
|
52
|
+
/** Runs a call once: a result that could not be sent is sent again after a reconnect, not made again. */
|
|
53
|
+
protected answer(tool: ClientTool, request: TaClientToolRequest): Promise<void>;
|
|
54
|
+
protected run(tool: ClientTool, request: TaClientToolRequest): Promise<TaClientToolResult>;
|
|
30
55
|
}
|
|
31
56
|
export {};
|
|
@@ -4,12 +4,19 @@ import { SessionReducer } from './session-reducer.js';
|
|
|
4
4
|
* itself after a reconnect, from the events it missed or, if those are gone, from the session.
|
|
5
5
|
*/
|
|
6
6
|
export class SessionStore {
|
|
7
|
-
constructor(connection, sessionId) {
|
|
7
|
+
constructor(connection, sessionId, options = {}) {
|
|
8
|
+
var _a;
|
|
8
9
|
this.connection = connection;
|
|
9
10
|
this.sessionId = sessionId;
|
|
10
11
|
this.current = SessionReducer.initial();
|
|
11
12
|
this.listeners = new Set();
|
|
13
|
+
this.eventListeners = new Set();
|
|
12
14
|
this.stop = [];
|
|
15
|
+
/** Calls of client tools this store is running or answering now, by id. */
|
|
16
|
+
this.running = new Set();
|
|
17
|
+
/** Results not yet taken by the server, by call id. */
|
|
18
|
+
this.results = new Map();
|
|
19
|
+
this.clientTools = new Map(((_a = options.clientTools) !== null && _a !== void 0 ? _a : []).map(tool => [tool.definition.name, tool]));
|
|
13
20
|
}
|
|
14
21
|
get state() {
|
|
15
22
|
return this.current;
|
|
@@ -30,6 +37,11 @@ export class SessionStore {
|
|
|
30
37
|
this.listeners.add(listener);
|
|
31
38
|
return () => this.listeners.delete(listener);
|
|
32
39
|
}
|
|
40
|
+
/** Each new event of the session, once the state has taken it in. */
|
|
41
|
+
onEvent(listener) {
|
|
42
|
+
this.eventListeners.add(listener);
|
|
43
|
+
return () => this.eventListeners.delete(listener);
|
|
44
|
+
}
|
|
33
45
|
/** Sends a message with a new id; a resend after a lost connection must reuse the id. */
|
|
34
46
|
prompt(message, input = {}) {
|
|
35
47
|
return this.connection.server.prompt(this.sessionId, { clientMessageId: crypto.randomUUID(), ...input, message });
|
|
@@ -44,7 +56,7 @@ export class SessionStore {
|
|
|
44
56
|
this.lastSeq = result.lastSeq;
|
|
45
57
|
}
|
|
46
58
|
this.apply(result.events);
|
|
47
|
-
this.update({ ...this.current, pendingUi: result.pendingUi, working: result.info.isStreaming });
|
|
59
|
+
this.update({ ...this.current, pendingUi: result.pendingUi, pendingClientTools: result.pendingClientTools, working: result.info.isStreaming });
|
|
48
60
|
return result;
|
|
49
61
|
}
|
|
50
62
|
async reload() {
|
|
@@ -54,6 +66,7 @@ export class SessionStore {
|
|
|
54
66
|
apply(events) {
|
|
55
67
|
let state = this.current;
|
|
56
68
|
let replaced = false;
|
|
69
|
+
const applied = [];
|
|
57
70
|
for (const { sessionId, seq, event } of events) {
|
|
58
71
|
if (sessionId !== this.sessionId || (this.lastSeq !== undefined && seq <= this.lastSeq)) {
|
|
59
72
|
continue;
|
|
@@ -61,9 +74,15 @@ export class SessionStore {
|
|
|
61
74
|
this.lastSeq = seq;
|
|
62
75
|
state = SessionReducer.reduce(state, event);
|
|
63
76
|
replaced || (replaced = event.type === 'session_replaced');
|
|
77
|
+
applied.push(event);
|
|
64
78
|
}
|
|
65
79
|
// One notification per batch: a UI renders once however many deltas arrived.
|
|
66
80
|
this.update(state);
|
|
81
|
+
for (const event of applied) {
|
|
82
|
+
for (const listener of this.eventListeners) {
|
|
83
|
+
listener(event);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
67
86
|
if (replaced) {
|
|
68
87
|
void this.reload();
|
|
69
88
|
}
|
|
@@ -74,6 +93,47 @@ export class SessionStore {
|
|
|
74
93
|
for (const listener of this.listeners) {
|
|
75
94
|
listener(state);
|
|
76
95
|
}
|
|
96
|
+
this.runClientTools(state);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
runClientTools(state) {
|
|
100
|
+
for (const request of state.pendingClientTools) {
|
|
101
|
+
const tool = this.clientTools.get(request.toolName);
|
|
102
|
+
if (tool && !this.running.has(request.id)) {
|
|
103
|
+
this.running.add(request.id);
|
|
104
|
+
void this.answer(tool, request);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const id of this.results.keys()) {
|
|
108
|
+
if (!state.pendingClientTools.some(request => request.id === id)) {
|
|
109
|
+
this.results.delete(id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Runs a call once: a result that could not be sent is sent again after a reconnect, not made again. */
|
|
114
|
+
async answer(tool, request) {
|
|
115
|
+
try {
|
|
116
|
+
let result = this.results.get(request.id);
|
|
117
|
+
if (!result) {
|
|
118
|
+
result = await this.run(tool, request);
|
|
119
|
+
this.results.set(request.id, result);
|
|
120
|
+
}
|
|
121
|
+
await this.connection.server.respondClientTool(this.sessionId, result);
|
|
122
|
+
this.results.delete(request.id);
|
|
123
|
+
}
|
|
124
|
+
catch (_a) {
|
|
125
|
+
// The connection was lost; the reconnect sends the result if the call still waits.
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
this.running.delete(request.id);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async run(tool, request) {
|
|
132
|
+
try {
|
|
133
|
+
return { id: request.id, output: await tool.run(request.input, request) };
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
return { id: request.id, output: error instanceof Error ? error.message : String(error), isError: true };
|
|
77
137
|
}
|
|
78
138
|
}
|
|
79
139
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-store.js","sourceRoot":"","sources":["../../src/common/session-store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"session-store.js","sourceRoot":"","sources":["../../src/common/session-store.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,cAAc,EAAqB,MAAM,mBAAmB,CAAC;AAoBtE;;;GAGG;AACH,MAAM,OAAO,YAAY;IAYrB,YACuB,UAAwB,EAClC,SAAiB,EAC1B,OAAO,GAAwB,EAAE;;QAFd,eAAU,GAAV,UAAU,CAAc;QAClC,cAAS,GAAT,SAAS,CAAQ;QAbpB,YAAO,GAAG,cAAc,CAAC,OAAO,EAAE,CAAC;QAE1B,cAAS,GAAG,IAAI,GAAG,EAAY,CAAC;QAChC,mBAAc,GAAG,IAAI,GAAG,EAA4B,CAAC;QACrD,SAAI,GAAmB,EAAE,CAAC;QAE7C,2EAA2E;QACxD,YAAO,GAAG,IAAI,GAAG,EAAU,CAAC;QAC/C,uDAAuD;QACpC,YAAO,GAAG,IAAI,GAAG,EAA8B,CAAC;QAO/D,IAAI,CAAC,WAAW,GAAG,IAAI,GAAG,CAAC,OAAC,OAAO,CAAC,WAAW,mCAAI,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IACtG,CAAC;IAED,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAED,oCAAoC;IACpC,KAAK,CAAC,MAAM;QACR,IAAI,CAAC,IAAI,CAAC,IAAI,CACV,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EACtD,IAAI,CAAC,UAAU,CAAC,MAAM,CAA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E,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,CAAC;QAC9E,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QAC7G,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type TaErrorCode, type TaEventEnvelope, type TaServer } from '@celljs/ta-protocol';
|
|
1
|
+
import { type TaAuthEvent, type TaAuthServer, TaErrorCode, type TaEventEnvelope, type TaPackageServer, type TaRelayServer, type TaServer, type TaSettingsServer, type TaTerminalServer, type TaWorkspaceServer } from '@celljs/ta-protocol';
|
|
2
2
|
/** A failed call. `code` is set for failures the server names. */
|
|
3
3
|
export declare class TaRemoteError extends Error {
|
|
4
4
|
readonly code?: TaErrorCode | undefined;
|
|
@@ -14,41 +14,90 @@ export interface TaConnectionOptions {
|
|
|
14
14
|
reconnectDelayMs?: number;
|
|
15
15
|
/** Default 10000. */
|
|
16
16
|
maxReconnectDelayMs?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Another way to ta serve than a WebSocket to `url`, such as a relay (RelaySocket of @celljs/ta-relay):
|
|
19
|
+
* a new socket for each connection, which takes care of access itself.
|
|
20
|
+
*/
|
|
21
|
+
createSocket?: () => TaSocket;
|
|
22
|
+
}
|
|
23
|
+
/** What TaConnection uses of a WebSocket. */
|
|
24
|
+
export interface TaSocket {
|
|
25
|
+
readonly readyState: number;
|
|
26
|
+
onopen: ((event?: unknown) => void) | null;
|
|
27
|
+
onmessage: ((message: {
|
|
28
|
+
data: unknown;
|
|
29
|
+
}) => void) | null;
|
|
30
|
+
onclose: ((event?: unknown) => void) | null;
|
|
31
|
+
onerror?: ((event?: unknown) => void) | null;
|
|
32
|
+
send(text: string): void;
|
|
33
|
+
close(): void;
|
|
17
34
|
}
|
|
18
35
|
interface PendingCall {
|
|
19
36
|
resolve(result: unknown): void;
|
|
20
37
|
reject(error: Error): void;
|
|
21
38
|
}
|
|
22
39
|
type EventsListener = (events: TaEventEnvelope[]) => void;
|
|
40
|
+
type TerminalListener = (terminalId: string, data: string, exitCode?: number) => void;
|
|
41
|
+
type ExtensionDataListener = (name: string, keys: string[], cwd?: string) => void;
|
|
42
|
+
type AuthEventListener = (loginId: string, event: TaAuthEvent) => void;
|
|
23
43
|
type OpenListener = () => void;
|
|
44
|
+
type CloseListener = () => void;
|
|
45
|
+
type ReconnectingListener = (delayMs: number, attempt: number) => void;
|
|
24
46
|
/**
|
|
25
47
|
* A connection to `ta serve` on the `WebSocket` of the platform, with no other dependency, so that it
|
|
26
48
|
* fits a browser, Electron or a mobile app. It reconnects until closed; `onOpen` runs each time, for
|
|
27
|
-
* attaching again ({@link SessionStore} does).
|
|
49
|
+
* attaching again ({@link SessionStore} does). Each service of ta serve is a property; all of them go
|
|
50
|
+
* over the one WebSocket.
|
|
51
|
+
*
|
|
52
|
+
* Calls fail when the connection is down; they are not queued, because only the caller knows whether to resend.
|
|
28
53
|
*/
|
|
29
54
|
export declare class TaConnection {
|
|
30
55
|
protected readonly options: TaConnectionOptions;
|
|
31
|
-
/** Calls fail when the connection is down; they are not queued, because only the caller knows whether to resend. */
|
|
32
56
|
readonly server: TaServer;
|
|
33
|
-
|
|
57
|
+
readonly workspace: TaWorkspaceServer;
|
|
58
|
+
readonly terminals: TaTerminalServer;
|
|
59
|
+
readonly settings: TaSettingsServer;
|
|
60
|
+
readonly packages: TaPackageServer;
|
|
61
|
+
readonly auth: TaAuthServer;
|
|
62
|
+
readonly relay: TaRelayServer;
|
|
63
|
+
protected socket?: TaSocket;
|
|
34
64
|
protected closed: boolean;
|
|
35
65
|
protected attempts: number;
|
|
36
66
|
protected reconnectTimer?: ReturnType<typeof setTimeout>;
|
|
37
67
|
protected nextId: number;
|
|
38
68
|
protected readonly pending: Map<number, PendingCall>;
|
|
39
69
|
protected readonly eventListeners: Set<EventsListener>;
|
|
70
|
+
protected readonly terminalListeners: Set<TerminalListener>;
|
|
71
|
+
protected readonly extensionDataListeners: Set<ExtensionDataListener>;
|
|
72
|
+
protected readonly authListeners: Set<AuthEventListener>;
|
|
40
73
|
protected readonly openListeners: Set<OpenListener>;
|
|
74
|
+
protected readonly closeListeners: Set<CloseListener>;
|
|
75
|
+
protected readonly reconnectingListeners: Set<ReconnectingListener>;
|
|
41
76
|
constructor(options: TaConnectionOptions);
|
|
42
77
|
/** Resolves when the first connection is open, or rejects if it cannot be made; later losses reconnect. */
|
|
43
78
|
connect(): Promise<void>;
|
|
44
79
|
close(): void;
|
|
45
80
|
onEvents(listener: EventsListener): () => void;
|
|
81
|
+
/** Output of the terminals of this connection (TaTerminalServer.openTerminal). */
|
|
82
|
+
onTerminalData(listener: TerminalListener): () => void;
|
|
83
|
+
/** Keys of the data of packages that changed (TaClient.onExtensionData), for the data this connection read; `cwd` for a key of a directory. */
|
|
84
|
+
onExtensionData(listener: ExtensionDataListener): () => void;
|
|
85
|
+
/** What happens during the sign-ins of this connection (TaAuthServer.login). */
|
|
86
|
+
onAuthEvent(listener: AuthEventListener): () => void;
|
|
46
87
|
onOpen(listener: OpenListener): () => void;
|
|
88
|
+
/** Runs each time an open connection is lost; it reconnects unless closed. */
|
|
89
|
+
onClose(listener: CloseListener): () => void;
|
|
90
|
+
/** Runs each time it waits to try again: how long, and which try it is (1 for the first after a loss). */
|
|
91
|
+
onReconnecting(listener: ReconnectingListener): () => void;
|
|
92
|
+
/** While the connection is down, tries again now rather than at the end of the wait, which starts over; otherwise does nothing. */
|
|
93
|
+
reconnect(): void;
|
|
47
94
|
/** With `resolve` and `reject`: the first connection, whose failure is the caller's to handle. */
|
|
48
95
|
protected open(resolve?: () => void, reject?: (error: Error) => void): void;
|
|
96
|
+
protected createWebSocket(): TaSocket;
|
|
49
97
|
protected scheduleReconnect(): void;
|
|
50
98
|
protected failPending(): void;
|
|
51
|
-
protected
|
|
99
|
+
protected service<T extends object>(name: string): T;
|
|
100
|
+
protected call(service: string, method: string, params: unknown[]): Promise<unknown>;
|
|
52
101
|
protected receive(text: string): void;
|
|
53
102
|
}
|
|
54
103
|
export {};
|