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.
Files changed (111) 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/architecture/design.md +2 -2
  7. package/docs/architecture/extensibility.md +1 -1
  8. package/docs/guides/cross-harness-workers.md +4 -4
  9. package/docs/guides/history.md +1 -3
  10. package/docs/guides/workspace-configuration.md +4 -4
  11. package/docs/hosts/claude-code.md +2 -2
  12. package/docs/hosts/codex-qoder.md +1 -1
  13. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +1 -1
  14. package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +1 -1
  15. package/lib/antigravity-plugin/lib/extension/acp/member.js +11 -10
  16. package/lib/antigravity-plugin/lib/extension/acp-worker.js +346 -201
  17. package/lib/antigravity-plugin/lib/extension/cli.js +563 -538
  18. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +1 -1
  19. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +3 -4
  20. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +0 -11
  21. package/lib/antigravity-plugin/lib/extension/mcp.js +397 -278
  22. package/lib/antigravity-plugin/lib/model/acp.d.ts +29 -35
  23. package/lib/antigravity-plugin/lib/model/agent.d.ts +33 -17
  24. package/lib/antigravity-plugin/lib/model/agent.js +0 -1
  25. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +5 -8
  26. package/lib/antigravity-plugin/lib/storage/acp-files.js +34 -40
  27. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +19 -10
  28. package/lib/antigravity-plugin/lib/storage/agent-files.js +147 -43
  29. package/lib/antigravity-plugin/package.json +1 -1
  30. package/lib/antigravity-plugin/plugin.json +1 -1
  31. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
  32. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
  33. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
  34. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +1 -1
  35. package/lib/extension/acp/member.d.ts +1 -1
  36. package/lib/extension/acp/member.js +11 -10
  37. package/lib/extension/acp/worker.js +34 -33
  38. package/lib/extension/acp-worker.js +346 -201
  39. package/lib/extension/cli.js +563 -538
  40. package/lib/extension/hosts/dsh/acp-member.js +1 -1
  41. package/lib/extension/hosts/dsh/command.js +1 -1
  42. package/lib/extension/hosts/dsh/workspace.js +1 -3
  43. package/lib/extension/hosts/pi/acp-member.js +3 -4
  44. package/lib/extension/hosts/plugin-runtime.js +0 -11
  45. package/lib/extension/mcp/tool-contract.d.ts +8 -50
  46. package/lib/extension/mcp/tool-contract.js +0 -8
  47. package/lib/extension/mcp/workers.d.ts +7 -27
  48. package/lib/extension/mcp/workers.js +35 -17
  49. package/lib/extension/mcp.js +397 -278
  50. package/lib/integrations/acp/agents.d.ts +2 -3
  51. package/lib/integrations/acp/agents.js +22 -22
  52. package/lib/integrations/acp/harnesses/claude.js +1 -1
  53. package/lib/model/acp.d.ts +29 -35
  54. package/lib/model/agent.d.ts +33 -17
  55. package/lib/model/agent.js +0 -1
  56. package/lib/qoder-plugin/.mcp.json +2 -2
  57. package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
  58. package/lib/qoder-plugin/hooks/hooks.json +3 -3
  59. package/lib/qoder-plugin/skills/pbot/SKILL.md +1 -1
  60. package/lib/services/agents/acp-run.d.ts +6 -4
  61. package/lib/services/agents/acp-run.js +62 -43
  62. package/lib/services/agents/agent-run.d.ts +10 -6
  63. package/lib/services/agents/agent-run.js +142 -65
  64. package/lib/services/agents/agent-service.d.ts +3 -21
  65. package/lib/services/agents/agent-service.js +180 -126
  66. package/lib/storage/acp-files.d.ts +5 -8
  67. package/lib/storage/acp-files.js +34 -40
  68. package/lib/storage/agent-files.d.ts +19 -10
  69. package/lib/storage/agent-files.js +147 -43
  70. package/package.json +1 -1
  71. package/src/extension/acp/member.ts +14 -11
  72. package/src/extension/acp/worker.ts +31 -28
  73. package/src/extension/cli/commands/upgrade.ts +8 -36
  74. package/src/extension/cli/commands/workspace.ts +3 -8
  75. package/src/extension/cli/index.ts +1 -2
  76. package/src/extension/hosts/dsh/acp-member.ts +1 -1
  77. package/src/extension/hosts/dsh/command.ts +1 -1
  78. package/src/extension/hosts/dsh/workspace.ts +1 -3
  79. package/src/extension/hosts/pi/acp-member.ts +3 -4
  80. package/src/extension/hosts/pi/command.ts +1 -1
  81. package/src/extension/hosts/pi/lifecycle.ts +0 -2
  82. package/src/extension/hosts/plugin-runtime.ts +0 -10
  83. package/src/extension/mcp/tool-contract.ts +0 -8
  84. package/src/extension/mcp/workers.ts +34 -17
  85. package/src/integrations/acp/agents.ts +28 -27
  86. package/src/integrations/acp/harnesses/claude.ts +1 -1
  87. package/src/integrations/hosts/dsh/session.ts +2 -2
  88. package/src/integrations/installation/cli-update.ts +2 -2
  89. package/src/integrations/installation/installers/claude.ts +4 -4
  90. package/src/integrations/installation/installers/qoder.ts +5 -5
  91. package/src/integrations/installation/upgraders/antigravity.ts +57 -15
  92. package/src/integrations/installation/upgraders/claude.ts +1 -4
  93. package/src/integrations/installation/upgraders/codex.ts +85 -15
  94. package/src/integrations/installation/upgraders/dsh.ts +46 -24
  95. package/src/integrations/installation/upgraders/pi.ts +18 -11
  96. package/src/integrations/installation/upgraders/qoder.ts +95 -20
  97. package/src/model/acp.ts +27 -29
  98. package/src/model/agent.ts +80 -16
  99. package/src/model/installation.ts +0 -2
  100. package/src/pbot.ts +1 -1
  101. package/src/services/agents/acp-run.ts +81 -49
  102. package/src/services/agents/agent-run.ts +154 -56
  103. package/src/services/agents/agent-service.ts +204 -133
  104. package/src/services/installation/upgrade-service.ts +1 -5
  105. package/src/storage/acp-files.ts +50 -45
  106. package/src/storage/agent-files.ts +159 -46
  107. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +0 -15
  108. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +0 -126
  109. package/lib/services/workspace/plugin-releases.d.ts +0 -15
  110. package/lib/services/workspace/plugin-releases.js +0 -126
  111. package/src/services/workspace/plugin-releases.ts +0 -121
