dsh-chat-tide 0.0.0-stage → 0.2.2

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/CONTRIBUTING.md +60 -0
  3. package/LICENSE +21 -0
  4. package/README.en.md +68 -0
  5. package/README.md +67 -2
  6. package/RELEASE.md +55 -0
  7. package/assets/icon.png +0 -0
  8. package/cordis.patch.yml +35 -0
  9. package/lib/ambient.js +44 -0
  10. package/lib/api.js +199 -0
  11. package/lib/attention.js +191 -0
  12. package/lib/budget.js +37 -0
  13. package/lib/circuit.js +42 -0
  14. package/lib/client.js +543 -0
  15. package/lib/config.js +139 -0
  16. package/lib/conflict.js +44 -0
  17. package/lib/const.js +24 -0
  18. package/lib/context.js +232 -0
  19. package/lib/db.js +325 -0
  20. package/lib/dedupe.js +29 -0
  21. package/lib/egress.js +84 -0
  22. package/lib/embed-child.mjs +41 -0
  23. package/lib/embed-worker.mjs +35 -0
  24. package/lib/embed.js +309 -0
  25. package/lib/events.js +82 -0
  26. package/lib/exemplar.js +48 -0
  27. package/lib/extract.js +183 -0
  28. package/lib/facts.js +311 -0
  29. package/lib/fts.js +97 -0
  30. package/lib/images.js +57 -0
  31. package/lib/index.js +1074 -0
  32. package/lib/inject.js +107 -0
  33. package/lib/janitor.js +137 -0
  34. package/lib/llmroute.js +43 -0
  35. package/lib/merge.js +54 -0
  36. package/lib/people.js +55 -0
  37. package/lib/prefilter.js +74 -0
  38. package/lib/qqtext.js +42 -0
  39. package/lib/repair.js +119 -0
  40. package/lib/route-decision.js +303 -0
  41. package/lib/route.js +42 -0
  42. package/lib/schema.js +134 -0
  43. package/lib/scope.js +121 -0
  44. package/lib/scrub.js +23 -0
  45. package/lib/sessions.js +69 -0
  46. package/lib/settings.js +135 -0
  47. package/lib/silence.js +92 -0
  48. package/lib/status-page.js +263 -0
  49. package/lib/topics.js +72 -0
  50. package/lib/vec.js +63 -0
  51. package/package.json +92 -4
  52. package/presets/README.md +38 -0
  53. package/presets/attention/example.json +10 -0
  54. package/presets/config/profiles.json +32 -0
  55. package/presets/private.example.json +18 -0
  56. package/presets/rules/generic.json +79 -0
  57. package/presets/rules.schema.json +32 -0
  58. package/probes/01-sqlite-vec.mjs +70 -0
  59. package/probes/02-fts-bigram.mjs +68 -0
  60. package/probes/03-embedding.mjs +73 -0
  61. package/probes/04-storage.mjs +118 -0
  62. package/probes/05-facts.mjs +113 -0
  63. package/probes/06-events-egress.mjs +72 -0
  64. package/probes/07-extract.mjs +66 -0
  65. package/probes/08-context-janitor.mjs +80 -0
  66. package/probes/09-route-silence.mjs +75 -0
  67. package/probes/10-vector-pipeline.mjs +77 -0
  68. package/probes/11-llm-fallback.mjs +28 -0
  69. package/probes/12-exemplar-budget-merge.mjs +76 -0
  70. package/probes/13-circuit.mjs +45 -0
  71. package/probes/14-embed-worker.mjs +41 -0
  72. package/probes/15-backfill.mjs +57 -0
  73. package/probes/16-scope-preset.mjs +36 -0
  74. package/probes/17-prefilter.mjs +33 -0
  75. package/probes/18-qqtext.mjs +33 -0
  76. package/probes/19-repair.mjs +47 -0
  77. package/probes/20-context-message.mjs +49 -0
  78. package/probes/21-status-page.mjs +75 -0
  79. package/probes/22-retention.mjs +45 -0
  80. package/probes/23-ambient-probability.mjs +60 -0
  81. package/probes/24-inject-delta.mjs +50 -0
  82. package/probes/25-route-decision.mjs +122 -0
  83. package/probes/26-attention.mjs +52 -0
  84. package/probes/27-people-alias.mjs +42 -0
  85. package/probes/28-wake-gates.mjs +88 -0
  86. package/probes/29-scope-modes.mjs +53 -0
  87. package/probes/30-attention-vector.mjs +84 -0
  88. package/probes/31-group-speakers.mjs +58 -0
  89. package/probes/32-image-cache.mjs +54 -0
  90. package/probes/33-topic-growth.mjs +60 -0
  91. package/probes/34-integrity.mjs +80 -0
  92. package/probes/35-privacy.mjs +52 -0
  93. package/probes/36-inject-scopes.mjs +62 -0
  94. package/probes/37-inject-e2e.mjs +92 -0
  95. package/probes/38-review-fixes.mjs +74 -0
  96. package/probes/39-image-only.mjs +31 -0
  97. package/probes/40-scope-follow.mjs +40 -0
  98. package/probes/41-attention-label.mjs +35 -0
  99. package/scripts/attention-sim.mjs +54 -0
  100. package/scripts/check-dead-config.mjs +27 -0
  101. package/scripts/check-docs.mjs +71 -0
  102. package/scripts/check-naming.mjs +48 -0
  103. package/scripts/curate-report.mjs +228 -0
  104. package/scripts/doctor.mjs +110 -0
  105. package/scripts/export-private-rules.mjs +35 -0
  106. package/scripts/make-icon.mjs +114 -0
  107. package/scripts/metrics.mjs +94 -0
  108. package/scripts/pack.mjs +36 -0
  109. package/scripts/recall-check.mjs +56 -0
  110. package/scripts/run-checks.mjs +45 -0
  111. package/scripts/seed.mjs +82 -0
