personal-bot 0.2.10 → 0.2.12
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/README.md +17 -3
- package/README.zh-CN.md +17 -3
- package/bin/pbot-pi-acp.mjs +20 -0
- package/docs/agent-collaboration.md +38 -2
- package/docs/capability-design.md +6 -0
- package/docs/dsh.md +9 -8
- package/docs/file-storage.md +9 -1
- package/docs/interaction-design.md +2 -2
- package/docs/model-context.md +10 -2
- package/docs/technical-design.md +21 -3
- package/docs/workspace-configuration.md +20 -1
- package/lib/client.js +40 -7
- package/lib/dsh.js +2 -0
- package/lib/extension/acp-member.d.ts +16 -0
- package/lib/extension/acp-member.js +45 -0
- package/lib/extension/acp-worker.d.ts +1 -0
- package/lib/extension/acp-worker.js +156 -0
- package/lib/extension/dsh/acp-member.d.ts +4 -0
- package/lib/extension/dsh/acp-member.js +48 -0
- package/lib/extension/dsh/command.js +11 -1
- package/lib/extension/dsh/management.js +3 -1
- package/lib/extension/dsh/workspace.d.ts +35 -1
- package/lib/extension/dsh/workspace.js +21 -3
- package/lib/integrations/acp/agents.d.ts +24 -0
- package/lib/integrations/acp/agents.js +168 -0
- package/lib/integrations/acp/claude.d.ts +18 -0
- package/lib/integrations/acp/claude.js +33 -0
- package/lib/integrations/acp/connection.d.ts +27 -0
- package/lib/integrations/acp/connection.js +167 -0
- package/lib/integrations/acp/input.d.ts +4 -0
- package/lib/integrations/acp/input.js +45 -0
- package/lib/integrations/acp/launch.d.ts +4 -0
- package/lib/integrations/acp/launch.js +52 -0
- package/lib/integrations/acp/model-selection.d.ts +12 -0
- package/lib/integrations/acp/model-selection.js +65 -0
- package/lib/integrations/dsh/dsh-agents.d.ts +2 -1
- package/lib/integrations/dsh/dsh-agents.js +4 -0
- package/lib/model/acp.d.ts +59 -0
- package/lib/model/acp.js +1 -0
- package/lib/model/agent.d.ts +15 -1
- package/lib/model/workspace-config.d.ts +3 -0
- package/lib/services/agents/acp-run.d.ts +42 -0
- package/lib/services/agents/acp-run.js +149 -0
- package/lib/services/agents/agent-service.d.ts +114 -6
- package/lib/services/agents/agent-service.js +81 -10
- package/lib/services/agents/team-service.js +4 -2
- package/lib/services/workspace/workspace-config.js +12 -1
- package/lib/storage/acp-files.d.ts +9 -0
- package/lib/storage/acp-files.js +50 -0
- package/lib/storage/agent-files.d.ts +5 -4
- package/lib/storage/agent-files.js +11 -2
- package/lib/tools/agents/actions.d.ts +16 -2
- package/lib/tools/agents/actions.js +3 -1
- package/lib/tools/agents/contract.d.ts +48 -3
- package/lib/tools/agents/contract.js +15 -9
- package/lib/tools/agents/dsh-contract.d.ts +8 -15
- package/lib/tools/agents/dsh-contract.js +12 -8
- package/lib/ui/dsh/management.js +36 -8
- package/npm-shrinkwrap.json +953 -12
- package/package.json +8 -1
- package/python/README.md +3 -3
- package/resources/skills/.claude-plugin/plugin.json +4 -0
- package/scripts/README.md +6 -0
- package/scripts/setup-python.py +33 -7
- package/src/dsh.ts +1 -0
- package/src/extension/acp-member.ts +44 -0
- package/src/extension/acp-worker.ts +124 -0
- package/src/extension/agent-events.ts +28 -3
- package/src/extension/agents-command.ts +33 -5
- package/src/extension/dsh/acp-member.ts +44 -0
- package/src/extension/dsh/command.ts +10 -1
- package/src/extension/dsh/management.ts +2 -1
- package/src/extension/dsh/workspace.ts +21 -3
- package/src/extension/lifecycle.ts +2 -2
- package/src/extension/pi-acp-member.ts +64 -0
- package/src/integrations/acp/agents.ts +118 -0
- package/src/integrations/acp/claude.ts +40 -0
- package/src/integrations/acp/connection.ts +152 -0
- package/src/integrations/acp/input.ts +39 -0
- package/src/integrations/acp/launch.ts +50 -0
- package/src/integrations/acp/model-selection.ts +66 -0
- package/src/integrations/dsh/dsh-agents.ts +4 -1
- package/src/integrations/pi/pi-agents.ts +4 -1
- package/src/model/acp.ts +26 -0
- package/src/model/agent.ts +7 -1
- package/src/model/workspace-config.ts +4 -0
- package/src/pbot.ts +3 -2
- package/src/services/agents/acp-run.ts +123 -0
- package/src/services/agents/agent-service.ts +85 -12
- package/src/services/agents/team-service.ts +3 -2
- package/src/services/workspace/workspace-config.ts +10 -1
- package/src/storage/acp-files.ts +50 -0
- package/src/storage/agent-files.ts +12 -4
- package/src/tools/agents/actions.ts +4 -2
- package/src/tools/agents/contract.ts +16 -9
- package/src/tools/agents/dsh-contract.ts +12 -8
- package/src/ui/dsh/management.ts +31 -8
|
@@ -4,7 +4,7 @@ import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { CURRENT_SESSION_VERSION, getAgentDir, getPackageDir, parseSessionEntries, SessionManager } from "@earendil-works/pi-coding-agent";
|
|
6
6
|
import { requireThat } from "../../common/validation.js";
|
|
7
|
-
import type { AgentDescriptor, AgentSessionReference } from "../../model/agent.js";
|
|
7
|
+
import type { AgentDescriptor, AgentOptions, AgentSessionReference } from "../../model/agent.js";
|
|
8
8
|
import type { AgentHost } from "../../services/agents/agent-service.js";
|
|
9
9
|
|
|
10
10
|
export const agentDirectory = () => join(getAgentDir(), "pbot/subagents");
|
|
@@ -26,6 +26,9 @@ export class PiAgents implements AgentHost {
|
|
|
26
26
|
requireThat(reference.host === "pi", "不能接管其他宿主的会话。");
|
|
27
27
|
return reference.file;
|
|
28
28
|
}
|
|
29
|
+
validateOptions(options: AgentOptions) {
|
|
30
|
+
requireThat(options.thinking === undefined || ["off", "minimal", "low", "medium", "high", "xhigh", "max"].includes(options.thinking), "Pi 思考级别无效。");
|
|
31
|
+
}
|
|
29
32
|
available() {
|
|
30
33
|
requireThat(spawnSync("tmux", ["-V"], { encoding: "utf8" }).status === 0, "子会话需要 tmux。");
|
|
31
34
|
requireThat(existsSync(join(getPackageDir(), "dist/cli.js")), "无法定位 Pi CLI。");
|
package/src/model/acp.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type AcpBackend = "pi" | "dsh" | "claude";
|
|
2
|
+
export interface AcpCommand { command: string; args: string[] }
|
|
3
|
+
export type AcpCommands = Partial<Record<AcpBackend, AcpCommand>>;
|
|
4
|
+
export interface AcpSessionReference extends AcpCommand {
|
|
5
|
+
host: "acp";
|
|
6
|
+
backend: AcpBackend;
|
|
7
|
+
sessionId: string | null;
|
|
8
|
+
capabilities?: { resume: boolean; load: boolean; close: boolean };
|
|
9
|
+
}
|
|
10
|
+
export interface AcpModelSelection { provider?: string; model?: string; resolvedModel?: string; thinking?: string }
|
|
11
|
+
export interface AcpModelState extends AcpModelSelection { generation: string }
|
|
12
|
+
export interface AcpProcess { generation: string; pid: number; startedAt: number }
|
|
13
|
+
export interface AcpInput { generation: string; requestId: string }
|
|
14
|
+
export interface AcpAnswer extends AcpInput {
|
|
15
|
+
answer: string;
|
|
16
|
+
state: "completed" | "failed" | "cancelled";
|
|
17
|
+
error?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface AcpReport { generation: string; cycleId: string; text: string }
|
|
20
|
+
export interface AcpPermission {
|
|
21
|
+
generation: string;
|
|
22
|
+
requestId: string;
|
|
23
|
+
description: string;
|
|
24
|
+
options: { optionId: string; name: string; kind: string }[];
|
|
25
|
+
}
|
|
26
|
+
export interface AcpPermissionReply { generation: string; requestId: string; optionId: string | null }
|
package/src/model/agent.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { AcpBackend, AcpModelSelection, AcpSessionReference } from "./acp.js";
|
|
2
|
+
|
|
3
|
+
export interface AgentStartOptions extends AgentOptions { backend?: AcpBackend }
|
|
1
4
|
export interface AgentOptions {
|
|
2
5
|
name?: string; message?: string; files?: string[]; prompts?: string[];
|
|
3
6
|
provider?: string; model?: string; thinking?: string; cwd?: string; tools?: string[];
|
|
@@ -11,7 +14,8 @@ export interface AgentDescriptor {
|
|
|
11
14
|
/** null means no conversation input has been handed to Pi yet. */
|
|
12
15
|
export interface AgentSessionRef { file: string | null }
|
|
13
16
|
/** Native references are interpreted only by their owning host adapter. */
|
|
14
|
-
export type
|
|
17
|
+
export type NativeAgentHost = "pi" | "dsh";
|
|
18
|
+
export type AgentSessionReference = { host: "pi"; file: string | null } | { host: "dsh"; sessionId: string } | AcpSessionReference;
|
|
15
19
|
export interface AgentLaunch { generation: string; name: string; sequence: number; stop: boolean; startedAt: number }
|
|
16
20
|
export interface AgentMessage { requestId: string; generation: string; sequence: number; message: string; delivery: "steer" | "followUp"; files: string[] }
|
|
17
21
|
export interface AgentCycle {
|
|
@@ -26,6 +30,8 @@ export interface AgentStatus {
|
|
|
26
30
|
}
|
|
27
31
|
export interface AgentView extends AgentDescriptor {
|
|
28
32
|
name: string; generation: string; state: AgentStatus["state"]; activity: string;
|
|
33
|
+
backend?: AcpBackend; transport?: "native" | "acp"; selection?: AcpModelSelection;
|
|
34
|
+
capabilities?: { steer: boolean; resume: boolean; team: boolean; transcript: boolean };
|
|
29
35
|
cycle?: AgentCycle; error?: string; owned: boolean;
|
|
30
36
|
}
|
|
31
37
|
export interface AgentCompletion { agentId: string; name: string; cycle: AgentCycle; report: string }
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import type { AcpCommands } from "./acp.js";
|
|
2
|
+
|
|
1
3
|
export interface WorkspaceConfig {
|
|
2
4
|
enabled?: boolean;
|
|
5
|
+
acp?: AcpCommands;
|
|
3
6
|
}
|
|
4
7
|
|
|
5
8
|
export interface WorkspaceConfiguration {
|
|
@@ -7,4 +10,5 @@ export interface WorkspaceConfiguration {
|
|
|
7
10
|
present: boolean;
|
|
8
11
|
enabled: boolean;
|
|
9
12
|
error?: string;
|
|
13
|
+
acp?: AcpCommands;
|
|
10
14
|
}
|
package/src/pbot.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { PiAgents, agentDirectory } from "./integrations/pi/pi-agents.js";
|
|
3
|
+
import { AgentBackends } from "./integrations/acp/agents.js";
|
|
3
4
|
import { PythonEnvironmentService } from "./services/workspace/python-environment-service.js";
|
|
4
5
|
import { AgentService } from "./services/agents/agent-service.js";
|
|
5
6
|
import { TeamService } from "./services/agents/team-service.js";
|
|
@@ -20,8 +21,8 @@ export default function pbot(pi: ExtensionAPI) {
|
|
|
20
21
|
// The explicitly loaded child entry owns the child; normal discovery never grants main privileges.
|
|
21
22
|
if (process.env.PBOT_SUBAGENT_RUN_ID) return;
|
|
22
23
|
const confirmations = new Confirmations(), registry = new ToolRegistry(pi);
|
|
23
|
-
const lifecycle = new Lifecycle(root => {
|
|
24
|
-
const agents = new AgentService(root, agentDirectory(), new PiAgents());
|
|
24
|
+
const lifecycle = new Lifecycle((root, configuration) => {
|
|
25
|
+
const agents = new AgentService(root, agentDirectory(), new AgentBackends(new PiAgents(), agentDirectory(), configuration.acp));
|
|
25
26
|
const history = new HistoryService(root, new HistoryFiles(join(root, ".pbot/history")), new PiHistory(root, () =>
|
|
26
27
|
agents.files.list().filter(agent => agent.workspace === root).flatMap(agent => {
|
|
27
28
|
const file = agents.files.optional(agent.agentId, "session-ref.json", sessionRef)?.file;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
+
import type { AgentDescriptor, AgentMessage } from "../../model/agent.js";
|
|
4
|
+
import type { AcpAnswer, AcpPermission } from "../../model/acp.js";
|
|
5
|
+
import { AgentFiles, launch } from "../../storage/agent-files.js";
|
|
6
|
+
import { acpAnswer, acpInput, acpPermission, acpPermissionReply, acpReport } from "../../storage/acp-files.js";
|
|
7
|
+
import { AgentRun } from "./agent-run.js";
|
|
8
|
+
|
|
9
|
+
export interface AcpProgress { kind: "text" | "thought" | "tool" | "activity"; text?: string; messageId?: string; activity?: string }
|
|
10
|
+
export class AcpAnswerBuffer {
|
|
11
|
+
#text = "";
|
|
12
|
+
#messageId: string | undefined;
|
|
13
|
+
get text() { return this.#text; }
|
|
14
|
+
reset() { this.#text = ""; this.#messageId = undefined; }
|
|
15
|
+
update(value: AcpProgress) {
|
|
16
|
+
if (value.kind === "tool") this.reset();
|
|
17
|
+
if (value.kind !== "text") return;
|
|
18
|
+
if (value.messageId && value.messageId !== this.#messageId) this.#text = "";
|
|
19
|
+
this.#messageId = value.messageId; this.#text += value.text ?? "";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export interface AcpWork {
|
|
23
|
+
readonly signal: AbortSignal;
|
|
24
|
+
prompt(request: AgentMessage): Promise<{ stopReason: string }>;
|
|
25
|
+
cancel(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** ACP prompt completion is the receipt boundary; final reports publish before completions. */
|
|
29
|
+
export class AcpAgentRun {
|
|
30
|
+
readonly execution: AgentRun;
|
|
31
|
+
readonly #answer = new AcpAnswerBuffer();
|
|
32
|
+
#lastActivity = 0;
|
|
33
|
+
#stopping = false;
|
|
34
|
+
#work: Promise<void> | undefined;
|
|
35
|
+
#stop: Promise<void> | undefined;
|
|
36
|
+
#failure: unknown;
|
|
37
|
+
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);
|
|
40
|
+
}
|
|
41
|
+
private get id() { return this.descriptor.agentId; }
|
|
42
|
+
private current() { return this.files.read(this.id, "launch.json", launch).generation === this.generation; }
|
|
43
|
+
update(value: AcpProgress) {
|
|
44
|
+
if (!this.execution.current || !this.current()) return;
|
|
45
|
+
this.#answer.update(value);
|
|
46
|
+
if (Date.now() - this.#lastActivity >= 500) {
|
|
47
|
+
this.#lastActivity = Date.now();
|
|
48
|
+
this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: value.activity ?? (value.kind === "text" ? "回答" : "思考") });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
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.write(this.id, "permissions/" + requestId + ".json", request, acpPermission, true);
|
|
54
|
+
this.execution.save({ state: "busy", activity: "等待确认" });
|
|
55
|
+
while (!signal.aborted && !this.#stopping && this.current() && !this.files.read(this.id, "launch.json", launch).stop) {
|
|
56
|
+
const reply = this.files.optional(this.id, "permissions/" + requestId + "-reply.json", acpPermissionReply);
|
|
57
|
+
if (reply?.generation === this.generation) return reply.optionId;
|
|
58
|
+
await delay(100);
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
async run() {
|
|
63
|
+
this.execution.recover(); this.execution.save({ state: "idle", activity: "空闲" });
|
|
64
|
+
while (!this.#stopping && this.current() && !this.work.signal.aborted) {
|
|
65
|
+
this.files.check(); if (this.#failure) throw this.#failure;
|
|
66
|
+
if (this.files.read(this.id, "launch.json", launch).stop) { await this.stop(); break; }
|
|
67
|
+
if (!this.#work) {
|
|
68
|
+
const request = this.files.messages(this.id).find(message => message.generation === this.generation && !this.execution.accepted(message.requestId));
|
|
69
|
+
if (request) {
|
|
70
|
+
const pending = this.perform(request).finally(() => { if (this.#work === pending) this.#work = undefined; });
|
|
71
|
+
this.#work = pending;
|
|
72
|
+
void pending.catch(error => { this.#failure = error; });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
await delay(100);
|
|
76
|
+
}
|
|
77
|
+
if (!this.current() && !this.#stopping) await this.stop();
|
|
78
|
+
if (!this.#stopping) await this.#work;
|
|
79
|
+
if (this.work.signal.aborted && !this.#stopping) throw this.work.signal.reason;
|
|
80
|
+
}
|
|
81
|
+
private async perform(request: AgentMessage) {
|
|
82
|
+
this.#answer.reset();
|
|
83
|
+
this.execution.claim(request.requestId); this.execution.enter(request.requestId); this.execution.begin();
|
|
84
|
+
this.files.write(this.id, "acp-input.json", { generation: this.generation, requestId: request.requestId }, acpInput);
|
|
85
|
+
let result: AcpAnswer;
|
|
86
|
+
try {
|
|
87
|
+
const end = await this.work.prompt(request);
|
|
88
|
+
let snapshot: AcpAnswer | undefined;
|
|
89
|
+
if (this.exactAnswer) {
|
|
90
|
+
snapshot = this.files.optional(this.id, "acp-answer.json", acpAnswer);
|
|
91
|
+
if (snapshot?.generation !== this.generation || snapshot.requestId !== request.requestId) snapshot = undefined;
|
|
92
|
+
}
|
|
93
|
+
const answer = this.exactAnswer ? snapshot?.answer ?? "" : this.#answer.text;
|
|
94
|
+
const state = end.stopReason === "cancelled" || this.#stopping ? "cancelled" : end.stopReason === "end_turn" &&
|
|
95
|
+
(!this.exactAnswer || snapshot?.state === "completed") && answer.trim() ? "completed" : "failed";
|
|
96
|
+
if (state === "completed") this.execution.consume(request.requestId);
|
|
97
|
+
result = { generation: this.generation, requestId: request.requestId, state, answer,
|
|
98
|
+
...(state === "failed" ? { error: snapshot?.error ?? "ACP 工作未产生有效最终回答:" + end.stopReason } : {}) };
|
|
99
|
+
} catch (error) {
|
|
100
|
+
result = { generation: this.generation, requestId: request.requestId, answer: this.#answer.text,
|
|
101
|
+
state: this.#stopping || this.files.read(this.id, "launch.json", launch).stop ? "cancelled" : "failed", error: String(error) };
|
|
102
|
+
}
|
|
103
|
+
if (!this.current()) return;
|
|
104
|
+
const cycleId = this.execution.current!.cycleId;
|
|
105
|
+
this.files.write(this.id, "reports/" + cycleId + ".json", { generation: this.generation, cycleId, text: result.answer }, acpReport, true);
|
|
106
|
+
this.execution.finish({ state: this.work.signal.aborted && !this.#stopping ? "interrupted" : result.state,
|
|
107
|
+
position: "acp:" + cycleId, answer: result.answer, ...(result.error ? { error: result.error } : {}) });
|
|
108
|
+
}
|
|
109
|
+
settled() { return this.#work; }
|
|
110
|
+
stop(): Promise<void> {
|
|
111
|
+
return this.#stop ??= (async () => {
|
|
112
|
+
this.#stopping = true;
|
|
113
|
+
if (this.current()) this.execution.save({ state: "stopping", activity: "停止" });
|
|
114
|
+
const pending = this.#work;
|
|
115
|
+
if (!pending) return;
|
|
116
|
+
const grace = new AbortController();
|
|
117
|
+
try {
|
|
118
|
+
const cancelAndSettle = this.work.cancel().catch(() => {}).then(() => pending);
|
|
119
|
+
await Promise.race([cancelAndSettle, delay(4000, undefined, { signal: grace.signal })]);
|
|
120
|
+
} finally { grace.abort(); }
|
|
121
|
+
})();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { realpathSync } from "node:fs";
|
|
2
|
+
import { realpathSync, readdirSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { packageVersion, releaseLine } from "../../common/version.js";
|
|
5
5
|
import { requireThat } from "../../common/validation.js";
|
|
6
6
|
import { AgentFiles, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
|
|
7
7
|
import { TeamFiles } from "../../storage/team-files.js";
|
|
8
8
|
import type { TeamAgent, TeamInput } from "../../model/team.js";
|
|
9
|
-
import type { AgentOwner, AgentOptions, AgentDescriptor, AgentCycle, AgentCompletion, AgentSessionReference } from "../../model/agent.js";
|
|
9
|
+
import type { AgentOwner, AgentOptions, AgentStartOptions, AgentDescriptor, AgentCycle, AgentCompletion, AgentSessionReference, NativeAgentHost } from "../../model/agent.js";
|
|
10
|
+
import type { AcpBackend, AcpSessionReference } from "../../model/acp.js";
|
|
11
|
+
import { acpModelState, acpPermission, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
|
|
10
12
|
|
|
11
13
|
export interface AgentHost {
|
|
12
|
-
readonly kind:
|
|
13
|
-
available(): void;
|
|
14
|
+
readonly kind: NativeAgentHost;
|
|
15
|
+
available(backend?: AcpBackend): void;
|
|
16
|
+
prepare?(backend: AcpBackend, options: AgentOptions): AcpSessionReference;
|
|
17
|
+
validateOptions?(options: AgentOptions, backend?: AcpBackend): void;
|
|
14
18
|
alive(id: string): boolean;
|
|
15
19
|
validateSession(value: AgentDescriptor, reference: AgentSessionReference): void | Promise<void>;
|
|
16
20
|
launch(value: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, resume: boolean, signal: AbortSignal): void | Promise<void>;
|
|
@@ -51,15 +55,28 @@ export class AgentService {
|
|
|
51
55
|
const current = saved?.generation === run.generation ? saved : undefined;
|
|
52
56
|
const alive = this.alive(d.agentId), pending = this.#starting.get(d.agentId);
|
|
53
57
|
const starting = pending && !pending.controller.signal.aborted;
|
|
58
|
+
const reference = this.files.optional(d.agentId, "acp-session.json", acpReference);
|
|
59
|
+
const model = reference ? this.files.optional(d.agentId, "acp-model.json", acpModelState) : undefined;
|
|
60
|
+
const selection = model?.generation === run.generation ? { ...(model.provider ? { provider: model.provider } : {}),
|
|
61
|
+
...(model.model ? { model: model.model } : {}), ...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
|
|
62
|
+
...(model.thinking ? { thinking: model.thinking } : {}) } : undefined;
|
|
54
63
|
return { ...d, name: run.name, generation: run.generation, owned: this.owned(d, owner), alive,
|
|
64
|
+
...(reference?.host === "acp" ? { backend: reference.backend, transport: "acp" as const,
|
|
65
|
+
capabilities: { steer: false, resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load), team: false, transcript: false },
|
|
66
|
+
...(selection ? { selection } : {}) } : {}),
|
|
55
67
|
state: alive ? starting ? "starting" : run.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
|
|
56
68
|
activity: starting ? "启动" : alive ? current?.activity ?? "启动" : current?.error ?? "已停止",
|
|
57
69
|
...(current?.cycle ? { cycle: current.cycle } : {}), ...(current?.error ? { error: current.error } : {}) };
|
|
58
70
|
}
|
|
59
|
-
async start(
|
|
60
|
-
this.check();
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
async start(input: AgentStartOptions, owner: AgentOwner) {
|
|
72
|
+
this.check();
|
|
73
|
+
const { backend, ...supplied } = input;
|
|
74
|
+
this.host.available(backend);
|
|
75
|
+
const options: AgentOptions = Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined));
|
|
76
|
+
this.host.validateOptions?.(options, backend);
|
|
77
|
+
requireThat(backend || options.provider && options.model, "请选择可在专家会话使用的模型。");
|
|
78
|
+
const reference = backend ? this.host.prepare?.(backend, options) : undefined;
|
|
79
|
+
requireThat(!backend || reference, "当前执行接入不支持 ACP 后端。");
|
|
63
80
|
const prepared = this.files.control(() => {
|
|
64
81
|
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中子会话;请先停止一个空闲会话。");
|
|
65
82
|
const agentId = randomUUID();
|
|
@@ -68,7 +85,7 @@ export class AgentService {
|
|
|
68
85
|
const d: AgentDescriptor = { agentId, release: packageVersion, workspace: this.root, cwd,
|
|
69
86
|
parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd } };
|
|
70
87
|
const run = { generation: randomUUID(), name: options.name ?? agentId.slice(0, 8), sequence: 0, stop: false, startedAt: Date.now() };
|
|
71
|
-
this.files.create(d, run);
|
|
88
|
+
this.files.create(d, run, reference);
|
|
72
89
|
const initial = [options.message, ...(options.prompts ?? [])].filter(Boolean).join("\n\n");
|
|
73
90
|
const request = initial || files.length ? this.files.enqueue(agentId, initial, "steer", files) : undefined;
|
|
74
91
|
this.#starting.set(agentId, { generation: run.generation, controller: new AbortController(), stage: "launch" });
|
|
@@ -99,12 +116,15 @@ export class AgentService {
|
|
|
99
116
|
this.#launches.add(operation);
|
|
100
117
|
try { await operation; } finally { this.#launches.delete(operation); }
|
|
101
118
|
}
|
|
102
|
-
send(id: string, text: string, delivery: "steer" | "followUp", owner: AgentOwner) {
|
|
119
|
+
send(id: string, text: string, delivery: "steer" | "followUp", owner: AgentOwner, attachments: readonly string[] = []) {
|
|
103
120
|
const d = this.get(id, owner);
|
|
104
121
|
return this.files.control(() => {
|
|
105
122
|
requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "子会话已停止或仍在恢复校验,请等待显式恢复完成。");
|
|
106
123
|
this.assertPrivate(id);
|
|
107
|
-
const
|
|
124
|
+
const view = this.view(d, owner);
|
|
125
|
+
requireThat(!(view.transport === "acp" && delivery === "steer" && view.state !== "idle"), "该子代理运行中不支持 steer;请用 delivery: followUp 排队。");
|
|
126
|
+
const files = attachments.map(path => realpathSync(resolve(this.root, path)));
|
|
127
|
+
const request = this.files.enqueue(id, text, delivery, files);
|
|
108
128
|
return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" as const };
|
|
109
129
|
});
|
|
110
130
|
}
|
|
@@ -160,7 +180,8 @@ export class AgentService {
|
|
|
160
180
|
return this.restart(id, owner, text);
|
|
161
181
|
}
|
|
162
182
|
private async restart(id: string, owner: AgentOwner, text?: string) {
|
|
163
|
-
const d = this.get(id, owner)
|
|
183
|
+
const d = this.get(id, owner), reference = this.files.reference(id);
|
|
184
|
+
this.host.available(reference.host === "acp" ? reference.backend : undefined);
|
|
164
185
|
const reserved = this.files.control(() => {
|
|
165
186
|
requireThat(!this.alive(id), "子会话仍在运行。");
|
|
166
187
|
this.assertPrivate(id);
|
|
@@ -240,6 +261,58 @@ export class AgentService {
|
|
|
240
261
|
if (c.leaf === null) return "";
|
|
241
262
|
return this.host.report(d, this.files.reference(d.agentId), c.leaf);
|
|
242
263
|
}
|
|
264
|
+
async recordPage(id: string, owner: AgentOwner, options: { seq?: number; offset?: number; limit?: number; textOffset?: number; textLimit?: number; format?: string } = {}) {
|
|
265
|
+
const d = this.get(id, owner), view = this.view(d, owner);
|
|
266
|
+
requireThat(view.transport === "acp", "该记录入口用于 ACP 成员。");
|
|
267
|
+
const messages = this.files.messages(id), cycles = this.cycles(id, owner).sort((a, b) => a.startedAt - b.startedAt);
|
|
268
|
+
const records: { seq: number; type: string; time: number; text: string; requestId?: string; cycleId?: string; state: string }[] = [];
|
|
269
|
+
const included = new Set<string>();
|
|
270
|
+
for (const c of cycles) {
|
|
271
|
+
for (const requestId of c.requestIds) {
|
|
272
|
+
const request = messages.find(message => message.requestId === requestId);
|
|
273
|
+
if (!request || included.has(requestId)) continue;
|
|
274
|
+
included.add(requestId);
|
|
275
|
+
records.push({ seq: records.length, type: "acp/input", time: c.startedAt, requestId, state: c.state,
|
|
276
|
+
text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
|
|
277
|
+
}
|
|
278
|
+
const report = await this.report(d, c); this.check();
|
|
279
|
+
records.push({ seq: records.length, type: "acp/report", time: c.finishedAt ?? c.startedAt, cycleId: c.cycleId, state: c.state,
|
|
280
|
+
text: [c.error, report].filter(Boolean).join("\n\n") });
|
|
281
|
+
}
|
|
282
|
+
const run = this.files.read(id, "launch.json", launch);
|
|
283
|
+
for (const request of messages.filter(message => message.generation === run.generation && !included.has(message.requestId))) {
|
|
284
|
+
records.push({ seq: records.length, type: "acp/input", time: run.startedAt, requestId: request.requestId, state: "queued",
|
|
285
|
+
text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
|
|
286
|
+
}
|
|
287
|
+
const cursor = run.generation + ":" + records.length + ":" + cycles.at(-1)?.state;
|
|
288
|
+
if (options.seq !== undefined) {
|
|
289
|
+
const record = records[options.seq]; requireThat(record, "ACP 记录位置不存在。");
|
|
290
|
+
const text = options.format === "json" ? JSON.stringify(record, null, 2) : record.text;
|
|
291
|
+
const offset = options.textOffset ?? 0, end = offset + (options.textLimit ?? 12000);
|
|
292
|
+
requireThat(offset <= text.length, "ACP 记录分页位置超出范围。");
|
|
293
|
+
return { kind: "acp", agent: view, cursor, seq: record.seq, offset, text: text.slice(offset, end), totalLength: text.length, nextOffset: end < text.length ? end : null };
|
|
294
|
+
}
|
|
295
|
+
const limit = options.limit ?? 30, offset = options.offset ?? Math.max(0, records.length - limit);
|
|
296
|
+
requireThat(offset <= records.length, "ACP 记录分页位置超出范围。");
|
|
297
|
+
return { kind: "acp", agent: view, cursor, total: records.length, offset, nextOffset: offset + limit < records.length ? offset + limit : null,
|
|
298
|
+
records: records.slice(offset, offset + limit).map(record => { const { text, ...fields } = record; return { ...fields, preview: text.slice(0, 800) }; }) };
|
|
299
|
+
}
|
|
300
|
+
pendingPermissions(owner: AgentOwner) {
|
|
301
|
+
return this.list(owner).filter(view => view.owned && view.alive && view.transport === "acp").flatMap(view =>
|
|
302
|
+
readdirSync(resolve(this.files.directory(view.agentId), "permissions")).filter(name => name.endsWith(".json") && !name.endsWith("-reply.json")).flatMap(name => {
|
|
303
|
+
const request = this.files.read(view.agentId, "permissions/" + name, acpPermission);
|
|
304
|
+
if (request.generation !== view.generation || this.files.optional(view.agentId, "permissions/" + request.requestId + "-reply.json", acpPermissionReply)) return [];
|
|
305
|
+
return [{ ...request, agentId: view.agentId, name: view.name }];
|
|
306
|
+
})).slice(0, 32);
|
|
307
|
+
}
|
|
308
|
+
answerPermission(id: string, requestId: string, optionId: string | null, owner: AgentOwner) {
|
|
309
|
+
this.get(id, owner);
|
|
310
|
+
const request = this.files.read(id, "permissions/" + requestId + ".json", acpPermission);
|
|
311
|
+
const run = this.files.read(id, "launch.json", launch);
|
|
312
|
+
requireThat(run.generation === request.generation && !run.stop && this.alive(id), "ACP 确认请求已失效。");
|
|
313
|
+
requireThat(optionId === null || request.options.some(option => option.optionId === optionId), "ACP 确认选项无效。");
|
|
314
|
+
this.files.write(id, "permissions/" + requestId + "-reply.json", { generation: run.generation, requestId, optionId }, acpPermissionReply, true);
|
|
315
|
+
}
|
|
243
316
|
private release(id: string) {
|
|
244
317
|
let pending = this.#releasing.get(id);
|
|
245
318
|
if (!pending) {
|
|
@@ -108,7 +108,8 @@ export class TeamService {
|
|
|
108
108
|
if (changed) this.files.save(team);
|
|
109
109
|
}
|
|
110
110
|
pauseAll(owner: AgentOwner, reason: string) {
|
|
111
|
-
|
|
111
|
+
if (!this.records(owner.sessionId).length) return;
|
|
112
|
+
this.agents.control(() => { for (const team of this.records(owner.sessionId)) this.pauseRecord(team, reason); });
|
|
112
113
|
}
|
|
113
114
|
recover(owner: AgentOwner) {
|
|
114
115
|
if (this.#recovered.has(owner.sessionId)) return;
|
|
@@ -201,7 +202,7 @@ export class TeamService {
|
|
|
201
202
|
});
|
|
202
203
|
}
|
|
203
204
|
advance(owner: AgentOwner, views: TeamAgent[], completions: AgentCompletion[]): TeamInput[] {
|
|
204
|
-
this.
|
|
205
|
+
if (!this.records(owner.sessionId).length) return [];
|
|
205
206
|
return this.agents.control(() => {
|
|
206
207
|
const inputs: TeamInput[] = [], records = this.records(owner.sessionId), links = this.files.links(records);
|
|
207
208
|
for (const team of records) {
|
|
@@ -7,6 +7,14 @@ function config(value: unknown): Record<string, unknown> {
|
|
|
7
7
|
if (!object(value) || value.enabled !== undefined && typeof value.enabled !== "boolean") {
|
|
8
8
|
throw new Error("配置必须是 JSON 对象,enabled 必须是布尔值。");
|
|
9
9
|
}
|
|
10
|
+
if (value.acp !== undefined) {
|
|
11
|
+
if (!object(value.acp) || Object.keys(value.acp).some(key => !["pi", "dsh", "claude"].includes(key))) throw new Error("acp 仅接受 pi、dsh 和 claude 启动配置。");
|
|
12
|
+
for (const command of Object.values(value.acp)) {
|
|
13
|
+
if (!object(command) || typeof command.command !== "string" || !command.command.trim() || command.command.includes("\0") ||
|
|
14
|
+
!Array.isArray(command.args) || command.args.some(arg => typeof arg !== "string" || arg.includes("\0")) ||
|
|
15
|
+
Object.keys(command).some(key => !["command", "args"].includes(key))) throw new Error("ACP 启动配置需要 command 和 args 字符串数组。");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
10
18
|
return value;
|
|
11
19
|
}
|
|
12
20
|
import type { WorkspaceConfig, WorkspaceConfiguration } from "../../model/workspace-config.js";
|
|
@@ -26,7 +34,8 @@ export async function readWorkspaceConfig(root: string): Promise<WorkspaceConfig
|
|
|
26
34
|
}
|
|
27
35
|
try {
|
|
28
36
|
const value = config(JSON.parse(content));
|
|
29
|
-
|
|
37
|
+
const settings = value as WorkspaceConfig;
|
|
38
|
+
return { path, present: true, enabled: settings.enabled === true, ...(settings.acp ? { acp: settings.acp } : {}) };
|
|
30
39
|
} catch (error) {
|
|
31
40
|
return { path, present: true, enabled: false, error: `工作区配置无效 ${path}: ${String(error)}` };
|
|
32
41
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import { object } from "../common/json.js";
|
|
3
|
+
import { requireThat } from "../common/validation.js";
|
|
4
|
+
import { fileId } from "./file-access.js";
|
|
5
|
+
import type { AcpAnswer, AcpInput, AcpPermission, AcpPermissionReply, AcpModelState, AcpProcess, AcpReport, AcpSessionReference } from "../model/acp.js";
|
|
6
|
+
|
|
7
|
+
const text = (value: unknown): value is string => typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
8
|
+
export function acpReference(value: unknown): AcpSessionReference {
|
|
9
|
+
requireThat(object(value) && value.host === "acp" && ["pi", "dsh", "claude"].includes(String(value.backend)) &&
|
|
10
|
+
text(value.command) && isAbsolute(value.command) && Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")) &&
|
|
11
|
+
(value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
|
|
12
|
+
const capabilities = value.capabilities;
|
|
13
|
+
if (capabilities !== undefined) requireThat(object(capabilities) &&
|
|
14
|
+
["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
|
|
15
|
+
return value as unknown as AcpSessionReference;
|
|
16
|
+
}
|
|
17
|
+
export function acpModelState(value: unknown): AcpModelState {
|
|
18
|
+
requireThat(object(value) && text(value.generation) && ["provider", "model", "resolvedModel", "thinking"].every(key => value[key] === undefined || text(value[key])), "ACP 模型选择无效。");
|
|
19
|
+
return value as unknown as AcpModelState;
|
|
20
|
+
}
|
|
21
|
+
export function acpProcess(value: unknown): AcpProcess {
|
|
22
|
+
requireThat(object(value) && text(value.generation) && Number.isSafeInteger(value.pid) && Number(value.pid) > 1 &&
|
|
23
|
+
typeof value.startedAt === "number" && Number.isFinite(value.startedAt), "ACP 进程记录无效。");
|
|
24
|
+
return value as unknown as AcpProcess;
|
|
25
|
+
}
|
|
26
|
+
export function acpInput(value: unknown): AcpInput {
|
|
27
|
+
requireThat(object(value) && text(value.generation) && text(value.requestId), "ACP 输入关联无效。");
|
|
28
|
+
fileId(value.requestId); return value as unknown as AcpInput;
|
|
29
|
+
}
|
|
30
|
+
export function acpAnswer(value: unknown): AcpAnswer {
|
|
31
|
+
acpInput(value);
|
|
32
|
+
requireThat(object(value) && typeof value.answer === "string" && ["completed", "failed", "cancelled"].includes(String(value.state)) &&
|
|
33
|
+
(value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
|
|
34
|
+
return value as unknown as AcpAnswer;
|
|
35
|
+
}
|
|
36
|
+
export function acpReport(value: unknown): AcpReport {
|
|
37
|
+
requireThat(object(value) && text(value.generation) && text(value.cycleId) && typeof value.text === "string", "ACP 周期报告无效。");
|
|
38
|
+
fileId(value.cycleId); return value as unknown as AcpReport;
|
|
39
|
+
}
|
|
40
|
+
export function acpPermission(value: unknown): AcpPermission {
|
|
41
|
+
acpInput(value);
|
|
42
|
+
requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option =>
|
|
43
|
+
object(option) && text(option.optionId) && text(option.name) && ["allow_once", "allow_always", "reject_once", "reject_always"].includes(String(option.kind))), "ACP 确认请求无效。");
|
|
44
|
+
return value as unknown as AcpPermission;
|
|
45
|
+
}
|
|
46
|
+
export function acpPermissionReply(value: unknown): AcpPermissionReply {
|
|
47
|
+
acpInput(value);
|
|
48
|
+
requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
|
|
49
|
+
return value as unknown as AcpPermissionReply;
|
|
50
|
+
}
|
|
@@ -5,7 +5,9 @@ import { canonical, object } from "../common/json.js";
|
|
|
5
5
|
import { 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 type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionRef, AgentSessionReference } from "../model/agent.js";
|
|
8
|
+
import type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionRef, AgentSessionReference, NativeAgentHost } from "../model/agent.js";
|
|
9
|
+
import type { AcpSessionReference } from "../model/acp.js";
|
|
10
|
+
import { acpReference } from "./acp-files.js";
|
|
9
11
|
|
|
10
12
|
const text = (v: unknown): v is string => typeof v === "string" && v.length > 0;
|
|
11
13
|
const number = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
|
|
@@ -46,7 +48,7 @@ export function status(v: unknown): AgentStatus {
|
|
|
46
48
|
}
|
|
47
49
|
export class AgentFiles {
|
|
48
50
|
#failure: unknown;
|
|
49
|
-
constructor(readonly root: string, readonly host:
|
|
51
|
+
constructor(readonly root: string, readonly host: NativeAgentHost = "pi") {}
|
|
50
52
|
check() { if (this.#failure) throw this.#failure; }
|
|
51
53
|
fail(error: unknown) { this.#failure = error; }
|
|
52
54
|
directory(id: string) { return join(this.root, fileId(id)); }
|
|
@@ -61,14 +63,18 @@ export class AgentFiles {
|
|
|
61
63
|
catch (error) { this.fail(error); throw error; }
|
|
62
64
|
requireThat(published, "子会话文件已存在。");
|
|
63
65
|
}
|
|
64
|
-
create(value: AgentDescriptor, run: AgentLaunch) {
|
|
66
|
+
create(value: AgentDescriptor, run: AgentLaunch, acp?: AcpSessionReference) {
|
|
65
67
|
this.check();
|
|
66
68
|
mkdirSync(this.root, { recursive: true, mode: 0o700 });
|
|
67
69
|
mkdirSync(this.directory(value.agentId), { mode: 0o700 });
|
|
68
70
|
for (const dir of ["inbox", "completions"]) privateDirectory(this.directory(value.agentId), dir);
|
|
69
71
|
this.write(value.agentId, "descriptor.json", value, descriptor, true);
|
|
70
72
|
this.write(value.agentId, "launch.json", run, launch, true);
|
|
71
|
-
if (
|
|
73
|
+
if (acp) {
|
|
74
|
+
for (const dir of ["reports", "permissions"]) privateDirectory(this.directory(value.agentId), dir);
|
|
75
|
+
this.write(value.agentId, "acp-session.json", acp, acpReference, true);
|
|
76
|
+
}
|
|
77
|
+
else if (this.host === "pi") this.write(value.agentId, "session-ref.json", { file: null }, sessionRef, true);
|
|
72
78
|
else this.write(value.agentId, "native-session.json", { host: "dsh", sessionId: value.agentId }, dshSessionRef, true);
|
|
73
79
|
}
|
|
74
80
|
list() {
|
|
@@ -86,6 +92,8 @@ export class AgentFiles {
|
|
|
86
92
|
.map(n => this.read(id, "completions/" + n, cycle)).sort((a, b) => (a.finishedAt ?? 0) - (b.finishedAt ?? 0));
|
|
87
93
|
}
|
|
88
94
|
reference(id: string): AgentSessionReference {
|
|
95
|
+
const acp = this.optional(id, "acp-session.json", acpReference);
|
|
96
|
+
if (acp) return acp;
|
|
89
97
|
if (this.host === "pi") return { host: "pi", file: this.session(id) };
|
|
90
98
|
const ref = this.read(id, "native-session.json", dshSessionRef);
|
|
91
99
|
requireThat(ref.sessionId === id, "dsh 原生会话身份不匹配。");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Static } from "typebox";
|
|
2
2
|
import type { AgentService } from "../../services/agents/agent-service.js";
|
|
3
|
-
import type { AgentOptions, AgentOwner, AgentView } from "../../model/agent.js";
|
|
3
|
+
import type { AgentOptions, AgentStartOptions, AgentOwner, AgentView } from "../../model/agent.js";
|
|
4
4
|
import { markdownResult } from "../result.js";
|
|
5
5
|
import type { agentParametersSchema, agentViewSchema } from "./contract.js";
|
|
6
6
|
|
|
@@ -11,6 +11,8 @@ function agentOutput(view: AgentView & Partial<AgentOutput>): AgentOutput {
|
|
|
11
11
|
return {
|
|
12
12
|
agentId: view.agentId, name: view.name, cwd: view.cwd, createdAt: view.createdAt,
|
|
13
13
|
owned: view.owned, state: view.state, activity: view.activity,
|
|
14
|
+
...(view.backend ? { backend: view.backend, transport: view.transport, capabilities: view.capabilities } : {}),
|
|
15
|
+
...(view.selection ? { selection: view.selection } : {}),
|
|
14
16
|
...(c ? { cycle: { cycleId: c.cycleId, startedAt: c.startedAt, state: c.state, requestIds: c.requestIds, source: c.source,
|
|
15
17
|
...(c.finishedAt === undefined ? {} : { finishedAt: c.finishedAt }), ...(c.error === undefined ? {} : { error: c.error }) } } : {}),
|
|
16
18
|
...(view.error === undefined ? {} : { error: view.error }),
|
|
@@ -29,7 +31,7 @@ export async function executeAgent(service: AgentService, input: AgentInput, own
|
|
|
29
31
|
switch (input.action) {
|
|
30
32
|
case "start": {
|
|
31
33
|
const { action: _action, ...supplied } = input;
|
|
32
|
-
value = await service.start({ ...defaults, ...supplied } as
|
|
34
|
+
value = await service.start({ ...(supplied.backend ? {} : defaults), ...supplied } as AgentStartOptions, owner); break;
|
|
33
35
|
}
|
|
34
36
|
case "list": return markdownResult({ agents: service.list(owner).map(agentOutput) }, "子会话列表");
|
|
35
37
|
case "send": value = service.send(input.agentId!, input.message!, input.delivery ?? "steer", owner); break;
|
|
@@ -6,25 +6,26 @@ const id = Type.String({ minLength: 1, maxLength: 128 });
|
|
|
6
6
|
const message = Type.String({ minLength: 1, maxLength: 24000, description: "本次目标、相关资料、约束和交付要求。" });
|
|
7
7
|
const name = Type.String({ minLength: 1, maxLength: 64, pattern: "^[^\\r\\n\\x00-\\x1f]+$" });
|
|
8
8
|
export const agentStartOptions = {
|
|
9
|
+
backend: Type.Optional(Type.Union([Type.Literal("pi"), Type.Literal("dsh"), Type.Literal("claude")], { description: "选择已安装并配置的子代理程序:pi、dsh 或 claude(Claude Code)。省略时使用当前宿主的原生专家;显式选择时使用目标程序自己的配置。团队成员须省略 backend。" })),
|
|
9
10
|
name: Type.Optional(name), message: Type.Optional(message),
|
|
10
11
|
files: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { maxItems: 32, description: "随初始消息读取的文件;相对路径基于原工作区根目录。" })),
|
|
11
12
|
prompts: Type.Optional(Type.Array(message, { maxItems: 32, description: "与 message 按顺序合并为同一初始消息。" })),
|
|
12
|
-
provider: Type.Optional(Type.String({ ...id, description: "
|
|
13
|
-
model: Type.Optional(Type.String({ ...id, description: "Pi
|
|
14
|
-
thinking: Type.Optional(Type.
|
|
13
|
+
provider: Type.Optional(Type.String({ ...id, description: "模型提供方。省略 backend 时默认继承主会话;指定 backend 时,此参数须与 model 一起提供。Claude 子代理省略此参数。" })),
|
|
14
|
+
model: Type.Optional(Type.String({ ...id, description: "模型 ID;指定 backend 时也可使用目标公开的完整模型值。默认值:省略 backend 时继承主会话,指定时使用目标配置。Pi/dsh 同名模型可用 provider 区分。" })),
|
|
15
|
+
thinking: Type.Optional(Type.String({ minLength: 1, maxLength: 128, description: "思考等级 ID,可单独指定。默认值:省略 backend 时继承主会话,指定时使用目标配置。Pi 使用 off/minimal/low/medium/high/xhigh/max,其他程序使用其原生等级 ID。" })),
|
|
15
16
|
cwd: Type.Optional(Type.String({ minLength: 1, description: "执行目录,相对原工作区根目录;省略时使用工作区根目录。" })),
|
|
16
|
-
tools: Type.Optional(Type.Array(id, { minItems: 1, description: "
|
|
17
|
-
noExtensions: Type.Optional(Type.Boolean({ description: "
|
|
18
|
-
noSkills: Type.Optional(Type.Boolean({ description: "
|
|
19
|
-
noPromptTemplates: Type.Optional(Type.Boolean({ description: "
|
|
20
|
-
noContextFiles: Type.Optional(Type.Boolean({ description: "
|
|
17
|
+
tools: Type.Optional(Type.Array(id, { minItems: 1, description: "Pi/dsh 子代理的工具名;省略时使用所选程序默认工具。Claude 子代理省略此参数并沿用原生配置。" })),
|
|
18
|
+
noExtensions: Type.Optional(Type.Boolean({ description: "仅用于 Pi 子代理:关闭其他扩展的自动发现,默认 true。" })),
|
|
19
|
+
noSkills: Type.Optional(Type.Boolean({ description: "Pi/dsh 子代理关闭技能发现,默认 false;Claude 子代理省略此参数并沿用原生配置。" })),
|
|
20
|
+
noPromptTemplates: Type.Optional(Type.Boolean({ description: "仅用于 Pi 子代理:关闭提示模板发现,默认 false。" })),
|
|
21
|
+
noContextFiles: Type.Optional(Type.Boolean({ description: "仅用于 Pi 子代理:关闭原生上下文文件加载,默认 false。" })),
|
|
21
22
|
};
|
|
22
23
|
const page = {
|
|
23
24
|
cycleId: Type.Optional(Type.String({ ...id, description: "仅 read:省略时读取最近周期;按 nextOffset 续读时须固定为上页返回的 cycle.cycleId,避免切换到新周期。" })),
|
|
24
25
|
offset: Type.Optional(Type.Integer({ minimum: 0, description: "仅 read:报告正文的 UTF-16 字符偏移,默认 0;续读使用上页 nextOffset。" })),
|
|
25
26
|
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 16000, description: "仅 read:最多返回的正文 UTF-16 字符数,默认 16000,不是消息数或 token 数。" })),
|
|
26
27
|
};
|
|
27
|
-
const delivery = Type.Optional(Type.Union([Type.Literal("steer"), Type.Literal("followUp")], { description: "send 默认 steer 补充当前工作;followUp 排到当前工作之后。" }));
|
|
28
|
+
const delivery = Type.Optional(Type.Union([Type.Literal("steer"), Type.Literal("followUp")], { description: "send 默认 steer 补充当前工作;followUp 排到当前工作之后。capabilities.steer 为 false 的成员运行中使用 followUp。" }));
|
|
28
29
|
export const agentParametersSchema = Type.Object({
|
|
29
30
|
action: Type.Union([Type.Literal("start"), Type.Literal("send"), Type.Literal("rename"), Type.Literal("status"), Type.Literal("list"), Type.Literal("read"), Type.Literal("stop"), Type.Literal("resume")], {
|
|
30
31
|
description: "start 仅接受启动选项,可省略 message/prompts/files 创建待命会话,禁止 agentId/delivery/分页字段;list 只接受 action;status/stop 只接受并必填 agentId;send 必填 agentId/message,仅可选 delivery;resume 必填 agentId/message;rename 必填 agentId/name;read 必填 agentId,仅可选 cycleId/offset/limit。禁止携带其他 action 的字段。",
|
|
@@ -47,6 +48,12 @@ const cycle = Type.Object({ cycleId: Type.String(), startedAt: Type.Number(), fi
|
|
|
47
48
|
source: Type.Union([Type.Literal("parent"), Type.Literal("interactive"), Type.Literal("mixed")]), error: Type.Optional(Type.String()),
|
|
48
49
|
}, { additionalProperties: false });
|
|
49
50
|
export const agentViewSchema = Type.Object({ agentId: Type.String(), name: Type.String(), cwd: Type.String(), createdAt: Type.Number(),
|
|
51
|
+
backend: Type.Optional(Type.Union([Type.Literal("pi"), Type.Literal("dsh"), Type.Literal("claude")])),
|
|
52
|
+
transport: Type.Optional(Type.Union([Type.Literal("native"), Type.Literal("acp")])),
|
|
53
|
+
selection: Type.Optional(Type.Object({ provider: Type.Optional(Type.String()), model: Type.Optional(Type.String()),
|
|
54
|
+
resolvedModel: Type.Optional(Type.String({ description: "目标原生会话报告的模型 ID,可用于识别配置别名。" })), thinking: Type.Optional(Type.String()) },
|
|
55
|
+
{ additionalProperties: false, description: "子代理实际模型配置与有效思考等级,包含默认值。" })),
|
|
56
|
+
capabilities: Type.Optional(Type.Object({ steer: Type.Boolean(), resume: Type.Boolean(), team: Type.Boolean(), transcript: Type.Boolean() }, { additionalProperties: false })),
|
|
50
57
|
owned: Type.Boolean({ description: "是否属于当前父会话分支;false 时须回到所属分支操作。" }),
|
|
51
58
|
state: Type.Union([Type.Literal("starting"), Type.Literal("busy"), Type.Literal("idle"), Type.Literal("stopping"), Type.Literal("stopped"), Type.Literal("error")], { description: "执行状态;idle 不代表上轮工作成功,结果见 cycle.state。" }),
|
|
52
59
|
activity: Type.String(), cycle: Type.Optional(cycle), error: Type.Optional(Type.String()),
|