@modusensus/dsh-mneme 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +239 -207
- package/cordis.patch.yml +15 -15
- package/lib/api.js +277 -245
- package/lib/client.js +461 -461
- package/lib/commands.js +64 -64
- package/lib/config.js +49 -15
- package/lib/dream/clustering.js +118 -0
- package/lib/dream/decisions.js +128 -121
- package/lib/dream.js +428 -209
- package/lib/embedding.js +97 -97
- package/lib/index.js +176 -120
- package/lib/inject.js +47 -47
- package/lib/local-embedder.js +265 -0
- package/lib/mirror.js +131 -131
- package/lib/reranker.js +203 -0
- package/lib/service.js +268 -174
- package/lib/settings.js +142 -142
- package/lib/store.js +409 -310
- package/lib/summarize.js +171 -171
- package/lib/tools.js +230 -241
- package/lib/vector-index.js +106 -0
- package/package.json +7 -3
- package/src/api.js +277 -245
- package/src/commands.js +64 -64
- package/src/config.js +49 -15
- package/src/dream/clustering.js +118 -0
- package/src/dream/decisions.js +128 -121
- package/src/dream.js +428 -209
- package/src/embedding.js +97 -97
- package/src/index.js +176 -120
- package/src/inject.js +47 -47
- package/src/local-embedder.js +265 -0
- package/src/mirror.js +131 -131
- package/src/reranker.js +203 -0
- package/src/service.js +268 -174
- package/src/settings.js +142 -142
- package/src/store.js +409 -310
- package/src/summarize.js +171 -171
- package/src/tools.js +230 -241
- package/src/vector-index.js +106 -0
package/lib/tools.js
CHANGED
|
@@ -1,241 +1,230 @@
|
|
|
1
|
-
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
2
|
-
|
|
3
|
-
const TEXT_OUTPUT = (text) => [{ type: "text", text }];
|
|
4
|
-
|
|
5
|
-
// Wire shape emitted by service.toApiList: shared by memory_search and
|
|
6
|
-
// memory_list so their output schemas always declare every key the runtime
|
|
7
|
-
// value carries (additionalProperties: false would reject undeclared keys).
|
|
8
|
-
const MEMORY_ITEM_SCHEMA = {
|
|
9
|
-
type: "object",
|
|
10
|
-
additionalProperties: false,
|
|
11
|
-
properties: {
|
|
12
|
-
id: { type: "string", required: true },
|
|
13
|
-
type: { type: "string", required: true },
|
|
14
|
-
title: { type: "string", required: true },
|
|
15
|
-
content: { type: "string", required: true },
|
|
16
|
-
tags: { type: "array", items: { type: "string" } },
|
|
17
|
-
importance: { type: "integer", required: true },
|
|
18
|
-
source: { type: "string" },
|
|
19
|
-
created_at: { type: "string", required: true },
|
|
20
|
-
updated_at: { type: "string", required: true }
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export function createTools(ctx, service, config, embedder) {
|
|
25
|
-
const tools = [
|
|
26
|
-
defineTool({
|
|
27
|
-
name: "memory_save",
|
|
28
|
-
description:
|
|
29
|
-
"Persist one memory entry for future sessions (user preferences, project state, decisions). " +
|
|
30
|
-
"Call this when the user states a durable preference, a project decision is made, or a lesson is learned. " +
|
|
31
|
-
"Merges into an existing entry of the same type when the title matches.",
|
|
32
|
-
parameters: {
|
|
33
|
-
type: { type: "string", required: true, enum: ["preference", "project", "decision", "history"], description: "preference=user profile; project=project knowledge/state; decision=key decision; history=conversation summary" },
|
|
34
|
-
title: { type: "string", required: true, description: "Short unique title" },
|
|
35
|
-
content: { type: "string", required: true, description: "Memory body" },
|
|
36
|
-
tags: { type: "array", items: { type: "string" }, description: "Optional tags" },
|
|
37
|
-
importance: { type: "integer", description: "1-5; >= threshold auto-injects into future sessions" },
|
|
38
|
-
source: { type: "string", description: "Optional provenance" }
|
|
39
|
-
},
|
|
40
|
-
output: {
|
|
41
|
-
schema: {
|
|
42
|
-
type: "object",
|
|
43
|
-
additionalProperties: false,
|
|
44
|
-
properties: {
|
|
45
|
-
action: { type: "string", required: true, enum: ["created", "merged"] },
|
|
46
|
-
id: { type: "string", required: true }
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
render: (_args, value) => TEXT_OUTPUT(`memory ${value.action}: ${value.id}`)
|
|
50
|
-
},
|
|
51
|
-
async execute(args) {
|
|
52
|
-
const { action, memory } = service.saveWithDedupe({
|
|
53
|
-
type: args.type,
|
|
54
|
-
title: args.title,
|
|
55
|
-
content: args.content,
|
|
56
|
-
tags: args.tags ?? [],
|
|
57
|
-
importance: args.importance ?? 3,
|
|
58
|
-
source: args.source ?? "tool"
|
|
59
|
-
});
|
|
60
|
-
return { action, id: memory.id };
|
|
61
|
-
}
|
|
62
|
-
}),
|
|
63
|
-
|
|
64
|
-
defineTool({
|
|
65
|
-
name: "memory_search",
|
|
66
|
-
description: "Search the cross-session memory store. Use when you need past context: how a problem was solved, user preferences, project decisions. Substring-matches title/content/tags, and
|
|
67
|
-
parameters: {
|
|
68
|
-
query: { type: "string", required: true, description: "Search text; substring match over title/content/tags" },
|
|
69
|
-
limit: { type: "integer", description: "Max results (default 20)" },
|
|
70
|
-
mode: { type: "string", enum: ["auto", "keyword", "vector"], description: "auto (default) = keyword hits first + vector fill when enabled; keyword = text only; vector = semantic recall first (falls back to keyword)" },
|
|
71
|
-
semantic: { type: "boolean", description: "Shorthand: enable semantic (vector) recall (same as mode=vector when true)" }
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return { memory: { id: memory.id, forgotten: memory.forgotten } };
|
|
232
|
-
}
|
|
233
|
-
})
|
|
234
|
-
];
|
|
235
|
-
|
|
236
|
-
for (const tool of tools) {
|
|
237
|
-
ctx.tools.register(tool);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return tools;
|
|
241
|
-
}
|
|
1
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
2
|
+
|
|
3
|
+
const TEXT_OUTPUT = (text) => [{ type: "text", text }];
|
|
4
|
+
|
|
5
|
+
// Wire shape emitted by service.toApiList: shared by memory_search and
|
|
6
|
+
// memory_list so their output schemas always declare every key the runtime
|
|
7
|
+
// value carries (additionalProperties: false would reject undeclared keys).
|
|
8
|
+
const MEMORY_ITEM_SCHEMA = {
|
|
9
|
+
type: "object",
|
|
10
|
+
additionalProperties: false,
|
|
11
|
+
properties: {
|
|
12
|
+
id: { type: "string", required: true },
|
|
13
|
+
type: { type: "string", required: true },
|
|
14
|
+
title: { type: "string", required: true },
|
|
15
|
+
content: { type: "string", required: true },
|
|
16
|
+
tags: { type: "array", items: { type: "string" } },
|
|
17
|
+
importance: { type: "integer", required: true },
|
|
18
|
+
source: { type: "string" },
|
|
19
|
+
created_at: { type: "string", required: true },
|
|
20
|
+
updated_at: { type: "string", required: true }
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function createTools(ctx, service, config, embedder) {
|
|
25
|
+
const tools = [
|
|
26
|
+
defineTool({
|
|
27
|
+
name: "memory_save",
|
|
28
|
+
description:
|
|
29
|
+
"Persist one memory entry for future sessions (user preferences, project state, decisions). " +
|
|
30
|
+
"Call this when the user states a durable preference, a project decision is made, or a lesson is learned. " +
|
|
31
|
+
"Merges into an existing entry of the same type when the title matches.",
|
|
32
|
+
parameters: {
|
|
33
|
+
type: { type: "string", required: true, enum: ["preference", "project", "decision", "history"], description: "preference=user profile; project=project knowledge/state; decision=key decision; history=conversation summary" },
|
|
34
|
+
title: { type: "string", required: true, description: "Short unique title" },
|
|
35
|
+
content: { type: "string", required: true, description: "Memory body" },
|
|
36
|
+
tags: { type: "array", items: { type: "string" }, description: "Optional tags" },
|
|
37
|
+
importance: { type: "integer", description: "1-5; >= threshold auto-injects into future sessions" },
|
|
38
|
+
source: { type: "string", description: "Optional provenance" }
|
|
39
|
+
},
|
|
40
|
+
output: {
|
|
41
|
+
schema: {
|
|
42
|
+
type: "object",
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
properties: {
|
|
45
|
+
action: { type: "string", required: true, enum: ["created", "merged"] },
|
|
46
|
+
id: { type: "string", required: true }
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
render: (_args, value) => TEXT_OUTPUT(`memory ${value.action}: ${value.id}`)
|
|
50
|
+
},
|
|
51
|
+
async execute(args) {
|
|
52
|
+
const { action, memory } = service.saveWithDedupe({
|
|
53
|
+
type: args.type,
|
|
54
|
+
title: args.title,
|
|
55
|
+
content: args.content,
|
|
56
|
+
tags: args.tags ?? [],
|
|
57
|
+
importance: args.importance ?? 3,
|
|
58
|
+
source: args.source ?? "tool"
|
|
59
|
+
});
|
|
60
|
+
return { action, id: memory.id };
|
|
61
|
+
}
|
|
62
|
+
}),
|
|
63
|
+
|
|
64
|
+
defineTool({
|
|
65
|
+
name: "memory_search",
|
|
66
|
+
description: "Search the cross-session memory store. Use when you need past context: how a problem was solved, user preferences, project decisions. Substring-matches title/content/tags, and augments results with semantic (vector) recall + optional rerank when an embeddings provider is configured. Returns matching entries with source and timestamps.",
|
|
67
|
+
parameters: {
|
|
68
|
+
query: { type: "string", required: true, description: "Search text; substring match over title/content/tags" },
|
|
69
|
+
limit: { type: "integer", description: "Max results (default 20)" },
|
|
70
|
+
mode: { type: "string", enum: ["auto", "keyword", "vector", "hybrid"], description: "auto (default) = keyword hits first + vector fill when enabled; keyword = text only; vector = semantic recall first (falls back to keyword); hybrid = vector leads, keyword fills remaining slots" },
|
|
71
|
+
semantic: { type: "boolean", description: "Shorthand: enable semantic (vector) recall (same as mode=vector when true)" },
|
|
72
|
+
rerank: { type: "boolean", description: "Run cross-encoder rerank over candidates when a local reranker is configured (default true)" }
|
|
73
|
+
},
|
|
74
|
+
output: {
|
|
75
|
+
schema: {
|
|
76
|
+
type: "object",
|
|
77
|
+
additionalProperties: false,
|
|
78
|
+
properties: {
|
|
79
|
+
items: {
|
|
80
|
+
type: "array", required: true,
|
|
81
|
+
items: MEMORY_ITEM_SCHEMA
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
render: (_args, value) => TEXT_OUTPUT(`Found ${value.items.length} memory entr${value.items.length === 1 ? "y" : "ies"}.`)
|
|
86
|
+
},
|
|
87
|
+
async execute(args) {
|
|
88
|
+
const limit = args.limit ?? 20;
|
|
89
|
+
const mode = args.semantic === true && !args.mode ? "vector" : args.mode ?? "auto";
|
|
90
|
+
const rows = await service.searchMemories(args.query, {
|
|
91
|
+
mode,
|
|
92
|
+
topK: limit,
|
|
93
|
+
useRerank: args.rerank !== false
|
|
94
|
+
});
|
|
95
|
+
return { items: service.toApiList(rows) };
|
|
96
|
+
}
|
|
97
|
+
}),
|
|
98
|
+
|
|
99
|
+
defineTool({
|
|
100
|
+
name: "memory_list",
|
|
101
|
+
description: "List memory entries by type, high-importance first, then newest, paginated.",
|
|
102
|
+
parameters: {
|
|
103
|
+
type: { type: "string", enum: ["preference", "project", "decision", "history"], description: "Filter by type; omit for all" },
|
|
104
|
+
limit: { type: "integer", description: "Page size (default 50)" },
|
|
105
|
+
offset: { type: "integer", description: "Page offset (default 0)" }
|
|
106
|
+
},
|
|
107
|
+
output: {
|
|
108
|
+
schema: {
|
|
109
|
+
type: "object",
|
|
110
|
+
additionalProperties: false,
|
|
111
|
+
properties: {
|
|
112
|
+
items: {
|
|
113
|
+
type: "array", required: true,
|
|
114
|
+
items: MEMORY_ITEM_SCHEMA
|
|
115
|
+
},
|
|
116
|
+
total: { type: "integer", required: true }
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
render: (_args, value) => TEXT_OUTPUT(`${value.items.length} memory entries (of ${value.total}).`)
|
|
120
|
+
},
|
|
121
|
+
async execute(args) {
|
|
122
|
+
const rows = service.toApiList(service.list({ type: args.type, limit: args.limit ?? 50, offset: args.offset ?? 0 }));
|
|
123
|
+
return { items: rows, total: service.count(args.type) };
|
|
124
|
+
}
|
|
125
|
+
}),
|
|
126
|
+
|
|
127
|
+
defineTool({
|
|
128
|
+
name: "memory_update",
|
|
129
|
+
description: "Modify an existing memory entry (title, content, type, tags, importance).",
|
|
130
|
+
parameters: {
|
|
131
|
+
id: { type: "string", required: true, description: "Memory id" },
|
|
132
|
+
title: { type: "string" },
|
|
133
|
+
content: { type: "string" },
|
|
134
|
+
type: { type: "string", enum: ["preference", "project", "decision", "history"] },
|
|
135
|
+
tags: { type: "array", items: { type: "string" } },
|
|
136
|
+
importance: { type: "integer", description: "1-5" }
|
|
137
|
+
},
|
|
138
|
+
output: {
|
|
139
|
+
schema: {
|
|
140
|
+
type: "object",
|
|
141
|
+
additionalProperties: false,
|
|
142
|
+
properties: {
|
|
143
|
+
memory: {
|
|
144
|
+
type: "object",
|
|
145
|
+
additionalProperties: false,
|
|
146
|
+
properties: {
|
|
147
|
+
id: { type: "string", required: true },
|
|
148
|
+
title: { type: "string", required: true },
|
|
149
|
+
content: { type: "string", required: true }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
render: (_args, value) => TEXT_OUTPUT(`Updated memory ${value.memory.id}: ${value.memory.title}`)
|
|
155
|
+
},
|
|
156
|
+
async execute(args) {
|
|
157
|
+
const memory = service.update(args.id, {
|
|
158
|
+
title: args.title,
|
|
159
|
+
content: args.content,
|
|
160
|
+
type: args.type,
|
|
161
|
+
tags: args.tags,
|
|
162
|
+
importance: args.importance
|
|
163
|
+
});
|
|
164
|
+
return { memory: { id: memory.id, title: memory.title, content: memory.content } };
|
|
165
|
+
}
|
|
166
|
+
}),
|
|
167
|
+
|
|
168
|
+
defineTool({
|
|
169
|
+
name: "memory_delete",
|
|
170
|
+
description: "Permanently delete a memory entry.",
|
|
171
|
+
parameters: {
|
|
172
|
+
id: { type: "string", required: true }
|
|
173
|
+
},
|
|
174
|
+
output: {
|
|
175
|
+
schema: {
|
|
176
|
+
type: "object",
|
|
177
|
+
additionalProperties: false,
|
|
178
|
+
properties: { deleted: { type: "boolean", required: true } }
|
|
179
|
+
},
|
|
180
|
+
render: (_args, value) => TEXT_OUTPUT(value.deleted ? "Memory deleted." : "Memory not found.")
|
|
181
|
+
},
|
|
182
|
+
async execute(args) {
|
|
183
|
+
const existed = service.getById(args.id) !== undefined;
|
|
184
|
+
if (existed) service.remove(args.id);
|
|
185
|
+
return { deleted: existed };
|
|
186
|
+
}
|
|
187
|
+
}),
|
|
188
|
+
|
|
189
|
+
defineTool({
|
|
190
|
+
name: "memory_forget",
|
|
191
|
+
description:
|
|
192
|
+
"Stop a memory from being auto-injected and from appearing in searches and lists without deleting it. " +
|
|
193
|
+
"The entry stays in storage; pass forgotten: false to restore it.",
|
|
194
|
+
parameters: {
|
|
195
|
+
id: { type: "string", required: true },
|
|
196
|
+
forgotten: { type: "boolean", description: "Suppress (true, default) or restore (false) the entry's visibility" }
|
|
197
|
+
},
|
|
198
|
+
output: {
|
|
199
|
+
schema: {
|
|
200
|
+
type: "object",
|
|
201
|
+
additionalProperties: false,
|
|
202
|
+
properties: {
|
|
203
|
+
memory: {
|
|
204
|
+
type: "object",
|
|
205
|
+
additionalProperties: false,
|
|
206
|
+
properties: {
|
|
207
|
+
id: { type: "string", required: true },
|
|
208
|
+
forgotten: { type: "boolean", required: true }
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
render: (_args, value) => TEXT_OUTPUT(`Memory ${value.memory.id} injection ${value.memory.forgotten ? "suppressed" : "restored"}.`)
|
|
214
|
+
},
|
|
215
|
+
async execute(args) {
|
|
216
|
+
if (service.getById(args.id) === undefined) {
|
|
217
|
+
throw new Error("memory not found");
|
|
218
|
+
}
|
|
219
|
+
const memory = service.setForget(args.id, args.forgotten ?? true);
|
|
220
|
+
return { memory: { id: memory.id, forgotten: memory.forgotten } };
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
for (const tool of tools) {
|
|
226
|
+
ctx.tools.register(tool);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return tools;
|
|
230
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Thin vector-index layer over the store's embedding column. Keeps the
|
|
2
|
+
// current model fingerprint + stats in a dedicated `vector_meta` table so the
|
|
3
|
+
// index can detect drift (embeddings generated by a different model) and the
|
|
4
|
+
// Web panel can report index health. The heavy lifting (cosine search) lives
|
|
5
|
+
// in store.searchVector; this module owns the index lifecycle.
|
|
6
|
+
|
|
7
|
+
const META_SCHEMA = `
|
|
8
|
+
CREATE TABLE IF NOT EXISTS vector_meta (
|
|
9
|
+
key TEXT PRIMARY KEY,
|
|
10
|
+
value TEXT NOT NULL
|
|
11
|
+
);
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
export function createVectorIndex({ store, logger }) {
|
|
15
|
+
const db = store.db;
|
|
16
|
+
db.exec(META_SCHEMA);
|
|
17
|
+
|
|
18
|
+
function getMeta(key) {
|
|
19
|
+
const row = db.prepare("SELECT value FROM vector_meta WHERE key = ?").get(key);
|
|
20
|
+
return row?.value ?? undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function setMeta(key, value) {
|
|
24
|
+
db.prepare(
|
|
25
|
+
`INSERT INTO vector_meta (key, value) VALUES (?, ?)
|
|
26
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value`
|
|
27
|
+
).run(key, String(value));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
/** Record the model that generated the current vectors. */
|
|
32
|
+
markModel(modelHash, dimension) {
|
|
33
|
+
setMeta("model_hash", modelHash);
|
|
34
|
+
setMeta("dimension", String(dimension ?? ""));
|
|
35
|
+
setMeta("updated_at", new Date().toISOString());
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
modelHash() {
|
|
39
|
+
return getMeta("model_hash");
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
dimension() {
|
|
43
|
+
const raw = getMeta("dimension");
|
|
44
|
+
return raw && Number.isInteger(Number(raw)) ? Number(raw) : undefined;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
/** Store (or clear with null vector) one memory's embedding. */
|
|
48
|
+
saveEmbedding(id, vector) {
|
|
49
|
+
store.setEmbedding(id, vector);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/** Drop one memory's embedding. */
|
|
53
|
+
deleteEmbedding(id) {
|
|
54
|
+
store.setEmbedding(id, null);
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
/** Read one memory's cached vector; undefined when none/parse failure. */
|
|
58
|
+
getEmbedding(id) {
|
|
59
|
+
const row = db.prepare("SELECT embedding FROM memories WHERE id = ?").get(id);
|
|
60
|
+
if (!row?.embedding) return undefined;
|
|
61
|
+
try {
|
|
62
|
+
const v = JSON.parse(row.embedding);
|
|
63
|
+
return Array.isArray(v) && v.length ? v : undefined;
|
|
64
|
+
} catch {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
/** Cosine search over embedded rows; results carry a 0..1 score. */
|
|
70
|
+
search(queryVector, { limit = 20, threshold = 0.65 } = {}) {
|
|
71
|
+
return store.searchVector(queryVector, { limit, threshold });
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
/** Re-embed every row missing an embedding. Returns indexed count. */
|
|
75
|
+
async rebuildIndex(embedder, { limit = 1000 } = {}) {
|
|
76
|
+
if (!embedder || typeof embedder.embed !== "function") return { indexed: 0, skipped: 0 };
|
|
77
|
+
const rows = store.needsEmbedding(limit);
|
|
78
|
+
let indexed = 0;
|
|
79
|
+
for (const row of rows) {
|
|
80
|
+
try {
|
|
81
|
+
const text = [row.title, row.content].filter(Boolean).join("\n");
|
|
82
|
+
const vector = await embedder.embedSingle(text);
|
|
83
|
+
if (vector && vector.length) {
|
|
84
|
+
store.setEmbedding(row.id, vector);
|
|
85
|
+
indexed++;
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
logger?.warn?.(`[dsh-mneme] rebuild embed failed for ${row.id}: ${String(error)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (indexed > 0 && embedder.modelHash) this.markModel(embedder.modelHash, embedder.dimension);
|
|
92
|
+
return { indexed, skipped: rows.length - indexed };
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/** Index stats for the Web panel / debugging. */
|
|
96
|
+
getStats() {
|
|
97
|
+
return {
|
|
98
|
+
modelHash: getMeta("model_hash"),
|
|
99
|
+
dimension: this.dimension(),
|
|
100
|
+
updatedAt: getMeta("updated_at"),
|
|
101
|
+
embeddedCount: store.embeddedCount(),
|
|
102
|
+
totalCount: store.count()
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
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, 6 model tools, automatic injection, session summarization, user profile/rules, custom slash commands, vector (semantic) search, and a Web GUI panel",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|
|
@@ -52,7 +52,11 @@
|
|
|
52
52
|
"scripts": {
|
|
53
53
|
"sync": "node scripts/sync-lib.js",
|
|
54
54
|
"prepack": "npm run sync",
|
|
55
|
-
"test": "node --test
|
|
56
|
-
"e2e": "node scripts/e2e-dsh.js"
|
|
55
|
+
"test": "node --test test/*.test.js",
|
|
56
|
+
"e2e": "node scripts/e2e-dsh.js",
|
|
57
|
+
"stress": "node scripts/stress-dsh.js"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@huggingface/transformers": "^4.2.0"
|
|
57
61
|
}
|
|
58
62
|
}
|