@noetic-tools/acp 2.0.0-next.1 → 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 +1 -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 +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-types.d.ts +1 -1
- 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
package/dist/stdio.js
CHANGED
|
@@ -10,11 +10,17 @@
|
|
|
10
10
|
import { spawn } from 'node:child_process';
|
|
11
11
|
import { Readable, Writable } from 'node:stream';
|
|
12
12
|
import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
|
|
13
|
+
import { attachKeeper, keeperHandleOf, startKeeper } from './keeper/client.js';
|
|
13
14
|
import { serverEnv } from './server-env.js';
|
|
15
|
+
import { stopProcessTree, stopWithHost } from './stdio-process.js';
|
|
16
|
+
export { stopProcessTree, TERM_GRACE_MS } from './stdio-process.js';
|
|
14
17
|
/**
|
|
15
18
|
* Build a transport factory that spawns `spec.command` and speaks ACP over its
|
|
16
19
|
* stdin/stdout. The child's stderr is inherited so agent diagnostics reach the
|
|
17
|
-
* host's logs instead of vanishing.
|
|
20
|
+
* host's logs instead of vanishing. Closing the transport (or aborting its
|
|
21
|
+
* signal) closes the pipes and stops the agent's whole process group —
|
|
22
|
+
* SIGTERM, a grace period, then SIGKILL; on Windows, its process tree. Close
|
|
23
|
+
* is idempotent: every call shares one shutdown.
|
|
18
24
|
* @public
|
|
19
25
|
*/
|
|
20
26
|
export function stdioAcpTransport(spec) {
|
|
@@ -27,44 +33,121 @@ export function stdioAcpTransport(spec) {
|
|
|
27
33
|
...opts.env,
|
|
28
34
|
},
|
|
29
35
|
cwd: opts.cwd,
|
|
30
|
-
},
|
|
36
|
+
}, {
|
|
37
|
+
signal: opts.signal,
|
|
38
|
+
whenHostExits: opts.whenHostExits,
|
|
39
|
+
});
|
|
31
40
|
}
|
|
32
41
|
/**
|
|
33
|
-
* This machine: the
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
42
|
+
* This machine: the agent runs through a keeper (see
|
|
43
|
+
* {@link LocalMachineOptions.keeper}), which also serves the tool bridge to it
|
|
44
|
+
* on a port that outlives the host; an MCP server beside it is a child
|
|
45
|
+
* process. The agent gets this process's environment; an MCP server, as the
|
|
46
|
+
* MCP SDK starts one, only `serverEnv`'s short list.
|
|
37
47
|
* @public
|
|
38
48
|
*/
|
|
39
|
-
export function localMachine(agentId = 'local') {
|
|
40
|
-
return async ({ signal }) => {
|
|
49
|
+
export function localMachine(agentId = 'local', options = {}) {
|
|
50
|
+
return async ({ signal, whenHostExits = 'keep' }) => {
|
|
41
51
|
const started = new Set();
|
|
52
|
+
const useKeeper = options.keeper !== false;
|
|
53
|
+
let keeper;
|
|
54
|
+
const keeperOptions = {
|
|
55
|
+
whenHostExits,
|
|
56
|
+
detachedTimeoutMs: options.detachedTimeoutMs ?? 0,
|
|
57
|
+
};
|
|
58
|
+
const openKeeper = () => {
|
|
59
|
+
keeper ??= useKeeper ? startKeeper(agentId, keeperOptions) : Promise.resolve(undefined);
|
|
60
|
+
return keeper;
|
|
61
|
+
};
|
|
62
|
+
signal?.addEventListener('abort', () => {
|
|
63
|
+
void keeper?.then((link) => link?.stop());
|
|
64
|
+
}, {
|
|
65
|
+
once: true,
|
|
66
|
+
});
|
|
42
67
|
const start = async (command, env) => {
|
|
43
68
|
const transport = await spawnStdio(agentId, {
|
|
44
69
|
...command,
|
|
45
70
|
env,
|
|
46
|
-
},
|
|
71
|
+
}, {
|
|
72
|
+
signal,
|
|
73
|
+
whenHostExits,
|
|
74
|
+
});
|
|
47
75
|
started.add(transport);
|
|
48
76
|
return transport;
|
|
49
77
|
};
|
|
50
78
|
return {
|
|
51
|
-
reach: async (bridge) =>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
reach: async (bridge) => {
|
|
80
|
+
const link = await openKeeper();
|
|
81
|
+
if (!link) {
|
|
82
|
+
return bridge;
|
|
83
|
+
}
|
|
84
|
+
const url = await link.reach(new URL(bridge.url).pathname, bridge.url);
|
|
85
|
+
return {
|
|
86
|
+
...bridge,
|
|
87
|
+
url,
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
launch: async (command) => {
|
|
91
|
+
const env = {
|
|
92
|
+
...process.env,
|
|
93
|
+
...command.env,
|
|
94
|
+
};
|
|
95
|
+
const link = await openKeeper();
|
|
96
|
+
if (!link) {
|
|
97
|
+
return await start(command, env);
|
|
98
|
+
}
|
|
99
|
+
const transport = await link.launch({
|
|
100
|
+
...command,
|
|
101
|
+
env,
|
|
102
|
+
});
|
|
103
|
+
started.add(transport);
|
|
104
|
+
return transport;
|
|
105
|
+
},
|
|
106
|
+
attach: async (handle) => {
|
|
107
|
+
const named = keeperHandleOf(handle);
|
|
108
|
+
if (!named) {
|
|
109
|
+
throw new AcpConnectError({
|
|
110
|
+
agentId,
|
|
111
|
+
message: `ACP agent '${agentId}' cannot be reattached: its handle names no keeper.`,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const link = attachKeeper(agentId, named, whenHostExits);
|
|
115
|
+
keeper = link;
|
|
116
|
+
const transport = (await link).transport();
|
|
117
|
+
started.add(transport);
|
|
118
|
+
return transport;
|
|
119
|
+
},
|
|
56
120
|
spawn: (command) => start(command, serverEnv(command.env)),
|
|
57
121
|
async close() {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
122
|
+
// In parallel: each stop may take its whole grace period.
|
|
123
|
+
const closing = [
|
|
124
|
+
...started,
|
|
125
|
+
].map((transport) => transport.close().catch(() => undefined));
|
|
61
126
|
started.clear();
|
|
127
|
+
closing.push((keeper ?? Promise.resolve(undefined))
|
|
128
|
+
.then((link) => link?.stop())
|
|
129
|
+
.catch(() => undefined));
|
|
130
|
+
await Promise.all(closing);
|
|
62
131
|
},
|
|
63
132
|
};
|
|
64
133
|
};
|
|
65
134
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Spawn `command` with exactly its `env` and carry its stdin/stdout as a byte
|
|
137
|
+
* stream. The child leads its own process group (POSIX), so closing or
|
|
138
|
+
* aborting stops everything it started — a wrapper's native grandchild
|
|
139
|
+
* included — and releases the pipes; see `stopProcessTree`. A missing binary
|
|
140
|
+
* or a signal that is already aborted rejects here, not at `close()`.
|
|
141
|
+
*/
|
|
142
|
+
async function spawnStdio(agentId, command, { signal, whenHostExits = 'keep', }) {
|
|
143
|
+
const fail = (message, cause) => new AcpConnectError({
|
|
144
|
+
agentId,
|
|
145
|
+
message: `ACP agent '${agentId}' (${command.command}) ${message}.`,
|
|
146
|
+
cause,
|
|
147
|
+
});
|
|
148
|
+
if (signal?.aborted) {
|
|
149
|
+
throw fail('was not started: the launch was already aborted', signal.reason);
|
|
150
|
+
}
|
|
68
151
|
const child = spawn(command.command, [
|
|
69
152
|
...(command.args ?? []),
|
|
70
153
|
], {
|
|
@@ -75,50 +158,61 @@ async function spawnStdio(agentId, command, signal) {
|
|
|
75
158
|
'pipe',
|
|
76
159
|
'inherit',
|
|
77
160
|
],
|
|
161
|
+
// Its own process group, so shutdown can signal the whole tree without
|
|
162
|
+
// ever signalling the host's group. Windows stops the tree by pid.
|
|
163
|
+
detached: process.platform !== 'win32',
|
|
164
|
+
windowsHide: true,
|
|
78
165
|
});
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
166
|
+
const forgetOnExit = whenHostExits === 'stop' ? stopWithHost(child) : () => undefined;
|
|
167
|
+
let closing;
|
|
168
|
+
const close = () => {
|
|
169
|
+
signal?.removeEventListener('abort', onAbort);
|
|
170
|
+
forgetOnExit();
|
|
171
|
+
closing ??= stopProcessTree(child).catch((error) => {
|
|
172
|
+
throw fail('could not be stopped', error);
|
|
86
173
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// returned. Capture it so the failure is reported against the agent rather
|
|
90
|
-
// than escaping as an unhandled 'error' event.
|
|
91
|
-
let spawnError;
|
|
92
|
-
child.once('error', (error) => {
|
|
93
|
-
spawnError = error;
|
|
94
|
-
});
|
|
174
|
+
return closing;
|
|
175
|
+
};
|
|
95
176
|
const onAbort = () => {
|
|
96
|
-
|
|
177
|
+
// An explicit close() still observes a failure; this only keeps it handled.
|
|
178
|
+
void close().catch(() => undefined);
|
|
97
179
|
};
|
|
98
180
|
signal?.addEventListener('abort', onAbort, {
|
|
99
181
|
once: true,
|
|
100
182
|
});
|
|
101
|
-
|
|
183
|
+
try {
|
|
184
|
+
await new Promise((resolve, reject) => {
|
|
185
|
+
child.once('spawn', resolve);
|
|
186
|
+
child.once('error', reject);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
await close().catch(() => undefined);
|
|
191
|
+
throw fail('failed to start', error);
|
|
192
|
+
}
|
|
193
|
+
// A late error (a broken pipe after spawn) must not escape as an unhandled
|
|
194
|
+
// 'error' event; the stream ending reports it to the connection.
|
|
195
|
+
child.on('error', () => undefined);
|
|
196
|
+
// Shutdown destroys our ends of the pipes, possibly mid-read; the web
|
|
197
|
+
// streams report that to their reader, so the Node-side error is handled.
|
|
198
|
+
child.stdin?.on('error', () => undefined);
|
|
199
|
+
child.stdout?.on('error', () => undefined);
|
|
200
|
+
if (signal?.aborted) {
|
|
201
|
+
await close().catch(() => undefined);
|
|
202
|
+
throw fail('was aborted while starting', signal.reason);
|
|
203
|
+
}
|
|
204
|
+
const stdin = child.stdin;
|
|
205
|
+
const stdout = child.stdout;
|
|
206
|
+
if (!stdin || !stdout) {
|
|
207
|
+
await close().catch(() => undefined);
|
|
208
|
+
throw fail('was spawned without usable stdio pipes', undefined);
|
|
209
|
+
}
|
|
102
210
|
return {
|
|
103
211
|
// `node:stream`'s web-stream types are structurally the same as the
|
|
104
212
|
// global ones but nominally distinct, so the bridge is cast once here
|
|
105
213
|
// rather than leaking a Node-specific type through the transport contract.
|
|
106
214
|
readable: frameworkCast(Readable.toWeb(stdout)),
|
|
107
215
|
writable: frameworkCast(Writable.toWeb(stdin)),
|
|
108
|
-
|
|
109
|
-
if (closed) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
closed = true;
|
|
113
|
-
signal?.removeEventListener('abort', onAbort);
|
|
114
|
-
child.kill();
|
|
115
|
-
if (spawnError) {
|
|
116
|
-
throw new AcpConnectError({
|
|
117
|
-
agentId,
|
|
118
|
-
message: `ACP agent '${agentId}' (${command.command}) failed to start.`,
|
|
119
|
-
cause: spawnError,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
},
|
|
216
|
+
close,
|
|
123
217
|
};
|
|
124
218
|
}
|
package/dist/tool-bridge.d.ts
CHANGED
|
@@ -47,8 +47,15 @@ export interface ToolBridge {
|
|
|
47
47
|
readonly server: AcpMcpServer;
|
|
48
48
|
readonly env: Record<string, string>;
|
|
49
49
|
readonly permits?: BridgedPermissionCheck;
|
|
50
|
+
/** What a later host needs to serve this bridge to the same agent. */
|
|
51
|
+
readonly credentials: ToolBridgeCredentials;
|
|
50
52
|
close(): Promise<void>;
|
|
51
53
|
}
|
|
54
|
+
/** The path and token a bridge serves under. */
|
|
55
|
+
export interface ToolBridgeCredentials {
|
|
56
|
+
readonly path: string;
|
|
57
|
+
readonly token: string;
|
|
58
|
+
}
|
|
52
59
|
/**
|
|
53
60
|
* Start the loopback MCP server, carry it to the agent's machine, and work
|
|
54
61
|
* out the agent's launch settings from the address it reaches there. The
|
|
@@ -64,4 +71,6 @@ export declare function startToolBridge(opts: {
|
|
|
64
71
|
env?: Record<string, string | undefined>;
|
|
65
72
|
/** Carries the bridge to the agent's machine; this machine's loopback by default. */
|
|
66
73
|
reach?: (bridge: ToolBridgeAddress) => Promise<ToolBridgeAddress>;
|
|
74
|
+
/** Serve under the path and token an earlier host gave a reattached agent. */
|
|
75
|
+
reuse?: ToolBridgeCredentials;
|
|
67
76
|
}): Promise<ToolBridge>;
|
package/dist/tool-bridge.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
export async function startToolBridge(opts) {
|
|
19
19
|
const { serveToolsOverHttp } = await import('./mcp/node.js');
|
|
20
|
-
const http = await serveToolsOverHttp(opts.getHost);
|
|
20
|
+
const http = await serveToolsOverHttp(opts.getHost, opts.reuse);
|
|
21
21
|
const local = {
|
|
22
22
|
serverName: opts.serverName,
|
|
23
23
|
url: http.url,
|
|
@@ -56,6 +56,10 @@ export async function startToolBridge(opts) {
|
|
|
56
56
|
},
|
|
57
57
|
env,
|
|
58
58
|
permits: opts.wiring.permits,
|
|
59
|
+
credentials: {
|
|
60
|
+
path: new URL(http.url).pathname,
|
|
61
|
+
token: http.token,
|
|
62
|
+
},
|
|
59
63
|
close: () => http.close(),
|
|
60
64
|
};
|
|
61
65
|
}
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* both directions — capability negotiation, session updates, permission
|
|
7
7
|
* round-trips, `fs/*` and `terminal/*` callbacks — without spawning a process.
|
|
8
8
|
*/
|
|
9
|
+
import type * as acp from '@agentclientprotocol/sdk';
|
|
10
|
+
import { AgentSideConnection } from '@agentclientprotocol/sdk';
|
|
9
11
|
import type { AcpMachineFactory, AcpTransport, AcpTransportFactory, SpawnCommand } from '@noetic-tools/types';
|
|
10
|
-
import type * as acp from '@zed-industries/agent-client-protocol';
|
|
11
|
-
import { AgentSideConnection } from '@zed-industries/agent-client-protocol';
|
|
12
12
|
/** @public The two ends of a loopback connection. */
|
|
13
13
|
export interface AcpLoopbackPair {
|
|
14
14
|
/** Give this to `openAcpConnection`. */
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* both directions — capability negotiation, session updates, permission
|
|
7
7
|
* round-trips, `fs/*` and `terminal/*` callbacks — without spawning a process.
|
|
8
8
|
*/
|
|
9
|
+
import { AgentSideConnection, ndJsonStream } from '@agentclientprotocol/sdk';
|
|
9
10
|
import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
|
|
10
|
-
import { AgentSideConnection, ndJsonStream } from '@zed-industries/agent-client-protocol';
|
|
11
11
|
function endpoint(readable, writable) {
|
|
12
12
|
let closed = false;
|
|
13
13
|
return {
|
package/dist/turn.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* {@link AcpTurnResult}: Noetic conversation items, the assistant text, the
|
|
4
4
|
* agent's plan, its slash commands, and its final mode.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Transcript updates are folded here; every update also reaches the host. See `user_message_chunk`
|
|
7
7
|
* below for the one whose payload is deliberately not turned into an item.
|
|
8
8
|
*/
|
|
9
9
|
import type { AcpPlanEntry, AcpSessionNotification, AcpStopReason, AcpToolCallContent, AcpTurnResult, TokenUsage } from '@noetic-tools/types';
|
|
@@ -12,7 +12,7 @@ import type { AcpPlanEntry, AcpSessionNotification, AcpStopReason, AcpToolCallCo
|
|
|
12
12
|
* carries. Diff and terminal content are rendered structurally rather than
|
|
13
13
|
* discarded, so the transcript keeps what the agent actually did.
|
|
14
14
|
*/
|
|
15
|
-
export declare function renderToolCallContent(content: ReadonlyArray<AcpToolCallContent>, rawOutput?:
|
|
15
|
+
export declare function renderToolCallContent(content: ReadonlyArray<AcpToolCallContent>, rawOutput?: unknown): string;
|
|
16
16
|
/** @public Folds a turn's session updates into a result. */
|
|
17
17
|
export declare class AcpTurnAccumulator {
|
|
18
18
|
private reasoning;
|
|
@@ -36,6 +36,7 @@ export declare class AcpTurnAccumulator {
|
|
|
36
36
|
stopReason: AcpStopReason;
|
|
37
37
|
usage?: TokenUsage;
|
|
38
38
|
cost?: number;
|
|
39
|
+
reportedUsage?: AcpTurnResult['reportedUsage'];
|
|
39
40
|
}): AcpTurnResult;
|
|
40
41
|
/**
|
|
41
42
|
* The agent's last message — the text after its final tool call — as a
|
package/dist/turn.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* {@link AcpTurnResult}: Noetic conversation items, the assistant text, the
|
|
4
4
|
* agent's plan, its slash commands, and its final mode.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Transcript updates are folded here; every update also reaches the host. See `user_message_chunk`
|
|
7
7
|
* below for the one whose payload is deliberately not turned into an item.
|
|
8
8
|
*/
|
|
9
9
|
import { assistantMessageItem, contentBlockText, functionCallItem, functionCallOutputItem, } from './items.js';
|
|
@@ -100,6 +100,7 @@ export class AcpTurnAccumulator {
|
|
|
100
100
|
text: this.finalText(),
|
|
101
101
|
usage: opts.usage,
|
|
102
102
|
cost: opts.cost,
|
|
103
|
+
reportedUsage: opts.reportedUsage,
|
|
103
104
|
plan: this.plan,
|
|
104
105
|
availableCommands: this.availableCommands,
|
|
105
106
|
currentModeId: this.currentModeId,
|
|
@@ -157,13 +158,32 @@ export class AcpTurnAccumulator {
|
|
|
157
158
|
];
|
|
158
159
|
return;
|
|
159
160
|
}
|
|
161
|
+
if (update.sessionUpdate === 'plan_update') {
|
|
162
|
+
// The draft plan protocol: an item plan replaces the snapshot; a file or
|
|
163
|
+
// markdown plan has no entries to fold, and stays on the event stream.
|
|
164
|
+
if (update.plan.type === 'items') {
|
|
165
|
+
this.plan = [
|
|
166
|
+
...update.plan.entries,
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (update.sessionUpdate === 'plan_removed') {
|
|
172
|
+
this.plan = [];
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
160
175
|
if (update.sessionUpdate === 'available_commands_update') {
|
|
161
176
|
this.availableCommands = [
|
|
162
177
|
...update.availableCommands,
|
|
163
178
|
];
|
|
164
179
|
return;
|
|
165
180
|
}
|
|
166
|
-
|
|
181
|
+
if (update.sessionUpdate === 'current_mode_update') {
|
|
182
|
+
this.currentModeId = update.currentModeId;
|
|
183
|
+
}
|
|
184
|
+
// Every other update (`usage_update`, `config_option_update`,
|
|
185
|
+
// `session_info_update`, …) carries no transcript content; it still reaches
|
|
186
|
+
// the host through `onSessionUpdate`.
|
|
167
187
|
}
|
|
168
188
|
trackToolCall(update) {
|
|
169
189
|
if (!this.toolCalls.has(update.toolCallId)) {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session updates newer than the protocol library.
|
|
3
|
+
*
|
|
4
|
+
* The SDK validates every `session/update` notification against the variants
|
|
5
|
+
* it knows and answers any other with `-32602 Invalid params`, dropping it. An
|
|
6
|
+
* agent built on a newer protocol revision would then lose data and log an
|
|
7
|
+
* error for normal traffic. This filter sits on the agent's byte stream before
|
|
8
|
+
* the SDK: a `session/update` whose variant the SDK does not know is handed to
|
|
9
|
+
* the host as it is and kept from the SDK. Every other line passes through
|
|
10
|
+
* byte-identical.
|
|
11
|
+
*/
|
|
12
|
+
import type { AcpSessionNotification } from '@noetic-tools/types';
|
|
13
|
+
/**
|
|
14
|
+
* The notification when `line` is a `session/update` with a variant the SDK
|
|
15
|
+
* does not know; otherwise undefined.
|
|
16
|
+
*/
|
|
17
|
+
export declare function newerSessionUpdate(line: string): AcpSessionNotification | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Pass the agent's stream through, handing `deliver` each `session/update`
|
|
20
|
+
* the SDK would reject for its variant instead of passing it on.
|
|
21
|
+
*/
|
|
22
|
+
export declare function divertNewerUpdates(readable: ReadableStream<Uint8Array>, deliver: (notification: AcpSessionNotification) => void): ReadableStream<Uint8Array>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session updates newer than the protocol library.
|
|
3
|
+
*
|
|
4
|
+
* The SDK validates every `session/update` notification against the variants
|
|
5
|
+
* it knows and answers any other with `-32602 Invalid params`, dropping it. An
|
|
6
|
+
* agent built on a newer protocol revision would then lose data and log an
|
|
7
|
+
* error for normal traffic. This filter sits on the agent's byte stream before
|
|
8
|
+
* the SDK: a `session/update` whose variant the SDK does not know is handed to
|
|
9
|
+
* the host as it is and kept from the SDK. Every other line passes through
|
|
10
|
+
* byte-identical.
|
|
11
|
+
*/
|
|
12
|
+
import { frameworkCast } from '@noetic-tools/types';
|
|
13
|
+
/**
|
|
14
|
+
* Every variant the pinned SDK parses. A `Record` over the union, so a library
|
|
15
|
+
* bump that adds one fails to compile until it is listed here.
|
|
16
|
+
*/
|
|
17
|
+
const KNOWN = {
|
|
18
|
+
user_message_chunk: true,
|
|
19
|
+
agent_message_chunk: true,
|
|
20
|
+
agent_thought_chunk: true,
|
|
21
|
+
tool_call: true,
|
|
22
|
+
tool_call_update: true,
|
|
23
|
+
plan: true,
|
|
24
|
+
plan_update: true,
|
|
25
|
+
plan_removed: true,
|
|
26
|
+
available_commands_update: true,
|
|
27
|
+
current_mode_update: true,
|
|
28
|
+
config_option_update: true,
|
|
29
|
+
session_info_update: true,
|
|
30
|
+
usage_update: true,
|
|
31
|
+
notice: true,
|
|
32
|
+
compaction_update: true,
|
|
33
|
+
compaction_summary_chunk: true,
|
|
34
|
+
subagent_update: true,
|
|
35
|
+
session_message: true,
|
|
36
|
+
session_message_chunk: true,
|
|
37
|
+
};
|
|
38
|
+
function isKnown(kind) {
|
|
39
|
+
return Object.hasOwn(KNOWN, kind);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region Helpers
|
|
43
|
+
function isRecord(value) {
|
|
44
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The notification when `line` is a `session/update` with a variant the SDK
|
|
48
|
+
* does not know; otherwise undefined.
|
|
49
|
+
*/
|
|
50
|
+
export function newerSessionUpdate(line) {
|
|
51
|
+
if (!line.includes('"session/update"')) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
let message;
|
|
55
|
+
try {
|
|
56
|
+
message = JSON.parse(line);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (!isRecord(message) || message.method !== 'session/update' || 'id' in message) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const params = message.params;
|
|
65
|
+
if (!isRecord(params) || typeof params.sessionId !== 'string' || !isRecord(params.update)) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const kind = params.update.sessionUpdate;
|
|
69
|
+
if (typeof kind !== 'string' || isKnown(kind)) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
// A variant from a newer protocol revision: the host's handlers treat an
|
|
73
|
+
// unknown `sessionUpdate` as opaque (the event bridge's `acp.<name>` event).
|
|
74
|
+
return frameworkCast(params);
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region Public API
|
|
78
|
+
/**
|
|
79
|
+
* Pass the agent's stream through, handing `deliver` each `session/update`
|
|
80
|
+
* the SDK would reject for its variant instead of passing it on.
|
|
81
|
+
*/
|
|
82
|
+
export function divertNewerUpdates(readable, deliver) {
|
|
83
|
+
const decoder = new TextDecoder();
|
|
84
|
+
const encoder = new TextEncoder();
|
|
85
|
+
let carry = '';
|
|
86
|
+
const pass = (line, controller) => {
|
|
87
|
+
const newer = newerSessionUpdate(line);
|
|
88
|
+
if (newer) {
|
|
89
|
+
deliver(newer);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
controller.enqueue(encoder.encode(`${line}\n`));
|
|
93
|
+
};
|
|
94
|
+
return readable.pipeThrough(new TransformStream({
|
|
95
|
+
transform(chunk, controller) {
|
|
96
|
+
carry += decoder.decode(chunk, {
|
|
97
|
+
stream: true,
|
|
98
|
+
});
|
|
99
|
+
let newline = carry.indexOf('\n');
|
|
100
|
+
while (newline >= 0) {
|
|
101
|
+
pass(carry.slice(0, newline), controller);
|
|
102
|
+
carry = carry.slice(newline + 1);
|
|
103
|
+
newline = carry.indexOf('\n');
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
flush(controller) {
|
|
107
|
+
const tail = carry + decoder.decode();
|
|
108
|
+
if (tail.length > 0) {
|
|
109
|
+
const newer = newerSessionUpdate(tail);
|
|
110
|
+
if (newer) {
|
|
111
|
+
deliver(newer);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
controller.enqueue(encoder.encode(tail));
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/acp",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Both directions of the Agent Client Protocol for Noetic. Drives any ACP-speaking coding agent (Claude Code, Codex, Gemini CLI, …) as a step, and serves a Noetic harness as an ACP agent for editors like Zed (toAcpAgent / serveAcp).",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -64,8 +64,8 @@
|
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@modelcontextprotocol/node": "2.1.2",
|
|
66
66
|
"@modelcontextprotocol/server": "2.4.0",
|
|
67
|
-
"@noetic-tools/types": "^3.19.0-next.
|
|
68
|
-
"@
|
|
67
|
+
"@noetic-tools/types": "^3.19.0-next.4",
|
|
68
|
+
"@agentclientprotocol/sdk": "1.9.0",
|
|
69
69
|
"ws": "8.22.1",
|
|
70
70
|
"zod": "^4.0.0"
|
|
71
71
|
},
|