personal-bot 0.3.6 → 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.
Files changed (143) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +5 -8
  4. package/README.zh-CN.md +5 -8
  5. package/bin/pbot-pi-acp.mjs +1 -1
  6. package/docs/README.md +1 -1
  7. package/docs/architecture/design.md +2 -2
  8. package/docs/architecture/extensibility.md +1 -1
  9. package/docs/architecture/model-context.md +1 -2
  10. package/docs/guides/capabilities.md +4 -8
  11. package/docs/guides/cross-harness-workers.md +4 -4
  12. package/docs/guides/history.md +1 -3
  13. package/docs/guides/workspace-configuration.md +4 -4
  14. package/docs/hosts/claude-code.md +2 -2
  15. package/docs/hosts/codex-qoder.md +1 -1
  16. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +1 -1
  17. package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +1 -1
  18. package/lib/antigravity-plugin/lib/extension/acp/member.js +11 -10
  19. package/lib/antigravity-plugin/lib/extension/acp-worker.js +348 -203
  20. package/lib/antigravity-plugin/lib/extension/cli.js +574 -549
  21. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +1 -1
  22. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +3 -4
  23. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +0 -11
  24. package/lib/antigravity-plugin/lib/extension/mcp.js +462 -326
  25. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +2 -2
  26. package/lib/antigravity-plugin/lib/model/acp.d.ts +29 -35
  27. package/lib/antigravity-plugin/lib/model/agent.d.ts +33 -17
  28. package/lib/antigravity-plugin/lib/model/agent.js +0 -1
  29. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +5 -8
  30. package/lib/antigravity-plugin/lib/storage/acp-files.js +34 -40
  31. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +19 -10
  32. package/lib/antigravity-plugin/lib/storage/agent-files.js +147 -43
  33. package/lib/antigravity-plugin/package.json +1 -1
  34. package/lib/antigravity-plugin/plugin.json +1 -1
  35. package/lib/antigravity-plugin/resources/agents/coordinator.md +3 -7
  36. package/lib/antigravity-plugin/resources/agents/general-purpose.md +3 -3
  37. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -2
  38. package/lib/antigravity-plugin/resources/prompts/workspace.md +12 -16
  39. package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +7 -6
  40. package/lib/antigravity-plugin/resources/templates/workspace-agents.md +0 -4
  41. package/lib/antigravity-plugin/skills/pbot/SKILL.md +14 -6
  42. package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +7 -6
  43. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
  44. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
  45. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
  46. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +2 -2
  47. package/lib/extension/acp/member.d.ts +1 -1
  48. package/lib/extension/acp/member.js +11 -10
  49. package/lib/extension/acp/worker.js +34 -33
  50. package/lib/extension/acp-worker.js +348 -203
  51. package/lib/extension/cli.js +574 -549
  52. package/lib/extension/hosts/dsh/acp-member.js +1 -1
  53. package/lib/extension/hosts/dsh/command.js +1 -1
  54. package/lib/extension/hosts/dsh/workspace.js +1 -3
  55. package/lib/extension/hosts/pi/acp-member.js +3 -4
  56. package/lib/extension/hosts/plugin-runtime.js +0 -11
  57. package/lib/extension/mcp/tool-contract.d.ts +8 -50
  58. package/lib/extension/mcp/tool-contract.js +48 -62
  59. package/lib/extension/mcp/workers.d.ts +13 -27
  60. package/lib/extension/mcp/workers.js +60 -25
  61. package/lib/extension/mcp.js +462 -326
  62. package/lib/integrations/acp/agents.d.ts +2 -3
  63. package/lib/integrations/acp/agents.js +22 -22
  64. package/lib/integrations/acp/harnesses/claude.js +1 -1
  65. package/lib/integrations/agent-resources.js +2 -2
  66. package/lib/model/acp.d.ts +29 -35
  67. package/lib/model/agent.d.ts +33 -17
  68. package/lib/model/agent.js +0 -1
  69. package/lib/qoder-plugin/.mcp.json +2 -2
  70. package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
  71. package/lib/qoder-plugin/hooks/hooks.json +3 -3
  72. package/lib/qoder-plugin/skills/pbot/SKILL.md +2 -2
  73. package/lib/services/agents/acp-run.d.ts +6 -4
  74. package/lib/services/agents/acp-run.js +62 -43
  75. package/lib/services/agents/agent-run.d.ts +10 -6
  76. package/lib/services/agents/agent-run.js +142 -65
  77. package/lib/services/agents/agent-service.d.ts +3 -21
  78. package/lib/services/agents/agent-service.js +180 -126
  79. package/lib/storage/acp-files.d.ts +5 -8
  80. package/lib/storage/acp-files.js +34 -40
  81. package/lib/storage/agent-files.d.ts +19 -10
  82. package/lib/storage/agent-files.js +147 -43
  83. package/lib/tools/history/contract.js +12 -12
  84. package/package.json +1 -1
  85. package/resources/agents/coordinator.md +3 -7
  86. package/resources/agents/general-purpose.md +3 -3
  87. package/resources/antigravity/skills/pbot/SKILL.md +14 -6
  88. package/resources/claude/skills/pbot/SKILL.md +14 -6
  89. package/resources/plugin/skills/pbot/SKILL.md +2 -2
  90. package/resources/prompts/history-coordinator.md +1 -2
  91. package/resources/prompts/plugin-coordinator.md +1 -2
  92. package/resources/prompts/workspace.md +12 -16
  93. package/resources/skills/provider-connections/SKILL.md +7 -6
  94. package/resources/templates/workspace-agents.md +0 -4
  95. package/src/extension/acp/member.ts +14 -11
  96. package/src/extension/acp/worker.ts +31 -28
  97. package/src/extension/cli/commands/upgrade.ts +8 -36
  98. package/src/extension/cli/commands/workspace.ts +3 -8
  99. package/src/extension/cli/index.ts +1 -2
  100. package/src/extension/hosts/dsh/acp-member.ts +1 -1
  101. package/src/extension/hosts/dsh/command.ts +1 -1
  102. package/src/extension/hosts/dsh/workspace.ts +1 -3
  103. package/src/extension/hosts/pi/acp-member.ts +3 -4
  104. package/src/extension/hosts/pi/command.ts +1 -1
  105. package/src/extension/hosts/pi/lifecycle.ts +0 -2
  106. package/src/extension/hosts/plugin-runtime.ts +0 -10
  107. package/src/extension/mcp/tool-contract.ts +48 -62
  108. package/src/extension/mcp/workers.ts +66 -27
  109. package/src/integrations/acp/agents.ts +28 -27
  110. package/src/integrations/acp/harnesses/claude.ts +1 -1
  111. package/src/integrations/agent-resources.ts +2 -2
  112. package/src/integrations/hosts/dsh/session.ts +2 -2
  113. package/src/integrations/installation/cli-update.ts +2 -2
  114. package/src/integrations/installation/installers/claude.ts +4 -4
  115. package/src/integrations/installation/installers/qoder.ts +5 -5
  116. package/src/integrations/installation/upgraders/antigravity.ts +57 -15
  117. package/src/integrations/installation/upgraders/claude.ts +1 -4
  118. package/src/integrations/installation/upgraders/codex.ts +85 -15
  119. package/src/integrations/installation/upgraders/dsh.ts +46 -24
  120. package/src/integrations/installation/upgraders/pi.ts +18 -11
  121. package/src/integrations/installation/upgraders/qoder.ts +95 -20
  122. package/src/model/acp.ts +27 -29
  123. package/src/model/agent.ts +80 -16
  124. package/src/model/installation.ts +0 -2
  125. package/src/pbot.ts +2 -2
  126. package/src/services/agents/acp-run.ts +81 -49
  127. package/src/services/agents/agent-run.ts +154 -56
  128. package/src/services/agents/agent-service.ts +204 -133
  129. package/src/services/installation/upgrade-service.ts +1 -5
  130. package/src/storage/acp-files.ts +50 -45
  131. package/src/storage/agent-files.ts +159 -46
  132. package/src/tools/history/contract.ts +12 -12
  133. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +0 -15
  134. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +0 -126
  135. package/lib/antigravity-plugin/resources/references/compute-finance.md +0 -22
  136. package/lib/antigravity-plugin/resources/references/compute.md +0 -16
  137. package/lib/antigravity-plugin/resources/references/task-conductor.md +0 -25
  138. package/lib/services/workspace/plugin-releases.d.ts +0 -15
  139. package/lib/services/workspace/plugin-releases.js +0 -126
  140. package/resources/references/compute-finance.md +0 -22
  141. package/resources/references/compute.md +0 -16
  142. package/resources/references/task-conductor.md +0 -25
  143. 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, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
