@noetic-tools/acp 2.0.0-next.1 → 2.0.0-next.3
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/dist/agents.d.ts +5 -0
- package/dist/agents.js +13 -1
- package/dist/bin.js +19 -2
- package/dist/client-adapters.d.ts +2 -2
- package/dist/client.d.ts +9 -2
- package/dist/client.js +40 -1
- package/dist/connection.d.ts +25 -2
- package/dist/connection.js +455 -36
- package/dist/define.d.ts +13 -0
- package/dist/define.js +122 -4
- package/dist/index.d.ts +1 -1
- package/dist/items.d.ts +1 -1
- package/dist/keeper/client.d.ts +93 -0
- package/dist/keeper/client.js +468 -0
- package/dist/keeper/main.d.ts +6 -0
- package/dist/keeper/main.js +31 -0
- package/dist/keeper/process.d.ts +24 -0
- package/dist/keeper/process.js +425 -0
- package/dist/keeper/protocol.d.ts +74 -0
- package/dist/keeper/protocol.js +108 -0
- package/dist/keeper/relay.d.ts +88 -0
- package/dist/keeper/relay.js +420 -0
- package/dist/mcp/node.d.ts +6 -1
- package/dist/mcp/node.js +4 -4
- package/dist/permission-channel.d.ts +5 -0
- package/dist/permission-channel.js +16 -1
- package/dist/runner/host.js +74 -16
- package/dist/runner/options.d.ts +2 -0
- package/dist/runner/options.js +2 -0
- package/dist/runner/protocol.d.ts +12 -0
- package/dist/runner/protocol.js +24 -0
- package/dist/runner/serve.d.ts +5 -0
- package/dist/runner/serve.js +93 -3
- package/dist/serve-events.d.ts +2 -0
- package/dist/serve-events.js +1 -0
- package/dist/serve-questions.d.ts +26 -0
- package/dist/serve-questions.js +105 -0
- package/dist/serve-types.d.ts +1 -1
- package/dist/serve.d.ts +2 -2
- package/dist/serve.js +10 -1
- package/dist/server.js +1 -1
- package/dist/stdio-process.d.ts +38 -0
- package/dist/stdio-process.js +214 -0
- package/dist/stdio.d.ts +50 -7
- package/dist/stdio.js +192 -52
- package/dist/tool-bridge.d.ts +9 -0
- package/dist/tool-bridge.js +5 -1
- package/dist/transport-loopback.d.ts +2 -2
- package/dist/transport-loopback.js +1 -1
- package/dist/turn.d.ts +3 -2
- package/dist/turn.js +22 -2
- package/dist/update-filter.d.ts +38 -0
- package/dist/update-filter.js +193 -0
- package/package.json +3 -3
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keeper: a small detached process that runs one local agent for its
|
|
3
|
+
* host and keeps it running when the host goes away.
|
|
4
|
+
*
|
|
5
|
+
* It owns the agent's stdio and relays its ACP stream (`relay.ts`) to whichever
|
|
6
|
+
* host is attached over a private local socket — a Unix socket in a directory
|
|
7
|
+
* only its user can open, or a named pipe on Windows — that every connection
|
|
8
|
+
* must open with the keeper's token. It also owns the loopback port the agent
|
|
9
|
+
* reaches the host's tool bridge on, so the agent's MCP settings stay valid
|
|
10
|
+
* when a later host serves the bridge.
|
|
11
|
+
*
|
|
12
|
+
* When the attached host's connection drops without a `stop`, the keeper
|
|
13
|
+
* either stops the agent (`whenHostExits: 'stop'`) or keeps it working and
|
|
14
|
+
* holds what it sends until a host attaches again, for at most
|
|
15
|
+
* `detachedTimeoutMs`. It exits when the agent does.
|
|
16
|
+
*/
|
|
17
|
+
import { spawn } from 'node:child_process';
|
|
18
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
19
|
+
import { appendFileSync, rmSync } from 'node:fs';
|
|
20
|
+
import * as http from 'node:http';
|
|
21
|
+
import * as net from 'node:net';
|
|
22
|
+
import { frameworkCast } from '@noetic-tools/types';
|
|
23
|
+
import { stopProcessTree } from '../stdio-process.js';
|
|
24
|
+
import { isControlLine, KeeperHostLineSchema, LineSplitter } from './protocol.js';
|
|
25
|
+
import { AcpRelay } from './relay.js';
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region Constants
|
|
28
|
+
/** Request headers that belong to one hop. */
|
|
29
|
+
const HOP_HEADERS = new Set([
|
|
30
|
+
'host',
|
|
31
|
+
'connection',
|
|
32
|
+
'keep-alive',
|
|
33
|
+
'transfer-encoding',
|
|
34
|
+
]);
|
|
35
|
+
/** Connection errors that mean the request never reached a bridge. */
|
|
36
|
+
const UNDELIVERED = new Set([
|
|
37
|
+
'ECONNREFUSED',
|
|
38
|
+
'ENOENT',
|
|
39
|
+
'ECONNRESET',
|
|
40
|
+
]);
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region Helpers
|
|
43
|
+
function sameSecret(given, expected) {
|
|
44
|
+
const a = Buffer.from(given);
|
|
45
|
+
const b = Buffer.from(expected);
|
|
46
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
47
|
+
}
|
|
48
|
+
function errorCode(error) {
|
|
49
|
+
if (typeof error !== 'object' || error === null) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const code = Reflect.get(error, 'code');
|
|
53
|
+
return typeof code === 'string' ? code : undefined;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region Keeper
|
|
57
|
+
class Keeper {
|
|
58
|
+
config;
|
|
59
|
+
host;
|
|
60
|
+
agent;
|
|
61
|
+
whenHostExits = 'keep';
|
|
62
|
+
detachedTimer;
|
|
63
|
+
ports = new Map();
|
|
64
|
+
stopping;
|
|
65
|
+
/** The log stays after exit unless a host stopped the keeper on purpose. */
|
|
66
|
+
keepLog = true;
|
|
67
|
+
relay;
|
|
68
|
+
control;
|
|
69
|
+
constructor(config) {
|
|
70
|
+
this.config = config;
|
|
71
|
+
this.relay = new AcpRelay({
|
|
72
|
+
toHost: (line) => this.writeHost(line),
|
|
73
|
+
toAgent: (line) => {
|
|
74
|
+
this.agent?.stdin?.write(`${line}\n`);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
this.control = {
|
|
78
|
+
hello: () => undefined,
|
|
79
|
+
reach: (line) => this.reach(line),
|
|
80
|
+
launch: (line) => this.launch(line),
|
|
81
|
+
stop: () => {
|
|
82
|
+
// A host that closes its agent leaves nothing to look into later.
|
|
83
|
+
this.keepLog = false;
|
|
84
|
+
void this.stop();
|
|
85
|
+
},
|
|
86
|
+
release: () => {
|
|
87
|
+
this.whenHostExits = 'keep';
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
log(message) {
|
|
92
|
+
try {
|
|
93
|
+
appendFileSync(this.config.log, `${new Date().toISOString()} ${message}\n`);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Logging must never take the keeper down.
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//#region Hosts
|
|
100
|
+
/** A connection: it must say hello with the token before anything else. */
|
|
101
|
+
accept(socket) {
|
|
102
|
+
const splitter = new LineSplitter();
|
|
103
|
+
let greeted = false;
|
|
104
|
+
socket.on('error', () => undefined);
|
|
105
|
+
socket.on('data', (chunk) => {
|
|
106
|
+
for (const line of splitter.push(chunk)) {
|
|
107
|
+
if (!greeted) {
|
|
108
|
+
greeted = this.greet(socket, line);
|
|
109
|
+
if (!greeted) {
|
|
110
|
+
socket.destroy();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
this.fromHost(line);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
socket.on('close', () => {
|
|
119
|
+
if (greeted && this.host === socket) {
|
|
120
|
+
this.hostGone();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
greet(socket, line) {
|
|
125
|
+
const parsed = KeeperHostLineSchema.safeParse(this.json(line));
|
|
126
|
+
if (!parsed.success || parsed.data.keeper !== 'hello') {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
if (!sameSecret(parsed.data.token, this.config.token)) {
|
|
130
|
+
this.log('refused a connection with the wrong token');
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
// A host that reattaches while its old connection still looks open takes over.
|
|
134
|
+
const previous = this.host;
|
|
135
|
+
if (previous) {
|
|
136
|
+
this.host = undefined;
|
|
137
|
+
this.relay.detach();
|
|
138
|
+
previous.destroy();
|
|
139
|
+
}
|
|
140
|
+
this.host = socket;
|
|
141
|
+
this.whenHostExits = parsed.data.whenHostExits;
|
|
142
|
+
clearTimeout(this.detachedTimer);
|
|
143
|
+
this.send({
|
|
144
|
+
keeper: 'welcome',
|
|
145
|
+
agent: this.agent ? 'running' : 'none',
|
|
146
|
+
});
|
|
147
|
+
this.relay.attach();
|
|
148
|
+
this.log('host attached');
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
fromHost(line) {
|
|
152
|
+
if (!isControlLine(line)) {
|
|
153
|
+
this.relay.fromHost(line);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const parsed = KeeperHostLineSchema.safeParse(this.json(line));
|
|
157
|
+
if (!parsed.success) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const message = parsed.data;
|
|
161
|
+
const handler = frameworkCast(this.control[message.keeper]);
|
|
162
|
+
void Promise.resolve(handler(message)).catch((error) => {
|
|
163
|
+
if ('id' in message) {
|
|
164
|
+
this.send({
|
|
165
|
+
keeper: 'error',
|
|
166
|
+
id: message.id,
|
|
167
|
+
message: error instanceof Error ? error.message : String(error),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
hostGone() {
|
|
173
|
+
this.host = undefined;
|
|
174
|
+
this.relay.detach();
|
|
175
|
+
for (const port of this.ports.values()) {
|
|
176
|
+
port.target = undefined;
|
|
177
|
+
}
|
|
178
|
+
if (!this.agent || this.whenHostExits === 'stop') {
|
|
179
|
+
this.log('host gone; stopping');
|
|
180
|
+
void this.stop();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.log('host gone; keeping the agent');
|
|
184
|
+
if (this.config.detachedTimeoutMs > 0) {
|
|
185
|
+
this.detachedTimer = setTimeout(() => {
|
|
186
|
+
this.log('no host reattached in time; stopping');
|
|
187
|
+
void this.stop();
|
|
188
|
+
}, this.config.detachedTimeoutMs);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
send(line) {
|
|
192
|
+
this.writeHost(JSON.stringify(line));
|
|
193
|
+
}
|
|
194
|
+
writeHost(line) {
|
|
195
|
+
if (!this.host || this.host.destroyed) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
this.host.write(`${line}\n`);
|
|
199
|
+
}
|
|
200
|
+
json(line) {
|
|
201
|
+
try {
|
|
202
|
+
return JSON.parse(line);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region Agent
|
|
210
|
+
async launch(line) {
|
|
211
|
+
if (this.agent) {
|
|
212
|
+
throw new Error('The keeper already runs an agent.');
|
|
213
|
+
}
|
|
214
|
+
const agent = spawn(line.command, line.args, {
|
|
215
|
+
cwd: line.cwd,
|
|
216
|
+
env: line.env,
|
|
217
|
+
stdio: [
|
|
218
|
+
'pipe',
|
|
219
|
+
'pipe',
|
|
220
|
+
'pipe',
|
|
221
|
+
],
|
|
222
|
+
detached: process.platform !== 'win32',
|
|
223
|
+
windowsHide: true,
|
|
224
|
+
});
|
|
225
|
+
await new Promise((resolve, reject) => {
|
|
226
|
+
agent.once('spawn', resolve);
|
|
227
|
+
agent.once('error', reject);
|
|
228
|
+
});
|
|
229
|
+
agent.on('error', () => undefined);
|
|
230
|
+
agent.stdin?.on('error', () => undefined);
|
|
231
|
+
this.agent = agent;
|
|
232
|
+
const out = new LineSplitter();
|
|
233
|
+
agent.stdout?.on('data', (chunk) => {
|
|
234
|
+
for (const agentLine of out.push(chunk)) {
|
|
235
|
+
this.relay.fromAgent(agentLine);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
agent.stderr?.on('data', (chunk) => this.stderr(chunk));
|
|
239
|
+
agent.once('exit', (code, signal) => {
|
|
240
|
+
this.log(`agent exited (${code ?? signal})`);
|
|
241
|
+
this.agent = undefined;
|
|
242
|
+
void this.stop();
|
|
243
|
+
});
|
|
244
|
+
this.send({
|
|
245
|
+
keeper: 'launched',
|
|
246
|
+
id: line.id,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
stderr(chunk) {
|
|
250
|
+
if (this.host) {
|
|
251
|
+
this.send({
|
|
252
|
+
keeper: 'stderr',
|
|
253
|
+
data: chunk.toString('utf8'),
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
this.log(`agent stderr: ${chunk.toString('utf8').trimEnd()}`);
|
|
258
|
+
}
|
|
259
|
+
/** Stop the agent's tree, release everything, and exit. Shared by every caller. */
|
|
260
|
+
stop() {
|
|
261
|
+
this.stopping ??= this.shutdown();
|
|
262
|
+
return this.stopping;
|
|
263
|
+
}
|
|
264
|
+
async shutdown() {
|
|
265
|
+
clearTimeout(this.detachedTimer);
|
|
266
|
+
const agent = this.agent;
|
|
267
|
+
this.agent = undefined;
|
|
268
|
+
if (agent) {
|
|
269
|
+
await stopProcessTree(agent).catch((error) => {
|
|
270
|
+
this.log(`could not stop the agent: ${String(error)}`);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
for (const port of this.ports.values()) {
|
|
274
|
+
for (const held of port.held) {
|
|
275
|
+
held.res.writeHead(503).end();
|
|
276
|
+
}
|
|
277
|
+
port.server.close();
|
|
278
|
+
port.server.closeAllConnections();
|
|
279
|
+
}
|
|
280
|
+
this.host?.end();
|
|
281
|
+
if (!this.keepLog) {
|
|
282
|
+
rmSync(this.config.log, {
|
|
283
|
+
force: true,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
this.onStopped();
|
|
287
|
+
}
|
|
288
|
+
/** Set by {@link runKeeper}: release the socket and exit. */
|
|
289
|
+
onStopped = () => undefined;
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region Bridge port
|
|
292
|
+
async reach(line) {
|
|
293
|
+
const existing = this.ports.get(line.path);
|
|
294
|
+
const port = existing ?? (await this.openPort(line.path));
|
|
295
|
+
port.target = new URL(line.target);
|
|
296
|
+
this.send({
|
|
297
|
+
keeper: 'reached',
|
|
298
|
+
id: line.id,
|
|
299
|
+
url: port.url,
|
|
300
|
+
});
|
|
301
|
+
const held = port.held.splice(0);
|
|
302
|
+
for (const request of held) {
|
|
303
|
+
this.forward(port, request);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
async openPort(path) {
|
|
307
|
+
const held = [];
|
|
308
|
+
let port;
|
|
309
|
+
const server = http.createServer((req, res) => {
|
|
310
|
+
const chunks = [];
|
|
311
|
+
req.on('data', (chunk) => chunks.push(chunk));
|
|
312
|
+
req.on('end', () => {
|
|
313
|
+
if (!port) {
|
|
314
|
+
res.writeHead(503).end();
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const request = {
|
|
318
|
+
req,
|
|
319
|
+
res,
|
|
320
|
+
body: Buffer.concat(chunks),
|
|
321
|
+
};
|
|
322
|
+
if (!port.target) {
|
|
323
|
+
port.held.push(request);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
this.forward(port, request);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
await new Promise((resolve, reject) => {
|
|
330
|
+
server.once('error', reject);
|
|
331
|
+
server.listen(0, '127.0.0.1', () => {
|
|
332
|
+
server.off('error', reject);
|
|
333
|
+
resolve();
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
const address = server.address();
|
|
337
|
+
if (address === null || typeof address === 'string') {
|
|
338
|
+
throw new Error('The bridge port did not bind.');
|
|
339
|
+
}
|
|
340
|
+
port = {
|
|
341
|
+
server,
|
|
342
|
+
url: `http://127.0.0.1:${address.port}${path}`,
|
|
343
|
+
held,
|
|
344
|
+
};
|
|
345
|
+
this.ports.set(path, port);
|
|
346
|
+
return port;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Send one request to the host's bridge. A request that never reached it —
|
|
350
|
+
* the host went away — waits for the next host's bridge instead of failing.
|
|
351
|
+
*/
|
|
352
|
+
forward(port, request) {
|
|
353
|
+
const target = port.target;
|
|
354
|
+
if (!target) {
|
|
355
|
+
port.held.push(request);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const headers = {};
|
|
359
|
+
for (const [name, value] of Object.entries(request.req.headers)) {
|
|
360
|
+
if (value !== undefined && !HOP_HEADERS.has(name)) {
|
|
361
|
+
headers[name] = value;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const path = new URL(request.req.url ?? '/', target.origin);
|
|
365
|
+
const outgoing = http.request(new URL(path.pathname + path.search, target.origin), {
|
|
366
|
+
method: request.req.method,
|
|
367
|
+
headers,
|
|
368
|
+
}, (response) => {
|
|
369
|
+
request.res.writeHead(response.statusCode ?? 502, response.headers);
|
|
370
|
+
response.pipe(request.res);
|
|
371
|
+
});
|
|
372
|
+
outgoing.on('error', (error) => {
|
|
373
|
+
if (UNDELIVERED.has(errorCode(error) ?? '') && !request.res.headersSent) {
|
|
374
|
+
if (port.target === target) {
|
|
375
|
+
port.target = undefined;
|
|
376
|
+
}
|
|
377
|
+
port.held.push(request);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (!request.res.headersSent) {
|
|
381
|
+
request.res.writeHead(502);
|
|
382
|
+
}
|
|
383
|
+
request.res.end();
|
|
384
|
+
});
|
|
385
|
+
outgoing.end(request.body);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region Public API
|
|
390
|
+
/**
|
|
391
|
+
* Run a keeper until its agent exits or it is stopped. Resolves once it
|
|
392
|
+
* listens on `config.address`.
|
|
393
|
+
*/
|
|
394
|
+
export async function runKeeper(config) {
|
|
395
|
+
const keeper = new Keeper(config);
|
|
396
|
+
const server = net.createServer((socket) => keeper.accept(socket));
|
|
397
|
+
if (process.platform !== 'win32') {
|
|
398
|
+
rmSync(config.address, {
|
|
399
|
+
force: true,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
await new Promise((resolve, reject) => {
|
|
403
|
+
server.once('error', reject);
|
|
404
|
+
server.listen(config.address, () => {
|
|
405
|
+
server.off('error', reject);
|
|
406
|
+
resolve();
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
const stopped = new Promise((resolve) => {
|
|
410
|
+
keeper.onStopped = () => {
|
|
411
|
+
server.close();
|
|
412
|
+
if (process.platform !== 'win32') {
|
|
413
|
+
rmSync(config.address, {
|
|
414
|
+
force: true,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
resolve();
|
|
418
|
+
};
|
|
419
|
+
});
|
|
420
|
+
keeper.log(`listening on ${config.address}`);
|
|
421
|
+
return {
|
|
422
|
+
stopped,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
//#endregion
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keeper's control lines. They share the host's socket with the agent's
|
|
3
|
+
* ACP stream; a control line is a JSON object with a `keeper` key, which no
|
|
4
|
+
* JSON-RPC message has.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/** @public Host → keeper. */
|
|
8
|
+
export declare const KeeperHostLineSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
9
|
+
keeper: z.ZodLiteral<"hello">;
|
|
10
|
+
token: z.ZodString;
|
|
11
|
+
whenHostExits: z.ZodEnum<{
|
|
12
|
+
stop: "stop";
|
|
13
|
+
keep: "keep";
|
|
14
|
+
}>;
|
|
15
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
16
|
+
keeper: z.ZodLiteral<"reach">;
|
|
17
|
+
id: z.ZodString;
|
|
18
|
+
path: z.ZodString;
|
|
19
|
+
target: z.ZodString;
|
|
20
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
21
|
+
keeper: z.ZodLiteral<"launch">;
|
|
22
|
+
id: z.ZodString;
|
|
23
|
+
command: z.ZodString;
|
|
24
|
+
args: z.ZodArray<z.ZodString>;
|
|
25
|
+
env: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
26
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
27
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
28
|
+
keeper: z.ZodLiteral<"stop">;
|
|
29
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
30
|
+
keeper: z.ZodLiteral<"release">;
|
|
31
|
+
}, z.core.$strip>], "keeper">;
|
|
32
|
+
/** @public Keeper → host. */
|
|
33
|
+
export declare const KeeperLineSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
34
|
+
keeper: z.ZodLiteral<"welcome">;
|
|
35
|
+
agent: z.ZodEnum<{
|
|
36
|
+
running: "running";
|
|
37
|
+
none: "none";
|
|
38
|
+
}>;
|
|
39
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
40
|
+
keeper: z.ZodLiteral<"reached">;
|
|
41
|
+
id: z.ZodString;
|
|
42
|
+
url: z.ZodString;
|
|
43
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
44
|
+
keeper: z.ZodLiteral<"launched">;
|
|
45
|
+
id: z.ZodString;
|
|
46
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
47
|
+
keeper: z.ZodLiteral<"error">;
|
|
48
|
+
id: z.ZodString;
|
|
49
|
+
message: z.ZodString;
|
|
50
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
51
|
+
keeper: z.ZodLiteral<"stderr">;
|
|
52
|
+
data: z.ZodString;
|
|
53
|
+
}, z.core.$strip>], "keeper">;
|
|
54
|
+
/** @public What the keeper reads from its stdin when it starts. */
|
|
55
|
+
export declare const KeeperConfigSchema: z.ZodObject<{
|
|
56
|
+
address: z.ZodString;
|
|
57
|
+
token: z.ZodString;
|
|
58
|
+
log: z.ZodString;
|
|
59
|
+
detachedTimeoutMs: z.ZodNumber;
|
|
60
|
+
}, z.core.$strip>;
|
|
61
|
+
/** @public */
|
|
62
|
+
export type KeeperHostLine = z.infer<typeof KeeperHostLineSchema>;
|
|
63
|
+
/** @public */
|
|
64
|
+
export type KeeperLine = z.infer<typeof KeeperLineSchema>;
|
|
65
|
+
/** @public */
|
|
66
|
+
export type KeeperConfig = z.infer<typeof KeeperConfigSchema>;
|
|
67
|
+
/** Whether a line is a keeper control line rather than a JSON-RPC message. */
|
|
68
|
+
export declare function isControlLine(line: string): boolean;
|
|
69
|
+
/** Split a byte stream's text into lines, keeping the unterminated tail. */
|
|
70
|
+
export declare class LineSplitter {
|
|
71
|
+
private carry;
|
|
72
|
+
private readonly decoder;
|
|
73
|
+
push(chunk: Uint8Array): string[];
|
|
74
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keeper's control lines. They share the host's socket with the agent's
|
|
3
|
+
* ACP stream; a control line is a JSON object with a `keeper` key, which no
|
|
4
|
+
* JSON-RPC message has.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
//#region Schemas
|
|
8
|
+
/** @public Host → keeper. */
|
|
9
|
+
export const KeeperHostLineSchema = z.discriminatedUnion('keeper', [
|
|
10
|
+
/** The first line on every connection. */
|
|
11
|
+
z.object({
|
|
12
|
+
keeper: z.literal('hello'),
|
|
13
|
+
token: z.string(),
|
|
14
|
+
whenHostExits: z.enum([
|
|
15
|
+
'keep',
|
|
16
|
+
'stop',
|
|
17
|
+
]),
|
|
18
|
+
}),
|
|
19
|
+
/**
|
|
20
|
+
* Serve the bridge at `target` on a loopback port the keeper owns. The same
|
|
21
|
+
* `path` again keeps the port and points it at the new target.
|
|
22
|
+
*/
|
|
23
|
+
z.object({
|
|
24
|
+
keeper: z.literal('reach'),
|
|
25
|
+
id: z.string(),
|
|
26
|
+
path: z.string().startsWith('/'),
|
|
27
|
+
target: z.string().url(),
|
|
28
|
+
}),
|
|
29
|
+
/** Start the agent. */
|
|
30
|
+
z.object({
|
|
31
|
+
keeper: z.literal('launch'),
|
|
32
|
+
id: z.string(),
|
|
33
|
+
command: z.string().min(1),
|
|
34
|
+
args: z.array(z.string()),
|
|
35
|
+
env: z.record(z.string(), z.string()),
|
|
36
|
+
cwd: z.string().optional(),
|
|
37
|
+
}),
|
|
38
|
+
/** Stop the agent's process tree and exit. */
|
|
39
|
+
z.object({
|
|
40
|
+
keeper: z.literal('stop'),
|
|
41
|
+
}),
|
|
42
|
+
/**
|
|
43
|
+
* This host lets go without adopting the agent: whatever exit policy its
|
|
44
|
+
* hello set, the agent keeps running for a later host.
|
|
45
|
+
*/
|
|
46
|
+
z.object({
|
|
47
|
+
keeper: z.literal('release'),
|
|
48
|
+
}),
|
|
49
|
+
]);
|
|
50
|
+
/** @public Keeper → host. */
|
|
51
|
+
export const KeeperLineSchema = z.discriminatedUnion('keeper', [
|
|
52
|
+
z.object({
|
|
53
|
+
keeper: z.literal('welcome'),
|
|
54
|
+
agent: z.enum([
|
|
55
|
+
'running',
|
|
56
|
+
'none',
|
|
57
|
+
]),
|
|
58
|
+
}),
|
|
59
|
+
z.object({
|
|
60
|
+
keeper: z.literal('reached'),
|
|
61
|
+
id: z.string(),
|
|
62
|
+
url: z.string().url(),
|
|
63
|
+
}),
|
|
64
|
+
z.object({
|
|
65
|
+
keeper: z.literal('launched'),
|
|
66
|
+
id: z.string(),
|
|
67
|
+
}),
|
|
68
|
+
z.object({
|
|
69
|
+
keeper: z.literal('error'),
|
|
70
|
+
id: z.string(),
|
|
71
|
+
message: z.string(),
|
|
72
|
+
}),
|
|
73
|
+
/** The agent's stderr. */
|
|
74
|
+
z.object({
|
|
75
|
+
keeper: z.literal('stderr'),
|
|
76
|
+
data: z.string(),
|
|
77
|
+
}),
|
|
78
|
+
]);
|
|
79
|
+
/** @public What the keeper reads from its stdin when it starts. */
|
|
80
|
+
export const KeeperConfigSchema = z.object({
|
|
81
|
+
/** Unix socket path or Windows named pipe the keeper listens on. */
|
|
82
|
+
address: z.string(),
|
|
83
|
+
token: z.string().min(16),
|
|
84
|
+
/** Where the keeper logs, and the agent's stderr goes while no host is attached. */
|
|
85
|
+
log: z.string(),
|
|
86
|
+
/** How long the agent may run with no host attached; 0 = no limit. */
|
|
87
|
+
detachedTimeoutMs: z.number().int().min(0),
|
|
88
|
+
});
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region Helpers
|
|
91
|
+
/** Whether a line is a keeper control line rather than a JSON-RPC message. */
|
|
92
|
+
export function isControlLine(line) {
|
|
93
|
+
return line.startsWith('{"keeper":');
|
|
94
|
+
}
|
|
95
|
+
/** Split a byte stream's text into lines, keeping the unterminated tail. */
|
|
96
|
+
export class LineSplitter {
|
|
97
|
+
carry = '';
|
|
98
|
+
decoder = new TextDecoder();
|
|
99
|
+
push(chunk) {
|
|
100
|
+
this.carry += this.decoder.decode(chunk, {
|
|
101
|
+
stream: true,
|
|
102
|
+
});
|
|
103
|
+
const lines = this.carry.split('\n');
|
|
104
|
+
this.carry = lines.pop() ?? '';
|
|
105
|
+
return lines.filter((line) => line.length > 0);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ACP relay inside an agent's keeper: it sits between the agent's stdio
|
|
3
|
+
* and whichever host is attached, so the agent can outlive a host and a later
|
|
4
|
+
* host can take it over.
|
|
5
|
+
*
|
|
6
|
+
* Pure: it consumes JSON-RPC lines from each side and emits lines through the
|
|
7
|
+
* two senders it is given. The keeper process (`main.ts`) owns the I/O.
|
|
8
|
+
*
|
|
9
|
+
* What it does:
|
|
10
|
+
* - Rewrites every host request id to its own, so a response is routed to the
|
|
11
|
+
* host attachment that asked; a later host's ids never collide with
|
|
12
|
+
* requests an earlier host left in flight.
|
|
13
|
+
* - Answers `initialize` from its cache after the first, so a reattaching host
|
|
14
|
+
* negotiates without re-initializing the agent.
|
|
15
|
+
* - Keeps, per session, the modes and config options the agent advertised,
|
|
16
|
+
* the updates of the turn in flight, updates sent while no host held the
|
|
17
|
+
* session, the agent's unanswered requests, and a turn result no host was
|
|
18
|
+
* attached to receive.
|
|
19
|
+
* - Serves two keeper requests a reattaching host makes:
|
|
20
|
+
* `_noetic/keeper/session` (what it knows of a session) and
|
|
21
|
+
* `_noetic/keeper/turn` (hand the session to this host: send the turn's
|
|
22
|
+
* updates and the agent's waiting requests again, then the turn's result
|
|
23
|
+
* once there is one).
|
|
24
|
+
*/
|
|
25
|
+
/** What {@link AcpRelay} sends. */
|
|
26
|
+
export interface RelaySenders {
|
|
27
|
+
toHost(line: string): void;
|
|
28
|
+
toAgent(line: string): void;
|
|
29
|
+
}
|
|
30
|
+
/** The answer to `_noetic/keeper/session`. */
|
|
31
|
+
export interface KeeperSessionInfo {
|
|
32
|
+
known: boolean;
|
|
33
|
+
modes?: unknown;
|
|
34
|
+
configOptions?: unknown;
|
|
35
|
+
turn: 'running' | 'finished' | 'idle';
|
|
36
|
+
}
|
|
37
|
+
/** @public The keeper's own requests, which never reach the agent. */
|
|
38
|
+
export declare const KEEPER_METHODS: {
|
|
39
|
+
readonly session: "_noetic/keeper/session";
|
|
40
|
+
readonly turn: "_noetic/keeper/turn";
|
|
41
|
+
};
|
|
42
|
+
/** @public See the module comment. */
|
|
43
|
+
export declare class AcpRelay {
|
|
44
|
+
private readonly send;
|
|
45
|
+
/** Bumped on every detach, so what an earlier attachment asked has no host. */
|
|
46
|
+
private epoch;
|
|
47
|
+
private attached;
|
|
48
|
+
private nextId;
|
|
49
|
+
private readonly asked;
|
|
50
|
+
private readonly sessions;
|
|
51
|
+
/** The agent's requests that name no session. */
|
|
52
|
+
private readonly unscoped;
|
|
53
|
+
private initialized?;
|
|
54
|
+
constructor(send: RelaySenders);
|
|
55
|
+
/** A host attached. */
|
|
56
|
+
attach(): void;
|
|
57
|
+
/** The host went away; everything it held is kept for the next one. */
|
|
58
|
+
detach(): void;
|
|
59
|
+
get isAttached(): boolean;
|
|
60
|
+
fromHost(raw: string): void;
|
|
61
|
+
private hostRequest;
|
|
62
|
+
private sessionInfo;
|
|
63
|
+
/**
|
|
64
|
+
* Hand a session to this host: its turn's updates and the agent's waiting
|
|
65
|
+
* requests again, then the turn's result — now, or when the agent answers.
|
|
66
|
+
*/
|
|
67
|
+
private resumeTurn;
|
|
68
|
+
/**
|
|
69
|
+
* The current attachment takes the session: what it missed (the whole turn
|
|
70
|
+
* when resuming one, else the backlog) and the agent's waiting requests.
|
|
71
|
+
*/
|
|
72
|
+
private claim;
|
|
73
|
+
private forget;
|
|
74
|
+
private answerHost;
|
|
75
|
+
fromAgent(raw: string): void;
|
|
76
|
+
private agentResponse;
|
|
77
|
+
/** Keep what an answer says about the agent and its sessions. */
|
|
78
|
+
private remember;
|
|
79
|
+
private turnEnded;
|
|
80
|
+
private agentRequest;
|
|
81
|
+
private agentNotification;
|
|
82
|
+
/** Follow the mode and config option updates a session reports. */
|
|
83
|
+
private track;
|
|
84
|
+
private session;
|
|
85
|
+
/** Whether the attached host holds the session, so it gets it live. */
|
|
86
|
+
private holds;
|
|
87
|
+
private isLive;
|
|
88
|
+
}
|