macro-memory 0.0.0-stage → 0.9.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 +384 -2
- package/bin/macro-memory.mjs +732 -0
- package/docs/CHANGELOG.md +250 -0
- package/docs/MAC.md +156 -0
- package/docs/images/_architecture.svg +70 -0
- package/docs/images/_memory-flow.svg +75 -0
- package/docs/images/_token-budget.svg +40 -0
- package/docs/images/architecture.png +0 -0
- package/docs/images/hero.png +0 -0
- package/docs/images/memory-flow.png +0 -0
- package/docs/images/src/graph.html +78 -0
- package/docs/images/src/hero.html +88 -0
- package/docs/images/src/panel.html +63 -0
- package/docs/images/src/pipeline.html +97 -0
- package/docs/images/token-budget.png +0 -0
- package/lib/client.js +3811 -0
- package/lib/core/agents-file.js +95 -0
- package/lib/core/client.js +313 -0
- package/lib/core/compat/dsh-llm.js +78 -0
- package/lib/core/config.js +117 -0
- package/lib/core/engine.js +456 -0
- package/lib/core/fence.js +109 -0
- package/lib/core/index.js +102 -0
- package/lib/core/migrate.js +70 -0
- package/lib/core/mini-host.js +324 -0
- package/lib/core/owner.js +347 -0
- package/lib/core/paths.js +95 -0
- package/lib/core/serve.js +378 -0
- package/lib/core/trust.js +173 -0
- package/lib/index.js +567 -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 +528 -0
- package/lib/macro/defs/dream.js +24 -0
- package/lib/macro/defs/link.js +408 -0
- package/lib/macro/defs/project.js +237 -0
- package/lib/macro/defs/read.js +160 -0
- package/lib/macro/defs/remember.js +411 -0
- package/lib/macro/defs/search.js +899 -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 +188 -0
- package/lib/macro/injectledger.js +843 -0
- package/lib/macro/linkflow.js +391 -0
- package/lib/macro/links.js +448 -0
- package/lib/macro/neighbor.js +1083 -0
- package/lib/macro/panel.js +96 -0
- package/lib/macro/platform.js +134 -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/shortid.js +114 -0
- package/lib/macro/snapshot.js +700 -0
- package/lib/macro/spend.js +312 -0
- package/lib/macro/store.js +238 -0
- package/lib/macro/supersede.js +197 -0
- package/lib/macro/tokencost.js +90 -0
- package/lib/macro/tools.js +67 -0
- package/lib/mcp/server.js +196 -0
- package/lib/mcp/stdio.js +151 -0
- package/lib/ui/page.js +300 -0
- package/lib/ui/server.js +226 -0
- package/package.json +56 -4
- package/vendor/VENDOR.json +39 -0
- package/vendor/cosmokit/LICENSE +21 -0
- package/vendor/cosmokit/README.md +30 -0
- package/vendor/cosmokit/lib/index.js +418 -0
- package/vendor/cosmokit/lib/types/array.d.ts +15 -0
- package/vendor/cosmokit/lib/types/array.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/index.d.ts +13 -0
- package/vendor/cosmokit/lib/types/index.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/misc.d.ts +41 -0
- package/vendor/cosmokit/lib/types/misc.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/string.d.ts +89 -0
- package/vendor/cosmokit/lib/types/string.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/time.d.ts +19 -0
- package/vendor/cosmokit/lib/types/time.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/types.d.ts +42 -0
- package/vendor/cosmokit/lib/types/types.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/volatile.d.ts +40 -0
- package/vendor/cosmokit/lib/types/volatile.d.ts.map +1 -0
- package/vendor/cosmokit/package.json +32 -0
- package/vendor/cosmokit/src/array.ts +42 -0
- package/vendor/cosmokit/src/index.ts +12 -0
- package/vendor/cosmokit/src/misc.ts +78 -0
- package/vendor/cosmokit/src/string.ts +113 -0
- package/vendor/cosmokit/src/time.ts +92 -0
- package/vendor/cosmokit/src/types.ts +168 -0
- package/vendor/cosmokit/src/volatile.ts +86 -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 +526 -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 +3354 -0
- package/vendor/meow-memory/lib/client.js.map +7 -0
- package/vendor/meow-memory/lib/index.js +13500 -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 +104 -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 +104 -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 +104 -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/vendor/schemastery/LICENSE +21 -0
- package/vendor/schemastery/README.md +393 -0
- package/vendor/schemastery/lib/index.cjs +635 -0
- package/vendor/schemastery/lib/index.mjs +637 -0
- package/vendor/schemastery/lib/types/index.d.ts +211 -0
- package/vendor/schemastery/lib/types/index.d.ts.map +1 -0
- package/vendor/schemastery/package.json +39 -0
- package/vendor/schemastery/src/index.ts +962 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/engine —— 不依赖 DSH 的记忆引擎装配层。
|
|
3
|
+
*
|
|
4
|
+
* 它做的事,和 `lib/index.js` 的 `apply()` 是同两件:
|
|
5
|
+
* 1. 把两个引擎(meow / nebula)挂进一个「宿主」里,用 bridge 收下它们的工具与注入;
|
|
6
|
+
* 2. 用 `lib/macro/tools.js` 的 8 个统一工具重新组织,对外只暴露 memory_*。
|
|
7
|
+
*
|
|
8
|
+
* 区别只有一处:宿主不是 cordis,而是 `mini-host.js`。所以这个文件不 import
|
|
9
|
+
* `@deepseek-ai/*`、不读宿主环境变量,任何 agent(MCP / CLI / 自己的 loop)都能直接用它。
|
|
10
|
+
*
|
|
11
|
+
* 库的位置沿用 meow 自己的语义(这是「一份真身」的关键):
|
|
12
|
+
* workspace 库 = <workspace>/<projectDir>/memory.db (默认 .dsh-meow,与 DSH 侧同一个文件)
|
|
13
|
+
* 全局库 = <homeDir>/memory.db (默认 ~/.dsh-meow)
|
|
14
|
+
* 也就是说:DSH 里存的记忆,Codex 接进来就该看得见——不需要同步、不需要导入。
|
|
15
|
+
* dataDir(平台数据目录)只放配置、注入块、日志这些派生物,不放记忆正文。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import path from 'node:path'
|
|
19
|
+
import fs from 'node:fs'
|
|
20
|
+
import { createMiniHost } from './mini-host.js'
|
|
21
|
+
import { detectHomeDir, resolveDataDir } from './paths.js'
|
|
22
|
+
import { ensureTrust, isTrusted, listTrusted, trustWorkspace } from './trust.js'
|
|
23
|
+
import { checkSchema, readSchemaVersion, schemaRefusalText, stampSchema, SCHEMA_VERSION } from './migrate.js'
|
|
24
|
+
import { buildVisibility, createOwnerStore, normalizeScope, tagOwners } from './owner.js'
|
|
25
|
+
import { discover, pendingCount, serveFilePath, sharedCall } from './client.js'
|
|
26
|
+
import { createBridge, errText, wrapPlugin } from '../macro/bridge.js'
|
|
27
|
+
import { renderToolText } from '../macro/render.js'
|
|
28
|
+
import { createUnifiedTools } from '../macro/tools.js'
|
|
29
|
+
import { GUIDE_MAX_CHARS, GUIDE_TEXT, buildFullGuide, clampGuide, guideBudget } from '../macro/prompt.js'
|
|
30
|
+
import * as Meow from '../../vendor/meow-memory/lib/index.js'
|
|
31
|
+
import * as Nebula from '../../vendor/dsh-memory-nebula/lib/index.js'
|
|
32
|
+
|
|
33
|
+
/** 统一工具的默认等待上限(毫秒):引擎装配是同步的,这个值只在极端情况下兜底。 */
|
|
34
|
+
export const DEFAULT_TOOL_WAIT_MS = 30000
|
|
35
|
+
|
|
36
|
+
/** core 的版本号,由 package.json 之外单独维护一份,方便 CLI 自报家门。 */
|
|
37
|
+
export const CORE_NAME = 'macro-memory'
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 造一个会话执行上下文。
|
|
41
|
+
*
|
|
42
|
+
* 工具的写路径要回答两个问题:「这条记忆属于哪个工作区」「谁写的」。
|
|
43
|
+
* DSH 的答案是 `exec.agent.session.header.cwd` —— core 里照同一条取法,
|
|
44
|
+
* 这样同一份代码在两边对同一个目录算出的 key 一致(库才真的是同一份)。
|
|
45
|
+
*/
|
|
46
|
+
export function makeExec({ cwd, sessionId, agentId, homeDir, visibility } = {}) {
|
|
47
|
+
const header = { cwd: typeof cwd === 'string' ? cwd : process.cwd() }
|
|
48
|
+
const session = { id: sessionId ?? `core-${process.pid}`, header }
|
|
49
|
+
return {
|
|
50
|
+
agent: { session, id: agentId ?? 'core' },
|
|
51
|
+
session,
|
|
52
|
+
homeDir,
|
|
53
|
+
// 可见性(0.9.0):引擎在执行前把「这个 agent 看不见哪些 id」算好挂在这里,
|
|
54
|
+
// meow 侧只问一句 `hidden.has(id)`。不挂就是「不限制」——老库、单 agent 场景照旧。
|
|
55
|
+
...(visibility ? { visibility } : {}),
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 信任门拒绝时给的话。
|
|
61
|
+
* 要求:说清「是哪个目录 / 为什么」,并给一条能直接粘进终端的命令——
|
|
62
|
+
* 只说「被拒绝」等于把排查成本丢回给人。
|
|
63
|
+
*/
|
|
64
|
+
function refusalText(guard, homeDir) {
|
|
65
|
+
const lines = [`这个工作区还没被信任,记忆读写已暂停:${guard.dir}`]
|
|
66
|
+
if (guard.reason === 'external' && Array.isArray(guard.leftovers)) {
|
|
67
|
+
const shown = guard.leftovers.slice(0, 3).join('、')
|
|
68
|
+
const more = guard.leftovers.length > 3 ? ` 等 ${guard.leftovers.length} 项` : ''
|
|
69
|
+
lines.push(`目录里有不是本插件产生的文件(${shown}${more}),像是在别处建好的库被带进来了。`)
|
|
70
|
+
lines.push('不猜、不写,是为了避免把别人的「记忆」当成你的——这条是有意的。')
|
|
71
|
+
} else if (guard.reason === 'policy-never') {
|
|
72
|
+
lines.push('当前是 trust=never:任何工作区都不自动信任,只认显式信任。')
|
|
73
|
+
} else if (typeof guard.reason === 'string' && guard.reason.startsWith('unreadable:')) {
|
|
74
|
+
lines.push(`目录读不出来(${guard.reason.slice('unreadable:'.length)}),先修权限再谈信任。`)
|
|
75
|
+
}
|
|
76
|
+
lines.push('确认这是你自己的项目后,跑一次即可恢复(写进信任表,立刻生效,不需要重启):')
|
|
77
|
+
lines.push(` npx -y macro-memory trust "${guard.workspace}"`)
|
|
78
|
+
lines.push(`信任表在 ${path.join(homeDir, 'trusted-workspaces')},与 DSH 的 /meow-trust 同一份——两边互相认。`)
|
|
79
|
+
return lines.join('\n')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 装配引擎。
|
|
84
|
+
*
|
|
85
|
+
* @param {object} [opts]
|
|
86
|
+
* @param {string} [opts.dataDir] 平台数据目录(配置/日志),缺省按平台算
|
|
87
|
+
* @param {string} [opts.projectDir] workspace 内的库目录名,默认 .dsh-meow(与 DSH 共用)
|
|
88
|
+
* @param {string} [opts.homeDir] 全局库目录,缺省 = detectHomeDir()(装了 DSH 就用它的 storages,否则 ~/.dsh-meow)
|
|
89
|
+
* @param {object} [opts.meow] 覆盖 meow 引擎配置
|
|
90
|
+
* @param {object} [opts.nebula] 覆盖星图引擎配置
|
|
91
|
+
* @param {object} [opts.macro] 覆盖融合层配置(guideMaxChars 等)
|
|
92
|
+
* @param {boolean} [opts.verbose] 打调试日志到 stderr
|
|
93
|
+
* @param {boolean} [opts.withNebula] 是否挂星图引擎,默认 true
|
|
94
|
+
* @param {number} [opts.waitMs] 等引擎工具挂上的上限(毫秒),默认 5000;0 = 不等
|
|
95
|
+
* @param {'auto'|'always'|'never'} [opts.trust] 信任策略,默认 auto(只信空库/本机新建)
|
|
96
|
+
*/
|
|
97
|
+
export async function createMacroEngine(opts = {}) {
|
|
98
|
+
const dataDir = resolveDataDir({ dir: opts.dataDir })
|
|
99
|
+
const log = opts.verbose === true ? (...a) => console.error('[macro-memory]', ...a) : () => {}
|
|
100
|
+
const warn = (...a) => console.error('[macro-memory]', ...a)
|
|
101
|
+
|
|
102
|
+
const host = createMiniHost({ verbose: opts.verbose === true, logger: opts.logger })
|
|
103
|
+
const bridge = createBridge({ log, warn })
|
|
104
|
+
|
|
105
|
+
const meowCfg = {
|
|
106
|
+
projectDir: opts.projectDir ?? '.dsh-meow',
|
|
107
|
+
homeDir: opts.homeDir ?? detectHomeDir(),
|
|
108
|
+
...(opts.meow ?? {}),
|
|
109
|
+
}
|
|
110
|
+
const nebulaCfg = { ...(opts.nebula ?? {}) }
|
|
111
|
+
const macroCfg = { guideMaxChars: GUIDE_MAX_CHARS, ...(opts.macro ?? {}) }
|
|
112
|
+
const meowDirReader = () => (typeof meowCfg.projectDir === 'string' && meowCfg.projectDir.length > 0
|
|
113
|
+
? meowCfg.projectDir
|
|
114
|
+
: '.dsh-meow')
|
|
115
|
+
|
|
116
|
+
host.ctx.plugin(wrapPlugin(Meow, bridge, 'meow'), meowCfg)
|
|
117
|
+
if (opts.withNebula !== false) {
|
|
118
|
+
host.ctx.plugin(wrapPlugin(Nebula, bridge, 'nebula'), nebulaCfg)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const defs = createUnifiedTools({ bridge, config: macroCfg, meowDir: meowDirReader })
|
|
122
|
+
const byName = new Map(defs.map((d) => [d.name, d]))
|
|
123
|
+
|
|
124
|
+
// 信任门的落点:meow 自己算出来的全局目录(装了 DSH 就是 DSH 的 storages,没装就是
|
|
125
|
+
// ~/.dsh-meow)。core 不另建一套,这样两边共用同一张信任表。
|
|
126
|
+
const homeNow = () => Meow.activeHomeDir() ?? ''
|
|
127
|
+
const trustPolicy = opts.trust ?? 'auto'
|
|
128
|
+
// 共享层开关(PLAN 第 5 节):`auto` = 探到 serve 就走服务、探不到就直连;
|
|
129
|
+
// `off` = 永远直连(serve 自己、以及要「结果可复现」的探针都用这档)。
|
|
130
|
+
const serveMode = opts.serve === 'off' ? 'off' : 'auto'
|
|
131
|
+
// 关掉之后不再服务:MCP / CLI 里进程会退,但库文件句柄与定时器要跟着停,
|
|
132
|
+
// 否则「关完了还在读写」这种半死状态最难查——所以这里显式立一个标志。
|
|
133
|
+
let closed = false
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 库版本门(PLAN P0-5 / U8)。只在库文件**已经存在**时读——库还没建就交给 meow
|
|
137
|
+
* 自己建,不因为「检查版本」顺手创建一个空库(那会让 doctor 产生副作用)。
|
|
138
|
+
* 读不出来(权重、坏文件)一律放行并在回执里记账:诊断功能不该挡死主流程。
|
|
139
|
+
*/
|
|
140
|
+
function schemaGate(cwd, dir) {
|
|
141
|
+
const file = path.join(cwd, dir, 'memory.db')
|
|
142
|
+
if (!fs.existsSync(file)) return { checked: false, ok: true, readOnly: false, reason: null, dbVersion: null, adapterVersion: SCHEMA_VERSION }
|
|
143
|
+
try {
|
|
144
|
+
return { checked: true, ...checkSchema(Meow.getDb(cwd, dir)) }
|
|
145
|
+
} catch (err) {
|
|
146
|
+
log('版本门读不出来,放行:', errText(err))
|
|
147
|
+
return { checked: false, ok: true, readOnly: false, reason: 'unreadable', dbVersion: null, adapterVersion: SCHEMA_VERSION }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 归属账(PLAN P1-2 的第一半):谁写的、能给谁看。
|
|
153
|
+
*
|
|
154
|
+
* 只在库文件已经存在时打开——和版本门同一个理由:检查不该顺手建库。
|
|
155
|
+
* 打不开就返回 null,整个归属环节当没发生:它永远不该挡住「记一条记忆」这件事。
|
|
156
|
+
*/
|
|
157
|
+
const ownerStores = new Map()
|
|
158
|
+
function ownerStoreFor(cwd, dir) {
|
|
159
|
+
const file = path.join(cwd, dir, 'memory.db')
|
|
160
|
+
if (!fs.existsSync(file)) return null
|
|
161
|
+
if (ownerStores.has(file)) return ownerStores.get(file)
|
|
162
|
+
try {
|
|
163
|
+
const store = createOwnerStore(Meow.getDb(cwd, dir))
|
|
164
|
+
ownerStores.set(file, store)
|
|
165
|
+
return store
|
|
166
|
+
} catch (err) {
|
|
167
|
+
log('归属账打不开,跳过:', errText(err))
|
|
168
|
+
return null
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 写工具要记账,读工具要标注;其余(dream / admin / link)不碰归属。
|
|
173
|
+
const WRITE_TOOLS = new Set(['memory_remember', 'memory_update'])
|
|
174
|
+
const READ_TOOLS = new Set(['memory_search', 'memory_read', 'memory_project'])
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 从返回值里认出这条记忆的 id:结构化字段优先,认不出再从回执文本里捞 `id=xxxx`。
|
|
178
|
+
*
|
|
179
|
+
* 两条判据都得**放连字符进来**:meow 的 id 是 `<base36 时间戳>-<26 位 uuid>`(`newId()`,
|
|
180
|
+
* vendor/meow-memory/lib/index.js:421),而回执只印 `id.slice(0, 8)`——那 8 位是**时间前缀**,
|
|
181
|
+
* 精度 36 ms。原来这里写 `[0-9a-z]{6,}`,含 `-` 的完整 id 一律判非法,于是每次都退化成从回执
|
|
182
|
+
* 文本里捞那 8 位;两条记忆在同一 36 ms 窗口里写入就会捞到**同一个**短 id,后写的那次把前一条的
|
|
183
|
+
* 归属行覆盖掉(`record()` 走 UPDATE 分支)。Windows 上每条写入跨毫秒,撞不上;Linux CI 上三条
|
|
184
|
+
* 连写落在同一窗口,probe-share 的 S5 就把 soul/rules 都读成了最后一次写的 `global`。
|
|
185
|
+
* 结构化的 `value.id` 一直都是完整的,只是被这条正则挡住了。
|
|
186
|
+
*/
|
|
187
|
+
function newIdOf(value, text) {
|
|
188
|
+
const cands = [value?.id, value?.memory?.id, value?.result?.id, value?.value?.id]
|
|
189
|
+
for (const c of cands) {
|
|
190
|
+
if (typeof c === 'string' && /^[0-9a-z][0-9a-z-]{5,}$/.test(c)) return c
|
|
191
|
+
}
|
|
192
|
+
// 兜底只在结构化字段缺失时才走:文本里只有 8 位前缀,撞了就没法分辨,能不用就不用。
|
|
193
|
+
const m = /id\s*=\s*([0-9a-z][0-9a-z-]{5,})/.exec(String(text ?? ''))
|
|
194
|
+
return m ? m[1] : null
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** 记一条归属。失败不抛:归属是增强,写坏记忆才是事故。 */
|
|
198
|
+
function recordOwnership(name, args, value, text, store, self) {
|
|
199
|
+
try {
|
|
200
|
+
if (name === 'memory_remember') {
|
|
201
|
+
const id = newIdOf(value, text)
|
|
202
|
+
if (!id) return
|
|
203
|
+
store.record(id, {
|
|
204
|
+
owner: self,
|
|
205
|
+
level: args.level,
|
|
206
|
+
scope: normalizeScope(args.scope, args.level),
|
|
207
|
+
visibility: normalizeScope(args.visibility, args.level),
|
|
208
|
+
})
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
if (name === 'memory_update') {
|
|
212
|
+
const id = typeof args.id === 'string' ? args.id : newIdOf(value, text)
|
|
213
|
+
if (id) store.touch(id, { editor: self })
|
|
214
|
+
}
|
|
215
|
+
} catch (err) {
|
|
216
|
+
log('归属记账失败(不影响写入):', errText(err))
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 引擎的 apply 是异步的:meow 要迁移库、等设置服务,工具会在几百毫秒后才挂上。
|
|
221
|
+
// 这里等它一次(默认 5s,成功即返回),否则「装配好了」的引擎前几秒是半空壳,
|
|
222
|
+
// MCP 客户端 tools/list 会少一半工具,而且没有任何报错。
|
|
223
|
+
const waitMs = Number.isFinite(opts.waitMs) ? opts.waitMs : 5000
|
|
224
|
+
if (opts.wait !== false) {
|
|
225
|
+
await bridge.awaitTool('memory_remember', waitMs)
|
|
226
|
+
await bridge.awaitTool('nebula_recall', Math.min(waitMs, 1500))
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 引擎工具是同步注册的(apply 里没有异步等待),这里只做一次兜底确认:
|
|
230
|
+
// 少一个子引擎工具时,统一工具会自己回「尚未就绪」,不在这里提前抛。
|
|
231
|
+
const ready = {
|
|
232
|
+
meow: bridge.has('memory_remember') && bridge.has('memory_search'),
|
|
233
|
+
nebula: bridge.has('nebula_recall'),
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
name: CORE_NAME,
|
|
238
|
+
dataDir,
|
|
239
|
+
projectDir: meowCfg.projectDir,
|
|
240
|
+
homeDir: meowCfg.homeDir,
|
|
241
|
+
/** 8 个统一工具的声明(给 MCP / function calling 用,不含执行体)。 */
|
|
242
|
+
tools: defs.map((d) => ({ name: d.name, description: d.description, parameters: d.parameters })),
|
|
243
|
+
ready,
|
|
244
|
+
/** 直接拿到某个统一工具的原始定义(测试与 CLI 用)。 */
|
|
245
|
+
def(name) {
|
|
246
|
+
return byName.get(name)
|
|
247
|
+
},
|
|
248
|
+
/**
|
|
249
|
+
* 调一个统一工具。
|
|
250
|
+
*
|
|
251
|
+
* 开工前先过信任门:工作区里躺着一个不是本机创建的记忆库时(clone 了别人的仓库),
|
|
252
|
+
* 一律不读不写,回一段能照着敲的说明——比抛一个「写入被拒绝」更有用。
|
|
253
|
+
*
|
|
254
|
+
* @returns {Promise<{text: string, value: any, refused?: boolean}>}
|
|
255
|
+
*/
|
|
256
|
+
async call(name, args = {}, execOptions = {}) {
|
|
257
|
+
if (closed) throw new Error('macro-memory:引擎已关闭,不再提供服务')
|
|
258
|
+
const def = byName.get(name)
|
|
259
|
+
if (!def) throw new Error(`macro-memory:没有这个工具 ${name}(有:${[...byName.keys()].join('、')})`)
|
|
260
|
+
const cwd = path.resolve(execOptions.cwd ?? process.cwd())
|
|
261
|
+
const guard = ensureTrust({
|
|
262
|
+
workspace: cwd,
|
|
263
|
+
homeDir: homeNow(),
|
|
264
|
+
projectDir: meowDirReader(),
|
|
265
|
+
policy: trustPolicy,
|
|
266
|
+
})
|
|
267
|
+
if (!guard.trusted) {
|
|
268
|
+
return { text: refusalText(guard, homeNow()), value: null, refused: true }
|
|
269
|
+
}
|
|
270
|
+
// 信任门过了才谈版本:库比适配层新时一律只读,不猜新字段的规矩。
|
|
271
|
+
const schema = schemaGate(cwd, meowDirReader())
|
|
272
|
+
if (schema.readOnly) {
|
|
273
|
+
return { text: schemaRefusalText(schema), value: null, refused: true, schema }
|
|
274
|
+
}
|
|
275
|
+
// 共享层(PLAN 第 5、6 节 / P1-1 / U7 / U9):本机有 serve 在跑时,读写都交给它。
|
|
276
|
+
// 没有 `serve.json` 就是「没装服务」,这一支直接返回,一次 fs 读、零连接——
|
|
277
|
+
// 不装服务的人不该为这套东西付任何代价。
|
|
278
|
+
if (execOptions.noServe !== true && serveMode !== 'off') {
|
|
279
|
+
const share = await sharedCall({
|
|
280
|
+
dataDir,
|
|
281
|
+
tool: name,
|
|
282
|
+
args,
|
|
283
|
+
cwd,
|
|
284
|
+
agentId: execOptions.agentId,
|
|
285
|
+
sessionId: execOptions.sessionId,
|
|
286
|
+
})
|
|
287
|
+
if (share.mode !== 'standalone') {
|
|
288
|
+
return { text: share.text, value: share.value, refused: share.refused === true, share }
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// 可见性要在执行**之前**算:别人的 private 条目连候选池都不该进,
|
|
292
|
+
// 过滤放在渲染之后等于先把答案给了再划掉——k 条被占位、时间轴也会漏。
|
|
293
|
+
//
|
|
294
|
+
// 注意这里是**两次**取 store,不是一次:`ownerStoreFor` 在库文件还不存在时返回 null
|
|
295
|
+
// (检查不该顺手建库),而「第一次写入」这个场景正是库还不存在——用前置那一次记账,
|
|
296
|
+
// 第一条记忆的归属就会永久丢失(probe-share 的 S0/S1 就这么抓到过)。
|
|
297
|
+
const selfId = typeof execOptions.agentId === 'string' && execOptions.agentId !== '' ? execOptions.agentId : 'core'
|
|
298
|
+
const before = ownerStoreFor(cwd, meowDirReader())
|
|
299
|
+
const visibility = before ? buildVisibility(before, selfId) : null
|
|
300
|
+
const exec = makeExec({ ...execOptions, cwd, ...(visibility ? { visibility } : {}) })
|
|
301
|
+
const value = await def.execute(args, exec)
|
|
302
|
+
let text = renderToolText(def, args, value)
|
|
303
|
+
// 归属(PLAN P1-2 / U6):写侧记账、读侧标注。整段失败只记日志,回执照旧。
|
|
304
|
+
const store = ownerStoreFor(cwd, meowDirReader())
|
|
305
|
+
if (store) {
|
|
306
|
+
const self = exec.agent?.id ?? 'core'
|
|
307
|
+
try {
|
|
308
|
+
if (WRITE_TOOLS.has(name)) recordOwnership(name, args, value, text, store, self)
|
|
309
|
+
if (READ_TOOLS.has(name)) text = tagOwners(text, store, self)
|
|
310
|
+
} catch (err) {
|
|
311
|
+
log('归属环节出错(不影响本次调用):', errText(err))
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return { text, value }
|
|
315
|
+
},
|
|
316
|
+
/** 这个工作区信不信任(只读,不写 marker)。 */
|
|
317
|
+
trustState(cwd = process.cwd()) {
|
|
318
|
+
const ws = path.resolve(cwd)
|
|
319
|
+
return {
|
|
320
|
+
workspace: ws,
|
|
321
|
+
dir: path.join(ws, meowDirReader()),
|
|
322
|
+
trusted: isTrusted(ws, homeNow()),
|
|
323
|
+
homeDir: homeNow(),
|
|
324
|
+
policy: trustPolicy,
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
/** 显式信任一个工作区(CLI 的 `macro-memory trust` 走这里)。 */
|
|
328
|
+
trust(cwd = process.cwd(), source = 'macro-memory:cli') {
|
|
329
|
+
const ws = path.resolve(cwd)
|
|
330
|
+
const ok = trustWorkspace(ws, { homeDir: homeNow(), source })
|
|
331
|
+
return { workspace: ws, trusted: ok, homeDir: homeNow() }
|
|
332
|
+
},
|
|
333
|
+
/** 已信任的工作区清单。 */
|
|
334
|
+
trustedList() {
|
|
335
|
+
return listTrusted(homeNow())
|
|
336
|
+
},
|
|
337
|
+
/** L0 常驻注入文本(字符上限按配置裁)。 */
|
|
338
|
+
injection({ maxChars } = {}) {
|
|
339
|
+
const budget = guideBudget(maxChars ?? macroCfg.guideMaxChars)
|
|
340
|
+
return clampGuide(GUIDE_TEXT, budget.value)
|
|
341
|
+
},
|
|
342
|
+
/** 库版本门状态(doctor / CLI migrate 用,只读不写)。 */
|
|
343
|
+
schemaState(cwd = process.cwd()) {
|
|
344
|
+
const ws = path.resolve(cwd)
|
|
345
|
+
return { workspace: ws, dir: path.join(ws, meowDirReader()), ...schemaGate(ws, meowDirReader()) }
|
|
346
|
+
},
|
|
347
|
+
/**
|
|
348
|
+
* 归属分布账(UI 概览 / 共享矩阵 / agent 页用,只读不写)。
|
|
349
|
+
*
|
|
350
|
+
* 只读的意思很具体:库不存在就返回空账,**不建库、不建表**——打开 UI 看一眼不该在
|
|
351
|
+
* 任何工作区里留下 `.dsh-meow/`。要看「各层真实条数」请调 `memory_admin action=stats`,
|
|
352
|
+
* 那是 meow 自己的账;这里只报「我们记过归属的那部分」,两者差多少正好说明还有多少
|
|
353
|
+
* 老条目没有归属(0.9 之前写的一律没有)。
|
|
354
|
+
*/
|
|
355
|
+
census(cwd = process.cwd()) {
|
|
356
|
+
const ws = path.resolve(cwd)
|
|
357
|
+
const dir = meowDirReader()
|
|
358
|
+
const store = ownerStoreFor(ws, dir)
|
|
359
|
+
const c = store && typeof store.census === 'function' ? store.census() : null
|
|
360
|
+
const agents = store && typeof store.agents === 'function' ? store.agents() : []
|
|
361
|
+
return {
|
|
362
|
+
workspace: ws,
|
|
363
|
+
dir: path.join(ws, dir),
|
|
364
|
+
dataDir,
|
|
365
|
+
...(c ?? { total: 0, scopes: { private: 0, workspace: 0, global: 0 }, layers: {}, matrix: {}, owners: 0 }),
|
|
366
|
+
agents: agents.map((a) => ({ owner: String(a.owner ?? ''), n: Number(a.n ?? 0), first: a.first ?? null, last: a.last ?? null })),
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
/**
|
|
370
|
+
* 共享层状态(doctor / `macro-memory stats` 用,只读不写)。
|
|
371
|
+
*
|
|
372
|
+
* 三档的说法与回执一致:`standalone` 没装服务、`shared` 服务在跑、`readonly`
|
|
373
|
+
* `serve.json` 还在但连不上(待补交队列这时候才有意义)。
|
|
374
|
+
*/
|
|
375
|
+
async shareState() {
|
|
376
|
+
const found = await discover(dataDir)
|
|
377
|
+
const mode = found.mode === 'stale' ? 'readonly' : found.mode
|
|
378
|
+
return {
|
|
379
|
+
mode,
|
|
380
|
+
dataDir,
|
|
381
|
+
serveFile: serveFilePath(dataDir),
|
|
382
|
+
pending: pendingCount(dataDir),
|
|
383
|
+
info: found.info
|
|
384
|
+
? { pid: found.info.pid, url: found.info.url, startedAt: found.info.startedAt, version: found.info.version }
|
|
385
|
+
: null,
|
|
386
|
+
stats: found.stats ?? null,
|
|
387
|
+
reason: found.reason ?? null,
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
/**
|
|
391
|
+
* 显式迁移(只在用户敲 `macro-memory migrate` 时跑)。
|
|
392
|
+
*
|
|
393
|
+
* 只做一件事:把 `PRAGMA user_version` 抬到适配层认识的版本。v0 → v1 的表结构
|
|
394
|
+
* 与 meow 建出来的完全一样(meow 从不写 user_version),所以这一步是**记账**,
|
|
395
|
+
* 不是改结构;将来加列/加表的 DDL 都挂在这个函数里,一处升级。
|
|
396
|
+
*
|
|
397
|
+
* 真写之前先 `vacuumInto` 出一份一致副本(不是 `copyFile`——WAL 里可能还有页);
|
|
398
|
+
* 库比适配层新时**不降级**:那条路只能靠升级适配层,猜着打戳等于替别人做决定。
|
|
399
|
+
*/
|
|
400
|
+
migrateSchema(cwd = process.cwd(), { to = SCHEMA_VERSION, dryRun = false } = {}) {
|
|
401
|
+
const ws = path.resolve(cwd)
|
|
402
|
+
const dir = meowDirReader()
|
|
403
|
+
const file = path.join(ws, dir, 'memory.db')
|
|
404
|
+
if (!fs.existsSync(file)) return { ok: false, reason: 'no-db', from: null, to, backup: null }
|
|
405
|
+
const db = Meow.getDb(ws, dir)
|
|
406
|
+
const from = readSchemaVersion(db)
|
|
407
|
+
if (from === null) return { ok: false, reason: 'unreadable', from, to, backup: null }
|
|
408
|
+
if (from === to) return { ok: true, reason: 'already', from, to, backup: null }
|
|
409
|
+
if (from > to) return { ok: false, reason: 'db-newer', from, to, backup: null }
|
|
410
|
+
if (dryRun) return { ok: true, reason: 'would-stamp', from, to, backup: null }
|
|
411
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
|
|
412
|
+
const backup = `${file}.bak-${stamp}`
|
|
413
|
+
try {
|
|
414
|
+
db.vacuumInto(backup)
|
|
415
|
+
} catch (err) {
|
|
416
|
+
warn('一致副本没做出来,退回整文件复制:', errText(err))
|
|
417
|
+
fs.copyFileSync(file, backup)
|
|
418
|
+
}
|
|
419
|
+
const written = stampSchema(db, to)
|
|
420
|
+
return { ok: true, reason: 'stamped', from, to: written, backup }
|
|
421
|
+
},
|
|
422
|
+
/** L2 完整手册(按需给)。 */
|
|
423
|
+
fullGuide() {
|
|
424
|
+
return buildFullGuide(bridge)
|
|
425
|
+
},
|
|
426
|
+
/** 引擎里实际注册到的子插件工具名(排障用)。 */
|
|
427
|
+
captured() {
|
|
428
|
+
return bridge.names()
|
|
429
|
+
},
|
|
430
|
+
stats() {
|
|
431
|
+
return { ...host.stats(), ready, captured: bridge.names().length }
|
|
432
|
+
},
|
|
433
|
+
async close() {
|
|
434
|
+
closed = true
|
|
435
|
+
try {
|
|
436
|
+
await host.dispose()
|
|
437
|
+
} catch (err) {
|
|
438
|
+
warn('拆卸宿主失败:', errText(err))
|
|
439
|
+
}
|
|
440
|
+
try {
|
|
441
|
+
await Meow.closeAllDbs?.()
|
|
442
|
+
} catch (err) {
|
|
443
|
+
warn('关库失败:', errText(err))
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
await Nebula.closeAllDbs?.()
|
|
447
|
+
} catch {
|
|
448
|
+
/* 星图没导出这个函数是正常的 */
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
export { writeConfigFile } from './config.js'
|
|
455
|
+
export { createMiniHost } from './mini-host.js'
|
|
456
|
+
export { defaultDataDir, resolveDataDir, dbDirOf, CONFIG_FILE } from './paths.js'
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文件围栏:唯一会碰用户文件的地方(PLAN-UNIVERSAL §8.3)。
|
|
3
|
+
*
|
|
4
|
+
* 规矩三条,写死在实现里:
|
|
5
|
+
* 1. **块外零改动**:只做字符串拼接/替换,绝不对块外内容做 EOL 归一化、trim、排序。
|
|
6
|
+
* 2. **幂等**:同一份 L0 写两次,文件字节不变(begin/end 之间原地替换,不叠加)。
|
|
7
|
+
* 3. **可还原**:块里用 `sep="N"` 记住追加时我们额外插了几个换行,摘块时按它精确回收——
|
|
8
|
+
* 用户原有文件(哪怕以两个空行结尾)能回到**逐字节**原样,而不是「差不多」。
|
|
9
|
+
*
|
|
10
|
+
* 为什么把 sep 记在块里而不是外部账本:卸载可能发生在另一个进程、另一个工作区、甚至另一台机器,
|
|
11
|
+
* 只有文件自己带着「我是怎么被放进去的」才靠得住。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const FENCE_BEGIN = '<!-- macro-memory:begin'
|
|
15
|
+
export const FENCE_END = '<!-- macro-memory:end -->'
|
|
16
|
+
export const FENCE_VERSION = 1
|
|
17
|
+
|
|
18
|
+
const BEGIN_RE = /<!--\s*macro-memory:begin(?:\s+sep="(\d+)")?(?:\s+v="(\d+)")?\s*-->/
|
|
19
|
+
const END_RE = /<!--\s*macro-memory:end\s*-->/
|
|
20
|
+
|
|
21
|
+
/** 文件里用的是 LF 还是 CRLF——按第一处换行判定,全空或单行视为 LF。 */
|
|
22
|
+
export function detectEol(text) {
|
|
23
|
+
const i = text.indexOf('\n')
|
|
24
|
+
return i > 0 && text[i - 1] === '\r' ? '\r\n' : '\n'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 块正文。`l0` 是引擎给的常驻注入文本(1,266 字符那一份),不在这里改写语义,
|
|
29
|
+
* 只加一行业主说明(谁维护、怎么更新、怎么摘)。
|
|
30
|
+
*/
|
|
31
|
+
export function renderBlock(l0, { sep = 0, eol = '\n', version = FENCE_VERSION } = {}) {
|
|
32
|
+
const body = String(l0 ?? '').replace(/\r\n/g, '\n').replace(/\s+$/, '')
|
|
33
|
+
const lines = [
|
|
34
|
+
`${FENCE_BEGIN} sep="${sep}" v="${version}" -->`,
|
|
35
|
+
`<!-- 这一块由 macro-memory 维护,不要手改:更新用 \`macro-memory refresh\`,摘掉用 \`macro-memory uninstall\` -->`,
|
|
36
|
+
'',
|
|
37
|
+
body,
|
|
38
|
+
FENCE_END,
|
|
39
|
+
]
|
|
40
|
+
return lines.join(eol)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 找到块的区间;没有则 null。begin 有、end 没有也算「坏了」,交给调用方决定怎么报。 */
|
|
44
|
+
export function locate(text) {
|
|
45
|
+
const b = BEGIN_RE.exec(text)
|
|
46
|
+
if (!b) return null
|
|
47
|
+
const rest = text.slice(b.index + b[0].length)
|
|
48
|
+
const e = END_RE.exec(rest)
|
|
49
|
+
if (!e) return { start: b.index, end: null, sep: Number(b[1] ?? 0), broken: true }
|
|
50
|
+
return {
|
|
51
|
+
start: b.index,
|
|
52
|
+
end: b.index + b[0].length + e.index + e[0].length,
|
|
53
|
+
sep: Number(b[1] ?? 0),
|
|
54
|
+
version: Number(b[2] ?? 1),
|
|
55
|
+
broken: false,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 写入/更新块。返回新文本与做了什么(`created` 文件原本不存在、`appended` 追加、`replaced` 原地替换)。
|
|
61
|
+
* 注意:`created` 与 `appended` 的区别要由调用方按「原文本是否为空」自己判——这里只报事实。
|
|
62
|
+
*/
|
|
63
|
+
export function spliceBlock(text, l0, { eol } = {}) {
|
|
64
|
+
const src = String(text ?? '')
|
|
65
|
+
const useEol = eol ?? detectEol(src)
|
|
66
|
+
const hit = locate(src)
|
|
67
|
+
if (hit && !hit.broken) {
|
|
68
|
+
// 原地替换:保留原来的 sep(当初怎么插的就还是怎么插的),块外一个字不动
|
|
69
|
+
const block = renderBlock(l0, { sep: hit.sep, eol: useEol })
|
|
70
|
+
return {
|
|
71
|
+
text: src.slice(0, hit.start) + block + src.slice(hit.end),
|
|
72
|
+
action: 'replaced',
|
|
73
|
+
sep: hit.sep,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// 追加:sep = 为了和上文隔开我们额外插了几个 eol
|
|
77
|
+
let sep = 0
|
|
78
|
+
if (src.length > 0) {
|
|
79
|
+
sep = src.endsWith(useEol + useEol) ? 0 : src.endsWith(useEol) ? 1 : 2
|
|
80
|
+
}
|
|
81
|
+
const block = renderBlock(l0, { sep, eol: useEol })
|
|
82
|
+
const pad = sep === 0 ? '' : useEol.repeat(sep)
|
|
83
|
+
return { text: src + pad + block + useEol, action: src.length === 0 ? 'created' : 'appended', sep }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 摘块。按 `sep` 精确回收当初插入的那几个换行,块后那个换行也是我们加的。
|
|
88
|
+
* 返回 `{ text, removed, broken }`——`broken` 表示 begin 在、end 不在(只报,不改)。
|
|
89
|
+
*/
|
|
90
|
+
export function stripBlock(text) {
|
|
91
|
+
const src = String(text ?? '')
|
|
92
|
+
const hit = locate(src)
|
|
93
|
+
if (!hit) return { text: src, removed: false, broken: false }
|
|
94
|
+
if (hit.broken) return { text: src, removed: false, broken: true }
|
|
95
|
+
const eol = detectEol(src)
|
|
96
|
+
const padLen = hit.sep * eol.length
|
|
97
|
+
const start = Math.max(0, hit.start - padLen)
|
|
98
|
+
// 我们 append 时在块后补了一个 eol;只有当它确实紧跟块尾时才吃
|
|
99
|
+
const eatsTail = src.slice(hit.end, hit.end + eol.length) === eol
|
|
100
|
+
const end = eatsTail ? hit.end + eol.length : hit.end
|
|
101
|
+
const before = src.slice(0, start)
|
|
102
|
+
const after = src.slice(end)
|
|
103
|
+
return { text: before + after, removed: true, broken: false, sep: hit.sep }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 摘完之后这个文件是不是「本来就不该存在」(只剩空白):是则调用方应当删掉它。 */
|
|
107
|
+
export function looksEmpty(text) {
|
|
108
|
+
return String(text ?? '').trim().length === 0
|
|
109
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/index —— 这个包对外的「core 面」:装配一个引擎、认出库在哪、把写交给谁。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要这一支(PLAN §11.2):`exports` 里 `"."` 与 `"./core"` 都指向这里,于是
|
|
5
|
+
*
|
|
6
|
+
* import { createMacroEngine } from 'macro-memory/core'
|
|
7
|
+
*
|
|
8
|
+
* 就能拿到整套 core,而不用记住 `engine.js` / `owner.js` / `serve.js` 各在哪一格。
|
|
9
|
+
* 这不是又一层实现——**这里一行逻辑都不写**,只把已经存在、已经有判据盯着的东西
|
|
10
|
+
* 摆到门口。多写一行「顺手做点什么」的代码,就多一处没人盯的口径。
|
|
11
|
+
*
|
|
12
|
+
* 分三组摆:
|
|
13
|
+
* 1. 装配与位置:引擎、数据目录、配置文件;
|
|
14
|
+
* 2. 归属与可见性:谁写的、能给谁看(PRIVATE / workspace / global);
|
|
15
|
+
* 3. 共享与围栏:单写者服务、待补交队列、AGENTS.md 的块。
|
|
16
|
+
*
|
|
17
|
+
* 不给的东西(故意的):`lib/macro/**` 的八层实现细节与 `lib/ui/**` 的页面。
|
|
18
|
+
* 前者是内部构造,改起来不该被外部调用固定住;后者在 `macro-memory/ui`。
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
CONFIG_FILE,
|
|
23
|
+
CORE_NAME,
|
|
24
|
+
DEFAULT_TOOL_WAIT_MS,
|
|
25
|
+
createMacroEngine,
|
|
26
|
+
createMiniHost,
|
|
27
|
+
dbDirOf,
|
|
28
|
+
defaultDataDir,
|
|
29
|
+
makeExec,
|
|
30
|
+
resolveDataDir,
|
|
31
|
+
writeConfigFile,
|
|
32
|
+
} from './engine.js'
|
|
33
|
+
|
|
34
|
+
export { CONFIG_VERSION, defaultConfig, loadConfig, mergeConfig, saveConfig } from './config.js'
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
DEFAULT_SCOPE_BY_LEVEL,
|
|
38
|
+
SCOPES,
|
|
39
|
+
buildVisibility,
|
|
40
|
+
createOwnerStore,
|
|
41
|
+
defaultScopeOf,
|
|
42
|
+
labelOf,
|
|
43
|
+
normalizeScope,
|
|
44
|
+
tagOwners,
|
|
45
|
+
} from './owner.js'
|
|
46
|
+
|
|
47
|
+
export { serveStats, startServe } from './serve.js'
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
DEDUP_WINDOW,
|
|
51
|
+
PENDING_FILE,
|
|
52
|
+
SERVE_FILE,
|
|
53
|
+
discover,
|
|
54
|
+
enqueuePending,
|
|
55
|
+
flushPending,
|
|
56
|
+
makeRequestId,
|
|
57
|
+
modeNote,
|
|
58
|
+
offlineText,
|
|
59
|
+
pendingCount,
|
|
60
|
+
pendingFilePath,
|
|
61
|
+
readPending,
|
|
62
|
+
readServeInfo,
|
|
63
|
+
removeServeInfo,
|
|
64
|
+
request,
|
|
65
|
+
rewritePending,
|
|
66
|
+
serveFilePath,
|
|
67
|
+
sharedCall,
|
|
68
|
+
writeServeInfo,
|
|
69
|
+
} from './client.js'
|
|
70
|
+
|
|
71
|
+
export {
|
|
72
|
+
CREATION_IGNORE,
|
|
73
|
+
CREATION_PREFIX,
|
|
74
|
+
ensureTrust,
|
|
75
|
+
isCreationArtifact,
|
|
76
|
+
isTrusted,
|
|
77
|
+
listTrusted,
|
|
78
|
+
markerPathOf,
|
|
79
|
+
markersDirOf,
|
|
80
|
+
normalizeWorkspacePath,
|
|
81
|
+
readTrustMarker,
|
|
82
|
+
trustWorkspace,
|
|
83
|
+
untrustWorkspace,
|
|
84
|
+
} from './trust.js'
|
|
85
|
+
|
|
86
|
+
export { SCHEMA_VERSION, checkSchema, readSchemaVersion, schemaRefusalText, stampSchema } from './migrate.js'
|
|
87
|
+
|
|
88
|
+
export { APP_DIR_NAME, detectHomeDir, pathFor } from './paths.js'
|
|
89
|
+
|
|
90
|
+
export {
|
|
91
|
+
FENCE_BEGIN,
|
|
92
|
+
FENCE_END,
|
|
93
|
+
FENCE_VERSION,
|
|
94
|
+
detectEol,
|
|
95
|
+
locate,
|
|
96
|
+
looksEmpty,
|
|
97
|
+
renderBlock,
|
|
98
|
+
spliceBlock,
|
|
99
|
+
stripBlock,
|
|
100
|
+
} from './fence.js'
|
|
101
|
+
|
|
102
|
+
export { DEFAULT_TARGETS, applyBlock, inspectBlock, removeBlock, resolveTargets } from './agents-file.js'
|