8
- import { acpElicitationReply, acpModelState, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
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(d, owner) {
28
- return d.workspace === this.root && d.parentSessionId === owner.sessionId && (d.parentLeaf === null || owner.branch.includes(d.parentLeaf));
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 d = this.files.read(id, "descriptor.json", descriptor);
33
- requireThat(this.owned(d, owner), "worker 不属于当前会话。");
34
- requireThat(releaseLine(d.release) === releaseLine(packageVersion), "worker 版本不兼容;请停止后使用匹配版本。");
35
- return d;
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(d => d.workspace === this.root && d.parentSessionId === owner.sessionId &&
40
- releaseLine(d.release) === releaseLine(packageVersion));
39
+ return this.files.list().filter(w => w.workspace === this.root && w.parentSessionId === owner.sessionId &&
40
+ releaseLine(w.release) === releaseLine(packageVersion));
41
41
  }
42
- list(owner) { return this.records(owner).map(d => this.view(d, owner)); }
43
- view(d, owner) {
44
- const run = this.files.read(d.agentId, "launch.json", launch);
45
- const saved = this.files.optional(d.agentId, "status.json", status);
46
- const current = saved?.generation === run.generation ? saved : undefined;
47
- const alive = this.alive(d.agentId), pending = this.#starting.get(d.agentId);
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 reference = this.files.read(d.agentId, "acp-session.json", acpReference);
50
- const model = this.files.optional(d.agentId, "acp-model.json", acpModelState);
51
- const selection = model?.generation === run.generation ? {
52
- ...(model.provider ? { provider: model.provider } : {}),
53
- ...(model.model ? { model: model.model } : {}),
54
- ...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
55
- ...(model.thinking ? { thinking: model.thinking } : {}),
56
- } : undefined;
57
- const pendingRequests = alive ? [
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
- ...d,
67
- name: run.name,
68
- generation: run.generation,
69
- owned: this.owned(d, owner),
64
+ ...toDescriptor(w),
65
+ generation: w.generation,
66
+ owned: this.owned(w, owner),
70
67
  alive,
71
- backend: reference.backend,
68
+ backend: w.acp.backend,
72
69
  transport: "acp",
73
70
  capabilities: {
74
71
  steer: false,
75
- resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load),
72
+ resume: Boolean(capabilities?.resume || capabilities?.load),
76
73
  transcript: false,
77
74
  },
78
75
  ...(selection ? { selection } : {}),
79
- state: alive ? starting ? "starting" : run.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
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 d = this.get(id, owner), run = this.files.read(id, "launch.json", launch);
89
- const pending = this.view(d, owner).pendingRequests ?? [];
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.ensureSubdirectory(id, "permissions");
129
- this.files.write(id, "permissions/" + target.requestId + "-reply.json", { generation: run.generation, requestId: target.requestId, optionId: resolved }, acpPermissionReply, true);
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.ensureSubdirectory(id, "elicitations");
141
- this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
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
- }, acpElicitationReply, true);
147
- return this.view(d, owner);
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.ensureSubdirectory(id, "elicitations");
152
- this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
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
- }, acpElicitationReply, true);
157
- return this.view(d, owner);
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 cwd = realpathSync(resolve(this.root, options.cwd ?? "."));
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, release: packageVersion, workspace: this.root, cwd,
173
- parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd },
168
+ agentId,
169
+ release: packageVersion,
170
+ workspace: this.root,
171
+ parentSessionId: owner.sessionId,
172
+ options,
174
173
  };
