personal-bot 0.3.1 → 0.3.2
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 +1 -1
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/bin/pbot-pi-acp.mjs +27 -11
- package/bin/pbot.mjs +1 -1
- package/docs/README.md +1 -1
- package/docs/architecture/design.md +23 -10
- package/docs/architecture/extensibility.md +20 -18
- package/docs/architecture/model-context.md +2 -2
- package/docs/architecture/principles.md +4 -3
- package/docs/guides/capabilities.md +3 -3
- package/docs/guides/history.md +50 -16
- package/docs/guides/workspace-configuration.md +31 -6
- package/docs/hosts/antigravity.md +2 -0
- package/docs/hosts/claude-code.md +1 -1
- package/docs/hosts/dsh.md +1 -1
- package/docs/hosts/pi.md +1 -1
- package/hooks/hooks.json +3 -0
- package/lib/antigravity-plugin/hooks.json +7 -0
- package/lib/antigravity-plugin/lib/claude.js +2 -2
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +156 -11
- package/lib/antigravity-plugin/lib/extension/{plugin-runtime.d.ts → hosts/plugin-runtime.d.ts} +2 -2
- package/lib/antigravity-plugin/lib/extension/{plugin-runtime.js → hosts/plugin-runtime.js} +34 -8
- package/lib/antigravity-plugin/lib/extension/mcp.js +1113 -78
- package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +1 -0
- package/lib/antigravity-plugin/lib/integrations/agent-resources.js +5 -1
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.d.ts +6 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.js +171 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/{claude.d.ts → claude/adapter.d.ts} +1 -1
- package/lib/antigravity-plugin/lib/integrations/hosts/{claude.js → claude/adapter.js} +32 -4
- package/lib/antigravity-plugin/lib/integrations/hosts/{registry.js → plugin-registry.js} +2 -2
- package/lib/antigravity-plugin/lib/model/history.d.ts +169 -0
- package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +26 -0
- package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +25 -0
- package/lib/antigravity-plugin/lib/plugin.js +2 -2
- package/lib/antigravity-plugin/lib/services/history/history-capture.d.ts +4 -0
- package/lib/antigravity-plugin/lib/services/history/history-capture.js +86 -0
- package/lib/antigravity-plugin/lib/services/history/history-index.d.ts +17 -0
- package/lib/antigravity-plugin/lib/services/history/history-index.js +112 -0
- package/lib/antigravity-plugin/lib/services/history/history-migrate.d.ts +12 -0
- package/lib/antigravity-plugin/lib/services/history/history-migrate.js +88 -0
- package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.d.ts +7 -0
- package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.js +121 -0
- package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.d.ts +7 -0
- package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.js +124 -0
- package/lib/antigravity-plugin/lib/services/history/summary-registry.d.ts +7 -0
- package/lib/antigravity-plugin/lib/services/history/summary-registry.js +36 -0
- package/lib/antigravity-plugin/lib/services/history/summary-runner.d.ts +37 -0
- package/lib/antigravity-plugin/lib/services/history/summary-runner.js +301 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.d.ts +5 -1
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +73 -4
- package/lib/antigravity-plugin/lib/storage/file-access.d.ts +2 -0
- package/lib/antigravity-plugin/lib/storage/file-access.js +26 -0
- package/lib/antigravity-plugin/lib/storage/history-files.d.ts +54 -0
- package/lib/antigravity-plugin/lib/storage/history-files.js +467 -0
- package/lib/antigravity-plugin/lib/storage/history-format.d.ts +6 -0
- package/lib/antigravity-plugin/lib/storage/history-format.js +62 -0
- package/lib/antigravity-plugin/package.json +1 -1
- package/lib/antigravity-plugin/plugin.json +1 -1
- package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +22 -12
- package/lib/claude.js +2 -2
- package/lib/client.js +21 -11
- package/lib/dsh.js +4 -4
- package/lib/extension/{acp-member.d.ts → acp/member.d.ts} +5 -5
- package/lib/extension/{acp-member.js → acp/member.js} +6 -6
- package/lib/extension/acp/worker.d.ts +1 -0
- package/lib/extension/acp/worker.js +384 -0
- package/lib/extension/acp-worker.js +156 -11
- package/lib/extension/{dsh → hosts/dsh}/acp-member.js +8 -8
- package/lib/extension/hosts/dsh/command.js +92 -0
- package/lib/extension/{dsh → hosts/dsh}/files.js +3 -3
- package/lib/extension/hosts/dsh/history.d.ts +4 -0
- package/lib/extension/hosts/dsh/history.js +62 -0
- package/lib/extension/{dsh → hosts/dsh}/management.d.ts +1 -1
- package/lib/extension/{dsh → hosts/dsh}/management.js +12 -10
- package/lib/extension/{dsh → hosts/dsh}/workspace.d.ts +3 -9
- package/lib/extension/{dsh → hosts/dsh}/workspace.js +32 -70
- package/lib/extension/{plugin-runtime.d.ts → hosts/plugin-runtime.d.ts} +2 -2
- package/lib/extension/{plugin-runtime.js → hosts/plugin-runtime.js} +34 -8
- package/lib/extension/{mcp-bridge.d.ts → mcp/bridge.d.ts} +1 -1
- package/lib/extension/{mcp-bridge.js → mcp/bridge.js} +4 -4
- package/lib/extension/{mcp-workers.d.ts → mcp/workers.d.ts} +222 -2
- package/lib/extension/{mcp-workers.js → mcp/workers.js} +229 -63
- package/lib/extension/mcp.js +1113 -78
- package/lib/extension/{setup-cli.js → setup/cli.js} +9 -9
- package/lib/integrations/acp/connection.d.ts +11 -10
- package/lib/integrations/acp/connection.js +6 -0
- package/lib/integrations/acp/harnesses/claude.js +1 -1
- package/lib/integrations/acp/harnesses/dsh.js +1 -1
- package/lib/integrations/acp/history-summary.d.ts +17 -0
- package/lib/integrations/acp/history-summary.js +100 -0
- package/lib/integrations/agent-resources.d.ts +1 -0
- package/lib/integrations/agent-resources.js +5 -1
- package/lib/integrations/hosts/antigravity/adapter.d.ts +6 -0
- package/lib/integrations/hosts/antigravity/adapter.js +171 -0
- package/lib/integrations/hosts/{claude.d.ts → claude/adapter.d.ts} +1 -1
- package/lib/integrations/hosts/{claude.js → claude/adapter.js} +32 -4
- package/lib/integrations/{dsh → hosts/dsh}/mcp.d.ts +1 -1
- package/lib/integrations/{dsh → hosts/dsh}/resources.d.ts +1 -1
- package/lib/integrations/{dsh → hosts/dsh}/resources.js +4 -4
- package/lib/integrations/{dsh → hosts/dsh}/schema.js +1 -1
- package/lib/integrations/{dsh → hosts/dsh}/skills.js +1 -1
- package/lib/integrations/hosts/{registry.js → plugin-registry.js} +2 -2
- package/lib/integrations/installation/pi-setup.js +1 -1
- package/lib/model/history.d.ts +133 -34
- package/lib/model/plugin-host.d.ts +26 -0
- package/lib/model/workspace-config.d.ts +25 -0
- package/lib/plugin.js +2 -2
- package/lib/services/agents/agent-run.d.ts +1 -1
- package/lib/services/history/history-capture.d.ts +4 -0
- package/lib/services/history/history-capture.js +86 -0
- package/lib/services/history/history-index.d.ts +16 -7
- package/lib/services/history/history-index.js +48 -70
- package/lib/services/history/history-migrate.d.ts +12 -0
- package/lib/services/history/history-migrate.js +88 -0
- package/lib/services/history/history-query.d.ts +51 -0
- package/lib/services/history/history-query.js +371 -0
- package/lib/services/history/summary-command.d.ts +20 -0
- package/lib/services/history/summary-command.js +68 -0
- package/lib/services/history/summary-dimensions/monthly.d.ts +7 -0
- package/lib/services/history/summary-dimensions/monthly.js +121 -0
- package/lib/services/history/summary-dimensions/session.d.ts +7 -0
- package/lib/services/history/summary-dimensions/session.js +124 -0
- package/lib/services/history/summary-registry.d.ts +7 -0
- package/lib/services/history/summary-registry.js +36 -0
- package/lib/services/history/summary-runner.d.ts +37 -0
- package/lib/services/history/summary-runner.js +301 -0
- package/lib/services/workspace/workspace-config.d.ts +5 -1
- package/lib/services/workspace/workspace-config.js +73 -4
- package/lib/storage/history-files.d.ts +45 -9
- package/lib/storage/history-files.js +373 -247
- package/lib/storage/history-format.d.ts +3 -12
- package/lib/storage/history-format.js +26 -50
- package/lib/tools/history/actions.d.ts +3 -68
- package/lib/tools/history/actions.js +7 -5
- package/lib/tools/history/contract.d.ts +14 -7
- package/lib/tools/history/contract.js +58 -18
- package/lib/tools/history/dsh-tool.d.ts +1 -2
- package/lib/tools/history/dsh-tool.js +11 -5
- package/lib/ui/dsh/management.js +18 -5
- package/package.json +1 -1
- package/resources/prompts/history-coordinator.md +1 -1
- package/resources/prompts/history-monthly-summary.md +13 -0
- package/resources/prompts/history-summary.md +10 -4
- package/resources/skills/provider-connections/SKILL.md +22 -12
- package/src/claude.ts +2 -2
- package/src/dsh.ts +4 -4
- package/src/extension/{acp-member.ts → acp/member.ts} +7 -7
- package/src/extension/{acp-worker.ts → acp/worker.ts} +131 -17
- package/src/extension/{dsh → hosts/dsh}/acp-member.ts +9 -9
- package/src/extension/hosts/dsh/command.ts +92 -0
- package/src/extension/{dsh → hosts/dsh}/files.ts +3 -3
- package/src/extension/hosts/dsh/history.ts +67 -0
- package/src/extension/{dsh → hosts/dsh}/management.ts +11 -9
- package/src/extension/{dsh → hosts/dsh}/workspace.ts +31 -50
- package/src/extension/{pi-acp-member.ts → hosts/pi/acp-member.ts} +9 -9
- package/src/extension/hosts/pi/command.ts +158 -0
- package/src/extension/{events.ts → hosts/pi/events.ts} +14 -4
- package/src/extension/hosts/pi/history.ts +45 -0
- package/src/extension/{lifecycle.ts → hosts/pi/lifecycle.ts} +7 -11
- package/src/extension/{plugin-runtime.ts → hosts/plugin-runtime.ts} +29 -9
- package/src/extension/{mcp-bridge.ts → mcp/bridge.ts} +5 -5
- package/src/extension/{mcp-workers.ts → mcp/workers.ts} +237 -64
- package/src/extension/{setup-cli.ts → setup/cli.ts} +11 -11
- package/src/integrations/acp/connection.ts +6 -0
- package/src/integrations/acp/harnesses/claude.ts +1 -1
- package/src/integrations/acp/harnesses/dsh.ts +1 -1
- package/src/integrations/acp/history-summary.ts +108 -0
- package/src/integrations/agent-resources.ts +6 -0
- package/src/integrations/hosts/antigravity/adapter.ts +161 -0
- package/src/integrations/hosts/{claude.ts → claude/adapter.ts} +28 -5
- package/src/integrations/{dsh → hosts/dsh}/mcp.ts +1 -1
- package/src/integrations/{dsh → hosts/dsh}/resources.ts +5 -5
- package/src/integrations/{dsh → hosts/dsh}/schema.ts +1 -1
- package/src/integrations/{dsh → hosts/dsh}/session.ts +2 -2
- package/src/integrations/{dsh → hosts/dsh}/skills.ts +1 -1
- package/src/integrations/{pi → hosts/pi}/mcp.ts +1 -1
- package/src/integrations/{pi → hosts/pi}/skills.ts +1 -1
- package/src/integrations/hosts/{registry.ts → plugin-registry.ts} +2 -2
- package/src/integrations/installation/pi-setup.ts +1 -1
- package/src/mcp.ts +1 -1
- package/src/model/history.ts +151 -26
- package/src/model/plugin-host.ts +26 -2
- package/src/model/workspace-config.ts +30 -0
- package/src/pbot.ts +13 -22
- package/src/plugin.ts +2 -2
- package/src/services/history/history-capture.ts +83 -0
- package/src/services/history/history-index.ts +48 -62
- package/src/services/history/history-migrate.ts +89 -0
- package/src/services/history/history-query.ts +415 -0
- package/src/services/history/summary-command.ts +69 -0
- package/src/services/history/summary-dimensions/monthly.ts +139 -0
- package/src/services/history/summary-dimensions/session.ts +149 -0
- package/src/services/history/summary-registry.ts +44 -0
- package/src/services/history/summary-runner.ts +352 -0
- package/src/services/workspace/workspace-config.ts +91 -5
- package/src/storage/history-files.ts +408 -188
- package/src/storage/history-format.ts +46 -60
- package/src/tools/history/actions.ts +8 -6
- package/src/tools/history/contract.ts +65 -20
- package/src/tools/history/dsh-tool.ts +17 -8
- package/src/tools/history/pi-tool.ts +21 -0
- package/src/tools/register.ts +3 -4
- package/src/ui/dsh/management.ts +27 -11
- package/src/ui/{confirmation.ts → pi/confirmation.ts} +1 -1
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.d.ts +0 -4
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.js +0 -79
- package/lib/extension/dsh/command.js +0 -50
- package/lib/extension/dsh/history.d.ts +0 -15
- package/lib/extension/dsh/history.js +0 -87
- package/lib/integrations/dsh/history-summary.d.ts +0 -11
- package/lib/integrations/dsh/history-summary.js +0 -36
- package/lib/integrations/dsh/history.d.ts +0 -16
- package/lib/integrations/dsh/history.js +0 -167
- package/lib/integrations/history-summary.d.ts +0 -4
- package/lib/integrations/history-summary.js +0 -32
- package/lib/integrations/hosts/antigravity.d.ts +0 -4
- package/lib/integrations/hosts/antigravity.js +0 -79
- package/lib/services/history/history-service.d.ts +0 -106
- package/lib/services/history/history-service.js +0 -242
- package/lib/storage/acp-history.d.ts +0 -10
- package/lib/storage/acp-history.js +0 -57
- package/src/extension/command.ts +0 -76
- package/src/extension/dsh/command.ts +0 -54
- package/src/extension/dsh/history.ts +0 -74
- package/src/extension/history.ts +0 -26
- package/src/integrations/dsh/history-summary.ts +0 -39
- package/src/integrations/dsh/history.ts +0 -84
- package/src/integrations/history-summary.ts +0 -30
- package/src/integrations/hosts/antigravity.ts +0 -83
- package/src/integrations/pi/history.ts +0 -99
- package/src/services/history/history-service.ts +0 -195
- package/src/storage/acp-history.ts +0 -49
- package/src/tools/history/tool.ts +0 -19
- /package/lib/antigravity-plugin/lib/integrations/hosts/{registry.d.ts → plugin-registry.d.ts} +0 -0
- /package/lib/{extension/acp-worker.d.ts → antigravity-plugin/lib/model/history.js} +0 -0
- /package/lib/extension/{dsh → hosts/dsh}/acp-member.d.ts +0 -0
- /package/lib/extension/{dsh → hosts/dsh}/command.d.ts +0 -0
- /package/lib/extension/{dsh → hosts/dsh}/files.d.ts +0 -0
- /package/lib/extension/{setup-guard.js → setup/guard.js} +0 -0
- /package/lib/integrations/acp/{claude.d.ts → claude-settings.d.ts} +0 -0
- /package/lib/integrations/acp/{claude.js → claude-settings.js} +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/mcp.js +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/messages.d.ts +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/messages.js +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/root-agents.d.ts +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/root-agents.js +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/schema.d.ts +0 -0
- /package/lib/integrations/{dsh → hosts/dsh}/skills.d.ts +0 -0
- /package/lib/integrations/hosts/{registry.d.ts → plugin-registry.d.ts} +0 -0
- /package/src/extension/{setup-guard.ts → setup/guard.ts} +0 -0
- /package/src/integrations/acp/{claude.ts → claude-settings.ts} +0 -0
- /package/src/integrations/{dsh → hosts/dsh}/messages.ts +0 -0
- /package/src/integrations/{dsh → hosts/dsh}/root-agents.ts +0 -0
- /package/src/integrations/{pi → hosts/pi}/agent-tools.ts +0 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { linkSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmdirSync, unlinkSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import { isDeepStrictEqual } from "node:util";
|
|
5
|
+
import { requireThat } from "../common/validation.js";
|
|
6
|
+
import { internalPath, readJsonFile, syncDirectory, writeJsonFile } from "./atomic-json.js";
|
|
7
|
+
import { fileId, privateDirectory } from "./file-access.js";
|
|
8
|
+
import { isHistoryHost, isSummaryDimension, matchIdentity, rawHistory, summaryArtifact } from "./history-format.js";
|
|
9
|
+
const yearPattern = /^\d{4}$/;
|
|
10
|
+
const monthPattern = /^(?:0[1-9]|1[0-2])$/;
|
|
11
|
+
const monthlyIdPattern = /^(\d{4})-(0[1-9]|1[0-2])$/;
|
|
12
|
+
const monthlyRelativeJsonPattern = /^(?:\d{4}\/(?:0[1-9]|1[0-2])|unknown)\/[^/]+\.json$/;
|
|
13
|
+
const yearlyRelativeJsonPattern = /^(?:\d{4}|unknown)\/[^/]+\.json$/;
|
|
14
|
+
export const unknownHistoryBucket = "unknown";
|
|
15
|
+
function ordinaryDirectory(path) {
|
|
16
|
+
const stat = lstatSync(path, { throwIfNoEntry: false });
|
|
17
|
+
if (!stat)
|
|
18
|
+
return false;
|
|
19
|
+
requireThat(stat.isDirectory() && !stat.isSymbolicLink(), "历史目录必须使用普通目录:" + path);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
function ordinaryFile(path) {
|
|
23
|
+
const stat = lstatSync(path, { throwIfNoEntry: false });
|
|
24
|
+
return Boolean(stat && stat.isFile() && !stat.isSymbolicLink());
|
|
25
|
+
}
|
|
26
|
+
function sharedDirectory(path) {
|
|
27
|
+
const parent = dirname(path);
|
|
28
|
+
return (basename(parent) !== ".pbot" || ordinaryDirectory(parent)) && ordinaryDirectory(path);
|
|
29
|
+
}
|
|
30
|
+
function ensureDirectory(path) {
|
|
31
|
+
if (sharedDirectory(path))
|
|
32
|
+
return;
|
|
33
|
+
const first = mkdirSync(path, { recursive: true, mode: 0o700 });
|
|
34
|
+
requireThat(sharedDirectory(path), "无法创建历史目录:" + path);
|
|
35
|
+
if (first) {
|
|
36
|
+
const existing = dirname(resolve(first));
|
|
37
|
+
for (let child = resolve(path); child !== existing && child !== dirname(child); child = dirname(child)) {
|
|
38
|
+
syncDirectory(realpathSync(dirname(child)));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function earliestTimestamp(values) {
|
|
43
|
+
let bestMs;
|
|
44
|
+
let bestValue;
|
|
45
|
+
for (const value of values) {
|
|
46
|
+
if (!value)
|
|
47
|
+
continue;
|
|
48
|
+
const ms = Date.parse(value);
|
|
49
|
+
if (Number.isNaN(ms))
|
|
50
|
+
continue;
|
|
51
|
+
if (bestMs === undefined || ms < bestMs) {
|
|
52
|
+
bestMs = ms;
|
|
53
|
+
bestValue = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return bestValue;
|
|
57
|
+
}
|
|
58
|
+
/** Resolve UTC YYYY/MM bucket from an ISO timestamp, falling back to "unknown". */
|
|
59
|
+
export function historyBucketFromTimestamp(timestamp) {
|
|
60
|
+
if (!timestamp)
|
|
61
|
+
return unknownHistoryBucket;
|
|
62
|
+
const ms = Date.parse(timestamp);
|
|
63
|
+
if (Number.isNaN(ms))
|
|
64
|
+
return unknownHistoryBucket;
|
|
65
|
+
const date = new Date(ms);
|
|
66
|
+
const year = String(date.getUTCFullYear()).padStart(4, "0");
|
|
67
|
+
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
|
|
68
|
+
return `${year}/${month}`;
|
|
69
|
+
}
|
|
70
|
+
export function rawHistoryBucket(raw) {
|
|
71
|
+
const earliest = earliestTimestamp(raw.exchanges.map(item => item.from))
|
|
72
|
+
?? earliestTimestamp(raw.exchanges.map(item => item.to));
|
|
73
|
+
return historyBucketFromTimestamp(earliest);
|
|
74
|
+
}
|
|
75
|
+
export function summaryArtifactBucket(artifact) {
|
|
76
|
+
if (artifact.dimension === "monthly") {
|
|
77
|
+
const match = monthlyIdPattern.exec(artifact.id);
|
|
78
|
+
if (match)
|
|
79
|
+
return match[1];
|
|
80
|
+
const bucket = historyBucketFromTimestamp(artifact.from || artifact.to);
|
|
81
|
+
return bucket === unknownHistoryBucket ? unknownHistoryBucket : bucket.slice(0, 4);
|
|
82
|
+
}
|
|
83
|
+
return historyBucketFromTimestamp(artifact.from || artifact.to);
|
|
84
|
+
}
|
|
85
|
+
const topLevelJsonNames = (directory) => readdirSync(directory, { withFileTypes: true })
|
|
86
|
+
.filter(entry => entry.isFile() && !entry.isSymbolicLink() && entry.name.endsWith(".json"))
|
|
87
|
+
.map(entry => entry.name)
|
|
88
|
+
.sort();
|
|
89
|
+
function bucketDirs(directory, depth) {
|
|
90
|
+
if (!ordinaryDirectory(directory))
|
|
91
|
+
return [];
|
|
92
|
+
const buckets = [];
|
|
93
|
+
const entries = readdirSync(directory, { withFileTypes: true });
|
|
94
|
+
const years = entries
|
|
95
|
+
.filter(entry => entry.isDirectory() && !entry.isSymbolicLink() && yearPattern.test(entry.name))
|
|
96
|
+
.map(entry => entry.name)
|
|
97
|
+
.sort((a, b) => b.localeCompare(a));
|
|
98
|
+
if (depth === "year") {
|
|
99
|
+
buckets.push(...years);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
for (const year of years) {
|
|
103
|
+
const yearDir = join(directory, year);
|
|
104
|
+
const months = readdirSync(yearDir, { withFileTypes: true })
|
|
105
|
+
.filter(entry => entry.isDirectory() && !entry.isSymbolicLink() && monthPattern.test(entry.name))
|
|
106
|
+
.map(entry => entry.name)
|
|
107
|
+
.sort((a, b) => b.localeCompare(a));
|
|
108
|
+
for (const month of months)
|
|
109
|
+
buckets.push(`${year}/${month}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (entries.some(entry => entry.isDirectory() && !entry.isSymbolicLink() && entry.name === unknownHistoryBucket)) {
|
|
113
|
+
buckets.push(unknownHistoryBucket);
|
|
114
|
+
}
|
|
115
|
+
return buckets;
|
|
116
|
+
}
|
|
117
|
+
function listLayoutJsonFiles(directory, layout, depth = "month") {
|
|
118
|
+
if (!ordinaryDirectory(directory))
|
|
119
|
+
return [];
|
|
120
|
+
if (layout === "flat")
|
|
121
|
+
return topLevelJsonNames(directory);
|
|
122
|
+
const results = [];
|
|
123
|
+
for (const bucket of bucketDirs(directory, depth)) {
|
|
124
|
+
const dir = join(directory, bucket);
|
|
125
|
+
for (const name of topLevelJsonNames(dir)) {
|
|
126
|
+
results.push(`${bucket}/${name}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return results;
|
|
130
|
+
}
|
|
131
|
+
function locateLayoutFile(directory, name, layout, depth = "month") {
|
|
132
|
+
if (!ordinaryDirectory(directory))
|
|
133
|
+
return undefined;
|
|
134
|
+
if (layout === "flat") {
|
|
135
|
+
return ordinaryFile(join(directory, name)) ? name : undefined;
|
|
136
|
+
}
|
|
137
|
+
for (const bucket of bucketDirs(directory, depth)) {
|
|
138
|
+
if (ordinaryFile(join(directory, bucket, name)))
|
|
139
|
+
return `${bucket}/${name}`;
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const filename = (id) => fileId(id) + ".json";
|
|
144
|
+
const diagnostic = (path, error) => path + ": " + String(error);
|
|
145
|
+
function optional(read) {
|
|
146
|
+
try {
|
|
147
|
+
return read();
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (error.code !== "ENOENT")
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function namedRaw(value, relPath) {
|
|
155
|
+
const raw = rawHistory(value);
|
|
156
|
+
requireThat(basename(relPath) === filename(raw.id), "历史文件名与根 ID 不匹配。");
|
|
157
|
+
return raw;
|
|
158
|
+
}
|
|
159
|
+
function namedSummary(value, relPath, dimension) {
|
|
160
|
+
const artifact = summaryArtifact(value, dimension);
|
|
161
|
+
requireThat(basename(relPath) === filename(artifact.id), "历史摘要文件名与 ID 不匹配。");
|
|
162
|
+
return artifact;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Use ripgrep when available to pre-filter candidate JSON files containing a literal query.
|
|
166
|
+
* Queries containing JSON-escaped characters fall back to direct Node reading to avoid false negatives.
|
|
167
|
+
*/
|
|
168
|
+
function canPrefilterWithRg(query) {
|
|
169
|
+
return query.length > 0 && !/["\\\n\r\t]/.test(query);
|
|
170
|
+
}
|
|
171
|
+
export function findCandidateJsonFiles(directory, query, layout = "monthly", depth = "month") {
|
|
172
|
+
if (!ordinaryDirectory(directory))
|
|
173
|
+
return [];
|
|
174
|
+
const trimmed = query.trim();
|
|
175
|
+
if (!trimmed)
|
|
176
|
+
return listLayoutJsonFiles(directory, layout, depth);
|
|
177
|
+
const relativePattern = depth === "year" ? yearlyRelativeJsonPattern : monthlyRelativeJsonPattern;
|
|
178
|
+
if (canPrefilterWithRg(trimmed)) {
|
|
179
|
+
const args = [
|
|
180
|
+
"--ignore-case",
|
|
181
|
+
"--fixed-strings",
|
|
182
|
+
"--files-with-matches",
|
|
183
|
+
"--glob", "*.json",
|
|
184
|
+
"--no-ignore",
|
|
185
|
+
"--hidden",
|
|
186
|
+
...(layout === "flat" ? ["--max-depth", "1"] : []),
|
|
187
|
+
"--",
|
|
188
|
+
trimmed,
|
|
189
|
+
directory,
|
|
190
|
+
];
|
|
191
|
+
const result = spawnSync("rg", args, { encoding: "utf8", maxBuffer: 8 * 1024 * 1024 });
|
|
192
|
+
if (!result.error && (result.status === 0 || result.status === 1)) {
|
|
193
|
+
if (result.status === 1)
|
|
194
|
+
return [];
|
|
195
|
+
const base = realpathSync(directory);
|
|
196
|
+
const matched = [];
|
|
197
|
+
for (const line of result.stdout.split(/\r?\n/)) {
|
|
198
|
+
const trimmedLine = line.trim();
|
|
199
|
+
if (!trimmedLine)
|
|
200
|
+
continue;
|
|
201
|
+
const rel = relative(base, resolve(trimmedLine)).replaceAll("\\", "/");
|
|
202
|
+
if (layout === "flat") {
|
|
203
|
+
if (!rel.includes("/") && rel.endsWith(".json"))
|
|
204
|
+
matched.push(rel);
|
|
205
|
+
}
|
|
206
|
+
else if (relativePattern.test(rel)) {
|
|
207
|
+
matched.push(rel);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return matched;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Fallback: direct case-insensitive file scan via Node built-ins.
|
|
214
|
+
const needle = trimmed.toLowerCase();
|
|
215
|
+
const matched = [];
|
|
216
|
+
for (const rel of listLayoutJsonFiles(directory, layout, depth)) {
|
|
217
|
+
try {
|
|
218
|
+
const content = readFileSync(join(directory, rel), "utf8");
|
|
219
|
+
if (content.toLowerCase().includes(needle))
|
|
220
|
+
matched.push(rel);
|
|
221
|
+
}
|
|
222
|
+
catch { /* Individual unreadable files are reported during JSON parse if needed. */ }
|
|
223
|
+
}
|
|
224
|
+
return matched;
|
|
225
|
+
}
|
|
226
|
+
export class HistoryFiles {
|
|
227
|
+
directory;
|
|
228
|
+
host;
|
|
229
|
+
layout;
|
|
230
|
+
#warnings = [];
|
|
231
|
+
#rawWarnings = [];
|
|
232
|
+
constructor(directory, host = "pi", layout = "monthly") {
|
|
233
|
+
this.directory = directory;
|
|
234
|
+
this.host = host;
|
|
235
|
+
this.layout = layout;
|
|
236
|
+
requireThat(isHistoryHost(host), "历史来源无效。");
|
|
237
|
+
}
|
|
238
|
+
get warnings() { return this.#warnings; }
|
|
239
|
+
get rawWarnings() { return this.#rawWarnings; }
|
|
240
|
+
get rawDirectory() { return join(this.directory, "raw"); }
|
|
241
|
+
get summariesDirectory() { return join(this.directory, "summaries"); }
|
|
242
|
+
summaryDirectory(dimension) {
|
|
243
|
+
requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
|
|
244
|
+
return join(this.summariesDirectory, dimension);
|
|
245
|
+
}
|
|
246
|
+
withLock(lockName, label, operation) {
|
|
247
|
+
ensureDirectory(this.directory);
|
|
248
|
+
const lock = join(this.directory, lockName);
|
|
249
|
+
try {
|
|
250
|
+
mkdirSync(lock, { mode: 0o700 });
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
if (error.code === "EEXIST") {
|
|
254
|
+
throw Object.assign(new Error("历史写锁被占用:" + label), { code: "PBOT_HISTORY_BUSY" });
|
|
255
|
+
}
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
return operation();
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
rmdirSync(lock);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** Synchronous host write lock for raw history files. */
|
|
266
|
+
control(operation) {
|
|
267
|
+
return this.withLock(".write-" + this.host, this.host, operation);
|
|
268
|
+
}
|
|
269
|
+
/** Synchronous dimension write lock for derived summary files. */
|
|
270
|
+
controlSummary(dimension, operation) {
|
|
271
|
+
requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
|
|
272
|
+
return this.withLock(".write-summary-" + dimension, "summary:" + dimension, operation);
|
|
273
|
+
}
|
|
274
|
+
/** Synchronous migration lock for explicit layout migration. */
|
|
275
|
+
controlMigration(operation) {
|
|
276
|
+
return this.withLock(".write-migrate", "migrate", operation);
|
|
277
|
+
}
|
|
278
|
+
/** Detect unmigrated top-level flat JSON files when running under monthly layout. */
|
|
279
|
+
detectFlatFiles() {
|
|
280
|
+
if (!sharedDirectory(this.directory))
|
|
281
|
+
return { rawCount: 0, summaryCount: 0, total: 0 };
|
|
282
|
+
const rawCount = ordinaryDirectory(this.rawDirectory) ? topLevelJsonNames(this.rawDirectory).length : 0;
|
|
283
|
+
let summaryCount = 0;
|
|
284
|
+
for (const dim of this.listDimensions()) {
|
|
285
|
+
const dir = this.summaryDirectory(dim);
|
|
286
|
+
if (ordinaryDirectory(dir))
|
|
287
|
+
summaryCount += topLevelJsonNames(dir).length;
|
|
288
|
+
}
|
|
289
|
+
const total = rawCount + summaryCount;
|
|
290
|
+
if (total === 0)
|
|
291
|
+
return { rawCount: 0, summaryCount: 0, total: 0 };
|
|
292
|
+
return {
|
|
293
|
+
rawCount,
|
|
294
|
+
summaryCount,
|
|
295
|
+
total,
|
|
296
|
+
message: `检测到 ${total} 个未迁移的平铺历史文件(raw ${rawCount},summaries ${summaryCount});当前为年月目录布局(history.layout: monthly),未迁移文件不会被读取。` +
|
|
297
|
+
`请运行 /pbot history migrate 整理到 YYYY/MM/ 目录,或在 .pbot/config.json 中设置 "history": { "layout": "flat" } 切回平铺布局。`,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
listFlatRawFiles() {
|
|
301
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.rawDirectory))
|
|
302
|
+
return [];
|
|
303
|
+
return topLevelJsonNames(this.rawDirectory);
|
|
304
|
+
}
|
|
305
|
+
listFlatSummaryFiles(dimension) {
|
|
306
|
+
requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
|
|
307
|
+
const dir = this.summaryDirectory(dimension);
|
|
308
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory) || !ordinaryDirectory(dir))
|
|
309
|
+
return [];
|
|
310
|
+
return topLevelJsonNames(dir);
|
|
311
|
+
}
|
|
312
|
+
readFlatRawFile(name) {
|
|
313
|
+
return readJsonFile(this.directory, "raw/" + name, value => namedRaw(value, name));
|
|
314
|
+
}
|
|
315
|
+
readFlatSummaryFile(dimension, name) {
|
|
316
|
+
requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
|
|
317
|
+
return readJsonFile(this.directory, `summaries/${dimension}/${name}`, value => namedSummary(value, name, dimension));
|
|
318
|
+
}
|
|
319
|
+
/** Atomically move a file within .pbot/history/ without ever overwriting an existing destination. */
|
|
320
|
+
moveHistoryFile(fromRelative, toRelative) {
|
|
321
|
+
ensureDirectory(this.directory);
|
|
322
|
+
const src = internalPath(this.directory, fromRelative);
|
|
323
|
+
const dstParentRel = dirname(toRelative);
|
|
324
|
+
if (dstParentRel && dstParentRel !== ".") {
|
|
325
|
+
privateDirectory(this.directory, dstParentRel);
|
|
326
|
+
}
|
|
327
|
+
const dst = internalPath(this.directory, toRelative);
|
|
328
|
+
try {
|
|
329
|
+
linkSync(src, dst);
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
if (error.code === "EEXIST")
|
|
333
|
+
return "conflict";
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
unlinkSync(src);
|
|
337
|
+
syncDirectory(dirname(dst));
|
|
338
|
+
syncDirectory(dirname(src));
|
|
339
|
+
return "moved";
|
|
340
|
+
}
|
|
341
|
+
listRaw(filterHost, onlyFiles) {
|
|
342
|
+
this.#warnings = [];
|
|
343
|
+
this.#rawWarnings = [];
|
|
344
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.rawDirectory))
|
|
345
|
+
return [];
|
|
346
|
+
const files = onlyFiles ?? listLayoutJsonFiles(this.rawDirectory, this.layout);
|
|
347
|
+
const results = [];
|
|
348
|
+
for (const rel of files) {
|
|
349
|
+
const path = join(this.rawDirectory, rel);
|
|
350
|
+
try {
|
|
351
|
+
const raw = readJsonFile(this.directory, "raw/" + rel, value => namedRaw(value, rel));
|
|
352
|
+
if (filterHost !== undefined && raw.host !== filterHost)
|
|
353
|
+
continue;
|
|
354
|
+
results.push(raw);
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
if (error.code !== "ENOENT") {
|
|
358
|
+
const message = diagnostic(path, error);
|
|
359
|
+
this.#warnings.push(message);
|
|
360
|
+
this.#rawWarnings.push(message);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return results;
|
|
365
|
+
}
|
|
366
|
+
readRaw(id) {
|
|
367
|
+
const name = filename(id);
|
|
368
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.rawDirectory))
|
|
369
|
+
return undefined;
|
|
370
|
+
const rel = locateLayoutFile(this.rawDirectory, name, this.layout);
|
|
371
|
+
if (!rel)
|
|
372
|
+
return undefined;
|
|
373
|
+
return optional(() => readJsonFile(this.directory, "raw/" + rel, value => namedRaw(value, rel)));
|
|
374
|
+
}
|
|
375
|
+
saveRaw(raw) {
|
|
376
|
+
requireThat(isHistoryHost(raw.host) && raw.host === this.host, "历史归档只允许所属来源写入。");
|
|
377
|
+
rawHistory(raw);
|
|
378
|
+
ensureDirectory(this.directory);
|
|
379
|
+
const name = filename(raw.id);
|
|
380
|
+
const existingRel = ordinaryDirectory(this.rawDirectory)
|
|
381
|
+
? locateLayoutFile(this.rawDirectory, name, this.layout)
|
|
382
|
+
: undefined;
|
|
383
|
+
const bucket = this.layout === "monthly" ? rawHistoryBucket(raw) : undefined;
|
|
384
|
+
const rel = existingRel ?? (bucket ? `${bucket}/${name}` : name);
|
|
385
|
+
const current = optional(() => readJsonFile(this.directory, "raw/" + rel, value => namedRaw(value, rel)));
|
|
386
|
+
if (current) {
|
|
387
|
+
matchIdentity(current, raw);
|
|
388
|
+
if (isDeepStrictEqual(current, raw))
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
privateDirectory(this.directory, bucket ? `raw/${bucket}` : "raw");
|
|
393
|
+
}
|
|
394
|
+
requireThat(writeJsonFile(this.directory, "raw/" + rel, raw, rawHistory, !current), "历史根 ID 已被占用:" + rel);
|
|
395
|
+
}
|
|
396
|
+
listDimensions() {
|
|
397
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory))
|
|
398
|
+
return [];
|
|
399
|
+
const entries = readdirSync(this.summariesDirectory, { withFileTypes: true });
|
|
400
|
+
return entries
|
|
401
|
+
.filter(entry => entry.isDirectory() && !entry.isSymbolicLink() && isSummaryDimension(entry.name))
|
|
402
|
+
.map(entry => entry.name)
|
|
403
|
+
.sort();
|
|
404
|
+
}
|
|
405
|
+
summaryBucketDepth(dimension) {
|
|
406
|
+
return dimension === "monthly" ? "year" : "month";
|
|
407
|
+
}
|
|
408
|
+
listSummaries(dimension, onlyFiles) {
|
|
409
|
+
requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
|
|
410
|
+
const dir = this.summaryDirectory(dimension);
|
|
411
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory) || !ordinaryDirectory(dir))
|
|
412
|
+
return [];
|
|
413
|
+
const files = onlyFiles ?? listLayoutJsonFiles(dir, this.layout, this.summaryBucketDepth(dimension));
|
|
414
|
+
const results = [];
|
|
415
|
+
for (const rel of files) {
|
|
416
|
+
const path = join(dir, rel);
|
|
417
|
+
try {
|
|
418
|
+
results.push(readJsonFile(this.directory, `summaries/${dimension}/${rel}`, value => namedSummary(value, rel, dimension)));
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
if (error.code !== "ENOENT") {
|
|
422
|
+
this.#warnings.push(diagnostic(path, error));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return results;
|
|
427
|
+
}
|
|
428
|
+
readSummary(dimension, id) {
|
|
429
|
+
requireThat(isSummaryDimension(dimension), "历史摘要维度无效:" + dimension);
|
|
430
|
+
const dir = this.summaryDirectory(dimension);
|
|
431
|
+
if (!sharedDirectory(this.directory) || !ordinaryDirectory(this.summariesDirectory) || !ordinaryDirectory(dir))
|
|
432
|
+
return undefined;
|
|
433
|
+
const name = filename(id);
|
|
434
|
+
const rel = locateLayoutFile(dir, name, this.layout, this.summaryBucketDepth(dimension));
|
|
435
|
+
if (!rel)
|
|
436
|
+
return undefined;
|
|
437
|
+
return optional(() => readJsonFile(this.directory, `summaries/${dimension}/${rel}`, value => namedSummary(value, rel, dimension)));
|
|
438
|
+
}
|
|
439
|
+
saveSummary(artifact) {
|
|
440
|
+
summaryArtifact(artifact);
|
|
441
|
+
ensureDirectory(this.directory);
|
|
442
|
+
const name = filename(artifact.id);
|
|
443
|
+
const dir = this.summaryDirectory(artifact.dimension);
|
|
444
|
+
const depth = this.summaryBucketDepth(artifact.dimension);
|
|
445
|
+
const existingRel = ordinaryDirectory(this.summariesDirectory) && ordinaryDirectory(dir)
|
|
446
|
+
? locateLayoutFile(dir, name, this.layout, depth)
|
|
447
|
+
: undefined;
|
|
448
|
+
const bucket = this.layout === "monthly" ? summaryArtifactBucket(artifact) : undefined;
|
|
449
|
+
const rel = existingRel ?? (bucket ? `${bucket}/${name}` : name);
|
|
450
|
+
const relativePath = `summaries/${artifact.dimension}/${rel}`;
|
|
451
|
+
privateDirectory(this.directory, dirname(relativePath));
|
|
452
|
+
const current = optional(() => readJsonFile(this.directory, relativePath, value => namedSummary(value, rel, artifact.dimension)));
|
|
453
|
+
if (current && isDeepStrictEqual(current, artifact))
|
|
454
|
+
return;
|
|
455
|
+
requireThat(writeJsonFile(this.directory, relativePath, artifact, value => summaryArtifact(value, artifact.dimension), !current), "历史摘要文件已被占用:" + relativePath);
|
|
456
|
+
}
|
|
457
|
+
/** Isolated read-only candidate search across raw/ and summaries/<dimension>/. */
|
|
458
|
+
searchCandidateRaw(query) {
|
|
459
|
+
const names = findCandidateJsonFiles(this.rawDirectory, query, this.layout, "month");
|
|
460
|
+
return this.listRaw(undefined, names);
|
|
461
|
+
}
|
|
462
|
+
searchCandidateSummaries(dimension, query) {
|
|
463
|
+
const dir = this.summaryDirectory(dimension);
|
|
464
|
+
const names = findCandidateJsonFiles(dir, query, this.layout, this.summaryBucketDepth(dimension));
|
|
465
|
+
return this.listSummaries(dimension, names);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HistoryHost, HistoryIdentity, RawHistory, SummaryArtifact } from "../model/history.js";
|
|
2
|
+
export declare const isHistoryHost: (value: unknown) => value is HistoryHost;
|
|
3
|
+
export declare const isSummaryDimension: (value: unknown) => value is string;
|
|
4
|
+
export declare function matchIdentity(value: HistoryIdentity, expected: HistoryIdentity): void;
|
|
5
|
+
export declare function rawHistory(value: unknown): RawHistory;
|
|
6
|
+
export declare function summaryArtifact(value: unknown, expectedDimension?: string): SummaryArtifact;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { object } from "../common/json.js";
|
|
2
|
+
import { requireThat } from "../common/validation.js";
|
|
3
|
+
import { fileId } from "./file-access.js";
|
|
4
|
+
const strings = (value) => Array.isArray(value) && value.every(item => typeof item === "string");
|
|
5
|
+
const fields = (value, names) => Object.keys(value).every(key => names.includes(key));
|
|
6
|
+
const rawFields = ["host", "id", "sessionId", "name", "exchanges", "branches"];
|
|
7
|
+
const exchangeFields = ["id", "entryIds", "text", "from", "to", "completed"];
|
|
8
|
+
const summaryArtifactFields = ["id", "dimension", "host", "sessionId", "title", "text", "from", "to", "updatedAt", "source"];
|
|
9
|
+
const summarySourceFields = ["rawIds", "exchangeIds", "fingerprint"];
|
|
10
|
+
const identifierPattern = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
11
|
+
export const isHistoryHost = (value) => typeof value === "string" && identifierPattern.test(value);
|
|
12
|
+
export const isSummaryDimension = (value) => typeof value === "string" && identifierPattern.test(value);
|
|
13
|
+
function identity(value) {
|
|
14
|
+
requireThat(object(value) && isHistoryHost(value.host) && typeof value.id === "string" &&
|
|
15
|
+
typeof value.sessionId === "string" && value.sessionId.length > 0, "历史归档身份无效。");
|
|
16
|
+
fileId(value.id);
|
|
17
|
+
}
|
|
18
|
+
export function matchIdentity(value, expected) {
|
|
19
|
+
for (const key of ["host", "id", "sessionId"]) {
|
|
20
|
+
requireThat(value[key] === expected[key], `历史归档 ${key} 不匹配,预期 ${expected[key]},实际 ${value[key]}。`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function exchange(value) {
|
|
24
|
+
return object(value) && typeof value.id === "string" && value.id.length > 0 && strings(value.entryIds) &&
|
|
25
|
+
typeof value.text === "string" && typeof value.from === "string" && typeof value.to === "string" && typeof value.completed === "boolean";
|
|
26
|
+
}
|
|
27
|
+
export function rawHistory(value) {
|
|
28
|
+
identity(value);
|
|
29
|
+
requireThat(object(value) && fields(value, rawFields) && typeof value.name === "string" &&
|
|
30
|
+
Array.isArray(value.exchanges) && Array.isArray(value.branches), "历史原文归档无效。");
|
|
31
|
+
const ids = new Set();
|
|
32
|
+
for (const item of value.exchanges) {
|
|
33
|
+
requireThat(exchange(item) && fields(item, exchangeFields) && item.entryIds.length > 0, "历史问答或其来源消息 ID 无效。");
|
|
34
|
+
requireThat(item.id !== value.id && !ids.has(item.id), "历史问答 ID 重复:" + item.id);
|
|
35
|
+
ids.add(item.id);
|
|
36
|
+
}
|
|
37
|
+
for (const branch of value.branches) {
|
|
38
|
+
requireThat(strings(branch) && new Set(branch).size === branch.length && branch.every(id => ids.has(id)), "历史分支包含重复或未知的问答 ID。");
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
export function summaryArtifact(value, expectedDimension) {
|
|
43
|
+
requireThat(object(value) && fields(value, summaryArtifactFields), "历史摘要产物格式无效。");
|
|
44
|
+
requireThat(typeof value.id === "string" && value.id.length > 0, "历史摘要 ID 无效。");
|
|
45
|
+
fileId(value.id);
|
|
46
|
+
requireThat(isSummaryDimension(value.dimension), "历史摘要维度无效。");
|
|
47
|
+
if (expectedDimension !== undefined) {
|
|
48
|
+
requireThat(value.dimension === expectedDimension, `历史摘要维度不匹配,预期 ${expectedDimension},实际 ${value.dimension}。`);
|
|
49
|
+
}
|
|
50
|
+
if (value.host !== undefined)
|
|
51
|
+
requireThat(isHistoryHost(value.host), "历史摘要来源 host 无效。");
|
|
52
|
+
if (value.sessionId !== undefined)
|
|
53
|
+
requireThat(typeof value.sessionId === "string" && value.sessionId.length > 0, "历史摘要 sessionId 无效。");
|
|
54
|
+
requireThat(typeof value.title === "string" && typeof value.text === "string" && value.text.trim().length > 0 &&
|
|
55
|
+
typeof value.from === "string" && typeof value.to === "string" && typeof value.updatedAt === "string", "历史摘要正文或时间字段无效。");
|
|
56
|
+
const source = value.source;
|
|
57
|
+
requireThat(object(source) && fields(source, summarySourceFields) &&
|
|
58
|
+
strings(source.rawIds) && source.rawIds.length > 0 &&
|
|
59
|
+
strings(source.exchangeIds) &&
|
|
60
|
+
typeof source.fingerprint === "string" && source.fingerprint.length > 0, "历史摘要来源指纹无效。");
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
@@ -5,31 +5,41 @@ description: 接入或修复工作区共享 MCP 与原生 CLI/MCP,核对认证
|
|
|
5
5
|
|
|
6
6
|
# 工具与 MCP 接入
|
|
7
7
|
|
|
8
|
-
先查看工作区 `providers/AGENTS.md`、`providers/<id>/GUIDE.md`、`.pbot/mcp.json`
|
|
8
|
+
先查看工作区 `providers/AGENTS.md`、`providers/<id>/GUIDE.md`、`.pbot/mcp.json` 和当前宿主原生 MCP 列表,优先复用已有接入;同一个 MCP 服务只在一处注册,不同时写入 `.pbot/mcp.json` 与宿主原生配置。
|
|
9
9
|
|
|
10
10
|
## CLI
|
|
11
11
|
|
|
12
12
|
确认已安装版本,用本次所需子命令的 `--help` 核对参数、默认值和校验规则,沿用原生登录与 profile。
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## MCP 归属判断
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
接入新 MCP 前先判断归属;边界不明确或用户未说明作用域时,写入前先向用户确认放工作区共享还是当前宿主原生:
|
|
17
17
|
|
|
18
|
-
-
|
|
18
|
+
- **工作区共享 MCP(`.pbot/mcp.json`)**:归属当前工作区业务、需要跨宿主(Pi、dsh、Claude Code、Antigravity、Codex 及 ACP worker)共用,使用 stdio 或 Streamable HTTP + 环境变量/本地凭据,纯 `tools/list` + `tools/call` 请求-响应且可并发连接的服务。
|
|
19
|
+
- **宿主原生 MCP(全局或项目级)**:满足以下任一条件时走当前宿主原生入口,不写入 `.pbot/mcp.json`:
|
|
20
|
+
1. 跨项目通用的本机全局基础设施或开发工具(如浏览器调试、全局调度);
|
|
21
|
+
2. 依赖宿主浏览器交互式 OAuth 登录;
|
|
22
|
+
3. 依赖宿主配套插件、斜杠命令、原生技能、MCP resources/prompts 或服务端主动事件推送;
|
|
23
|
+
4. 工具极多且高频交互,或独占本机端口/浏览器目录、不宜自动透传给 ACP worker;
|
|
24
|
+
5. 用户明确要求绑定到当前宿主原生配置。
|
|
25
|
+
|
|
26
|
+
## 工作区共享 MCP(`.pbot/mcp.json`)
|
|
27
|
+
|
|
28
|
+
不保存明文凭据,使用 `${ENV_VAR}` 引用,保留无关项,不能使用保留名 `pbot`:
|
|
29
|
+
|
|
30
|
+
- **查看**:`pbot mcp list --workspace <workspaceRoot>` 或(轻插件宿主)调用 `workspace_mcp_discover`
|
|
19
31
|
- **新增 / 更新 stdio**:`pbot mcp add <name> --workspace <workspaceRoot> [--env K=\${VAR}] [--cwd .] [--description "..."] -- <command> [args...]`
|
|
20
32
|
- **新增 / 更新 HTTP**:`pbot mcp add <name> --workspace <workspaceRoot> --url https://... [--header Authorization="Bearer \${TOKEN}"] [--description "..."]`
|
|
21
33
|
- **删除**:`pbot mcp remove <name> --workspace <workspaceRoot>`
|
|
22
34
|
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
|
|
28
|
-
## 宿主原生 MCP(交互式 OAuth 或全局服务)
|
|
35
|
+
生效方式(原生挂载与桥接二选一,不重复暴露):
|
|
36
|
+
- Pi 执行 `/reload`、dsh 重启会话后由宿主直接原生挂载(不暴露 `workspace_mcp_*`);
|
|
37
|
+
- 轻插件宿主与任意 MCP 宿主通过 `workspace_mcp_discover` / `workspace_mcp_call` 桥接,即改即生效,无需重启;
|
|
38
|
+
- ACP worker 在下次新建或恢复会话时自动加载最新 `.pbot/mcp.json`。
|
|
29
39
|
|
|
30
|
-
|
|
40
|
+
## 宿主原生 MCP
|
|
31
41
|
|
|
32
|
-
- **`host = pi
|
|
42
|
+
- **`host = pi`**:项目级用 `pi mcp add --local <server> ...`(写入 `.pi/mcp.json`),全局省略 `--local`;改完后请用户在会话中执行 `/reload` 并用 `/mcp` 检查。
|
|
33
43
|
- **`host = dsh`**:通过原生界面或 profile 配置管理(注意同一 profile 可能被多工作区共用)。
|
|
34
44
|
- **`host = claude`**:`claude mcp add --scope project --transport http <server> <url>`(写入 `.mcp.json`),改完后请用户在会话中用 `/mcp` 核对认证与工具。
|
|
35
45
|
- **`host = antigravity`**:全局使用 `agy mcp add <name> <commandOrUrl> [args...]` / `agy mcp remove <name>`(管理 `~/.gemini/config/mcp_config.json`),通过 `agy mcp list` 或 UI 面板核对。
|
package/lib/claude.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { realpathSync } from "node:fs";
|
|
2
2
|
import { pathToFileURL } from "node:url";
|
|
3
|
-
import { runPluginHost } from "./extension/plugin-runtime.js";
|
|
4
|
-
import { claudeAdapter } from "./integrations/hosts/claude.js";
|
|
3
|
+
import { runPluginHost } from "./extension/hosts/plugin-runtime.js";
|
|
4
|
+
import { claudeAdapter } from "./integrations/hosts/claude/adapter.js";
|
|
5
5
|
export function main(args) {
|
|
6
6
|
return runPluginHost(claudeAdapter, args);
|
|
7
7
|
}
|