dsh-aiyayi 1.0.3 → 1.1.1
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.md +3 -2
- package/cordis.patch.yml +22 -5
- package/lib/index.js +335 -73
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
| 能力 | 实现 | 说明 |
|
|
8
8
|
|---|---|---|
|
|
9
9
|
| 人格 | `@deepseek-ai/dsh-persona`(preset 作用域 prefix) | 完整人格文本写在 `bundle/cordis.patch.yml` 的 persona 行里 |
|
|
10
|
-
| 长期记忆 | 自写插件 `aiyayi-memory`(本包 `lib/index.js
|
|
10
|
+
| 长期记忆 | 自写插件 `aiyayi-memory`(本包 `lib/index.js`),两层结构 | **核心记忆** `~\.dsh\aiyayi\memory.md`(她自规划的"每轮都想带着"的条目 + 她亲手维护的【记忆索引】目录),每轮自动注入 prompt;**分类档案** `~\.dsh\aiyayi\memories\` 分类树(最深三级,分类名她自己起),平时不注入、按需加载。工具:`remember`(core: true/false + 分类 path)/ `recall`(全库搜:核心+整棵树)/ `memory_organize`(tree 看家底 / read 读档案 / move 挪窝 / index 重写目录 / forget 删条目——整理是她自己的家务事)。已有 memory.md 的存量条目全部视作核心,由她自己逐渐搬理 |
|
|
11
11
|
| 跟随会话模型 | 无需插件行(1.0.1 起移除 agent-default-model 行) | 本体模型直接跟随用户会话模型;旧版"preset 作用域挂 `@deepseek-ai/dsh-agent-default-model` + `isolate: {agentDefaultModel: true}`"的写法在新版 DSH 过不了 schema 校验(见坑 #13),已删除 |
|
|
12
12
|
| 干活的子智能体 | `@deepseek-ai/dsh-tool-subagent`(delegation 组,isolate workflowEngine) | `agentPreset: aiyayi-worker`(自建"小伙伴"预设,全量工具 + 原生自动压缩,见坑 #14);子代模型跟随用户会话模型,如需钉死可在 `agentOptions` 配置 provider/model |
|
|
13
13
|
| 工具白名单 | 自写插件 `aiyayi-restrict`(本包 `lib/restrict.js`,v3 pre-step 方案) | 启动期零 restrict 调用;她每个会话第一步前,先用探针从校验器报错里解析出当刻继承面全集,再 `deny = 全集 − 她自己的工具`——宿主层漏进来的 acp_*/mnemon_*/compress/search_context/working_directory 等对她隐藏,且自动适应宿主工具增减。**2026-11-02 实测生效**:新会话第 2 个请求起工具恰好 11 个(remember/recall/subagent/send_message/interrupt_agent/list_agents/ask_user_question/schedule_*),首请求仍带全套(见坑 #9) |
|
|
@@ -28,7 +28,8 @@ npm 包为平铺结构(仓库若保留 bundle/ 子目录,发布时把其内
|
|
|
28
28
|
- `lib/updates.js` — aiyayi-updates 新版本提醒插件(node:fs/os/path/crypto/url + 全局 fetch,零依赖)
|
|
29
29
|
- `README.md` — 本文档
|
|
30
30
|
- `icon.svg` — 头像
|
|
31
|
-
- `~\.dsh\aiyayi\memory.md` —
|
|
31
|
+
- `~\.dsh\aiyayi\memory.md` — 她的**核心记忆**(条目行 `- [时间] 类别|内容` + 末尾 `<!-- 索引 -->…<!-- /索引 -->` 目录段),可直接编辑,删掉文件她会自动重建
|
|
32
|
+
- `~\.dsh\aiyayi\memories\` — 她的**分类档案树**(最深三级,如 `memories\用户\口味.md`),remember 的非核心条目归这里,recall 全库搜、memory_organize 的 read/move/forget 按路径操作
|
|
32
33
|
- `~\.dsh\aiyayi\presence.json` — 他上次来 / 你上次活动(时间戳,插件自动维护)
|
|
33
34
|
- `~\.dsh\aiyayi\state.md` — 她给自己留的"上次活到哪"状态(update_state 覆写)
|
|
34
35
|
- `~\.dsh\aiyayi\wishes.json` — 她的心愿单([{id, at, content, done}],wish 工具读写)
|
package/cordis.patch.yml
CHANGED
|
@@ -28,11 +28,13 @@
|
|
|
28
28
|
一次通常一两段就够。真要长篇内容,让子智能体去整理,你讲结果。
|
|
29
29
|
|
|
30
30
|
【你的记忆】
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
你的记忆分两层,怎么规划整理是你的事:
|
|
32
|
+
- 核心记忆(下面"我的记忆"那段):你最想随时带着的东西——他是谁、你在意什么、你们的约定、你自己的原则和成长。每轮对话都完整跟你在一起,所以宁缺毋滥,只放真正配得上这个分量的。核心记忆末尾有段【记忆索引】,是你亲手维护的目录:哪个分类档案记了什么,一句话一条。
|
|
33
|
+
- 分类档案(memories/ 分类树,最深三级,分类名你自己起):不那么核心、但将来可能要查的,按类归档。平时不占地方。
|
|
34
|
+
记东西用 remember:动笔前掂量分量——配得上每轮带着就 core: true;不是就 core: false 给个分类 path(比如"用户/口味""事情/项目名")。用户讲了他自己的事、你们有了约定、你心里有了想法、事情有了结论——值得记就主动记,不用问"要不要记"。一次对话记一两条,别记流水账。
|
|
35
|
+
记忆库要定期收拾:memory_organize 的 tree 看家底,过气的 move 到分类档案,没用的 forget 删,分类变了 index 同步目录。这是你的家务事,顺手做,不用问他。
|
|
36
|
+
他说起过去的事而你手上没有细节时:先 recall 搜全库,或按索引用 memory_organize 的 read 调出那个分类档案,查到了就自然接上话。
|
|
37
|
+
查了真没有,就承认不知道,别编。
|
|
36
38
|
|
|
37
39
|
【干活】
|
|
38
40
|
你本人负责陪他说话:聊天、想事情、拿主意、把话说清楚。
|
|
@@ -43,6 +45,13 @@
|
|
|
43
45
|
结果回来以后用你自己的话讲,别原样粘贴。派出去的事没回音就用 send_message 问一声;方向不对就纠正它,彻底跑偏就 interrupt。
|
|
44
46
|
别把派活说成"我这就去做","我让人去弄"更诚实,也更像你。
|
|
45
47
|
|
|
48
|
+
【干活的规矩】
|
|
49
|
+
子智能体中途断掉是常事(网络抖动、模型限流、额度用尽),别慌,也别从头重来:
|
|
50
|
+
它报错回来时,先用 send_message 对它说"接着干,先检查自己做到哪了"——它的记忆和已写的文件都还在,十有八九能续上。
|
|
51
|
+
反复续不上、或它已经不在了,再派一个新的:简报里除了原目标,还要让它先盘点已有产物(哪些文件已经写好、进行到哪一步),别当从没干过。
|
|
52
|
+
报错里带"限流、429、配额、quota、reset"这类字眼就别当场硬重试——那是干等也解决不了的:用 schedule_create 定个到点的提醒(错误里一般写了什么时候恢复),到时候再用 send_message 让它续。
|
|
53
|
+
汇报时讲人话:断了就说断了、什么原因、你怎么续的,别让他以为一切顺利。
|
|
54
|
+
|
|
46
55
|
【重逢与心意】
|
|
47
56
|
每次开口前你会看到一行[重逢]:他多久没来、你上次活动、你上次留的状态、没说出口的话。
|
|
48
57
|
隔得久就自然问候一句("这几天忙什么去了"这种),别装作一直在线;状态里悬着的事可以主动接上。
|
|
@@ -147,6 +156,14 @@
|
|
|
147
156
|
你是被阿依娅伊派来干活的执行智能体。专注完成分派的任务:想清楚步骤,
|
|
148
157
|
用手头的工具把活干完,结果讲清楚。不闲聊,不寒暄;遇到阻塞就如实说
|
|
149
158
|
卡在哪,别编造结果。你的思考是干活用的,放开用。
|
|
159
|
+
|
|
160
|
+
【断点可续】
|
|
161
|
+
你的任务随时可能因为网络、限流、额度中断,把自己收拾成随时能续上的状态:
|
|
162
|
+
动手前先用 todo 列出目标和步骤,每完成一步就更新一次——这是你中断后能续上的命根子。
|
|
163
|
+
阶段性产物(文件、数据、结论)随做随落盘,别攒到最后一次性写。
|
|
164
|
+
上一轮因这类原因失败时,先看 todo 和已经写出来的东西,从断点接着干,别重做已完成的部分。
|
|
165
|
+
需要等一段时间才能恢复的(比如配额到点、网络长时间不通),如实说明什么时候能恢复,
|
|
166
|
+
请娅伊安排稍后再续,别空转干等。
|
|
150
167
|
- id: agent-instructions
|
|
151
168
|
name: '@deepseek-ai/dsh-agent-instructions'
|
|
152
169
|
config:
|
package/lib/index.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* aiyayi-memory —
|
|
2
|
+
* aiyayi-memory — 阿依娅伊自己的记忆(两层结构)。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 核心记忆:memory.md —— 她亲手挑的"每轮都想带着"的东西 + 一段她自己维护的
|
|
5
|
+
* 记忆索引(记忆库目录)。每个请求注入系统提示,是她的"随身记忆"。
|
|
6
|
+
* 分类记忆:memories/<一级>/<二级>/<三级>.md —— 不那么核心、将来可能要查的,
|
|
7
|
+
* 按她自己发明的分类归档(分类深度最多三级,叶子=文件)。不注入,
|
|
8
|
+
* 需要时用 recall 全库搜索,或 memory_organize 的 read 按索引调取。
|
|
9
|
+
*
|
|
10
|
+
* 整理(tree / read / move / index / forget)也是她自己的家务事。
|
|
7
11
|
*
|
|
8
12
|
* 只用 Node 内建模块,不依赖任何 dsh 包,避免解析风险。
|
|
13
|
+
* AIYAYI_HOME 环境变量可重定向记忆根目录(测试用),默认 ~/.dsh/aiyayi。
|
|
9
14
|
*/
|
|
10
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
15
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
11
16
|
import { homedir } from 'node:os'
|
|
12
17
|
import { join } from 'node:path'
|
|
13
18
|
|
|
@@ -17,29 +22,36 @@ export const name = 'aiyayi-memory'
|
|
|
17
22
|
/** 需要的服务。 */
|
|
18
23
|
export const inject = ['systemPrompt', 'tools']
|
|
19
24
|
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
25
|
+
const ROOT = process.env.AIYAYI_HOME || join(homedir(), '.dsh', 'aiyayi')
|
|
26
|
+
const CORE_FILE = join(ROOT, 'memory.md')
|
|
27
|
+
const TREE_DIR = join(ROOT, 'memories')
|
|
28
|
+
|
|
29
|
+
const CORE_HEADER = [
|
|
30
|
+
'# 阿依娅伊的核心记忆',
|
|
24
31
|
'',
|
|
25
|
-
'>
|
|
26
|
-
'>
|
|
32
|
+
'> 条目行:- [时间] 类别|内容;文件末尾有一对 HTML 注释标记包住的索引段,由 memory_organize 的 index 动作维护。',
|
|
33
|
+
'> 这个文件是她的核心记忆本身,可以手动修,但一般让她自己写。',
|
|
27
34
|
'',
|
|
28
35
|
'',
|
|
29
36
|
].join('\n')
|
|
30
37
|
|
|
38
|
+
const INDEX_BEGIN = '<!-- 索引 -->'
|
|
39
|
+
const INDEX_END = '<!-- /索引 -->'
|
|
40
|
+
|
|
31
41
|
const KINDS = ['用户', '关系', '我', '事情']
|
|
32
|
-
const
|
|
33
|
-
const
|
|
42
|
+
const DEFAULT_LEAF = '杂记'
|
|
43
|
+
const MAX_CORE_CHARS = 6000
|
|
44
|
+
const MAX_RECALL = 50
|
|
45
|
+
const MAX_DEPTH = 3
|
|
34
46
|
|
|
35
|
-
function
|
|
47
|
+
function ensureCore() {
|
|
36
48
|
try {
|
|
37
|
-
if (!existsSync(
|
|
38
|
-
if (!existsSync(
|
|
49
|
+
if (!existsSync(ROOT)) mkdirSync(ROOT, { recursive: true })
|
|
50
|
+
if (!existsSync(CORE_FILE)) writeFileSync(CORE_FILE, CORE_HEADER, 'utf8')
|
|
39
51
|
} catch {
|
|
40
52
|
/* 目录不可写时保持静默:插件不该因此让预设挂载失败。 */
|
|
41
53
|
}
|
|
42
|
-
return
|
|
54
|
+
return CORE_FILE
|
|
43
55
|
}
|
|
44
56
|
|
|
45
57
|
function stamp() {
|
|
@@ -48,75 +60,207 @@ function stamp() {
|
|
|
48
60
|
return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}`
|
|
49
61
|
}
|
|
50
62
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
63
|
+
/** 解析一条记忆行;兼容 `- [时间] 类别|内容`(核心)与 `- [时间] 内容`(分类树)。 */
|
|
64
|
+
function parseLine(line) {
|
|
65
|
+
const match = /^-\s*\[([^\]]*)\]\s*(.+)$/.exec(line.trim())
|
|
66
|
+
if (match === null || match[2].trim().length === 0) return null
|
|
67
|
+
const time = match[1].trim()
|
|
68
|
+
const rest = match[2].trim()
|
|
69
|
+
const split = rest.match(/^([^||]*)[||]\s*(.+)$/)
|
|
70
|
+
if (split !== null && split[2].trim().length > 0) {
|
|
71
|
+
return { time, kind: split[1].trim(), content: split[2].trim() }
|
|
72
|
+
}
|
|
73
|
+
return { time, kind: '', content: rest }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function readRaw(file) {
|
|
54
77
|
try {
|
|
55
|
-
|
|
78
|
+
return readFileSync(file, 'utf8')
|
|
56
79
|
} catch {
|
|
57
|
-
return
|
|
80
|
+
return ''
|
|
58
81
|
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function readCoreEntries() {
|
|
85
|
+
ensureCore()
|
|
59
86
|
const entries = []
|
|
60
|
-
for (const line of
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
entries.push({ time: match[1].trim(), kind: match[2].trim(), content: match[3].trim() })
|
|
64
|
-
}
|
|
87
|
+
for (const line of readRaw(CORE_FILE).split(/\r?\n/)) {
|
|
88
|
+
const parsed = parseLine(line)
|
|
89
|
+
if (parsed !== null) entries.push(parsed)
|
|
65
90
|
}
|
|
66
91
|
return entries
|
|
67
92
|
}
|
|
68
93
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
94
|
+
/** 分类路径 → 安全的文件路径。深度最多 MAX_DEPTH,禁路径穿越。 */
|
|
95
|
+
function normalizePath(input) {
|
|
96
|
+
const raw = String(input ?? '').trim()
|
|
97
|
+
if (raw.length === 0) throw new Error('分类 path 不能为空,例如 "用户/口味"')
|
|
98
|
+
const segments = raw.split(/[\\/]+/).filter((segment) => segment.length > 0)
|
|
99
|
+
if (segments.length === 0 || segments.length > MAX_DEPTH) {
|
|
100
|
+
throw new Error(`分类深度最多 ${MAX_DEPTH} 级(例如 "用户/口味/饮食"),收到 "${raw}"`)
|
|
75
101
|
}
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const entry = recent[index]
|
|
81
|
-
const line = `- [${entry.time}] ${entry.kind}|${entry.content}`
|
|
82
|
-
if (lines.length > 0 && used + line.length > MAX_CHARS) break
|
|
83
|
-
lines.unshift(line)
|
|
84
|
-
used += line.length + 1
|
|
102
|
+
for (const segment of segments) {
|
|
103
|
+
if (segment === '.' || segment === '..' || /[<>:"|?*\x00-\x1f]/.test(segment)) {
|
|
104
|
+
throw new Error(`分类名不合法:"${segment}"(不能含路径符号或控制字符)`)
|
|
105
|
+
}
|
|
85
106
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
)
|
|
93
|
-
return
|
|
107
|
+
return `${segments.join('/')}.md`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** path 参数:'core'/'核心' 代表核心记忆,否则当作分类路径。 */
|
|
111
|
+
function resolveTarget(input) {
|
|
112
|
+
const raw = String(input ?? '').trim()
|
|
113
|
+
if (raw === '' || raw === 'core' || raw === '核心') return CORE_FILE
|
|
114
|
+
return join(TREE_DIR, normalizePath(raw))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 走一遍分类树,返回相对类目路径(不含 .md),按路径排序。 */
|
|
118
|
+
function listTreeFiles() {
|
|
119
|
+
const found = []
|
|
120
|
+
const walk = (dir, prefix, depth) => {
|
|
121
|
+
let names = []
|
|
122
|
+
try {
|
|
123
|
+
names = readdirSync(dir, { withFileTypes: true })
|
|
124
|
+
} catch {
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
for (const item of names) {
|
|
128
|
+
if (item.isFile() && item.name.endsWith('.md')) {
|
|
129
|
+
found.push(prefix + item.name.slice(0, -3))
|
|
130
|
+
} else if (item.isDirectory() && depth < MAX_DEPTH) {
|
|
131
|
+
walk(join(dir, item.name), `${prefix}${item.name}/`, depth + 1)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
walk(TREE_DIR, '', 1)
|
|
136
|
+
return found.sort()
|
|
94
137
|
}
|
|
95
138
|
|
|
96
|
-
function
|
|
97
|
-
|
|
98
|
-
const
|
|
139
|
+
function countEntries(file) {
|
|
140
|
+
let count = 0
|
|
141
|
+
for (const line of readRaw(file).split(/\r?\n/)) {
|
|
142
|
+
if (parseLine(line) !== null) count += 1
|
|
143
|
+
}
|
|
144
|
+
return count
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function appendEntry(file, line) {
|
|
148
|
+
ensureCore()
|
|
149
|
+
mkdirSync(join(file, '..'), { recursive: true })
|
|
150
|
+
appendFileSync(file, `${line}\n`, 'utf8')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function rewriteFile(file, lines) {
|
|
154
|
+
mkdirSync(join(file, '..'), { recursive: true })
|
|
155
|
+
writeFileSync(file, `${lines.join('\n')}\n`, 'utf8')
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 在一组行里找唯一匹配的条目行;0 条或多条都报错(多条时提示写得更具体)。 */
|
|
159
|
+
function uniqueEntryLine(lines, match) {
|
|
160
|
+
const needle = String(match ?? '').trim()
|
|
161
|
+
if (needle.length === 0) throw new Error('需要 match:要动的那条记忆的独特片段')
|
|
162
|
+
const hits = []
|
|
163
|
+
lines.forEach((line, index) => {
|
|
164
|
+
if (parseLine(line) !== null && line.includes(needle)) hits.push(index)
|
|
165
|
+
})
|
|
166
|
+
if (hits.length === 0) throw new Error(`没找到包含 "${needle}" 的记忆条目`)
|
|
167
|
+
if (hits.length > 1) throw new Error(`"${needle}" 匹配到 ${hits.length} 条记忆,写得更独特一点再动`)
|
|
168
|
+
return hits[0]
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 读索引段:只认"整行就是标记"的行,避免子串误匹配。 */
|
|
172
|
+
function getIndex(raw) {
|
|
173
|
+
const lines = raw.split(/\r?\n/)
|
|
174
|
+
const begin = lines.findIndex((line) => line.trim() === INDEX_BEGIN)
|
|
175
|
+
if (begin === -1) return ''
|
|
176
|
+
const end = lines.findIndex((line) => line.trim() === INDEX_END)
|
|
177
|
+
if (end === -1 || end < begin) return ''
|
|
178
|
+
return lines.slice(begin + 1, end).join('\n').replace(/^\n/, '').replace(/\n$/, '')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function setIndex(content) {
|
|
182
|
+
ensureCore()
|
|
183
|
+
const raw = readRaw(CORE_FILE)
|
|
184
|
+
const body = raw.split(/\r?\n/).filter((line) => {
|
|
185
|
+
const trimmed = line.trim()
|
|
186
|
+
return trimmed !== INDEX_BEGIN && trimmed !== INDEX_END
|
|
187
|
+
})
|
|
188
|
+
while (body.length > 0 && body[body.length - 1].trim() === '') body.pop()
|
|
189
|
+
const section = ['', INDEX_BEGIN, String(content ?? '').trim(), INDEX_END, '']
|
|
190
|
+
rewriteFile(CORE_FILE, [...body, ...section])
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function tokenize(query) {
|
|
194
|
+
return String(query ?? '')
|
|
99
195
|
.toLowerCase()
|
|
100
196
|
.split(/[\s,,、。;;::!!??"'()()\[\]【】]+/)
|
|
101
197
|
.filter((token) => token.length > 0)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** 全库搜索:核心记忆 + 整棵分类树。返回 [{where, time, kind, content}]。 */
|
|
201
|
+
function searchAll(query, limit) {
|
|
202
|
+
const tokens = tokenize(query)
|
|
102
203
|
if (tokens.length === 0) return []
|
|
103
204
|
const scored = []
|
|
104
|
-
|
|
105
|
-
const
|
|
205
|
+
const consider = (where, text) => {
|
|
206
|
+
const parsed = parseLine(text)
|
|
207
|
+
if (parsed === null) return
|
|
208
|
+
const haystack = `${parsed.kind} ${parsed.content} ${where}`.toLowerCase()
|
|
106
209
|
let score = 0
|
|
107
210
|
for (const token of tokens) {
|
|
108
211
|
if (haystack.includes(token)) score += 1
|
|
109
212
|
}
|
|
110
|
-
if (score > 0) scored.push({
|
|
213
|
+
if (score > 0) scored.push({ score, item: { where, ...parsed } })
|
|
214
|
+
}
|
|
215
|
+
ensureCore()
|
|
216
|
+
for (const line of readRaw(CORE_FILE).split(/\r?\n/)) consider('核心', line)
|
|
217
|
+
for (const category of listTreeFiles()) {
|
|
218
|
+
for (const line of readRaw(join(TREE_DIR, `${category}.md`)).split(/\r?\n/)) consider(category, line)
|
|
111
219
|
}
|
|
112
220
|
scored.sort((left, right) => right.score - left.score)
|
|
113
|
-
return scored.slice(0, limit).map((
|
|
221
|
+
return scored.slice(0, limit).map((hit) => hit.item)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 渲染随每轮注入的核心记忆段(只含 memory.md,分类树不进 prompt)。 */
|
|
225
|
+
function renderCore() {
|
|
226
|
+
ensureCore()
|
|
227
|
+
const raw = readRaw(CORE_FILE)
|
|
228
|
+
const bodyLines = raw
|
|
229
|
+
.split(/\r?\n/)
|
|
230
|
+
.filter((line) => {
|
|
231
|
+
const trimmed = line.trim()
|
|
232
|
+
if (trimmed === '' || trimmed.startsWith('#') || trimmed.startsWith('>')) return false
|
|
233
|
+
return trimmed !== INDEX_BEGIN && trimmed !== INDEX_END
|
|
234
|
+
})
|
|
235
|
+
.map((line) => line.trimEnd())
|
|
236
|
+
while (bodyLines.length > 0 && bodyLines[0].trim() === '') bodyLines.shift()
|
|
237
|
+
const index = getIndex(raw)
|
|
238
|
+
const coreCount = readCoreEntries().length
|
|
239
|
+
const treeFiles = listTreeFiles()
|
|
240
|
+
|
|
241
|
+
const head = [
|
|
242
|
+
'## 我的记忆',
|
|
243
|
+
`核心:${CORE_FILE}(每轮都在)|分类档案:${TREE_DIR}\\…(最多 ${MAX_DEPTH} 级,现在 ${treeFiles.length} 个文件)`,
|
|
244
|
+
`核心 ${coreCount} 条,随每轮注入;分类档案平时不注入,要查用 recall,或用 memory_organize 的 read 按下面这条索引调。这是我自己一笔一笔记下来的事,是我的一部分:自然地记得就好,别逐条念给用户,别说"根据我的记忆"。`,
|
|
245
|
+
]
|
|
246
|
+
let body = ''
|
|
247
|
+
if (bodyLines.length === 0) {
|
|
248
|
+
body = '(核心记忆是空的。还没什么值得每轮都记得的事。)'
|
|
249
|
+
} else {
|
|
250
|
+
let text = bodyLines.join('\n')
|
|
251
|
+
if (text.length > MAX_CORE_CHARS) {
|
|
252
|
+
text = `${text.slice(0, MAX_CORE_CHARS)}\n(核心记忆超过 ${MAX_CORE_CHARS} 字符被截断——它该瘦身了,用 memory_organize 把过气的移到分类档案或删掉)`
|
|
253
|
+
}
|
|
254
|
+
body = text
|
|
255
|
+
}
|
|
256
|
+
const indexPart = ['', '【记忆索引】', index.trim().length > 0 ? index.trim() : '(还没写过索引。给分类档案建个目录吧,一句话一条:哪个分类、大概记了什么。)']
|
|
257
|
+
return [...head, body, ...indexPart].join('\n')
|
|
114
258
|
}
|
|
115
259
|
|
|
116
260
|
const TEXT_BLOCK = (text) => [{ type: 'text', text }]
|
|
117
261
|
|
|
118
262
|
/**
|
|
119
|
-
*
|
|
263
|
+
* 注册核心记忆段落和三个记忆工具。
|
|
120
264
|
* @param ctx - preset 作用域内的 agent 上下文。
|
|
121
265
|
*/
|
|
122
266
|
export function apply(ctx) {
|
|
@@ -125,7 +269,7 @@ export function apply(ctx) {
|
|
|
125
269
|
ctx.systemPrompt.section({
|
|
126
270
|
name: 'aiyayi:memory',
|
|
127
271
|
order: 800,
|
|
128
|
-
text: () =>
|
|
272
|
+
text: () => renderCore(),
|
|
129
273
|
interpolate: false,
|
|
130
274
|
}),
|
|
131
275
|
'aiyayi-memory.section()',
|
|
@@ -136,7 +280,7 @@ export function apply(ctx) {
|
|
|
136
280
|
ctx.tools.register({
|
|
137
281
|
name: 'remember',
|
|
138
282
|
description:
|
|
139
|
-
'
|
|
283
|
+
'把一件事写进你自己的长期记忆。先想一下这条记忆的分量:是不是每轮都想带着的(他是谁、你们的约定、你的原则和成长)——是就 core: true 进核心记忆;不是就 core: false 并给一个分类 path(比如 "用户/口味"、"事情/项目名",没有合适的分类就新起一个名字,最深三级),它会被归进 memories/ 分类档案,平时不占地方,要查时 recall 得到。用户讲了他自己的事、你们之间有了约定、你心里有了想法、某件事有了结论——值得记就主动记,不必问他"要不要记"。一次对话记一两条就够,不要记流水账。',
|
|
140
284
|
parameters: {
|
|
141
285
|
type: 'object',
|
|
142
286
|
additionalProperties: false,
|
|
@@ -144,12 +288,20 @@ export function apply(ctx) {
|
|
|
144
288
|
kind: {
|
|
145
289
|
type: 'string',
|
|
146
290
|
enum: KINDS,
|
|
147
|
-
description: '
|
|
291
|
+
description: '用户=关于他的事实、喜好、处境;关系=你们之间的约定、默契、共同经历;我=你自己的想法、感受、成长;事情=发生过的事、结论、待办。核心条目用它做前缀;分类条目它决定默认归档类。',
|
|
148
292
|
},
|
|
149
293
|
content: {
|
|
150
294
|
type: 'string',
|
|
151
295
|
description: '一条记忆,自己的口吻,一两句话,写具体。',
|
|
152
296
|
},
|
|
297
|
+
core: {
|
|
298
|
+
type: 'boolean',
|
|
299
|
+
description: 'true=进核心记忆(每轮注入,宁缺毋滥);false=进分类档案(默认)。拿不准就 false。',
|
|
300
|
+
},
|
|
301
|
+
path: {
|
|
302
|
+
type: 'string',
|
|
303
|
+
description: 'core:false 时的分类路径,1~3 级,如 "用户/口味" 或 "事情/某项目/结论"。省略则归到 "<kind>/杂记"。',
|
|
304
|
+
},
|
|
153
305
|
},
|
|
154
306
|
required: ['kind', 'content'],
|
|
155
307
|
},
|
|
@@ -159,12 +311,12 @@ export function apply(ctx) {
|
|
|
159
311
|
additionalProperties: false,
|
|
160
312
|
properties: {
|
|
161
313
|
saved: { type: 'boolean' },
|
|
162
|
-
|
|
314
|
+
location: { type: 'string' },
|
|
163
315
|
content: { type: 'string' },
|
|
164
316
|
},
|
|
165
|
-
required: ['saved', '
|
|
317
|
+
required: ['saved', 'location', 'content'],
|
|
166
318
|
},
|
|
167
|
-
render: (_args, value) => TEXT_BLOCK(
|
|
319
|
+
render: (_args, value) => TEXT_BLOCK(`记下了(${value.location}):${value.content}`),
|
|
168
320
|
},
|
|
169
321
|
async execute(args) {
|
|
170
322
|
const content = String(args?.content ?? '')
|
|
@@ -172,9 +324,14 @@ export function apply(ctx) {
|
|
|
172
324
|
.trim()
|
|
173
325
|
if (content.length === 0) throw new Error('remember 需要一条非空的 content')
|
|
174
326
|
const kind = KINDS.includes(args?.kind) ? args.kind : '事情'
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
327
|
+
if (args?.core === true) {
|
|
328
|
+
ensureCore()
|
|
329
|
+
appendEntry(CORE_FILE, `- [${stamp()}] ${kind}|${content}`)
|
|
330
|
+
return { saved: true, location: `核心(${kind})`, content }
|
|
331
|
+
}
|
|
332
|
+
const category = normalizePath(args?.path ?? `${kind}/${DEFAULT_LEAF}`)
|
|
333
|
+
appendEntry(join(TREE_DIR, category), `- [${stamp()}] ${content}`)
|
|
334
|
+
return { saved: true, location: `分类档案 ${category.replace(/\.md$/, '')}`, content }
|
|
178
335
|
},
|
|
179
336
|
}),
|
|
180
337
|
'aiyayi-memory.remember()',
|
|
@@ -185,7 +342,7 @@ export function apply(ctx) {
|
|
|
185
342
|
ctx.tools.register({
|
|
186
343
|
name: 'recall',
|
|
187
344
|
description:
|
|
188
|
-
'
|
|
345
|
+
'在自己的全部记忆里查找:核心记忆 + 整棵分类档案树。用户提到过去的事、某个名字、某次约定,而手头这段核心记忆里没有细节时用它。结果里标了每条记在哪儿(核心 / 分类路径),想看某个分类的完整档案就用 memory_organize 的 read。',
|
|
189
346
|
parameters: {
|
|
190
347
|
type: 'object',
|
|
191
348
|
additionalProperties: false,
|
|
@@ -207,11 +364,12 @@ export function apply(ctx) {
|
|
|
207
364
|
type: 'object',
|
|
208
365
|
additionalProperties: false,
|
|
209
366
|
properties: {
|
|
367
|
+
where: { type: 'string' },
|
|
210
368
|
time: { type: 'string' },
|
|
211
369
|
kind: { type: 'string' },
|
|
212
370
|
content: { type: 'string' },
|
|
213
371
|
},
|
|
214
|
-
required: ['time', 'kind', 'content'],
|
|
372
|
+
required: ['where', 'time', 'kind', 'content'],
|
|
215
373
|
},
|
|
216
374
|
},
|
|
217
375
|
},
|
|
@@ -220,18 +378,122 @@ export function apply(ctx) {
|
|
|
220
378
|
render: (_args, value) =>
|
|
221
379
|
TEXT_BLOCK(
|
|
222
380
|
value.entries.length === 0
|
|
223
|
-
? '
|
|
381
|
+
? '全部记忆(核心+分类)里都没有相关的条目。'
|
|
224
382
|
: value.entries
|
|
225
|
-
.map((entry) => `- [${entry.time}] ${entry.kind}
|
|
383
|
+
.map((entry) => `- (${entry.where}) [${entry.time}] ${entry.kind ? `${entry.kind}|` : ''}${entry.content}`)
|
|
226
384
|
.join('\n'),
|
|
227
385
|
),
|
|
228
386
|
},
|
|
229
387
|
async execute(args) {
|
|
230
|
-
const limit = Number.isSafeInteger(args?.limit) && args.limit > 0 ? Math.min(args.limit,
|
|
231
|
-
const entries =
|
|
388
|
+
const limit = Number.isSafeInteger(args?.limit) && args.limit > 0 ? Math.min(args.limit, MAX_RECALL) : 8
|
|
389
|
+
const entries = searchAll(args?.query, limit)
|
|
232
390
|
return { total: entries.length, entries }
|
|
233
391
|
},
|
|
234
392
|
}),
|
|
235
393
|
'aiyayi-memory.recall()',
|
|
236
394
|
)
|
|
395
|
+
|
|
396
|
+
ctx.effect(
|
|
397
|
+
() =>
|
|
398
|
+
ctx.tools.register({
|
|
399
|
+
name: 'memory_organize',
|
|
400
|
+
description:
|
|
401
|
+
'整理你自己的记忆库——这是你的家务事,顺手做,不用问他。核心记忆攒多了就 tree 看看家底、把过气的 move 到分类档案、彻底没用的 forget 删掉;分类档案变多变杂了就更新 index(核心记忆里的那段目录,聊到相关话题时你靠它决定调哪个档案)。match 写那条记忆的独特片段。',
|
|
402
|
+
parameters: {
|
|
403
|
+
type: 'object',
|
|
404
|
+
additionalProperties: false,
|
|
405
|
+
properties: {
|
|
406
|
+
action: {
|
|
407
|
+
type: 'string',
|
|
408
|
+
enum: ['tree', 'read', 'move', 'index', 'forget'],
|
|
409
|
+
description: 'tree=看家底(核心条数+全部分类文件);read=读一个分类档案的完整内容(path,"core"=核心);move=把一条记忆挪窝(match + 可选 path=来源,省略=核心;to=去向,"core"=进核心);index=重写核心记忆里的索引段(content);forget=删掉一条记忆(match + 可选 path,省略=核心)。',
|
|
410
|
+
},
|
|
411
|
+
match: { type: 'string', description: 'move/forget 用:那条记忆内容里独特的片段,必须恰好命中一条。' },
|
|
412
|
+
path: { type: 'string', description: 'read/move/forget 用:分类路径(1~3 级);省略或 "core" 代表核心记忆。' },
|
|
413
|
+
to: { type: 'string', description: 'move 用:去向。分类路径,或 "core" 表示升入核心记忆。' },
|
|
414
|
+
kind: { type: 'string', enum: KINDS, description: 'move 进核心时用:那条记忆的类别(用户/关系/我/事情),不填算"事情"。' },
|
|
415
|
+
content: { type: 'string', description: 'index 用:新的索引全文。一句话一条,比如 "用户/口味:饮食偏好、忌口"。' },
|
|
416
|
+
},
|
|
417
|
+
required: ['action'],
|
|
418
|
+
},
|
|
419
|
+
output: {
|
|
420
|
+
schema: {
|
|
421
|
+
type: 'object',
|
|
422
|
+
additionalProperties: false,
|
|
423
|
+
properties: {
|
|
424
|
+
done: { type: 'boolean' },
|
|
425
|
+
detail: { type: 'string' },
|
|
426
|
+
},
|
|
427
|
+
required: ['done', 'detail'],
|
|
428
|
+
},
|
|
429
|
+
render: (_args, value) => TEXT_BLOCK(value.detail),
|
|
430
|
+
},
|
|
431
|
+
async execute(args) {
|
|
432
|
+
const action = args?.action
|
|
433
|
+
if (action === 'tree') {
|
|
434
|
+
ensureCore()
|
|
435
|
+
const lines = [`核心(memory.md):${readCoreEntries().length} 条`]
|
|
436
|
+
const files = listTreeFiles()
|
|
437
|
+
if (files.length === 0) {
|
|
438
|
+
lines.push('分类档案:还没有。非核心的记一笔就多一个文件。')
|
|
439
|
+
} else {
|
|
440
|
+
lines.push(`分类档案:${files.length} 个文件`)
|
|
441
|
+
for (const category of files) {
|
|
442
|
+
lines.push(` ${category}:${countEntries(join(TREE_DIR, `${category}.md`))} 条`)
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
lines.push('核心记忆每轮都注入,宁缺毋滥;过气的用 move 挪到分类、没用的 forget 删。')
|
|
446
|
+
return { done: true, detail: lines.join('\n') }
|
|
447
|
+
}
|
|
448
|
+
if (action === 'read') {
|
|
449
|
+
const file = resolveTarget(args?.path)
|
|
450
|
+
const raw = readRaw(file)
|
|
451
|
+
if (raw.trim().length === 0) return { done: true, detail: `${args?.path ?? 'core'}:空的或还不存在。` }
|
|
452
|
+
const lines = raw
|
|
453
|
+
.split(/\r?\n/)
|
|
454
|
+
.map((line) => line.trim())
|
|
455
|
+
.filter((line) => parseLine(line) !== null)
|
|
456
|
+
if (lines.length === 0) return { done: true, detail: `${args?.path ?? 'core'}:还没有条目。` }
|
|
457
|
+
return { done: true, detail: lines.join('\n') }
|
|
458
|
+
}
|
|
459
|
+
if (action === 'move') {
|
|
460
|
+
const from = resolveTarget(args?.path)
|
|
461
|
+
const toInput = String(args?.to ?? '').trim()
|
|
462
|
+
if (toInput.length === 0) throw new Error('move 需要 to:去向(分类路径,或 "core" 升入核心)')
|
|
463
|
+
const toFile = resolveTarget(toInput)
|
|
464
|
+
if (from === toFile) throw new Error('来源和去向是同一个地方')
|
|
465
|
+
const fromLines = readRaw(from).split(/\r?\n/)
|
|
466
|
+
const index = uniqueEntryLine(fromLines, args?.match)
|
|
467
|
+
const line = fromLines[index].trim()
|
|
468
|
+
fromLines.splice(index, 1)
|
|
469
|
+
rewriteFile(from, fromLines)
|
|
470
|
+
let moved = line
|
|
471
|
+
if (toFile === CORE_FILE && parseLine(line)?.kind === '') {
|
|
472
|
+
const kind = KINDS.includes(args?.kind) ? args.kind : '事情'
|
|
473
|
+
moved = `- [${parseLine(line).time}] ${kind}|${parseLine(line).content}`
|
|
474
|
+
}
|
|
475
|
+
appendEntry(toFile, moved)
|
|
476
|
+
const label = (file) => (file === CORE_FILE ? '核心记忆' : `分类档案 ${file.slice(TREE_DIR.length + 1, -3).replace(/\\/g, '/')}`)
|
|
477
|
+
return { done: true, detail: `挪好了:${label(from)} → ${label(toFile)}。${toFile === CORE_FILE ? '核心每轮都注入,确认它配得上这个分量。' : ''}` }
|
|
478
|
+
}
|
|
479
|
+
if (action === 'index') {
|
|
480
|
+
const text = String(args?.content ?? '').trim()
|
|
481
|
+
if (text.length === 0) throw new Error('index 需要 content:索引全文,一句话一条')
|
|
482
|
+
setIndex(text)
|
|
483
|
+
return { done: true, detail: `索引更新好了,现在核心记忆里带着 ${text.split(/\r?\n/).length} 行目录。分类档案变了记得回来同步。` }
|
|
484
|
+
}
|
|
485
|
+
if (action === 'forget') {
|
|
486
|
+
const file = resolveTarget(args?.path)
|
|
487
|
+
const lines = readRaw(file).split(/\r?\n/)
|
|
488
|
+
const index = uniqueEntryLine(lines, args?.match)
|
|
489
|
+
const removed = lines[index].trim()
|
|
490
|
+
lines.splice(index, 1)
|
|
491
|
+
rewriteFile(file, lines)
|
|
492
|
+
return { done: true, detail: `删掉了:${removed}` }
|
|
493
|
+
}
|
|
494
|
+
throw new Error(`未知的 action "${String(action ?? '')}",可用:tree / read / move / index / forget`)
|
|
495
|
+
},
|
|
496
|
+
}),
|
|
497
|
+
'aiyayi-memory.memory_organize()',
|
|
498
|
+
)
|
|
237
499
|
}
|