@celljs/ta-host 0.0.0-stage → 4.5.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.
Files changed (155) hide show
  1. package/README.md +33 -2
  2. package/cell.schema.json +33 -0
  3. package/cell.yml +14 -0
  4. package/esm/node/client-channel.d.ts +31 -0
  5. package/esm/node/client-channel.js +84 -0
  6. package/esm/node/client-channel.js.map +1 -0
  7. package/esm/node/event-log.d.ts +28 -0
  8. package/esm/node/event-log.js +48 -0
  9. package/esm/node/event-log.js.map +1 -0
  10. package/esm/node/host-protocol.d.ts +27 -0
  11. package/esm/node/host-protocol.js +4 -0
  12. package/esm/node/host-protocol.js.map +1 -0
  13. package/esm/node/index.d.ts +13 -0
  14. package/esm/node/index.js +14 -0
  15. package/esm/node/index.js.map +1 -0
  16. package/esm/node/local-exec-environment-factory.d.ts +4 -0
  17. package/esm/node/local-exec-environment-factory.js +18 -0
  18. package/esm/node/local-exec-environment-factory.js.map +1 -0
  19. package/esm/node/module.d.ts +3 -0
  20. package/esm/node/module.js +4 -0
  21. package/esm/node/module.js.map +1 -0
  22. package/esm/node/receipt-book.d.ts +12 -0
  23. package/esm/node/receipt-book.js +38 -0
  24. package/esm/node/receipt-book.js.map +1 -0
  25. package/esm/node/remote-ui-context.d.ts +63 -0
  26. package/esm/node/remote-ui-context.js +130 -0
  27. package/esm/node/remote-ui-context.js.map +1 -0
  28. package/esm/node/ta-access.d.ts +30 -0
  29. package/esm/node/ta-access.js +107 -0
  30. package/esm/node/ta-access.js.map +1 -0
  31. package/esm/node/ta-errors.d.ts +11 -0
  32. package/esm/node/ta-errors.js +44 -0
  33. package/esm/node/ta-errors.js.map +1 -0
  34. package/esm/node/ta-event-util.d.ts +11 -0
  35. package/esm/node/ta-event-util.js +20 -0
  36. package/esm/node/ta-event-util.js.map +1 -0
  37. package/esm/node/ta-server-impl.d.ts +50 -0
  38. package/esm/node/ta-server-impl.js +226 -0
  39. package/esm/node/ta-server-impl.js.map +1 -0
  40. package/esm/node/ta-session-factory.d.ts +32 -0
  41. package/esm/node/ta-session-factory.js +151 -0
  42. package/esm/node/ta-session-factory.js.map +1 -0
  43. package/esm/node/ta-session-registry.d.ts +20 -0
  44. package/esm/node/ta-session-registry.js +85 -0
  45. package/esm/node/ta-session-registry.js.map +1 -0
  46. package/esm/node/ta-session.d.ts +53 -0
  47. package/esm/node/ta-session.js +157 -0
  48. package/esm/node/ta-session.js.map +1 -0
  49. package/esm/package.json +3 -0
  50. package/lib/node/client-channel.d.ts +32 -0
  51. package/lib/node/client-channel.d.ts.map +1 -0
  52. package/lib/node/client-channel.js +88 -0
  53. package/lib/node/client-channel.js.map +1 -0
  54. package/lib/node/client-channel.spec.d.ts +2 -0
  55. package/lib/node/client-channel.spec.d.ts.map +1 -0
  56. package/lib/node/client-channel.spec.js +106 -0
  57. package/lib/node/client-channel.spec.js.map +1 -0
  58. package/lib/node/event-log.d.ts +29 -0
  59. package/lib/node/event-log.d.ts.map +1 -0
  60. package/lib/node/event-log.js +52 -0
  61. package/lib/node/event-log.js.map +1 -0
  62. package/lib/node/event-log.spec.d.ts +2 -0
  63. package/lib/node/event-log.spec.d.ts.map +1 -0
  64. package/lib/node/event-log.spec.js +48 -0
  65. package/lib/node/event-log.spec.js.map +1 -0
  66. package/lib/node/host-protocol.d.ts +28 -0
  67. package/lib/node/host-protocol.d.ts.map +1 -0
  68. package/lib/node/host-protocol.js +7 -0
  69. package/lib/node/host-protocol.js.map +1 -0
  70. package/lib/node/index.d.ts +14 -0
  71. package/lib/node/index.d.ts.map +1 -0
  72. package/lib/node/index.js +30 -0
  73. package/lib/node/index.js.map +1 -0
  74. package/lib/node/local-exec-environment-factory.d.ts +5 -0
  75. package/lib/node/local-exec-environment-factory.d.ts.map +1 -0
  76. package/lib/node/local-exec-environment-factory.js +21 -0
  77. package/lib/node/local-exec-environment-factory.js.map +1 -0
  78. package/lib/node/module.d.ts +4 -0
  79. package/lib/node/module.d.ts.map +1 -0
  80. package/lib/node/module.js +6 -0
  81. package/lib/node/module.js.map +1 -0
  82. package/lib/node/receipt-book.d.ts +13 -0
  83. package/lib/node/receipt-book.d.ts.map +1 -0
  84. package/lib/node/receipt-book.js +42 -0
  85. package/lib/node/receipt-book.js.map +1 -0
  86. package/lib/node/receipt-book.spec.d.ts +2 -0
  87. package/lib/node/receipt-book.spec.d.ts.map +1 -0
  88. package/lib/node/receipt-book.spec.js +39 -0
  89. package/lib/node/receipt-book.spec.js.map +1 -0
  90. package/lib/node/remote-ui-context.d.ts +64 -0
  91. package/lib/node/remote-ui-context.d.ts.map +1 -0
  92. package/lib/node/remote-ui-context.js +134 -0
  93. package/lib/node/remote-ui-context.js.map +1 -0
  94. package/lib/node/remote-ui-context.spec.d.ts +2 -0
  95. package/lib/node/remote-ui-context.spec.d.ts.map +1 -0
  96. package/lib/node/remote-ui-context.spec.js +62 -0
  97. package/lib/node/remote-ui-context.spec.js.map +1 -0
  98. package/lib/node/ta-access.d.ts +31 -0
  99. package/lib/node/ta-access.d.ts.map +1 -0
  100. package/lib/node/ta-access.js +110 -0
  101. package/lib/node/ta-access.js.map +1 -0
  102. package/lib/node/ta-access.spec.d.ts +2 -0
  103. package/lib/node/ta-access.spec.d.ts.map +1 -0
  104. package/lib/node/ta-access.spec.js +25 -0
  105. package/lib/node/ta-access.spec.js.map +1 -0
  106. package/lib/node/ta-errors.d.ts +12 -0
  107. package/lib/node/ta-errors.d.ts.map +1 -0
  108. package/lib/node/ta-errors.js +47 -0
  109. package/lib/node/ta-errors.js.map +1 -0
  110. package/lib/node/ta-event-util.d.ts +12 -0
  111. package/lib/node/ta-event-util.d.ts.map +1 -0
  112. package/lib/node/ta-event-util.js +23 -0
  113. package/lib/node/ta-event-util.js.map +1 -0
  114. package/lib/node/ta-event-util.spec.d.ts +2 -0
  115. package/lib/node/ta-event-util.spec.d.ts.map +1 -0
  116. package/lib/node/ta-event-util.spec.js +28 -0
  117. package/lib/node/ta-event-util.spec.js.map +1 -0
  118. package/lib/node/ta-server-impl.d.ts +51 -0
  119. package/lib/node/ta-server-impl.d.ts.map +1 -0
  120. package/lib/node/ta-server-impl.js +229 -0
  121. package/lib/node/ta-server-impl.js.map +1 -0
  122. package/lib/node/ta-session-factory.d.ts +33 -0
  123. package/lib/node/ta-session-factory.d.ts.map +1 -0
  124. package/lib/node/ta-session-factory.js +154 -0
  125. package/lib/node/ta-session-factory.js.map +1 -0
  126. package/lib/node/ta-session-registry.d.ts +21 -0
  127. package/lib/node/ta-session-registry.d.ts.map +1 -0
  128. package/lib/node/ta-session-registry.js +86 -0
  129. package/lib/node/ta-session-registry.js.map +1 -0
  130. package/lib/node/ta-session.d.ts +54 -0
  131. package/lib/node/ta-session.d.ts.map +1 -0
  132. package/lib/node/ta-session.js +161 -0
  133. package/lib/node/ta-session.js.map +1 -0
  134. package/package.json +80 -4
  135. package/src/node/client-channel.spec.ts +117 -0
  136. package/src/node/client-channel.ts +94 -0
  137. package/src/node/event-log.spec.ts +52 -0
  138. package/src/node/event-log.ts +59 -0
  139. package/src/node/host-protocol.ts +32 -0
  140. package/src/node/index.ts +13 -0
  141. package/src/node/local-exec-environment-factory.ts +9 -0
  142. package/src/node/module.ts +4 -0
  143. package/src/node/receipt-book.spec.ts +41 -0
  144. package/src/node/receipt-book.ts +40 -0
  145. package/src/node/remote-ui-context.spec.ts +68 -0
  146. package/src/node/remote-ui-context.ts +168 -0
  147. package/src/node/ta-access.spec.ts +27 -0
  148. package/src/node/ta-access.ts +98 -0
  149. package/src/node/ta-errors.ts +55 -0
  150. package/src/node/ta-event-util.spec.ts +29 -0
  151. package/src/node/ta-event-util.ts +23 -0
  152. package/src/node/ta-server-impl.ts +264 -0
  153. package/src/node/ta-session-factory.ts +156 -0
  154. package/src/node/ta-session-registry.ts +75 -0
  155. package/src/node/ta-session.ts +193 -0
