pi-ate-memory 1.0.0 → 1.1.0
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/README.en.md +8 -0
- package/README.md +8 -0
- package/index.ts +281 -198
- package/lib/conflict-detect.ts +44 -38
- package/lib/memory-hook.ts +47 -37
- package/lib/memory-inject.ts +74 -72
- package/lib/memory-parser.ts +100 -95
- package/lib/types.ts +24 -32
- package/lib/writer.ts +68 -69
- package/package.json +60 -60
- package/prompts/update-description.md +1 -1
- package/tsconfig.json +10 -10
package/lib/memory-hook.ts
CHANGED
|
@@ -1,37 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory 注入 — before_agent_start hook 注册
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as fs from "node:fs";
|
|
6
|
-
import * as path from "node:path";
|
|
7
|
-
import type {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Memory 注入 — before_agent_start hook 注册
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import type {
|
|
8
|
+
BeforeAgentStartEvent,
|
|
9
|
+
BeforeAgentStartEventResult,
|
|
10
|
+
ExtensionAPI,
|
|
11
|
+
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { resolveMemoryInjection } from "./memory-inject";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 注册 before_agent_start hook:
|
|
16
|
+
* 1. 注入 memory-prompt.md 说明文本
|
|
17
|
+
* 2. 注入 L1/L2 MEMORY.md 索引内容
|
|
18
|
+
*/
|
|
19
|
+
export function registerMemoryHook(pi: ExtensionAPI): void {
|
|
20
|
+
const memoryPromptPath = path.join(__dirname, "..", "memory-prompt.md");
|
|
21
|
+
let memoryPrompt = "";
|
|
22
|
+
try {
|
|
23
|
+
if (fs.existsSync(memoryPromptPath)) {
|
|
24
|
+
memoryPrompt = fs.readFileSync(memoryPromptPath, "utf-8").trim();
|
|
25
|
+
}
|
|
26
|
+
} catch {
|
|
27
|
+
// 文件不存在或权限异常 → 跳过注入
|
|
28
|
+
/* ignore */
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!memoryPrompt) return;
|
|
32
|
+
|
|
33
|
+
pi.on(
|
|
34
|
+
"before_agent_start",
|
|
35
|
+
async (
|
|
36
|
+
event: BeforeAgentStartEvent,
|
|
37
|
+
): Promise<BeforeAgentStartEventResult> => {
|
|
38
|
+
const cwd = event.systemPromptOptions?.cwd || process.cwd();
|
|
39
|
+
const injection = resolveMemoryInjection(cwd);
|
|
40
|
+
const parts = [memoryPrompt];
|
|
41
|
+
if (injection) parts.push(injection);
|
|
42
|
+
return {
|
|
43
|
+
systemPrompt: `${event.systemPrompt}\n\n${parts.join("\n\n")}`,
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
}
|
package/lib/memory-inject.ts
CHANGED
|
@@ -1,72 +1,74 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory 注入 — 读取 MEMORY.md 索引并拼接为 systemPrompt 注入文本
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as fs from "node:fs";
|
|
6
|
-
import * as path from "node:path";
|
|
7
|
-
import { AGENT_DIR } from "./types";
|
|
8
|
-
|
|
9
|
-
// ── 注入截断常量 ─────────────────────────────────────────
|
|
10
|
-
|
|
11
|
-
/** 注入索引的最大行数 */
|
|
12
|
-
const MAX_MEMORY_LINES = 200;
|
|
13
|
-
|
|
14
|
-
/** 注入索引的最大字节数 */
|
|
15
|
-
const MAX_MEMORY_BYTES = 25_000;
|
|
16
|
-
|
|
17
|
-
// ── 工具函数 ─────────────────────────────────────────────
|
|
18
|
-
|
|
19
|
-
/** 读取文件,成功返回 trim 内容,否则空字符串 */
|
|
20
|
-
export function readMemoryFile(filePath: string): string {
|
|
21
|
-
try {
|
|
22
|
-
if (fs.existsSync(filePath))
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (entries.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Memory 注入 — 读取 MEMORY.md 索引并拼接为 systemPrompt 注入文本
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { AGENT_DIR } from "./types";
|
|
8
|
+
|
|
9
|
+
// ── 注入截断常量 ─────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
/** 注入索引的最大行数 */
|
|
12
|
+
const MAX_MEMORY_LINES = 200;
|
|
13
|
+
|
|
14
|
+
/** 注入索引的最大字节数 */
|
|
15
|
+
const MAX_MEMORY_BYTES = 25_000;
|
|
16
|
+
|
|
17
|
+
// ── 工具函数 ─────────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
/** 读取文件,成功返回 trim 内容,否则空字符串 */
|
|
20
|
+
export function readMemoryFile(filePath: string): string {
|
|
21
|
+
try {
|
|
22
|
+
if (fs.existsSync(filePath))
|
|
23
|
+
return fs.readFileSync(filePath, "utf-8").trim();
|
|
24
|
+
} catch {
|
|
25
|
+
// 文件不存在或权限异常 → 降级为空字符串
|
|
26
|
+
}
|
|
27
|
+
return "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 截断超长记忆索引 */
|
|
31
|
+
export function truncateMemory(raw: string): string {
|
|
32
|
+
const lines = raw.split("\n");
|
|
33
|
+
let truncated =
|
|
34
|
+
lines.length > MAX_MEMORY_LINES
|
|
35
|
+
? lines.slice(0, MAX_MEMORY_LINES).join("\n")
|
|
36
|
+
: raw;
|
|
37
|
+
if (truncated.length > MAX_MEMORY_BYTES) {
|
|
38
|
+
const cutAt = truncated.lastIndexOf("\n", MAX_MEMORY_BYTES);
|
|
39
|
+
truncated = truncated.slice(0, cutAt > 0 ? cutAt : MAX_MEMORY_BYTES);
|
|
40
|
+
}
|
|
41
|
+
if (truncated.length < raw.length) {
|
|
42
|
+
truncated += "\n\n> ⚠️ MEMORY.md 索引已截断。";
|
|
43
|
+
}
|
|
44
|
+
return truncated;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 拼接记忆注入文本。
|
|
49
|
+
* @returns 完整的注入文本,或 null(无记忆时)
|
|
50
|
+
*/
|
|
51
|
+
export function buildMemoryPrompt(
|
|
52
|
+
globalMemory: string,
|
|
53
|
+
projectMemory: string,
|
|
54
|
+
): string | null {
|
|
55
|
+
const entries: string[] = [];
|
|
56
|
+
if (globalMemory) entries.push(globalMemory);
|
|
57
|
+
if (projectMemory) entries.push(projectMemory);
|
|
58
|
+
if (entries.length === 0) return null;
|
|
59
|
+
return `## 记忆\n\n${entries.join("\n\n")}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 读取 L1/L2 的 MEMORY.md 索引,返回注入文本。
|
|
64
|
+
* 供 before_agent_start hook 调用。
|
|
65
|
+
*/
|
|
66
|
+
export function resolveMemoryInjection(cwd: string): string | null {
|
|
67
|
+
const l1Path = path.join(AGENT_DIR, "MEMORY.md");
|
|
68
|
+
const l2Path = path.join(cwd, ".pi", "memory", "MEMORY.md");
|
|
69
|
+
|
|
70
|
+
const globalMemory = truncateMemory(readMemoryFile(l1Path));
|
|
71
|
+
const projectMemory = truncateMemory(readMemoryFile(l2Path));
|
|
72
|
+
|
|
73
|
+
return buildMemoryPrompt(globalMemory, projectMemory);
|
|
74
|
+
}
|
package/lib/memory-parser.ts
CHANGED
|
@@ -1,95 +1,100 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Parser — 共享的记忆文件名解析与目录扫描
|
|
3
|
-
*
|
|
4
|
-
* 文件名格式:topic--kw1,kw2,kw3,kw4,kw5.md
|
|
5
|
-
* -- 分隔 topic 和关键词
|
|
6
|
-
* , 分隔关键词
|
|
7
|
-
*
|
|
8
|
-
* 被 memory 扩展和 smart-context 扩展共同使用。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import * as fs from "node:fs";
|
|
12
|
-
import * as path from "node:path";
|
|
13
|
-
|
|
14
|
-
// ── 类型定义 ─────────────────────────────────────────────
|
|
15
|
-
|
|
16
|
-
export interface MemoryEntry {
|
|
17
|
-
name: string;
|
|
18
|
-
file: string;
|
|
19
|
-
description: string;
|
|
20
|
-
lines: number;
|
|
21
|
-
scope: "L1" | "L2";
|
|
22
|
-
topic: string;
|
|
23
|
-
keywords: string[];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// ── 文件名解析 ───────────────────────────────────────────
|
|
27
|
-
|
|
28
|
-
/** 从文件名解析 topic 和 keywords
|
|
29
|
-
*
|
|
30
|
-
* 格式:topic--kw1,kw2,kw3.md
|
|
31
|
-
* 无 -- 时 topic = basename(兼容旧格式)
|
|
32
|
-
*/
|
|
33
|
-
export function parseFileName(fileName: string): {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Memory Parser — 共享的记忆文件名解析与目录扫描
|
|
3
|
+
*
|
|
4
|
+
* 文件名格式:topic--kw1,kw2,kw3,kw4,kw5.md
|
|
5
|
+
* -- 分隔 topic 和关键词
|
|
6
|
+
* , 分隔关键词
|
|
7
|
+
*
|
|
8
|
+
* 被 memory 扩展和 smart-context 扩展共同使用。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import * as fs from "node:fs";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
|
|
14
|
+
// ── 类型定义 ─────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
export interface MemoryEntry {
|
|
17
|
+
name: string;
|
|
18
|
+
file: string;
|
|
19
|
+
description: string;
|
|
20
|
+
lines: number;
|
|
21
|
+
scope: "L1" | "L2";
|
|
22
|
+
topic: string;
|
|
23
|
+
keywords: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// ── 文件名解析 ───────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
/** 从文件名解析 topic 和 keywords
|
|
29
|
+
*
|
|
30
|
+
* 格式:topic--kw1,kw2,kw3.md
|
|
31
|
+
* 无 -- 时 topic = basename(兼容旧格式)
|
|
32
|
+
*/
|
|
33
|
+
export function parseFileName(fileName: string): {
|
|
34
|
+
topic: string;
|
|
35
|
+
keywords: string[];
|
|
36
|
+
} {
|
|
37
|
+
if (fileName == null) return { topic: "", keywords: [] };
|
|
38
|
+
const base = fileName.replace(/\.md$/, "");
|
|
39
|
+
const idx = base.indexOf("--");
|
|
40
|
+
if (idx === -1) {
|
|
41
|
+
return { topic: base, keywords: [] };
|
|
42
|
+
}
|
|
43
|
+
const topic = base.substring(0, idx);
|
|
44
|
+
const kwStr = base.substring(idx + 2);
|
|
45
|
+
const keywords = kwStr ? kwStr.split(",") : [];
|
|
46
|
+
return { topic, keywords };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 生成文件名:topic--kw1,kw2,...md */
|
|
50
|
+
export function buildFileName(topic: string, keywords: string[]): string {
|
|
51
|
+
const kwPart = keywords.length > 0 ? keywords.join(",") : "";
|
|
52
|
+
const base = kwPart ? `${topic}--${kwPart}` : topic;
|
|
53
|
+
return `${base}.md`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── 目录扫描 ─────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
/** 扫描目录下的记忆文件,从文件名解析 topic + keywords */
|
|
59
|
+
export function scanMemoryDir(dir: string, scope: "L1" | "L2"): MemoryEntry[] {
|
|
60
|
+
const entries: MemoryEntry[] = [];
|
|
61
|
+
if (!fs.existsSync(dir)) return entries;
|
|
62
|
+
|
|
63
|
+
const files = fs
|
|
64
|
+
.readdirSync(dir)
|
|
65
|
+
.filter((f) => f.endsWith(".md") && f !== "MEMORY.md")
|
|
66
|
+
.sort();
|
|
67
|
+
|
|
68
|
+
for (const file of files) {
|
|
69
|
+
const filePath = path.join(dir, file);
|
|
70
|
+
try {
|
|
71
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
72
|
+
const lines = content.split("\n").length;
|
|
73
|
+
const titleLine = content.split("\n").find((l) => l.startsWith("# "));
|
|
74
|
+
const description = titleLine?.replace(/^#\s+/, "") || "";
|
|
75
|
+
const parsed = parseFileName(file);
|
|
76
|
+
entries.push({
|
|
77
|
+
name: file.replace(".md", ""),
|
|
78
|
+
file,
|
|
79
|
+
description,
|
|
80
|
+
lines,
|
|
81
|
+
scope,
|
|
82
|
+
topic: parsed.topic,
|
|
83
|
+
keywords: parsed.keywords,
|
|
84
|
+
});
|
|
85
|
+
} catch {
|
|
86
|
+
// 文件读取失败 → 用降级信息填充
|
|
87
|
+
entries.push({
|
|
88
|
+
name: file.replace(".md", ""),
|
|
89
|
+
file,
|
|
90
|
+
description: "(读取失败)",
|
|
91
|
+
lines: 0,
|
|
92
|
+
scope,
|
|
93
|
+
topic: parseFileName(file).topic,
|
|
94
|
+
keywords: parseFileName(file).keywords,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return entries;
|
|
100
|
+
}
|
package/lib/types.ts
CHANGED
|
@@ -1,32 +1,24 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Tools — 公共类型与常量
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/** 记忆文件总数硬限制(超过拒绝写入) */
|
|
26
|
-
export const HARD_FILE_LIMIT = 40;
|
|
27
|
-
|
|
28
|
-
/** 记忆文件总数软限制(超过发出警告) */
|
|
29
|
-
export const SOFT_FILE_LIMIT = 25;
|
|
30
|
-
|
|
31
|
-
/** 记忆文件总数提示阈值 */
|
|
32
|
-
export const HINT_FILE_LIMIT = 20;
|
|
1
|
+
/**
|
|
2
|
+
* Memory Tools — 公共类型与常量
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
|
|
8
|
+
/** L1 全局目录 */
|
|
9
|
+
export const AGENT_DIR = path.join(homedir(), ".pi/agent");
|
|
10
|
+
|
|
11
|
+
/** 记忆文件行数上限 */
|
|
12
|
+
export const MAX_FILE_LINES = 200;
|
|
13
|
+
|
|
14
|
+
/** 记忆文件合并后行数上限 */
|
|
15
|
+
export const MAX_MERGED_LINES = MAX_FILE_LINES * 2;
|
|
16
|
+
|
|
17
|
+
/** 记忆文件总数硬限制(超过拒绝写入) */
|
|
18
|
+
export const HARD_FILE_LIMIT = 80;
|
|
19
|
+
|
|
20
|
+
/** 记忆文件总数软限制(超过发出警告) */
|
|
21
|
+
export const SOFT_FILE_LIMIT = 25;
|
|
22
|
+
|
|
23
|
+
/** 记忆文件总数提示阈值 */
|
|
24
|
+
export const HINT_FILE_LIMIT = 20;
|
package/lib/writer.ts
CHANGED
|
@@ -1,69 +1,68 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Tools — 文件写入与索引重建
|
|
3
|
-
*
|
|
4
|
-
* 索引(MEMORY.md)以磁盘文件为唯一真相源。
|
|
5
|
-
* 每次 write 后全量重建索引,保证索引与磁盘始终一致。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import * as fs from "node:fs";
|
|
9
|
-
import * as path from "node:path";
|
|
10
|
-
import { scanMemoryDir } from "./memory-parser";
|
|
11
|
-
import { MAX_FILE_LINES } from "./types";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 全量重建 MEMORY.md 索引
|
|
15
|
-
*
|
|
16
|
-
* 扫描 targetDir 下所有 .md 文件(排除 MEMORY.md 自身),
|
|
17
|
-
* 生成 表格 + 链接区,覆盖写入 indexPath。
|
|
18
|
-
*
|
|
19
|
-
* 这是保证「索引 === 磁盘」的唯一函数。
|
|
20
|
-
*/
|
|
21
|
-
export function rebuildIndex(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
fs.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Memory Tools — 文件写入与索引重建
|
|
3
|
+
*
|
|
4
|
+
* 索引(MEMORY.md)以磁盘文件为唯一真相源。
|
|
5
|
+
* 每次 write 后全量重建索引,保证索引与磁盘始终一致。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import { scanMemoryDir } from "./memory-parser";
|
|
11
|
+
import { MAX_FILE_LINES } from "./types";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 全量重建 MEMORY.md 索引
|
|
15
|
+
*
|
|
16
|
+
* 扫描 targetDir 下所有 .md 文件(排除 MEMORY.md 自身),
|
|
17
|
+
* 生成 表格 + 链接区,覆盖写入 indexPath。
|
|
18
|
+
*
|
|
19
|
+
* 这是保证「索引 === 磁盘」的唯一函数。
|
|
20
|
+
*/
|
|
21
|
+
export function rebuildIndex(
|
|
22
|
+
targetDir: string,
|
|
23
|
+
indexPath: string,
|
|
24
|
+
scope: "L1" | "L2",
|
|
25
|
+
): void {
|
|
26
|
+
const entries = scanMemoryDir(targetDir, scope);
|
|
27
|
+
|
|
28
|
+
if (entries.length === 0) {
|
|
29
|
+
// 没有记忆文件时,写一个空索引(或删除索引)
|
|
30
|
+
if (fs.existsSync(indexPath)) {
|
|
31
|
+
fs.writeFileSync(indexPath, "", "utf-8");
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const scopeTitle = scope === "L1" ? "Global Memory Index (L1)" : "Project Memory Index (L2)";
|
|
37
|
+
const lines: string[] = [
|
|
38
|
+
`# ${scopeTitle}`,
|
|
39
|
+
"",
|
|
40
|
+
"> 自动生成 — 列出所有记忆文件及其关键词摘要;「文件」列为实际文件名(topic--kws.md),可直接 read",
|
|
41
|
+
"",
|
|
42
|
+
`## 文件清单 (${entries.length})`,
|
|
43
|
+
"",
|
|
44
|
+
"| # | 文件 | 关键词 |",
|
|
45
|
+
"|---|------|--------|",
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < entries.length; i++) {
|
|
49
|
+
const e = entries[i];
|
|
50
|
+
const kws = e.keywords.join(", ") || "";
|
|
51
|
+
lines.push(`| ${i + 1} | ${e.file} | ${kws} |`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
lines.push(""); // trailing newline
|
|
55
|
+
|
|
56
|
+
fs.mkdirSync(path.dirname(indexPath), { recursive: true });
|
|
57
|
+
fs.writeFileSync(indexPath, lines.join("\n"), "utf-8");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 检查文件行数是否超限,返回警告或 null */
|
|
61
|
+
export function checkLineCount(filePath: string): string | null {
|
|
62
|
+
if (!fs.existsSync(filePath)) return null;
|
|
63
|
+
const lines = fs.readFileSync(filePath, "utf-8").split("\n").length;
|
|
64
|
+
if (lines > MAX_FILE_LINES) {
|
|
65
|
+
return `⚠️ ${filePath} 超过 ${MAX_FILE_LINES} 行(当前 ${lines} 行),请拆分`;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|