@modusensus/dsh-mneme 0.6.0 → 0.6.5
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/README.md +25 -2
- package/lib/api.js +178 -1
- package/lib/client.js +363 -9
- package/lib/config.js +27 -0
- package/lib/dream/tag-extractor.js +149 -0
- package/lib/dream.js +20 -0
- package/lib/mirror.js +9 -0
- package/lib/parser/tag.js +59 -0
- package/lib/parser/wiki-link.js +38 -0
- package/lib/search/tag-boost.js +61 -0
- package/lib/service.js +211 -4
- package/lib/store.js +242 -2
- package/package.json +1 -1
- package/src/api.js +178 -1
- package/src/config.js +27 -0
- package/src/dream/tag-extractor.js +149 -0
- package/src/dream.js +20 -0
- package/src/mirror.js +9 -0
- package/src/parser/tag.js +59 -0
- package/src/parser/wiki-link.js +38 -0
- package/src/search/tag-boost.js +61 -0
- package/src/service.js +211 -4
- package/src/store.js +242 -2
- package/test/api.test.js +80 -0
- package/test/boundary-v0625.test.js +82 -0
- package/test/client.test.js +134 -0
- package/test/directory.test.js +134 -0
- package/test/tag-boost.test.js +125 -0
- package/test/tag.test.js +294 -0
- package/test/wiki-link.test.js +332 -0
package/src/dream.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { validateDecisions, applyDecisions } from "./dream/decisions.js";
|
|
2
2
|
import { clusterMemories, findPotentialConflicts } from "./dream/clustering.js";
|
|
3
|
+
import { runAutoTag } from "./dream/tag-extractor.js";
|
|
3
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
4
5
|
export { validateDecisions, applyDecisions, normalizeDecisions };
|
|
5
6
|
|
|
@@ -815,6 +816,25 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
815
816
|
// that only froze conflicts is not a noop.
|
|
816
817
|
const noChange = frozenCount === 0 && applied === 0 && committed.every((c) => c.action === "keep");
|
|
817
818
|
|
|
819
|
+
// v0.6.2 auto-tag: a light LLM pass over the retained (post-consolidation)
|
|
820
|
+
// memories. Opt-in via config.autoTagEnabled, bounded by autoTagMaxPerRun,
|
|
821
|
+
// and always fail-safe — a tag failure must never change the consolidation
|
|
822
|
+
// outcome reported below (it is logged and counted, nothing more).
|
|
823
|
+
let autoTagged = 0;
|
|
824
|
+
if (config.autoTagEnabled === true) {
|
|
825
|
+
try {
|
|
826
|
+
const tagResult = await runAutoTag({ ctx, service, config, route });
|
|
827
|
+
autoTagged = tagResult?.tagged ?? 0;
|
|
828
|
+
if (tagResult?.ok === false && tagResult?.skippedBy && tagResult.skippedBy !== "empty") {
|
|
829
|
+
logger?.warn?.(`dsh-mneme dream: auto-tag skipped (${tagResult.skippedBy})`);
|
|
830
|
+
} else if (autoTagged > 0) {
|
|
831
|
+
logger?.info?.(`[dsh-mneme] auto-tag: ${autoTagged} memory(ies) tagged`);
|
|
832
|
+
}
|
|
833
|
+
} catch (error) {
|
|
834
|
+
logger?.warn?.(`dsh-mneme dream: auto-tag failed: ${String(error)}`);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
818
838
|
// Keep the vector index consistent with the post-dream store state.
|
|
819
839
|
if (semantic?.embedder && semantic?.vectorIndex) {
|
|
820
840
|
try {
|
package/src/mirror.js
CHANGED
|
@@ -31,6 +31,15 @@ function renderMemory(m) {
|
|
|
31
31
|
.digest("hex");
|
|
32
32
|
const lines = [];
|
|
33
33
|
lines.push(`## ${esc(m.title)}`);
|
|
34
|
+
// v0.6.2 tag line: entity_attrs-backed tags (attached by the service as
|
|
35
|
+
// `entityTags`) rendered as `#tag` space-separated under the title. No tags
|
|
36
|
+
// → no line. Legacy `- **标签**:` metadata below keeps the memories.tags
|
|
37
|
+
// column (still written by save/update) readable.
|
|
38
|
+
const entityTags = Array.isArray(m.entityTags) ? m.entityTags : [];
|
|
39
|
+
if (entityTags.length) {
|
|
40
|
+
lines.push("");
|
|
41
|
+
lines.push(entityTags.map((t) => `#${esc(t)}`).join(" "));
|
|
42
|
+
}
|
|
34
43
|
lines.push("");
|
|
35
44
|
lines.push(`- **ID**: \`${m.id}\``);
|
|
36
45
|
lines.push(`- **类型**: ${m.type}`);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tag parser (v0.6.2). Given a memory's content, extract explicit hashtags of
|
|
3
|
+
* the form `#标签`. The allowed character set is `[a-zA-Z0-9_一-龥-]+` (ASCII
|
|
4
|
+
* letters/digits/underscore, CJK characters, and hyphen), so `#linux`,
|
|
5
|
+
* `#deepseek` and `#考研` are all valid tags.
|
|
6
|
+
*
|
|
7
|
+
* Rules:
|
|
8
|
+
* - multiple tags on one line are all extracted, in source order;
|
|
9
|
+
* - duplicates collapse to the first occurrence;
|
|
10
|
+
* - tags longer than MAX_TAG_LENGTH (20) characters are dropped as illegal;
|
|
11
|
+
* - edge hyphens (leading/trailing, e.g. a sentence-terminating `#tag-`) are
|
|
12
|
+
* stripped; a tag that collapses to empty is dropped;
|
|
13
|
+
* - markdown `## heading` never matches (the second `#`/space is not in the
|
|
14
|
+
* allowed set).
|
|
15
|
+
*
|
|
16
|
+
* Pure module: no store, no side effects — persistence (store.setMemoryTags)
|
|
17
|
+
* is a separate step. sanitizeTags shares the same validation so LLM-extracted
|
|
18
|
+
* tag arrays (autoDream tag-extractor) are filtered identically.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const MAX_TAG_LENGTH = 20;
|
|
22
|
+
|
|
23
|
+
// `一` (U+4E00) .. `龥` (U+9FA5) is the CJK Unified Ideographs block (the
|
|
24
|
+
// rule's `一-龥`). Matching requires a tag character right after `#` so a bare
|
|
25
|
+
// `#` and markdown `## heading` are naturally excluded (#/space not in set).
|
|
26
|
+
const TAG_RE = /#[a-zA-Z0-9_一-龥-]+/g;
|
|
27
|
+
const EDGE_HYPHEN = /^-+|-+$/g;
|
|
28
|
+
const VALID_TAG = /^[a-zA-Z0-9_一-龥-]+$/;
|
|
29
|
+
|
|
30
|
+
/** Validate + dedupe an array of tag strings. Non-strings, blanks, over-long
|
|
31
|
+
* and illegal-character tags are dropped (fail-safe). A leading `#` is
|
|
32
|
+
* tolerated so raw LLM output can be passed through directly. */
|
|
33
|
+
export function sanitizeTags(tags) {
|
|
34
|
+
if (!Array.isArray(tags)) return [];
|
|
35
|
+
const seen = new Set();
|
|
36
|
+
const out = [];
|
|
37
|
+
for (const t of tags) {
|
|
38
|
+
if (typeof t !== "string") continue;
|
|
39
|
+
let tag = t.trim();
|
|
40
|
+
if (tag.startsWith("#")) tag = tag.slice(1);
|
|
41
|
+
tag = tag.replace(EDGE_HYPHEN, "");
|
|
42
|
+
if (!tag || tag.length > MAX_TAG_LENGTH) continue;
|
|
43
|
+
if (!VALID_TAG.test(tag)) continue;
|
|
44
|
+
if (seen.has(tag)) continue; // 去重(保留首次出现顺序)
|
|
45
|
+
seen.add(tag);
|
|
46
|
+
out.push(tag);
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @returns {string[]} validated tags in source order, deduplicated. */
|
|
52
|
+
export function parseTags(content) {
|
|
53
|
+
if (typeof content !== "string" || content.length === 0) return [];
|
|
54
|
+
const raw = [];
|
|
55
|
+
TAG_RE.lastIndex = 0;
|
|
56
|
+
let m;
|
|
57
|
+
while ((m = TAG_RE.exec(content)) !== null) raw.push(m[0]);
|
|
58
|
+
return sanitizeTags(raw);
|
|
59
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wiki-Link parser (v0.6.1). Given a memory's content, extract explicit
|
|
3
|
+
* cross-memory links of the form [[target]] or [[显示|target]]:
|
|
4
|
+
* [[target]] → { display: "target", target: "target" }
|
|
5
|
+
* [[显示|target]] → { display: "显示", target: "target" }
|
|
6
|
+
*
|
|
7
|
+
* Unclosed / empty-target / multi-pipe / bracket-nested markers are treated as
|
|
8
|
+
* illegal and ignored. Pure module: no store, no side effects — resolution is a
|
|
9
|
+
* separate step (resolveWikiLink) that needs a store handle.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** @returns {{display: string, target: string}[]} in source order. */
|
|
13
|
+
export function parseWikiLinks(content) {
|
|
14
|
+
if (typeof content !== "string" || content.length === 0) return [];
|
|
15
|
+
const links = [];
|
|
16
|
+
// [^\[\]]* keeps a single match from crossing `]]`; an unclosed `[[` never
|
|
17
|
+
// matches, and `[[a [[b]] c]]` only yields the inner `[[b]]`.
|
|
18
|
+
const re = /\[\[([^\[\]]*)\]\]/g;
|
|
19
|
+
let m;
|
|
20
|
+
while ((m = re.exec(content)) !== null) {
|
|
21
|
+
const inner = m[1];
|
|
22
|
+
const parts = inner.split("|");
|
|
23
|
+
if (parts.length > 2) continue; // 多管道 → 非法,忽略
|
|
24
|
+
const rawTarget = (parts.length === 2 ? parts[1] : parts[0]).trim();
|
|
25
|
+
if (!rawTarget) continue; // 空目标 → 非法,忽略
|
|
26
|
+
const rawDisplay = parts[0].trim();
|
|
27
|
+
links.push({ display: rawDisplay || rawTarget, target: rawTarget });
|
|
28
|
+
}
|
|
29
|
+
return links;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Resolve a wiki-link target title to a memory row via a case-insensitive
|
|
33
|
+
* exact title match (store.findByTitle). Returns undefined when absent or the
|
|
34
|
+
* store exposes no such lookup. */
|
|
35
|
+
export function resolveWikiLink(store, title) {
|
|
36
|
+
if (!store || typeof title !== "string" || !title.trim()) return undefined;
|
|
37
|
+
return store.findByTitle?.(title.trim());
|
|
38
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// v0.6.4 Tag-weighted re-rank helpers.
|
|
2
|
+
// Query tags come from `#hashtags` in the query plus any known tags the query
|
|
3
|
+
// mentions; candidates whose tags overlap the query tags or the session's
|
|
4
|
+
// hot-memory tags get their score boosted before the final top-K cut.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extract tags from a query: explicit `#hashtags` (CJK allowed) plus known
|
|
8
|
+
* tags whose lowercase form appears in the query. Deduped, order-preserving.
|
|
9
|
+
*/
|
|
10
|
+
export function extractQueryTags(query, knownTags = []) {
|
|
11
|
+
if (typeof query !== "string" || !query) return [];
|
|
12
|
+
const set = new Set();
|
|
13
|
+
const re = /#([a-zA-Z0-9_一-龥-]+)/g;
|
|
14
|
+
let m;
|
|
15
|
+
while ((m = re.exec(query)) !== null) set.add(m[1]);
|
|
16
|
+
const q = query.toLowerCase();
|
|
17
|
+
for (const t of knownTags) {
|
|
18
|
+
if (typeof t === "string" && q.includes(t.toLowerCase())) set.add(t);
|
|
19
|
+
}
|
|
20
|
+
return Array.from(set);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function hasOverlap(a, b) {
|
|
24
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
25
|
+
const setB = new Set(b);
|
|
26
|
+
return a.some((x) => setB.has(x));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Boost candidate scores by tag overlap with the query and/or session tags.
|
|
31
|
+
* Each candidate is expected to carry a `tags` array (enriched by the caller).
|
|
32
|
+
* Returns a new array sorted by boosted score descending; candidates that were
|
|
33
|
+
* boosted are marked `tagBoost: true`. Unchanged candidates keep their
|
|
34
|
+
* relative order (stable).
|
|
35
|
+
*/
|
|
36
|
+
export function applyTagBoost(
|
|
37
|
+
candidates,
|
|
38
|
+
{ queryTags = [], sessionTags = [], factor = 1.15, sessionFactor = 1.08 }
|
|
39
|
+
) {
|
|
40
|
+
const boosted = candidates.map((c) => {
|
|
41
|
+
const tags = c.tags ?? [];
|
|
42
|
+
let score = typeof c.score === "number" ? c.score : 0;
|
|
43
|
+
let didBoost = false;
|
|
44
|
+
|
|
45
|
+
if (queryTags.length && hasOverlap(tags, queryTags)) {
|
|
46
|
+
score = Math.min(1, score * factor);
|
|
47
|
+
didBoost = true;
|
|
48
|
+
}
|
|
49
|
+
if (sessionTags.length && hasOverlap(tags, sessionTags)) {
|
|
50
|
+
score = Math.min(1, score * sessionFactor);
|
|
51
|
+
didBoost = true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const out = { ...c, score };
|
|
55
|
+
if (didBoost) out.tagBoost = true;
|
|
56
|
+
return out;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
boosted.sort((a, b) => (b.score ?? 0) - (a.score ?? 0));
|
|
60
|
+
return boosted;
|
|
61
|
+
}
|
package/src/service.js
CHANGED
|
@@ -3,6 +3,8 @@ import { TYPE_FILE } from "./mirror.js";
|
|
|
3
3
|
import { evaluateMemoryQuality } from "./quality-filter.js";
|
|
4
4
|
import { createBM25Index } from "./search/bm25.js";
|
|
5
5
|
import { adaptiveThreshold } from "./search/adaptive.js";
|
|
6
|
+
import { parseWikiLinks } from "./parser/wiki-link.js";
|
|
7
|
+
import { extractQueryTags, applyTagBoost } from "./search/tag-boost.js";
|
|
6
8
|
|
|
7
9
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
8
10
|
|
|
@@ -228,6 +230,36 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
228
230
|
}
|
|
229
231
|
}
|
|
230
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Fire-and-forget wiki-link resolution for a freshly saved/updated memory
|
|
235
|
+
* (v0.6.1). Opt-in via config.wikiLinkEnabled. Parses [[target]] /
|
|
236
|
+
* [[显示|target]] markers out of the memory content and writes links_to
|
|
237
|
+
* relations (idempotent via the unique relation index). Runs through
|
|
238
|
+
* service.enqueue so it serializes with autoDream/sleep and never overlaps
|
|
239
|
+
* another background pass. Fully fail-safe: parse/store errors are swallowed
|
|
240
|
+
* and logged, never a write failure.
|
|
241
|
+
*/
|
|
242
|
+
function scheduleWikiLinkResolve(memory) {
|
|
243
|
+
if (txDepth > 0) return; // deferred to the transaction's commit
|
|
244
|
+
if (!config.wikiLinkEnabled || !memory?.id) return;
|
|
245
|
+
try {
|
|
246
|
+
const links = parseWikiLinks(memory?.content ?? "");
|
|
247
|
+
if (!links.length) return;
|
|
248
|
+
const targets = [...new Set(links.map((l) => l.target).filter(Boolean))];
|
|
249
|
+
enqueue(() => {
|
|
250
|
+
try {
|
|
251
|
+
store.saveWikiLinks({ memoryId: memory.id, title: memory.title, targets });
|
|
252
|
+
} catch (err) {
|
|
253
|
+
logger?.warn?.("wiki link resolve failed:", err);
|
|
254
|
+
}
|
|
255
|
+
}).catch((err) => {
|
|
256
|
+
logger?.warn?.("wiki link resolve failed:", err);
|
|
257
|
+
});
|
|
258
|
+
} catch (err) {
|
|
259
|
+
logger?.warn?.("wiki link resolve failed:", err);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
231
263
|
/**
|
|
232
264
|
* Cross-encoder rerank over a candidate list (best effort). Reranker
|
|
233
265
|
* failures degrade to the original candidate order — reranking is an
|
|
@@ -296,6 +328,55 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
296
328
|
return hits;
|
|
297
329
|
}
|
|
298
330
|
|
|
331
|
+
/**
|
|
332
|
+
* Search for memories carrying a given tag set (v0.6.2). Multiple tag:
|
|
333
|
+
* tokens in one query use AND semantics — a memory must carry every tag.
|
|
334
|
+
* Combinable with the leftover query: an entity:/attr: prefix is intersected
|
|
335
|
+
* with the tag-matched set, and plain keyword text ranks it (only rows whose
|
|
336
|
+
* keyword score > 0 survive). Tags are first-class recall, not entity-gated:
|
|
337
|
+
* the tag match itself does not depend on config.entitySearchEnabled.
|
|
338
|
+
* @param {string[]} tagTokens
|
|
339
|
+
* @param {string} q — leftover query after tag: tokens were stripped
|
|
340
|
+
* @param {object} [options]
|
|
341
|
+
* @param {number} [options.topK=20]
|
|
342
|
+
* @returns {any[]}
|
|
343
|
+
*/
|
|
344
|
+
function searchByTags(tagTokens, q, options) {
|
|
345
|
+
const { topK = 20 } = options;
|
|
346
|
+
const tags = (Array.isArray(tagTokens) ? tagTokens : [])
|
|
347
|
+
.map((t) => String(t).trim()).filter(Boolean);
|
|
348
|
+
if (!tags.length) return [];
|
|
349
|
+
let hits = store.findMemoriesByTags(tags);
|
|
350
|
+
if (!hits.length) return [];
|
|
351
|
+
// Intersect with entity:/attr: prefixes present in the leftover query.
|
|
352
|
+
if (config?.entitySearchEnabled) {
|
|
353
|
+
if (q.startsWith("entity:")) {
|
|
354
|
+
const ids = new Set(searchByEntity(q.slice(7).trim(), { topK: 10000 }).map((m) => m.id));
|
|
355
|
+
hits = hits.filter((m) => ids.has(m.id));
|
|
356
|
+
}
|
|
357
|
+
if (q.startsWith("attr:")) {
|
|
358
|
+
const [key, value] = q.slice(5).split("=");
|
|
359
|
+
const ids = new Set(searchByAttr(key, value, { topK: 10000 }).map((m) => m.id));
|
|
360
|
+
hits = hits.filter((m) => ids.has(m.id));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// Leftover plain text ranks the tag-matched set; only rows whose title or
|
|
364
|
+
// content actually mentions the keyword survive (scoreKeyword's 0.3 base is
|
|
365
|
+
// a non-match, so the containment check is the real gate); otherwise
|
|
366
|
+
// preserve insertion order.
|
|
367
|
+
const keywordOnly = q && !q.startsWith("entity:") && !q.startsWith("attr:");
|
|
368
|
+
hits = keywordOnly
|
|
369
|
+
? hits
|
|
370
|
+
.map((m) => ({ ...m, score: scoreKeyword(m, q), source: "tag" }))
|
|
371
|
+
.filter((m) => (m.title ?? "").toLowerCase().includes(q.toLowerCase())
|
|
372
|
+
|| (m.content ?? "").toLowerCase().includes(q.toLowerCase()))
|
|
373
|
+
.sort((a, b) => b.score - a.score)
|
|
374
|
+
: hits.map((m) => ({ ...m, source: "tag" }));
|
|
375
|
+
const result = hits.slice(0, topK);
|
|
376
|
+
touchRecalled(result);
|
|
377
|
+
return result;
|
|
378
|
+
}
|
|
379
|
+
|
|
299
380
|
/**
|
|
300
381
|
* Semantic-aware memory search: keyword recall (store.search) plus optional
|
|
301
382
|
* vector recall + rerank. mode:
|
|
@@ -402,8 +483,18 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
402
483
|
|
|
403
484
|
async function searchMemories(query, options = {}) {
|
|
404
485
|
const { mode = "auto", topK = 20, threshold, useRerank = true, recordRecall = false } = options;
|
|
405
|
-
const
|
|
406
|
-
if (!
|
|
486
|
+
const raw = String(query ?? "").trim();
|
|
487
|
+
if (!raw) return [];
|
|
488
|
+
|
|
489
|
+
// tag: 前缀(v0.6.2)。先把所有 tag: 令牌从 query 里剥出来,剩余的 q 仍可带
|
|
490
|
+
// entity:/attr: 前缀或普通关键词 —— searchByTags 负责交集/排序。没有 tag:
|
|
491
|
+
// 令牌则走下面的 entity:/attr:/文本原逻辑(完全向后兼容)。
|
|
492
|
+
const tagTokens = [];
|
|
493
|
+
const q = raw.replace(/\btag:([^\s]+)/g, (_, tok) => {
|
|
494
|
+
if (tok) tagTokens.push(tok);
|
|
495
|
+
return "";
|
|
496
|
+
}).trim();
|
|
497
|
+
if (tagTokens.length) return searchByTags(tagTokens, q, options);
|
|
407
498
|
|
|
408
499
|
// entity:/attr: 前缀路由(v0.3.0 Phase 3)。entitySearchEnabled 关闭时走原逻辑。
|
|
409
500
|
if (config?.entitySearchEnabled) {
|
|
@@ -508,7 +599,10 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
508
599
|
const ranked = [...byId.values()]
|
|
509
600
|
.sort((a, b) => (b.score ?? 0) - (a.score ?? 0))
|
|
510
601
|
.map((r) => ({ ...r, score: Math.max(0, Math.min(1, r.score ?? 0)) }));
|
|
511
|
-
|
|
602
|
+
// Tag boost (v0.6.4) needs the full ranked pool, not just the top-lim
|
|
603
|
+
// slice, so a tagged candidate just below the line can be re-admitted
|
|
604
|
+
// after the boost. Without boost this is the legacy lim truncation.
|
|
605
|
+
merged = config.tagBoostEnabled === true ? ranked : ranked.slice(0, lim);
|
|
512
606
|
if (merged.length < lim && !merged.length) {
|
|
513
607
|
// Vector unavailable entirely: fall back to plain keyword.
|
|
514
608
|
merged = keyword.slice(0, lim);
|
|
@@ -534,7 +628,31 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
534
628
|
// it is the documented text-only path and must not be altered by
|
|
535
629
|
// embedding state.
|
|
536
630
|
merged = mode === "keyword" ? merged : semanticDeduplicate(merged);
|
|
631
|
+
|
|
632
|
+
// Tag-weighted re-rank (v0.6.4): boost candidates whose tags overlap the
|
|
633
|
+
// query tags or the current session's hot-memory tags — applied BEFORE the
|
|
634
|
+
// final top-K cut so tagged candidates just below the line can be
|
|
635
|
+
// re-admitted. Opt-in, and skipped entirely on the keyword-only path. When
|
|
636
|
+
// a reranker is configured (opt-in), it remains the final authority on
|
|
637
|
+
// order; the boost still shapes which candidates reach it.
|
|
638
|
+
if (config.tagBoostEnabled === true && mode !== "keyword" && merged.length) {
|
|
639
|
+
const ids = merged.map((m) => m.id);
|
|
640
|
+
const tagsMap = store.getMemoryTagsMap(ids);
|
|
641
|
+
const enriched = merged.map((m) => ({ ...m, tags: tagsMap.get(m.id) ?? [] }));
|
|
642
|
+
const knownTags = [...tagsMap.values()].flat();
|
|
643
|
+
const queryTags = extractQueryTags(q, knownTags);
|
|
644
|
+
const sessionTags = Array.isArray(options?.sessionTags) ? options.sessionTags : [];
|
|
645
|
+
if (queryTags.length || sessionTags.length) {
|
|
646
|
+
merged = applyTagBoost(enriched, {
|
|
647
|
+
queryTags,
|
|
648
|
+
sessionTags,
|
|
649
|
+
factor: config.tagBoostFactor,
|
|
650
|
+
sessionFactor: config.sessionTagBoostFactor,
|
|
651
|
+
}).map(({ tags, ...rest }) => rest);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
537
654
|
merged = merged.slice(0, lim);
|
|
655
|
+
|
|
538
656
|
let result = useRerank && reranker && merged.length
|
|
539
657
|
? await rerankCandidates(q, merged, lim)
|
|
540
658
|
: merged;
|
|
@@ -802,6 +920,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
802
920
|
afterSync("write");
|
|
803
921
|
notifyWrite();
|
|
804
922
|
scheduleEmbed(result);
|
|
923
|
+
scheduleWikiLinkResolve(result);
|
|
805
924
|
return { action: "merged", memory: result };
|
|
806
925
|
}
|
|
807
926
|
const created = store.save({
|
|
@@ -821,6 +940,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
821
940
|
notifyWrite();
|
|
822
941
|
scheduleEmbed(result);
|
|
823
942
|
scheduleEntityExtraction(result);
|
|
943
|
+
scheduleWikiLinkResolve(result);
|
|
824
944
|
return { action: "created", memory: result };
|
|
825
945
|
}
|
|
826
946
|
|
|
@@ -1010,6 +1130,21 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1010
1130
|
}));
|
|
1011
1131
|
}
|
|
1012
1132
|
|
|
1133
|
+
/**
|
|
1134
|
+
* Directory view (v0.6.3): group live memories by tag. Delegates to
|
|
1135
|
+
* store.getDirectory (tag-sorted groups, importance/updated DESC members,
|
|
1136
|
+
* live-only filtering) and maps every memory to the wire DTO so the result
|
|
1137
|
+
* is JSON-safe for the API endpoint.
|
|
1138
|
+
* @returns {{groups: {tag: string, memories: object[]}[], untagged: object[]}}
|
|
1139
|
+
*/
|
|
1140
|
+
function getDirectory() {
|
|
1141
|
+
const { groups, untagged } = store.getDirectory();
|
|
1142
|
+
return {
|
|
1143
|
+
groups: groups.map((g) => ({ tag: g.tag, memories: toApiList(g.memories) })),
|
|
1144
|
+
untagged: toApiList(untagged)
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1013
1148
|
/**
|
|
1014
1149
|
* Three-way merge of in-flight human mirror edits before a re-render.
|
|
1015
1150
|
* Runs on every syncMirror, so a human edit made between two store writes is
|
|
@@ -1117,8 +1252,14 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1117
1252
|
// leaving committed files mislabeled as failed and masking partial state.
|
|
1118
1253
|
// Absent entries (a type with no memories) count as success: sync prunes
|
|
1119
1254
|
// the stale file, which is itself a completed physical state.
|
|
1255
|
+
// v0.6.2: attach entity_attrs-backed tags (entityTags) after the human-edit
|
|
1256
|
+
// merge so renderMemory can draw the `#tag` line under each title. The
|
|
1257
|
+
// bulk map is a single query, and a missing row simply renders no line.
|
|
1258
|
+
const reconciled = reconcileHumanEdits(list);
|
|
1259
|
+
const tagsMap = store.getMemoryTagsMap(reconciled.map((m) => m.id));
|
|
1260
|
+
const tagged = reconciled.map((m) => ({ ...m, entityTags: tagsMap.get(m.id) ?? [] }));
|
|
1120
1261
|
let allOk = true;
|
|
1121
|
-
const results = mirror.sync(
|
|
1262
|
+
const results = mirror.sync(tagged) ?? {};
|
|
1122
1263
|
for (const type of Object.keys(TYPE_FILE)) {
|
|
1123
1264
|
const r = results[type];
|
|
1124
1265
|
const ok = !r || r.ok === true;
|
|
@@ -1286,14 +1427,59 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1286
1427
|
}
|
|
1287
1428
|
}
|
|
1288
1429
|
|
|
1430
|
+
/**
|
|
1431
|
+
* Forward links (v0.6.1): memories the given memory explicitly links to via
|
|
1432
|
+
* [[wiki-links]] in its content. Reads the links_to relations whose
|
|
1433
|
+
* from_entity is this memory's title and resolves each to_entity back to a
|
|
1434
|
+
* memory row (case-insensitive title match). Returns [{ target, relation }];
|
|
1435
|
+
* a target title with no matching memory surfaces as { target: null }.
|
|
1436
|
+
*/
|
|
1437
|
+
function getForwardLinks(memoryId) {
|
|
1438
|
+
const memory = store.getById(memoryId);
|
|
1439
|
+
if (!memory) return [];
|
|
1440
|
+
const out = [];
|
|
1441
|
+
for (const rel of store.getRelations(memory.title) ?? []) {
|
|
1442
|
+
if (rel.relation_type !== "links_to" || rel.from_entity !== memory.title) continue;
|
|
1443
|
+
out.push({ target: store.findByTitle?.(rel.to_entity) ?? null, relation: rel });
|
|
1444
|
+
}
|
|
1445
|
+
return out;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
/**
|
|
1449
|
+
* Back links (v0.6.1): memories that explicitly link TO the given memory
|
|
1450
|
+
* (their content carries a wiki-link whose target resolves to this memory's
|
|
1451
|
+
* title). Reads the links_to relations whose to_entity is this memory's
|
|
1452
|
+
* title; the linking memory is rel.memory_id (the source that wrote the
|
|
1453
|
+
* relation). Deduped per source memory; missing/self links are dropped.
|
|
1454
|
+
* Returns [{ source, relation }].
|
|
1455
|
+
*/
|
|
1456
|
+
function getBacklinks(memoryId) {
|
|
1457
|
+
const memory = store.getById(memoryId);
|
|
1458
|
+
if (!memory) return [];
|
|
1459
|
+
const out = [];
|
|
1460
|
+
const seen = new Set();
|
|
1461
|
+
for (const rel of store.getRelations(memory.title) ?? []) {
|
|
1462
|
+
if (rel.relation_type !== "links_to" || rel.to_entity !== memory.title) continue;
|
|
1463
|
+
const source = rel.memory_id ? store.getById(rel.memory_id) : undefined;
|
|
1464
|
+
if (!source || source.id === memory.id || seen.has(source.id)) continue;
|
|
1465
|
+
seen.add(source.id);
|
|
1466
|
+
out.push({ source, relation: rel });
|
|
1467
|
+
}
|
|
1468
|
+
return out;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1289
1471
|
return {
|
|
1290
1472
|
saveWithDedupe,
|
|
1473
|
+
getBacklinks,
|
|
1474
|
+
getForwardLinks,
|
|
1475
|
+
resolveWikiLink: (title) => store.findByTitle?.(title),
|
|
1291
1476
|
recoverMirror,
|
|
1292
1477
|
getMirrorHealth,
|
|
1293
1478
|
getMirrorState: () => store.getMirrorState(),
|
|
1294
1479
|
injectCandidates,
|
|
1295
1480
|
mergeHumanEdits,
|
|
1296
1481
|
toApiList,
|
|
1482
|
+
getDirectory,
|
|
1297
1483
|
transaction,
|
|
1298
1484
|
enqueue,
|
|
1299
1485
|
setDreamHook(fn) { dreamHook = fn; },
|
|
@@ -1393,6 +1579,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1393
1579
|
const sync = afterSync("write");
|
|
1394
1580
|
notifyWrite();
|
|
1395
1581
|
scheduleEmbed(updated);
|
|
1582
|
+
scheduleWikiLinkResolve(updated);
|
|
1396
1583
|
// Audit peer B: when the mirror sync failed, the store write landed but
|
|
1397
1584
|
// the mirror did not converge — return an explicit degraded receipt rather
|
|
1398
1585
|
// than a plain success. Non-enumerable so existing deepEqual assertions on
|
|
@@ -1504,8 +1691,28 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1504
1691
|
// migrates entity_attrs on merge. Bookkeeping writes like the audit
|
|
1505
1692
|
// passthroughs above — never write-hook-triggering memory mutations.
|
|
1506
1693
|
saveRelation: (r) => store.saveRelation(r),
|
|
1694
|
+
saveWikiLinks: (r) => store.saveWikiLinks(r),
|
|
1507
1695
|
listEntities: (o) => store.listEntities(o),
|
|
1508
1696
|
getRelations: (id) => store.getRelations(id),
|
|
1697
|
+
// Tag system (v0.6.2). setMemoryTags is the manual/user path: gated by
|
|
1698
|
+
// config.manualTagEnabled (default true), re-renders the mirror and fires
|
|
1699
|
+
// the write hook. applyMemoryTags is the raw write used by the autoDream
|
|
1700
|
+
// tag pass (gated by autoTagEnabled, wrapped in a transaction by the
|
|
1701
|
+
// extractor so the mirror re-renders exactly once).
|
|
1702
|
+
setMemoryTags: (memoryId, tags) => {
|
|
1703
|
+
if (config.manualTagEnabled === false) {
|
|
1704
|
+
return { ok: false, error: "manualTagEnabled is off" };
|
|
1705
|
+
}
|
|
1706
|
+
const stored = store.setMemoryTags(memoryId, tags);
|
|
1707
|
+
afterSync("write");
|
|
1708
|
+
notifyWrite();
|
|
1709
|
+
return { ok: true, tags: stored };
|
|
1710
|
+
},
|
|
1711
|
+
getMemoryTags: (memoryId) => store.getMemoryTags(memoryId),
|
|
1712
|
+
// Read-only gate flag so the Web panel can hide tag editing when the
|
|
1713
|
+
// manual path is disabled (default: manual tagging is on).
|
|
1714
|
+
manualTagEnabled: () => config.manualTagEnabled !== false,
|
|
1715
|
+
applyMemoryTags: (memoryId, tags) => store.setMemoryTags(memoryId, tags),
|
|
1509
1716
|
saveAttr: (r) => store.saveAttr(r),
|
|
1510
1717
|
createEntity: (r) => store.createEntity(r),
|
|
1511
1718
|
findEntityByName: (n) => store.findEntityByName(n),
|