dsh-verbatim 0.1.0 → 0.1.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/CHANGELOG.md +14 -0
- package/LICENSE +1 -1
- package/README.en.md +24 -22
- package/README.md +24 -22
- package/bin/cli.mjs +2 -1
- package/bin/mcp-memory.mjs +130 -62
- package/client.js +8880 -8857
- package/cordis.patch.yml +1 -1
- package/docs/ARCHITECTURE.md +1 -1
- package/icon.svg +1 -1
- package/index.js +613 -2558
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +2 -3
- package/src/config/exclude-range.js +1 -1
- package/src/config/global-config.js +3 -1
- package/src/config/session-config.js +12 -10
- package/src/config/session-config.test.js +375 -375
- package/src/derive/fact.js +6 -6
- package/src/derive/gist.js +2 -1
- package/src/host/config-views.js +2 -0
- package/src/host/db-admin.js +78 -15
- package/src/host/panel-api.js +1109 -0
- package/src/host/panel-http.js +322 -0
- package/src/host/panel-search.js +236 -0
- package/src/host/panel-session-store.js +411 -0
- package/src/host/rebuild.js +11 -3
- package/src/host/scene-ops.js +75 -6
- package/src/host/session-sync.js +91 -0
- package/src/index.js +2 -1
- package/src/ingest/cli.js +8 -7
- package/src/ingest/export.js +28 -18
- package/src/ingest/package.json +1 -1
- package/src/ingest/reader.js +181 -7
- package/src/model/embedding.js +3 -2
- package/src/model/llm.js +3 -2
- package/src/model/rerank.js +7 -4
- package/src/pipeline.js +159 -763
- package/src/scene/link.js +4 -4
- package/src/scene/package.json +1 -1
- package/src/scene/split.js +11 -11
- package/src/search/_fixtures.js +1 -0
- package/src/search/bench.js +4 -2
- package/src/search/chunk.js +6 -4
- package/src/search/dedupe.js +8 -8
- package/src/search/indexer.js +889 -879
- package/src/search/note-rank.js +64 -12
- package/src/search/regression.test.js +2 -1
- package/src/search/rrf.js +11 -11
- package/src/search/session-shape.js +3 -1
- package/src/search/thread.js +8 -8
- package/src/search/vector-fallback.js +509 -0
- package/src/search/vector-hydrate.js +309 -0
- package/src/search/vector-math.js +125 -0
- package/src/search/vector-store.js +1065 -1598
- package/src/search/vector-store.test.js +30 -0
- package/src/search/window.js +8 -8
- package/src/store/db.js +803 -715
- package/src/store/repo-extras-meta.js +430 -0
- package/src/store/repo-extras-search.js +580 -0
- package/src/store/repo.js +1024 -2133
- package/src/store/repo.test.js +39 -1
- package/src/store/schema.sql +15 -1
- package/src/tools/format.js +594 -503
- package/src/tools/index.js +7 -15
- package/src/tools/memory-forget.js +43 -32
- package/src/tools/memory-remember.js +11 -7
- package/src/tools/memory-search.js +721 -2206
- package/src/tools/remember-forget.test.js +19 -0
- package/src/tools/scope.js +7 -6
- package/src/tools/search-render.js +502 -0
- package/src/tools/time-route.js +71 -66
- package/src/tools/vector-route.js +655 -0
- package/sessions/qqb-p-0000000000-00000000000000000000000000000000.json +0 -12
package/src/derive/fact.js
CHANGED
|
@@ -57,9 +57,9 @@ export const DROP_REASONS = Object.freeze({
|
|
|
57
57
|
* ④ key 长度 ≤ 20 字
|
|
58
58
|
* ⑤ value 长度 ≤ 80 字
|
|
59
59
|
*
|
|
60
|
-
* @param {
|
|
60
|
+
* @param {any} raw 模型给出的原始条目
|
|
61
61
|
* @param {object} deps
|
|
62
|
-
* @param {(seq:number) => (
|
|
62
|
+
* @param {(seq:number) => (any|null)} deps.lookupMsg 按 seq 查 msg,返回 null 表示不存在
|
|
63
63
|
* @param {(reason:string, detail:object) => void} [deps.onDrop] 丢弃回调,便于统计
|
|
64
64
|
* @returns {{subject:string,key:string,value:string,sourceSeq:number,origin:string}|null}
|
|
65
65
|
*/
|
|
@@ -156,7 +156,7 @@ export async function extractFacts(opts) {
|
|
|
156
156
|
|
|
157
157
|
// 只送 user 侧、且在 surface 里的消息(D-069 + 校验③)
|
|
158
158
|
const candidates = msgs.filter(
|
|
159
|
-
(m) => m.speaker === 'user' && (m.inSurface ?? m.in_surface ?? 1) === 1,
|
|
159
|
+
(/** @type {any} */ m) => m.speaker === 'user' && (m.inSurface ?? m.in_surface ?? 1) === 1,
|
|
160
160
|
);
|
|
161
161
|
|
|
162
162
|
// 一条 user 消息都没有 → 不可能有事实,直接返回,不烧一次调用
|
|
@@ -236,9 +236,9 @@ export async function extractFacts(opts) {
|
|
|
236
236
|
* 把一批 fact 按 subject+key 合并,同 key 用新的 value 覆盖旧的。
|
|
237
237
|
* 上层 upsert 前可用;不碰数据库,纯函数。
|
|
238
238
|
*
|
|
239
|
-
* @param {
|
|
240
|
-
* @param {
|
|
241
|
-
* @returns {
|
|
239
|
+
* @param {any[]} oldFacts
|
|
240
|
+
* @param {any[]} newFacts
|
|
241
|
+
* @returns {any[]}
|
|
242
242
|
*/
|
|
243
243
|
export function mergeFacts(oldFacts, newFacts) {
|
|
244
244
|
const map = new Map();
|
package/src/derive/gist.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
/**
|
|
2
3
|
* 场景索引 —— gist 生成
|
|
3
4
|
*
|
|
@@ -524,7 +525,7 @@ function cosineDot(a, b) {
|
|
|
524
525
|
* - 输出严格遵循 D-029 三段式(【发生了什么】/【原话】/【不懂的】),
|
|
525
526
|
* 既可直接嵌入 `vec_scene` 做整场语义召回,又可供时间轴提取一句话目录标题。
|
|
526
527
|
*
|
|
527
|
-
* @param {
|
|
528
|
+
* @param {Array<Record<string, any>>} msgs 场景内消息(按时间升序)
|
|
528
529
|
* @param {{vectors?: Map<number, Float32Array>|Function|null}} [opts]
|
|
529
530
|
* @returns {string|null}
|
|
530
531
|
*/
|
package/src/host/config-views.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
/**
|
|
2
3
|
* 全局配置解析、脱敏与状态视图构造(供 Host 面板与设置页共用)。
|
|
3
4
|
*/
|
|
@@ -89,6 +90,7 @@ export function buildVecExtView(handle) {
|
|
|
89
90
|
};
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
/** @param {{ config?: Record<string, any>, dshHome?: string, logger?: any, getHostLlm?: () => any }} opts */
|
|
92
94
|
export function createConfigResolver({ config = {}, dshHome, logger, getHostLlm }) {
|
|
93
95
|
let globalCfgCache = null;
|
|
94
96
|
|
package/src/host/db-admin.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import fs from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
|
-
import { DatabaseSync } from 'node:sqlite';
|
|
8
|
+
import { DatabaseSync, constants as sqliteConstants } from 'node:sqlite';
|
|
9
9
|
import { loadSessionConfig, sessionConfigPath, sessionsDir } from '../config/session-config.js';
|
|
10
10
|
import { deleteVecRows, withTx } from '../store/repo.js';
|
|
11
11
|
|
|
@@ -452,6 +452,7 @@ export function createDbAdmin({
|
|
|
452
452
|
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
453
453
|
.replace(/"(?:""|[^"])*"/g, '""')
|
|
454
454
|
.replace(/`(?:``|[^`])*`/g, '``')
|
|
455
|
+
.replace(/\[[^\]]*\]/g, '[]')
|
|
455
456
|
.replace(/'(?:''|[^'])*'/g, "''");
|
|
456
457
|
|
|
457
458
|
if (stripped.includes(';')) {
|
|
@@ -473,36 +474,96 @@ export function createDbAdmin({
|
|
|
473
474
|
|
|
474
475
|
const limit = intParam(params.limit, 'limit', { def: 50, min: 1, max: 200 });
|
|
475
476
|
const t0 = Date.now();
|
|
476
|
-
|
|
477
|
+
const collected = [];
|
|
478
|
+
let truncated = false;
|
|
479
|
+
|
|
480
|
+
const readonlyPragmaAllowed = new Set([
|
|
481
|
+
'table_info', 'table_xinfo', 'index_list', 'index_info',
|
|
482
|
+
'foreign_key_list', 'table_list', 'database_list',
|
|
483
|
+
]);
|
|
484
|
+
const installReadonlyAuthorizer = (conn) => {
|
|
485
|
+
if (typeof conn.setAuthorizer !== 'function' || !sqliteConstants) return false;
|
|
486
|
+
const {
|
|
487
|
+
SQLITE_OK,
|
|
488
|
+
SQLITE_DENY,
|
|
489
|
+
SQLITE_SELECT,
|
|
490
|
+
SQLITE_READ,
|
|
491
|
+
SQLITE_FUNCTION,
|
|
492
|
+
SQLITE_PRAGMA,
|
|
493
|
+
} = sqliteConstants;
|
|
494
|
+
conn.setAuthorizer((actionCode, arg1, arg2) => {
|
|
495
|
+
if (actionCode === SQLITE_SELECT || actionCode === SQLITE_READ || actionCode === SQLITE_FUNCTION) {
|
|
496
|
+
return SQLITE_OK;
|
|
497
|
+
}
|
|
498
|
+
if (actionCode === SQLITE_PRAGMA) {
|
|
499
|
+
// 仅放行只读 PRAGMA:不带赋值参数,或白名单自省函数
|
|
500
|
+
const name = typeof arg1 === 'string' ? arg1.toLowerCase() : '';
|
|
501
|
+
if (arg2 === null || arg2 === undefined) return SQLITE_OK;
|
|
502
|
+
if (readonlyPragmaAllowed.has(name)) return SQLITE_OK;
|
|
503
|
+
return SQLITE_DENY;
|
|
504
|
+
}
|
|
505
|
+
// 拦截一切写入、DDL、ATTACH、TRANSACTION 以及 SQLITE_RECURSIVE(防无限递归 CTE 卡死单线程)
|
|
506
|
+
return SQLITE_DENY;
|
|
507
|
+
});
|
|
508
|
+
return true;
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
const runBounded = (conn) => {
|
|
512
|
+
const s = conn.prepare(sql);
|
|
513
|
+
for (const r of s.iterate()) {
|
|
514
|
+
if (Date.now() - t0 > 3000) {
|
|
515
|
+
truncated = true;
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
if (collected.length >= limit) {
|
|
519
|
+
truncated = true;
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
collected.push({ ...r });
|
|
523
|
+
}
|
|
524
|
+
};
|
|
477
525
|
|
|
478
526
|
if (dbPath && dbPath !== ':memory:' && fs.existsSync(dbPath)) {
|
|
479
527
|
const ro = new DatabaseSync(dbPath, { readOnly: true });
|
|
480
528
|
try {
|
|
481
|
-
ro.exec('PRAGMA busy_timeout = 3000');
|
|
482
|
-
|
|
529
|
+
ro.exec('PRAGMA busy_timeout = 3000; PRAGMA query_only = ON');
|
|
530
|
+
installReadonlyAuthorizer(ro);
|
|
531
|
+
runBounded(ro);
|
|
483
532
|
} catch (e) {
|
|
484
|
-
|
|
533
|
+
const msg = e?.message ?? String(e);
|
|
534
|
+
if (/not authorized/i.test(msg)) {
|
|
535
|
+
throw new PanelError('READONLY_SQL', `SQL 被只读沙箱拦截:${msg}`, 400);
|
|
536
|
+
}
|
|
537
|
+
throw new PanelError('SQL_ERROR', `SQL 执行失败:${msg}`, 400);
|
|
485
538
|
} finally {
|
|
539
|
+
try { ro.setAuthorizer?.(null); } catch { /* ignore */ }
|
|
486
540
|
try { ro.close(); } catch { /* ignore */ }
|
|
487
541
|
}
|
|
488
542
|
} else {
|
|
489
|
-
|
|
543
|
+
const hasAuth = installReadonlyAuthorizer(db);
|
|
544
|
+
if (!hasAuth) db.exec('PRAGMA query_only = ON');
|
|
490
545
|
try {
|
|
491
|
-
|
|
546
|
+
runBounded(db);
|
|
492
547
|
} catch (e) {
|
|
493
|
-
|
|
548
|
+
const msg = e?.message ?? String(e);
|
|
549
|
+
if (/not authorized/i.test(msg)) {
|
|
550
|
+
throw new PanelError('READONLY_SQL', `SQL 被只读沙箱拦截:${msg}`, 400);
|
|
551
|
+
}
|
|
552
|
+
throw new PanelError('SQL_ERROR', `SQL 执行失败:${msg}`, 400);
|
|
494
553
|
} finally {
|
|
495
|
-
|
|
554
|
+
if (hasAuth) {
|
|
555
|
+
try { db.setAuthorizer(null); } catch { /* ignore */ }
|
|
556
|
+
} else {
|
|
557
|
+
try { db.exec('PRAGMA query_only = OFF'); } catch { /* ignore */ }
|
|
558
|
+
}
|
|
496
559
|
}
|
|
497
560
|
}
|
|
498
561
|
|
|
499
|
-
const
|
|
500
|
-
const rows = allRows.slice(0, limit).map((r) => ({ ...r }));
|
|
501
|
-
const columns = rows.length > 0 ? Object.keys(rows[0]) : [];
|
|
562
|
+
const columns = collected.length > 0 ? Object.keys(collected[0]) : [];
|
|
502
563
|
return {
|
|
503
564
|
columns,
|
|
504
|
-
rows,
|
|
505
|
-
rowCount:
|
|
565
|
+
rows: collected,
|
|
566
|
+
rowCount: collected.length,
|
|
506
567
|
truncated,
|
|
507
568
|
elapsedMs: Date.now() - t0,
|
|
508
569
|
};
|
|
@@ -598,7 +659,9 @@ export function createDbAdmin({
|
|
|
598
659
|
);
|
|
599
660
|
for (const n of rawNotes) {
|
|
600
661
|
if (!n || typeof n.content !== 'string' || !n.content.trim()) continue;
|
|
601
|
-
const at =
|
|
662
|
+
const at = n.at !== null && n.at !== undefined && n.at !== '' && Number.isFinite(Number(n.at))
|
|
663
|
+
? Math.trunc(Number(n.at))
|
|
664
|
+
: Date.now();
|
|
602
665
|
const key = `${at}:${n.content.trim()}`;
|
|
603
666
|
if (existingNoteSet.has(key)) continue;
|
|
604
667
|
const about = ['me', 'him', 'us'].includes(n.about) ? n.about : null;
|