personal-bot 0.2.19 → 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 +17 -22
- package/resources/skills/provider-connections/SKILL.md +24 -40
- package/resources/templates/workspace-agents.md +8 -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,270 +0,0 @@
|
|
|
1
|
-
import { readFile, stat } from "node:fs/promises";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { existsSync, realpathSync } from "node:fs";
|
|
4
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import { AgentFiles, descriptor, launch } from "../storage/agent-files.js";
|
|
6
|
-
import { AgentRun } from "../services/agents/agent-run.js";
|
|
7
|
-
import { TeamFiles } from "../storage/team-files.js";
|
|
8
|
-
import { TeamService } from "../services/agents/team-service.js";
|
|
9
|
-
import { teamMessageTool } from "../tools/teams/tool.js";
|
|
10
|
-
import { teamContext } from "../integrations/pi/team-context.js";
|
|
11
|
-
import { teamBrief } from "../integrations/team-context.js";
|
|
12
|
-
import { readAgentSession } from "../integrations/pi/pi-agents.js";
|
|
13
|
-
import type { TeamLink } from "../model/team.js";
|
|
14
|
-
import { authorizeMutation } from "../services/workspace/workspace-files.js";
|
|
15
|
-
import { agentText } from "../integrations/agent-resources.js";
|
|
16
|
-
import { agentInput } from "../integrations/pi/agent-input.js";
|
|
17
|
-
import { agentFileTools } from "../integrations/pi/agent-tools.js";
|
|
18
|
-
import { resourceRoot } from "../common/paths.js";
|
|
19
|
-
import { requireThat } from "../common/validation.js";
|
|
20
|
-
import { packageVersion, releaseLine } from "../common/version.js";
|
|
21
|
-
import { readWorkspaceConfig, workspaceDisabledMessage } from "../services/workspace/workspace-config.js";
|
|
22
|
-
|
|
23
|
-
/** Explicit child composition; never installs main commands or approval capabilities. */
|
|
24
|
-
export default async function subagent(pi: ExtensionAPI) {
|
|
25
|
-
const id = process.env.PBOT_SUBAGENT_RUN_ID, directory = process.env.PBOT_SUBAGENT_DIR, generation = process.env.PBOT_SUBAGENT_GENERATION;
|
|
26
|
-
requireThat(id && directory && generation, "缺少子会话启动身份。");
|
|
27
|
-
const files = new AgentFiles(dirname(realpathSync(directory)));
|
|
28
|
-
requireThat(files.directory(id) === realpathSync(directory), "子会话路径不匹配。");
|
|
29
|
-
const d = files.read(id, "descriptor.json", descriptor);
|
|
30
|
-
requireThat(d.agentId === id && releaseLine(d.release) === releaseLine(packageVersion), "子会话身份或版本无效。");
|
|
31
|
-
requireThat(files.read(id, "launch.json", launch).generation === generation, "子会话启动代次已失效。");
|
|
32
|
-
const configuration = await readWorkspaceConfig(d.workspace);
|
|
33
|
-
requireThat(configuration.enabled, workspaceDisabledMessage(configuration));
|
|
34
|
-
const teams = new TeamService(d.workspace, files, new TeamFiles(files));
|
|
35
|
-
const lifetime = new AbortController();
|
|
36
|
-
let activeTeam: TeamLink | undefined, pendingPause: string | undefined;
|
|
37
|
-
let ready = false, disposed = false;
|
|
38
|
-
let ctx: ExtensionContext | undefined, timer: ReturnType<typeof setInterval> | undefined;
|
|
39
|
-
let initialized = false;
|
|
40
|
-
let processing = false, closing = false, startLeaf: string | null = null;
|
|
41
|
-
const execution = new AgentRun(files, id, generation);
|
|
42
|
-
let lastActivity = 0;
|
|
43
|
-
const save = execution.save.bind(execution);
|
|
44
|
-
const ensure = () => {
|
|
45
|
-
lifetime.signal.throwIfAborted(); files.check();
|
|
46
|
-
const run = files.read(id, "launch.json", launch);
|
|
47
|
-
requireThat(run.generation === generation && !run.stop, "子会话已停止或启动代次变化。");
|
|
48
|
-
requireThat(initialized, "子会话尚未就绪。");
|
|
49
|
-
};
|
|
50
|
-
const bindSession = () => {
|
|
51
|
-
const file = ctx?.sessionManager.getSessionFile();
|
|
52
|
-
requireThat(file, "子会话必须使用持久原生会话。");
|
|
53
|
-
files.bindSession(id, file);
|
|
54
|
-
};
|
|
55
|
-
const pauseTeam = (reason: string) => {
|
|
56
|
-
pendingPause = reason;
|
|
57
|
-
try { teams.pauseMember(id, reason); pendingPause = undefined; }
|
|
58
|
-
catch (error) { if ((error as NodeJS.ErrnoException).code !== "PBOT_CONTROL_BUSY") throw error; }
|
|
59
|
-
};
|
|
60
|
-
const finish = (aborted: boolean) => {
|
|
61
|
-
if (!ctx || !execution.current) return;
|
|
62
|
-
const branch = ctx.sessionManager.getBranch();
|
|
63
|
-
const at = startLeaf === null ? -1 : branch.findIndex(e => e.id === startLeaf);
|
|
64
|
-
const last = branch.slice(at + 1).filter(e => e.type === "message" && e.message.role === "assistant").at(-1);
|
|
65
|
-
const assistant = last?.type === "message" && last.message.role === "assistant" ? last.message : undefined;
|
|
66
|
-
const text = assistant?.content.filter(p => p.type === "text").map(p => p.text).join("\n") ?? "";
|
|
67
|
-
execution.finish({ position: assistant ? ctx.sessionManager.getLeafId() : null, answer: text,
|
|
68
|
-
state: aborted ? "cancelled" : assistant?.stopReason === "stop" ? "completed" : "failed",
|
|
69
|
-
...(!assistant || assistant.stopReason !== "stop" ? { error: assistant?.errorMessage ?? "没有有效最终回答。" } : {}) });
|
|
70
|
-
activeTeam = undefined;
|
|
71
|
-
};
|
|
72
|
-
const shutdown = async () => {
|
|
73
|
-
if (closing) return;
|
|
74
|
-
closing = true;
|
|
75
|
-
lifetime.abort(); ctx?.abort();
|
|
76
|
-
try {
|
|
77
|
-
teams.files.setReady(id, generation, false); ready = false;
|
|
78
|
-
pauseTeam("成员停止");
|
|
79
|
-
save({ state: "stopping", activity: "停止" });
|
|
80
|
-
} finally { ctx?.shutdown(); }
|
|
81
|
-
};
|
|
82
|
-
const pump = async () => {
|
|
83
|
-
if (processing || !ctx || closing) return;
|
|
84
|
-
processing = true;
|
|
85
|
-
try {
|
|
86
|
-
const run = files.read(id, "launch.json", launch);
|
|
87
|
-
if (run.generation !== generation || run.stop) { await shutdown(); return; }
|
|
88
|
-
if (pi.getSessionName() !== run.name) pi.setSessionName(run.name);
|
|
89
|
-
if (pendingPause) { pauseTeam(pendingPause); if (pendingPause) return; }
|
|
90
|
-
const capable = pi.getActiveTools().includes("pbot_team_message");
|
|
91
|
-
if (capable !== ready) {
|
|
92
|
-
teams.files.setReady(id, generation, capable); ready = capable;
|
|
93
|
-
if (!ready) pauseTeam("成员团队工具已停用");
|
|
94
|
-
}
|
|
95
|
-
if (activeTeam) {
|
|
96
|
-
if (Date.now() >= activeTeam.run.deadlineAt) {
|
|
97
|
-
pauseTeam("已到期限");
|
|
98
|
-
if (!execution.interactive && execution.current?.source !== "mixed") {
|
|
99
|
-
ctx.abort();
|
|
100
|
-
if (!execution.current && ctx.isIdle()) await shutdown();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
const links = teams.links();
|
|
106
|
-
for (const request of files.messages(id)) {
|
|
107
|
-
if (request.generation !== generation || execution.accepted(request.requestId)) continue;
|
|
108
|
-
const link = links.get(request.requestId);
|
|
109
|
-
if (link && (link.team.closed || link.run.state !== "running" || link.run.deadlineAt <= Date.now())) continue;
|
|
110
|
-
if (link && (!ctx.isIdle() || ctx.hasPendingMessages() || execution.current || execution.interactive || execution.pending)) break;
|
|
111
|
-
const content = await agentInput(d.cwd, request, lifetime.signal);
|
|
112
|
-
requireThat(ctx.model, "子会话没有可用模型。");
|
|
113
|
-
requireThat(ctx.modelRegistry.hasConfiguredAuth(ctx.model) || await ctx.modelRegistry.getProviderAuth(ctx.model.provider), "子会话模型未认证,请配置原生 Pi 认证后显式恢复。");
|
|
114
|
-
ensure();
|
|
115
|
-
const accept = () => {
|
|
116
|
-
bindSession();
|
|
117
|
-
pi.appendEntry("pbot.agent-input", { requestId: request.requestId, generation });
|
|
118
|
-
execution.claim(request.requestId);
|
|
119
|
-
execution.consume(request.requestId);
|
|
120
|
-
};
|
|
121
|
-
if (link) {
|
|
122
|
-
if (!ctx.isIdle() || ctx.hasPendingMessages() || execution.current || execution.interactive || execution.pending) break;
|
|
123
|
-
activeTeam = teams.claim(id, generation, request.requestId, accept);
|
|
124
|
-
if (!activeTeam) continue;
|
|
125
|
-
} else accept();
|
|
126
|
-
pi.sendUserMessage(content, ctx.isIdle() ? {} : { deliverAs: request.delivery });
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
} catch (error) {
|
|
130
|
-
if ((error as NodeJS.ErrnoException).code === "PBOT_CONTROL_BUSY") return;
|
|
131
|
-
save({ state: "error", activity: "控制失败", error: String(error) });
|
|
132
|
-
await shutdown();
|
|
133
|
-
} finally { processing = false; }
|
|
134
|
-
};
|
|
135
|
-
pi.registerTool({ ...teamMessageTool(teams, { check: ensure }, () => ({
|
|
136
|
-
kind: "member", agentId: id, generation,
|
|
137
|
-
...(activeTeam && execution.current?.source === "parent" ? { requestId: activeTeam.request.requestId } : {}),
|
|
138
|
-
})), namespace: { name: "pbot", description: "团队公共通信。" } });
|
|
139
|
-
pi.on("resources_discover", async () => {
|
|
140
|
-
if (d.options.noSkills) return {};
|
|
141
|
-
const skillPaths = [join(resourceRoot, "skills")];
|
|
142
|
-
const workspaceSkills = join(d.workspace, ".agents/skills");
|
|
143
|
-
try {
|
|
144
|
-
if ((await stat(workspaceSkills)).isDirectory()) skillPaths.push(workspaceSkills);
|
|
145
|
-
} catch (error) {
|
|
146
|
-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
147
|
-
}
|
|
148
|
-
return { skillPaths };
|
|
149
|
-
});
|
|
150
|
-
pi.on("session_start", async (_event, context) => {
|
|
151
|
-
ctx = context;
|
|
152
|
-
try {
|
|
153
|
-
const run = files.read(id, "launch.json", launch);
|
|
154
|
-
requireThat(run.generation === generation && !run.stop, "子会话启动代次已失效或已停止。");
|
|
155
|
-
teams.files.setReady(id, generation, false);
|
|
156
|
-
pauseTeam("子会话载入:旧批次不自动恢复");
|
|
157
|
-
const file = ctx.sessionManager.getSessionFile(), previous = files.session(id);
|
|
158
|
-
requireThat(realpathSync(ctx.cwd) === d.cwd && ctx.sessionManager.getSessionId() === id && file &&
|
|
159
|
-
(previous === null || previous === file), "子会话身份、工作目录或文件不匹配。");
|
|
160
|
-
// Native setup-only sessions have a path but no file. Do not manufacture a conversation to persist it.
|
|
161
|
-
if (previous !== null || existsSync(file)) { readAgentSession(d, file, true); bindSession(); }
|
|
162
|
-
const receipts: { requestId: string; generation: string }[] = [];
|
|
163
|
-
for (const entry of ctx.sessionManager.getEntries()) if (entry.type === "custom" && entry.customType === "pbot.agent-input") {
|
|
164
|
-
const receipt = entry.data as { requestId?: string; generation?: string };
|
|
165
|
-
if (typeof receipt.requestId === "string" && typeof receipt.generation === "string") receipts.push({ requestId: receipt.requestId, generation: receipt.generation });
|
|
166
|
-
}
|
|
167
|
-
execution.recover(receipts);
|
|
168
|
-
const active = pi.getActiveTools();
|
|
169
|
-
for (const tool of agentFileTools(d.cwd, lifetime.signal, async (path, change, cancel) => {
|
|
170
|
-
ensure();
|
|
171
|
-
await authorizeMutation(d.workspace, path, change, async () => false, cancel);
|
|
172
|
-
ensure();
|
|
173
|
-
})) pi.registerTool(tool);
|
|
174
|
-
// Keep the native CLI loadout, including MCP exposure and explicit --tools choices.
|
|
175
|
-
pi.setActiveTools(active);
|
|
176
|
-
initialized = true;
|
|
177
|
-
save({ state: "idle", activity: "空闲" });
|
|
178
|
-
ready = !pendingPause && pi.getActiveTools().includes("pbot_team_message");
|
|
179
|
-
teams.files.setReady(id, generation, ready);
|
|
180
|
-
const tick = () => void pump().catch(error => {
|
|
181
|
-
closing = true; lifetime.abort(); ctx?.abort();
|
|
182
|
-
console.error("子会话控制失败:", error); ctx?.shutdown();
|
|
183
|
-
});
|
|
184
|
-
timer = setInterval(tick, 250); timer.unref(); tick();
|
|
185
|
-
} catch (error) {
|
|
186
|
-
try { save({ state: "error", activity: "启动失败", error: String(error) }); }
|
|
187
|
-
finally { await shutdown(); }
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
pi.on("before_agent_start", async (event, context) => {
|
|
191
|
-
ctx = context;
|
|
192
|
-
try {
|
|
193
|
-
ensure(); bindSession();
|
|
194
|
-
const workspace = await readFile(join(resourceRoot, "prompts/workspace.md"), "utf8");
|
|
195
|
-
const role = await agentText("general-purpose", d.workspace);
|
|
196
|
-
let brief: string | undefined, message;
|
|
197
|
-
if (activeTeam && !execution.interactive && execution.current?.source !== "mixed") {
|
|
198
|
-
const link = teams.links().get(activeTeam.request.requestId);
|
|
199
|
-
requireThat(link, "团队关联不可读取。");
|
|
200
|
-
brief = teamBrief(link);
|
|
201
|
-
message = teamContext(link, context, link.request.throughSeq);
|
|
202
|
-
}
|
|
203
|
-
Object.assign(event.systemPromptOptions.sections, {
|
|
204
|
-
pbot_environment: JSON.stringify({ workspaceRoot: d.workspace, resourceRoot }),
|
|
205
|
-
pbot_workspace: workspace, pbot_child: role,
|
|
206
|
-
});
|
|
207
|
-
if (brief) event.systemPromptOptions.sections.pbot_team = brief;
|
|
208
|
-
else delete event.systemPromptOptions.sections.pbot_team;
|
|
209
|
-
return message ? { message } : undefined;
|
|
210
|
-
} catch (error) {
|
|
211
|
-
context.abort(); throw error;
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
pi.on("turn_end", (event, context) => {
|
|
215
|
-
if (!activeTeam || execution.current?.source !== "parent" || event.outcome === "aborted") return;
|
|
216
|
-
const link = teams.links().get(activeTeam.request.requestId);
|
|
217
|
-
if (!link) return;
|
|
218
|
-
const message = teamContext(link, context);
|
|
219
|
-
if (message) return { entries: [...event.entries, message] };
|
|
220
|
-
return;
|
|
221
|
-
});
|
|
222
|
-
pi.on("input", async event => {
|
|
223
|
-
try { ensure(); bindSession(); }
|
|
224
|
-
catch (error) {
|
|
225
|
-
try { save({ state: "error", activity: "会话引用失败", error: String(error) }); }
|
|
226
|
-
catch { /* A failed store must not allow the native input fallback. */ }
|
|
227
|
-
try { await shutdown(); } catch { ctx?.shutdown(); }
|
|
228
|
-
return { action: "handled" };
|
|
229
|
-
}
|
|
230
|
-
if (event.source !== "extension") {
|
|
231
|
-
execution.humanInput();
|
|
232
|
-
pauseTeam("成员由人工接管");
|
|
233
|
-
}
|
|
234
|
-
return;
|
|
235
|
-
});
|
|
236
|
-
pi.on("agent_start", (_event, context) => {
|
|
237
|
-
ctx = context;
|
|
238
|
-
if (!execution.current) startLeaf = ctx.sessionManager.getLeafId();
|
|
239
|
-
execution.begin();
|
|
240
|
-
});
|
|
241
|
-
pi.on("tool_call", event => {
|
|
242
|
-
try {
|
|
243
|
-
ensure();
|
|
244
|
-
requireThat(event.toolName !== "pbot_agent" && event.toolName !== "pbot_team", "子会话禁止管理和委派。");
|
|
245
|
-
if (activeTeam && execution.current?.source === "parent" && Date.now() >= activeTeam.run.deadlineAt) {
|
|
246
|
-
ctx?.abort(); throw new Error("团队请求已到期限。");
|
|
247
|
-
}
|
|
248
|
-
save({ state: "busy", activity: event.toolName });
|
|
249
|
-
} catch (error) { return { block: true, reason: String(error) }; }
|
|
250
|
-
return;
|
|
251
|
-
});
|
|
252
|
-
pi.on("message_update", () => { if (execution.current && Date.now() - lastActivity > 1000) { lastActivity = Date.now(); save({ state: "busy", activity: "回答" }); } });
|
|
253
|
-
pi.on("agent_settled", (event, context) => { ctx = context; finish(event.aborted); });
|
|
254
|
-
pi.on("session_before_switch", () => ({ cancel: true }));
|
|
255
|
-
pi.on("session_before_fork", () => ({ cancel: true }));
|
|
256
|
-
pi.on("session_before_tree", (_event, context) => ({ cancel: !context.isIdle() }));
|
|
257
|
-
pi.on("session_shutdown", async event => {
|
|
258
|
-
if (disposed) return;
|
|
259
|
-
disposed = true;
|
|
260
|
-
if (timer) clearInterval(timer); timer = undefined; closing = true; lifetime.abort();
|
|
261
|
-
try {
|
|
262
|
-
teams.files.setReady(id, generation, false); ready = false;
|
|
263
|
-
pauseTeam("子会话关闭或重新载入");
|
|
264
|
-
if (execution.current) finish(true);
|
|
265
|
-
if (event.reason !== "reload" && execution.state !== "error") save({ state: "stopped", activity: "已停止" });
|
|
266
|
-
} finally {
|
|
267
|
-
initialized = false; teams.dispose(); ctx = undefined;
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { agentOwner } from "../integrations/pi/agent-context.js";
|
|
4
|
-
import type { WorkspaceInstance } from "./lifecycle.js";
|
|
5
|
-
import type { Confirmations } from "../ui/confirmation.js";
|
|
6
|
-
import { readPage } from "../ui/pi/reader.js";
|
|
7
|
-
import { choose } from "../ui/pi/menu.js";
|
|
8
|
-
import { stateLabel } from "../ui/presentation.js";
|
|
9
|
-
import { showAgents } from "./agents-command.js";
|
|
10
|
-
|
|
11
|
-
export async function showTeams(pi: ExtensionAPI, instance: WorkspaceInstance, ctx: ExtensionCommandContext, confirmations: Confirmations) {
|
|
12
|
-
instance.check();
|
|
13
|
-
const owner = () => agentOwner(ctx), views = () => instance.agents.list(owner()), service = instance.teams;
|
|
14
|
-
service.recover(owner());
|
|
15
|
-
if (ctx.mode !== "tui") { ctx.ui.notify(JSON.stringify({ teams: service.list(owner(), views()) }), "info"); return; }
|
|
16
|
-
if (confirmations.presenting) { ctx.ui.notify("请先处理待确认操作。", "info"); return; }
|
|
17
|
-
for (;;) {
|
|
18
|
-
instance.check(); const values = service.list(owner(), views());
|
|
19
|
-
if (!values.length) { ctx.ui.notify("还没有团队。请主助手为已有专家组队。", "info"); return; }
|
|
20
|
-
const options = values.map(v => v.name + " · " + (v.owned ? stateLabel(v.closed ? "closed" : v.run?.state ?? "open") + " · 在途 " + v.activeCount : "其他分支"));
|
|
21
|
-
const selected = await choose(ctx, "团队", options), picked = values[options.indexOf(selected ?? "")]; if (!picked) return;
|
|
22
|
-
if (!picked.owned) { ctx.ui.notify("请返回所属分支操作。", "info"); continue; }
|
|
23
|
-
for (;;) {
|
|
24
|
-
instance.check(); const value = service.status(picked.teamId, owner(), views()); if (!value.owned) break;
|
|
25
|
-
const action = await choose(ctx, value.name + " · " + stateLabel(value.closed ? "closed" : value.run?.state ?? "open"),
|
|
26
|
-
["状态", "公共记录", "成员会话", ...(!value.closed ? ["发布消息", ...(value.run?.state === "running" ? ["暂停"] : ["新批次"]), "关闭团队"] : [])]);
|
|
27
|
-
if (!action) break;
|
|
28
|
-
instance.check();
|
|
29
|
-
try {
|
|
30
|
-
if (action === "状态") {
|
|
31
|
-
const run = value.run;
|
|
32
|
-
await readPage(ctx, value.name, [value.brief, "", ...value.members.map(member => `${member.name} · ${member.role} · ${stateLabel(member.state)}`), "",
|
|
33
|
-
...(run ? [`批次 ${stateLabel(run.state)} · ${run.mode === "parallel" ? "并行" : "轮流"} · ${run.rounds} 轮`,
|
|
34
|
-
`进展 已安排 ${run.scheduled} · 已交付 ${run.published} · 在途 ${value.activeCount}`, ...(run.reason ? [run.reason] : [])] : ["尚无批次"])].join("\n"));
|
|
35
|
-
} else if (action === "成员会话") await showAgents(pi, instance, ctx, confirmations, value.members.map(m => m.agentId));
|
|
36
|
-
else if (action === "暂停") { service.pause(value.teamId, owner()); ctx.ui.notify("已暂停新安排。", "info"); }
|
|
37
|
-
else if (action === "关闭团队") { service.close(value.teamId, owner(), views()); ctx.ui.notify("团队已关闭。", "info"); }
|
|
38
|
-
else if (action === "发布消息") {
|
|
39
|
-
const text = await ctx.ui.editor("公共消息"); instance.check();
|
|
40
|
-
if (text?.trim()) { service.post(value.teamId, { kind: "user", owner: owner() }, randomUUID(), text); ctx.ui.notify("消息已发布。", "info"); }
|
|
41
|
-
} else if (action === "新批次") {
|
|
42
|
-
const strategy = await ctx.ui.select("讨论模式", ["轮流讨论", "并行轮次"]); if (!strategy) continue;
|
|
43
|
-
const instruction = await ctx.ui.editor("任务与交付要求"); if (!instruction?.trim()) continue;
|
|
44
|
-
const rounds = await ctx.ui.input("轮数(1–10,默认 2)"); if (rounds === undefined) continue;
|
|
45
|
-
instance.check(); service.run(value.teamId, owner(), { instruction, mode: strategy === "并行轮次" ? "parallel" : "roundRobin", rounds: rounds.trim() ? Number(rounds) : 2 }, views());
|
|
46
|
-
ctx.ui.notify("新批次已开始。", "info");
|
|
47
|
-
} else if (action === "公共记录") await messages(value.teamId);
|
|
48
|
-
} catch (error) { ctx.ui.notify(error instanceof Error ? error.message : String(error), "error"); }
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
async function messages(teamId: string) {
|
|
52
|
-
let page: { afterSeq?: number; seq?: number; offset?: number } = {};
|
|
53
|
-
for (;;) {
|
|
54
|
-
instance.check(); const data = service.read(teamId, { kind: "user", owner: owner() }, page);
|
|
55
|
-
await readPage(ctx, "公共记录", data.messages.map(message => `#${message.seq} ${message.author.name}${message.replyTo ? ` → #${message.replyTo}` : ""}\n${message.text}` +
|
|
56
|
-
(message.nextOffset === null ? "" : `\n[未完 · ${message.nextOffset}/${message.totalLength}]`)).join("\n\n") || "尚无消息。");
|
|
57
|
-
const last = data.messages.at(-1);
|
|
58
|
-
if (last?.nextOffset !== null && last?.nextOffset !== undefined) {
|
|
59
|
-
if (await ctx.ui.select("公共记录", ["继续本条", "返回"]) !== "继续本条") return;
|
|
60
|
-
page = { seq: last.seq, offset: last.nextOffset };
|
|
61
|
-
} else if (last && last.seq < data.lastSeq) {
|
|
62
|
-
if (await ctx.ui.select("公共记录", ["后续消息", "返回"]) !== "后续消息") return;
|
|
63
|
-
page = { afterSeq: last.seq };
|
|
64
|
-
} else return;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import type { Context } from "@deepseek-ai/cordis";
|
|
2
|
-
import type { Agent, AgentHandle, AgentSetup } from "@deepseek-ai/dsh-agent";
|
|
3
|
-
import { SessionId } from "@deepseek-ai/dsh-session";
|
|
4
|
-
import type { AgentDescriptor, AgentSessionReference } from "../../model/agent.js";
|
|
5
|
-
import type { AgentHost } from "../../services/agents/agent-service.js";
|
|
6
|
-
import { requireThat } from "../../common/validation.js";
|
|
7
|
-
import { openDshAgent, readDshAgentSession, readDshReport } from "./session.js";
|
|
8
|
-
|
|
9
|
-
type NativeRun = {
|
|
10
|
-
generation: string;
|
|
11
|
-
controller: AbortController;
|
|
12
|
-
opening: Promise<AgentHandle>;
|
|
13
|
-
handle?: AgentHandle;
|
|
14
|
-
closing?: Promise<void>;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/** Native handles belong to the execution component, independently of browser observers. */
|
|
18
|
-
export class DshAgents implements AgentHost {
|
|
19
|
-
readonly kind = "dsh";
|
|
20
|
-
readonly #runs = new Map<string, NativeRun>();
|
|
21
|
-
#closed = false;
|
|
22
|
-
constructor(readonly ctx: Context, readonly parent: Agent,
|
|
23
|
-
readonly composition: {
|
|
24
|
-
setup(d: AgentDescriptor, directory: string, generation: string): AgentSetup;
|
|
25
|
-
ready(agent: Agent, generation: string): void;
|
|
26
|
-
stopping(agent: Agent, generation: string): Promise<void>;
|
|
27
|
-
}, readonly lifetime: AbortSignal) {}
|
|
28
|
-
available() {
|
|
29
|
-
this.lifetime.throwIfAborted();
|
|
30
|
-
requireThat(!this.#closed && this.ctx.agents.get(this.parent.id) === this.parent, "dsh 父会话或执行组件已关闭。");
|
|
31
|
-
}
|
|
32
|
-
alive(id: string) {
|
|
33
|
-
const run = this.#runs.get(id);
|
|
34
|
-
return Boolean(this.ctx.agents.get(SessionId(id))) || Boolean(run && (run.closing || !run.handle));
|
|
35
|
-
}
|
|
36
|
-
private reference(d: AgentDescriptor, reference: AgentSessionReference) {
|
|
37
|
-
requireThat(reference.host === "dsh" && reference.sessionId === d.agentId, "不能接管其他宿主或其他身份的原生会话。");
|
|
38
|
-
}
|
|
39
|
-
async validateSession(d: AgentDescriptor, reference: AgentSessionReference) {
|
|
40
|
-
this.available(); this.reference(d, reference);
|
|
41
|
-
requireThat(!this.alive(d.agentId), "原生专家仍被占用,不能恢复。");
|
|
42
|
-
await readDshAgentSession(this.ctx, d);
|
|
43
|
-
requireThat(!this.alive(d.agentId), "原生专家仍被占用,不能恢复。");
|
|
44
|
-
}
|
|
45
|
-
async launch(d: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, resume: boolean, signal: AbortSignal) {
|
|
46
|
-
this.available(); this.reference(d, reference);
|
|
47
|
-
requireThat(!this.alive(d.agentId), "dsh 专家仍在运行或释放中。");
|
|
48
|
-
const previous = this.#runs.get(d.agentId);
|
|
49
|
-
if (previous) await this.release(d.agentId, previous.generation);
|
|
50
|
-
signal.throwIfAborted(); this.available();
|
|
51
|
-
const controller = new AbortController();
|
|
52
|
-
const combined = AbortSignal.any([signal, this.lifetime, controller.signal]);
|
|
53
|
-
const opening = Promise.resolve().then(() => openDshAgent(this.ctx, d, this.parent, resume,
|
|
54
|
-
this.composition.setup(d, directory, generation), combined));
|
|
55
|
-
const run: NativeRun = { generation, controller, opening };
|
|
56
|
-
this.#runs.set(d.agentId, run);
|
|
57
|
-
try {
|
|
58
|
-
run.handle = await opening;
|
|
59
|
-
if (combined.aborted) {
|
|
60
|
-
await this.release(d.agentId, generation);
|
|
61
|
-
combined.throwIfAborted();
|
|
62
|
-
}
|
|
63
|
-
this.composition.ready(run.handle.agent, generation);
|
|
64
|
-
} catch (error) {
|
|
65
|
-
if (!run.handle && this.#runs.get(d.agentId) === run) this.#runs.delete(d.agentId);
|
|
66
|
-
throw error;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
release(id: string, generation: string) {
|
|
70
|
-
const run = this.#runs.get(id);
|
|
71
|
-
if (!run) return this.ctx.agents.get(SessionId(id)) ? Promise.reject(new Error("原生专家仍由其他执行实例持有;当前组件不能释放。")) : Promise.resolve();
|
|
72
|
-
if (run.generation !== generation) return Promise.resolve();
|
|
73
|
-
if (run.closing) return run.closing;
|
|
74
|
-
run.controller.abort();
|
|
75
|
-
run.closing = (async () => {
|
|
76
|
-
const handle = run.handle ?? await run.opening.catch(() => undefined);
|
|
77
|
-
let failure: unknown;
|
|
78
|
-
if (handle) {
|
|
79
|
-
try { await this.composition.stopping(handle.agent, generation); } catch (error) { failure = error; }
|
|
80
|
-
await handle.dispose();
|
|
81
|
-
}
|
|
82
|
-
if (this.#runs.get(id) === run) this.#runs.delete(id);
|
|
83
|
-
if (failure) throw failure;
|
|
84
|
-
})();
|
|
85
|
-
return run.closing;
|
|
86
|
-
}
|
|
87
|
-
report(d: AgentDescriptor, reference: AgentSessionReference, position: string) {
|
|
88
|
-
this.reference(d, reference);
|
|
89
|
-
return readDshReport(this.ctx, d, position);
|
|
90
|
-
}
|
|
91
|
-
async close() {
|
|
92
|
-
this.#closed = true;
|
|
93
|
-
const results = await Promise.allSettled([...this.#runs].map(([id, run]) => this.release(id, run.generation)));
|
|
94
|
-
const errors = results.flatMap(r => r.status === "rejected" ? [r.reason] : []);
|
|
95
|
-
if (errors.length) throw new AggregateError(errors, "dsh 专家仍有未释放的执行资源。");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { basename, extname } from "node:path";
|
|
2
|
-
import type { Context } from "@deepseek-ai/cordis";
|
|
3
|
-
import { createUserMessage, type FileBlock, type ImageBlock } from "@deepseek-ai/dsh-llm";
|
|
4
|
-
import type { AttachmentAdmissionPart, ImageMediaType } from "@deepseek-ai/dsh-attachment";
|
|
5
|
-
import type { FsTarget } from "@deepseek-ai/dsh-fs";
|
|
6
|
-
import type { AgentDescriptor, AgentMessage } from "../../model/agent.js";
|
|
7
|
-
import { requireThat } from "../../common/validation.js";
|
|
8
|
-
import "./messages.js";
|
|
9
|
-
|
|
10
|
-
const imageTypes: Record<string, ImageMediaType> = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp", ".gif": "image/gif" };
|
|
11
|
-
|
|
12
|
-
/** Native file attachments remain verbatim; image admission uses the host's policy. */
|
|
13
|
-
async function contentFor(ctx: Context, cwd: string, message: string, files: readonly string[], signal: AbortSignal) {
|
|
14
|
-
const content: AttachmentAdmissionPart[] = [];
|
|
15
|
-
let imageCount = 0, imageBytes = 0;
|
|
16
|
-
for (const path of files) {
|
|
17
|
-
signal.throwIfAborted();
|
|
18
|
-
const target = await ctx.fs.resolve(path, { cwd, signal });
|
|
19
|
-
const before = await ctx.fs.stat(target, signal);
|
|
20
|
-
requireThat(before?.type === "file", "附件必须是普通文件:" + path);
|
|
21
|
-
const name = basename(path), mediaType = imageTypes[extname(path).toLowerCase()];
|
|
22
|
-
if (mediaType) {
|
|
23
|
-
const limits = ctx.attachments.imageLimits;
|
|
24
|
-
requireThat(++imageCount <= limits.maxImagesPerMessage &&
|
|
25
|
-
(before.size === undefined || imageBytes + before.size <= limits.maxMessageImageBytes), "图片附件超过宿主消息准入上限。");
|
|
26
|
-
const data = await ctx.fs.readBytes(target, signal, limits.maxImageBytes);
|
|
27
|
-
imageBytes += data.byteLength;
|
|
28
|
-
requireThat(imageBytes <= limits.maxMessageImageBytes, "图片附件超过宿主消息准入上限。");
|
|
29
|
-
content.push({ type: "image", mediaType, name, data: Buffer.from(data).toString("base64") });
|
|
30
|
-
} else {
|
|
31
|
-
async function* bytes(file: FsTarget) {
|
|
32
|
-
let offset = 0;
|
|
33
|
-
for (;;) {
|
|
34
|
-
signal.throwIfAborted();
|
|
35
|
-
const chunk = await ctx.fs.readByteRange(file, { offset, length: 256 * 1024 }, signal);
|
|
36
|
-
if (!chunk.length) break;
|
|
37
|
-
yield chunk; offset += chunk.length;
|
|
38
|
-
}
|
|
39
|
-
requireThat((await ctx.fs.stat(file, signal))?.version === before?.version, "读取期间附件已变化:" + path);
|
|
40
|
-
}
|
|
41
|
-
content.push({ type: "file", attachment: await ctx.attachments.saveFileStream({ data: bytes(target), name, signal }) });
|
|
42
|
-
}
|
|
43
|
-
requireThat((await ctx.fs.stat(target, signal))?.version === before.version, "读取期间附件已变化:" + path);
|
|
44
|
-
content.push({ type: "text", text: "附件原始路径:" + JSON.stringify(path) + "。原生附件保留本次文件内容;需要查看时使用宿主提供的附件路径。" });
|
|
45
|
-
}
|
|
46
|
-
content.push({ type: "text", text: message || "请处理所附文件。" });
|
|
47
|
-
const admitted = await ctx.attachments.admitPromptContent(content);
|
|
48
|
-
signal.throwIfAborted(); return admitted;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export async function dshInput(ctx: Context, d: AgentDescriptor, request: AgentMessage, signal: AbortSignal) {
|
|
52
|
-
const content = await contentFor(ctx, d.cwd, request.message, request.files, signal);
|
|
53
|
-
return createUserMessage({ content, source: { kind: "pbot-input", form: "relay",
|
|
54
|
-
requestId: request.requestId, generation: request.generation, parentSessionId: d.parentSessionId } });
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Command attachments have already passed native upload admission; the caller never supplies arbitrary references. */
|
|
58
|
-
export async function dshHumanInput(ctx: Context, cwd: string, text: string, files: readonly string[],
|
|
59
|
-
attachments: readonly (ImageBlock | FileBlock)[], signal: AbortSignal) {
|
|
60
|
-
requireThat(files.length + attachments.length <= 32, "一次最多附加 32 个文件。");
|
|
61
|
-
requireThat(!files.length || !attachments.length, "一次对话请选择服务器文件或输入框附件,不能同时使用。");
|
|
62
|
-
signal.throwIfAborted();
|
|
63
|
-
const content = attachments.length ? [...attachments, { type: "text" as const, text: text || "请处理所附文件。" }] :
|
|
64
|
-
await contentFor(ctx, cwd, text, files, signal);
|
|
65
|
-
signal.throwIfAborted(); return createUserMessage({ content, source: { kind: "user" } });
|
|
66
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { SessionEvent } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import type { UserMessage } from "@deepseek-ai/dsh-llm";
|
|
3
|
-
import type {} from "@deepseek-ai/dsh-tool-executor-ptc";
|
|
4
|
-
import { object } from "../../common/json.js";
|
|
5
|
-
import "./messages.js";
|
|
6
|
-
|
|
7
|
-
function keys(value: unknown, field: string) {
|
|
8
|
-
if (!object(value)) return [];
|
|
9
|
-
const items = value[field];
|
|
10
|
-
return Array.isArray(items) && items.every(item => typeof item === "string") ? items as string[] : [];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Only committed native context or successful model tool results claim notices.
|
|
14
|
-
* PTC inner reads require their enclosing transport to have committed success. */
|
|
15
|
-
export function deliveredDshNotices(events: readonly SessionEvent[]) {
|
|
16
|
-
const delivered = new Set<string>();
|
|
17
|
-
const reads = new Set<string>(), nested = new Map<string, string[]>();
|
|
18
|
-
for (const event of events) {
|
|
19
|
-
if (event.type === "turn/start" || event.type === "step/start" || event.type === "turn/end") { reads.clear(); nested.clear(); }
|
|
20
|
-
if (event.type === "user/message" && event.data.source.kind === "pbot-results") {
|
|
21
|
-
for (const key of keys(event.data.source, "keys")) delivered.add(key);
|
|
22
|
-
}
|
|
23
|
-
if (event.type === "tool/call" && event.data.name === "pbot_agent") {
|
|
24
|
-
try { const args: unknown = JSON.parse(event.data.arguments); if (object(args) && args.action === "read") reads.add(event.data.callId); } catch { /* An invalid call cannot claim a report. */ }
|
|
25
|
-
}
|
|
26
|
-
if (event.type === "tool/ptc-dispatch" && event.data.name === "pbot_agent" && !event.data.isError &&
|
|
27
|
-
object(event.data.arguments) && event.data.arguments.action === "read") {
|
|
28
|
-
const values = nested.get(event.data.rootCallId) ?? [];
|
|
29
|
-
values.push(...keys(event.data.meta, "pbotAgentReadKeys")); nested.set(event.data.rootCallId, values);
|
|
30
|
-
}
|
|
31
|
-
if (event.type === "tool/result") {
|
|
32
|
-
const message = event.data.message;
|
|
33
|
-
if (!message.isError) {
|
|
34
|
-
if (reads.has(message.toolCallId)) for (const key of keys(event.data.meta, "pbotAgentReadKeys")) delivered.add(key);
|
|
35
|
-
for (const key of nested.get(message.toolCallId) ?? []) delivered.add(key);
|
|
36
|
-
}
|
|
37
|
-
reads.delete(message.toolCallId); nested.delete(message.toolCallId);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return delivered;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Pending inbox entries prevent duplicate submission, but are not delivery receipts. */
|
|
44
|
-
export function pendingDshNotices(messages: readonly UserMessage[]) {
|
|
45
|
-
return messages.flatMap(message => message.source.kind === "pbot-results" ? keys(message.source, "keys") : []);
|
|
46
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { AgentOwner } from "../../model/agent.js";
|
|
3
|
-
export function agentOwner(ctx: ExtensionContext): AgentOwner {
|
|
4
|
-
return { sessionId: ctx.sessionManager.getSessionId(), leaf: ctx.sessionManager.getLeafId(), branch: ctx.sessionManager.getBranch().map(e => e.id) };
|
|
5
|
-
}
|
|
6
|
-
export function deliveredCycles(ctx: ExtensionContext): Set<string> {
|
|
7
|
-
const keys = new Set<string>();
|
|
8
|
-
for (const entry of ctx.sessionManager.getBranch()) {
|
|
9
|
-
const data = entry.type === "custom_message" && ["pbot.agent-results", "pbot.agent-error", "pbot.agent-recovered"].includes(entry.customType) ? entry.details
|
|
10
|
-
: entry.type === "custom" && entry.customType === "pbot.agent-read" ? entry.data : undefined;
|
|
11
|
-
if (entry.type === "custom" && entry.customType === "pbot.agent-read") {
|
|
12
|
-
const receipt = entry.data as { toolCallId?: string };
|
|
13
|
-
if (!ctx.sessionManager.getBranch().some(e => e.type === "message" && e.message.role === "toolResult" && e.message.toolCallId === receipt.toolCallId)) continue;
|
|
14
|
-
}
|
|
15
|
-
if (data && typeof data === "object" && "keys" in data && Array.isArray(data.keys)) for (const key of data.keys) if (typeof key === "string") keys.add(key);
|
|
16
|
-
}
|
|
17
|
-
return keys;
|
|
18
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
import { createReadTool } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import type { TextContent, ImageContent } from "@earendil-works/pi-ai";
|
|
4
|
-
import type { AgentMessage } from "../../model/agent.js";
|
|
5
|
-
|
|
6
|
-
export async function agentInput(cwd: string, request: Pick<AgentMessage, "requestId" | "files" | "message">,
|
|
7
|
-
signal?: AbortSignal): Promise<(TextContent | ImageContent)[]> {
|
|
8
|
-
const content: (TextContent | ImageContent)[] = [];
|
|
9
|
-
const read = createReadTool(cwd);
|
|
10
|
-
for (const file of request.files) {
|
|
11
|
-
const path = resolve(cwd, file);
|
|
12
|
-
const output = await read.execute("file-" + request.requestId, { path }, signal);
|
|
13
|
-
content.push({ type: "text", text: "附件来源(绝对 path):" + JSON.stringify(path) +
|
|
14
|
-
"\n以下为原生 read 内容;如有截断,按原生提示用此 path 与 offset/limit 续读。" });
|
|
15
|
-
content.push(...output.content.filter((part): part is TextContent | ImageContent => part.type === "text" || part.type === "image"));
|
|
16
|
-
}
|
|
17
|
-
content.push({ type: "text", text: request.message || "请处理所附文件。" });
|
|
18
|
-
return content;
|
|
19
|
-
}
|