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,124 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { resourceRoot } from "../../../common/paths.js";
|
|
4
|
+
import { requireThat } from "../../../common/validation.js";
|
|
5
|
+
import { fingerprintExchanges, orderedRawExchanges, rawSessionSpan, rawSessionTitle } from "../history-index.js";
|
|
6
|
+
let cachedPrompt;
|
|
7
|
+
function systemPrompt() {
|
|
8
|
+
cachedPrompt ??= readFileSync(join(resourceRoot, "prompts/history-summary.md"), "utf8");
|
|
9
|
+
return cachedPrompt;
|
|
10
|
+
}
|
|
11
|
+
function formatExchanges(exchanges) {
|
|
12
|
+
return exchanges
|
|
13
|
+
.map(item => `[${item.from} — ${item.to}${item.completed ? "" : ";未完成"}]\n${item.text}`)
|
|
14
|
+
.join("\n\n");
|
|
15
|
+
}
|
|
16
|
+
function isStrictUnchangedPrefix(existing, ordered) {
|
|
17
|
+
const prevIds = existing.source.exchangeIds;
|
|
18
|
+
if (prevIds.length === 0 || prevIds.length >= ordered.length)
|
|
19
|
+
return false;
|
|
20
|
+
for (let i = 0; i < prevIds.length; i++) {
|
|
21
|
+
if (ordered[i].id !== prevIds[i])
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
const prefixFingerprint = fingerprintExchanges(ordered.slice(0, prevIds.length));
|
|
25
|
+
return prefixFingerprint === existing.source.fingerprint;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Default per-session task summary dimension.
|
|
29
|
+
* Produces one concise summary per raw session archive, supporting exact deduplication
|
|
30
|
+
* (0 tokens on unchanged sessions) and rolling incremental updates when new turns are appended.
|
|
31
|
+
*/
|
|
32
|
+
export const sessionSummaryDimension = {
|
|
33
|
+
id: "session",
|
|
34
|
+
title: "会话任务摘要",
|
|
35
|
+
description: "按单个会话提炼任务目标、关键决定、结果与未决事项,支持去重跳过与增量滚动更新。",
|
|
36
|
+
plan(raws, existing, options = {}) {
|
|
37
|
+
const force = Boolean(options.force);
|
|
38
|
+
const tasks = [];
|
|
39
|
+
let skipped = 0;
|
|
40
|
+
for (const raw of raws) {
|
|
41
|
+
const ordered = orderedRawExchanges(raw);
|
|
42
|
+
if (!ordered.length)
|
|
43
|
+
continue;
|
|
44
|
+
const exchangeIds = ordered.map(item => item.id);
|
|
45
|
+
const fingerprint = fingerprintExchanges(ordered);
|
|
46
|
+
const prev = existing.get(raw.id);
|
|
47
|
+
if (!force && prev && prev.source.fingerprint === fingerprint) {
|
|
48
|
+
skipped++;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const span = rawSessionSpan(ordered);
|
|
52
|
+
const title = rawSessionTitle(raw);
|
|
53
|
+
const source = { rawIds: [raw.id], exchangeIds, fingerprint };
|
|
54
|
+
if (!force && prev && isStrictUnchangedPrefix(prev, ordered)) {
|
|
55
|
+
tasks.push({
|
|
56
|
+
id: raw.id,
|
|
57
|
+
dimension: this.id,
|
|
58
|
+
mode: "incremental",
|
|
59
|
+
title,
|
|
60
|
+
host: raw.host,
|
|
61
|
+
sessionId: raw.sessionId,
|
|
62
|
+
from: span.from,
|
|
63
|
+
to: span.to,
|
|
64
|
+
source,
|
|
65
|
+
exchanges: ordered,
|
|
66
|
+
existing: prev,
|
|
67
|
+
addedExchanges: ordered.slice(prev.source.exchangeIds.length),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
tasks.push({
|
|
72
|
+
id: raw.id,
|
|
73
|
+
dimension: this.id,
|
|
74
|
+
mode: prev ? "rebuild" : "create",
|
|
75
|
+
title,
|
|
76
|
+
host: raw.host,
|
|
77
|
+
sessionId: raw.sessionId,
|
|
78
|
+
from: span.from,
|
|
79
|
+
to: span.to,
|
|
80
|
+
source,
|
|
81
|
+
exchanges: ordered,
|
|
82
|
+
...(prev ? { existing: prev } : {}),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Process most recently active sessions first.
|
|
87
|
+
tasks.sort((a, b) => b.to.localeCompare(a.to) || a.id.localeCompare(b.id));
|
|
88
|
+
return { tasks, skipped };
|
|
89
|
+
},
|
|
90
|
+
buildRequest(task) {
|
|
91
|
+
const system = systemPrompt();
|
|
92
|
+
const heading = task.title ? [`会话标题:${task.title}`] : [];
|
|
93
|
+
if (task.mode === "incremental" && task.existing && task.addedExchanges?.length) {
|
|
94
|
+
const prompt = [
|
|
95
|
+
...heading,
|
|
96
|
+
`【已有摘要(${task.existing.from} — ${task.existing.to})】\n${task.existing.text}`,
|
|
97
|
+
`【新增对话】\n${formatExchanges(task.addedExchanges)}`,
|
|
98
|
+
"在已有摘要基础上合并新增对话,输出更新后的完整摘要:保留仍有效的事实,用新决定覆盖旧决定,去重并保持简洁。",
|
|
99
|
+
].join("\n\n");
|
|
100
|
+
return { system, prompt, maxTokens: 2048 };
|
|
101
|
+
}
|
|
102
|
+
const prompt = [
|
|
103
|
+
...heading,
|
|
104
|
+
formatExchanges(task.exchanges),
|
|
105
|
+
].join("\n\n");
|
|
106
|
+
return { system, prompt, maxTokens: 2048 };
|
|
107
|
+
},
|
|
108
|
+
finalize(task, output, now) {
|
|
109
|
+
const text = output.trim();
|
|
110
|
+
requireThat(text.length > 0, "历史摘要为空。");
|
|
111
|
+
return {
|
|
112
|
+
id: task.id,
|
|
113
|
+
dimension: this.id,
|
|
114
|
+
...(task.host ? { host: task.host } : {}),
|
|
115
|
+
...(task.sessionId ? { sessionId: task.sessionId } : {}),
|
|
116
|
+
title: task.title,
|
|
117
|
+
text,
|
|
118
|
+
from: task.from,
|
|
119
|
+
to: task.to,
|
|
120
|
+
updatedAt: now,
|
|
121
|
+
source: task.source,
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HistoryLayout, SummaryDimension } from "../../model/history.js";
|
|
2
|
+
/** Register a custom summary dimension (e.g. daily, decisions, topics). */
|
|
3
|
+
export declare function registerSummaryDimension(dimension: SummaryDimension): void;
|
|
4
|
+
export declare function unregisterSummaryDimension(id: string): boolean;
|
|
5
|
+
export declare function getSummaryDimension(id: string): SummaryDimension | undefined;
|
|
6
|
+
export declare function listSummaryDimensions(layout?: HistoryLayout): SummaryDimension[];
|
|
7
|
+
export declare function resolveSummaryDimensions(ids?: readonly string[], layout?: HistoryLayout): SummaryDimension[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { requireThat } from "../../common/validation.js";
|
|
2
|
+
import { isSummaryDimension } from "../../storage/history-format.js";
|
|
3
|
+
import { monthlySummaryDimension } from "./summary-dimensions/monthly.js";
|
|
4
|
+
import { sessionSummaryDimension } from "./summary-dimensions/session.js";
|
|
5
|
+
const registry = new Map([
|
|
6
|
+
[sessionSummaryDimension.id, sessionSummaryDimension],
|
|
7
|
+
[monthlySummaryDimension.id, monthlySummaryDimension],
|
|
8
|
+
]);
|
|
9
|
+
/** Register a custom summary dimension (e.g. daily, decisions, topics). */
|
|
10
|
+
export function registerSummaryDimension(dimension) {
|
|
11
|
+
requireThat(isSummaryDimension(dimension.id), "摘要维度 ID 无效:" + dimension.id);
|
|
12
|
+
registry.set(dimension.id, dimension);
|
|
13
|
+
}
|
|
14
|
+
export function unregisterSummaryDimension(id) {
|
|
15
|
+
return registry.delete(id);
|
|
16
|
+
}
|
|
17
|
+
export function getSummaryDimension(id) {
|
|
18
|
+
return registry.get(id);
|
|
19
|
+
}
|
|
20
|
+
export function listSummaryDimensions(layout) {
|
|
21
|
+
const all = [...registry.values()];
|
|
22
|
+
if (layout === "flat")
|
|
23
|
+
return all.filter(dim => !dim.requiresMonthlyLayout);
|
|
24
|
+
return all;
|
|
25
|
+
}
|
|
26
|
+
export function resolveSummaryDimensions(ids, layout = "monthly") {
|
|
27
|
+
if (!ids || ids.length === 0)
|
|
28
|
+
return listSummaryDimensions(layout);
|
|
29
|
+
const unique = [...new Set(ids)];
|
|
30
|
+
return unique.map(id => {
|
|
31
|
+
const found = registry.get(id);
|
|
32
|
+
requireThat(found, `未知的摘要维度:${id}(可用:${[...registry.keys()].join(", ")})。`);
|
|
33
|
+
requireThat(!found.requiresMonthlyLayout || layout === "monthly", `摘要维度 ${id} 仅在年月目录布局(history.layout: "monthly")下启用。`);
|
|
34
|
+
return found;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { HistoryLayout, HistorySummarizer, HistorySummaryReminder, SummaryDimensionStatus, SummaryRunProgress, SummaryRunReport } from "../../model/history.js";
|
|
2
|
+
export declare const defaultSummaryReminderDays = 7;
|
|
3
|
+
export interface InspectSummaryOptions {
|
|
4
|
+
dimensions?: readonly string[];
|
|
5
|
+
thresholdDays?: number;
|
|
6
|
+
now?: Date;
|
|
7
|
+
}
|
|
8
|
+
export interface CheckSummaryReminderOptions {
|
|
9
|
+
dimensions?: readonly string[];
|
|
10
|
+
thresholdDays?: number;
|
|
11
|
+
now?: Date;
|
|
12
|
+
}
|
|
13
|
+
/** Read-only startup probe: reminds when raw history has existed >= thresholdDays and no summary ran within thresholdDays. */
|
|
14
|
+
export declare function checkHistorySummaryReminder(root: string, options?: CheckSummaryReminderOptions): HistorySummaryReminder | undefined;
|
|
15
|
+
/** Read-only inspection of raw archives vs derived summaries across requested dimensions. */
|
|
16
|
+
export declare function inspectWorkspaceSummaries(root: string, options?: InspectSummaryOptions): {
|
|
17
|
+
layout: HistoryLayout;
|
|
18
|
+
rawCount: number;
|
|
19
|
+
dimensions: SummaryDimensionStatus[];
|
|
20
|
+
warnings: string[];
|
|
21
|
+
reminder?: HistorySummaryReminder;
|
|
22
|
+
};
|
|
23
|
+
export interface RunSummaryOptions {
|
|
24
|
+
root: string;
|
|
25
|
+
summarizer: HistorySummarizer;
|
|
26
|
+
dimensions?: readonly string[];
|
|
27
|
+
force?: boolean;
|
|
28
|
+
concurrency?: number;
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
onProgress?: (progress: SummaryRunProgress) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Trigger-agnostic summary execution engine.
|
|
34
|
+
* Reads raw archives once, plans deduplicated incremental/full tasks per dimension,
|
|
35
|
+
* executes LLM calls with bounded concurrency, and persists each artifact atomically.
|
|
36
|
+
*/
|
|
37
|
+
export declare function runWorkspaceSummaries(options: RunSummaryOptions): Promise<SummaryRunReport>;
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
+
import { requireThat } from "../../common/validation.js";
|
|
4
|
+
import { HistoryFiles } from "../../storage/history-files.js";
|
|
5
|
+
import { readWorkspaceHistoryLayoutSync } from "../workspace/workspace-config.js";
|
|
6
|
+
import { rawSessionSpan } from "./history-index.js";
|
|
7
|
+
import { resolveSummaryDimensions } from "./summary-registry.js";
|
|
8
|
+
const defaultConcurrency = 3;
|
|
9
|
+
const maxConcurrency = 8;
|
|
10
|
+
const lockRetryAttempts = 5;
|
|
11
|
+
const lockRetryDelayMs = 40;
|
|
12
|
+
export const defaultSummaryReminderDays = 7;
|
|
13
|
+
const dayMs = 24 * 60 * 60 * 1000;
|
|
14
|
+
async function withSummaryLockRetry(files, dimension, operation, signal) {
|
|
15
|
+
for (let attempt = 0;; attempt++) {
|
|
16
|
+
signal?.throwIfAborted();
|
|
17
|
+
try {
|
|
18
|
+
return files.controlSummary(dimension, operation);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
const code = error.code;
|
|
22
|
+
if (code === "PBOT_HISTORY_BUSY" && attempt < lockRetryAttempts - 1) {
|
|
23
|
+
await delay(lockRetryDelayMs * (attempt + 1), undefined, { signal });
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function oldestRawTimestamp(raws) {
|
|
31
|
+
let oldest;
|
|
32
|
+
for (const raw of raws) {
|
|
33
|
+
if (!raw.exchanges.length)
|
|
34
|
+
continue;
|
|
35
|
+
const { from } = rawSessionSpan(raw.exchanges);
|
|
36
|
+
if (!from)
|
|
37
|
+
continue;
|
|
38
|
+
const parsed = Date.parse(from);
|
|
39
|
+
if (Number.isNaN(parsed))
|
|
40
|
+
continue;
|
|
41
|
+
if (oldest === undefined || parsed < oldest)
|
|
42
|
+
oldest = parsed;
|
|
43
|
+
}
|
|
44
|
+
return oldest;
|
|
45
|
+
}
|
|
46
|
+
function latestSummaryTimestamp(artifacts) {
|
|
47
|
+
let latest;
|
|
48
|
+
for (const artifact of artifacts) {
|
|
49
|
+
const parsed = Date.parse(artifact.updatedAt);
|
|
50
|
+
if (Number.isNaN(parsed))
|
|
51
|
+
continue;
|
|
52
|
+
if (latest === undefined || parsed > latest)
|
|
53
|
+
latest = parsed;
|
|
54
|
+
}
|
|
55
|
+
return latest;
|
|
56
|
+
}
|
|
57
|
+
function evaluateSummaryReminder(raws, allSummaries, pendingCount, options = {}) {
|
|
58
|
+
if (!raws.length || pendingCount <= 0)
|
|
59
|
+
return undefined;
|
|
60
|
+
const thresholdDays = options.thresholdDays ?? defaultSummaryReminderDays;
|
|
61
|
+
requireThat(Number.isFinite(thresholdDays) && thresholdDays > 0, "历史摘要提醒天数必须大于 0。");
|
|
62
|
+
const thresholdMs = thresholdDays * dayMs;
|
|
63
|
+
const nowMs = (options.now ?? new Date()).getTime();
|
|
64
|
+
const oldestRawMs = oldestRawTimestamp(raws);
|
|
65
|
+
if (oldestRawMs === undefined || nowMs - oldestRawMs < thresholdMs)
|
|
66
|
+
return undefined;
|
|
67
|
+
const latestSummaryMs = latestSummaryTimestamp(allSummaries);
|
|
68
|
+
if (latestSummaryMs !== undefined && nowMs - latestSummaryMs < thresholdMs)
|
|
69
|
+
return undefined;
|
|
70
|
+
const rawAgeDays = Math.floor((nowMs - oldestRawMs) / dayMs);
|
|
71
|
+
const daysSinceLastSummary = latestSummaryMs === undefined ? undefined : Math.floor((nowMs - latestSummaryMs) / dayMs);
|
|
72
|
+
const message = daysSinceLastSummary === undefined
|
|
73
|
+
? `历史原文已累计 ${rawAgeDays} 天(待摘要 ${pendingCount} 项),尚未触发过摘要整理;可运行 /pbot history summary 生成摘要。`
|
|
74
|
+
: `历史原文已累计 ${rawAgeDays} 天,且已 ${daysSinceLastSummary} 天未触发摘要整理(待摘要 ${pendingCount} 项);可运行 /pbot history summary 更新摘要。`;
|
|
75
|
+
return {
|
|
76
|
+
rawCount: raws.length,
|
|
77
|
+
pendingCount,
|
|
78
|
+
oldestRawAt: new Date(oldestRawMs).toISOString(),
|
|
79
|
+
...(latestSummaryMs !== undefined ? { latestSummaryAt: new Date(latestSummaryMs).toISOString() } : {}),
|
|
80
|
+
rawAgeDays,
|
|
81
|
+
...(daysSinceLastSummary !== undefined ? { daysSinceLastSummary } : {}),
|
|
82
|
+
thresholdDays,
|
|
83
|
+
message,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Read-only startup probe: reminds when raw history has existed >= thresholdDays and no summary ran within thresholdDays. */
|
|
87
|
+
export function checkHistorySummaryReminder(root, options = {}) {
|
|
88
|
+
const layout = readWorkspaceHistoryLayoutSync(root);
|
|
89
|
+
const files = new HistoryFiles(join(root, ".pbot/history"), "pi", layout);
|
|
90
|
+
const raws = files.listRaw();
|
|
91
|
+
if (!raws.length)
|
|
92
|
+
return undefined;
|
|
93
|
+
const thresholdDays = options.thresholdDays ?? defaultSummaryReminderDays;
|
|
94
|
+
requireThat(Number.isFinite(thresholdDays) && thresholdDays > 0, "历史摘要提醒天数必须大于 0。");
|
|
95
|
+
const nowMs = (options.now ?? new Date()).getTime();
|
|
96
|
+
const oldestRawMs = oldestRawTimestamp(raws);
|
|
97
|
+
if (oldestRawMs === undefined || nowMs - oldestRawMs < thresholdDays * dayMs)
|
|
98
|
+
return undefined;
|
|
99
|
+
const dimensions = resolveSummaryDimensions(options.dimensions, layout);
|
|
100
|
+
const seenDimensions = new Set();
|
|
101
|
+
const summariesByDimension = new Map();
|
|
102
|
+
const allSummaries = [];
|
|
103
|
+
let pendingCount = 0;
|
|
104
|
+
const loadDimension = (dimId) => {
|
|
105
|
+
const cached = summariesByDimension.get(dimId);
|
|
106
|
+
if (cached)
|
|
107
|
+
return cached;
|
|
108
|
+
const list = files.listSummaries(dimId);
|
|
109
|
+
const map = new Map(list.map(item => [item.id, item]));
|
|
110
|
+
summariesByDimension.set(dimId, map);
|
|
111
|
+
return map;
|
|
112
|
+
};
|
|
113
|
+
for (const dimension of dimensions) {
|
|
114
|
+
seenDimensions.add(dimension.id);
|
|
115
|
+
if (dimension.id !== "session")
|
|
116
|
+
loadDimension("session");
|
|
117
|
+
const existing = loadDimension(dimension.id);
|
|
118
|
+
allSummaries.push(...existing.values());
|
|
119
|
+
const plan = dimension.plan(raws, existing, { force: false, summariesByDimension });
|
|
120
|
+
pendingCount += plan.tasks.length;
|
|
121
|
+
}
|
|
122
|
+
for (const dim of files.listDimensions()) {
|
|
123
|
+
if (seenDimensions.has(dim))
|
|
124
|
+
continue;
|
|
125
|
+
allSummaries.push(...loadDimension(dim).values());
|
|
126
|
+
}
|
|
127
|
+
return evaluateSummaryReminder(raws, allSummaries, pendingCount, options);
|
|
128
|
+
}
|
|
129
|
+
/** Read-only inspection of raw archives vs derived summaries across requested dimensions. */
|
|
130
|
+
export function inspectWorkspaceSummaries(root, options = {}) {
|
|
131
|
+
const layout = readWorkspaceHistoryLayoutSync(root);
|
|
132
|
+
const files = new HistoryFiles(join(root, ".pbot/history"), "pi", layout);
|
|
133
|
+
const raws = files.listRaw();
|
|
134
|
+
const warnings = [...files.warnings];
|
|
135
|
+
if (layout === "monthly") {
|
|
136
|
+
const flatNotice = files.detectFlatFiles().message;
|
|
137
|
+
if (flatNotice)
|
|
138
|
+
warnings.push(flatNotice);
|
|
139
|
+
}
|
|
140
|
+
const dimensions = resolveSummaryDimensions(options.dimensions, layout);
|
|
141
|
+
const statuses = [];
|
|
142
|
+
const seenDimensions = new Set();
|
|
143
|
+
const summariesByDimension = new Map();
|
|
144
|
+
const allSummaries = [];
|
|
145
|
+
let totalPending = 0;
|
|
146
|
+
const loadDimension = (dimId) => {
|
|
147
|
+
const cached = summariesByDimension.get(dimId);
|
|
148
|
+
if (cached)
|
|
149
|
+
return cached;
|
|
150
|
+
const list = files.listSummaries(dimId);
|
|
151
|
+
warnings.push(...files.warnings);
|
|
152
|
+
const map = new Map(list.map(item => [item.id, item]));
|
|
153
|
+
summariesByDimension.set(dimId, map);
|
|
154
|
+
return map;
|
|
155
|
+
};
|
|
156
|
+
for (const dimension of dimensions) {
|
|
157
|
+
seenDimensions.add(dimension.id);
|
|
158
|
+
if (dimension.id !== "session")
|
|
159
|
+
loadDimension("session");
|
|
160
|
+
const existing = loadDimension(dimension.id);
|
|
161
|
+
allSummaries.push(...existing.values());
|
|
162
|
+
const plan = dimension.plan(raws, existing, { force: false, summariesByDimension });
|
|
163
|
+
totalPending += plan.tasks.length;
|
|
164
|
+
let pendingCreate = 0;
|
|
165
|
+
let pendingIncremental = 0;
|
|
166
|
+
let pendingRebuild = 0;
|
|
167
|
+
for (const task of plan.tasks) {
|
|
168
|
+
if (task.mode === "create")
|
|
169
|
+
pendingCreate++;
|
|
170
|
+
else if (task.mode === "incremental")
|
|
171
|
+
pendingIncremental++;
|
|
172
|
+
else
|
|
173
|
+
pendingRebuild++;
|
|
174
|
+
}
|
|
175
|
+
statuses.push({
|
|
176
|
+
dimension: dimension.id,
|
|
177
|
+
title: dimension.title,
|
|
178
|
+
description: dimension.description,
|
|
179
|
+
total: plan.skipped + plan.tasks.length,
|
|
180
|
+
ready: plan.skipped,
|
|
181
|
+
pendingCreate,
|
|
182
|
+
pendingIncremental,
|
|
183
|
+
pendingRebuild,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
for (const dim of files.listDimensions()) {
|
|
187
|
+
if (seenDimensions.has(dim))
|
|
188
|
+
continue;
|
|
189
|
+
allSummaries.push(...loadDimension(dim).values());
|
|
190
|
+
}
|
|
191
|
+
const reminder = evaluateSummaryReminder(raws, allSummaries, totalPending, options);
|
|
192
|
+
return {
|
|
193
|
+
layout,
|
|
194
|
+
rawCount: raws.length,
|
|
195
|
+
dimensions: statuses,
|
|
196
|
+
warnings: [...new Set(warnings)],
|
|
197
|
+
...(reminder ? { reminder } : {}),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Trigger-agnostic summary execution engine.
|
|
202
|
+
* Reads raw archives once, plans deduplicated incremental/full tasks per dimension,
|
|
203
|
+
* executes LLM calls with bounded concurrency, and persists each artifact atomically.
|
|
204
|
+
*/
|
|
205
|
+
export async function runWorkspaceSummaries(options) {
|
|
206
|
+
const startedAt = Date.now();
|
|
207
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
208
|
+
signal.throwIfAborted();
|
|
209
|
+
const concurrency = Math.max(1, Math.min(maxConcurrency, Math.floor(options.concurrency ?? defaultConcurrency)));
|
|
210
|
+
const layout = readWorkspaceHistoryLayoutSync(options.root);
|
|
211
|
+
const dimensions = resolveSummaryDimensions(options.dimensions, layout);
|
|
212
|
+
const files = new HistoryFiles(join(options.root, ".pbot/history"), "pi", layout);
|
|
213
|
+
const raws = files.listRaw();
|
|
214
|
+
const warnings = [...files.warnings];
|
|
215
|
+
if (layout === "monthly") {
|
|
216
|
+
const flatNotice = files.detectFlatFiles().message;
|
|
217
|
+
if (flatNotice)
|
|
218
|
+
warnings.push(flatNotice);
|
|
219
|
+
}
|
|
220
|
+
const summariesByDimension = new Map();
|
|
221
|
+
const reports = [];
|
|
222
|
+
for (const dimension of dimensions) {
|
|
223
|
+
signal.throwIfAborted();
|
|
224
|
+
if (dimension.id !== "session" && !summariesByDimension.has("session")) {
|
|
225
|
+
const sessionList = files.listSummaries("session");
|
|
226
|
+
warnings.push(...files.warnings);
|
|
227
|
+
summariesByDimension.set("session", new Map(sessionList.map(item => [item.id, item])));
|
|
228
|
+
}
|
|
229
|
+
const existingList = files.listSummaries(dimension.id);
|
|
230
|
+
warnings.push(...files.warnings);
|
|
231
|
+
const existing = new Map(existingList.map(item => [item.id, item]));
|
|
232
|
+
summariesByDimension.set(dimension.id, existing);
|
|
233
|
+
const plan = dimension.plan(raws, existing, { force: Boolean(options.force), summariesByDimension });
|
|
234
|
+
const report = {
|
|
235
|
+
dimension: dimension.id,
|
|
236
|
+
scanned: dimension.id === "monthly" ? (summariesByDimension.get("session")?.size ?? 0) : raws.length,
|
|
237
|
+
skipped: plan.skipped,
|
|
238
|
+
created: 0,
|
|
239
|
+
incremental: 0,
|
|
240
|
+
rebuilt: 0,
|
|
241
|
+
failed: [],
|
|
242
|
+
};
|
|
243
|
+
if (plan.tasks.length > 0) {
|
|
244
|
+
let nextIndex = 0;
|
|
245
|
+
let completed = 0;
|
|
246
|
+
const total = plan.tasks.length;
|
|
247
|
+
const worker = async () => {
|
|
248
|
+
for (;;) {
|
|
249
|
+
signal.throwIfAborted();
|
|
250
|
+
const index = nextIndex++;
|
|
251
|
+
if (index >= total)
|
|
252
|
+
return;
|
|
253
|
+
const task = plan.tasks[index];
|
|
254
|
+
try {
|
|
255
|
+
const request = dimension.buildRequest(task);
|
|
256
|
+
const output = await options.summarizer(request, signal);
|
|
257
|
+
signal.throwIfAborted();
|
|
258
|
+
const artifact = dimension.finalize(task, output, new Date().toISOString());
|
|
259
|
+
await withSummaryLockRetry(files, dimension.id, () => {
|
|
260
|
+
signal.throwIfAborted();
|
|
261
|
+
files.saveSummary(artifact);
|
|
262
|
+
}, signal);
|
|
263
|
+
existing.set(artifact.id, artifact);
|
|
264
|
+
if (task.mode === "create")
|
|
265
|
+
report.created++;
|
|
266
|
+
else if (task.mode === "incremental")
|
|
267
|
+
report.incremental++;
|
|
268
|
+
else
|
|
269
|
+
report.rebuilt++;
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
if (signal.aborted)
|
|
273
|
+
throw error;
|
|
274
|
+
report.failed.push({ id: task.id, error: error instanceof Error ? error.message : String(error) });
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
completed++;
|
|
278
|
+
if (!signal.aborted) {
|
|
279
|
+
options.onProgress?.({
|
|
280
|
+
dimension: dimension.id,
|
|
281
|
+
completed,
|
|
282
|
+
total,
|
|
283
|
+
currentId: task.id,
|
|
284
|
+
mode: task.mode,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const workerCount = Math.min(concurrency, total);
|
|
291
|
+
await Promise.all(Array.from({ length: workerCount }, () => worker()));
|
|
292
|
+
}
|
|
293
|
+
reports.push(report);
|
|
294
|
+
}
|
|
295
|
+
requireThat(!signal.aborted, "历史摘要已取消。");
|
|
296
|
+
return {
|
|
297
|
+
dimensions: reports,
|
|
298
|
+
durationMs: Date.now() - startedAt,
|
|
299
|
+
warnings: [...new Set(warnings)],
|
|
300
|
+
};
|
|
301
|
+
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import type { ResolvedWorkspaceFeatures, WorkspaceConfiguration, WorkspaceFeatures } from "../../model/workspace-config.js";
|
|
1
|
+
import type { HistoryLayout, ResolvedWorkspaceFeatures, ResolvedWorkspaceHistoryConfig, ResolvedWorkspaceSummaryConfig, WorkspaceConfiguration, WorkspaceFeatures, WorkspaceHistoryConfig, WorkspaceSummaryConfig } from "../../model/workspace-config.js";
|
|
2
2
|
export declare function workspaceFeatures(configuration?: Pick<WorkspaceConfiguration, "features"> | WorkspaceFeatures): ResolvedWorkspaceFeatures;
|
|
3
|
+
export declare function workspaceSummaryConfig(configuration?: Pick<WorkspaceConfiguration, "summary"> | WorkspaceSummaryConfig): ResolvedWorkspaceSummaryConfig;
|
|
4
|
+
export declare function workspaceHistoryConfig(configuration?: Pick<WorkspaceConfiguration, "history"> | WorkspaceHistoryConfig): ResolvedWorkspaceHistoryConfig;
|
|
3
5
|
export declare function workspaceConfigPath(root: string): string;
|
|
6
|
+
/** Synchronous read of history.layout for storage and query callers; defaults to "monthly". */
|
|
7
|
+
export declare function readWorkspaceHistoryLayoutSync(root: string): HistoryLayout;
|
|
4
8
|
/** Read only this workspace's configuration; parent directories never grant activation. */
|
|
5
9
|
export declare function readWorkspaceConfig(root: string): Promise<WorkspaceConfiguration>;
|
|
6
10
|
/** Explicit init opts in this workspace while preserving its other configuration fields. */
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
1
2
|
import { mkdir, readFile } from "node:fs/promises";
|
|
2
3
|
import { dirname, join } from "node:path";
|
|
3
4
|
import { object } from "../../common/json.js";
|
|
4
5
|
import { isAcpBackend } from "../../common/validation.js";
|
|
5
6
|
import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
|
|
6
|
-
const featureKeys = ["prompts", "skills", "mcp"];
|
|
7
|
+
const featureKeys = ["prompts", "skills", "mcp", "history"];
|
|
8
|
+
const summaryKeys = ["backend", "model", "thinking", "concurrency"];
|
|
9
|
+
const historyKeys = ["layout"];
|
|
10
|
+
const historyLayouts = ["monthly", "flat"];
|
|
11
|
+
function nonEmptyString(value) {
|
|
12
|
+
return typeof value === "string" && value.trim().length > 0 && !value.includes("\0");
|
|
13
|
+
}
|
|
7
14
|
function config(value) {
|
|
8
15
|
if (!object(value) || value.enabled !== undefined && typeof value.enabled !== "boolean") {
|
|
9
16
|
throw new Error("配置必须是 JSON 对象,enabled 必须是布尔值。");
|
|
@@ -11,7 +18,7 @@ function config(value) {
|
|
|
11
18
|
if (value.features !== undefined) {
|
|
12
19
|
if (!object(value.features) ||
|
|
13
20
|
Object.entries(value.features).some(([key, entry]) => !featureKeys.includes(key) || (entry !== undefined && typeof entry !== "boolean"))) {
|
|
14
|
-
throw new Error("features 必须是 JSON 对象,仅支持 prompts、skills、mcp 布尔开关。");
|
|
21
|
+
throw new Error("features 必须是 JSON 对象,仅支持 prompts、skills、mcp、history 布尔开关。");
|
|
15
22
|
}
|
|
16
23
|
}
|
|
17
24
|
if (value.acp !== undefined) {
|
|
@@ -32,6 +39,32 @@ function config(value) {
|
|
|
32
39
|
}
|
|
33
40
|
}
|
|
34
41
|
}
|
|
42
|
+
if (value.summary !== undefined) {
|
|
43
|
+
if (!object(value.summary) || Object.keys(value.summary).some(key => !summaryKeys.includes(key))) {
|
|
44
|
+
throw new Error("summary 必须是 JSON 对象,仅支持 backend、model、thinking、concurrency。");
|
|
45
|
+
}
|
|
46
|
+
if (value.summary.backend !== undefined && !isAcpBackend(value.summary.backend)) {
|
|
47
|
+
throw new Error("summary.backend 必须为小写字母、数字和连字符(1–64 字符)。");
|
|
48
|
+
}
|
|
49
|
+
if (value.summary.model !== undefined && !nonEmptyString(value.summary.model)) {
|
|
50
|
+
throw new Error("summary.model 必须是非空字符串。");
|
|
51
|
+
}
|
|
52
|
+
if (value.summary.thinking !== undefined && !nonEmptyString(value.summary.thinking)) {
|
|
53
|
+
throw new Error("summary.thinking 必须是非空字符串。");
|
|
54
|
+
}
|
|
55
|
+
if (value.summary.concurrency !== undefined &&
|
|
56
|
+
(typeof value.summary.concurrency !== "number" || !Number.isInteger(value.summary.concurrency) || value.summary.concurrency < 1 || value.summary.concurrency > 8)) {
|
|
57
|
+
throw new Error("summary.concurrency 必须是 1 到 8 之间的整数。");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (value.history !== undefined) {
|
|
61
|
+
if (!object(value.history) || Object.keys(value.history).some(key => !historyKeys.includes(key))) {
|
|
62
|
+
throw new Error("history 必须是 JSON 对象,仅支持 layout。");
|
|
63
|
+
}
|
|
64
|
+
if (value.history.layout !== undefined && !historyLayouts.includes(String(value.history.layout))) {
|
|
65
|
+
throw new Error('history.layout 必须是 "monthly" 或 "flat"。');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
35
68
|
return value;
|
|
36
69
|
}
|
|
37
70
|
export function workspaceFeatures(configuration) {
|
|
@@ -42,11 +75,43 @@ export function workspaceFeatures(configuration) {
|
|
|
42
75
|
prompts: raw?.prompts !== false,
|
|
43
76
|
skills: raw?.skills !== false,
|
|
44
77
|
mcp: raw?.mcp !== false,
|
|
78
|
+
history: raw?.history !== false,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function workspaceSummaryConfig(configuration) {
|
|
82
|
+
const raw = configuration && "summary" in configuration
|
|
83
|
+
? configuration.summary
|
|
84
|
+
: configuration;
|
|
85
|
+
return {
|
|
86
|
+
backend: raw?.backend ?? "pi",
|
|
87
|
+
...(raw?.model ? { model: raw.model } : {}),
|
|
88
|
+
...(raw?.thinking ? { thinking: raw.thinking } : {}),
|
|
89
|
+
concurrency: raw?.concurrency ?? 3,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export function workspaceHistoryConfig(configuration) {
|
|
93
|
+
const raw = configuration && "history" in configuration
|
|
94
|
+
? configuration.history
|
|
95
|
+
: configuration;
|
|
96
|
+
return {
|
|
97
|
+
layout: raw?.layout === "flat" ? "flat" : "monthly",
|
|
45
98
|
};
|
|
46
99
|
}
|
|
47
100
|
export function workspaceConfigPath(root) {
|
|
48
101
|
return join(root, ".pbot", "config.json");
|
|
49
102
|
}
|
|
103
|
+
/** Synchronous read of history.layout for storage and query callers; defaults to "monthly". */
|
|
104
|
+
export function readWorkspaceHistoryLayoutSync(root) {
|
|
105
|
+
const path = workspaceConfigPath(root);
|
|
106
|
+
try {
|
|
107
|
+
const content = readFileSync(path, "utf8");
|
|
108
|
+
const parsed = config(JSON.parse(content));
|
|
109
|
+
return workspaceHistoryConfig(parsed.history).layout;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return "monthly";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
50
115
|
/** Read only this workspace's configuration; parent directories never grant activation. */
|
|
51
116
|
export async function readWorkspaceConfig(root) {
|
|
52
117
|
const path = workspaceConfigPath(root);
|
|
@@ -68,6 +133,8 @@ export async function readWorkspaceConfig(root) {
|
|
|
68
133
|
enabled: settings.enabled === true,
|
|
69
134
|
...(settings.features ? { features: settings.features } : {}),
|
|
70
135
|
...(settings.acp ? { acp: settings.acp } : {}),
|
|
136
|
+
...(settings.summary ? { summary: settings.summary } : {}),
|
|
137
|
+
...(settings.history ? { history: settings.history } : {}),
|
|
71
138
|
};
|
|
72
139
|
}
|
|
73
140
|
catch (error) {
|
|
@@ -80,13 +147,15 @@ export async function initializeWorkspaceConfig(root) {
|
|
|
80
147
|
if (current.error)
|
|
81
148
|
throw new Error(current.error);
|
|
82
149
|
const hasExplicitFeatures = featureKeys.every(key => typeof current.features?.[key] === "boolean");
|
|
83
|
-
|
|
150
|
+
const hasExplicitSummary = typeof current.summary?.backend === "string" && typeof current.summary?.concurrency === "number";
|
|
151
|
+
if (current.enabled && hasExplicitFeatures && hasExplicitSummary)
|
|
84
152
|
return;
|
|
85
153
|
const directory = dirname(current.path);
|
|
86
154
|
await mkdir(directory, { recursive: true });
|
|
87
155
|
const value = current.present ? readJsonFile(directory, "config.json", config) : {};
|
|
88
156
|
const features = workspaceFeatures(value.features);
|
|
89
|
-
const
|
|
157
|
+
const summary = workspaceSummaryConfig(value.summary);
|
|
158
|
+
const written = writeJsonFile(directory, "config.json", { ...value, enabled: true, features, summary }, config, !current.present);
|
|
90
159
|
if (!written)
|
|
91
160
|
await initializeWorkspaceConfig(root);
|
|
92
161
|
}
|