dsh-macro-memory 0.0.0-stage → 0.3.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 +457 -2
- package/cordis.patch.yml +6 -0
- package/lib/client.js +3623 -0
- package/lib/index.js +469 -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 +439 -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 +145 -0
- package/lib/macro/defs/remember.js +213 -0
- package/lib/macro/defs/search.js +661 -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/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 +317 -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/graph.js +1663 -0
- package/vendor/dsh-memory-nebula/lib/host/graphcache.js +282 -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 +451 -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 +759 -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 +4159 -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 +9567 -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,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 记忆来源发现与只读抽取。
|
|
3
|
+
*
|
|
4
|
+
* 铁律:本文件只读别的插件写下的东西,绝不写入它们的目录。
|
|
5
|
+
* 所有可能失败的地方都吞掉异常 —— 星图挂了不能影响宿主。
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
8
|
+
import { join, basename } from 'node:path'
|
|
9
|
+
import { clip } from './text.js'
|
|
10
|
+
import { dshHome } from './adapters/util.js'
|
|
11
|
+
|
|
12
|
+
/** meow-memory 的记忆层(表名)。 */
|
|
13
|
+
export const LEVELS = ['soul', 'user', 'project', 'fact', 'lesson', 'topic', 'rules']
|
|
14
|
+
|
|
15
|
+
/** 每一层在星图里的语义(给 UI 用)。 */
|
|
16
|
+
export const LEVEL_META = {
|
|
17
|
+
soul: { label: '灵魂', zh: 'AI 自身', rank: 6 },
|
|
18
|
+
user: { label: '主人', zh: '用户画像', rank: 5 },
|
|
19
|
+
project: { label: '项目', zh: '项目档案', rank: 4 },
|
|
20
|
+
lesson: { label: '教训', zh: '经验', rank: 3 },
|
|
21
|
+
topic: { label: '话题', zh: '事线', rank: 3 },
|
|
22
|
+
fact: { label: '事实', zh: '碎事实', rank: 2 },
|
|
23
|
+
rules: { label: '准则', zh: '行为准则', rank: 5 },
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** DSH 家目录 —— 只从 adapters/util.js 取一处定义,这里转出去给老调用方。 */
|
|
27
|
+
export { dshHome }
|
|
28
|
+
|
|
29
|
+
/** 星图自己的数据目录(唯一我们写的地方)。 */
|
|
30
|
+
export function nebulaHome() {
|
|
31
|
+
const dir = join(dshHome(), 'memory-nebula')
|
|
32
|
+
return dir
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 安全读 JSON(Windows 上这些文件常带 BOM,JSON.parse 会直接炸)。 */
|
|
36
|
+
function readJsonFile(file, fallback = null) {
|
|
37
|
+
try {
|
|
38
|
+
if (!existsSync(file)) return fallback
|
|
39
|
+
const text = readFileSync(file, 'utf8').replace(/^\uFEFF/, '')
|
|
40
|
+
if (!text.trim()) return fallback
|
|
41
|
+
return JSON.parse(text)
|
|
42
|
+
} catch {
|
|
43
|
+
return fallback
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 读 workspace 注册表:拿到所有工作区(id / title / path)。
|
|
49
|
+
* @returns {{id:string,title:string,path:string}[]}
|
|
50
|
+
*/
|
|
51
|
+
export function readWorkspaceRegistry() {
|
|
52
|
+
const file = join(dshHome(), 'storages', 'workspace.json')
|
|
53
|
+
const data = readJsonFile(file, null)
|
|
54
|
+
const out = []
|
|
55
|
+
const table = data?.tables?.workspaces
|
|
56
|
+
if (table && typeof table === 'object') {
|
|
57
|
+
for (const [id, rec] of Object.entries(table)) {
|
|
58
|
+
const path = typeof rec?.path === 'string' ? rec.path : ''
|
|
59
|
+
if (!path) continue
|
|
60
|
+
out.push({ id, title: String(rec?.title || basename(path)), path })
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return out
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 工作区短键:给 UI 颜色、以及稳定的 node 前缀用。
|
|
68
|
+
* @param {string} wsPath
|
|
69
|
+
* @param {string} title
|
|
70
|
+
*/
|
|
71
|
+
export function workspaceKey(wsPath, title) {
|
|
72
|
+
const t = String(title || basename(wsPath) || wsPath)
|
|
73
|
+
return t.replace(/\s+/g, '').slice(0, 24)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 发现所有 meow-memory 记忆库(只读)。
|
|
78
|
+
* 扫描范围:注册表里的工作区 + DSH_HOME 自身,外加可选的额外根目录。
|
|
79
|
+
* @param {{extraRoots?: string[]}} [opts]
|
|
80
|
+
* @returns {{id:string,title:string,path:string,key:string,dbPath:string,sessionsDir:string,size:number,mtimeMs:number}[]}
|
|
81
|
+
*/
|
|
82
|
+
export function discoverStores(opts = {}) {
|
|
83
|
+
const workspaces = readWorkspaceRegistry()
|
|
84
|
+
const seen = new Set()
|
|
85
|
+
const stores = []
|
|
86
|
+
|
|
87
|
+
const consider = (wsPath, title, id) => {
|
|
88
|
+
const norm = String(wsPath).replace(/[\\/]+$/, '')
|
|
89
|
+
if (!norm || seen.has(norm.toLowerCase())) return
|
|
90
|
+
seen.add(norm.toLowerCase())
|
|
91
|
+
const dir = join(norm, '.dsh-meow')
|
|
92
|
+
const dbPath = join(dir, 'memory.db')
|
|
93
|
+
if (!existsSync(dbPath)) return
|
|
94
|
+
let size = 0
|
|
95
|
+
let mtimeMs = 0
|
|
96
|
+
try {
|
|
97
|
+
const st = statSync(dbPath)
|
|
98
|
+
size = st.size
|
|
99
|
+
mtimeMs = st.mtimeMs
|
|
100
|
+
} catch {
|
|
101
|
+
/* 文件在但读不到 stat 就跳过 */
|
|
102
|
+
}
|
|
103
|
+
stores.push({
|
|
104
|
+
id: id || norm,
|
|
105
|
+
title: String(title || basename(norm)),
|
|
106
|
+
path: norm,
|
|
107
|
+
key: workspaceKey(norm, title),
|
|
108
|
+
dbPath,
|
|
109
|
+
sessionsDir: join(dir, 'sessions'),
|
|
110
|
+
size,
|
|
111
|
+
mtimeMs,
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const ws of workspaces) consider(ws.path, ws.title, ws.id)
|
|
116
|
+
|
|
117
|
+
// 兜底:注册表里没有、但工作区目录里确实有记忆库的,也收进来
|
|
118
|
+
for (const root of opts.extraRoots || []) {
|
|
119
|
+
try {
|
|
120
|
+
if (!existsSync(root)) continue
|
|
121
|
+
for (const name of readdirSync(root)) {
|
|
122
|
+
const p = join(root, name)
|
|
123
|
+
try {
|
|
124
|
+
if (statSync(p).isDirectory()) consider(p, name, p)
|
|
125
|
+
} catch {
|
|
126
|
+
/* ignore */
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
/* ignore */
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
stores.sort((a, b) => b.size - a.size)
|
|
135
|
+
return stores
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 容忍老库缺列的读取器。 */
|
|
139
|
+
function columnsOf(db, table) {
|
|
140
|
+
try {
|
|
141
|
+
return new Set(db.prepare(`PRAGMA table_info(${table})`).all().map((c) => String(c.name)))
|
|
142
|
+
} catch {
|
|
143
|
+
return new Set()
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 读 meow-memory 每会话的活动账本(injected / searched / written ...)。
|
|
149
|
+
* 这些账本是「谁和谁一起被点亮过」的原始证据,用来做 Hebbian 共激活连边。
|
|
150
|
+
* @param {string} sessionsDir
|
|
151
|
+
* @returns {{sessionId:string, kind:string, ids:string[], currentProject:string}[]}
|
|
152
|
+
*/
|
|
153
|
+
export function readSessionLedgers(sessionsDir) {
|
|
154
|
+
const out = []
|
|
155
|
+
if (!sessionsDir || !existsSync(sessionsDir)) return out
|
|
156
|
+
let names
|
|
157
|
+
try {
|
|
158
|
+
names = readdirSync(sessionsDir)
|
|
159
|
+
} catch {
|
|
160
|
+
return out
|
|
161
|
+
}
|
|
162
|
+
for (const name of names) {
|
|
163
|
+
if (!name.endsWith('.json')) continue
|
|
164
|
+
const rec = readJsonFile(join(sessionsDir, name), null)
|
|
165
|
+
if (!rec || typeof rec !== 'object') continue
|
|
166
|
+
const sessionId = name.replace(/\.json$/, '')
|
|
167
|
+
const currentProject = typeof rec.currentProject === 'string' ? rec.currentProject : ''
|
|
168
|
+
for (const [kind, value] of Object.entries(rec)) {
|
|
169
|
+
if (!Array.isArray(value)) continue
|
|
170
|
+
const ids = value.filter((x) => typeof x === 'string' && x.length > 0)
|
|
171
|
+
if (ids.length === 0) continue
|
|
172
|
+
out.push({ sessionId, kind, ids, currentProject })
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return out
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 安装了什么插件 / 技能 / MCP —— 星图的「记录者」图层。
|
|
180
|
+
* 只读 profile 与 skills 目录,不动它们。
|
|
181
|
+
* @returns {{plugins:{name:string,version:string,bundle:boolean,local:boolean}[], skills:{name:string,description:string}[], mcp:{name:string,transport:string}[]}}
|
|
182
|
+
*/
|
|
183
|
+
export function readInventory() {
|
|
184
|
+
const inventory = { plugins: [], skills: [], mcp: [] }
|
|
185
|
+
|
|
186
|
+
// 1) profile 里选中的 bundle + 依赖
|
|
187
|
+
const profileDir = join(dshHome(), 'profiles', 'desktop')
|
|
188
|
+
const pkg = readJsonFile(join(profileDir, 'package.json'), null)
|
|
189
|
+
const bundles = Array.isArray(pkg?.dsh?.profile?.bundles) ? pkg.dsh.profile.bundles : []
|
|
190
|
+
const deps = pkg?.dependencies && typeof pkg.dependencies === 'object' ? pkg.dependencies : {}
|
|
191
|
+
for (const name of bundles) {
|
|
192
|
+
const spec = typeof deps[name] === 'string' ? deps[name] : ''
|
|
193
|
+
let version = ''
|
|
194
|
+
try {
|
|
195
|
+
const modPath = join(profileDir, 'node_modules', name, 'package.json')
|
|
196
|
+
const mod = readJsonFile(modPath, null)
|
|
197
|
+
version = String(mod?.version || '')
|
|
198
|
+
} catch {
|
|
199
|
+
/* ignore */
|
|
200
|
+
}
|
|
201
|
+
inventory.plugins.push({
|
|
202
|
+
name,
|
|
203
|
+
version,
|
|
204
|
+
bundle: true,
|
|
205
|
+
local: spec.startsWith('file:') || spec.startsWith('link:') || spec === '',
|
|
206
|
+
spec,
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// 2) 技能目录(SKILL.md 的 frontmatter name/description)
|
|
211
|
+
const skillsDir = join(dshHome(), 'skills')
|
|
212
|
+
try {
|
|
213
|
+
for (const name of readdirSync(skillsDir)) {
|
|
214
|
+
const file = join(skillsDir, name, 'SKILL.md')
|
|
215
|
+
if (!existsSync(file)) continue
|
|
216
|
+
let text = ''
|
|
217
|
+
try {
|
|
218
|
+
text = readFileSync(file, 'utf8').slice(0, 4000)
|
|
219
|
+
} catch {
|
|
220
|
+
continue
|
|
221
|
+
}
|
|
222
|
+
let title = name
|
|
223
|
+
let description = ''
|
|
224
|
+
const fm = text.match(/^---\s*\n([\s\S]*?)\n---/)
|
|
225
|
+
if (fm) {
|
|
226
|
+
const body = fm[1]
|
|
227
|
+
const nameMatch = body.match(/^\s*name:\s*(.+)$/m)
|
|
228
|
+
const descMatch = body.match(/^\s*description:\s*(.+)$/m)
|
|
229
|
+
if (nameMatch) title = nameMatch[1].trim().replace(/^["']|["']$/g, '')
|
|
230
|
+
if (descMatch) description = descMatch[1].trim().replace(/^["']|["']$/g, '')
|
|
231
|
+
}
|
|
232
|
+
inventory.skills.push({ name: title, dir: name, description: clip(description, 160) })
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
/* ignore */
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// 3) MCP:profile patch + 家目录 patch 里 dsh-mcp-client 的条目
|
|
239
|
+
const patches = [join(profileDir, 'cordis.patch.yml'), join(dshHome(), 'cordis.patch.yml')]
|
|
240
|
+
for (const file of patches) {
|
|
241
|
+
let text = ''
|
|
242
|
+
try {
|
|
243
|
+
text = readFileSync(file, 'utf8')
|
|
244
|
+
} catch {
|
|
245
|
+
continue
|
|
246
|
+
}
|
|
247
|
+
// 极简 YAML 嗅探:只认 `- id: xxx` 后面 10 行内出现 mcp 的情况
|
|
248
|
+
const lines = text.split(/\r?\n/)
|
|
249
|
+
for (let i = 0; i < lines.length; i++) {
|
|
250
|
+
const m = lines[i].match(/^\s*-\s*id:\s*([\w@./-]+)\s*$/)
|
|
251
|
+
if (!m) continue
|
|
252
|
+
// 只看到下一个条目为止:原来固定看 24 行,会把下一个插件的 mcp 字样算进来,
|
|
253
|
+
// 于是普通插件被错认成 MCP 服务器(1.11.39)。
|
|
254
|
+
const nextId = lines.findIndex((line, k) => k > i && /^\s*-\s*id:\s*[\w@./-]+\s*$/.test(line))
|
|
255
|
+
const stop = nextId < 0 ? Math.min(lines.length, i + 24) : nextId
|
|
256
|
+
const window = lines.slice(i, stop).join('\n')
|
|
257
|
+
if (!/mcp/i.test(window)) continue
|
|
258
|
+
const id = m[1]
|
|
259
|
+
if (inventory.mcp.some((x) => x.name === id)) continue
|
|
260
|
+
const transport = /"?(stdio|sse|streamable-http|http)"?/.exec(window)?.[1] || 'mcp'
|
|
261
|
+
inventory.mcp.push({ name: id, transport })
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return inventory
|
|
266
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 星图自己的 sidecar 数据库。
|
|
3
|
+
*
|
|
4
|
+
* 位置:$DSH_HOME/memory-nebula/nebula.db
|
|
5
|
+
* 只放三样东西:人工策展的连边、缓存、事件日志。
|
|
6
|
+
* **绝不写别的插件的记忆库**。
|
|
7
|
+
*/
|
|
8
|
+
import { DatabaseSync } from 'node:sqlite'
|
|
9
|
+
import { mkdirSync, existsSync, renameSync } from 'node:fs'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
import { nebulaHome } from './sources.js'
|
|
12
|
+
|
|
13
|
+
let db = null
|
|
14
|
+
let dbPath = ''
|
|
15
|
+
let recovery = null
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 上一次打开 sidecar 库时,是否把坏掉的文件挪开了。
|
|
19
|
+
* 坏库不该让整张图起不来,但也不该悄悄发生了没人说。
|
|
20
|
+
* @returns {{path: string, movedTo: string, error: string, at: number}|null}
|
|
21
|
+
*/
|
|
22
|
+
export function storeRecovery() {
|
|
23
|
+
return recovery
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 打开并建表;打不开就抛给调用方。 */
|
|
27
|
+
function tryOpen(file) {
|
|
28
|
+
const handle = new DatabaseSync(file)
|
|
29
|
+
try {
|
|
30
|
+
handle.exec(`
|
|
31
|
+
PRAGMA journal_mode = WAL;
|
|
32
|
+
PRAGMA synchronous = NORMAL;
|
|
33
|
+
CREATE TABLE IF NOT EXISTS links (
|
|
34
|
+
id TEXT PRIMARY KEY,
|
|
35
|
+
a TEXT NOT NULL,
|
|
36
|
+
b TEXT NOT NULL,
|
|
37
|
+
note TEXT NOT NULL DEFAULT '',
|
|
38
|
+
created_at INTEGER NOT NULL,
|
|
39
|
+
updated_at INTEGER NOT NULL
|
|
40
|
+
);
|
|
41
|
+
CREATE TABLE IF NOT EXISTS meta (
|
|
42
|
+
k TEXT PRIMARY KEY,
|
|
43
|
+
v TEXT NOT NULL
|
|
44
|
+
);
|
|
45
|
+
CREATE TABLE IF NOT EXISTS cache (
|
|
46
|
+
k TEXT PRIMARY KEY,
|
|
47
|
+
v TEXT NOT NULL,
|
|
48
|
+
updated_at INTEGER NOT NULL
|
|
49
|
+
);
|
|
50
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
51
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
52
|
+
ts INTEGER NOT NULL,
|
|
53
|
+
kind TEXT NOT NULL,
|
|
54
|
+
payload TEXT NOT NULL DEFAULT ''
|
|
55
|
+
);
|
|
56
|
+
CREATE INDEX IF NOT EXISTS idx_events_ts ON events(ts DESC);
|
|
57
|
+
CREATE TABLE IF NOT EXISTS mutes (
|
|
58
|
+
id TEXT PRIMARY KEY,
|
|
59
|
+
a TEXT NOT NULL,
|
|
60
|
+
b TEXT NOT NULL,
|
|
61
|
+
note TEXT NOT NULL DEFAULT '',
|
|
62
|
+
created_at INTEGER NOT NULL
|
|
63
|
+
);
|
|
64
|
+
`)
|
|
65
|
+
} catch (err) {
|
|
66
|
+
try {
|
|
67
|
+
handle.close()
|
|
68
|
+
} catch {
|
|
69
|
+
/* 连关都关不掉就让它带着句柄走 */
|
|
70
|
+
}
|
|
71
|
+
throw err
|
|
72
|
+
}
|
|
73
|
+
return handle
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 坏掉的那个文件挪到旁边:留证据,也让新库能建起来。 */
|
|
77
|
+
function moveAside(file) {
|
|
78
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
|
|
79
|
+
const target = file + '.broken-' + stamp
|
|
80
|
+
try {
|
|
81
|
+
renameSync(file, target)
|
|
82
|
+
} catch {
|
|
83
|
+
return ''
|
|
84
|
+
}
|
|
85
|
+
for (const extra of ['-wal', '-shm']) {
|
|
86
|
+
if (existsSync(file + extra)) {
|
|
87
|
+
try {
|
|
88
|
+
renameSync(file + extra, target + extra)
|
|
89
|
+
} catch {
|
|
90
|
+
/* 附属文件挪不动不影响新库 */
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return target
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 打开(并在需要时创建)sidecar 库;坏库挪开重建,不拖垮建图。 */
|
|
98
|
+
export function openStore() {
|
|
99
|
+
if (db) return db
|
|
100
|
+
const dir = nebulaHome()
|
|
101
|
+
try {
|
|
102
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
|
103
|
+
} catch {
|
|
104
|
+
/* 建不出目录也让下面的打开去报错,统一走自愈那一条路 */
|
|
105
|
+
}
|
|
106
|
+
dbPath = join(dir, 'nebula.db')
|
|
107
|
+
try {
|
|
108
|
+
db = tryOpen(dbPath)
|
|
109
|
+
} catch (err) {
|
|
110
|
+
const movedTo = existsSync(dbPath) ? moveAside(dbPath) : ''
|
|
111
|
+
const first = String((err && err.message) || err)
|
|
112
|
+
try {
|
|
113
|
+
db = tryOpen(dbPath)
|
|
114
|
+
} catch (again) {
|
|
115
|
+
// 挪开也建不起来(目录只读、磁盘满…):退到内存库,
|
|
116
|
+
// 让建图与检索照常跑,并把这件事说清楚。
|
|
117
|
+
recovery = {
|
|
118
|
+
path: dbPath,
|
|
119
|
+
movedTo,
|
|
120
|
+
error: first + ' / 重建失败:' + String((again && again.message) || again),
|
|
121
|
+
memoryOnly: true,
|
|
122
|
+
at: Date.now(),
|
|
123
|
+
}
|
|
124
|
+
db = tryOpen(':memory:')
|
|
125
|
+
return db
|
|
126
|
+
}
|
|
127
|
+
recovery = { path: dbPath, movedTo, error: first, memoryOnly: false, at: Date.now() }
|
|
128
|
+
}
|
|
129
|
+
return db
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 当前 sidecar 库路径。 */
|
|
133
|
+
export function storePath() {
|
|
134
|
+
return dbPath || join(nebulaHome(), 'nebula.db')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 关掉 sidecar 库,并忘掉它的路径。
|
|
139
|
+
*
|
|
140
|
+
* 生产里一个进程只有一个 DSH_HOME,用不上;但测例要在同一个进程里换 DSH_HOME,
|
|
141
|
+
* 不关掉的话 `db` 与 `dbPath` 会一直指向第一个临时目录——
|
|
142
|
+
* 1.11.26 的策展测试就是这么被上一跑的链接污染了一次(同一对 id 已经在库里,
|
|
143
|
+
* 就地策展于是变成「边已存在」,测试看到的是 1 条边而不是 2 条)。
|
|
144
|
+
*/
|
|
145
|
+
export function closeStore() {
|
|
146
|
+
if (db) {
|
|
147
|
+
try {
|
|
148
|
+
db.close()
|
|
149
|
+
} catch {
|
|
150
|
+
/* 已经关了就算了 */
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
db = null
|
|
154
|
+
dbPath = ''
|
|
155
|
+
recovery = null
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 列出人工策展的连边。
|
|
160
|
+
* @returns {{a:string,b:string,note:string,createdAt:number,updatedAt:number}[]}
|
|
161
|
+
*/
|
|
162
|
+
export function listLinks() {
|
|
163
|
+
const d = openStore()
|
|
164
|
+
const rows = d.prepare('SELECT a, b, note, created_at, updated_at FROM links ORDER BY updated_at DESC').all()
|
|
165
|
+
return rows.map((r) => ({
|
|
166
|
+
a: String(r.a),
|
|
167
|
+
b: String(r.b),
|
|
168
|
+
note: String(r.note || ''),
|
|
169
|
+
createdAt: Number(r.created_at) || 0,
|
|
170
|
+
updatedAt: Number(r.updated_at) || 0,
|
|
171
|
+
}))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 建立(或更新)一条人工策展边。
|
|
176
|
+
* @param {string} a
|
|
177
|
+
* @param {string} b
|
|
178
|
+
* @param {string} [note]
|
|
179
|
+
*/
|
|
180
|
+
export function addLink(a, b, note = '') {
|
|
181
|
+
const [x, y] = a < b ? [a, b] : [b, a]
|
|
182
|
+
const d = openStore()
|
|
183
|
+
const now = Date.now()
|
|
184
|
+
d.prepare(
|
|
185
|
+
`INSERT INTO links (id, a, b, note, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)
|
|
186
|
+
ON CONFLICT(id) DO UPDATE SET note = excluded.note, updated_at = excluded.updated_at`,
|
|
187
|
+
).run(x + '\u0000' + y, x, y, String(note || ''), now, now)
|
|
188
|
+
logEvent('link.add', { a: x, b: y, note })
|
|
189
|
+
return { a: x, b: y, note }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 删掉一条人工策展边。
|
|
194
|
+
* @param {string} a
|
|
195
|
+
* @param {string} b
|
|
196
|
+
*/
|
|
197
|
+
export function removeLink(a, b) {
|
|
198
|
+
const [x, y] = a < b ? [a, b] : [b, a]
|
|
199
|
+
const d = openStore()
|
|
200
|
+
const res = d.prepare('DELETE FROM links WHERE id = ?').run(x + '\u0000' + y)
|
|
201
|
+
logEvent('link.remove', { a: x, b: y })
|
|
202
|
+
return Number(res.changes || 0) > 0
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 列出被人工屏蔽的关联对(重建时不会再连上)。
|
|
207
|
+
* @returns {{a:string,b:string,note:string,createdAt:number}[]}
|
|
208
|
+
*/
|
|
209
|
+
export function listMutes() {
|
|
210
|
+
const d = openStore()
|
|
211
|
+
const rows = d.prepare('SELECT a, b, note, created_at FROM mutes ORDER BY created_at DESC').all()
|
|
212
|
+
return rows.map((r) => ({
|
|
213
|
+
a: String(r.a),
|
|
214
|
+
b: String(r.b),
|
|
215
|
+
note: String(r.note || ''),
|
|
216
|
+
createdAt: Number(r.created_at) || 0,
|
|
217
|
+
}))
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 屏蔽一对节点之间的自动关联。
|
|
222
|
+
* 回答的是「这条连线是错的」——不是删掉一条边,而是让**每次重建**都不再连它。
|
|
223
|
+
* @param {string} a
|
|
224
|
+
* @param {string} b
|
|
225
|
+
* @param {string} [note]
|
|
226
|
+
*/
|
|
227
|
+
export function addMute(a, b, note = '') {
|
|
228
|
+
const [x, y] = a < b ? [a, b] : [b, a]
|
|
229
|
+
const d = openStore()
|
|
230
|
+
d.prepare(
|
|
231
|
+
'INSERT INTO mutes (id, a, b, note, created_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET note = excluded.note',
|
|
232
|
+
).run(x + '\u0000' + y, x, y, String(note || ''), Date.now())
|
|
233
|
+
logEvent('mute.add', { a: x, b: y, note })
|
|
234
|
+
return { a: x, b: y, note }
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 取消屏蔽。
|
|
239
|
+
* @param {string} a
|
|
240
|
+
* @param {string} b
|
|
241
|
+
*/
|
|
242
|
+
export function removeMute(a, b) {
|
|
243
|
+
const [x, y] = a < b ? [a, b] : [b, a]
|
|
244
|
+
const d = openStore()
|
|
245
|
+
const res = d.prepare('DELETE FROM mutes WHERE id = ?').run(x + '\u0000' + y)
|
|
246
|
+
logEvent('mute.remove', { a: x, b: y })
|
|
247
|
+
return Number(res.changes || 0) > 0
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 读 meta。
|
|
252
|
+
* @param {string} k
|
|
253
|
+
* @param {string} [fallback]
|
|
254
|
+
*/export function getMeta(k, fallback = '') {
|
|
255
|
+
const d = openStore()
|
|
256
|
+
const row = d.prepare('SELECT v FROM meta WHERE k = ?').get(String(k))
|
|
257
|
+
return row ? String(row.v) : fallback
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 写 meta。
|
|
262
|
+
* @param {string} k
|
|
263
|
+
* @param {string} v
|
|
264
|
+
*/
|
|
265
|
+
export function setMeta(k, v) {
|
|
266
|
+
const d = openStore()
|
|
267
|
+
d.prepare('INSERT INTO meta (k, v) VALUES (?, ?) ON CONFLICT(k) DO UPDATE SET v = excluded.v').run(String(k), String(v))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 读缓存。
|
|
272
|
+
* @param {string} k
|
|
273
|
+
*/
|
|
274
|
+
export function cacheGet(k) {
|
|
275
|
+
const d = openStore()
|
|
276
|
+
const row = d.prepare('SELECT v, updated_at FROM cache WHERE k = ?').get(String(k))
|
|
277
|
+
if (!row) return null
|
|
278
|
+
try {
|
|
279
|
+
return { value: JSON.parse(String(row.v)), updatedAt: Number(row.updated_at) || 0 }
|
|
280
|
+
} catch {
|
|
281
|
+
return null
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 写缓存。
|
|
287
|
+
* @param {string} k
|
|
288
|
+
* @param {unknown} value
|
|
289
|
+
*/
|
|
290
|
+
export function cacheSet(k, value) {
|
|
291
|
+
const d = openStore()
|
|
292
|
+
let text
|
|
293
|
+
try {
|
|
294
|
+
text = JSON.stringify(value)
|
|
295
|
+
} catch {
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
d.prepare(
|
|
299
|
+
'INSERT INTO cache (k, v, updated_at) VALUES (?, ?, ?) ON CONFLICT(k) DO UPDATE SET v = excluded.v, updated_at = excluded.updated_at',
|
|
300
|
+
).run(String(k), text, Date.now())
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 记一条事件(谁在什么时候连了什么 / 星图什么时候重建)。
|
|
305
|
+
* @param {string} kind
|
|
306
|
+
* @param {unknown} [payload]
|
|
307
|
+
*/
|
|
308
|
+
export function logEvent(kind, payload) {
|
|
309
|
+
try {
|
|
310
|
+
const d = openStore()
|
|
311
|
+
let text = ''
|
|
312
|
+
try {
|
|
313
|
+
text = typeof payload === 'string' ? payload : JSON.stringify(payload ?? null)
|
|
314
|
+
} catch {
|
|
315
|
+
text = ''
|
|
316
|
+
}
|
|
317
|
+
d.prepare('INSERT INTO events (ts, kind, payload) VALUES (?, ?, ?)').run(Date.now(), String(kind), text)
|
|
318
|
+
// 只留最近 2000 条,避免无限膨胀
|
|
319
|
+
d.prepare('DELETE FROM events WHERE id <= (SELECT MAX(id) - 2000 FROM events)').run()
|
|
320
|
+
} catch {
|
|
321
|
+
/* 日志失败不影响主流程 */
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 读最近事件。
|
|
327
|
+
* @param {number} [limit]
|
|
328
|
+
*/
|
|
329
|
+
export function listEvents(limit = 40) {
|
|
330
|
+
const d = openStore()
|
|
331
|
+
const rows = d.prepare('SELECT ts, kind, payload FROM events ORDER BY id DESC LIMIT ?').all(Math.max(1, Math.min(500, limit)))
|
|
332
|
+
return rows.map((r) => ({ ts: Number(r.ts), kind: String(r.kind), payload: String(r.payload || '') }))
|
|
333
|
+
}
|