personal-bot 0.2.11 → 0.2.12
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/README.md +17 -3
- package/README.zh-CN.md +17 -3
- package/bin/pbot-pi-acp.mjs +20 -0
- package/docs/agent-collaboration.md +38 -2
- package/docs/capability-design.md +6 -0
- package/docs/dsh.md +9 -8
- package/docs/file-storage.md +9 -1
- package/docs/interaction-design.md +2 -2
- package/docs/model-context.md +10 -2
- package/docs/technical-design.md +20 -2
- package/docs/workspace-configuration.md +20 -1
- package/lib/client.js +40 -7
- package/lib/dsh.js +2 -0
- package/lib/extension/acp-member.d.ts +16 -0
- package/lib/extension/acp-member.js +45 -0
- package/lib/extension/acp-worker.d.ts +1 -0
- package/lib/extension/acp-worker.js +156 -0
- package/lib/extension/dsh/acp-member.d.ts +4 -0
- package/lib/extension/dsh/acp-member.js +48 -0
- package/lib/extension/dsh/command.js +11 -1
- package/lib/extension/dsh/management.js +3 -1
- package/lib/extension/dsh/workspace.d.ts +35 -1
- package/lib/extension/dsh/workspace.js +21 -3
- package/lib/integrations/acp/agents.d.ts +24 -0
- package/lib/integrations/acp/agents.js +168 -0
- package/lib/integrations/acp/claude.d.ts +18 -0
- package/lib/integrations/acp/claude.js +33 -0
- package/lib/integrations/acp/connection.d.ts +27 -0
- package/lib/integrations/acp/connection.js +167 -0
- package/lib/integrations/acp/input.d.ts +4 -0
- package/lib/integrations/acp/input.js +45 -0
- package/lib/integrations/acp/launch.d.ts +4 -0
- package/lib/integrations/acp/launch.js +52 -0
- package/lib/integrations/acp/model-selection.d.ts +12 -0
- package/lib/integrations/acp/model-selection.js +65 -0
- package/lib/integrations/dsh/dsh-agents.d.ts +2 -1
- package/lib/integrations/dsh/dsh-agents.js +4 -0
- package/lib/model/acp.d.ts +59 -0
- package/lib/model/acp.js +1 -0
- package/lib/model/agent.d.ts +15 -1
- package/lib/model/workspace-config.d.ts +3 -0
- package/lib/services/agents/acp-run.d.ts +42 -0
- package/lib/services/agents/acp-run.js +149 -0
- package/lib/services/agents/agent-service.d.ts +114 -6
- package/lib/services/agents/agent-service.js +81 -10
- package/lib/services/agents/team-service.js +4 -2
- package/lib/services/workspace/workspace-config.js +12 -1
- package/lib/storage/acp-files.d.ts +9 -0
- package/lib/storage/acp-files.js +50 -0
- package/lib/storage/agent-files.d.ts +5 -4
- package/lib/storage/agent-files.js +11 -2
- package/lib/tools/agents/actions.d.ts +16 -2
- package/lib/tools/agents/actions.js +3 -1
- package/lib/tools/agents/contract.d.ts +48 -3
- package/lib/tools/agents/contract.js +15 -9
- package/lib/tools/agents/dsh-contract.d.ts +8 -15
- package/lib/tools/agents/dsh-contract.js +12 -8
- package/lib/ui/dsh/management.js +36 -8
- package/npm-shrinkwrap.json +953 -12
- package/package.json +8 -1
- package/resources/skills/.claude-plugin/plugin.json +4 -0
- package/scripts/README.md +6 -0
- package/src/dsh.ts +1 -0
- package/src/extension/acp-member.ts +44 -0
- package/src/extension/acp-worker.ts +124 -0
- package/src/extension/agent-events.ts +28 -3
- package/src/extension/agents-command.ts +33 -5
- package/src/extension/dsh/acp-member.ts +44 -0
- package/src/extension/dsh/command.ts +10 -1
- package/src/extension/dsh/management.ts +2 -1
- package/src/extension/dsh/workspace.ts +21 -3
- package/src/extension/lifecycle.ts +2 -2
- package/src/extension/pi-acp-member.ts +64 -0
- package/src/integrations/acp/agents.ts +118 -0
- package/src/integrations/acp/claude.ts +40 -0
- package/src/integrations/acp/connection.ts +152 -0
- package/src/integrations/acp/input.ts +39 -0
- package/src/integrations/acp/launch.ts +50 -0
- package/src/integrations/acp/model-selection.ts +66 -0
- package/src/integrations/dsh/dsh-agents.ts +4 -1
- package/src/integrations/pi/pi-agents.ts +4 -1
- package/src/model/acp.ts +26 -0
- package/src/model/agent.ts +7 -1
- package/src/model/workspace-config.ts +4 -0
- package/src/pbot.ts +3 -2
- package/src/services/agents/acp-run.ts +123 -0
- package/src/services/agents/agent-service.ts +85 -12
- package/src/services/agents/team-service.ts +3 -2
- package/src/services/workspace/workspace-config.ts +10 -1
- package/src/storage/acp-files.ts +50 -0
- package/src/storage/agent-files.ts +12 -4
- package/src/tools/agents/actions.ts +4 -2
- package/src/tools/agents/contract.ts +16 -9
- package/src/tools/agents/dsh-contract.ts +12 -8
- package/src/ui/dsh/management.ts +31 -8
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
+
import { AgentFiles, launch } from "../../storage/agent-files.js";
|
|
4
|
+
import { acpAnswer, acpInput, acpPermission, acpPermissionReply, acpReport } from "../../storage/acp-files.js";
|
|
5
|
+
import { AgentRun } from "./agent-run.js";
|
|
6
|
+
export class AcpAnswerBuffer {
|
|
7
|
+
#text = "";
|
|
8
|
+
#messageId;
|
|
9
|
+
get text() { return this.#text; }
|
|
10
|
+
reset() { this.#text = ""; this.#messageId = undefined; }
|
|
11
|
+
update(value) {
|
|
12
|
+
if (value.kind === "tool")
|
|
13
|
+
this.reset();
|
|
14
|
+
if (value.kind !== "text")
|
|
15
|
+
return;
|
|
16
|
+
if (value.messageId && value.messageId !== this.#messageId)
|
|
17
|
+
this.#text = "";
|
|
18
|
+
this.#messageId = value.messageId;
|
|
19
|
+
this.#text += value.text ?? "";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** ACP prompt completion is the receipt boundary; final reports publish before completions. */
|
|
23
|
+
export class AcpAgentRun {
|
|
24
|
+
files;
|
|
25
|
+
descriptor;
|
|
26
|
+
generation;
|
|
27
|
+
work;
|
|
28
|
+
exactAnswer;
|
|
29
|
+
execution;
|
|
30
|
+
#answer = new AcpAnswerBuffer();
|
|
31
|
+
#lastActivity = 0;
|
|
32
|
+
#stopping = false;
|
|
33
|
+
#work;
|
|
34
|
+
#stop;
|
|
35
|
+
#failure;
|
|
36
|
+
constructor(files, descriptor, generation, work, exactAnswer) {
|
|
37
|
+
this.files = files;
|
|
38
|
+
this.descriptor = descriptor;
|
|
39
|
+
this.generation = generation;
|
|
40
|
+
this.work = work;
|
|
41
|
+
this.exactAnswer = exactAnswer;
|
|
42
|
+
this.execution = new AgentRun(files, descriptor.agentId, generation);
|
|
43
|
+
}
|
|
44
|
+
get id() { return this.descriptor.agentId; }
|
|
45
|
+
current() { return this.files.read(this.id, "launch.json", launch).generation === this.generation; }
|
|
46
|
+
update(value) {
|
|
47
|
+
if (!this.execution.current || !this.current())
|
|
48
|
+
return;
|
|
49
|
+
this.#answer.update(value);
|
|
50
|
+
if (Date.now() - this.#lastActivity >= 500) {
|
|
51
|
+
this.#lastActivity = Date.now();
|
|
52
|
+
this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: value.activity ?? (value.kind === "text" ? "回答" : "思考") });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async permission(description, options, signal) {
|
|
56
|
+
const requestId = randomUUID(), request = { generation: this.generation, requestId, description, options };
|
|
57
|
+
this.files.write(this.id, "permissions/" + requestId + ".json", request, acpPermission, true);
|
|
58
|
+
this.execution.save({ state: "busy", activity: "等待确认" });
|
|
59
|
+
while (!signal.aborted && !this.#stopping && this.current() && !this.files.read(this.id, "launch.json", launch).stop) {
|
|
60
|
+
const reply = this.files.optional(this.id, "permissions/" + requestId + "-reply.json", acpPermissionReply);
|
|
61
|
+
if (reply?.generation === this.generation)
|
|
62
|
+
return reply.optionId;
|
|
63
|
+
await delay(100);
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
async run() {
|
|
68
|
+
this.execution.recover();
|
|
69
|
+
this.execution.save({ state: "idle", activity: "空闲" });
|
|
70
|
+
while (!this.#stopping && this.current() && !this.work.signal.aborted) {
|
|
71
|
+
this.files.check();
|
|
72
|
+
if (this.#failure)
|
|
73
|
+
throw this.#failure;
|
|
74
|
+
if (this.files.read(this.id, "launch.json", launch).stop) {
|
|
75
|
+
await this.stop();
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
if (!this.#work) {
|
|
79
|
+
const request = this.files.messages(this.id).find(message => message.generation === this.generation && !this.execution.accepted(message.requestId));
|
|
80
|
+
if (request) {
|
|
81
|
+
const pending = this.perform(request).finally(() => { if (this.#work === pending)
|
|
82
|
+
this.#work = undefined; });
|
|
83
|
+
this.#work = pending;
|
|
84
|
+
void pending.catch(error => { this.#failure = error; });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
await delay(100);
|
|
88
|
+
}
|
|
89
|
+
if (!this.current() && !this.#stopping)
|
|
90
|
+
await this.stop();
|
|
91
|
+
if (!this.#stopping)
|
|
92
|
+
await this.#work;
|
|
93
|
+
if (this.work.signal.aborted && !this.#stopping)
|
|
94
|
+
throw this.work.signal.reason;
|
|
95
|
+
}
|
|
96
|
+
async perform(request) {
|
|
97
|
+
this.#answer.reset();
|
|
98
|
+
this.execution.claim(request.requestId);
|
|
99
|
+
this.execution.enter(request.requestId);
|
|
100
|
+
this.execution.begin();
|
|
101
|
+
this.files.write(this.id, "acp-input.json", { generation: this.generation, requestId: request.requestId }, acpInput);
|
|
102
|
+
let result;
|
|
103
|
+
try {
|
|
104
|
+
const end = await this.work.prompt(request);
|
|
105
|
+
let snapshot;
|
|
106
|
+
if (this.exactAnswer) {
|
|
107
|
+
snapshot = this.files.optional(this.id, "acp-answer.json", acpAnswer);
|
|
108
|
+
if (snapshot?.generation !== this.generation || snapshot.requestId !== request.requestId)
|
|
109
|
+
snapshot = undefined;
|
|
110
|
+
}
|
|
111
|
+
const answer = this.exactAnswer ? snapshot?.answer ?? "" : this.#answer.text;
|
|
112
|
+
const state = end.stopReason === "cancelled" || this.#stopping ? "cancelled" : end.stopReason === "end_turn" &&
|
|
113
|
+
(!this.exactAnswer || snapshot?.state === "completed") && answer.trim() ? "completed" : "failed";
|
|
114
|
+
if (state === "completed")
|
|
115
|
+
this.execution.consume(request.requestId);
|
|
116
|
+
result = { generation: this.generation, requestId: request.requestId, state, answer,
|
|
117
|
+
...(state === "failed" ? { error: snapshot?.error ?? "ACP 工作未产生有效最终回答:" + end.stopReason } : {}) };
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
result = { generation: this.generation, requestId: request.requestId, answer: this.#answer.text,
|
|
121
|
+
state: this.#stopping || this.files.read(this.id, "launch.json", launch).stop ? "cancelled" : "failed", error: String(error) };
|
|
122
|
+
}
|
|
123
|
+
if (!this.current())
|
|
124
|
+
return;
|
|
125
|
+
const cycleId = this.execution.current.cycleId;
|
|
126
|
+
this.files.write(this.id, "reports/" + cycleId + ".json", { generation: this.generation, cycleId, text: result.answer }, acpReport, true);
|
|
127
|
+
this.execution.finish({ state: this.work.signal.aborted && !this.#stopping ? "interrupted" : result.state,
|
|
128
|
+
position: "acp:" + cycleId, answer: result.answer, ...(result.error ? { error: result.error } : {}) });
|
|
129
|
+
}
|
|
130
|
+
settled() { return this.#work; }
|
|
131
|
+
stop() {
|
|
132
|
+
return this.#stop ??= (async () => {
|
|
133
|
+
this.#stopping = true;
|
|
134
|
+
if (this.current())
|
|
135
|
+
this.execution.save({ state: "stopping", activity: "停止" });
|
|
136
|
+
const pending = this.#work;
|
|
137
|
+
if (!pending)
|
|
138
|
+
return;
|
|
139
|
+
const grace = new AbortController();
|
|
140
|
+
try {
|
|
141
|
+
const cancelAndSettle = this.work.cancel().catch(() => { }).then(() => pending);
|
|
142
|
+
await Promise.race([cancelAndSettle, delay(4000, undefined, { signal: grace.signal })]);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
grace.abort();
|
|
146
|
+
}
|
|
147
|
+
})();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { AgentFiles } from "../../storage/agent-files.js";
|
|
2
2
|
import { TeamFiles } from "../../storage/team-files.js";
|
|
3
3
|
import type { TeamAgent, TeamInput } from "../../model/team.js";
|
|
4
|
-
import type { AgentOwner, AgentOptions, AgentDescriptor, AgentCycle, AgentCompletion, AgentSessionReference } from "../../model/agent.js";
|
|
4
|
+
import type { AgentOwner, AgentOptions, AgentStartOptions, AgentDescriptor, AgentCycle, AgentCompletion, AgentSessionReference, NativeAgentHost } from "../../model/agent.js";
|
|
5
|
+
import type { AcpBackend, AcpSessionReference } from "../../model/acp.js";
|
|
5
6
|
export interface AgentHost {
|
|
6
|
-
readonly kind:
|
|
7
|
-
available(): void;
|
|
7
|
+
readonly kind: NativeAgentHost;
|
|
8
|
+
available(backend?: AcpBackend): void;
|
|
9
|
+
prepare?(backend: AcpBackend, options: AgentOptions): AcpSessionReference;
|
|
10
|
+
validateOptions?(options: AgentOptions, backend?: AcpBackend): void;
|
|
8
11
|
alive(id: string): boolean;
|
|
9
12
|
validateSession(value: AgentDescriptor, reference: AgentSessionReference): void | Promise<void>;
|
|
10
13
|
launch(value: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, resume: boolean, signal: AbortSignal): void | Promise<void>;
|
|
@@ -25,13 +28,22 @@ export declare class AgentService {
|
|
|
25
28
|
private records;
|
|
26
29
|
list(owner: AgentOwner): TeamAgent[];
|
|
27
30
|
private view;
|
|
28
|
-
start(
|
|
31
|
+
start(input: AgentStartOptions, owner: AgentOwner): Promise<{
|
|
29
32
|
requestId?: string;
|
|
30
33
|
messageStatus?: "queued";
|
|
31
34
|
name: string;
|
|
32
35
|
generation: string;
|
|
33
36
|
state: import("../../model/agent.js").AgentStatus["state"];
|
|
34
37
|
activity: string;
|
|
38
|
+
backend?: AcpBackend;
|
|
39
|
+
transport?: "native" | "acp";
|
|
40
|
+
selection?: import("../../model/acp.js").AcpModelSelection;
|
|
41
|
+
capabilities?: {
|
|
42
|
+
steer: boolean;
|
|
43
|
+
resume: boolean;
|
|
44
|
+
team: boolean;
|
|
45
|
+
transcript: boolean;
|
|
46
|
+
};
|
|
35
47
|
cycle?: AgentCycle;
|
|
36
48
|
error?: string;
|
|
37
49
|
owned: boolean;
|
|
@@ -46,13 +58,22 @@ export declare class AgentService {
|
|
|
46
58
|
alive: boolean;
|
|
47
59
|
}>;
|
|
48
60
|
private launch;
|
|
49
|
-
send(id: string, text: string, delivery: "steer" | "followUp", owner: AgentOwner): {
|
|
61
|
+
send(id: string, text: string, delivery: "steer" | "followUp", owner: AgentOwner, attachments?: readonly string[]): {
|
|
50
62
|
requestId: string;
|
|
51
63
|
messageStatus: "queued";
|
|
52
64
|
name: string;
|
|
53
65
|
generation: string;
|
|
54
66
|
state: import("../../model/agent.js").AgentStatus["state"];
|
|
55
67
|
activity: string;
|
|
68
|
+
backend?: AcpBackend;
|
|
69
|
+
transport?: "native" | "acp";
|
|
70
|
+
selection?: import("../../model/acp.js").AcpModelSelection;
|
|
71
|
+
capabilities?: {
|
|
72
|
+
steer: boolean;
|
|
73
|
+
resume: boolean;
|
|
74
|
+
team: boolean;
|
|
75
|
+
transcript: boolean;
|
|
76
|
+
};
|
|
56
77
|
cycle?: AgentCycle;
|
|
57
78
|
error?: string;
|
|
58
79
|
owned: boolean;
|
|
@@ -74,6 +95,15 @@ export declare class AgentService {
|
|
|
74
95
|
generation: string;
|
|
75
96
|
state: import("../../model/agent.js").AgentStatus["state"];
|
|
76
97
|
activity: string;
|
|
98
|
+
backend?: AcpBackend;
|
|
99
|
+
transport?: "native" | "acp";
|
|
100
|
+
selection?: import("../../model/acp.js").AcpModelSelection;
|
|
101
|
+
capabilities?: {
|
|
102
|
+
steer: boolean;
|
|
103
|
+
resume: boolean;
|
|
104
|
+
team: boolean;
|
|
105
|
+
transcript: boolean;
|
|
106
|
+
};
|
|
77
107
|
cycle?: AgentCycle;
|
|
78
108
|
error?: string;
|
|
79
109
|
owned: boolean;
|
|
@@ -98,6 +128,15 @@ export declare class AgentService {
|
|
|
98
128
|
generation: string;
|
|
99
129
|
state: import("../../model/agent.js").AgentStatus["state"];
|
|
100
130
|
activity: string;
|
|
131
|
+
backend?: AcpBackend;
|
|
132
|
+
transport?: "native" | "acp";
|
|
133
|
+
selection?: import("../../model/acp.js").AcpModelSelection;
|
|
134
|
+
capabilities?: {
|
|
135
|
+
steer: boolean;
|
|
136
|
+
resume: boolean;
|
|
137
|
+
team: boolean;
|
|
138
|
+
transcript: boolean;
|
|
139
|
+
};
|
|
101
140
|
cycle?: AgentCycle;
|
|
102
141
|
error?: string;
|
|
103
142
|
owned: boolean;
|
|
@@ -119,6 +158,15 @@ export declare class AgentService {
|
|
|
119
158
|
generation: string;
|
|
120
159
|
state: import("../../model/agent.js").AgentStatus["state"];
|
|
121
160
|
activity: string;
|
|
161
|
+
backend?: AcpBackend;
|
|
162
|
+
transport?: "native" | "acp";
|
|
163
|
+
selection?: import("../../model/acp.js").AcpModelSelection;
|
|
164
|
+
capabilities?: {
|
|
165
|
+
steer: boolean;
|
|
166
|
+
resume: boolean;
|
|
167
|
+
team: boolean;
|
|
168
|
+
transcript: boolean;
|
|
169
|
+
};
|
|
122
170
|
error?: string;
|
|
123
171
|
owned: boolean;
|
|
124
172
|
agentId: string;
|
|
@@ -134,7 +182,7 @@ export declare class AgentService {
|
|
|
134
182
|
read(id: string, owner: AgentOwner, cycleId?: string, offset?: number, limit?: number): Promise<{
|
|
135
183
|
cycles: {
|
|
136
184
|
cycleId: string;
|
|
137
|
-
state: "
|
|
185
|
+
state: "completed" | "failed" | "cancelled" | "running" | "interrupted";
|
|
138
186
|
}[];
|
|
139
187
|
report: string;
|
|
140
188
|
totalLength: number;
|
|
@@ -144,6 +192,15 @@ export declare class AgentService {
|
|
|
144
192
|
generation: string;
|
|
145
193
|
state: import("../../model/agent.js").AgentStatus["state"];
|
|
146
194
|
activity: string;
|
|
195
|
+
backend?: AcpBackend;
|
|
196
|
+
transport?: "native" | "acp";
|
|
197
|
+
selection?: import("../../model/acp.js").AcpModelSelection;
|
|
198
|
+
capabilities?: {
|
|
199
|
+
steer: boolean;
|
|
200
|
+
resume: boolean;
|
|
201
|
+
team: boolean;
|
|
202
|
+
transcript: boolean;
|
|
203
|
+
};
|
|
147
204
|
error?: string;
|
|
148
205
|
owned: boolean;
|
|
149
206
|
agentId: string;
|
|
@@ -158,6 +215,57 @@ export declare class AgentService {
|
|
|
158
215
|
}>;
|
|
159
216
|
completions(owner: AgentOwner): Promise<AgentCompletion[]>;
|
|
160
217
|
private report;
|
|
218
|
+
recordPage(id: string, owner: AgentOwner, options?: {
|
|
219
|
+
seq?: number;
|
|
220
|
+
offset?: number;
|
|
221
|
+
limit?: number;
|
|
222
|
+
textOffset?: number;
|
|
223
|
+
textLimit?: number;
|
|
224
|
+
format?: string;
|
|
225
|
+
}): Promise<{
|
|
226
|
+
kind: string;
|
|
227
|
+
agent: TeamAgent;
|
|
228
|
+
cursor: string;
|
|
229
|
+
seq: number;
|
|
230
|
+
offset: number;
|
|
231
|
+
text: string;
|
|
232
|
+
totalLength: number;
|
|
233
|
+
nextOffset: number | null;
|
|
234
|
+
total?: never;
|
|
235
|
+
records?: never;
|
|
236
|
+
} | {
|
|
237
|
+
kind: string;
|
|
238
|
+
agent: TeamAgent;
|
|
239
|
+
cursor: string;
|
|
240
|
+
total: number;
|
|
241
|
+
offset: number;
|
|
242
|
+
nextOffset: number | null;
|
|
243
|
+
records: {
|
|
244
|
+
preview: string;
|
|
245
|
+
seq: number;
|
|
246
|
+
type: string;
|
|
247
|
+
time: number;
|
|
248
|
+
requestId?: string;
|
|
249
|
+
cycleId?: string;
|
|
250
|
+
state: string;
|
|
251
|
+
}[];
|
|
252
|
+
seq?: never;
|
|
253
|
+
text?: never;
|
|
254
|
+
totalLength?: never;
|
|
255
|
+
}>;
|
|
256
|
+
pendingPermissions(owner: AgentOwner): {
|
|
257
|
+
agentId: string;
|
|
258
|
+
name: string;
|
|
259
|
+
generation: string;
|
|
260
|
+
requestId: string;
|
|
261
|
+
description: string;
|
|
262
|
+
options: {
|
|
263
|
+
optionId: string;
|
|
264
|
+
name: string;
|
|
265
|
+
kind: string;
|
|
266
|
+
}[];
|
|
267
|
+
}[];
|
|
268
|
+
answerPermission(id: string, requestId: string, optionId: string | null, owner: AgentOwner): void;
|
|
161
269
|
private release;
|
|
162
270
|
/** Called by the parent scanner; stop remains nonblocking to the model. */
|
|
163
271
|
refresh(owner: AgentOwner): Promise<void>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { realpathSync } from "node:fs";
|
|
2
|
+
import { realpathSync, readdirSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { packageVersion, releaseLine } from "../../common/version.js";
|
|
5
5
|
import { requireThat } from "../../common/validation.js";
|
|
6
6
|
import { AgentFiles, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
|
|
7
7
|
import { TeamFiles } from "../../storage/team-files.js";
|
|
8
|
+
import { acpModelState, acpPermission, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
|
|
8
9
|
export class AgentService {
|
|
9
10
|
root;
|
|
10
11
|
host;
|
|
@@ -46,16 +47,28 @@ export class AgentService {
|
|
|
46
47
|
const current = saved?.generation === run.generation ? saved : undefined;
|
|
47
48
|
const alive = this.alive(d.agentId), pending = this.#starting.get(d.agentId);
|
|
48
49
|
const starting = pending && !pending.controller.signal.aborted;
|
|
50
|
+
const reference = this.files.optional(d.agentId, "acp-session.json", acpReference);
|
|
51
|
+
const model = reference ? this.files.optional(d.agentId, "acp-model.json", acpModelState) : undefined;
|
|
52
|
+
const selection = model?.generation === run.generation ? { ...(model.provider ? { provider: model.provider } : {}),
|
|
53
|
+
...(model.model ? { model: model.model } : {}), ...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
|
|
54
|
+
...(model.thinking ? { thinking: model.thinking } : {}) } : undefined;
|
|
49
55
|
return { ...d, name: run.name, generation: run.generation, owned: this.owned(d, owner), alive,
|
|
56
|
+
...(reference?.host === "acp" ? { backend: reference.backend, transport: "acp",
|
|
57
|
+
capabilities: { steer: false, resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load), team: false, transcript: false },
|
|
58
|
+
...(selection ? { selection } : {}) } : {}),
|
|
50
59
|
state: alive ? starting ? "starting" : run.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
|
|
51
60
|
activity: starting ? "启动" : alive ? current?.activity ?? "启动" : current?.error ?? "已停止",
|
|
52
61
|
...(current?.cycle ? { cycle: current.cycle } : {}), ...(current?.error ? { error: current.error } : {}) };
|
|
53
62
|
}
|
|
54
|
-
async start(
|
|
63
|
+
async start(input, owner) {
|
|
55
64
|
this.check();
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
const { backend, ...supplied } = input;
|
|
66
|
+
this.host.available(backend);
|
|
67
|
+
const options = Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined));
|
|
68
|
+
this.host.validateOptions?.(options, backend);
|
|
69
|
+
requireThat(backend || options.provider && options.model, "请选择可在专家会话使用的模型。");
|
|
70
|
+
const reference = backend ? this.host.prepare?.(backend, options) : undefined;
|
|
71
|
+
requireThat(!backend || reference, "当前执行接入不支持 ACP 后端。");
|
|
59
72
|
const prepared = this.files.control(() => {
|
|
60
73
|
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中子会话;请先停止一个空闲会话。");
|
|
61
74
|
const agentId = randomUUID();
|
|
@@ -64,7 +77,7 @@ export class AgentService {
|
|
|
64
77
|
const d = { agentId, release: packageVersion, workspace: this.root, cwd,
|
|
65
78
|
parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd } };
|
|
66
79
|
const run = { generation: randomUUID(), name: options.name ?? agentId.slice(0, 8), sequence: 0, stop: false, startedAt: Date.now() };
|
|
67
|
-
this.files.create(d, run);
|
|
80
|
+
this.files.create(d, run, reference);
|
|
68
81
|
const initial = [options.message, ...(options.prompts ?? [])].filter(Boolean).join("\n\n");
|
|
69
82
|
const request = initial || files.length ? this.files.enqueue(agentId, initial, "steer", files) : undefined;
|
|
70
83
|
this.#starting.set(agentId, { generation: run.generation, controller: new AbortController(), stage: "launch" });
|
|
@@ -107,12 +120,15 @@ export class AgentService {
|
|
|
107
120
|
this.#launches.delete(operation);
|
|
108
121
|
}
|
|
109
122
|
}
|
|
110
|
-
send(id, text, delivery, owner) {
|
|
123
|
+
send(id, text, delivery, owner, attachments = []) {
|
|
111
124
|
const d = this.get(id, owner);
|
|
112
125
|
return this.files.control(() => {
|
|
113
126
|
requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "子会话已停止或仍在恢复校验,请等待显式恢复完成。");
|
|
114
127
|
this.assertPrivate(id);
|
|
115
|
-
const
|
|
128
|
+
const view = this.view(d, owner);
|
|
129
|
+
requireThat(!(view.transport === "acp" && delivery === "steer" && view.state !== "idle"), "该子代理运行中不支持 steer;请用 delivery: followUp 排队。");
|
|
130
|
+
const files = attachments.map(path => realpathSync(resolve(this.root, path)));
|
|
131
|
+
const request = this.files.enqueue(id, text, delivery, files);
|
|
116
132
|
return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" };
|
|
117
133
|
});
|
|
118
134
|
}
|
|
@@ -171,8 +187,8 @@ export class AgentService {
|
|
|
171
187
|
return this.restart(id, owner, text);
|
|
172
188
|
}
|
|
173
189
|
async restart(id, owner, text) {
|
|
174
|
-
const d = this.get(id, owner);
|
|
175
|
-
this.host.available();
|
|
190
|
+
const d = this.get(id, owner), reference = this.files.reference(id);
|
|
191
|
+
this.host.available(reference.host === "acp" ? reference.backend : undefined);
|
|
176
192
|
const reserved = this.files.control(() => {
|
|
177
193
|
requireThat(!this.alive(id), "子会话仍在运行。");
|
|
178
194
|
this.assertPrivate(id);
|
|
@@ -260,6 +276,61 @@ export class AgentService {
|
|
|
260
276
|
return "";
|
|
261
277
|
return this.host.report(d, this.files.reference(d.agentId), c.leaf);
|
|
262
278
|
}
|
|
279
|
+
async recordPage(id, owner, options = {}) {
|
|
280
|
+
const d = this.get(id, owner), view = this.view(d, owner);
|
|
281
|
+
requireThat(view.transport === "acp", "该记录入口用于 ACP 成员。");
|
|
282
|
+
const messages = this.files.messages(id), cycles = this.cycles(id, owner).sort((a, b) => a.startedAt - b.startedAt);
|
|
283
|
+
const records = [];
|
|
284
|
+
const included = new Set();
|
|
285
|
+
for (const c of cycles) {
|
|
286
|
+
for (const requestId of c.requestIds) {
|
|
287
|
+
const request = messages.find(message => message.requestId === requestId);
|
|
288
|
+
if (!request || included.has(requestId))
|
|
289
|
+
continue;
|
|
290
|
+
included.add(requestId);
|
|
291
|
+
records.push({ seq: records.length, type: "acp/input", time: c.startedAt, requestId, state: c.state,
|
|
292
|
+
text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
|
|
293
|
+
}
|
|
294
|
+
const report = await this.report(d, c);
|
|
295
|
+
this.check();
|
|
296
|
+
records.push({ seq: records.length, type: "acp/report", time: c.finishedAt ?? c.startedAt, cycleId: c.cycleId, state: c.state,
|
|
297
|
+
text: [c.error, report].filter(Boolean).join("\n\n") });
|
|
298
|
+
}
|
|
299
|
+
const run = this.files.read(id, "launch.json", launch);
|
|
300
|
+
for (const request of messages.filter(message => message.generation === run.generation && !included.has(message.requestId))) {
|
|
301
|
+
records.push({ seq: records.length, type: "acp/input", time: run.startedAt, requestId: request.requestId, state: "queued",
|
|
302
|
+
text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
|
|
303
|
+
}
|
|
304
|
+
const cursor = run.generation + ":" + records.length + ":" + cycles.at(-1)?.state;
|
|
305
|
+
if (options.seq !== undefined) {
|
|
306
|
+
const record = records[options.seq];
|
|
307
|
+
requireThat(record, "ACP 记录位置不存在。");
|
|
308
|
+
const text = options.format === "json" ? JSON.stringify(record, null, 2) : record.text;
|
|
309
|
+
const offset = options.textOffset ?? 0, end = offset + (options.textLimit ?? 12000);
|
|
310
|
+
requireThat(offset <= text.length, "ACP 记录分页位置超出范围。");
|
|
311
|
+
return { kind: "acp", agent: view, cursor, seq: record.seq, offset, text: text.slice(offset, end), totalLength: text.length, nextOffset: end < text.length ? end : null };
|
|
312
|
+
}
|
|
313
|
+
const limit = options.limit ?? 30, offset = options.offset ?? Math.max(0, records.length - limit);
|
|
314
|
+
requireThat(offset <= records.length, "ACP 记录分页位置超出范围。");
|
|
315
|
+
return { kind: "acp", agent: view, cursor, total: records.length, offset, nextOffset: offset + limit < records.length ? offset + limit : null,
|
|
316
|
+
records: records.slice(offset, offset + limit).map(record => { const { text, ...fields } = record; return { ...fields, preview: text.slice(0, 800) }; }) };
|
|
317
|
+
}
|
|
318
|
+
pendingPermissions(owner) {
|
|
319
|
+
return this.list(owner).filter(view => view.owned && view.alive && view.transport === "acp").flatMap(view => readdirSync(resolve(this.files.directory(view.agentId), "permissions")).filter(name => name.endsWith(".json") && !name.endsWith("-reply.json")).flatMap(name => {
|
|
320
|
+
const request = this.files.read(view.agentId, "permissions/" + name, acpPermission);
|
|
321
|
+
if (request.generation !== view.generation || this.files.optional(view.agentId, "permissions/" + request.requestId + "-reply.json", acpPermissionReply))
|
|
322
|
+
return [];
|
|
323
|
+
return [{ ...request, agentId: view.agentId, name: view.name }];
|
|
324
|
+
})).slice(0, 32);
|
|
325
|
+
}
|
|
326
|
+
answerPermission(id, requestId, optionId, owner) {
|
|
327
|
+
this.get(id, owner);
|
|
328
|
+
const request = this.files.read(id, "permissions/" + requestId + ".json", acpPermission);
|
|
329
|
+
const run = this.files.read(id, "launch.json", launch);
|
|
330
|
+
requireThat(run.generation === request.generation && !run.stop && this.alive(id), "ACP 确认请求已失效。");
|
|
331
|
+
requireThat(optionId === null || request.options.some(option => option.optionId === optionId), "ACP 确认选项无效。");
|
|
332
|
+
this.files.write(id, "permissions/" + requestId + "-reply.json", { generation: run.generation, requestId, optionId }, acpPermissionReply, true);
|
|
333
|
+
}
|
|
263
334
|
release(id) {
|
|
264
335
|
let pending = this.#releasing.get(id);
|
|
265
336
|
if (!pending) {
|
|
@@ -129,7 +129,8 @@ export class TeamService {
|
|
|
129
129
|
this.files.save(team);
|
|
130
130
|
}
|
|
131
131
|
pauseAll(owner, reason) {
|
|
132
|
-
this.
|
|
132
|
+
if (!this.records(owner.sessionId).length)
|
|
133
|
+
return;
|
|
133
134
|
this.agents.control(() => { for (const team of this.records(owner.sessionId))
|
|
134
135
|
this.pauseRecord(team, reason); });
|
|
135
136
|
}
|
|
@@ -244,7 +245,8 @@ export class TeamService {
|
|
|
244
245
|
});
|
|
245
246
|
}
|
|
246
247
|
advance(owner, views, completions) {
|
|
247
|
-
this.
|
|
248
|
+
if (!this.records(owner.sessionId).length)
|
|
249
|
+
return [];
|
|
248
250
|
return this.agents.control(() => {
|
|
249
251
|
const inputs = [], records = this.records(owner.sessionId), links = this.files.links(records);
|
|
250
252
|
for (const team of records) {
|
|
@@ -6,6 +6,16 @@ function config(value) {
|
|
|
6
6
|
if (!object(value) || value.enabled !== undefined && typeof value.enabled !== "boolean") {
|
|
7
7
|
throw new Error("配置必须是 JSON 对象,enabled 必须是布尔值。");
|
|
8
8
|
}
|
|
9
|
+
if (value.acp !== undefined) {
|
|
10
|
+
if (!object(value.acp) || Object.keys(value.acp).some(key => !["pi", "dsh", "claude"].includes(key)))
|
|
11
|
+
throw new Error("acp 仅接受 pi、dsh 和 claude 启动配置。");
|
|
12
|
+
for (const command of Object.values(value.acp)) {
|
|
13
|
+
if (!object(command) || typeof command.command !== "string" || !command.command.trim() || command.command.includes("\0") ||
|
|
14
|
+
!Array.isArray(command.args) || command.args.some(arg => typeof arg !== "string" || arg.includes("\0")) ||
|
|
15
|
+
Object.keys(command).some(key => !["command", "args"].includes(key)))
|
|
16
|
+
throw new Error("ACP 启动配置需要 command 和 args 字符串数组。");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
9
19
|
return value;
|
|
10
20
|
}
|
|
11
21
|
export function workspaceConfigPath(root) {
|
|
@@ -25,7 +35,8 @@ export async function readWorkspaceConfig(root) {
|
|
|
25
35
|
}
|
|
26
36
|
try {
|
|
27
37
|
const value = config(JSON.parse(content));
|
|
28
|
-
|
|
38
|
+
const settings = value;
|
|
39
|
+
return { path, present: true, enabled: settings.enabled === true, ...(settings.acp ? { acp: settings.acp } : {}) };
|
|
29
40
|
}
|
|
30
41
|
catch (error) {
|
|
31
42
|
return { path, present: true, enabled: false, error: `工作区配置无效 ${path}: ${String(error)}` };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AcpAnswer, AcpInput, AcpPermission, AcpPermissionReply, AcpModelState, AcpProcess, AcpReport, AcpSessionReference } from "../model/acp.js";
|
|
2
|
+
export declare function acpReference(value: unknown): AcpSessionReference;
|
|
3
|
+
export declare function acpModelState(value: unknown): AcpModelState;
|
|
4
|
+
export declare function acpProcess(value: unknown): AcpProcess;
|
|
5
|
+
export declare function acpInput(value: unknown): AcpInput;
|
|
6
|
+
export declare function acpAnswer(value: unknown): AcpAnswer;
|
|
7
|
+
export declare function acpReport(value: unknown): AcpReport;
|
|
8
|
+
export declare function acpPermission(value: unknown): AcpPermission;
|
|
9
|
+
export declare function acpPermissionReply(value: unknown): AcpPermissionReply;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import { object } from "../common/json.js";
|
|
3
|
+
import { requireThat } from "../common/validation.js";
|
|
4
|
+
import { fileId } from "./file-access.js";
|
|
5
|
+
const text = (value) => typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
6
|
+
export function acpReference(value) {
|
|
7
|
+
requireThat(object(value) && value.host === "acp" && ["pi", "dsh", "claude"].includes(String(value.backend)) &&
|
|
8
|
+
text(value.command) && isAbsolute(value.command) && Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")) &&
|
|
9
|
+
(value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
|
|
10
|
+
const capabilities = value.capabilities;
|
|
11
|
+
if (capabilities !== undefined)
|
|
12
|
+
requireThat(object(capabilities) &&
|
|
13
|
+
["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
export function acpModelState(value) {
|
|
17
|
+
requireThat(object(value) && text(value.generation) && ["provider", "model", "resolvedModel", "thinking"].every(key => value[key] === undefined || text(value[key])), "ACP 模型选择无效。");
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
export function acpProcess(value) {
|
|
21
|
+
requireThat(object(value) && text(value.generation) && Number.isSafeInteger(value.pid) && Number(value.pid) > 1 &&
|
|
22
|
+
typeof value.startedAt === "number" && Number.isFinite(value.startedAt), "ACP 进程记录无效。");
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
export function acpInput(value) {
|
|
26
|
+
requireThat(object(value) && text(value.generation) && text(value.requestId), "ACP 输入关联无效。");
|
|
27
|
+
fileId(value.requestId);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
export function acpAnswer(value) {
|
|
31
|
+
acpInput(value);
|
|
32
|
+
requireThat(object(value) && typeof value.answer === "string" && ["completed", "failed", "cancelled"].includes(String(value.state)) &&
|
|
33
|
+
(value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
export function acpReport(value) {
|
|
37
|
+
requireThat(object(value) && text(value.generation) && text(value.cycleId) && typeof value.text === "string", "ACP 周期报告无效。");
|
|
38
|
+
fileId(value.cycleId);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
export function acpPermission(value) {
|
|
42
|
+
acpInput(value);
|
|
43
|
+
requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && ["allow_once", "allow_always", "reject_once", "reject_always"].includes(String(option.kind))), "ACP 确认请求无效。");
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
export function acpPermissionReply(value) {
|
|
47
|
+
acpInput(value);
|
|
48
|
+
requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionRef, AgentSessionReference } from "../model/agent.js";
|
|
1
|
+
import type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionRef, AgentSessionReference, NativeAgentHost } from "../model/agent.js";
|
|
2
|
+
import type { AcpSessionReference } from "../model/acp.js";
|
|
2
3
|
export declare function descriptor(v: unknown): AgentDescriptor;
|
|
3
4
|
export declare function sessionRef(v: unknown): AgentSessionRef;
|
|
4
5
|
export declare function launch(v: unknown): AgentLaunch;
|
|
@@ -8,15 +9,15 @@ export declare function status(v: unknown): AgentStatus;
|
|
|
8
9
|
export declare class AgentFiles {
|
|
9
10
|
#private;
|
|
10
11
|
readonly root: string;
|
|
11
|
-
readonly host:
|
|
12
|
-
constructor(root: string, host?:
|
|
12
|
+
readonly host: NativeAgentHost;
|
|
13
|
+
constructor(root: string, host?: NativeAgentHost);
|
|
13
14
|
check(): void;
|
|
14
15
|
fail(error: unknown): void;
|
|
15
16
|
directory(id: string): string;
|
|
16
17
|
read<T>(id: string, path: string, parse: (v: unknown) => T): T;
|
|
17
18
|
optional<T>(id: string, path: string, parse: (v: unknown) => T): T | undefined;
|
|
18
19
|
write<T>(id: string, path: string, value: T, parse: (v: unknown) => T, create?: boolean): void;
|
|
19
|
-
create(value: AgentDescriptor, run: AgentLaunch): void;
|
|
20
|
+
create(value: AgentDescriptor, run: AgentLaunch, acp?: AcpSessionReference): void;
|
|
20
21
|
list(): AgentDescriptor[];
|
|
21
22
|
messages(id: string): AgentMessage[];
|
|
22
23
|
completions(id: string): AgentCycle[];
|