@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
package/dist/connection.js
CHANGED
|
@@ -14,11 +14,18 @@
|
|
|
14
14
|
* - translate an aborted turn into `session/cancel` plus the `cancelled`
|
|
15
15
|
* stop reason the specification requires.
|
|
16
16
|
*/
|
|
17
|
+
import { ClientSideConnection, ndJsonStream, PROTOCOL_VERSION } from '@agentclientprotocol/sdk';
|
|
17
18
|
import { AcpCapabilityError, AcpConnectError } from '@noetic-tools/types';
|
|
18
|
-
import {
|
|
19
|
+
import { z } from 'zod';
|
|
19
20
|
import { clientCapabilitiesFor, NoeticAcpClient } from './client.js';
|
|
21
|
+
import { KEEPER_METHODS } from './keeper/relay.js';
|
|
20
22
|
import { watchTransport } from './liveness.js';
|
|
21
23
|
import { AcpTurnAccumulator } from './turn.js';
|
|
24
|
+
import { divertNewerUpdates } from './update-filter.js';
|
|
25
|
+
/** Updates held for a session nobody has claimed yet, at most. */
|
|
26
|
+
const EARLY_UPDATE_LIMIT = 1e3;
|
|
27
|
+
/** Child sessions remembered per connection, at most; the oldest go first. */
|
|
28
|
+
const CHILD_LIMIT = 1e3;
|
|
22
29
|
//#endregion
|
|
23
30
|
//#region Content capability gating
|
|
24
31
|
/**
|
|
@@ -49,17 +56,105 @@ export function assertPromptContentSupported(agentId, content, capabilities) {
|
|
|
49
56
|
}
|
|
50
57
|
}
|
|
51
58
|
}
|
|
59
|
+
/** What the keeper says about a session (`_noetic/keeper/session`). */
|
|
60
|
+
const KeeperSessionSchema = z.object({
|
|
61
|
+
known: z.boolean(),
|
|
62
|
+
modes: z.custom((value) => isRecord(value)).optional(),
|
|
63
|
+
configOptions: z.custom((value) => Array.isArray(value)).optional(),
|
|
64
|
+
});
|
|
65
|
+
const StopReasonSchema = z.enum([
|
|
66
|
+
'end_turn',
|
|
67
|
+
'max_tokens',
|
|
68
|
+
'max_turn_requests',
|
|
69
|
+
'refusal',
|
|
70
|
+
'cancelled',
|
|
71
|
+
]);
|
|
72
|
+
/** How the keeper answers `_noetic/keeper/turn`. */
|
|
73
|
+
const KeeperTurnSchema = z.discriminatedUnion('state', [
|
|
74
|
+
z.object({
|
|
75
|
+
state: z.literal('idle'),
|
|
76
|
+
}),
|
|
77
|
+
z.object({
|
|
78
|
+
state: z.literal('finished'),
|
|
79
|
+
response: z.object({
|
|
80
|
+
stopReason: StopReasonSchema,
|
|
81
|
+
usage: z.custom((value) => isRecord(value)).nullish(),
|
|
82
|
+
}),
|
|
83
|
+
}),
|
|
84
|
+
]);
|
|
85
|
+
function isRecord(value) {
|
|
86
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What a session is set up with beyond its request, stable across hosts: the
|
|
90
|
+
* tools the bridge serves (names, descriptions, schemas), the bridge's name and
|
|
91
|
+
* how it is declared, and the client's path rules. Snapshotted when the
|
|
92
|
+
* connection opens, which is when the agent reads its tool list.
|
|
93
|
+
*/
|
|
94
|
+
function setupOf(host, tools) {
|
|
95
|
+
return {
|
|
96
|
+
tools: (host.tools?.tools ?? []).map((tool) => ({
|
|
97
|
+
name: tool.name,
|
|
98
|
+
description: tool.description,
|
|
99
|
+
inputSchema: tool.inputSchema,
|
|
100
|
+
})),
|
|
101
|
+
bridge: tools
|
|
102
|
+
? {
|
|
103
|
+
serverName: tools.server.name,
|
|
104
|
+
declare: tools.declare ?? null,
|
|
105
|
+
}
|
|
106
|
+
: null,
|
|
107
|
+
paths: {
|
|
108
|
+
additionalDirectories: [
|
|
109
|
+
...(host.capabilities?.additionalDirectories ?? []),
|
|
110
|
+
].sort(),
|
|
111
|
+
allowAnyPath: host.capabilities?.allowAnyPath === true,
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** JSON with object keys sorted, so two spellings of one value compare equal. */
|
|
116
|
+
export function canonicalJson(value) {
|
|
117
|
+
return JSON.stringify(value, (_key, inner) => {
|
|
118
|
+
if (!isRecord(inner)) {
|
|
119
|
+
return inner;
|
|
120
|
+
}
|
|
121
|
+
return Object.fromEntries(Object.entries(inner).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The child session an update announces: the subagents RFD's
|
|
126
|
+
* `subagent_update`, or codex-acp's earlier draft `subagent_spawned`.
|
|
127
|
+
*/
|
|
128
|
+
export function announcedChild(update) {
|
|
129
|
+
if (update.sessionUpdate === 'subagent_update') {
|
|
130
|
+
return update.sessionId;
|
|
131
|
+
}
|
|
132
|
+
// A draft variant the library does not know arrives as sent (update-filter).
|
|
133
|
+
const draft = {
|
|
134
|
+
...update,
|
|
135
|
+
};
|
|
136
|
+
if (draft.sessionUpdate !== 'subagent_spawned') {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
return typeof draft.subagentSessionId === 'string' ? draft.subagentSessionId : undefined;
|
|
140
|
+
}
|
|
141
|
+
/** JSON-RPC's "method not found". */
|
|
142
|
+
const METHOD_NOT_FOUND = -32601;
|
|
143
|
+
function isMethodNotFound(error) {
|
|
144
|
+
return (typeof error === 'object' && error !== null && Reflect.get(error, 'code') === METHOD_NOT_FOUND);
|
|
145
|
+
}
|
|
52
146
|
//#endregion
|
|
53
147
|
//#region Session
|
|
54
148
|
class AcpSessionImpl {
|
|
55
149
|
agentId;
|
|
56
150
|
agent;
|
|
57
151
|
sessionId;
|
|
58
|
-
|
|
59
|
-
|
|
152
|
+
modeState;
|
|
153
|
+
options;
|
|
60
154
|
commands;
|
|
61
155
|
death;
|
|
62
156
|
promptCapabilities;
|
|
157
|
+
fingerprint;
|
|
63
158
|
/** Set for the duration of a turn; notifications fan out to it. */
|
|
64
159
|
activeTurn;
|
|
65
160
|
/**
|
|
@@ -71,23 +166,45 @@ class AcpSessionImpl {
|
|
|
71
166
|
* deserve a correct answer, just not simultaneously.
|
|
72
167
|
*/
|
|
73
168
|
queue = Promise.resolve();
|
|
74
|
-
constructor(agentId, agent, sessionId,
|
|
169
|
+
constructor(agentId, agent, sessionId, modeState, options, commands,
|
|
75
170
|
/** Rejects if the agent dies, so a request cannot outlive it. */
|
|
76
171
|
death,
|
|
77
172
|
/** What the agent said it accepts, enforced before a turn goes on the wire. */
|
|
78
|
-
promptCapabilities
|
|
173
|
+
promptCapabilities,
|
|
174
|
+
/** What the session was opened with; undefined on a reattached session. */
|
|
175
|
+
fingerprint) {
|
|
79
176
|
this.agentId = agentId;
|
|
80
177
|
this.agent = agent;
|
|
81
178
|
this.sessionId = sessionId;
|
|
82
|
-
this.
|
|
83
|
-
this.
|
|
179
|
+
this.modeState = modeState;
|
|
180
|
+
this.options = options;
|
|
84
181
|
this.commands = commands;
|
|
85
182
|
this.death = death;
|
|
86
183
|
this.promptCapabilities = promptCapabilities;
|
|
184
|
+
this.fingerprint = fingerprint;
|
|
185
|
+
}
|
|
186
|
+
get modes() {
|
|
187
|
+
return this.modeState;
|
|
188
|
+
}
|
|
189
|
+
get configOptions() {
|
|
190
|
+
return this.options ?? [];
|
|
87
191
|
}
|
|
88
192
|
get availableCommands() {
|
|
89
193
|
return this.commands;
|
|
90
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Take the modes and config options a `session/load` answer advertises. The
|
|
197
|
+
* session is registered before the load (so replayed updates reach it), so
|
|
198
|
+
* these arrive after construction.
|
|
199
|
+
*/
|
|
200
|
+
applySettings(settings) {
|
|
201
|
+
if (settings.modes) {
|
|
202
|
+
this.modeState = settings.modes;
|
|
203
|
+
}
|
|
204
|
+
if (settings.configOptions) {
|
|
205
|
+
this.options = settings.configOptions;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
91
208
|
/** Feed a notification to the running turn, if any. */
|
|
92
209
|
receive(notification) {
|
|
93
210
|
const update = notification.update;
|
|
@@ -96,6 +213,15 @@ class AcpSessionImpl {
|
|
|
96
213
|
...update.availableCommands,
|
|
97
214
|
];
|
|
98
215
|
}
|
|
216
|
+
if (update.sessionUpdate === 'current_mode_update' && this.modeState) {
|
|
217
|
+
this.modeState = {
|
|
218
|
+
...this.modeState,
|
|
219
|
+
currentModeId: update.currentModeId,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (update.sessionUpdate === 'config_option_update') {
|
|
223
|
+
this.options = update.configOptions;
|
|
224
|
+
}
|
|
99
225
|
this.activeTurn?.push(notification);
|
|
100
226
|
}
|
|
101
227
|
async prompt(opts) {
|
|
@@ -111,8 +237,51 @@ class AcpSessionImpl {
|
|
|
111
237
|
return await ours;
|
|
112
238
|
}
|
|
113
239
|
async runTurn(opts) {
|
|
240
|
+
const result = await this.collectTurn(opts.signal, async () => this.agent.prompt({
|
|
241
|
+
sessionId: this.sessionId,
|
|
242
|
+
prompt: [
|
|
243
|
+
...opts.content,
|
|
244
|
+
],
|
|
245
|
+
}));
|
|
246
|
+
if (!result) {
|
|
247
|
+
throw new Error('session/prompt returned no answer.');
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Make this a reattached session: {@link resumeTurn} collects the turn the
|
|
253
|
+
* previous host left running through the keeper.
|
|
254
|
+
*/
|
|
255
|
+
enableResume() {
|
|
256
|
+
this.resumeTurn = async (opts) => {
|
|
257
|
+
const ours = this.queue.then(() => this.collectTurn(opts.signal, () => this.keeperTurn()), () => this.collectTurn(opts.signal, () => this.keeperTurn()));
|
|
258
|
+
this.queue = ours.catch(() => undefined);
|
|
259
|
+
return await ours;
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/** Set on a reattached session; see `AcpSession.resumeTurn`. */
|
|
263
|
+
resumeTurn;
|
|
264
|
+
/** Ask the keeper for the turn in flight: its updates arrive, then its answer. */
|
|
265
|
+
async keeperTurn() {
|
|
266
|
+
const request = this.agent.extMethod?.bind(this.agent);
|
|
267
|
+
if (!request) {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
const answer = KeeperTurnSchema.parse(await request(KEEPER_METHODS.turn, {
|
|
271
|
+
sessionId: this.sessionId,
|
|
272
|
+
}));
|
|
273
|
+
return answer.state === 'finished' ? answer.response : undefined;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Run one turn: updates go to a fresh accumulator while `send` waits for
|
|
277
|
+
* the agent's answer. Undefined when there was no turn to collect.
|
|
278
|
+
*/
|
|
279
|
+
async collectTurn(signal, send) {
|
|
114
280
|
const accumulator = new AcpTurnAccumulator();
|
|
115
281
|
this.activeTurn = accumulator;
|
|
282
|
+
const opts = {
|
|
283
|
+
signal,
|
|
284
|
+
};
|
|
116
285
|
const onAbort = () => {
|
|
117
286
|
// Fire-and-forget: the specification says the agent still answers the
|
|
118
287
|
// original `session/prompt` with the `cancelled` stop reason.
|
|
@@ -123,17 +292,20 @@ class AcpSessionImpl {
|
|
|
123
292
|
});
|
|
124
293
|
try {
|
|
125
294
|
const response = await Promise.race([
|
|
126
|
-
|
|
127
|
-
sessionId: this.sessionId,
|
|
128
|
-
prompt: [
|
|
129
|
-
...opts.content,
|
|
130
|
-
],
|
|
131
|
-
}),
|
|
295
|
+
send(),
|
|
132
296
|
this.death,
|
|
133
297
|
]);
|
|
134
|
-
|
|
298
|
+
if (!response) {
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
const result = accumulator.result({
|
|
135
302
|
stopReason: response.stopReason,
|
|
303
|
+
reportedUsage: response.usage ?? undefined,
|
|
136
304
|
});
|
|
305
|
+
return {
|
|
306
|
+
...result,
|
|
307
|
+
currentModeId: result.currentModeId ?? this.modeState?.currentModeId,
|
|
308
|
+
};
|
|
137
309
|
}
|
|
138
310
|
finally {
|
|
139
311
|
opts.signal?.removeEventListener('abort', onAbort);
|
|
@@ -146,7 +318,7 @@ class AcpSessionImpl {
|
|
|
146
318
|
});
|
|
147
319
|
}
|
|
148
320
|
async setMode(modeId) {
|
|
149
|
-
if (!this.
|
|
321
|
+
if (!this.modeState || !this.agent.setSessionMode) {
|
|
150
322
|
throw new AcpCapabilityError({
|
|
151
323
|
agentId: this.agentId,
|
|
152
324
|
capability: 'session/set_mode',
|
|
@@ -161,19 +333,62 @@ class AcpSessionImpl {
|
|
|
161
333
|
]);
|
|
162
334
|
}
|
|
163
335
|
async setModel(modelId) {
|
|
164
|
-
|
|
336
|
+
const model = this.modelOption();
|
|
337
|
+
if (model && this.agent.setSessionConfigOption) {
|
|
338
|
+
const response = await Promise.race([
|
|
339
|
+
this.agent.setSessionConfigOption({
|
|
340
|
+
sessionId: this.sessionId,
|
|
341
|
+
configId: model.id,
|
|
342
|
+
value: modelId,
|
|
343
|
+
}),
|
|
344
|
+
this.death,
|
|
345
|
+
]);
|
|
346
|
+
this.options = response.configOptions;
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
await this.setModelDirectly(modelId);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* The `model` config option: by category, else by id, for an agent that
|
|
353
|
+
* leaves the (optional, UX-only) category off.
|
|
354
|
+
*/
|
|
355
|
+
modelOption() {
|
|
356
|
+
const selects = (this.options ?? []).filter((option) => option.type === 'select');
|
|
357
|
+
return (selects.find((option) => option.category === 'model') ??
|
|
358
|
+
selects.find((option) => option.id === 'model'));
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* An agent that advertises no `model` config option may still take the
|
|
362
|
+
* draft `session/set_model` request (it predates config options). One that
|
|
363
|
+
* does not know it answers "method not found", which is the capability
|
|
364
|
+
* error a step expects.
|
|
365
|
+
*/
|
|
366
|
+
async setModelDirectly(modelId) {
|
|
367
|
+
const request = this.agent.extMethod?.bind(this.agent);
|
|
368
|
+
if (!request) {
|
|
165
369
|
throw new AcpCapabilityError({
|
|
166
370
|
agentId: this.agentId,
|
|
167
|
-
capability: 'session/
|
|
371
|
+
capability: 'session/set_config_option:model',
|
|
168
372
|
});
|
|
169
373
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
374
|
+
try {
|
|
375
|
+
await Promise.race([
|
|
376
|
+
request('session/set_model', {
|
|
377
|
+
sessionId: this.sessionId,
|
|
378
|
+
modelId,
|
|
379
|
+
}),
|
|
380
|
+
this.death,
|
|
381
|
+
]);
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
if (isMethodNotFound(error)) {
|
|
385
|
+
throw new AcpCapabilityError({
|
|
386
|
+
agentId: this.agentId,
|
|
387
|
+
capability: 'session/set_config_option:model',
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
177
392
|
}
|
|
178
393
|
}
|
|
179
394
|
//#endregion
|
|
@@ -188,8 +403,15 @@ class AcpAgentConnectionImpl {
|
|
|
188
403
|
protocolVersion;
|
|
189
404
|
watch;
|
|
190
405
|
tools;
|
|
406
|
+
reattach;
|
|
191
407
|
sessions = new Map();
|
|
192
|
-
|
|
408
|
+
/** Each subagent's child session, to the session that announced it. */
|
|
409
|
+
children = new Map();
|
|
410
|
+
/** Updates for a session not yet known, oldest first. */
|
|
411
|
+
early = [];
|
|
412
|
+
/** Set by {@link openAcpConnection}. */
|
|
413
|
+
settings = {};
|
|
414
|
+
constructor(agentId, agent, transport, client, agentCapabilities, authMethods, protocolVersion, watch, tools, reattach, reattached) {
|
|
193
415
|
this.agentId = agentId;
|
|
194
416
|
this.agent = agent;
|
|
195
417
|
this.transport = transport;
|
|
@@ -199,6 +421,72 @@ class AcpAgentConnectionImpl {
|
|
|
199
421
|
this.protocolVersion = protocolVersion;
|
|
200
422
|
this.watch = watch;
|
|
201
423
|
this.tools = tools;
|
|
424
|
+
this.reattach = reattach;
|
|
425
|
+
if (reattached) {
|
|
426
|
+
this.attachSession = (sessionId) => this.attachTo(sessionId);
|
|
427
|
+
}
|
|
428
|
+
if (transport.detach) {
|
|
429
|
+
this.detach = () => this.letGo();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/** Set on a reattached connection; see `AcpAgentConnection.attachSession`. */
|
|
433
|
+
attachSession;
|
|
434
|
+
/** The live session `sessionId`, as the keeper last saw it. */
|
|
435
|
+
async attachTo(sessionId) {
|
|
436
|
+
const request = this.agent.extMethod?.bind(this.agent);
|
|
437
|
+
const info = request
|
|
438
|
+
? KeeperSessionSchema.parse(await Promise.race([
|
|
439
|
+
request(KEEPER_METHODS.session, {
|
|
440
|
+
sessionId,
|
|
441
|
+
}),
|
|
442
|
+
this.watch.death,
|
|
443
|
+
]))
|
|
444
|
+
: undefined;
|
|
445
|
+
if (!info?.known) {
|
|
446
|
+
throw new AcpConnectError({
|
|
447
|
+
agentId: this.agentId,
|
|
448
|
+
message: `ACP agent '${this.agentId}' no longer has session ${sessionId}.`,
|
|
449
|
+
gone: true,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
const session = new AcpSessionImpl(this.agentId, this.agent, sessionId, info.modes, info.configOptions, [], this.watch.death, this.agentCapabilities.promptCapabilities);
|
|
453
|
+
session.enableResume();
|
|
454
|
+
return this.track(session);
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Route one `session/update` (spec 27 § Subagents' own sessions): an update
|
|
458
|
+
* of one of this connection's sessions is delivered; a subagent's child
|
|
459
|
+
* session's is dropped, never the step's output; one for a session not known
|
|
460
|
+
* yet is held until it is.
|
|
461
|
+
*/
|
|
462
|
+
route(notification) {
|
|
463
|
+
const owner = notification.sessionId;
|
|
464
|
+
const child = announcedChild(notification.update);
|
|
465
|
+
if (child !== undefined && child !== owner && this.owns(owner)) {
|
|
466
|
+
this.adoptChild(child, owner);
|
|
467
|
+
}
|
|
468
|
+
if (this.children.has(owner)) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
if (this.sessions.has(owner)) {
|
|
472
|
+
this.settings.deliver?.(notification);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
this.early.push(notification);
|
|
476
|
+
if (this.early.length > EARLY_UPDATE_LIMIT) {
|
|
477
|
+
this.early.shift();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
owns(sessionId) {
|
|
481
|
+
return this.sessions.has(sessionId) || this.children.has(sessionId);
|
|
482
|
+
}
|
|
483
|
+
adoptChild(child, parent) {
|
|
484
|
+
this.children.set(child, parent);
|
|
485
|
+
const oldest = this.children.keys().next().value;
|
|
486
|
+
if (this.children.size > CHILD_LIMIT && oldest !== undefined) {
|
|
487
|
+
this.children.delete(oldest);
|
|
488
|
+
}
|
|
489
|
+
this.early = this.early.filter((held) => held.sessionId !== child);
|
|
202
490
|
}
|
|
203
491
|
/** The sink the client hands every `session/update` notification. */
|
|
204
492
|
get sink() {
|
|
@@ -214,15 +502,25 @@ class AcpAgentConnectionImpl {
|
|
|
214
502
|
this.watch.death,
|
|
215
503
|
]);
|
|
216
504
|
}
|
|
505
|
+
fingerprintFor(opts) {
|
|
506
|
+
return canonicalJson({
|
|
507
|
+
cwd: opts.cwd,
|
|
508
|
+
fields: this.sessionFields(opts),
|
|
509
|
+
mcpServers: (opts.mcpServers ?? []).map((server) => server.name),
|
|
510
|
+
capabilities: this.settings.capabilities ?? {},
|
|
511
|
+
setup: this.settings.setup ?? {},
|
|
512
|
+
});
|
|
513
|
+
}
|
|
217
514
|
async newSession(opts) {
|
|
218
515
|
const response = await Promise.race([
|
|
219
516
|
this.agent.newSession({
|
|
220
517
|
cwd: opts.cwd,
|
|
221
518
|
mcpServers: this.sessionMcpServers(opts.mcpServers),
|
|
519
|
+
...this.sessionFields(opts),
|
|
222
520
|
}),
|
|
223
521
|
this.watch.death,
|
|
224
522
|
]);
|
|
225
|
-
return this.track(new AcpSessionImpl(this.agentId, this.agent, response.sessionId, response.modes ?? undefined, response.
|
|
523
|
+
return this.track(new AcpSessionImpl(this.agentId, this.agent, response.sessionId, response.modes ?? undefined, response.configOptions ?? undefined, [], this.watch.death, this.agentCapabilities.promptCapabilities, this.fingerprintFor(opts)));
|
|
226
524
|
}
|
|
227
525
|
async loadSession(opts) {
|
|
228
526
|
if (this.agentCapabilities.loadSession !== true || !this.agent.loadSession) {
|
|
@@ -231,21 +529,44 @@ class AcpAgentConnectionImpl {
|
|
|
231
529
|
capability: 'loadSession',
|
|
232
530
|
});
|
|
233
531
|
}
|
|
532
|
+
const fields = this.sessionFields(opts);
|
|
234
533
|
// Register before the call: `session/load` replays the conversation through
|
|
235
534
|
// `session/update` notifications, which must not be dropped on the floor.
|
|
236
|
-
const session = this.track(new AcpSessionImpl(this.agentId, this.agent, opts.sessionId, undefined, undefined, [], this.watch.death, this.agentCapabilities.promptCapabilities));
|
|
237
|
-
await Promise.race([
|
|
535
|
+
const session = this.track(new AcpSessionImpl(this.agentId, this.agent, opts.sessionId, undefined, undefined, [], this.watch.death, this.agentCapabilities.promptCapabilities, this.fingerprintFor(opts)));
|
|
536
|
+
const response = await Promise.race([
|
|
238
537
|
this.agent.loadSession({
|
|
239
538
|
sessionId: opts.sessionId,
|
|
240
539
|
cwd: opts.cwd,
|
|
241
540
|
mcpServers: this.sessionMcpServers(opts.mcpServers),
|
|
541
|
+
...fields,
|
|
242
542
|
}),
|
|
243
543
|
this.watch.death,
|
|
244
|
-
])
|
|
544
|
+
]).catch((error) => {
|
|
545
|
+
// A failed load leaves nothing behind: no session, no children of it.
|
|
546
|
+
this.forget(opts.sessionId);
|
|
547
|
+
throw error;
|
|
548
|
+
});
|
|
549
|
+
if (response) {
|
|
550
|
+
session.applySettings(response);
|
|
551
|
+
}
|
|
245
552
|
return session;
|
|
246
553
|
}
|
|
554
|
+
/** Set when the transport can let go of an agent that outlives this host. */
|
|
555
|
+
detach;
|
|
556
|
+
/** Release this host's side, leaving the agent running for a later host. */
|
|
557
|
+
async letGo() {
|
|
558
|
+
this.sessions.clear();
|
|
559
|
+
this.children.clear();
|
|
560
|
+
this.early = [];
|
|
561
|
+
this.watch.shutdown();
|
|
562
|
+
await this.transport.detach?.().catch(() => undefined);
|
|
563
|
+
await this.client.dispose();
|
|
564
|
+
await this.tools?.close().catch(() => undefined);
|
|
565
|
+
}
|
|
247
566
|
async close() {
|
|
248
567
|
this.sessions.clear();
|
|
568
|
+
this.children.clear();
|
|
569
|
+
this.early = [];
|
|
249
570
|
// Reject anything still in flight first. Once the transport is down
|
|
250
571
|
// nothing can answer it, so a pending request would hang forever.
|
|
251
572
|
this.watch.shutdown();
|
|
@@ -258,8 +579,36 @@ class AcpAgentConnectionImpl {
|
|
|
258
579
|
//#region internals
|
|
259
580
|
track(session) {
|
|
260
581
|
this.sessions.set(session.sessionId, session);
|
|
582
|
+
// Everything held while no session was known goes through the routing
|
|
583
|
+
// again, in order: the new session's own updates are delivered, a child
|
|
584
|
+
// announced on it is adopted (dropping that child's held updates), and
|
|
585
|
+
// the rest stays held.
|
|
586
|
+
const held = this.early;
|
|
587
|
+
this.early = [];
|
|
588
|
+
for (const notification of held) {
|
|
589
|
+
this.route(notification);
|
|
590
|
+
}
|
|
261
591
|
return session;
|
|
262
592
|
}
|
|
593
|
+
/** Forget a session and every descendant it announced. */
|
|
594
|
+
forget(sessionId) {
|
|
595
|
+
this.sessions.delete(sessionId);
|
|
596
|
+
const gone = new Set([
|
|
597
|
+
sessionId,
|
|
598
|
+
]);
|
|
599
|
+
for (let grew = true; grew;) {
|
|
600
|
+
grew = false;
|
|
601
|
+
for (const [child, parent] of this.children) {
|
|
602
|
+
if (gone.has(parent) && !gone.has(child)) {
|
|
603
|
+
gone.add(child);
|
|
604
|
+
grew = true;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
for (const child of gone) {
|
|
609
|
+
this.children.delete(child);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
263
612
|
/**
|
|
264
613
|
* The servers `session/new` carries: the user's plus the tool bridge, or
|
|
265
614
|
* none when the agent was given them all at launch.
|
|
@@ -278,6 +627,57 @@ class AcpAgentConnectionImpl {
|
|
|
278
627
|
: []),
|
|
279
628
|
]);
|
|
280
629
|
}
|
|
630
|
+
/**
|
|
631
|
+
* The `_meta` and system prompt a session request carries (spec 27
|
|
632
|
+
* § Instructions): instructions as the protocol's `systemPrompt` when the
|
|
633
|
+
* agent advertises it, else through the adapter's extension, else refused
|
|
634
|
+
* rather than dropped; the adapter's `sessionMeta` over that extension.
|
|
635
|
+
*/
|
|
636
|
+
sessionFields(opts) {
|
|
637
|
+
const instructions = opts.instructions;
|
|
638
|
+
const native = instructions !== undefined && this.advertisesInstructions();
|
|
639
|
+
const fallback = instructions !== undefined && !native ? this.instructionsFallback(instructions) : undefined;
|
|
640
|
+
const sessionMeta = this.settings.sessionMeta;
|
|
641
|
+
const meta = fallback || sessionMeta
|
|
642
|
+
? {
|
|
643
|
+
...fallback,
|
|
644
|
+
...sessionMeta,
|
|
645
|
+
}
|
|
646
|
+
: undefined;
|
|
647
|
+
return {
|
|
648
|
+
...(meta
|
|
649
|
+
? {
|
|
650
|
+
_meta: meta,
|
|
651
|
+
}
|
|
652
|
+
: {}),
|
|
653
|
+
...(native && instructions !== undefined
|
|
654
|
+
? {
|
|
655
|
+
systemPrompt: {
|
|
656
|
+
mode: 'append',
|
|
657
|
+
content: [
|
|
658
|
+
{
|
|
659
|
+
type: 'text',
|
|
660
|
+
text: instructions,
|
|
661
|
+
},
|
|
662
|
+
],
|
|
663
|
+
},
|
|
664
|
+
}
|
|
665
|
+
: {}),
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
advertisesInstructions() {
|
|
669
|
+
return Boolean(this.agentCapabilities.sessionCapabilities?.systemPrompt);
|
|
670
|
+
}
|
|
671
|
+
instructionsFallback(instructions) {
|
|
672
|
+
const make = this.settings.instructionsMeta;
|
|
673
|
+
if (!make) {
|
|
674
|
+
throw new AcpCapabilityError({
|
|
675
|
+
agentId: this.agentId,
|
|
676
|
+
capability: 'sessionCapabilities.systemPrompt',
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
return make(instructions);
|
|
680
|
+
}
|
|
281
681
|
/** Reject HTTP/SSE MCP transports the agent never advertised support for. */
|
|
282
682
|
checkedMcpServers(servers) {
|
|
283
683
|
if (!servers || servers.length === 0) {
|
|
@@ -320,19 +720,24 @@ export async function openAcpConnection(opts) {
|
|
|
320
720
|
// policy and event sink to whichever step opened the connection, silently
|
|
321
721
|
// ignoring the configuration of every step that later reuses the session.
|
|
322
722
|
let sink;
|
|
723
|
+
let connection;
|
|
323
724
|
const host = opts.host;
|
|
324
725
|
const client = new NoeticAcpClient({
|
|
325
726
|
host,
|
|
326
727
|
permitsBridged: opts.tools?.permits,
|
|
327
|
-
onNotify: (notification) =>
|
|
328
|
-
sink?.(notification);
|
|
329
|
-
host.onSessionUpdate(notification);
|
|
330
|
-
},
|
|
728
|
+
onNotify: (notification) => notify(notification),
|
|
331
729
|
});
|
|
332
730
|
// The protocol library never rejects pending responses when the stream ends,
|
|
333
731
|
// so without this a dead agent leaves every request hanging forever.
|
|
334
732
|
const watch = watchTransport(opts.agentId, opts.transport, opts.signal);
|
|
335
|
-
const
|
|
733
|
+
const notify = (notification) => {
|
|
734
|
+
// Only this connection's own sessions produce the step's output.
|
|
735
|
+
if (!connection) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
connection.route(notification);
|
|
739
|
+
};
|
|
740
|
+
const agent = new ClientSideConnection(() => client, ndJsonStream(opts.transport.writable, divertNewerUpdates(watch.readable, notify)));
|
|
336
741
|
let initialized;
|
|
337
742
|
try {
|
|
338
743
|
initialized = await Promise.race([
|
|
@@ -345,15 +750,29 @@ export async function openAcpConnection(opts) {
|
|
|
345
750
|
}
|
|
346
751
|
catch (error) {
|
|
347
752
|
watch.shutdown('failed the initialize handshake');
|
|
348
|
-
|
|
753
|
+
// An agent reached for an earlier host is let go of, never stopped.
|
|
754
|
+
const release = opts.reattached && opts.transport.detach
|
|
755
|
+
? opts.transport.detach.bind(opts.transport)
|
|
756
|
+
: opts.transport.close.bind(opts.transport);
|
|
757
|
+
await release().catch(() => undefined);
|
|
349
758
|
throw new AcpConnectError({
|
|
350
759
|
agentId: opts.agentId,
|
|
351
760
|
message: `ACP agent '${opts.agentId}' failed the initialize handshake.`,
|
|
352
761
|
cause: error,
|
|
353
762
|
});
|
|
354
763
|
}
|
|
355
|
-
|
|
764
|
+
connection = new AcpAgentConnectionImpl(opts.agentId, agent, opts.transport, client, initialized.agentCapabilities ?? {}, initialized.authMethods ?? [], initialized.protocolVersion, watch, opts.tools, opts.reattach, opts.reattached === true);
|
|
356
765
|
sink = connection.sink;
|
|
766
|
+
connection.settings = {
|
|
767
|
+
instructionsMeta: opts.instructionsMeta,
|
|
768
|
+
sessionMeta: opts.sessionMeta,
|
|
769
|
+
capabilities: clientCapabilitiesFor(host),
|
|
770
|
+
setup: setupOf(host, opts.tools),
|
|
771
|
+
deliver: (notification) => {
|
|
772
|
+
sink?.(notification);
|
|
773
|
+
host.onSessionUpdate(notification);
|
|
774
|
+
},
|
|
775
|
+
};
|
|
357
776
|
return connection;
|
|
358
777
|
}
|
|
359
778
|
//#endregion
|
package/dist/define.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* {@link openAcpConnection}.
|
|
9
9
|
*/
|
|
10
10
|
import type { AcpAgent, AcpMachineFactory, AcpTransportFactory, SpawnCommand } from '@noetic-tools/types';
|
|
11
|
+
import type { InstructionsMeta } from './connection.js';
|
|
11
12
|
import type { AcpToolWiring } from './tool-bridge.js';
|
|
12
13
|
/** @public Options for {@link defineAcpAgent}. */
|
|
13
14
|
export interface DefineAcpAgentOptions {
|
|
@@ -32,6 +33,18 @@ export interface DefineAcpAgentOptions {
|
|
|
32
33
|
* the bridge joins `session/new` with no agent-specific settings.
|
|
33
34
|
*/
|
|
34
35
|
tools?: AcpToolWiring;
|
|
36
|
+
/**
|
|
37
|
+
* How this agent takes a step's `instructions` when it does not advertise
|
|
38
|
+
* `sessionCapabilities.systemPrompt`: the `_meta` to send instead. Without
|
|
39
|
+
* one, such instructions are refused (spec 27 § Instructions).
|
|
40
|
+
*/
|
|
41
|
+
instructionsMeta?: InstructionsMeta;
|
|
42
|
+
/**
|
|
43
|
+
* This agent's own session options, sent as `_meta` on every session it
|
|
44
|
+
* opens (Claude Code's `{ claudeCode: { options } }`). Vendor options live
|
|
45
|
+
* here, never on a step.
|
|
46
|
+
*/
|
|
47
|
+
sessionMeta?: Readonly<Record<string, unknown>>;
|
|
35
48
|
}
|
|
36
49
|
/** @public Build an {@link AcpAgent} from a command or a transport factory. */
|
|
37
50
|
export declare function defineAcpAgent(def: DefineAcpAgentOptions): AcpAgent;
|