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
package/src/model/acp.ts CHANGED
@@ -46,8 +46,6 @@ export interface AcpModelsResult {
46
46
  currentThinking?: string;
47
47
  selection: AcpModelSelection;
48
48
  }
49
- export interface AcpModelState extends AcpModelSelection { generation: string }
50
- export interface AcpProcess { generation: string; pid: number; startedAt: number }
51
49
  export interface AcpInput { generation: string; requestId: string }
52
50
  export const acpAnswerStates = ["completed", "failed", "cancelled"] as const;
53
51
  export interface AcpAnswer extends AcpInput {
@@ -55,14 +53,6 @@ export interface AcpAnswer extends AcpInput {
55
53
  state: typeof acpAnswerStates[number];
56
54
  error?: string;
57
55
  }
58
- export interface AcpReport { generation: string; cycleId: string; text: string }
59
- export interface AcpPermission {
60
- generation: string;
61
- requestId: string;
62
- description: string;
63
- options: { optionId: string; name: string; kind: string }[];
64
- }
65
- export interface AcpPermissionReply { generation: string; requestId: string; optionId: string | null }
66
56
  export const acpElicitationActions = ["accept", "decline", "cancel"] as const;
67
57
  export type AcpElicitationAction = typeof acpElicitationActions[number];
68
58
  export type AcpElicitationValue = string | number | boolean | string[];
@@ -72,29 +62,37 @@ export function isAcpElicitationValue(value: unknown): value is AcpElicitationVa
72
62
  }
73
63
 
74
64
  export type AcpElicitationContent = Record<string, AcpElicitationValue>;
75
- export interface AcpElicitation {
76
- generation: string;
77
- requestId: string;
78
- mode: "form" | "url";
79
- message: string;
80
- schema?: Record<string, unknown>;
81
- url?: string;
82
- }
83
- export interface AcpElicitationReply {
84
- generation: string;
85
- requestId: string;
86
- action: AcpElicitationAction;
87
- content?: AcpElicitationContent;
88
- }
65
+ export type AcpInteraction =
66
+ | {
67
+ generation: string;
68
+ requestId: string;
69
+ kind: "permission";
70
+ description: string;
71
+ options: { optionId: string; name: string; kind: string }[];
72
+ }
73
+ | {
74
+ generation: string;
75
+ requestId: string;
76
+ kind: "form";
77
+ message: string;
78
+ schema: Record<string, unknown>;
79
+ }
80
+ | {
81
+ generation: string;
82
+ requestId: string;
83
+ kind: "url";
84
+ message: string;
85
+ url: string;
86
+ };
87
+ export type AcpInteractionReply =
88
+ | { generation: string; requestId: string; kind: "permission"; optionId: string | null }
89
+ | { generation: string; requestId: string; kind: "form"; action: AcpElicitationAction; content?: AcpElicitationContent }
90
+ | { generation: string; requestId: string; kind: "url"; action: AcpElicitationAction };
89
91
 
