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,51 @@
|
|
|
1
|
+
import type { HistoryHost, HistoryQuery } from "../../model/history.js";
|
|
2
|
+
export interface HistoryNodeBrief {
|
|
3
|
+
host: HistoryHost;
|
|
4
|
+
id: string;
|
|
5
|
+
kind: "session" | "exchange" | "summary";
|
|
6
|
+
dimension?: string;
|
|
7
|
+
sessionId: string;
|
|
8
|
+
name: string;
|
|
9
|
+
preview: string;
|
|
10
|
+
from: string;
|
|
11
|
+
to: string;
|
|
12
|
+
ready: boolean;
|
|
13
|
+
completed: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface HistorySearchMatch extends HistoryNodeBrief {
|
|
16
|
+
parents: string[];
|
|
17
|
+
snippet: string;
|
|
18
|
+
}
|
|
19
|
+
export interface HistorySearchResult {
|
|
20
|
+
matches: HistorySearchMatch[];
|
|
21
|
+
total: number;
|
|
22
|
+
nextOffset: number | null;
|
|
23
|
+
warnings: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface HistoryReadNode extends HistoryNodeBrief {
|
|
26
|
+
text: string;
|
|
27
|
+
totalLength: number;
|
|
28
|
+
nextOffset: number | null;
|
|
29
|
+
parents: HistoryNodeBrief[];
|
|
30
|
+
children: HistoryNodeBrief[];
|
|
31
|
+
nextChildrenOffset: number | null;
|
|
32
|
+
source: {
|
|
33
|
+
host: HistoryHost;
|
|
34
|
+
sessionId: string;
|
|
35
|
+
entryIds: string[];
|
|
36
|
+
dimension?: string;
|
|
37
|
+
};
|
|
38
|
+
warnings: string[];
|
|
39
|
+
}
|
|
40
|
+
export declare function summaryNodeId(dimension: string, artifactId: string): string;
|
|
41
|
+
export declare function parseSummaryNodeId(id: string): {
|
|
42
|
+
dimension: string;
|
|
43
|
+
artifactId: string;
|
|
44
|
+
} | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Isolated, stateless read-only search over .pbot/history/raw/ and .pbot/history/summaries/<dimension>/.
|
|
47
|
+
* Uses ripgrep (with automatic Node fallback) to pre-filter candidate JSON files before parsing.
|
|
48
|
+
*/
|
|
49
|
+
export declare function searchWorkspaceHistory(root: string, query?: HistoryQuery): HistorySearchResult;
|
|
50
|
+
/** O(1) read of a session root, summary artifact, or individual raw exchange. */
|
|
51
|
+
export declare function readWorkspaceHistory(root: string, id: string, offset?: number, limit?: number, childrenOffset?: number, host?: HistoryHost): HistoryReadNode;
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { requireThat } from "../../common/validation.js";
|
|
3
|
+
import { HistoryFiles, historyBucketFromTimestamp, rawHistoryBucket, unknownHistoryBucket, } from "../../storage/history-files.js";
|
|
4
|
+
import { readWorkspaceHistoryLayoutSync } from "../workspace/workspace-config.js";
|
|
5
|
+
import { orderedRawExchanges, rawSessionSpan, rawSessionTitle } from "./history-index.js";
|
|
6
|
+
const summaryIdPrefix = "summary:";
|
|
7
|
+
const page = (values, offset, limit) => ({
|
|
8
|
+
values: values.slice(offset, offset + limit),
|
|
9
|
+
nextOffset: offset + limit < values.length ? offset + limit : null,
|
|
10
|
+
});
|
|
11
|
+
const previewText = (text) => text.replace(/\s+/g, " ").trim().slice(0, 100);
|
|
12
|
+
export function summaryNodeId(dimension, artifactId) {
|
|
13
|
+
return `${summaryIdPrefix}${dimension}:${artifactId}`;
|
|
14
|
+
}
|
|
15
|
+
export function parseSummaryNodeId(id) {
|
|
16
|
+
if (!id.startsWith(summaryIdPrefix))
|
|
17
|
+
return undefined;
|
|
18
|
+
const rest = id.slice(summaryIdPrefix.length);
|
|
19
|
+
const sep = rest.indexOf(":");
|
|
20
|
+
if (sep <= 0 || sep === rest.length - 1)
|
|
21
|
+
return undefined;
|
|
22
|
+
return { dimension: rest.slice(0, sep), artifactId: rest.slice(sep + 1) };
|
|
23
|
+
}
|
|
24
|
+
function parseExchangeNodeId(id) {
|
|
25
|
+
const index = id.indexOf(":0:");
|
|
26
|
+
if (index <= 0)
|
|
27
|
+
return undefined;
|
|
28
|
+
return { rawId: id.slice(0, index) };
|
|
29
|
+
}
|
|
30
|
+
function sessionBrief(raw, summaryPreview) {
|
|
31
|
+
const ordered = orderedRawExchanges(raw);
|
|
32
|
+
const span = rawSessionSpan(ordered);
|
|
33
|
+
const name = rawSessionTitle(raw);
|
|
34
|
+
return {
|
|
35
|
+
host: raw.host,
|
|
36
|
+
id: raw.id,
|
|
37
|
+
kind: "session",
|
|
38
|
+
sessionId: raw.sessionId,
|
|
39
|
+
name,
|
|
40
|
+
preview: summaryPreview ? previewText(summaryPreview) : previewText(name || ordered[0]?.text || ""),
|
|
41
|
+
from: span.from,
|
|
42
|
+
to: span.to,
|
|
43
|
+
ready: true,
|
|
44
|
+
completed: span.completed,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function exchangeBrief(raw, exchange, title) {
|
|
48
|
+
return {
|
|
49
|
+
host: raw.host,
|
|
50
|
+
id: exchange.id,
|
|
51
|
+
kind: "exchange",
|
|
52
|
+
sessionId: raw.sessionId,
|
|
53
|
+
name: title,
|
|
54
|
+
preview: previewText(exchange.text),
|
|
55
|
+
from: exchange.from,
|
|
56
|
+
to: exchange.to,
|
|
57
|
+
ready: true,
|
|
58
|
+
completed: exchange.completed,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function summaryBrief(artifact) {
|
|
62
|
+
return {
|
|
63
|
+
host: artifact.host ?? "workspace",
|
|
64
|
+
id: summaryNodeId(artifact.dimension, artifact.id),
|
|
65
|
+
kind: "summary",
|
|
66
|
+
dimension: artifact.dimension,
|
|
67
|
+
sessionId: artifact.sessionId ?? artifact.id,
|
|
68
|
+
name: artifact.title,
|
|
69
|
+
preview: previewText(artifact.text),
|
|
70
|
+
from: artifact.from,
|
|
71
|
+
to: artifact.to,
|
|
72
|
+
ready: true,
|
|
73
|
+
completed: true,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function sliceSnippet(text, needle) {
|
|
77
|
+
if (!needle)
|
|
78
|
+
return text.slice(0, 400);
|
|
79
|
+
const start = Math.max(0, text.toLowerCase().indexOf(needle) - 80);
|
|
80
|
+
return text.slice(start, start + 400);
|
|
81
|
+
}
|
|
82
|
+
function inTimeRange(from, to, after, before) {
|
|
83
|
+
const fromMs = from ? Date.parse(from) : NaN;
|
|
84
|
+
const toMs = to ? Date.parse(to) : NaN;
|
|
85
|
+
if (after !== -Infinity && !Number.isNaN(toMs) && toMs < after)
|
|
86
|
+
return false;
|
|
87
|
+
if (before !== Infinity && !Number.isNaN(fromMs) && fromMs > before)
|
|
88
|
+
return false;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Isolated, stateless read-only search over .pbot/history/raw/ and .pbot/history/summaries/<dimension>/.
|
|
93
|
+
* Uses ripgrep (with automatic Node fallback) to pre-filter candidate JSON files before parsing.
|
|
94
|
+
*/
|
|
95
|
+
export function searchWorkspaceHistory(root, query = {}) {
|
|
96
|
+
const { host, sessionId, dimension, from, to, offset = 0, limit = 20 } = query;
|
|
97
|
+
const needle = (query.query ?? "").trim().toLowerCase();
|
|
98
|
+
const after = from === undefined ? -Infinity : Date.parse(from);
|
|
99
|
+
const before = to === undefined ? Infinity : Date.parse(to);
|
|
100
|
+
requireThat(!Number.isNaN(after) && !Number.isNaN(before) && after <= before, "时间范围无效,请使用 ISO 日期或时间。");
|
|
101
|
+
const layout = readWorkspaceHistoryLayoutSync(root);
|
|
102
|
+
const files = new HistoryFiles(join(root, ".pbot/history"), "pi", layout);
|
|
103
|
+
const warnings = [];
|
|
104
|
+
if (layout === "monthly") {
|
|
105
|
+
const flatNotice = files.detectFlatFiles().message;
|
|
106
|
+
if (flatNotice)
|
|
107
|
+
warnings.push(flatNotice);
|
|
108
|
+
}
|
|
109
|
+
const matches = [];
|
|
110
|
+
if (!needle) {
|
|
111
|
+
// Empty query: list sessions (default / dimension="raw") or list summaries of a specific dimension.
|
|
112
|
+
if (dimension && dimension !== "raw") {
|
|
113
|
+
const summaries = files.listSummaries(dimension);
|
|
114
|
+
warnings.push(...files.warnings);
|
|
115
|
+
for (const artifact of summaries) {
|
|
116
|
+
if (host && artifact.host !== host)
|
|
117
|
+
continue;
|
|
118
|
+
if (sessionId && artifact.sessionId !== sessionId)
|
|
119
|
+
continue;
|
|
120
|
+
if (!inTimeRange(artifact.from, artifact.to, after, before))
|
|
121
|
+
continue;
|
|
122
|
+
const brief = summaryBrief(artifact);
|
|
123
|
+
matches.push({
|
|
124
|
+
...brief,
|
|
125
|
+
parents: artifact.dimension === "monthly" ? [] : artifact.source.rawIds,
|
|
126
|
+
snippet: sliceSnippet(artifact.text, ""),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const raws = files.listRaw(host);
|
|
132
|
+
warnings.push(...files.warnings);
|
|
133
|
+
const sessionSummaries = new Map(files.listSummaries("session").map(item => [item.id, item]));
|
|
134
|
+
warnings.push(...files.warnings);
|
|
135
|
+
for (const raw of raws) {
|
|
136
|
+
if (sessionId && raw.sessionId !== sessionId)
|
|
137
|
+
continue;
|
|
138
|
+
const summary = sessionSummaries.get(raw.id);
|
|
139
|
+
const brief = sessionBrief(raw, summary?.text);
|
|
140
|
+
if (!inTimeRange(brief.from, brief.to, after, before))
|
|
141
|
+
continue;
|
|
142
|
+
matches.push({
|
|
143
|
+
...brief,
|
|
144
|
+
parents: [],
|
|
145
|
+
snippet: summary ? sliceSnippet(summary.text, "") : brief.preview,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// Keyword query: pre-filter candidate files via rg/fallback, then parse and match exact fields.
|
|
152
|
+
const searchRaw = !dimension || dimension === "raw";
|
|
153
|
+
const summaryDimensions = dimension === "raw"
|
|
154
|
+
? []
|
|
155
|
+
: dimension
|
|
156
|
+
? [dimension]
|
|
157
|
+
: files.listDimensions();
|
|
158
|
+
if (searchRaw) {
|
|
159
|
+
const candidateRaws = files.searchCandidateRaw(needle);
|
|
160
|
+
warnings.push(...files.warnings);
|
|
161
|
+
for (const raw of candidateRaws) {
|
|
162
|
+
if (host && raw.host !== host)
|
|
163
|
+
continue;
|
|
164
|
+
if (sessionId && raw.sessionId !== sessionId)
|
|
165
|
+
continue;
|
|
166
|
+
const ordered = orderedRawExchanges(raw);
|
|
167
|
+
const title = rawSessionTitle(raw);
|
|
168
|
+
for (const item of ordered) {
|
|
169
|
+
if (!item.text.toLowerCase().includes(needle))
|
|
170
|
+
continue;
|
|
171
|
+
if (!inTimeRange(item.from, item.to, after, before))
|
|
172
|
+
continue;
|
|
173
|
+
matches.push({
|
|
174
|
+
...exchangeBrief(raw, item, title),
|
|
175
|
+
parents: [raw.id],
|
|
176
|
+
snippet: sliceSnippet(item.text, needle),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const dim of summaryDimensions) {
|
|
182
|
+
const candidateSummaries = files.searchCandidateSummaries(dim, needle);
|
|
183
|
+
warnings.push(...files.warnings);
|
|
184
|
+
for (const artifact of candidateSummaries) {
|
|
185
|
+
if (host && artifact.host !== host)
|
|
186
|
+
continue;
|
|
187
|
+
if (sessionId && artifact.sessionId !== sessionId)
|
|
188
|
+
continue;
|
|
189
|
+
const searchable = `${artifact.title}\n${artifact.text}`;
|
|
190
|
+
if (!searchable.toLowerCase().includes(needle))
|
|
191
|
+
continue;
|
|
192
|
+
if (!inTimeRange(artifact.from, artifact.to, after, before))
|
|
193
|
+
continue;
|
|
194
|
+
matches.push({
|
|
195
|
+
...summaryBrief(artifact),
|
|
196
|
+
parents: artifact.dimension === "monthly" ? [] : artifact.source.rawIds,
|
|
197
|
+
snippet: sliceSnippet(artifact.text, needle),
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
matches.sort((a, b) => b.to.localeCompare(a.to) || a.id.localeCompare(b.id));
|
|
203
|
+
const paged = page(matches, offset, limit);
|
|
204
|
+
return {
|
|
205
|
+
matches: paged.values,
|
|
206
|
+
total: matches.length,
|
|
207
|
+
nextOffset: paged.nextOffset,
|
|
208
|
+
warnings: [...new Set(warnings)],
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function sliceText(text, offset, limit) {
|
|
212
|
+
requireThat(offset <= text.length, "读取位置超出正文。");
|
|
213
|
+
let start = offset;
|
|
214
|
+
if (start && /[\uDC00-\uDFFF]/.test(text[start] ?? ""))
|
|
215
|
+
start--;
|
|
216
|
+
let end = Math.min(text.length, start + limit);
|
|
217
|
+
if (end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1] ?? ""))
|
|
218
|
+
end += end - start === 1 ? 1 : -1;
|
|
219
|
+
return {
|
|
220
|
+
text: text.slice(start, end),
|
|
221
|
+
totalLength: text.length,
|
|
222
|
+
nextOffset: end < text.length ? end : null,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function findMonthlyParent(files, bucket, rawId) {
|
|
226
|
+
if (files.layout !== "monthly" || bucket === unknownHistoryBucket)
|
|
227
|
+
return undefined;
|
|
228
|
+
const monthly = files.readSummary("monthly", bucket.replace("/", "-"));
|
|
229
|
+
if (!monthly || !monthly.source.rawIds.includes(rawId))
|
|
230
|
+
return undefined;
|
|
231
|
+
return summaryBrief(monthly);
|
|
232
|
+
}
|
|
233
|
+
/** O(1) read of a session root, summary artifact, or individual raw exchange. */
|
|
234
|
+
export function readWorkspaceHistory(root, id, offset = 0, limit = 12000, childrenOffset = 0, host) {
|
|
235
|
+
const layout = readWorkspaceHistoryLayoutSync(root);
|
|
236
|
+
const files = new HistoryFiles(join(root, ".pbot/history"), "pi", layout);
|
|
237
|
+
const baseWarnings = [];
|
|
238
|
+
if (layout === "monthly") {
|
|
239
|
+
const flatNotice = files.detectFlatFiles().message;
|
|
240
|
+
if (flatNotice)
|
|
241
|
+
baseWarnings.push(flatNotice);
|
|
242
|
+
}
|
|
243
|
+
// 1. Summary artifact: summary:<dimension>:<artifactId>
|
|
244
|
+
const parsedSummary = parseSummaryNodeId(id);
|
|
245
|
+
if (parsedSummary) {
|
|
246
|
+
const artifact = files.readSummary(parsedSummary.dimension, parsedSummary.artifactId);
|
|
247
|
+
requireThat(artifact && (!host || artifact.host === host), "历史节点不存在,请重新搜索。");
|
|
248
|
+
const parents = [];
|
|
249
|
+
const children = [];
|
|
250
|
+
if (artifact.dimension === "monthly") {
|
|
251
|
+
// Monthly summary drills down to per-session summaries (falling back to raw session roots).
|
|
252
|
+
for (const rawId of artifact.source.rawIds) {
|
|
253
|
+
const sessionSummary = files.readSummary("session", rawId);
|
|
254
|
+
if (sessionSummary) {
|
|
255
|
+
children.push(summaryBrief(sessionSummary));
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const raw = files.readRaw(rawId);
|
|
259
|
+
if (raw)
|
|
260
|
+
children.push(sessionBrief(raw));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
const wantedExchanges = new Set(artifact.source.exchangeIds);
|
|
265
|
+
for (const rawId of artifact.source.rawIds) {
|
|
266
|
+
const raw = files.readRaw(rawId);
|
|
267
|
+
if (!raw)
|
|
268
|
+
continue;
|
|
269
|
+
parents.push(sessionBrief(raw, artifact.dimension === "session" ? artifact.text : undefined));
|
|
270
|
+
const ordered = orderedRawExchanges(raw);
|
|
271
|
+
const title = rawSessionTitle(raw);
|
|
272
|
+
for (const item of ordered) {
|
|
273
|
+
if (!wantedExchanges.size || wantedExchanges.has(item.id)) {
|
|
274
|
+
children.push(exchangeBrief(raw, item, title));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (artifact.dimension === "session") {
|
|
279
|
+
const bucket = historyBucketFromTimestamp(artifact.from || artifact.to);
|
|
280
|
+
const monthlyParent = findMonthlyParent(files, bucket, artifact.id);
|
|
281
|
+
if (monthlyParent)
|
|
282
|
+
parents.push(monthlyParent);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const pagedChildren = page(children, childrenOffset, 20);
|
|
286
|
+
const sliced = sliceText(artifact.text, offset, limit);
|
|
287
|
+
return {
|
|
288
|
+
...summaryBrief(artifact),
|
|
289
|
+
...sliced,
|
|
290
|
+
parents,
|
|
291
|
+
children: pagedChildren.values,
|
|
292
|
+
nextChildrenOffset: pagedChildren.nextOffset,
|
|
293
|
+
source: {
|
|
294
|
+
host: artifact.host ?? "workspace",
|
|
295
|
+
sessionId: artifact.sessionId ?? artifact.id,
|
|
296
|
+
entryIds: artifact.source.exchangeIds,
|
|
297
|
+
dimension: artifact.dimension,
|
|
298
|
+
},
|
|
299
|
+
warnings: [...new Set([...baseWarnings, ...files.warnings])],
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
// 2. Raw exchange: <rawId>:0:<end>
|
|
303
|
+
const parsedExchange = parseExchangeNodeId(id);
|
|
304
|
+
if (parsedExchange) {
|
|
305
|
+
const raw = files.readRaw(parsedExchange.rawId);
|
|
306
|
+
requireThat(raw && (!host || raw.host === host), "历史节点不存在,请重新搜索。");
|
|
307
|
+
const exchange = raw.exchanges.find(item => item.id === id);
|
|
308
|
+
requireThat(exchange, "历史节点不存在,请重新搜索。");
|
|
309
|
+
const title = rawSessionTitle(raw);
|
|
310
|
+
const parents = [sessionBrief(raw)];
|
|
311
|
+
for (const dim of files.listDimensions()) {
|
|
312
|
+
if (dim === "monthly")
|
|
313
|
+
continue;
|
|
314
|
+
const artifact = files.readSummary(dim, raw.id);
|
|
315
|
+
if (artifact)
|
|
316
|
+
parents.push(summaryBrief(artifact));
|
|
317
|
+
}
|
|
318
|
+
const sliced = sliceText(exchange.text, offset, limit);
|
|
319
|
+
return {
|
|
320
|
+
...exchangeBrief(raw, exchange, title),
|
|
321
|
+
...sliced,
|
|
322
|
+
parents,
|
|
323
|
+
children: [],
|
|
324
|
+
nextChildrenOffset: null,
|
|
325
|
+
source: {
|
|
326
|
+
host: raw.host,
|
|
327
|
+
sessionId: raw.sessionId,
|
|
328
|
+
entryIds: exchange.entryIds,
|
|
329
|
+
},
|
|
330
|
+
warnings: [...new Set([...baseWarnings, ...files.warnings])],
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
// 3. Session root: <rawId>
|
|
334
|
+
const raw = files.readRaw(id);
|
|
335
|
+
requireThat(raw && (!host || raw.host === host), "历史节点不存在,请重新搜索。");
|
|
336
|
+
const ordered = orderedRawExchanges(raw);
|
|
337
|
+
const title = rawSessionTitle(raw);
|
|
338
|
+
const summaryChildren = [];
|
|
339
|
+
let sessionSummaryText;
|
|
340
|
+
for (const dim of files.listDimensions()) {
|
|
341
|
+
if (dim === "monthly")
|
|
342
|
+
continue;
|
|
343
|
+
const artifact = files.readSummary(dim, raw.id);
|
|
344
|
+
if (!artifact)
|
|
345
|
+
continue;
|
|
346
|
+
if (dim === "session")
|
|
347
|
+
sessionSummaryText = artifact.text;
|
|
348
|
+
summaryChildren.push(summaryBrief(artifact));
|
|
349
|
+
}
|
|
350
|
+
const monthlyParent = findMonthlyParent(files, rawHistoryBucket(raw), raw.id);
|
|
351
|
+
const allChildren = [
|
|
352
|
+
...summaryChildren,
|
|
353
|
+
...ordered.map(item => exchangeBrief(raw, item, title)),
|
|
354
|
+
];
|
|
355
|
+
const pagedChildren = page(allChildren, childrenOffset, 20);
|
|
356
|
+
const body = sessionSummaryText ? (title ? `${title}\n\n${sessionSummaryText}` : sessionSummaryText) : title;
|
|
357
|
+
const sliced = sliceText(body, offset, limit);
|
|
358
|
+
return {
|
|
359
|
+
...sessionBrief(raw, sessionSummaryText),
|
|
360
|
+
...sliced,
|
|
361
|
+
parents: monthlyParent ? [monthlyParent] : [],
|
|
362
|
+
children: pagedChildren.values,
|
|
363
|
+
nextChildrenOffset: pagedChildren.nextOffset,
|
|
364
|
+
source: {
|
|
365
|
+
host: raw.host,
|
|
366
|
+
sessionId: raw.sessionId,
|
|
367
|
+
entryIds: ordered.flatMap(item => item.entryIds),
|
|
368
|
+
},
|
|
369
|
+
warnings: [...new Set([...baseWarnings, ...files.warnings])],
|
|
370
|
+
};
|
|
371
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type ParsedHistoryCommand = {
|
|
2
|
+
kind: "status";
|
|
3
|
+
} | {
|
|
4
|
+
kind: "list";
|
|
5
|
+
} | {
|
|
6
|
+
kind: "migrate";
|
|
7
|
+
dryRun: boolean;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "summary";
|
|
10
|
+
dimensions?: string[];
|
|
11
|
+
force: boolean;
|
|
12
|
+
backend?: string;
|
|
13
|
+
model?: string;
|
|
14
|
+
thinking?: string;
|
|
15
|
+
} | {
|
|
16
|
+
kind: "query";
|
|
17
|
+
input: Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
export declare const historyCommandUsage = "\u7528\u6CD5\uFF1A/pbot history summary [\u7EF4\u5EA6...] [--force] [--backend <acp>] [--model <model>] [--thinking <level>] | migrate [--dry-run] | status | list | {\"action\":\"search\"|\"read\",...}";
|
|
20
|
+
export declare function parseHistoryCommand(raw: string): ParsedHistoryCommand;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { object } from "../../common/json.js";
|
|
2
|
+
import { requireThat } from "../../common/validation.js";
|
|
3
|
+
export const historyCommandUsage = '用法:/pbot history summary [维度...] [--force] [--backend <acp>] [--model <model>] [--thinking <level>] | migrate [--dry-run] | status | list | {"action":"search"|"read",...}';
|
|
4
|
+
export function parseHistoryCommand(raw) {
|
|
5
|
+
const trimmed = raw.trim();
|
|
6
|
+
if (!trimmed || trimmed === "status")
|
|
7
|
+
return { kind: "status" };
|
|
8
|
+
if (trimmed === "list")
|
|
9
|
+
return { kind: "list" };
|
|
10
|
+
if (trimmed.startsWith("{")) {
|
|
11
|
+
const parsed = JSON.parse(trimmed);
|
|
12
|
+
requireThat(object(parsed), "历史查询参数必须是 JSON 对象。");
|
|
13
|
+
return { kind: "query", input: parsed };
|
|
14
|
+
}
|
|
15
|
+
const tokens = trimmed.split(/\s+/);
|
|
16
|
+
const sub = tokens[0];
|
|
17
|
+
if (sub === "migrate") {
|
|
18
|
+
let dryRun = false;
|
|
19
|
+
for (const token of tokens.slice(1)) {
|
|
20
|
+
if (token === "--dry-run")
|
|
21
|
+
dryRun = true;
|
|
22
|
+
else
|
|
23
|
+
throw new Error(`未知参数:${token}\n${historyCommandUsage}`);
|
|
24
|
+
}
|
|
25
|
+
return { kind: "migrate", dryRun };
|
|
26
|
+
}
|
|
27
|
+
if (sub === "summary") {
|
|
28
|
+
let force = false;
|
|
29
|
+
let backend;
|
|
30
|
+
let model;
|
|
31
|
+
let thinking;
|
|
32
|
+
const dimensions = [];
|
|
33
|
+
const rest = tokens.slice(1);
|
|
34
|
+
for (let i = 0; i < rest.length; i++) {
|
|
35
|
+
const token = rest[i];
|
|
36
|
+
if (token === "--force" || token === "-f") {
|
|
37
|
+
force = true;
|
|
38
|
+
}
|
|
39
|
+
else if (token === "--backend") {
|
|
40
|
+
backend = rest[++i];
|
|
41
|
+
requireThat(backend && !backend.startsWith("-"), "--backend 需要指定 ACP 后端名称(如 pi、dsh、claude)。");
|
|
42
|
+
}
|
|
43
|
+
else if (token === "--model") {
|
|
44
|
+
model = rest[++i];
|
|
45
|
+
requireThat(model && !model.startsWith("-"), "--model 需要指定模型标识。");
|
|
46
|
+
}
|
|
47
|
+
else if (token === "--thinking") {
|
|
48
|
+
thinking = rest[++i];
|
|
49
|
+
requireThat(thinking && !thinking.startsWith("-"), "--thinking 需要指定思考深度。");
|
|
50
|
+
}
|
|
51
|
+
else if (token.startsWith("-")) {
|
|
52
|
+
throw new Error(`未知参数:${token}\n${historyCommandUsage}`);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
dimensions.push(token);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
kind: "summary",
|
|
60
|
+
...(dimensions.length ? { dimensions } : {}),
|
|
61
|
+
force,
|
|
62
|
+
...(backend ? { backend } : {}),
|
|
63
|
+
...(model ? { model } : {}),
|
|
64
|
+
...(thinking ? { thinking } : {}),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
throw new Error(historyCommandUsage);
|
|
68
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SummaryDimension } from "../../../model/history.js";
|
|
2
|
+
/**
|
|
3
|
+
* Monthly cross-session summary dimension (enabled only under history.layout: "monthly").
|
|
4
|
+
* Aggregates per-session summaries grouped by UTC YYYY/MM into one monthly artifact per month,
|
|
5
|
+
* preserving source.rawIds for pbot_history drill-down to session summaries and raw exchanges.
|
|
6
|
+
*/
|
|
7
|
+
export declare const monthlySummaryDimension: SummaryDimension;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { resourceRoot } from "../../../common/paths.js";
|
|
5
|
+
import { requireThat } from "../../../common/validation.js";
|
|
6
|
+
import { historyBucketFromTimestamp, unknownHistoryBucket } from "../../../storage/history-files.js";
|
|
7
|
+
let cachedPrompt;
|
|
8
|
+
function systemPrompt() {
|
|
9
|
+
cachedPrompt ??= readFileSync(join(resourceRoot, "prompts/history-monthly-summary.md"), "utf8");
|
|
10
|
+
return cachedPrompt;
|
|
11
|
+
}
|
|
12
|
+
function fingerprintSessionSummaries(summaries) {
|
|
13
|
+
const hash = createHash("sha256");
|
|
14
|
+
for (const item of summaries) {
|
|
15
|
+
hash.update(JSON.stringify([item.id, item.title, item.from, item.to, item.source.fingerprint, item.text]));
|
|
16
|
+
hash.update("\n");
|
|
17
|
+
}
|
|
18
|
+
return hash.digest("hex");
|
|
19
|
+
}
|
|
20
|
+
function monthSpan(summaries) {
|
|
21
|
+
const dates = summaries.flatMap(item => [item.from, item.to]).filter(Boolean).sort();
|
|
22
|
+
return {
|
|
23
|
+
from: dates[0] ?? "",
|
|
24
|
+
to: dates.at(-1) ?? "",
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function formatSessionSummaries(summaries) {
|
|
28
|
+
return summaries
|
|
29
|
+
.map(item => {
|
|
30
|
+
const header = [`会话 ID:${item.id}`];
|
|
31
|
+
if (item.title)
|
|
32
|
+
header.push(`标题:${item.title}`);
|
|
33
|
+
if (item.host)
|
|
34
|
+
header.push(`来源:${item.host}`);
|
|
35
|
+
header.push(`时间:${item.from} — ${item.to}`);
|
|
36
|
+
return `[${header.join(" | ")}]\n${item.text}`;
|
|
37
|
+
})
|
|
38
|
+
.join("\n\n");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Monthly cross-session summary dimension (enabled only under history.layout: "monthly").
|
|
42
|
+
* Aggregates per-session summaries grouped by UTC YYYY/MM into one monthly artifact per month,
|
|
43
|
+
* preserving source.rawIds for pbot_history drill-down to session summaries and raw exchanges.
|
|
44
|
+
*/
|
|
45
|
+
export const monthlySummaryDimension = {
|
|
46
|
+
id: "monthly",
|
|
47
|
+
title: "月度历史汇总",
|
|
48
|
+
description: "按自然月(UTC YYYY/MM)归纳当月单会话摘要,提炼月度主题、关键决定与产出,支持逐层下钻。",
|
|
49
|
+
requiresMonthlyLayout: true,
|
|
50
|
+
plan(_raws, existing, options = {}) {
|
|
51
|
+
const force = Boolean(options.force);
|
|
52
|
+
const sessionMap = options.summariesByDimension?.get("session");
|
|
53
|
+
if (!sessionMap || sessionMap.size === 0)
|
|
54
|
+
return { tasks: [], skipped: 0 };
|
|
55
|
+
const byMonth = new Map();
|
|
56
|
+
for (const artifact of sessionMap.values()) {
|
|
57
|
+
const bucket = historyBucketFromTimestamp(artifact.from || artifact.to);
|
|
58
|
+
if (bucket === unknownHistoryBucket)
|
|
59
|
+
continue;
|
|
60
|
+
const monthId = bucket.replace("/", "-");
|
|
61
|
+
const list = byMonth.get(monthId);
|
|
62
|
+
if (list)
|
|
63
|
+
list.push(artifact);
|
|
64
|
+
else
|
|
65
|
+
byMonth.set(monthId, [artifact]);
|
|
66
|
+
}
|
|
67
|
+
const tasks = [];
|
|
68
|
+
let skipped = 0;
|
|
69
|
+
for (const [monthId, summaries] of byMonth) {
|
|
70
|
+
const ordered = [...summaries].sort((a, b) => a.from.localeCompare(b.from) || a.id.localeCompare(b.id));
|
|
71
|
+
const fingerprint = fingerprintSessionSummaries(ordered);
|
|
72
|
+
const prev = existing.get(monthId);
|
|
73
|
+
if (!force && prev && prev.source.fingerprint === fingerprint) {
|
|
74
|
+
skipped++;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const rawIds = [...new Set(ordered.flatMap(item => item.source.rawIds))];
|
|
78
|
+
if (!rawIds.length)
|
|
79
|
+
continue;
|
|
80
|
+
const span = monthSpan(ordered);
|
|
81
|
+
const [year, month] = monthId.split("-");
|
|
82
|
+
tasks.push({
|
|
83
|
+
id: monthId,
|
|
84
|
+
dimension: this.id,
|
|
85
|
+
mode: prev ? "rebuild" : "create",
|
|
86
|
+
title: `${year}年${month}月历史汇总`,
|
|
87
|
+
from: span.from,
|
|
88
|
+
to: span.to,
|
|
89
|
+
source: { rawIds, exchangeIds: [], fingerprint },
|
|
90
|
+
exchanges: [],
|
|
91
|
+
sourceSummaries: ordered,
|
|
92
|
+
...(prev ? { existing: prev } : {}),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
tasks.sort((a, b) => b.id.localeCompare(a.id));
|
|
96
|
+
return { tasks, skipped };
|
|
97
|
+
},
|
|
98
|
+
buildRequest(task) {
|
|
99
|
+
const system = systemPrompt();
|
|
100
|
+
const summaries = task.sourceSummaries ?? [];
|
|
101
|
+
const prompt = [
|
|
102
|
+
`月份:${task.id}(共 ${summaries.length} 个会话摘要)`,
|
|
103
|
+
formatSessionSummaries(summaries),
|
|
104
|
+
].join("\n\n");
|
|
105
|
+
return { system, prompt, maxTokens: 3072 };
|
|
106
|
+
},
|
|
107
|
+
finalize(task, output, now) {
|
|
108
|
+
const text = output.trim();
|
|
109
|
+
requireThat(text.length > 0, "月度历史汇总为空。");
|
|
110
|
+
return {
|
|
111
|
+
id: task.id,
|
|
112
|
+
dimension: this.id,
|
|
113
|
+
title: task.title,
|
|
114
|
+
text,
|
|
115
|
+
from: task.from,
|
|
116
|
+
to: task.to,
|
|
117
|
+
updatedAt: now,
|
|
118
|
+
source: task.source,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SummaryDimension } from "../../../model/history.js";
|
|
2
|
+
/**
|
|
3
|
+
* Default per-session task summary dimension.
|
|
4
|
+
* Produces one concise summary per raw session archive, supporting exact deduplication
|
|
5
|
+
* (0 tokens on unchanged sessions) and rolling incremental updates when new turns are appended.
|
|
6
|
+
*/
|
|
7
|
+
export declare const sessionSummaryDimension: SummaryDimension;
|