@noetic-tools/acp 2.0.0-next.2 → 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/client.d.ts +7 -0
- package/dist/client.js +39 -0
- package/dist/connection.d.ts +20 -1
- package/dist/connection.js +246 -9
- package/dist/define.d.ts +13 -0
- package/dist/define.js +31 -1
- package/dist/index.d.ts +1 -1
- package/dist/keeper/client.d.ts +9 -0
- package/dist/keeper/client.js +33 -1
- package/dist/keeper/process.js +3 -0
- package/dist/keeper/protocol.d.ts +2 -0
- package/dist/keeper/protocol.js +7 -0
- 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.js +9 -0
- package/dist/stdio.d.ts +22 -1
- package/dist/stdio.js +60 -14
- package/dist/update-filter.d.ts +17 -1
- package/dist/update-filter.js +75 -1
- package/package.json +2 -2
package/dist/agents.d.ts
CHANGED
|
@@ -75,6 +75,11 @@ export interface AcpPresetOptions {
|
|
|
75
75
|
machine?: AcpMachineFactory;
|
|
76
76
|
/** Attach to an agent this package does not start, in place of the preset's command. */
|
|
77
77
|
transport?: AcpTransportFactory;
|
|
78
|
+
/**
|
|
79
|
+
* The agent's own session options, sent as `_meta` on every session it opens
|
|
80
|
+
* (Claude Code's `{ claudeCode: { options } }`).
|
|
81
|
+
*/
|
|
82
|
+
sessionMeta?: Readonly<Record<string, unknown>>;
|
|
78
83
|
}
|
|
79
84
|
/**
|
|
80
85
|
* Claude Code via `@agentclientprotocol/claude-agent-acp`, the ACP bridge over
|
package/dist/agents.js
CHANGED
|
@@ -89,7 +89,7 @@ export const PRESET_ENV = {
|
|
|
89
89
|
'NOETIC_TOOLS_HEADERS',
|
|
90
90
|
],
|
|
91
91
|
};
|
|
92
|
-
function preset({ agentId, base, opts = {}, wiring, }) {
|
|
92
|
+
function preset({ agentId, base, opts = {}, wiring, instructionsMeta, }) {
|
|
93
93
|
const spec = {
|
|
94
94
|
command: opts.command ?? base.command,
|
|
95
95
|
args: [
|
|
@@ -116,9 +116,20 @@ function preset({ agentId, base, opts = {}, wiring, }) {
|
|
|
116
116
|
}),
|
|
117
117
|
env: spec.env,
|
|
118
118
|
tools: wiring,
|
|
119
|
+
instructionsMeta,
|
|
120
|
+
sessionMeta: opts.sessionMeta,
|
|
119
121
|
});
|
|
120
122
|
}
|
|
121
123
|
//#endregion
|
|
124
|
+
/**
|
|
125
|
+
* claude-agent-acp's own system prompt extension, read by bridges that predate
|
|
126
|
+
* `sessionCapabilities.systemPrompt`: appended to Claude Code's own prompt.
|
|
127
|
+
*/
|
|
128
|
+
const claudeInstructionsMeta = (instructions) => ({
|
|
129
|
+
systemPrompt: {
|
|
130
|
+
append: instructions,
|
|
131
|
+
},
|
|
132
|
+
});
|
|
122
133
|
//#region Presets
|
|
123
134
|
/**
|
|
124
135
|
* Claude Code via `@agentclientprotocol/claude-agent-acp`, the ACP bridge over
|
|
@@ -131,6 +142,7 @@ export function claudeCode(opts = {}) {
|
|
|
131
142
|
base: PRESET_COMMANDS['claude-code'],
|
|
132
143
|
opts,
|
|
133
144
|
wiring: claudeToolWiring,
|
|
145
|
+
instructionsMeta: claudeInstructionsMeta,
|
|
134
146
|
});
|
|
135
147
|
}
|
|
136
148
|
/**
|
package/dist/client.d.ts
CHANGED
|
@@ -63,6 +63,13 @@ export declare class NoeticAcpClient implements acp.Client {
|
|
|
63
63
|
/** Release every terminal this connection created. */
|
|
64
64
|
dispose(): Promise<void>;
|
|
65
65
|
requestPermission(params: acp.RequestPermissionRequest): Promise<acp.RequestPermissionResponse>;
|
|
66
|
+
/**
|
|
67
|
+
* Ask the current step's handler. With none, or when it throws, the answer
|
|
68
|
+
* is `cancel`: the agent stops asking rather than waiting forever.
|
|
69
|
+
*/
|
|
70
|
+
createElicitation(params: acp.CreateElicitationRequest): Promise<acp.CreateElicitationResponse>;
|
|
71
|
+
/** A URL-mode elicitation finished; nothing waits on it here. */
|
|
72
|
+
completeElicitation(_params: acp.CompleteElicitationNotification): Promise<void>;
|
|
66
73
|
sessionUpdate(params: acp.SessionNotification): Promise<void>;
|
|
67
74
|
/**
|
|
68
75
|
* Keep each `tool_call` until it settles; a permission request can only
|
package/dist/client.js
CHANGED
|
@@ -17,6 +17,7 @@ import { isAbsolutePath, isWithinRoots } from './paths.js';
|
|
|
17
17
|
import { resolvePermission, selectPermissionOption } from './permissions.js';
|
|
18
18
|
import { buildCommandLine, TerminalRegistry } from './terminals.js';
|
|
19
19
|
import { allowOnceOption } from './tool-permissions.js';
|
|
20
|
+
import { untagElicitation } from './update-filter.js';
|
|
20
21
|
//#region Capability advertisement
|
|
21
22
|
/** Build the `ClientCapabilities` sent during `initialize`. */
|
|
22
23
|
export function clientCapabilitiesFor(host) {
|
|
@@ -27,6 +28,15 @@ export function clientCapabilitiesFor(host) {
|
|
|
27
28
|
writeTextFile: config?.writeTextFile !== false,
|
|
28
29
|
},
|
|
29
30
|
terminal: config?.terminal !== false,
|
|
31
|
+
// An agent's questions are held for a person (spec 27 § Questions the agent
|
|
32
|
+
// asks); URL questions are not advertised, since nothing here opens a page.
|
|
33
|
+
...(config?.elicitation === false
|
|
34
|
+
? {}
|
|
35
|
+
: {
|
|
36
|
+
elicitation: {
|
|
37
|
+
form: {},
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
30
40
|
};
|
|
31
41
|
}
|
|
32
42
|
//#endregion
|
|
@@ -122,6 +132,35 @@ export class NoeticAcpClient {
|
|
|
122
132
|
outcome: selected,
|
|
123
133
|
};
|
|
124
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Ask the current step's handler. With none, or when it throws, the answer
|
|
137
|
+
* is `cancel`: the agent stops asking rather than waiting forever.
|
|
138
|
+
*/
|
|
139
|
+
async createElicitation(params) {
|
|
140
|
+
const { request, requestId } = untagElicitation(params);
|
|
141
|
+
const handler = this.host.onElicitation;
|
|
142
|
+
// A withdrawn capability is refused here too: an agent that asks anyway
|
|
143
|
+
// must not reach a person nobody set up to answer.
|
|
144
|
+
if (!handler || this.host.capabilities?.elicitation === false) {
|
|
145
|
+
return {
|
|
146
|
+
action: 'cancel',
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
return await handler(request, {
|
|
151
|
+
requestId,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return {
|
|
156
|
+
action: 'cancel',
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** A URL-mode elicitation finished; nothing waits on it here. */
|
|
161
|
+
async completeElicitation(_params) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
125
164
|
async sessionUpdate(params) {
|
|
126
165
|
this.trackAnnounced(params.update);
|
|
127
166
|
this.onNotify(params);
|
package/dist/connection.d.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* stop reason the specification requires.
|
|
16
16
|
*/
|
|
17
17
|
import type * as acp from '@agentclientprotocol/sdk';
|
|
18
|
-
import type { AcpAgentConnection, AcpClientHost, AcpContentBlock, AcpReattachHandle, AcpTransport } from '@noetic-tools/types';
|
|
18
|
+
import type { AcpAgentConnection, AcpClientHost, AcpContentBlock, AcpReattachHandle, AcpSessionNotification, AcpTransport } from '@noetic-tools/types';
|
|
19
19
|
import type { ToolBridge } from './tool-bridge.js';
|
|
20
20
|
/** @public Options for {@link openAcpConnection}. */
|
|
21
21
|
export interface OpenAcpConnectionOptions {
|
|
@@ -29,13 +29,32 @@ export interface OpenAcpConnectionOptions {
|
|
|
29
29
|
reattach?: AcpReattachHandle;
|
|
30
30
|
/** The transport reaches an agent an earlier host started (through its keeper). */
|
|
31
31
|
reattached?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* The agent's own way to take instructions when it does not advertise
|
|
34
|
+
* `sessionCapabilities.systemPrompt`: the `_meta` to send instead.
|
|
35
|
+
*/
|
|
36
|
+
instructionsMeta?: InstructionsMeta;
|
|
37
|
+
/**
|
|
38
|
+
* The agent's own session options, sent as `_meta` on every session this
|
|
39
|
+
* connection opens. Its keys win over {@link instructionsMeta}'s.
|
|
40
|
+
*/
|
|
41
|
+
sessionMeta?: Readonly<Record<string, unknown>>;
|
|
32
42
|
}
|
|
43
|
+
/** @public See {@link OpenAcpConnectionOptions.instructionsMeta}. */
|
|
44
|
+
export type InstructionsMeta = (instructions: string) => Record<string, unknown>;
|
|
33
45
|
/**
|
|
34
46
|
* The spec requires clients to restrict prompt content to what the agent
|
|
35
47
|
* advertised, so an unsupported block is a configuration error raised here
|
|
36
48
|
* rather than an opaque failure on the wire.
|
|
37
49
|
*/
|
|
38
50
|
export declare function assertPromptContentSupported(agentId: string, content: ReadonlyArray<AcpContentBlock>, capabilities: acp.PromptCapabilities | undefined): void;
|
|
51
|
+
/** JSON with object keys sorted, so two spellings of one value compare equal. */
|
|
52
|
+
export declare function canonicalJson(value: unknown): string;
|
|
53
|
+
/**
|
|
54
|
+
* The child session an update announces: the subagents RFD's
|
|
55
|
+
* `subagent_update`, or codex-acp's earlier draft `subagent_spawned`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function announcedChild(update: AcpSessionNotification['update']): string | undefined;
|
|
39
58
|
/**
|
|
40
59
|
* Open a transport, run the `initialize` handshake, and return the negotiated
|
|
41
60
|
* connection.
|
package/dist/connection.js
CHANGED
|
@@ -22,6 +22,10 @@ import { KEEPER_METHODS } from './keeper/relay.js';
|
|
|
22
22
|
import { watchTransport } from './liveness.js';
|
|
23
23
|
import { AcpTurnAccumulator } from './turn.js';
|
|
24
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;
|
|
25
29
|
//#endregion
|
|
26
30
|
//#region Content capability gating
|
|
27
31
|
/**
|
|
@@ -81,6 +85,59 @@ const KeeperTurnSchema = z.discriminatedUnion('state', [
|
|
|
81
85
|
function isRecord(value) {
|
|
82
86
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
83
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
|
+
}
|
|
84
141
|
/** JSON-RPC's "method not found". */
|
|
85
142
|
const METHOD_NOT_FOUND = -32601;
|
|
86
143
|
function isMethodNotFound(error) {
|
|
@@ -97,6 +154,7 @@ class AcpSessionImpl {
|
|
|
97
154
|
commands;
|
|
98
155
|
death;
|
|
99
156
|
promptCapabilities;
|
|
157
|
+
fingerprint;
|
|
100
158
|
/** Set for the duration of a turn; notifications fan out to it. */
|
|
101
159
|
activeTurn;
|
|
102
160
|
/**
|
|
@@ -112,7 +170,9 @@ class AcpSessionImpl {
|
|
|
112
170
|
/** Rejects if the agent dies, so a request cannot outlive it. */
|
|
113
171
|
death,
|
|
114
172
|
/** What the agent said it accepts, enforced before a turn goes on the wire. */
|
|
115
|
-
promptCapabilities
|
|
173
|
+
promptCapabilities,
|
|
174
|
+
/** What the session was opened with; undefined on a reattached session. */
|
|
175
|
+
fingerprint) {
|
|
116
176
|
this.agentId = agentId;
|
|
117
177
|
this.agent = agent;
|
|
118
178
|
this.sessionId = sessionId;
|
|
@@ -121,6 +181,7 @@ class AcpSessionImpl {
|
|
|
121
181
|
this.commands = commands;
|
|
122
182
|
this.death = death;
|
|
123
183
|
this.promptCapabilities = promptCapabilities;
|
|
184
|
+
this.fingerprint = fingerprint;
|
|
124
185
|
}
|
|
125
186
|
get modes() {
|
|
126
187
|
return this.modeState;
|
|
@@ -237,10 +298,14 @@ class AcpSessionImpl {
|
|
|
237
298
|
if (!response) {
|
|
238
299
|
return undefined;
|
|
239
300
|
}
|
|
240
|
-
|
|
301
|
+
const result = accumulator.result({
|
|
241
302
|
stopReason: response.stopReason,
|
|
242
303
|
reportedUsage: response.usage ?? undefined,
|
|
243
304
|
});
|
|
305
|
+
return {
|
|
306
|
+
...result,
|
|
307
|
+
currentModeId: result.currentModeId ?? this.modeState?.currentModeId,
|
|
308
|
+
};
|
|
244
309
|
}
|
|
245
310
|
finally {
|
|
246
311
|
opts.signal?.removeEventListener('abort', onAbort);
|
|
@@ -340,6 +405,12 @@ class AcpAgentConnectionImpl {
|
|
|
340
405
|
tools;
|
|
341
406
|
reattach;
|
|
342
407
|
sessions = new Map();
|
|
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 = {};
|
|
343
414
|
constructor(agentId, agent, transport, client, agentCapabilities, authMethods, protocolVersion, watch, tools, reattach, reattached) {
|
|
344
415
|
this.agentId = agentId;
|
|
345
416
|
this.agent = agent;
|
|
@@ -354,6 +425,9 @@ class AcpAgentConnectionImpl {
|
|
|
354
425
|
if (reattached) {
|
|
355
426
|
this.attachSession = (sessionId) => this.attachTo(sessionId);
|
|
356
427
|
}
|
|
428
|
+
if (transport.detach) {
|
|
429
|
+
this.detach = () => this.letGo();
|
|
430
|
+
}
|
|
357
431
|
}
|
|
358
432
|
/** Set on a reattached connection; see `AcpAgentConnection.attachSession`. */
|
|
359
433
|
attachSession;
|
|
@@ -372,12 +446,48 @@ class AcpAgentConnectionImpl {
|
|
|
372
446
|
throw new AcpConnectError({
|
|
373
447
|
agentId: this.agentId,
|
|
374
448
|
message: `ACP agent '${this.agentId}' no longer has session ${sessionId}.`,
|
|
449
|
+
gone: true,
|
|
375
450
|
});
|
|
376
451
|
}
|
|
377
452
|
const session = new AcpSessionImpl(this.agentId, this.agent, sessionId, info.modes, info.configOptions, [], this.watch.death, this.agentCapabilities.promptCapabilities);
|
|
378
453
|
session.enableResume();
|
|
379
454
|
return this.track(session);
|
|
380
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);
|
|
490
|
+
}
|
|
381
491
|
/** The sink the client hands every `session/update` notification. */
|
|
382
492
|
get sink() {
|
|
383
493
|
return (notification) => {
|
|
@@ -392,15 +502,25 @@ class AcpAgentConnectionImpl {
|
|
|
392
502
|
this.watch.death,
|
|
393
503
|
]);
|
|
394
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
|
+
}
|
|
395
514
|
async newSession(opts) {
|
|
396
515
|
const response = await Promise.race([
|
|
397
516
|
this.agent.newSession({
|
|
398
517
|
cwd: opts.cwd,
|
|
399
518
|
mcpServers: this.sessionMcpServers(opts.mcpServers),
|
|
519
|
+
...this.sessionFields(opts),
|
|
400
520
|
}),
|
|
401
521
|
this.watch.death,
|
|
402
522
|
]);
|
|
403
|
-
return this.track(new AcpSessionImpl(this.agentId, this.agent, response.sessionId, response.modes ?? undefined, response.configOptions ?? undefined, [], this.watch.death, this.agentCapabilities.promptCapabilities));
|
|
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)));
|
|
404
524
|
}
|
|
405
525
|
async loadSession(opts) {
|
|
406
526
|
if (this.agentCapabilities.loadSession !== true || !this.agent.loadSession) {
|
|
@@ -409,24 +529,44 @@ class AcpAgentConnectionImpl {
|
|
|
409
529
|
capability: 'loadSession',
|
|
410
530
|
});
|
|
411
531
|
}
|
|
532
|
+
const fields = this.sessionFields(opts);
|
|
412
533
|
// Register before the call: `session/load` replays the conversation through
|
|
413
534
|
// `session/update` notifications, which must not be dropped on the floor.
|
|
414
|
-
const session = this.track(new AcpSessionImpl(this.agentId, this.agent, opts.sessionId, undefined, undefined, [], this.watch.death, this.agentCapabilities.promptCapabilities));
|
|
535
|
+
const session = this.track(new AcpSessionImpl(this.agentId, this.agent, opts.sessionId, undefined, undefined, [], this.watch.death, this.agentCapabilities.promptCapabilities, this.fingerprintFor(opts)));
|
|
415
536
|
const response = await Promise.race([
|
|
416
537
|
this.agent.loadSession({
|
|
417
538
|
sessionId: opts.sessionId,
|
|
418
539
|
cwd: opts.cwd,
|
|
419
540
|
mcpServers: this.sessionMcpServers(opts.mcpServers),
|
|
541
|
+
...fields,
|
|
420
542
|
}),
|
|
421
543
|
this.watch.death,
|
|
422
|
-
])
|
|
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
|
+
});
|
|
423
549
|
if (response) {
|
|
424
550
|
session.applySettings(response);
|
|
425
551
|
}
|
|
426
552
|
return session;
|
|
427
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
|
+
}
|
|
428
566
|
async close() {
|
|
429
567
|
this.sessions.clear();
|
|
568
|
+
this.children.clear();
|
|
569
|
+
this.early = [];
|
|
430
570
|
// Reject anything still in flight first. Once the transport is down
|
|
431
571
|
// nothing can answer it, so a pending request would hang forever.
|
|
432
572
|
this.watch.shutdown();
|
|
@@ -439,8 +579,36 @@ class AcpAgentConnectionImpl {
|
|
|
439
579
|
//#region internals
|
|
440
580
|
track(session) {
|
|
441
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
|
+
}
|
|
442
591
|
return session;
|
|
443
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
|
+
}
|
|
444
612
|
/**
|
|
445
613
|
* The servers `session/new` carries: the user's plus the tool bridge, or
|
|
446
614
|
* none when the agent was given them all at launch.
|
|
@@ -459,6 +627,57 @@ class AcpAgentConnectionImpl {
|
|
|
459
627
|
: []),
|
|
460
628
|
]);
|
|
461
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
|
+
}
|
|
462
681
|
/** Reject HTTP/SSE MCP transports the agent never advertised support for. */
|
|
463
682
|
checkedMcpServers(servers) {
|
|
464
683
|
if (!servers || servers.length === 0) {
|
|
@@ -501,6 +720,7 @@ export async function openAcpConnection(opts) {
|
|
|
501
720
|
// policy and event sink to whichever step opened the connection, silently
|
|
502
721
|
// ignoring the configuration of every step that later reuses the session.
|
|
503
722
|
let sink;
|
|
723
|
+
let connection;
|
|
504
724
|
const host = opts.host;
|
|
505
725
|
const client = new NoeticAcpClient({
|
|
506
726
|
host,
|
|
@@ -511,8 +731,11 @@ export async function openAcpConnection(opts) {
|
|
|
511
731
|
// so without this a dead agent leaves every request hanging forever.
|
|
512
732
|
const watch = watchTransport(opts.agentId, opts.transport, opts.signal);
|
|
513
733
|
const notify = (notification) => {
|
|
514
|
-
|
|
515
|
-
|
|
734
|
+
// Only this connection's own sessions produce the step's output.
|
|
735
|
+
if (!connection) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
connection.route(notification);
|
|
516
739
|
};
|
|
517
740
|
const agent = new ClientSideConnection(() => client, ndJsonStream(opts.transport.writable, divertNewerUpdates(watch.readable, notify)));
|
|
518
741
|
let initialized;
|
|
@@ -527,15 +750,29 @@ export async function openAcpConnection(opts) {
|
|
|
527
750
|
}
|
|
528
751
|
catch (error) {
|
|
529
752
|
watch.shutdown('failed the initialize handshake');
|
|
530
|
-
|
|
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);
|
|
531
758
|
throw new AcpConnectError({
|
|
532
759
|
agentId: opts.agentId,
|
|
533
760
|
message: `ACP agent '${opts.agentId}' failed the initialize handshake.`,
|
|
534
761
|
cause: error,
|
|
535
762
|
});
|
|
536
763
|
}
|
|
537
|
-
|
|
764
|
+
connection = new AcpAgentConnectionImpl(opts.agentId, agent, opts.transport, client, initialized.agentCapabilities ?? {}, initialized.authMethods ?? [], initialized.protocolVersion, watch, opts.tools, opts.reattach, opts.reattached === true);
|
|
538
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
|
+
};
|
|
539
776
|
return connection;
|
|
540
777
|
}
|
|
541
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;
|
package/dist/define.js
CHANGED
|
@@ -48,6 +48,8 @@ export function defineAcpAgent(def) {
|
|
|
48
48
|
host: opts.host,
|
|
49
49
|
signal,
|
|
50
50
|
tools: bridge,
|
|
51
|
+
instructionsMeta: def.instructionsMeta,
|
|
52
|
+
sessionMeta: def.sessionMeta,
|
|
51
53
|
reattach: reattachHandle(transport.reattach, bridge),
|
|
52
54
|
});
|
|
53
55
|
}
|
|
@@ -136,9 +138,15 @@ function lazyLocalMachine(agentId) {
|
|
|
136
138
|
}
|
|
137
139
|
/** Closing the agent's stream releases the machine it runs on. */
|
|
138
140
|
function closingWith(transport, opened) {
|
|
141
|
+
const detach = transport.detach?.bind(transport);
|
|
139
142
|
return {
|
|
140
143
|
readable: transport.readable,
|
|
141
144
|
writable: transport.writable,
|
|
145
|
+
...(transport.reattach
|
|
146
|
+
? {
|
|
147
|
+
reattach: transport.reattach,
|
|
148
|
+
}
|
|
149
|
+
: {}),
|
|
142
150
|
async close() {
|
|
143
151
|
try {
|
|
144
152
|
await transport.close();
|
|
@@ -147,6 +155,12 @@ function closingWith(transport, opened) {
|
|
|
147
155
|
await opened.close();
|
|
148
156
|
}
|
|
149
157
|
},
|
|
158
|
+
// Letting go keeps the machine: it runs the agent a later host reattaches to.
|
|
159
|
+
...(detach
|
|
160
|
+
? {
|
|
161
|
+
detach,
|
|
162
|
+
}
|
|
163
|
+
: {}),
|
|
150
164
|
};
|
|
151
165
|
}
|
|
152
166
|
/**
|
|
@@ -168,6 +182,7 @@ async function reattachTo(def, opts, handle) {
|
|
|
168
182
|
whenHostExits,
|
|
169
183
|
});
|
|
170
184
|
let bridge;
|
|
185
|
+
let attached;
|
|
171
186
|
try {
|
|
172
187
|
if (!machine.attach) {
|
|
173
188
|
throw new AcpConnectError({
|
|
@@ -176,6 +191,7 @@ async function reattachTo(def, opts, handle) {
|
|
|
176
191
|
});
|
|
177
192
|
}
|
|
178
193
|
const transport = await machine.attach(handle);
|
|
194
|
+
attached = transport;
|
|
179
195
|
bridge = await openBridge(def, opts, {
|
|
180
196
|
reach: (address) => machine.reach(address),
|
|
181
197
|
reuse: bridgeOf(handle),
|
|
@@ -190,16 +206,30 @@ async function reattachTo(def, opts, handle) {
|
|
|
190
206
|
host: opts.host,
|
|
191
207
|
signal,
|
|
192
208
|
tools: bridge,
|
|
209
|
+
instructionsMeta: def.instructionsMeta,
|
|
210
|
+
sessionMeta: def.sessionMeta,
|
|
193
211
|
reattach: reattachHandle(transport.reattach ?? handle, bridge),
|
|
194
212
|
reattached: true,
|
|
195
213
|
});
|
|
196
214
|
}
|
|
197
215
|
catch (e) {
|
|
198
216
|
await bridge?.close();
|
|
199
|
-
await
|
|
217
|
+
await letGoOf(attached, machine);
|
|
200
218
|
throw e;
|
|
201
219
|
}
|
|
202
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* A reattach that failed before this host adopted the agent: let go of it, so
|
|
223
|
+
* it keeps running for a host that can — never stop it. Only a machine that
|
|
224
|
+
* reached nothing is closed.
|
|
225
|
+
*/
|
|
226
|
+
async function letGoOf(attached, machine) {
|
|
227
|
+
if (attached?.detach) {
|
|
228
|
+
await attached.detach().catch(() => undefined);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
await machine.close().catch(() => undefined);
|
|
232
|
+
}
|
|
203
233
|
const BridgeCredentialsSchema = z.object({
|
|
204
234
|
path: z.string().startsWith('/'),
|
|
205
235
|
token: z.string().min(1),
|