nexum-code 0.14.1 → 0.15.1
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/dist/{app-cVy5vxdq.mjs → app-5jOmOWG7.mjs} +10416 -9026
- package/dist/{dispatch-B2jPWSmW.mjs → dispatch-C98jHQkj.mjs} +13796 -11357
- package/dist/{dispatch-GH0cUcBE.mjs → dispatch-DA_D42qp.mjs} +1 -1
- package/dist/{dist-B_OG4w84.mjs → dist-CFFnObvk.mjs} +2 -2
- package/dist/main.mjs +1 -1
- package/dist/public/assets/index-CrkM1eFC.css +1 -0
- package/dist/public/assets/index-DYpA3Upa.js +93 -0
- package/dist/public/assets/index-DYpA3Upa.js.map +1 -0
- package/dist/public/gateway.html +156 -0
- package/dist/public/index.html +2 -2
- package/dist/{src-DCp4eCi5.mjs → src-h6ZExh4A.mjs} +1 -1
- package/dist/{src-BH9W5k24.mjs → src-tDEINaMV.mjs} +196 -17
- package/dist/{text-input-dialog-9TU2p7Py.mjs → text-input-dialog-Dvux6kIR.mjs} +74 -26
- package/dist/{text-input-dialog-D7p11_lV.mjs → text-input-dialog-WSd537y1.mjs} +1 -1
- package/package.json +96 -91
- package/dist/public/assets/index-CSHfYrxD.css +0 -1
- package/dist/public/assets/index-DCh1yGQ_.js +0 -90
- package/dist/public/assets/index-DCh1yGQ_.js.map +0 -1
- package/dist/{src-DlpH2wBe.mjs → src-CFHysu0q.mjs} +1 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="theme-color" content="#0d1117">
|
|
7
|
+
<title>Nexum — 访问验证</title>
|
|
8
|
+
<style>
|
|
9
|
+
:root { color-scheme: light dark; }
|
|
10
|
+
* { box-sizing: border-box; }
|
|
11
|
+
body {
|
|
12
|
+
margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center;
|
|
13
|
+
font-family: -apple-system, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
|
|
14
|
+
background: #0d1117; color: #e6edf3; padding: 24px;
|
|
15
|
+
position: relative;
|
|
16
|
+
}
|
|
17
|
+
/* Ambient glow: same shape/params as --ambient-glow (dark) */
|
|
18
|
+
body::before {
|
|
19
|
+
content: ""; position: fixed; inset: 0; z-index: 0; pointer-events: none;
|
|
20
|
+
background:
|
|
21
|
+
radial-gradient(42rem 30rem at 12% -8%, rgba(121, 235, 0, 0.07), transparent 60%),
|
|
22
|
+
radial-gradient(50rem 36rem at 105% 112%, rgba(88, 166, 255, 0.05), transparent 60%);
|
|
23
|
+
}
|
|
24
|
+
@media (prefers-color-scheme: light) {
|
|
25
|
+
body { background: #f7f8f7; color: #182019; }
|
|
26
|
+
body::before {
|
|
27
|
+
background:
|
|
28
|
+
radial-gradient(44rem 32rem at 12% -10%, rgba(11, 143, 63, 0.05), transparent 60%),
|
|
29
|
+
radial-gradient(48rem 34rem at 108% 112%, rgba(9, 105, 218, 0.04), transparent 60%);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
@keyframes rise-in {
|
|
33
|
+
from { opacity: 0; transform: translateY(6px); }
|
|
34
|
+
to { opacity: 1; transform: translateY(0); }
|
|
35
|
+
}
|
|
36
|
+
.card {
|
|
37
|
+
position: relative; z-index: 1;
|
|
38
|
+
width: 100%; max-width: min(360px, calc(100vw - 32px));
|
|
39
|
+
padding: 28px 24px 24px; border-radius: 16px; text-align: center;
|
|
40
|
+
background: #161b22; border: 1px solid #30363d;
|
|
41
|
+
box-shadow: 0 16px 48px rgba(0, 0, 0, 0.6);
|
|
42
|
+
animation: rise-in 320ms cubic-bezier(0.22, 1, 0.36, 1) both;
|
|
43
|
+
}
|
|
44
|
+
@media (prefers-color-scheme: light) {
|
|
45
|
+
.card {
|
|
46
|
+
background: #ffffff; border-color: #e7ebe8;
|
|
47
|
+
box-shadow: 0 16px 48px rgba(26, 29, 33, 0.18);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
.mark { font-size: 12px; letter-spacing: .2em; text-transform: uppercase; color: #8b949e; margin-bottom: 24px; }
|
|
51
|
+
h1 { font-size: 17px; font-weight: 600; margin: 0 0 6px; }
|
|
52
|
+
p.hint { font-size: 13px; color: #8b949e; margin: 0 0 22px; line-height: 1.5; }
|
|
53
|
+
@media (prefers-color-scheme: light) {
|
|
54
|
+
.mark, p.hint { color: #667069; }
|
|
55
|
+
}
|
|
56
|
+
input {
|
|
57
|
+
width: 100%; min-height: 44px; padding: 12px 14px; font-size: 15px; border-radius: 10px; text-align: center;
|
|
58
|
+
border: 1px solid rgba(127,127,127,.35); background: rgba(127,127,127,.08); color: inherit; outline: none;
|
|
59
|
+
transition: border-color 120ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 120ms cubic-bezier(0.22, 1, 0.36, 1);
|
|
60
|
+
}
|
|
61
|
+
input:focus { border-color: #79eb00; box-shadow: 0 0 0 3px rgba(121, 235, 0, 0.16); }
|
|
62
|
+
button {
|
|
63
|
+
width: 100%; min-height: 44px; margin-top: 12px; padding: 12px 14px; font-size: 15px; font-weight: 600; border: none;
|
|
64
|
+
border-radius: 10px; cursor: pointer; background: linear-gradient(135deg, #a4ff3d 0%, #66cc00 100%); color: #0d1117;
|
|
65
|
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
|
|
66
|
+
transition: box-shadow 120ms cubic-bezier(0.22, 1, 0.36, 1), transform 120ms cubic-bezier(0.22, 1, 0.36, 1), filter 120ms cubic-bezier(0.22, 1, 0.36, 1);
|
|
67
|
+
}
|
|
68
|
+
button:not(:disabled):hover { box-shadow: 0 0 18px rgba(121, 235, 0, 0.28); transform: translateY(-1px); }
|
|
69
|
+
button:not(:disabled):active { transform: translateY(0); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); }
|
|
70
|
+
button:disabled { opacity: .6; cursor: default; }
|
|
71
|
+
@media (prefers-color-scheme: light) {
|
|
72
|
+
input:focus { border-color: #16a34a; box-shadow: 0 0 0 3px rgba(11, 143, 63, 0.14); }
|
|
73
|
+
button { background: linear-gradient(135deg, #16a34a 0%, #0b8f3f 100%); color: #ffffff; box-shadow: 0 1px 2px rgba(26, 29, 33, 0.1); }
|
|
74
|
+
button:not(:disabled):hover { box-shadow: 0 0 14px rgba(11, 143, 63, 0.2); }
|
|
75
|
+
}
|
|
76
|
+
.error { min-height: 18px; font-size: 12.5px; margin-top: 12px; color: #f85149; }
|
|
77
|
+
.error.warn { color: #d29922; }
|
|
78
|
+
@media (prefers-color-scheme: light) {
|
|
79
|
+
.error { color: #cf222e; }
|
|
80
|
+
.error.warn { color: #9a6700; }
|
|
81
|
+
}
|
|
82
|
+
@media (prefers-reduced-motion: reduce) {
|
|
83
|
+
.card { animation: none; }
|
|
84
|
+
}
|
|
85
|
+
</style>
|
|
86
|
+
</head>
|
|
87
|
+
<body>
|
|
88
|
+
<main class="card">
|
|
89
|
+
<div class="mark">Nexum</div>
|
|
90
|
+
<h1>访问验证</h1>
|
|
91
|
+
<p class="hint">本服务开启了局域网共享,请输入访问密钥继续。</p>
|
|
92
|
+
<form id="form">
|
|
93
|
+
<input id="key" type="password" autocomplete="current-password" placeholder="访问密钥" autofocus>
|
|
94
|
+
<button id="go" type="submit">进入</button>
|
|
95
|
+
</form>
|
|
96
|
+
<div class="error" id="msg" role="alert"></div>
|
|
97
|
+
</main>
|
|
98
|
+
<script>
|
|
99
|
+
(function () {
|
|
100
|
+
var input = document.getElementById('key');
|
|
101
|
+
var button = document.getElementById('go');
|
|
102
|
+
var msg = document.getElementById('msg');
|
|
103
|
+
|
|
104
|
+
function safeNext() {
|
|
105
|
+
var params = new URLSearchParams(location.search);
|
|
106
|
+
var next = params.get('next') || '/';
|
|
107
|
+
if (next.charAt(0) !== '/' || next.charAt(1) === '/') next = '/';
|
|
108
|
+
return next;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fetch('/api/v1/gateway/status', { cache: 'no-store' })
|
|
112
|
+
.then(function (r) { return r.ok ? r.json() : null; })
|
|
113
|
+
.then(function (status) {
|
|
114
|
+
if (!status || !status.authRequired || status.authenticated) location.replace(safeNext());
|
|
115
|
+
})
|
|
116
|
+
.catch(function () { /* 状态不可知时仍显示表单 */ });
|
|
117
|
+
|
|
118
|
+
document.getElementById('form').addEventListener('submit', function (event) {
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
var key = input.value.trim();
|
|
121
|
+
if (!key) { msg.textContent = '请输入访问密钥'; msg.className = 'error'; return; }
|
|
122
|
+
button.disabled = true;
|
|
123
|
+
msg.textContent = '';
|
|
124
|
+
fetch('/api/v1/gateway/login', {
|
|
125
|
+
method: 'POST',
|
|
126
|
+
headers: { 'Content-Type': 'application/json' },
|
|
127
|
+
body: JSON.stringify({ key: key })
|
|
128
|
+
}).then(function (res) {
|
|
129
|
+
return res.json().catch(function () { return {}; }).then(function (body) {
|
|
130
|
+
return { status: res.status, body: body };
|
|
131
|
+
});
|
|
132
|
+
}).then(function (result) {
|
|
133
|
+
if (result.status === 200 && result.body && result.body.ok) {
|
|
134
|
+
location.replace(safeNext());
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
button.disabled = false;
|
|
138
|
+
if (result.status === 429) {
|
|
139
|
+
var wait = result.body && result.body.retryAfter ? result.body.retryAfter : 60;
|
|
140
|
+
msg.textContent = '尝试过于频繁,请 ' + wait + ' 秒后重试';
|
|
141
|
+
msg.className = 'error warn';
|
|
142
|
+
} else {
|
|
143
|
+
msg.textContent = '访问密钥错误';
|
|
144
|
+
msg.className = 'error';
|
|
145
|
+
input.select();
|
|
146
|
+
}
|
|
147
|
+
}).catch(function () {
|
|
148
|
+
button.disabled = false;
|
|
149
|
+
msg.textContent = '网络错误,请重试';
|
|
150
|
+
msg.className = 'error';
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
})();
|
|
154
|
+
</script>
|
|
155
|
+
</body>
|
|
156
|
+
</html>
|
package/dist/public/index.html
CHANGED
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
}
|
|
21
21
|
})();
|
|
22
22
|
</script>
|
|
23
|
-
<script type="module" crossorigin src="./assets/index-
|
|
24
|
-
<link rel="stylesheet" crossorigin href="./assets/index-
|
|
23
|
+
<script type="module" crossorigin src="./assets/index-DYpA3Upa.js"></script>
|
|
24
|
+
<link rel="stylesheet" crossorigin href="./assets/index-CrkM1eFC.css">
|
|
25
25
|
</head>
|
|
26
26
|
<body>
|
|
27
27
|
<div id="app"></div>
|
|
@@ -3,5 +3,5 @@ import { fileURLToPath as __cjsShimFileURLToPath } from 'node:url';
|
|
|
3
3
|
import { dirname as __cjsShimDirname } from 'node:path';
|
|
4
4
|
const __filename = __cjsShimFileURLToPath(import.meta.url);
|
|
5
5
|
const __dirname = __cjsShimDirname(__filename);
|
|
6
|
-
import { n as multiSearchWithTrace } from "./src-
|
|
6
|
+
import { n as multiSearchWithTrace } from "./src-tDEINaMV.mjs";
|
|
7
7
|
export { multiSearchWithTrace };
|
|
@@ -3,6 +3,7 @@ import { fileURLToPath as __cjsShimFileURLToPath } from 'node:url';
|
|
|
3
3
|
import { dirname as __cjsShimDirname } from 'node:path';
|
|
4
4
|
const __filename = __cjsShimFileURLToPath(import.meta.url);
|
|
5
5
|
const __dirname = __cjsShimDirname(__filename);
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
6
7
|
import { mkdir, readFile, readdir } from "node:fs/promises";
|
|
7
8
|
import { DatabaseSync } from "node:sqlite";
|
|
8
9
|
//#region ../../node_modules/.pnpm/pathe@2.0.3/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs
|
|
@@ -203,6 +204,8 @@ function jsonToVector(json) {
|
|
|
203
204
|
function isEntityType(value) {
|
|
204
205
|
return ENTITY_TYPES$1.has(value);
|
|
205
206
|
}
|
|
207
|
+
/** Meta key recording which embedding model the stored vectors came from. */
|
|
208
|
+
const EMBEDDING_MODEL_META_KEY = "embedding_model";
|
|
206
209
|
var KnowledgeStore = class {
|
|
207
210
|
dbPath;
|
|
208
211
|
db;
|
|
@@ -288,6 +291,22 @@ var KnowledgeStore = class {
|
|
|
288
291
|
this.db.exec("ROLLBACK");
|
|
289
292
|
} catch {}
|
|
290
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Read a metadata value (e.g. the embedding model identity the library was
|
|
296
|
+
* built with). Returns null when the key is absent.
|
|
297
|
+
*/
|
|
298
|
+
async getMeta(key) {
|
|
299
|
+
await this.init();
|
|
300
|
+
if (this.db === void 0) return null;
|
|
301
|
+
const row = this.db.prepare("SELECT value FROM knowledge_meta WHERE key = ?").get(key);
|
|
302
|
+
return row === void 0 ? null : asString(row["value"]);
|
|
303
|
+
}
|
|
304
|
+
/** Write a metadata value (upsert). */
|
|
305
|
+
async setMeta(key, value) {
|
|
306
|
+
await this.init();
|
|
307
|
+
if (this.db === void 0) throw new Error("knowledge store not initialized");
|
|
308
|
+
this.db.prepare("INSERT INTO knowledge_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(key, value);
|
|
309
|
+
}
|
|
291
310
|
createSchema() {
|
|
292
311
|
if (this.db === void 0) return;
|
|
293
312
|
this.db.exec(`
|
|
@@ -383,22 +402,39 @@ var KnowledgeStore = class {
|
|
|
383
402
|
CREATE VIRTUAL TABLE IF NOT EXISTS knowledge_entities_fts USING fts5(
|
|
384
403
|
name, description, content='knowledge_entities', content_rowid='rowid'
|
|
385
404
|
);
|
|
405
|
+
|
|
406
|
+
CREATE TABLE IF NOT EXISTS knowledge_meta (
|
|
407
|
+
key TEXT PRIMARY KEY,
|
|
408
|
+
value TEXT NOT NULL
|
|
409
|
+
);
|
|
386
410
|
`);
|
|
411
|
+
try {
|
|
412
|
+
this.db.exec("ALTER TABLE knowledge_sources ADD COLUMN content_hash TEXT");
|
|
413
|
+
} catch (error) {
|
|
414
|
+
if (!(error instanceof Error ? error.message : String(error)).includes("duplicate column")) throw error;
|
|
415
|
+
}
|
|
387
416
|
}
|
|
388
417
|
async createSource(params) {
|
|
389
418
|
await this.init();
|
|
390
419
|
if (this.db === void 0) throw new Error("knowledge store not initialized");
|
|
391
420
|
const id = generateId("src");
|
|
392
421
|
const createdAt = Date.now();
|
|
393
|
-
this.db.prepare("INSERT INTO knowledge_sources (id, name, file_path, description, created_at) VALUES (?, ?, ?, ?, ?)").run(id, params.name, params.filePath ?? null, params.description ?? null, createdAt);
|
|
422
|
+
this.db.prepare("INSERT INTO knowledge_sources (id, name, file_path, description, content_hash, created_at) VALUES (?, ?, ?, ?, ?, ?)").run(id, params.name, params.filePath ?? null, params.description ?? null, params.contentHash ?? null, createdAt);
|
|
394
423
|
return {
|
|
395
424
|
id,
|
|
396
425
|
name: params.name,
|
|
397
426
|
filePath: params.filePath ?? null,
|
|
398
427
|
description: params.description ?? null,
|
|
428
|
+
contentHash: params.contentHash ?? null,
|
|
399
429
|
createdAt
|
|
400
430
|
};
|
|
401
431
|
}
|
|
432
|
+
/** Backfill or refresh a source's content fingerprint (legacy rows / replacements). */
|
|
433
|
+
async updateSourceContentHash(id, contentHash) {
|
|
434
|
+
await this.init();
|
|
435
|
+
if (this.db === void 0) throw new Error("knowledge store not initialized");
|
|
436
|
+
this.db.prepare("UPDATE knowledge_sources SET content_hash = ? WHERE id = ?").run(contentHash, id);
|
|
437
|
+
}
|
|
402
438
|
async findSourceByFilePath(filePath) {
|
|
403
439
|
await this.init();
|
|
404
440
|
if (this.db === void 0) return void 0;
|
|
@@ -766,6 +802,87 @@ var KnowledgeStore = class {
|
|
|
766
802
|
}
|
|
767
803
|
return out;
|
|
768
804
|
}
|
|
805
|
+
/**
|
|
806
|
+
* Recompute every embedding for one source from the already-stored texts
|
|
807
|
+
* (no LLM extraction). Repairs libraries whose vectors are missing
|
|
808
|
+
* (ingested while the embedding model was unavailable) or stale (produced
|
|
809
|
+
* by a different model). All texts are embedded up front — any batch
|
|
810
|
+
* failure aborts before the first write, so the library is never left
|
|
811
|
+
* half-updated. On success the embedding-model meta is stamped with the
|
|
812
|
+
* engine's identity, unblocking future ingests after a model change.
|
|
813
|
+
*/
|
|
814
|
+
async reembedSource(sourceId, engine) {
|
|
815
|
+
await this.init();
|
|
816
|
+
if (this.db === void 0) throw new Error("knowledge store not initialized");
|
|
817
|
+
if (!engine.available) throw new Error("embedding engine unavailable");
|
|
818
|
+
const chunkRows = this.db.prepare("SELECT id, heading, content FROM knowledge_chunks WHERE source_id = ? ORDER BY rank ASC").all(sourceId);
|
|
819
|
+
const chunkTexts = chunkRows.map((c) => c.heading !== null ? `${c.heading}\n${c.content}` : c.content);
|
|
820
|
+
const eventRows = this.db.prepare("SELECT id, title, content FROM knowledge_events WHERE source_id = ? ORDER BY rank ASC").all(sourceId);
|
|
821
|
+
const eventTitleTexts = eventRows.map((e) => e.title);
|
|
822
|
+
const eventContentTexts = eventRows.map((e) => `${e.title}\n\n${e.content}`);
|
|
823
|
+
const entityRows = this.db.prepare("SELECT id, name FROM knowledge_entities WHERE source_id = ?").all(sourceId);
|
|
824
|
+
const relationRows = this.db.prepare(`SELECT ee.id, ee.description, e.title, en.name
|
|
825
|
+
FROM knowledge_event_entities ee
|
|
826
|
+
JOIN knowledge_events e ON e.id = ee.event_id
|
|
827
|
+
JOIN knowledge_entities en ON en.id = ee.entity_id
|
|
828
|
+
WHERE e.source_id = ?`).all(sourceId);
|
|
829
|
+
const relationTexts = relationRows.map((r) => r.description !== null && r.description.length > 0 ? r.description : `${r.title} ${r.name}`);
|
|
830
|
+
const embedAll = async (texts) => {
|
|
831
|
+
if (texts.length === 0) return [];
|
|
832
|
+
const vectors = await engine.embedBatch(texts);
|
|
833
|
+
if (vectors === null || vectors.length !== texts.length) throw new Error("向量嵌入失败,已取消本次重新嵌入");
|
|
834
|
+
return vectors;
|
|
835
|
+
};
|
|
836
|
+
const chunkVecs = await embedAll(chunkTexts);
|
|
837
|
+
const eventTitleVecs = await embedAll(eventTitleTexts);
|
|
838
|
+
const eventContentVecs = await embedAll(eventContentTexts);
|
|
839
|
+
const entityVecs = await embedAll(entityRows.map((e) => e.name));
|
|
840
|
+
const relationVecs = await embedAll(relationTexts);
|
|
841
|
+
this.beginTransaction();
|
|
842
|
+
try {
|
|
843
|
+
const updChunk = this.db.prepare("UPDATE knowledge_chunks SET embedding_json = ? WHERE id = ?");
|
|
844
|
+
for (let i = 0; i < chunkRows.length; i++) updChunk.run(vectorToJson(chunkVecs[i] ?? null), chunkRows[i].id);
|
|
845
|
+
const updEvent = this.db.prepare("UPDATE knowledge_events SET title_embedding_json = ?, content_embedding_json = ? WHERE id = ?");
|
|
846
|
+
for (let i = 0; i < eventRows.length; i++) updEvent.run(vectorToJson(eventTitleVecs[i] ?? null), vectorToJson(eventContentVecs[i] ?? null), eventRows[i].id);
|
|
847
|
+
const updEntity = this.db.prepare("UPDATE knowledge_entities SET embedding_json = ? WHERE id = ?");
|
|
848
|
+
for (let i = 0; i < entityRows.length; i++) updEntity.run(vectorToJson(entityVecs[i] ?? null), entityRows[i].id);
|
|
849
|
+
const updRelation = this.db.prepare("UPDATE knowledge_event_entities SET embedding_json = ? WHERE id = ?");
|
|
850
|
+
for (let i = 0; i < relationRows.length; i++) updRelation.run(vectorToJson(relationVecs[i] ?? null), relationRows[i].id);
|
|
851
|
+
await this.setMeta(EMBEDDING_MODEL_META_KEY, engine.modelName);
|
|
852
|
+
this.commitTransaction();
|
|
853
|
+
} catch (error) {
|
|
854
|
+
this.rollbackTransaction();
|
|
855
|
+
throw error;
|
|
856
|
+
}
|
|
857
|
+
return {
|
|
858
|
+
chunks: chunkRows.length,
|
|
859
|
+
events: eventRows.length,
|
|
860
|
+
entities: entityRows.length,
|
|
861
|
+
relations: relationRows.length
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Chunk vector coverage — how many of the stored chunks carry an embedding.
|
|
866
|
+
* Used by the TUI to surface partial/skipped embedding after an ingest.
|
|
867
|
+
*/
|
|
868
|
+
async embeddingCoverage() {
|
|
869
|
+
await this.init();
|
|
870
|
+
if (this.db === void 0) return {
|
|
871
|
+
total: 0,
|
|
872
|
+
embedded: 0
|
|
873
|
+
};
|
|
874
|
+
const row = this.db.prepare(`SELECT COUNT(*) AS total,
|
|
875
|
+
SUM(CASE WHEN embedding_json IS NOT NULL THEN 1 ELSE 0 END) AS embedded
|
|
876
|
+
FROM knowledge_chunks`).get();
|
|
877
|
+
if (row === void 0) return {
|
|
878
|
+
total: 0,
|
|
879
|
+
embedded: 0
|
|
880
|
+
};
|
|
881
|
+
return {
|
|
882
|
+
total: Number(row["total"] ?? 0),
|
|
883
|
+
embedded: Number(row["embedded"] ?? 0)
|
|
884
|
+
};
|
|
885
|
+
}
|
|
769
886
|
async stats() {
|
|
770
887
|
await this.init();
|
|
771
888
|
if (this.db === void 0) return {
|
|
@@ -825,6 +942,7 @@ function rowToSource(row) {
|
|
|
825
942
|
name: asString(row["name"]),
|
|
826
943
|
filePath: asNullableString(row["file_path"]),
|
|
827
944
|
description: asNullableString(row["description"]),
|
|
945
|
+
contentHash: asNullableString(row["content_hash"]),
|
|
828
946
|
createdAt: Number(row["created_at"])
|
|
829
947
|
};
|
|
830
948
|
}
|
|
@@ -1487,6 +1605,22 @@ async function mapWithConcurrency(items, limit, fn, onProgress) {
|
|
|
1487
1605
|
return results;
|
|
1488
1606
|
}
|
|
1489
1607
|
/**
|
|
1608
|
+
* Stamp or verify the embedding-model identity recorded in the library.
|
|
1609
|
+
* A library written by a different model would mix vector spaces — block the
|
|
1610
|
+
* ingest with a clear message instead of silently storing incompatible
|
|
1611
|
+
* embeddings. A legacy library without the marker is stamped with the current
|
|
1612
|
+
* model (all existing data was written by it).
|
|
1613
|
+
*/
|
|
1614
|
+
async function ensureEmbeddingModelMatches(store, engine) {
|
|
1615
|
+
if (engine === void 0) return;
|
|
1616
|
+
const recorded = await store.getMeta(EMBEDDING_MODEL_META_KEY);
|
|
1617
|
+
if (recorded === null) {
|
|
1618
|
+
await store.setMeta(EMBEDDING_MODEL_META_KEY, engine.modelName);
|
|
1619
|
+
return;
|
|
1620
|
+
}
|
|
1621
|
+
if (recorded !== engine.modelName) throw new Error(`知识库由模型 ${recorded} 嵌入,当前模型为 ${engine.modelName},请先在 /knowledge 中重新嵌入知识库`);
|
|
1622
|
+
}
|
|
1623
|
+
/**
|
|
1490
1624
|
* Ingest a markdown file into the knowledge base.
|
|
1491
1625
|
*
|
|
1492
1626
|
* Steps:
|
|
@@ -1511,34 +1645,53 @@ async function ingestFile(store, llm, filePath, onProgress) {
|
|
|
1511
1645
|
stage: "embedding-check",
|
|
1512
1646
|
message: "向量模型未就绪,本次摄入跳过向量嵌入"
|
|
1513
1647
|
});
|
|
1514
|
-
|
|
1515
|
-
if (existing !== void 0) {
|
|
1516
|
-
onProgress?.({
|
|
1517
|
-
stage: "error",
|
|
1518
|
-
message: `文件已摄入:${existing.name}(source id=${existing.id})`
|
|
1519
|
-
});
|
|
1520
|
-
throw new Error(`文件已摄入过: ${filePath} (source ${existing.id})`);
|
|
1521
|
-
}
|
|
1648
|
+
await ensureEmbeddingModelMatches(store, engine);
|
|
1522
1649
|
onProgress?.({
|
|
1523
1650
|
stage: "chunking",
|
|
1524
1651
|
message: `读取并切分文件: ${basename(filePath)}`
|
|
1525
1652
|
});
|
|
1526
1653
|
const content = await readFileContent(filePath);
|
|
1527
|
-
const
|
|
1528
|
-
|
|
1654
|
+
const contentHash = createHash("sha256").update(content).digest("hex");
|
|
1655
|
+
const existing = await store.findSourceByFilePath(filePath);
|
|
1656
|
+
let replacedOldVersion = false;
|
|
1657
|
+
if (existing !== void 0) {
|
|
1658
|
+
if (existing.contentHash === contentHash || existing.contentHash === null) {
|
|
1659
|
+
if (existing.contentHash === null) await store.updateSourceContentHash(existing.id, contentHash);
|
|
1660
|
+
onProgress?.({
|
|
1661
|
+
stage: "completed",
|
|
1662
|
+
message: "内容未变化,已跳过"
|
|
1663
|
+
});
|
|
1664
|
+
return {
|
|
1665
|
+
documentId: null,
|
|
1666
|
+
chunkCount: 0,
|
|
1667
|
+
eventCount: 0,
|
|
1668
|
+
entityCount: 0,
|
|
1669
|
+
outcome: "unchanged"
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1529
1672
|
onProgress?.({
|
|
1530
|
-
stage: "
|
|
1531
|
-
message:
|
|
1673
|
+
stage: "chunking",
|
|
1674
|
+
message: `检测到内容变更,替换旧版本: ${existing.name}`
|
|
1532
1675
|
});
|
|
1533
|
-
|
|
1676
|
+
await store.deleteSource(existing.id);
|
|
1677
|
+
replacedOldVersion = true;
|
|
1534
1678
|
}
|
|
1679
|
+
const sections = chunkContent(content, getFileExtension(filePath));
|
|
1535
1680
|
store.beginTransaction();
|
|
1536
1681
|
try {
|
|
1682
|
+
if (sections.length === 0) {
|
|
1683
|
+
onProgress?.({
|
|
1684
|
+
stage: "error",
|
|
1685
|
+
message: "文件无有效内容"
|
|
1686
|
+
});
|
|
1687
|
+
throw new Error("文件无有效内容可摄入");
|
|
1688
|
+
}
|
|
1537
1689
|
const fileName = basename(filePath);
|
|
1538
1690
|
const source = await store.createSource({
|
|
1539
1691
|
name: fileName,
|
|
1540
1692
|
filePath,
|
|
1541
|
-
description: null
|
|
1693
|
+
description: null,
|
|
1694
|
+
contentHash
|
|
1542
1695
|
});
|
|
1543
1696
|
const document = await store.createDocument({
|
|
1544
1697
|
sourceId: source.id,
|
|
@@ -1687,10 +1840,15 @@ async function ingestFile(store, llm, filePath, onProgress) {
|
|
|
1687
1840
|
documentId: document.id,
|
|
1688
1841
|
chunkCount: chunks.length,
|
|
1689
1842
|
eventCount: events.length,
|
|
1690
|
-
entityCount: uniqueEntities.length
|
|
1843
|
+
entityCount: uniqueEntities.length,
|
|
1844
|
+
outcome: replacedOldVersion ? "updated" : "created"
|
|
1691
1845
|
};
|
|
1692
1846
|
} catch (error) {
|
|
1693
1847
|
store.rollbackTransaction();
|
|
1848
|
+
if (replacedOldVersion) {
|
|
1849
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1850
|
+
throw new Error(`旧版本已删除、新版本摄入失败,请重新摄入该文件: ${filePath}(${message})`);
|
|
1851
|
+
}
|
|
1694
1852
|
throw error;
|
|
1695
1853
|
}
|
|
1696
1854
|
}
|
|
@@ -1709,6 +1867,9 @@ async function ingestDirectory(store, llm, dirPath, onProgress) {
|
|
|
1709
1867
|
if (files.length === 0) throw new Error(`目录下没有支持的文件 (.md, .markdown, .txt): ${dirPath}`);
|
|
1710
1868
|
const errors = [];
|
|
1711
1869
|
let succeeded = 0;
|
|
1870
|
+
let created = 0;
|
|
1871
|
+
let updated = 0;
|
|
1872
|
+
let unchanged = 0;
|
|
1712
1873
|
let totalChunks = 0;
|
|
1713
1874
|
let totalEvents = 0;
|
|
1714
1875
|
let totalEntities = 0;
|
|
@@ -1724,6 +1885,9 @@ async function ingestDirectory(store, llm, dirPath, onProgress) {
|
|
|
1724
1885
|
try {
|
|
1725
1886
|
const result = await ingestFile(store, llm, filePath, fileProgress);
|
|
1726
1887
|
succeeded += 1;
|
|
1888
|
+
if (result.outcome === "created") created += 1;
|
|
1889
|
+
else if (result.outcome === "updated") updated += 1;
|
|
1890
|
+
else unchanged += 1;
|
|
1727
1891
|
totalChunks += result.chunkCount;
|
|
1728
1892
|
totalEvents += result.eventCount;
|
|
1729
1893
|
totalEntities += result.entityCount;
|
|
@@ -1741,11 +1905,14 @@ async function ingestDirectory(store, llm, dirPath, onProgress) {
|
|
|
1741
1905
|
}
|
|
1742
1906
|
onProgress?.({
|
|
1743
1907
|
stage: "completed",
|
|
1744
|
-
message:
|
|
1908
|
+
message: `批量摄入完成:新增 ${created} · 更新 ${updated} · 未变化 ${unchanged} · 失败 ${errors.length}(${totalChunks} chunks, ${totalEvents} events, ${totalEntities} entities)`
|
|
1745
1909
|
});
|
|
1746
1910
|
return {
|
|
1747
1911
|
succeeded,
|
|
1748
1912
|
failed: errors.length,
|
|
1913
|
+
created,
|
|
1914
|
+
updated,
|
|
1915
|
+
unchanged,
|
|
1749
1916
|
totalChunks,
|
|
1750
1917
|
totalEvents,
|
|
1751
1918
|
totalEntities,
|
|
@@ -1820,6 +1987,18 @@ async function multiSearchWithTrace(store, llm, query, options = {}) {
|
|
|
1820
1987
|
}
|
|
1821
1988
|
};
|
|
1822
1989
|
}
|
|
1990
|
+
const recordedModel = await store.getMeta(EMBEDDING_MODEL_META_KEY);
|
|
1991
|
+
if (recordedModel !== null && recordedModel !== engine.modelName) {
|
|
1992
|
+
fallbackReason = `embedding model mismatch (library: ${recordedModel}, current: ${engine.modelName}); used FTS5 keyword fallback`;
|
|
1993
|
+
return {
|
|
1994
|
+
results: await ftsFallback(store, query, topK),
|
|
1995
|
+
trace: {
|
|
1996
|
+
steps,
|
|
1997
|
+
rerankedEventTitles,
|
|
1998
|
+
fallbackReason
|
|
1999
|
+
}
|
|
2000
|
+
};
|
|
2001
|
+
}
|
|
1823
2002
|
const queryEmbeddings = await timed(onStep, "queryEmbedding", "把用户问题转成向量,用于召回相关事件和切片。", async () => engine.embedBatch([query]));
|
|
1824
2003
|
if (queryEmbeddings === null || queryEmbeddings.length === 0) {
|
|
1825
2004
|
fallbackReason = "query embedding returned null; used FTS5 fallback";
|