dsh-macro-memory 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +506 -2
- package/cordis.patch.yml +6 -0
- package/lib/client.js +3623 -0
- package/lib/index.js +502 -0
- package/lib/macro/brand.js +82 -0
- package/lib/macro/bridge.js +158 -0
- package/lib/macro/chain.js +206 -0
- package/lib/macro/contract.js +471 -0
- package/lib/macro/cost.js +164 -0
- package/lib/macro/defs/admin.js +442 -0
- package/lib/macro/defs/dream.js +24 -0
- package/lib/macro/defs/link.js +257 -0
- package/lib/macro/defs/project.js +25 -0
- package/lib/macro/defs/read.js +160 -0
- package/lib/macro/defs/remember.js +411 -0
- package/lib/macro/defs/search.js +871 -0
- package/lib/macro/defs/update.js +161 -0
- package/lib/macro/dreamledger.js +1398 -0
- package/lib/macro/facade.js +135 -0
- package/lib/macro/fusion.js +182 -0
- package/lib/macro/linkflow.js +391 -0
- package/lib/macro/links.js +448 -0
- package/lib/macro/neighbor.js +209 -0
- package/lib/macro/panel.js +96 -0
- package/lib/macro/prompt.js +139 -0
- package/lib/macro/provenance.js +148 -0
- package/lib/macro/render.js +35 -0
- package/lib/macro/revisions.js +253 -0
- package/lib/macro/shared.js +113 -0
- package/lib/macro/store.js +103 -0
- package/lib/macro/supersede.js +197 -0
- package/lib/macro/tokencost.js +90 -0
- package/lib/macro/tools.js +67 -0
- package/package.json +54 -4
- package/vendor/VENDOR.json +23 -0
- package/vendor/dsh-memory-nebula/CONTRIBUTING.md +48 -0
- package/vendor/dsh-memory-nebula/LICENSE +21 -0
- package/vendor/dsh-memory-nebula/README.en.md +203 -0
- package/vendor/dsh-memory-nebula/README.md +253 -0
- package/vendor/dsh-memory-nebula/SECURITY.md +43 -0
- package/vendor/dsh-memory-nebula/SUPPORT.md +33 -0
- package/vendor/dsh-memory-nebula/cordis.patch.yml +4 -0
- package/vendor/dsh-memory-nebula/lib/client.js +105 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/index.js +279 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/ingest.js +230 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/meow.js +320 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/notes.js +305 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/sqlite.js +287 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/util.js +231 -0
- package/vendor/dsh-memory-nebula/lib/host/bridge.js +351 -0
- package/vendor/dsh-memory-nebula/lib/host/compress.js +97 -0
- package/vendor/dsh-memory-nebula/lib/host/demo.js +357 -0
- package/vendor/dsh-memory-nebula/lib/host/diagnostics.js +125 -0
- package/vendor/dsh-memory-nebula/lib/host/doctor.js +270 -0
- package/vendor/dsh-memory-nebula/lib/host/entities.js +230 -0
- package/vendor/dsh-memory-nebula/lib/host/graph.js +1762 -0
- package/vendor/dsh-memory-nebula/lib/host/graphcache.js +290 -0
- package/vendor/dsh-memory-nebula/lib/host/http.js +993 -0
- package/vendor/dsh-memory-nebula/lib/host/injectcfg.js +95 -0
- package/vendor/dsh-memory-nebula/lib/host/ledger.js +175 -0
- package/vendor/dsh-memory-nebula/lib/host/live.js +204 -0
- package/vendor/dsh-memory-nebula/lib/host/nebula.js +2105 -0
- package/vendor/dsh-memory-nebula/lib/host/page.js +75 -0
- package/vendor/dsh-memory-nebula/lib/host/path.js +83 -0
- package/vendor/dsh-memory-nebula/lib/host/recall.js +524 -0
- package/vendor/dsh-memory-nebula/lib/host/share.js +633 -0
- package/vendor/dsh-memory-nebula/lib/host/sources.js +266 -0
- package/vendor/dsh-memory-nebula/lib/host/store.js +333 -0
- package/vendor/dsh-memory-nebula/lib/host/text.js +410 -0
- package/vendor/dsh-memory-nebula/lib/host/transcripts.js +396 -0
- package/vendor/dsh-memory-nebula/lib/index.js +782 -0
- package/vendor/dsh-memory-nebula/lib/standalone.mjs +315 -0
- package/vendor/dsh-memory-nebula/lib/web/api.js +426 -0
- package/vendor/dsh-memory-nebula/lib/web/app.js +4172 -0
- package/vendor/dsh-memory-nebula/lib/web/i18n.js +580 -0
- package/vendor/dsh-memory-nebula/lib/web/index.html +174 -0
- package/vendor/dsh-memory-nebula/lib/web/nav.js +132 -0
- package/vendor/dsh-memory-nebula/lib/web/render.js +1890 -0
- package/vendor/dsh-memory-nebula/lib/web/space3d.js +1883 -0
- package/vendor/dsh-memory-nebula/lib/web/style.css +1402 -0
- package/vendor/dsh-memory-nebula/locale/en.json +7 -0
- package/vendor/dsh-memory-nebula/locale/zh.json +7 -0
- package/vendor/dsh-memory-nebula/market/assets/icon-180.png +0 -0
- package/vendor/dsh-memory-nebula/market/assets/icon.svg +40 -0
- package/vendor/dsh-memory-nebula/market/index.json +101 -0
- package/vendor/dsh-memory-nebula/package.json +87 -0
- package/vendor/dsh-memory-nebula/sidecar.ps1 +119 -0
- package/vendor/meow-memory/LICENSE +21 -0
- package/vendor/meow-memory/README.en.md +192 -0
- package/vendor/meow-memory/README.md +174 -0
- package/vendor/meow-memory/README.pt-br.md +180 -0
- package/vendor/meow-memory/cordis.patch.yml +7 -0
- package/vendor/meow-memory/lib/client.js +3344 -0
- package/vendor/meow-memory/lib/client.js.map +7 -0
- package/vendor/meow-memory/lib/index.js +9802 -0
- package/vendor/meow-memory/lib/index.js.map +7 -0
- package/vendor/meow-memory/lib/prompts/README.md +65 -0
- package/vendor/meow-memory/lib/prompts/en/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/en/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/en/dream-project-summary.md +12 -0
- package/vendor/meow-memory/lib/prompts/en/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/en/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/en/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/en/labels.md +98 -0
- package/vendor/meow-memory/lib/prompts/en/reflect.md +31 -0
- package/vendor/meow-memory/lib/prompts/en/system-guide.md +121 -0
- package/vendor/meow-memory/lib/prompts/en/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/en/welcome-guide.md +12 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-project-summary.md +12 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/pt-br/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/pt-br/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/pt-br/labels.md +98 -0
- package/vendor/meow-memory/lib/prompts/pt-br/reflect.md +31 -0
- package/vendor/meow-memory/lib/prompts/pt-br/system-guide.md +121 -0
- package/vendor/meow-memory/lib/prompts/pt-br/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/pt-br/welcome-guide.md +12 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-project-summary.md +19 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/zh/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/zh/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/zh/labels.md +98 -0
- package/vendor/meow-memory/lib/prompts/zh/reflect.md +30 -0
- package/vendor/meow-memory/lib/prompts/zh/system-guide.md +119 -0
- package/vendor/meow-memory/lib/prompts/zh/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/zh/welcome-guide.md +12 -0
- package/vendor/meow-memory/package.json +64 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 适配器:meow-memory
|
|
3
|
+
*
|
|
4
|
+
* 数据面:每个工作区目录下的 `.dsh-meow/memory.db`(SQLite,7 张层表)
|
|
5
|
+
* + `.dsh-meow/sessions/*.json`(每会话的活动账本:injected / searched / accessed / written)
|
|
6
|
+
*
|
|
7
|
+
* 这是星图最早、也是最深的来源:节点自带层(soul/user/project/fact/lesson/topic/rules)、
|
|
8
|
+
* 重要性、命中次数,账本还能算「谁和谁被一起点亮过」(Hebbian 共激活)。
|
|
9
|
+
*
|
|
10
|
+
* 只读:`new DatabaseSync(path, { readOnly: true })`,从不写对方目录。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readdirSync, statSync } from 'node:fs'
|
|
13
|
+
import { join, basename } from 'node:path'
|
|
14
|
+
import { homedir } from 'node:os'
|
|
15
|
+
import { DatabaseSync } from 'node:sqlite'
|
|
16
|
+
import { dshHome, readJsonSafe, readTextSafe, slugKey, toMs, toKeywordList } from './util.js'
|
|
17
|
+
import { clip } from '../text.js'
|
|
18
|
+
|
|
19
|
+
/** meow-memory 的记忆层(表名)。有顺序:越靠前越「重」。 */
|
|
20
|
+
export const LEVELS = ['soul', 'user', 'project', 'fact', 'lesson', 'topic', 'rules']
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 会话账本里哪些键才是「点亮了哪些记忆」。
|
|
24
|
+
*
|
|
25
|
+
* 真实账本长这样:{ injected: [...], searched: [...], accessed: [...], written: [...],
|
|
26
|
+
* projectsQueried: [...], reinjectPending: false, currentProject: '...' }。
|
|
27
|
+
* 四个活动键装的是记忆 id;projectsQueried 装的是项目名——当成记忆 id 去查,
|
|
28
|
+
* 真实库里 90 个 id 全部落空(探针实测),除了白算一遍没有任何作用。
|
|
29
|
+
*/
|
|
30
|
+
export const LEDGER_KINDS = new Set(['injected', 'searched', 'accessed', 'written'])
|
|
31
|
+
|
|
32
|
+
export const meta = {
|
|
33
|
+
id: 'meow',
|
|
34
|
+
name: 'meow-memory',
|
|
35
|
+
kind: 'sqlite',
|
|
36
|
+
description: '每个工作区 .dsh-meow/memory.db 里的分层记忆 + sessions/*.json 活动账本。星图的原生来源,无需配置。',
|
|
37
|
+
config: {
|
|
38
|
+
extraRoots: '额外的父目录数组:会扫描其下每个子目录,发现带 .dsh-meow/memory.db 的就算一个工作区。留空则只用工作区注册表 + 默认探针。',
|
|
39
|
+
dirName: '记忆目录名,默认 .dsh-meow',
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 读 workspace 注册表:拿到所有工作区(id / title / path)。
|
|
45
|
+
* @returns {{id:string,title:string,path:string}[]}
|
|
46
|
+
*/
|
|
47
|
+
export function readWorkspaceRegistry() {
|
|
48
|
+
const file = join(dshHome(), 'storages', 'workspace.json')
|
|
49
|
+
const data = readJsonSafe(file, null)
|
|
50
|
+
const out = []
|
|
51
|
+
const table = data?.tables?.workspaces
|
|
52
|
+
if (table && typeof table === 'object') {
|
|
53
|
+
for (const [id, rec] of Object.entries(table)) {
|
|
54
|
+
const path = typeof rec?.path === 'string' ? rec.path : ''
|
|
55
|
+
if (!path) continue
|
|
56
|
+
out.push({ id, title: String(rec?.title || basename(path)), path })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 扫描默认根:常见的工作区父目录(抓注册表漏掉的项目)。 */
|
|
63
|
+
export function defaultRoots() {
|
|
64
|
+
// 一台机器一个样子:只从「家目录」和「DSH_HOME 的兄弟目录」推,不写死任何盘符。
|
|
65
|
+
// 想多扫几处就用 NEBULA_DEFAULT_ROOTS(分号或逗号分隔),别改代码。
|
|
66
|
+
const extra = String(process.env.NEBULA_DEFAULT_ROOTS || '')
|
|
67
|
+
.split(/[;,]/)
|
|
68
|
+
.map((s) => s.trim())
|
|
69
|
+
.filter(Boolean)
|
|
70
|
+
const candidates = [
|
|
71
|
+
...extra,
|
|
72
|
+
join(dshHome(), '..', 'document'),
|
|
73
|
+
join(homedir(), 'Documents'),
|
|
74
|
+
join(homedir(), 'document'),
|
|
75
|
+
]
|
|
76
|
+
const out = []
|
|
77
|
+
const seen = new Set()
|
|
78
|
+
for (const c of candidates) {
|
|
79
|
+
const norm = String(c).replace(/[\\/]+$/, '')
|
|
80
|
+
const key = norm.toLowerCase()
|
|
81
|
+
if (!norm || seen.has(key)) continue
|
|
82
|
+
try {
|
|
83
|
+
if (!existsSync(norm)) continue
|
|
84
|
+
seen.add(key)
|
|
85
|
+
out.push(norm)
|
|
86
|
+
} catch {
|
|
87
|
+
/* ignore */
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 发现所有 meow-memory 记忆库(只读)。
|
|
95
|
+
* @param {{extraRoots?: string[], dirName?: string}} [config]
|
|
96
|
+
* @returns {object[]} store 列表
|
|
97
|
+
*/
|
|
98
|
+
export function detect(config = {}) {
|
|
99
|
+
const dirName = config.dirName || '.dsh-meow'
|
|
100
|
+
const roots = Array.isArray(config.extraRoots) && config.extraRoots.length > 0 ? config.extraRoots : defaultRoots()
|
|
101
|
+
const workspaces = readWorkspaceRegistry()
|
|
102
|
+
const seen = new Set()
|
|
103
|
+
const stores = []
|
|
104
|
+
|
|
105
|
+
const consider = (wsPath, title, id, via) => {
|
|
106
|
+
const norm = String(wsPath || '').replace(/[\\/]+$/, '')
|
|
107
|
+
if (!norm || seen.has(norm.toLowerCase())) return
|
|
108
|
+
seen.add(norm.toLowerCase())
|
|
109
|
+
const dir = join(norm, dirName)
|
|
110
|
+
const dbPath = join(dir, 'memory.db')
|
|
111
|
+
if (!existsSync(dbPath)) return
|
|
112
|
+
let size = 0
|
|
113
|
+
let mtimeMs = 0
|
|
114
|
+
try {
|
|
115
|
+
const st = statSync(dbPath)
|
|
116
|
+
size = st.size
|
|
117
|
+
mtimeMs = st.mtimeMs
|
|
118
|
+
} catch {
|
|
119
|
+
/* 有文件但 stat 不到:仍然收进来,读的时候再失败一次 */
|
|
120
|
+
}
|
|
121
|
+
stores.push({
|
|
122
|
+
id: 'meow:' + (id || norm),
|
|
123
|
+
provider: 'meow-memory',
|
|
124
|
+
adapter: 'meow',
|
|
125
|
+
title: String(title || basename(norm)),
|
|
126
|
+
path: norm,
|
|
127
|
+
key: slugKey(title || basename(norm), 'ws'),
|
|
128
|
+
dbPath,
|
|
129
|
+
sessionsDir: join(dir, 'sessions'),
|
|
130
|
+
size,
|
|
131
|
+
mtimeMs,
|
|
132
|
+
via,
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
for (const ws of workspaces) consider(ws.path, ws.title, ws.id, 'registry')
|
|
137
|
+
|
|
138
|
+
// 兜底:注册表里没有、但目录里确实有记忆库的,也收进来
|
|
139
|
+
for (const root of roots) {
|
|
140
|
+
try {
|
|
141
|
+
if (!existsSync(root)) continue
|
|
142
|
+
for (const name of readdirSyncSafe(root)) {
|
|
143
|
+
const p = join(root, name)
|
|
144
|
+
try {
|
|
145
|
+
if (statSync(p).isDirectory()) consider(p, name, p, 'scan')
|
|
146
|
+
} catch {
|
|
147
|
+
/* ignore */
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} catch {
|
|
151
|
+
/* ignore */
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
stores.sort((a, b) => b.size - a.size)
|
|
156
|
+
return stores
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** readdir 的容错包装。 */
|
|
160
|
+
function readdirSyncSafe(dir) {
|
|
161
|
+
try {
|
|
162
|
+
return readdirSync(dir)
|
|
163
|
+
} catch {
|
|
164
|
+
return []
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 容忍老库缺列的读取器。 */
|
|
169
|
+
function columnsOf(db, table) {
|
|
170
|
+
try {
|
|
171
|
+
return new Set(db.prepare(`PRAGMA table_info(${table})`).all().map((c) => String(c.name)))
|
|
172
|
+
} catch {
|
|
173
|
+
return new Set()
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 只读打开一个 meow-memory 库并抽出全部记忆。
|
|
179
|
+
* @param {object} store
|
|
180
|
+
* @returns {object[]} 记忆节点(未做 token/实体计算)
|
|
181
|
+
*/
|
|
182
|
+
export function* scanSteps(store) {
|
|
183
|
+
const nodes = []
|
|
184
|
+
let db = null
|
|
185
|
+
try {
|
|
186
|
+
db = new DatabaseSync(store.dbPath, { readOnly: true })
|
|
187
|
+
} catch {
|
|
188
|
+
return { nodes, ledgers: [], error: '打不开 ' + basename(store.dbPath) }
|
|
189
|
+
}
|
|
190
|
+
// 文件能打开不代表能用:空文件 / 被截断 / 根本不是 SQLite 时,
|
|
191
|
+
// PRAGMA 会一律失败,而 columnsOf 会把「读不动」当成「没有这张表」,
|
|
192
|
+
// 于是这个来源安静地显示 0 条。这里先用 sqlite_master 探一次,
|
|
193
|
+
// 坏库就带着原文错误返回,让诊断里能看见到底是谁坏了。
|
|
194
|
+
try {
|
|
195
|
+
db.prepare('SELECT count(*) AS n FROM sqlite_master').get()
|
|
196
|
+
} catch (err) {
|
|
197
|
+
try {
|
|
198
|
+
db.close()
|
|
199
|
+
} catch {
|
|
200
|
+
/* ignore */
|
|
201
|
+
}
|
|
202
|
+
const why = String(err?.message || err).replace(/\s+/g, ' ').trim().slice(0, 160)
|
|
203
|
+
return { nodes, ledgers: [], error: `读不动 ${basename(store.dbPath)}:${why}` }
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
for (const level of LEVELS) {
|
|
207
|
+
const cols = columnsOf(db, level)
|
|
208
|
+
if (cols.size === 0) continue
|
|
209
|
+
let rows
|
|
210
|
+
try {
|
|
211
|
+
// iterate() 而不是 all():all() 会先把 12,500 行一次拉进内存(实测 59ms
|
|
212
|
+
// 一个库不让路),iterate() 是取一行给一行,可以在行与行之间让路。
|
|
213
|
+
rows = db.prepare(`SELECT * FROM ${level}`).iterate()
|
|
214
|
+
} catch {
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
let batch = 0
|
|
218
|
+
for (const row of rows) {
|
|
219
|
+
if ((++batch & 511) === 0) yield 'rows'
|
|
220
|
+
const rawId = String(row.id ?? '')
|
|
221
|
+
if (!rawId) continue
|
|
222
|
+
const keywords = toKeywordList(row.keywords)
|
|
223
|
+
const title = row.title === null || row.title === undefined ? '' : String(row.title)
|
|
224
|
+
const content = row.content === null || row.content === undefined ? '' : String(row.content)
|
|
225
|
+
const project = row.project === null || row.project === undefined ? '' : String(row.project)
|
|
226
|
+
nodes.push({
|
|
227
|
+
id: `meow:${store.key}:${level}:${rawId}`,
|
|
228
|
+
uid: rawId,
|
|
229
|
+
provider: 'meow-memory',
|
|
230
|
+
adapter: 'meow',
|
|
231
|
+
storeId: store.id,
|
|
232
|
+
ws: store.key,
|
|
233
|
+
wsTitle: store.title,
|
|
234
|
+
wsPath: store.path,
|
|
235
|
+
level,
|
|
236
|
+
title: title.trim(),
|
|
237
|
+
label: clip(title.trim() || content, 42),
|
|
238
|
+
content,
|
|
239
|
+
importance: Number(row.importance) || 0,
|
|
240
|
+
status: String(row.status ?? 'active'),
|
|
241
|
+
project: project.trim(),
|
|
242
|
+
subcategory: cols.has('subcategory') ? String(row.subcategory ?? '').trim() : '',
|
|
243
|
+
goal: cols.has('goal') ? String(row.goal ?? '').trim() : '',
|
|
244
|
+
corrected: cols.has('corrected') ? Number(row.corrected) === 1 : false,
|
|
245
|
+
keywords,
|
|
246
|
+
sessionId: String(row.source_session ?? ''),
|
|
247
|
+
hits: Number(row.hit_count) || 0,
|
|
248
|
+
// [macro-fusion 0.4.0] 真被 memory_read 打开过几次(老库没这列 → 0)。
|
|
249
|
+
// 体检的 unused 判的是「召回过但没人读」,所以这两个数必须分开传上来。
|
|
250
|
+
reads: cols.has('read_count') ? Number(row.read_count) || 0 : 0,
|
|
251
|
+
createdAt: toMs(row.created_at),
|
|
252
|
+
updatedAt: toMs(row.updated_at),
|
|
253
|
+
accessedAt: toMs(row.last_accessed_at),
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} finally {
|
|
258
|
+
try {
|
|
259
|
+
db.close()
|
|
260
|
+
} catch {
|
|
261
|
+
/* ignore */
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const ledgers = readSessionLedgers(store.sessionsDir).map((row) => ({ ...row, ws: store.key, storeId: store.id }))
|
|
265
|
+
return { nodes, ledgers }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 同步壳:跑完 scanSteps 拿到结果。
|
|
270
|
+
*
|
|
271
|
+
* 单测与 MCP 工具走这条(它们不在乎让路);HTTP 那条路走 scanSteps,
|
|
272
|
+
* 靠适配器层在每个 yield 后 setImmediate。
|
|
273
|
+
*/
|
|
274
|
+
export function scan(store) {
|
|
275
|
+
const it = scanSteps(store)
|
|
276
|
+
let step = it.next()
|
|
277
|
+
while (!step.done) step = it.next()
|
|
278
|
+
return step.value
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 读 meow-memory 每会话的活动账本(injected / searched / written ...)。
|
|
283
|
+
* 这些账本是「谁和谁一起被点亮过」的原始证据,用来做 Hebbian 共激活连边。
|
|
284
|
+
* @param {string} sessionsDir
|
|
285
|
+
*/
|
|
286
|
+
export function readSessionLedgers(sessionsDir) {
|
|
287
|
+
const out = []
|
|
288
|
+
if (!sessionsDir || !existsSync(sessionsDir)) return out
|
|
289
|
+
let names
|
|
290
|
+
try {
|
|
291
|
+
names = readdirSync(sessionsDir)
|
|
292
|
+
} catch {
|
|
293
|
+
return out
|
|
294
|
+
}
|
|
295
|
+
for (const name of names) {
|
|
296
|
+
if (!name.endsWith('.json')) continue
|
|
297
|
+
const rec = readJsonSafe(join(sessionsDir, name), null)
|
|
298
|
+
if (!rec || typeof rec !== 'object') continue
|
|
299
|
+
const sessionId = name.replace(/\.json$/, '')
|
|
300
|
+
const currentProject = typeof rec.currentProject === 'string' ? rec.currentProject : ''
|
|
301
|
+
for (const [kind, value] of Object.entries(rec)) {
|
|
302
|
+
if (!LEDGER_KINDS.has(kind)) continue
|
|
303
|
+
if (!Array.isArray(value)) continue
|
|
304
|
+
const ids = value.filter((x) => typeof x === 'string' && x.length > 0)
|
|
305
|
+
if (ids.length === 0) continue
|
|
306
|
+
out.push({ sessionId, kind, ids, currentProject })
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return out
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** 探测信息(给 nebula_sources 用)。 */
|
|
313
|
+
export function probe(config = {}) {
|
|
314
|
+
const stores = detect(config)
|
|
315
|
+
return {
|
|
316
|
+
ok: stores.length > 0,
|
|
317
|
+
stores: stores.map((s) => ({ id: s.id, title: s.title, db: s.dbPath, size: s.size })),
|
|
318
|
+
note: stores.length ? `${stores.length} 个工作区记忆库,只读` : '没有找到 .dsh-meow/memory.db',
|
|
319
|
+
}
|
|
320
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 适配器:notes —— 通用「文件即记忆」来源
|
|
3
|
+
*
|
|
4
|
+
* 吃这几种落盘形态:
|
|
5
|
+
* · Markdown(.md / .markdown)—— frontmatter 认 title / tags / level / project / importance;
|
|
6
|
+
* hindsight 的知识页、Obsidian 库、日记、openviking 导出的 md 全在这一类。
|
|
7
|
+
* · JSON —— 整份是一个数组,或对象里有 items / pages / memories / entries 之一。
|
|
8
|
+
* · JSONL —— 一行一个对象(各种 agent 的日志式记忆)。
|
|
9
|
+
* · 纯文本(.txt)—— 一个文件当一条,标题取文件名。
|
|
10
|
+
*
|
|
11
|
+
* 只读。目录不存在就返回空,不报错。
|
|
12
|
+
*/
|
|
13
|
+
import { statSync } from 'node:fs'
|
|
14
|
+
import {
|
|
15
|
+
dirExists,
|
|
16
|
+
firstLine,
|
|
17
|
+
hashShort,
|
|
18
|
+
parseFrontmatter,
|
|
19
|
+
readJsonSafe,
|
|
20
|
+
readTextSafe,
|
|
21
|
+
slugKey,
|
|
22
|
+
stem,
|
|
23
|
+
toKeywordList,
|
|
24
|
+
toMs,
|
|
25
|
+
walkFiles,
|
|
26
|
+
} from './util.js'
|
|
27
|
+
|
|
28
|
+
const DEFAULT_EXTS = ['.md', '.markdown', '.json', '.jsonl', '.txt']
|
|
29
|
+
const ARRAY_HINTS = ['items', 'pages', 'memories', 'entries', 'records', 'notes', 'data']
|
|
30
|
+
|
|
31
|
+
export const meta = {
|
|
32
|
+
id: 'notes',
|
|
33
|
+
name: '文件笔记',
|
|
34
|
+
kind: 'files',
|
|
35
|
+
description:
|
|
36
|
+
'把一个目录当成记忆来源:每个 md / json / jsonl / txt 文件(或 JSON 数组里的每一条)算一条记忆。适合 hindsight 知识页、openviking 导出、Obsidian/日记目录。',
|
|
37
|
+
config: {
|
|
38
|
+
path: '必填。目录绝对路径(支持 ~ 开头)。',
|
|
39
|
+
title: '展示名;默认取目录名。',
|
|
40
|
+
level: '默认层级,默认 note。',
|
|
41
|
+
levelField: '从 frontmatter 读层级的字段名,默认 level(也认 type / kind)。',
|
|
42
|
+
titleField: '标题字段名,默认 title(也认 name / heading)。',
|
|
43
|
+
keywordsField: '关键词字段名,默认 tags(也认 keywords)。',
|
|
44
|
+
importanceField: '重要性字段名,默认 importance。',
|
|
45
|
+
exts: '扩展名数组,默认 [.md,.markdown,.json,.jsonl,.txt]。',
|
|
46
|
+
maxFiles: '最多读多少个文件,默认 4000。',
|
|
47
|
+
maxDepth: '目录递归深度,默认 4。',
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 配置里 path 允许写 ~。 */
|
|
52
|
+
function expandHome(p) {
|
|
53
|
+
const raw = String(p || '').trim()
|
|
54
|
+
if (!raw) return ''
|
|
55
|
+
if (raw === '~') return process.env.USERPROFILE || process.env.HOME || raw
|
|
56
|
+
if (raw.startsWith('~/') || raw.startsWith('~\\')) {
|
|
57
|
+
const home = process.env.USERPROFILE || process.env.HOME || ''
|
|
58
|
+
return home ? home + raw.slice(1) : raw
|
|
59
|
+
}
|
|
60
|
+
return raw
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 探测:只看配置;没配 path 就不产出(notes 无法盲猜)。
|
|
65
|
+
* @param {object} config 单个适配器配置
|
|
66
|
+
*/
|
|
67
|
+
export function detect(config = {}) {
|
|
68
|
+
const path = expandHome(config.path)
|
|
69
|
+
if (!path || !dirExists(path)) return []
|
|
70
|
+
const exts = Array.isArray(config.exts) && config.exts.length ? config.exts : DEFAULT_EXTS
|
|
71
|
+
let size = 0
|
|
72
|
+
let mtimeMs = 0
|
|
73
|
+
let count = 0
|
|
74
|
+
const cap = Math.min(20000, Number(config.maxFiles) || 4000)
|
|
75
|
+
const stats = {}
|
|
76
|
+
const files = walkFiles(path, {
|
|
77
|
+
exts,
|
|
78
|
+
maxDepth: Number(config.maxDepth) || 4,
|
|
79
|
+
maxFiles: cap,
|
|
80
|
+
stats,
|
|
81
|
+
})
|
|
82
|
+
for (const f of files) {
|
|
83
|
+
size += f.size
|
|
84
|
+
count += 1
|
|
85
|
+
mtimeMs = Math.max(mtimeMs, Math.round(f.mtimeMs))
|
|
86
|
+
}
|
|
87
|
+
if (count === 0) return []
|
|
88
|
+
const title = String(config.title || '').trim() || path.split(/[\\/]/).filter(Boolean).pop() || 'notes'
|
|
89
|
+
return [
|
|
90
|
+
{
|
|
91
|
+
id: 'notes:' + (config.id || slugKey(title)),
|
|
92
|
+
provider: String(config.provider || config.id || 'notes'),
|
|
93
|
+
adapter: 'notes',
|
|
94
|
+
title,
|
|
95
|
+
path,
|
|
96
|
+
key: slugKey(title, 'notes'),
|
|
97
|
+
size,
|
|
98
|
+
mtimeMs,
|
|
99
|
+
fileCount: count,
|
|
100
|
+
// 爬到上限就停了:剩下的文件没进图,界面要说出来(1.11.40)
|
|
101
|
+
truncated: Boolean(stats.truncated),
|
|
102
|
+
maxFiles: cap,
|
|
103
|
+
config,
|
|
104
|
+
},
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 从 frontmatter / 对象里挑第一个存在的字段。 */
|
|
109
|
+
function pickField(obj, names, fallback = '') {
|
|
110
|
+
for (const name of names) {
|
|
111
|
+
const v = obj?.[name]
|
|
112
|
+
if (v === undefined || v === null || v === '') continue
|
|
113
|
+
return v
|
|
114
|
+
}
|
|
115
|
+
return fallback
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 扫描一个 notes store。
|
|
120
|
+
* @param {object} store
|
|
121
|
+
*/
|
|
122
|
+
export function scan(store) {
|
|
123
|
+
const config = store.config || {}
|
|
124
|
+
const exts = Array.isArray(config.exts) && config.exts.length ? config.exts : DEFAULT_EXTS
|
|
125
|
+
const stats = {}
|
|
126
|
+
const files = walkFiles(store.path, {
|
|
127
|
+
exts,
|
|
128
|
+
maxDepth: Number(config.maxDepth) || 4,
|
|
129
|
+
maxFiles: Math.min(20000, Number(config.maxFiles) || 4000),
|
|
130
|
+
stats,
|
|
131
|
+
})
|
|
132
|
+
const nodes = []
|
|
133
|
+
for (const file of files) {
|
|
134
|
+
try {
|
|
135
|
+
const ext = file.file.toLowerCase().split('.').pop()
|
|
136
|
+
if (ext === 'json') nodes.push(...readJsonNodes(store, config, file))
|
|
137
|
+
else if (ext === 'jsonl') nodes.push(...readJsonlNodes(store, config, file))
|
|
138
|
+
else nodes.push(readNoteNode(store, config, file))
|
|
139
|
+
} catch {
|
|
140
|
+
/* 单个文件坏了不影响别的 */
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { nodes, ledgers: [], truncated: Boolean(stats.truncated) }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 造一个标准节点。 */
|
|
147
|
+
function makeNode(store, config, file, index, fields) {
|
|
148
|
+
const level = String(fields.level || config.level || 'note').trim() || 'note'
|
|
149
|
+
const content = String(fields.content || '').trim()
|
|
150
|
+
const title = String(fields.title || '').trim() || firstLine(content, 46) || stem(file.rel)
|
|
151
|
+
const uid = hashShort(file.rel + '#' + index)
|
|
152
|
+
return {
|
|
153
|
+
id: `notes:${store.key}:${level}:${uid}`,
|
|
154
|
+
uid,
|
|
155
|
+
provider: store.provider,
|
|
156
|
+
adapter: 'notes',
|
|
157
|
+
storeId: store.id,
|
|
158
|
+
ws: store.key,
|
|
159
|
+
wsTitle: store.title,
|
|
160
|
+
wsPath: store.path,
|
|
161
|
+
level,
|
|
162
|
+
title,
|
|
163
|
+
label: title.length > 42 ? title.slice(0, 42) + '…' : title,
|
|
164
|
+
content,
|
|
165
|
+
importance: Math.max(0, Math.min(4, Number(fields.importance) || 0)),
|
|
166
|
+
status: String(fields.status || 'active'),
|
|
167
|
+
project: String(fields.project || '').trim(),
|
|
168
|
+
subcategory: String(fields.subcategory || '').trim(),
|
|
169
|
+
goal: String(fields.goal || ''),
|
|
170
|
+
corrected: false,
|
|
171
|
+
keywords: toKeywordList(fields.keywords),
|
|
172
|
+
sessionId: String(fields.sessionId || ''),
|
|
173
|
+
hits: Number(fields.hits) || 0,
|
|
174
|
+
createdAt: toMs(fields.createdAt) || Math.round(file.mtimeMs),
|
|
175
|
+
updatedAt: toMs(fields.updatedAt) || Math.round(file.mtimeMs),
|
|
176
|
+
accessedAt: 0,
|
|
177
|
+
file: file.rel,
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 单个 md / txt 文件 → 一个节点。 */
|
|
182
|
+
function readNoteNode(store, config, file) {
|
|
183
|
+
const text = readTextSafe(file.file, 400_000)
|
|
184
|
+
const { data, body } = parseFrontmatter(text)
|
|
185
|
+
const level = pickField(data, [config.levelField || 'level', 'type', 'kind'], config.level || 'note')
|
|
186
|
+
const title = pickField(data, [config.titleField || 'title', 'name', 'heading'], '')
|
|
187
|
+
const keywords = pickField(data, [config.keywordsField || 'tags', 'keywords', 'tag'], '')
|
|
188
|
+
return makeNode(store, config, file, 0, {
|
|
189
|
+
level,
|
|
190
|
+
title: title || stem(file.rel),
|
|
191
|
+
content: body.trim() || text,
|
|
192
|
+
keywords,
|
|
193
|
+
importance: pickField(data, [config.importanceField || 'importance', 'weight', 'priority'], 0),
|
|
194
|
+
status: data.status || 'active',
|
|
195
|
+
project: data.project || '',
|
|
196
|
+
createdAt: pickField(data, ['created', 'createdAt', 'date'], 0),
|
|
197
|
+
updatedAt: pickField(data, ['updated', 'updatedAt', 'modified'], 0),
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** JSON:整份数组 / 或对象里带一个数组字段 → 每条一个节点。 */
|
|
202
|
+
function readJsonNodes(store, config, file) {
|
|
203
|
+
const data = readJsonSafe(file.file, null)
|
|
204
|
+
if (!data) return []
|
|
205
|
+
let list = null
|
|
206
|
+
if (Array.isArray(data)) list = data
|
|
207
|
+
else if (data && typeof data === 'object') {
|
|
208
|
+
for (const key of ARRAY_HINTS) {
|
|
209
|
+
if (Array.isArray(data[key])) {
|
|
210
|
+
list = data[key]
|
|
211
|
+
break
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
216
|
+
// 单个对象也当一条记忆(很多导出是 {title, content} 一件一文件)
|
|
217
|
+
if (data && typeof data === 'object' && !Array.isArray(data)) {
|
|
218
|
+
return [
|
|
219
|
+
makeNode(store, config, file, 0, {
|
|
220
|
+
level: pickField(data, [config.levelField || 'level', 'type', 'kind'], config.level || 'note'),
|
|
221
|
+
title: pickField(data, [config.titleField || 'title', 'name', 'heading'], stem(file.rel)),
|
|
222
|
+
content: pickField(data, ['content', 'text', 'body', 'summary', 'value'], ''),
|
|
223
|
+
keywords: pickField(data, [config.keywordsField || 'tags', 'keywords'], ''),
|
|
224
|
+
importance: pickField(data, ['importance', 'weight', 'priority'], 0),
|
|
225
|
+
status: data.status || 'active',
|
|
226
|
+
project: data.project || '',
|
|
227
|
+
createdAt: pickField(data, ['created', 'createdAt', 'date', 'ts'], 0),
|
|
228
|
+
updatedAt: pickField(data, ['updated', 'updatedAt', 'modified'], 0),
|
|
229
|
+
}),
|
|
230
|
+
]
|
|
231
|
+
}
|
|
232
|
+
return []
|
|
233
|
+
}
|
|
234
|
+
const out = []
|
|
235
|
+
const cap = Math.min(list.length, 5000)
|
|
236
|
+
for (let i = 0; i < cap; i++) {
|
|
237
|
+
const row = list[i]
|
|
238
|
+
if (!row || typeof row !== 'object') continue
|
|
239
|
+
const content = pickField(row, ['content', 'text', 'body', 'summary', 'value', 'detail'], '')
|
|
240
|
+
if (!content && !pickField(row, ['title', 'name'], '')) continue
|
|
241
|
+
out.push(
|
|
242
|
+
makeNode(store, config, file, i, {
|
|
243
|
+
level: pickField(row, [config.levelField || 'level', 'type', 'kind'], config.level || 'note'),
|
|
244
|
+
title: pickField(row, [config.titleField || 'title', 'name', 'heading'], ''),
|
|
245
|
+
content,
|
|
246
|
+
keywords: pickField(row, [config.keywordsField || 'tags', 'keywords'], ''),
|
|
247
|
+
importance: pickField(row, ['importance', 'weight', 'priority'], 0),
|
|
248
|
+
status: row.status || 'active',
|
|
249
|
+
project: row.project || '',
|
|
250
|
+
createdAt: pickField(row, ['created', 'createdAt', 'date', 'ts', 'timestamp'], 0),
|
|
251
|
+
updatedAt: pickField(row, ['updated', 'updatedAt', 'modified'], 0),
|
|
252
|
+
}),
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
return out
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** JSONL:一行一条。 */
|
|
259
|
+
function readJsonlNodes(store, config, file) {
|
|
260
|
+
const text = readTextSafe(file.file, 4_000_000)
|
|
261
|
+
if (!text) return []
|
|
262
|
+
const out = []
|
|
263
|
+
const lines = text.split(/\r?\n/)
|
|
264
|
+
const cap = Math.min(lines.length, 5000)
|
|
265
|
+
for (let i = 0; i < cap; i++) {
|
|
266
|
+
const line = lines[i].trim()
|
|
267
|
+
if (!line || line.startsWith('//')) continue
|
|
268
|
+
let row
|
|
269
|
+
try {
|
|
270
|
+
row = JSON.parse(line)
|
|
271
|
+
} catch {
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
if (!row || typeof row !== 'object') continue
|
|
275
|
+
const content = pickField(row, ['content', 'text', 'body', 'summary', 'message', 'value'], '')
|
|
276
|
+
if (!content && !pickField(row, ['title', 'name'], '')) continue
|
|
277
|
+
out.push(
|
|
278
|
+
makeNode(store, config, file, i, {
|
|
279
|
+
level: pickField(row, [config.levelField || 'level', 'type', 'kind'], config.level || 'note'),
|
|
280
|
+
title: pickField(row, [config.titleField || 'title', 'name'], ''),
|
|
281
|
+
content,
|
|
282
|
+
keywords: pickField(row, [config.keywordsField || 'tags', 'keywords'], ''),
|
|
283
|
+
importance: pickField(row, ['importance', 'weight'], 0),
|
|
284
|
+
status: row.status || 'active',
|
|
285
|
+
project: row.project || '',
|
|
286
|
+
createdAt: pickField(row, ['created', 'createdAt', 'date', 'ts', 'timestamp'], 0),
|
|
287
|
+
updatedAt: pickField(row, ['updated', 'updatedAt'], 0),
|
|
288
|
+
}),
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
return out
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function probe(config = {}) {
|
|
295
|
+
const stores = detect(config)
|
|
296
|
+
if (stores.length === 0) {
|
|
297
|
+
return { ok: false, note: '目录不存在或里面没有可读的 md/json/jsonl/txt:' + (config.path || '(未配置 path)') }
|
|
298
|
+
}
|
|
299
|
+
const st = stores[0]
|
|
300
|
+
return {
|
|
301
|
+
ok: true,
|
|
302
|
+
stores: stores.map((s) => ({ id: s.id, title: s.title, path: s.path, files: s.fileCount, size: s.size })),
|
|
303
|
+
note: `${st.fileCount} 个文件,只读`,
|
|
304
|
+
}
|
|
305
|
+
}
|