personal-bot 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +2 -1
- package/DISCLAIMER.md +17 -0
- package/LICENSE +21 -0
- package/README.md +15 -5
- package/README.zh-CN.md +15 -5
- package/bin/pbot-pi-acp.mjs +1 -1
- package/bin/pbot.mjs +22 -2
- package/docs/README.md +2 -0
- package/docs/architecture/design.md +16 -16
- package/docs/architecture/extensibility.md +8 -2
- package/docs/architecture/model-context.md +3 -2
- package/docs/architecture/principles.md +2 -2
- package/docs/guides/capabilities.md +1 -1
- package/docs/guides/cross-harness-workers.md +3 -1
- package/docs/guides/history.md +30 -7
- package/docs/guides/workspace-configuration.md +7 -3
- package/docs/hosts/antigravity.md +1 -1
- package/docs/hosts/claude-code.md +1 -1
- package/docs/hosts/codex-qoder.md +63 -0
- package/docs/maintainers/architecture-governance.md +78 -0
- package/lib/antigravity-plugin/DISCLAIMER.md +17 -0
- package/lib/antigravity-plugin/LICENSE +21 -0
- package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +36 -0
- package/lib/antigravity-plugin/lib/common/files.d.ts +2 -0
- package/lib/antigravity-plugin/lib/common/files.js +44 -0
- package/lib/antigravity-plugin/lib/common/validation.d.ts +4 -0
- package/lib/antigravity-plugin/lib/common/validation.js +8 -0
- package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +18 -0
- package/lib/antigravity-plugin/lib/extension/acp/member.js +44 -0
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +71 -45
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-entry.js +22 -0
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.d.ts +4 -0
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +58 -0
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/files.d.ts +5 -0
- package/lib/antigravity-plugin/lib/extension/hosts/dsh/files.js +67 -0
- package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.d.ts +3 -0
- package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +100 -0
- package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.d.ts +3 -1
- package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +31 -23
- package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.d.ts +6 -0
- package/lib/antigravity-plugin/lib/extension/hosts/workspace-mcp.js +21 -0
- package/lib/antigravity-plugin/lib/extension/mcp.js +2577 -2255
- package/lib/antigravity-plugin/lib/extension/workspace-cli.js +20127 -0
- package/lib/antigravity-plugin/lib/integrations/agent-resources.js +1 -1
- package/lib/antigravity-plugin/lib/integrations/hosts/claude/adapter.js +7 -59
- package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.d.ts +2 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/codex/adapter.js +14 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/mcp.d.ts +4 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/mcp.js +28 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/messages.d.ts +36 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/messages.js +11 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.d.ts +6 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.js +47 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/root-agents.d.ts +4 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/root-agents.js +3 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/skills.d.ts +8 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/dsh/skills.js +31 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/pi/agent-tools.js +32 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.d.ts +4 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/pi/mcp.js +23 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/plugin-registry.d.ts +1 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/plugin-registry.js +8 -1
- package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.js +76 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/qoder/adapter.js +16 -0
- package/lib/antigravity-plugin/lib/model/acp.d.ts +7 -3
- package/lib/antigravity-plugin/lib/model/acp.js +8 -0
- package/lib/antigravity-plugin/lib/model/agent.d.ts +121 -0
- package/lib/antigravity-plugin/lib/model/agent.js +4 -0
- package/lib/antigravity-plugin/lib/model/history.d.ts +10 -0
- package/lib/antigravity-plugin/lib/model/history.js +4 -1
- package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +5 -0
- package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +8 -12
- package/lib/antigravity-plugin/lib/model/workspace-config.js +2 -1
- package/lib/antigravity-plugin/lib/plugin.d.ts +2 -1
- package/lib/antigravity-plugin/lib/plugin.js +2 -2
- package/lib/antigravity-plugin/lib/services/history/history-capture.js +34 -22
- package/lib/antigravity-plugin/lib/services/history/summary-registry.js +2 -3
- package/lib/antigravity-plugin/lib/services/history/summary-runner.d.ts +2 -5
- package/lib/antigravity-plugin/lib/services/history/summary-runner.js +72 -74
- package/lib/antigravity-plugin/lib/services/history/workspace-history.d.ts +3 -0
- package/lib/antigravity-plugin/lib/services/history/workspace-history.js +6 -0
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +3 -0
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +78 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +9 -8
- package/lib/antigravity-plugin/lib/services/workspace/workspace-files.d.ts +2 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-files.js +17 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.d.ts +14 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-mcp.js +193 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +1 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +4 -2
- package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +11 -0
- package/lib/antigravity-plugin/lib/storage/acp-files.js +75 -0
- package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +31 -0
- package/lib/antigravity-plugin/lib/storage/agent-files.js +159 -0
- package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +3 -1
- package/lib/antigravity-plugin/lib/storage/atomic-json.js +43 -3
- package/lib/antigravity-plugin/lib/storage/history-files.d.ts +2 -1
- package/lib/antigravity-plugin/lib/storage/history-files.js +21 -15
- package/lib/antigravity-plugin/lib/storage/history-format.d.ts +2 -3
- package/lib/antigravity-plugin/lib/storage/history-format.js +5 -7
- package/lib/antigravity-plugin/package.json +1 -1
- package/lib/antigravity-plugin/plugin.json +1 -1
- package/lib/antigravity-plugin/resources/prompts/history-monthly-summary.md +13 -0
- package/lib/antigravity-plugin/resources/prompts/history-summary.md +13 -0
- package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
- package/lib/antigravity-plugin/resources/skills/.claude-plugin/plugin.json +4 -0
- package/lib/antigravity-plugin/resources/skills/project-maintenance/SKILL.md +35 -0
- package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +52 -0
- package/lib/antigravity-plugin/skills/pbot/SKILL.md +11 -1
- package/lib/client.js +22 -50
- package/lib/codex-marketplace/.agents/plugins/marketplace.json +17 -0
- package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +7 -0
- package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +17 -0
- package/lib/codex-marketplace/plugins/personal-bot/DISCLAIMER.md +17 -0
- package/lib/codex-marketplace/plugins/personal-bot/LICENSE +21 -0
- package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +37 -0
- package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +9 -0
- package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/agents/openai.yaml +2 -0
- package/lib/common/validation.d.ts +4 -0
- package/lib/common/validation.js +8 -0
- package/lib/dsh.js +2 -2
- package/lib/extension/acp/member.d.ts +1 -1
- package/lib/extension/acp-worker.js +71 -45
- package/lib/extension/history/command.d.ts +37 -0
- package/lib/extension/history/command.js +57 -0
- package/lib/extension/hosts/dsh/acp-entry.d.ts +1 -0
- package/lib/extension/hosts/dsh/acp-entry.js +22 -0
- package/lib/extension/hosts/dsh/acp-member.js +4 -18
- package/lib/extension/hosts/dsh/command.d.ts +1 -2
- package/lib/extension/hosts/dsh/command.js +5 -49
- package/lib/extension/hosts/dsh/workspace.js +13 -44
- package/lib/extension/hosts/pi/acp-member.d.ts +3 -0
- package/lib/extension/hosts/pi/acp-member.js +100 -0
- package/lib/extension/hosts/plugin-runtime.d.ts +3 -1
- package/lib/extension/hosts/plugin-runtime.js +31 -23
- package/lib/extension/hosts/workspace-mcp.d.ts +6 -0
- package/lib/extension/hosts/workspace-mcp.js +21 -0
- package/lib/extension/mcp/bridge.d.ts +6 -8
- package/lib/extension/mcp/bridge.js +66 -84
- package/lib/extension/mcp/tool-contract.d.ts +735 -0
- package/lib/extension/mcp/tool-contract.js +441 -0
- package/lib/extension/mcp/workers.d.ts +29 -742
- package/lib/extension/mcp/workers.js +75 -525
- package/lib/extension/mcp.js +2577 -2255
- package/lib/extension/setup/cli.js +3 -0
- package/lib/extension/workspace-cli.js +20127 -0
- package/lib/integrations/acp/agents.d.ts +2 -3
- package/lib/integrations/acp/harnesses/dsh.js +1 -1
- package/lib/integrations/acp/harnesses/presets.js +8 -0
- package/lib/integrations/acp/history-summary.js +2 -1
- package/lib/integrations/acp/launch.d.ts +2 -2
- package/lib/integrations/agent-resources.js +1 -1
- package/lib/integrations/hosts/claude/adapter.js +7 -59
- package/lib/integrations/hosts/codex/adapter.d.ts +2 -0
- package/lib/integrations/hosts/codex/adapter.js +14 -0
- package/lib/integrations/hosts/pi/agent-tools.d.ts +3 -0
- package/lib/integrations/hosts/pi/agent-tools.js +32 -0
- package/lib/integrations/hosts/pi/mcp.d.ts +4 -0
- package/lib/integrations/hosts/pi/mcp.js +23 -0
- package/lib/integrations/hosts/plugin-registry.d.ts +1 -0
- package/lib/integrations/hosts/plugin-registry.js +8 -1
- package/lib/integrations/hosts/prompt-hooks.d.ts +76 -0
- package/lib/integrations/hosts/prompt-hooks.js +76 -0
- package/lib/integrations/hosts/qoder/adapter.d.ts +2 -0
- package/lib/integrations/hosts/qoder/adapter.js +16 -0
- package/lib/model/acp.d.ts +7 -3
- package/lib/model/acp.js +8 -0
- package/lib/model/agent.d.ts +18 -4
- package/lib/model/agent.js +4 -1
- package/lib/model/history.d.ts +10 -0
- package/lib/model/history.js +4 -1
- package/lib/model/installation.js +1 -0
- package/lib/model/plugin-host.d.ts +5 -0
- package/lib/model/workspace-config.d.ts +8 -12
- package/lib/model/workspace-config.js +2 -1
- package/lib/plugin.d.ts +2 -1
- package/lib/plugin.js +2 -2
- package/lib/qoder-plugin/.mcp.json +17 -0
- package/lib/qoder-plugin/.qoder-plugin/plugin.json +7 -0
- package/lib/qoder-plugin/DISCLAIMER.md +17 -0
- package/lib/qoder-plugin/LICENSE +21 -0
- package/lib/qoder-plugin/hooks/hooks.json +37 -0
- package/lib/qoder-plugin/skills/pbot/SKILL.md +9 -0
- package/lib/services/agents/agent-service.d.ts +15 -45
- package/lib/services/agents/agent-service.js +22 -3
- package/lib/services/history/history-capture.js +34 -22
- package/lib/services/history/history-migrate.js +26 -60
- package/lib/services/history/history-query.js +38 -57
- package/lib/services/history/summary-command.d.ts +1 -1
- package/lib/services/history/summary-command.js +8 -7
- package/lib/services/history/summary-registry.js +2 -3
- package/lib/services/history/summary-runner.d.ts +2 -5
- package/lib/services/history/summary-runner.js +72 -74
- package/lib/services/history/workspace-history.d.ts +3 -0
- package/lib/services/history/workspace-history.js +6 -0
- package/lib/services/workspace/plugin-releases.d.ts +3 -0
- package/lib/services/workspace/plugin-releases.js +78 -0
- package/lib/services/workspace/workspace-config.js +9 -8
- package/lib/services/workspace/workspace-mcp.js +22 -29
- package/lib/services/workspace/workspace-skills.d.ts +1 -0
- package/lib/services/workspace/workspace-skills.js +4 -2
- package/lib/storage/acp-files.js +7 -12
- package/lib/storage/agent-files.js +6 -5
- package/lib/storage/atomic-json.d.ts +3 -1
- package/lib/storage/atomic-json.js +43 -3
- package/lib/storage/history-files.d.ts +2 -1
- package/lib/storage/history-files.js +21 -15
- package/lib/storage/history-format.d.ts +2 -3
- package/lib/storage/history-format.js +5 -7
- package/lib/tools/history/contract.d.ts +3 -11
- package/lib/tools/history/contract.js +8 -7
- package/lib/ui/dsh/elements.d.ts +2 -5
- package/lib/ui/dsh/elements.js +3 -16
- package/lib/ui/dsh/management.js +9 -9
- package/lib/ui/dsh/styles.d.ts +1 -1
- package/lib/ui/dsh/styles.js +5 -33
- package/package.json +4 -1
- package/resources/antigravity/skills/pbot/SKILL.md +11 -1
- package/resources/claude/skills/pbot/SKILL.md +11 -1
- package/resources/plugin/skills/pbot/SKILL.md +9 -0
- package/resources/prompts/history-coordinator.md +1 -0
- package/resources/prompts/plugin-coordinator.md +1 -0
- package/scripts/README.md +3 -2
- package/src/common/validation.ts +10 -0
- package/src/dsh.ts +2 -2
- package/src/extension/history/command.ts +68 -0
- package/src/extension/hosts/dsh/acp-entry.ts +21 -0
- package/src/extension/hosts/dsh/acp-member.ts +4 -15
- package/src/extension/hosts/dsh/command.ts +6 -46
- package/src/extension/hosts/dsh/workspace.ts +12 -38
- package/src/extension/hosts/pi/acp-member.ts +4 -10
- package/src/extension/hosts/pi/command.ts +14 -63
- package/src/extension/hosts/pi/events.ts +2 -8
- package/src/extension/hosts/pi/lifecycle.ts +3 -2
- package/src/extension/hosts/plugin-runtime.ts +32 -19
- package/src/extension/hosts/workspace-mcp.ts +25 -0
- package/src/extension/mcp/bridge.ts +80 -80
- package/src/extension/mcp/tool-contract.ts +462 -0
- package/src/extension/mcp/workers.ts +81 -551
- package/src/extension/setup/cli.ts +4 -1
- package/src/extension/setup/workspace-cli.ts +159 -0
- package/src/integrations/acp/agents.ts +2 -3
- package/src/integrations/acp/harnesses/dsh.ts +1 -1
- package/src/integrations/acp/harnesses/presets.ts +8 -0
- package/src/integrations/acp/history-summary.ts +2 -1
- package/src/integrations/agent-resources.ts +1 -1
- package/src/integrations/hosts/claude/adapter.ts +7 -53
- package/src/integrations/hosts/codex/adapter.ts +16 -0
- package/src/integrations/hosts/plugin-registry.ts +9 -1
- package/src/integrations/hosts/prompt-hooks.ts +72 -0
- package/src/integrations/hosts/qoder/adapter.ts +19 -0
- package/src/integrations/installation/cli-update.ts +116 -0
- package/src/integrations/installation/pi-bridge.ts +1 -1
- package/src/integrations/installation/pi-setup.ts +1 -1
- package/src/integrations/installation/processes.ts +1 -1
- package/src/model/acp.ts +11 -3
- package/src/model/agent.ts +20 -4
- package/src/model/history.ts +5 -0
- package/src/model/installation.ts +41 -0
- package/src/model/plugin-host.ts +5 -1
- package/src/model/workspace-config.ts +6 -12
- package/src/pbot.ts +13 -32
- package/src/plugin.ts +3 -3
- package/src/services/agents/agent-service.ts +29 -17
- package/src/services/history/history-capture.ts +30 -22
- package/src/services/history/history-migrate.ts +23 -50
- package/src/services/history/history-query.ts +34 -48
- package/src/services/history/summary-command.ts +9 -8
- package/src/services/history/summary-registry.ts +2 -3
- package/src/services/history/summary-runner.ts +72 -81
- package/src/services/history/workspace-history.ts +8 -0
- package/src/services/installation/setup-service.ts +2 -41
- package/src/services/workspace/plugin-releases.ts +68 -0
- package/src/services/workspace/workspace-config.ts +9 -8
- package/src/services/workspace/workspace-mcp.ts +23 -28
- package/src/services/workspace/workspace-skills.ts +5 -2
- package/src/storage/acp-files.ts +7 -12
- package/src/storage/agent-files.ts +6 -5
- package/src/storage/atomic-json.ts +31 -4
- package/src/storage/history-files.ts +22 -15
- package/src/storage/history-format.ts +6 -8
- package/src/tools/history/contract.ts +9 -9
- package/src/tools/history/pi-tool.ts +6 -4
- package/src/ui/dsh/elements.ts +5 -12
- package/src/ui/dsh/management.ts +10 -10
- package/src/ui/dsh/styles.ts +5 -33
- package/lib/antigravity-plugin/lib/services/history/history-migrate.d.ts +0 -12
- package/lib/antigravity-plugin/lib/services/history/history-migrate.js +0 -88
- package/lib/ui/presentation.d.ts +0 -3
- package/lib/ui/presentation.js +0 -12
- package/src/tools/access.ts +0 -7
- package/src/tools/register.ts +0 -21
- package/src/ui/presentation.ts +0 -12
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
1
|
import { realpath } from "node:fs/promises";
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
2
|
import * as McpClient from "@deepseek-ai/dsh-mcp-client";
|
|
5
3
|
import { rootAgents } from "../../../integrations/hosts/dsh/root-agents.js";
|
|
6
4
|
import { dshResources } from "../../../integrations/hosts/dsh/resources.js";
|
|
7
5
|
import { registerDshFileEvents } from "./files.js";
|
|
8
6
|
import { registerDshHistoryEvents } from "./history.js";
|
|
9
7
|
import { registerDshHistoryTool } from "../../../tools/history/dsh-tool.js";
|
|
10
|
-
import { resourceRoot } from "../../../common/paths.js";
|
|
11
8
|
import { requireThat } from "../../../common/validation.js";
|
|
12
9
|
import { toDshMcpConfig } from "../../../integrations/hosts/dsh/mcp.js";
|
|
13
10
|
import { registerDshSkills } from "../../../integrations/hosts/dsh/skills.js";
|
|
11
|
+
import { bindWorkspacePlugin } from "../../../services/workspace/plugin-releases.js";
|
|
14
12
|
import { readWorkspaceConfig, workspaceDisabledMessage, workspaceFeatures } from "../../../services/workspace/workspace-config.js";
|
|
15
|
-
import {
|
|
13
|
+
import { loadNativeWorkspaceServers } from "../workspace-mcp.js";
|
|
16
14
|
import { initializeWorkspace } from "../../../services/workspace/workspace-preparation.js";
|
|
17
|
-
import {
|
|
18
|
-
import { checkHistorySummaryReminder } from "../../../services/history/summary-runner.js";
|
|
15
|
+
import { historyStartupNotices } from "../../../services/history/summary-runner.js";
|
|
19
16
|
import { McpWorkerRuntime, defaultWorkerDirectory } from "../../mcp/workers.js";
|
|
20
17
|
export const dshDependencies = ["agents", "sessions", "sessionPersistence", "sessionQuery", "llm", "tools", "systemPrompt", "skills", "fs", "workingDirectory", "approval"];
|
|
21
18
|
export class DshWorkspace {
|
|
@@ -39,8 +36,6 @@ export class DshWorkspace {
|
|
|
39
36
|
check = () => { this.component.throwIfAborted(); this.lifetime.signal.throwIfAborted(); };
|
|
40
37
|
async open() {
|
|
41
38
|
this.check();
|
|
42
|
-
const mcpEntry = resolve(resourceRoot, "../lib/extension/mcp.js");
|
|
43
|
-
const workerDir = defaultWorkerDirectory();
|
|
44
39
|
this.#scope = await this.agent.ctx.plugin({
|
|
45
40
|
name: "pbot-coordinator",
|
|
46
41
|
inject: dshDependencies,
|
|
@@ -53,32 +48,20 @@ export class DshWorkspace {
|
|
|
53
48
|
if (this.features.history) {
|
|
54
49
|
registerDshHistoryTool(ctx, this.agent, this.root, this.lifetime.signal, this.check);
|
|
55
50
|
registerDshHistoryEvents(ctx, this.agent, this.root);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (migrationNotice)
|
|
59
|
-
this.ctx.logger.info(migrationNotice);
|
|
60
|
-
const reminder = checkHistorySummaryReminder(this.root);
|
|
61
|
-
if (reminder)
|
|
62
|
-
this.ctx.logger.info(reminder.message);
|
|
63
|
-
}
|
|
64
|
-
catch { /* Never block workspace startup if history reminder probe fails. */ }
|
|
51
|
+
for (const notice of historyStartupNotices(this.root))
|
|
52
|
+
this.ctx.logger.info(notice);
|
|
65
53
|
}
|
|
66
|
-
|
|
54
|
+
const server = this.workers.nativeServer(this.root);
|
|
55
|
+
if (server) {
|
|
67
56
|
await ctx.plugin(McpClient, McpClient.Config({
|
|
68
57
|
transport: "stdio",
|
|
69
58
|
serverName: "pbot",
|
|
70
|
-
|
|
71
|
-
args: [mcpEntry],
|
|
59
|
+
...server,
|
|
72
60
|
env: {
|
|
73
61
|
...(process.env.PATH ? { PATH: process.env.PATH } : {}),
|
|
74
62
|
...(process.env.HOME ? { HOME: process.env.HOME } : {}),
|
|
75
63
|
...(process.env.DSH_HOME ? { DSH_HOME: process.env.DSH_HOME } : {}),
|
|
76
|
-
|
|
77
|
-
PBOT_WORKSPACE_ROOT: this.root,
|
|
78
|
-
PBOT_MCP_AGENT_DIR: workerDir,
|
|
79
|
-
PBOT_MCP_SESSION_ID: this.agent.id,
|
|
80
|
-
PBOT_NATIVE_WORKSPACE_MCP: "1",
|
|
81
|
-
PBOT_NATIVE_HISTORY: "1",
|
|
64
|
+
...server.env,
|
|
82
65
|
},
|
|
83
66
|
cwd: this.root,
|
|
84
67
|
toolCallTimeoutMs: 600_000,
|
|
@@ -86,24 +69,9 @@ export class DshWorkspace {
|
|
|
86
69
|
}));
|
|
87
70
|
}
|
|
88
71
|
if (this.features.mcp) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
this.ctx.logger.warn(mcpConfig.error);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
for (const [serverName, server] of Object.entries(mcpConfig.servers)) {
|
|
96
|
-
if (server.enabled === false)
|
|
97
|
-
continue;
|
|
98
|
-
try {
|
|
99
|
-
const resolved = resolveWorkspaceMcpServer(this.root, serverName, server);
|
|
100
|
-
await ctx.plugin(McpClient, toDshMcpConfig(resolved));
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
this.ctx.logger.warn(error instanceof Error ? error.message : String(error));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
72
|
+
await loadNativeWorkspaceServers(this.root, async (server) => {
|
|
73
|
+
await ctx.plugin(McpClient, toDshMcpConfig(server));
|
|
74
|
+
}, { check: this.check, warn: message => this.ctx.logger.warn(message) });
|
|
107
75
|
}
|
|
108
76
|
},
|
|
109
77
|
});
|
|
@@ -169,6 +137,7 @@ export class DshWorkspaces {
|
|
|
169
137
|
}
|
|
170
138
|
let workspace;
|
|
171
139
|
try {
|
|
140
|
+
bindWorkspacePlugin(root, "dsh");
|
|
172
141
|
workspace = new DshWorkspace(this.ctx, root, agent, this.#lifetime.signal, workspaceFeatures(configuration));
|
|
173
142
|
const owned = workspace;
|
|
174
143
|
const dispose = agent.ctx.effect(() => async () => {
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { isMcpWorkerTool } from "../../../model/acp.js";
|
|
5
|
+
import { acpMember } from "../../acp/member.js";
|
|
6
|
+
import { agentFileTools } from "../../../integrations/hosts/pi/agent-tools.js";
|
|
7
|
+
import { toPiMcpConfig } from "../../../integrations/hosts/pi/mcp.js";
|
|
8
|
+
import { authorizeMutation } from "../../../services/workspace/workspace-files.js";
|
|
9
|
+
import { loadNativeWorkspaceServers } from "../workspace-mcp.js";
|
|
10
|
+
import { resourceRoot } from "../../../common/paths.js";
|
|
11
|
+
import { launch } from "../../../storage/agent-files.js";
|
|
12
|
+
import { requireThat } from "../../../common/validation.js";
|
|
13
|
+
const isBlockedTool = (name) => name === "pbot_history" || isMcpWorkerTool(name);
|
|
14
|
+
/** Member resources and authoritative final answers; ACP owns input delivery. */
|
|
15
|
+
export default async function piAcpMember(pi) {
|
|
16
|
+
const member = await acpMember(), lifetime = new AbortController();
|
|
17
|
+
const registeredMcp = [];
|
|
18
|
+
let input, startLeaf = null;
|
|
19
|
+
const checkSession = (ctx) => {
|
|
20
|
+
const reference = member.files.reference(member.id);
|
|
21
|
+
requireThat(reference.host === "acp" && reference.backend === "pi" && realpathSync(ctx.cwd) === member.d.cwd &&
|
|
22
|
+
(reference.sessionId === null || reference.sessionId === ctx.sessionManager.getSessionId()), "Pi ACP 会话身份或工作目录不匹配。");
|
|
23
|
+
};
|
|
24
|
+
const workspaceMcpServers = [];
|
|
25
|
+
if (member.features.mcp && process.env.PBOT_ACP_VALIDATE !== "1") {
|
|
26
|
+
await loadNativeWorkspaceServers(member.d.workspace, server => { workspaceMcpServers.push(server); });
|
|
27
|
+
}
|
|
28
|
+
pi.on("resources_discover", async () => {
|
|
29
|
+
if (!member.features.skills || member.d.options.noSkills)
|
|
30
|
+
return {};
|
|
31
|
+
const skillPaths = [join(resourceRoot, "skills")], workspaceSkills = join(member.d.workspace, ".agents/skills");
|
|
32
|
+
try {
|
|
33
|
+
if ((await stat(workspaceSkills)).isDirectory())
|
|
34
|
+
skillPaths.push(workspaceSkills);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error.code !== "ENOENT")
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
return { skillPaths };
|
|
41
|
+
});
|
|
42
|
+
pi.on("session_start", (_event, ctx) => {
|
|
43
|
+
checkSession(ctx);
|
|
44
|
+
const active = pi.getActiveTools();
|
|
45
|
+
for (const tool of agentFileTools(member.d.cwd, lifetime.signal, async (path, change, signal) => {
|
|
46
|
+
member.check();
|
|
47
|
+
await authorizeMutation(member.d.workspace, path, change, async () => false, signal);
|
|
48
|
+
member.check();
|
|
49
|
+
}))
|
|
50
|
+
pi.registerTool(tool);
|
|
51
|
+
for (const server of workspaceMcpServers) {
|
|
52
|
+
pi.registerMcpServer(server.name, toPiMcpConfig(server));
|
|
53
|
+
registeredMcp.push(server.name);
|
|
54
|
+
}
|
|
55
|
+
pi.setActiveTools(active.filter(name => !isBlockedTool(name)));
|
|
56
|
+
pi.setSessionName(member.files.read(member.id, "launch.json", launch).name);
|
|
57
|
+
startLeaf = ctx.sessionManager.getLeafId();
|
|
58
|
+
});
|
|
59
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
60
|
+
try {
|
|
61
|
+
checkSession(ctx);
|
|
62
|
+
const current = member.input();
|
|
63
|
+
if (current.requestId !== input?.requestId) {
|
|
64
|
+
input = current;
|
|
65
|
+
startLeaf = ctx.sessionManager.getLeafId();
|
|
66
|
+
}
|
|
67
|
+
if (!member.features.prompts)
|
|
68
|
+
return;
|
|
69
|
+
const context = await member.context();
|
|
70
|
+
member.check();
|
|
71
|
+
Object.assign(event.systemPromptOptions.sections, { pbot_workspace: context.workspace, pbot_child: context.role, pbot_environment: context.environment });
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
ctx.abort();
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
pi.on("tool_call", event => {
|
|
79
|
+
member.check();
|
|
80
|
+
if (isBlockedTool(event.toolName))
|
|
81
|
+
return { block: true, reason: "专家不提供协调管理工具。" };
|
|
82
|
+
return;
|
|
83
|
+
});
|
|
84
|
+
pi.on("agent_settled", (event, ctx) => {
|
|
85
|
+
if (!input)
|
|
86
|
+
return;
|
|
87
|
+
const branch = ctx.sessionManager.getBranch(), at = startLeaf === null ? -1 : branch.findIndex(entry => entry.id === startLeaf);
|
|
88
|
+
const last = branch.slice(at + 1).findLast(entry => entry.type === "message" && entry.message.role === "assistant");
|
|
89
|
+
const assistant = last?.type === "message" && last.message.role === "assistant" ? last.message : undefined;
|
|
90
|
+
const answer = assistant?.content.filter(block => block.type === "text").map(block => block.text).join("\n") ?? "";
|
|
91
|
+
member.answer(input, event.aborted ? "cancelled" : assistant?.stopReason === "stop" ? "completed" : "failed", answer, assistant?.errorMessage);
|
|
92
|
+
});
|
|
93
|
+
pi.on("session_before_switch", () => ({ cancel: true }));
|
|
94
|
+
pi.on("session_before_fork", () => ({ cancel: true }));
|
|
95
|
+
pi.on("session_shutdown", () => {
|
|
96
|
+
for (const name of registeredMcp.splice(0))
|
|
97
|
+
pi.unregisterMcpServer(name);
|
|
98
|
+
lifetime.abort();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -5,6 +5,8 @@ export declare function pluginCommand(adapter: PluginHostAdapter, args: readonly
|
|
|
5
5
|
resourceRoot: string;
|
|
6
6
|
configuration: import("../../model/workspace-config.js").WorkspaceConfiguration;
|
|
7
7
|
}>;
|
|
8
|
-
export
|
|
8
|
+
export type ReplayPluginHook = (root: string, input: string) => Promise<number | undefined>;
|
|
9
|
+
export declare function runPluginHost(adapter: PluginHostAdapter, args: readonly string[], replay?: ReplayPluginHook): Promise<void>;
|
|
9
10
|
/** Shared hook execution engine; host-specific protocols stay in PluginHostAdapter implementations. */
|
|
10
11
|
export declare function pluginHook(adapter: PluginHostAdapter, payload: unknown): Promise<Record<string, unknown>>;
|
|
12
|
+
export declare function pluginHook(adapter: PluginHostAdapter, payload: unknown, replay: ((root: string) => Promise<number | undefined>) | undefined): Promise<Record<string, unknown> | number>;
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
1
2
|
import { resourceRoot } from "../../common/paths.js";
|
|
2
3
|
import { packageVersion } from "../../common/version.js";
|
|
3
4
|
import { requireThat } from "../../common/validation.js";
|
|
4
5
|
import { loadWorkspaceContext } from "../../integrations/agent-resources.js";
|
|
5
6
|
import { recordHistoryCapture } from "../../services/history/history-capture.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { historyStartupNotices } from "../../services/history/summary-runner.js";
|
|
8
|
+
import { bindWorkspacePlugin } from "../../services/workspace/plugin-releases.js";
|
|
8
9
|
import { readWorkspaceConfig, workspaceFeatures } from "../../services/workspace/workspace-config.js";
|
|
9
10
|
import { initializeWorkspace } from "../../services/workspace/workspace-preparation.js";
|
|
10
|
-
import { discoverWorkspaceSkills } from "../../services/workspace/workspace-skills.js";
|
|
11
|
+
import { discoverSkills, discoverWorkspaceSkills } from "../../services/workspace/workspace-skills.js";
|
|
11
12
|
function disabledMessage(adapter, configuration) {
|
|
12
13
|
return configuration.error ?? `Personal Bot 未在此工作区启用。运行 ${adapter.initCommandHint};配置文件:${configuration.path}`;
|
|
13
14
|
}
|
|
@@ -15,25 +16,30 @@ export async function pluginCommand(adapter, args, cwd) {
|
|
|
15
16
|
const root = await adapter.resolveRoot(cwd);
|
|
16
17
|
const action = args.join(" ");
|
|
17
18
|
requireThat(["init", "status"].includes(action), adapter.usage);
|
|
18
|
-
if (action === "init")
|
|
19
|
+
if (action === "init") {
|
|
19
20
|
await initializeWorkspace(root);
|
|
21
|
+
bindWorkspacePlugin(root, adapter.host, process.env.PBOT_PLUGIN_MANIFEST || process.env.PBOT_PLUGIN_VERSION);
|
|
22
|
+
}
|
|
20
23
|
const configuration = await readWorkspaceConfig(root);
|
|
21
24
|
return { version: packageVersion, workspaceRoot: root, resourceRoot, configuration };
|
|
22
25
|
}
|
|
23
|
-
export async function runPluginHost(adapter, args) {
|
|
26
|
+
export async function runPluginHost(adapter, args, replay) {
|
|
24
27
|
if (args[0] === "hook") {
|
|
25
28
|
let input = "";
|
|
26
29
|
process.stdin.setEncoding("utf8");
|
|
27
30
|
for await (const chunk of process.stdin)
|
|
28
31
|
input += String(chunk);
|
|
29
|
-
|
|
32
|
+
const result = await pluginHook(adapter, JSON.parse(input), replay && (root => replay(root, input)));
|
|
33
|
+
if (typeof result === "number")
|
|
34
|
+
process.exitCode = result;
|
|
35
|
+
else
|
|
36
|
+
process.stdout.write(JSON.stringify(result) + "\n");
|
|
30
37
|
}
|
|
31
38
|
else {
|
|
32
39
|
process.stdout.write(JSON.stringify(await pluginCommand(adapter, args, process.cwd()), null, 2) + "\n");
|
|
33
40
|
}
|
|
34
41
|
}
|
|
35
|
-
|
|
36
|
-
export async function pluginHook(adapter, payload) {
|
|
42
|
+
export async function pluginHook(adapter, payload, replay) {
|
|
37
43
|
if (process.env.PBOT_SUBAGENT_RUN_ID)
|
|
38
44
|
return adapter.formatPass(payload);
|
|
39
45
|
const event = adapter.parseEvent(payload);
|
|
@@ -41,6 +47,11 @@ export async function pluginHook(adapter, payload) {
|
|
|
41
47
|
return adapter.formatPass(payload);
|
|
42
48
|
const root = await adapter.resolveRoot(event.cwd, event.raw);
|
|
43
49
|
const configuration = await readWorkspaceConfig(root);
|
|
50
|
+
if (configuration.enabled && replay) {
|
|
51
|
+
const code = await replay(root);
|
|
52
|
+
if (code !== undefined)
|
|
53
|
+
return code;
|
|
54
|
+
}
|
|
44
55
|
const features = workspaceFeatures(configuration);
|
|
45
56
|
if (event.kind === "check-skill") {
|
|
46
57
|
if (!configuration.enabled)
|
|
@@ -51,6 +62,8 @@ export async function pluginHook(adapter, payload) {
|
|
|
51
62
|
}
|
|
52
63
|
if (!configuration.enabled)
|
|
53
64
|
return adapter.formatPass(payload);
|
|
65
|
+
if (event.kind === "inject-context")
|
|
66
|
+
bindWorkspacePlugin(root, adapter.host, process.env.PBOT_PLUGIN_MANIFEST || process.env.PBOT_PLUGIN_VERSION);
|
|
54
67
|
if (features.history && adapter.extractHistory) {
|
|
55
68
|
try {
|
|
56
69
|
const capture = await adapter.extractHistory(event);
|
|
@@ -61,30 +74,25 @@ export async function pluginHook(adapter, payload) {
|
|
|
61
74
|
}
|
|
62
75
|
if (event.kind === "record-history")
|
|
63
76
|
return adapter.formatPass(payload);
|
|
64
|
-
const
|
|
77
|
+
const includePrompts = features.prompts && !event.noticesOnly;
|
|
78
|
+
const includeSkills = features.skills && !event.noticesOnly;
|
|
79
|
+
const workspaceSkills = includeSkills && !adapter.nativeWorkspaceSkills
|
|
65
80
|
? await discoverWorkspaceSkills(root)
|
|
66
81
|
: [];
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const migrationNotice = checkHistoryMigrationNotice(root);
|
|
71
|
-
if (migrationNotice)
|
|
72
|
-
notices.push(migrationNotice);
|
|
73
|
-
const reminder = checkHistorySummaryReminder(root);
|
|
74
|
-
if (reminder)
|
|
75
|
-
notices.push(reminder.message);
|
|
76
|
-
}
|
|
77
|
-
catch { /* Never block session start if history reminder probe fails. */ }
|
|
82
|
+
if (includeSkills && adapter.indexBundledSkills) {
|
|
83
|
+
const directory = join(resourceRoot, "skills");
|
|
84
|
+
workspaceSkills.push(...await discoverSkills(directory, directory));
|
|
78
85
|
}
|
|
79
|
-
|
|
86
|
+
const notices = event.isSessionStart && features.history ? historyStartupNotices(root) : [];
|
|
87
|
+
if (!includePrompts && !workspaceSkills.length && !notices.length)
|
|
80
88
|
return adapter.formatPass(payload);
|
|
81
89
|
const context = await loadWorkspaceContext({
|
|
82
90
|
host: adapter.host,
|
|
83
91
|
root,
|
|
84
92
|
role: event.role,
|
|
85
93
|
...(adapter.coordinatorPrompt ? { coordinatorPrompt: adapter.coordinatorPrompt } : {}),
|
|
86
|
-
includePrompts
|
|
87
|
-
includeRootAgents:
|
|
94
|
+
includePrompts,
|
|
95
|
+
includeRootAgents: includePrompts && !adapter.nativeRootAgents,
|
|
88
96
|
...(event.localRoleOnly ? { localRoleOnly: true } : {}),
|
|
89
97
|
workspaceSkills,
|
|
90
98
|
notices,
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ResolvedWorkspaceMcpServer } from "../../model/workspace-mcp.js";
|
|
2
|
+
/** A native host can keep its other tools when one workspace server cannot be mounted. */
|
|
3
|
+
export declare function loadNativeWorkspaceServers(root: string, mount: (server: ResolvedWorkspaceMcpServer) => void | Promise<void>, options?: {
|
|
4
|
+
check?: () => void;
|
|
5
|
+
warn?: (message: string) => void;
|
|
6
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { readWorkspaceMcpConfig, resolveWorkspaceMcpServer } from "../../services/workspace/workspace-mcp.js";
|
|
2
|
+
/** A native host can keep its other tools when one workspace server cannot be mounted. */
|
|
3
|
+
export async function loadNativeWorkspaceServers(root, mount, options = {}) {
|
|
4
|
+
const config = await readWorkspaceMcpConfig(root);
|
|
5
|
+
options.check?.();
|
|
6
|
+
if (config.error) {
|
|
7
|
+
options.warn?.(config.error);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
for (const [name, server] of Object.entries(config.servers)) {
|
|
11
|
+
if (server.enabled === false)
|
|
12
|
+
continue;
|
|
13
|
+
options.check?.();
|
|
14
|
+
try {
|
|
15
|
+
await mount(resolveWorkspaceMcpServer(root, name, server));
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
options.warn?.(error instanceof Error ? error.message : String(error));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -2,6 +2,12 @@ import { resolveWorkspaceMcpServers } from "../../services/workspace/workspace-m
|
|
|
2
2
|
export declare class WorkspaceMcpBridge {
|
|
3
3
|
#private;
|
|
4
4
|
discover(root: string, serverName?: string): Promise<{
|
|
5
|
+
tools: unknown[];
|
|
6
|
+
description?: string;
|
|
7
|
+
workspace: string;
|
|
8
|
+
server: string;
|
|
9
|
+
transport: "stdio" | "http";
|
|
10
|
+
} | {
|
|
5
11
|
workspace: string;
|
|
6
12
|
configPath: string;
|
|
7
13
|
servers: ({
|
|
@@ -20,14 +26,6 @@ export declare class WorkspaceMcpBridge {
|
|
|
20
26
|
transport: string;
|
|
21
27
|
enabled: boolean;
|
|
22
28
|
})[];
|
|
23
|
-
} | {
|
|
24
|
-
tools: unknown[];
|
|
25
|
-
description?: string;
|
|
26
|
-
workspace: string;
|
|
27
|
-
server: string;
|
|
28
|
-
transport: "stdio" | "http";
|
|
29
|
-
configPath?: never;
|
|
30
|
-
servers?: never;
|
|
31
29
|
}>;
|
|
32
30
|
call(root: string, serverName: string, toolName: string, args?: Record<string, unknown>): Promise<{
|
|
33
31
|
structuredContent?: {} | null;
|
|
@@ -260,70 +260,66 @@ class HttpMcpClient {
|
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
|
+
function configuredServer(root, configuration, serverName) {
|
|
264
|
+
const raw = Object.hasOwn(configuration.servers, serverName) ? configuration.servers[serverName] : undefined;
|
|
265
|
+
requireThat(raw, `工作区未注册名为 "${serverName}" 的 MCP 服务(已注册:${Object.keys(configuration.servers).join(", ") || "无"})。`);
|
|
266
|
+
requireThat(raw.enabled !== false, `工作区 MCP 服务 "${serverName}" 当前已停用(enabled: false)。`);
|
|
267
|
+
return resolveWorkspaceMcpServer(root, serverName, raw);
|
|
268
|
+
}
|
|
263
269
|
export class WorkspaceMcpBridge {
|
|
264
|
-
#
|
|
265
|
-
#
|
|
266
|
-
#
|
|
267
|
-
|
|
268
|
-
|
|
270
|
+
#connections = new Set();
|
|
271
|
+
#closing;
|
|
272
|
+
#close(connection) {
|
|
273
|
+
return connection.closing ??= connection.ready.catch(() => { }).then(() => connection.client.close())
|
|
274
|
+
.finally(() => this.#connections.delete(connection));
|
|
275
|
+
}
|
|
276
|
+
#retire(connection) {
|
|
277
|
+
connection.retired = true;
|
|
278
|
+
if (!connection.users)
|
|
279
|
+
return this.#close(connection);
|
|
269
280
|
}
|
|
270
|
-
async #
|
|
271
|
-
requireThat(!this.#
|
|
272
|
-
const key =
|
|
281
|
+
async #withClient(root, server, operation) {
|
|
282
|
+
requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
|
|
283
|
+
const key = `${root}\0${server.name}`;
|
|
273
284
|
const fingerprint = JSON.stringify(server);
|
|
274
|
-
|
|
275
|
-
if (
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
this.#clients.delete(key);
|
|
279
|
-
await existing.close().catch(() => { });
|
|
285
|
+
let connection = [...this.#connections].find(entry => entry.key === key && !entry.retired);
|
|
286
|
+
if (connection && (!connection.client.alive || connection.client.fingerprint !== fingerprint)) {
|
|
287
|
+
void this.#retire(connection)?.catch(() => { });
|
|
288
|
+
connection = undefined;
|
|
280
289
|
}
|
|
281
|
-
|
|
282
|
-
if (inflight)
|
|
283
|
-
return inflight;
|
|
284
|
-
const creating = (async () => {
|
|
290
|
+
if (!connection) {
|
|
285
291
|
const client = server.transport === "http"
|
|
286
292
|
? new HttpMcpClient(server, fingerprint)
|
|
287
293
|
: new StdioMcpClient(server, fingerprint);
|
|
294
|
+
connection = { key, client, ready: client.initialize(), users: 0, retired: false };
|
|
295
|
+
this.#connections.add(connection);
|
|
296
|
+
}
|
|
297
|
+
connection.users++;
|
|
298
|
+
try {
|
|
288
299
|
try {
|
|
289
|
-
await
|
|
290
|
-
if (this.#closed) {
|
|
291
|
-
await client.close();
|
|
292
|
-
throw new Error("Workspace MCP 桥接已关闭。");
|
|
293
|
-
}
|
|
294
|
-
this.#clients.set(key, client);
|
|
295
|
-
return client;
|
|
300
|
+
await connection.ready;
|
|
296
301
|
}
|
|
297
302
|
catch (error) {
|
|
298
|
-
|
|
303
|
+
connection.retired = true;
|
|
299
304
|
throw error;
|
|
300
305
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
return await creating;
|
|
306
|
+
requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
|
|
307
|
+
return await operation(connection.client);
|
|
305
308
|
}
|
|
306
309
|
finally {
|
|
307
|
-
|
|
308
|
-
|
|
310
|
+
connection.users--;
|
|
311
|
+
if (connection.retired && !connection.users)
|
|
312
|
+
await this.#close(connection).catch(() => { });
|
|
309
313
|
}
|
|
310
314
|
}
|
|
311
315
|
async #pruneRemoved(root, activeNames) {
|
|
312
316
|
const prefix = `${root}\0`;
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
continue;
|
|
317
|
-
const name = key.slice(prefix.length);
|
|
318
|
-
if (!activeNames.has(name)) {
|
|
319
|
-
this.#clients.delete(key);
|
|
320
|
-
stale.push(client);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
await Promise.allSettled(stale.map(client => client.close()));
|
|
317
|
+
await Promise.allSettled([...this.#connections]
|
|
318
|
+
.filter(entry => entry.key.startsWith(prefix) && !activeNames.has(entry.key.slice(prefix.length)))
|
|
319
|
+
.map(entry => this.#retire(entry)));
|
|
324
320
|
}
|
|
325
321
|
async discover(root, serverName) {
|
|
326
|
-
requireThat(!this.#
|
|
322
|
+
requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
|
|
327
323
|
const configuration = await readWorkspaceMcpConfig(root);
|
|
328
324
|
if (configuration.error)
|
|
329
325
|
throw new Error(configuration.error);
|
|
@@ -346,41 +342,35 @@ export class WorkspaceMcpBridge {
|
|
|
346
342
|
servers,
|
|
347
343
|
};
|
|
348
344
|
}
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
tools,
|
|
371
|
-
};
|
|
345
|
+
const resolved = configuredServer(root, configuration, serverName);
|
|
346
|
+
return this.#withClient(root, resolved, async (client) => {
|
|
347
|
+
const tools = [];
|
|
348
|
+
let cursor;
|
|
349
|
+
for (;;) {
|
|
350
|
+
const page = await client.request("tools/list", cursor ? { cursor } : {});
|
|
351
|
+
if (object(page) && Array.isArray(page.tools))
|
|
352
|
+
tools.push(...page.tools);
|
|
353
|
+
const next = object(page) && typeof page.nextCursor === "string" && page.nextCursor ? page.nextCursor : undefined;
|
|
354
|
+
if (!next)
|
|
355
|
+
break;
|
|
356
|
+
cursor = next;
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
workspace: root,
|
|
360
|
+
server: resolved.name,
|
|
361
|
+
transport: resolved.transport,
|
|
362
|
+
...(resolved.description ? { description: resolved.description } : {}),
|
|
363
|
+
tools,
|
|
364
|
+
};
|
|
365
|
+
});
|
|
372
366
|
}
|
|
373
367
|
async call(root, serverName, toolName, args = {}) {
|
|
374
|
-
requireThat(!this.#
|
|
368
|
+
requireThat(!this.#closing, "Workspace MCP 桥接已关闭。");
|
|
375
369
|
const configuration = await readWorkspaceMcpConfig(root);
|
|
376
370
|
if (configuration.error)
|
|
377
371
|
throw new Error(configuration.error);
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
requireThat(raw.enabled !== false, `工作区 MCP 服务 "${serverName}" 当前已停用(enabled: false)。`);
|
|
381
|
-
const resolved = resolveWorkspaceMcpServer(root, serverName, raw);
|
|
382
|
-
const client = await this.#connect(root, resolved);
|
|
383
|
-
const result = await client.request("tools/call", { name: toolName, arguments: args });
|
|
372
|
+
const resolved = configuredServer(root, configuration, serverName);
|
|
373
|
+
const result = await this.#withClient(root, resolved, client => client.request("tools/call", { name: toolName, arguments: args }));
|
|
384
374
|
if (object(result)) {
|
|
385
375
|
return {
|
|
386
376
|
server: resolved.name,
|
|
@@ -397,16 +387,8 @@ export class WorkspaceMcpBridge {
|
|
|
397
387
|
content: result === undefined ? [] : [{ type: "text", text: JSON.stringify(result) }],
|
|
398
388
|
};
|
|
399
389
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return;
|
|
403
|
-
this.#closed = true;
|
|
404
|
-
const inflight = [...this.#connecting.values()];
|
|
405
|
-
this.#connecting.clear();
|
|
406
|
-
await Promise.allSettled(inflight);
|
|
407
|
-
const clients = [...this.#clients.values()];
|
|
408
|
-
this.#clients.clear();
|
|
409
|
-
await Promise.allSettled(clients.map(client => client.close()));
|
|
390
|
+
close() {
|
|
391
|
+
return this.#closing ??= Promise.allSettled([...this.#connections].map(entry => this.#close(entry))).then(() => { });
|
|
410
392
|
}
|
|
411
393
|
}
|
|
412
394
|
export { resolveWorkspaceMcpServers };
|