@noetic-tools/acp 1.3.1 → 1.4.0

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.
Files changed (51) hide show
  1. package/dist/agents.d.ts +72 -8
  2. package/dist/agents.js +162 -58
  3. package/dist/bin.d.ts +19 -0
  4. package/dist/bin.js +99 -0
  5. package/dist/client.d.ts +14 -0
  6. package/dist/client.js +49 -0
  7. package/dist/connection.d.ts +3 -0
  8. package/dist/connection.js +26 -4
  9. package/dist/define.d.ts +21 -7
  10. package/dist/define.js +139 -15
  11. package/dist/index.d.ts +13 -3
  12. package/dist/index.js +10 -2
  13. package/dist/mcp/node.d.ts +24 -0
  14. package/dist/mcp/node.js +146 -0
  15. package/dist/mcp/server.d.ts +16 -0
  16. package/dist/mcp/server.js +104 -0
  17. package/dist/pi/extension.d.ts +31 -0
  18. package/dist/pi/extension.js +223 -0
  19. package/dist/pi/launch.d.ts +11 -0
  20. package/dist/pi/launch.js +35 -0
  21. package/dist/remote.d.ts +52 -0
  22. package/dist/remote.js +108 -0
  23. package/dist/runner/grants.d.ts +47 -0
  24. package/dist/runner/grants.js +127 -0
  25. package/dist/runner/host.d.ts +21 -0
  26. package/dist/runner/host.js +318 -0
  27. package/dist/runner/options.d.ts +21 -0
  28. package/dist/runner/options.js +82 -0
  29. package/dist/runner/protocol.d.ts +101 -0
  30. package/dist/runner/protocol.js +218 -0
  31. package/dist/runner/serve.d.ts +60 -0
  32. package/dist/runner/serve.js +285 -0
  33. package/dist/runner/ws-link.d.ts +6 -0
  34. package/dist/runner/ws-link.js +47 -0
  35. package/dist/server-env.d.ts +10 -0
  36. package/dist/server-env.js +47 -0
  37. package/dist/spawn.d.ts +12 -0
  38. package/dist/spawn.js +37 -0
  39. package/dist/stdio.d.ts +9 -1
  40. package/dist/stdio.js +97 -55
  41. package/dist/tool-bridge.d.ts +67 -0
  42. package/dist/tool-bridge.js +62 -0
  43. package/dist/tool-permissions.d.ts +46 -0
  44. package/dist/tool-permissions.js +70 -0
  45. package/dist/tool-wiring.d.ts +46 -0
  46. package/dist/tool-wiring.js +203 -0
  47. package/dist/transport-loopback.d.ts +8 -1
  48. package/dist/transport-loopback.js +22 -1
  49. package/dist/turn.d.ts +11 -5
  50. package/dist/turn.js +39 -16
  51. package/package.json +17 -4
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Per-agent settings that make bridged tools reach the agent's model exactly
3
+ * as they reach a `callModel` model: listed up front, output uncut, no extra
4
+ * approval step, and no timeout a model-driven call would not have.
5
+ */
6
+ import { z } from 'zod';
7
+ import { claudeBridgedPermission, codexBridgedPermission, geminiBridgedPermission, } from './tool-permissions.js';
8
+ const JsonObjectSchema = z.record(z.string(), z.unknown());
9
+ //#region Helpers
10
+ /** Largest timeout Node timers accept, in milliseconds. */
11
+ const NO_TIMEOUT_MS = 2147483647;
12
+ const NO_TIMEOUT_SEC = Math.floor(NO_TIMEOUT_MS / 1e3);
13
+ const UNBOUNDED = 1e9;
14
+ function hostEnv(name) {
15
+ const env = Reflect.get(globalThis, 'process')?.env;
16
+ return env?.[name];
17
+ }
18
+ /** The JSON config an agent already has in `name`, from the preset or the host. */
19
+ function baseJsonConfig(launch, name) {
20
+ const raw = launch.env[name] ?? hostEnv(name);
21
+ if (!raw) {
22
+ return {};
23
+ }
24
+ const parsed = JsonObjectSchema.safeParse(parseJson(raw));
25
+ if (!parsed.success) {
26
+ throw new TypeError(`${name} must hold a JSON object.`);
27
+ }
28
+ return parsed.data;
29
+ }
30
+ function parseJson(raw) {
31
+ try {
32
+ return JSON.parse(raw);
33
+ }
34
+ catch {
35
+ return undefined;
36
+ }
37
+ }
38
+ function recordAt(config, key) {
39
+ const parsed = JsonObjectSchema.safeParse(config[key]);
40
+ return parsed.success ? parsed.data : {};
41
+ }
42
+ function pairsToRecord(pairs) {
43
+ return Object.fromEntries(pairs.map((pair) => [
44
+ pair.name,
45
+ pair.value,
46
+ ]));
47
+ }
48
+ //#endregion
49
+ //#region Codex
50
+ /** A user's ACP MCP server as a Codex `mcp_servers` entry. SSE is refused later by the capability check. */
51
+ function codexServer(server) {
52
+ if (!('type' in server)) {
53
+ return {
54
+ command: server.command,
55
+ args: server.args,
56
+ env: pairsToRecord(server.env),
57
+ };
58
+ }
59
+ if (server.type === 'http') {
60
+ return {
61
+ url: server.url,
62
+ http_headers: pairsToRecord(server.headers),
63
+ };
64
+ }
65
+ return undefined;
66
+ }
67
+ /**
68
+ * codex-acp replaces `CODEX_CONFIG.mcp_servers` with the servers in
69
+ * `session/new`, and builds those from address fields only. So every server —
70
+ * the bridge with its settings, and the user's own — is declared here, and
71
+ * `session/new` carries none.
72
+ */
73
+ export const codexToolWiring = {
74
+ declare: 'launch',
75
+ permits: codexBridgedPermission,
76
+ launchEnv(launch) {
77
+ const config = baseJsonConfig(launch, 'CODEX_CONFIG');
78
+ const servers = recordAt(config, 'mcp_servers');
79
+ for (const server of launch.mcpServers) {
80
+ const entry = codexServer(server);
81
+ if (entry) {
82
+ servers[server.name] = entry;
83
+ }
84
+ }
85
+ servers[launch.bridge.serverName] = {
86
+ url: launch.bridge.url,
87
+ http_headers: pairsToRecord(launch.bridge.headers),
88
+ // Codex otherwise reaches MCP tools only from model-written code.
89
+ omit_tools_from: [
90
+ 'code_mode',
91
+ 'deferred',
92
+ ],
93
+ // Codex otherwise runs its own "Guardian Review" before each call.
94
+ default_tools_approval_mode: 'approve',
95
+ tool_timeout_sec: NO_TIMEOUT_SEC,
96
+ tools: Object.fromEntries(launch.tools.map((tool) => [
97
+ tool.name,
98
+ {
99
+ output_token_limit: UNBOUNDED,
100
+ },
101
+ ])),
102
+ };
103
+ // The config holds the bridge token and the user's server env; keep it out
104
+ // of the environment of every command Codex runs.
105
+ const shellPolicy = recordAt(config, 'shell_environment_policy');
106
+ const excluded = Array.isArray(shellPolicy.exclude) ? shellPolicy.exclude : [];
107
+ return {
108
+ CODEX_CONFIG: JSON.stringify({
109
+ ...config,
110
+ mcp_servers: servers,
111
+ shell_environment_policy: {
112
+ ...shellPolicy,
113
+ exclude: [
114
+ ...excluded,
115
+ 'CODEX_CONFIG',
116
+ ],
117
+ },
118
+ }),
119
+ };
120
+ },
121
+ };
122
+ //#endregion
123
+ //#region Others
124
+ /**
125
+ * Claude Code reads its per-tool hints from the tool listing; the call
126
+ * timeout and its MCP output token cap are set here.
127
+ */
128
+ export const claudeToolWiring = {
129
+ declare: 'session',
130
+ permits: claudeBridgedPermission,
131
+ launchEnv(launch) {
132
+ const setting = (name, value) => launch.env[name] ?? hostEnv(name) ?? String(value);
133
+ return {
134
+ MCP_TOOL_TIMEOUT: setting('MCP_TOOL_TIMEOUT', NO_TIMEOUT_MS),
135
+ MAX_MCP_OUTPUT_TOKENS: setting('MAX_MCP_OUTPUT_TOKENS', UNBOUNDED),
136
+ };
137
+ },
138
+ };
139
+ /** opencode otherwise cuts tool output at 50 KiB and saves the rest to a file. */
140
+ export const opencodeToolWiring = {
141
+ declare: 'session',
142
+ launchEnv(launch) {
143
+ const config = baseJsonConfig(launch, 'OPENCODE_CONFIG_CONTENT');
144
+ return {
145
+ OPENCODE_CONFIG_CONTENT: JSON.stringify({
146
+ ...config,
147
+ tool_output: {
148
+ max_lines: UNBOUNDED,
149
+ max_bytes: UNBOUNDED,
150
+ },
151
+ }),
152
+ };
153
+ },
154
+ };
155
+ /**
156
+ * Gemini CLI lists tools up front and returns output whole as is. It starts
157
+ * the bridge only in a folder it trusts, which is the user's call to make
158
+ * (`gemini({ trustWorkspace })`), never the wiring's.
159
+ */
160
+ export const geminiToolWiring = {
161
+ declare: 'session',
162
+ permits: geminiBridgedPermission,
163
+ };
164
+ /**
165
+ * pi has no MCP client, and pi-acp drops the MCP servers it is given. So the
166
+ * bridge is declared at launch: pi-acp starts the `noetic-pi` launcher in
167
+ * place of `pi` (`PI_ACP_PI_COMMAND`), which adds the `pi-noetic-tools`
168
+ * extension. The extension registers each bridged tool as a native pi tool —
169
+ * same name, description and schema — and pi cuts no extension tool's result.
170
+ * A `PI_ACP_PI_COMMAND` the host already set is the pi the launcher starts.
171
+ */
172
+ export const piToolWiring = {
173
+ declare: 'launch',
174
+ launchEnv(launch) {
175
+ if (launch.mcpServers.length > 0) {
176
+ throw new TypeError('pi takes no MCP servers of its own: pi-acp drops them. Give their tools to the step as `tools` instead.');
177
+ }
178
+ const pi = launch.env.PI_ACP_PI_COMMAND ?? hostEnv('PI_ACP_PI_COMMAND') ?? 'pi';
179
+ return {
180
+ PI_ACP_PI_COMMAND: launch.env.NOETIC_PI_LAUNCHER ?? hostEnv('NOETIC_PI_LAUNCHER') ?? piLauncherPath(),
181
+ NOETIC_PI_COMMAND: launch.env.NOETIC_PI_COMMAND ?? hostEnv('NOETIC_PI_COMMAND') ?? pi,
182
+ NOETIC_TOOLS_URL: launch.bridge.url,
183
+ NOETIC_TOOLS_HEADERS: JSON.stringify(launch.bridge.headers),
184
+ };
185
+ },
186
+ };
187
+ /**
188
+ * The launcher shipped beside this module. A runner started with
189
+ * `--agent pi` names its own in place of the one a host sends.
190
+ */
191
+ export function piLauncherPath() {
192
+ const suffix = import.meta.url.endsWith('.ts') ? 'ts' : 'js';
193
+ return decodeURIComponent(new URL(`./pi/launch.${suffix}`, import.meta.url).pathname);
194
+ }
195
+ /**
196
+ * Any agent that takes MCP servers in `session/new` and needs no settings.
197
+ * It has no way to recognize the agent's permission requests for bridged
198
+ * tools, so those go to the step's policy.
199
+ */
200
+ export const sessionToolWiring = {
201
+ declare: 'session',
202
+ };
203
+ //#endregion
@@ -6,7 +6,7 @@
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 { AcpTransport, AcpTransportFactory } from '@noetic-tools/types';
9
+ import type { AcpMachineFactory, AcpTransport, AcpTransportFactory, SpawnCommand } from '@noetic-tools/types';
10
10
  import type * as acp from '@zed-industries/agent-client-protocol';
