@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.
Files changed (54) hide show
  1. package/dist/agents.d.ts +5 -0
  2. package/dist/agents.js +13 -1
  3. package/dist/bin.js +19 -2
  4. package/dist/client-adapters.d.ts +2 -2
  5. package/dist/client.d.ts +9 -2
  6. package/dist/client.js +40 -1
  7. package/dist/connection.d.ts +25 -2
  8. package/dist/connection.js +455 -36
  9. package/dist/define.d.ts +13 -0
  10. package/dist/define.js +122 -4
  11. package/dist/index.d.ts +1 -1
  12. package/dist/items.d.ts +1 -1
  13. package/dist/keeper/client.d.ts +93 -0
  14. package/dist/keeper/client.js +468 -0
  15. package/dist/keeper/main.d.ts +6 -0
  16. package/dist/keeper/main.js +31 -0
  17. package/dist/keeper/process.d.ts +24 -0
  18. package/dist/keeper/process.js +425 -0
  19. package/dist/keeper/protocol.d.ts +74 -0
  20. package/dist/keeper/protocol.js +108 -0
  21. package/dist/keeper/relay.d.ts +88 -0
  22. package/dist/keeper/relay.js +420 -0
  23. package/dist/mcp/node.d.ts +6 -1
  24. package/dist/mcp/node.js +4 -4
  25. package/dist/permission-channel.d.ts +5 -0
  26. package/dist/permission-channel.js +16 -1
  27. package/dist/runner/host.js +74 -16
  28. package/dist/runner/options.d.ts +2 -0
  29. package/dist/runner/options.js +2 -0
  30. package/dist/runner/protocol.d.ts +12 -0
  31. package/dist/runner/protocol.js +24 -0
  32. package/dist/runner/serve.d.ts +5 -0
  33. package/dist/runner/serve.js +93 -3
  34. package/dist/serve-events.d.ts +2 -0
  35. package/dist/serve-events.js +1 -0
  36. package/dist/serve-questions.d.ts +26 -0
  37. package/dist/serve-questions.js +105 -0
  38. package/dist/serve-types.d.ts +1 -1
  39. package/dist/serve.d.ts +2 -2
  40. package/dist/serve.js +10 -1
  41. package/dist/server.js +1 -1
  42. package/dist/stdio-process.d.ts +38 -0
  43. package/dist/stdio-process.js +214 -0
  44. package/dist/stdio.d.ts +50 -7
  45. package/dist/stdio.js +192 -52
  46. package/dist/tool-bridge.d.ts +9 -0
  47. package/dist/tool-bridge.js +5 -1
  48. package/dist/transport-loopback.d.ts +2 -2
  49. package/dist/transport-loopback.js +1 -1
  50. package/dist/turn.d.ts +3 -2
  51. package/dist/turn.js +22 -2
  52. package/dist/update-filter.d.ts +38 -0
  53. package/dist/update-filter.js +193 -0
  54. package/package.json +3 -3
package/dist/define.js CHANGED
@@ -7,6 +7,8 @@
7
7
  * prompts, permissions, terminals — is protocol-generic and lives in
8
8
  * {@link openAcpConnection}.
9
9
  */
10
+ import { AcpConnectError } from '@noetic-tools/types';
11
+ import { z } from 'zod';
10
12
  import { openAcpConnection } from './connection.js';
11
13
  import { spawnOn } from './spawn.js';
12
14
  import { startToolBridge } from './tool-bridge.js';
