@xiaofanzhao/dsh-plugin-knowledge-base 0.0.0-stage → 0.2.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.
@@ -0,0 +1,148 @@
1
+ // ============================================================================
2
+ // 插件自己的设置文件:$DSH_HOME/kb/settings.json(0600)
3
+ //
4
+ // 放两类会反复试的配置:向量检索(embeddings)和图片理解(视觉模型)。
5
+ // 理由:改 YAML 要重载插件才生效,而这两样用户会来回换服务、换模型、填错 key。
6
+ // 放这里可以即改即用,且 KEY 不落到 patch 里。
7
+ // 优先级:settings.json > 插件 config。
8
+ // ============================================================================
9
+ import fs from 'node:fs';
10
+ import path from 'node:path';
11
+
12
+ /**
13
+ * 出厂的默认端点(只有地址和模型名,**没有任何密钥**)。
14
+ * 用户装上后在设置里只需要填 API Key + 勾选启用。
15
+ */
16
+ export const DEFAULT_EMBEDDING = {
17
+ baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
18
+ model: 'text-embedding-v4',
19
+ };
20
+
21
+ export const DEFAULT_VISION = {
22
+ baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
23
+ model: 'qwen3-vl-plus',
24
+ };
25
+
26
+ function clean(value) {
27
+ return typeof value === 'string' ? value.trim() : '';
28
+ }
29
+
30
+ function normalizeEndpoint(input) {
31
+ const raw = input && typeof input === 'object' ? input : {};
32
+ return {
33
+ enabled: Boolean(raw.enabled),
34
+ baseUrl: clean(raw.baseUrl).replace(/\/+$/, ''),
35
+ model: clean(raw.model),
36
+ apiKey: clean(raw.apiKey),
37
+ };
38
+ }
39
+
40
+ export function createSettingsStore({ root, logger }) {
41
+ const file = path.join(root, 'settings.json');
42
+
43
+ function readAll() {
44
+ try {
45
+ const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
46
+ return raw && typeof raw === 'object' ? raw : {};
47
+ } catch {
48
+ return {};
49
+ }
50
+ }
51
+
52
+ function writeAll(next) {
53
+ try {
54
+ fs.writeFileSync(file, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
55
+ try { fs.chmodSync(file, 0o600); } catch { /* Windows 上可能不支持,忽略 */ }
56
+ return true;
57
+ } catch (error) {
58
+ logger?.warn?.(`[knowledge-base] 写设置失败:${error instanceof Error ? error.message : String(error)}`);
59
+ return false;
60
+ }
61
+ }
62
+
63
+ /** 读当前向量配置(未填过就是空值)。 */
64
+ function embedding() {
65
+ return normalizeEndpoint(readAll().embedding);
66
+ }
67
+
68
+ /** 读当前图片理解配置。 */
69
+ function vision() {
70
+ return normalizeEndpoint(readAll().vision);
71
+ }
72
+
73
+ /** 某个配置块有没有被界面保存过(没保存就回落到插件 YAML)。 */
74
+ function hasSaved(section = 'embedding') {
75
+ const raw = readAll()[section];
76
+ return Boolean(raw && typeof raw === 'object');
77
+ }
78
+
79
+ function saveSection(section, patch) {
80
+ const current = normalizeEndpoint(readAll()[section]);
81
+ const next = { ...current };
82
+ if (patch && typeof patch === 'object') {
83
+ if ('enabled' in patch) next.enabled = Boolean(patch.enabled);
84
+ if ('baseUrl' in patch) next.baseUrl = clean(patch.baseUrl).replace(/\/+$/, '');
85
+ if ('model' in patch) next.model = clean(patch.model);
86
+ if (patch.clearKey === true) next.apiKey = '';
87
+ else if (typeof patch.apiKey === 'string' && patch.apiKey.trim()) next.apiKey = patch.apiKey.trim();
88
+ }
89
+ const all = readAll();
90
+ all[section] = next;
91
+ const ok = writeAll(all);
92
+ return { ok, [section]: next };
93
+ }
94
+
95
+ /**
96
+ * 保存向量配置。
97
+ * @param {object} patch - { enabled?, baseUrl?, model?, apiKey?, clearKey? }
98
+ */
99
+ function saveEmbedding(patch) {
100
+ const result = saveSection('embedding', patch);
101
+ return { ok: result.ok, embedding: result.embedding };
102
+ }
103
+
104
+ /** 保存图片理解配置。 */
105
+ function saveVision(patch) {
106
+ const result = saveSection('vision', patch);
107
+ return { ok: result.ok, vision: result.vision };
108
+ }
109
+
110
+ /** 给界面看的版本:key 只留尾号。 */
111
+ function publicSection(section, fallback, defaults, keyFallback = '') {
112
+ const saved = readAll()[section];
113
+ const effective = saved && typeof saved === 'object' ? normalizeEndpoint(saved) : normalizeEndpoint(fallback);
114
+ const key = effective.apiKey || keyFallback || process.env[`DSH_KB_${section.toUpperCase()}_KEY`] || '';
115
+ return {
116
+ enabled: effective.enabled,
117
+ // 没填过地址/模型就回落到出厂默认,用户只需要补 Key
118
+ baseUrl: effective.baseUrl || defaults.baseUrl,
119
+ model: effective.model || defaults.model,
120
+ defaultBaseUrl: defaults.baseUrl,
121
+ defaultModel: defaults.model,
122
+ hasKey: Boolean(key),
123
+ keyTail: key ? key.slice(-4) : '',
124
+ fromFile: Boolean(saved && typeof saved === 'object'),
125
+ };
126
+ }
127
+
128
+ function publicEmbedding(fallback = {}) {
129
+ return publicSection('embedding', fallback, DEFAULT_EMBEDDING);
130
+ }
131
+
132
+ /** 图片理解常和向量检索用同一个服务商,没单独填 key 时可以复用嵌入的 key。 */
133
+ function publicVision(fallback = {}) {
134
+ const embeddingKey = embedding().apiKey || process.env.DSH_KB_EMBEDDING_KEY || '';
135
+ return publicSection('vision', fallback, DEFAULT_VISION, embeddingKey);
136
+ }
137
+
138
+ return {
139
+ file,
140
+ embedding,
141
+ vision,
142
+ hasSaved,
143
+ saveEmbedding,
144
+ saveVision,
145
+ publicEmbedding,
146
+ publicVision,
147
+ };
148
+ }
package/lib/store.mjs ADDED
@@ -0,0 +1,445 @@
1
+ // ============================================================================
2
+ // 库存储:SQLite(node:sqlite,随 Node 24 内置,无第三方依赖)
3
+ //
4
+ // 三张业务表 + 一张全文索引表:
5
+ // kbs 知识库
6
+ // nodes 树:folder / doc 两种节点,adjacency + 物化 id 路径(移动子树只需一次前缀更新)
7
+ // doc_text 文档抽取出的正文(阅读器按字符偏移分页读)
8
+ // chunks 检索分块;chunks_fts 是它的 CJK 归一化全文索引(rowid 对齐 chunks.id)
9
+ // ============================================================================
10
+ import { DatabaseSync } from 'node:sqlite';
11
+ import fs from 'node:fs';
12
+ import path from 'node:path';
13
+
14
+ const SCHEMA = `
15
+ CREATE TABLE IF NOT EXISTS kbs (
16
+ id TEXT PRIMARY KEY,
17
+ name TEXT NOT NULL,
18
+ created_at INTEGER NOT NULL,
19
+ updated_at INTEGER NOT NULL,
20
+ sort INTEGER NOT NULL DEFAULT 0
21
+ );
22
+
23
+ CREATE TABLE IF NOT EXISTS nodes (
24
+ id TEXT PRIMARY KEY,
25
+ kb_id TEXT NOT NULL,
26
+ parent_id TEXT,
27
+ kind TEXT NOT NULL,
28
+ name TEXT NOT NULL,
29
+ path TEXT NOT NULL,
30
+ sort INTEGER NOT NULL DEFAULT 0,
31
+ file_path TEXT,
32
+ ext TEXT,
33
+ mime TEXT,
34
+ bytes INTEGER,
35
+ sha256 TEXT,
36
+ status TEXT NOT NULL DEFAULT 'ready',
37
+ error TEXT,
38
+ page_count INTEGER,
39
+ chars INTEGER,
40
+ indexed_at INTEGER,
41
+ created_at INTEGER NOT NULL,
42
+ updated_at INTEGER NOT NULL
43
+ );
44
+ CREATE INDEX IF NOT EXISTS nodes_by_parent ON nodes(kb_id, parent_id, sort);
45
+ CREATE INDEX IF NOT EXISTS nodes_by_path ON nodes(kb_id, path);
46
+
47
+ CREATE TABLE IF NOT EXISTS doc_text (
48
+ node_id TEXT PRIMARY KEY,
49
+ text TEXT NOT NULL,
50
+ updated_at INTEGER NOT NULL
51
+ );
52
+
53
+ CREATE TABLE IF NOT EXISTS chunks (
54
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
55
+ node_id TEXT NOT NULL,
56
+ kb_id TEXT NOT NULL,
57
+ ord INTEGER NOT NULL,
58
+ heading TEXT,
59
+ page INTEGER,
60
+ char_start INTEGER NOT NULL,
61
+ char_end INTEGER NOT NULL,
62
+ text TEXT NOT NULL
63
+ );
64
+ CREATE INDEX IF NOT EXISTS chunks_by_doc ON chunks(node_id, ord);
65
+
66
+ CREATE VIRTUAL TABLE IF NOT EXISTS chunks_fts USING fts5(
67
+ cjk,
68
+ kb_id UNINDEXED,
69
+ node_id UNINDEXED,
70
+ tokenize='unicode61'
71
+ );
72
+
73
+ CREATE TABLE IF NOT EXISTS chunk_vec (
74
+ node_id TEXT NOT NULL,
75
+ ord INTEGER NOT NULL,
76
+ dim INTEGER NOT NULL,
77
+ vec BLOB NOT NULL,
78
+ PRIMARY KEY (node_id, ord)
79
+ );
80
+
81
+ CREATE TABLE IF NOT EXISTS meta (
82
+ key TEXT PRIMARY KEY,
83
+ value TEXT
84
+ );
85
+
86
+ CREATE TABLE IF NOT EXISTS search_log (
87
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
88
+ ts INTEGER NOT NULL,
89
+ query TEXT NOT NULL,
90
+ mode TEXT NOT NULL,
91
+ hits INTEGER NOT NULL,
92
+ took_ms INTEGER NOT NULL,
93
+ kb_id TEXT
94
+ );
95
+ CREATE INDEX IF NOT EXISTS search_log_by_ts ON search_log(ts);
96
+ `;
97
+
98
+ export function openStore({ root, logger }) {
99
+ const filesDir = path.join(root, 'files');
100
+ const tmpDir = path.join(root, 'tmp');
101
+ fs.mkdirSync(filesDir, { recursive: true });
102
+ fs.mkdirSync(tmpDir, { recursive: true });
103
+ // 上次进程留下的半成品上传与临时文件,启动时清掉
104
+ for (const name of fs.readdirSync(tmpDir)) {
105
+ try { fs.rmSync(path.join(tmpDir, name), { recursive: true, force: true }); } catch { /* ignore */ }
106
+ }
107
+
108
+ const db = new DatabaseSync(path.join(root, 'kb.db'));
109
+ db.exec('PRAGMA journal_mode = WAL;');
110
+ db.exec('PRAGMA synchronous = NORMAL;');
111
+ db.exec(SCHEMA);
112
+
113
+ const q = (sql) => db.prepare(sql);
114
+
115
+ // ── 知识库 ────────────────────────────────────────────────────────────
116
+ function listKbs() {
117
+ const rows = q(`SELECT id, name, created_at AS createdAt, updated_at AS updatedAt
118
+ FROM kbs ORDER BY sort, created_at`).all();
119
+ const counts = q(`SELECT kb_id AS kbId, COUNT(*) AS n FROM nodes WHERE kind = 'doc' GROUP BY kb_id`).all();
120
+ const byId = new Map(counts.map((row) => [row.kbId, row.n]));
121
+ return rows.map((row) => ({ ...row, docs: byId.get(row.id) ?? 0 }));
122
+ }
123
+
124
+ function getKb(id) {
125
+ return q(`SELECT id, name, created_at AS createdAt, updated_at AS updatedAt FROM kbs WHERE id = ?`).get(id) ?? null;
126
+ }
127
+
128
+ function createKb({ id, name, now }) {
129
+ const sort = (q(`SELECT COALESCE(MAX(sort), -1) AS s FROM kbs`).get()?.s ?? -1) + 1;
130
+ q(`INSERT INTO kbs (id, name, created_at, updated_at, sort) VALUES (?, ?, ?, ?, ?)`).run(id, name, now, now, sort);
131
+ return getKb(id);
132
+ }
133
+
134
+ function renameKb(id, name, now) {
135
+ q(`UPDATE kbs SET name = ?, updated_at = ? WHERE id = ?`).run(name, now, id);
136
+ return getKb(id);
137
+ }
138
+
139
+ function deleteKb(id) {
140
+ const nodes = q(`SELECT id FROM nodes WHERE kb_id = ?`).all(id).map((row) => row.id);
141
+ for (const nodeId of nodes) removeDocData(nodeId);
142
+ q(`DELETE FROM nodes WHERE kb_id = ?`).run(id);
143
+ q(`DELETE FROM kbs WHERE id = ?`).run(id);
144
+ return { removed: nodes.length };
145
+ }
146
+
147
+ // ── 节点 ──────────────────────────────────────────────────────────────
148
+ function nodeRow(row) {
149
+ if (!row) return null;
150
+ return {
151
+ id: row.id,
152
+ kbId: row.kb_id,
153
+ parentId: row.parent_id ?? null,
154
+ kind: row.kind,
155
+ name: row.name,
156
+ path: row.path,
157
+ sort: row.sort,
158
+ ext: row.ext ?? '',
159
+ mime: row.mime ?? '',
160
+ bytes: row.bytes ?? 0,
161
+ status: row.status,
162
+ error: row.error ?? '',
163
+ pageCount: row.page_count ?? 0,
164
+ chars: row.chars ?? 0,
165
+ indexedAt: row.indexed_at ?? 0,
166
+ createdAt: row.created_at,
167
+ updatedAt: row.updated_at,
168
+ };
169
+ }
170
+
171
+ function getNode(id) {
172
+ return nodeRow(q(`SELECT * FROM nodes WHERE id = ?`).get(id));
173
+ }
174
+
175
+ function listNodes(kbId) {
176
+ return q(`SELECT * FROM nodes WHERE kb_id = ? ORDER BY parent_id, sort`).all(kbId).map(nodeRow);
177
+ }
178
+
179
+ function children(parentId, kbId) {
180
+ return q(`SELECT * FROM nodes WHERE kb_id = ? AND parent_id IS ? ORDER BY sort`).all(kbId, parentId).map(nodeRow);
181
+ }
182
+
183
+ function nextSort(kbId, parentId) {
184
+ const row = q(`SELECT COALESCE(MAX(sort), -1) AS s FROM nodes WHERE kb_id = ? AND parent_id IS ?`).get(kbId, parentId);
185
+ return (row?.s ?? -1) + 1;
186
+ }
187
+
188
+ function createNode({ id, kbId, parentId, kind, name, now, ext = '', mime = '', bytes = 0, filePath = null, status = 'ready' }) {
189
+ const parent = parentId ? getNode(parentId) : null;
190
+ if (parentId && (!parent || parent.kbId !== kbId)) throw new Error('父节点不存在');
191
+ const parentPath = parent ? parent.path : '';
192
+ const sort = nextSort(kbId, parentId);
193
+ q(`INSERT INTO nodes (id, kb_id, parent_id, kind, name, path, sort, file_path, ext, mime, bytes, status,
194
+ created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
195
+ .run(id, kbId, parentId, kind, name, `${parentPath}/${id}`, sort, filePath, ext, mime, bytes, status, now, now);
196
+ return getNode(id);
197
+ }
198
+
199
+ function renameNode(id, name, now) {
200
+ q(`UPDATE nodes SET name = ?, updated_at = ? WHERE id = ?`).run(name, now, id);
201
+ return getNode(id);
202
+ }
203
+
204
+ function moveNode(id, parentId, index, now) {
205
+ const node = getNode(id);
206
+ if (!node) throw new Error('节点不存在');
207
+ if (parentId === id) throw new Error('不能移动到自身');
208
+ const target = parentId ? getNode(parentId) : null;
209
+ if (parentId && (!target || target.kbId !== node.kbId)) throw new Error('目标目录不存在');
210
+ if (parentId && (target.path === node.path || target.path.startsWith(`${node.path}/`))) {
211
+ throw new Error('不能移动到自己的子目录');
212
+ }
213
+ const newPath = `${target ? target.path : ''}/${id}`;
214
+ db.exec('BEGIN');
215
+ try {
216
+ q(`UPDATE nodes SET path = replace(path, ?, ?) WHERE kb_id = ? AND (path = ? OR path LIKE ?)`)
217
+ .run(node.path, newPath, node.kbId, node.path, `${node.path}/%`);
218
+ q(`UPDATE nodes SET parent_id = ?, sort = ?, updated_at = ? WHERE id = ?`)
219
+ .run(parentId, 1e9, now, id);
220
+ const siblings = q(`SELECT id FROM nodes WHERE kb_id = ? AND parent_id IS ? AND id <> ? ORDER BY sort`)
221
+ .all(node.kbId, parentId, id).map((row) => row.id);
222
+ const at = Number.isInteger(index) && index >= 0 ? Math.min(index, siblings.length) : siblings.length;
223
+ siblings.splice(at, 0, id);
224
+ const stmt = q(`UPDATE nodes SET sort = ? WHERE id = ?`);
225
+ siblings.forEach((siblingId, position) => stmt.run(position, siblingId));
226
+ db.exec('COMMIT');
227
+ } catch (error) {
228
+ db.exec('ROLLBACK');
229
+ throw error;
230
+ }
231
+ return getNode(id);
232
+ }
233
+
234
+ function subtreeIds(node) {
235
+ const rows = q(`SELECT id, kind FROM nodes WHERE path = ? OR path LIKE ?`).all(node.path, `${node.path}/%`);
236
+ return rows;
237
+ }
238
+
239
+ function removeDocData(nodeId) {
240
+ const chunkRows = q(`SELECT id FROM chunks WHERE node_id = ?`).all(nodeId);
241
+ const delFts = q(`DELETE FROM chunks_fts WHERE rowid = ?`);
242
+ for (const row of chunkRows) delFts.run(row.id);
243
+ q(`DELETE FROM chunks WHERE node_id = ?`).run(nodeId);
244
+ q(`DELETE FROM chunk_vec WHERE node_id = ?`).run(nodeId);
245
+ q(`DELETE FROM doc_text WHERE node_id = ?`).run(nodeId);
246
+ const doc = q(`SELECT file_path AS filePath FROM nodes WHERE id = ?`).get(nodeId);
247
+ if (doc?.filePath) {
248
+ try { fs.rmSync(doc.filePath, { force: true }); } catch { /* ignore */ }
249
+ }
250
+ // 转换出来的预览 PDF 也一并清掉
251
+ if (doc?.filePath) {
252
+ try { fs.rmSync(path.join(path.dirname(doc.filePath), `${nodeId}.preview.pdf`), { force: true }); } catch { /* ignore */ }
253
+ }
254
+ }
255
+
256
+ function deleteNode(id) {
257
+ const node = getNode(id);
258
+ if (!node) throw new Error('节点不存在');
259
+ const rows = subtreeIds(node);
260
+ db.exec('BEGIN');
261
+ try {
262
+ for (const row of rows) removeDocData(row.id);
263
+ q(`DELETE FROM nodes WHERE id = ?`).run(id);
264
+ const rest = rows.filter((row) => row.id !== id).map((row) => row.id);
265
+ for (const restId of rest) q(`DELETE FROM nodes WHERE id = ?`).run(restId);
266
+ db.exec('COMMIT');
267
+ } catch (error) {
268
+ db.exec('ROLLBACK');
269
+ throw error;
270
+ }
271
+ return { removed: rows.length };
272
+ }
273
+
274
+ function setDocStatus(id, status, extra = {}) {
275
+ q(`UPDATE nodes SET status = ?, error = ?, page_count = ?, chars = ?, indexed_at = ?, updated_at = ?
276
+ WHERE id = ?`).run(
277
+ status,
278
+ extra.error ?? null,
279
+ extra.pageCount ?? null,
280
+ extra.chars ?? null,
281
+ extra.indexedAt ?? null,
282
+ extra.updatedAt ?? Date.now(),
283
+ id,
284
+ );
285
+ return getNode(id);
286
+ }
287
+
288
+ function saveText(nodeId, text, now) { q(`INSERT INTO doc_text (node_id, text, updated_at) VALUES (?, ?, ?)
289
+ ON CONFLICT(node_id) DO UPDATE SET text = excluded.text, updated_at = excluded.updated_at`)
290
+ .run(nodeId, text, now);
291
+ }
292
+
293
+ function getText(nodeId) {
294
+ const row = q(`SELECT text FROM doc_text WHERE node_id = ?`).get(nodeId);
295
+ return row ? row.text : '';
296
+ }
297
+
298
+ function breadcrumb(nodeId) {
299
+ const names = [];
300
+ let current = getNode(nodeId);
301
+ let guardCount = 0;
302
+ while (current && guardCount < 32) {
303
+ names.unshift(current.name);
304
+ current = current.parentId ? getNode(current.parentId) : null;
305
+ guardCount += 1;
306
+ }
307
+ return names.join(' / ');
308
+ }
309
+
310
+ function stats() {
311
+ return {
312
+ kbs: q(`SELECT COUNT(*) AS n FROM kbs`).get()?.n ?? 0,
313
+ docs: q(`SELECT COUNT(*) AS n FROM nodes WHERE kind = 'doc'`).get()?.n ?? 0,
314
+ chunks: q(`SELECT COUNT(*) AS n FROM chunks`).get()?.n ?? 0,
315
+ bytes: q(`SELECT COALESCE(SUM(bytes), 0) AS n FROM nodes WHERE kind = 'doc'`).get()?.n ?? 0,
316
+ vectors: q(`SELECT COUNT(*) AS n FROM chunk_vec`).get()?.n ?? 0,
317
+ };
318
+ }
319
+
320
+ function mapNodeByIds(ids) {
321
+ const out = new Map();
322
+ const stmt = q(`SELECT * FROM nodes WHERE id = ?`);
323
+ for (const id of ids) {
324
+ const row = stmt.get(id);
325
+ if (row) out.set(id, nodeRow(row));
326
+ }
327
+ return out;
328
+ }
329
+
330
+ // ── 检索日志(看板用)──────────────────────────────────────────────────
331
+ const SEARCH_LOG_KEEP = 2000;
332
+
333
+ function logSearch({ now, query, mode, hits, tookMs, kbId }) {
334
+ try {
335
+ q(`INSERT INTO search_log (ts, query, mode, hits, took_ms, kb_id) VALUES (?, ?, ?, ?, ?, ?)`)
336
+ .run(now, String(query ?? '').slice(0, 200), String(mode ?? 'keyword'), Number(hits) || 0,
337
+ Number(tookMs) || 0, kbId || null);
338
+ // 只留最近 SEARCH_LOG_KEEP 条,别让日志把库撑大
339
+ q(`DELETE FROM search_log WHERE id <= (SELECT COALESCE(MAX(id), 0) - ? FROM search_log)`).run(SEARCH_LOG_KEEP);
340
+ } catch { /* 埋点失败不能影响检索 */ }
341
+ }
342
+
343
+ /** 看板用的聚合:总量、类型分布、状态分布、检索概览、最近活动。 */
344
+ function dashboard(now = Date.now()) {
345
+ const dayStart = new Date(now);
346
+ dayStart.setHours(0, 0, 0, 0);
347
+ const dayStartMs = dayStart.getTime();
348
+
349
+ const byExt = q(`SELECT ext, COUNT(*) AS docs, COALESCE(SUM(bytes), 0) AS bytes
350
+ FROM nodes WHERE kind = 'doc' GROUP BY ext ORDER BY docs DESC, bytes DESC`).all()
351
+ .map((row) => ({ ext: row.ext || '?', docs: Number(row.docs), bytes: Number(row.bytes) }));
352
+
353
+ const byStatus = { ready: 0, pending: 0, parsing: 0, failed: 0 };
354
+ for (const row of q(`SELECT status, COUNT(*) AS n FROM nodes WHERE kind = 'doc' GROUP BY status`).all()) {
355
+ if (row.status in byStatus) byStatus[row.status] = Number(row.n);
356
+ }
357
+
358
+ const byKb = q(`SELECT k.id, k.name, COUNT(n.id) AS docs, COALESCE(SUM(n.bytes), 0) AS bytes
359
+ FROM kbs k LEFT JOIN nodes n ON n.kb_id = k.id AND n.kind = 'doc'
360
+ GROUP BY k.id ORDER BY docs DESC`).all()
361
+ .map((row) => ({ id: row.id, name: row.name, docs: Number(row.docs), bytes: Number(row.bytes) }));
362
+
363
+ const search = {
364
+ total: Number(q(`SELECT COUNT(*) AS n FROM search_log`).get()?.n ?? 0),
365
+ today: Number(q(`SELECT COUNT(*) AS n FROM search_log WHERE ts >= ?`).get(dayStartMs)?.n ?? 0),
366
+ zeroHit: Number(q(`SELECT COUNT(*) AS n FROM search_log WHERE hits = 0`).get()?.n ?? 0),
367
+ hybrid: Number(q(`SELECT COUNT(*) AS n FROM search_log WHERE mode = 'hybrid'`).get()?.n ?? 0),
368
+ avgMs: Math.round(Number(q(`SELECT COALESCE(AVG(took_ms), 0) AS n FROM search_log`).get()?.n ?? 0)),
369
+ lastAt: Number(q(`SELECT COALESCE(MAX(ts), 0) AS n FROM search_log`).get()?.n ?? 0),
370
+ };
371
+ search.zeroRate = search.total ? Math.round((search.zeroHit / search.total) * 100) : 0;
372
+
373
+ const trend = q(`SELECT date(ts / 1000, 'unixepoch', 'localtime') AS day, COUNT(*) AS n
374
+ FROM search_log GROUP BY day ORDER BY day DESC LIMIT 7`).all()
375
+ .map((row) => ({ day: row.day, count: Number(row.n) }))
376
+ .reverse();
377
+
378
+ const topQueries = q(`SELECT query, COUNT(*) AS n FROM search_log
379
+ WHERE length(trim(query)) > 0 GROUP BY lower(trim(query)) ORDER BY n DESC, MAX(ts) DESC LIMIT 8`).all()
380
+ .map((row) => ({ query: row.query, count: Number(row.n) }));
381
+
382
+ const recent = q(`SELECT ts, query, mode, hits, took_ms AS tookMs FROM search_log
383
+ ORDER BY id DESC LIMIT 10`).all()
384
+ .map((row) => ({ ts: Number(row.ts), query: row.query, mode: row.mode, hits: Number(row.hits), tookMs: Number(row.tookMs) }));
385
+
386
+ const recentDocs = q(`SELECT id, name, ext, bytes, status, created_at AS createdAt, indexed_at AS indexedAt
387
+ FROM nodes WHERE kind = 'doc' ORDER BY created_at DESC LIMIT 6`).all()
388
+ .map((row) => ({
389
+ id: row.id,
390
+ name: row.name,
391
+ ext: row.ext ?? '',
392
+ bytes: Number(row.bytes ?? 0),
393
+ status: row.status,
394
+ createdAt: Number(row.createdAt ?? 0),
395
+ indexedAt: Number(row.indexedAt ?? 0),
396
+ }));
397
+
398
+ return {
399
+ now,
400
+ stats: stats(),
401
+ byExt,
402
+ byStatus,
403
+ byKb,
404
+ search,
405
+ trend,
406
+ topQueries,
407
+ recent,
408
+ recentDocs,
409
+ root: root,
410
+ };
411
+ }
412
+
413
+ return {
414
+ db,
415
+ root,
416
+ filesDir,
417
+ tmpDir,
418
+ listKbs,
419
+ getKb,
420
+ createKb,
421
+ renameKb,
422
+ deleteKb,
423
+ getNode,
424
+ listNodes,
425
+ children,
426
+ createNode,
427
+ renameNode,
428
+ moveNode,
429
+ deleteNode,
430
+ setDocStatus,
431
+ saveText,
432
+ getText,
433
+ breadcrumb,
434
+ stats,
435
+ mapNodeByIds,
436
+ removeDocData,
437
+ logSearch,
438
+ dashboard,
439
+ close() {
440
+ try { db.close(); } catch { /* ignore */ }
441
+ },
442
+ /** 供 index.mjs 直接使用的表操作。 */
443
+ raw: { q },
444
+ };
445
+ }