personal-bot 0.3.2 → 0.3.4
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 +29 -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 +20 -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 +37 -22
- 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 +20171 -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 +15 -0
- package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +126 -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 +37 -22
- 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 +15 -0
- package/lib/extension/setup/upgrade.js +153 -0
- package/lib/extension/workspace-cli.js +20171 -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/integrations/installation/cli-update.js +146 -0
- package/lib/integrations/installation/upgraders/antigravity.js +67 -0
- package/lib/integrations/installation/upgraders/claude.js +123 -0
- package/lib/integrations/installation/upgraders/codex.js +73 -0
- package/lib/integrations/installation/upgraders/dsh.js +62 -0
- package/lib/integrations/installation/upgraders/pi.js +90 -0
- package/lib/integrations/installation/upgraders/qoder.js +67 -0
- package/lib/integrations/installation/upgraders/registry.js +40 -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/installation/setup-service.js +4 -1
- package/lib/services/installation/upgrade-service.js +95 -0
- package/lib/services/workspace/plugin-releases.d.ts +15 -0
- package/lib/services/workspace/plugin-releases.js +126 -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 +39 -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 +11 -1
- package/src/extension/setup/upgrade.ts +158 -0
- package/src/extension/setup/workspace-cli.ts +167 -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 +144 -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/integrations/installation/upgraders/antigravity.ts +68 -0
- package/src/integrations/installation/upgraders/claude.ts +123 -0
- package/src/integrations/installation/upgraders/codex.ts +73 -0
- package/src/integrations/installation/upgraders/dsh.ts +63 -0
- package/src/integrations/installation/upgraders/pi.ts +87 -0
- package/src/integrations/installation/upgraders/qoder.ts +68 -0
- package/src/integrations/installation/upgraders/registry.ts +48 -0
- 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 +86 -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 +9 -43
- package/src/services/installation/upgrade-service.ts +127 -0
- package/src/services/workspace/plugin-releases.ts +121 -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
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import { object } from "../../common/json.js";
|
|
6
|
+
import { resourceRoot } from "../../common/paths.js";
|
|
7
|
+
import { requireThat } from "../../common/validation.js";
|
|
8
|
+
import { releaseLine } from "../../common/version.js";
|
|
9
|
+
import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
|
|
10
|
+
function compareSemver(a, b) {
|
|
11
|
+
const parse = (value) => {
|
|
12
|
+
const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?/.exec(value);
|
|
13
|
+
return match
|
|
14
|
+
? { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), pre: match[4] ?? "" }
|
|
15
|
+
: undefined;
|
|
16
|
+
};
|
|
17
|
+
const left = parse(a);
|
|
18
|
+
const right = parse(b);
|
|
19
|
+
if (!left || !right)
|
|
20
|
+
return a.localeCompare(b);
|
|
21
|
+
if (left.major !== right.major)
|
|
22
|
+
return left.major - right.major;
|
|
23
|
+
if (left.minor !== right.minor)
|
|
24
|
+
return left.minor - right.minor;
|
|
25
|
+
if (left.patch !== right.patch)
|
|
26
|
+
return left.patch - right.patch;
|
|
27
|
+
if (left.pre === right.pre)
|
|
28
|
+
return 0;
|
|
29
|
+
if (!left.pre)
|
|
30
|
+
return 1;
|
|
31
|
+
if (!right.pre)
|
|
32
|
+
return -1;
|
|
33
|
+
return left.pre.localeCompare(right.pre);
|
|
34
|
+
}
|
|
35
|
+
function bindingDirectory(root) {
|
|
36
|
+
const key = createHash("sha256").update(root).digest("hex");
|
|
37
|
+
return join(homedir(), ".pbot", "workspaces", key);
|
|
38
|
+
}
|
|
39
|
+
// Full plugins own a local manifest. Thin clients can supply the release stamped into their cached hooks.
|
|
40
|
+
function pluginBinding(value) {
|
|
41
|
+
requireThat(typeof value === "string", "无效的插件安装记录。");
|
|
42
|
+
if (!isAbsolute(value))
|
|
43
|
+
releaseLine(value);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
function permissionDenied(error) {
|
|
47
|
+
return ["EACCES", "EPERM", "EROFS"].includes(error.code ?? "");
|
|
48
|
+
}
|
|
49
|
+
/** Each host owns its file, including when two hosts activate the same canonical workspace concurrently. */
|
|
50
|
+
export function bindWorkspacePlugin(root, host, binding = resolve(resourceRoot, "../package.json")) {
|
|
51
|
+
const directory = bindingDirectory(root);
|
|
52
|
+
const file = encodeURIComponent(host) + ".json";
|
|
53
|
+
try {
|
|
54
|
+
if (readJsonFile(directory, file, pluginBinding) === binding)
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (permissionDenied(error))
|
|
59
|
+
return;
|
|
60
|
+
if (error.code !== "ENOENT")
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
65
|
+
writeJsonFile(directory, file, binding, pluginBinding);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
if (!permissionDenied(error))
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function workspacePluginBindings(root) {
|
|
73
|
+
const directory = bindingDirectory(root);
|
|
74
|
+
let files;
|
|
75
|
+
try {
|
|
76
|
+
files = readdirSync(directory);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error.code === "ENOENT" || permissionDenied(error))
|
|
80
|
+
return {};
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
const bindings = Object.create(null);
|
|
84
|
+
for (const file of files.filter(name => name.endsWith(".json"))) {
|
|
85
|
+
const host = decodeURIComponent(file.slice(0, -5));
|
|
86
|
+
try {
|
|
87
|
+
const binding = readJsonFile(directory, file, pluginBinding);
|
|
88
|
+
if (!isAbsolute(binding)) {
|
|
89
|
+
bindings[host] = { version: binding };
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const manifest = JSON.parse(readFileSync(binding, "utf8"));
|
|
93
|
+
requireThat(object(manifest) && typeof manifest.version === "string", "无效的插件安装清单:" + binding);
|
|
94
|
+
releaseLine(manifest.version);
|
|
95
|
+
bindings[host] = { version: manifest.version, bindingPath: binding };
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (error.code !== "ENOENT" && !permissionDenied(error))
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return bindings;
|
|
103
|
+
}
|
|
104
|
+
export function workspacePluginReleases(root) {
|
|
105
|
+
const bindings = workspacePluginBindings(root);
|
|
106
|
+
const releases = Object.create(null);
|
|
107
|
+
for (const [host, entry] of Object.entries(bindings))
|
|
108
|
+
releases[host] = entry.version;
|
|
109
|
+
return releases;
|
|
110
|
+
}
|
|
111
|
+
export function workspacePluginDrift(releases) {
|
|
112
|
+
let highest;
|
|
113
|
+
let lowest;
|
|
114
|
+
for (const version of Object.values(releases)) {
|
|
115
|
+
if (!highest || compareSemver(version, highest) > 0)
|
|
116
|
+
highest = version;
|
|
117
|
+
if (!lowest || compareSemver(version, lowest) < 0)
|
|
118
|
+
lowest = version;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
outOfSync: Boolean(highest && lowest && highest !== lowest),
|
|
122
|
+
...(highest ? { highest } : {}),
|
|
123
|
+
...(lowest ? { lowest } : {}),
|
|
124
|
+
versions: releases,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -2,12 +2,13 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { mkdir, readFile } from "node:fs/promises";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { object } from "../../common/json.js";
|
|
5
|
-
import {
|
|
5
|
+
import { acpPermissionPolicies } from "../../model/acp.js";
|
|
6
|
+
import { historyLayouts, summaryConcurrency } from "../../model/workspace-config.js";
|
|
7
|
+
import { isAcpBackend, isOneOf } from "../../common/validation.js";
|
|
6
8
|
import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
|
|
7
9
|
const featureKeys = ["prompts", "skills", "mcp", "history"];
|
|
8
10
|
const summaryKeys = ["backend", "model", "thinking", "concurrency"];
|
|
9
11
|
const historyKeys = ["layout"];
|
|
10
|
-
const historyLayouts = ["monthly", "flat"];
|
|
11
12
|
function nonEmptyString(value) {
|
|
12
13
|
return typeof value === "string" && value.trim().length > 0 && !value.includes("\0");
|
|
13
14
|
}
|
|
@@ -33,7 +34,7 @@ function config(value) {
|
|
|
33
34
|
(command.args === undefined || (Array.isArray(command.args) && command.args.every(arg => typeof arg === "string" && !arg.includes("\0"))));
|
|
34
35
|
if (!object(command) || !launch ||
|
|
35
36
|
(command.env !== undefined && (!object(command.env) || Object.entries(command.env).some(([k, v]) => !k || k.includes("\0") || typeof v !== "string" || v.includes("\0")))) ||
|
|
36
|
-
(command.permission !== undefined && !
|
|
37
|
+
(command.permission !== undefined && !isOneOf(command.permission, acpPermissionPolicies)) ||
|
|
37
38
|
Object.keys(command).some(key => !["command", "args", "env", "permission"].includes(key))) {
|
|
38
39
|
throw new Error("ACP 配置需要 command(内置目标可省略)及可选 args 字符串数组、env 字符串映射、permission(ask、allow 或 deny)。");
|
|
39
40
|
}
|
|
@@ -53,15 +54,15 @@ function config(value) {
|
|
|
53
54
|
throw new Error("summary.thinking 必须是非空字符串。");
|
|
54
55
|
}
|
|
55
56
|
if (value.summary.concurrency !== undefined &&
|
|
56
|
-
(typeof value.summary.concurrency !== "number" || !Number.isInteger(value.summary.concurrency) || value.summary.concurrency < 1 || value.summary.concurrency >
|
|
57
|
-
throw new Error(
|
|
57
|
+
(typeof value.summary.concurrency !== "number" || !Number.isInteger(value.summary.concurrency) || value.summary.concurrency < 1 || value.summary.concurrency > summaryConcurrency.max)) {
|
|
58
|
+
throw new Error(`summary.concurrency 必须是 1 到 ${summaryConcurrency.max} 之间的整数。`);
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
if (value.history !== undefined) {
|
|
61
62
|
if (!object(value.history) || Object.keys(value.history).some(key => !historyKeys.includes(key))) {
|
|
62
63
|
throw new Error("history 必须是 JSON 对象,仅支持 layout。");
|
|
63
64
|
}
|
|
64
|
-
if (value.history.layout !== undefined && !
|
|
65
|
+
if (value.history.layout !== undefined && !isOneOf(value.history.layout, historyLayouts)) {
|
|
65
66
|
throw new Error('history.layout 必须是 "monthly" 或 "flat"。');
|
|
66
67
|
}
|
|
67
68
|
}
|
|
@@ -86,7 +87,7 @@ export function workspaceSummaryConfig(configuration) {
|
|
|
86
87
|
backend: raw?.backend ?? "pi",
|
|
87
88
|
...(raw?.model ? { model: raw.model } : {}),
|
|
88
89
|
...(raw?.thinking ? { thinking: raw.thinking } : {}),
|
|
89
|
-
concurrency: raw?.concurrency ??
|
|
90
|
+
concurrency: raw?.concurrency ?? summaryConcurrency.default,
|
|
90
91
|
};
|
|
91
92
|
}
|
|
92
93
|
export function workspaceHistoryConfig(configuration) {
|
|
@@ -160,5 +161,5 @@ export async function initializeWorkspaceConfig(root) {
|
|
|
160
161
|
await initializeWorkspaceConfig(root);
|
|
161
162
|
}
|
|
162
163
|
export function workspaceDisabledMessage(configuration) {
|
|
163
|
-
return configuration.error ?? `Personal Bot
|
|
164
|
+
return configuration.error ?? `Personal Bot 未在此工作区启用。请在工作区根目录运行 pbot init,也可在 Pi 或 dsh 中执行 /pbot init。配置文件位于 ${configuration.path}`;
|
|
164
165
|
}
|
|
@@ -2,7 +2,7 @@ import { mkdir, readFile } from "node:fs/promises";
|
|
|
2
2
|
import { dirname, join, resolve } from "node:path";
|
|
3
3
|
import { object } from "../../common/json.js";
|
|
4
4
|
import { requireThat } from "../../common/validation.js";
|
|
5
|
-
import {
|
|
5
|
+
import { updateJsonFile } from "../../storage/atomic-json.js";
|
|
6
6
|
const serverNamePattern = /^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/;
|
|
7
7
|
const reservedServerNames = new Set(["pbot"]);
|
|
8
8
|
const stdioKeys = new Set(["command", "args", "env", "cwd", "description", "timeout", "enabled"]);
|
|
@@ -17,12 +17,10 @@ export function isWorkspaceMcpHttpServer(server) {
|
|
|
17
17
|
}
|
|
18
18
|
function validateStringRecord(value, label) {
|
|
19
19
|
requireThat(object(value), `${label} 必须是字符串映射。`);
|
|
20
|
-
|
|
21
|
-
for (const [key, entry] of Object.entries(value)) {
|
|
20
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => {
|
|
22
21
|
requireThat(key.length > 0 && !key.includes("\0") && typeof entry === "string" && !entry.includes("\0"), `${label}.${key} 必须是非空键与无空字节字符串值。`);
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
return result;
|
|
22
|
+
return [key, entry];
|
|
23
|
+
}));
|
|
26
24
|
}
|
|
27
25
|
export function validateWorkspaceMcpServer(name, value) {
|
|
28
26
|
requireThat(serverNamePattern.test(name), `MCP 服务名无效:${name}(需匹配 [A-Za-z0-9][A-Za-z0-9_-]{0,31})。`);
|
|
@@ -90,7 +88,7 @@ function validateMcpFile(value) {
|
|
|
90
88
|
for (const [name, entry] of Object.entries(rawServers)) {
|
|
91
89
|
servers[name] = validateWorkspaceMcpServer(name, entry);
|
|
92
90
|
}
|
|
93
|
-
return { mcpServers: servers };
|
|
91
|
+
return { ...value, mcpServers: servers };
|
|
94
92
|
}
|
|
95
93
|
export function workspaceMcpPath(root) {
|
|
96
94
|
return join(root, ".pbot", "mcp.json");
|
|
@@ -121,37 +119,32 @@ export function expandEnvTemplate(value, env, context) {
|
|
|
121
119
|
return resolved;
|
|
122
120
|
});
|
|
123
121
|
}
|
|
122
|
+
function expandEnvRecord(values, env, context) {
|
|
123
|
+
return Object.fromEntries(Object.entries(values ?? {}).map(([key, value]) => [key, expandEnvTemplate(value, env, `${context}.${key}`)]));
|
|
124
|
+
}
|
|
124
125
|
export function resolveWorkspaceMcpServer(root, name, server, env = process.env) {
|
|
125
126
|
const timeout = server.timeout ?? defaultTimeoutSeconds;
|
|
126
127
|
if (isWorkspaceMcpHttpServer(server)) {
|
|
127
128
|
const url = expandEnvTemplate(server.url, env, `MCP 服务 "${name}" 的 url`);
|
|
128
129
|
requireThat(/^https?:\/\/.+/.test(url), `MCP 服务 "${name}" 展开后的 url 必须以 http:// 或 https:// 开头。`);
|
|
129
|
-
const headers = {};
|
|
130
|
-
for (const [key, value] of Object.entries(server.headers ?? {})) {
|
|
131
|
-
headers[key] = expandEnvTemplate(value, env, `MCP 服务 "${name}" 的 headers.${key}`);
|
|
132
|
-
}
|
|
133
130
|
return {
|
|
134
131
|
transport: "http",
|
|
135
132
|
name,
|
|
136
133
|
url,
|
|
137
|
-
headers,
|
|
134
|
+
headers: expandEnvRecord(server.headers, env, `MCP 服务 "${name}" 的 headers`),
|
|
138
135
|
...(server.description ? { description: server.description } : {}),
|
|
139
136
|
timeout,
|
|
140
137
|
};
|
|
141
138
|
}
|
|
142
139
|
const command = expandEnvTemplate(server.command, env, `MCP 服务 "${name}" 的 command`);
|
|
143
140
|
const args = (server.args ?? []).map((arg, index) => expandEnvTemplate(arg, env, `MCP 服务 "${name}" 的 args[${index}]`));
|
|
144
|
-
const resolvedEnv = {};
|
|
145
|
-
for (const [key, value] of Object.entries(server.env ?? {})) {
|
|
146
|
-
resolvedEnv[key] = expandEnvTemplate(value, env, `MCP 服务 "${name}" 的 env.${key}`);
|
|
147
|
-
}
|
|
148
141
|
const cwd = server.cwd ? resolve(root, expandEnvTemplate(server.cwd, env, `MCP 服务 "${name}" 的 cwd`)) : root;
|
|
149
142
|
return {
|
|
150
143
|
transport: "stdio",
|
|
151
144
|
name,
|
|
152
145
|
command,
|
|
153
146
|
args,
|
|
154
|
-
env:
|
|
147
|
+
env: expandEnvRecord(server.env, env, `MCP 服务 "${name}" 的 env`),
|
|
155
148
|
cwd,
|
|
156
149
|
...(server.description ? { description: server.description } : {}),
|
|
157
150
|
timeout,
|
|
@@ -175,11 +168,10 @@ export async function upsertWorkspaceMcpServer(root, name, server) {
|
|
|
175
168
|
throw new Error(current.error);
|
|
176
169
|
const directory = dirname(current.path);
|
|
177
170
|
await mkdir(directory, { recursive: true });
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return upsertWorkspaceMcpServer(root, name, server);
|
|
171
|
+
await updateJsonFile(directory, "mcp.json", validateMcpFile, existing => ({
|
|
172
|
+
...existing,
|
|
173
|
+
mcpServers: { ...existing?.mcpServers, [name]: validated },
|
|
174
|
+
}));
|
|
183
175
|
return readWorkspaceMcpConfig(root);
|
|
184
176
|
}
|
|
185
177
|
export async function removeWorkspaceMcpServer(root, name) {
|
|
@@ -190,11 +182,12 @@ export async function removeWorkspaceMcpServer(root, name) {
|
|
|
190
182
|
return { removed: false, configuration: current };
|
|
191
183
|
}
|
|
192
184
|
const directory = dirname(current.path);
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
185
|
+
const removed = await updateJsonFile(directory, "mcp.json", validateMcpFile, existing => {
|
|
186
|
+
if (!existing || !Object.hasOwn(existing.mcpServers, name))
|
|
187
|
+
return undefined;
|
|
188
|
+
const remaining = { ...existing.mcpServers };
|
|
189
|
+
delete remaining[name];
|
|
190
|
+
return { ...existing, mcpServers: remaining };
|
|
191
|
+
});
|
|
192
|
+
return { removed, configuration: await readWorkspaceMcpConfig(root) };
|
|
200
193
|
}
|
|
@@ -6,3 +6,4 @@ export declare function parseSkillFrontmatter(content: string): {
|
|
|
6
6
|
} | undefined;
|
|
7
7
|
/** Scan `<workspace>/.agents/skills/<dir>/SKILL.md` for workspace-authored skills. */
|
|
8
8
|
export declare function discoverWorkspaceSkills(root: string): Promise<WorkspaceSkillMeta[]>;
|
|
9
|
+
export declare function discoverSkills(skillsDir: string, indexPath: string): Promise<WorkspaceSkillMeta[]>;
|
|
@@ -55,7 +55,9 @@ export function parseSkillFrontmatter(content) {
|
|
|
55
55
|
}
|
|
56
56
|
/** Scan `<workspace>/.agents/skills/<dir>/SKILL.md` for workspace-authored skills. */
|
|
57
57
|
export async function discoverWorkspaceSkills(root) {
|
|
58
|
-
|
|
58
|
+
return discoverSkills(join(root, ".agents", "skills"), ".agents/skills");
|
|
59
|
+
}
|
|
60
|
+
export async function discoverSkills(skillsDir, indexPath) {
|
|
59
61
|
let entries;
|
|
60
62
|
try {
|
|
61
63
|
entries = await readdir(skillsDir, { withFileTypes: true });
|
|
@@ -83,7 +85,7 @@ export async function discoverWorkspaceSkills(root) {
|
|
|
83
85
|
results.push({
|
|
84
86
|
name: meta.name,
|
|
85
87
|
description: meta.description,
|
|
86
|
-
path:
|
|
88
|
+
path: join(indexPath, entry.name, "SKILL.md"),
|
|
87
89
|
});
|
|
88
90
|
}
|
|
89
91
|
results.sort((a, b) => a.name.localeCompare(b.name));
|
package/lib/storage/acp-files.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { isAbsolute } from "node:path";
|
|
2
2
|
import { object } from "../common/json.js";
|
|
3
|
-
import { isAcpBackend, requireThat } from "../common/validation.js";
|
|
3
|
+
import { isAcpBackend, isOneOf, requireThat } from "../common/validation.js";
|
|
4
4
|
import { fileId } from "./file-access.js";
|
|
5
|
+
import { acpAnswerStates, acpElicitationActions, acpPermissionPolicies, isAcpElicitationValue } from "../model/acp.js";
|
|
5
6
|
const text = (value) => typeof value === "string" && value.length > 0 && !value.includes("\0");
|
|
6
7
|
export function acpReference(value) {
|
|
7
8
|
requireThat(object(value) && value.host === "acp" && isAcpBackend(value.backend) &&
|
|
@@ -10,7 +11,7 @@ export function acpReference(value) {
|
|
|
10
11
|
if (value.env !== undefined) {
|
|
11
12
|
requireThat(object(value.env) && Object.entries(value.env).every(([k, v]) => text(k) && typeof v === "string" && !v.includes("\0")), "ACP 环境变量配置无效。");
|
|
12
13
|
}
|
|
13
|
-
requireThat(value.permission === undefined ||
|
|
14
|
+
requireThat(value.permission === undefined || isOneOf(value.permission, acpPermissionPolicies), "ACP 权限策略无效。");
|
|
14
15
|
const capabilities = value.capabilities;
|
|
15
16
|
if (capabilities !== undefined)
|
|
16
17
|
requireThat(object(capabilities) &&
|
|
@@ -33,7 +34,7 @@ export function acpInput(value) {
|
|
|
33
34
|
}
|
|
34
35
|
export function acpAnswer(value) {
|
|
35
36
|
acpInput(value);
|
|
36
|
-
requireThat(object(value) && typeof value.answer === "string" &&
|
|
37
|
+
requireThat(object(value) && typeof value.answer === "string" && isOneOf(value.state, acpAnswerStates) &&
|
|
37
38
|
(value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
|
|
38
39
|
return value;
|
|
39
40
|
}
|
|
@@ -44,7 +45,7 @@ export function acpReport(value) {
|
|
|
44
45
|
}
|
|
45
46
|
export function acpPermission(value) {
|
|
46
47
|
acpInput(value);
|
|
47
|
-
requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && ["allow_once", "allow_always", "reject_once", "reject_always"]
|
|
48
|
+
requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option => object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
|
|
48
49
|
return value;
|
|
49
50
|
}
|
|
50
51
|
export function acpPermissionReply(value) {
|
|
@@ -52,12 +53,6 @@ export function acpPermissionReply(value) {
|
|
|
52
53
|
requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
|
|
53
54
|
return value;
|
|
54
55
|
}
|
|
55
|
-
function elicitationValue(value) {
|
|
56
|
-
return typeof value === "string"
|
|
57
|
-
|| (typeof value === "number" && Number.isFinite(value))
|
|
58
|
-
|| typeof value === "boolean"
|
|
59
|
-
|| (Array.isArray(value) && value.every(item => typeof item === "string"));
|
|
60
|
-
}
|
|
61
56
|
export function acpElicitation(value) {
|
|
62
57
|
acpInput(value);
|
|
63
58
|
requireThat(object(value) && (value.mode === "form" || value.mode === "url") && typeof value.message === "string", "ACP 交互请求无效。");
|
|
@@ -71,10 +66,10 @@ export function acpElicitation(value) {
|
|
|
71
66
|
}
|
|
72
67
|
export function acpElicitationReply(value) {
|
|
73
68
|
acpInput(value);
|
|
74
|
-
requireThat(object(value) &&
|
|
69
|
+
requireThat(object(value) && isOneOf(value.action, acpElicitationActions), "ACP 交互答复无效。");
|
|
75
70
|
if (value.content !== undefined) {
|
|
76
71
|
requireThat(value.action === "accept" && object(value.content) &&
|
|
77
|
-
Object.values(value.content).every(
|
|
72
|
+
Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
|
|
78
73
|
}
|
|
79
74
|
return value;
|
|
80
75
|
}
|
|
@@ -2,9 +2,10 @@ import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { canonical, object } from "../common/json.js";
|
|
5
|
-
import { requireThat } from "../common/validation.js";
|
|
5
|
+
import { isOneOf, requireThat } from "../common/validation.js";
|
|
6
6
|
import { readJsonFile, writeJsonFile } from "./atomic-json.js";
|
|
7
7
|
import { fileId, privateDirectory } from "./file-access.js";
|
|
8
|
+
import { agentStates, cycleSources, cycleStates, messageDeliveries } from "../model/agent.js";
|
|
8
9
|
import { acpElicitation, acpPermission, acpReference } from "./acp-files.js";
|
|
9
10
|
const text = (v) => typeof v === "string" && v.length > 0;
|
|
10
11
|
const number = (v) => typeof v === "number" && Number.isFinite(v);
|
|
@@ -20,18 +21,18 @@ export function launch(v) {
|
|
|
20
21
|
}
|
|
21
22
|
export function message(v) {
|
|
22
23
|
requireThat(object(v) && text(v.requestId) && text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.message === "string" &&
|
|
23
|
-
|
|
24
|
+
isOneOf(v.delivery, messageDeliveries) && Array.isArray(v.files) && v.files.every(text), "子会话消息无效。");
|
|
24
25
|
return v;
|
|
25
26
|
}
|
|
26
27
|
export function cycle(v) {
|
|
27
28
|
requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) &&
|
|
28
|
-
|
|
29
|
+
isOneOf(v.state, cycleStates) &&
|
|
29
30
|
(v.leaf === null || text(v.leaf)) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
|
|
30
|
-
|
|
31
|
+
isOneOf(v.source, cycleSources) && (v.finishedAt === undefined || number(v.finishedAt)), "子会话周期无效。");
|
|
31
32
|
return v;
|
|
32
33
|
}
|
|
33
34
|
export function status(v) {
|
|
34
|
-
requireThat(object(v) && text(v.generation) &&
|
|
35
|
+
requireThat(object(v) && text(v.generation) && isOneOf(v.state, agentStates) &&
|
|
35
36
|
typeof v.activity === "string" && number(v.updatedAt), "子会话状态无效。");
|
|
36
37
|
if (v.cycle)
|
|
37
38
|
cycle(v.cycle);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** The root must already exist. Never follow aliases inside internal storage. */
|
|
2
|
-
export declare function internalPath(directory: string, relative: string): string;
|
|
2
|
+
export declare function internalPath(directory: string, relative: string, allowMissingParents?: boolean): string;
|
|
3
3
|
export declare function syncDirectory(directory: string): void;
|
|
4
4
|
/** Validators return plain data and must reject incomplete contracts. */
|
|
5
5
|
export declare function readJsonFile<T>(directory: string, relative: string, validate: (value: unknown) => T): T;
|
|
@@ -9,3 +9,5 @@ export declare function readJsonFile<T>(directory: string, relative: string, val
|
|
|
9
9
|
* createOnly publishes by link, so an existing destination is never overwritten.
|
|
10
10
|
*/
|
|
11
11
|
export declare function writeJsonFile<T>(directory: string, relative: string, value: unknown, validate: (value: unknown) => T, createOnly?: boolean): boolean;
|
|
12
|
+
/** Serialize a read-modify-write across processes. An undefined replacement means no change. */
|
|
13
|
+
export declare function updateJsonFile<T>(directory: string, relative: string, validate: (value: unknown) => T, update: (current: T | undefined) => T | undefined): Promise<boolean>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { constants, closeSync, fstatSync, fsyncSync, linkSync, lstatSync, openSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { constants, closeSync, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { dirname, join } from "node:path";
|
|
3
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
4
|
import { randomUUID } from "node:crypto";
|
|
4
5
|
import { canonical } from "../common/json.js";
|
|
5
6
|
import { requireThat } from "../common/validation.js";
|
|
6
7
|
/** The root must already exist. Never follow aliases inside internal storage. */
|
|
7
|
-
export function internalPath(directory, relative) {
|
|
8
|
+
export function internalPath(directory, relative, allowMissingParents = false) {
|
|
8
9
|
const parts = relative.split("/");
|
|
9
10
|
requireThat(parts.every(part => part && part !== "." && part !== ".." && !/[\\\x00-\x1f]/.test(part)), "内部状态路径无效。");
|
|
10
11
|
const info = lstatSync(directory);
|
|
@@ -17,7 +18,7 @@ export function internalPath(directory, relative) {
|
|
|
17
18
|
requireThat(!info.isSymbolicLink() && (index === parts.length - 1 ? info.isFile() : info.isDirectory()), "内部状态不能使用符号链接或特殊文件。");
|
|
18
19
|
}
|
|
19
20
|
catch (error) {
|
|
20
|
-
if (error.code !== "ENOENT" || index !== parts.length - 1)
|
|
21
|
+
if (error.code !== "ENOENT" || (!allowMissingParents && index !== parts.length - 1))
|
|
21
22
|
throw error;
|
|
22
23
|
}
|
|
23
24
|
}
|
|
@@ -99,3 +100,42 @@ export function writeJsonFile(directory, relative, value, validate, createOnly =
|
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
}
|
|
103
|
+
/** Serialize a read-modify-write across processes. An undefined replacement means no change. */
|
|
104
|
+
export async function updateJsonFile(directory, relative, validate, update) {
|
|
105
|
+
const lock = internalPath(directory, relative) + ".lock";
|
|
106
|
+
const deadline = Date.now() + 3000;
|
|
107
|
+
for (;;) {
|
|
108
|
+
try {
|
|
109
|
+
mkdirSync(lock, { mode: 0o700 });
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error.code !== "EEXIST")
|
|
114
|
+
throw error;
|
|
115
|
+
if (Date.now() >= deadline)
|
|
116
|
+
throw new Error(`写锁被占用:${lock}。若写入进程已退出,可手动移除遗留锁目录。`);
|
|
117
|
+
await delay(25);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
for (;;) {
|
|
122
|
+
let current;
|
|
123
|
+
try {
|
|
124
|
+
current = readJsonFile(directory, relative, validate);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
if (error.code !== "ENOENT")
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
const next = update(current);
|
|
131
|
+
if (next === undefined)
|
|
132
|
+
return false;
|
|
133
|
+
if (writeJsonFile(directory, relative, next, validate, current === undefined))
|
|
134
|
+
return true;
|
|
135
|
+
// An external writer created the file first. Apply the edit to its value.
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
rmdirSync(lock);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -31,6 +31,7 @@ export declare class HistoryFiles {
|
|
|
31
31
|
controlSummary<T>(dimension: string, operation: () => T): T;
|
|
32
32
|
/** Synchronous migration lock for explicit layout migration. */
|
|
33
33
|
controlMigration<T>(operation: () => T): T;
|
|
34
|
+
migrationNotice(): string | undefined;
|
|
34
35
|
/** Detect unmigrated top-level flat JSON files when running under monthly layout. */
|
|
35
36
|
detectFlatFiles(): FlatHistoryInventory;
|
|
36
37
|
listFlatRawFiles(): string[];
|
|
@@ -38,7 +39,7 @@ export declare class HistoryFiles {
|
|
|
38
39
|
readFlatRawFile(name: string): RawHistory;
|
|
39
40
|
readFlatSummaryFile(dimension: string, name: string): SummaryArtifact;
|
|
40
41
|
/** Atomically move a file within .pbot/history/ without ever overwriting an existing destination. */
|
|
41
|
-
moveHistoryFile(fromRelative: string, toRelative: string): "moved" | "conflict";
|
|
42
|
+
moveHistoryFile(fromRelative: string, toRelative: string, dryRun?: boolean): "moved" | "conflict";
|
|
42
43
|
listRaw(filterHost?: HistoryHost, onlyFiles?: readonly string[]): RawHistory[];
|
|
43
44
|
readRaw(id: string): RawHistory | undefined;
|
|
44
45
|
saveRaw(raw: RawHistory): void;
|