@celljs/ta-host 0.0.0-stage → 4.5.1
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 +33 -2
- package/cell.schema.json +33 -0
- package/cell.yml +14 -0
- package/esm/node/client-channel.d.ts +31 -0
- package/esm/node/client-channel.js +84 -0
- package/esm/node/client-channel.js.map +1 -0
- package/esm/node/event-log.d.ts +28 -0
- package/esm/node/event-log.js +48 -0
- package/esm/node/event-log.js.map +1 -0
- package/esm/node/host-protocol.d.ts +27 -0
- package/esm/node/host-protocol.js +4 -0
- package/esm/node/host-protocol.js.map +1 -0
- package/esm/node/index.d.ts +13 -0
- package/esm/node/index.js +14 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/local-exec-environment-factory.d.ts +4 -0
- package/esm/node/local-exec-environment-factory.js +18 -0
- package/esm/node/local-exec-environment-factory.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/receipt-book.d.ts +12 -0
- package/esm/node/receipt-book.js +38 -0
- package/esm/node/receipt-book.js.map +1 -0
- package/esm/node/remote-ui-context.d.ts +63 -0
- package/esm/node/remote-ui-context.js +130 -0
- package/esm/node/remote-ui-context.js.map +1 -0
- package/esm/node/ta-access.d.ts +30 -0
- package/esm/node/ta-access.js +107 -0
- package/esm/node/ta-access.js.map +1 -0
- package/esm/node/ta-errors.d.ts +11 -0
- package/esm/node/ta-errors.js +44 -0
- package/esm/node/ta-errors.js.map +1 -0
- package/esm/node/ta-event-util.d.ts +11 -0
- package/esm/node/ta-event-util.js +20 -0
- package/esm/node/ta-event-util.js.map +1 -0
- package/esm/node/ta-server-impl.d.ts +50 -0
- package/esm/node/ta-server-impl.js +226 -0
- package/esm/node/ta-server-impl.js.map +1 -0
- package/esm/node/ta-session-factory.d.ts +32 -0
- package/esm/node/ta-session-factory.js +151 -0
- package/esm/node/ta-session-factory.js.map +1 -0
- package/esm/node/ta-session-registry.d.ts +20 -0
- package/esm/node/ta-session-registry.js +85 -0
- package/esm/node/ta-session-registry.js.map +1 -0
- package/esm/node/ta-session.d.ts +53 -0
- package/esm/node/ta-session.js +157 -0
- package/esm/node/ta-session.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/client-channel.d.ts +32 -0
- package/lib/node/client-channel.d.ts.map +1 -0
- package/lib/node/client-channel.js +88 -0
- package/lib/node/client-channel.js.map +1 -0
- package/lib/node/client-channel.spec.d.ts +2 -0
- package/lib/node/client-channel.spec.d.ts.map +1 -0
- package/lib/node/client-channel.spec.js +106 -0
- package/lib/node/client-channel.spec.js.map +1 -0
- package/lib/node/event-log.d.ts +29 -0
- package/lib/node/event-log.d.ts.map +1 -0
- package/lib/node/event-log.js +52 -0
- package/lib/node/event-log.js.map +1 -0
- package/lib/node/event-log.spec.d.ts +2 -0
- package/lib/node/event-log.spec.d.ts.map +1 -0
- package/lib/node/event-log.spec.js +48 -0
- package/lib/node/event-log.spec.js.map +1 -0
- package/lib/node/host-protocol.d.ts +28 -0
- package/lib/node/host-protocol.d.ts.map +1 -0
- package/lib/node/host-protocol.js +7 -0
- package/lib/node/host-protocol.js.map +1 -0
- package/lib/node/index.d.ts +14 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +30 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/local-exec-environment-factory.d.ts +5 -0
- package/lib/node/local-exec-environment-factory.d.ts.map +1 -0
- package/lib/node/local-exec-environment-factory.js +21 -0
- package/lib/node/local-exec-environment-factory.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/receipt-book.d.ts +13 -0
- package/lib/node/receipt-book.d.ts.map +1 -0
- package/lib/node/receipt-book.js +42 -0
- package/lib/node/receipt-book.js.map +1 -0
- package/lib/node/receipt-book.spec.d.ts +2 -0
- package/lib/node/receipt-book.spec.d.ts.map +1 -0
- package/lib/node/receipt-book.spec.js +39 -0
- package/lib/node/receipt-book.spec.js.map +1 -0
- package/lib/node/remote-ui-context.d.ts +64 -0
- package/lib/node/remote-ui-context.d.ts.map +1 -0
- package/lib/node/remote-ui-context.js +134 -0
- package/lib/node/remote-ui-context.js.map +1 -0
- package/lib/node/remote-ui-context.spec.d.ts +2 -0
- package/lib/node/remote-ui-context.spec.d.ts.map +1 -0
- package/lib/node/remote-ui-context.spec.js +62 -0
- package/lib/node/remote-ui-context.spec.js.map +1 -0
- package/lib/node/ta-access.d.ts +31 -0
- package/lib/node/ta-access.d.ts.map +1 -0
- package/lib/node/ta-access.js +110 -0
- package/lib/node/ta-access.js.map +1 -0
- package/lib/node/ta-access.spec.d.ts +2 -0
- package/lib/node/ta-access.spec.d.ts.map +1 -0
- package/lib/node/ta-access.spec.js +25 -0
- package/lib/node/ta-access.spec.js.map +1 -0
- package/lib/node/ta-errors.d.ts +12 -0
- package/lib/node/ta-errors.d.ts.map +1 -0
- package/lib/node/ta-errors.js +47 -0
- package/lib/node/ta-errors.js.map +1 -0
- package/lib/node/ta-event-util.d.ts +12 -0
- package/lib/node/ta-event-util.d.ts.map +1 -0
- package/lib/node/ta-event-util.js +23 -0
- package/lib/node/ta-event-util.js.map +1 -0
- package/lib/node/ta-event-util.spec.d.ts +2 -0
- package/lib/node/ta-event-util.spec.d.ts.map +1 -0
- package/lib/node/ta-event-util.spec.js +28 -0
- package/lib/node/ta-event-util.spec.js.map +1 -0
- package/lib/node/ta-server-impl.d.ts +51 -0
- package/lib/node/ta-server-impl.d.ts.map +1 -0
- package/lib/node/ta-server-impl.js +229 -0
- package/lib/node/ta-server-impl.js.map +1 -0
- package/lib/node/ta-session-factory.d.ts +33 -0
- package/lib/node/ta-session-factory.d.ts.map +1 -0
- package/lib/node/ta-session-factory.js +154 -0
- package/lib/node/ta-session-factory.js.map +1 -0
- package/lib/node/ta-session-registry.d.ts +21 -0
- package/lib/node/ta-session-registry.d.ts.map +1 -0
- package/lib/node/ta-session-registry.js +86 -0
- package/lib/node/ta-session-registry.js.map +1 -0
- package/lib/node/ta-session.d.ts +54 -0
- package/lib/node/ta-session.d.ts.map +1 -0
- package/lib/node/ta-session.js +161 -0
- package/lib/node/ta-session.js.map +1 -0
- package/package.json +80 -4
- package/src/node/client-channel.spec.ts +117 -0
- package/src/node/client-channel.ts +94 -0
- package/src/node/event-log.spec.ts +52 -0
- package/src/node/event-log.ts +59 -0
- package/src/node/host-protocol.ts +32 -0
- package/src/node/index.ts +13 -0
- package/src/node/local-exec-environment-factory.ts +9 -0
- package/src/node/module.ts +4 -0
- package/src/node/receipt-book.spec.ts +41 -0
- package/src/node/receipt-book.ts +40 -0
- package/src/node/remote-ui-context.spec.ts +68 -0
- package/src/node/remote-ui-context.ts +168 -0
- package/src/node/ta-access.spec.ts +27 -0
- package/src/node/ta-access.ts +98 -0
- package/src/node/ta-errors.ts +55 -0
- package/src/node/ta-event-util.spec.ts +29 -0
- package/src/node/ta-event-util.ts +23 -0
- package/src/node/ta-server-impl.ts +264 -0
- package/src/node/ta-session-factory.ts +156 -0
- package/src/node/ta-session-registry.ts +75 -0
- package/src/node/ta-session.ts +193 -0
package/package.json
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-host",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.5.1",
|
|
4
|
+
"description": "ta serve: pi sessions for many clients over WebSocket",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cell-component"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/cellbang/cell",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/cellbang/cell/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22.19.0"
|
|
19
|
+
},
|
|
20
|
+
"main": "lib/node/index.js",
|
|
21
|
+
"typings": "lib/node/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"esm",
|
|
25
|
+
"src",
|
|
26
|
+
"cell.yml",
|
|
27
|
+
"cell.schema.json"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -b && bun ../../scripts/build-esm.ts",
|
|
31
|
+
"clean": "rm -rf lib esm *.tsbuildinfo",
|
|
32
|
+
"test": "bun test --preload reflect-metadata ./lib",
|
|
33
|
+
"watch": "tsc -b -w --preserveWatchOutput"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@celljs/ai-agent": "4.5.1",
|
|
37
|
+
"@celljs/ai-coding-agent": "4.5.1",
|
|
38
|
+
"@celljs/core": "4.5.1",
|
|
39
|
+
"@celljs/rpc": "4.5.1",
|
|
40
|
+
"@celljs/ta-exec": "4.5.1",
|
|
41
|
+
"@celljs/ta-harness": "4.5.1",
|
|
42
|
+
"@celljs/ta-protocol": "4.5.1",
|
|
43
|
+
"@celljs/web": "4.5.1",
|
|
44
|
+
"@earendil-works/pi-coding-agent": "^1.5.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@earendil-works/pi-ai": "^1.5.0"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"cell": {
|
|
53
|
+
"modules": {
|
|
54
|
+
"backend": [
|
|
55
|
+
"lib/node/module"
|
|
56
|
+
]
|
|
57
|
+
},
|
|
58
|
+
"optionalDependencies": [
|
|
59
|
+
"@earendil-works/pi-coding-agent"
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
"exports": {
|
|
63
|
+
".": {
|
|
64
|
+
"import": "./esm/node/index.js",
|
|
65
|
+
"default": "./lib/node/index.js"
|
|
66
|
+
},
|
|
67
|
+
"./package.json": "./package.json",
|
|
68
|
+
"./lib/node": {
|
|
69
|
+
"import": "./esm/node/index.js",
|
|
70
|
+
"default": "./lib/node/index.js"
|
|
71
|
+
},
|
|
72
|
+
"./lib/*.js": {
|
|
73
|
+
"import": "./esm/*.js",
|
|
74
|
+
"default": "./lib/*.js"
|
|
75
|
+
},
|
|
76
|
+
"./lib/*": {
|
|
77
|
+
"import": "./esm/*.js",
|
|
78
|
+
"default": "./lib/*.js"
|
|
79
|
+
},
|
|
80
|
+
"./*": "./*"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
3
|
+
import type { TaEvent, TaEventEnvelope } from '@celljs/ta-protocol';
|
|
4
|
+
import { ClientChannel } from './client-channel';
|
|
5
|
+
import type { LoggedEvent } from './event-log';
|
|
6
|
+
|
|
7
|
+
/** A streamed event by default, which waits for the interval. */
|
|
8
|
+
const logged = (seq: number, size = 10, type = 'message_update'): LoggedEvent => ({ envelope: { sessionId: 's', seq, event: { type } as TaEvent }, size });
|
|
9
|
+
|
|
10
|
+
function setup(maxBacklog = 1000) {
|
|
11
|
+
const batches: TaEventEnvelope[][] = [];
|
|
12
|
+
const answers: (() => void)[] = [];
|
|
13
|
+
const state = { closed: false };
|
|
14
|
+
const connection = {
|
|
15
|
+
get closed() {
|
|
16
|
+
return state.closed;
|
|
17
|
+
},
|
|
18
|
+
close: () => {
|
|
19
|
+
state.closed = true;
|
|
20
|
+
},
|
|
21
|
+
client: () => ({
|
|
22
|
+
onEvents: (batch: TaEventEnvelope[]) => {
|
|
23
|
+
batches.push(batch);
|
|
24
|
+
return new Promise<void>(done => answers.push(done));
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
} as unknown as RpcConnection;
|
|
28
|
+
return { channel: new ClientChannel(connection, maxBacklog), batches, answers, state };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const seqsOf = (batch: TaEventEnvelope[]) => batch.map(envelope => envelope.seq);
|
|
32
|
+
|
|
33
|
+
describe('ClientChannel', () => {
|
|
34
|
+
it('sends the first events at once, together when they arrive together', async () => {
|
|
35
|
+
const { channel, batches } = setup();
|
|
36
|
+
channel.send(logged(1));
|
|
37
|
+
channel.send(logged(2));
|
|
38
|
+
expect(batches).toEqual([]);
|
|
39
|
+
await Bun.sleep(5);
|
|
40
|
+
expect(batches.map(seqsOf)).toEqual([[1, 2]]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('sends the events of an interval as one batch after the first', async () => {
|
|
44
|
+
const { channel, batches, answers } = setup();
|
|
45
|
+
channel.send(logged(1));
|
|
46
|
+
await Bun.sleep(5);
|
|
47
|
+
answers[0]();
|
|
48
|
+
channel.send(logged(2));
|
|
49
|
+
await Bun.sleep(5);
|
|
50
|
+
channel.send(logged(3));
|
|
51
|
+
expect(batches.map(seqsOf)).toEqual([[1]]);
|
|
52
|
+
await Bun.sleep(60);
|
|
53
|
+
expect(batches.map(seqsOf)).toEqual([[1], [2, 3]]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('sends other events at once, with the deltas queued before them', async () => {
|
|
57
|
+
const { channel, batches, answers } = setup();
|
|
58
|
+
channel.send(logged(1));
|
|
59
|
+
await Bun.sleep(5);
|
|
60
|
+
answers[0]();
|
|
61
|
+
channel.send(logged(2));
|
|
62
|
+
channel.send(logged(3, 10, 'tool_execution_end'));
|
|
63
|
+
await Bun.sleep(5);
|
|
64
|
+
expect(batches.map(seqsOf)).toEqual([[1], [2, 3]]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('sends the first delta of a reply at once, even right after its message started', async () => {
|
|
68
|
+
const { channel, batches, answers } = setup();
|
|
69
|
+
channel.send(logged(1, 10, 'message_start'));
|
|
70
|
+
await Bun.sleep(5);
|
|
71
|
+
answers[0]();
|
|
72
|
+
channel.send(logged(2));
|
|
73
|
+
await Bun.sleep(5);
|
|
74
|
+
expect(batches.map(seqsOf)).toEqual([[1], [2]]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('sends at once again after a quiet interval', async () => {
|
|
78
|
+
const { channel, batches, answers } = setup();
|
|
79
|
+
channel.send(logged(1));
|
|
80
|
+
await Bun.sleep(5);
|
|
81
|
+
answers[0]();
|
|
82
|
+
await Bun.sleep(40);
|
|
83
|
+
channel.send(logged(2));
|
|
84
|
+
await Bun.sleep(5);
|
|
85
|
+
expect(batches.map(seqsOf)).toEqual([[1], [2]]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('waits for the client before the next batch, and then sends the rest together', async () => {
|
|
89
|
+
const { channel, batches, answers } = setup();
|
|
90
|
+
channel.send(logged(1));
|
|
91
|
+
await Bun.sleep(60);
|
|
92
|
+
channel.send(logged(2));
|
|
93
|
+
channel.send(logged(3));
|
|
94
|
+
await Bun.sleep(60);
|
|
95
|
+
expect(batches.length).toBe(1);
|
|
96
|
+
answers[0]();
|
|
97
|
+
await Bun.sleep(60);
|
|
98
|
+
expect(batches.map(seqsOf)).toEqual([[1], [2, 3]]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('disconnects a client too far behind instead of queuing without end', () => {
|
|
102
|
+
const { channel, state } = setup(25);
|
|
103
|
+
channel.send(logged(1));
|
|
104
|
+
channel.send(logged(2));
|
|
105
|
+
expect(state.closed).toBe(false);
|
|
106
|
+
channel.send(logged(3));
|
|
107
|
+
expect(state.closed).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('sends nothing to a closed connection', async () => {
|
|
111
|
+
const { channel, batches, state } = setup();
|
|
112
|
+
channel.send(logged(1));
|
|
113
|
+
state.closed = true;
|
|
114
|
+
await Bun.sleep(60);
|
|
115
|
+
expect(batches).toEqual([]);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
2
|
+
import { TaClient, type TaEventEnvelope } from '@celljs/ta-protocol';
|
|
3
|
+
import type { LoggedEvent } from './event-log';
|
|
4
|
+
|
|
5
|
+
const FLUSH_INTERVAL_MS = 30;
|
|
6
|
+
|
|
7
|
+
/** The events that come by the hundred while a model or a tool streams; the others leave at once. */
|
|
8
|
+
const STREAMED_EVENTS = new Set(['message_update', 'tool_execution_update']);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Events on their way to one client, one message at a time: the next leaves when the client has
|
|
12
|
+
* answered the last, so that a slow client costs a bounded queue and nothing while no events flow.
|
|
13
|
+
* A model streams hundreds of deltas a second: streamed events leave at most once per interval, the
|
|
14
|
+
* first of them at once. Other events (a message starts, a tool ends, a dialog opens) leave at once,
|
|
15
|
+
* with what is queued, so that nothing a user waits for is delayed.
|
|
16
|
+
*/
|
|
17
|
+
export class ClientChannel {
|
|
18
|
+
protected queue: TaEventEnvelope[] = [];
|
|
19
|
+
protected queuedBytes = 0;
|
|
20
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
21
|
+
protected sending = false;
|
|
22
|
+
protected lastFlushAt = Number.NEGATIVE_INFINITY;
|
|
23
|
+
/** When the timer fires, on the clock of `performance.now()`. */
|
|
24
|
+
protected dueAt = 0;
|
|
25
|
+
/** The queue holds an event that should not wait. */
|
|
26
|
+
protected urgent = false;
|
|
27
|
+
/** The queue holds a streamed event. */
|
|
28
|
+
protected streamed = false;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
readonly connection: RpcConnection,
|
|
32
|
+
protected readonly maxBacklogBytes: number,
|
|
33
|
+
) {}
|
|
34
|
+
|
|
35
|
+
send(logged: LoggedEvent): void {
|
|
36
|
+
this.queue.push(logged.envelope);
|
|
37
|
+
this.queuedBytes += logged.size;
|
|
38
|
+
if (this.queuedBytes > this.maxBacklogBytes) {
|
|
39
|
+
this.clear();
|
|
40
|
+
this.connection.close(1013, 'The client is too far behind; attach again with the last seq it has.');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const streamed = STREAMED_EVENTS.has(logged.envelope.event.type);
|
|
44
|
+
this.streamed ||= streamed;
|
|
45
|
+
this.urgent ||= !streamed;
|
|
46
|
+
this.schedule();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A timer, not a direct send: the events of the same turn of the event loop leave together. */
|
|
50
|
+
protected schedule(): void {
|
|
51
|
+
const due = this.urgent ? 0 : this.lastFlushAt + FLUSH_INTERVAL_MS;
|
|
52
|
+
if (this.timer !== undefined && this.dueAt <= due) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
clearTimeout(this.timer);
|
|
56
|
+
this.dueAt = due;
|
|
57
|
+
this.timer = setTimeout(() => this.flush(), Math.max(0, due - performance.now()));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
protected flush(): void {
|
|
61
|
+
this.timer = undefined;
|
|
62
|
+
if (this.sending || !this.queue.length || this.connection.closed) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const batch = this.queue;
|
|
66
|
+
this.queue = [];
|
|
67
|
+
this.queuedBytes = 0;
|
|
68
|
+
if (this.streamed) {
|
|
69
|
+
this.lastFlushAt = performance.now();
|
|
70
|
+
}
|
|
71
|
+
this.urgent = false;
|
|
72
|
+
this.streamed = false;
|
|
73
|
+
this.sending = true;
|
|
74
|
+
this.connection
|
|
75
|
+
.client<TaClient>(TaClient)
|
|
76
|
+
.onEvents(batch)
|
|
77
|
+
.catch(() => undefined)
|
|
78
|
+
.finally(() => {
|
|
79
|
+
this.sending = false;
|
|
80
|
+
if (this.queue.length) {
|
|
81
|
+
this.schedule();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected clear(): void {
|
|
87
|
+
clearTimeout(this.timer);
|
|
88
|
+
this.timer = undefined;
|
|
89
|
+
this.queue = [];
|
|
90
|
+
this.queuedBytes = 0;
|
|
91
|
+
this.urgent = false;
|
|
92
|
+
this.streamed = false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import type { TaEvent } from '@celljs/ta-protocol';
|
|
3
|
+
import { EventLog } from './event-log';
|
|
4
|
+
|
|
5
|
+
const event = (size = 1): TaEvent => ({ type: 'ui_request_resolved', id: 'x'.repeat(size) });
|
|
6
|
+
const seqs = (log: EventLog, after: number) => log.since(after).events.map(envelope => envelope.seq);
|
|
7
|
+
|
|
8
|
+
describe('EventLog', () => {
|
|
9
|
+
it('numbers events from one and replays those after a seq', () => {
|
|
10
|
+
const log = new EventLog('s', 10000);
|
|
11
|
+
log.append(event());
|
|
12
|
+
log.append(event());
|
|
13
|
+
log.append(event());
|
|
14
|
+
expect(log.lastSeq).toBe(3);
|
|
15
|
+
expect(seqs(log, 1)).toEqual([2, 3]);
|
|
16
|
+
expect(seqs(log, 3)).toEqual([]);
|
|
17
|
+
expect(log.append(event()).envelope.sessionId).toBe('s');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('drops the oldest events beyond its size and reports the gap', () => {
|
|
21
|
+
const log = new EventLog('s', 200);
|
|
22
|
+
for (let i = 0; i < 5; i++) {
|
|
23
|
+
log.append(event(80));
|
|
24
|
+
}
|
|
25
|
+
expect(log.since(0).gap).toBe(true);
|
|
26
|
+
expect(log.since(0).events).toEqual([]);
|
|
27
|
+
expect(log.since(4)).toMatchObject({ gap: false });
|
|
28
|
+
expect(seqs(log, 4)).toEqual([5]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('keeps the latest event even when it alone is too big', () => {
|
|
32
|
+
const log = new EventLog('s', 10);
|
|
33
|
+
log.append(event(100));
|
|
34
|
+
expect(seqs(log, 0)).toEqual([1]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('reports a gap for a seq it never gave', () => {
|
|
38
|
+
const log = new EventLog('s', 1000);
|
|
39
|
+
log.append(event());
|
|
40
|
+
expect(log.since(7).gap).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('stays usable over many events without growing', () => {
|
|
44
|
+
const log = new EventLog('s', 500);
|
|
45
|
+
for (let i = 0; i < 20000; i++) {
|
|
46
|
+
log.append(event(20));
|
|
47
|
+
}
|
|
48
|
+
expect(log.lastSeq).toBe(20000);
|
|
49
|
+
expect(seqs(log, 19999)).toEqual([20000]);
|
|
50
|
+
expect((log as unknown as { entries: unknown[] }).entries.length).toBeLessThan(2100);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { TaEvent, TaEventEnvelope } from '@celljs/ta-protocol';
|
|
2
|
+
|
|
3
|
+
export interface LoggedEvent {
|
|
4
|
+
envelope: TaEventEnvelope;
|
|
5
|
+
/** Approximate size in bytes. */
|
|
6
|
+
size: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The latest events of a session, by size, so that a client that reconnects can catch up. Only that:
|
|
11
|
+
* what happened is in the session file, and a client that fell further behind reads it again.
|
|
12
|
+
*/
|
|
13
|
+
export class EventLog {
|
|
14
|
+
protected entries: LoggedEvent[] = [];
|
|
15
|
+
protected head = 0;
|
|
16
|
+
protected bytes = 0;
|
|
17
|
+
protected seq = 0;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
protected readonly sessionId: string,
|
|
21
|
+
protected readonly maxBytes: number,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
get lastSeq(): number {
|
|
25
|
+
return this.seq;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
append(event: TaEvent): LoggedEvent {
|
|
29
|
+
const logged = { envelope: { sessionId: this.sessionId, seq: ++this.seq, event }, size: this.sizeOf(event) };
|
|
30
|
+
this.entries.push(logged);
|
|
31
|
+
this.bytes += logged.size;
|
|
32
|
+
while (this.bytes > this.maxBytes && this.entries.length - this.head > 1) {
|
|
33
|
+
this.bytes -= this.entries[this.head++].size;
|
|
34
|
+
}
|
|
35
|
+
if (this.head > 1024 && this.head * 2 > this.entries.length) {
|
|
36
|
+
this.entries = this.entries.slice(this.head);
|
|
37
|
+
this.head = 0;
|
|
38
|
+
}
|
|
39
|
+
return logged;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The events after `afterSeq`; `gap` when some of them are gone. */
|
|
43
|
+
since(afterSeq: number): { events: TaEventEnvelope[]; gap: boolean } {
|
|
44
|
+
const oldest = this.head < this.entries.length ? this.entries[this.head].envelope.seq : this.seq + 1;
|
|
45
|
+
if (afterSeq + 1 < oldest || afterSeq > this.seq) {
|
|
46
|
+
return { events: [], gap: true };
|
|
47
|
+
}
|
|
48
|
+
return { events: this.entries.slice(this.head + afterSeq + 1 - oldest).map(entry => entry.envelope), gap: false };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Deltas are most of the events and their size is known; the others are rare enough to measure. */
|
|
52
|
+
protected sizeOf(event: TaEvent): number {
|
|
53
|
+
if (event.type === 'message_update') {
|
|
54
|
+
const delta = (event.assistantMessageEvent as { delta?: string }).delta;
|
|
55
|
+
return 256 + (delta?.length ?? 0);
|
|
56
|
+
}
|
|
57
|
+
return JSON.stringify(event).length;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
2
|
+
import type { TaOpenSessionOptions } from '@celljs/ta-protocol';
|
|
3
|
+
import type { ClientChannel } from './client-channel';
|
|
4
|
+
import type { TaSession } from './ta-session';
|
|
5
|
+
|
|
6
|
+
export const TaSessionFactory = Symbol('TaSessionFactory');
|
|
7
|
+
export const TaSessionRegistry = Symbol('TaSessionRegistry');
|
|
8
|
+
|
|
9
|
+
export const TA_HOST_OPTIONS = 'ta.host';
|
|
10
|
+
|
|
11
|
+
/** `ta.host`. */
|
|
12
|
+
export interface TaHostOptions {
|
|
13
|
+
maxSessions: number;
|
|
14
|
+
idleTimeoutMs: number;
|
|
15
|
+
eventBufferBytes: number;
|
|
16
|
+
clientBacklogBytes: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface TaSessionFactory {
|
|
20
|
+
create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The open sessions. */
|
|
24
|
+
export interface TaSessionRegistry {
|
|
25
|
+
open(options: TaOpenSessionOptions): Promise<TaSession>;
|
|
26
|
+
/** Throws CELL_TA_SESSION_NOT_FOUND. */
|
|
27
|
+
require(sessionId: string): TaSession;
|
|
28
|
+
list(): TaSession[];
|
|
29
|
+
close(sessionId: string): Promise<void>;
|
|
30
|
+
closeAll(): Promise<void>;
|
|
31
|
+
channel(connection: RpcConnection): ClientChannel;
|
|
32
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './client-channel';
|
|
2
|
+
export * from './event-log';
|
|
3
|
+
export * from './host-protocol';
|
|
4
|
+
export * from './local-exec-environment-factory';
|
|
5
|
+
export * from './receipt-book';
|
|
6
|
+
export * from './remote-ui-context';
|
|
7
|
+
export * from './ta-access';
|
|
8
|
+
export * from './ta-errors';
|
|
9
|
+
export * from './ta-event-util';
|
|
10
|
+
export * from './ta-server-impl';
|
|
11
|
+
export * from './ta-session';
|
|
12
|
+
export * from './ta-session-factory';
|
|
13
|
+
export * from './ta-session-registry';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Component } from '@celljs/core';
|
|
2
|
+
import { type ExecEnvironment, ExecEnvironmentFactory, LocalExecEnvironment } from '@celljs/ta-exec';
|
|
3
|
+
|
|
4
|
+
@Component(ExecEnvironmentFactory)
|
|
5
|
+
export class LocalExecEnvironmentFactory implements ExecEnvironmentFactory {
|
|
6
|
+
create(cwd: string): ExecEnvironment {
|
|
7
|
+
return new LocalExecEnvironment(cwd);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { ReceiptBook } from './receipt-book';
|
|
3
|
+
|
|
4
|
+
const input = (clientMessageId: string, message = 'hi') => ({ clientMessageId, message });
|
|
5
|
+
|
|
6
|
+
describe('ReceiptBook', () => {
|
|
7
|
+
it('takes a new message and reports the same one again as a duplicate', () => {
|
|
8
|
+
const book = new ReceiptBook();
|
|
9
|
+
expect(book.take(input('a'))).toBeUndefined();
|
|
10
|
+
expect(book.take(input('a'))).toEqual({ clientMessageId: 'a', status: 'duplicate' });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('refuses the same id for a different message', () => {
|
|
14
|
+
const book = new ReceiptBook();
|
|
15
|
+
book.take(input('a'));
|
|
16
|
+
expect(() => book.take(input('a', 'other'))).toThrow('different message');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('tells images apart', () => {
|
|
20
|
+
const book = new ReceiptBook();
|
|
21
|
+
const image = (data: string) => ({ type: 'image', data, mimeType: 'image/png' }) as const;
|
|
22
|
+
book.take({ ...input('a'), images: [image('1')] });
|
|
23
|
+
expect(() => book.take({ ...input('a'), images: [image('2')] })).toThrow();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('takes a message again after it was refused', () => {
|
|
27
|
+
const book = new ReceiptBook();
|
|
28
|
+
book.take(input('a'));
|
|
29
|
+
book.forget('a');
|
|
30
|
+
expect(book.take(input('a'))).toBeUndefined();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('remembers only the latest ids', () => {
|
|
34
|
+
const book = new ReceiptBook(2);
|
|
35
|
+
book.take(input('a'));
|
|
36
|
+
book.take(input('b'));
|
|
37
|
+
book.take(input('c'));
|
|
38
|
+
expect(book.take(input('a'))).toBeUndefined();
|
|
39
|
+
expect(book.take(input('c'))?.status).toBe('duplicate');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type { TaPromptInput, TaPromptReceipt } from '@celljs/ta-protocol';
|
|
3
|
+
import { TaErrors } from './ta-errors';
|
|
4
|
+
|
|
5
|
+
/** The messages a session has taken, by the id the client gave them; a resend does not run twice. Holds hashes, not messages. */
|
|
6
|
+
export class ReceiptBook {
|
|
7
|
+
protected readonly seen = new Map<string, string>();
|
|
8
|
+
|
|
9
|
+
constructor(protected readonly capacity = 256) {}
|
|
10
|
+
|
|
11
|
+
/** The receipt of a message taken before; undefined for a new one, which is now recorded. */
|
|
12
|
+
take(input: TaPromptInput): TaPromptReceipt | undefined {
|
|
13
|
+
const hash = this.hash(input);
|
|
14
|
+
const seen = this.seen.get(input.clientMessageId);
|
|
15
|
+
if (seen === hash) {
|
|
16
|
+
return { clientMessageId: input.clientMessageId, status: 'duplicate' };
|
|
17
|
+
}
|
|
18
|
+
if (seen !== undefined) {
|
|
19
|
+
throw TaErrors.messageConflict(input.clientMessageId);
|
|
20
|
+
}
|
|
21
|
+
this.seen.set(input.clientMessageId, hash);
|
|
22
|
+
if (this.seen.size > this.capacity) {
|
|
23
|
+
this.seen.delete(this.seen.keys().next().value as string);
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The message was refused: the client may send it again. */
|
|
29
|
+
forget(clientMessageId: string): void {
|
|
30
|
+
this.seen.delete(clientMessageId);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected hash(input: TaPromptInput): string {
|
|
34
|
+
const hash = createHash('sha1').update(input.message);
|
|
35
|
+
for (const image of input.images ?? []) {
|
|
36
|
+
hash.update(image.data);
|
|
37
|
+
}
|
|
38
|
+
return hash.digest('base64');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import type { TaEvent } from '@celljs/ta-protocol';
|
|
3
|
+
import { RemoteUiContext } from './remote-ui-context';
|
|
4
|
+
|
|
5
|
+
function setup() {
|
|
6
|
+
const events: any[] = [];
|
|
7
|
+
return { ui: new RemoteUiContext((event: TaEvent) => events.push(event)), events };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('RemoteUiContext', () => {
|
|
11
|
+
it('publishes a dialog and resolves with the first answer', async () => {
|
|
12
|
+
const { ui, events } = setup();
|
|
13
|
+
const answer = ui.confirm('Sure?', 'Really');
|
|
14
|
+
expect(events[0]).toMatchObject({ type: 'extension_ui_request', method: 'confirm', title: 'Sure?' });
|
|
15
|
+
expect(ui.pendingRequests).toHaveLength(1);
|
|
16
|
+
expect(ui.respond({ type: 'extension_ui_response', id: events[0].id, confirmed: true })).toBe(true);
|
|
17
|
+
expect(ui.respond({ type: 'extension_ui_response', id: events[0].id, confirmed: false })).toBe(false);
|
|
18
|
+
expect(await answer).toBe(true);
|
|
19
|
+
expect(events[1]).toEqual({ type: 'ui_request_resolved', id: events[0].id });
|
|
20
|
+
expect(ui.pendingRequests).toEqual([]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('gives the extension its default at the timeout, or when aborted', async () => {
|
|
24
|
+
const { ui, events } = setup();
|
|
25
|
+
expect(await ui.confirm('t', 'm', { timeout: 10 })).toBe(false);
|
|
26
|
+
expect(events.at(-1).type).toBe('ui_request_resolved');
|
|
27
|
+
const controller = new AbortController();
|
|
28
|
+
const selected = ui.select('t', ['a'], { signal: controller.signal });
|
|
29
|
+
controller.abort();
|
|
30
|
+
expect(await selected).toBeUndefined();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('treats a cancelled answer as no answer', async () => {
|
|
34
|
+
const { ui, events } = setup();
|
|
35
|
+
const value = ui.input('t');
|
|
36
|
+
ui.respond({ type: 'extension_ui_response', id: events[0].id, cancelled: true });
|
|
37
|
+
expect(await value).toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('ends open dialogs when the session closes', async () => {
|
|
41
|
+
const { ui } = setup();
|
|
42
|
+
const answer = ui.editor('t');
|
|
43
|
+
ui.cancelAll();
|
|
44
|
+
expect(await answer).toBeUndefined();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('sends what needs no answer and forgets it', () => {
|
|
48
|
+
const { ui, events } = setup();
|
|
49
|
+
ui.notify('hello', 'info');
|
|
50
|
+
ui.setStatus('k', 'v');
|
|
51
|
+
expect(events.map(event => event.method)).toEqual(['notify', 'setStatus']);
|
|
52
|
+
expect(ui.pendingRequests).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('sends only widgets a client can draw', () => {
|
|
56
|
+
const { ui, events } = setup();
|
|
57
|
+
ui.setWidget('w', () => undefined);
|
|
58
|
+
expect(events).toEqual([]);
|
|
59
|
+
ui.setWidget('w', ['line']);
|
|
60
|
+
expect(events[0]).toMatchObject({ method: 'setWidget', widgetLines: ['line'] });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('leaves styled text plain', () => {
|
|
64
|
+
const { ui } = setup();
|
|
65
|
+
expect(ui.theme.fg('accent', 'text')).toBe('text');
|
|
66
|
+
expect(ui.theme.bold('text')).toBe('text');
|
|
67
|
+
});
|
|
68
|
+
});
|