@@ -3,72 +3,77 @@ import { object } from "../common/json.js";
3
3
  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
- import type { AcpAnswer, AcpElicitation, AcpElicitationReply, AcpInput, AcpPermission, AcpPermissionReply, AcpModelState, AcpProcess, AcpReport, AcpSessionReference } from "../model/acp.js";
6
+ import type { AcpAnswer, AcpInput, AcpInteraction, AcpInteractionReply, AcpSessionReference } from "../model/acp.js";
7
+ import type { AgentAcpConfig } from "../model/agent.js";
7
8
 
8
9
  const text = (value: unknown): value is string => typeof value === "string" && value.length > 0 && !value.includes("\0");
9
- export function acpReference(value: unknown): AcpSessionReference {
10
- requireThat(object(value) && value.host === "acp" && isAcpBackend(value.backend) &&
11
- text(value.command) && isAbsolute(value.command) && Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")) &&
12
- (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
10
+
11
+ export function acpConfig(value: unknown): AgentAcpConfig {
12
+ requireThat(object(value) && isAcpBackend(value.backend) &&
13
+ text(value.command) && isAbsolute(value.command) &&
14
+ Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")), "ACP 启动配置无效。");
13
15
  if (value.env !== undefined) {
14
16
  requireThat(object(value.env) && Object.entries(value.env).every(([k, v]) => text(k) && typeof v === "string" && !v.includes("\0")), "ACP 环境变量配置无效。");
15
17
  }
16
18
  requireThat(value.permission === undefined || isOneOf(value.permission, acpPermissionPolicies), "ACP 权限策略无效。");
19
+ return value as unknown as AgentAcpConfig;
20
+ }
21
+
22
+ export function acpReference(value: unknown): AcpSessionReference {
23
+ requireThat(object(value) && value.host === "acp" && (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
24
+ acpConfig(value);
17
25
  const capabilities = value.capabilities;
18
- if (capabilities !== undefined) requireThat(object(capabilities) &&
19
- ["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
26
+ if (capabilities !== undefined) {
27
+ requireThat(object(capabilities) &&
28
+ ["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
29
+ }
20
30
  return value as unknown as AcpSessionReference;
21
31
  }
22
- export function acpModelState(value: unknown): AcpModelState {
23
- requireThat(object(value) && text(value.generation) && ["provider", "model", "resolvedModel", "thinking"].every(key => value[key] === undefined || text(value[key])), "ACP 模型选择无效。");
24
- return value as unknown as AcpModelState;
25
- }
26
- export function acpProcess(value: unknown): AcpProcess {
27
- requireThat(object(value) && text(value.generation) && Number.isSafeInteger(value.pid) && Number(value.pid) > 1 &&
28
- typeof value.startedAt === "number" && Number.isFinite(value.startedAt), "ACP 进程记录无效。");
29
- return value as unknown as AcpProcess;
30
- }
32
+
31
33
  export function acpInput(value: unknown): AcpInput {
32
34
  requireThat(object(value) && text(value.generation) && text(value.requestId), "ACP 输入关联无效。");
33
- fileId(value.requestId); return value as unknown as AcpInput;
35
+ fileId(value.requestId);
36
+ return value as unknown as AcpInput;
34
37
  }
38
+
35
39
  export function acpAnswer(value: unknown): AcpAnswer {
36
40
  acpInput(value);
37
41
  requireThat(object(value) && typeof value.answer === "string" && isOneOf(value.state, acpAnswerStates) &&
38
42
  (value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
39
43
  return value as unknown as AcpAnswer;
40
44
  }
41
- export function acpReport(value: unknown): AcpReport {
42
- requireThat(object(value) && text(value.generation) && text(value.cycleId) && typeof value.text === "string", "ACP 周期报告无效。");
43
- fileId(value.cycleId); return value as unknown as AcpReport;
44
- }
45
- export function acpPermission(value: unknown): AcpPermission {
46
- acpInput(value);
47
- requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option =>
48
- object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
49
- return value as unknown as AcpPermission;
50
- }
51
- export function acpPermissionReply(value: unknown): AcpPermissionReply {
52
- acpInput(value);
53
- requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
54
- return value as unknown as AcpPermissionReply;
55
- }
56
- export function acpElicitation(value: unknown): AcpElicitation {
45
+
46
+ export function acpInteraction(value: unknown): AcpInteraction {
57
47
  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。");
61
- } else {
62
- requireThat(text(value.url) && value.schema === undefined, "ACP URL 交互请求缺少有效 url。");
48
+ requireThat(object(value), "ACP 交互请求无效。");
49
+ if (value.kind === "permission") {
50
+ requireThat(typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option =>
51
+ object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
52
+ return value as unknown as AcpInteraction;
53
+ }
54
+ if (value.kind === "form") {
55
+ requireThat(typeof value.message === "string" && object(value.schema), "ACP 表单交互请求缺少有效 schema。");
56
+ return value as unknown as AcpInteraction;
63
57
  }
64
- return value as unknown as AcpElicitation;
58
+ requireThat(value.kind === "url" && typeof value.message === "string" && text(value.url), "ACP URL 交互请求无效。");
59
+ return value as unknown as AcpInteraction;
65
60
  }
66
- export function acpElicitationReply(value: unknown): AcpElicitationReply {
61
+
62
+ export function acpInteractionReply(value: unknown): AcpInteractionReply {
67
63
  acpInput(value);
68
- requireThat(object(value) && isOneOf(value.action, acpElicitationActions), "ACP 交互答复无效。");
69
- if (value.content !== undefined) {
70
- requireThat(value.action === "accept" && object(value.content) &&
71
- Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
64
+ requireThat(object(value), "ACP 交互答复无效。");
65
+ if (value.kind === "permission") {
66
+ requireThat(value.optionId === null || text(value.optionId), "ACP 确认答复无效。");
67
+ return value as unknown as AcpInteractionReply;
68
+ }
69
+ if (value.kind === "form") {
70
+ requireThat(isOneOf(value.action, acpElicitationActions), "ACP 表单答复 action 无效。");
71
+ if (value.content !== undefined) {
72
+ requireThat(value.action === "accept" && object(value.content) &&
73
+ Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
74
+ }
75
+ return value as unknown as AcpInteractionReply;
72
76
  }
73
- return value as unknown as AcpElicitationReply;
77
+ requireThat(value.kind === "url" && isOneOf(value.action, acpElicitationActions), "ACP URL 交互答复无效。");
78
+ return value as unknown as AcpInteractionReply;
74
79
  }
@@ -1,43 +1,103 @@
1
- import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
1
+ import { existsSync, mkdirSync, readdirSync, rmdirSync, rmSync, unlinkSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { canonical, object } from "../common/json.js";
5
5
  import { isOneOf, requireThat } from "../common/validation.js";
6
6
  import { readJsonFile, writeJsonFile } from "./atomic-json.js";
7
7
  import { fileId, privateDirectory } from "./file-access.js";
8
- import { agentStates, cycleSources, cycleStates, messageDeliveries } from "../model/agent.js";
9
- import type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionReference } from "../model/agent.js";
8
+ import { agentStates, cycleStates, messageDeliveries } from "../model/agent.js";
9
+ import type {
10
+ AgentCycle,
11
+ AgentDescriptor,
12
+ AgentMessage,
13
+ AgentOutput,
14
+ AgentSessionReference,
15
+ AgentStatus,
16
+ AgentWorkerRecord,
17
+ } from "../model/agent.js";
10
18
  import type { AcpSessionReference } from "../model/acp.js";
11
- import { acpElicitation, acpPermission, acpReference } from "./acp-files.js";
19
+ import { acpConfig, acpInteraction, acpReference } from "./acp-files.js";
12
20
 
13
21
  const text = (v: unknown): v is string => typeof v === "string" && v.length > 0;
14
22
  const number = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
15
- export function descriptor(v: unknown): AgentDescriptor {
16
- requireThat(object(v) && text(v.agentId) && text(v.release) && text(v.workspace) && text(v.cwd) && text(v.parentSessionId) &&
17
- (v.parentLeaf === null || text(v.parentLeaf)) && number(v.createdAt) && object(v.options), "子会话身份无效。");
18
- fileId(v.agentId as string); return v as unknown as AgentDescriptor;
19
- }
20
- export function launch(v: unknown): AgentLaunch {
21
- requireThat(object(v) && text(v.generation) && text(v.name) && Number.isSafeInteger(v.sequence) && typeof v.stop === "boolean" && number(v.startedAt), "子会话启动状态无效。");
22
- return v as unknown as AgentLaunch;
23
+
24
+ export function workerRecord(v: unknown): AgentWorkerRecord {
25
+ requireThat(object(v) && text(v.agentId) && text(v.release) && text(v.workspace) && text(v.parentSessionId) &&
26
+ text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.stop === "boolean" && number(v.startedAt) &&
27
+ (v.pid === undefined || (Number.isSafeInteger(v.pid) && Number(v.pid) > 1)) &&
28
+ (v.options === undefined || object(v.options)), "worker 控制记录无效。");
29
+ fileId(v.agentId as string);
30
+ acpConfig(v.acp);
31
+ return v as unknown as AgentWorkerRecord;
23
32
  }
33
+
24
34
  export function message(v: unknown): AgentMessage {
25
35
  requireThat(object(v) && text(v.requestId) && text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.message === "string" &&
26
36
  isOneOf(v.delivery, messageDeliveries) && Array.isArray(v.files) && v.files.every(text), "子会话消息无效。");
27
37
  return v as unknown as AgentMessage;
28
38
  }
39
+
29
40
  export function cycle(v: unknown): AgentCycle {
30
41
  requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) &&
31
- isOneOf(v.state, cycleStates) &&
32
- (v.leaf === null || text(v.leaf)) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
33
- isOneOf(v.source, cycleSources) && (v.finishedAt === undefined || number(v.finishedAt)), "子会话周期无效。");
42
+ isOneOf(v.state, cycleStates) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
43
+ (v.finishedAt === undefined || number(v.finishedAt)) &&
44
+ (v.error === undefined || typeof v.error === "string"), "子会话周期无效。");
34
45
  return v as unknown as AgentCycle;
35
46
  }
47
+
36
48
  export function status(v: unknown): AgentStatus {
37
49
  requireThat(object(v) && text(v.generation) && isOneOf(v.state, agentStates) &&
38
50
  typeof v.activity === "string" && number(v.updatedAt), "子会话状态无效。");
39
- if (v.cycle) cycle(v.cycle); return v as unknown as AgentStatus;
51
+ if (v.sessionId !== undefined) requireThat(v.sessionId === null || text(v.sessionId), "ACP 会话 ID 无效。");
52
+ if (v.capabilities !== undefined) {
53
+ requireThat(object(v.capabilities) &&
54
+ ["resume", "load", "close"].every(key => typeof (v.capabilities as Record<string, unknown>)[key] === "boolean"), "ACP 会话能力无效。");
55
+ }
56
+ if (v.selection !== undefined) {
57
+ requireThat(object(v.selection) &&
58
+ ["provider", "model", "resolvedModel", "thinking"].every(key => (v.selection as Record<string, unknown>)[key] === undefined || text((v.selection as Record<string, unknown>)[key])), "ACP 模型选择无效。");
59
+ }
60
+ if (v.activeRequestId !== undefined) {
61
+ requireThat(text(v.activeRequestId), "ACP 输入关联无效。");
62
+ fileId(v.activeRequestId);
63
+ }
64
+ if (v.accepted !== undefined) requireThat(Array.isArray(v.accepted) && v.accepted.every(text), "已领取请求列表无效。");
65
+ if (v.cycle !== undefined) cycle(v.cycle);
66
+ if (v.error !== undefined) requireThat(typeof v.error === "string", "子会话错误信息无效。");
67
+ return v as unknown as AgentStatus;
40
68
  }
69
+
70
+ export function outputRecord(v: unknown): AgentOutput {
71
+ requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) && number(v.finishedAt) &&
72
+ isOneOf(v.state, ["completed", "failed", "cancelled", "interrupted"] as const) &&
73
+ Array.isArray(v.requestIds) && v.requestIds.every(text) &&
74
+ typeof v.text === "string" && (v.error === undefined || typeof v.error === "string"), "子会话输出记录无效。");
75
+ fileId(v.cycleId as string);
76
+ return v as unknown as AgentOutput;
77
+ }
78
+
79
+ export function toDescriptor(record: AgentWorkerRecord): AgentDescriptor {
80
+ return {
81
+ agentId: record.agentId,
82
+ release: record.release,
83
+ workspace: record.workspace,
84
+ parentSessionId: record.parentSessionId,
85
+ options: record.options ?? {},
86
+ };
87
+ }
88
+
89
+ function tryUnlink(path: string) {
90
+ try { unlinkSync(path); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
91
+ }
92
+
93
+ function tryRmdir(path: string) {
94
+ try { rmdirSync(path); }
95
+ catch (error) {
96
+ const code = (error as NodeJS.ErrnoException).code;
97
+ if (code !== "ENOENT" && code !== "ENOTEMPTY" && code !== "EEXIST") throw error;
98
+ }
99
+ }
100
+
41
101
  export class AgentFiles {
42
102
  #failure: unknown;
43
103
  constructor(readonly root: string) {}
@@ -55,51 +115,101 @@ export class AgentFiles {
55
115
  catch (error) { this.fail(error); throw error; }
56
116
  requireThat(published, "子会话文件已存在。");
57
117
  }
58
- create(value: AgentDescriptor, run: AgentLaunch, acp: AcpSessionReference) {
118
+ remove(id: string, path: string) {
119
+ tryUnlink(join(this.directory(id), path));
120
+ }
121
+ create(value: AgentDescriptor, run: { generation: string; sequence: number; stop: boolean; startedAt: number }, acp: AcpSessionReference) {
59
122
  this.check();
60
123
  acpReference(acp);
61
124
  mkdirSync(this.root, { recursive: true, mode: 0o700 });
62
125
  mkdirSync(this.directory(value.agentId), { mode: 0o700 });
63
- for (const dir of ["inbox", "completions", "reports", "permissions", "elicitations"]) privateDirectory(this.directory(value.agentId), dir);
64
- this.write(value.agentId, "descriptor.json", value, descriptor, true);
65
- this.write(value.agentId, "launch.json", run, launch, true);
66
- this.write(value.agentId, "acp-session.json", acp, acpReference, true);
126
+ const hasOptions = Object.keys(value.options).length > 0;
127
+ const record: AgentWorkerRecord = {
128
+ agentId: value.agentId,
129
+ release: value.release,
130
+ workspace: value.workspace,
131
+ parentSessionId: value.parentSessionId,
132
+ generation: run.generation,
133
+ sequence: run.sequence,
134
+ stop: run.stop,
135
+ startedAt: run.startedAt,
136
+ acp: {
137
+ backend: acp.backend,
138
+ command: acp.command,
139
+ args: acp.args,
140
+ ...(acp.env ? { env: acp.env } : {}),
141
+ ...(acp.permission ? { permission: acp.permission } : {}),
142
+ },
143
+ ...(hasOptions ? { options: value.options } : {}),
144
+ };
145
+ this.write(value.agentId, "worker.json", record, workerRecord, true);
67
146
  }
68
- list() {
147
+ worker(id: string): AgentWorkerRecord {
148
+ return this.read(id, "worker.json", workerRecord);
149
+ }
150
+ list(): AgentWorkerRecord[] {
69
151
  if (!existsSync(this.root)) return [];
70
152
  return readdirSync(this.root, { withFileTypes: true }).filter(e => e.isDirectory() && !e.name.startsWith(".")).flatMap(e => {
71
- const d = this.optional(e.name, "descriptor.json", descriptor); return d ? [d] : [];
153
+ const record = this.optional(e.name, "worker.json", workerRecord);
154
+ return record ? [record] : [];
72
155
  });
73
156
  }
74
- messages(id: string) {
75
- return readdirSync(join(this.directory(id), "inbox")).filter(n => n.endsWith(".json"))
157
+ messages(id: string): AgentMessage[] {
158
+ const dir = join(this.directory(id), "inbox");
159
+ if (!existsSync(dir)) return [];
160
+ return readdirSync(dir).filter(n => n.endsWith(".json"))
76
161
  .map(n => this.read(id, "inbox/" + n, message)).sort((a, b) => a.sequence - b.sequence);
77
162
  }
78
- completions(id: string) {
79
- return readdirSync(join(this.directory(id), "completions")).filter(n => n.endsWith(".json"))
80
- .map(n => this.read(id, "completions/" + n, cycle)).sort((a, b) => (a.finishedAt ?? 0) - (b.finishedAt ?? 0));
163
+ removeMessage(id: string, requestId: string) {
164
+ fileId(requestId);
165
+ const dir = join(this.directory(id), "inbox");
166
+ tryUnlink(join(dir, requestId + ".json"));
167
+ tryRmdir(dir);
168
+ }
169
+ output(id: string): AgentOutput | undefined {
170
+ return this.optional(id, "output.json", outputRecord);
81
171
  }
82
- /** Permission requests of a generation that have no reply yet. */
83
- pendingPermissions(id: string, generation: string) {
84
- const dir = join(this.directory(id), "permissions");
172
+ pendingInteractions(id: string, generation: string) {
173
+ const dir = join(this.directory(id), "interactions");
85
174
  if (!existsSync(dir)) return [];
86
175
  const names = readdirSync(dir);
87
176
  return names.filter(n => n.endsWith(".json") && !n.endsWith("-reply.json") && !names.includes(n.slice(0, -5) + "-reply.json"))
88
- .map(n => this.read(id, "permissions/" + n, acpPermission)).filter(item => item.generation === generation);
177
+ .map(n => this.read(id, "interactions/" + n, acpInteraction)).filter(item => item.generation === generation);
89
178
  }
90
- /** Elicitation requests (forms, choices, text inputs, URL confirmations) of a generation that have no reply yet. */
91
- pendingElicitations(id: string, generation: string) {
92
- const dir = join(this.directory(id), "elicitations");
93
- if (!existsSync(dir)) return [];
94
- const names = readdirSync(dir);
95
- return names.filter(n => n.endsWith(".json") && !n.endsWith("-reply.json") && !names.includes(n.slice(0, -5) + "-reply.json"))
96
- .map(n => this.read(id, "elicitations/" + n, acpElicitation)).filter(item => item.generation === generation);
179
+ removeInteraction(id: string, requestId: string) {
180
+ fileId(requestId);
181
+ const dir = join(this.directory(id), "interactions");
182
+ tryUnlink(join(dir, requestId + ".json"));
183
+ tryUnlink(join(dir, requestId + "-reply.json"));
184
+ tryRmdir(dir);
185
+ }
186
+ clearTransient(id: string) {
187
+ const base = this.directory(id);
188
+ if (!existsSync(base)) return;
189
+ rmSync(join(base, "inbox"), { recursive: true, force: true });
190
+ rmSync(join(base, "interactions"), { recursive: true, force: true });
191
+ tryUnlink(join(base, "acp-answer.json"));
192
+ }
193
+ removeWorker(id: string) {
194
+ rmSync(this.directory(id), { recursive: true, force: true });
195
+ tryRmdir(this.root);
97
196
  }
98
197
  ensureSubdirectory(id: string, name: string) {
99
198
  privateDirectory(this.directory(id), name);
100
199
  }
101
200
  reference(id: string): AgentSessionReference {
102
- return this.read(id, "acp-session.json", acpReference);
201
+ const w = this.worker(id);
202
+ const saved = this.optional(id, "state.json", status);
203
+ return {
204
+ host: "acp",
205
+ backend: w.acp.backend,
206
+ command: w.acp.command,
207
+ args: w.acp.args,
208
+ ...(w.acp.env ? { env: w.acp.env } : {}),
209
+ ...(w.acp.permission ? { permission: w.acp.permission } : {}),
210
+ sessionId: saved?.sessionId ?? null,
211
+ ...(saved?.capabilities ? { capabilities: saved.capabilities } : {}),
212
+ };
103
213
  }
104
214
  /** Short controller edits are serialized; an abandoned lock fails closed. */
105
215
  control<T>(work: () => T): T {
@@ -115,14 +225,17 @@ export class AgentFiles {
115
225
  }
116
226
  enqueue(id: string, text: string, delivery: AgentMessage["delivery"], files: string[] = [], requestId: string = randomUUID()) {
117
227
  fileId(requestId);
118
- const run = this.read(id, "launch.json", launch), previous = this.optional(id, "inbox/" + requestId + ".json", message);
228
+ const w = this.worker(id);
229
+ const previous = this.optional(id, "inbox/" + requestId + ".json", message);
119
230
  if (previous) {
120
- requireThat(previous.generation === run.generation && previous.message === text && previous.delivery === delivery && canonical(previous.files) === canonical(files), "请求 ID 已被不同输入使用。");
231
+ requireThat(previous.generation === w.generation && previous.message === text && previous.delivery === delivery && canonical(previous.files) === canonical(files), "请求 ID 已被不同输入使用。");
121
232
  return previous;
122
233
  }
123
- run.sequence++;
124
- this.write(id, "launch.json", run, launch);
125
- const value: AgentMessage = { requestId, generation: run.generation, sequence: run.sequence, message: text, delivery, files };
126
- this.write(id, "inbox/" + value.requestId + ".json", value, message, true); return value;
234
+ w.sequence++;
235
+ this.write(id, "worker.json", w, workerRecord);
236
+ this.ensureSubdirectory(id, "inbox");
237
+ const value: AgentMessage = { requestId, generation: w.generation, sequence: w.sequence, message: text, delivery, files };
238
+ this.write(id, "inbox/" + value.requestId + ".json", value, message, true);
239
+ return value;
127
240
  }
128
241
  }
@@ -1,15 +0,0 @@
1
- /** Each host owns its file, including when two hosts activate the same canonical workspace concurrently. */
2
- export declare function bindWorkspacePlugin(root: string, host: string, binding?: string): void;
3
- export interface WorkspacePluginBinding {
4
- version: string;
5
- bindingPath?: string;
6
- }
7
- export interface WorkspacePluginDrift {
8
- outOfSync: boolean;
9
- highest?: string;
10
- lowest?: string;
11
- versions: Record<string, string>;
12
- }
13
- export declare function workspacePluginBindings(root: string): Record<string, WorkspacePluginBinding>;
14
- export declare function workspacePluginReleases(root: string): Record<string, string>;
15
- export declare function workspacePluginDrift(releases: Record<string, string>): WorkspacePluginDrift;
@@ -1,126 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { mkdirSync, readdirSync, readFileSync } from "node:fs";
3
- import { homedir } from "node:os";
4
- import { isAbsolute, join, resolve } from "node:path";
5
- import { object } from "../../common/json.js";
6
- import { resourceRoot } from "../../common/paths.js";
7
- import { requireThat } from "../../common/validation.js";
8
- import { releaseLine } from "../../common/version.js";
9
- import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
10
- function compareSemver(a, b) {
11
- const parse = (value) => {
12
- const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?/.exec(value);
13
- return match
14
- ? { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), pre: match[4] ?? "" }
15
- : undefined;
16
- };
17
- const left = parse(a);
18
- const right = parse(b);
19
- if (!left || !right)
20
- return a.localeCompare(b);
21
- if (left.major !== right.major)
22
- return left.major - right.major;
23
- if (left.minor !== right.minor)
24
- return left.minor - right.minor;
25
- if (left.patch !== right.patch)
26
- return left.patch - right.patch;
27
- if (left.pre === right.pre)
28
- return 0;
29
- if (!left.pre)
30
- return 1;
31
- if (!right.pre)
32
- return -1;
33
- return left.pre.localeCompare(right.pre);
34
- }
35
- function bindingDirectory(root) {
36
- const key = createHash("sha256").update(root).digest("hex");
37
- return join(homedir(), ".pbot", "workspaces", key);
38
- }
39
- // Full plugins own a local manifest. Thin clients can supply the release stamped into their cached hooks.
40
- function pluginBinding(value) {
41
- requireThat(typeof value === "string", "无效的插件安装记录。");
42
- if (!isAbsolute(value))
43
- releaseLine(value);
44
- return value;
45
- }
46
- function permissionDenied(error) {
47
- return ["EACCES", "EPERM", "EROFS"].includes(error.code ?? "");
48
- }
49
- /** Each host owns its file, including when two hosts activate the same canonical workspace concurrently. */
50
- export function bindWorkspacePlugin(root, host, binding = resolve(resourceRoot, "../package.json")) {
51
- const directory = bindingDirectory(root);
52
- const file = encodeURIComponent(host) + ".json";
53
- try {
54
- if (readJsonFile(directory, file, pluginBinding) === binding)
55
- return;
56
- }
57
- catch (error) {
58
- if (permissionDenied(error))
59
- return;
60
- if (error.code !== "ENOENT")
61
- throw error;
62
- }
63
- try {
64
- mkdirSync(directory, { recursive: true, mode: 0o700 });
65
- writeJsonFile(directory, file, binding, pluginBinding);
66
- }
67
- catch (error) {
68
- if (!permissionDenied(error))
69
- throw error;
70
- }
71
- }
72
- export function workspacePluginBindings(root) {
73
- const directory = bindingDirectory(root);
74
- let files;
75
- try {
76
- files = readdirSync(directory);
77
- }
78
- catch (error) {
79
- if (error.code === "ENOENT" || permissionDenied(error))
80
- return {};
81
- throw error;
82
- }
83
- const bindings = Object.create(null);
84
- for (const file of files.filter(name => name.endsWith(".json"))) {
85
- const host = decodeURIComponent(file.slice(0, -5));
86
- try {
87
- const binding = readJsonFile(directory, file, pluginBinding);
88
- if (!isAbsolute(binding)) {
89
- bindings[host] = { version: binding };
90
- continue;
91
- }
92
- const manifest = JSON.parse(readFileSync(binding, "utf8"));
93
- requireThat(object(manifest) && typeof manifest.version === "string", "无效的插件安装清单:" + binding);
94
- releaseLine(manifest.version);
95
- bindings[host] = { version: manifest.version, bindingPath: binding };
96
- }
97
- catch (error) {
98
- if (error.code !== "ENOENT" && !permissionDenied(error))
99
- throw error;
100
- }
101
- }
102
- return bindings;
103
- }
104
- export function workspacePluginReleases(root) {
105
- const bindings = workspacePluginBindings(root);
106
- const releases = Object.create(null);
107
- for (const [host, entry] of Object.entries(bindings))
108
- releases[host] = entry.version;
109
- return releases;
110
- }
111
- export function workspacePluginDrift(releases) {
112
- let highest;
113
- let lowest;
114
- for (const version of Object.values(releases)) {
115
- if (!highest || compareSemver(version, highest) > 0)
116
- highest = version;
117
- if (!lowest || compareSemver(version, lowest) < 0)
118
- lowest = version;
119
- }
120
- return {
121
- outOfSync: Boolean(highest && lowest && highest !== lowest),
122
- ...(highest ? { highest } : {}),
123
- ...(lowest ? { lowest } : {}),
124
- versions: releases,
125
- };
126
- }
@@ -1,15 +0,0 @@
1
- /** Each host owns its file, including when two hosts activate the same canonical workspace concurrently. */
2
- export declare function bindWorkspacePlugin(root: string, host: string, binding?: string): void;
3
- export interface WorkspacePluginBinding {
4
- version: string;
5
- bindingPath?: string;
6
- }
7
- export interface WorkspacePluginDrift {
8
- outOfSync: boolean;
9
- highest?: string;
10
- lowest?: string;
11
- versions: Record<string, string>;
12
- }
13
- export declare function workspacePluginBindings(root: string): Record<string, WorkspacePluginBinding>;
14
- export declare function workspacePluginReleases(root: string): Record<string, string>;
15
- export declare function workspacePluginDrift(releases: Record<string, string>): WorkspacePluginDrift;