@noetic-tools/acp 1.0.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.
- package/LICENSE +201 -0
- package/README.md +89 -0
- package/dist/agents.d.ts +74 -0
- package/dist/agents.js +136 -0
- package/dist/client.d.ts +92 -0
- package/dist/client.js +288 -0
- package/dist/connection.d.ts +37 -0
- package/dist/connection.js +337 -0
- package/dist/define.d.ts +23 -0
- package/dist/define.js +29 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +41 -0
- package/dist/items.d.ts +36 -0
- package/dist/items.js +76 -0
- package/dist/liveness.d.ts +46 -0
- package/dist/liveness.js +73 -0
- package/dist/paths.d.ts +46 -0
- package/dist/paths.js +84 -0
- package/dist/permission-channel.d.ts +99 -0
- package/dist/permission-channel.js +172 -0
- package/dist/permissions.d.ts +58 -0
- package/dist/permissions.js +150 -0
- package/dist/registry.d.ts +10 -0
- package/dist/registry.js +13 -0
- package/dist/stdio.d.ts +27 -0
- package/dist/stdio.js +82 -0
- package/dist/terminals.d.ts +70 -0
- package/dist/terminals.js +221 -0
- package/dist/transport-loopback.d.ts +31 -0
- package/dist/transport-loopback.js +60 -0
- package/dist/turn.d.ts +43 -0
- package/dist/turn.js +214 -0
- package/package.json +70 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backs the ACP `terminal/*` client methods with Noetic's {@link ShellAdapter}.
|
|
3
|
+
*
|
|
4
|
+
* ACP models a terminal as a long-lived handle the agent creates, polls, waits
|
|
5
|
+
* on, kills, and releases. `ShellAdapter.exec` is a single call that streams
|
|
6
|
+
* through `onData`, so this registry brackets one `exec` per terminal and
|
|
7
|
+
* exposes the handle semantics on top of it. Everything the sub-agent runs
|
|
8
|
+
* therefore goes through the same shell adapter as first-party steps.
|
|
9
|
+
*/
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region Helpers
|
|
12
|
+
/**
|
|
13
|
+
* POSIX single-quote escaping. `ShellAdapter.exec` takes one command string
|
|
14
|
+
* while ACP supplies `command` + `args`, so arguments are quoted rather than
|
|
15
|
+
* concatenated — otherwise a path with a space would silently split.
|
|
16
|
+
*/
|
|
17
|
+
export function quoteShellArg(arg) {
|
|
18
|
+
if (/^[\w./:=@-]+$/.test(arg)) {
|
|
19
|
+
return arg;
|
|
20
|
+
}
|
|
21
|
+
return `'${arg.replace(/'/g, `'\\''`)}'`;
|
|
22
|
+
}
|
|
23
|
+
/** Build the single command string `ShellAdapter.exec` expects. */
|
|
24
|
+
export function buildCommandLine(command, args) {
|
|
25
|
+
if (!args || args.length === 0) {
|
|
26
|
+
return command;
|
|
27
|
+
}
|
|
28
|
+
return [
|
|
29
|
+
quoteShellArg(command),
|
|
30
|
+
...args.map(quoteShellArg),
|
|
31
|
+
].join(' ');
|
|
32
|
+
}
|
|
33
|
+
function toEnvRecord(env) {
|
|
34
|
+
if (!env || env.length === 0) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const record = {};
|
|
38
|
+
for (const entry of env) {
|
|
39
|
+
record[entry.name] = entry.value;
|
|
40
|
+
}
|
|
41
|
+
return record;
|
|
42
|
+
}
|
|
43
|
+
/** The status of a command we killed, whichever way `exec` chose to settle. */
|
|
44
|
+
const KILLED_STATUS = {
|
|
45
|
+
exitCode: null,
|
|
46
|
+
signal: 'SIGTERM',
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Exit status for a rejected `exec`. The adapter signals a timeout kill through
|
|
50
|
+
* the message channel, which ACP represents as a signal rather than a code.
|
|
51
|
+
*/
|
|
52
|
+
function exitStatusFromError(error) {
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
if (message.startsWith('timeout:')) {
|
|
55
|
+
return {
|
|
56
|
+
exitCode: null,
|
|
57
|
+
signal: 'SIGTERM',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
exitCode: 1,
|
|
62
|
+
signal: null,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Trim to the last `limit` bytes, then forward past any UTF-8 continuation
|
|
67
|
+
* bytes so the result never starts mid-character.
|
|
68
|
+
*
|
|
69
|
+
* ACP asks the client to truncate *from the beginning of the output*; dropping
|
|
70
|
+
* whole chunks instead meant one over-limit chunk destroyed the entire buffer —
|
|
71
|
+
* a `npm test` producing megabytes could read back empty.
|
|
72
|
+
*/
|
|
73
|
+
function trimToLimit(buffer, limit) {
|
|
74
|
+
if (buffer.byteLength <= limit) {
|
|
75
|
+
return buffer;
|
|
76
|
+
}
|
|
77
|
+
let start = buffer.byteLength - limit;
|
|
78
|
+
while (start < buffer.byteLength && (buffer[start] & 0xc0) === 0x80) {
|
|
79
|
+
start++;
|
|
80
|
+
}
|
|
81
|
+
return buffer.subarray(start);
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region Registry
|
|
85
|
+
/**
|
|
86
|
+
* Owns every terminal an ACP session created. One instance per session, torn
|
|
87
|
+
* down with the session so no child command outlives the step that spawned it.
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
export class TerminalRegistry {
|
|
91
|
+
shell;
|
|
92
|
+
defaultCwd;
|
|
93
|
+
terminals = new Map();
|
|
94
|
+
nextId = 0;
|
|
95
|
+
disposed = false;
|
|
96
|
+
constructor(shell, defaultCwd) {
|
|
97
|
+
this.shell = shell;
|
|
98
|
+
this.defaultCwd = defaultCwd;
|
|
99
|
+
}
|
|
100
|
+
/** Start a command and return its handle id. */
|
|
101
|
+
create(opts) {
|
|
102
|
+
if (this.disposed) {
|
|
103
|
+
// Closing the connection is supposed to guarantee no command outlives it;
|
|
104
|
+
// spawning one during or after teardown would orphan a child process.
|
|
105
|
+
throw new Error('cannot create a terminal: the ACP connection is closing');
|
|
106
|
+
}
|
|
107
|
+
const id = `acp-term-${this.nextId++}`;
|
|
108
|
+
const byteLimit = typeof opts.outputByteLimit === 'number' && opts.outputByteLimit > 0
|
|
109
|
+
? opts.outputByteLimit
|
|
110
|
+
: undefined;
|
|
111
|
+
const record = {
|
|
112
|
+
id,
|
|
113
|
+
abort: new AbortController(),
|
|
114
|
+
byteLimit,
|
|
115
|
+
done: Promise.resolve(),
|
|
116
|
+
output: Buffer.alloc(0),
|
|
117
|
+
truncated: false,
|
|
118
|
+
killed: false,
|
|
119
|
+
exitStatus: null,
|
|
120
|
+
};
|
|
121
|
+
this.terminals.set(id, record);
|
|
122
|
+
record.done = this.run(record, opts);
|
|
123
|
+
return id;
|
|
124
|
+
}
|
|
125
|
+
/** Drive one `exec` to completion, folding its output into the record. */
|
|
126
|
+
async run(record, opts) {
|
|
127
|
+
try {
|
|
128
|
+
const result = await this.shell.exec(buildCommandLine(opts.command, opts.args), {
|
|
129
|
+
cwd: opts.cwd ?? this.defaultCwd,
|
|
130
|
+
env: toEnvRecord(opts.env),
|
|
131
|
+
signal: record.abort.signal,
|
|
132
|
+
onData: (data) => {
|
|
133
|
+
appendChunk(record, data);
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
// A shell adapter that buffers rather than streams reports its output
|
|
137
|
+
// only here; append it when `onData` delivered nothing.
|
|
138
|
+
if (record.output.byteLength === 0) {
|
|
139
|
+
const combined = `${result.stdout}${result.stderr}`;
|
|
140
|
+
if (combined.length > 0) {
|
|
141
|
+
appendChunk(record, Buffer.from(combined, 'utf8'));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// `killed` is the authority, not how `exec` settled: the local adapter
|
|
145
|
+
// resolves normally on a signal-driven abort.
|
|
146
|
+
record.exitStatus = record.killed
|
|
147
|
+
? KILLED_STATUS
|
|
148
|
+
: {
|
|
149
|
+
exitCode: result.exitCode,
|
|
150
|
+
signal: null,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
record.exitStatus = record.killed ? KILLED_STATUS : exitStatusFromError(error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** Current output and exit status, or `null` when the handle is unknown. */
|
|
158
|
+
output(id) {
|
|
159
|
+
const record = this.terminals.get(id);
|
|
160
|
+
if (!record) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
output: record.output.toString('utf8'),
|
|
165
|
+
truncated: record.truncated,
|
|
166
|
+
exitStatus: record.exitStatus,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** Resolve once the command has exited. */
|
|
170
|
+
async waitForExit(id) {
|
|
171
|
+
const record = this.terminals.get(id);
|
|
172
|
+
if (!record) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
await record.done;
|
|
176
|
+
return record.exitStatus;
|
|
177
|
+
}
|
|
178
|
+
/** Kill the command but keep the handle readable, per the ACP contract. */
|
|
179
|
+
async kill(id) {
|
|
180
|
+
const record = this.terminals.get(id);
|
|
181
|
+
if (!record) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
record.killed = true;
|
|
185
|
+
record.abort.abort();
|
|
186
|
+
await record.done;
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
/** Kill the command and drop the handle. */
|
|
190
|
+
async release(id) {
|
|
191
|
+
const killed = await this.kill(id);
|
|
192
|
+
this.terminals.delete(id);
|
|
193
|
+
return killed;
|
|
194
|
+
}
|
|
195
|
+
/** Release every live terminal. Called when the session closes. */
|
|
196
|
+
async releaseAll() {
|
|
197
|
+
this.disposed = true;
|
|
198
|
+
const ids = [
|
|
199
|
+
...this.terminals.keys(),
|
|
200
|
+
];
|
|
201
|
+
await Promise.all(ids.map((id) => this.release(id)));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Append output, dropping the oldest BYTES once the cap is exceeded. */
|
|
205
|
+
function appendChunk(record, data) {
|
|
206
|
+
const combined = Buffer.concat([
|
|
207
|
+
record.output,
|
|
208
|
+
data,
|
|
209
|
+
]);
|
|
210
|
+
const limit = record.byteLimit;
|
|
211
|
+
if (limit === undefined) {
|
|
212
|
+
record.output = combined;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const trimmed = trimToLimit(combined, limit);
|
|
216
|
+
if (trimmed.byteLength < combined.byteLength) {
|
|
217
|
+
record.truncated = true;
|
|
218
|
+
}
|
|
219
|
+
record.output = trimmed;
|
|
220
|
+
}
|
|
221
|
+
//#endregion
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory duplex transport pair.
|
|
3
|
+
*
|
|
4
|
+
* Wires a Noetic ACP client directly to an `AgentSideConnection` running in the
|
|
5
|
+
* same process. Tests therefore exercise the **real** JSON-RPC wire protocol in
|
|
6
|
+
* both directions — capability negotiation, session updates, permission
|
|
7
|
+
* round-trips, `fs/*` and `terminal/*` callbacks — without spawning a process.
|
|
8
|
+
*/
|
|
9
|
+
import type { AcpTransport, AcpTransportFactory } 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
|
+
/** @public The two ends of a loopback connection. */
|
|
13
|
+
export interface AcpLoopbackPair {
|
|
14
|
+
/** Give this to `openAcpConnection`. */
|
|
15
|
+
client: AcpTransport;
|
|
16
|
+
/** Give this to an `AgentSideConnection`. */
|
|
17
|
+
agent: AcpTransport;
|
|
18
|
+
}
|
|
19
|
+
/** @public Create a connected pair of in-memory transports. */
|
|
20
|
+
export declare function createAcpLoopbackPair(): AcpLoopbackPair;
|
|
21
|
+
/**
|
|
22
|
+
* A transport factory that stands an in-process ACP agent on the far end.
|
|
23
|
+
*
|
|
24
|
+
* Pair it with {@link defineAcpAgent} to get an {@link AcpAgent} that behaves
|
|
25
|
+
* exactly like a spawned one — full handshake, sessions, notifications,
|
|
26
|
+
* client callbacks — with no process and no network. This is the recommended
|
|
27
|
+
* way to test an ACP-backed step.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export declare function loopbackTransport(toAgent: (conn: AgentSideConnection) => acp.Agent): AcpTransportFactory;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory duplex transport pair.
|
|
3
|
+
*
|
|
4
|
+
* Wires a Noetic ACP client directly to an `AgentSideConnection` running in the
|
|
5
|
+
* same process. Tests therefore exercise the **real** JSON-RPC wire protocol in
|
|
6
|
+
* both directions — capability negotiation, session updates, permission
|
|
7
|
+
* round-trips, `fs/*` and `terminal/*` callbacks — without spawning a process.
|
|
8
|
+
*/
|
|
9
|
+
import { AgentSideConnection, ndJsonStream } from '@zed-industries/agent-client-protocol';
|
|
10
|
+
function endpoint(readable, writable) {
|
|
11
|
+
let closed = false;
|
|
12
|
+
return {
|
|
13
|
+
readable,
|
|
14
|
+
writable,
|
|
15
|
+
async close() {
|
|
16
|
+
if (closed) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
closed = true;
|
|
20
|
+
// The peer's readable ends when this writable closes, which is how each
|
|
21
|
+
// side learns the connection is gone.
|
|
22
|
+
await writable.close().catch(() => undefined);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** @public Create a connected pair of in-memory transports. */
|
|
27
|
+
export function createAcpLoopbackPair() {
|
|
28
|
+
const clientToAgent = new TransformStream();
|
|
29
|
+
const agentToClient = new TransformStream();
|
|
30
|
+
return {
|
|
31
|
+
client: endpoint(agentToClient.readable, clientToAgent.writable),
|
|
32
|
+
agent: endpoint(clientToAgent.readable, agentToClient.writable),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A transport factory that stands an in-process ACP agent on the far end.
|
|
37
|
+
*
|
|
38
|
+
* Pair it with {@link defineAcpAgent} to get an {@link AcpAgent} that behaves
|
|
39
|
+
* exactly like a spawned one — full handshake, sessions, notifications,
|
|
40
|
+
* client callbacks — with no process and no network. This is the recommended
|
|
41
|
+
* way to test an ACP-backed step.
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export function loopbackTransport(toAgent) {
|
|
46
|
+
return async () => {
|
|
47
|
+
const pair = createAcpLoopbackPair();
|
|
48
|
+
// The connection registers its own message pump on construction; holding
|
|
49
|
+
// the reference keeps it alive for the life of the transport.
|
|
50
|
+
const connection = new AgentSideConnection(toAgent, ndJsonStream(pair.agent.writable, pair.agent.readable));
|
|
51
|
+
return {
|
|
52
|
+
readable: pair.client.readable,
|
|
53
|
+
writable: pair.client.writable,
|
|
54
|
+
async close() {
|
|
55
|
+
void connection;
|
|
56
|
+
await pair.client.close();
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
}
|
package/dist/turn.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accumulates the `session/update` notifications of one ACP prompt turn into an
|
|
3
|
+
* {@link AcpTurnResult}: Noetic conversation items, the assistant text, the
|
|
4
|
+
* agent's plan, its slash commands, and its final mode.
|
|
5
|
+
*
|
|
6
|
+
* All eight of ACP's update variants are handled; see `user_message_chunk`
|
|
7
|
+
* below for the one whose payload is deliberately not turned into an item.
|
|
8
|
+
*/
|
|
9
|
+
import type { AcpPlanEntry, AcpSessionNotification, AcpStopReason, AcpToolCallContent, AcpTurnResult, TokenUsage } from '@noetic-tools/types';
|
|
10
|
+
/**
|
|
11
|
+
* Render a tool call's ACP content as the string a `function_call_output` item
|
|
12
|
+
* carries. Diff and terminal content are rendered structurally rather than
|
|
13
|
+
* discarded, so the transcript keeps what the agent actually did.
|
|
14
|
+
*/
|
|
15
|
+
export declare function renderToolCallContent(content: ReadonlyArray<AcpToolCallContent>, rawOutput?: Record<string, unknown>): string;
|
|
16
|
+
/** @public Folds a turn's session updates into a result. */
|
|
17
|
+
export declare class AcpTurnAccumulator {
|
|
18
|
+
private text;
|
|
19
|
+
private reasoning;
|
|
20
|
+
private readonly toolCalls;
|
|
21
|
+
/** Insertion order, so items come out in the order the agent produced them. */
|
|
22
|
+
private readonly toolCallOrder;
|
|
23
|
+
private plan?;
|
|
24
|
+
private availableCommands?;
|
|
25
|
+
private currentModeId?;
|
|
26
|
+
/** Feed one `session/update` notification. */
|
|
27
|
+
push(notification: AcpSessionNotification): void;
|
|
28
|
+
/** Assistant text accumulated so far. */
|
|
29
|
+
get textContent(): string;
|
|
30
|
+
/** Reasoning text accumulated so far. */
|
|
31
|
+
get reasoningContent(): string;
|
|
32
|
+
/** Latest plan snapshot, when the agent reported one. */
|
|
33
|
+
get planEntries(): ReadonlyArray<AcpPlanEntry> | undefined;
|
|
34
|
+
/** Build the turn result. */
|
|
35
|
+
result(opts: {
|
|
36
|
+
stopReason: AcpStopReason;
|
|
37
|
+
usage?: TokenUsage;
|
|
38
|
+
cost?: number;
|
|
39
|
+
}): AcpTurnResult;
|
|
40
|
+
private apply;
|
|
41
|
+
private trackToolCall;
|
|
42
|
+
private mergeToolCall;
|
|
43
|
+
}
|
package/dist/turn.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accumulates the `session/update` notifications of one ACP prompt turn into an
|
|
3
|
+
* {@link AcpTurnResult}: Noetic conversation items, the assistant text, the
|
|
4
|
+
* agent's plan, its slash commands, and its final mode.
|
|
5
|
+
*
|
|
6
|
+
* All eight of ACP's update variants are handled; see `user_message_chunk`
|
|
7
|
+
* below for the one whose payload is deliberately not turned into an item.
|
|
8
|
+
*/
|
|
9
|
+
import { assistantMessageItem, contentBlockText, functionCallItem, functionCallOutputItem, } from './items.js';
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region Helpers
|
|
12
|
+
/**
|
|
13
|
+
* ACP marks "no change" on an update field with `null` and omits fields it is
|
|
14
|
+
* not touching, so both must be excluded before an assignment.
|
|
15
|
+
*/
|
|
16
|
+
function present(value) {
|
|
17
|
+
return value !== null && value !== undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Render a tool call's ACP content as the string a `function_call_output` item
|
|
21
|
+
* carries. Diff and terminal content are rendered structurally rather than
|
|
22
|
+
* discarded, so the transcript keeps what the agent actually did.
|
|
23
|
+
*/
|
|
24
|
+
export function renderToolCallContent(content, rawOutput) {
|
|
25
|
+
const parts = [];
|
|
26
|
+
for (const entry of content) {
|
|
27
|
+
if (entry.type === 'content') {
|
|
28
|
+
parts.push(contentBlockText(entry.content));
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (entry.type === 'diff') {
|
|
32
|
+
parts.push(`[diff ${entry.path}]\n${entry.newText}`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
parts.push(`[terminal ${entry.terminalId}]`);
|
|
36
|
+
}
|
|
37
|
+
if (parts.length === 0 && rawOutput !== undefined) {
|
|
38
|
+
return JSON.stringify(rawOutput);
|
|
39
|
+
}
|
|
40
|
+
return parts.join('\n');
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region Accumulator
|
|
44
|
+
/** @public Folds a turn's session updates into a result. */
|
|
45
|
+
export class AcpTurnAccumulator {
|
|
46
|
+
text = '';
|
|
47
|
+
reasoning = '';
|
|
48
|
+
toolCalls = new Map();
|
|
49
|
+
/** Insertion order, so items come out in the order the agent produced them. */
|
|
50
|
+
toolCallOrder = [];
|
|
51
|
+
plan;
|
|
52
|
+
availableCommands;
|
|
53
|
+
currentModeId;
|
|
54
|
+
/** Feed one `session/update` notification. */
|
|
55
|
+
push(notification) {
|
|
56
|
+
this.apply(notification.update);
|
|
57
|
+
}
|
|
58
|
+
/** Assistant text accumulated so far. */
|
|
59
|
+
get textContent() {
|
|
60
|
+
return this.text;
|
|
61
|
+
}
|
|
62
|
+
/** Reasoning text accumulated so far. */
|
|
63
|
+
get reasoningContent() {
|
|
64
|
+
return this.reasoning;
|
|
65
|
+
}
|
|
66
|
+
/** Latest plan snapshot, when the agent reported one. */
|
|
67
|
+
get planEntries() {
|
|
68
|
+
return this.plan;
|
|
69
|
+
}
|
|
70
|
+
/** Build the turn result. */
|
|
71
|
+
result(opts) {
|
|
72
|
+
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);
|
|
78
|
+
if (!call) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
items.push(functionCallItem({
|
|
82
|
+
title: call.title,
|
|
83
|
+
toolCallId: call.toolCallId,
|
|
84
|
+
rawInput: call.rawInput,
|
|
85
|
+
kind: call.kind,
|
|
86
|
+
}));
|
|
87
|
+
// A pending or in-progress call has no result yet — the turn ended
|
|
88
|
+
// before it settled, so it gets no output item.
|
|
89
|
+
if (call.status !== 'completed' && call.status !== 'failed') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
items.push(functionCallOutputItem({
|
|
93
|
+
toolCallId: call.toolCallId,
|
|
94
|
+
output: renderToolCallContent(call.content, call.rawOutput),
|
|
95
|
+
failed: call.status === 'failed',
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
stopReason: opts.stopReason,
|
|
100
|
+
items,
|
|
101
|
+
text: this.text,
|
|
102
|
+
usage: opts.usage,
|
|
103
|
+
cost: opts.cost,
|
|
104
|
+
plan: this.plan,
|
|
105
|
+
availableCommands: this.availableCommands,
|
|
106
|
+
currentModeId: this.currentModeId,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
//#region internals
|
|
110
|
+
apply(update) {
|
|
111
|
+
if (update.sessionUpdate === 'agent_message_chunk') {
|
|
112
|
+
this.text += contentBlockText(update.content);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (update.sessionUpdate === 'agent_thought_chunk') {
|
|
116
|
+
this.reasoning += contentBlockText(update.content);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (update.sessionUpdate === 'user_message_chunk') {
|
|
120
|
+
// Replayed history, sent when the agent reloads a session. It reaches the
|
|
121
|
+
// event stream but is not added to the turn's items: for a session this
|
|
122
|
+
// Noetic run started, the same messages are already in the item log and
|
|
123
|
+
// would double. For a `session.load` of a session Noetic never ran, that
|
|
124
|
+
// history is genuinely not captured here — see the note in the spec.
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (update.sessionUpdate === 'tool_call') {
|
|
128
|
+
this.trackToolCall(update);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (update.sessionUpdate === 'tool_call_update') {
|
|
132
|
+
this.mergeToolCall(update);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (update.sessionUpdate === 'plan') {
|
|
136
|
+
this.plan = [
|
|
137
|
+
...update.entries,
|
|
138
|
+
];
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (update.sessionUpdate === 'available_commands_update') {
|
|
142
|
+
this.availableCommands = [
|
|
143
|
+
...update.availableCommands,
|
|
144
|
+
];
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.currentModeId = update.currentModeId;
|
|
148
|
+
}
|
|
149
|
+
trackToolCall(update) {
|
|
150
|
+
if (!this.toolCalls.has(update.toolCallId)) {
|
|
151
|
+
this.toolCallOrder.push(update.toolCallId);
|
|
152
|
+
}
|
|
153
|
+
this.toolCalls.set(update.toolCallId, {
|
|
154
|
+
toolCallId: update.toolCallId,
|
|
155
|
+
title: update.title,
|
|
156
|
+
kind: update.kind,
|
|
157
|
+
rawInput: update.rawInput,
|
|
158
|
+
status: update.status ?? 'pending',
|
|
159
|
+
content: update.content
|
|
160
|
+
? [
|
|
161
|
+
...update.content,
|
|
162
|
+
]
|
|
163
|
+
: [],
|
|
164
|
+
rawOutput: update.rawOutput,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
mergeToolCall(update) {
|
|
168
|
+
const existing = this.toolCalls.get(update.toolCallId);
|
|
169
|
+
if (!existing) {
|
|
170
|
+
// An update for a call we never saw announced. Track it anyway rather
|
|
171
|
+
// than dropping the agent's work.
|
|
172
|
+
this.toolCallOrder.push(update.toolCallId);
|
|
173
|
+
this.toolCalls.set(update.toolCallId, {
|
|
174
|
+
toolCallId: update.toolCallId,
|
|
175
|
+
title: update.title ?? update.toolCallId,
|
|
176
|
+
kind: update.kind ?? undefined,
|
|
177
|
+
rawInput: update.rawInput,
|
|
178
|
+
status: update.status ?? 'pending',
|
|
179
|
+
content: update.content
|
|
180
|
+
? [
|
|
181
|
+
...update.content,
|
|
182
|
+
]
|
|
183
|
+
: [],
|
|
184
|
+
rawOutput: update.rawOutput,
|
|
185
|
+
});
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
// ACP sends `null` to mean "no change" and omits fields it is not touching,
|
|
189
|
+
// so only a genuinely present value overwrites what we already tracked.
|
|
190
|
+
if (present(update.title)) {
|
|
191
|
+
existing.title = update.title;
|
|
192
|
+
}
|
|
193
|
+
if (present(update.kind)) {
|
|
194
|
+
existing.kind = update.kind;
|
|
195
|
+
}
|
|
196
|
+
if (present(update.rawInput)) {
|
|
197
|
+
existing.rawInput = update.rawInput;
|
|
198
|
+
}
|
|
199
|
+
if (present(update.rawOutput)) {
|
|
200
|
+
existing.rawOutput = update.rawOutput;
|
|
201
|
+
}
|
|
202
|
+
if (present(update.status)) {
|
|
203
|
+
existing.status = update.status;
|
|
204
|
+
}
|
|
205
|
+
// ACP defines `content` on an update as a REPLACEMENT of the collection,
|
|
206
|
+
// not an append.
|
|
207
|
+
if (present(update.content)) {
|
|
208
|
+
existing.content = [
|
|
209
|
+
...update.content,
|
|
210
|
+
];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@noetic-tools/acp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Agent Client Protocol client for Noetic. Drives any ACP-speaking coding agent (Claude Code, Codex, Gemini CLI, …) as a step, serving fs/*, terminal/*, and permission requests from Noetic's own adapters.",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/mattapperson/noetic.git",
|
|
10
|
+
"directory": "packages/acp"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/mattapperson/noetic#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/mattapperson/noetic/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"noetic",
|
|
18
|
+
"agents",
|
|
19
|
+
"acp",
|
|
20
|
+
"agent-client-protocol",
|
|
21
|
+
"claude-code",
|
|
22
|
+
"codex",
|
|
23
|
+
"coding-agent"
|
|
24
|
+
],
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./stdio": {
|
|
33
|
+
"types": "./dist/stdio.d.ts",
|
|
34
|
+
"default": "./dist/stdio.js"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"bun-types": "latest"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"zod": "^4.0.0"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@noetic-tools/types": "^3.2.2",
|
|
54
|
+
"@zed-industries/agent-client-protocol": "^0.4.5",
|
|
55
|
+
"zod": "^4.0.0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "bunx tsc -p tsconfig.build.json && bun scripts/add-js-extensions.ts",
|
|
59
|
+
"verify:esm": "bun scripts/verify-node-esm.ts",
|
|
60
|
+
"prepublishOnly": "rm -rf dist && bun run build && bun run verify:esm && bun scripts/pin-workspace-deps.ts && bun scripts/strip-dev-conditions.ts",
|
|
61
|
+
"test": "bun test",
|
|
62
|
+
"test:coverage": "bun test --coverage --coverage-reporter=text --coverage-reporter=lcov",
|
|
63
|
+
"test:ci": "bun test --coverage --coverage-reporter=lcov",
|
|
64
|
+
"typecheck": "bunx tsc --noEmit",
|
|
65
|
+
"lint": "bunx biome check .",
|
|
66
|
+
"lint:fix": "bunx biome check --fix .",
|
|
67
|
+
"format": "bunx biome format --write .",
|
|
68
|
+
"format:check": "bunx biome format ."
|
|
69
|
+
}
|
|
70
|
+
}
|