@modusensus/dsh-mneme 0.8.13 → 0.8.15
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 -21
- package/README.md +655 -626
- package/assets/icon.svg +3 -3
- package/bin/cli.mjs +689 -689
- package/bin/dsh-mneme-mcp.mjs +510 -510
- package/bin/dsh-mneme-serve.mjs +151 -0
- package/cordis.patch.yml +21 -21
- package/docs/CONFIGURATION.md +263 -0
- package/docs/CONTINUITY.md +124 -0
- package/docs/DAEMON.md +46 -0
- package/docs/ENTITIES.md +245 -0
- package/docs/LOCAL_MODEL.md +222 -0
- package/docs/SEMANTIC.md +256 -0
- package/docs/SLEEP.md +164 -0
- package/docs/STORAGE.md +89 -0
- package/lib/api-standalone.js +673 -572
- package/lib/api.js +1437 -1433
- package/lib/bootstrap.js +211 -211
- package/lib/client.js +5444 -5295
- package/lib/commands.js +95 -95
- package/lib/config.js +880 -836
- package/lib/content-hash.js +54 -54
- package/lib/continuity.js +220 -220
- package/lib/document-index.js +94 -94
- package/lib/document.js +303 -303
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +674 -674
- package/lib/dream/narratives.js +44 -44
- package/lib/dream/sleep.js +819 -819
- package/lib/dream.js +1501 -1501
- package/lib/embedding.js +177 -177
- package/lib/entities/extractor.js +252 -252
- package/lib/graph/anchoring.js +179 -179
- package/lib/guide.js +74 -74
- package/lib/heat.js +144 -144
- package/lib/hot-memory.js +46 -46
- package/lib/index.js +535 -671
- package/lib/inject.js +464 -450
- package/lib/lang.js +248 -229
- package/lib/local-embedder.js +358 -358
- package/lib/maintenance.js +142 -142
- package/lib/mirror.js +330 -330
- package/lib/organize.js +450 -450
- package/lib/peak-hours.js +117 -117
- package/lib/placement.js +13 -0
- package/lib/pre-inject-gate.js +340 -0
- package/lib/quality-filter.js +126 -126
- package/lib/recall-stats.js +175 -175
- package/lib/reranker.js +269 -269
- package/lib/runtime/adopt.js +176 -176
- package/lib/runtime/closure.js +172 -172
- package/lib/runtime/download.js +247 -247
- package/lib/runtime/layout.js +265 -265
- package/lib/runtime/loader.js +252 -252
- package/lib/runtime/model-fetch.js +361 -361
- package/lib/runtime/provision.js +237 -237
- package/lib/runtime/tarball.js +130 -130
- package/lib/runtime/verify.js +223 -204
- package/lib/scope.js +146 -146
- package/lib/search/adaptive.js +60 -60
- package/lib/search/bm25.js +96 -96
- package/lib/semantic.js +229 -0
- package/lib/sensitive-scan.js +209 -209
- package/lib/serve.js +215 -0
- package/lib/service.js +2663 -2596
- package/lib/settings.js +517 -509
- package/lib/store.js +3007 -3007
- package/lib/summarize.js +891 -856
- package/lib/tools.js +743 -737
- package/lib/vector-index.js +110 -110
- package/lib/version-check.js +122 -122
- package/lib/write-admission.js +402 -402
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +8 -4
- package/runtime-manifest.json +526 -526
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +249 -249
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/build-runtime-manifest.mjs +224 -224
- package/scripts/changelog-prep.mjs +36 -0
- package/scripts/check-sync.js +42 -42
- package/scripts/e2e-dsh.js +220 -220
- package/scripts/mneme-runtime.mjs +277 -277
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +47 -47
- package/scripts/sync-test-badge.mjs +69 -69
- package/scripts/test-count-sync.mjs +44 -44
- package/src/api-standalone.js +673 -572
- package/src/api.js +1437 -1433
- package/src/bootstrap.js +211 -211
- package/src/commands.js +95 -95
- package/src/config.js +880 -836
- package/src/content-hash.js +54 -54
- package/src/continuity.js +220 -220
- package/src/document-index.js +94 -94
- package/src/document.js +303 -303
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +674 -674
- package/src/dream/narratives.js +44 -44
- package/src/dream/sleep.js +819 -819
- package/src/dream.js +1501 -1501
- package/src/embedding.js +177 -177
- package/src/entities/extractor.js +252 -252
- package/src/graph/anchoring.js +179 -179
- package/src/guide.js +74 -74
- package/src/heat.js +144 -144
- package/src/hot-memory.js +46 -46
- package/src/index.js +535 -671
- package/src/inject.js +464 -450
- package/src/lang.js +248 -229
- package/src/local-embedder.js +358 -358
- package/src/maintenance.js +142 -142
- package/src/mirror.js +330 -330
- package/src/organize.js +450 -450
- package/src/peak-hours.js +117 -117
- package/src/placement.js +13 -0
- package/src/pre-inject-gate.js +340 -0
- package/src/quality-filter.js +126 -126
- package/src/recall-stats.js +175 -175
- package/src/reranker.js +269 -269
- package/src/runtime/adopt.js +176 -176
- package/src/runtime/closure.js +172 -172
- package/src/runtime/download.js +247 -247
- package/src/runtime/layout.js +265 -265
- package/src/runtime/loader.js +252 -252
- package/src/runtime/model-fetch.js +361 -361
- package/src/runtime/provision.js +237 -237
- package/src/runtime/tarball.js +130 -130
- package/src/runtime/verify.js +223 -204
- package/src/scope.js +146 -146
- package/src/search/adaptive.js +60 -60
- package/src/search/bm25.js +96 -96
- package/src/semantic.js +229 -0
- package/src/sensitive-scan.js +209 -209
- package/src/serve.js +215 -0
- package/src/service.js +2663 -2596
- package/src/settings.js +517 -509
- package/src/store.js +3007 -3007
- package/src/summarize.js +891 -856
- package/src/tools.js +743 -737
- package/src/vector-index.js +110 -110
- package/src/version-check.js +122 -122
- package/src/write-admission.js +402 -402
- package/test/adaptive-inject-budget.test.js +85 -85
- package/test/api-routes-gaps.test.js +218 -218
- package/test/api.test.js +1702 -1688
- package/test/audit.test.js +448 -448
- package/test/baseline-persist.test.js +248 -248
- package/test/benchmark.test.js +94 -94
- package/test/bootstrap.test.js +220 -220
- package/test/changelog-prep.test.js +60 -0
- package/test/client.test.js +1230 -1169
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +106 -106
- package/test/config.test.js +66 -66
- package/test/conflict-freeze.test.js +300 -300
- package/test/content-hash.test.js +204 -204
- package/test/continuity.test.js +333 -333
- package/test/document-dir.test.js +233 -233
- package/test/document.test.js +671 -671
- package/test/dream-archive-guardrail.test.js +116 -116
- package/test/dream-candidate-hybrid.test.js +281 -281
- package/test/dream-coverage-degrade.test.js +86 -86
- package/test/dream-failure-backoff.test.js +173 -173
- package/test/dream-peak-hours.test.js +194 -194
- package/test/dream.test.js +1385 -1383
- package/test/embedding-ready.test.js +196 -196
- package/test/entities.test.js +627 -547
- package/test/entity-recall.test.js +179 -179
- package/test/epistemic.test.js +325 -325
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-anchoring.test.js +184 -184
- package/test/graph-api.test.js +175 -175
- package/test/graph-inject-budget.test.js +103 -103
- package/test/graph-passive-confirm.test.js +91 -91
- package/test/graph-weight-evolution.test.js +104 -104
- package/test/heat.test.js +175 -175
- package/test/helpers/dream-mock.js +82 -82
- package/test/helpers/peer-worker.mjs +25 -25
- package/test/helpers/runtime-source.js +60 -60
- package/test/helpers/tar-builder.js +80 -80
- package/test/helpers/write-admission-samples.js +308 -308
- package/test/hot-memory.test.js +145 -145
- package/test/inject-content-cap.test.js +91 -91
- package/test/inject-first-round-bm25.test.js +89 -89
- package/test/inject-heat.test.js +77 -61
- package/test/inject-parent-gate.test.js +106 -106
- package/test/inject-pools.test.js +194 -194
- package/test/inject-recall.test.js +105 -105
- package/test/inject-rotation.test.js +161 -161
- package/test/inject.test.js +420 -420
- package/test/lang.test.js +87 -87
- package/test/lastrunat-persist.test.js +101 -101
- package/test/lib-smoke.test.js +96 -96
- package/test/llm-audit.test.js +598 -598
- package/test/llm-message-source.test.js +207 -207
- package/test/local-embedder.test.js +292 -292
- package/test/maintenance.test.js +391 -391
- package/test/mcp-stdio.test.js +385 -385
- package/test/mirror-dirty.test.js +424 -424
- package/test/mirror-edit-digest.test.js +187 -187
- package/test/mirror-generation.test.js +499 -499
- package/test/mirror-types.test.js +208 -208
- package/test/mirror.test.js +249 -249
- package/test/narrative-bars.test.js +162 -162
- package/test/organize.test.js +418 -418
- package/test/peer-blockers.test.js +193 -193
- package/test/placement.test.js +36 -0
- package/test/policy-epoch.test.js +259 -259
- package/test/pre-inject-gate.test.js +434 -0
- package/test/quality-filter.test.js +186 -186
- package/test/reasoning-effort.test.js +752 -752
- package/test/recall-anti-update.test.js +82 -82
- package/test/recall-evals.test.js +253 -253
- package/test/recall-layer.test.js +326 -326
- package/test/recall-stats.test.js +189 -189
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reindex-backfill.test.js +62 -62
- package/test/reranker-single-logit.test.js +73 -73
- package/test/reranker.test.js +197 -197
- package/test/resident-status.test.js +83 -83
- package/test/runtime-adopt.test.js +206 -206
- package/test/runtime-cli.test.js +89 -89
- package/test/runtime-closure.test.js +193 -193
- package/test/runtime-download.test.js +302 -302
- package/test/runtime-layout.test.js +163 -163
- package/test/runtime-loader.test.js +255 -255
- package/test/runtime-manifest.test.js +159 -159
- package/test/runtime-model-fetch.test.js +348 -348
- package/test/runtime-provision.test.js +101 -101
- package/test/runtime-tarball.test.js +81 -81
- package/test/runtime-verify.test.js +319 -279
- package/test/scope-candidates.test.js +239 -239
- package/test/scope-explicit.test.js +452 -452
- package/test/scope-inject-soft.test.js +106 -106
- package/test/scope-retrieval.test.js +197 -197
- package/test/scope-storage.test.js +255 -255
- package/test/scope-strict.test.js +367 -343
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/sensitive-scan.test.js +207 -207
- package/test/serve-bin.test.js +121 -0
- package/test/serve.test.js +196 -0
- package/test/service-search.test.js +174 -174
- package/test/service.test.js +405 -405
- package/test/settings.test.js +261 -261
- package/test/skip-invalid-restore.test.js +262 -262
- package/test/sleep-action-set.test.js +422 -422
- package/test/sleep-heat.test.js +126 -126
- package/test/sleep-scheduler-rearm.test.js +89 -89
- package/test/sleep.test.js +527 -527
- package/test/standalone-api.test.js +751 -544
- package/test/store-vector-cache.test.js +98 -98
- package/test/store.test.js +396 -396
- package/test/stress.test.js +209 -209
- package/test/summarize-occurred-at.test.js +127 -0
- package/test/summarize-reasoning-effort.test.js +189 -189
- package/test/summarize.test.js +1465 -1465
- package/test/supersede-state-tracking.test.js +394 -0
- package/test/test-count-sync.test.js +60 -60
- package/test/tools-dto-schema-matrix.test.js +325 -325
- package/test/tools-memory-get-schema.test.js +65 -65
- package/test/tools.test.js +428 -374
- package/test/updated-at-semantics.test.js +113 -113
- package/test/vector-index.test.js +221 -221
- package/test/version-check.test.js +152 -152
- package/test/webServer-optional.test.js +82 -82
- package/test/write-admission.test.js +668 -668
package/lib/serve.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// src/serve.js —— 独立服务(daemon)运行时:mneme 在 DSH 宿主之外常驻的最小装配面
|
|
2
|
+
// (#363 承诺的「官方推荐第三方挂载姿势」)。bin/dsh-mneme-serve.mjs 是它的 CLI 壳,
|
|
3
|
+
// Mneme Bridge 这类第三方也可直接 import 本模块自行托管生命周期。
|
|
4
|
+
//
|
|
5
|
+
// 与宿主装配(src/index.js apply)的关系:只搬数据面 + 语义管线两块,每步注释锚定
|
|
6
|
+
// index.js 来源行号。刻意不抽公共装配函数——apply 的其余环节(注入/工具/dream)与
|
|
7
|
+
// 宿主 ctx 纠缠,防御段纪律是「最后动或不动」;本文件的漂移风险由 serve-bin 测试的
|
|
8
|
+
// 多进程共存用例兜底(两侧真开同一个库互写互读)。
|
|
9
|
+
//
|
|
10
|
+
// 无 LLM:巩固(autoDream)与蒸馏(autoSummarize)结构上不在这里——巩固只属于 DSH
|
|
11
|
+
// 宿主进程,这就是 daemon 与宿主「单写者」的机械保证(AGENTS.md 的 externalApi/
|
|
12
|
+
// autoDream 单侧纪律),不依赖用户自觉。语义检索(PR2 起)与宿主共用 src/semantic.js
|
|
13
|
+
// 的同一份装配;daemon 不参与做梦,但向量检索/语义去重与宿主同质。
|
|
14
|
+
import { mkdirSync } from "node:fs";
|
|
15
|
+
import { homedir } from "node:os";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import { createStore } from "./store.js";
|
|
18
|
+
import { createSettings } from "./settings.js";
|
|
19
|
+
import { createMirror, TYPE_FILE } from "./mirror.js";
|
|
20
|
+
import { langOf } from "./lang.js";
|
|
21
|
+
import { createService } from "./service.js";
|
|
22
|
+
import { createMaintenance } from "./maintenance.js";
|
|
23
|
+
import { createStandaloneApi } from "./api-standalone.js";
|
|
24
|
+
import { createSemantic } from "./semantic.js";
|
|
25
|
+
import { createVectorIndex } from "./vector-index.js";
|
|
26
|
+
import { provisionRuntime } from "./runtime/provision.js";
|
|
27
|
+
import { defaultRuntimeDir, listPayloadDirs } from "./runtime/layout.js";
|
|
28
|
+
|
|
29
|
+
const EMBED_PROVIDERS = new Set(["off", "local", "ollama", "openai"]);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 组装并启动一个独立数据面(含语义管线)。
|
|
33
|
+
* @param {object} opts
|
|
34
|
+
* memoryDir — 数据目录;缺省与宿主同默认 ~/.dsh/memory(config.js:9),支持前导 ~。
|
|
35
|
+
* port/host — 透传 createStandaloneApi;缺省走 kv external_api 持久值 > 8790/127.0.0.1。
|
|
36
|
+
* logger — console 形状(info/warn/error,收单字符串);缺省 null(全链路容缺)。
|
|
37
|
+
* strictPort — 默认 true:配置端口被占即失败(第三方把 URL 写死,顺延=静默打到
|
|
38
|
+
* 错误端口)。显式 port=0(测试/OS 分配)不受影响。
|
|
39
|
+
* embed — "local"(默认,自管 runtime 取件 + ONNX 嵌入)|"ollama"|"openai"
|
|
40
|
+
* (读宿主面板配的 vector-config)|"off"(纯关键词 + BM25)。
|
|
41
|
+
* embedder/reranker — 注入式覆盖(测试/宿主方自管):给了就跳过 createSemantic,
|
|
42
|
+
* 直接 setEmbedder/setReranker;仅 embed="off" 之外有意为之。
|
|
43
|
+
* @returns {api, store, service, settings, maintenance, semantic, tokenExisted, dispose}
|
|
44
|
+
* tokenExisted — 启动前 kv 里是否已有 token;false 时本次为首次生成,入口层可提示。
|
|
45
|
+
*/
|
|
46
|
+
export async function createServeRuntime({
|
|
47
|
+
memoryDir, port, host, logger = null, strictPort = true, embed = "local", embedder = null, reranker = null
|
|
48
|
+
} = {}) {
|
|
49
|
+
if (!EMBED_PROVIDERS.has(embed)) {
|
|
50
|
+
throw new Error(`invalid embed provider: ${String(embed)}(允许 off/local/ollama/openai)`);
|
|
51
|
+
}
|
|
52
|
+
// index.js:192-195:~ 展开只认前导;目录不存在时 node:sqlite 直接抛,先 mkdir。
|
|
53
|
+
const dir = String(memoryDir || join(homedir(), ".dsh", "memory")).replace(/^~(?=$|[\\/])/, homedir());
|
|
54
|
+
mkdirSync(dir, { recursive: true });
|
|
55
|
+
|
|
56
|
+
// 装配顺序照 index.js:197-292:store → settings → mirror → service → recoverMirror。
|
|
57
|
+
const store = createStore(join(dir, "memory.db"));
|
|
58
|
+
const settings = createSettings(store.db);
|
|
59
|
+
// 第一期不吃宿主 config schema(schemastery 是宿主 peer 依赖,独立进程装不装随缘),
|
|
60
|
+
// 配最小集,与第三方 embedded 挂载实测同形:language 参与镜像渲染与去重标题;
|
|
61
|
+
// documentMemoryEnabled 关掉 document 子系统(daemon 不建 documentIndex,service
|
|
62
|
+
// 内部 if-guard 安全,index.js:261 的 documentIndex 仅宿主装配)。
|
|
63
|
+
const cfg = { language: "zh", documentMemoryEnabled: false };
|
|
64
|
+
const mirror = createMirror(dir, langOf(cfg));
|
|
65
|
+
const service = createService({ store, mirror, config: cfg, logger });
|
|
66
|
+
|
|
67
|
+
// index.js:292:上次镜像同步失败留下的 dirty 状态,启动时安全重渲一次(内部自吞)。
|
|
68
|
+
service.recoverMirror();
|
|
69
|
+
|
|
70
|
+
// index.js:319-332:人改镜像先合并——镜像文件里的手工编辑每次启动都赢。
|
|
71
|
+
// readHumanEdits 全类型一次读齐:mergeHumanEdits 成功会重渲全部镜像,逐类型读改
|
|
72
|
+
// 循环会拿没读到的类型覆盖掉未合并的编辑(index.js:324-327 注释同款坑)。
|
|
73
|
+
// 何时 apply 由 semantic 的分支时序契约决定(见 semantic.js 文件头),本文件只造闭包。
|
|
74
|
+
const humanEdits = new Map();
|
|
75
|
+
for (const type of Object.keys(TYPE_FILE)) humanEdits.set(type, mirror.readHumanEdits(type));
|
|
76
|
+
const applyHumanEdits = () => {
|
|
77
|
+
for (const [type, edits] of humanEdits) {
|
|
78
|
+
if (edits.length) service.mergeHumanEdits(type, edits);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// index.js:316-317:向量索引包住 store 的 embedding 列并跟踪活跃模型指纹。
|
|
83
|
+
const vectorIndex = createVectorIndex({ store, logger });
|
|
84
|
+
service.setVectorIndex(vectorIndex);
|
|
85
|
+
|
|
86
|
+
// ---- 语义管线(PR2):三路二选一 ------------------------------------------
|
|
87
|
+
let semantic = null;
|
|
88
|
+
if (embedder !== null) {
|
|
89
|
+
// 注入式覆盖(测试 / 宿主方自管 embedder):绕过装配,人改合并立刻做
|
|
90
|
+
//(对齐宿主 openai 分支的同步语义)。
|
|
91
|
+
service.setEmbedder(embedder);
|
|
92
|
+
if (reranker !== null) service.setReranker(reranker);
|
|
93
|
+
applyHumanEdits();
|
|
94
|
+
} else if (embed === "off") {
|
|
95
|
+
// 纯关键词 + BM25:对齐宿主 lightMode 分支——不建 embedder,人改合并立即。
|
|
96
|
+
applyHumanEdits();
|
|
97
|
+
} else {
|
|
98
|
+
let semanticReady = true;
|
|
99
|
+
if (embed === "local") {
|
|
100
|
+
// 自管 runtime 缺失时取件:只走 download 档(宿主 adopt 推导对独立进程无意义
|
|
101
|
+
// ——没有宿主 node_modules 可推)。失败不阻断 daemon:降级关键词 + 可操作提示。
|
|
102
|
+
semanticReady = await ensureRuntimePayload(logger);
|
|
103
|
+
}
|
|
104
|
+
if (semanticReady) {
|
|
105
|
+
semantic = createSemantic({
|
|
106
|
+
store, service, settings,
|
|
107
|
+
cfg: daemonSemanticCfg(embed),
|
|
108
|
+
logger, vectorIndex, applyHumanEdits, lightMode: false
|
|
109
|
+
});
|
|
110
|
+
} else {
|
|
111
|
+
applyHumanEdits();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// index.js:294-309:检索回执落 recall_runs(searchMemories 的 recordRecall 默认开,
|
|
116
|
+
// 第三方检索统计因此不缺数)。best-effort:回执写失败绝不影响检索本身。
|
|
117
|
+
service.setRecallRecorder((recall) => {
|
|
118
|
+
try {
|
|
119
|
+
store.saveRecallRun({
|
|
120
|
+
query: recall.query,
|
|
121
|
+
mode: recall.mode,
|
|
122
|
+
topK: recall.topK,
|
|
123
|
+
threshold: recall.threshold ?? null,
|
|
124
|
+
candidates: recall.candidates ?? [],
|
|
125
|
+
created_at: recall.createdAt
|
|
126
|
+
});
|
|
127
|
+
} catch { /* non-fatal: recall recording is bookkeeping */ }
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// index.js:636:#275 无损回收。daemon 带上它,POST /maintenance/reclaim 才有后端
|
|
131
|
+
// (api-standalone 缺 maintenance 时该路由 503);同样不挂启动路径、不接定时器。
|
|
132
|
+
const maintenance = createMaintenance({ store, config: cfg, logger });
|
|
133
|
+
|
|
134
|
+
// index.js:643-649 同一工厂;差别只有 strictPort 默认开(daemon 语义,见上)。
|
|
135
|
+
// token 与宿主共用同一 kv 键 external_api:首次启动自动生成并持久化
|
|
136
|
+
// (api-standalone.js:203-212),DSH 面板 / CLI / daemon 三方零配置共享凭证。
|
|
137
|
+
const tokenExisted = Boolean(settings.getExternalApi?.()?.token);
|
|
138
|
+
// embedder 注入 /context 的查询嵌入(issue #370):三路取当前生效的同一句柄——
|
|
139
|
+
// 注入覆盖用注入值,semantic 路用它的产物,off/lightMode 为 null(路由侧降级
|
|
140
|
+
// 规则档)。与 service.setEmbedder 的是同一个实例,不存在第二真相源。
|
|
141
|
+
const api = createStandaloneApi({ service, store, config: cfg, logger, settings, port, host, maintenance, strictPort, embedder: embedder ?? semantic?.embedder ?? null });
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
api,
|
|
145
|
+
store,
|
|
146
|
+
service,
|
|
147
|
+
settings,
|
|
148
|
+
maintenance,
|
|
149
|
+
semantic,
|
|
150
|
+
tokenExisted,
|
|
151
|
+
/**
|
|
152
|
+
* 收尾:先停收新请求、等在途请求跑完,再关库——直接同步关库会让在途的
|
|
153
|
+
* PUT/POST 撞上已关的 store(500 或丢写)。closeIdleConnections 排干
|
|
154
|
+
* keep-alive 空闲连接(node ≥18.2,旧版无此 API 则跳过),否则 server.close
|
|
155
|
+
* 的回调要等 keep-alive 超时才触发。node:sqlite 对未 finalize 语句可能抛,
|
|
156
|
+
* 吞掉——WAL 会在下次打开时回放,已提交事务不丢。
|
|
157
|
+
*/
|
|
158
|
+
async dispose() {
|
|
159
|
+
await new Promise((resolve) => {
|
|
160
|
+
try {
|
|
161
|
+
api.server.close(() => resolve());
|
|
162
|
+
api.server.closeIdleConnections?.();
|
|
163
|
+
} catch { resolve(); }
|
|
164
|
+
});
|
|
165
|
+
try { semantic?.dispose(); } catch { /* 同上 */ }
|
|
166
|
+
try { store.close(); } catch { /* 同上 */ }
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* daemon 侧语义配置:默认值逐键锚定 src/config.js(309-420),daemon 不装 schemastery
|
|
173
|
+
* (宿主 peer 依赖),同值硬编码;config.js 改默认值时此处与 docs/DAEMON.md 同步。
|
|
174
|
+
* 只放开 embedProvider(CLI --embed);rerank 暂不提供 CLI(默认关,与宿主同默认)。
|
|
175
|
+
*/
|
|
176
|
+
function daemonSemanticCfg(embed) {
|
|
177
|
+
return {
|
|
178
|
+
embedProvider: embed, // config.js:309(宿主默认 openai;daemon 默认 local——独立部署唯一自洽路径)
|
|
179
|
+
localEmbedModel: "Xenova/bge-small-zh-v1.5", // :312
|
|
180
|
+
localEmbedDimension: 512, // :313
|
|
181
|
+
localEmbedDevice: "cpu", // :314
|
|
182
|
+
localEmbedBatchSize: 8, // :315
|
|
183
|
+
localEmbedPooling: "auto", // :323(BGE 系 CLS 口径,改它=换向量空间)
|
|
184
|
+
ollamaBaseUrl: "http://localhost:11434", // :326
|
|
185
|
+
ollamaModel: "nomic-embed-text", // :331
|
|
186
|
+
embedModelCacheDir: "", // :336(空=local-embedder 内部解析 ~/.dsh/mneme/models)
|
|
187
|
+
embedModelMirror: "https://hf-mirror.com", // :337
|
|
188
|
+
resilientModelDownload: true, // :342
|
|
189
|
+
runtimeDir: process.env.DSH_MNEME_RUNTIME_DIR || "", // :348(空=defaultRuntimeDir;取件/加载同源)
|
|
190
|
+
rerankEnabled: false, // :419
|
|
191
|
+
rerankProvider: "none", // :420
|
|
192
|
+
autoReindexOnBoot: true // :367
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 本地推理 runtime 取件(download 档)。返回 payload 是否可用:
|
|
198
|
+
* 已存在(含收编/早前下载)→ true 不重复取;缺失 → 取件成功 true / 失败 false(内部
|
|
199
|
+
* 已打降级日志)。并发安全由 provisionRuntime 的 IN_FLIGHT 锁保证(按归一化 runtimeDir 串行)。
|
|
200
|
+
*/
|
|
201
|
+
async function ensureRuntimePayload(logger) {
|
|
202
|
+
const runtimeDir = process.env.DSH_MNEME_RUNTIME_DIR || "";
|
|
203
|
+
const dir = runtimeDir || defaultRuntimeDir();
|
|
204
|
+
if (listPayloadDirs(dir).length > 0) return true;
|
|
205
|
+
logger?.info?.("[dsh-mneme] 本地推理运行时缺失,开始取件(download 档,约 200MB;离线可用 DSH_MNEME_RUNTIME_TARBALL_DIR / DSH_MNEME_RUNTIME_MIRROR)…");
|
|
206
|
+
try {
|
|
207
|
+
const result = await provisionRuntime({ runtimeDir, localTarballDir: process.env.DSH_MNEME_RUNTIME_TARBALL_DIR || "", mirror: process.env.DSH_MNEME_RUNTIME_MIRROR || "" });
|
|
208
|
+
if (!result.ok) throw new Error(result.reason);
|
|
209
|
+
logger?.info?.(`[dsh-mneme] 运行时就绪(${result.strategy}):${result.packages} 包 / ${result.files} 文件`);
|
|
210
|
+
return true;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
logger?.warn?.(`[dsh-mneme] runtime provisioning failed, search degrades to keyword: ${String(error)}`);
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|