90
92
  export interface AcpStartOptions {
91
- name?: string;
92
- message?: string;
93
- files?: string[];
94
93
  provider?: string;
95
94
  model?: string;
96
95
  thinking?: string;
97
- cwd?: string;
98
96
  tools?: string[];
99
97
  noExtensions?: boolean;
100
98
  noSkills?: boolean;
@@ -104,7 +102,7 @@ export interface AcpStartOptions {
104
102
 
105
103
  export interface AcpLaunchContext {
106
104
  readonly reference: AcpSessionReference;
107
- readonly cwd: string;
105
+ readonly workspace: string;
108
106
  readonly directory: string;
109
107
  readonly packageRoot: string;
110
108
  readonly options: AcpStartOptions;
@@ -1,36 +1,102 @@
1
- import type { AcpBackend, AcpElicitationAction, AcpElicitationContent, AcpModelSelection, AcpSessionReference, AcpStartOptions } from "./acp.js";
1
+ import type {
2
+ AcpBackend,
3
+ AcpCommand,
4
+ AcpElicitationAction,
5
+ AcpElicitationContent,
6
+ AcpModelSelection,
7
+ AcpPermissionPolicy,
8
+ AcpSessionReference,
9
+ AcpStartOptions,
10
+ } from "./acp.js";
2
11
 
3
12
  export type AgentOptions = AcpStartOptions;
4
- export interface AgentStartOptions extends AgentOptions { backend: AcpBackend }
13
+ export interface AgentStartOptions extends AgentOptions {
14
+ backend: AcpBackend;
15
+ message?: string;
16
+ files?: string[];
17
+ }
5
18
  export interface AgentOwner { sessionId: string; leaf: string | null; branch: string[] }
6
19
  export interface AgentDescriptor {
7
- agentId: string; release: string; workspace: string; cwd: string; parentSessionId: string;
8
- parentLeaf: string | null; createdAt: number; options: AgentOptions;
20
+ agentId: string;
21
+ release: string;
22
+ workspace: string;
23
+ parentSessionId: string;
24
+ options: AgentOptions;
9
25
  }
10
26
  export type AgentSessionReference = AcpSessionReference;
11
- export interface AgentLaunch { generation: string; name: string; sequence: number; stop: boolean; startedAt: number }
27
+ export interface AgentAcpConfig extends AcpCommand {
28
+ backend: AcpBackend;
29
+ permission?: AcpPermissionPolicy;
30
+ }
31
+ export interface AgentWorkerRecord {
32
+ agentId: string;
33
+ release: string;
34
+ workspace: string;
35
+ parentSessionId: string;
36
+ generation: string;
37
+ sequence: number;
38
+ stop: boolean;
39
+ startedAt: number;
40
+ pid?: number;
41
+ acp: AgentAcpConfig;
42
+ options?: AgentOptions;
43
+ }
12
44
  export const messageDeliveries = ["steer", "followUp"] as const;
13
45
  export const cycleStates = ["running", "completed", "failed", "cancelled", "interrupted"] as const;
14
- export const cycleSources = ["parent", "interactive", "mixed"] as const;
15
46
  export const agentStates = ["starting", "busy", "idle", "stopping", "stopped", "error"] as const;
16
47
 
17
- export interface AgentMessage { requestId: string; generation: string; sequence: number; message: string; delivery: typeof messageDeliveries[number]; files: string[] }
48
+ export interface AgentMessage {
49
+ requestId: string;
50
+ generation: string;
51
+ sequence: number;
52
+ message: string;
53
+ delivery: typeof messageDeliveries[number];
54
+ files: string[];
55
+ }
18
56
  export interface AgentCycle {
19
- cycleId: string; generation: string; startedAt: number; finishedAt?: number;
57
+ cycleId: string;
58
+ generation: string;
59
+ startedAt: number;
60
+ finishedAt?: number;
20
61
  state: typeof cycleStates[number];
21
- leaf: string | null; requestIds: string[]; source: typeof cycleSources[number];
62
+ requestIds: string[];
22
63
  error?: string;
23
64
  }
24
65
  export interface AgentStatus {
25
- generation: string; state: typeof agentStates[number];
26
- activity: string; updatedAt: number; accepted?: string[]; cycle?: AgentCycle; error?: string;
66
+ generation: string;
67
+ state: typeof agentStates[number];
68
+ activity: string;
69
+ updatedAt: number;
70
+ sessionId?: string | null;
71
+ capabilities?: { resume: boolean; load: boolean; close: boolean };
72
+ selection?: AcpModelSelection;
73
+ activeRequestId?: string;
74
+ accepted?: string[];
75
+ cycle?: AgentCycle;
76
+ error?: string;
77
+ }
78
+ export interface AgentOutput {
79
+ cycleId: string;
80
+ generation: string;
81
+ startedAt: number;
82
+ finishedAt: number;
83
+ state: Exclude<AgentCycle["state"], "running">;
84
+ requestIds: string[];
85
+ text: string;
86
+ error?: string;
27
87
  }
28
88
  export interface AgentView extends AgentDescriptor {
29
- name: string; generation: string; state: AgentStatus["state"]; activity: string;
89
+ generation: string;
90
+ state: AgentStatus["state"];
91
+ activity: string;
30
92
  alive: boolean;
31
- backend: AcpBackend; transport: "acp"; selection?: AcpModelSelection;
93
+ backend: AcpBackend;
94
+ transport: "acp";
95
+ selection?: AcpModelSelection;
32
96
  capabilities: { steer: boolean; resume: boolean; transcript: boolean };
33
- cycle?: AgentCycle; error?: string; owned: boolean;
97
+ cycle?: AgentCycle;
98
+ error?: string;
99
+ owned: boolean;
34
100
  pendingRequests?: AgentPendingRequest[];
35
101
  }
36
102
  export type AgentPendingRequest =
@@ -43,7 +109,6 @@ export interface AgentResponseInput {
43
109
  optionId?: string;
44
110
  content?: AcpElicitationContent;
45
111
  }
46
- export interface AgentCompletion { agentId: string; name: string; cycle: AgentCycle; report: string }
47
112
 
48
113
  export interface AgentHost {
49
114
  available(backend: AcpBackend): void;
@@ -53,5 +118,4 @@ export interface AgentHost {
53
118
  validateSession(value: AgentDescriptor, reference: AgentSessionReference): void | Promise<void>;
54
119
  launch(value: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, resume: boolean, signal: AbortSignal): void | Promise<void>;
55
120
  release(id: string, generation: string): void | Promise<void>;
56
- report(value: AgentDescriptor, reference: AgentSessionReference, leaf: string): string | Promise<string>;
57
121
  }
@@ -37,9 +37,7 @@ export type HostUpgradeState = "updated" | "up-to-date" | "skipped" | "failed";
37
37
  export interface HostUpgradeContext {
38
38
  workspace: string;
39
39
  targetVersion: string;
40
- currentVersion?: string;
41
40
  packageRoot: string;
42
- bindingPath?: string;
43
41
  env: NodeJS.ProcessEnv;
44
42
  signal: AbortSignal;
45
43
  options: Readonly<Record<string, string | undefined>>;
package/src/pbot.ts CHANGED
@@ -21,7 +21,7 @@ export default function pbot(pi: ExtensionAPI) {
21
21
 
22
22
  const lifecycle = new Lifecycle(
23
23
  (root, sessionId, configuration) => {
24
- const workerDir = defaultWorkerDirectory();
24
+ const workerDir = defaultWorkerDirectory(root);
25
25
  const workers = new McpWorkerRuntime(workerDir, sessionId, "pi");
26
26
  return new WorkspaceInstance(root, sessionId, workers, workspaceFeatures(configuration));
27
27
  },
@@ -1,9 +1,9 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { setTimeout as delay } from "node:timers/promises";
3
3
  import type { AgentDescriptor, AgentMessage } from "../../model/agent.js";
4
- import type { AcpAnswer, AcpElicitationAction, AcpElicitationContent, AcpPermission } from "../../model/acp.js";
5
- import { AgentFiles, launch } from "../../storage/agent-files.js";
6
- import { acpAnswer, acpElicitation, acpElicitationReply, acpInput, acpPermission, acpPermissionReply, acpReport } from "../../storage/acp-files.js";
4
+ import type { AcpAnswer, AcpElicitationAction, AcpElicitationContent, AcpInteraction } from "../../model/acp.js";
5
+ import { AgentFiles } from "../../storage/agent-files.js";
6
+ import { acpAnswer, acpInteraction, acpInteractionReply } from "../../storage/acp-files.js";
7
7
  import { AgentRun } from "./agent-run.js";
8
8
 
9
9
  export interface AcpProgress { kind: "text" | "thought" | "tool" | "activity"; text?: string; messageId?: string; activity?: string }
@@ -25,7 +25,7 @@ export interface AcpWork {
25
25
  cancel(): Promise<void>;
26
26
  }
27
27
 
28
- /** ACP prompt completion is the receipt boundary; final reports publish before completions. */
28
+ /** ACP prompt completion is the receipt boundary; output publishes before state returns to idle. */
29
29
  export class AcpAgentRun {
30
30
  readonly execution: AgentRun;
31
31
  readonly #answer = new AcpAnswerBuffer();
@@ -34,12 +34,15 @@ export class AcpAgentRun {
34
34
  #work: Promise<void> | undefined;
35
35
  #stop: Promise<void> | undefined;
36
36
  #failure: unknown;
37
+
37
38
  constructor(readonly files: AgentFiles, readonly descriptor: AgentDescriptor, readonly generation: string,
38
- readonly work: AcpWork, readonly exactAnswer: boolean) {
39
- this.execution = new AgentRun(files, descriptor.agentId, generation);
39
+ readonly work: AcpWork, readonly exactAnswer: boolean, execution?: AgentRun) {
40
+ this.execution = execution ?? new AgentRun(files, descriptor.agentId, generation);
40
41
  }
42
+
41
43
  private get id() { return this.descriptor.agentId; }
42
- private current() { return this.files.read(this.id, "launch.json", launch).generation === this.generation; }
44
+ private current() { return this.files.worker(this.id).generation === this.generation; }
45
+
43
46
  update(value: AcpProgress) {
44
47
  if (!this.execution.current || !this.current()) return;
45
48
  this.#answer.update(value);
@@ -48,54 +51,65 @@ export class AcpAgentRun {
48
51
  this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: value.activity ?? (value.kind === "text" ? "回答" : "思考") });
49
52
  }
50
53
  }
51
- async permission(description: string, options: AcpPermission["options"], signal: AbortSignal): Promise<string | null> {
52
- const requestId = randomUUID(), request = { generation: this.generation, requestId, description, options };
53
- this.files.ensureSubdirectory(this.id, "permissions");
54
- this.files.write(this.id, "permissions/" + requestId + ".json", request, acpPermission, true);
54
+
55
+ async permission(
56
+ description: string,
57
+ options: Extract<AcpInteraction, { kind: "permission" }>["options"],
58
+ signal: AbortSignal,
59
+ ): Promise<string | null> {
60
+ const requestId = randomUUID();
61
+ const request: AcpInteraction = { generation: this.generation, requestId, kind: "permission", description, options };
62
+ this.files.ensureSubdirectory(this.id, "interactions");
63
+ this.files.write(this.id, "interactions/" + requestId + ".json", request, acpInteraction, true);
55
64
  this.execution.save({ state: "busy", activity: "等待确认" });
56
- while (!signal.aborted && !this.#stopping && this.current() && !this.files.read(this.id, "launch.json", launch).stop) {
57
- const reply = this.files.optional(this.id, "permissions/" + requestId + "-reply.json", acpPermissionReply);
58
- if (reply?.generation === this.generation) {
59
- this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: reply.optionId ? "已获确认" : "确认被拒绝" });
60
- return reply.optionId;
65
+ try {
66
+ while (!signal.aborted && !this.#stopping && this.current() && !this.files.worker(this.id).stop) {
67
+ const reply = this.files.optional(this.id, "interactions/" + requestId + "-reply.json", acpInteractionReply);
68
+ if (reply?.generation === this.generation && reply.kind === "permission") {
69
+ this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: reply.optionId ? "已获确认" : "确认被拒绝" });
70
+ return reply.optionId;
71
+ }
72
+ await delay(100);
61
73
  }
62
- await delay(100);
74
+ return null;
75
+ } finally {
76
+ this.files.removeInteraction(this.id, requestId);
63
77
  }
64
- // Close the request so it no longer shows as pending once the turn or worker has ended.
65
- if (this.current()) this.files.write(this.id, "permissions/" + requestId + "-reply.json", { generation: this.generation, requestId, optionId: null }, acpPermissionReply, true);
66
- return null;
67
78
  }
79
+
68
80
  async elicitation(
69
81
  input: { mode: "form"; message: string; schema: Record<string, unknown> } | { mode: "url"; message: string; url: string },
70
82
  signal: AbortSignal,
71
83
  ): Promise<{ action: AcpElicitationAction; content?: AcpElicitationContent }> {
72
84
  const requestId = randomUUID();
73
- const request = input.mode === "form"
74
- ? { generation: this.generation, requestId, mode: "form" as const, message: input.message, schema: input.schema }
75
- : { generation: this.generation, requestId, mode: "url" as const, message: input.message, url: input.url };
76
- this.files.ensureSubdirectory(this.id, "elicitations");
77
- this.files.write(this.id, "elicitations/" + requestId + ".json", request, acpElicitation, true);
85
+ const request: AcpInteraction = input.mode === "form"
86
+ ? { generation: this.generation, requestId, kind: "form", message: input.message, schema: input.schema }
87
+ : { generation: this.generation, requestId, kind: "url", message: input.message, url: input.url };
88
+ this.files.ensureSubdirectory(this.id, "interactions");
89
+ this.files.write(this.id, "interactions/" + requestId + ".json", request, acpInteraction, true);
78
90
  this.execution.save({ state: "busy", activity: "等待回复" });
79
- while (!signal.aborted && !this.#stopping && this.current() && !this.files.read(this.id, "launch.json", launch).stop) {
80
- const reply = this.files.optional(this.id, "elicitations/" + requestId + "-reply.json", acpElicitationReply);
81
- if (reply?.generation === this.generation) {
82
- const label = reply.action === "accept" ? "已收到回复" : reply.action === "decline" ? "回复已拒绝" : "回复已取消";
83
- this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: label });
84
- return { action: reply.action, ...(reply.content ? { content: reply.content } : {}) };
91
+ try {
92
+ while (!signal.aborted && !this.#stopping && this.current() && !this.files.worker(this.id).stop) {
93
+ const reply = this.files.optional(this.id, "interactions/" + requestId + "-reply.json", acpInteractionReply);
94
+ if (reply?.generation === this.generation && reply.kind !== "permission") {
95
+ const label = reply.action === "accept" ? "已收到回复" : reply.action === "decline" ? "回复已拒绝" : "回复已取消";
96
+ this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: label });
97
+ return { action: reply.action, ...(reply.kind === "form" && reply.content ? { content: reply.content } : {}) };
98
+ }
99
+ await delay(100);
85
100
  }
86
- await delay(100);
87
- }
88
- if (this.current()) {
89
- this.files.write(this.id, "elicitations/" + requestId + "-reply.json",
90
- { generation: this.generation, requestId, action: "cancel" }, acpElicitationReply, true);
101
+ return { action: "cancel" };
102
+ } finally {
103
+ this.files.removeInteraction(this.id, requestId);
91
104
  }
92
- return { action: "cancel" };
93
105
  }
106
+
94
107
  async run() {
95
- this.execution.recover(); this.execution.save({ state: "idle", activity: "空闲" });
108
+ this.execution.recover();
109
+ this.execution.save({ state: "idle", activity: "空闲" });
96
110
  while (!this.#stopping && this.current() && !this.work.signal.aborted) {
97
111
  this.files.check(); if (this.#failure) throw this.#failure;
98
- if (this.files.read(this.id, "launch.json", launch).stop) { await this.stop(); break; }
112
+ if (this.files.worker(this.id).stop) { await this.stop(); break; }
99
113
  if (!this.#work) {
100
114
  const request = this.files.messages(this.id).find(message => message.generation === this.generation && !this.execution.accepted(message.requestId));
101
115
  if (request) {
@@ -110,35 +124,53 @@ export class AcpAgentRun {
110
124
  if (!this.#stopping) await this.#work;
111
125
  if (this.work.signal.aborted && !this.#stopping) throw this.work.signal.reason;
112
126
  }
127
+
113
128
  private async perform(request: AgentMessage) {
114
129
  this.#answer.reset();
115
- this.execution.claim(request.requestId); this.execution.enter(request.requestId); this.execution.begin();
116
- this.files.write(this.id, "acp-input.json", { generation: this.generation, requestId: request.requestId }, acpInput);
130
+ this.execution.claim(request.requestId);
131
+ this.execution.enter(request.requestId);
132
+ this.execution.begin();
133
+ this.execution.setActiveRequest(request.requestId);
117
134
  let result: AcpAnswer;
118
135
  try {
119
136
  const end = await this.work.prompt(request);
120
137
  let snapshot: AcpAnswer | undefined;
121
138
  if (this.exactAnswer) {
122
139
  snapshot = this.files.optional(this.id, "acp-answer.json", acpAnswer);
140
+ this.files.remove(this.id, "acp-answer.json");
123
141
  if (snapshot?.generation !== this.generation || snapshot.requestId !== request.requestId) snapshot = undefined;
124
142
  }
125
143
  const answer = this.exactAnswer ? snapshot?.answer ?? "" : this.#answer.text;
126
144
  const state = end.stopReason === "cancelled" || this.#stopping ? "cancelled" : end.stopReason === "end_turn" &&
127
145
  (!this.exactAnswer || snapshot?.state === "completed") && answer.trim() ? "completed" : "failed";
128
146
  if (state === "completed") this.execution.consume(request.requestId);
129
- result = { generation: this.generation, requestId: request.requestId, state, answer,
130
- ...(state === "failed" ? { error: snapshot?.error ?? "ACP 工作未产生有效最终回答:" + end.stopReason } : {}) };
147
+ result = {
148
+ generation: this.generation,
149
+ requestId: request.requestId,
150
+ state,
151
+ answer,
152
+ ...(state === "failed" ? { error: snapshot?.error ?? "ACP 工作未产生有效最终回答:" + end.stopReason } : {}),
153
+ };
131
154
  } catch (error) {
132
- result = { generation: this.generation, requestId: request.requestId, answer: this.#answer.text,
133
- state: this.#stopping || this.files.read(this.id, "launch.json", launch).stop ? "cancelled" : "failed", error: String(error) };
155
+ if (this.exactAnswer) this.files.remove(this.id, "acp-answer.json");
156
+ result = {
157
+ generation: this.generation,
158
+ requestId: request.requestId,
159
+ answer: this.#answer.text,
160
+ state: this.#stopping || this.files.worker(this.id).stop ? "cancelled" : "failed",
161
+ error: String(error),
162
+ };
134
163
  }
135
164
  if (!this.current()) return;
136
- const cycleId = this.execution.current!.cycleId;
137
- this.files.write(this.id, "reports/" + cycleId + ".json", { generation: this.generation, cycleId, text: result.answer }, acpReport, true);
138
- this.execution.finish({ state: this.work.signal.aborted && !this.#stopping ? "interrupted" : result.state,
139
- position: "acp:" + cycleId, answer: result.answer, ...(result.error ? { error: result.error } : {}) });
165
+ this.execution.finish({
166
+ state: this.work.signal.aborted && !this.#stopping ? "interrupted" : result.state,
167
+ answer: result.answer,
168
+ ...(result.error ? { error: result.error } : {}),
169
+ });
140
170
  }
171
+
141
172
  settled() { return this.#work; }
173
+
142
174
  stop(): Promise<void> {
143
175
  return this.#stop ??= (async () => {
144
176
  this.#stopping = true;