@@ -0,0 +1,157 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { EventLog } from './event-log.js';
3
+ import { ReceiptBook } from './receipt-book.js';
4
+ import { RemoteUiContext } from './remote-ui-context.js';
5
+ import { TaEventUtil } from './ta-event-util.js';
6
+ /**
7
+ * One open session of `ta serve`: pi's runtime, its event log, and the clients attached to it. Its id
8
+ * stays the same while the runtime moves between conversations.
9
+ */
10
+ export class TaSession {
11
+ constructor(context) {
12
+ this.context = context;
13
+ this.id = randomUUID();
14
+ this.createdAt = Date.now();
15
+ this.receipts = new ReceiptBook();
16
+ this.channels = new Set();
17
+ this.started = false;
18
+ this.runtime = context.runtime;
19
+ this.log = new EventLog(this.id, context.options.eventBufferBytes);
20
+ this.ui = new RemoteUiContext(event => this.publish(event));
21
+ }
22
+ get pi() {
23
+ return this.runtime.session;
24
+ }
25
+ async start() {
26
+ this.runtime.setRebindSession(() => this.bind());
27
+ await this.bind();
28
+ this.started = true;
29
+ this.watchIdle();
30
+ }
31
+ info() {
32
+ const { pi } = this;
33
+ // Without a model pi's agent holds a placeholder that no runtime knows.
34
+ const model = pi.model && pi.modelRuntime.getModel(pi.model.provider, pi.model.id);
35
+ return {
36
+ sessionId: this.id,
37
+ piSessionId: pi.sessionId,
38
+ cwd: this.runtime.cwd,
39
+ sessionFile: pi.sessionFile,
40
+ sessionName: pi.sessionName,
41
+ model: model ? `${model.provider}/${model.id}` : undefined,
42
+ isStreaming: pi.isStreaming,
43
+ messageCount: pi.messages.length,
44
+ attached: this.channels.size,
45
+ createdAt: this.createdAt,
46
+ };
47
+ }
48
+ attach(channel, afterSeq) {
49
+ this.channels.add(channel);
50
+ this.watchIdle();
51
+ const { events, gap } = afterSeq === undefined ? { events: [], gap: false } : this.log.since(afterSeq);
52
+ return { info: this.info(), events, pendingUi: this.ui.pendingRequests, lastSeq: this.log.lastSeq, gap };
53
+ }
54
+ detach(channel) {
55
+ if (this.channels.delete(channel)) {
56
+ this.watchIdle();
57
+ }
58
+ }
59
+ respondUi(response) {
60
+ return this.ui.respond(response);
61
+ }
62
+ prompt(input) {
63
+ return this.accept(input, () => new Promise((resolve, reject) => {
64
+ var _a;
65
+ this.pi
66
+ .prompt(input.message, {
67
+ images: input.images,
68
+ streamingBehavior: (_a = input.streamingBehavior) !== null && _a !== void 0 ? _a : 'followUp',
69
+ source: 'rpc',
70
+ preflightResult: resolve,
71
+ })
72
+ // After the preflight, failures of the run reach the clients as events.
73
+ .then(() => resolve('handled'), reject);
74
+ }));
75
+ }
76
+ steer(input) {
77
+ return this.accept(input, () => this.pi.steer(input.message, input.images, { source: 'rpc' }));
78
+ }
79
+ followUp(input) {
80
+ return this.accept(input, () => this.pi.followUp(input.message, input.images, { source: 'rpc' }));
81
+ }
82
+ async close() {
83
+ var _a;
84
+ clearTimeout(this.idleTimer);
85
+ this.ui.cancelAll();
86
+ this.publish({ type: 'session_closed' });
87
+ this.channels.clear();
88
+ (_a = this.unsubscribe) === null || _a === void 0 ? void 0 : _a.call(this);
89
+ if (this.pi.isStreaming) {
90
+ await this.pi.abort();
91
+ }
92
+ await this.runtime.dispose();
93
+ this.context.lock.releaseAll();
94
+ }
95
+ async accept(input, run) {
96
+ const duplicate = this.receipts.take(input);
97
+ if (duplicate) {
98
+ return duplicate;
99
+ }
100
+ try {
101
+ const disposition = await run();
102
+ return { clientMessageId: input.clientMessageId, status: disposition === 'queued' ? 'queued' : 'accepted' };
103
+ }
104
+ catch (error) {
105
+ this.receipts.forget(input.clientMessageId);
106
+ throw error;
107
+ }
108
+ }
109
+ /** Connects this object to the session the runtime has now: at the start and after each replacement. */
110
+ async bind() {
111
+ var _a;
112
+ (_a = this.unsubscribe) === null || _a === void 0 ? void 0 : _a.call(this);
113
+ const { pi, runtime } = this;
114
+ await pi.bindExtensions({
115
+ uiContext: this.ui,
116
+ mode: 'rpc',
117
+ commandContextActions: {
118
+ waitForIdle: () => pi.waitForIdle(),
119
+ newSession: options => runtime.newSession(options),
120
+ fork: async (entryId, options) => ({ cancelled: (await runtime.fork(entryId, options)).cancelled }),
121
+ navigateTree: async (targetId, options) => ({ cancelled: (await pi.navigateTree(targetId, options)).cancelled }),
122
+ switchSession: (sessionPath, options) => runtime.switchSession(sessionPath, options),
123
+ reload: () => pi.reload(),
124
+ },
125
+ shutdownHandler: () => this.context.requestClose(this),
126
+ onError: error => this.publish({ type: 'extension_error', extensionPath: error.extensionPath, event: error.event, error: error.error }),
127
+ });
128
+ this.unsubscribe = pi.subscribe(event => {
129
+ this.publish(TaEventUtil.toWire(event));
130
+ if (event.type === 'agent_settled') {
131
+ this.watchIdle();
132
+ }
133
+ });
134
+ if (this.started) {
135
+ this.publish({ type: 'session_replaced', info: this.info() });
136
+ }
137
+ }
138
+ publish(event) {
139
+ const logged = this.log.append(event);
140
+ for (const channel of this.channels) {
141
+ channel.send(logged);
142
+ }
143
+ }
144
+ isIdle() {
145
+ return this.channels.size === 0 && !this.pi.isStreaming && this.ui.pendingRequests.length === 0;
146
+ }
147
+ /** One timer per session, armed only when nothing is attached: an idle server does no work. */
148
+ watchIdle() {
149
+ clearTimeout(this.idleTimer);
150
+ this.idleTimer = undefined;
151
+ if (this.isIdle()) {
152
+ this.idleTimer = setTimeout(() => this.isIdle() && this.context.requestClose(this), this.context.options.idleTimeoutMs);
153
+ this.idleTimer.unref();
154
+ }
155
+ }
156
+ }
157
+ //# sourceMappingURL=ta-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ta-session.js","sourceRoot":"","sources":["../../src/node/ta-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAIzC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAc9C;;;GAGG;AACH,MAAM,OAAO,SAAS;IAYlB,YAA+B,OAAyB;QAAzB,YAAO,GAAP,OAAO,CAAkB;QAX/C,OAAE,GAAG,UAAU,EAAE,CAAC;QAClB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AE,CAAC,CAAC;QAClE,CAAC;IACL,CAAC;IAES,OAAO,CAAC,KAAc;QAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACtC,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACzB,CAAC;IACL,CAAC;IAES,MAAM;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,WAAW,IAAI,IAAI,CAAC,EAAE,CAAC,eAAe,CAAC,MAAM,KAAK,CAAC,CAAC;IACpG,CAAC;IAED,+FAA+F;IACrF,SAAS;QACf,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;YAChB,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;YACxH,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;IACL,CAAC;CACJ"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,32 @@
1
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
2
+ import { type TaEventEnvelope } from '@celljs/ta-protocol';
3
+ import type { LoggedEvent } from './event-log';
4
+ /**
5
+ * Events on their way to one client, one message at a time: the next leaves when the client has
6
+ * answered the last, so that a slow client costs a bounded queue and nothing while no events flow.
7
+ * A model streams hundreds of deltas a second: streamed events leave at most once per interval, the
8
+ * first of them at once. Other events (a message starts, a tool ends, a dialog opens) leave at once,
9
+ * with what is queued, so that nothing a user waits for is delayed.
10
+ */
11
+ export declare class ClientChannel {
12
+ readonly connection: RpcConnection;
13
+ protected readonly maxBacklogBytes: number;
14
+ protected queue: TaEventEnvelope[];
15
+ protected queuedBytes: number;
16
+ protected timer?: ReturnType<typeof setTimeout>;
17
+ protected sending: boolean;
18
+ protected lastFlushAt: number;
19
+ /** When the timer fires, on the clock of `performance.now()`. */
20
+ protected dueAt: number;
21
+ /** The queue holds an event that should not wait. */
22
+ protected urgent: boolean;
23
+ /** The queue holds a streamed event. */
24
+ protected streamed: boolean;
25
+ constructor(connection: RpcConnection, maxBacklogBytes: number);
26
+ send(logged: LoggedEvent): void;
27
+ /** A timer, not a direct send: the events of the same turn of the event loop leave together. */
28
+ protected schedule(): void;
29
+ protected flush(): void;
30
+ protected clear(): void;
31
+ }
32
+ //# sourceMappingURL=client-channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-channel.d.ts","sourceRoot":"","sources":["../../src/node/client-channel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAY,KAAK,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAO/C;;;;;;GAMG;AACH,qBAAa,aAAa;IAclB,QAAQ,CAAC,UAAU,EAAE,aAAa;IAClC,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,MAAM;IAd9C,SAAS,CAAC,KAAK,EAAE,eAAe,EAAE,CAAM;IACxC,SAAS,CAAC,WAAW,SAAK;IAC1B,SAAS,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;IAChD,SAAS,CAAC,OAAO,UAAS;IAC1B,SAAS,CAAC,WAAW,SAA4B;IACjD,iEAAiE;IACjE,SAAS,CAAC,KAAK,SAAK;IACpB,qDAAqD;IACrD,SAAS,CAAC,MAAM,UAAS;IACzB,wCAAwC;IACxC,SAAS,CAAC,QAAQ,UAAS;IAE3B,YACa,UAAU,EAAE,aAAa,EACf,eAAe,EAAE,MAAM,EAC1C;IAEJ,IAAI,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAY9B;IAED,gGAAgG;IAChG,SAAS,CAAC,QAAQ,IAAI,IAAI,CAQzB;IAED,SAAS,CAAC,KAAK,IAAI,IAAI,CAwBtB;IAED,SAAS,CAAC,KAAK,IAAI,IAAI,CAOtB;CACJ"}
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientChannel = void 0;
4
+ const ta_protocol_1 = require("@celljs/ta-protocol");
5
+ const FLUSH_INTERVAL_MS = 30;
6
+ /** The events that come by the hundred while a model or a tool streams; the others leave at once. */
7
+ const STREAMED_EVENTS = new Set(['message_update', 'tool_execution_update']);
8
+ /**
9
+ * Events on their way to one client, one message at a time: the next leaves when the client has
10
+ * answered the last, so that a slow client costs a bounded queue and nothing while no events flow.
11
+ * A model streams hundreds of deltas a second: streamed events leave at most once per interval, the
12
+ * first of them at once. Other events (a message starts, a tool ends, a dialog opens) leave at once,
13
+ * with what is queued, so that nothing a user waits for is delayed.
14
+ */
15
+ class ClientChannel {
16
+ constructor(connection, maxBacklogBytes) {
17
+ this.connection = connection;
18
+ this.maxBacklogBytes = maxBacklogBytes;
19
+ this.queue = [];
20
+ this.queuedBytes = 0;
21
+ this.sending = false;
22
+ this.lastFlushAt = Number.NEGATIVE_INFINITY;
23
+ /** When the timer fires, on the clock of `performance.now()`. */
24
+ this.dueAt = 0;
25
+ /** The queue holds an event that should not wait. */
26
+ this.urgent = false;
27
+ /** The queue holds a streamed event. */
28
+ this.streamed = false;
29
+ }
30
+ send(logged) {
31
+ this.queue.push(logged.envelope);
32
+ this.queuedBytes += logged.size;
33
+ if (this.queuedBytes > this.maxBacklogBytes) {
34
+ this.clear();
35
+ this.connection.close(1013, 'The client is too far behind; attach again with the last seq it has.');
36
+ return;
37
+ }
38
+ const streamed = STREAMED_EVENTS.has(logged.envelope.event.type);
39
+ this.streamed || (this.streamed = streamed);
40
+ this.urgent || (this.urgent = !streamed);
41
+ this.schedule();
42
+ }
43
+ /** A timer, not a direct send: the events of the same turn of the event loop leave together. */
44
+ schedule() {
45
+ const due = this.urgent ? 0 : this.lastFlushAt + FLUSH_INTERVAL_MS;
46
+ if (this.timer !== undefined && this.dueAt <= due) {
47
+ return;
48
+ }
49
+ clearTimeout(this.timer);
50
+ this.dueAt = due;
51
+ this.timer = setTimeout(() => this.flush(), Math.max(0, due - performance.now()));
52
+ }
53
+ flush() {
54
+ this.timer = undefined;
55
+ if (this.sending || !this.queue.length || this.connection.closed) {
56
+ return;
57
+ }
58
+ const batch = this.queue;
59
+ this.queue = [];
60
+ this.queuedBytes = 0;
61
+ if (this.streamed) {
62
+ this.lastFlushAt = performance.now();
63
+ }
64
+ this.urgent = false;
65
+ this.streamed = false;
66
+ this.sending = true;
67
+ this.connection
68
+ .client(ta_protocol_1.TaClient)
69
+ .onEvents(batch)
70
+ .catch(() => undefined)
71
+ .finally(() => {
72
+ this.sending = false;
73
+ if (this.queue.length) {
74
+ this.schedule();
75
+ }
76
+ });
77
+ }
78
+ clear() {
79
+ clearTimeout(this.timer);
80
+ this.timer = undefined;
81
+ this.queue = [];
82
+ this.queuedBytes = 0;
83
+ this.urgent = false;
84
+ this.streamed = false;
85
+ }
86
+ }
87
+ exports.ClientChannel = ClientChannel;
88
+ //# sourceMappingURL=client-channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-channel.js","sourceRoot":"","sources":["../../src/node/client-channel.ts"],"names":[],"mappings":";;;AACA,qDAAqE;AAGrE,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,qGAAqG;AACrG,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,gBAAgB,EAAE,uBAAuB,CAAC,CAAC,CAAC;AAE7E;;;;;;GA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C;QACL,CAAC,CAAC,CAAC;IACX,CAAC;IAES,KAAK;QACX,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;IAC1B,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=client-channel.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-channel.spec.d.ts","sourceRoot":"","sources":["../../src/node/client-channel.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bun_test_1 = require("bun:test");
4
+ const client_channel_1 = require("./client-channel");
5
+ /** A streamed event by default, which waits for the interval. */
6
+ const logged = (seq, size = 10, type = 'message_update') => ({ envelope: { sessionId: 's', seq, event: { type } }, size });
7
+ function setup(maxBacklog = 1000) {
8
+ const batches = [];
9
+ const answers = [];
10
+ const state = { closed: false };
11
+ const connection = {
12
+ get closed() {
13
+ return state.closed;
14
+ },
15
+ close: () => {
16
+ state.closed = true;
17
+ },
18
+ client: () => ({
19
+ onEvents: (batch) => {
20
+ batches.push(batch);
21
+ return new Promise(done => answers.push(done));
22
+ },
23
+ }),
24
+ };
25
+ return { channel: new client_channel_1.ClientChannel(connection, maxBacklog), batches, answers, state };
26
+ }
27
+ const seqsOf = (batch) => batch.map(envelope => envelope.seq);
28
+ (0, bun_test_1.describe)('ClientChannel', () => {
29
+ (0, bun_test_1.it)('sends the first events at once, together when they arrive together', async () => {
30
+ const { channel, batches } = setup();
31
+ channel.send(logged(1));
32
+ channel.send(logged(2));
33
+ (0, bun_test_1.expect)(batches).toEqual([]);
34
+ await Bun.sleep(5);
35
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1, 2]]);
36
+ });
37
+ (0, bun_test_1.it)('sends the events of an interval as one batch after the first', async () => {
38
+ const { channel, batches, answers } = setup();
39
+ channel.send(logged(1));
40
+ await Bun.sleep(5);
41
+ answers[0]();
42
+ channel.send(logged(2));
43
+ await Bun.sleep(5);
44
+ channel.send(logged(3));
45
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1]]);
46
+ await Bun.sleep(60);
47
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1], [2, 3]]);
48
+ });
49
+ (0, bun_test_1.it)('sends other events at once, with the deltas queued before them', async () => {
50
+ const { channel, batches, answers } = setup();
51
+ channel.send(logged(1));
52
+ await Bun.sleep(5);
53
+ answers[0]();
54
+ channel.send(logged(2));
55
+ channel.send(logged(3, 10, 'tool_execution_end'));
56
+ await Bun.sleep(5);
57
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1], [2, 3]]);
58
+ });
59
+ (0, bun_test_1.it)('sends the first delta of a reply at once, even right after its message started', async () => {
60
+ const { channel, batches, answers } = setup();
61
+ channel.send(logged(1, 10, 'message_start'));
62
+ await Bun.sleep(5);
63
+ answers[0]();
64
+ channel.send(logged(2));
65
+ await Bun.sleep(5);
66
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1], [2]]);
67
+ });
68
+ (0, bun_test_1.it)('sends at once again after a quiet interval', async () => {
69
+ const { channel, batches, answers } = setup();
70
+ channel.send(logged(1));
71
+ await Bun.sleep(5);
72
+ answers[0]();
73
+ await Bun.sleep(40);
74
+ channel.send(logged(2));
75
+ await Bun.sleep(5);
76
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1], [2]]);
77
+ });
78
+ (0, bun_test_1.it)('waits for the client before the next batch, and then sends the rest together', async () => {
79
+ const { channel, batches, answers } = setup();
80
+ channel.send(logged(1));
81
+ await Bun.sleep(60);
82
+ channel.send(logged(2));
83
+ channel.send(logged(3));
84
+ await Bun.sleep(60);
85
+ (0, bun_test_1.expect)(batches.length).toBe(1);
86
+ answers[0]();
87
+ await Bun.sleep(60);
88
+ (0, bun_test_1.expect)(batches.map(seqsOf)).toEqual([[1], [2, 3]]);
89
+ });
90
+ (0, bun_test_1.it)('disconnects a client too far behind instead of queuing without end', () => {
91
+ const { channel, state } = setup(25);
92
+ channel.send(logged(1));
93
+ channel.send(logged(2));
94
+ (0, bun_test_1.expect)(state.closed).toBe(false);
95
+ channel.send(logged(3));
96
+ (0, bun_test_1.expect)(state.closed).toBe(true);
97
+ });
98
+ (0, bun_test_1.it)('sends nothing to a closed connection', async () => {
99
+ const { channel, batches, state } = setup();
100
+ channel.send(logged(1));
101
+ state.closed = true;
102
+ await Bun.sleep(60);
103
+ (0, bun_test_1.expect)(batches).toEqual([]);
104
+ });
105
+ });
106
+ //# sourceMappingURL=client-channel.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-channel.spec.js","sourceRoot":"","sources":["../../src/node/client-channel.spec.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAGhD,qDAAiD;AAGjD,iEAAiE;AACjE,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,IAAI,GAAG,EAAE,EAAE,IAAI,GAAG,gBAAgB,EAAe,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,IAAI,EAAa,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;AAE3J,SAAS,KAAK,CAAC,UAAU,GAAG,IAAI;IAC5B,MAAM,OAAO,GAAwB,EAAE,CAAC;IACxC,MAAM,OAAO,GAAmB,EAAE,CAAC;IACnC,MAAM,KAAK,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAChC,MAAM,UAAU,GAAG;QACf,IAAI,MAAM;YACN,OAAO,KAAK,CAAC,MAAM,CAAC;QACxB,CAAC;QACD,KAAK,EAAE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AAK,EAAE,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC;QACrC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACxB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACxB,IAAA,iBAAM,EAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACjC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACxB,IAAA,iBAAM,EAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,IAAA,aAAE,EAAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;QAClD,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE,CAAC;QAC5C,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACxB,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC;QACpB,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACpB,IAAA,iBAAM,EAAC,OAAO,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -0,0 +1,29 @@
1
+ import type { TaEvent, TaEventEnvelope } from '@celljs/ta-protocol';
2
+ export interface LoggedEvent {
3
+ envelope: TaEventEnvelope;
4
+ /** Approximate size in bytes. */
5
+ size: number;
6
+ }
7
+ /**
8
+ * The latest events of a session, by size, so that a client that reconnects can catch up. Only that:
9
+ * what happened is in the session file, and a client that fell further behind reads it again.
10
+ */
11
+ export declare class EventLog {
12
+ protected readonly sessionId: string;
13
+ protected readonly maxBytes: number;
14
+ protected entries: LoggedEvent[];
15
+ protected head: number;
16
+ protected bytes: number;
17
+ protected seq: number;
18
+ constructor(sessionId: string, maxBytes: number);
19
+ get lastSeq(): number;
20
+ append(event: TaEvent): LoggedEvent;
21
+ /** The events after `afterSeq`; `gap` when some of them are gone. */
22
+ since(afterSeq: number): {
23
+ events: TaEventEnvelope[];
24
+ gap: boolean;
25
+ };
26
+ /** Deltas are most of the events and their size is known; the others are rare enough to measure. */
27
+ protected sizeOf(event: TaEvent): number;
28
+ }
29
+ //# sourceMappingURL=event-log.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-log.d.ts","sourceRoot":"","sources":["../../src/node/event-log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEpE,MAAM,WAAW,WAAW;IACxB,QAAQ,EAAE,eAAe,CAAC;IAC1B,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,qBAAa,QAAQ;IAOb,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM;IACpC,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM;IAPvC,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,CAAM;IACtC,SAAS,CAAC,IAAI,SAAK;IACnB,SAAS,CAAC,KAAK,SAAK;IACpB,SAAS,CAAC,GAAG,SAAK;IAElB,YACuB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EACnC;IAEJ,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAYlC;IAED,qEAAqE;IACrE,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG;QAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QAAC,GAAG,EAAE,OAAO,CAAA;KAAE,CAMnE;IAED,oGAAoG;IACpG,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAMvC;CACJ"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventLog = void 0;
4
+ /**
5
+ * The latest events of a session, by size, so that a client that reconnects can catch up. Only that:
6
+ * what happened is in the session file, and a client that fell further behind reads it again.
7
+ */
8
+ class EventLog {
9
+ constructor(sessionId, maxBytes) {
10
+ this.sessionId = sessionId;
11
+ this.maxBytes = maxBytes;
12
+ this.entries = [];
13
+ this.head = 0;
14
+ this.bytes = 0;
15
+ this.seq = 0;
16
+ }
17
+ get lastSeq() {
18
+ return this.seq;
19
+ }
20
+ append(event) {
21
+ const logged = { envelope: { sessionId: this.sessionId, seq: ++this.seq, event }, size: this.sizeOf(event) };
22
+ this.entries.push(logged);
23
+ this.bytes += logged.size;
24
+ while (this.bytes > this.maxBytes && this.entries.length - this.head > 1) {
25
+ this.bytes -= this.entries[this.head++].size;
26
+ }
27
+ if (this.head > 1024 && this.head * 2 > this.entries.length) {
28
+ this.entries = this.entries.slice(this.head);
29
+ this.head = 0;
30
+ }
31
+ return logged;
32
+ }
33
+ /** The events after `afterSeq`; `gap` when some of them are gone. */
34
+ since(afterSeq) {
35
+ const oldest = this.head < this.entries.length ? this.entries[this.head].envelope.seq : this.seq + 1;
36
+ if (afterSeq + 1 < oldest || afterSeq > this.seq) {
37
+ return { events: [], gap: true };
38
+ }
39
+ return { events: this.entries.slice(this.head + afterSeq + 1 - oldest).map(entry => entry.envelope), gap: false };
40
+ }
41
+ /** Deltas are most of the events and their size is known; the others are rare enough to measure. */
42
+ sizeOf(event) {
43
+ var _a;
44
+ if (event.type === 'message_update') {
45
+ const delta = event.assistantMessageEvent.delta;
46
+ return 256 + ((_a = delta === null || delta === void 0 ? void 0 : delta.length) !== null && _a !== void 0 ? _a : 0);
47
+ }
48
+ return JSON.stringify(event).length;
49
+ }
50
+ }
51
+ exports.EventLog = EventLog;
52
+ //# sourceMappingURL=event-log.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-log.js","sourceRoot":"","sources":["../../src/node/event-log.ts"],"names":[],"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"use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bun_test_1 = require("bun:test");
4
+ const event_log_1 = require("./event-log");
5
+ const event = (size = 1) => ({ type: 'ui_request_resolved', id: 'x'.repeat(size) });
6
+ const seqs = (log, after) => log.since(after).events.map(envelope => envelope.seq);
7
+ (0, bun_test_1.describe)('EventLog', () => {
8
+ (0, bun_test_1.it)('numbers events from one and replays those after a seq', () => {
9
+ const log = new event_log_1.EventLog('s', 10000);
10
+ log.append(event());
11
+ log.append(event());
12
+ log.append(event());
13
+ (0, bun_test_1.expect)(log.lastSeq).toBe(3);
14
+ (0, bun_test_1.expect)(seqs(log, 1)).toEqual([2, 3]);
15
+ (0, bun_test_1.expect)(seqs(log, 3)).toEqual([]);
16
+ (0, bun_test_1.expect)(log.append(event()).envelope.sessionId).toBe('s');
17
+ });
18
+ (0, bun_test_1.it)('drops the oldest events beyond its size and reports the gap', () => {
19
+ const log = new event_log_1.EventLog('s', 200);
20
+ for (let i = 0; i < 5; i++) {
21
+ log.append(event(80));
22
+ }
23
+ (0, bun_test_1.expect)(log.since(0).gap).toBe(true);
24
+ (0, bun_test_1.expect)(log.since(0).events).toEqual([]);
25
+ (0, bun_test_1.expect)(log.since(4)).toMatchObject({ gap: false });
26
+ (0, bun_test_1.expect)(seqs(log, 4)).toEqual([5]);
27
+ });
28
+ (0, bun_test_1.it)('keeps the latest event even when it alone is too big', () => {
29
+ const log = new event_log_1.EventLog('s', 10);
30
+ log.append(event(100));
31
+ (0, bun_test_1.expect)(seqs(log, 0)).toEqual([1]);
32
+ });
33
+ (0, bun_test_1.it)('reports a gap for a seq it never gave', () => {
34
+ const log = new event_log_1.EventLog('s', 1000);
35
+ log.append(event());
36
+ (0, bun_test_1.expect)(log.since(7).gap).toBe(true);
37
+ });
38
+ (0, bun_test_1.it)('stays usable over many events without growing', () => {
39
+ const log = new event_log_1.EventLog('s', 500);
40
+ for (let i = 0; i < 20000; i++) {
41
+ log.append(event(20));
42
+ }
43
+ (0, bun_test_1.expect)(log.lastSeq).toBe(20000);
44
+ (0, bun_test_1.expect)(seqs(log, 19999)).toEqual([20000]);
45
+ (0, bun_test_1.expect)(log.entries.length).toBeLessThan(2100);
46
+ });
47
+ });
48
+ //# sourceMappingURL=event-log.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-log.spec.js","sourceRoot":"","sources":["../../src/node/event-log.spec.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAEhD,2CAAuC;AAEvC,MAAM,KAAK,GAAG,CAAC,IAAI,GAAG,CAAC,EAAW,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,qBAAqB,EAAE,EAAE,EAAE,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC7F,MAAM,IAAI,GAAG,CAAC,GAAa,EAAE,KAAa,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AAErG,IAAA,mBAAQ,EAAC,UAAU,EAAE,GAAG,EAAE;IACtB,IAAA,aAAE,EAAC,uDAAuD,EAAE,GAAG,EAAE;QAC7D,MAAM,GAAG,GAAG,IAAI,oBAAQ,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACrC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACpB,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACpB,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACpB,IAAA,iBAAM,EAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC5B,IAAA,iBAAM,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACrC,IAAA,iBAAM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vB,IAAA,iBAAM,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,IAAA,aAAE,EAAC,uCAAuC,EAAE,GAAG,EAAE;QAC7C,MAAM,GAAG,GAAG,IAAI,oBAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QACpC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACpB,IAAA,iBAAM,EAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,IAAA,aAAE,EAAC,+CAA+C,EAAE,GAAG,EAAE;QACrD,MAAM,GAAG,GAAG,IAAI,oBAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACnC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7B,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;QAC1B,CAAC;QACD,IAAA,iBAAM,EAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,IAAA,iBAAM,EAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1C,IAAA,iBAAM,EAAE,GAAyC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IACzF,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -0,0 +1,28 @@
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
+ export declare const TaSessionFactory: unique symbol;
6
+ export declare const TaSessionRegistry: unique symbol;
7
+ export declare const TA_HOST_OPTIONS = "ta.host";
8
+ /** `ta.host`. */
9
+ export interface TaHostOptions {
10
+ maxSessions: number;
11
+ idleTimeoutMs: number;
12
+ eventBufferBytes: number;
13
+ clientBacklogBytes: number;
14
+ }
15
+ export interface TaSessionFactory {
16
+ create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession>;
17
+ }
18
+ /** The open sessions. */
19
+ export interface TaSessionRegistry {
20
+ open(options: TaOpenSessionOptions): Promise<TaSession>;
21
+ /** Throws CELL_TA_SESSION_NOT_FOUND. */
22
+ require(sessionId: string): TaSession;
23
+ list(): TaSession[];
24
+ close(sessionId: string): Promise<void>;
25
+ closeAll(): Promise<void>;
26
+ channel(connection: RpcConnection): ClientChannel;
27
+ }
28
+ //# sourceMappingURL=host-protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host-protocol.d.ts","sourceRoot":"","sources":["../../src/node/host-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,eAAO,MAAM,gBAAgB,eAA6B,CAAC;AAC3D,eAAO,MAAM,iBAAiB,eAA8B,CAAC;AAE7D,eAAO,MAAM,eAAe,YAAY,CAAC;AAEzC,iBAAiB;AACjB,MAAM,WAAW,aAAa;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,kBAAkB,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,WAAW,gBAAgB;IAC7B,MAAM,CAAC,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;CACzG;AAED,yBAAyB;AACzB,MAAM,WAAW,iBAAiB;IAC9B,IAAI,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACxD,wCAAwC;IACxC,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,IAAI,IAAI,SAAS,EAAE,CAAC;IACpB,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,OAAO,CAAC,UAAU,EAAE,aAAa,GAAG,aAAa,CAAC;CACrD"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TA_HOST_OPTIONS = exports.TaSessionRegistry = exports.TaSessionFactory = void 0;
4
+ exports.TaSessionFactory = Symbol('TaSessionFactory');
5
+ exports.TaSessionRegistry = Symbol('TaSessionRegistry');
6
+ exports.TA_HOST_OPTIONS = 'ta.host';
7
+ //# sourceMappingURL=host-protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host-protocol.js","sourceRoot":"","sources":["../../src/node/host-protocol.ts"],"names":[],"mappings":";;;AAKa,QAAA,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAC9C,QAAA,iBAAiB,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAEhD,QAAA,eAAe,GAAG,SAAS,CAAC"}