@noetic-tools/acp 1.4.0 → 2.0.0-next.2
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/bin.js +19 -2
- package/dist/client-adapters.d.ts +2 -2
- package/dist/client.d.ts +2 -2
- package/dist/client.js +2 -1
- package/dist/connection.d.ts +6 -2
- package/dist/connection.js +213 -31
- package/dist/define.js +92 -4
- package/dist/items.d.ts +1 -1
- package/dist/keeper/client.d.ts +84 -0
- package/dist/keeper/client.js +436 -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 +422 -0
- package/dist/keeper/protocol.d.ts +72 -0
- package/dist/keeper/protocol.js +101 -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 +7 -2
- package/dist/permission-channel.js +39 -13
- package/dist/permissions.d.ts +6 -0
- package/dist/permissions.js +7 -0
- 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-permissions.d.ts +8 -2
- package/dist/serve-permissions.js +26 -5
- package/dist/serve-types.d.ts +7 -6
- package/dist/serve.d.ts +2 -2
- package/dist/serve.js +1 -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 +28 -6
- package/dist/stdio.js +145 -51
- 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 +22 -0
- package/dist/update-filter.js +119 -0
- package/package.json +3 -3
|
@@ -0,0 +1,420 @@
|
|
|
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
|
+
//#endregion
|
|
26
|
+
//#region Constants
|
|
27
|
+
/** @public The keeper's own requests, which never reach the agent. */
|
|
28
|
+
export const KEEPER_METHODS = {
|
|
29
|
+
session: '_noetic/keeper/session',
|
|
30
|
+
turn: '_noetic/keeper/turn',
|
|
31
|
+
};
|
|
32
|
+
/** Updates kept per session while no host holds it, beyond which the oldest go. */
|
|
33
|
+
const MAX_BACKLOG = 1e4;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region Helpers
|
|
36
|
+
function isRecord(value) {
|
|
37
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
38
|
+
}
|
|
39
|
+
function isId(value) {
|
|
40
|
+
return typeof value === 'string' || typeof value === 'number';
|
|
41
|
+
}
|
|
42
|
+
function parse(line) {
|
|
43
|
+
try {
|
|
44
|
+
const value = JSON.parse(line);
|
|
45
|
+
return isRecord(value) ? value : undefined;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function sessionIdOf(params) {
|
|
52
|
+
return isRecord(params) && typeof params.sessionId === 'string' ? params.sessionId : undefined;
|
|
53
|
+
}
|
|
54
|
+
function line(message) {
|
|
55
|
+
return JSON.stringify({
|
|
56
|
+
jsonrpc: '2.0',
|
|
57
|
+
...message,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region Relay
|
|
62
|
+
/** @public See the module comment. */
|
|
63
|
+
export class AcpRelay {
|
|
64
|
+
send;
|
|
65
|
+
/** Bumped on every detach, so what an earlier attachment asked has no host. */
|
|
66
|
+
epoch = 0;
|
|
67
|
+
attached = false;
|
|
68
|
+
nextId = 1;
|
|
69
|
+
asked = new Map();
|
|
70
|
+
sessions = new Map();
|
|
71
|
+
/** The agent's requests that name no session. */
|
|
72
|
+
unscoped = new Map();
|
|
73
|
+
initialized;
|
|
74
|
+
constructor(send) {
|
|
75
|
+
this.send = send;
|
|
76
|
+
}
|
|
77
|
+
/** A host attached. */
|
|
78
|
+
attach() {
|
|
79
|
+
this.attached = true;
|
|
80
|
+
for (const pending of this.unscoped.values()) {
|
|
81
|
+
this.send.toHost(pending);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** The host went away; everything it held is kept for the next one. */
|
|
85
|
+
detach() {
|
|
86
|
+
this.attached = false;
|
|
87
|
+
this.epoch += 1;
|
|
88
|
+
}
|
|
89
|
+
get isAttached() {
|
|
90
|
+
return this.attached;
|
|
91
|
+
}
|
|
92
|
+
//#region From the host
|
|
93
|
+
fromHost(raw) {
|
|
94
|
+
const message = parse(raw);
|
|
95
|
+
if (!message) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const method = typeof message.method === 'string' ? message.method : undefined;
|
|
99
|
+
const id = isId(message.id) ? message.id : undefined;
|
|
100
|
+
if (method !== undefined && id !== undefined) {
|
|
101
|
+
this.hostRequest(method, id, isRecord(message.params) ? message.params : {}, message);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (method === undefined && id !== undefined) {
|
|
105
|
+
// The host's answer to one of the agent's requests.
|
|
106
|
+
this.forget(id);
|
|
107
|
+
this.send.toAgent(raw);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.send.toAgent(raw);
|
|
111
|
+
}
|
|
112
|
+
hostRequest(method, hostId, params, message) {
|
|
113
|
+
if (method === KEEPER_METHODS.session) {
|
|
114
|
+
this.answerHost(hostId, {
|
|
115
|
+
result: this.sessionInfo(sessionIdOf(params)),
|
|
116
|
+
});
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (method === KEEPER_METHODS.turn) {
|
|
120
|
+
this.resumeTurn(sessionIdOf(params), hostId);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (method === 'initialize' && this.initialized !== undefined) {
|
|
124
|
+
this.answerHost(hostId, {
|
|
125
|
+
result: this.initialized,
|
|
126
|
+
});
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const keeperId = this.nextId;
|
|
130
|
+
this.nextId += 1;
|
|
131
|
+
this.asked.set(keeperId, {
|
|
132
|
+
epoch: this.epoch,
|
|
133
|
+
hostId,
|
|
134
|
+
method,
|
|
135
|
+
params,
|
|
136
|
+
});
|
|
137
|
+
const sessionId = sessionIdOf(params);
|
|
138
|
+
if (sessionId && (method === 'session/prompt' || method === 'session/load')) {
|
|
139
|
+
const session = this.session(sessionId);
|
|
140
|
+
this.claim(session, false);
|
|
141
|
+
if (method === 'session/prompt') {
|
|
142
|
+
session.turn = keeperId;
|
|
143
|
+
session.finished = undefined;
|
|
144
|
+
session.journal = [];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
this.send.toAgent(JSON.stringify({
|
|
148
|
+
...message,
|
|
149
|
+
id: keeperId,
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
sessionInfo(sessionId) {
|
|
153
|
+
const session = sessionId ? this.sessions.get(sessionId) : undefined;
|
|
154
|
+
if (!session) {
|
|
155
|
+
return {
|
|
156
|
+
known: false,
|
|
157
|
+
turn: 'idle',
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
known: true,
|
|
162
|
+
modes: session.modes,
|
|
163
|
+
configOptions: session.configOptions,
|
|
164
|
+
turn: session.turn !== undefined ? 'running' : session.finished ? 'finished' : 'idle',
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Hand a session to this host: its turn's updates and the agent's waiting
|
|
169
|
+
* requests again, then the turn's result — now, or when the agent answers.
|
|
170
|
+
*/
|
|
171
|
+
resumeTurn(sessionId, hostId) {
|
|
172
|
+
const session = sessionId ? this.sessions.get(sessionId) : undefined;
|
|
173
|
+
if (!session) {
|
|
174
|
+
this.answerHost(hostId, {
|
|
175
|
+
error: {
|
|
176
|
+
code: -32602,
|
|
177
|
+
message: `The keeper has no session ${String(sessionId)}.`,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.claim(session, true);
|
|
183
|
+
if (session.finished) {
|
|
184
|
+
const finished = session.finished;
|
|
185
|
+
session.finished = undefined;
|
|
186
|
+
this.answerHost(hostId, turnAnswer(finished));
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (session.turn === undefined) {
|
|
190
|
+
this.answerHost(hostId, {
|
|
191
|
+
result: {
|
|
192
|
+
state: 'idle',
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
session.waiter = {
|
|
198
|
+
epoch: this.epoch,
|
|
199
|
+
hostId,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The current attachment takes the session: what it missed (the whole turn
|
|
204
|
+
* when resuming one, else the backlog) and the agent's waiting requests.
|
|
205
|
+
*/
|
|
206
|
+
claim(session, replayTurn) {
|
|
207
|
+
if (session.claimedBy === this.epoch) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
session.claimedBy = this.epoch;
|
|
211
|
+
const missed = replayTurn ? session.journal : session.backlog;
|
|
212
|
+
for (const update of missed) {
|
|
213
|
+
this.send.toHost(update);
|
|
214
|
+
}
|
|
215
|
+
session.backlog = [];
|
|
216
|
+
for (const pending of session.pending.values()) {
|
|
217
|
+
this.send.toHost(pending);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
forget(agentId) {
|
|
221
|
+
if (this.unscoped.delete(agentId)) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
for (const session of this.sessions.values()) {
|
|
225
|
+
if (session.pending.delete(agentId)) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
answerHost(hostId, answer) {
|
|
231
|
+
this.send.toHost(line({
|
|
232
|
+
id: hostId,
|
|
233
|
+
...answer,
|
|
234
|
+
}));
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region From the agent
|
|
238
|
+
fromAgent(raw) {
|
|
239
|
+
const message = parse(raw);
|
|
240
|
+
if (!message) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const method = typeof message.method === 'string' ? message.method : undefined;
|
|
244
|
+
const id = isId(message.id) ? message.id : undefined;
|
|
245
|
+
if (method === undefined && typeof id === 'number') {
|
|
246
|
+
this.agentResponse(id, message);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (method !== undefined && id !== undefined) {
|
|
250
|
+
this.agentRequest(id, message.params, raw);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (method !== undefined) {
|
|
254
|
+
this.agentNotification(method, message.params, raw);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
agentResponse(keeperId, message) {
|
|
258
|
+
const asked = this.asked.get(keeperId);
|
|
259
|
+
if (!asked) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this.asked.delete(keeperId);
|
|
263
|
+
const answer = 'error' in message
|
|
264
|
+
? {
|
|
265
|
+
error: message.error,
|
|
266
|
+
}
|
|
267
|
+
: {
|
|
268
|
+
result: message.result,
|
|
269
|
+
};
|
|
270
|
+
this.remember(asked, answer);
|
|
271
|
+
if (asked.method === 'session/prompt') {
|
|
272
|
+
this.turnEnded(asked, answer);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (this.isLive(asked.epoch)) {
|
|
276
|
+
this.answerHost(asked.hostId, answer);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/** Keep what an answer says about the agent and its sessions. */
|
|
280
|
+
remember(asked, answer) {
|
|
281
|
+
if (!('result' in answer) || !isRecord(answer.result)) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const result = answer.result;
|
|
285
|
+
if (asked.method === 'initialize') {
|
|
286
|
+
this.initialized = result;
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const sessionId = asked.method === 'session/new' ? sessionIdOf(result) : sessionIdOf(asked.params);
|
|
290
|
+
if (!sessionId) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const session = this.session(sessionId);
|
|
294
|
+
if (asked.method === 'session/new') {
|
|
295
|
+
session.claimedBy = asked.epoch;
|
|
296
|
+
}
|
|
297
|
+
if ('modes' in result && result.modes !== null) {
|
|
298
|
+
session.modes = result.modes;
|
|
299
|
+
}
|
|
300
|
+
if ('configOptions' in result && result.configOptions !== null) {
|
|
301
|
+
session.configOptions = result.configOptions;
|
|
302
|
+
}
|
|
303
|
+
if (asked.method === 'session/set_mode' && isRecord(session.modes)) {
|
|
304
|
+
session.modes = {
|
|
305
|
+
...session.modes,
|
|
306
|
+
currentModeId: asked.params.modeId,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
turnEnded(asked, answer) {
|
|
311
|
+
const sessionId = sessionIdOf(asked.params);
|
|
312
|
+
const session = sessionId ? this.sessions.get(sessionId) : undefined;
|
|
313
|
+
if (session) {
|
|
314
|
+
session.turn = undefined;
|
|
315
|
+
}
|
|
316
|
+
if (this.isLive(asked.epoch)) {
|
|
317
|
+
this.answerHost(asked.hostId, answer);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const waiter = session?.waiter;
|
|
321
|
+
if (session && waiter && this.isLive(waiter.epoch)) {
|
|
322
|
+
session.waiter = undefined;
|
|
323
|
+
this.answerHost(waiter.hostId, turnAnswer(answer));
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (session) {
|
|
327
|
+
session.finished = answer;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
agentRequest(agentId, params, raw) {
|
|
331
|
+
const sessionId = sessionIdOf(params);
|
|
332
|
+
if (!sessionId) {
|
|
333
|
+
this.unscoped.set(agentId, raw);
|
|
334
|
+
if (this.attached) {
|
|
335
|
+
this.send.toHost(raw);
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const session = this.session(sessionId);
|
|
340
|
+
session.pending.set(agentId, raw);
|
|
341
|
+
if (this.holds(session)) {
|
|
342
|
+
this.send.toHost(raw);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
agentNotification(method, params, raw) {
|
|
346
|
+
const sessionId = sessionIdOf(params);
|
|
347
|
+
if (method !== 'session/update' || !sessionId) {
|
|
348
|
+
if (this.attached) {
|
|
349
|
+
this.send.toHost(raw);
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const session = this.session(sessionId);
|
|
354
|
+
this.track(session, params);
|
|
355
|
+
if (session.turn !== undefined) {
|
|
356
|
+
session.journal.push(raw);
|
|
357
|
+
}
|
|
358
|
+
if (this.holds(session)) {
|
|
359
|
+
this.send.toHost(raw);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (session.turn === undefined) {
|
|
363
|
+
session.backlog.push(raw);
|
|
364
|
+
if (session.backlog.length > MAX_BACKLOG) {
|
|
365
|
+
session.backlog.shift();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
/** Follow the mode and config option updates a session reports. */
|
|
370
|
+
track(session, params) {
|
|
371
|
+
const update = isRecord(params) && isRecord(params.update) ? params.update : undefined;
|
|
372
|
+
if (!update) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (update.sessionUpdate === 'current_mode_update' && isRecord(session.modes)) {
|
|
376
|
+
session.modes = {
|
|
377
|
+
...session.modes,
|
|
378
|
+
currentModeId: update.currentModeId,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
if (update.sessionUpdate === 'config_option_update') {
|
|
382
|
+
session.configOptions = update.configOptions;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region State
|
|
387
|
+
session(sessionId) {
|
|
388
|
+
let session = this.sessions.get(sessionId);
|
|
389
|
+
if (!session) {
|
|
390
|
+
session = {
|
|
391
|
+
claimedBy: -1,
|
|
392
|
+
journal: [],
|
|
393
|
+
backlog: [],
|
|
394
|
+
pending: new Map(),
|
|
395
|
+
};
|
|
396
|
+
this.sessions.set(sessionId, session);
|
|
397
|
+
}
|
|
398
|
+
return session;
|
|
399
|
+
}
|
|
400
|
+
/** Whether the attached host holds the session, so it gets it live. */
|
|
401
|
+
holds(session) {
|
|
402
|
+
return this.attached && session.claimedBy === this.epoch;
|
|
403
|
+
}
|
|
404
|
+
isLive(epoch) {
|
|
405
|
+
return this.attached && epoch === this.epoch;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/** A finished turn, as `_noetic/keeper/turn` answers it. */
|
|
409
|
+
function turnAnswer(answer) {
|
|
410
|
+
if ('error' in answer) {
|
|
411
|
+
return answer;
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
result: {
|
|
415
|
+
state: 'finished',
|
|
416
|
+
response: answer.result,
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
//#endregion
|
package/dist/mcp/node.d.ts
CHANGED
|
@@ -17,8 +17,13 @@ export interface HttpToolServer {
|
|
|
17
17
|
readonly token: string;
|
|
18
18
|
close(): Promise<void>;
|
|
19
19
|
}
|
|
20
|
+
/** The path and token a bridge serves under, kept so a later host can serve the same. */
|
|
21
|
+
export interface BridgeCredentials {
|
|
22
|
+
readonly path: string;
|
|
23
|
+
readonly token: string;
|
|
24
|
+
}
|
|
20
25
|
/**
|
|
21
26
|
* Start a tool server on `127.0.0.1` at a random port and path. Each MCP
|
|
22
27
|
* session the agent opens gets its own protocol server over the same host.
|
|
23
28
|
*/
|
|
24
|
-
export declare function serveToolsOverHttp(getHost: () => AcpToolHost | undefined): Promise<HttpToolServer>;
|
|
29
|
+
export declare function serveToolsOverHttp(getHost: () => AcpToolHost | undefined, reuse?: BridgeCredentials): Promise<HttpToolServer>;
|
package/dist/mcp/node.js
CHANGED
|
@@ -44,15 +44,15 @@ function refusal(req, expect) {
|
|
|
44
44
|
}
|
|
45
45
|
return undefined;
|
|
46
46
|
}
|
|
47
|
-
//#endregion
|
|
48
47
|
//#region Public API
|
|
49
48
|
/**
|
|
50
49
|
* Start a tool server on `127.0.0.1` at a random port and path. Each MCP
|
|
51
50
|
* session the agent opens gets its own protocol server over the same host.
|
|
52
51
|
*/
|
|
53
|
-
export async function serveToolsOverHttp(getHost) {
|
|
54
|
-
|
|
55
|
-
const
|
|
52
|
+
export async function serveToolsOverHttp(getHost, reuse) {
|
|
53
|
+
// A reattached agent keeps the path and token an earlier host gave it.
|
|
54
|
+
const token = reuse?.token ?? randomUUID();
|
|
55
|
+
const path = reuse?.path ?? `/mcp/${randomUUID()}`;
|
|
56
56
|
const sessions = new Map();
|
|
57
57
|
const open = async () => {
|
|
58
58
|
const transport = new NodeStreamableHTTPServerTransport({
|
|
@@ -70,12 +70,17 @@ export type AcpPermissionReply = z.infer<typeof AcpPermissionReplySchema>;
|
|
|
70
70
|
export declare const acpPermissionRequests: ExternalChannel<AcpPermissionPrompt>;
|
|
71
71
|
/** @public Decisions broadcast to every parked handler; each filters by its own `requestId`. */
|
|
72
72
|
export declare const acpPermissionDecisions: ExternalChannel<AcpPermissionReply>;
|
|
73
|
+
/**
|
|
74
|
+
* ACP types a tool call's `rawInput` as any JSON value; the request record
|
|
75
|
+
* keeps an object, so a scalar or array is carried as `{ value }`.
|
|
76
|
+
*/
|
|
77
|
+
export declare function rawInputRecord(value: unknown): Record<string, unknown> | undefined;
|
|
73
78
|
/** @public Options for {@link askUserForPermission}. */
|
|
74
79
|
export interface AskUserForPermissionOptions {
|
|
75
80
|
/**
|
|
76
81
|
* How long to wait for an answer, in milliseconds. On expiry the handler
|
|
77
|
-
* resolves with {@link onTimeout}
|
|
78
|
-
*
|
|
82
|
+
* resolves with {@link onTimeout}. With none, the request waits until it is
|
|
83
|
+
* answered or the turn is cancelled.
|
|
79
84
|
*/
|
|
80
85
|
timeout?: number;
|
|
81
86
|
/**
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* topic (broadcast, each waiter filters for its own `requestId`) — per-request
|
|
22
22
|
* channels would leak one channel state per prompt for the harness lifetime.
|
|
23
23
|
*/
|
|
24
|
+
import { isNoeticError } from '@noetic-tools/types';
|
|
24
25
|
import { z } from 'zod';
|
|
25
26
|
//#region Scope
|
|
26
27
|
/**
|
|
@@ -91,6 +92,28 @@ export const acpPermissionDecisions = {
|
|
|
91
92
|
mode: 'topic',
|
|
92
93
|
external: true,
|
|
93
94
|
};
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region Helpers
|
|
97
|
+
/** The longest wait one timer takes, about 24.8 days. A wait with no deadline parks again after it. */
|
|
98
|
+
const MAX_WAIT_MS = 2 ** 31 - 1;
|
|
99
|
+
function isChannelTimeout(error) {
|
|
100
|
+
return isNoeticError(error) && error.noeticError.kind === 'channel_timeout';
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* ACP types a tool call's `rawInput` as any JSON value; the request record
|
|
104
|
+
* keeps an object, so a scalar or array is carried as `{ value }`.
|
|
105
|
+
*/
|
|
106
|
+
export function rawInputRecord(value) {
|
|
107
|
+
if (value === undefined || value === null) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
111
|
+
return Object.fromEntries(Object.entries(value));
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
value,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
94
117
|
/**
|
|
95
118
|
* Build an {@link AcpPermissionHandler} that asks a human over the channel pair
|
|
96
119
|
* above. Pass it as a step's `onPermissionRequest`; it runs after the step's
|
|
@@ -98,21 +121,22 @@ export const acpPermissionDecisions = {
|
|
|
98
121
|
* @public
|
|
99
122
|
*/
|
|
100
123
|
export function askUserForPermission(opts = {}) {
|
|
101
|
-
const timeout = opts
|
|
124
|
+
const { timeout } = opts;
|
|
102
125
|
const onTimeout = opts.onTimeout ?? {
|
|
103
126
|
decision: 'deny',
|
|
104
127
|
reason: 'no answer from the user before the permission request timed out',
|
|
105
128
|
};
|
|
106
129
|
return async (request, ctx, info) => {
|
|
107
130
|
const requestId = crypto.randomUUID();
|
|
108
|
-
const deadline = Date.now() + timeout;
|
|
131
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
132
|
+
const park = () => ctx.recv(acpPermissionDecisions, {
|
|
133
|
+
timeout: deadline === undefined ? MAX_WAIT_MS : Math.max(1, deadline - Date.now()),
|
|
134
|
+
});
|
|
109
135
|
// Park on the decision topic BEFORE publishing the request. Topic delivery
|
|
110
136
|
// reaches only subscribers that are parked at send time, so a reviewer who
|
|
111
137
|
// answers immediately would otherwise answer into the void and the agent
|
|
112
138
|
// would wait out the whole timeout.
|
|
113
|
-
let waiting =
|
|
114
|
-
timeout,
|
|
115
|
-
});
|
|
139
|
+
let waiting = park();
|
|
116
140
|
await ctx.send(acpPermissionRequests, {
|
|
117
141
|
requestId,
|
|
118
142
|
agentId: info.agentId,
|
|
@@ -121,7 +145,7 @@ export function askUserForPermission(opts = {}) {
|
|
|
121
145
|
stepId: info.stepId,
|
|
122
146
|
title: request.toolCall.title ?? request.toolCall.toolCallId,
|
|
123
147
|
kind: request.toolCall.kind ?? undefined,
|
|
124
|
-
rawInput: request.toolCall.rawInput,
|
|
148
|
+
rawInput: rawInputRecord(request.toolCall.rawInput),
|
|
125
149
|
options: request.options.map((option) => ({
|
|
126
150
|
optionId: option.optionId,
|
|
127
151
|
name: option.name,
|
|
@@ -135,13 +159,18 @@ export function askUserForPermission(opts = {}) {
|
|
|
135
159
|
//
|
|
136
160
|
// Re-parking between messages leaves a microtask-sized window in which a
|
|
137
161
|
// decision could be missed — only reachable when two prompts are answered
|
|
138
|
-
// in the same tick
|
|
162
|
+
// in the same tick.
|
|
139
163
|
while (true) {
|
|
140
164
|
let reply;
|
|
141
165
|
try {
|
|
142
166
|
reply = await waiting;
|
|
143
167
|
}
|
|
144
|
-
catch {
|
|
168
|
+
catch (error) {
|
|
169
|
+
// No deadline was set, so the end of one timer is not the end of the wait.
|
|
170
|
+
if (deadline === undefined && isChannelTimeout(error)) {
|
|
171
|
+
waiting = park();
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
145
174
|
return onTimeout;
|
|
146
175
|
}
|
|
147
176
|
if (reply.requestId === requestId) {
|
|
@@ -151,13 +180,10 @@ export function askUserForPermission(opts = {}) {
|
|
|
151
180
|
reason: reply.reason,
|
|
152
181
|
};
|
|
153
182
|
}
|
|
154
|
-
|
|
155
|
-
if (remaining <= 0) {
|
|
183
|
+
if (deadline !== undefined && deadline - Date.now() <= 0) {
|
|
156
184
|
return onTimeout;
|
|
157
185
|
}
|
|
158
|
-
waiting =
|
|
159
|
-
timeout: remaining,
|
|
160
|
-
});
|
|
186
|
+
waiting = park();
|
|
161
187
|
}
|
|
162
188
|
};
|
|
163
189
|
}
|
package/dist/permissions.d.ts
CHANGED
|
@@ -21,6 +21,12 @@ export interface AcpPermissionResolverOptions {
|
|
|
21
21
|
policy?: AcpPermissionPolicy;
|
|
22
22
|
steer?: AcpPermissionSteerer;
|
|
23
23
|
handler?: AcpBoundPermissionHandler;
|
|
24
|
+
/**
|
|
25
|
+
* The harness's approval seam, asked last: when nothing above decided and the
|
|
26
|
+
* policy names no `default`. It holds the request for a person rather than
|
|
27
|
+
* denying it (spec 08 § Held tool calls).
|
|
28
|
+
*/
|
|
29
|
+
approve?: AcpPermissionSteerer;
|
|
24
30
|
}
|
|
25
31
|
/** The concrete ACP outcome payload sent back to the agent. */
|
|
26
32
|
export type AcpRequestPermissionOutcome = {
|
package/dist/permissions.js
CHANGED
|
@@ -142,6 +142,13 @@ export async function resolvePermission(request, opts) {
|
|
|
142
142
|
if (opts.handler) {
|
|
143
143
|
return await opts.handler(request);
|
|
144
144
|
}
|
|
145
|
+
// A policy that names its default has decided. Otherwise a person does.
|
|
146
|
+
if (opts.policy?.default === undefined && opts.approve) {
|
|
147
|
+
const approved = await opts.approve(request);
|
|
148
|
+
if (approved) {
|
|
149
|
+
return approved;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
145
152
|
return {
|
|
146
153
|
decision: opts.policy?.default ?? 'deny',
|
|
147
154
|
reason: 'no permission rule, steering decision, or handler matched',
|