11
11
  import { AgentSideConnection } from '@zed-industries/agent-client-protocol';
12
12
  /** @public The two ends of a loopback connection. */
@@ -29,3 +29,10 @@ export declare function createAcpLoopbackPair(): AcpLoopbackPair;
29
29
  * @public
30
30
  */
31
31
  export declare function loopbackTransport(toAgent: (conn: AgentSideConnection) => acp.Agent): AcpTransportFactory;
32
+ /**
33
+ * A machine that is this process: the bridge is reached where it listens, and
34
+ * `launch` stands the in-process agent up with the command it was given, so a
35
+ * test can see the launch settings. It runs no processes.
36
+ * @public
37
+ */
38
+ export declare function loopbackMachine(toAgent: (conn: AgentSideConnection, command: SpawnCommand) => acp.Agent): AcpMachineFactory;
@@ -6,7 +6,7 @@
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 { frameworkCast } from '@noetic-tools/types';
9
+ import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
10
10
  import { AgentSideConnection, ndJsonStream } from '@zed-industries/agent-client-protocol';
11
11
  function endpoint(readable, writable) {
12
12
  let closed = false;
@@ -71,3 +71,24 @@ export function loopbackTransport(toAgent) {
71
71
  };
72
72
  };
73
73
  }
74
+ /**
75
+ * A machine that is this process: the bridge is reached where it listens, and
76
+ * `launch` stands the in-process agent up with the command it was given, so a
77
+ * test can see the launch settings. It runs no processes.
78
+ * @public
79
+ */
80
+ export function loopbackMachine(toAgent) {
81
+ return async () => ({
82
+ reach: async (bridge) => bridge,
83
+ launch: (command) => loopbackTransport((conn) => toAgent(conn, command))({
84
+ cwd: command.cwd ?? '',
85
+ }),
86
+ async spawn(command) {
87
+ throw new AcpConnectError({
88
+ agentId: 'loopback',
89
+ message: `A loopback machine runs no processes, so it cannot start '${command.command}'.`,
90
+ });
91
+ },
92
+ close: async () => undefined,
93
+ });
94
+ }
package/dist/turn.d.ts CHANGED
@@ -15,18 +15,18 @@ import type { AcpPlanEntry, AcpSessionNotification, AcpStopReason, AcpToolCallCo
15
15
  export declare function renderToolCallContent(content: ReadonlyArray<AcpToolCallContent>, rawOutput?: Record<string, unknown>): string;
16
16
  /** @public Folds a turn's session updates into a result. */
17
17
  export declare class AcpTurnAccumulator {
18
- private text;
19
18
  private reasoning;
20
19
  private readonly toolCalls;
21
- /** Insertion order, so items come out in the order the agent produced them. */
22
- private readonly toolCallOrder;
20
+ /**
21
+ * The turn in the order the agent produced it: runs of text between tool
22
+ * calls, so items interleave as a `callModel` round's do.
23
+ */
24
+ private readonly order;
23
25
  private plan?;
24
26
  private availableCommands?;
25
27
  private currentModeId?;
26
28
  /** Feed one `session/update` notification. */
27
29
  push(notification: AcpSessionNotification): void;
28
- /** Assistant text accumulated so far. */
29
- get textContent(): string;
30
30
  /** Reasoning text accumulated so far. */
31
31
  get reasoningContent(): string;
32
32
  /** Latest plan snapshot, when the agent reported one. */
@@ -37,6 +37,12 @@ export declare class AcpTurnAccumulator {
37
37
  usage?: TokenUsage;
38
38
  cost?: number;
39
39
  }): AcpTurnResult;
40
+ /**
41
+ * The agent's last message — the text after its final tool call — as a
42
+ * `callModel` step returns its final message rather than every message.
43
+ */
44
+ private finalText;
45
+ private appendText;
40
46
  private apply;
41
47
  private trackToolCall;
42
48
  private mergeToolCall;
package/dist/turn.js CHANGED
@@ -43,11 +43,13 @@ export function renderToolCallContent(content, rawOutput) {
43
43
  //#region Accumulator
44
44
  /** @public Folds a turn's session updates into a result. */
45
45
  export class AcpTurnAccumulator {
46
- text = '';
47
46
  reasoning = '';
48
47
  toolCalls = new Map();
49
- /** Insertion order, so items come out in the order the agent produced them. */
50
- toolCallOrder = [];
48
+ /**
49
+ * The turn in the order the agent produced it: runs of text between tool
50
+ * calls, so items interleave as a `callModel` round's do.
51
+ */
52
+ order = [];
51
53
  plan;
52
54
  availableCommands;
53
55
  currentModeId;
@@ -55,10 +57,6 @@ export class AcpTurnAccumulator {
55
57
  push(notification) {
56
58
  this.apply(notification.update);
57
59
  }
58
- /** Assistant text accumulated so far. */
59
- get textContent() {
60
- return this.text;
61
- }
62
60
  /** Reasoning text accumulated so far. */
63
61
  get reasoningContent() {
64
62
  return this.reasoning;
@@ -70,11 +68,12 @@ export class AcpTurnAccumulator {
70
68
  /** Build the turn result. */
71
69
  result(opts) {
72
70
  const items = [];
73
- if (this.text.length > 0) {
74
- items.push(assistantMessageItem(this.text));
75
- }
76
- for (const id of this.toolCallOrder) {
77
- const call = this.toolCalls.get(id);
71
+ for (const entry of this.order) {
72
+ if ('text' in entry) {
73
+ items.push(assistantMessageItem(entry.text));
74
+ continue;
75
+ }
76
+ const call = this.toolCalls.get(entry.toolCallId);
78
77
  if (!call) {
79
78
  continue;
80
79
  }
@@ -98,7 +97,7 @@ export class AcpTurnAccumulator {
98
97
  return {
99
98
  stopReason: opts.stopReason,
100
99
  items,
101
- text: this.text,
100
+ text: this.finalText(),
102
101
  usage: opts.usage,
103
102
  cost: opts.cost,
104
103
  plan: this.plan,
@@ -107,9 +106,29 @@ export class AcpTurnAccumulator {
107
106
  };
108
107
  }
109
108
  //#region internals
109
+ /**
110
+ * The agent's last message — the text after its final tool call — as a
111
+ * `callModel` step returns its final message rather than every message.
112
+ */
113
+ finalText() {
114
+ const last = this.order[this.order.length - 1];
115
+ return last && 'text' in last ? last.text : '';
116
+ }
117
+ appendText(chunk) {
118
+ const last = this.order[this.order.length - 1];
119
+ if (last && 'text' in last) {
120
+ last.text += chunk;
121
+ return;
122
+ }
123
+ if (chunk.length > 0) {
124
+ this.order.push({
125
+ text: chunk,
126
+ });
127
+ }
128
+ }
110
129
  apply(update) {
111
130
  if (update.sessionUpdate === 'agent_message_chunk') {
112
- this.text += contentBlockText(update.content);
131
+ this.appendText(contentBlockText(update.content));
113
132
  return;
114
133
  }
115
134
  if (update.sessionUpdate === 'agent_thought_chunk') {
@@ -148,7 +167,9 @@ export class AcpTurnAccumulator {
148
167
  }
149
168
  trackToolCall(update) {
150
169
  if (!this.toolCalls.has(update.toolCallId)) {
151
- this.toolCallOrder.push(update.toolCallId);
170
+ this.order.push({
171
+ toolCallId: update.toolCallId,
172
+ });
152
173
  }
153
174
  this.toolCalls.set(update.toolCallId, {
154
175
  toolCallId: update.toolCallId,
@@ -169,7 +190,9 @@ export class AcpTurnAccumulator {
169
190
  if (!existing) {
170
191
  // An update for a call we never saw announced. Track it anyway rather
171
192
  // than dropping the agent's work.
172
- this.toolCallOrder.push(update.toolCallId);
193
+ this.order.push({
194
+ toolCallId: update.toolCallId,
195
+ });
173
196
  this.toolCalls.set(update.toolCallId, {
174
197
  toolCallId: update.toolCallId,
175
198
  title: update.title ?? update.toolCallId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/acp",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
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",
@@ -37,6 +37,10 @@
37
37
  "types": "./dist/server.d.ts",
38
38
  "default": "./dist/server.js"
39
39
  },
40
+ "./remote": {
41
+ "types": "./dist/remote.d.ts",
42
+ "default": "./dist/remote.js"
43
+ },
40
44
  "./package.json": "./package.json"
41
45
  },
42
46
  "files": [
@@ -48,16 +52,21 @@
48
52
  "access": "public"
49
53
  },
50
54
  "devDependencies": {
51
- "bun-types": "latest",
55
+ "@modelcontextprotocol/client": "2.4.0",
56
+ "@noetic-tools/context": "workspace:*",
52
57
  "@noetic-tools/core": "workspace:*",
53
- "@noetic-tools/context": "workspace:*"
58
+ "@types/ws": "8.18.2",
59
+ "bun-types": "latest"
54
60
  },
55
61
  "peerDependencies": {
56
62
  "zod": "^4.0.0"
57
63
  },
58
64
  "dependencies": {
59
- "@noetic-tools/types": "^3.17.1",
65
+ "@modelcontextprotocol/node": "2.1.2",
66
+ "@modelcontextprotocol/server": "2.4.0",
67
+ "@noetic-tools/types": "^3.18.0",
60
68
  "@zed-industries/agent-client-protocol": "^0.4.5",
69
+ "ws": "8.22.1",
61
70
  "zod": "^4.0.0"
62
71
  },
63
72
  "scripts": {
@@ -72,5 +81,9 @@
72
81
  "lint:fix": "bunx biome check --fix .",
73
82
  "format": "bunx biome format --write .",
74
83
  "format:check": "bunx biome format ."
84
+ },
85
+ "bin": {
86
+ "noetic-acp": "./dist/bin.js",
87
+ "noetic-pi": "./dist/pi/launch.js"
75
88
  }
76
89
  }