175
- const run = { generation: randomUUID(), name: options.name ?? agentId.slice(0, 8), sequence: 0, stop: false, startedAt: Date.now() };
174
+ const run = { generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
176
175
  this.files.create(d, run, reference);
177
- const initial = options.message ?? "";
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 { ...this.view(prepared.d, owner), ...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" } : {}) };
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 run = this.files.read(d.agentId, "launch.json", launch);
194
- requireThat(run.generation === generation && !run.stop, "worker 启动已取消或代次变化。");
195
+ const w = this.files.worker(d.agentId);
196
+ requireThat(w.generation === generation && !w.stop, "worker 启动已取消或代次变化。");
195
197
  }
196
198
  catch (error) {
197
- const run = this.files.read(d.agentId, "launch.json", launch);
198
- if (run.generation === generation) {
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.write(d.agentId, "status.json", {
202
- generation, state: run.stop ? "stopped" : "error",
203
- activity: run.stop ? "已停止" : "启动失败", updatedAt: Date.now(), ...(run.stop ? {} : { error: String(error) }),
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 d = this.get(id, owner);
230
+ const w = this.get(id, owner);
223
231
  return this.files.control(() => {
224
- requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
225
- const view = this.view(d, owner);
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(d, owner), requestId: request.requestId, messageStatus: "queued" };
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
- const d = this.get(id, owner);
242
+ this.get(id, owner);
240
243
  this.requestStop(id);
241
- return this.view(d, owner);
244
+ return this.view(this.files.worker(id), owner);
242
245
  }
243
246
  requestStop(id) {
244
- this.files.control(() => { const run = this.files.read(id, "launch.json", launch); run.stop = true; this.files.write(id, "launch.json", run, launch); });
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 d = this.get(id, owner), reference = this.files.reference(id);
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.read(id, "launch.json", launch), interrupted = this.cycles(id, owner);
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.read(id, "launch.json", launch).generation === reserved.previous.generation, "worker 已由其他操作恢复。");
275
- for (const ended of reserved.interrupted) {
276
- if (ended.generation === reserved.previous.generation && !this.files.completions(id).some(c => c.cycleId === ended.cycleId)) {
277
- this.files.write(id, "completions/" + ended.cycleId + ".json", ended, cycle, true);
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
- const run = { ...reserved.previous, generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
281
- this.files.write(id, "launch.json", run, launch);
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 = run.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 { ...this.view(d, owner), ...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" } : {}) };
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 = this.files.completions(id);
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" ? { ...view.cycle, state: "interrupted", finishedAt: view.cycle.finishedAt ?? Date.now() } : view.cycle);
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 run = this.files.read(id, "launch.json", launch), saved = this.files.optional(id, "status.json", status);
311
- for (const requestId of saved?.generation === run.generation ? saved.accepted ?? [] : []) {
312
- if (values.some(c => c.generation === run.generation && c.requestIds.includes(requestId)))
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, generation: run.generation, startedAt: run.startedAt, finishedAt: Date.now(),
316
- state: "interrupted", leaf: null, requestIds: [requestId], source: "parent", error: "领取后执行结果不明;未重跑。",
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 (run.sequence > 0 && !values.some(c => c.generation === view.generation)) {
357
+ if (w.sequence > 0 && !values.some(c => c.generation === view.generation)) {
320
358
  values.push({
321
- cycleId: run.generation, generation: run.generation, startedAt: run.startedAt, finishedAt: run.startedAt,
322
- state: "interrupted", leaf: null, requestIds: [], source: "parent", error: view.error ?? "子进程在交付前退出。",
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 d = this.get(id, owner), cycles = this.cycles(id, owner);
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 report = c ? await this.report(d, c) : "";
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(d, owner),
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.read(id, "launch.json", launch).generation;
410
+ const generation = this.files.worker(id).generation;
372
411
  pending = Promise.resolve().then(async () => {
373
- if (this.files.read(id, "launch.json", launch).generation === generation)
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 ids = !preserve && owner ? this.list(owner).filter(v => v.alive).map(v => v.agentId) : [];
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 ids) {
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 (ids.some(id => this.alive(id)) && Date.now() < deadline)
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(ids.filter(id => this.host.alive(id)).map(id => this.release(id)));
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, AcpElicitation, AcpElicitationReply, AcpInput, AcpPermission, AcpPermissionReply, AcpModelState, AcpProcess, AcpReport, AcpSessionReference } from "../model/acp.js";
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 acpReport(value: unknown): AcpReport;
8
- export declare function acpPermission(value: unknown): AcpPermission;
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;
@@ -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 acpReference(value) {
8
- requireThat(object(value) && value.host === "acp" && isAcpBackend(value.backend) &&
9
- text(value.command) && isAbsolute(value.command) && Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")) &&
10
- (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
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
- return value;
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 acpReport(value) {
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) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
54
- return value;
55
- }
56
- export function acpElicitation(value) {
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
- else {
63
- requireThat(text(value.url) && value.schema === undefined, "ACP URL 交互请求缺少有效 url。");
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 acpElicitationReply(value) {
52
+ export function acpInteractionReply(value) {
68
53
  acpInput(value);
69
- requireThat(object(value) && isOneOf(value.action, acpElicitationActions), "ACP 交互答复无效。");
70
- if (value.content !== undefined) {
71
- requireThat(value.action === "accept" && object(value.content) &&
72
- Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
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
  }