@@ -25,10 +27,20 @@ export function defineAcpAgent(def) {
25
27
  spawn: def.command ? spawnOn(def.machine ?? lazyLocalMachine(def.agentId)) : undefined,
26
28
  async connect(opts) {
27
29
  const signal = opts.signal ?? opts.host.signal;
28
- const opened = await openTarget(def, opts.host.cwd, signal);
30
+ const whenHostExits = opts.whenHostExits ?? 'keep';
31
+ if (opts.reattach) {
32
+ return await reattachTo(def, opts, opts.reattach);
33
+ }
34
+ const opened = await openTarget(def, {
35
+ cwd: opts.host.cwd,
36
+ signal,
37
+ whenHostExits,
38
+ });
29
39
  let bridge;
30
40
  try {
31
- bridge = await openBridge(def, opts, opened.reach);
41
+ bridge = await openBridge(def, opts, {
42
+ reach: opened.reach,
43
+ });
32
44
  const transport = await opened.open(bridge?.env ?? {});
33
45
  return await openAcpConnection({
34
46
  agentId: def.agentId,
@@ -36,6 +48,9 @@ export function defineAcpAgent(def) {
36
48
  host: opts.host,
37
49
  signal,
38
50
  tools: bridge,
51
+ instructionsMeta: def.instructionsMeta,
52
+ sessionMeta: def.sessionMeta,
53
+ reattach: reattachHandle(transport.reattach, bridge),
39
54
  });
40
55
  }
41
56
  catch (e) {
@@ -69,7 +84,7 @@ function attachedRefusal(wiring) {
69
84
  };
70
85
  }
71
86
  /** A command starts on its machine; a transport attaches as it is. */
72
- async function openTarget(def, cwd, signal) {
87
+ async function openTarget(def, { cwd, signal, whenHostExits, }) {
73
88
  const command = def.command;
74
89
  if (!command) {
75
90
  const transport = def.transport;
@@ -84,6 +99,7 @@ async function openTarget(def, cwd, signal) {
84
99
  ...env,
85
100
  },
86
101
  signal,
102
+ whenHostExits,
87
103
  }),
88
104
  // Unreached: a step never gives an attached agent tools (`refusesTools`).
89
105
  reach: async () => {
@@ -94,6 +110,7 @@ async function openTarget(def, cwd, signal) {
94
110
  }
95
111
  const machine = await (def.machine ?? lazyLocalMachine(def.agentId))({
96
112
  signal,
113
+ whenHostExits,
97
114
  });
98
115
  return {
99
116
  open: (env) => machine.launch({
@@ -121,9 +138,15 @@ function lazyLocalMachine(agentId) {
121
138
  }
122
139
  /** Closing the agent's stream releases the machine it runs on. */
123
140
  function closingWith(transport, opened) {
141
+ const detach = transport.detach?.bind(transport);
124
142
  return {
125
143
  readable: transport.readable,
126
144
  writable: transport.writable,
145
+ ...(transport.reattach
146
+ ? {
147
+ reattach: transport.reattach,
148
+ }
149
+ : {}),
127
150
  async close() {
128
151
  try {
129
152
  await transport.close();
@@ -132,10 +155,104 @@ function closingWith(transport, opened) {
132
155
  await opened.close();
133
156
  }
134
157
  },
158
+ // Letting go keeps the machine: it runs the agent a later host reattaches to.
159
+ ...(detach
160
+ ? {
161
+ detach,
162
+ }
163
+ : {}),
135
164
  };
136
165
  }
166
+ /**
167
+ * Reach the agent an earlier host started, through the machine that runs it:
168
+ * the stream from its keeper, and the bridge served again under the path and
169
+ * token the agent was given, at the address it already uses.
170
+ */
171
+ async function reattachTo(def, opts, handle) {
172
+ const signal = opts.signal ?? opts.host.signal;
173
+ const whenHostExits = opts.whenHostExits ?? 'keep';
174
+ if (!def.command) {
175
+ throw new AcpConnectError({
176
+ agentId: def.agentId,
177
+ message: `ACP agent '${def.agentId}' attaches through a transport and cannot be reattached.`,
178
+ });
179
+ }
180
+ const machine = await (def.machine ?? lazyLocalMachine(def.agentId))({
181
+ signal,
182
+ whenHostExits,
183
+ });
184
+ let bridge;
185
+ let attached;
186
+ try {
187
+ if (!machine.attach) {
188
+ throw new AcpConnectError({
189
+ agentId: def.agentId,
190
+ message: `The machine ACP agent '${def.agentId}' runs on cannot reattach to an agent.`,
191
+ });
192
+ }
193
+ const transport = await machine.attach(handle);
194
+ attached = transport;
195
+ bridge = await openBridge(def, opts, {
196
+ reach: (address) => machine.reach(address),
197
+ reuse: bridgeOf(handle),
198
+ });
199
+ return await openAcpConnection({
200
+ agentId: def.agentId,
201
+ transport: closingWith(transport, {
202
+ open: async () => transport,
203
+ reach: (address) => machine.reach(address),
204
+ close: () => machine.close(),
205
+ }),
206
+ host: opts.host,
207
+ signal,
208
+ tools: bridge,
209
+ instructionsMeta: def.instructionsMeta,
210
+ sessionMeta: def.sessionMeta,
211
+ reattach: reattachHandle(transport.reattach ?? handle, bridge),
212
+ reattached: true,
213
+ });
214
+ }
215
+ catch (e) {
216
+ await bridge?.close();
217
+ await letGoOf(attached, machine);
218
+ throw e;
219
+ }
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
+ }
233
+ const BridgeCredentialsSchema = z.object({
234
+ path: z.string().startsWith('/'),
235
+ token: z.string().min(1),
236
+ });
237
+ /** The bridge credentials a reattach handle carries, if any. */
238
+ function bridgeOf(handle) {
239
+ const parsed = BridgeCredentialsSchema.safeParse(handle.bridge);
240
+ return parsed.success ? parsed.data : undefined;
241
+ }
242
+ /** The handle a later host reattaches with: the machine's, plus the bridge it must serve again. */
243
+ function reattachHandle(machine, bridge) {
244
+ if (!machine) {
245
+ return undefined;
246
+ }
247
+ return bridge
248
+ ? {
249
+ ...machine,
250
+ bridge: bridge.credentials,
251
+ }
252
+ : machine;
253
+ }
137
254
  /** Start the tool bridge when the step gave tools, before the agent starts. */
138
- async function openBridge(def, opts, reach) {
255
+ async function openBridge(def, opts, { reach, reuse, }) {
139
256
  const host = opts.host.tools;
140
257
  if (!host) {
141
258
  return undefined;
@@ -148,6 +265,7 @@ async function openBridge(def, opts, reach) {
148
265
  mcpServers: opts.mcpServers ?? [],
149
266
  env: def.env,
150
267
  reach,
268
+ reuse,
151
269
  });
152
270
  }
153
271
  //#endregion
package/dist/index.d.ts CHANGED
@@ -86,6 +86,6 @@ export { NoeticAcpClient } from './client.js';
86
86
  * pieces underneath it.
87
87
  * @public
88
88
  */
89
- export type { OpenAcpConnectionOptions } from './connection.js';
89
+ export type { InstructionsMeta, OpenAcpConnectionOptions } from './connection.js';
90
90
  /** @public */
91
91
  export { openAcpConnection } from './connection.js';
package/dist/items.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare function assistantMessageItem(text: string, id?: string): Message
17
17
  export declare function functionCallItem(opts: {
18
18
  title: string;
19
19
  toolCallId: string;
20
- rawInput?: Record<string, unknown>;
20
+ rawInput?: unknown;
21
21
  kind?: string;
22
22
  }): FunctionCallItem;
23
23
  /** @public Build the tool-result Item paired with a completed ACP tool call. */
@@ -0,0 +1,93 @@
1
+ /**
2
+ * The host's side of a keeper (`process.ts`): start one for a new agent, or
3
+ * attach to the one an earlier host left running, and carry the agent's ACP
4
+ * stream and the bridge port over its socket.
5
+ */
6
+ import * as net from 'node:net';
7
+ import type { AcpReattachHandle, AcpTransport, AcpWhenHostExits, SpawnCommand } from '@noetic-tools/types';
8
+ import { z } from 'zod';
9
+ /** @public Options for starting a keeper. */
10
+ export interface KeeperStartOptions {
11
+ whenHostExits: AcpWhenHostExits;
12
+ /** How long the agent may run with no host attached; 0 = no limit. */
13
+ detachedTimeoutMs: number;
14
+ }
15
+ declare const KeeperHandleSchema: z.ZodObject<{
16
+ kind: z.ZodLiteral<"noetic-acp-keeper">;
17
+ version: z.ZodLiteral<1>;
18
+ address: z.ZodString;
19
+ token: z.ZodString;
20
+ }, z.core.$strip>;
21
+ /** @public What a keeper's reattach handle holds. */
22
+ export type KeeperHandle = z.infer<typeof KeeperHandleSchema>;
23
+ /** The keeper part of a reattach handle, or undefined when it names no keeper. */
24
+ export declare function keeperHandleOf(handle: AcpReattachHandle): KeeperHandle | undefined;
25
+ /**
26
+ * The keeper's entry script beside this module, or undefined when there is
27
+ * none on disk (a bundled single-file build), in which case agents are
28
+ * started directly and do not outlive their host.
29
+ */
30
+ export declare function keeperScript(): string | undefined;
31
+ /**
32
+ * Whether `handle` names a keeper this user's keepers would listen on — so a
33
+ * runner never connects to an arbitrary socket a host names.
34
+ */
35
+ export declare function isKeeperAddress(address: string): boolean;
36
+ /**
37
+ * One host's connection to a keeper. Control lines are answered here; every
38
+ * other line is the agent's ACP stream.
39
+ * @public
40
+ */
41
+ export declare class KeeperLink {
42
+ private readonly socket;
43
+ readonly handle: KeeperHandle;
44
+ private readonly agentId;
45
+ private next;
46
+ private readonly waiters;
47
+ private readonly welcomed;
48
+ private acp?;
49
+ /** ACP lines that arrived before the stream was taken. */
50
+ private readonly early;
51
+ private readonly encoder;
52
+ private closed;
53
+ private stopping?;
54
+ /** Where the agent's stderr goes. Default: this process's stderr. */
55
+ onStderr: (chunk: string) => void;
56
+ constructor(socket: net.Socket, handle: KeeperHandle, agentId: string);
57
+ /** Say hello; resolves with whether the keeper runs an agent. */
58
+ hello(whenHostExits: AcpWhenHostExits): Promise<'running' | 'none'>;
59
+ /** Serve the bridge at `target` on the keeper's port for `path`. */
60
+ reach(path: string, target: string): Promise<string>;
61
+ /** Start the agent and return its stream. */
62
+ launch(command: SpawnCommand): Promise<AcpTransport>;
63
+ /** The stream of the agent the keeper already runs. */
64
+ transport(): AcpTransport;
65
+ /** Stop the agent's tree and the keeper. Shared by every caller. */
66
+ stop(): Promise<void>;
67
+ /** Let go of the keeper without stopping it, as a host that goes away does. */
68
+ detach(): void;
69
+ /**
70
+ * Let go of an agent this host did not adopt: the keeper keeps it running
71
+ * whatever exit policy this host's hello set, then the connection drops.
72
+ */
73
+ release(): Promise<void>;
74
+ private shutdown;
75
+ private receive;
76
+ private ask;
77
+ private write;
78
+ private id;
79
+ private lost;
80
+ }
81
+ /**
82
+ * Start a keeper and connect to it, or return undefined when there is no
83
+ * keeper script to run.
84
+ */
85
+ export declare function startKeeper(agentId: string, opts: KeeperStartOptions): Promise<KeeperLink | undefined>;
86
+ /** Whether a failed connect means no keeper is there: its socket is missing or refuses. */
87
+ export declare function keeperIsGone(cause: unknown): boolean;
88
+ /**
89
+ * Connect to the keeper `handle` names. Rejects with AcpConnectError when it
90
+ * is gone or no longer runs its agent.
91
+ */
92
+ export declare function attachKeeper(agentId: string, handle: KeeperHandle, whenHostExits: AcpWhenHostExits): Promise<KeeperLink>;
93
+ export {};