pi-ate-memory 1.0.0 → 1.2.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 +22 -1
- package/index.ts +285 -198
- package/lib/asset-catalog.ts +63 -0
- 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/read-tracker.ts +77 -0
- package/lib/routing-register.ts +67 -0
- package/lib/routing.ts +160 -0
- package/lib/types.ts +24 -32
- package/lib/writer.ts +68 -69
- package/package.json +63 -60
- package/prompts/update-description.md +1 -1
- package/tsconfig.json +10 -10
package/README.en.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
# pi-memory
|
|
4
4
|
|
|
5
|
+
[Source Code](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
6
|
+
|
|
5
7
|
Persistent cross-session memory for [pi](https://github.com/earendil-works/pi-coding-agent).
|
|
6
8
|
|
|
7
9
|
Every AI agent conversation starts from scratch — it doesn't remember what was discussed yesterday, what pitfalls were hit, or what decisions were made. pi-memory solves this using the filesystem: it lets the agent maintain a knowledge base automatically, and loads it into every new session without manual intervention.
|
|
@@ -54,6 +56,8 @@ Scans the memory directories and returns a structured index table.
|
|
|
54
56
|
```
|
|
55
57
|
# Memory Index
|
|
56
58
|
|
|
59
|
+
[Source Code](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
60
|
+
|
|
57
61
|
## L1 — ~/.pi/agent/memory
|
|
58
62
|
|
|
59
63
|
| topic | keywords | lines | description |
|
|
@@ -124,6 +128,8 @@ Markdown format, with the title describing the topic:
|
|
|
124
128
|
```markdown
|
|
125
129
|
# Coding Standards & File Formatting
|
|
126
130
|
|
|
131
|
+
[Source Code](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
132
|
+
|
|
127
133
|
Keywords: coding git lint format
|
|
128
134
|
|
|
129
135
|
## Core Rules
|
|
@@ -142,6 +148,8 @@ Keywords: coding git lint format
|
|
|
142
148
|
```markdown
|
|
143
149
|
# Memory Index
|
|
144
150
|
|
|
151
|
+
[Source Code](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
152
|
+
|
|
145
153
|
> Auto-generated — lists all memory files and their keyword summaries
|
|
146
154
|
|
|
147
155
|
## Files (3)
|
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
# pi-memory
|
|
6
6
|
|
|
7
|
+
[源码仓库](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
8
|
+
|
|
7
9
|
为 [pi](https://github.com/earendil-works/pi-coding-agent) 提供跨会话持久记忆。
|
|
8
10
|
|
|
9
11
|
AI agent 的每次对话都是全新开始——不记得昨天讨论了什么、踩过哪些坑、做过什么决策。pi-memory 用文件系统解决这个问题:让 agent 自动维护一个知识库,每次新会话自动加载。
|
|
@@ -56,6 +58,8 @@ pi install git:github.com/catlain/pi-memory
|
|
|
56
58
|
```
|
|
57
59
|
# 记忆文件索引
|
|
58
60
|
|
|
61
|
+
[源码仓库](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
62
|
+
|
|
59
63
|
## L1 — ~/.pi/agent/memory
|
|
60
64
|
|
|
61
65
|
| topic | keywords | 行数 | 描述 |
|
|
@@ -126,6 +130,8 @@ Markdown 格式,标题即主题描述:
|
|
|
126
130
|
```markdown
|
|
127
131
|
# 编码规范与文件格式
|
|
128
132
|
|
|
133
|
+
[源码仓库](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
134
|
+
|
|
129
135
|
关键词: 编码 git lint 格式
|
|
130
136
|
|
|
131
137
|
## 核心规则
|
|
@@ -144,6 +150,8 @@ Markdown 格式,标题即主题描述:
|
|
|
144
150
|
```markdown
|
|
145
151
|
# Memory Index
|
|
146
152
|
|
|
153
|
+
[源码仓库](https://github.com/catlain/pi-memory) | [npm](https://www.npmjs.com/package/pi-ate-memory)
|
|
154
|
+
|
|
147
155
|
> 自动生成 — 列出所有记忆文件及其关键词摘要
|
|
148
156
|
|
|
149
157
|
## 文件清单 (3)
|
|
@@ -225,10 +233,23 @@ extensions/memory/
|
|
|
225
233
|
```
|
|
226
234
|
|
|
227
235
|
**依赖**:
|
|
228
|
-
- `@pi-atelier/shared-utils
|
|
236
|
+
- `@pi-atelier/shared-utils`(^1.5.0)— scanMemoryDir、parseFileName、**judge 骨架(createJudge/setContext/三层模型链)**
|
|
229
237
|
- `@earendil-works/pi-coding-agent` — ExtensionAPI
|
|
230
238
|
- 无其他依赖
|
|
231
239
|
|
|
240
|
+
## 记忆按需路由(2026-11-23)
|
|
241
|
+
|
|
242
|
+
批量判定路由器:AI 叙述(text 块)计满 N 条 → LLM 判定与哪些**未读**记忆/spec 强相关 → steer 注入指路。
|
|
243
|
+
|
|
244
|
+
```jsonc
|
|
245
|
+
// settings.json(可选;缺省 enabled=true/batchSize=5/includeSpecs=true)
|
|
246
|
+
"memoryRouting": { "enabled": true, "batchSize": 5, "includeSpecs": true, "judgeModel": "" }
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
- `/memory-routing` — 查看当前配置;`/memory-routing 8` 改批量;`on/off` 开关(会话内存效,重启回 settings 缺省)
|
|
250
|
+
- 防护:白名单容错匹配(幻觉路径丢弃,max 3)、sha1 幂等、fail-open、已读剔除、已提醒未读标注自降权
|
|
251
|
+
- 判定日志:`~/.pi/agent/logs/memory-routing-llmjudge.log`
|
|
252
|
+
|
|
232
253
|
## 许可证
|
|
233
254
|
|
|
234
255
|
MIT
|
package/index.ts
CHANGED
|
@@ -1,198 +1,285 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Tools — 记忆索引查询 + 写入
|
|
3
|
-
*
|
|
4
|
-
* 提供两个工具供 AI 主动调用:
|
|
5
|
-
* - memory_index: 扫描记忆目录,返回结构化清单(从文件名解析 topic + keywords)
|
|
6
|
-
* - memory_update: 写文件 + 更新 MEMORY.md 索引(去重由 AI 判断)
|
|
7
|
-
*
|
|
8
|
-
* 记忆架构:
|
|
9
|
-
* L1 ~/.pi/agent/MEMORY.md + memory/*.md ← 跨项目通用
|
|
10
|
-
* L2 {project}/.pi/memory/MEMORY.md + *.md ← 项目主题
|
|
11
|
-
*
|
|
12
|
-
* 文件名格式:topic--kw1,kw2,kw3,kw4,kw5.md
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import
|
|
16
|
-
import
|
|
17
|
-
import
|
|
18
|
-
import
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Memory Tools — 记忆索引查询 + 写入
|
|
3
|
+
*
|
|
4
|
+
* 提供两个工具供 AI 主动调用:
|
|
5
|
+
* - memory_index: 扫描记忆目录,返回结构化清单(从文件名解析 topic + keywords)
|
|
6
|
+
* - memory_update: 写文件 + 更新 MEMORY.md 索引(去重由 AI 判断)
|
|
7
|
+
*
|
|
8
|
+
* 记忆架构:
|
|
9
|
+
* L1 ~/.pi/agent/MEMORY.md + memory/*.md ← 跨项目通用
|
|
10
|
+
* L2 {project}/.pi/memory/MEMORY.md + *.md ← 项目主题
|
|
11
|
+
*
|
|
12
|
+
* 文件名格式:topic--kw1,kw2,kw3,kw4,kw5.md
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as fs from "node:fs";
|
|
16
|
+
import * as path from "node:path";
|
|
17
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
18
|
+
import { Type } from "@sinclair/typebox";
|
|
19
|
+
import { detectConflicts } from "./lib/conflict-detect";
|
|
20
|
+
import { registerMemoryHook } from "./lib/memory-hook";
|
|
21
|
+
import {
|
|
22
|
+
type MemoryEntry,
|
|
23
|
+
parseFileName,
|
|
24
|
+
scanMemoryDir,
|
|
25
|
+
} from "./lib/memory-parser";
|
|
26
|
+
import {
|
|
27
|
+
AGENT_DIR,
|
|
28
|
+
HARD_FILE_LIMIT,
|
|
29
|
+
HINT_FILE_LIMIT,
|
|
30
|
+
MAX_FILE_LINES,
|
|
31
|
+
SOFT_FILE_LIMIT,
|
|
32
|
+
} from "./lib/types";
|
|
33
|
+
import { rebuildIndex } from "./lib/writer";
|
|
34
|
+
import { registerRouting } from "./lib/routing-register";
|
|
35
|
+
|
|
36
|
+
export default function memoryToolsExtension(pi: ExtensionAPI) {
|
|
37
|
+
// ── before_agent_start: 注入记忆说明 + 索引到 systemPrompt ──
|
|
38
|
+
registerMemoryHook(pi);
|
|
39
|
+
|
|
40
|
+
// ── memory routing: 批量判定式记忆路由(拉模式,change memory-routing)──
|
|
41
|
+
registerRouting(pi);
|
|
42
|
+
|
|
43
|
+
// ── memory_index: 查询记忆清单 ──────────────────────
|
|
44
|
+
pi.registerTool({
|
|
45
|
+
name: "memory_index",
|
|
46
|
+
label: "Memory Index",
|
|
47
|
+
description: [
|
|
48
|
+
"扫描并返回所有记忆文件的索引清单(L1 全局 + L2 项目)。",
|
|
49
|
+
"从文件名解析 topic 和关键词,返回结构化表格。",
|
|
50
|
+
"用于:了解已有记忆、判断重复、决定是否新建文件。",
|
|
51
|
+
].join("\n"),
|
|
52
|
+
promptSnippet: "列出所有记忆文件索引",
|
|
53
|
+
parameters: Type.Object({
|
|
54
|
+
scope: Type.Optional(
|
|
55
|
+
Type.Union(
|
|
56
|
+
[Type.Literal("all"), Type.Literal("L1"), Type.Literal("L2")],
|
|
57
|
+
{
|
|
58
|
+
description: "扫描范围:all=全部,L1=全局,L2=项目。默认 all。",
|
|
59
|
+
default: "all",
|
|
60
|
+
},
|
|
61
|
+
),
|
|
62
|
+
),
|
|
63
|
+
}),
|
|
64
|
+
async execute(
|
|
65
|
+
_toolCallId,
|
|
66
|
+
params: { scope?: "all" | "L1" | "L2" },
|
|
67
|
+
_signal,
|
|
68
|
+
_onUpdate,
|
|
69
|
+
ctx,
|
|
70
|
+
) {
|
|
71
|
+
const cwd = (ctx as any).cwd || process.cwd();
|
|
72
|
+
const l1Dir = path.join(AGENT_DIR, "memory");
|
|
73
|
+
const l2Dir = path.join(cwd, ".pi", "memory");
|
|
74
|
+
const entries: MemoryEntry[] = [];
|
|
75
|
+
|
|
76
|
+
if ((params.scope || "all") !== "L2") {
|
|
77
|
+
entries.push(...scanMemoryDir(l1Dir, "L1"));
|
|
78
|
+
// 顺便重建索引,修复手动删文件后的不一致
|
|
79
|
+
const l1IndexPath = path.join(AGENT_DIR, "MEMORY.md");
|
|
80
|
+
if (fs.existsSync(l1Dir)) rebuildIndex(l1Dir, l1IndexPath, "L1");
|
|
81
|
+
}
|
|
82
|
+
if ((params.scope || "all") !== "L1") {
|
|
83
|
+
entries.push(...scanMemoryDir(l2Dir, "L2"));
|
|
84
|
+
// 顺便重建索引,修复手动删文件后的不一致
|
|
85
|
+
const l2IndexPath = path.join(cwd, ".pi", "memory", "MEMORY.md");
|
|
86
|
+
if (fs.existsSync(l2Dir)) rebuildIndex(l2Dir, l2IndexPath, "L2");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (entries.length === 0) {
|
|
90
|
+
return {
|
|
91
|
+
content: [{ type: "text", text: "未找到任何记忆文件。" }],
|
|
92
|
+
details: {},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
entries.sort((a, b) => {
|
|
97
|
+
if (a.scope !== b.scope) return a.scope === "L1" ? -1 : 1;
|
|
98
|
+
return a.name.localeCompare(b.name);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const lines: string[] = ["# 记忆文件索引\n"];
|
|
102
|
+
let currentScope = "";
|
|
103
|
+
for (const e of entries) {
|
|
104
|
+
if (e.scope !== currentScope) {
|
|
105
|
+
currentScope = e.scope;
|
|
106
|
+
const scopeDir = e.scope === "L1" ? l1Dir : l2Dir;
|
|
107
|
+
lines.push(`\n## ${e.scope} — ${scopeDir}\n`);
|
|
108
|
+
lines.push("| topic | keywords | 行数 | 描述 |");
|
|
109
|
+
lines.push("|-------|----------|------|------|");
|
|
110
|
+
}
|
|
111
|
+
const topic = e.topic || e.name;
|
|
112
|
+
const kws = e.keywords.join(", ") || "";
|
|
113
|
+
const lineWarning = e.lines > MAX_FILE_LINES ? " ⚠️" : "";
|
|
114
|
+
lines.push(
|
|
115
|
+
`| ${topic} | ${kws} | ${e.lines}${lineWarning} | ${e.description} |`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
121
|
+
details: {},
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// ── memory_update: 写文件 + 更新索引 ─────────────────
|
|
127
|
+
// AI 负责:看 memory_index → 判断重复 → 决定文件名 → 调用本工具
|
|
128
|
+
pi.registerTool({
|
|
129
|
+
name: "memory_update",
|
|
130
|
+
label: "Memory Update",
|
|
131
|
+
description: fs
|
|
132
|
+
.readFileSync(
|
|
133
|
+
path.join(__dirname, "prompts", "update-description.md"),
|
|
134
|
+
"utf-8",
|
|
135
|
+
)
|
|
136
|
+
.trim(),
|
|
137
|
+
promptSnippet: "写入记忆文件并更新索引",
|
|
138
|
+
parameters: Type.Object({
|
|
139
|
+
fileName: Type.String({
|
|
140
|
+
description: "目标文件名,格式 topic--kw1,kw2,kw3.md",
|
|
141
|
+
}),
|
|
142
|
+
content: Type.String({ description: "完整的记忆文件内容(markdown)" }),
|
|
143
|
+
scope: Type.Optional(
|
|
144
|
+
Type.Union([Type.Literal("L1"), Type.Literal("L2")], {
|
|
145
|
+
description: "目标作用域。默认 L2。",
|
|
146
|
+
default: "L2",
|
|
147
|
+
}),
|
|
148
|
+
),
|
|
149
|
+
}),
|
|
150
|
+
async execute(
|
|
151
|
+
_toolCallId,
|
|
152
|
+
params: { fileName: string; content: string; scope?: "L1" | "L2" },
|
|
153
|
+
_signal,
|
|
154
|
+
_onUpdate,
|
|
155
|
+
ctx,
|
|
156
|
+
) {
|
|
157
|
+
const cwd = (ctx as any).cwd || process.cwd();
|
|
158
|
+
const effectiveScope: "L1" | "L2" = params.scope === "L1" ? "L1" : "L2";
|
|
159
|
+
const targetDir =
|
|
160
|
+
effectiveScope === "L1"
|
|
161
|
+
? path.join(AGENT_DIR, "memory")
|
|
162
|
+
: path.join(cwd, ".pi", "memory");
|
|
163
|
+
const indexPath =
|
|
164
|
+
effectiveScope === "L1"
|
|
165
|
+
? path.join(AGENT_DIR, "MEMORY.md")
|
|
166
|
+
: path.join(cwd, ".pi", "memory", "MEMORY.md");
|
|
167
|
+
|
|
168
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
169
|
+
|
|
170
|
+
// 安全检查:只允许 .md 文件,不允许路径遍历
|
|
171
|
+
const fileName = `${params.fileName.replace(/\.md$/, "")}.md`;
|
|
172
|
+
if (fileName.includes("/") || fileName.includes("\\")) {
|
|
173
|
+
return {
|
|
174
|
+
content: [{ type: "text", text: "❌ 文件名不允许包含路径分隔符" }],
|
|
175
|
+
details: {},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const filePath = path.join(targetDir, fileName);
|
|
180
|
+
const parsed = parseFileName(fileName);
|
|
181
|
+
const isOverwrite = fs.existsSync(filePath);
|
|
182
|
+
const contentBytes = Buffer.byteLength(params.content, "utf-8");
|
|
183
|
+
const MAX_CONTENT_BYTES = 50_000; // 50KB
|
|
184
|
+
if (contentBytes > MAX_CONTENT_BYTES) {
|
|
185
|
+
return {
|
|
186
|
+
content: [
|
|
187
|
+
{
|
|
188
|
+
type: "text",
|
|
189
|
+
text: `❌ 内容 ${(contentBytes / 1024).toFixed(1)}KB 超过 ${MAX_CONTENT_BYTES / 1000}KB 限制`,
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
details: {},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const contentLines = params.content.split("\n").length;
|
|
196
|
+
|
|
197
|
+
// 行数硬限制
|
|
198
|
+
if (contentLines > MAX_FILE_LINES) {
|
|
199
|
+
return {
|
|
200
|
+
content: [
|
|
201
|
+
{
|
|
202
|
+
type: "text",
|
|
203
|
+
text: `❌ 内容 ${contentLines} 行,超过 ${MAX_FILE_LINES} 行硬限制,请精简或拆分后重试`,
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
details: {},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// 记忆总数硬限制 — 先检查再写
|
|
211
|
+
const totalFiles = fs.existsSync(targetDir)
|
|
212
|
+
? fs
|
|
213
|
+
.readdirSync(targetDir)
|
|
214
|
+
.filter((f) => f.endsWith(".md") && f !== "MEMORY.md").length
|
|
215
|
+
: 0;
|
|
216
|
+
// 覆盖已有文件不增加总数,不算超标
|
|
217
|
+
const effectiveTotal = isOverwrite ? totalFiles : totalFiles + 1;
|
|
218
|
+
if (effectiveTotal > HARD_FILE_LIMIT) {
|
|
219
|
+
return {
|
|
220
|
+
content: [
|
|
221
|
+
{
|
|
222
|
+
type: "text",
|
|
223
|
+
text: `❌ ${effectiveScope} 记忆文件已达 ${totalFiles} 个(硬限制 ${HARD_FILE_LIMIT}),拒绝写入。请先用 memory_index 查看,合并/删除后重试。`,
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
details: {},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 写入前检测冲突:同 topic 或高关键词重叠 → 硬拒绝
|
|
231
|
+
if (fs.existsSync(targetDir)) {
|
|
232
|
+
const conflicts = detectConflicts(targetDir, fileName, parsed);
|
|
233
|
+
if (conflicts.length > 0) {
|
|
234
|
+
const conflictList = conflicts
|
|
235
|
+
.map((c) => ` - ${c.name} (${c.reason})`)
|
|
236
|
+
.join("\n");
|
|
237
|
+
return {
|
|
238
|
+
content: [
|
|
239
|
+
{
|
|
240
|
+
type: "text",
|
|
241
|
+
text: [
|
|
242
|
+
`❌ 检测到相关记忆,拒绝写入。请先处理冲突:`,
|
|
243
|
+
``,
|
|
244
|
+
conflictList,
|
|
245
|
+
``,
|
|
246
|
+
`处理方法(按优先级):`,
|
|
247
|
+
`1. 合并:内容互补 → 读取所有冲突文件 → 整合为一份 → 用 memory_update 覆盖其中一个的文件名 → 删除其余文件`,
|
|
248
|
+
`2. 取代:新内容完全覆盖旧结论 → 改用冲突文件的文件名写入(覆盖),不要新建`,
|
|
249
|
+
`3. 确认不相关 → 在 fileName 中调整 topic 或关键词使重叠 < 3,再重试`,
|
|
250
|
+
``,
|
|
251
|
+
`⚠️ 不允许无视此提示。必须处理冲突后才能写入。`,
|
|
252
|
+
].join("\n"),
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
details: {},
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// 所有检查通过,写入文件
|
|
261
|
+
fs.writeFileSync(filePath, params.content, "utf-8");
|
|
262
|
+
// 全量重建索引,保证索引 === 磁盘
|
|
263
|
+
rebuildIndex(targetDir, indexPath, effectiveScope);
|
|
264
|
+
|
|
265
|
+
// 成功消息
|
|
266
|
+
const action = isOverwrite ? "覆盖" : "新建";
|
|
267
|
+
const totalWarning =
|
|
268
|
+
totalFiles > SOFT_FILE_LIMIT
|
|
269
|
+
? `\n⚠️ ${effectiveScope} 记忆文件 ${totalFiles} 个,接近 ${HARD_FILE_LIMIT} 硬限制,请尽快清理合并`
|
|
270
|
+
: totalFiles > HINT_FILE_LIMIT
|
|
271
|
+
? `\n💡 ${effectiveScope} 记忆文件 ${totalFiles} 个,注意控制数量`
|
|
272
|
+
: "";
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
content: [
|
|
276
|
+
{
|
|
277
|
+
type: "text",
|
|
278
|
+
text: `✅ ${action}: ${fileName} (${contentLines} 行)${totalWarning}`,
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
details: {},
|
|
282
|
+
};
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asset Catalog — 知识资产清单构建(记忆 L1/L2 + openspec specs)
|
|
3
|
+
*
|
|
4
|
+
* 供 routing 判定拼装清单用:每条资产 = 相对路径 + 一句话描述。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from "node:fs";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import { AGENT_DIR } from "./types";
|
|
10
|
+
|
|
11
|
+
export interface KnowledgeAsset {
|
|
12
|
+
/** 展示/匹配路径(相对,如 memory/xxx.md 或 openspec/specs/yyy/spec.md) */
|
|
13
|
+
entry: string;
|
|
14
|
+
/** 一句话描述(关键词 or Purpose 首句) */
|
|
15
|
+
desc: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** L1+L2 记忆条目:memory 目录扫描(MEMORY.md 索引文件本身除外) */
|
|
19
|
+
export function collectMemoryAssets(cwd: string): KnowledgeAsset[] {
|
|
20
|
+
const assets: KnowledgeAsset[] = [];
|
|
21
|
+
const sources: Array<[string, string]> = [
|
|
22
|
+
["memory", path.join(AGENT_DIR, "memory")],
|
|
23
|
+
[".pi/memory", path.join(cwd, ".pi", "memory")],
|
|
24
|
+
];
|
|
25
|
+
for (const [prefix, dir] of sources) {
|
|
26
|
+
try {
|
|
27
|
+
for (const f of fs.readdirSync(dir)) {
|
|
28
|
+
if (!f.endsWith(".md") || f === "MEMORY.md") continue;
|
|
29
|
+
assets.push({ entry: `${prefix}/${f}`, desc: "" });
|
|
30
|
+
}
|
|
31
|
+
} catch {
|
|
32
|
+
// 目录不存在(L2 常见):跳过
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return assets;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** spec 清单:openspec/specs/<cap>/spec.md 的 capability + Purpose 首句 */
|
|
39
|
+
export function collectSpecAssets(cwd: string): KnowledgeAsset[] {
|
|
40
|
+
const specsDir = path.join(cwd, "openspec", "specs");
|
|
41
|
+
const assets: KnowledgeAsset[] = [];
|
|
42
|
+
try {
|
|
43
|
+
for (const cap of fs.readdirSync(specsDir)) {
|
|
44
|
+
const p = path.join(specsDir, cap, "spec.md");
|
|
45
|
+
try {
|
|
46
|
+
const text = fs.readFileSync(p, "utf8");
|
|
47
|
+
const m = text.match(/^##\s*Purpose\s*\n+(.+)$/m);
|
|
48
|
+
const purpose = m?.[1] ?? "";
|
|
49
|
+
const firstSentence =
|
|
50
|
+
purpose.split(/[。\n]/)[0]?.replace(/^#+\s*/, "").trim() ?? "";
|
|
51
|
+
assets.push({
|
|
52
|
+
entry: `openspec/specs/${cap}/spec.md`,
|
|
53
|
+
desc: firstSentence.slice(0, 120),
|
|
54
|
+
});
|
|
55
|
+
} catch {
|
|
56
|
+
// 单条读失败跳过
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
// 无 specs 目录
|
|
61
|
+
}
|
|
62
|
+
return assets;
|
|
63
|
+
}
|