personal-bot 0.2.20 → 0.3.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 +3 -3
- package/.claude-plugin/plugin.json +2 -5
- package/.mcp.json +11 -0
- package/CONTEXT.md +17 -73
- package/README.md +41 -120
- package/README.zh-CN.md +41 -120
- package/docs/README.md +31 -23
- package/docs/architecture/design.md +74 -0
- package/docs/architecture/extensibility.md +78 -0
- package/docs/architecture/model-context.md +31 -0
- package/docs/architecture/model-instruction-guidelines.md +34 -0
- package/docs/architecture/principles.md +33 -0
- package/docs/guides/capabilities.md +36 -0
- package/docs/guides/cross-harness-workers.md +83 -0
- package/docs/guides/history.md +25 -0
- package/docs/guides/workspace-configuration.md +102 -0
- package/docs/hosts/antigravity.md +52 -0
- package/docs/hosts/claude-code.md +47 -0
- package/docs/hosts/dsh.md +67 -0
- package/docs/hosts/pi.md +49 -0
- package/docs/maintainers/release.md +59 -0
- package/hooks/hooks.json +0 -3
- package/lib/antigravity-plugin/hooks.json +23 -0
- package/lib/antigravity-plugin/lib/claude.d.ts +1 -0
- package/lib/antigravity-plugin/lib/claude.js +13 -0
- package/lib/antigravity-plugin/lib/common/json.d.ts +6 -0
- package/lib/antigravity-plugin/lib/common/json.js +16 -0
- package/lib/antigravity-plugin/lib/common/paths.d.ts +1 -0
- package/lib/antigravity-plugin/lib/common/paths.js +2 -0
- package/lib/antigravity-plugin/lib/common/validation.d.ts +3 -0
- package/lib/antigravity-plugin/lib/common/validation.js +8 -0
- package/lib/antigravity-plugin/lib/common/version.d.ts +3 -0
- package/lib/antigravity-plugin/lib/common/version.js +13 -0
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +11597 -0
- package/lib/antigravity-plugin/lib/extension/mcp.js +2715 -0
- package/lib/antigravity-plugin/lib/extension/plugin-runtime.d.ts +10 -0
- package/lib/antigravity-plugin/lib/extension/plugin-runtime.js +67 -0
- package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +16 -0
- package/lib/antigravity-plugin/lib/integrations/agent-resources.js +56 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.d.ts +4 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.js +79 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/claude.d.ts +3 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/claude.js +60 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/registry.d.ts +15 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/registry.js +34 -0
- package/lib/antigravity-plugin/lib/model/acp.d.ts +164 -0
- package/lib/antigravity-plugin/lib/model/acp.js +12 -0
- package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +48 -0
- package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +24 -0
- package/lib/antigravity-plugin/lib/model/workspace-config.js +1 -0
- package/lib/antigravity-plugin/lib/model/workspace-mcp.d.ts +51 -0
- package/lib/antigravity-plugin/lib/model/workspace-mcp.js +4 -0
- package/lib/antigravity-plugin/lib/plugin.d.ts +1 -0
- package/lib/antigravity-plugin/lib/plugin.js +17 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.d.ts +8 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +93 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-preparation.d.ts +3 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-preparation.js +26 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +8 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +91 -0
- package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +11 -0
- package/lib/antigravity-plugin/lib/storage/atomic-json.js +101 -0
- package/lib/antigravity-plugin/mcp_config.json +13 -0
- package/lib/antigravity-plugin/package.json +6 -0
- package/lib/antigravity-plugin/plugin.json +7 -0
- package/lib/antigravity-plugin/resources/agents/coordinator.md +11 -0
- package/{resources/claude → lib/antigravity-plugin/resources}/agents/general-purpose.md +0 -5
- package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
- package/lib/antigravity-plugin/resources/prompts/workspace.md +31 -0
- package/lib/antigravity-plugin/resources/references/compute-finance.md +22 -0
- package/lib/antigravity-plugin/resources/references/compute.md +16 -0
- package/lib/antigravity-plugin/resources/references/task-conductor.md +25 -0
- package/lib/antigravity-plugin/resources/templates/workspace-agents.md +21 -0
- package/lib/antigravity-plugin/skills/pbot/SKILL.md +16 -0
- package/lib/antigravity-plugin/skills/project-maintenance/SKILL.md +35 -0
- package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +42 -0
- package/lib/claude.js +4 -11
- package/lib/client.js +14 -531
- package/lib/common/markdown.js +2 -3
- package/lib/common/validation.d.ts +2 -0
- package/lib/common/validation.js +4 -0
- package/lib/extension/acp-member.d.ts +2 -0
- package/lib/extension/acp-member.js +9 -8
- package/lib/extension/acp-worker.js +11578 -121
- package/lib/extension/dsh/acp-member.js +26 -2
- package/lib/extension/dsh/command.js +19 -57
- package/lib/extension/dsh/history.js +2 -4
- package/lib/extension/dsh/management.d.ts +0 -2
- package/lib/extension/dsh/management.js +27 -126
- package/lib/extension/dsh/workspace.d.ts +5 -47
- package/lib/extension/dsh/workspace.js +76 -108
- package/lib/extension/mcp-bridge.d.ts +41 -0
- package/lib/extension/mcp-bridge.js +412 -0
- package/lib/extension/mcp-workers.d.ts +980 -0
- package/lib/extension/mcp-workers.js +882 -0
- package/lib/extension/mcp.js +2715 -0
- package/lib/extension/plugin-runtime.d.ts +10 -0
- package/lib/extension/plugin-runtime.js +67 -0
- package/lib/extension/setup-cli.js +215 -2
- package/lib/integrations/acp/agents.d.ts +10 -11
- package/lib/integrations/acp/agents.js +65 -33
- package/lib/integrations/acp/claude.d.ts +2 -2
- package/lib/integrations/acp/claude.js +5 -3
- package/lib/integrations/acp/connection.d.ts +17 -4
- package/lib/integrations/acp/connection.js +53 -27
- package/lib/integrations/acp/executable.d.ts +3 -0
- package/lib/integrations/acp/executable.js +42 -0
- package/lib/integrations/acp/harnesses/claude.d.ts +2 -0
- package/lib/integrations/acp/harnesses/claude.js +49 -0
- package/lib/integrations/acp/harnesses/dsh.d.ts +2 -0
- package/lib/integrations/acp/harnesses/dsh.js +35 -0
- package/lib/integrations/acp/harnesses/generic.d.ts +13 -0
- package/lib/integrations/acp/harnesses/generic.js +37 -0
- package/lib/integrations/acp/harnesses/pi.d.ts +2 -0
- package/lib/integrations/acp/harnesses/pi.js +34 -0
- package/lib/integrations/acp/harnesses/presets.d.ts +3 -0
- package/lib/integrations/acp/harnesses/presets.js +20 -0
- package/lib/integrations/acp/launch.d.ts +33 -4
- package/lib/integrations/acp/launch.js +60 -48
- package/lib/integrations/acp/mcp.d.ts +6 -0
- package/lib/integrations/acp/mcp.js +40 -0
- package/lib/integrations/acp/model-selection.d.ts +9 -2
- package/lib/integrations/acp/model-selection.js +73 -27
- package/lib/integrations/acp/registry.d.ts +18 -0
- package/lib/integrations/acp/registry.js +50 -0
- package/lib/integrations/agent-resources.d.ts +16 -1
- package/lib/integrations/agent-resources.js +45 -9
- package/lib/integrations/dsh/mcp.d.ts +4 -0
- package/lib/integrations/dsh/mcp.js +28 -0
- package/lib/integrations/dsh/resources.d.ts +3 -3
- package/lib/integrations/dsh/resources.js +21 -12
- package/lib/integrations/dsh/skills.d.ts +5 -1
- package/lib/integrations/dsh/skills.js +10 -6
- package/lib/integrations/hosts/antigravity.d.ts +4 -0
- package/lib/integrations/hosts/antigravity.js +79 -0
- package/lib/integrations/hosts/claude.d.ts +3 -0
- package/lib/integrations/hosts/claude.js +60 -0
- package/lib/integrations/hosts/registry.d.ts +15 -0
- package/lib/integrations/hosts/registry.js +34 -0
- package/lib/model/acp.d.ts +107 -2
- package/lib/model/acp.js +12 -1
- package/lib/model/agent.d.ts +35 -33
- package/lib/model/plugin-host.d.ts +48 -0
- package/lib/model/plugin-host.js +1 -0
- package/lib/model/workspace-config.d.ts +12 -0
- package/lib/model/workspace-mcp.d.ts +51 -0
- package/lib/model/workspace-mcp.js +4 -0
- package/lib/plugin.d.ts +1 -0
- package/lib/plugin.js +17 -0
- package/lib/services/agents/acp-run.d.ts +13 -1
- package/lib/services/agents/acp-run.js +30 -2
- package/lib/services/agents/agent-service.d.ts +35 -120
- package/lib/services/agents/agent-service.js +156 -160
- package/lib/services/installation/setup-service.js +17 -1
- package/lib/services/workspace/workspace-config.d.ts +2 -1
- package/lib/services/workspace/workspace-config.js +39 -7
- package/lib/services/workspace/workspace-mcp.d.ts +14 -0
- package/lib/services/workspace/workspace-mcp.js +200 -0
- package/lib/services/workspace/workspace-skills.d.ts +8 -0
- package/lib/services/workspace/workspace-skills.js +91 -0
- package/lib/storage/acp-files.d.ts +3 -1
- package/lib/storage/acp-files.js +32 -2
- package/lib/storage/acp-history.d.ts +3 -2
- package/lib/storage/acp-history.js +19 -7
- package/lib/storage/agent-files.d.ts +8 -8
- package/lib/storage/agent-files.js +27 -47
- package/lib/tools/history/contract.d.ts +1 -1
- package/lib/tools/history/contract.js +17 -11
- package/lib/ui/dsh/index.js +8 -3
- package/lib/ui/dsh/management.d.ts +1 -1
- package/lib/ui/dsh/management.js +24 -456
- package/lib/ui/dsh/remote.d.ts +0 -2
- package/lib/ui/dsh/remote.js +1 -1
- package/package.json +12 -7
- package/resources/agents/coordinator.md +2 -2
- package/resources/antigravity/skills/pbot/SKILL.md +16 -0
- package/resources/claude/skills/pbot/SKILL.md +1 -1
- package/resources/prompts/history-coordinator.md +1 -0
- package/resources/prompts/plugin-coordinator.md +1 -0
- package/resources/prompts/workspace.md +9 -8
- package/resources/references/compute.md +9 -11
- package/resources/references/task-conductor.md +12 -26
- package/resources/skills/project-maintenance/SKILL.md +16 -21
- package/resources/skills/provider-connections/SKILL.md +24 -40
- package/resources/templates/workspace-agents.md +4 -0
- package/scripts/README.md +17 -34
- package/src/claude.ts +4 -8
- package/src/common/markdown.ts +2 -3
- package/src/common/validation.ts +6 -0
- package/src/extension/acp-member.ts +9 -8
- package/src/extension/acp-worker.ts +239 -97
- package/src/extension/command.ts +38 -36
- package/src/extension/dsh/acp-member.ts +21 -2
- package/src/extension/dsh/command.ts +21 -51
- package/src/extension/dsh/files.ts +1 -0
- package/src/extension/dsh/history.ts +9 -6
- package/src/extension/dsh/management.ts +27 -58
- package/src/extension/dsh/workspace.ts +88 -99
- package/src/extension/events.ts +13 -10
- package/src/extension/history.ts +2 -50
- package/src/extension/lifecycle.ts +37 -22
- package/src/extension/mcp-bridge.ts +412 -0
- package/src/extension/mcp-workers.ts +941 -0
- package/src/extension/pi-acp-member.ts +29 -5
- package/src/extension/plugin-runtime.ts +63 -0
- package/src/extension/setup-cli.ts +186 -2
- package/src/integrations/acp/agents.ts +70 -24
- package/src/integrations/acp/claude.ts +11 -4
- package/src/integrations/acp/connection.ts +63 -25
- package/src/integrations/acp/executable.ts +41 -0
- package/src/integrations/acp/harnesses/claude.ts +50 -0
- package/src/integrations/acp/harnesses/dsh.ts +40 -0
- package/src/integrations/acp/harnesses/generic.ts +52 -0
- package/src/integrations/acp/harnesses/pi.ts +35 -0
- package/src/integrations/acp/harnesses/presets.ts +22 -0
- package/src/integrations/acp/launch.ts +69 -47
- package/src/integrations/acp/mcp.ts +43 -0
- package/src/integrations/acp/model-selection.ts +68 -25
- package/src/integrations/acp/registry.ts +60 -0
- package/src/integrations/agent-resources.ts +70 -6
- package/src/integrations/dsh/mcp.ts +30 -0
- package/src/integrations/dsh/resources.ts +28 -13
- package/src/integrations/dsh/skills.ts +16 -6
- package/src/integrations/hosts/antigravity.ts +83 -0
- package/src/integrations/hosts/claude.ts +62 -0
- package/src/integrations/hosts/registry.ts +42 -0
- package/src/integrations/pi/mcp.ts +26 -0
- package/src/mcp.ts +14 -0
- package/src/model/acp.ts +113 -3
- package/src/model/agent.ts +18 -14
- package/src/model/plugin-host.ts +39 -0
- package/src/model/workspace-config.ts +14 -0
- package/src/model/workspace-mcp.ts +64 -0
- package/src/pbot.ts +69 -20
- package/src/plugin.ts +19 -0
- package/src/services/agents/acp-run.ts +35 -3
- package/src/services/agents/agent-service.ts +180 -160
- package/src/services/installation/setup-service.ts +17 -2
- package/src/services/workspace/workspace-config.ts +42 -6
- package/src/services/workspace/workspace-mcp.ts +242 -0
- package/src/services/workspace/workspace-skills.ts +78 -0
- package/src/storage/acp-files.ts +32 -3
- package/src/storage/acp-history.ts +18 -7
- package/src/storage/agent-files.ts +27 -41
- package/src/tools/history/contract.ts +17 -11
- package/src/tools/register.ts +4 -8
- package/src/ui/dsh/index.ts +8 -3
- package/src/ui/dsh/management.ts +40 -282
- package/src/ui/dsh/remote.ts +2 -2
- package/docs/adr/0001-claude-skill-discovery.md +0 -3
- package/docs/adr/0002-claude-native-collaboration.md +0 -3
- package/docs/agent-collaboration.md +0 -190
- package/docs/architecture-guidelines.md +0 -37
- package/docs/capability-design.md +0 -55
- package/docs/claude-code-design.md +0 -44
- package/docs/claude-code.md +0 -57
- package/docs/dsh.md +0 -337
- package/docs/file-storage.md +0 -97
- package/docs/history.md +0 -54
- package/docs/installation-design.md +0 -86
- package/docs/interaction-design.md +0 -21
- package/docs/model-context.md +0 -76
- package/docs/model-instruction-guidelines.md +0 -51
- package/docs/release.md +0 -108
- package/docs/technical-design.md +0 -163
- package/docs/workspace-configuration.md +0 -79
- package/lib/extension/claude-hooks.d.ts +0 -9
- package/lib/extension/claude-hooks.js +0 -78
- package/lib/extension/dsh/agent-events.d.ts +0 -24
- package/lib/extension/dsh/agent-events.js +0 -242
- package/lib/extension/dsh/agent-runtime.d.ts +0 -49
- package/lib/extension/dsh/agent-runtime.js +0 -351
- package/lib/integrations/dsh/dsh-agents.d.ts +0 -30
- package/lib/integrations/dsh/dsh-agents.js +0 -104
- package/lib/integrations/dsh/input.d.ts +0 -21
- package/lib/integrations/dsh/input.js +0 -62
- package/lib/integrations/dsh/notifications.d.ts +0 -8
- package/lib/integrations/dsh/notifications.js +0 -55
- package/lib/integrations/dsh/session.d.ts +0 -14
- package/lib/integrations/dsh/session.js +0 -60
- package/lib/integrations/team-context.d.ts +0 -18
- package/lib/integrations/team-context.js +0 -61
- package/lib/model/team.d.ts +0 -85
- package/lib/services/agents/agent-notices.d.ts +0 -13
- package/lib/services/agents/agent-notices.js +0 -25
- package/lib/services/agents/start-options.d.ts +0 -4
- package/lib/services/agents/start-options.js +0 -20
- package/lib/services/agents/team-service.d.ts +0 -168
- package/lib/services/agents/team-service.js +0 -345
- package/lib/storage/team-files.d.ts +0 -16
- package/lib/storage/team-files.js +0 -111
- package/lib/tools/agents/actions.d.ts +0 -62
- package/lib/tools/agents/actions.js +0 -62
- package/lib/tools/agents/contract.d.ts +0 -181
- package/lib/tools/agents/contract.js +0 -84
- package/lib/tools/agents/dsh-tool.d.ts +0 -5
- package/lib/tools/agents/dsh-tool.js +0 -34
- package/lib/tools/result.d.ts +0 -18
- package/lib/tools/result.js +0 -10
- package/lib/tools/teams/actions.d.ts +0 -23
- package/lib/tools/teams/actions.js +0 -40
- package/lib/tools/teams/contract.d.ts +0 -201
- package/lib/tools/teams/contract.js +0 -91
- package/lib/tools/teams/dsh-tool.d.ts +0 -8
- package/lib/tools/teams/dsh-tool.js +0 -39
- package/lib/ui/dsh/attachments.d.ts +0 -5
- package/lib/ui/dsh/attachments.js +0 -39
- package/resources/prompts/claude-coordinator.md +0 -1
- package/resources/prompts/native-coordinator.md +0 -1
- package/resources/prompts/team-member.md +0 -7
- package/src/extension/agent-events.ts +0 -210
- package/src/extension/agents-command.ts +0 -89
- package/src/extension/claude-hooks.ts +0 -74
- package/src/extension/dsh/agent-events.ts +0 -148
- package/src/extension/dsh/agent-runtime.ts +0 -252
- package/src/extension/subagent-entry.ts +0 -270
- package/src/extension/teams-command.ts +0 -67
- package/src/integrations/dsh/dsh-agents.ts +0 -97
- package/src/integrations/dsh/input.ts +0 -66
- package/src/integrations/dsh/notifications.ts +0 -46
- package/src/integrations/pi/agent-context.ts +0 -18
- package/src/integrations/pi/agent-input.ts +0 -19
- package/src/integrations/pi/pi-agents.ts +0 -84
- package/src/integrations/pi/team-context.ts +0 -26
- package/src/integrations/team-context.ts +0 -64
- package/src/model/team.ts +0 -29
- package/src/services/agents/agent-notices.ts +0 -28
- package/src/services/agents/start-options.ts +0 -19
- package/src/services/agents/team-service.ts +0 -275
- package/src/storage/team-files.ts +0 -93
- package/src/tools/agents/actions.ts +0 -57
- package/src/tools/agents/contract.ts +0 -101
- package/src/tools/agents/dsh-tool.ts +0 -37
- package/src/tools/agents/tool.ts +0 -20
- package/src/tools/teams/actions.ts +0 -34
- package/src/tools/teams/contract.ts +0 -109
- package/src/tools/teams/dsh-tool.ts +0 -45
- package/src/tools/teams/tool.ts +0 -29
- package/src/ui/agent-display.ts +0 -14
- package/src/ui/dsh/attachments.ts +0 -42
- package/src/ui/pi/menu.ts +0 -37
- package/src/ui/pi/reader.ts +0 -46
- /package/lib/{model/team.js → antigravity-plugin/lib/model/plugin-host.js} +0 -0
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { realpathSync
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
|
+
import { object } from "../../common/json.js";
|
|
4
5
|
import { packageVersion, releaseLine } from "../../common/version.js";
|
|
5
6
|
import { requireThat } from "../../common/validation.js";
|
|
6
7
|
import { AgentFiles, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import { applicableStartOptions } from "./start-options.js";
|
|
8
|
+
import { acpElicitationReply, acpModelState, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
|
|
9
|
+
/** Host-neutral lifecycle manager for cross-harness ACP workers. */
|
|
10
10
|
export class AgentService {
|
|
11
11
|
root;
|
|
12
12
|
host;
|
|
13
13
|
files;
|
|
14
|
-
teamFiles;
|
|
15
14
|
#closed = false;
|
|
16
15
|
#preserve = false;
|
|
17
16
|
#starting = new Map();
|
|
@@ -21,19 +20,18 @@ export class AgentService {
|
|
|
21
20
|
constructor(root, directory, host) {
|
|
22
21
|
this.root = root;
|
|
23
22
|
this.host = host;
|
|
24
|
-
this.files = new AgentFiles(directory
|
|
25
|
-
this.teamFiles = new TeamFiles(this.files);
|
|
23
|
+
this.files = new AgentFiles(directory);
|
|
26
24
|
}
|
|
27
25
|
alive(id) { return this.#starting.has(id) || this.#releasing.has(id) || this.host.alive(id); }
|
|
28
|
-
check() { requireThat(!this.#closed, "
|
|
26
|
+
check() { requireThat(!this.#closed, "worker 服务已关闭。"); }
|
|
29
27
|
owned(d, owner) {
|
|
30
28
|
return d.workspace === this.root && d.parentSessionId === owner.sessionId && (d.parentLeaf === null || owner.branch.includes(d.parentLeaf));
|
|
31
29
|
}
|
|
32
30
|
get(id, owner) {
|
|
33
31
|
this.check();
|
|
34
32
|
const d = this.files.read(id, "descriptor.json", descriptor);
|
|
35
|
-
requireThat(this.owned(d, owner), "
|
|
36
|
-
requireThat(releaseLine(d.release) === releaseLine(packageVersion), "
|
|
33
|
+
requireThat(this.owned(d, owner), "worker 不属于当前会话。");
|
|
34
|
+
requireThat(releaseLine(d.release) === releaseLine(packageVersion), "worker 版本不兼容;请停止后使用匹配版本。");
|
|
37
35
|
return d;
|
|
38
36
|
}
|
|
39
37
|
records(owner) {
|
|
@@ -48,35 +46,132 @@ export class AgentService {
|
|
|
48
46
|
const current = saved?.generation === run.generation ? saved : undefined;
|
|
49
47
|
const alive = this.alive(d.agentId), pending = this.#starting.get(d.agentId);
|
|
50
48
|
const starting = pending && !pending.controller.signal.aborted;
|
|
51
|
-
const reference = this.files.
|
|
52
|
-
const model =
|
|
53
|
-
const selection = model?.generation === run.generation ? {
|
|
54
|
-
...(model.
|
|
55
|
-
...(model.
|
|
56
|
-
|
|
57
|
-
...(
|
|
58
|
-
|
|
59
|
-
|
|
49
|
+
const reference = this.files.read(d.agentId, "acp-session.json", acpReference);
|
|
50
|
+
const model = this.files.optional(d.agentId, "acp-model.json", acpModelState);
|
|
51
|
+
const selection = model?.generation === run.generation ? {
|
|
52
|
+
...(model.provider ? { provider: model.provider } : {}),
|
|
53
|
+
...(model.model ? { model: model.model } : {}),
|
|
54
|
+
...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
|
|
55
|
+
...(model.thinking ? { thinking: model.thinking } : {}),
|
|
56
|
+
} : undefined;
|
|
57
|
+
const pendingRequests = alive ? [
|
|
58
|
+
...this.files.pendingPermissions(d.agentId, run.generation)
|
|
59
|
+
.map(item => ({ requestId: item.requestId, kind: "permission", description: item.description, options: item.options })),
|
|
60
|
+
...this.files.pendingElicitations(d.agentId, run.generation)
|
|
61
|
+
.map(item => (item.mode === "form"
|
|
62
|
+
? { requestId: item.requestId, kind: "form", message: item.message, schema: item.schema ?? { type: "object", properties: {} } }
|
|
63
|
+
: { requestId: item.requestId, kind: "url", message: item.message, url: item.url ?? "" })),
|
|
64
|
+
] : [];
|
|
65
|
+
return {
|
|
66
|
+
...d,
|
|
67
|
+
name: run.name,
|
|
68
|
+
generation: run.generation,
|
|
69
|
+
owned: this.owned(d, owner),
|
|
70
|
+
alive,
|
|
71
|
+
backend: reference.backend,
|
|
72
|
+
transport: "acp",
|
|
73
|
+
capabilities: {
|
|
74
|
+
steer: false,
|
|
75
|
+
resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load),
|
|
76
|
+
transcript: false,
|
|
77
|
+
},
|
|
78
|
+
...(selection ? { selection } : {}),
|
|
60
79
|
state: alive ? starting ? "starting" : run.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
|
|
61
80
|
activity: starting ? "启动" : alive ? current?.activity ?? "启动" : current?.error ?? "已停止",
|
|
62
|
-
...(current?.cycle ? { cycle: current.cycle } : {}),
|
|
81
|
+
...(current?.cycle ? { cycle: current.cycle } : {}),
|
|
82
|
+
...(current?.error ? { error: current.error } : {}),
|
|
83
|
+
...(pendingRequests.length ? { pendingRequests } : {}),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Answer a pending permission or elicitation request forwarded by the worker. */
|
|
87
|
+
respond(id, input, owner) {
|
|
88
|
+
const d = this.get(id, owner), run = this.files.read(id, "launch.json", launch);
|
|
89
|
+
const pending = this.view(d, owner).pendingRequests ?? [];
|
|
90
|
+
if (!input.requestId && pending.length > 1) {
|
|
91
|
+
throw new Error("存在多条待回复请求,请指定 requestId:" + pending.map(item => item.requestId).join(", "));
|
|
92
|
+
}
|
|
93
|
+
const target = input.requestId ? pending.find(item => item.requestId === input.requestId) : pending[0];
|
|
94
|
+
requireThat(target, input.requestId
|
|
95
|
+
? "没有待回复的请求:" + input.requestId + ";用 worker_discover 查看 pendingRequests。"
|
|
96
|
+
: "当前没有待回复的请求。");
|
|
97
|
+
if (target.kind === "permission") {
|
|
98
|
+
requireThat(input.content === undefined, "权限请求不使用 content;请用 action(accept / decline / cancel)或指定 optionId。");
|
|
99
|
+
let resolved = null;
|
|
100
|
+
if (input.action === "cancel") {
|
|
101
|
+
requireThat(input.optionId === undefined, "action 为 cancel 时不能同时指定 optionId。");
|
|
102
|
+
}
|
|
103
|
+
else if (input.optionId !== undefined) {
|
|
104
|
+
const exact = target.options.find(option => option.optionId === input.optionId);
|
|
105
|
+
const byKind = target.options.filter(option => option.kind === input.optionId);
|
|
106
|
+
const matched = exact ?? (byKind.length === 1 ? byKind[0] : undefined);
|
|
107
|
+
requireThat(matched, "权限 optionId 无效:" + input.optionId + ";可选:" +
|
|
108
|
+
target.options.map(option => `${option.optionId}(${option.name}/${option.kind})`).join(", "));
|
|
109
|
+
if (input.action === "accept") {
|
|
110
|
+
requireThat(!matched.kind.startsWith("reject_"), `optionId ${matched.optionId} 是拒绝选项,与 action: "accept" 冲突。`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
requireThat(!matched.kind.startsWith("allow_"), `optionId ${matched.optionId} 是允许选项,与 action: "decline" 冲突。`);
|
|
114
|
+
}
|
|
115
|
+
resolved = matched.optionId;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const pick = (...kinds) => kinds.map(kind => target.options.find(option => option.kind === kind)).find(Boolean)?.optionId;
|
|
119
|
+
if (input.action === "accept") {
|
|
120
|
+
const chosen = pick("allow_once", "allow_always");
|
|
121
|
+
requireThat(chosen, "该权限请求没有可允许的选项;可选:" + target.options.map(option => option.optionId).join(", "));
|
|
122
|
+
resolved = chosen;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
resolved = pick("reject_once", "reject_always") ?? null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this.files.ensureSubdirectory(id, "permissions");
|
|
129
|
+
this.files.write(id, "permissions/" + target.requestId + "-reply.json", { generation: run.generation, requestId: target.requestId, optionId: resolved }, acpPermissionReply, true);
|
|
130
|
+
return this.view(d, owner);
|
|
131
|
+
}
|
|
132
|
+
if (target.kind === "form") {
|
|
133
|
+
requireThat(input.optionId === undefined, "问答/表单请求不使用 optionId;请将答案填入 content。");
|
|
134
|
+
if (input.action !== "accept") {
|
|
135
|
+
requireThat(input.content === undefined, `action 为 ${input.action} 时不能传入 content。`);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
requireThat(input.content === undefined || object(input.content), "问答/表单答复的 content 必须是对象。");
|
|
139
|
+
}
|
|
140
|
+
this.files.ensureSubdirectory(id, "elicitations");
|
|
141
|
+
this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
|
|
142
|
+
generation: run.generation,
|
|
143
|
+
requestId: target.requestId,
|
|
144
|
+
action: input.action,
|
|
145
|
+
...(input.action === "accept" ? { content: input.content ?? {} } : {}),
|
|
146
|
+
}, acpElicitationReply, true);
|
|
147
|
+
return this.view(d, owner);
|
|
148
|
+
}
|
|
149
|
+
requireThat(input.optionId === undefined, "URL 确认请求不使用 optionId。");
|
|
150
|
+
requireThat(input.content === undefined, "URL 确认请求不使用 content。");
|
|
151
|
+
this.files.ensureSubdirectory(id, "elicitations");
|
|
152
|
+
this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
|
|
153
|
+
generation: run.generation,
|
|
154
|
+
requestId: target.requestId,
|
|
155
|
+
action: input.action,
|
|
156
|
+
}, acpElicitationReply, true);
|
|
157
|
+
return this.view(d, owner);
|
|
63
158
|
}
|
|
64
159
|
async start(input, owner) {
|
|
65
160
|
this.check();
|
|
66
161
|
const { backend, ...supplied } = input;
|
|
67
162
|
this.host.available(backend);
|
|
68
|
-
const
|
|
69
|
-
this.host.
|
|
70
|
-
|
|
71
|
-
const reference = backend ? this.host.prepare?.(backend, options) : undefined;
|
|
72
|
-
requireThat(!backend || reference, "当前执行接入不支持 ACP 后端。");
|
|
163
|
+
const clean = Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined));
|
|
164
|
+
const options = this.host.filterOptions ? this.host.filterOptions(backend, clean) : clean;
|
|
165
|
+
const reference = this.host.prepare(backend, options);
|
|
73
166
|
const prepared = this.files.control(() => {
|
|
74
|
-
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50
|
|
167
|
+
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker;请先停止一个空闲会话。");
|
|
75
168
|
const agentId = randomUUID();
|
|
76
169
|
const cwd = realpathSync(resolve(this.root, options.cwd ?? "."));
|
|
77
170
|
const files = (options.files ?? []).map(p => realpathSync(resolve(this.root, p)));
|
|
78
|
-
const d = {
|
|
79
|
-
|
|
171
|
+
const d = {
|
|
172
|
+
agentId, release: packageVersion, workspace: this.root, cwd,
|
|
173
|
+
parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd },
|
|
174
|
+
};
|
|
80
175
|
const run = { generation: randomUUID(), name: options.name ?? agentId.slice(0, 8), sequence: 0, stop: false, startedAt: Date.now() };
|
|
81
176
|
this.files.create(d, run, reference);
|
|
82
177
|
const initial = options.message ?? "";
|
|
@@ -93,18 +188,20 @@ export class AgentService {
|
|
|
93
188
|
const operation = (async () => {
|
|
94
189
|
try {
|
|
95
190
|
await this.host.launch(d, this.files.directory(d.agentId), generation, this.files.reference(d.agentId), resume, pending.controller.signal);
|
|
96
|
-
requireThat(!this.#closed || this.#preserve, "
|
|
191
|
+
requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
|
|
97
192
|
pending.controller.signal.throwIfAborted();
|
|
98
193
|
const run = this.files.read(d.agentId, "launch.json", launch);
|
|
99
|
-
requireThat(run.generation === generation && !run.stop, "
|
|
194
|
+
requireThat(run.generation === generation && !run.stop, "worker 启动已取消或代次变化。");
|
|
100
195
|
}
|
|
101
196
|
catch (error) {
|
|
102
197
|
const run = this.files.read(d.agentId, "launch.json", launch);
|
|
103
198
|
if (run.generation === generation) {
|
|
104
199
|
if (this.host.alive(d.agentId))
|
|
105
200
|
await this.release(d.agentId);
|
|
106
|
-
this.files.write(d.agentId, "status.json", {
|
|
107
|
-
|
|
201
|
+
this.files.write(d.agentId, "status.json", {
|
|
202
|
+
generation, state: run.stop ? "stopped" : "error",
|
|
203
|
+
activity: run.stop ? "已停止" : "启动失败", updatedAt: Date.now(), ...(run.stop ? {} : { error: String(error) }),
|
|
204
|
+
}, status);
|
|
108
205
|
}
|
|
109
206
|
throw error;
|
|
110
207
|
}
|
|
@@ -124,43 +221,14 @@ export class AgentService {
|
|
|
124
221
|
send(id, text, delivery, owner, attachments = []) {
|
|
125
222
|
const d = this.get(id, owner);
|
|
126
223
|
return this.files.control(() => {
|
|
127
|
-
requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "
|
|
128
|
-
this.assertPrivate(id);
|
|
224
|
+
requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
|
|
129
225
|
const view = this.view(d, owner);
|
|
130
|
-
requireThat(!(
|
|
226
|
+
requireThat(!(delivery === "steer" && view.state !== "idle"), "该 ACP worker 运行中不支持 steer;请用 delivery: followUp 排队。");
|
|
131
227
|
const files = attachments.map(path => realpathSync(resolve(this.root, path)));
|
|
132
228
|
const request = this.files.enqueue(id, text, delivery, files);
|
|
133
229
|
return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" };
|
|
134
230
|
});
|
|
135
231
|
}
|
|
136
|
-
assertPrivate(id) {
|
|
137
|
-
this.teamFiles.check();
|
|
138
|
-
const run = this.files.read(id, "launch.json", launch), saved = this.files.optional(id, "status.json", status);
|
|
139
|
-
const complete = this.files.completions(id), alive = this.host.alive(id);
|
|
140
|
-
const occupied = this.teamFiles.list().some(t => t.members.some(m => m.agentId === id) && t.runs.some(r => r.state === "running" || r.issued.some(q => q.agentId === id && q.generation === run.generation && alive &&
|
|
141
|
-
saved?.generation === run.generation && saved.accepted?.includes(q.requestId) &&
|
|
142
|
-
!complete.some(c => c.generation === q.generation && c.requestIds.includes(q.requestId)))));
|
|
143
|
-
if (occupied)
|
|
144
|
-
throw Object.assign(new Error("TEAM_BUSY:请先暂停团队并等待在途工作结束。"), { code: "TEAM_BUSY" });
|
|
145
|
-
}
|
|
146
|
-
enqueueTeam(input, owner) {
|
|
147
|
-
const d = this.get(input.agentId, owner);
|
|
148
|
-
return this.files.control(() => {
|
|
149
|
-
this.teamFiles.check();
|
|
150
|
-
const team = this.teamFiles.read(input.teamId), run = team.runs.find(r => r.runId === input.runId);
|
|
151
|
-
const q = run?.issued.find(q => q.requestId === input.requestId), ready = this.teamFiles.ready(input.agentId);
|
|
152
|
-
const process = this.files.read(input.agentId, "launch.json", launch);
|
|
153
|
-
const valid = team.workspace === this.root && team.parentSessionId === owner.sessionId &&
|
|
154
|
-
(team.parentLeaf === null || owner.branch.includes(team.parentLeaf)) && !team.closed && run?.state === "running" &&
|
|
155
|
-
run.deadlineAt > Date.now() && q?.agentId === input.agentId && q.generation === input.generation &&
|
|
156
|
-
process.generation === input.generation && !process.stop && this.host.alive(input.agentId) &&
|
|
157
|
-
ready?.ready && ready.generation === input.generation;
|
|
158
|
-
if (!valid)
|
|
159
|
-
throw Object.assign(new Error("团队安排已失效;未重新执行。"), { code: "TEAM_INPUT_STALE" });
|
|
160
|
-
const request = this.files.enqueue(input.agentId, input.message, "followUp", [], input.requestId);
|
|
161
|
-
return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" };
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
232
|
rename(id, name, owner) {
|
|
165
233
|
requireThat(name.trim().length > 0 && name.length <= 64 && !/[\x00-\x1f]/.test(name), "名称必须是 1 到 64 字符的单行文本。");
|
|
166
234
|
const d = this.get(id, owner);
|
|
@@ -179,7 +247,7 @@ export class AgentService {
|
|
|
179
247
|
async enter(id, owner) {
|
|
180
248
|
const d = this.get(id, owner);
|
|
181
249
|
if (this.alive(id)) {
|
|
182
|
-
requireThat(!this.files.read(id, "launch.json", launch).stop, "
|
|
250
|
+
requireThat(!this.files.read(id, "launch.json", launch).stop, "worker 正在停止,请稍后操作。");
|
|
183
251
|
return this.view(d, owner);
|
|
184
252
|
}
|
|
185
253
|
return this.restart(id, owner);
|
|
@@ -189,11 +257,10 @@ export class AgentService {
|
|
|
189
257
|
}
|
|
190
258
|
async restart(id, owner, text) {
|
|
191
259
|
const d = this.get(id, owner), reference = this.files.reference(id);
|
|
192
|
-
this.host.available(reference.
|
|
260
|
+
this.host.available(reference.backend);
|
|
193
261
|
const reserved = this.files.control(() => {
|
|
194
|
-
requireThat(!this.alive(id), "
|
|
195
|
-
this.
|
|
196
|
-
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中子会话。");
|
|
262
|
+
requireThat(!this.alive(id), "worker 仍在运行。");
|
|
263
|
+
requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker。");
|
|
197
264
|
const previous = this.files.read(id, "launch.json", launch), interrupted = this.cycles(id, owner);
|
|
198
265
|
const pending = { generation: previous.generation, controller: new AbortController(), stage: "validate" };
|
|
199
266
|
this.#starting.set(id, pending);
|
|
@@ -201,18 +268,15 @@ export class AgentService {
|
|
|
201
268
|
});
|
|
202
269
|
const operation = (async () => {
|
|
203
270
|
await this.host.validateSession(d, this.files.reference(id));
|
|
204
|
-
requireThat(!this.#closed || this.#preserve, "
|
|
271
|
+
requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
|
|
205
272
|
reserved.pending.controller.signal.throwIfAborted();
|
|
206
273
|
const prepared = this.files.control(() => {
|
|
207
|
-
requireThat(!this.host.alive(id) && this.files.read(id, "launch.json", launch).generation === reserved.previous.generation, "
|
|
208
|
-
this.assertPrivate(id);
|
|
209
|
-
// Only the generation known to be stopped before validation can be sealed.
|
|
274
|
+
requireThat(!this.host.alive(id) && this.files.read(id, "launch.json", launch).generation === reserved.previous.generation, "worker 已由其他操作恢复。");
|
|
210
275
|
for (const ended of reserved.interrupted) {
|
|
211
276
|
if (ended.generation === reserved.previous.generation && !this.files.completions(id).some(c => c.cycleId === ended.cycleId)) {
|
|
212
277
|
this.files.write(id, "completions/" + ended.cycleId + ".json", ended, cycle, true);
|
|
213
278
|
}
|
|
214
279
|
}
|
|
215
|
-
this.#stopping.delete(id);
|
|
216
280
|
const run = { ...reserved.previous, generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
|
|
217
281
|
this.files.write(id, "launch.json", run, launch);
|
|
218
282
|
const request = text === undefined ? undefined : this.files.enqueue(id, text, "steer");
|
|
@@ -240,16 +304,20 @@ export class AgentService {
|
|
|
240
304
|
}
|
|
241
305
|
if (["stopped", "error"].includes(view.state)) {
|
|
242
306
|
const run = this.files.read(id, "launch.json", launch), saved = this.files.optional(id, "status.json", status);
|
|
243
|
-
// Preserve receipt-only crash windows before resume replaces the generation and status.
|
|
244
307
|
for (const requestId of saved?.generation === run.generation ? saved.accepted ?? [] : []) {
|
|
245
308
|
if (values.some(c => c.generation === run.generation && c.requestIds.includes(requestId)))
|
|
246
309
|
continue;
|
|
247
|
-
values.push({
|
|
248
|
-
|
|
310
|
+
values.push({
|
|
311
|
+
cycleId: requestId, generation: run.generation, startedAt: run.startedAt, finishedAt: Date.now(),
|
|
312
|
+
state: "interrupted", leaf: null, requestIds: [requestId], source: "parent", error: "领取后执行结果不明;未重跑。",
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
if (run.sequence > 0 && !values.some(c => c.generation === view.generation)) {
|
|
316
|
+
values.push({
|
|
317
|
+
cycleId: run.generation, generation: run.generation, startedAt: run.startedAt, finishedAt: run.startedAt,
|
|
318
|
+
state: "interrupted", leaf: null, requestIds: [], source: "parent", error: view.error ?? "子进程在交付前退出。",
|
|
319
|
+
});
|
|
249
320
|
}
|
|
250
|
-
if (run.sequence > 0 && !values.some(c => c.generation === view.generation))
|
|
251
|
-
values.push({ cycleId: run.generation, generation: run.generation, startedAt: run.startedAt, finishedAt: run.startedAt,
|
|
252
|
-
state: "interrupted", leaf: null, requestIds: [], source: "parent", error: view.error ?? "子进程在交付前退出。" });
|
|
253
321
|
}
|
|
254
322
|
return values;
|
|
255
323
|
}
|
|
@@ -264,74 +332,20 @@ export class AgentService {
|
|
|
264
332
|
const report = c ? await this.report(d, c) : "";
|
|
265
333
|
this.check();
|
|
266
334
|
requireThat(offset <= report.length, "分页位置超出报告。");
|
|
267
|
-
return {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
335
|
+
return {
|
|
336
|
+
...this.view(d, owner),
|
|
337
|
+
...(c ? { cycle: c } : {}),
|
|
338
|
+
cycles: cycles.map(v => ({ cycleId: v.cycleId, state: v.state })),
|
|
339
|
+
report: report.slice(offset, offset + limit),
|
|
340
|
+
totalLength: report.length,
|
|
341
|
+
nextOffset: offset + limit < report.length ? offset + limit : null,
|
|
342
|
+
};
|
|
274
343
|
}
|
|
275
344
|
report(d, c) {
|
|
276
345
|
if (c.leaf === null)
|
|
277
346
|
return "";
|
|
278
347
|
return this.host.report(d, this.files.reference(d.agentId), c.leaf);
|
|
279
348
|
}
|
|
280
|
-
async recordPage(id, owner, options = {}) {
|
|
281
|
-
const d = this.get(id, owner), view = this.view(d, owner);
|
|
282
|
-
requireThat(view.transport === "acp", "该记录入口用于 ACP 成员。");
|
|
283
|
-
const messages = this.files.messages(id), cycles = this.cycles(id, owner).sort((a, b) => a.startedAt - b.startedAt);
|
|
284
|
-
const records = [];
|
|
285
|
-
const included = new Set();
|
|
286
|
-
for (const c of cycles) {
|
|
287
|
-
for (const requestId of c.requestIds) {
|
|
288
|
-
const request = messages.find(message => message.requestId === requestId);
|
|
289
|
-
if (!request || included.has(requestId))
|
|
290
|
-
continue;
|
|
291
|
-
included.add(requestId);
|
|
292
|
-
records.push({ seq: records.length, type: "acp/input", time: c.startedAt, requestId, state: c.state,
|
|
293
|
-
text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
|
|
294
|
-
}
|
|
295
|
-
const report = await this.report(d, c);
|
|
296
|
-
this.check();
|
|
297
|
-
records.push({ seq: records.length, type: "acp/report", time: c.finishedAt ?? c.startedAt, cycleId: c.cycleId, state: c.state,
|
|
298
|
-
text: [c.error, report].filter(Boolean).join("\n\n") });
|
|
299
|
-
}
|
|
300
|
-
const run = this.files.read(id, "launch.json", launch);
|
|
301
|
-
for (const request of messages.filter(message => message.generation === run.generation && !included.has(message.requestId))) {
|
|
302
|
-
records.push({ seq: records.length, type: "acp/input", time: run.startedAt, requestId: request.requestId, state: "queued",
|
|
303
|
-
text: [request.message, ...request.files.map(file => "附件:" + file)].filter(Boolean).join("\n\n") });
|
|
304
|
-
}
|
|
305
|
-
const cursor = run.generation + ":" + records.length + ":" + cycles.at(-1)?.state;
|
|
306
|
-
if (options.seq !== undefined) {
|
|
307
|
-
const record = records[options.seq];
|
|
308
|
-
requireThat(record, "ACP 记录位置不存在。");
|
|
309
|
-
const text = options.format === "json" ? JSON.stringify(record, null, 2) : record.text;
|
|
310
|
-
const offset = options.textOffset ?? 0, end = offset + (options.textLimit ?? 12000);
|
|
311
|
-
requireThat(offset <= text.length, "ACP 记录分页位置超出范围。");
|
|
312
|
-
return { kind: "acp", agent: view, cursor, seq: record.seq, offset, text: text.slice(offset, end), totalLength: text.length, nextOffset: end < text.length ? end : null };
|
|
313
|
-
}
|
|
314
|
-
const limit = options.limit ?? 30, offset = options.offset ?? Math.max(0, records.length - limit);
|
|
315
|
-
requireThat(offset <= records.length, "ACP 记录分页位置超出范围。");
|
|
316
|
-
return { kind: "acp", agent: view, cursor, total: records.length, offset, nextOffset: offset + limit < records.length ? offset + limit : null,
|
|
317
|
-
records: records.slice(offset, offset + limit).map(record => { const { text, ...fields } = record; return { ...fields, preview: text.slice(0, 800) }; }) };
|
|
318
|
-
}
|
|
319
|
-
pendingPermissions(owner) {
|
|
320
|
-
return this.list(owner).filter(view => view.owned && view.alive && view.transport === "acp").flatMap(view => readdirSync(resolve(this.files.directory(view.agentId), "permissions")).filter(name => name.endsWith(".json") && !name.endsWith("-reply.json")).flatMap(name => {
|
|
321
|
-
const request = this.files.read(view.agentId, "permissions/" + name, acpPermission);
|
|
322
|
-
if (request.generation !== view.generation || this.files.optional(view.agentId, "permissions/" + request.requestId + "-reply.json", acpPermissionReply))
|
|
323
|
-
return [];
|
|
324
|
-
return [{ ...request, agentId: view.agentId, name: view.name }];
|
|
325
|
-
})).slice(0, 32);
|
|
326
|
-
}
|
|
327
|
-
answerPermission(id, requestId, optionId, owner) {
|
|
328
|
-
this.get(id, owner);
|
|
329
|
-
const request = this.files.read(id, "permissions/" + requestId + ".json", acpPermission);
|
|
330
|
-
const run = this.files.read(id, "launch.json", launch);
|
|
331
|
-
requireThat(run.generation === request.generation && !run.stop && this.alive(id), "ACP 确认请求已失效。");
|
|
332
|
-
requireThat(optionId === null || request.options.some(option => option.optionId === optionId), "ACP 确认选项无效。");
|
|
333
|
-
this.files.write(id, "permissions/" + requestId + "-reply.json", { generation: run.generation, requestId, optionId }, acpPermissionReply, true);
|
|
334
|
-
}
|
|
335
349
|
release(id) {
|
|
336
350
|
let pending = this.#releasing.get(id);
|
|
337
351
|
if (!pending) {
|
|
@@ -344,24 +358,6 @@ export class AgentService {
|
|
|
344
358
|
}
|
|
345
359
|
return pending;
|
|
346
360
|
}
|
|
347
|
-
/** Called by the parent scanner; stop remains nonblocking to the model. */
|
|
348
|
-
async refresh(owner) {
|
|
349
|
-
const releases = [];
|
|
350
|
-
for (const d of this.records(owner)) {
|
|
351
|
-
const run = this.files.read(d.agentId, "launch.json", launch);
|
|
352
|
-
if (!run.stop)
|
|
353
|
-
this.#stopping.delete(d.agentId);
|
|
354
|
-
if (run.stop && this.alive(d.agentId)) {
|
|
355
|
-
this.#starting.get(d.agentId)?.controller.abort();
|
|
356
|
-
const since = this.#stopping.get(d.agentId) ?? Date.now();
|
|
357
|
-
this.#stopping.set(d.agentId, since);
|
|
358
|
-
if (Date.now() - since > 5000)
|
|
359
|
-
releases.push(this.release(d.agentId));
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
await Promise.all(releases);
|
|
363
|
-
}
|
|
364
|
-
#stopping = new Map();
|
|
365
361
|
close(owner, preserve = false) {
|
|
366
362
|
return this.#closing ??= this.dispose(owner, preserve);
|
|
367
363
|
}
|
|
@@ -392,6 +388,6 @@ export class AgentService {
|
|
|
392
388
|
errors.push(value.reason);
|
|
393
389
|
await Promise.allSettled([...this.#launches]);
|
|
394
390
|
if (errors.length)
|
|
395
|
-
throw new AggregateError(errors, "
|
|
391
|
+
throw new AggregateError(errors, "停止或释放 worker 失败;请核对仍存活的执行资源。");
|
|
396
392
|
}
|
|
397
393
|
}
|
|
@@ -45,8 +45,24 @@ export function parseSetupArgs(args) {
|
|
|
45
45
|
return { action: "help" };
|
|
46
46
|
if (args.length === 1 && ["--version", "-v"].includes(args[0]))
|
|
47
47
|
return { action: "version" };
|
|
48
|
+
if (args[0] === "install-antigravity") {
|
|
49
|
+
if (args.length === 1)
|
|
50
|
+
return { action: "install-antigravity" };
|
|
51
|
+
if (args.length === 2 && ["--help", "-h"].includes(args[1]))
|
|
52
|
+
return { action: "help" };
|
|
53
|
+
throw new Error("用法:pbot install-antigravity");
|
|
54
|
+
}
|
|
55
|
+
if (args[0] === "mcp") {
|
|
56
|
+
if (args.length === 1)
|
|
57
|
+
return { action: "mcp" };
|
|
58
|
+
if (args.length === 2 && ["--help", "-h"].includes(args[1]))
|
|
59
|
+
return { action: "help" };
|
|
60
|
+
if (["list", "add", "remove"].includes(args[1]))
|
|
61
|
+
return { action: "mcp-manage", mcpArgs: args.slice(1) };
|
|
62
|
+
throw new Error("用法:pbot mcp [list|add|remove ...]");
|
|
63
|
+
}
|
|
48
64
|
if (args[0] !== "setup")
|
|
49
|
-
throw new Error("用法:pbot setup [--workspace <目录>]");
|
|
65
|
+
throw new Error("用法:pbot setup [--workspace <目录>] | pbot install-antigravity | pbot mcp [list|add|remove ...]");
|
|
50
66
|
let workspace;
|
|
51
67
|
for (let index = 1; index < args.length; index++) {
|
|
52
68
|
const arg = args[index];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { WorkspaceConfiguration } from "../../model/workspace-config.js";
|
|
1
|
+
import type { ResolvedWorkspaceFeatures, WorkspaceConfiguration, WorkspaceFeatures } from "../../model/workspace-config.js";
|
|
2
|
+
export declare function workspaceFeatures(configuration?: Pick<WorkspaceConfiguration, "features"> | WorkspaceFeatures): ResolvedWorkspaceFeatures;
|
|
2
3
|
export declare function workspaceConfigPath(root: string): string;
|
|
3
4
|
/** Read only this workspace's configuration; parent directories never grant activation. */
|
|
4
5
|
export declare function readWorkspaceConfig(root: string): Promise<WorkspaceConfiguration>;
|
|
@@ -1,23 +1,49 @@
|
|
|
1
1
|
import { mkdir, readFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, join } from "node:path";
|
|
3
3
|
import { object } from "../../common/json.js";
|
|
4
|
+
import { isAcpBackend } from "../../common/validation.js";
|
|
4
5
|
import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
|
|
6
|
+
const featureKeys = ["prompts", "skills", "mcp"];
|
|
5
7
|
function config(value) {
|
|
6
8
|
if (!object(value) || value.enabled !== undefined && typeof value.enabled !== "boolean") {
|
|
7
9
|
throw new Error("配置必须是 JSON 对象,enabled 必须是布尔值。");
|
|
8
10
|
}
|
|
11
|
+
if (value.features !== undefined) {
|
|
12
|
+
if (!object(value.features) ||
|
|
13
|
+
Object.entries(value.features).some(([key, entry]) => !featureKeys.includes(key) || (entry !== undefined && typeof entry !== "boolean"))) {
|
|
14
|
+
throw new Error("features 必须是 JSON 对象,仅支持 prompts、skills、mcp 布尔开关。");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
9
17
|
if (value.acp !== undefined) {
|
|
10
|
-
if (!object(value.acp) || Object.keys(value.acp).some(key => !
|
|
11
|
-
throw new Error("acp
|
|
18
|
+
if (!object(value.acp) || Object.keys(value.acp).some(key => !isAcpBackend(key))) {
|
|
19
|
+
throw new Error("acp 键名必须为小写字母、数字和连字符(1–64 字符)。");
|
|
20
|
+
}
|
|
12
21
|
for (const command of Object.values(value.acp)) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
// Without command, an entry keeps the registered default command and only sets env or permission.
|
|
23
|
+
const launch = !object(command) ? false : command.command === undefined
|
|
24
|
+
? command.args === undefined
|
|
25
|
+
: typeof command.command === "string" && command.command.trim() && !command.command.includes("\0") &&
|
|
26
|
+
(command.args === undefined || (Array.isArray(command.args) && command.args.every(arg => typeof arg === "string" && !arg.includes("\0"))));
|
|
27
|
+
if (!object(command) || !launch ||
|
|
28
|
+
(command.env !== undefined && (!object(command.env) || Object.entries(command.env).some(([k, v]) => !k || k.includes("\0") || typeof v !== "string" || v.includes("\0")))) ||
|
|
29
|
+
(command.permission !== undefined && !["ask", "allow", "deny"].includes(String(command.permission))) ||
|
|
30
|
+
Object.keys(command).some(key => !["command", "args", "env", "permission"].includes(key))) {
|
|
31
|
+
throw new Error("ACP 配置需要 command(内置目标可省略)及可选 args 字符串数组、env 字符串映射、permission(ask、allow 或 deny)。");
|
|
32
|
+
}
|
|
17
33
|
}
|
|
18
34
|
}
|
|
19
35
|
return value;
|
|
20
36
|
}
|
|
37
|
+
export function workspaceFeatures(configuration) {
|
|
38
|
+
const raw = configuration && "features" in configuration
|
|
39
|
+
? configuration.features
|
|
40
|
+
: configuration;
|
|
41
|
+
return {
|
|
42
|
+
prompts: raw?.prompts !== false,
|
|
43
|
+
skills: raw?.skills !== false,
|
|
44
|
+
mcp: raw?.mcp !== false,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
21
47
|
export function workspaceConfigPath(root) {
|
|
22
48
|
return join(root, ".pbot", "config.json");
|
|
23
49
|
}
|
|
@@ -36,7 +62,13 @@ export async function readWorkspaceConfig(root) {
|
|
|
36
62
|
try {
|
|
37
63
|
const value = config(JSON.parse(content));
|
|
38
64
|
const settings = value;
|
|
39
|
-
return {
|
|
65
|
+
return {
|
|
66
|
+
path,
|
|
67
|
+
present: true,
|
|
68
|
+
enabled: settings.enabled === true,
|
|
69
|
+
...(settings.features ? { features: settings.features } : {}),
|
|
70
|
+
...(settings.acp ? { acp: settings.acp } : {}),
|
|
71
|
+
};
|
|
40
72
|
}
|
|
41
73
|
catch (error) {
|
|
42
74
|
return { path, present: true, enabled: false, error: `工作区配置无效 ${path}: ${String(error)}` };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ResolvedWorkspaceMcpServer, WorkspaceMcpConfiguration, WorkspaceMcpHttpServer, WorkspaceMcpServer } from "../../model/workspace-mcp.js";
|
|
2
|
+
export declare function isWorkspaceMcpServerName(name: string): boolean;
|
|
3
|
+
export declare function isWorkspaceMcpHttpServer(server: WorkspaceMcpServer): server is WorkspaceMcpHttpServer;
|
|
4
|
+
export declare function validateWorkspaceMcpServer(name: string, value: unknown): WorkspaceMcpServer;
|
|
5
|
+
export declare function workspaceMcpPath(root: string): string;
|
|
6
|
+
export declare function readWorkspaceMcpConfig(root: string): Promise<WorkspaceMcpConfiguration>;
|
|
7
|
+
export declare function expandEnvTemplate(value: string, env: NodeJS.ProcessEnv, context: string): string;
|
|
8
|
+
export declare function resolveWorkspaceMcpServer(root: string, name: string, server: WorkspaceMcpServer, env?: NodeJS.ProcessEnv): ResolvedWorkspaceMcpServer;
|
|
9
|
+
export declare function resolveWorkspaceMcpServers(root: string, configuration: WorkspaceMcpConfiguration, env?: NodeJS.ProcessEnv): ResolvedWorkspaceMcpServer[];
|
|
10
|
+
export declare function upsertWorkspaceMcpServer(root: string, name: string, server: WorkspaceMcpServer): Promise<WorkspaceMcpConfiguration>;
|
|
11
|
+
export declare function removeWorkspaceMcpServer(root: string, name: string): Promise<{
|
|
12
|
+
removed: boolean;
|
|
13
|
+
configuration: WorkspaceMcpConfiguration;
|
|
14
|
+
}>;
|