@modusensus/dsh-mneme 0.6.1 → 0.6.6
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 +17 -0
- package/lib/api.js +77 -1
- package/lib/client.js +234 -5
- package/lib/config.js +18 -0
- package/lib/dream/tag-extractor.js +156 -0
- package/lib/dream.js +20 -0
- package/lib/mirror.js +9 -0
- package/lib/parser/tag.js +59 -0
- package/lib/search/tag-boost.js +61 -0
- package/lib/service.js +134 -4
- package/lib/store.js +162 -0
- package/package.json +1 -1
- package/src/api.js +77 -1
- package/src/config.js +18 -0
- package/src/dream/tag-extractor.js +156 -0
- package/src/dream.js +20 -0
- package/src/mirror.js +9 -0
- package/src/parser/tag.js +59 -0
- package/src/search/tag-boost.js +61 -0
- package/src/service.js +134 -4
- package/src/store.js +162 -0
- package/test/api.test.js +80 -0
- package/test/boundary-v0625.test.js +82 -0
- package/test/client.test.js +98 -0
- package/test/directory.test.js +134 -0
- package/test/tag-boost.test.js +125 -0
- package/test/tag.test.js +312 -0
package/lib/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/lib/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,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/lib/service.js
CHANGED
|
@@ -4,6 +4,7 @@ import { evaluateMemoryQuality } from "./quality-filter.js";
|
|
|
4
4
|
import { createBM25Index } from "./search/bm25.js";
|
|
5
5
|
import { adaptiveThreshold } from "./search/adaptive.js";
|
|
6
6
|
import { parseWikiLinks } from "./parser/wiki-link.js";
|
|
7
|
+
import { extractQueryTags, applyTagBoost } from "./search/tag-boost.js";
|
|
7
8
|
|
|
8
9
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
9
10
|
|
|
@@ -327,6 +328,57 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
327
328
|
return hits;
|
|
328
329
|
}
|
|
329
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
|
+
// Only count toward recall stats/forgetting when the caller asked for
|
|
377
|
+
// recording — the panel's `tag:` search must not pollute the curve.
|
|
378
|
+
if (options.recordRecall) touchRecalled(result);
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
|
|
330
382
|
/**
|
|
331
383
|
* Semantic-aware memory search: keyword recall (store.search) plus optional
|
|
332
384
|
* vector recall + rerank. mode:
|
|
@@ -433,8 +485,18 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
433
485
|
|
|
434
486
|
async function searchMemories(query, options = {}) {
|
|
435
487
|
const { mode = "auto", topK = 20, threshold, useRerank = true, recordRecall = false } = options;
|
|
436
|
-
const
|
|
437
|
-
if (!
|
|
488
|
+
const raw = String(query ?? "").trim();
|
|
489
|
+
if (!raw) return [];
|
|
490
|
+
|
|
491
|
+
// tag: 前缀(v0.6.2)。先把所有 tag: 令牌从 query 里剥出来,剩余的 q 仍可带
|
|
492
|
+
// entity:/attr: 前缀或普通关键词 —— searchByTags 负责交集/排序。没有 tag:
|
|
493
|
+
// 令牌则走下面的 entity:/attr:/文本原逻辑(完全向后兼容)。
|
|
494
|
+
const tagTokens = [];
|
|
495
|
+
const q = raw.replace(/\btag:([^\s]+)/g, (_, tok) => {
|
|
496
|
+
if (tok) tagTokens.push(tok);
|
|
497
|
+
return "";
|
|
498
|
+
}).trim();
|
|
499
|
+
if (tagTokens.length) return searchByTags(tagTokens, q, options);
|
|
438
500
|
|
|
439
501
|
// entity:/attr: 前缀路由(v0.3.0 Phase 3)。entitySearchEnabled 关闭时走原逻辑。
|
|
440
502
|
if (config?.entitySearchEnabled) {
|
|
@@ -539,7 +601,10 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
539
601
|
const ranked = [...byId.values()]
|
|
540
602
|
.sort((a, b) => (b.score ?? 0) - (a.score ?? 0))
|
|
541
603
|
.map((r) => ({ ...r, score: Math.max(0, Math.min(1, r.score ?? 0)) }));
|
|
542
|
-
|
|
604
|
+
// Tag boost (v0.6.4) needs the full ranked pool, not just the top-lim
|
|
605
|
+
// slice, so a tagged candidate just below the line can be re-admitted
|
|
606
|
+
// after the boost. Without boost this is the legacy lim truncation.
|
|
607
|
+
merged = config.tagBoostEnabled === true ? ranked : ranked.slice(0, lim);
|
|
543
608
|
if (merged.length < lim && !merged.length) {
|
|
544
609
|
// Vector unavailable entirely: fall back to plain keyword.
|
|
545
610
|
merged = keyword.slice(0, lim);
|
|
@@ -565,7 +630,31 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
565
630
|
// it is the documented text-only path and must not be altered by
|
|
566
631
|
// embedding state.
|
|
567
632
|
merged = mode === "keyword" ? merged : semanticDeduplicate(merged);
|
|
633
|
+
|
|
634
|
+
// Tag-weighted re-rank (v0.6.4): boost candidates whose tags overlap the
|
|
635
|
+
// query tags or the current session's hot-memory tags — applied BEFORE the
|
|
636
|
+
// final top-K cut so tagged candidates just below the line can be
|
|
637
|
+
// re-admitted. Opt-in, and skipped entirely on the keyword-only path. When
|
|
638
|
+
// a reranker is configured (opt-in), it remains the final authority on
|
|
639
|
+
// order; the boost still shapes which candidates reach it.
|
|
640
|
+
if (config.tagBoostEnabled === true && mode !== "keyword" && merged.length) {
|
|
641
|
+
const ids = merged.map((m) => m.id);
|
|
642
|
+
const tagsMap = store.getMemoryTagsMap(ids);
|
|
643
|
+
const enriched = merged.map((m) => ({ ...m, tags: tagsMap.get(m.id) ?? [] }));
|
|
644
|
+
const knownTags = [...tagsMap.values()].flat();
|
|
645
|
+
const queryTags = extractQueryTags(q, knownTags);
|
|
646
|
+
const sessionTags = Array.isArray(options?.sessionTags) ? options.sessionTags : [];
|
|
647
|
+
if (queryTags.length || sessionTags.length) {
|
|
648
|
+
merged = applyTagBoost(enriched, {
|
|
649
|
+
queryTags,
|
|
650
|
+
sessionTags,
|
|
651
|
+
factor: config.tagBoostFactor,
|
|
652
|
+
sessionFactor: config.sessionTagBoostFactor,
|
|
653
|
+
}).map(({ tags, ...rest }) => rest);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
568
656
|
merged = merged.slice(0, lim);
|
|
657
|
+
|
|
569
658
|
let result = useRerank && reranker && merged.length
|
|
570
659
|
? await rerankCandidates(q, merged, lim)
|
|
571
660
|
: merged;
|
|
@@ -1043,6 +1132,21 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1043
1132
|
}));
|
|
1044
1133
|
}
|
|
1045
1134
|
|
|
1135
|
+
/**
|
|
1136
|
+
* Directory view (v0.6.3): group live memories by tag. Delegates to
|
|
1137
|
+
* store.getDirectory (tag-sorted groups, importance/updated DESC members,
|
|
1138
|
+
* live-only filtering) and maps every memory to the wire DTO so the result
|
|
1139
|
+
* is JSON-safe for the API endpoint.
|
|
1140
|
+
* @returns {{groups: {tag: string, memories: object[]}[], untagged: object[]}}
|
|
1141
|
+
*/
|
|
1142
|
+
function getDirectory() {
|
|
1143
|
+
const { groups, untagged } = store.getDirectory();
|
|
1144
|
+
return {
|
|
1145
|
+
groups: groups.map((g) => ({ tag: g.tag, memories: toApiList(g.memories) })),
|
|
1146
|
+
untagged: toApiList(untagged)
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1046
1150
|
/**
|
|
1047
1151
|
* Three-way merge of in-flight human mirror edits before a re-render.
|
|
1048
1152
|
* Runs on every syncMirror, so a human edit made between two store writes is
|
|
@@ -1150,8 +1254,14 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1150
1254
|
// leaving committed files mislabeled as failed and masking partial state.
|
|
1151
1255
|
// Absent entries (a type with no memories) count as success: sync prunes
|
|
1152
1256
|
// the stale file, which is itself a completed physical state.
|
|
1257
|
+
// v0.6.2: attach entity_attrs-backed tags (entityTags) after the human-edit
|
|
1258
|
+
// merge so renderMemory can draw the `#tag` line under each title. The
|
|
1259
|
+
// bulk map is a single query, and a missing row simply renders no line.
|
|
1260
|
+
const reconciled = reconcileHumanEdits(list);
|
|
1261
|
+
const tagsMap = store.getMemoryTagsMap(reconciled.map((m) => m.id));
|
|
1262
|
+
const tagged = reconciled.map((m) => ({ ...m, entityTags: tagsMap.get(m.id) ?? [] }));
|
|
1153
1263
|
let allOk = true;
|
|
1154
|
-
const results = mirror.sync(
|
|
1264
|
+
const results = mirror.sync(tagged) ?? {};
|
|
1155
1265
|
for (const type of Object.keys(TYPE_FILE)) {
|
|
1156
1266
|
const r = results[type];
|
|
1157
1267
|
const ok = !r || r.ok === true;
|
|
@@ -1371,6 +1481,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1371
1481
|
injectCandidates,
|
|
1372
1482
|
mergeHumanEdits,
|
|
1373
1483
|
toApiList,
|
|
1484
|
+
getDirectory,
|
|
1374
1485
|
transaction,
|
|
1375
1486
|
enqueue,
|
|
1376
1487
|
setDreamHook(fn) { dreamHook = fn; },
|
|
@@ -1585,6 +1696,25 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1585
1696
|
saveWikiLinks: (r) => store.saveWikiLinks(r),
|
|
1586
1697
|
listEntities: (o) => store.listEntities(o),
|
|
1587
1698
|
getRelations: (id) => store.getRelations(id),
|
|
1699
|
+
// Tag system (v0.6.2). setMemoryTags is the manual/user path: gated by
|
|
1700
|
+
// config.manualTagEnabled (default true), re-renders the mirror and fires
|
|
1701
|
+
// the write hook. applyMemoryTags is the raw write used by the autoDream
|
|
1702
|
+
// tag pass (gated by autoTagEnabled, wrapped in a transaction by the
|
|
1703
|
+
// extractor so the mirror re-renders exactly once).
|
|
1704
|
+
setMemoryTags: (memoryId, tags) => {
|
|
1705
|
+
if (config.manualTagEnabled === false) {
|
|
1706
|
+
return { ok: false, error: "manualTagEnabled is off" };
|
|
1707
|
+
}
|
|
1708
|
+
const stored = store.setMemoryTags(memoryId, tags);
|
|
1709
|
+
afterSync("write");
|
|
1710
|
+
notifyWrite();
|
|
1711
|
+
return { ok: true, tags: stored };
|
|
1712
|
+
},
|
|
1713
|
+
getMemoryTags: (memoryId) => store.getMemoryTags(memoryId),
|
|
1714
|
+
// Read-only gate flag so the Web panel can hide tag editing when the
|
|
1715
|
+
// manual path is disabled (default: manual tagging is on).
|
|
1716
|
+
manualTagEnabled: () => config.manualTagEnabled !== false,
|
|
1717
|
+
applyMemoryTags: (memoryId, tags) => store.setMemoryTags(memoryId, tags),
|
|
1588
1718
|
saveAttr: (r) => store.saveAttr(r),
|
|
1589
1719
|
createEntity: (r) => store.createEntity(r),
|
|
1590
1720
|
findEntityByName: (n) => store.findEntityByName(n),
|
package/lib/store.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { DatabaseSync } from "node:sqlite";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { sanitizeTags } from "./parser/tag.js";
|
|
3
4
|
|
|
4
5
|
const SCHEMA = `
|
|
5
6
|
CREATE TABLE IF NOT EXISTS memories (
|
|
@@ -1684,6 +1685,162 @@ export function createStore(path) {
|
|
|
1684
1685
|
return memories;
|
|
1685
1686
|
}
|
|
1686
1687
|
|
|
1688
|
+
// --- tag storage (v0.6.2) ------------------------------------------------
|
|
1689
|
+
// Tags ride the snapshot-style entity_attrs table (attr_key='tags'), so one
|
|
1690
|
+
// memory has exactly one live tags row; setMemoryTags invalidates any prior
|
|
1691
|
+
// live row and inserts a fresh one (idempotent overwrite). entity_id is the
|
|
1692
|
+
// memory id itself (the memory is its own tag entity), memory_id is kept so
|
|
1693
|
+
// the existing memory-scoped attr queries (getAttrsByMemory / findMemoriesByAttr)
|
|
1694
|
+
// and the bulk tag map all work without a special path.
|
|
1695
|
+
|
|
1696
|
+
/** Normalize an arbitrary tags input to a deduplicated string array.
|
|
1697
|
+
* Delegates to parser/tag.js sanitizeTags (shared validation with parseTags
|
|
1698
|
+
* and the autoDream tag-extractor): strips a leading `#`, trims, drops
|
|
1699
|
+
* non-strings/blanks/over-long/illegal-char tags. Kept as a thin alias so
|
|
1700
|
+
* the tag write path validates identically to the parser path. */
|
|
1701
|
+
function normalizeTags(tags) {
|
|
1702
|
+
return sanitizeTags(tags);
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
/**
|
|
1706
|
+
* Set (overwrite) the live tag set for a memory. Exactly one tags row stays
|
|
1707
|
+
* live per memory: any prior live row is invalidated first, then one fresh
|
|
1708
|
+
* row is written (no-op when tags is empty — the invalidated row is removed
|
|
1709
|
+
* so "clear tags" = no live row). Returns the stored tag array.
|
|
1710
|
+
*/
|
|
1711
|
+
function setMemoryTags(memoryId, tags) {
|
|
1712
|
+
const arr = normalizeTags(tags);
|
|
1713
|
+
const now = nowIso();
|
|
1714
|
+
// Atomic: the invalidation and the fresh row must land together, so a
|
|
1715
|
+
// mid-write crash never leaves the old live row gone without a replacement.
|
|
1716
|
+
// SAVEPOINT (not BEGIN) so this nests safely inside service.transaction().
|
|
1717
|
+
db.exec("SAVEPOINT set_memory_tags");
|
|
1718
|
+
try {
|
|
1719
|
+
db.prepare(
|
|
1720
|
+
`UPDATE entity_attrs SET valid_until = ?
|
|
1721
|
+
WHERE attr_key = 'tags' AND memory_id = ? AND valid_until IS NULL`
|
|
1722
|
+
).run(now, memoryId);
|
|
1723
|
+
if (arr.length) {
|
|
1724
|
+
const id = randomUUID();
|
|
1725
|
+
db.prepare(
|
|
1726
|
+
`INSERT INTO entity_attrs (id, entity_id, attr_key, attr_value, memory_id, valid_from, valid_until, confidence, source)
|
|
1727
|
+
VALUES (?, ?, 'tags', ?, ?, ?, NULL, 1.0, 'manual')`
|
|
1728
|
+
).run(id, memoryId, JSON.stringify(arr), memoryId, now);
|
|
1729
|
+
}
|
|
1730
|
+
db.exec("RELEASE set_memory_tags");
|
|
1731
|
+
} catch (e) {
|
|
1732
|
+
db.exec("ROLLBACK TO set_memory_tags");
|
|
1733
|
+
db.exec("RELEASE set_memory_tags");
|
|
1734
|
+
throw e;
|
|
1735
|
+
}
|
|
1736
|
+
return arr;
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
/** Live tags for a memory ([] when none / unknown). */
|
|
1740
|
+
function getMemoryTags(memoryId) {
|
|
1741
|
+
const row = db.prepare(
|
|
1742
|
+
`SELECT attr_value FROM entity_attrs
|
|
1743
|
+
WHERE attr_key = 'tags' AND memory_id = ? AND valid_until IS NULL
|
|
1744
|
+
ORDER BY valid_from DESC LIMIT 1`
|
|
1745
|
+
).get(memoryId);
|
|
1746
|
+
if (!row) return [];
|
|
1747
|
+
try {
|
|
1748
|
+
const arr = JSON.parse(row.attr_value);
|
|
1749
|
+
return Array.isArray(arr) ? arr : [];
|
|
1750
|
+
} catch {
|
|
1751
|
+
return [];
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
/** Bulk live-tags lookup for mirror rendering. Returns Map<memoryId, string[]>. */
|
|
1756
|
+
function getMemoryTagsMap(ids) {
|
|
1757
|
+
const out = new Map();
|
|
1758
|
+
const list = (Array.isArray(ids) ? ids : []).filter(Boolean);
|
|
1759
|
+
for (let i = 0; i < list.length; i += 100) {
|
|
1760
|
+
const chunk = list.slice(i, i + 100);
|
|
1761
|
+
const rows = db.prepare(
|
|
1762
|
+
`SELECT memory_id, attr_value FROM entity_attrs
|
|
1763
|
+
WHERE attr_key = 'tags' AND valid_until IS NULL
|
|
1764
|
+
AND memory_id IN (${chunk.map(() => "?").join(",")})`
|
|
1765
|
+
).all(...chunk);
|
|
1766
|
+
for (const row of rows) {
|
|
1767
|
+
try {
|
|
1768
|
+
const arr = JSON.parse(row.attr_value);
|
|
1769
|
+
if (Array.isArray(arr) && arr.length) out.set(row.memory_id, arr);
|
|
1770
|
+
} catch { /* corrupt row: skip */ }
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
return out;
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
/**
|
|
1777
|
+
* Memories carrying a live tags row that contains EVERY requested tag
|
|
1778
|
+
* (AND semantics for a multi-tag query). attr_value is a JSON array, so the
|
|
1779
|
+
* match uses quoted `"tag"` substrings — `tag:lin` never collides with
|
|
1780
|
+
* `linux` because JSON array elements are quote-delimited. Only live rows
|
|
1781
|
+
* (valid_until IS NULL) with a memory reference participate; each memory
|
|
1782
|
+
* appears once.
|
|
1783
|
+
*/
|
|
1784
|
+
function findMemoriesByTags(tags) {
|
|
1785
|
+
const list = normalizeTags(tags);
|
|
1786
|
+
if (!list.length) return [];
|
|
1787
|
+
const where = list.map(() => `attr_value LIKE ? ESCAPE '\\'`).join(" AND ");
|
|
1788
|
+
const params = list.map((t) => `%"${escapeLike(t)}"%`);
|
|
1789
|
+
const rows = db.prepare(
|
|
1790
|
+
`SELECT DISTINCT memory_id FROM entity_attrs
|
|
1791
|
+
WHERE attr_key = 'tags' AND valid_until IS NULL
|
|
1792
|
+
AND memory_id IS NOT NULL AND memory_id != ''
|
|
1793
|
+
AND (${where})`
|
|
1794
|
+
).all(...params);
|
|
1795
|
+
// Same live-memory filter as getDirectory/store.search: forgotten/archived/
|
|
1796
|
+
// session-disposed memories are invisible to `tag:` recall.
|
|
1797
|
+
const stmt = db.prepare(
|
|
1798
|
+
"SELECT * FROM memories WHERE id = ? AND forgotten = 0 AND archived = 0 AND session_disposed_at IS NULL"
|
|
1799
|
+
);
|
|
1800
|
+
const memories = [];
|
|
1801
|
+
for (const { memory_id } of rows) {
|
|
1802
|
+
const row = stmt.get(memory_id);
|
|
1803
|
+
if (row) memories.push(toRow(row));
|
|
1804
|
+
}
|
|
1805
|
+
return memories;
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
/**
|
|
1809
|
+
* Directory view (v0.6.3): group live memories by their entity_attrs-backed
|
|
1810
|
+
* tag set. A memory carrying N tags appears under all N tag folders; a memory
|
|
1811
|
+
* with no live tags lands in `untagged`. Only live rows participate —
|
|
1812
|
+
* forgotten, archived and session-disposed memories are excluded. Groups are
|
|
1813
|
+
* ordered by tag (locale-aware), group members and untagged follow the
|
|
1814
|
+
* canonical memory order (importance DESC, updated_at DESC, id).
|
|
1815
|
+
* @returns {{groups: {tag: string, memories: object[]}[], untagged: object[]}}
|
|
1816
|
+
*/
|
|
1817
|
+
function getDirectory() {
|
|
1818
|
+
const rows = db.prepare(
|
|
1819
|
+
`SELECT * FROM memories
|
|
1820
|
+
WHERE forgotten = 0 AND archived = 0 AND session_disposed_at IS NULL
|
|
1821
|
+
ORDER BY importance DESC, updated_at DESC, id`
|
|
1822
|
+
).all();
|
|
1823
|
+
const memories = rows.map(toRow);
|
|
1824
|
+
const tagMap = getMemoryTagsMap(memories.map((m) => m.id));
|
|
1825
|
+
const byTag = new Map(); // tag -> memory[]
|
|
1826
|
+
const untagged = [];
|
|
1827
|
+
for (const m of memories) {
|
|
1828
|
+
const tags = tagMap.get(m.id);
|
|
1829
|
+
if (!tags || tags.length === 0) {
|
|
1830
|
+
untagged.push(m);
|
|
1831
|
+
continue;
|
|
1832
|
+
}
|
|
1833
|
+
for (const tag of tags) {
|
|
1834
|
+
if (!byTag.has(tag)) byTag.set(tag, []);
|
|
1835
|
+
byTag.get(tag).push(m);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
const groups = [...byTag.entries()]
|
|
1839
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
1840
|
+
.map(([tag, ms]) => ({ tag, memories: ms }));
|
|
1841
|
+
return { groups, untagged };
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1687
1844
|
/**
|
|
1688
1845
|
* Record a typed relation between two entities. metadata (optional) is a
|
|
1689
1846
|
* free-form JSON blob describing the relation. Relations are append-only —
|
|
@@ -2055,6 +2212,11 @@ export function createStore(path) {
|
|
|
2055
2212
|
getAttrHistory,
|
|
2056
2213
|
getAttrsByMemory,
|
|
2057
2214
|
findMemoriesByAttr,
|
|
2215
|
+
setMemoryTags,
|
|
2216
|
+
getMemoryTags,
|
|
2217
|
+
getMemoryTagsMap,
|
|
2218
|
+
findMemoriesByTags,
|
|
2219
|
+
getDirectory,
|
|
2058
2220
|
saveRelation,
|
|
2059
2221
|
saveWikiLinks,
|
|
2060
2222
|
findByTitle,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modusensus/dsh-mneme",
|
|
3
3
|
"description": "Cross-session memory plugin for DeepSeek Harness with autoDream consolidation: SQLite store, Markdown mirrors, 7 model tools, automatic injection, session summarization, user profile/rules, custom slash commands, vector (semantic) search, and a Web GUI panel",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.6",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
package/src/api.js
CHANGED
|
@@ -608,8 +608,84 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
|
|
|
608
608
|
}
|
|
609
609
|
});
|
|
610
610
|
|
|
611
|
+
// --- memory tags (v0.6.2) ------------------------------------------------
|
|
612
|
+
// GET /api/dsh-mneme/memory/tags?id=<memoryId> → live entity_attrs-backed
|
|
613
|
+
// tag set for one memory plus the manualTagEnabled gate (so the panel
|
|
614
|
+
// can hide tag editing when the manual path is off). Read-only, stays
|
|
615
|
+
// open when apiToken is set (like list/search/semantic).
|
|
616
|
+
// POST /api/dsh-mneme/memory/tags { id, tags } → overwrite the live tag set
|
|
617
|
+
// via service.setMemoryTags (manualTagEnabled gate); 409 when the gate
|
|
618
|
+
// is closed. Auth-gated like the other write endpoints.
|
|
619
|
+
register({
|
|
620
|
+
kind: "exact",
|
|
621
|
+
path: "/api/dsh-mneme/memory/tags",
|
|
622
|
+
handler(req, res) {
|
|
623
|
+
try {
|
|
624
|
+
if (req.method === "POST" || req.method === "PUT") {
|
|
625
|
+
if (!requireAuth(req, res, apiToken)) return;
|
|
626
|
+
return readBody(req).then((text) => {
|
|
627
|
+
const body = parseBody(text);
|
|
628
|
+
const id = typeof body.id === "string" ? body.id.trim() : "";
|
|
629
|
+
if (!id) {
|
|
630
|
+
sendJson(res, 400, { error: "missing-id" });
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const memory = service.getById?.(id) ?? null;
|
|
634
|
+
if (!memory) {
|
|
635
|
+
sendJson(res, 404, { error: "memory-not-found" });
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const result = service.setMemoryTags(id, Array.isArray(body.tags) ? body.tags : []);
|
|
639
|
+
if (result?.ok === false) {
|
|
640
|
+
sendJson(res, 409, { error: result.error || "tags-disabled" });
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
sendJson(res, 200, { ok: true, memoryId: id, tags: result?.tags ?? [] });
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
const url = new URL(req.url, "http://localhost");
|
|
647
|
+
const id = (url.searchParams.get("id") ?? "").trim();
|
|
648
|
+
if (!id) {
|
|
649
|
+
sendJson(res, 400, { error: "missing-id" });
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const memory = service.getById?.(id) ?? null;
|
|
653
|
+
if (!memory) {
|
|
654
|
+
sendJson(res, 404, { error: "memory-not-found" });
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
const tags = service.getMemoryTags?.(id) ?? [];
|
|
658
|
+
sendJson(res, 200, {
|
|
659
|
+
memoryId: id,
|
|
660
|
+
tags: Array.isArray(tags) ? tags : [],
|
|
661
|
+
manualTagEnabled: service.manualTagEnabled?.() ?? true
|
|
662
|
+
});
|
|
663
|
+
} catch {
|
|
664
|
+
sendJson(res, 500, { error: "internal" });
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
// --- directory view (v0.6.3) ---------------------------------------------
|
|
670
|
+
// GET /api/dsh-mneme/directory → memories grouped by tag as
|
|
671
|
+
// { groups: [{ tag, memories: [...] }], untagged: [...] }. Live-only
|
|
672
|
+
// (forgotten/archived/session-disposed excluded), groups tag-sorted, members
|
|
673
|
+
// importance+updated DESC. Read-only, stays open when apiToken is set.
|
|
674
|
+
register({
|
|
675
|
+
kind: "exact",
|
|
676
|
+
path: "/api/dsh-mneme/directory",
|
|
677
|
+
handler(req, res) {
|
|
678
|
+
try {
|
|
679
|
+
const dir = service.getDirectory?.() ?? { groups: [], untagged: [] };
|
|
680
|
+
sendJson(res, 200, dir);
|
|
681
|
+
} catch {
|
|
682
|
+
sendJson(res, 500, { error: "internal" });
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
|
|
611
687
|
return {
|
|
612
|
-
routes:
|
|
688
|
+
routes: 19,
|
|
613
689
|
dispose: () => {
|
|
614
690
|
for (const dispose of disposers) dispose();
|
|
615
691
|
}
|
package/src/config.js
CHANGED
|
@@ -175,6 +175,24 @@ export const Config = z.object({
|
|
|
175
175
|
// regardless of this flag.
|
|
176
176
|
wikiLinkEnabled: z.boolean().default(false),
|
|
177
177
|
|
|
178
|
+
// --- tag system (v0.6.2) ---------------------------------------------------
|
|
179
|
+
// Opt-in: when autoTagEnabled is true, a light LLM pass runs after each
|
|
180
|
+
// autoDream consolidation and extracts 1-3 tags per retained memory
|
|
181
|
+
// (autoTagMaxPerRun bounds how many memories are tagged per run). The tag
|
|
182
|
+
// storage layer (store.setMemoryTags/getMemoryTags + tag: search + mirror
|
|
183
|
+
// `#tag` line) is always available regardless of this flag.
|
|
184
|
+
autoTagEnabled: z.boolean().default(false),
|
|
185
|
+
autoTagMaxPerRun: z.natural().min(1).max(100).default(10),
|
|
186
|
+
// Manual tagging (service.setMemoryTags / memory tools) is on by default;
|
|
187
|
+
// set false to disable the manual write path too.
|
|
188
|
+
manualTagEnabled: z.boolean().default(true),
|
|
189
|
+
|
|
190
|
+
// --- tag-weighted re-rank (v0.6.4) -------------------------------------
|
|
191
|
+
// Opt-in: boost candidates whose tags overlap the query/session tags.
|
|
192
|
+
tagBoostEnabled: z.boolean().default(false),
|
|
193
|
+
tagBoostFactor: z.number().min(1).max(2).default(1.15),
|
|
194
|
+
sessionTagBoostFactor: z.number().min(1).max(2).default(1.08),
|
|
195
|
+
|
|
178
196
|
// --- sleep mode: idle-triggered deep maintenance (v0.4.0) ---------------
|
|
179
197
|
// Opt-in, off by default. Unlike autoDream (threshold-triggered, lightweight)
|
|
180
198
|
// sleep fires when the store has been quiet for sleepIdleMinutes and deep-
|