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.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +5 -8
- package/README.zh-CN.md +5 -8
- package/bin/pbot-pi-acp.mjs +1 -1
- package/docs/README.md +1 -1
- package/docs/architecture/design.md +2 -2
- package/docs/architecture/extensibility.md +1 -1
- package/docs/architecture/model-context.md +1 -2
- package/docs/guides/capabilities.md +4 -8
- package/docs/guides/cross-harness-workers.md +4 -4
- package/docs/guides/history.md +1 -3
- package/docs/guides/workspace-configuration.md +4 -4
- package/docs/hosts/claude-code.md +2 -2
- package/docs/hosts/codex-qoder.md +1 -1
- package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +1 -1
- package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +1 -1
- package/lib/antigravity-plugin/lib/extension/acp/member.js +11 -10
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +348 -203
- package/lib/antigravity-plugin/lib/extension/cli.js +574 -549
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +1 -1
- package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +3 -4
- package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +0 -11
- package/lib/antigravity-plugin/lib/extension/mcp.js +462 -326
- package/lib/antigravity-plugin/lib/integrations/agent-resources.js +2 -2
- package/lib/antigravity-plugin/lib/model/acp.d.ts +29 -35
- package/lib/antigravity-plugin/lib/model/agent.d.ts +33 -17
- package/lib/antigravity-plugin/lib/model/agent.js +0 -1
- package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +5 -8
- package/lib/antigravity-plugin/lib/storage/acp-files.js +34 -40
- package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +19 -10
- package/lib/antigravity-plugin/lib/storage/agent-files.js +147 -43
- package/lib/antigravity-plugin/package.json +1 -1
- package/lib/antigravity-plugin/plugin.json +1 -1
- package/lib/antigravity-plugin/resources/agents/coordinator.md +3 -7
- package/lib/antigravity-plugin/resources/agents/general-purpose.md +3 -3
- package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -2
- package/lib/antigravity-plugin/resources/prompts/workspace.md +12 -16
- package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +7 -6
- package/lib/antigravity-plugin/resources/templates/workspace-agents.md +0 -4
- package/lib/antigravity-plugin/skills/pbot/SKILL.md +14 -6
- package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +7 -6
- package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
- package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
- package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
- package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +2 -2
- package/lib/extension/acp/member.d.ts +1 -1
- package/lib/extension/acp/member.js +11 -10
- package/lib/extension/acp/worker.js +34 -33
- package/lib/extension/acp-worker.js +348 -203
- package/lib/extension/cli.js +574 -549
- package/lib/extension/hosts/dsh/acp-member.js +1 -1
- package/lib/extension/hosts/dsh/command.js +1 -1
- package/lib/extension/hosts/dsh/workspace.js +1 -3
- package/lib/extension/hosts/pi/acp-member.js +3 -4
- package/lib/extension/hosts/plugin-runtime.js +0 -11
- package/lib/extension/mcp/tool-contract.d.ts +8 -50
- package/lib/extension/mcp/tool-contract.js +48 -62
- package/lib/extension/mcp/workers.d.ts +13 -27
- package/lib/extension/mcp/workers.js +60 -25
- package/lib/extension/mcp.js +462 -326
- package/lib/integrations/acp/agents.d.ts +2 -3
- package/lib/integrations/acp/agents.js +22 -22
- package/lib/integrations/acp/harnesses/claude.js +1 -1
- package/lib/integrations/agent-resources.js +2 -2
- package/lib/model/acp.d.ts +29 -35
- package/lib/model/agent.d.ts +33 -17
- package/lib/model/agent.js +0 -1
- package/lib/qoder-plugin/.mcp.json +2 -2
- package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
- package/lib/qoder-plugin/hooks/hooks.json +3 -3
- package/lib/qoder-plugin/skills/pbot/SKILL.md +2 -2
- package/lib/services/agents/acp-run.d.ts +6 -4
- package/lib/services/agents/acp-run.js +62 -43
- package/lib/services/agents/agent-run.d.ts +10 -6
- package/lib/services/agents/agent-run.js +142 -65
- package/lib/services/agents/agent-service.d.ts +3 -21
- package/lib/services/agents/agent-service.js +180 -126
- package/lib/storage/acp-files.d.ts +5 -8
- package/lib/storage/acp-files.js +34 -40
- package/lib/storage/agent-files.d.ts +19 -10
- package/lib/storage/agent-files.js +147 -43
- package/lib/tools/history/contract.js +12 -12
- package/package.json +1 -1
- package/resources/agents/coordinator.md +3 -7
- package/resources/agents/general-purpose.md +3 -3
- package/resources/antigravity/skills/pbot/SKILL.md +14 -6
- package/resources/claude/skills/pbot/SKILL.md +14 -6
- package/resources/plugin/skills/pbot/SKILL.md +2 -2
- package/resources/prompts/history-coordinator.md +1 -2
- package/resources/prompts/plugin-coordinator.md +1 -2
- package/resources/prompts/workspace.md +12 -16
- package/resources/skills/provider-connections/SKILL.md +7 -6
- package/resources/templates/workspace-agents.md +0 -4
- package/src/extension/acp/member.ts +14 -11
- package/src/extension/acp/worker.ts +31 -28
- package/src/extension/cli/commands/upgrade.ts +8 -36
- package/src/extension/cli/commands/workspace.ts +3 -8
- package/src/extension/cli/index.ts +1 -2
- package/src/extension/hosts/dsh/acp-member.ts +1 -1
- package/src/extension/hosts/dsh/command.ts +1 -1
- package/src/extension/hosts/dsh/workspace.ts +1 -3
- package/src/extension/hosts/pi/acp-member.ts +3 -4
- package/src/extension/hosts/pi/command.ts +1 -1
- package/src/extension/hosts/pi/lifecycle.ts +0 -2
- package/src/extension/hosts/plugin-runtime.ts +0 -10
- package/src/extension/mcp/tool-contract.ts +48 -62
- package/src/extension/mcp/workers.ts +66 -27
- package/src/integrations/acp/agents.ts +28 -27
- package/src/integrations/acp/harnesses/claude.ts +1 -1
- package/src/integrations/agent-resources.ts +2 -2
- package/src/integrations/hosts/dsh/session.ts +2 -2
- package/src/integrations/installation/cli-update.ts +2 -2
- package/src/integrations/installation/installers/claude.ts +4 -4
- package/src/integrations/installation/installers/qoder.ts +5 -5
- package/src/integrations/installation/upgraders/antigravity.ts +57 -15
- package/src/integrations/installation/upgraders/claude.ts +1 -4
- package/src/integrations/installation/upgraders/codex.ts +85 -15
- package/src/integrations/installation/upgraders/dsh.ts +46 -24
- package/src/integrations/installation/upgraders/pi.ts +18 -11
- package/src/integrations/installation/upgraders/qoder.ts +95 -20
- package/src/model/acp.ts +27 -29
- package/src/model/agent.ts +80 -16
- package/src/model/installation.ts +0 -2
- package/src/pbot.ts +2 -2
- package/src/services/agents/acp-run.ts +81 -49
- package/src/services/agents/agent-run.ts +154 -56
- package/src/services/agents/agent-service.ts +204 -133
- package/src/services/installation/upgrade-service.ts +1 -5
- package/src/storage/acp-files.ts +50 -45
- package/src/storage/agent-files.ts +159 -46
- package/src/tools/history/contract.ts +12 -12
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +0 -15
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +0 -126
- package/lib/antigravity-plugin/resources/references/compute-finance.md +0 -22
- package/lib/antigravity-plugin/resources/references/compute.md +0 -16
- package/lib/antigravity-plugin/resources/references/task-conductor.md +0 -25
- package/lib/services/workspace/plugin-releases.d.ts +0 -15
- package/lib/services/workspace/plugin-releases.js +0 -126
- package/resources/references/compute-finance.md +0 -22
- package/resources/references/compute.md +0 -16
- package/resources/references/task-conductor.md +0 -25
- package/src/services/workspace/plugin-releases.ts +0 -121
|
@@ -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,
|
|
5
|
-
import { AgentFiles
|
|
6
|
-
import { acpAnswer,
|
|
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;
|
|
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.
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
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,
|
|
75
|
-
: { generation: this.generation, requestId,
|
|
76
|
-
this.files.ensureSubdirectory(this.id, "
|
|
77
|
-
this.files.write(this.id, "
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
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();
|
|
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.
|
|
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);
|
|
116
|
-
this.
|
|
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 = {
|
|
130
|
-
|
|
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
|
-
|
|
133
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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;
|
|
@@ -1,42 +1,107 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { AgentFiles,
|
|
2
|
+
import { AgentFiles, outputRecord, status } from "../../storage/agent-files.js";
|
|
3
3
|
import { requireThat } from "../../common/validation.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { AcpModelSelection } from "../../model/acp.js";
|
|
5
|
+
import type { AgentCycle, AgentOutput, AgentStatus } from "../../model/agent.js";
|
|
5
6
|
|
|
6
|
-
/** Durable request receipts and work
|
|
7
|
+
/** Durable request receipts and latest work cycle in state.json + output.json. */
|
|
7
8
|
export class AgentRun {
|
|
8
9
|
readonly #accepted = new Set<string>();
|
|
10
|
+
readonly #inflight = new Set<string>();
|
|
9
11
|
readonly #assigned = new Set<string>();
|
|
10
12
|
readonly #consumed = new Set<string>();
|
|
11
13
|
readonly #queued: string[] = [];
|
|
12
14
|
readonly #saved: AgentStatus | undefined;
|
|
13
|
-
#interactive = false;
|
|
14
|
-
#humanConsumed = false;
|
|
15
15
|
#current: AgentCycle | undefined;
|
|
16
16
|
#snapshot: AgentStatus;
|
|
17
|
+
|
|
17
18
|
constructor(readonly files: AgentFiles, readonly id: string, readonly generation: string) {
|
|
18
|
-
const
|
|
19
|
-
this.#saved =
|
|
19
|
+
const existing = files.optional(id, "state.json", status);
|
|
20
|
+
this.#saved = existing?.generation === generation ? existing : undefined;
|
|
20
21
|
const requests = new Set(files.messages(id).filter(m => m.generation === generation).map(m => m.requestId));
|
|
21
|
-
for (const requestId of this.#saved?.accepted ?? [])
|
|
22
|
-
|
|
22
|
+
for (const requestId of this.#saved?.accepted ?? []) {
|
|
23
|
+
if (requests.has(requestId)) {
|
|
24
|
+
this.#accepted.add(requestId);
|
|
25
|
+
this.#inflight.add(requestId);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
this.#snapshot = {
|
|
29
|
+
generation,
|
|
30
|
+
state: "starting",
|
|
31
|
+
activity: "启动",
|
|
32
|
+
updatedAt: Date.now(),
|
|
33
|
+
...(existing?.sessionId !== undefined ? { sessionId: existing.sessionId } : {}),
|
|
34
|
+
...(existing?.capabilities ? { capabilities: existing.capabilities } : {}),
|
|
35
|
+
...(this.#saved?.selection ? { selection: this.#saved.selection } : {}),
|
|
36
|
+
...(this.#saved?.activeRequestId ? { activeRequestId: this.#saved.activeRequestId } : {}),
|
|
37
|
+
};
|
|
23
38
|
}
|
|
39
|
+
|
|
24
40
|
get current(): Readonly<AgentCycle> | undefined { return this.#current; }
|
|
25
41
|
get state() { return this.#snapshot.state; }
|
|
26
42
|
get pending() { return this.#queued.length; }
|
|
27
|
-
get interactive() { return this.#interactive; }
|
|
28
43
|
accepted(requestId: string) { return this.#accepted.has(requestId); }
|
|
44
|
+
|
|
45
|
+
setSession(sessionId: string | null, capabilities?: { resume: boolean; load: boolean; close: boolean }) {
|
|
46
|
+
this.#snapshot = {
|
|
47
|
+
...this.#snapshot,
|
|
48
|
+
sessionId,
|
|
49
|
+
...(capabilities ? { capabilities } : {}),
|
|
50
|
+
updatedAt: Date.now(),
|
|
51
|
+
};
|
|
52
|
+
this.persist();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
setSelection(selection: AcpModelSelection) {
|
|
56
|
+
const clean = Object.fromEntries(Object.entries(selection).filter(([, value]) => value !== undefined)) as AcpModelSelection;
|
|
57
|
+
this.#snapshot = {
|
|
58
|
+
...this.#snapshot,
|
|
59
|
+
...(Object.keys(clean).length ? { selection: clean } : {}),
|
|
60
|
+
updatedAt: Date.now(),
|
|
61
|
+
};
|
|
62
|
+
this.persist();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setActiveRequest(requestId: string | undefined) {
|
|
66
|
+
const next: AgentStatus = { ...this.#snapshot, updatedAt: Date.now() };
|
|
67
|
+
if (requestId) next.activeRequestId = requestId;
|
|
68
|
+
else delete next.activeRequestId;
|
|
69
|
+
this.#snapshot = next;
|
|
70
|
+
this.persist();
|
|
71
|
+
}
|
|
72
|
+
|
|
29
73
|
save(patch: Pick<AgentStatus, "state" | "activity"> & { error?: string }) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
74
|
+
const next: AgentStatus = {
|
|
75
|
+
...this.#snapshot,
|
|
76
|
+
state: patch.state,
|
|
77
|
+
activity: patch.activity,
|
|
78
|
+
updatedAt: Date.now(),
|
|
79
|
+
};
|
|
80
|
+
if (patch.error !== undefined) next.error = patch.error;
|
|
81
|
+
else delete next.error;
|
|
82
|
+
this.#snapshot = next;
|
|
83
|
+
this.persist();
|
|
33
84
|
}
|
|
85
|
+
|
|
86
|
+
private persist() {
|
|
87
|
+
const record: AgentStatus = {
|
|
88
|
+
...this.#snapshot,
|
|
89
|
+
...(this.#inflight.size ? { accepted: [...this.#inflight] } : {}),
|
|
90
|
+
...(this.#current ? { cycle: structuredClone(this.#current) } : {}),
|
|
91
|
+
};
|
|
92
|
+
if (!this.#inflight.size) delete record.accepted;
|
|
93
|
+
if (!this.#current) delete record.cycle;
|
|
94
|
+
this.files.write(this.id, "state.json", record, status);
|
|
95
|
+
}
|
|
96
|
+
|
|
34
97
|
/** A receipt is durable before input is passed to the native execution boundary. */
|
|
35
98
|
claim(requestId: string) {
|
|
36
99
|
requireThat(this.files.messages(this.id).some(m => m.requestId === requestId && m.generation === this.generation), "请求不属于当前启动代次。");
|
|
37
100
|
this.#accepted.add(requestId);
|
|
101
|
+
this.#inflight.add(requestId);
|
|
38
102
|
this.save({ state: "busy", activity: "消息已领取" });
|
|
39
103
|
}
|
|
104
|
+
|
|
40
105
|
/** Native ownership associates work even when preparation fails before model admission. */
|
|
41
106
|
enter(requestId: string) {
|
|
42
107
|
requireThat(this.#accepted.has(requestId), "请求尚未领取。");
|
|
@@ -44,71 +109,104 @@ export class AgentRun {
|
|
|
44
109
|
this.#assigned.add(requestId);
|
|
45
110
|
if (this.#current) {
|
|
46
111
|
if (!this.#current.requestIds.includes(requestId)) this.#current.requestIds.push(requestId);
|
|
47
|
-
|
|
48
|
-
|
|
112
|
+
} else if (!this.#queued.includes(requestId)) {
|
|
113
|
+
this.#queued.push(requestId);
|
|
114
|
+
}
|
|
49
115
|
this.save({ state: "busy", activity: "消息已接收" });
|
|
50
116
|
}
|
|
117
|
+
|
|
51
118
|
/** Model admission is a separate fact from a native inbox claim. */
|
|
52
119
|
consume(requestId: string) {
|
|
53
120
|
requireThat(this.#accepted.has(requestId), "请求尚未领取。");
|
|
54
121
|
this.#consumed.add(requestId);
|
|
55
122
|
this.enter(requestId);
|
|
56
123
|
}
|
|
57
|
-
|
|
58
|
-
if (!this.#accepted.has(requestId) || this.#assigned.has(requestId)) return;
|
|
59
|
-
const value: AgentCycle = { cycleId: requestId, generation: this.generation,
|
|
60
|
-
startedAt: this.files.read(this.id, "launch.json", launch).startedAt, finishedAt: Date.now(),
|
|
61
|
-
state: "cancelled", leaf: null, requestIds: [requestId], source: "parent" };
|
|
62
|
-
this.files.write(this.id, "completions/" + requestId + ".json", value, cycle, true);
|
|
63
|
-
this.#assigned.add(requestId);
|
|
64
|
-
}
|
|
65
|
-
humanInput() { this.#humanConsumed = true; this.enterHuman(); }
|
|
66
|
-
enterHuman() {
|
|
67
|
-
if (this.#current) {
|
|
68
|
-
if (this.#current.source === "parent") this.#current.source = this.#current.requestIds.length ? "mixed" : "interactive";
|
|
69
|
-
} else this.#interactive = true;
|
|
70
|
-
this.save({ state: "busy", activity: "人工输入" });
|
|
71
|
-
}
|
|
124
|
+
|
|
72
125
|
begin() {
|
|
73
126
|
if (!this.#current) {
|
|
74
127
|
const requestIds = this.#queued.splice(0);
|
|
75
|
-
this.#current = {
|
|
76
|
-
|
|
77
|
-
|
|
128
|
+
this.#current = {
|
|
129
|
+
cycleId: randomUUID(),
|
|
130
|
+
generation: this.generation,
|
|
131
|
+
startedAt: Date.now(),
|
|
132
|
+
state: "running",
|
|
133
|
+
requestIds,
|
|
134
|
+
};
|
|
78
135
|
}
|
|
79
136
|
this.save({ state: "busy", activity: "思考" });
|
|
80
137
|
}
|
|
81
|
-
|
|
138
|
+
|
|
139
|
+
finish(end: { state: Exclude<AgentCycle["state"], "running">; answer: string; error?: string }) {
|
|
82
140
|
if (!this.#current) return;
|
|
83
|
-
const valid = end.
|
|
84
|
-
|
|
85
|
-
this.#current.requestIds.every(id => this.#consumed.has(id))
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
141
|
+
const valid = end.answer.trim().length > 0 &&
|
|
142
|
+
this.#current.requestIds.length > 0 &&
|
|
143
|
+
this.#current.requestIds.every(id => this.#consumed.has(id));
|
|
144
|
+
const finalState = end.state === "completed" && !valid ? "failed" : end.state;
|
|
145
|
+
const error = end.error ?? (end.state === "completed" && !valid ? "没有已消费的请求或有效最终回答。" : undefined);
|
|
146
|
+
const finished: AgentOutput = {
|
|
147
|
+
cycleId: this.#current.cycleId,
|
|
148
|
+
generation: this.generation,
|
|
149
|
+
startedAt: this.#current.startedAt,
|
|
150
|
+
finishedAt: Date.now(),
|
|
151
|
+
state: finalState,
|
|
152
|
+
requestIds: [...this.#current.requestIds],
|
|
153
|
+
text: end.answer,
|
|
154
|
+
...(error ? { error } : {}),
|
|
155
|
+
};
|
|
156
|
+
const completedRequests = [...this.#current.requestIds];
|
|
157
|
+
for (const requestId of completedRequests) {
|
|
158
|
+
this.#inflight.delete(requestId);
|
|
159
|
+
this.files.removeMessage(this.id, requestId);
|
|
160
|
+
}
|
|
161
|
+
this.files.write(this.id, "output.json", finished, outputRecord);
|
|
162
|
+
this.#current = undefined;
|
|
163
|
+
delete this.#snapshot.activeRequestId;
|
|
164
|
+
this.save({ state: "idle", activity: finished.state });
|
|
93
165
|
}
|
|
166
|
+
|
|
94
167
|
/** Reload seals uncertainty. It never turns a receipt into permission to replay. */
|
|
95
168
|
recover(receipts: Iterable<{ generation: string; requestId: string }> = []) {
|
|
96
169
|
const requests = new Set(this.files.messages(this.id).filter(m => m.generation === this.generation).map(m => m.requestId));
|
|
97
|
-
for (const receipt of receipts)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
170
|
+
for (const receipt of receipts) {
|
|
171
|
+
if (receipt.generation === this.generation && requests.has(receipt.requestId)) {
|
|
172
|
+
this.#accepted.add(receipt.requestId);
|
|
173
|
+
this.#inflight.add(receipt.requestId);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
let latest = this.files.output(this.id);
|
|
177
|
+
if (latest?.generation === this.generation) {
|
|
178
|
+
for (const id of latest.requestIds) this.#assigned.add(id);
|
|
179
|
+
}
|
|
180
|
+
if (this.#saved?.cycle?.state === "running" && latest?.cycleId !== this.#saved.cycle.cycleId) {
|
|
181
|
+
const interrupted: AgentOutput = {
|
|
182
|
+
cycleId: this.#saved.cycle.cycleId,
|
|
183
|
+
generation: this.#saved.cycle.generation,
|
|
184
|
+
startedAt: this.#saved.cycle.startedAt,
|
|
185
|
+
finishedAt: Date.now(),
|
|
186
|
+
state: "interrupted",
|
|
187
|
+
requestIds: [...this.#saved.cycle.requestIds],
|
|
188
|
+
text: "",
|
|
189
|
+
...(this.#saved.cycle.error ? { error: this.#saved.cycle.error } : {}),
|
|
190
|
+
};
|
|
191
|
+
this.files.write(this.id, "output.json", interrupted, outputRecord);
|
|
192
|
+
latest = interrupted;
|
|
193
|
+
for (const id of interrupted.requestIds) this.#assigned.add(id);
|
|
104
194
|
}
|
|
105
195
|
for (const requestId of this.#accepted) {
|
|
106
196
|
this.#assigned.add(requestId);
|
|
107
|
-
if (
|
|
108
|
-
const interrupted:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
197
|
+
if (latest?.generation === this.generation && latest.requestIds.includes(requestId)) continue;
|
|
198
|
+
const interrupted: AgentOutput = {
|
|
199
|
+
cycleId: requestId,
|
|
200
|
+
generation: this.generation,
|
|
201
|
+
startedAt: this.files.worker(this.id).startedAt,
|
|
202
|
+
finishedAt: Date.now(),
|
|
203
|
+
state: "interrupted",
|
|
204
|
+
requestIds: [requestId],
|
|
205
|
+
text: "",
|
|
206
|
+
error: "领取后执行结果不明;未重跑。",
|
|
207
|
+
};
|
|
208
|
+
this.files.write(this.id, "output.json", interrupted, outputRecord);
|
|
209
|
+
latest = interrupted;
|
|
112
210
|
}
|
|
113
211
|
}
|
|
114
212
|
}
|