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,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把 L0 写进宿主的「规矩文件」(AGENTS.md / CLAUDE.md / .cursor/rules/*)——
|
|
3
|
+
* PLAN-UNIVERSAL §8.3 的文件围栏,唯一会碰用户文件的能力。
|
|
4
|
+
*
|
|
5
|
+
* 这一层只管读/写/删,语言与判断都留在 fence.js;CLI 只负责打印。三条不可越线:
|
|
6
|
+
* · 坏块(begin 在、end 不在)一律拒改,把决定权交回用户;
|
|
7
|
+
* · 写盘用 utf8 无 BOM,且保留文件原有的 BOM(如果本来有);
|
|
8
|
+
* · 摘完只剩空白 → 删文件(留一个空 AGENTS.md 比不写更脏),并在回执里说清楚。
|
|
9
|
+
*/
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
import { spliceBlock, stripBlock, locate, looksEmpty, detectEol } from './fence.js'
|
|
13
|
+
|
|
14
|
+
/** 默认写 AGENTS.md:Codex、Cursor、Aider、大多数 MCP 宿主都认这个名字。 */
|
|
15
|
+
export const DEFAULT_TARGETS = ['AGENTS.md']
|
|
16
|
+
|
|
17
|
+
/** `--file a,b --file c` → ['AGENTS.md'…] 覆盖:rel 路径按工作区解析。 */
|
|
18
|
+
export function resolveTargets(cwd, { files } = {}) {
|
|
19
|
+
const list = []
|
|
20
|
+
const push = (v) => {
|
|
21
|
+
if (typeof v !== 'string') return
|
|
22
|
+
for (const piece of v.split(',')) {
|
|
23
|
+
const s = piece.trim()
|
|
24
|
+
if (s.length > 0) list.push(path.resolve(cwd, s))
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(files)) files.forEach(push)
|
|
28
|
+
else push(files)
|
|
29
|
+
if (list.length === 0) for (const t of DEFAULT_TARGETS) list.push(path.resolve(cwd, t))
|
|
30
|
+
return [...new Set(list)]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function readTarget(p) {
|
|
34
|
+
try {
|
|
35
|
+
const raw = fs.readFileSync(p, 'utf8')
|
|
36
|
+
return { exists: true, text: raw, bom: raw.startsWith('\uFEFF') }
|
|
37
|
+
} catch (err) {
|
|
38
|
+
if (err.code === 'ENOENT') return { exists: false, text: '', bom: false }
|
|
39
|
+
throw err
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function writeTarget(p, text, bom) {
|
|
44
|
+
fs.mkdirSync(path.dirname(p), { recursive: true })
|
|
45
|
+
fs.writeFileSync(p, (bom ? '\uFEFF' : '') + text, 'utf8')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 写入或刷新块。返回 `{ path, action, chars, bytes, eol }`;
|
|
50
|
+
* action ∈ created | appended | replaced。坏块抛错,不擅自修。
|
|
51
|
+
*/
|
|
52
|
+
export function applyBlock(p, l0) {
|
|
53
|
+
const cur = readTarget(p)
|
|
54
|
+
const hit = cur.exists ? locate(cur.text) : null
|
|
55
|
+
if (hit?.broken) {
|
|
56
|
+
const err = new Error(`${p} 里有 macro-memory 的 begin 标记但没有 end(多半被手改坏),先修好或删掉那半块再跑`)
|
|
57
|
+
err.code = 'MACRO_FENCE_BROKEN'
|
|
58
|
+
throw err
|
|
59
|
+
}
|
|
60
|
+
const res = spliceBlock(cur.text, l0)
|
|
61
|
+
if (res.text !== cur.text) writeTarget(p, res.text, cur.bom)
|
|
62
|
+
return {
|
|
63
|
+
path: p,
|
|
64
|
+
action: res.action,
|
|
65
|
+
chars: l0.length,
|
|
66
|
+
bytes: Buffer.byteLength(res.text, 'utf8'),
|
|
67
|
+
eol: detectEol(cur.text),
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 摘块。`{ path, status, deleted, broken }`;status ∈ removed | absent | skipped。
|
|
73
|
+
* 摘完只剩空白就删文件——只有当我们真的改动了它才删。
|
|
74
|
+
*/
|
|
75
|
+
export function removeBlock(p) {
|
|
76
|
+
const cur = readTarget(p)
|
|
77
|
+
if (!cur.exists) return { path: p, status: 'absent', deleted: false, broken: false }
|
|
78
|
+
const res = stripBlock(cur.text)
|
|
79
|
+
if (res.broken) return { path: p, status: 'skipped', deleted: false, broken: true }
|
|
80
|
+
if (!res.removed) return { path: p, status: 'absent', deleted: false, broken: false }
|
|
81
|
+
if (looksEmpty(res.text)) {
|
|
82
|
+
fs.unlinkSync(p)
|
|
83
|
+
return { path: p, status: 'removed', deleted: true, broken: false }
|
|
84
|
+
}
|
|
85
|
+
writeTarget(p, res.text, cur.bom)
|
|
86
|
+
return { path: p, status: 'removed', deleted: false, broken: false }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 只读检查:文件里有没有块、块是否完整——给 doctor / init 打印用。 */
|
|
90
|
+
export function inspectBlock(p) {
|
|
91
|
+
const cur = readTarget(p)
|
|
92
|
+
if (!cur.exists) return { path: p, exists: false, has: false, broken: false }
|
|
93
|
+
const hit = locate(cur.text)
|
|
94
|
+
return { path: p, exists: true, has: Boolean(hit), broken: Boolean(hit?.broken), bytes: Buffer.byteLength(cur.text, 'utf8') }
|
|
95
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core 的共享层客户端(PLAN 第 5、6 节 / P1-1 / U7 / U9)。
|
|
3
|
+
*
|
|
4
|
+
* 三档模式:
|
|
5
|
+
* standalone 没有 `serve.json`(没装服务)→ 直连本地库,一切照旧
|
|
6
|
+
* shared `serve.json` 在、探得通 → 读写都交给服务(它是唯一写连接)
|
|
7
|
+
* readonly `serve.json` 在、探不通(服务被 kill 了 / 还没起来)→ 不写库,
|
|
8
|
+
* 这一条先落 `pending.jsonl`,服务回来自动补交
|
|
9
|
+
*
|
|
10
|
+
* 「探不通也排队」而不是「探不通就自己写」,是因为:有 `serve.json` 说明这台机器
|
|
11
|
+
* 上**本来**有个单写者,两个写者同时开库才是真正会丢数据的那种坏。宁可让用户
|
|
12
|
+
* 多敲一次 `macro-memory serve`,也不让两条连接各写一半。
|
|
13
|
+
*
|
|
14
|
+
* 只有一条例外:令牌对不上(`401`)→ 当成「这不是我们的服务」,回落直连。
|
|
15
|
+
* 否则换个用户、换个 dataDir 的旧 `serve.json` 会把整台机器锁成只读。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import crypto from 'node:crypto'
|
|
19
|
+
import fs from 'node:fs'
|
|
20
|
+
import http from 'node:http'
|
|
21
|
+
import path from 'node:path'
|
|
22
|
+
import { readJsonl } from '../macro/store.js'
|
|
23
|
+
import { isWindows } from '../macro/platform.js'
|
|
24
|
+
|
|
25
|
+
/** 服务发现文件(与数据目录同级,跟着 dataDir 隔离——探针各用各的临时目录)。 */
|
|
26
|
+
export const SERVE_FILE = 'serve.json'
|
|
27
|
+
/** 服务不通时待补交的写队列:一行一条 JSON,先来先补。 */
|
|
28
|
+
export const PENDING_FILE = 'pending.jsonl'
|
|
29
|
+
/** 探测超时:本机回环,300 ms 还没应就算不通。 */
|
|
30
|
+
const PROBE_TIMEOUT_MS = 300
|
|
31
|
+
/** 正常调用超时:写一条记忆不该慢过这个数。 */
|
|
32
|
+
const CALL_TIMEOUT_MS = 30000
|
|
33
|
+
/** `requestId` 记忆上限:服务端去重窗口,够覆盖一次重连的重发量。 */
|
|
34
|
+
export const DEDUP_WINDOW = 5000
|
|
35
|
+
|
|
36
|
+
export function serveFilePath(dataDir) {
|
|
37
|
+
return path.join(dataDir, SERVE_FILE)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function pendingFilePath(dataDir) {
|
|
41
|
+
return path.join(dataDir, PENDING_FILE)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 读 `serve.json`;没有、坏掉、不是对象一律 null(坏文件不该让整个进程起不来)。 */
|
|
45
|
+
export function readServeInfo(dataDir) {
|
|
46
|
+
try {
|
|
47
|
+
const raw = fs.readFileSync(serveFilePath(dataDir), 'utf8')
|
|
48
|
+
const info = JSON.parse(raw)
|
|
49
|
+
if (!info || typeof info !== 'object') return null
|
|
50
|
+
if (typeof info.token !== 'string' || info.token.length < 8) return null
|
|
51
|
+
if (typeof info.url !== 'string' && typeof info.socketPath !== 'string') return null
|
|
52
|
+
return info
|
|
53
|
+
} catch {
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function writeServeInfo(dataDir, info, opts = {}) {
|
|
59
|
+
// publish=false:不落发现文件(管理界面临时起的服务没有资格换掉别人的 serve.json)。
|
|
60
|
+
if (opts.publish === false) return null
|
|
61
|
+
fs.mkdirSync(dataDir, { recursive: true })
|
|
62
|
+
const file = serveFilePath(dataDir)
|
|
63
|
+
fs.writeFileSync(file, `${JSON.stringify(info, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 })
|
|
64
|
+
return file
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 只在 token 对得上时删——别把别人(另一个 dataDir/用户)的发现文件撤了。 */
|
|
68
|
+
export function removeServeInfo(dataDir, token) {
|
|
69
|
+
const info = readServeInfo(dataDir)
|
|
70
|
+
if (!info) return false
|
|
71
|
+
if (typeof token === 'string' && token.length > 0 && info.token !== token) return false
|
|
72
|
+
try {
|
|
73
|
+
fs.unlinkSync(serveFilePath(dataDir))
|
|
74
|
+
return true
|
|
75
|
+
} catch {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function makeRequestId() {
|
|
81
|
+
return `${process.pid.toString(36)}-${Date.now().toString(36)}-${crypto.randomBytes(4).toString('hex')}`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 一次 HTTP 往返。Windows 走 `url`(127.0.0.1),Unix 优先 `socketPath`。 */
|
|
85
|
+
export function request(info, method, urlPath, body, opts = {}) {
|
|
86
|
+
const timeout = Number(opts.timeout ?? CALL_TIMEOUT_MS)
|
|
87
|
+
const payload = body === undefined ? null : Buffer.from(JSON.stringify(body), 'utf8')
|
|
88
|
+
const headers = {
|
|
89
|
+
authorization: `Bearer ${info.token}`,
|
|
90
|
+
accept: 'application/json',
|
|
91
|
+
}
|
|
92
|
+
if (payload) {
|
|
93
|
+
headers['content-type'] = 'application/json'
|
|
94
|
+
headers['content-length'] = String(payload.length)
|
|
95
|
+
}
|
|
96
|
+
const options = { method, path: urlPath, headers, timeout }
|
|
97
|
+
if (!isWindows() && typeof info.socketPath === 'string' && info.socketPath.length > 0) {
|
|
98
|
+
options.socketPath = info.socketPath
|
|
99
|
+
} else {
|
|
100
|
+
const u = new URL(info.url)
|
|
101
|
+
options.host = u.hostname
|
|
102
|
+
options.port = Number(u.port || 80)
|
|
103
|
+
}
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
const req = http.request(options, (res) => {
|
|
106
|
+
const chunks = []
|
|
107
|
+
res.on('data', (c) => chunks.push(c))
|
|
108
|
+
res.on('end', () => {
|
|
109
|
+
const text = Buffer.concat(chunks).toString('utf8')
|
|
110
|
+
let json = null
|
|
111
|
+
try {
|
|
112
|
+
json = text.length > 0 ? JSON.parse(text) : null
|
|
113
|
+
} catch {
|
|
114
|
+
json = null
|
|
115
|
+
}
|
|
116
|
+
resolve({ status: res.statusCode ?? 0, text, json })
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
req.on('timeout', () => {
|
|
120
|
+
req.destroy(new Error(`超时(${timeout} ms)`))
|
|
121
|
+
})
|
|
122
|
+
req.on('error', reject)
|
|
123
|
+
if (payload) req.write(payload)
|
|
124
|
+
req.end()
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 探一次服务:`/stats` 回 200 才算通。
|
|
130
|
+
* @returns {Promise<{mode:'standalone'|'shared'|'stale', info?:object, stats?:object, reason?:string}>}
|
|
131
|
+
*/
|
|
132
|
+
export async function discover(dataDir) {
|
|
133
|
+
const info = readServeInfo(dataDir)
|
|
134
|
+
if (!info) return { mode: 'standalone', reason: 'no-serve-json' }
|
|
135
|
+
try {
|
|
136
|
+
const res = await request(info, 'GET', '/stats', undefined, { timeout: PROBE_TIMEOUT_MS })
|
|
137
|
+
if (res.status === 401) return { mode: 'standalone', info, reason: 'unauthorized' }
|
|
138
|
+
if (res.status !== 200 || !res.json) return { mode: 'stale', info, reason: `stats ${res.status}` }
|
|
139
|
+
return { mode: 'shared', info, stats: res.json }
|
|
140
|
+
} catch (err) {
|
|
141
|
+
return { mode: 'stale', info, reason: String(err?.message ?? err) }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── 待补交队列 ────────────────────────────────────────────────────────────────
|
|
146
|
+
|
|
147
|
+
export function enqueuePending(dataDir, item) {
|
|
148
|
+
try {
|
|
149
|
+
fs.mkdirSync(dataDir, { recursive: true })
|
|
150
|
+
fs.appendFileSync(pendingFilePath(dataDir), `${JSON.stringify(item)}\n`, 'utf8')
|
|
151
|
+
return true
|
|
152
|
+
} catch {
|
|
153
|
+
return false
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function readPending(dataDir) {
|
|
158
|
+
// 逐行解析只许有一份(doctor 的「jsonl 逐行解析没有第二份」盯着这条):
|
|
159
|
+
// 这里直接借 store.js 的读法——空行跳过、坏行丢掉、文件不在就空。
|
|
160
|
+
return readJsonl(pendingFilePath(dataDir)).filter(
|
|
161
|
+
(item) => item && typeof item === 'object' && typeof item.tool === 'string',
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** 整份重写(原子替换),补交失败的行留下。 */
|
|
166
|
+
export function rewritePending(dataDir, items) {
|
|
167
|
+
const file = pendingFilePath(dataDir)
|
|
168
|
+
try {
|
|
169
|
+
if (items.length === 0) {
|
|
170
|
+
fs.rmSync(file, { force: true })
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
const tmp = `${file}.tmp`
|
|
174
|
+
fs.writeFileSync(tmp, items.map((it) => JSON.stringify(it)).join('\n') + '\n', 'utf8')
|
|
175
|
+
fs.renameSync(tmp, file)
|
|
176
|
+
} catch {
|
|
177
|
+
// 写不回去也不能崩:队列是尽力而为
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function pendingCount(dataDir) {
|
|
182
|
+
return readPending(dataDir).length
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 把待补交的写补上。服务端按 `requestId` 去重,所以「补交时其实已经写进去了」
|
|
187
|
+
* 不会变成两条——这也是队列里必须存原始 id 的原因。
|
|
188
|
+
* @returns {Promise<{sent:number, left:number, failed:number}>}
|
|
189
|
+
*/
|
|
190
|
+
export async function flushPending(dataDir, info) {
|
|
191
|
+
const items = readPending(dataDir)
|
|
192
|
+
if (items.length === 0) return { sent: 0, left: 0, failed: 0 }
|
|
193
|
+
const left = []
|
|
194
|
+
let sent = 0
|
|
195
|
+
for (const item of items) {
|
|
196
|
+
try {
|
|
197
|
+
const res = await request(info, 'POST', '/call', {
|
|
198
|
+
requestId: item.requestId,
|
|
199
|
+
tool: item.tool,
|
|
200
|
+
args: item.args ?? {},
|
|
201
|
+
cwd: item.cwd,
|
|
202
|
+
agent: item.agent,
|
|
203
|
+
session: item.session,
|
|
204
|
+
})
|
|
205
|
+
if (res.status === 200) sent += 1
|
|
206
|
+
else left.push(item)
|
|
207
|
+
} catch {
|
|
208
|
+
left.push(item)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
rewritePending(dataDir, left)
|
|
212
|
+
return { sent, left: left.length, failed: left.length }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ── 给引擎用的那一层 ─────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
const MODE_NOTE = {
|
|
218
|
+
standalone: '直连本地库',
|
|
219
|
+
shared: '读写都走服务(唯一写连接)',
|
|
220
|
+
readonly: '服务不在了:这次没写进库,先排队等补交',
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function modeNote(mode) {
|
|
224
|
+
return MODE_NOTE[mode] ?? mode
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** 服务不通时的回执:说清「没写进去」「排在第几条」「怎么补」。 */
|
|
228
|
+
export function offlineText(info, queuedCount, dataDir) {
|
|
229
|
+
const where = typeof info?.url === 'string' ? info.url : info?.socketPath ?? '服务'
|
|
230
|
+
return [
|
|
231
|
+
`服务连不上(${where},pid ${info?.pid ?? '?'}),**这次没有写进库**。`,
|
|
232
|
+
`已经排进待补交队列:${path.join(dataDir, PENDING_FILE)} 现在第 ${queuedCount} 条。`,
|
|
233
|
+
'',
|
|
234
|
+
'三条路,挑一条:',
|
|
235
|
+
' 1. `macro-memory serve` 起服务——下次调用会自动补交(服务端按 requestId 去重,不会写两遍);',
|
|
236
|
+
' 2. 直接再调一次同一件事,服务在的话先补交再执行;',
|
|
237
|
+
' 3. `macro-memory detach` 认输,回到直连模式(之后写库由本进程负责)。',
|
|
238
|
+
].join('\n')
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 引擎的共享层入口:`standalone` 就返回 `{mode:'standalone'}` 让调用方直连,
|
|
243
|
+
* 否则直接把结果(或只读回执)交给上层。
|
|
244
|
+
*/
|
|
245
|
+
export async function sharedCall({ dataDir, tool, args, cwd, agentId, sessionId }) {
|
|
246
|
+
const found = await discover(dataDir)
|
|
247
|
+
if (found.mode === 'standalone') return { mode: 'standalone', reason: found.reason }
|
|
248
|
+
const info = found.info
|
|
249
|
+
if (found.mode === 'stale') {
|
|
250
|
+
enqueuePending(dataDir, {
|
|
251
|
+
requestId: makeRequestId(),
|
|
252
|
+
tool,
|
|
253
|
+
args,
|
|
254
|
+
cwd,
|
|
255
|
+
agent: agentId,
|
|
256
|
+
session: sessionId,
|
|
257
|
+
at: Date.now(),
|
|
258
|
+
error: found.reason,
|
|
259
|
+
})
|
|
260
|
+
const queued = pendingCount(dataDir)
|
|
261
|
+
return {
|
|
262
|
+
mode: 'readonly',
|
|
263
|
+
refused: true,
|
|
264
|
+
queued: true,
|
|
265
|
+
reason: 'serve-down',
|
|
266
|
+
text: offlineText(info, queued, dataDir),
|
|
267
|
+
value: null,
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const requestId = makeRequestId()
|
|
271
|
+
let res
|
|
272
|
+
try {
|
|
273
|
+
res = await request(info, 'POST', '/call', {
|
|
274
|
+
requestId,
|
|
275
|
+
tool,
|
|
276
|
+
args,
|
|
277
|
+
cwd,
|
|
278
|
+
agent: agentId,
|
|
279
|
+
session: sessionId,
|
|
280
|
+
})
|
|
281
|
+
} catch (err) {
|
|
282
|
+
enqueuePending(dataDir, { requestId, tool, args, cwd, agent: agentId, session: sessionId, at: Date.now(), error: String(err?.message ?? err) })
|
|
283
|
+
const queued = pendingCount(dataDir)
|
|
284
|
+
return {
|
|
285
|
+
mode: 'readonly',
|
|
286
|
+
refused: true,
|
|
287
|
+
queued: true,
|
|
288
|
+
reason: 'serve-down',
|
|
289
|
+
text: offlineText(info, queued, dataDir),
|
|
290
|
+
value: null,
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (res.status === 401) return { mode: 'standalone', reason: 'unauthorized' }
|
|
294
|
+
if (res.status !== 200 || !res.json) {
|
|
295
|
+
enqueuePending(dataDir, { requestId, tool, args, cwd, agent: agentId, session: sessionId, at: Date.now(), error: `status ${res.status}` })
|
|
296
|
+
const queued = pendingCount(dataDir)
|
|
297
|
+
return {
|
|
298
|
+
mode: 'readonly',
|
|
299
|
+
refused: true,
|
|
300
|
+
queued: true,
|
|
301
|
+
reason: `serve-${res.status}`,
|
|
302
|
+
text: offlineText(info, queued, dataDir),
|
|
303
|
+
value: null,
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
// 顺手补交:只在队列非空时做,正常路径(队列空)零成本。
|
|
307
|
+
let flushed = 0
|
|
308
|
+
if (pendingCount(dataDir) > 0) {
|
|
309
|
+
const done = await flushPending(dataDir, info)
|
|
310
|
+
flushed = done.sent
|
|
311
|
+
}
|
|
312
|
+
return { mode: 'shared', requestId, text: res.json.text ?? '', value: res.json.value ?? null, deduped: res.json.deduped === true, flushed }
|
|
313
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@deepseek-ai/dsh-llm` 的最小兼容层(0.8.1)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:meow 引擎在模块顶层
|
|
5
|
+
* `import { boundContextSummary, createUserMessage } from '@deepseek-ai/dsh-llm'`。
|
|
6
|
+
* 在 DSH 里这个包由宿主提供,所以一直没暴露问题;但 0.8.0 承诺「不装 DSH 也能用」
|
|
7
|
+
* (npx / MCP),干净机器上根本没有这个包 → `ERR_MODULE_NOT_FOUND`,包当场起不来。
|
|
8
|
+
*
|
|
9
|
+
* 这里只复刻 meow 真正用到的两个符号(外加同族几个构造器,防上游将来多用),
|
|
10
|
+
* 语义与 DSH 的 `@deepseek-ai/dsh-llm` 逐字对齐:
|
|
11
|
+
* - createMessage:`{...input, id: randomUUID()}` → structuredClone → deepFreeze
|
|
12
|
+
* - boundContextSummary:超过 120 字符截到 119 + '…'
|
|
13
|
+
*
|
|
14
|
+
* 这不是「假实现」:这两个函数本来就是纯数据整形,没有模型调用、没有 I/O。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { randomUUID } from 'node:crypto'
|
|
18
|
+
|
|
19
|
+
/** 与 dsh-llm 的 CONTEXT_SUMMARY_MAX_CHARS 同值。 */
|
|
20
|
+
export const CONTEXT_SUMMARY_MAX_CHARS = 120
|
|
21
|
+
|
|
22
|
+
/** 逐层冻结(structuredClone 出来的对象没有共享引用,直接递归即可)。 */
|
|
23
|
+
function deepFreeze(value) {
|
|
24
|
+
if (value === null || typeof value !== 'object') return value
|
|
25
|
+
for (const key of Object.keys(value)) deepFreeze(value[key])
|
|
26
|
+
return Object.freeze(value)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 截断一条 notice 摘要,超出上限时补省略号。 */
|
|
30
|
+
export function boundContextSummary(summary) {
|
|
31
|
+
return summary.length <= CONTEXT_SUMMARY_MAX_CHARS
|
|
32
|
+
? summary
|
|
33
|
+
: `${summary.slice(0, CONTEXT_SUMMARY_MAX_CHARS - 1)}…`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 复制一份已带身份的 message 并冻结。 */
|
|
37
|
+
export function freezeMessage(message) {
|
|
38
|
+
return deepFreeze(structuredClone(message))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 造一条带稳定身份的消息并冻结。 */
|
|
42
|
+
export function createMessage(input) {
|
|
43
|
+
return deepFreeze(structuredClone({ ...input, id: randomUUID() }))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createDeveloperMessage(input) {
|
|
47
|
+
return createMessage({ ...input, role: 'developer' })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createUserMessage(input) {
|
|
51
|
+
return createMessage({ ...input, role: 'user' })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function createAssistantMessage(input) {
|
|
55
|
+
return createMessage({
|
|
56
|
+
role: 'assistant',
|
|
57
|
+
content: input.content,
|
|
58
|
+
source: { kind: 'model', ...input.source },
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createSystemMessage(text) {
|
|
63
|
+
return createMessage({
|
|
64
|
+
role: 'system',
|
|
65
|
+
content: text.length === 0 ? [] : [{ type: 'text', text }],
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export default {
|
|
70
|
+
CONTEXT_SUMMARY_MAX_CHARS,
|
|
71
|
+
boundContextSummary,
|
|
72
|
+
freezeMessage,
|
|
73
|
+
createMessage,
|
|
74
|
+
createDeveloperMessage,
|
|
75
|
+
createUserMessage,
|
|
76
|
+
createAssistantMessage,
|
|
77
|
+
createSystemMessage,
|
|
78
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/config —— 磁盘上的那一份 `config.json`。
|
|
3
|
+
*
|
|
4
|
+
* 为什么 core 需要自己的配置文件:DSH 侧的配置由 cordis schema 管(主人的 UI 里改),
|
|
5
|
+
* 而「不装 DSH 的那一半」(MCP / CLI)没有 UI,只有一张纸。写在 `dataDir/config.json`,
|
|
6
|
+
* 人可读、可 diff、可进 git,脚本也能直接改。
|
|
7
|
+
*
|
|
8
|
+
* 三条规矩:
|
|
9
|
+
* 1. **读不懂就说不懂**:JSON 坏了就报错并把原文留在原地,绝不「悄悄用默认值覆盖」——
|
|
10
|
+
* 那会把主人手写的配置吃掉,而且看不出来。
|
|
11
|
+
* 2. **写要原子**:先写 `config.json.tmp` 再 rename。写到一半断电,旧文件还在。
|
|
12
|
+
* 3. **不认识就留着**:本文件只认自己那几段,其余键原样保留(向前兼容:新版本写的
|
|
13
|
+
* 字段被老版本读到,不该被删掉)。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
import { CONFIG_FILE, resolveDataDir } from './paths.js'
|
|
19
|
+
|
|
20
|
+
/** 配置格式版本。改结构时 +1,并在这里写迁移。 */
|
|
21
|
+
export const CONFIG_VERSION = 1
|
|
22
|
+
|
|
23
|
+
/** 默认配置:正是 `macro-memory init` 写出来的样子。 */
|
|
24
|
+
export function defaultConfig(overrides = {}) {
|
|
25
|
+
return {
|
|
26
|
+
version: CONFIG_VERSION,
|
|
27
|
+
/** workspace 内的库目录名(与 DSH 侧同名 = 同一份库)。 */
|
|
28
|
+
projectDir: '.dsh-meow',
|
|
29
|
+
/** 全局库目录,空串 = meow 默认(~/.dsh-meow)。 */
|
|
30
|
+
homeDir: '',
|
|
31
|
+
/** 这个 agent 是谁:写进 owner 与回执里的「(codex 记)」。 */
|
|
32
|
+
agent: { id: '', label: '' },
|
|
33
|
+
/** 各引擎的覆盖配置,形状与 DSH 侧一致(meow 的键原样透传)。 */
|
|
34
|
+
engines: { meow: {}, nebula: {} },
|
|
35
|
+
/** 融合层(注入字符数、回执语言等)。 */
|
|
36
|
+
macro: {},
|
|
37
|
+
...overrides,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function configPathOf(opts) {
|
|
42
|
+
const dataDir = resolveDataDir(typeof opts === 'string' ? { dir: opts } : opts ?? {})
|
|
43
|
+
return { dataDir, file: path.join(dataDir, CONFIG_FILE) }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 读配置。
|
|
48
|
+
* @returns {{file:string, dataDir:string, exists:boolean, config:object, raw:string|null}}
|
|
49
|
+
* @throws 文件存在但不是合法 JSON 对象时抛错(调用方自己决定是提示还是退出)
|
|
50
|
+
*/
|
|
51
|
+
export function loadConfig(opts = {}) {
|
|
52
|
+
const { dataDir, file } = configPathOf(opts)
|
|
53
|
+
if (!fs.existsSync(file)) {
|
|
54
|
+
return { file, dataDir, exists: false, config: defaultConfig(), raw: null }
|
|
55
|
+
}
|
|
56
|
+
const raw = fs.readFileSync(file, 'utf8')
|
|
57
|
+
let parsed
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(raw)
|
|
60
|
+
} catch (err) {
|
|
61
|
+
throw new Error(`${file} 不是合法 JSON:${err.message}(文件没动,改好再跑)`)
|
|
62
|
+
}
|
|
63
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
64
|
+
throw new Error(`${file} 顶层必须是对象,现在是 ${Array.isArray(parsed) ? '数组' : typeof parsed}`)
|
|
65
|
+
}
|
|
66
|
+
return { file, dataDir, exists: true, config: { ...defaultConfig(), ...parsed }, raw }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 合并式改写:`patch` 只覆盖它提到的键,其余原样。 */
|
|
70
|
+
export function mergeConfig(current, patch) {
|
|
71
|
+
const out = { ...current }
|
|
72
|
+
for (const [k, v] of Object.entries(patch ?? {})) {
|
|
73
|
+
if (v === undefined) continue
|
|
74
|
+
const cur = out[k]
|
|
75
|
+
if (v !== null && typeof v === 'object' && !Array.isArray(v)
|
|
76
|
+
&& cur !== null && typeof cur === 'object' && !Array.isArray(cur)) {
|
|
77
|
+
out[k] = mergeConfig(cur, v)
|
|
78
|
+
} else {
|
|
79
|
+
out[k] = v
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return out
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 写配置(原子)。
|
|
87
|
+
* @returns {string} 落盘路径
|
|
88
|
+
*/
|
|
89
|
+
export function writeConfigFile(opts, config) {
|
|
90
|
+
// 两种调用姿势:writeConfigFile(config) / writeConfigFile(dataDirOrOpts, config)
|
|
91
|
+
const isConfigOnly = config === undefined
|
|
92
|
+
const target = isConfigOnly ? {} : opts
|
|
93
|
+
const body = isConfigOnly ? opts : config
|
|
94
|
+
const { dataDir, file } = configPathOf(target)
|
|
95
|
+
fs.mkdirSync(dataDir, { recursive: true })
|
|
96
|
+
const next = { ...defaultConfig(), ...(body ?? {}), version: CONFIG_VERSION }
|
|
97
|
+
// 键排序:stable stringify,主人 diff 时不会因为键序抖动看到假改动
|
|
98
|
+
const text = `${JSON.stringify(sortKeys(next), null, 2)}\n`
|
|
99
|
+
const tmp = `${file}.tmp`
|
|
100
|
+
fs.writeFileSync(tmp, text, 'utf8')
|
|
101
|
+
fs.renameSync(tmp, file)
|
|
102
|
+
return file
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 存一下(读旧的、合新的、写回去)。 */
|
|
106
|
+
export function saveConfig(opts, patch) {
|
|
107
|
+
const { config } = loadConfig(opts)
|
|
108
|
+
return writeConfigFile(opts, mergeConfig(config, patch))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sortKeys(value) {
|
|
112
|
+
if (Array.isArray(value)) return value.map(sortKeys)
|
|
113
|
+
if (value === null || typeof value !== 'object') return value
|
|
114
|
+
const out = {}
|
|
115
|
+
for (const k of Object.keys(value).sort()) out[k] = sortKeys(value[k])
|
|
116
|
+
return out
|
|
117
|
+
}
|