package/lib/facts.js ADDED
@@ -0,0 +1,311 @@
1
+ // 事实层:证据分级、时间仲裁、CAS 写入、插入竞态重试、冲突标记。
2
+ import { indexFact, unindexFact } from './fts.js';
3
+ import { scrubIdentifiers } from './scrub.js';
4
+
5
+ /** 删掉某个事实所有版本的向量(删除事实时用)。 */
6
+ export function pruneVectorsForFact(db, factId) {
7
+ try {
8
+ const rows = db.prepare('SELECT rowid FROM fact_version WHERE fact_id = ?').all(factId);
9
+ for (const r of rows) {
10
+ try {
11
+ db.prepare('DELETE FROM vec_fact WHERE rowid = ?').run(BigInt(r.rowid));
12
+ } catch {
13
+ /* 向量表不存在则跳过 */
14
+ }
15
+ }
16
+ } catch {
17
+ /* ignore */
18
+ }
19
+ }
20
+
21
+ /** 删掉某一版本的向量(覆盖写时用,避免旧值继续参与 KNN)。 */
22
+ export function pruneVectorForVersion(db, factId, version) {
23
+ try {
24
+ const row = db.prepare('SELECT rowid FROM fact_version WHERE fact_id = ? AND version = ?').get(factId, version);
25
+ if (row) db.prepare('DELETE FROM vec_fact WHERE rowid = ?').run(BigInt(row.rowid));
26
+ } catch {
27
+ /* ignore */
28
+ }
29
+ }
30
+ import { tx } from './db.js';
31
+
32
+ export const TIER_RANK = { human_explicit: 3, source_verifiable: 2, agent_inferred: 1 };
33
+ export const MAX_ATTEMPTS = 3;
34
+
35
+ const now = () => Date.now();
36
+
37
+ function readActive(db, { userId, entityId, canonicalKey, subtype }) {
38
+ return db
39
+ .prepare(
40
+ `SELECT * FROM fact
41
+ WHERE user_id = ? AND entity_id = ? AND canonical_key = ? AND subtype = ? AND status = 'active'
42
+ LIMIT 1`,
43
+ )
44
+ .get(userId, entityId, canonicalKey, subtype);
45
+ }
46
+
47
+ /**
48
+ * 冲突裁决(§4.4):先比来源层级,同层级再比 occurred_at。
49
+ * 返回 'replace'(新事实胜)或 'conflict'(裁决不了,进 pending)。
50
+ */
51
+ export function adjudicate(candidate, current) {
52
+ if (!current) return 'replace';
53
+ const ct = TIER_RANK[candidate.provenanceTier] ?? 0;
54
+ const et = TIER_RANK[current.provenance_tier] ?? 0;
55
+ if (ct > et) return 'replace';
56
+ if (ct < et) return 'conflict'; // 低层级不得覆盖高层级,但先按冲突保留
57
+ const cAt = candidate.occurredAt ?? candidate.observedAt ?? now();
58
+ const eAt = current.occurred_at ?? current.observed_at ?? 0;
59
+ if (cAt > eAt) return 'replace';
60
+ if (cAt < eAt) return 'conflict';
61
+ return 'conflict';
62
+ }
63
+
64
+ function insertVersion(db, factId, version, value, status, cand, t = now()) {
65
+ // surface:证据原句,用于中文语义召回(canonical_key 是英文,单独不可检索)
66
+ const surface = cand.surface ?? cand.evidence ?? null;
67
+ db.prepare(
68
+ `INSERT INTO fact_version(fact_id,version,value,status,valid_from,valid_to,occurred_at,observed_at,created_at,
69
+ embedding_model_version,embedding_pending,surface)
70
+ VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`,
71
+ ).run(
72
+ factId,
73
+ version,
74
+ value,
75
+ status,
76
+ cand.validFrom ?? null,
77
+ cand.validTo ?? null,
78
+ cand.occurredAt ?? null,
79
+ cand.observedAt ?? t,
80
+ t,
81
+ cand.embeddingModelVersion ?? null,
82
+ cand.embeddingPending ? 1 : 0,
83
+ surface,
84
+ );
85
+ return db.prepare('SELECT rowid FROM fact_version WHERE fact_id = ? AND version = ?').get(factId, version).rowid;
86
+ }
87
+
88
+ function newFactId() {
89
+ return `fact-${now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
90
+ }
91
+
92
+ /**
93
+ * 写入一个事实候选。
94
+ * @returns {{factId,status,version,outcome,versionRowid,conflictWith?}}
95
+ * outcome: created | replaced | conflicted | skipped
96
+ */
97
+ export function writeFact(db, rawCand) {
98
+ const cand = { ...rawCand, value: scrubIdentifiers(rawCand.value) };
99
+ const t0 = now();
100
+ const base = {
101
+ userId: cand.userId,
102
+ entityId: cand.entityId ?? cand.userId,
103
+ canonicalKey: cand.canonicalKey,
104
+ subtype: cand.subtype ?? 'profile',
105
+ };
106
+
107
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
108
+ const current = readActive(db, base);
109
+ const verdict = adjudicate(cand, current);
110
+
111
+ if (!current) {
112
+ // 插入竞态:唯一约束冲突 → 重读后重试
113
+ try {
114
+ return tx(db, () => {
115
+ const factId = newFactId();
116
+ db.prepare(
117
+ `INSERT INTO fact(fact_id,user_id,entity_id,canonical_key,subtype,status,current_version,
118
+ asserted_by,subject_id,attribution,provenance_tier,valid_from,valid_to,
119
+ occurred_at,observed_at,updated_at,source_record_id,scope)
120
+ VALUES (?,?,?,?,?, 'active', 1, ?,?,?,?,?,?,?,?,?,?,?)`,
121
+ ).run(
122
+ factId,
123
+ base.userId,
124
+ base.entityId,
125
+ base.canonicalKey,
126
+ base.subtype,
127
+ cand.assertedBy ?? null,
128
+ cand.subjectId ?? base.userId,
129
+ cand.attribution ?? null,
130
+ cand.provenanceTier,
131
+ cand.validFrom ?? null,
132
+ cand.validTo ?? null,
133
+ cand.occurredAt ?? null,
134
+ cand.observedAt ?? t0,
135
+ t0,
136
+ cand.sourceRecordId ?? null,
137
+ cand.scope ?? 'unknown',
138
+ );
139
+ const rowid = insertVersion(db, factId, 1, cand.value, 'active', cand, t0);
140
+ indexFact(db, factId, cand.value);
141
+ return { factId, status: 'active', version: 1, outcome: 'created', versionRowid: rowid };
142
+ });
143
+ } catch (err) {
144
+ if (/UNIQUE/.test(String(err?.message))) continue;
145
+ throw err;
146
+ }
147
+ }
148
+
149
+ if (verdict === 'replace') {
150
+ return tx(db, () => {
151
+ const nextVersion = current.current_version + 1;
152
+ const prevValue = db
153
+ .prepare('SELECT value FROM fact_version WHERE fact_id=? AND version=?')
154
+ .get(current.fact_id, current.current_version)?.value;
155
+ db.prepare(`UPDATE fact_version SET status='superseded', valid_to=? WHERE fact_id=? AND version=?`).run(
156
+ cand.validFrom ?? t0,
157
+ current.fact_id,
158
+ current.current_version,
159
+ );
160
+ // 旧事实的有效区间截止到新事实生效点
161
+ pruneVectorForVersion(db, current.fact_id, current.current_version);
162
+ const rowid = insertVersion(db, current.fact_id, nextVersion, cand.value, 'active', cand, t0);
163
+ db.prepare(
164
+ `UPDATE fact SET status='active', current_version=?, updated_at=?, asserted_by=?, subject_id=?,
165
+ attribution=?, provenance_tier=?, valid_from=?, valid_to=?, occurred_at=?,
166
+ observed_at=?, source_record_id=?, scope=?, conflicts_with=NULL
167
+ WHERE fact_id=?`,
168
+ ).run(
169
+ nextVersion,
170
+ t0,
171
+ cand.assertedBy ?? current.asserted_by,
172
+ cand.subjectId ?? current.subject_id,
173
+ cand.attribution ?? current.attribution,
174
+ cand.provenanceTier,
175
+ cand.validFrom ?? null,
176
+ cand.validTo ?? null,
177
+ cand.occurredAt ?? null,
178
+ cand.observedAt ?? t0,
179
+ cand.sourceRecordId ?? null,
180
+ cand.scope ?? current.scope ?? 'unknown',
181
+ current.fact_id,
182
+ );
183
+ indexFact(db, current.fact_id, cand.value);
184
+ return {
185
+ factId: current.fact_id,
186
+ status: 'active',
187
+ version: nextVersion,
188
+ outcome: 'replaced',
189
+ versionRowid: rowid,
190
+ previousValue: prevValue,
191
+ };
192
+ });
193
+ }
194
+
195
+ // conflict:新候选进 pending_confirmation,旧 active 保持,写冲突记录
196
+ return tx(db, () => {
197
+ const factId = newFactId();
198
+ db.prepare(
199
+ `INSERT INTO fact(fact_id,user_id,entity_id,canonical_key,subtype,status,current_version,
200
+ asserted_by,subject_id,attribution,provenance_tier,valid_from,valid_to,
201
+ occurred_at,observed_at,updated_at,source_record_id,conflicts_with)
202
+ VALUES (?,?,?,?,?, 'pending_confirmation', 1, ?,?,?,?,?,?,?,?,?,?,?)`,
203
+ ).run(
204
+ factId,
205
+ base.userId,
206
+ base.entityId,
207
+ base.canonicalKey,
208
+ base.subtype,
209
+ cand.assertedBy ?? null,
210
+ cand.subjectId ?? base.userId,
211
+ cand.attribution ?? null,
212
+ cand.provenanceTier,
213
+ cand.validFrom ?? null,
214
+ cand.validTo ?? null,
215
+ cand.occurredAt ?? null,
216
+ cand.observedAt ?? t0,
217
+ t0,
218
+ cand.sourceRecordId ?? null,
219
+ JSON.stringify([current.fact_id]),
220
+ );
221
+ const rowid = insertVersion(db, factId, 1, cand.value, 'pending_confirmation', cand, t0);
222
+ // 双向标记
223
+ const prev = current.conflicts_with ? JSON.parse(current.conflicts_with) : [];
224
+ db.prepare('UPDATE fact SET conflicts_with = ? WHERE fact_id = ?').run(
225
+ JSON.stringify([...new Set([...prev, factId])]),
226
+ current.fact_id,
227
+ );
228
+ db.prepare(
229
+ `INSERT INTO fact_conflict(conflict_id,fact_a,fact_b,reason,resolved,created_at)
230
+ VALUES (?,?,?,?,0,?)`,
231
+ ).run(`conf-${factId}`, current.fact_id, factId, 'tier/time undecidable', t0);
232
+ return { factId, status: 'pending_confirmation', version: 1, outcome: 'conflicted', versionRowid: rowid, conflictWith: current.fact_id };
233
+ });
234
+ }
235
+ return { outcome: 'skipped', reason: 'insert-race-exhausted' };
236
+ }
237
+
238
+ export function confirmFact(db, factId, value) {
239
+ return tx(db, () => {
240
+ const row = db
241
+ .prepare(
242
+ `SELECT f.*, v.value AS cur_value FROM fact f
243
+ JOIN fact_version v ON v.fact_id=f.fact_id AND v.version=f.current_version
244
+ WHERE f.fact_id=? AND f.status='pending_confirmation'`,
245
+ )
246
+ .get(factId);
247
+ if (!row) return { outcome: 'not-found' };
248
+ const active = readActive(db, {
249
+ userId: row.user_id,
250
+ entityId: row.entity_id,
251
+ canonicalKey: row.canonical_key,
252
+ subtype: row.subtype,
253
+ });
254
+ const nextVersion = row.current_version + 1;
255
+ if (active && active.fact_id !== factId) {
256
+ db.prepare(`UPDATE fact SET status='superseded', updated_at=? WHERE fact_id=?`).run(now(), active.fact_id);
257
+ }
258
+ insertVersion(db, factId, nextVersion, value ?? row.cur_value, 'active', {}, now());
259
+ db.prepare(`UPDATE fact SET status='active', current_version=?, updated_at=? WHERE fact_id=?`).run(
260
+ nextVersion,
261
+ now(),
262
+ factId,
263
+ );
264
+ indexFact(db, factId, value ?? row.cur_value);
265
+ return { outcome: 'confirmed', factId, version: nextVersion };
266
+ });
267
+ }
268
+
269
+ export function deleteFact(db, factId, { reason = 'manual' } = {}) {
270
+ return tx(db, () => {
271
+ const row = db.prepare('SELECT status, provenance_tier AS tier FROM fact WHERE fact_id=?').get(factId);
272
+ if (!row) return { outcome: 'not-found' };
273
+ db.prepare(`UPDATE fact SET status='deleted', updated_at=? WHERE fact_id=?`).run(now(), factId);
274
+ db.prepare(`UPDATE fact_version SET status='deleted' WHERE fact_id=?`).run(factId);
275
+ unindexFact(db, factId);
276
+ pruneVectorsForFact(db, factId);
277
+ // 删除审计:为什么删、什么时候删
278
+ db.prepare('INSERT INTO tombstone(target_type, target_id, created_at, reason) VALUES (?,?,?,?)').run('fact', factId, now(), reason);
279
+ return { outcome: 'deleted', reason };
280
+ });
281
+ }
282
+
283
+ /** 读当前有效值(固定一次 join,禁止 N+1)。 */
284
+ export function getCurrentFact(db, factId) {
285
+ return db
286
+ .prepare(
287
+ `SELECT f.fact_id AS factId, f.user_id AS userId, f.entity_id AS entityId, f.canonical_key AS canonicalKey,
288
+ f.subtype, f.status, f.current_version AS version, f.provenance_tier AS tier,
289
+ f.valid_from AS validFrom, f.valid_to AS validTo, f.occurred_at AS occurredAt,
290
+ f.conflicts_with AS conflictsWith, v.value
291
+ FROM fact f
292
+ JOIN fact_version v ON v.fact_id = f.fact_id AND v.version = f.current_version
293
+ WHERE f.fact_id = ?`,
294
+ )
295
+ .get(factId);
296
+ }
297
+
298
+ /** 按键取候选(含历史版本),供 fact_get 返回候选集。 */
299
+ export function getCandidates(db, { userId, entityId, canonicalKey, subtype }) {
300
+ return db
301
+ .prepare(
302
+ `SELECT f.fact_id AS factId, f.status, f.current_version AS version, f.provenance_tier AS tier,
303
+ f.valid_from AS validFrom, f.valid_to AS validTo, f.occurred_at AS occurredAt,
304
+ v.value, v.status AS versionStatus
305
+ FROM fact f
306
+ JOIN fact_version v ON v.fact_id = f.fact_id AND v.version = f.current_version
307
+ WHERE f.user_id = ? AND f.entity_id = ? AND f.canonical_key = ? AND (? IS NULL OR f.subtype = ?)
308
+ ORDER BY f.updated_at DESC`,
309
+ )
310
+ .all(userId, entityId, canonicalKey, subtype ?? null, subtype ?? null);
311
+ }
package/lib/fts.js ADDED
@@ -0,0 +1,97 @@
1
+ // bigram 分词与 FTS5 读写。
2
+ /**
3
+ * 中文按 2-gram 切分、ASCII 连续串整体保留,用空格连接。
4
+ * 单字中文保留原字,避免 1 字查询完全失配。
5
+ */
6
+ export function bigram(text) {
7
+ const out = [];
8
+ const segs = String(text ?? '').match(/[\u3400-\u9fff]+|[A-Za-z0-9_]+/g) ?? [];
9
+ for (const seg of segs) {
10
+ if (/^[\u3400-\u9fff]+$/.test(seg)) {
11
+ if (seg.length === 1) out.push(seg);
12
+ for (let i = 0; i + 1 < seg.length; i++) out.push(seg.slice(i, i + 2));
13
+ } else {
14
+ out.push(seg.toLowerCase());
15
+ }
16
+ }
17
+ return out.join(' ');
18
+ }
19
+
20
+ /** OR 版本:AND 全部命中太严,问句里的虚词会拖死召回。 */
21
+ export function matchExprOr(query) {
22
+ const grams = bigram(query).split(' ').filter(Boolean);
23
+ if (grams.length === 0) return null;
24
+ return grams.map((g) => `"${g.replace(/"/g, '""')}"`).join(' OR ');
25
+ }
26
+
27
+ /** 把查询词转成 FTS5 MATCH 表达式(每个 bigram 作为短语,AND 连接)。 */
28
+ export function matchExpr(query) {
29
+ const grams = bigram(query).split(' ').filter(Boolean);
30
+ if (grams.length === 0) return null;
31
+ return grams.map((g) => `"${g.replace(/"/g, '""')}"`).join(' AND ');
32
+ }
33
+
34
+ export function indexFact(db, factId, value) {
35
+ db.prepare('DELETE FROM fact_fts WHERE fact_id = ?').run(factId);
36
+ const g = bigram(value);
37
+ if (!g) return;
38
+ db.prepare('INSERT INTO fact_fts(bigram_text, fact_id) VALUES (?, ?)').run(g, factId);
39
+ }
40
+
41
+ export function unindexFact(db, factId) {
42
+ db.prepare('DELETE FROM fact_fts WHERE fact_id = ?').run(factId);
43
+ }
44
+
45
+ export function indexEvent(db, eventId, content) {
46
+ db.prepare('DELETE FROM event_fts WHERE event_id = ?').run(eventId);
47
+ const g = bigram(content);
48
+ if (!g) return;
49
+ db.prepare('INSERT INTO event_fts(bigram_text, event_id) VALUES (?, ?)').run(g, eventId);
50
+ }
51
+
52
+ export function unindexEvent(db, eventId) {
53
+ db.prepare('DELETE FROM event_fts WHERE event_id = ?').run(eventId);
54
+ }
55
+
56
+ /** 事实检索:返回 { factId, score },score 为 bm25(越小越相关,取负使越大越好)。 */
57
+ export function searchFacts(db, query, limit = 10) {
58
+ const and = matchExpr(query);
59
+ if (and === null) return [];
60
+ const run = (expr) =>
61
+ db
62
+ .prepare(
63
+ `SELECT fact_id AS factId, -bm25(fact_fts) AS score
64
+ FROM fact_fts
65
+ WHERE fact_fts MATCH ?
66
+ ORDER BY score DESC
67
+ LIMIT ?`,
68
+ )
69
+ .all(expr, limit);
70
+ const hits = run(and);
71
+ if (hits.length > 0) return hits;
72
+ const or = matchExprOr(query);
73
+ return or ? run(or) : [];
74
+ }
75
+
76
+ /** 事件检索:只查 event_fts(route_decision 等审计事件从不进索引)。 */
77
+ export function searchEvents(db, query, limit = 10) {
78
+ const and = matchExpr(query);
79
+ if (and === null) return [];
80
+ const run = (expr) =>
81
+ db
82
+ .prepare(
83
+ `SELECT e.event_id AS eventId, e.event_type AS eventType, e.content AS content, e.speaker_name AS speakerName, e.scope AS scope,
84
+ e.conversation_key AS conversationKey, e.occurred_at AS occurredAt,
85
+ -bm25(event_fts) AS score
86
+ FROM event_fts
87
+ JOIN event e ON e.event_id = event_fts.event_id
88
+ WHERE event_fts MATCH ? AND e.status = 'active'
89
+ ORDER BY score DESC
90
+ LIMIT ?`,
91
+ )
92
+ .all(expr, limit);
93
+ const hits = run(and);
94
+ if (hits.length > 0) return hits;
95
+ const or = matchExprOr(query);
96
+ return or ? run(or) : [];
97
+ }
package/lib/images.js ADDED
@@ -0,0 +1,57 @@
1
+ // 图片语义缓存:同一张图只让模型看一次
2
+ import { createHash } from 'node:crypto';
3
+ import { readFileSync, existsSync } from 'node:fs';
4
+ import { scrubIdentifiers } from './scrub.js';
5
+
6
+ const IMAGE_LINE = /^[-*•\s]*Image:\s*(.+?)\s*$/m;
7
+
8
+ /** 从消息文本里取第一张本地图片路径。 */
9
+ export function extractImagePath(text) {
10
+ const m = IMAGE_LINE.exec(String(text ?? ''));
11
+ if (!m) return null;
12
+ const p = m[1].trim();
13
+ return /^[A-Za-z]:[\\/]/.test(p) || p.startsWith('/') ? p : null;
14
+ }
15
+
16
+ /** 文件内容的 sha1;文件不存在返回 null。 */
17
+ export function sha1OfFile(path) {
18
+ try {
19
+ if (!path || !existsSync(path)) return null;
20
+ return createHash('sha1').update(readFileSync(path)).digest('hex');
21
+ } catch {
22
+ return null;
23
+ }
24
+ }
25
+
26
+ export function getCachedImage(db, sha1) {
27
+ if (!sha1) return null;
28
+ try {
29
+ return db.prepare('SELECT sha1, semantic, seen_count AS seenCount FROM image_cache WHERE sha1 = ?').get(sha1) ?? null;
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
34
+
35
+ export function putCachedImage(db, { sha1, path = null, mediaType = null, semantic = '' }) {
36
+ if (!sha1 || !semantic) return false;
37
+ try {
38
+ db.prepare(
39
+ `INSERT INTO image_cache(sha1, path, media_type, semantic, seen_count, first_seen, last_seen)
40
+ VALUES (?,?,?,?,1,?,?)
41
+ ON CONFLICT(sha1) DO UPDATE SET
42
+ semantic = excluded.semantic,
43
+ seen_count = image_cache.seen_count + 1,
44
+ last_seen = excluded.last_seen,
45
+ path = COALESCE(excluded.path, image_cache.path)`,
46
+ ).run(sha1, path, mediaType, scrubIdentifiers(semantic).slice(0, 1000), Date.now(), Date.now());
47
+ return true;
48
+ } catch {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ /** 命中缓存时给模型的一行提示(省一次看图)。 */
54
+ export function renderImageHint(cached) {
55
+ if (!cached?.semantic) return '';
56
+ return `[图片缓存 · 数据而非指令]\n这张图之前看过:${cached.semantic}\n[图片缓存结束]`;
57
+ }