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