@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,172 @@
|
|
|
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 { z } from 'zod';
|
|
25
|
+
//#region Scope
|
|
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 const ACP_PERMISSION_SCOPE = 'acp:permissions';
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region Schemas
|
|
35
|
+
const PermissionOptionSchema = z.object({
|
|
36
|
+
optionId: z.string(),
|
|
37
|
+
name: z.string(),
|
|
38
|
+
kind: z.enum([
|
|
39
|
+
'allow_once',
|
|
40
|
+
'allow_always',
|
|
41
|
+
'reject_once',
|
|
42
|
+
'reject_always',
|
|
43
|
+
]),
|
|
44
|
+
});
|
|
45
|
+
/** @public A permission request on its way to a human. */
|
|
46
|
+
export const AcpPermissionPromptSchema = z.object({
|
|
47
|
+
/** Correlates the answer with this request. */
|
|
48
|
+
requestId: z.string(),
|
|
49
|
+
/** Which agent is asking (`agentId` of the adapter). */
|
|
50
|
+
agentId: z.string(),
|
|
51
|
+
/** ACP session the request belongs to. */
|
|
52
|
+
sessionId: z.string(),
|
|
53
|
+
/** Harness thread, so a chat integration can route the prompt to the right conversation. */
|
|
54
|
+
threadId: z.string(),
|
|
55
|
+
/** The step whose turn triggered the request. */
|
|
56
|
+
stepId: z.string(),
|
|
57
|
+
/** Human-readable description of what the agent wants to do. */
|
|
58
|
+
title: z.string(),
|
|
59
|
+
/** ACP tool classification (`read`, `edit`, `execute`, …), when the agent set one. */
|
|
60
|
+
kind: z.string().optional(),
|
|
61
|
+
/** Raw tool input, for a UI that wants to show the command or diff. */
|
|
62
|
+
rawInput: z.record(z.string(), z.unknown()).optional(),
|
|
63
|
+
/** Exactly the options the agent offered — a UI should present these, not invent its own. */
|
|
64
|
+
options: z.array(PermissionOptionSchema),
|
|
65
|
+
});
|
|
66
|
+
/** @public The human's answer to an {@link AcpPermissionPrompt}. */
|
|
67
|
+
export const AcpPermissionReplySchema = z.object({
|
|
68
|
+
requestId: z.string(),
|
|
69
|
+
decision: z.enum([
|
|
70
|
+
'allow',
|
|
71
|
+
'deny',
|
|
72
|
+
'cancel',
|
|
73
|
+
]),
|
|
74
|
+
/** Pin an exact option the agent offered. Omit to let the client choose by `decision`. */
|
|
75
|
+
optionId: z.string().optional(),
|
|
76
|
+
reason: z.string().optional(),
|
|
77
|
+
});
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region Channels
|
|
80
|
+
/** @public Permission requests from every ACP step of a harness, in the order they parked. */
|
|
81
|
+
export const acpPermissionRequests = {
|
|
82
|
+
name: 'acp:permission-requests',
|
|
83
|
+
schema: AcpPermissionPromptSchema,
|
|
84
|
+
mode: 'queue',
|
|
85
|
+
external: true,
|
|
86
|
+
};
|
|
87
|
+
/** @public Decisions broadcast to every parked handler; each filters by its own `requestId`. */
|
|
88
|
+
export const acpPermissionDecisions = {
|
|
89
|
+
name: 'acp:permission-decisions',
|
|
90
|
+
schema: AcpPermissionReplySchema,
|
|
91
|
+
mode: 'topic',
|
|
92
|
+
external: true,
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Build an {@link AcpPermissionHandler} that asks a human over the channel pair
|
|
96
|
+
* above. Pass it as a step's `onPermissionRequest`; it runs after the step's
|
|
97
|
+
* declarative policy and steering have both abstained.
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
export function askUserForPermission(opts = {}) {
|
|
101
|
+
const timeout = opts.timeout ?? 5 * 60 * 1e3;
|
|
102
|
+
const onTimeout = opts.onTimeout ?? {
|
|
103
|
+
decision: 'deny',
|
|
104
|
+
reason: 'no answer from the user before the permission request timed out',
|
|
105
|
+
};
|
|
106
|
+
return async (request, ctx, info) => {
|
|
107
|
+
const requestId = crypto.randomUUID();
|
|
108
|
+
const deadline = Date.now() + timeout;
|
|
109
|
+
// Park on the decision topic BEFORE publishing the request. Topic delivery
|
|
110
|
+
// reaches only subscribers that are parked at send time, so a reviewer who
|
|
111
|
+
// answers immediately would otherwise answer into the void and the agent
|
|
112
|
+
// would wait out the whole timeout.
|
|
113
|
+
let waiting = ctx.recv(acpPermissionDecisions, {
|
|
114
|
+
timeout,
|
|
115
|
+
});
|
|
116
|
+
await ctx.send(acpPermissionRequests, {
|
|
117
|
+
requestId,
|
|
118
|
+
agentId: info.agentId,
|
|
119
|
+
sessionId: request.sessionId,
|
|
120
|
+
threadId: ctx.threadId,
|
|
121
|
+
stepId: info.stepId,
|
|
122
|
+
title: request.toolCall.title ?? request.toolCall.toolCallId,
|
|
123
|
+
kind: request.toolCall.kind ?? undefined,
|
|
124
|
+
rawInput: request.toolCall.rawInput,
|
|
125
|
+
options: request.options.map((option) => ({
|
|
126
|
+
optionId: option.optionId,
|
|
127
|
+
name: option.name,
|
|
128
|
+
kind: option.kind,
|
|
129
|
+
})),
|
|
130
|
+
});
|
|
131
|
+
// Decisions are broadcast, so a waiter also sees answers meant for other
|
|
132
|
+
// requests; keep reading until ours arrives. The deadline is shared across
|
|
133
|
+
// the whole wait, not restarted per message, so another request's answer
|
|
134
|
+
// cannot extend it.
|
|
135
|
+
//
|
|
136
|
+
// Re-parking between messages leaves a microtask-sized window in which a
|
|
137
|
+
// decision could be missed — only reachable when two prompts are answered
|
|
138
|
+
// in the same tick, and bounded by the timeout rather than hanging.
|
|
139
|
+
while (true) {
|
|
140
|
+
let reply;
|
|
141
|
+
try {
|
|
142
|
+
reply = await waiting;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return onTimeout;
|
|
146
|
+
}
|
|
147
|
+
if (reply.requestId === requestId) {
|
|
148
|
+
return {
|
|
149
|
+
decision: reply.decision,
|
|
150
|
+
optionId: reply.optionId,
|
|
151
|
+
reason: reply.reason,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const remaining = deadline - Date.now();
|
|
155
|
+
if (remaining <= 0) {
|
|
156
|
+
return onTimeout;
|
|
157
|
+
}
|
|
158
|
+
waiting = ctx.recv(acpPermissionDecisions, {
|
|
159
|
+
timeout: remaining,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Answer a prompt from outside the execution. Get the handle with
|
|
166
|
+
* `harness.getChannelHandle(acpPermissionDecisions, ACP_PERMISSION_SCOPE)`.
|
|
167
|
+
* @public
|
|
168
|
+
*/
|
|
169
|
+
export function resolveAcpPermission(handle, reply) {
|
|
170
|
+
handle.send(reply);
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves ACP `session/request_permission` calls.
|
|
3
|
+
*
|
|
4
|
+
* ACP makes permission a baseline client responsibility: before running a tool
|
|
5
|
+
* the agent asks, and offers a set of {@link AcpPermissionOption}s to choose
|
|
6
|
+
* from. Noetic answers in three tiers, first decisive one wins:
|
|
7
|
+
*
|
|
8
|
+
* 1. the step's declarative {@link AcpPermissionPolicy},
|
|
9
|
+
* 2. the steering pipeline (supplied by the caller as `steer`), so one rule
|
|
10
|
+
* set governs both LLM tool calls and sub-agent tool calls,
|
|
11
|
+
* 3. the async `onPermissionRequest` handler — the human-in-the-loop hatch
|
|
12
|
+
* that steering's synchronous predicate cannot express.
|
|
13
|
+
*
|
|
14
|
+
* When all three abstain the policy `default` applies, which is `deny` unless
|
|
15
|
+
* the step says otherwise: an unattended agent must not gain blanket approval
|
|
16
|
+
* by omission.
|
|
17
|
+
*/
|
|
18
|
+
import type { AcpBoundPermissionHandler, AcpPermissionOption, AcpPermissionOutcome, AcpPermissionPolicy, AcpPermissionRule, AcpPermissionSteerer, AcpRequestPermissionRequest } from '@noetic-tools/types';
|
|
19
|
+
/** @public Everything {@link resolvePermission} consults, in tier order. */
|
|
20
|
+
export interface AcpPermissionResolverOptions {
|
|
21
|
+
policy?: AcpPermissionPolicy;
|
|
22
|
+
steer?: AcpPermissionSteerer;
|
|
23
|
+
handler?: AcpBoundPermissionHandler;
|
|
24
|
+
}
|
|
25
|
+
/** The concrete ACP outcome payload sent back to the agent. */
|
|
26
|
+
export type AcpRequestPermissionOutcome = {
|
|
27
|
+
outcome: 'cancelled';
|
|
28
|
+
} | {
|
|
29
|
+
outcome: 'selected';
|
|
30
|
+
optionId: string;
|
|
31
|
+
};
|
|
32
|
+
/** True when every present field of the rule matches the tool call. */
|
|
33
|
+
export declare function ruleMatches(rule: AcpPermissionRule, toolCall: AcpRequestPermissionRequest['toolCall']): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Evaluate the declarative policy. `deny` is checked before `allow` so an
|
|
36
|
+
* explicit refusal always beats a broad grant. Returns `undefined` when no rule
|
|
37
|
+
* matched, leaving the decision to the later tiers.
|
|
38
|
+
*/
|
|
39
|
+
export declare function evaluatePolicy(policy: AcpPermissionPolicy | undefined, request: AcpRequestPermissionRequest): AcpPermissionOutcome | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Translate a decision into one of the options the agent actually offered.
|
|
42
|
+
*
|
|
43
|
+
* An explicit `optionId` wins when the agent offered it. Otherwise the decision
|
|
44
|
+
* picks by option `kind`, honouring `persist`.
|
|
45
|
+
*
|
|
46
|
+
* `cancelled` is reserved. The specification defines it as *the prompt turn was
|
|
47
|
+
* cancelled* — a conforming agent that receives it aborts the whole turn and
|
|
48
|
+
* answers `stopReason: 'cancelled'`. Returning it for a merely-denied tool
|
|
49
|
+
* therefore kills the entire step over one refusal, so a deny falls back to any
|
|
50
|
+
* reject option the agent offered, in either flavour, before giving up.
|
|
51
|
+
*
|
|
52
|
+
* When the agent genuinely offered nothing usable, {@link selectPermissionOption}
|
|
53
|
+
* returns `undefined` and the caller raises a Noetic-side error rather than
|
|
54
|
+
* telling the agent something untrue.
|
|
55
|
+
*/
|
|
56
|
+
export declare function selectPermissionOption(outcome: AcpPermissionOutcome, options: ReadonlyArray<AcpPermissionOption>, persist?: boolean): AcpRequestPermissionOutcome | undefined;
|
|
57
|
+
/** Run the three resolver tiers and fall back to the policy default. */
|
|
58
|
+
export declare function resolvePermission(request: AcpRequestPermissionRequest, opts: AcpPermissionResolverOptions): Promise<AcpPermissionOutcome>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves ACP `session/request_permission` calls.
|
|
3
|
+
*
|
|
4
|
+
* ACP makes permission a baseline client responsibility: before running a tool
|
|
5
|
+
* the agent asks, and offers a set of {@link AcpPermissionOption}s to choose
|
|
6
|
+
* from. Noetic answers in three tiers, first decisive one wins:
|
|
7
|
+
*
|
|
8
|
+
* 1. the step's declarative {@link AcpPermissionPolicy},
|
|
9
|
+
* 2. the steering pipeline (supplied by the caller as `steer`), so one rule
|
|
10
|
+
* set governs both LLM tool calls and sub-agent tool calls,
|
|
11
|
+
* 3. the async `onPermissionRequest` handler — the human-in-the-loop hatch
|
|
12
|
+
* that steering's synchronous predicate cannot express.
|
|
13
|
+
*
|
|
14
|
+
* When all three abstain the policy `default` applies, which is `deny` unless
|
|
15
|
+
* the step says otherwise: an unattended agent must not gain blanket approval
|
|
16
|
+
* by omission.
|
|
17
|
+
*/
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region Policy evaluation
|
|
20
|
+
/** True when every present field of the rule matches the tool call. */
|
|
21
|
+
export function ruleMatches(rule, toolCall) {
|
|
22
|
+
if (rule.kind !== undefined && toolCall.kind !== rule.kind) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (rule.title === undefined) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
const title = toolCall.title ?? '';
|
|
29
|
+
if (rule.title instanceof RegExp) {
|
|
30
|
+
return rule.title.test(title);
|
|
31
|
+
}
|
|
32
|
+
return title.toLowerCase().includes(rule.title.toLowerCase());
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Evaluate the declarative policy. `deny` is checked before `allow` so an
|
|
36
|
+
* explicit refusal always beats a broad grant. Returns `undefined` when no rule
|
|
37
|
+
* matched, leaving the decision to the later tiers.
|
|
38
|
+
*/
|
|
39
|
+
export function evaluatePolicy(policy, request) {
|
|
40
|
+
if (!policy) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const matched = (rules) => rules?.find((rule) => ruleMatches(rule, request.toolCall));
|
|
44
|
+
const denied = matched(policy.deny);
|
|
45
|
+
if (denied) {
|
|
46
|
+
return {
|
|
47
|
+
decision: 'deny',
|
|
48
|
+
reason: 'matched a deny rule in the step permission policy',
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const allowed = matched(policy.allow);
|
|
52
|
+
if (allowed) {
|
|
53
|
+
return {
|
|
54
|
+
decision: 'allow',
|
|
55
|
+
reason: 'matched an allow rule in the step permission policy',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region Option selection
|
|
62
|
+
const ALLOW_KINDS_ONCE_FIRST = [
|
|
63
|
+
'allow_once',
|
|
64
|
+
'allow_always',
|
|
65
|
+
];
|
|
66
|
+
const ALLOW_KINDS_ALWAYS_FIRST = [
|
|
67
|
+
'allow_always',
|
|
68
|
+
'allow_once',
|
|
69
|
+
];
|
|
70
|
+
const REJECT_KINDS_ONCE_FIRST = [
|
|
71
|
+
'reject_once',
|
|
72
|
+
'reject_always',
|
|
73
|
+
];
|
|
74
|
+
const REJECT_KINDS_ALWAYS_FIRST = [
|
|
75
|
+
'reject_always',
|
|
76
|
+
'reject_once',
|
|
77
|
+
];
|
|
78
|
+
function preferenceFor(decision, persist) {
|
|
79
|
+
if (decision === 'allow') {
|
|
80
|
+
return persist ? ALLOW_KINDS_ALWAYS_FIRST : ALLOW_KINDS_ONCE_FIRST;
|
|
81
|
+
}
|
|
82
|
+
return persist ? REJECT_KINDS_ALWAYS_FIRST : REJECT_KINDS_ONCE_FIRST;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Translate a decision into one of the options the agent actually offered.
|
|
86
|
+
*
|
|
87
|
+
* An explicit `optionId` wins when the agent offered it. Otherwise the decision
|
|
88
|
+
* picks by option `kind`, honouring `persist`.
|
|
89
|
+
*
|
|
90
|
+
* `cancelled` is reserved. The specification defines it as *the prompt turn was
|
|
91
|
+
* cancelled* — a conforming agent that receives it aborts the whole turn and
|
|
92
|
+
* answers `stopReason: 'cancelled'`. Returning it for a merely-denied tool
|
|
93
|
+
* therefore kills the entire step over one refusal, so a deny falls back to any
|
|
94
|
+
* reject option the agent offered, in either flavour, before giving up.
|
|
95
|
+
*
|
|
96
|
+
* When the agent genuinely offered nothing usable, {@link selectPermissionOption}
|
|
97
|
+
* returns `undefined` and the caller raises a Noetic-side error rather than
|
|
98
|
+
* telling the agent something untrue.
|
|
99
|
+
*/
|
|
100
|
+
export function selectPermissionOption(outcome, options, persist = false) {
|
|
101
|
+
// The one case where `cancelled` is the truth: the decision really is
|
|
102
|
+
// "abandon this turn".
|
|
103
|
+
if (outcome.decision === 'cancel') {
|
|
104
|
+
return {
|
|
105
|
+
outcome: 'cancelled',
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (outcome.optionId !== undefined) {
|
|
109
|
+
const exact = options.find((option) => option.optionId === outcome.optionId);
|
|
110
|
+
if (exact) {
|
|
111
|
+
return {
|
|
112
|
+
outcome: 'selected',
|
|
113
|
+
optionId: exact.optionId,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const kind of preferenceFor(outcome.decision, persist)) {
|
|
118
|
+
const match = options.find((option) => option.kind === kind);
|
|
119
|
+
if (match) {
|
|
120
|
+
return {
|
|
121
|
+
outcome: 'selected',
|
|
122
|
+
optionId: match.optionId,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region Public API
|
|
130
|
+
/** Run the three resolver tiers and fall back to the policy default. */
|
|
131
|
+
export async function resolvePermission(request, opts) {
|
|
132
|
+
const fromPolicy = evaluatePolicy(opts.policy, request);
|
|
133
|
+
if (fromPolicy) {
|
|
134
|
+
return fromPolicy;
|
|
135
|
+
}
|
|
136
|
+
if (opts.steer) {
|
|
137
|
+
const steered = await opts.steer(request);
|
|
138
|
+
if (steered) {
|
|
139
|
+
return steered;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (opts.handler) {
|
|
143
|
+
return await opts.handler(request);
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
decision: opts.policy?.default ?? 'deny',
|
|
147
|
+
reason: 'no permission rule, steering decision, or handler matched',
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A registry of ACP agent adapters keyed by `agentId`. Pass one to a JSON
|
|
3
|
+
* workflow's `HydrationContext.acpAgents` so `acp-agent` nodes resolve their
|
|
4
|
+
* `agent` key to a live adapter.
|
|
5
|
+
*/
|
|
6
|
+
import type { AcpAgent } from '@noetic-tools/types';
|
|
7
|
+
/** @public ACP agent adapters keyed by agent id. */
|
|
8
|
+
export type AcpAgentRegistry = Map<string, AcpAgent>;
|
|
9
|
+
/** @public Build an {@link AcpAgentRegistry} from a list of adapters. */
|
|
10
|
+
export declare function createAcpAgentRegistry(...agents: AcpAgent[]): AcpAgentRegistry;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A registry of ACP agent adapters keyed by `agentId`. Pass one to a JSON
|
|
3
|
+
* workflow's `HydrationContext.acpAgents` so `acp-agent` nodes resolve their
|
|
4
|
+
* `agent` key to a live adapter.
|
|
5
|
+
*/
|
|
6
|
+
/** @public Build an {@link AcpAgentRegistry} from a list of adapters. */
|
|
7
|
+
export function createAcpAgentRegistry(...agents) {
|
|
8
|
+
const registry = new Map();
|
|
9
|
+
for (const agent of agents) {
|
|
10
|
+
registry.set(agent.agentId, agent);
|
|
11
|
+
}
|
|
12
|
+
return registry;
|
|
13
|
+
}
|
package/dist/stdio.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node stdio transport — the standard way to reach a local ACP agent.
|
|
3
|
+
*
|
|
4
|
+
* Lives behind the `@noetic-tools/acp/stdio` subpath so the package's main
|
|
5
|
+
* entry stays runtime-neutral: nothing in `.` imports `node:*`, and a browser
|
|
6
|
+
* bundle never pulls in `child_process`. The agent presets reach this through a
|
|
7
|
+
* lazy dynamic import, so `claudeCode()` works with no extra wiring while
|
|
8
|
+
* keeping the dependency off the main graph.
|
|
9
|
+
*/
|
|
10
|
+
import type { AcpTransportFactory } from '@noetic-tools/types';
|
|
11
|
+
/** @public How to launch an ACP agent as a child process. */
|
|
12
|
+
export interface StdioAgentSpec {
|
|
13
|
+
/** Identifier used in error messages. */
|
|
14
|
+
agentId: string;
|
|
15
|
+
/** Executable to run (often `npx` or a globally installed CLI). */
|
|
16
|
+
command: string;
|
|
17
|
+
args?: ReadonlyArray<string>;
|
|
18
|
+
/** Environment overrides layered onto the parent environment. */
|
|
19
|
+
env?: Record<string, string | undefined>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Build a transport factory that spawns `spec.command` and speaks ACP over its
|
|
23
|
+
* stdin/stdout. The child's stderr is inherited so agent diagnostics reach the
|
|
24
|
+
* host's logs instead of vanishing.
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare function stdioAcpTransport(spec: StdioAgentSpec): AcpTransportFactory;
|
package/dist/stdio.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node stdio transport — the standard way to reach a local ACP agent.
|
|
3
|
+
*
|
|
4
|
+
* Lives behind the `@noetic-tools/acp/stdio` subpath so the package's main
|
|
5
|
+
* entry stays runtime-neutral: nothing in `.` imports `node:*`, and a browser
|
|
6
|
+
* bundle never pulls in `child_process`. The agent presets reach this through a
|
|
7
|
+
* lazy dynamic import, so `claudeCode()` works with no extra wiring while
|
|
8
|
+
* keeping the dependency off the main graph.
|
|
9
|
+
*/
|
|
10
|
+
import { spawn } from 'node:child_process';
|
|
11
|
+
import { Readable, Writable } from 'node:stream';
|
|
12
|
+
import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
|
|
13
|
+
/**
|
|
14
|
+
* Build a transport factory that spawns `spec.command` and speaks ACP over its
|
|
15
|
+
* stdin/stdout. The child's stderr is inherited so agent diagnostics reach the
|
|
16
|
+
* host's logs instead of vanishing.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export function stdioAcpTransport(spec) {
|
|
20
|
+
return async (opts) => {
|
|
21
|
+
const child = spawn(spec.command, [
|
|
22
|
+
...(spec.args ?? []),
|
|
23
|
+
], {
|
|
24
|
+
cwd: opts.cwd,
|
|
25
|
+
env: {
|
|
26
|
+
...process.env,
|
|
27
|
+
...spec.env,
|
|
28
|
+
...opts.env,
|
|
29
|
+
},
|
|
30
|
+
stdio: [
|
|
31
|
+
'pipe',
|
|
32
|
+
'pipe',
|
|
33
|
+
'inherit',
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
const stdin = child.stdin;
|
|
37
|
+
const stdout = child.stdout;
|
|
38
|
+
if (!stdin || !stdout) {
|
|
39
|
+
child.kill();
|
|
40
|
+
throw new AcpConnectError({
|
|
41
|
+
agentId: spec.agentId,
|
|
42
|
+
message: `ACP agent '${spec.agentId}' was spawned without usable stdio pipes.`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// A missing binary surfaces asynchronously on the child, long after spawn()
|
|
46
|
+
// returned. Capture it so the failure is reported against the agent rather
|
|
47
|
+
// than escaping as an unhandled 'error' event.
|
|
48
|
+
let spawnError;
|
|
49
|
+
child.once('error', (error) => {
|
|
50
|
+
spawnError = error;
|
|
51
|
+
});
|
|
52
|
+
const onAbort = () => {
|
|
53
|
+
child.kill();
|
|
54
|
+
};
|
|
55
|
+
opts.signal?.addEventListener('abort', onAbort, {
|
|
56
|
+
once: true,
|
|
57
|
+
});
|
|
58
|
+
let closed = false;
|
|
59
|
+
return {
|
|
60
|
+
// `node:stream`'s web-stream types are structurally the same as the
|
|
61
|
+
// global ones but nominally distinct, so the bridge is cast once here
|
|
62
|
+
// rather than leaking a Node-specific type through the transport contract.
|
|
63
|
+
readable: frameworkCast(Readable.toWeb(stdout)),
|
|
64
|
+
writable: frameworkCast(Writable.toWeb(stdin)),
|
|
65
|
+
async close() {
|
|
66
|
+
if (closed) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
closed = true;
|
|
70
|
+
opts.signal?.removeEventListener('abort', onAbort);
|
|
71
|
+
child.kill();
|
|
72
|
+
if (spawnError) {
|
|
73
|
+
throw new AcpConnectError({
|
|
74
|
+
agentId: spec.agentId,
|
|
75
|
+
message: `ACP agent '${spec.agentId}' (${spec.command}) failed to start.`,
|
|
76
|
+
cause: spawnError,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
import type { ShellAdapter } from '@noetic-tools/types';
|
|
11
|
+
/** @public A single environment entry as ACP transmits it. */
|
|
12
|
+
export interface AcpEnvVariable {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string;
|
|
15
|
+
}
|
|
16
|
+
/** @public Exit status of a finished terminal command. */
|
|
17
|
+
export interface AcpTerminalExitStatus {
|
|
18
|
+
exitCode: number | null;
|
|
19
|
+
signal: string | null;
|
|
20
|
+
}
|
|
21
|
+
/** @public Options for {@link TerminalRegistry.create}. */
|
|
22
|
+
export interface CreateTerminalOptions {
|
|
23
|
+
command: string;
|
|
24
|
+
args?: ReadonlyArray<string>;
|
|
25
|
+
cwd?: string | null;
|
|
26
|
+
env?: ReadonlyArray<AcpEnvVariable>;
|
|
27
|
+
/** Cap on retained output. Older bytes are dropped first. */
|
|
28
|
+
outputByteLimit?: number | null;
|
|
29
|
+
}
|
|
30
|
+
/** @public Snapshot of a terminal's captured output. */
|
|
31
|
+
export interface TerminalOutputSnapshot {
|
|
32
|
+
output: string;
|
|
33
|
+
truncated: boolean;
|
|
34
|
+
exitStatus: AcpTerminalExitStatus | null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* POSIX single-quote escaping. `ShellAdapter.exec` takes one command string
|
|
38
|
+
* while ACP supplies `command` + `args`, so arguments are quoted rather than
|
|
39
|
+
* concatenated — otherwise a path with a space would silently split.
|
|
40
|
+
*/
|
|
41
|
+
export declare function quoteShellArg(arg: string): string;
|
|
42
|
+
/** Build the single command string `ShellAdapter.exec` expects. */
|
|
43
|
+
export declare function buildCommandLine(command: string, args?: ReadonlyArray<string>): string;
|
|
44
|
+
/**
|
|
45
|
+
* Owns every terminal an ACP session created. One instance per session, torn
|
|
46
|
+
* down with the session so no child command outlives the step that spawned it.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export declare class TerminalRegistry {
|
|
50
|
+
private readonly shell;
|
|
51
|
+
private readonly defaultCwd;
|
|
52
|
+
private readonly terminals;
|
|
53
|
+
private nextId;
|
|
54
|
+
private disposed;
|
|
55
|
+
constructor(shell: ShellAdapter, defaultCwd: string);
|
|
56
|
+
/** Start a command and return its handle id. */
|
|
57
|
+
create(opts: CreateTerminalOptions): string;
|
|
58
|
+
/** Drive one `exec` to completion, folding its output into the record. */
|
|
59
|
+
private run;
|
|
60
|
+
/** Current output and exit status, or `null` when the handle is unknown. */
|
|
61
|
+
output(id: string): TerminalOutputSnapshot | null;
|
|
62
|
+
/** Resolve once the command has exited. */
|
|
63
|
+
waitForExit(id: string): Promise<AcpTerminalExitStatus | null>;
|
|
64
|
+
/** Kill the command but keep the handle readable, per the ACP contract. */
|
|
65
|
+
kill(id: string): Promise<boolean>;
|
|
66
|
+
/** Kill the command and drop the handle. */
|
|
67
|
+
release(id: string): Promise<boolean>;
|
|
68
|
+
/** Release every live terminal. Called when the session closes. */
|
|
69
|
+
releaseAll(): Promise<void>;
|
|
70
|
+
}
|