@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
package/dist/define.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `defineAcpAgent` — the one-call constructor behind every agent preset.
|
|
3
|
+
*
|
|
4
|
+
* Under ACP an "adapter" is just a way to reach a JSON-RPC peer, so an agent is
|
|
5
|
+
* fully described by its id plus a transport factory. Everything else — the
|
|
6
|
+
* handshake, sessions, prompts, permissions, terminals — is protocol-generic
|
|
7
|
+
* and lives in {@link openAcpConnection}.
|
|
8
|
+
*/
|
|
9
|
+
import { openAcpConnection } from './connection.js';
|
|
10
|
+
/** @public Build an {@link AcpAgent} from a transport factory. */
|
|
11
|
+
export function defineAcpAgent(def) {
|
|
12
|
+
return {
|
|
13
|
+
specificationVersion: 'acp-v1',
|
|
14
|
+
agentId: def.agentId,
|
|
15
|
+
async connect(opts) {
|
|
16
|
+
const transport = await def.transport({
|
|
17
|
+
cwd: opts.host.cwd,
|
|
18
|
+
env: def.env,
|
|
19
|
+
signal: opts.signal ?? opts.host.signal,
|
|
20
|
+
});
|
|
21
|
+
return await openAcpConnection({
|
|
22
|
+
agentId: def.agentId,
|
|
23
|
+
transport,
|
|
24
|
+
host: opts.host,
|
|
25
|
+
signal: opts.signal ?? opts.host.signal,
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@noetic-tools/acp` — an Agent Client Protocol client for Noetic.
|
|
3
|
+
*
|
|
4
|
+
* Drives any ACP-speaking coding agent as a step, and answers the protocol's
|
|
5
|
+
* client-side responsibilities (`fs/*`, `terminal/*`,
|
|
6
|
+
* `session/request_permission`) from Noetic's own adapters — with `fs/*` paths
|
|
7
|
+
* confined to the session working directory by default, since ACP places
|
|
8
|
+
* boundary enforcement on the client.
|
|
9
|
+
*
|
|
10
|
+
* The Node stdio transport lives at the `@noetic-tools/acp/stdio` subpath; this
|
|
11
|
+
* entry point is runtime-neutral.
|
|
12
|
+
*
|
|
13
|
+
* Everything exported here is public API. Internal helpers — the item builders,
|
|
14
|
+
* the turn accumulator, the terminal registry, the permission engine, the
|
|
15
|
+
* capability and path assertions — are deliberately not re-exported: they are
|
|
16
|
+
* implementation detail of the client and are free to change.
|
|
17
|
+
*/
|
|
18
|
+
/** @public */
|
|
19
|
+
export type { AcpPresetOptions, AcpProcessSpec } from './agents.js';
|
|
20
|
+
/** @public */
|
|
21
|
+
export { claudeCode, codex, customAcpAgent, gemini, opencode, pi } from './agents.js';
|
|
22
|
+
/** @public */
|
|
23
|
+
export type { DefineAcpAgentOptions } from './define.js';
|
|
24
|
+
/** @public */
|
|
25
|
+
export { defineAcpAgent } from './define.js';
|
|
26
|
+
/** @public */
|
|
27
|
+
export type { AcpAgentRegistry } from './registry.js';
|
|
28
|
+
/** @public */
|
|
29
|
+
export { createAcpAgentRegistry } from './registry.js';
|
|
30
|
+
/** @public */
|
|
31
|
+
export type { AcpLoopbackPair } from './transport-loopback.js';
|
|
32
|
+
/** @public */
|
|
33
|
+
export { createAcpLoopbackPair, loopbackTransport } from './transport-loopback.js';
|
|
34
|
+
/** @public */
|
|
35
|
+
export type { AcpPermissionPrompt, AcpPermissionReply, AskUserForPermissionOptions, } from './permission-channel.js';
|
|
36
|
+
/** @public */
|
|
37
|
+
export { ACP_PERMISSION_SCOPE, AcpPermissionPromptSchema, AcpPermissionReplySchema, acpPermissionDecisions, acpPermissionRequests, askUserForPermission, resolveAcpPermission, } from './permission-channel.js';
|
|
38
|
+
/**
|
|
39
|
+
* Exposed so a host writing a constraining `FsAdapter` can apply the same rules
|
|
40
|
+
* the client does — including resolving symlinks, which the client's lexical
|
|
41
|
+
* check deliberately does not do.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export { isAbsolutePath, isWithinRoots, normalizePath } from './paths.js';
|
|
45
|
+
/** @public */
|
|
46
|
+
export type { NoeticAcpClientOptions } from './client.js';
|
|
47
|
+
/** @public */
|
|
48
|
+
export { NoeticAcpClient } from './client.js';
|
|
49
|
+
/**
|
|
50
|
+
* For embedding an ACP connection outside a Noetic step — a custom runtime, a
|
|
51
|
+
* bridge, a test harness. `step.acpAgent` is the supported path; these are the
|
|
52
|
+
* pieces underneath it.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export type { OpenAcpConnectionOptions } from './connection.js';
|
|
56
|
+
/** @public */
|
|
57
|
+
export { openAcpConnection } from './connection.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@noetic-tools/acp` — an Agent Client Protocol client for Noetic.
|
|
3
|
+
*
|
|
4
|
+
* Drives any ACP-speaking coding agent as a step, and answers the protocol's
|
|
5
|
+
* client-side responsibilities (`fs/*`, `terminal/*`,
|
|
6
|
+
* `session/request_permission`) from Noetic's own adapters — with `fs/*` paths
|
|
7
|
+
* confined to the session working directory by default, since ACP places
|
|
8
|
+
* boundary enforcement on the client.
|
|
9
|
+
*
|
|
10
|
+
* The Node stdio transport lives at the `@noetic-tools/acp/stdio` subpath; this
|
|
11
|
+
* entry point is runtime-neutral.
|
|
12
|
+
*
|
|
13
|
+
* Everything exported here is public API. Internal helpers — the item builders,
|
|
14
|
+
* the turn accumulator, the terminal registry, the permission engine, the
|
|
15
|
+
* capability and path assertions — are deliberately not re-exported: they are
|
|
16
|
+
* implementation detail of the client and are free to change.
|
|
17
|
+
*/
|
|
18
|
+
/** @public */
|
|
19
|
+
export { claudeCode, codex, customAcpAgent, gemini, opencode, pi } from './agents.js';
|
|
20
|
+
/** @public */
|
|
21
|
+
export { defineAcpAgent } from './define.js';
|
|
22
|
+
/** @public */
|
|
23
|
+
export { createAcpAgentRegistry } from './registry.js';
|
|
24
|
+
/** @public */
|
|
25
|
+
export { createAcpLoopbackPair, loopbackTransport } from './transport-loopback.js';
|
|
26
|
+
/** @public */
|
|
27
|
+
export { ACP_PERMISSION_SCOPE, AcpPermissionPromptSchema, AcpPermissionReplySchema, acpPermissionDecisions, acpPermissionRequests, askUserForPermission, resolveAcpPermission, } from './permission-channel.js';
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region Path confinement
|
|
30
|
+
/**
|
|
31
|
+
* Exposed so a host writing a constraining `FsAdapter` can apply the same rules
|
|
32
|
+
* the client does — including resolving symlinks, which the client's lexical
|
|
33
|
+
* check deliberately does not do.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export { isAbsolutePath, isWithinRoots, normalizePath } from './paths.js';
|
|
37
|
+
/** @public */
|
|
38
|
+
export { NoeticAcpClient } from './client.js';
|
|
39
|
+
/** @public */
|
|
40
|
+
export { openAcpConnection } from './connection.js';
|
|
41
|
+
//#endregion
|
package/dist/items.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builders that turn ACP session output into Noetic conversation `Item`s. The
|
|
3
|
+
* shapes mirror the OpenRouter output-item schema the runtime stores;
|
|
4
|
+
* construction goes through `frameworkCast` because those provider types are
|
|
5
|
+
* not publicly constructible.
|
|
6
|
+
*/
|
|
7
|
+
import type { AcpContentBlock, FunctionCallItem, FunctionCallOutputItem, Item, MessageItem } from '@noetic-tools/types';
|
|
8
|
+
/** @public Build an assistant message Item from plain text. */
|
|
9
|
+
export declare function assistantMessageItem(text: string, id?: string): MessageItem;
|
|
10
|
+
/**
|
|
11
|
+
* Build a function-call Item recording a tool the agent invoked.
|
|
12
|
+
*
|
|
13
|
+
* ACP identifies a tool call by a human-readable `title` plus a `kind`
|
|
14
|
+
* classification — there is no machine tool name on the wire — so `title`
|
|
15
|
+
* becomes the item's `name` and `kind` rides along in the arguments payload.
|
|
16
|
+
*/
|
|
17
|
+
export declare function functionCallItem(opts: {
|
|
18
|
+
title: string;
|
|
19
|
+
toolCallId: string;
|
|
20
|
+
rawInput?: Record<string, unknown>;
|
|
21
|
+
kind?: string;
|
|
22
|
+
}): FunctionCallItem;
|
|
23
|
+
/** @public Build the tool-result Item paired with a completed ACP tool call. */
|
|
24
|
+
export declare function functionCallOutputItem(opts: {
|
|
25
|
+
toolCallId: string;
|
|
26
|
+
output: string;
|
|
27
|
+
failed?: boolean;
|
|
28
|
+
}): FunctionCallOutputItem;
|
|
29
|
+
/**
|
|
30
|
+
* Flatten an ACP content block to plain text. Non-text blocks are rendered as a
|
|
31
|
+
* short descriptor rather than dropped, so nothing the agent sent disappears
|
|
32
|
+
* from the transcript.
|
|
33
|
+
*/
|
|
34
|
+
export declare function contentBlockText(block: AcpContentBlock): string;
|
|
35
|
+
/** @public Coerce an arbitrary item collection to the `Item[]` type. */
|
|
36
|
+
export declare function asItems(items: ReadonlyArray<unknown>): Item[];
|
package/dist/items.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builders that turn ACP session output into Noetic conversation `Item`s. The
|
|
3
|
+
* shapes mirror the OpenRouter output-item schema the runtime stores;
|
|
4
|
+
* construction goes through `frameworkCast` because those provider types are
|
|
5
|
+
* not publicly constructible.
|
|
6
|
+
*/
|
|
7
|
+
import { frameworkCast } from '@noetic-tools/types';
|
|
8
|
+
/** @public Build an assistant message Item from plain text. */
|
|
9
|
+
export function assistantMessageItem(text, id) {
|
|
10
|
+
return frameworkCast({
|
|
11
|
+
id: id ?? `acp-msg-${crypto.randomUUID()}`,
|
|
12
|
+
type: 'message',
|
|
13
|
+
status: 'completed',
|
|
14
|
+
role: 'assistant',
|
|
15
|
+
content: [
|
|
16
|
+
{
|
|
17
|
+
type: 'output_text',
|
|
18
|
+
text,
|
|
19
|
+
annotations: [],
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Build a function-call Item recording a tool the agent invoked.
|
|
26
|
+
*
|
|
27
|
+
* ACP identifies a tool call by a human-readable `title` plus a `kind`
|
|
28
|
+
* classification — there is no machine tool name on the wire — so `title`
|
|
29
|
+
* becomes the item's `name` and `kind` rides along in the arguments payload.
|
|
30
|
+
*/
|
|
31
|
+
export function functionCallItem(opts) {
|
|
32
|
+
const args = JSON.stringify(opts.rawInput ?? {});
|
|
33
|
+
return frameworkCast({
|
|
34
|
+
id: opts.toolCallId,
|
|
35
|
+
type: 'function_call',
|
|
36
|
+
status: 'completed',
|
|
37
|
+
name: opts.title,
|
|
38
|
+
callId: opts.toolCallId,
|
|
39
|
+
arguments: args,
|
|
40
|
+
acpToolKind: opts.kind,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** @public Build the tool-result Item paired with a completed ACP tool call. */
|
|
44
|
+
export function functionCallOutputItem(opts) {
|
|
45
|
+
return frameworkCast({
|
|
46
|
+
id: `acp-tool-output-${opts.toolCallId}`,
|
|
47
|
+
type: 'function_call_output',
|
|
48
|
+
status: opts.failed === true ? 'failed' : 'completed',
|
|
49
|
+
callId: opts.toolCallId,
|
|
50
|
+
output: opts.output,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Flatten an ACP content block to plain text. Non-text blocks are rendered as a
|
|
55
|
+
* short descriptor rather than dropped, so nothing the agent sent disappears
|
|
56
|
+
* from the transcript.
|
|
57
|
+
*/
|
|
58
|
+
export function contentBlockText(block) {
|
|
59
|
+
if (block.type === 'text') {
|
|
60
|
+
return block.text;
|
|
61
|
+
}
|
|
62
|
+
if (block.type === 'resource_link') {
|
|
63
|
+
return `[resource: ${block.uri}]`;
|
|
64
|
+
}
|
|
65
|
+
if (block.type === 'resource') {
|
|
66
|
+
const resource = block.resource;
|
|
67
|
+
return 'text' in resource ? resource.text : `[resource: ${resource.uri}]`;
|
|
68
|
+
}
|
|
69
|
+
return `[${block.type}: ${block.mimeType}]`;
|
|
70
|
+
}
|
|
71
|
+
/** @public Coerce an arbitrary item collection to the `Item[]` type. */
|
|
72
|
+
export function asItems(items) {
|
|
73
|
+
return frameworkCast([
|
|
74
|
+
...items,
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping a request from outliving the agent that was supposed to answer it.
|
|
3
|
+
*
|
|
4
|
+
* The protocol library's read loop simply breaks when the stream ends:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const { value: message, done } = await reader.read();
|
|
8
|
+
* if (done) { break; }
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Nothing rejects the responses still pending at that moment, so if the agent's
|
|
12
|
+
* stdout closes — a missing binary, a crash on startup, an OOM mid-turn — every
|
|
13
|
+
* in-flight `initialize` or `prompt` waits forever. A step built on it hangs
|
|
14
|
+
* with no error and no timeout, and aborting the context does not help because
|
|
15
|
+
* the promise it would race against never settles either.
|
|
16
|
+
*
|
|
17
|
+
* This watches the readable for end-of-stream and turns it into a rejection,
|
|
18
|
+
* and does the same for an abort signal, so a dead agent surfaces as an error
|
|
19
|
+
* instead of silence.
|
|
20
|
+
*/
|
|
21
|
+
import type { AcpTransport } from '@noetic-tools/types';
|
|
22
|
+
/** @public A transport whose end-of-stream is observable. */
|
|
23
|
+
export interface WatchedTransport {
|
|
24
|
+
/** The readable to hand to `ndJsonStream`, instrumented for end-of-stream. */
|
|
25
|
+
readable: ReadableStream<Uint8Array>;
|
|
26
|
+
/**
|
|
27
|
+
* Rejects when the agent's stream ends or the signal aborts. Never resolves,
|
|
28
|
+
* so it is safe to `Promise.race` against any request.
|
|
29
|
+
*/
|
|
30
|
+
death: Promise<never>;
|
|
31
|
+
/**
|
|
32
|
+
* Report the connection as gone. A deliberate close must reject in-flight
|
|
33
|
+
* requests too — once the transport is down nothing can ever answer them,
|
|
34
|
+
* so leaving them pending would hang the caller just as a crash would.
|
|
35
|
+
*/
|
|
36
|
+
shutdown(reason?: string): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Instrument a transport so the death of the agent becomes an error.
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
42
|
+
* @param agentId - Named in the resulting {@link AcpConnectError}.
|
|
43
|
+
* @param transport - The transport whose readable is being consumed.
|
|
44
|
+
* @param signal - Aborting this counts as death for in-flight requests.
|
|
45
|
+
*/
|
|
46
|
+
export declare function watchTransport(agentId: string, transport: AcpTransport, signal?: AbortSignal): WatchedTransport;
|
package/dist/liveness.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping a request from outliving the agent that was supposed to answer it.
|
|
3
|
+
*
|
|
4
|
+
* The protocol library's read loop simply breaks when the stream ends:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const { value: message, done } = await reader.read();
|
|
8
|
+
* if (done) { break; }
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Nothing rejects the responses still pending at that moment, so if the agent's
|
|
12
|
+
* stdout closes — a missing binary, a crash on startup, an OOM mid-turn — every
|
|
13
|
+
* in-flight `initialize` or `prompt` waits forever. A step built on it hangs
|
|
14
|
+
* with no error and no timeout, and aborting the context does not help because
|
|
15
|
+
* the promise it would race against never settles either.
|
|
16
|
+
*
|
|
17
|
+
* This watches the readable for end-of-stream and turns it into a rejection,
|
|
18
|
+
* and does the same for an abort signal, so a dead agent surfaces as an error
|
|
19
|
+
* instead of silence.
|
|
20
|
+
*/
|
|
21
|
+
import { AcpConnectError } from '@noetic-tools/types';
|
|
22
|
+
/**
|
|
23
|
+
* Instrument a transport so the death of the agent becomes an error.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
* @param agentId - Named in the resulting {@link AcpConnectError}.
|
|
27
|
+
* @param transport - The transport whose readable is being consumed.
|
|
28
|
+
* @param signal - Aborting this counts as death for in-flight requests.
|
|
29
|
+
*/
|
|
30
|
+
export function watchTransport(agentId, transport, signal) {
|
|
31
|
+
// Doubles as the "already dead" flag: cleared on the first `die`, so the
|
|
32
|
+
// rejection can only ever fire once.
|
|
33
|
+
let reject;
|
|
34
|
+
const death = new Promise((_resolve, rejectFn) => {
|
|
35
|
+
reject = rejectFn;
|
|
36
|
+
});
|
|
37
|
+
// Nothing may await `death` on its own — it only ever rejects, and an
|
|
38
|
+
// unconsumed rejection would surface as an unhandled rejection the moment
|
|
39
|
+
// the agent exits normally.
|
|
40
|
+
death.catch(() => undefined);
|
|
41
|
+
const die = (reason) => {
|
|
42
|
+
const rejectDeath = reject;
|
|
43
|
+
if (!rejectDeath) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
reject = undefined;
|
|
47
|
+
// Whatever killed the connection, the signal can no longer tell us
|
|
48
|
+
// anything — drop the listener so it stops pinning this scope.
|
|
49
|
+
signal?.removeEventListener('abort', onAbort);
|
|
50
|
+
rejectDeath(new AcpConnectError({
|
|
51
|
+
agentId,
|
|
52
|
+
message: `ACP agent '${agentId}' ${reason}, so in-flight requests can never be answered.`,
|
|
53
|
+
}));
|
|
54
|
+
};
|
|
55
|
+
const onAbort = () => die('was aborted');
|
|
56
|
+
signal?.addEventListener('abort', onAbort, {
|
|
57
|
+
once: true,
|
|
58
|
+
});
|
|
59
|
+
// A pass-through that reports end-of-stream: `flush` fires once the agent's
|
|
60
|
+
// readable is exhausted, however it ended.
|
|
61
|
+
const readable = transport.readable.pipeThrough(new TransformStream({
|
|
62
|
+
flush() {
|
|
63
|
+
die('closed its output stream');
|
|
64
|
+
},
|
|
65
|
+
}));
|
|
66
|
+
return {
|
|
67
|
+
readable,
|
|
68
|
+
death,
|
|
69
|
+
shutdown(reason = 'connection was closed') {
|
|
70
|
+
die(reason);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
package/dist/paths.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Confining an agent's filesystem access to the workspace.
|
|
3
|
+
*
|
|
4
|
+
* ACP puts boundary enforcement on the **client**: the agent asks for a path
|
|
5
|
+
* and the client decides whether it may have it. Nothing in the protocol
|
|
6
|
+
* constrains what it asks for, and a `permissions` policy does not help — that
|
|
7
|
+
* answers `session/request_permission`, which covers the agent's *tool calls*,
|
|
8
|
+
* not the `fs/*` methods it invokes on us directly. An agent that simply never
|
|
9
|
+
* asks is never gated by it.
|
|
10
|
+
*
|
|
11
|
+
* So the confinement has to live here. By default an agent reaches the session
|
|
12
|
+
* working directory and nothing else; `additionalDirectories` widens that, and
|
|
13
|
+
* `allowAnyPath` removes it for callers who genuinely want an unconfined agent.
|
|
14
|
+
*
|
|
15
|
+
* Path handling is deliberately lexical — no `node:path`, no symlink
|
|
16
|
+
* resolution — so this module stays runtime-neutral. See
|
|
17
|
+
* {@link isWithinRoots} for what that does and does not stop.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* The specification requires every path an agent sends to be absolute. A
|
|
21
|
+
* relative path is therefore malformed input, not something to resolve against
|
|
22
|
+
* a guessed base.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare function isAbsolutePath(path: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Collapse `.`, `..`, duplicate separators, and backslashes into a canonical
|
|
28
|
+
* form. Lexical only: `..` is resolved by removing the preceding segment
|
|
29
|
+
* rather than by asking the filesystem, so it cannot be walked past the root.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare function normalizePath(path: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* True when `path` is inside one of `roots` (or is a root itself).
|
|
35
|
+
*
|
|
36
|
+
* **What this stops:** absolute paths outside the workspace, and `..` traversal
|
|
37
|
+
* out of it, including forms like `/work/../../etc/passwd`.
|
|
38
|
+
*
|
|
39
|
+
* **What it does not stop:** a symlink *inside* the workspace pointing outside
|
|
40
|
+
* it. Resolving that needs the filesystem, which this module deliberately does
|
|
41
|
+
* not touch; a caller who needs it should supply an `FsAdapter` that resolves
|
|
42
|
+
* and re-checks. Nor does it constrain what a terminal command does once
|
|
43
|
+
* running — see the note on terminals in the ACP docs.
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export declare function isWithinRoots(path: string, roots: ReadonlyArray<string>): boolean;
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Confining an agent's filesystem access to the workspace.
|
|
3
|
+
*
|
|
4
|
+
* ACP puts boundary enforcement on the **client**: the agent asks for a path
|
|
5
|
+
* and the client decides whether it may have it. Nothing in the protocol
|
|
6
|
+
* constrains what it asks for, and a `permissions` policy does not help — that
|
|
7
|
+
* answers `session/request_permission`, which covers the agent's *tool calls*,
|
|
8
|
+
* not the `fs/*` methods it invokes on us directly. An agent that simply never
|
|
9
|
+
* asks is never gated by it.
|
|
10
|
+
*
|
|
11
|
+
* So the confinement has to live here. By default an agent reaches the session
|
|
12
|
+
* working directory and nothing else; `additionalDirectories` widens that, and
|
|
13
|
+
* `allowAnyPath` removes it for callers who genuinely want an unconfined agent.
|
|
14
|
+
*
|
|
15
|
+
* Path handling is deliberately lexical — no `node:path`, no symlink
|
|
16
|
+
* resolution — so this module stays runtime-neutral. See
|
|
17
|
+
* {@link isWithinRoots} for what that does and does not stop.
|
|
18
|
+
*/
|
|
19
|
+
//#region Absolute paths
|
|
20
|
+
const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/;
|
|
21
|
+
/**
|
|
22
|
+
* The specification requires every path an agent sends to be absolute. A
|
|
23
|
+
* relative path is therefore malformed input, not something to resolve against
|
|
24
|
+
* a guessed base.
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export function isAbsolutePath(path) {
|
|
28
|
+
return path.startsWith('/') || WINDOWS_ABSOLUTE.test(path);
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region Normalization
|
|
32
|
+
/**
|
|
33
|
+
* Collapse `.`, `..`, duplicate separators, and backslashes into a canonical
|
|
34
|
+
* form. Lexical only: `..` is resolved by removing the preceding segment
|
|
35
|
+
* rather than by asking the filesystem, so it cannot be walked past the root.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export function normalizePath(path) {
|
|
39
|
+
const windowsRoot = WINDOWS_ABSOLUTE.test(path) ? path.slice(0, 2).toUpperCase() : '';
|
|
40
|
+
const body = windowsRoot ? path.slice(2) : path;
|
|
41
|
+
const segments = [];
|
|
42
|
+
for (const segment of body.replace(/\\/g, '/').split('/')) {
|
|
43
|
+
if (segment === '' || segment === '.') {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (segment === '..') {
|
|
47
|
+
// Popping at the root is a no-op, so `/../../etc` normalizes to `/etc`
|
|
48
|
+
// rather than escaping above it.
|
|
49
|
+
segments.pop();
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
segments.push(segment);
|
|
53
|
+
}
|
|
54
|
+
return `${windowsRoot}/${segments.join('/')}`;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region Confinement
|
|
58
|
+
/**
|
|
59
|
+
* True when `path` is inside one of `roots` (or is a root itself).
|
|
60
|
+
*
|
|
61
|
+
* **What this stops:** absolute paths outside the workspace, and `..` traversal
|
|
62
|
+
* out of it, including forms like `/work/../../etc/passwd`.
|
|
63
|
+
*
|
|
64
|
+
* **What it does not stop:** a symlink *inside* the workspace pointing outside
|
|
65
|
+
* it. Resolving that needs the filesystem, which this module deliberately does
|
|
66
|
+
* not touch; a caller who needs it should supply an `FsAdapter` that resolves
|
|
67
|
+
* and re-checks. Nor does it constrain what a terminal command does once
|
|
68
|
+
* running — see the note on terminals in the ACP docs.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export function isWithinRoots(path, roots) {
|
|
72
|
+
const target = normalizePath(path);
|
|
73
|
+
return roots.some((root) => {
|
|
74
|
+
const normalized = normalizePath(root);
|
|
75
|
+
if (target === normalized) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
// The separator matters: `/workspace-secrets` must not count as inside
|
|
79
|
+
// `/workspace`.
|
|
80
|
+
const prefix = normalized.endsWith('/') ? normalized : `${normalized}/`;
|
|
81
|
+
return target.startsWith(prefix);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Routing an ACP agent's permission requests to a human.
|
|
3
|
+
*
|
|
4
|
+
* ACP makes `session/request_permission` a client responsibility, but a
|
|
5
|
+
* declarative policy can only answer what it was told in advance. When the
|
|
6
|
+
* decision belongs to a person, the request has to leave the process and an
|
|
7
|
+
* answer has to come back — which is what external channels are for.
|
|
8
|
+
*
|
|
9
|
+
* The shape mirrors `@noetic-tools/chat-sdk`'s tool-approval flow, deliberately:
|
|
10
|
+
*
|
|
11
|
+
* 1. A step's handler sends an {@link AcpPermissionPrompt} on
|
|
12
|
+
* {@link acpPermissionRequests} and parks on {@link acpPermissionDecisions}.
|
|
13
|
+
* 2. The integration subscribes ONCE per harness —
|
|
14
|
+
* `harness.getChannelStream(acpPermissionRequests, ACP_PERMISSION_SCOPE)` —
|
|
15
|
+
* and shows the request to the user. One subscriber, because queue delivery
|
|
16
|
+
* is competing-consumer: a second would steal requests.
|
|
17
|
+
* 3. The answer calls {@link resolveAcpPermission}, which broadcasts on the
|
|
18
|
+
* decision topic; the parked handler matching `requestId` unparks.
|
|
19
|
+
*
|
|
20
|
+
* Requests use a queue (each belongs to exactly one reviewer) and decisions a
|
|
21
|
+
* topic (broadcast, each waiter filters for its own `requestId`) — per-request
|
|
22
|
+
* channels would leak one channel state per prompt for the harness lifetime.
|
|
23
|
+
*/
|
|
24
|
+
import type { AcpPermissionHandler, AcpPermissionOutcome, ChannelHandle, ExternalChannel } from '@noetic-tools/types';
|
|
25
|
+
import { z } from 'zod';
|
|
26
|
+
/**
|
|
27
|
+
* Lifetime scope id for permission subscriptions and write handles. No
|
|
28
|
+
* execution ever runs under it, so the stream and handle stay open until the
|
|
29
|
+
* integration ends them itself.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare const ACP_PERMISSION_SCOPE = "acp:permissions";
|
|
33
|
+
/** @public A permission request on its way to a human. */
|
|
34
|
+
export declare const AcpPermissionPromptSchema: z.ZodObject<{
|
|
35
|
+
requestId: z.ZodString;
|
|
36
|
+
agentId: z.ZodString;
|
|
37
|
+
sessionId: z.ZodString;
|
|
38
|
+
threadId: z.ZodString;
|
|
39
|
+
stepId: z.ZodString;
|
|
40
|
+
title: z.ZodString;
|
|
41
|
+
kind: z.ZodOptional<z.ZodString>;
|
|
42
|
+
rawInput: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
43
|
+
options: z.ZodArray<z.ZodObject<{
|
|
44
|
+
optionId: z.ZodString;
|
|
45
|
+
name: z.ZodString;
|
|
46
|
+
kind: z.ZodEnum<{
|
|
47
|
+
allow_always: "allow_always";
|
|
48
|
+
allow_once: "allow_once";
|
|
49
|
+
reject_always: "reject_always";
|
|
50
|
+
reject_once: "reject_once";
|
|
51
|
+
}>;
|
|
52
|
+
}, z.core.$strip>>;
|
|
53
|
+
}, z.core.$strip>;
|
|
54
|
+
/** @public A permission request awaiting a human answer. */
|
|
55
|
+
export type AcpPermissionPrompt = z.infer<typeof AcpPermissionPromptSchema>;
|
|
56
|
+
/** @public The human's answer to an {@link AcpPermissionPrompt}. */
|
|
57
|
+
export declare const AcpPermissionReplySchema: z.ZodObject<{
|
|
58
|
+
requestId: z.ZodString;
|
|
59
|
+
decision: z.ZodEnum<{
|
|
60
|
+
allow: "allow";
|
|
61
|
+
cancel: "cancel";
|
|
62
|
+
deny: "deny";
|
|
63
|
+
}>;
|
|
64
|
+
optionId: z.ZodOptional<z.ZodString>;
|
|
65
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
66
|
+
}, z.core.$strip>;
|
|
67
|
+
/** @public The human's answer to a permission prompt. */
|
|
68
|
+
export type AcpPermissionReply = z.infer<typeof AcpPermissionReplySchema>;
|
|
69
|
+
/** @public Permission requests from every ACP step of a harness, in the order they parked. */
|
|
70
|
+
export declare const acpPermissionRequests: ExternalChannel<AcpPermissionPrompt>;
|
|
71
|
+
/** @public Decisions broadcast to every parked handler; each filters by its own `requestId`. */
|
|
72
|
+
export declare const acpPermissionDecisions: ExternalChannel<AcpPermissionReply>;
|
|
73
|
+
/** @public Options for {@link askUserForPermission}. */
|
|
74
|
+
export interface AskUserForPermissionOptions {
|
|
75
|
+
/**
|
|
76
|
+
* How long to wait for an answer, in milliseconds. On expiry the handler
|
|
77
|
+
* resolves with {@link onTimeout} rather than hanging the agent's turn
|
|
78
|
+
* forever. Defaults to 5 minutes.
|
|
79
|
+
*/
|
|
80
|
+
timeout?: number;
|
|
81
|
+
/**
|
|
82
|
+
* What an unanswered prompt means. Defaults to denying: an unattended
|
|
83
|
+
* request must not become an approval by running out the clock.
|
|
84
|
+
*/
|
|
85
|
+
onTimeout?: AcpPermissionOutcome;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Build an {@link AcpPermissionHandler} that asks a human over the channel pair
|
|
89
|
+
* above. Pass it as a step's `onPermissionRequest`; it runs after the step's
|
|
90
|
+
* declarative policy and steering have both abstained.
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
export declare function askUserForPermission(opts?: AskUserForPermissionOptions): AcpPermissionHandler;
|
|
94
|
+
/**
|
|
95
|
+
* Answer a prompt from outside the execution. Get the handle with
|
|
96
|
+
* `harness.getChannelHandle(acpPermissionDecisions, ACP_PERMISSION_SCOPE)`.
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
export declare function resolveAcpPermission(handle: ChannelHandle<AcpPermissionReply>, reply: AcpPermissionReply): void;
|