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
package/lib/index.js
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-macro-memory —— 记忆总成插件。
|
|
3
|
+
*
|
|
4
|
+
* 定位:一个插件顶替 meow-memory(跨会话记忆条目)+ dsh-memory-nebula(记忆星图),
|
|
5
|
+
* 对外只暴露 8 个统一的 memory_* 工具与一段最小注入;
|
|
6
|
+
* 两个原插件在这里是「引擎」,不是并列的插件 —— 它们的工具与注入都被融合层接管重组。
|
|
7
|
+
*
|
|
8
|
+
* 结构:
|
|
9
|
+
* lib/index.js 本文件,唯一的插件入口(含宿主设置页要的 Config)
|
|
10
|
+
* lib/macro/bridge.js 接管两个子插件的 ctx(工具/注入进不下宿主,其余透传)
|
|
11
|
+
* lib/macro/tools.js 8 个统一工具(复用子插件自己的写路径,不重写记忆逻辑)
|
|
12
|
+
* lib/macro/prompt.js L0 常驻注入 + L2 按需完整手册
|
|
13
|
+
* lib/macro/cost.js 成本账本 $DSH_HOME/macro-memory/cost.jsonl
|
|
14
|
+
* lib/client.js 前端(由 build.mjs 从两个子插件的 web bundle 拼出)
|
|
15
|
+
*/
|
|
16
|
+
import z from '@deepseek-ai/schemastery'
|
|
17
|
+
import * as Meow from '../vendor/meow-memory/lib/index.js'
|
|
18
|
+
import * as Nebula from '../vendor/dsh-memory-nebula/lib/index.js'
|
|
19
|
+
import { brandText, withNameHints } from './macro/brand.js'
|
|
20
|
+
import { createBridge, errText, wrapPlugin } from './macro/bridge.js'
|
|
21
|
+
import { GUIDE_MAX_CHARS, GUIDE_NAME, GUIDE_ORDER, GUIDE_TEXT, clampGuide, guideBudget } from './macro/prompt.js'
|
|
22
|
+
import { createAliasTools, createUnifiedTools } from './macro/tools.js'
|
|
23
|
+
import { withCost } from './macro/cost.js'
|
|
24
|
+
import { NEIGHBOR_DEFAULT_CHARS, NEIGHBOR_DEFAULT_MAX, createNeighborInjector } from './macro/neighbor.js'
|
|
25
|
+
|
|
26
|
+
export const name = 'macro-memory'
|
|
27
|
+
export const version = '0.4.0'
|
|
28
|
+
export const inject = ['tools']
|
|
29
|
+
|
|
30
|
+
/** 融合层自己的默认值;配置里可写在顶层,也可写在 macro: 段下。 */
|
|
31
|
+
export const MACRO_DEFAULTS = {
|
|
32
|
+
/** 是否额外注册子插件原名工具(nebula_* / memory_find_similar / memory_home),默认关闭。 */
|
|
33
|
+
compatAliases: false,
|
|
34
|
+
/** L0 常驻注入的字符上限。 */
|
|
35
|
+
guideMaxChars: GUIDE_MAX_CHARS,
|
|
36
|
+
/** 统一工具等子插件装配完成的最长时间(毫秒)。 */
|
|
37
|
+
toolWaitMs: 30000,
|
|
38
|
+
/** 读路径补连线的节流窗口(毫秒);0 表示每次都补。 */
|
|
39
|
+
flushThrottleMs: 30000,
|
|
40
|
+
/** 每轮自动带一次星图邻域(0.2.145):关掉就退回「模型自己不查就没有」。 */
|
|
41
|
+
neighborInject: true,
|
|
42
|
+
/** 一次最多带几条邻居。 */
|
|
43
|
+
neighborMax: NEIGHBOR_DEFAULT_MAX,
|
|
44
|
+
/** 邻域注入那一块最多多少字符。 */
|
|
45
|
+
neighborChars: NEIGHBOR_DEFAULT_CHARS,
|
|
46
|
+
/**
|
|
47
|
+
* 查询分解(0.4.0 P1 / PLAN-0.4 §5 P1):off = 一个字都不变;conservative = 原榜不动,
|
|
48
|
+
* 只在并列/多问号句子上多查一遍,按空位补几条。写别的值(含大小写写错)一律当 off。
|
|
49
|
+
*/
|
|
50
|
+
multiQuery: 'off',
|
|
51
|
+
/**
|
|
52
|
+
* 写入端巩固(0.4.0 P2 / PLAN-0.4 §5 P2):off = 一个字都不变;suggest = 三条件成立时
|
|
53
|
+
* 在写入回执尾部贴一段「巩固材料」,**一个字不往库里写**;write = 才自动落那条摘要。
|
|
54
|
+
* 写别的值(只认这三个词,大小写与前后空格不敏感)一律当 off:配置写坏时必须退到最保守那一档。
|
|
55
|
+
*/
|
|
56
|
+
consolidate: 'suggest',
|
|
57
|
+
/**
|
|
58
|
+
* 写入端自动取代(0.4.0 P3 / PLAN-0.4 §5 P3):off = 连检索那一枪都不打;suggest = 认出「库里那条
|
|
59
|
+
* 是同一件事的旧说法」只在回执里给建议,**一个字不动库**;apply = 真把那几条标 stale。
|
|
60
|
+
* 只认这三个词,写别的值一律当 off:这个开关会改别人记忆的状态,配错必须退到最保守那一档。
|
|
61
|
+
*/
|
|
62
|
+
autoSupersede: 'suggest',
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const MACRO_KEYS = [
|
|
66
|
+
'compatAliases',
|
|
67
|
+
'guideMaxChars',
|
|
68
|
+
'toolWaitMs',
|
|
69
|
+
'flushThrottleMs',
|
|
70
|
+
'neighborInject',
|
|
71
|
+
'neighborMax',
|
|
72
|
+
'neighborChars',
|
|
73
|
+
'multiQuery',
|
|
74
|
+
'consolidate',
|
|
75
|
+
'autoSupersede',
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
/** 宿主设置页写入后的去抖窗口(毫秒):连点几下只重建一次工具面。 */
|
|
79
|
+
const VOLATILE_DEBOUNCE_MS = 150
|
|
80
|
+
|
|
81
|
+
/** cosmokit 的 volatile 写入协议符号(跨 ESM/CJS 副本也认得)。 */
|
|
82
|
+
const VOLATILE_WRITE = Symbol.for('cosmokit.volatile.write')
|
|
83
|
+
|
|
84
|
+
/** 这个值是不是 cosmokit 的 volatile 引用。 */
|
|
85
|
+
export function isVolatileRef(value) {
|
|
86
|
+
return typeof value === 'object' && value !== null && VOLATILE_WRITE in value
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 把配置读成普通对象:宿主传进来的可能是 volatile 引用(写入后原地更新),
|
|
91
|
+
* 也可能是测试/旧路径给的普通对象。引用逐层解包,最多 8 层,防自引用打转。
|
|
92
|
+
*/
|
|
93
|
+
export function readConfigRoot(value) {
|
|
94
|
+
let current = value
|
|
95
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
96
|
+
if (!isVolatileRef(current) || typeof current.get !== 'function') break
|
|
97
|
+
const next = current.get()
|
|
98
|
+
if (next === current) break
|
|
99
|
+
current = next
|
|
100
|
+
}
|
|
101
|
+
return current !== null && typeof current === 'object' ? current : {}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** meow 自己声明的字段(带默认值与校验)——设置页要靠它们在「默认值 / 用户值」两栏之间显示。 */
|
|
105
|
+
const MEOW_FIELDS = Meow.Config !== undefined && Meow.Config.dict !== undefined ? Meow.Config.dict : {}
|
|
106
|
+
const MEOW_KEYS = Object.keys(MEOW_FIELDS)
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* [macro-fusion 0.2.79] 宿主设置页只认「声明在 Config 里、且路径落在 volatile 节点下」的键:
|
|
110
|
+
* 1) 不导出 Config → describe() 里没有这个条目 → 设置页永远是「当前连接不支持设置写入」;
|
|
111
|
+
* 2) 导出了但没声明的键 → 每次写入都被宿主的 strip() 剃掉(profile 里的 meow / nebula 会整段消失);
|
|
112
|
+
* 3) 根 volatile + 子字段 volatile 不能共存(schemastery 会报 "volatile fields require a fixed
|
|
113
|
+
* object path without an enclosing volatile field"),所以跟 meow 一样:根 volatile、子字段普通。
|
|
114
|
+
* 平铺到根而不是塞进 meow: 段,是因为设置页的写入只支持顶层字段名(path:[field])。
|
|
115
|
+
*/
|
|
116
|
+
export const Config = z.object({
|
|
117
|
+
...MEOW_FIELDS,
|
|
118
|
+
/** 星图引擎的配置段(保留旧写法)。 */
|
|
119
|
+
nebula: z.any(),
|
|
120
|
+
/** 融合层的旧嵌套写法兼容段。 */
|
|
121
|
+
macro: z.any(),
|
|
122
|
+
/** meow 的旧嵌套写法兼容段(只读兜底;新写法平铺在根上)。 */
|
|
123
|
+
meow: z.any(),
|
|
124
|
+
compatAliases: z.boolean().default(MACRO_DEFAULTS.compatAliases),
|
|
125
|
+
guideMaxChars: z.number().default(MACRO_DEFAULTS.guideMaxChars),
|
|
126
|
+
toolWaitMs: z.number().default(MACRO_DEFAULTS.toolWaitMs),
|
|
127
|
+
flushThrottleMs: z.number().default(MACRO_DEFAULTS.flushThrottleMs),
|
|
128
|
+
neighborInject: z.boolean().default(MACRO_DEFAULTS.neighborInject),
|
|
129
|
+
neighborMax: z.number().default(MACRO_DEFAULTS.neighborMax),
|
|
130
|
+
neighborChars: z.number().default(MACRO_DEFAULTS.neighborChars),
|
|
131
|
+
/**
|
|
132
|
+
* 用 string 不用 enum:宿主设置页要把「默认值 / 用户值」两栏分开落盘,
|
|
133
|
+
* 枚举会给它一个固定候选列表,反而写不下自己那栏。合法值只有 off / conservative。
|
|
134
|
+
*/
|
|
135
|
+
multiQuery: z.string().default(MACRO_DEFAULTS.multiQuery),
|
|
136
|
+
/**
|
|
137
|
+
* 同上:string 不用 enum。合法值只有 off / suggest / write,写别的当 off。
|
|
138
|
+
*/
|
|
139
|
+
consolidate: z.string().default(MACRO_DEFAULTS.consolidate),
|
|
140
|
+
/**
|
|
141
|
+
* 同上:string 不用 enum。合法值只有 off / suggest / apply,写别的当 off。
|
|
142
|
+
*/
|
|
143
|
+
autoSupersede: z.string().default(MACRO_DEFAULTS.autoSupersede),
|
|
144
|
+
})
|
|
145
|
+
Object.assign(Config.meta, { default: {}, volatile: true })
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 把一份配置(volatile 引用或普通对象)解成三段:
|
|
149
|
+
* meow 子插件 meow 的配置:旧 meow: 段铺底,根上的同名字段覆盖(0.2.79 起是平铺的)
|
|
150
|
+
* nebula 星图配置段
|
|
151
|
+
* macro 融合层配置:macro: 段铺底,根上四个键覆盖,缺省用 MACRO_DEFAULTS
|
|
152
|
+
*/
|
|
153
|
+
export function resolveConfig(raw) {
|
|
154
|
+
const src = readConfigRoot(raw)
|
|
155
|
+
const nested = readConfigRoot(src.macro)
|
|
156
|
+
const legacyMeow = readConfigRoot(src.meow)
|
|
157
|
+
const macro = { ...MACRO_DEFAULTS }
|
|
158
|
+
for (const key of MACRO_KEYS) {
|
|
159
|
+
if (src[key] !== undefined) macro[key] = src[key]
|
|
160
|
+
if (nested[key] !== undefined) macro[key] = nested[key]
|
|
161
|
+
}
|
|
162
|
+
const meow = { ...legacyMeow }
|
|
163
|
+
for (const key of MEOW_KEYS) {
|
|
164
|
+
if (src[key] !== undefined) meow[key] = src[key]
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
meow,
|
|
168
|
+
nebula: readConfigRoot(src.nebula),
|
|
169
|
+
macro,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** [macro-fusion 0.2.97] meow 的 projectDir 读口:面板的「整理留痕」按它找 dream 账本。
|
|
174
|
+
* 装配时(apply)换成真正生效的值;没装配到就退回默认目录,绝不写死两份。 */
|
|
175
|
+
let meowDirReader = () => '.dsh-meow'
|
|
176
|
+
|
|
177
|
+
export function apply(ctx, config) {
|
|
178
|
+
const logger = safeLogger(ctx)
|
|
179
|
+
// [macro-fusion 0.2.79] 有 volatile 配置的插件,宿主会额外自动生成一张通用表单页;
|
|
180
|
+
// 我们自带「宏记忆」设置页,两张页面并排出现会互相打脸,这里把自动那张关掉。
|
|
181
|
+
configureSettingsPage(ctx, logger)
|
|
182
|
+
warnLegacySections(config, logger)
|
|
183
|
+
|
|
184
|
+
let macroCfg = resolveConfig(config).macro
|
|
185
|
+
let surface = null
|
|
186
|
+
let stopped = false
|
|
187
|
+
let timer
|
|
188
|
+
let chain = Promise.resolve()
|
|
189
|
+
let lastRoot = readConfigRoot(config)
|
|
190
|
+
|
|
191
|
+
const bridge = createBridge({
|
|
192
|
+
log: (msg) => logger?.debug?.(msg),
|
|
193
|
+
warn: (msg) => logger?.warn?.(msg),
|
|
194
|
+
// 子插件的返回文本里可能写着 nebula_recall / meow-memory 这些旧名字;
|
|
195
|
+
// compatAliases 关着的时候它们根本没注册,原样吐给模型就是死指针,所以先换口。
|
|
196
|
+
// 但「检索/读取」返回的是记忆原文:正文一个字不动,只在末尾补旧名对照(0.2.14)。
|
|
197
|
+
// 这两处读的是 macroCfg 的最新值,所以改 compatAliases 不用重挂子插件(0.2.79)。
|
|
198
|
+
brand: (text) => brandText(text, { aliases: macroCfg.compatAliases === true }),
|
|
199
|
+
hints: (text) => withNameHints(text, { aliases: macroCfg.compatAliases === true }),
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
// ── 1. 两个子插件照旧装配,但拿到的是 facade ctx:
|
|
203
|
+
// 它们注册的工具、想注入的 prompt 全被 bridge 收下,由下面重新组织。
|
|
204
|
+
// 喂给引擎的只能是普通对象:宿主会用我们的 Config 解析 apply 的入参,解析结果是一份
|
|
205
|
+
// volatile 引用;meow 的 readConfigRoot 只认「带 get 的普通对象」,拿到宿主这种引用
|
|
206
|
+
// 会当场装不起来(0.2.79 实测:它的 8 个工具一个都没注册,统一工具全线「这项能力
|
|
207
|
+
// 还没就绪」)。所以引擎配置一律先解成普通对象;设置页改了引擎字段就整只重挂,
|
|
208
|
+
// 由 syncSurface() 负责(0.2.79 修)。
|
|
209
|
+
const initial = resolveConfig(config)
|
|
210
|
+
let engineCfg = { meow: initial.meow, nebula: initial.nebula }
|
|
211
|
+
const engines = new Map()
|
|
212
|
+
|
|
213
|
+
// [macro-fusion 0.2.97] 整理留痕面板要读 meow 的账本,而账本按 meow 的 projectDir 放
|
|
214
|
+
// (默认 .dsh-meow,可配)。这个路径只有引擎配置知道,所以在这里留一个读口:
|
|
215
|
+
// 面板拿到的永远是「现在生效的那个目录」,而不是面板自己写死的 .dsh-meow。
|
|
216
|
+
meowDirReader = () => {
|
|
217
|
+
const d = engineCfg.meow?.projectDir
|
|
218
|
+
return typeof d === 'string' && d.length > 0 ? d : '.dsh-meow'
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const mountEngine = (vendor, mod, cfg) => {
|
|
222
|
+
try {
|
|
223
|
+
engines.set(vendor, ctx.plugin(wrapPlugin(mod, bridge, vendor), cfg))
|
|
224
|
+
} catch (err) {
|
|
225
|
+
logger?.warn?.(`[macro-memory] ${vendor} 引擎装配失败:${errText(err)}`)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const unmountEngine = async (vendor) => {
|
|
230
|
+
const fork = engines.get(vendor)
|
|
231
|
+
engines.delete(vendor)
|
|
232
|
+
if (fork === undefined || fork === null) return
|
|
233
|
+
try {
|
|
234
|
+
await fork.dispose?.()
|
|
235
|
+
} catch (err) {
|
|
236
|
+
logger?.debug?.(`[macro-memory] 拆卸 ${vendor} 引擎时出错:${errText(err)}`)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
mountEngine('meow', Meow, engineCfg.meow)
|
|
241
|
+
mountEngine('nebula', Nebula, engineCfg.nebula)
|
|
242
|
+
|
|
243
|
+
// ── 2. 工具面 + 注入 = 一层可整体重建的「表面」:
|
|
244
|
+
// 配置里融合层那四个键变了就换一层,子插件不动(重挂 meow 会打断它正在做的扫描)。
|
|
245
|
+
const stopSurface = async () => {
|
|
246
|
+
const current = surface
|
|
247
|
+
surface = null
|
|
248
|
+
if (current === null) return
|
|
249
|
+
await current.stop()
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const startSurface = () => {
|
|
253
|
+
surface = buildSurface(ctx, bridge, macroCfg, logger)
|
|
254
|
+
return surface
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const syncSurface = () => {
|
|
258
|
+
const next = resolveConfig(config)
|
|
259
|
+
const macroChanged = MACRO_KEYS.filter((key) => next.macro[key] !== macroCfg[key])
|
|
260
|
+
const meowChanged = !sameValue(next.meow, engineCfg.meow)
|
|
261
|
+
const nebulaChanged = !sameValue(next.nebula, engineCfg.nebula)
|
|
262
|
+
if (macroChanged.length === 0 && !meowChanged && !nebulaChanged) return
|
|
263
|
+
// 融合层的键立刻换新值:brand()/hints()/guide 都靠 macroCfg 现读,不用等异步重挂(0.2.79)
|
|
264
|
+
macroCfg = next.macro
|
|
265
|
+
const detail = macroChanged.map((key) => `${key}=${JSON.stringify(next.macro[key])}`).join('、')
|
|
266
|
+
chain = chain
|
|
267
|
+
.then(async () => {
|
|
268
|
+
if (stopped) return
|
|
269
|
+
if (meowChanged || nebulaChanged) {
|
|
270
|
+
// 引擎自己那份配置是「装配时拍下的快照」,热写改不到它 —— 要它真按新配置跑,
|
|
271
|
+
// 只能拆掉重挂。先收工具面:不然重挂那几百毫秒里统一工具会指向半死的引擎。
|
|
272
|
+
const plan = []
|
|
273
|
+
if (meowChanged) plan.push(['meow', Meow])
|
|
274
|
+
if (nebulaChanged) plan.push(['nebula', Nebula])
|
|
275
|
+
await stopSurface()
|
|
276
|
+
for (const [vendor] of plan) await unmountEngine(vendor)
|
|
277
|
+
engineCfg = { meow: next.meow, nebula: next.nebula }
|
|
278
|
+
if (stopped) return
|
|
279
|
+
for (const [vendor, mod] of plan) mountEngine(vendor, mod, engineCfg[vendor])
|
|
280
|
+
startSurface()
|
|
281
|
+
const which = plan.map(([vendor]) => vendor).join('+')
|
|
282
|
+
logger?.info?.(
|
|
283
|
+
`[macro-memory] 设置已热应用:记忆引擎(${which})重新装配,扫描会重启`
|
|
284
|
+
+ `${detail ? `;同时生效:${detail}` : ''}(工具面与注入已重建)`,
|
|
285
|
+
)
|
|
286
|
+
return
|
|
287
|
+
}
|
|
288
|
+
await stopSurface()
|
|
289
|
+
if (stopped) return
|
|
290
|
+
startSurface()
|
|
291
|
+
logger?.info?.(`[macro-memory] 设置已热应用:${detail}(工具面与注入已重建,引擎没动)`)
|
|
292
|
+
})
|
|
293
|
+
.catch((err) => logger?.warn?.(`[macro-memory] 设置热应用失败,仍在用旧工具面:${errText(err)}`))
|
|
294
|
+
return chain
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
startSurface()
|
|
298
|
+
|
|
299
|
+
// ── 3. 宿主设置页写入:volatile 引用被原地更新,这里只重建「表面」。
|
|
300
|
+
const onVolatile = (paths) => {
|
|
301
|
+
if (stopped) return
|
|
302
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
303
|
+
timer = setTimeout(() => {
|
|
304
|
+
timer = undefined
|
|
305
|
+
if (stopped) return
|
|
306
|
+
const nextRoot = readConfigRoot(config)
|
|
307
|
+
if (nextRoot.homeDir !== lastRoot.homeDir) {
|
|
308
|
+
logger?.info?.(
|
|
309
|
+
`[macro-memory] homeDir 从 ${JSON.stringify(lastRoot.homeDir)} 改成 ${JSON.stringify(nextRoot.homeDir)}:`
|
|
310
|
+
+ '记忆引擎会重新装配并切到新库目录(旧目录里的记忆不会自动搬过去)。',
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
lastRoot = nextRoot
|
|
314
|
+
logger?.debug?.(`[macro-memory] 设置页写入:${describePaths(paths)}`)
|
|
315
|
+
syncSurface()
|
|
316
|
+
}, VOLATILE_DEBOUNCE_MS)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (typeof ctx.on === 'function') {
|
|
320
|
+
try {
|
|
321
|
+
ctx.on('loader/volatile-update', onVolatile)
|
|
322
|
+
} catch (err) {
|
|
323
|
+
logger?.debug?.(`[macro-memory] 订阅设置热更新失败:${errText(err)}`)
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
ctx.effect(() => () => {
|
|
328
|
+
stopped = true
|
|
329
|
+
if (timer !== undefined) {
|
|
330
|
+
clearTimeout(timer)
|
|
331
|
+
timer = undefined
|
|
332
|
+
}
|
|
333
|
+
void stopSurface()
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
logger?.debug?.(
|
|
337
|
+
`[macro-memory] 融合层就绪:${surface?.tools ?? 0} 个统一工具${macroCfg.compatAliases ? ' + 兼容别名' : ''}`,
|
|
338
|
+
)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** 工具面 + 注入:可整体重建,重建时先拆旧的再装新的。 */
|
|
342
|
+
function buildSurface(ctx, bridge, macro, logger) {
|
|
343
|
+
const cleanups = []
|
|
344
|
+
const unified = createUnifiedTools({ bridge, config: macro, meowDir: meowDirReader })
|
|
345
|
+
for (const def of unified) pushTool(ctx, cleanups, def, logger)
|
|
346
|
+
if (macro.compatAliases === true) {
|
|
347
|
+
for (const def of createAliasTools({ bridge })) pushTool(ctx, cleanups, def, logger)
|
|
348
|
+
}
|
|
349
|
+
const disposeGuide = installGuide(ctx, macro, logger)
|
|
350
|
+
if (typeof disposeGuide === 'function') cleanups.push(disposeGuide)
|
|
351
|
+
// [macro-fusion 0.2.145] 每轮星图邻域:挂在 pre-step 的**尾部**,不进 system prompt 前缀。
|
|
352
|
+
const neighbor = createNeighborInjector({
|
|
353
|
+
ctx,
|
|
354
|
+
bridge,
|
|
355
|
+
config: macro,
|
|
356
|
+
logger: (msg) => logger?.debug?.(msg),
|
|
357
|
+
})
|
|
358
|
+
cleanups.push(() => neighbor.dispose())
|
|
359
|
+
|
|
360
|
+
let done = false
|
|
361
|
+
return {
|
|
362
|
+
tools: unified.length,
|
|
363
|
+
stop: async () => {
|
|
364
|
+
if (done) return
|
|
365
|
+
done = true
|
|
366
|
+
for (const fn of cleanups.reverse()) {
|
|
367
|
+
try {
|
|
368
|
+
await fn()
|
|
369
|
+
} catch (err) {
|
|
370
|
+
logger?.debug?.(`[macro-memory] 拆卸工具面时出错:${errText(err)}`)
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function pushTool(ctx, cleanups, def, logger) {
|
|
378
|
+
try {
|
|
379
|
+
const dispose = ctx.tools.register(withCost(def.name, def))
|
|
380
|
+
if (typeof dispose === 'function') cleanups.push(dispose)
|
|
381
|
+
} catch (err) {
|
|
382
|
+
logger?.warn?.(`[macro-memory] 工具 ${def.name} 注册失败:${errText(err)}`)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* 关掉宿主为「有 volatile 配置的插件」自动生成的通用表单页(auto: false)。
|
|
388
|
+
* 关不掉不算故障:多一张页面比没有页面好,所以只记 debug。
|
|
389
|
+
*/
|
|
390
|
+
function configureSettingsPage(ctx, logger) {
|
|
391
|
+
const configure = (child) => {
|
|
392
|
+
let settings
|
|
393
|
+
try {
|
|
394
|
+
settings = child.get?.('settings')
|
|
395
|
+
} catch {
|
|
396
|
+
settings = undefined
|
|
397
|
+
}
|
|
398
|
+
if (settings === undefined || settings === null || typeof settings.configure !== 'function') return
|
|
399
|
+
try {
|
|
400
|
+
child.effect(() => settings.configure({ auto: false }, ctx.fiber))
|
|
401
|
+
} catch (err) {
|
|
402
|
+
logger?.debug?.(`[macro-memory] 关闭自动设置页失败(不影响使用):${errText(err)}`)
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
try {
|
|
406
|
+
if (typeof ctx.inject === 'function') ctx.inject(['settings'], configure)
|
|
407
|
+
else configure(ctx)
|
|
408
|
+
} catch (err) {
|
|
409
|
+
logger?.debug?.(`[macro-memory] 取 settings 服务失败(不影响使用):${errText(err)}`)
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** 旧写法 config.meow / config.macro 还在的话,说清楚哪几个键被根字段盖住了。 */
|
|
414
|
+
function warnLegacySections(raw, logger) {
|
|
415
|
+
const src = readConfigRoot(raw)
|
|
416
|
+
const sections = [
|
|
417
|
+
['meow', 'meow'],
|
|
418
|
+
['macro', 'macro'],
|
|
419
|
+
]
|
|
420
|
+
for (const [section, label] of sections) {
|
|
421
|
+
const legacy = src[section]
|
|
422
|
+
if (legacy === null || typeof legacy !== 'object' || Array.isArray(legacy)) continue
|
|
423
|
+
const keys = Object.keys(legacy)
|
|
424
|
+
if (keys.length === 0) continue
|
|
425
|
+
const shadowed = keys.filter((key) => src[key] !== undefined && !sameValue(src[key], legacy[key]))
|
|
426
|
+
logger?.warn?.(
|
|
427
|
+
`[macro-memory] 配置里还有旧写法 config.${label}(${keys.join('、')}):0.2.79 起配置平铺在根上`
|
|
428
|
+
+ `${shadowed.length > 0 ? `,其中 ${shadowed.join('、')} 已被根字段覆盖(根字段优先)` : ',根上没写的键仍按旧段生效'}。`
|
|
429
|
+
+ '把这段里的键搬到根上,这条提示就不再出现。',
|
|
430
|
+
)
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function sameValue(a, b) {
|
|
435
|
+
if (a === b) return true
|
|
436
|
+
if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') return false
|
|
437
|
+
try {
|
|
438
|
+
return JSON.stringify(a) === JSON.stringify(b)
|
|
439
|
+
} catch {
|
|
440
|
+
return false
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function describePaths(paths) {
|
|
445
|
+
if (!Array.isArray(paths)) return '(未知路径)'
|
|
446
|
+
const list = paths.map((path) => (Array.isArray(path) && path.length > 0 ? path.join('.') : '(根)'))
|
|
447
|
+
return list.length > 0 ? list.join('、') : '(根)'
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function installGuide(ctx, macro, logger) {
|
|
451
|
+
// [macro-fusion 0.2.63] 上限先过唯一出口:配置写错要回默认并吭声,不许悄悄变成 0(=不注入)
|
|
452
|
+
const budget = guideBudget(macro.guideMaxChars)
|
|
453
|
+
if (budget.note) logger?.warn?.(`[macro-memory] ${budget.note}`)
|
|
454
|
+
const text = clampGuide(GUIDE_TEXT, budget.value)
|
|
455
|
+
let timer
|
|
456
|
+
let disposer
|
|
457
|
+
let tries = 0
|
|
458
|
+
|
|
459
|
+
const attempt = () => {
|
|
460
|
+
if (disposer) return
|
|
461
|
+
let svc
|
|
462
|
+
try {
|
|
463
|
+
svc = ctx.get?.('systemPrompt')
|
|
464
|
+
} catch {
|
|
465
|
+
svc = undefined
|
|
466
|
+
}
|
|
467
|
+
if (svc && typeof svc.section === 'function') {
|
|
468
|
+
try {
|
|
469
|
+
disposer = svc.section({ name: GUIDE_NAME, order: GUIDE_ORDER, text })
|
|
470
|
+
logger?.debug?.(`[macro-memory] L0 注入已装上(${text.length} 字符)`)
|
|
471
|
+
} catch (err) {
|
|
472
|
+
logger?.warn?.(`[macro-memory] L0 注入安装失败:${errText(err)}`)
|
|
473
|
+
}
|
|
474
|
+
return
|
|
475
|
+
}
|
|
476
|
+
tries += 1
|
|
477
|
+
if (tries <= 200) timer = setTimeout(attempt, 250)
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
attempt()
|
|
481
|
+
// 返回拆卸函数:工具面重建时由 buildSurface 统一收,不再各自挂 ctx.effect(0.2.79)
|
|
482
|
+
return () => {
|
|
483
|
+
if (timer !== undefined) {
|
|
484
|
+
clearTimeout(timer)
|
|
485
|
+
timer = undefined
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
disposer?.()
|
|
489
|
+
} catch {
|
|
490
|
+
/* ignore */
|
|
491
|
+
}
|
|
492
|
+
disposer = undefined
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function safeLogger(ctx) {
|
|
497
|
+
try {
|
|
498
|
+
return ctx.logger
|
|
499
|
+
} catch {
|
|
500
|
+
return undefined
|
|
501
|
+
}
|
|
502
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文案归口:把子插件原文里的「旧工具名」换成 macro-memory 自己对外的那一套。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:compatAliases 默认关掉,nebula_* / memory_find_similar / memory_home
|
|
5
|
+
* 并不注册。子插件的返回文本里如果还写着「可以先用 nebula_recall 搜一下」,
|
|
6
|
+
* 对模型就是一封死指针信 —— 它照着调,只会拿到「工具不存在」。
|
|
7
|
+
*
|
|
8
|
+
* 规则:
|
|
9
|
+
* - 只在 compatAliases=false 时替换(开着别名,旧名字确实能用,就不动原文)
|
|
10
|
+
* - 只做名字级替换,不改措辞、不重写子插件逻辑(DESIGN §7 R2)
|
|
11
|
+
* - 「星图」「记忆库」这类概念词保留,本来就是我们自己的说法
|
|
12
|
+
*/
|
|
13
|
+
export const BRAND_MAP = [
|
|
14
|
+
// nebula 系 → macro 的 8 个统一工具
|
|
15
|
+
['nebula_recall', 'memory_search(mode="graph")'],
|
|
16
|
+
['nebula_inspect', 'memory_read(neighbors>0)'],
|
|
17
|
+
['nebula_bridge', 'memory_search(mode="bridge")'],
|
|
18
|
+
['nebula_link', 'memory_link'],
|
|
19
|
+
['nebula_path', 'memory_link(action="path")'],
|
|
20
|
+
['nebula_stats', 'memory_admin(action="stats")'],
|
|
21
|
+
['nebula_hygiene', 'memory_admin(action="hygiene")'],
|
|
22
|
+
['nebula_sources', 'memory_admin(action="sources")'],
|
|
23
|
+
['nebula_ingest', 'memory_admin(action="ingest")'],
|
|
24
|
+
// meow 系里没进主工具面的两个
|
|
25
|
+
['memory_find_similar', 'memory_search(mode="similar")'],
|
|
26
|
+
['memory_home', 'memory_admin(action="home")'],
|
|
27
|
+
// 自称
|
|
28
|
+
['meow-memory', 'macro-memory'],
|
|
29
|
+
['meow 记忆库', '宏记忆库'],
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
/** 把一段子插件文本改成 macro 的说法;aliases=true 时原样返回。 */
|
|
33
|
+
export function brandText(text, { aliases = false } = {}) {
|
|
34
|
+
if (aliases === true) return text
|
|
35
|
+
if (typeof text !== 'string' || text.length === 0) return text
|
|
36
|
+
let out = text
|
|
37
|
+
for (const [from, to] of BRAND_MAP) {
|
|
38
|
+
if (out.includes(from)) out = out.split(from).join(to)
|
|
39
|
+
}
|
|
40
|
+
return out
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 这段文本里出现了哪几条旧名字(顺序同 BRAND_MAP)。 */
|
|
44
|
+
export function nameHints(text) {
|
|
45
|
+
if (typeof text !== 'string' || text.length === 0) return []
|
|
46
|
+
return BRAND_MAP.filter(([from]) => text.includes(from)).map(([from, to]) => ({ from, to }))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 「以上是原文」这句话的固定标记;用来保证重复过一遍不会叠两条。 */
|
|
50
|
+
export const ORIGINAL_MARK = '一个字没动'
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 返回的是记忆原文的场合(memory_search / memory_read)用它,而不是 brandText。
|
|
54
|
+
*
|
|
55
|
+
* 为什么不能直接替换:BRAND_MAP 里既有工具名(nebula_recall)也有自称(meow-memory),
|
|
56
|
+
* 而这两串完全可能出现在**用户自己存的正文**里——记忆是「事实的账本」,读回来的必须
|
|
57
|
+
* 跟存进去的一模一样。改了就是数据失真,模型照着读去改,还会把失真写回库里。
|
|
58
|
+
* 所以正文一个字不动,只在末尾补一句「旧名按宏记忆的说法叫什么」,
|
|
59
|
+
* 既不让模型拿着旧名去调(死指针),也不篡改内容。
|
|
60
|
+
*/
|
|
61
|
+
export function withNameHints(text, { aliases = false } = {}) {
|
|
62
|
+
if (aliases === true) return text
|
|
63
|
+
if (typeof text !== 'string' || text.length === 0) return text
|
|
64
|
+
if (text.includes(ORIGINAL_MARK)) return text
|
|
65
|
+
const hits = nameHints(text)
|
|
66
|
+
if (hits.length === 0) return text
|
|
67
|
+
const shown = hits.slice(0, 6).map((h) => `${h.from} → ${h.to}`)
|
|
68
|
+
const more = hits.length > shown.length ? `;另外还有 ${hits.length - shown.length} 条旧名` : ''
|
|
69
|
+
return `${text}\n\n(以上正文是记忆原文,${ORIGINAL_MARK}。里面的旧名字按宏记忆的说法是:${shown.join(';')}${more}——要调就调箭头右边的。)`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 递归把一个值里的字符串全部改口(数组/对象都走一遍)。 */
|
|
73
|
+
export function brandDeep(value, options) {
|
|
74
|
+
if (typeof value === 'string') return brandText(value, options)
|
|
75
|
+
if (Array.isArray(value)) return value.map((item) => brandDeep(item, options))
|
|
76
|
+
if (value && typeof value === 'object') {
|
|
77
|
+
const out = {}
|
|
78
|
+
for (const [k, v] of Object.entries(value)) out[k] = brandDeep(v, options)
|
|
79
|
+
return out
|
|
80
|
+
}
|
|
81
|
+
return value
|
|
82
|
+
}
|