personal-bot 0.3.7 → 0.4.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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +5 -8
- package/README.zh-CN.md +5 -8
- package/bin/pbot-pi-acp.mjs +1 -1
- package/docs/architecture/design.md +2 -2
- package/docs/architecture/extensibility.md +1 -1
- package/docs/guides/cross-harness-workers.md +4 -4
- package/docs/guides/history.md +1 -3
- package/docs/guides/workspace-configuration.md +4 -4
- package/docs/hosts/claude-code.md +2 -2
- package/docs/hosts/codex-qoder.md +1 -1
- package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +1 -1
- package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +1 -1
- package/lib/antigravity-plugin/lib/extension/acp/member.js +11 -10
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +346 -201
- package/lib/antigravity-plugin/lib/extension/cli.js +563 -538
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +1 -1
- package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +3 -4
- package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +0 -11
- package/lib/antigravity-plugin/lib/extension/mcp.js +397 -278
- package/lib/antigravity-plugin/lib/model/acp.d.ts +29 -35
- package/lib/antigravity-plugin/lib/model/agent.d.ts +33 -17
- package/lib/antigravity-plugin/lib/model/agent.js +0 -1
- package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +5 -8
- package/lib/antigravity-plugin/lib/storage/acp-files.js +34 -40
- package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +19 -10
- package/lib/antigravity-plugin/lib/storage/agent-files.js +147 -43
- package/lib/antigravity-plugin/package.json +1 -1
- package/lib/antigravity-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
- package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
- package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +1 -1
- package/lib/extension/acp/member.d.ts +1 -1
- package/lib/extension/acp/member.js +11 -10
- package/lib/extension/acp/worker.js +34 -33
- package/lib/extension/acp-worker.js +346 -201
- package/lib/extension/cli.js +563 -538
- package/lib/extension/hosts/dsh/acp-member.js +1 -1
- package/lib/extension/hosts/dsh/command.js +1 -1
- package/lib/extension/hosts/dsh/workspace.js +1 -3
- package/lib/extension/hosts/pi/acp-member.js +3 -4
- package/lib/extension/hosts/plugin-runtime.js +0 -11
- package/lib/extension/mcp/tool-contract.d.ts +8 -50
- package/lib/extension/mcp/tool-contract.js +0 -8
- package/lib/extension/mcp/workers.d.ts +7 -27
- package/lib/extension/mcp/workers.js +35 -17
- package/lib/extension/mcp.js +397 -278
- package/lib/integrations/acp/agents.d.ts +2 -3
- package/lib/integrations/acp/agents.js +22 -22
- package/lib/integrations/acp/harnesses/claude.js +1 -1
- package/lib/model/acp.d.ts +29 -35
- package/lib/model/agent.d.ts +33 -17
- package/lib/model/agent.js +0 -1
- package/lib/qoder-plugin/.mcp.json +2 -2
- package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
- package/lib/qoder-plugin/hooks/hooks.json +3 -3
- package/lib/qoder-plugin/skills/pbot/SKILL.md +1 -1
- package/lib/services/agents/acp-run.d.ts +6 -4
- package/lib/services/agents/acp-run.js +62 -43
- package/lib/services/agents/agent-run.d.ts +10 -6
- package/lib/services/agents/agent-run.js +142 -65
- package/lib/services/agents/agent-service.d.ts +3 -21
- package/lib/services/agents/agent-service.js +180 -126
- package/lib/storage/acp-files.d.ts +5 -8
- package/lib/storage/acp-files.js +34 -40
- package/lib/storage/agent-files.d.ts +19 -10
- package/lib/storage/agent-files.js +147 -43
- package/package.json +1 -1
- package/src/extension/acp/member.ts +14 -11
- package/src/extension/acp/worker.ts +31 -28
- package/src/extension/cli/commands/upgrade.ts +8 -36
- package/src/extension/cli/commands/workspace.ts +3 -8
- package/src/extension/cli/index.ts +1 -2
- package/src/extension/hosts/dsh/acp-member.ts +1 -1
- package/src/extension/hosts/dsh/command.ts +1 -1
- package/src/extension/hosts/dsh/workspace.ts +1 -3
- package/src/extension/hosts/pi/acp-member.ts +3 -4
- package/src/extension/hosts/pi/command.ts +1 -1
- package/src/extension/hosts/pi/lifecycle.ts +0 -2
- package/src/extension/hosts/plugin-runtime.ts +0 -10
- package/src/extension/mcp/tool-contract.ts +0 -8
- package/src/extension/mcp/workers.ts +34 -17
- package/src/integrations/acp/agents.ts +28 -27
- package/src/integrations/acp/harnesses/claude.ts +1 -1
- package/src/integrations/hosts/dsh/session.ts +2 -2
- package/src/integrations/installation/cli-update.ts +2 -2
- package/src/integrations/installation/installers/claude.ts +4 -4
- package/src/integrations/installation/installers/qoder.ts +5 -5
- package/src/integrations/installation/upgraders/antigravity.ts +57 -15
- package/src/integrations/installation/upgraders/claude.ts +1 -4
- package/src/integrations/installation/upgraders/codex.ts +85 -15
- package/src/integrations/installation/upgraders/dsh.ts +46 -24
- package/src/integrations/installation/upgraders/pi.ts +18 -11
- package/src/integrations/installation/upgraders/qoder.ts +95 -20
- package/src/model/acp.ts +27 -29
- package/src/model/agent.ts +80 -16
- package/src/model/installation.ts +0 -2
- package/src/pbot.ts +1 -1
- package/src/services/agents/acp-run.ts +81 -49
- package/src/services/agents/agent-run.ts +154 -56
- package/src/services/agents/agent-service.ts +204 -133
- package/src/services/installation/upgrade-service.ts +1 -5
- package/src/storage/acp-files.ts +50 -45
- package/src/storage/agent-files.ts +159 -46
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +0 -15
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +0 -126
- package/lib/services/workspace/plugin-releases.d.ts +0 -15
- package/lib/services/workspace/plugin-releases.js +0 -126
- package/src/services/workspace/plugin-releases.ts +0 -121
|
@@ -4,8 +4,8 @@ import { resolve } from "node:path";
|
|
|
4
4
|
import { object } from "../../common/json.js";
|
|
5
5
|
import { packageVersion, releaseLine } from "../../common/version.js";
|
|
6
6
|
import { requireThat } from "../../common/validation.js";
|
|
7
|
-
import { AgentFiles,
|
|
8
|
-
import {
|
|
7
|
+
import { AgentFiles, outputRecord, status, toDescriptor, workerRecord } from "../../storage/agent-files.js";
|
|
8
|
+
import { acpInteractionReply } from "../../storage/acp-files.js";
|
|
9
9
|
/** Host-neutral lifecycle manager for cross-harness ACP workers. */
|
|
10
10
|
export class AgentService {
|
|
11
11
|
root;
|
|
@@ -24,59 +24,56 @@ export class AgentService {
|
|
|
24
24
|
}
|
|
25
25
|
alive(id) { return this.#starting.has(id) || this.#releasing.has(id) || this.host.alive(id); }
|
|
26
26
|
check() { requireThat(!this.#closed, "worker 服务已关闭。"); }
|
|
27
|
-
owned(
|
|
28
|
-
return
|
|
27
|
+
owned(w, owner) {
|
|
28
|
+
return w.workspace === this.root && w.parentSessionId === owner.sessionId;
|
|
29
29
|
}
|
|
30
30
|
get(id, owner) {
|
|
31
31
|
this.check();
|
|
32
|
-
const
|
|
33
|
-
requireThat(this.owned(
|
|
34
|
-
requireThat(releaseLine(
|
|
35
|
-
return
|
|
32
|
+
const w = this.files.worker(id);
|
|
33
|
+
requireThat(this.owned(w, owner), "worker 不属于当前会话。");
|
|
34
|
+
requireThat(releaseLine(w.release) === releaseLine(packageVersion), "worker 版本不兼容;请停止后使用匹配版本。");
|
|
35
|
+
return w;
|
|
36
36
|
}
|
|
37
37
|
records(owner) {
|
|
38
38
|
this.check();
|
|
39
|
-
return this.files.list().filter(
|
|
40
|
-
releaseLine(
|
|
39
|
+
return this.files.list().filter(w => w.workspace === this.root && w.parentSessionId === owner.sessionId &&
|
|
40
|
+
releaseLine(w.release) === releaseLine(packageVersion));
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
has(id, owner) {
|
|
43
|
+
if (this.#closed)
|
|
44
|
+
return false;
|
|
45
|
+
const w = this.files.optional(id, "worker.json", workerRecord);
|
|
46
|
+
return Boolean(w && this.owned(w, owner) && releaseLine(w.release) === releaseLine(packageVersion));
|
|
47
|
+
}
|
|
48
|
+
list(owner) { return this.records(owner).map(w => this.view(w, owner)); }
|
|
49
|
+
view(w, owner) {
|
|
50
|
+
const saved = this.files.optional(w.agentId, "state.json", status);
|
|
51
|
+
const current = saved?.generation === w.generation ? saved : undefined;
|
|
52
|
+
const alive = this.alive(w.agentId), pending = this.#starting.get(w.agentId);
|
|
48
53
|
const starting = pending && !pending.controller.signal.aborted;
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
...this.files.pendingPermissions(d.agentId, run.generation)
|
|
59
|
-
.map(item => ({ requestId: item.requestId, kind: "permission", description: item.description, options: item.options })),
|
|
60
|
-
...this.files.pendingElicitations(d.agentId, run.generation)
|
|
61
|
-
.map(item => (item.mode === "form"
|
|
62
|
-
? { requestId: item.requestId, kind: "form", message: item.message, schema: item.schema ?? { type: "object", properties: {} } }
|
|
63
|
-
: { requestId: item.requestId, kind: "url", message: item.message, url: item.url ?? "" })),
|
|
64
|
-
] : [];
|
|
54
|
+
const selection = current?.selection && Object.keys(current.selection).length ? current.selection : undefined;
|
|
55
|
+
const pendingRequests = alive
|
|
56
|
+
? this.files.pendingInteractions(w.agentId, w.generation).map(item => (item.kind === "permission"
|
|
57
|
+
? { requestId: item.requestId, kind: "permission", description: item.description, options: item.options }
|
|
58
|
+
: item.kind === "form"
|
|
59
|
+
? { requestId: item.requestId, kind: "form", message: item.message, schema: item.schema }
|
|
60
|
+
: { requestId: item.requestId, kind: "url", message: item.message, url: item.url }))
|
|
61
|
+
: [];
|
|
62
|
+
const capabilities = saved?.capabilities;
|
|
65
63
|
return {
|
|
66
|
-
...
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
owned: this.owned(d, owner),
|
|
64
|
+
...toDescriptor(w),
|
|
65
|
+
generation: w.generation,
|
|
66
|
+
owned: this.owned(w, owner),
|
|
70
67
|
alive,
|
|
71
|
-
backend:
|
|
68
|
+
backend: w.acp.backend,
|
|
72
69
|
transport: "acp",
|
|
73
70
|
capabilities: {
|
|
74
71
|
steer: false,
|
|
75
|
-
resume: Boolean(
|
|
72
|
+
resume: Boolean(capabilities?.resume || capabilities?.load),
|
|
76
73
|
transcript: false,
|
|
77
74
|
},
|
|
78
75
|
...(selection ? { selection } : {}),
|
|
79
|
-
state: alive ? starting ? "starting" :
|
|
76
|
+
state: alive ? starting ? "starting" : w.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
|
|
80
77
|
activity: starting ? "启动" : alive ? current?.activity ?? "启动" : current?.error ?? "已停止",
|
|
81
78
|
...(current?.cycle ? { cycle: current.cycle } : {}),
|
|
82
79
|
...(current?.error ? { error: current.error } : {}),
|
|
@@ -85,8 +82,8 @@ export class AgentService {
|
|
|
85
82
|
}
|
|
86
83
|
/** Answer a pending permission or elicitation request forwarded by the worker. */
|
|
87
84
|
respond(id, input, owner) {
|
|
88
|
-
const
|
|
89
|
-
const pending = this.view(
|
|
85
|
+
const w = this.get(id, owner);
|
|
86
|
+
const pending = this.view(w, owner).pendingRequests ?? [];
|
|
90
87
|
if (!input.requestId && pending.length > 1) {
|
|
91
88
|
throw new Error("存在多条待回复请求,请指定 requestId:" + pending.map(item => item.requestId).join(", "));
|
|
92
89
|
}
|
|
@@ -94,6 +91,7 @@ export class AgentService {
|
|
|
94
91
|
requireThat(target, input.requestId
|
|
95
92
|
? "没有待回复的请求:" + input.requestId + ";用 worker_discover 查看 pendingRequests。"
|
|
96
93
|
: "当前没有待回复的请求。");
|
|
94
|
+
this.files.ensureSubdirectory(id, "interactions");
|
|
97
95
|
if (target.kind === "permission") {
|
|
98
96
|
requireThat(input.content === undefined, "权限请求不使用 content;请用 action(accept / decline / cancel)或指定 optionId。");
|
|
99
97
|
let resolved = null;
|
|
@@ -125,9 +123,8 @@ export class AgentService {
|
|
|
125
123
|
resolved = pick("reject_once", "reject_always") ?? null;
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
|
-
this.files.
|
|
129
|
-
this.
|
|
130
|
-
return this.view(d, owner);
|
|
126
|
+
this.files.write(id, "interactions/" + target.requestId + "-reply.json", { generation: w.generation, requestId: target.requestId, kind: "permission", optionId: resolved }, acpInteractionReply, true);
|
|
127
|
+
return this.view(w, owner);
|
|
131
128
|
}
|
|
132
129
|
if (target.kind === "form") {
|
|
133
130
|
requireThat(input.optionId === undefined, "问答/表单请求不使用 optionId;请将答案填入 content。");
|
|
@@ -137,28 +134,28 @@ export class AgentService {
|
|
|
137
134
|
else {
|
|
138
135
|
requireThat(input.content === undefined || object(input.content), "问答/表单答复的 content 必须是对象。");
|
|
139
136
|
}
|
|
140
|
-
this.files.
|
|
141
|
-
|
|
142
|
-
generation: run.generation,
|
|
137
|
+
this.files.write(id, "interactions/" + target.requestId + "-reply.json", {
|
|
138
|
+
generation: w.generation,
|
|
143
139
|
requestId: target.requestId,
|
|
140
|
+
kind: "form",
|
|
144
141
|
action: input.action,
|
|
145
142
|
...(input.action === "accept" ? { content: input.content ?? {} } : {}),
|
|
146
|
-
},
|
|
147
|
-
return this.view(
|
|
143
|
+
}, acpInteractionReply, true);
|
|
144
|
+
return this.view(w, owner);
|
|
148
145
|
}
|
|
149
146
|
requireThat(input.optionId === undefined, "URL 确认请求不使用 optionId。");
|
|
150
147
|
requireThat(input.content === undefined, "URL 确认请求不使用 content。");
|
|
151
|
-
this.files.
|
|
152
|
-
|
|
153
|
-
generation: run.generation,
|
|
148
|
+
this.files.write(id, "interactions/" + target.requestId + "-reply.json", {
|
|
149
|
+
generation: w.generation,
|
|
154
150
|
requestId: target.requestId,
|
|
151
|
+
kind: "url",
|
|
155
152
|
action: input.action,
|
|
156
|
-
},
|
|
157
|
-
return this.view(
|
|
153
|
+
}, acpInteractionReply, true);
|
|
154
|
+
return this.view(w, owner);
|
|
158
155
|
}
|
|
159
156
|
async start(input, owner) {
|
|
160
157
|
this.check();
|
|
161
|
-
const { backend, ...supplied } = input;
|
|
158
|
+
const { backend, message: initialMessage, files: initialFiles, ...supplied } = input;
|
|
162
159
|
this.host.available(backend);
|
|
163
160
|
const clean = Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined));
|
|
164
161
|
const options = this.host.filterOptions ? this.host.filterOptions(backend, clean) : clean;
|
|
@@ -166,21 +163,26 @@ export class AgentService {
|
|
|
166
163
|
const prepared = this.files.control(() => {
|
|
167
164
|
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker;请先停止一个空闲会话。");
|
|
168
165
|
const agentId = randomUUID();
|
|
169
|
-
const
|
|
170
|
-
const files = (options.files ?? []).map(p => realpathSync(resolve(this.root, p)));
|
|
166
|
+
const files = (initialFiles ?? []).map(p => realpathSync(resolve(this.root, p)));
|
|
171
167
|
const d = {
|
|
172
|
-
agentId,
|
|
173
|
-
|
|
168
|
+
agentId,
|
|
169
|
+
release: packageVersion,
|
|
170
|
+
workspace: this.root,
|
|
171
|
+
parentSessionId: owner.sessionId,
|
|
172
|
+
options,
|
|
174
173
|
};
|
|
175
|
-
const run = { generation: randomUUID(),
|
|
174
|
+
const run = { generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
|
|
176
175
|
this.files.create(d, run, reference);
|
|
177
|
-
const initial =
|
|
176
|
+
const initial = initialMessage ?? "";
|
|
178
177
|
const request = initial || files.length ? this.files.enqueue(agentId, initial, "steer", files) : undefined;
|
|
179
178
|
this.#starting.set(agentId, { generation: run.generation, controller: new AbortController(), stage: "launch" });
|
|
180
179
|
return { d, run, request };
|
|
181
180
|
});
|
|
182
181
|
await this.launch(prepared.d, prepared.run.generation);
|
|
183
|
-
return {
|
|
182
|
+
return {
|
|
183
|
+
...this.view(this.files.worker(prepared.d.agentId), owner),
|
|
184
|
+
...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" } : {}),
|
|
185
|
+
};
|
|
184
186
|
}
|
|
185
187
|
async launch(d, generation, resume = false) {
|
|
186
188
|
const pending = this.#starting.get(d.agentId);
|
|
@@ -190,17 +192,23 @@ export class AgentService {
|
|
|
190
192
|
await this.host.launch(d, this.files.directory(d.agentId), generation, this.files.reference(d.agentId), resume, pending.controller.signal);
|
|
191
193
|
requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
|
|
192
194
|
pending.controller.signal.throwIfAborted();
|
|
193
|
-
const
|
|
194
|
-
requireThat(
|
|
195
|
+
const w = this.files.worker(d.agentId);
|
|
196
|
+
requireThat(w.generation === generation && !w.stop, "worker 启动已取消或代次变化。");
|
|
195
197
|
}
|
|
196
198
|
catch (error) {
|
|
197
|
-
const
|
|
198
|
-
if (
|
|
199
|
+
const w = this.files.worker(d.agentId);
|
|
200
|
+
if (w.generation === generation) {
|
|
199
201
|
if (this.host.alive(d.agentId))
|
|
200
202
|
await this.release(d.agentId);
|
|
201
|
-
this.files.
|
|
202
|
-
|
|
203
|
-
|
|
203
|
+
const existing = this.files.optional(d.agentId, "state.json", status);
|
|
204
|
+
this.files.write(d.agentId, "state.json", {
|
|
205
|
+
generation,
|
|
206
|
+
state: w.stop ? "stopped" : "error",
|
|
207
|
+
activity: w.stop ? "已停止" : "启动失败",
|
|
208
|
+
updatedAt: Date.now(),
|
|
209
|
+
...(existing?.sessionId !== undefined ? { sessionId: existing.sessionId } : {}),
|
|
210
|
+
...(existing?.capabilities ? { capabilities: existing.capabilities } : {}),
|
|
211
|
+
...(w.stop ? {} : { error: String(error) }),
|
|
204
212
|
}, status);
|
|
205
213
|
}
|
|
206
214
|
throw error;
|
|
@@ -219,49 +227,40 @@ export class AgentService {
|
|
|
219
227
|
}
|
|
220
228
|
}
|
|
221
229
|
send(id, text, delivery, owner, attachments = []) {
|
|
222
|
-
const
|
|
230
|
+
const w = this.get(id, owner);
|
|
223
231
|
return this.files.control(() => {
|
|
224
|
-
|
|
225
|
-
|
|
232
|
+
const latest = this.files.worker(id);
|
|
233
|
+
requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !latest.stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
|
|
234
|
+
const view = this.view(latest, owner);
|
|
226
235
|
requireThat(!(delivery === "steer" && view.state !== "idle"), "该 ACP worker 运行中不支持 steer;请用 delivery: followUp 排队。");
|
|
227
236
|
const files = attachments.map(path => realpathSync(resolve(this.root, path)));
|
|
228
237
|
const request = this.files.enqueue(id, text, delivery, files);
|
|
229
|
-
return { ...this.view(
|
|
238
|
+
return { ...this.view(this.files.worker(w.agentId), owner), requestId: request.requestId, messageStatus: "queued" };
|
|
230
239
|
});
|
|
231
240
|
}
|
|
232
|
-
rename(id, name, owner) {
|
|
233
|
-
requireThat(name.trim().length > 0 && name.length <= 64 && !/[\x00-\x1f]/.test(name), "名称必须是 1 到 64 字符的单行文本。");
|
|
234
|
-
const d = this.get(id, owner);
|
|
235
|
-
this.files.control(() => { const run = this.files.read(id, "launch.json", launch); run.name = name; this.files.write(id, "launch.json", run, launch); });
|
|
236
|
-
return this.view(d, owner);
|
|
237
|
-
}
|
|
238
241
|
stop(id, owner) {
|
|
239
|
-
|
|
242
|
+
this.get(id, owner);
|
|
240
243
|
this.requestStop(id);
|
|
241
|
-
return this.view(
|
|
244
|
+
return this.view(this.files.worker(id), owner);
|
|
242
245
|
}
|
|
243
246
|
requestStop(id) {
|
|
244
|
-
this.files.control(() => {
|
|
247
|
+
this.files.control(() => {
|
|
248
|
+
const w = this.files.worker(id);
|
|
249
|
+
w.stop = true;
|
|
250
|
+
this.files.write(id, "worker.json", w, workerRecord);
|
|
251
|
+
});
|
|
245
252
|
this.#starting.get(id)?.controller.abort();
|
|
246
253
|
}
|
|
247
|
-
async enter(id, owner) {
|
|
248
|
-
const d = this.get(id, owner);
|
|
249
|
-
if (this.alive(id)) {
|
|
250
|
-
requireThat(!this.files.read(id, "launch.json", launch).stop, "worker 正在停止,请稍后操作。");
|
|
251
|
-
return this.view(d, owner);
|
|
252
|
-
}
|
|
253
|
-
return this.restart(id, owner);
|
|
254
|
-
}
|
|
255
254
|
resume(id, text, owner) {
|
|
256
255
|
return this.restart(id, owner, text);
|
|
257
256
|
}
|
|
258
257
|
async restart(id, owner, text) {
|
|
259
|
-
const
|
|
258
|
+
const w = this.get(id, owner), d = toDescriptor(w), reference = this.files.reference(id);
|
|
260
259
|
this.host.available(reference.backend);
|
|
261
260
|
const reserved = this.files.control(() => {
|
|
262
261
|
requireThat(!this.alive(id), "worker 仍在运行。");
|
|
263
262
|
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker。");
|
|
264
|
-
const previous = this.files.
|
|
263
|
+
const previous = this.files.worker(id), interrupted = this.cycles(id, owner);
|
|
265
264
|
const pending = { generation: previous.generation, controller: new AbortController(), stage: "validate" };
|
|
266
265
|
this.#starting.set(id, pending);
|
|
267
266
|
return { previous, interrupted, pending };
|
|
@@ -271,21 +270,41 @@ export class AgentService {
|
|
|
271
270
|
requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
|
|
272
271
|
reserved.pending.controller.signal.throwIfAborted();
|
|
273
272
|
const prepared = this.files.control(() => {
|
|
274
|
-
requireThat(!this.host.alive(id) && this.files.
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
273
|
+
requireThat(!this.host.alive(id) && this.files.worker(id).generation === reserved.previous.generation, "worker 已由其他操作恢复。");
|
|
274
|
+
const existingOutput = this.files.output(id);
|
|
275
|
+
const lastInterrupted = reserved.interrupted.findLast(ended => ended.generation === reserved.previous.generation && existingOutput?.cycleId !== ended.cycleId);
|
|
276
|
+
if (lastInterrupted && lastInterrupted.state !== "running") {
|
|
277
|
+
this.files.write(id, "output.json", {
|
|
278
|
+
cycleId: lastInterrupted.cycleId,
|
|
279
|
+
generation: lastInterrupted.generation,
|
|
280
|
+
startedAt: lastInterrupted.startedAt,
|
|
281
|
+
finishedAt: lastInterrupted.finishedAt ?? Date.now(),
|
|
282
|
+
state: lastInterrupted.state,
|
|
283
|
+
requestIds: lastInterrupted.requestIds,
|
|
284
|
+
text: "",
|
|
285
|
+
...(lastInterrupted.error ? { error: lastInterrupted.error } : {}),
|
|
286
|
+
}, outputRecord);
|
|
279
287
|
}
|
|
280
|
-
|
|
281
|
-
|
|
288
|
+
this.files.clearTransient(id);
|
|
289
|
+
const next = {
|
|
290
|
+
...reserved.previous,
|
|
291
|
+
generation: randomUUID(),
|
|
292
|
+
sequence: 0,
|
|
293
|
+
stop: false,
|
|
294
|
+
startedAt: Date.now(),
|
|
295
|
+
};
|
|
296
|
+
delete next.pid;
|
|
297
|
+
this.files.write(id, "worker.json", next, workerRecord);
|
|
282
298
|
const request = text === undefined ? undefined : this.files.enqueue(id, text, "steer");
|
|
283
|
-
reserved.pending.generation =
|
|
299
|
+
reserved.pending.generation = next.generation;
|
|
284
300
|
reserved.pending.stage = "launch";
|
|
285
|
-
return { run, request };
|
|
301
|
+
return { run: next, request };
|
|
286
302
|
});
|
|
287
303
|
await this.launch(d, prepared.run.generation, true);
|
|
288
|
-
return {
|
|
304
|
+
return {
|
|
305
|
+
...this.view(this.files.worker(id), owner),
|
|
306
|
+
...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" } : {}),
|
|
307
|
+
};
|
|
289
308
|
})();
|
|
290
309
|
this.#launches.add(operation);
|
|
291
310
|
try {
|
|
@@ -302,24 +321,48 @@ export class AgentService {
|
|
|
302
321
|
}
|
|
303
322
|
cyclesFor(view) {
|
|
304
323
|
const id = view.agentId;
|
|
305
|
-
const values =
|
|
324
|
+
const values = [];
|
|
325
|
+
const output = this.files.output(id);
|
|
326
|
+
if (output) {
|
|
327
|
+
values.push({
|
|
328
|
+
cycleId: output.cycleId,
|
|
329
|
+
generation: output.generation,
|
|
330
|
+
startedAt: output.startedAt,
|
|
331
|
+
finishedAt: output.finishedAt,
|
|
332
|
+
state: output.state,
|
|
333
|
+
requestIds: output.requestIds,
|
|
334
|
+
...(output.error ? { error: output.error } : {}),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
306
337
|
if (view.cycle && !values.some(c => c.cycleId === view.cycle.cycleId)) {
|
|
307
|
-
values.push(view.state === "stopped" || view.state === "error"
|
|
338
|
+
values.push(view.state === "stopped" || view.state === "error"
|
|
339
|
+
? { ...view.cycle, state: "interrupted", finishedAt: view.cycle.finishedAt ?? Date.now() }
|
|
340
|
+
: view.cycle);
|
|
308
341
|
}
|
|
309
342
|
if (["stopped", "error"].includes(view.state)) {
|
|
310
|
-
const
|
|
311
|
-
for (const requestId of saved?.generation ===
|
|
312
|
-
if (values.some(c => c.generation ===
|
|
343
|
+
const w = this.files.worker(id), saved = this.files.optional(id, "state.json", status);
|
|
344
|
+
for (const requestId of saved?.generation === w.generation ? saved.accepted ?? [] : []) {
|
|
345
|
+
if (values.some(c => c.generation === w.generation && c.requestIds.includes(requestId)))
|
|
313
346
|
continue;
|
|
314
347
|
values.push({
|
|
315
|
-
cycleId: requestId,
|
|
316
|
-
|
|
348
|
+
cycleId: requestId,
|
|
349
|
+
generation: w.generation,
|
|
350
|
+
startedAt: w.startedAt,
|
|
351
|
+
finishedAt: Date.now(),
|
|
352
|
+
state: "interrupted",
|
|
353
|
+
requestIds: [requestId],
|
|
354
|
+
error: "领取后执行结果不明;未重跑。",
|
|
317
355
|
});
|
|
318
356
|
}
|
|
319
|
-
if (
|
|
357
|
+
if (w.sequence > 0 && !values.some(c => c.generation === view.generation)) {
|
|
320
358
|
values.push({
|
|
321
|
-
cycleId:
|
|
322
|
-
|
|
359
|
+
cycleId: w.generation,
|
|
360
|
+
generation: w.generation,
|
|
361
|
+
startedAt: w.startedAt,
|
|
362
|
+
finishedAt: w.startedAt,
|
|
363
|
+
state: "interrupted",
|
|
364
|
+
requestIds: [],
|
|
365
|
+
error: view.error ?? "子进程在交付前退出。",
|
|
323
366
|
});
|
|
324
367
|
}
|
|
325
368
|
}
|
|
@@ -345,14 +388,15 @@ export class AgentService {
|
|
|
345
388
|
return { current, cycle, terminal };
|
|
346
389
|
}
|
|
347
390
|
async read(id, owner, cycleId, offset = 0, limit = 16000) {
|
|
348
|
-
const
|
|
391
|
+
const w = this.get(id, owner), cycles = this.cycles(id, owner);
|
|
349
392
|
const c = cycleId ? cycles.find(v => v.cycleId === cycleId) : cycles.at(-1);
|
|
350
393
|
requireThat(!cycleId || c, "周期不存在。");
|
|
351
|
-
const
|
|
394
|
+
const output = this.files.output(id);
|
|
395
|
+
const report = c && output?.cycleId === c.cycleId ? output.text : "";
|
|
352
396
|
this.check();
|
|
353
397
|
requireThat(offset <= report.length, "分页位置超出报告。");
|
|
354
398
|
return {
|
|
355
|
-
...this.view(
|
|
399
|
+
...this.view(w, owner),
|
|
356
400
|
...(c ? { cycle: c } : {}),
|
|
357
401
|
cycles: cycles.map(v => ({ cycleId: v.cycleId, state: v.state })),
|
|
358
402
|
report: report.slice(offset, offset + limit),
|
|
@@ -360,18 +404,15 @@ export class AgentService {
|
|
|
360
404
|
nextOffset: offset + limit < report.length ? offset + limit : null,
|
|
361
405
|
};
|
|
362
406
|
}
|
|
363
|
-
report(d, c) {
|
|
364
|
-
if (c.leaf === null)
|
|
365
|
-
return "";
|
|
366
|
-
return this.host.report(d, this.files.reference(d.agentId), c.leaf);
|
|
367
|
-
}
|
|
368
407
|
release(id) {
|
|
369
408
|
let pending = this.#releasing.get(id);
|
|
370
409
|
if (!pending) {
|
|
371
|
-
const generation = this.files.
|
|
410
|
+
const generation = this.files.worker(id).generation;
|
|
372
411
|
pending = Promise.resolve().then(async () => {
|
|
373
|
-
if (this.files.
|
|
412
|
+
if (this.files.worker(id).generation === generation) {
|
|
374
413
|
await this.host.release(id, generation);
|
|
414
|
+
this.files.clearTransient(id);
|
|
415
|
+
}
|
|
375
416
|
}).finally(() => this.#releasing.delete(id));
|
|
376
417
|
this.#releasing.set(id, pending);
|
|
377
418
|
}
|
|
@@ -383,11 +424,12 @@ export class AgentService {
|
|
|
383
424
|
async dispose(owner, preserve = false) {
|
|
384
425
|
if (this.#closed)
|
|
385
426
|
return;
|
|
386
|
-
const
|
|
427
|
+
const ownedRecords = !preserve && owner ? this.records(owner) : [];
|
|
428
|
+
const aliveIds = ownedRecords.filter(w => this.alive(w.agentId)).map(w => w.agentId);
|
|
387
429
|
this.#closed = true;
|
|
388
430
|
this.#preserve = preserve;
|
|
389
431
|
const errors = [];
|
|
390
|
-
for (const id of
|
|
432
|
+
for (const id of aliveIds) {
|
|
391
433
|
try {
|
|
392
434
|
this.requestStop(id);
|
|
393
435
|
}
|
|
@@ -399,13 +441,25 @@ export class AgentService {
|
|
|
399
441
|
for (const pending of this.#starting.values())
|
|
400
442
|
pending.controller.abort();
|
|
401
443
|
const deadline = Date.now() + 5000;
|
|
402
|
-
while (
|
|
444
|
+
while (aliveIds.some(id => this.alive(id)) && Date.now() < deadline)
|
|
403
445
|
await new Promise(r => setTimeout(r, 100));
|
|
404
|
-
const released = await Promise.allSettled(
|
|
446
|
+
const released = await Promise.allSettled(aliveIds.filter(id => this.host.alive(id)).map(id => this.release(id)));
|
|
405
447
|
for (const value of released)
|
|
406
448
|
if (value.status === "rejected")
|
|
407
449
|
errors.push(value.reason);
|
|
408
450
|
await Promise.allSettled([...this.#launches]);
|
|
451
|
+
if (!preserve && owner) {
|
|
452
|
+
for (const w of ownedRecords) {
|
|
453
|
+
if (!this.host.alive(w.agentId)) {
|
|
454
|
+
try {
|
|
455
|
+
this.files.removeWorker(w.agentId);
|
|
456
|
+
}
|
|
457
|
+
catch (error) {
|
|
458
|
+
errors.push(error);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
409
463
|
if (errors.length)
|
|
410
464
|
throw new AggregateError(errors, "停止或释放 worker 失败;请核对仍存活的执行资源。");
|
|
411
465
|
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import type { AcpAnswer,
|
|
1
|
+
import type { AcpAnswer, AcpInput, AcpInteraction, AcpInteractionReply, AcpSessionReference } from "../model/acp.js";
|
|
2
|
+
import type { AgentAcpConfig } from "../model/agent.js";
|
|
3
|
+
export declare function acpConfig(value: unknown): AgentAcpConfig;
|
|
2
4
|
export declare function acpReference(value: unknown): AcpSessionReference;
|
|
3
|
-
export declare function acpModelState(value: unknown): AcpModelState;
|
|
4
|
-
export declare function acpProcess(value: unknown): AcpProcess;
|
|
5
5
|
export declare function acpInput(value: unknown): AcpInput;
|
|
6
6
|
export declare function acpAnswer(value: unknown): AcpAnswer;
|
|
7
|
-
export declare function
|
|
8
|
-
export declare function
|
|
9
|
-
export declare function acpPermissionReply(value: unknown): AcpPermissionReply;
|
|
10
|
-
export declare function acpElicitation(value: unknown): AcpElicitation;
|
|
11
|
-
export declare function acpElicitationReply(value: unknown): AcpElicitationReply;
|
|
7
|
+
export declare function acpInteraction(value: unknown): AcpInteraction;
|
|
8
|
+
export declare function acpInteractionReply(value: unknown): AcpInteractionReply;
|
package/lib/storage/acp-files.js
CHANGED
|
@@ -4,27 +4,24 @@ import { isAcpBackend, isOneOf, requireThat } from "../common/validation.js";
|
|
|
4
4
|
import { fileId } from "./file-access.js";
|
|
5
5
|
import { acpAnswerStates, acpElicitationActions, acpPermissionPolicies, isAcpElicitationValue } from "../model/acp.js";
|
|
6
6
|
const text = (value) => typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
7
|
-
export function
|
|
8
|
-
requireThat(object(value) &&
|
|
9
|
-
text(value.command) && isAbsolute(value.command) &&
|
|
10
|
-
(value.
|
|
7
|
+
export function acpConfig(value) {
|
|
8
|
+
requireThat(object(value) && isAcpBackend(value.backend) &&
|
|
9
|
+
text(value.command) && isAbsolute(value.command) &&
|
|
10
|
+
Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")), "ACP 启动配置无效。");
|
|
11
11
|
if (value.env !== undefined) {
|
|
12
12
|
requireThat(object(value.env) && Object.entries(value.env).every(([k, v]) => text(k) && typeof v === "string" && !v.includes("\0")), "ACP 环境变量配置无效。");
|
|
13
13
|
}
|
|
14
14
|
requireThat(value.permission === undefined || isOneOf(value.permission, acpPermissionPolicies), "ACP 权限策略无效。");
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
export function acpReference(value) {
|
|
18
|
+
requireThat(object(value) && value.host === "acp" && (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
|
|
19
|
+
acpConfig(value);
|
|
15
20
|
const capabilities = value.capabilities;
|
|
16
|
-
if (capabilities !== undefined)
|
|
21
|
+
if (capabilities !== undefined) {
|
|
17
22
|
requireThat(object(capabilities) &&
|
|
18
23
|
["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
export function acpModelState(value) {
|
|
22
|
-
requireThat(object(value) && text(value.generation) && ["provider", "model", "resolvedModel", "thinking"].every(key => value[key] === undefined || text(value[key])), "ACP 模型选择无效。");
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
export function acpProcess(value) {
|
|
26
|
-
requireThat(object(value) && text(value.generation) && Number.isSafeInteger(value.pid) && Number(value.pid) > 1 &&
|
|
27
|
-
typeof value.startedAt === "number" && Number.isFinite(value.startedAt), "ACP 进程记录无效。");
|
|
24
|
+
}
|
|
28
25
|
return value;
|
|
29
26
|
}
|
|
30
27
|
export function acpInput(value) {
|
|
@@ -38,38 +35,35 @@ export function acpAnswer(value) {
|
|
|
38
35
|
(value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
|
|
39
36
|
return value;
|
|
40
37
|
}
|
|
41
|
-
export function
|
|
42
|
-
requireThat(object(value) && text(value.generation) && text(value.cycleId) && typeof value.text === "string", "ACP 周期报告无效。");
|
|
43
|
-
fileId(value.cycleId);
|
|
44
|
-
return value;
|
|
45
|
-
}
|
|
46
|
-
export function acpPermission(value) {
|
|
47
|
-
acpInput(value);
|
|
48
|
-
requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
|
|
49
|
-
return value;
|
|
50
|
-
}
|
|
51
|
-
export function acpPermissionReply(value) {
|
|
38
|
+
export function acpInteraction(value) {
|
|
52
39
|
acpInput(value);
|
|
53
|
-
requireThat(object(value)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
acpInput(value);
|
|
58
|
-
requireThat(object(value) && (value.mode === "form" || value.mode === "url") && typeof value.message === "string", "ACP 交互请求无效。");
|
|
59
|
-
if (value.mode === "form") {
|
|
60
|
-
requireThat(object(value.schema) && value.url === undefined, "ACP 表单交互请求缺少有效 schema。");
|
|
40
|
+
requireThat(object(value), "ACP 交互请求无效。");
|
|
41
|
+
if (value.kind === "permission") {
|
|
42
|
+
requireThat(typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
|
|
43
|
+
return value;
|
|
61
44
|
}
|
|
62
|
-
|
|
63
|
-
requireThat(
|
|
45
|
+
if (value.kind === "form") {
|
|
46
|
+
requireThat(typeof value.message === "string" && object(value.schema), "ACP 表单交互请求缺少有效 schema。");
|
|
47
|
+
return value;
|
|
64
48
|
}
|
|
49
|
+
requireThat(value.kind === "url" && typeof value.message === "string" && text(value.url), "ACP URL 交互请求无效。");
|
|
65
50
|
return value;
|
|
66
51
|
}
|
|
67
|
-
export function
|
|
52
|
+
export function acpInteractionReply(value) {
|
|
68
53
|
acpInput(value);
|
|
69
|
-
requireThat(object(value)
|
|
70
|
-
if (value.
|
|
71
|
-
requireThat(value.
|
|
72
|
-
|
|
54
|
+
requireThat(object(value), "ACP 交互答复无效。");
|
|
55
|
+
if (value.kind === "permission") {
|
|
56
|
+
requireThat(value.optionId === null || text(value.optionId), "ACP 确认答复无效。");
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
if (value.kind === "form") {
|
|
60
|
+
requireThat(isOneOf(value.action, acpElicitationActions), "ACP 表单答复 action 无效。");
|
|
61
|
+
if (value.content !== undefined) {
|
|
62
|
+
requireThat(value.action === "accept" && object(value.content) &&
|
|
63
|
+
Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
73
66
|
}
|
|
67
|
+
requireThat(value.kind === "url" && isOneOf(value.action, acpElicitationActions), "ACP URL 交互答复无效。");
|
|
74
68
|
return value;
|
|
75
69
|
}
|