@modusensus/dsh-mneme 0.6.7 → 0.6.8

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 (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +463 -463
  3. package/cordis.patch.yml +15 -15
  4. package/lib/api.js +783 -783
  5. package/lib/client.js +1757 -1757
  6. package/lib/commands.js +64 -64
  7. package/lib/config.js +288 -288
  8. package/lib/dream/clustering.js +118 -118
  9. package/lib/dream/decisions.js +439 -439
  10. package/lib/dream/sleep.js +561 -554
  11. package/lib/dream/tag-extractor.js +156 -156
  12. package/lib/dream.js +935 -929
  13. package/lib/embedding.js +154 -154
  14. package/lib/entities/extractor.js +242 -242
  15. package/lib/hot-memory.js +53 -53
  16. package/lib/index.js +361 -361
  17. package/lib/inject.js +208 -208
  18. package/lib/local-embedder.js +282 -282
  19. package/lib/mirror.js +170 -170
  20. package/lib/parser/tag.js +59 -59
  21. package/lib/parser/wiki-link.js +38 -38
  22. package/lib/quality-filter.js +123 -123
  23. package/lib/reranker.js +218 -218
  24. package/lib/search/adaptive.js +22 -22
  25. package/lib/search/bm25.js +96 -96
  26. package/lib/search/tag-boost.js +61 -61
  27. package/lib/service.js +1726 -1726
  28. package/lib/settings.js +172 -172
  29. package/lib/store.js +2238 -2238
  30. package/lib/summarize.js +236 -236
  31. package/lib/tools.js +290 -290
  32. package/lib/vector-index.js +116 -116
  33. package/package.json +80 -80
  34. package/scripts/benchmark-embed.js +201 -201
  35. package/scripts/benchmark-recall.js +133 -133
  36. package/scripts/benchmark-rerank.js +166 -166
  37. package/scripts/e2e-dsh.js +218 -218
  38. package/scripts/stress-dsh.js +255 -255
  39. package/scripts/sync-lib.js +52 -52
  40. package/src/api.js +783 -783
  41. package/src/commands.js +64 -64
  42. package/src/config.js +288 -288
  43. package/src/dream/clustering.js +118 -118
  44. package/src/dream/decisions.js +439 -439
  45. package/src/dream/sleep.js +561 -554
  46. package/src/dream/tag-extractor.js +156 -156
  47. package/src/dream.js +935 -929
  48. package/src/embedding.js +154 -154
  49. package/src/entities/extractor.js +242 -242
  50. package/src/hot-memory.js +53 -53
  51. package/src/index.js +361 -361
  52. package/src/inject.js +208 -208
  53. package/src/local-embedder.js +282 -282
  54. package/src/mirror.js +170 -170
  55. package/src/parser/tag.js +59 -59
  56. package/src/parser/wiki-link.js +38 -38
  57. package/src/quality-filter.js +123 -123
  58. package/src/reranker.js +218 -218
  59. package/src/search/adaptive.js +22 -22
  60. package/src/search/bm25.js +96 -96
  61. package/src/search/tag-boost.js +61 -61
  62. package/src/service.js +1726 -1726
  63. package/src/settings.js +172 -172
  64. package/src/store.js +2238 -2238
  65. package/src/summarize.js +236 -236
  66. package/src/tools.js +290 -290
  67. package/src/vector-index.js +116 -116
  68. package/test/api.test.js +594 -594
  69. package/test/audit.test.js +448 -448
  70. package/test/benchmark.test.js +35 -35
  71. package/test/boundary-v0625.test.js +82 -82
  72. package/test/client.test.js +368 -368
  73. package/test/clustering.test.js +100 -100
  74. package/test/commands.test.js +69 -69
  75. package/test/config.test.js +50 -50
  76. package/test/conflict-freeze.test.js +290 -290
  77. package/test/directory.test.js +134 -134
  78. package/test/dream.test.js +903 -901
  79. package/test/entities.test.js +522 -522
  80. package/test/epistemic.test.js +298 -298
  81. package/test/fnew-0112.test.js +311 -311
  82. package/test/fnew-03.test.js +422 -422
  83. package/test/graph-api.test.js +175 -175
  84. package/test/helpers/dream-mock.js +82 -82
  85. package/test/hot-memory.test.js +174 -174
  86. package/test/inject.test.js +103 -103
  87. package/test/llm-audit.test.js +279 -279
  88. package/test/local-embedder.test.js +227 -227
  89. package/test/mirror-dirty.test.js +424 -424
  90. package/test/mirror-edit-digest.test.js +187 -187
  91. package/test/mirror-generation.test.js +499 -499
  92. package/test/mirror.test.js +249 -249
  93. package/test/normalize-decisions.test.js +120 -120
  94. package/test/peer-blockers.test.js +190 -190
  95. package/test/policy-epoch.test.js +259 -259
  96. package/test/provenance.test.js +103 -103
  97. package/test/quality-filter.test.js +118 -118
  98. package/test/reasoning-effort.test.js +199 -199
  99. package/test/recall-evals.test.js +235 -235
  100. package/test/recall-layer.test.js +315 -315
  101. package/test/receipt-chain.test.js +451 -451
  102. package/test/reflection.test.js +226 -226
  103. package/test/reranker.test.js +240 -240
  104. package/test/search-fusion.test.js +90 -90
  105. package/test/semantic.test.js +124 -124
  106. package/test/service-search.test.js +199 -199
  107. package/test/service.test.js +435 -435
  108. package/test/settings.test.js +118 -118
  109. package/test/sleep.test.js +365 -365
  110. package/test/store.test.js +436 -436
  111. package/test/stress.test.js +209 -209
  112. package/test/summarize.test.js +191 -191
  113. package/test/tag-boost.test.js +125 -125
  114. package/test/tag.test.js +312 -312
  115. package/test/tools.test.js +285 -285
  116. package/test/vector-index.test.js +221 -221
  117. package/test/wiki-link.test.js +332 -332
package/src/tools.js CHANGED
@@ -1,290 +1,290 @@
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
- session_id: { type: "string" },
20
- created_at: { type: "string", required: true },
21
- updated_at: { type: "string", required: true }
22
- }
23
- };
24
-
25
- export function createTools(ctx, service, config, embedder) {
26
- const tools = [
27
- defineTool({
28
- name: "memory_save",
29
- description:
30
- "Persist one memory entry for future sessions (user preferences, project state, decisions). " +
31
- "Call this when the user states a durable preference, a project decision is made, or a lesson is learned. " +
32
- "Merges into an existing entry of the same type when the title matches.",
33
- parameters: {
34
- type: { type: "string", required: true, enum: ["preference", "project", "decision", "history"], description: "preference=user profile; project=project knowledge/state; decision=key decision; history=conversation summary" },
35
- title: { type: "string", required: true, description: "Short unique title" },
36
- content: { type: "string", required: true, description: "Memory body" },
37
- tags: { type: "array", items: { type: "string" }, description: "Optional tags" },
38
- importance: { type: "integer", description: "1-5; >= threshold auto-injects into future sessions" },
39
- source: { type: "string", description: "Optional provenance" }
40
- },
41
- output: {
42
- schema: {
43
- type: "object",
44
- additionalProperties: false,
45
- properties: {
46
- action: { type: "string", required: true, enum: ["created", "merged"] },
47
- id: { type: "string", required: true }
48
- }
49
- },
50
- render: (_args, value) => TEXT_OUTPUT(`memory ${value.action}: ${value.id}`)
51
- },
52
- async execute(args, exec) {
53
- const { action, memory } = service.saveWithDedupe({
54
- type: args.type,
55
- title: args.title,
56
- content: args.content,
57
- tags: args.tags ?? [],
58
- importance: args.importance ?? 3,
59
- source: args.source ?? "tool",
60
- // Provenance: the session that issued the tool call. exec.agent is
61
- // set by the agent loop (undefined in unit tests / direct calls) —
62
- // absent a session, session_id stays null rather than fabricating one.
63
- session_id: exec?.agent?.session?.id ?? undefined
64
- });
65
- return { action, id: memory.id };
66
- }
67
- }),
68
-
69
- defineTool({
70
- name: "memory_search",
71
- 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.",
72
- parameters: {
73
- query: { type: "string", required: true, description: "Search text; substring match over title/content/tags" },
74
- limit: { type: "integer", description: "Max results (default 20)" },
75
- 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" },
76
- semantic: { type: "boolean", description: "Shorthand: enable semantic (vector) recall (same as mode=vector when true)" },
77
- rerank: { type: "boolean", description: "Run cross-encoder rerank over candidates when a local reranker is configured (default true)" }
78
- },
79
- output: {
80
- schema: {
81
- type: "object",
82
- additionalProperties: false,
83
- properties: {
84
- items: {
85
- type: "array", required: true,
86
- items: MEMORY_ITEM_SCHEMA
87
- }
88
- }
89
- },
90
- render: (_args, value) => TEXT_OUTPUT(`Found ${value.items.length} memory entr${value.items.length === 1 ? "y" : "ies"}.`)
91
- },
92
- async execute(args) {
93
- const limit = args.limit ?? 20;
94
- const mode = args.semantic === true && !args.mode ? "vector" : args.mode ?? "auto";
95
- const rows = await service.searchMemories(args.query, {
96
- mode,
97
- topK: limit,
98
- useRerank: args.rerank !== false
99
- });
100
- return { items: service.toApiList(rows) };
101
- }
102
- }),
103
-
104
- defineTool({
105
- name: "memory_list",
106
- description: "List memory entries by type, high-importance first, then newest, paginated. Set include_archived=true to also list archived (hidden) entries so they can be located and restored.",
107
- parameters: {
108
- type: { type: "string", enum: ["preference", "project", "decision", "history"], description: "Filter by type; omit for all" },
109
- limit: { type: "integer", description: "Page size (default 50)" },
110
- offset: { type: "integer", description: "Page offset (default 0)" },
111
- include_archived: { type: "boolean", description: "Include archived (hidden) entries so they can be found and restored (default false)" }
112
- },
113
- output: {
114
- schema: {
115
- type: "object",
116
- additionalProperties: false,
117
- properties: {
118
- items: {
119
- type: "array", required: true,
120
- items: MEMORY_ITEM_SCHEMA
121
- },
122
- total: { type: "integer", required: true }
123
- }
124
- },
125
- render: (_args, value) => TEXT_OUTPUT(`${value.items.length} memory entries (of ${value.total}).`)
126
- },
127
- async execute(args) {
128
- const includeArchived = args.include_archived === true;
129
- const rows = service.toApiList(service.list({
130
- type: args.type,
131
- limit: args.limit ?? 50,
132
- offset: args.offset ?? 0,
133
- includeArchived
134
- }));
135
- return { items: rows, total: service.count(args.type, { includeArchived }) };
136
- }
137
- }),
138
-
139
- defineTool({
140
- name: "memory_update",
141
- description: "Modify an existing memory entry (title, content, type, tags, importance).",
142
- parameters: {
143
- id: { type: "string", required: true, description: "Memory id" },
144
- title: { type: "string" },
145
- content: { type: "string" },
146
- type: { type: "string", enum: ["preference", "project", "decision", "history"] },
147
- tags: { type: "array", items: { type: "string" } },
148
- importance: { type: "integer", description: "1-5" },
149
- reason: { type: "string", description: "Optional context for the correction (what the user actually said/wanted), recorded for reflection" }
150
- },
151
- output: {
152
- schema: {
153
- type: "object",
154
- additionalProperties: false,
155
- properties: {
156
- memory: {
157
- type: "object",
158
- additionalProperties: false,
159
- properties: {
160
- id: { type: "string", required: true },
161
- title: { type: "string", required: true },
162
- content: { type: "string", required: true }
163
- }
164
- }
165
- }
166
- },
167
- render: (_args, value) => TEXT_OUTPUT(`Updated memory ${value.memory.id}: ${value.memory.title}`)
168
- },
169
- async execute(args) {
170
- const memory = service.update(args.id, {
171
- title: args.title,
172
- content: args.content,
173
- type: args.type,
174
- tags: args.tags,
175
- importance: args.importance
176
- }, { query: args.reason });
177
- return { memory: { id: memory.id, title: memory.title, content: memory.content } };
178
- }
179
- }),
180
-
181
- defineTool({
182
- name: "memory_delete",
183
- description: "Permanently delete a memory entry. Pass id for exact delete, or query to delete the single best-matching entry by text — lets the agent honor 'delete the memory about X' without a prior list/search round trip.",
184
- parameters: {
185
- id: { type: "string", description: "Exact memory id to delete (from memory_list/memory_search output)" },
186
- query: { type: "string", description: "Delete the best-matching entry for this text (searches title/content/tags; uses hybrid recall when an embedder is configured)" }
187
- },
188
- output: {
189
- schema: {
190
- type: "object",
191
- additionalProperties: false,
192
- properties: { deleted: { type: "boolean", required: true } }
193
- },
194
- render: (_args, value) => TEXT_OUTPUT(value.deleted ? "Memory deleted." : "Memory not found.")
195
- },
196
- async execute(args) {
197
- if (args.id) {
198
- const existed = service.getById(args.id) !== undefined;
199
- if (existed) service.remove(args.id);
200
- return { deleted: existed };
201
- }
202
- if (args.query) {
203
- const [best] = await service.searchMemories(args.query, { mode: "auto", topK: 1, useRerank: true });
204
- if (best) {
205
- service.remove(best.id);
206
- return { deleted: true };
207
- }
208
- }
209
- return { deleted: false };
210
- }
211
- }),
212
-
213
- defineTool({
214
- name: "memory_forget",
215
- description:
216
- "Stop a memory from being auto-injected and from appearing in searches and lists without deleting it. " +
217
- "The entry stays in storage; pass forgotten: false to restore it.",
218
- parameters: {
219
- id: { type: "string", required: true },
220
- forgotten: { type: "boolean", description: "Suppress (true, default) or restore (false) the entry's visibility" }
221
- },
222
- output: {
223
- schema: {
224
- type: "object",
225
- additionalProperties: false,
226
- properties: {
227
- memory: {
228
- type: "object",
229
- additionalProperties: false,
230
- properties: {
231
- id: { type: "string", required: true },
232
- forgotten: { type: "boolean", required: true }
233
- }
234
- }
235
- }
236
- },
237
- render: (_args, value) => TEXT_OUTPUT(`Memory ${value.memory.id} injection ${value.memory.forgotten ? "suppressed" : "restored"}.`)
238
- },
239
- async execute(args) {
240
- if (service.getById(args.id) === undefined) {
241
- throw new Error("memory not found");
242
- }
243
- const memory = service.setForget(args.id, args.forgotten ?? true);
244
- return { memory: { id: memory.id, forgotten: memory.forgotten } };
245
- }
246
- }),
247
-
248
- defineTool({
249
- name: "memory_archive",
250
- description:
251
- "Archive a memory (hide it from active lists, search, injection and dream consolidation) or restore it. " +
252
- "Archived entries stay in storage and are recoverable: pass archived=false to restore, and use memory_list with " +
253
- "include_archived=true to find archived entries.",
254
- parameters: {
255
- id: { type: "string", required: true, description: "Memory id" },
256
- archived: { type: "boolean", description: "Archive (true, default) or restore (false) the entry" }
257
- },
258
- output: {
259
- schema: {
260
- type: "object",
261
- additionalProperties: false,
262
- properties: {
263
- memory: {
264
- type: "object",
265
- additionalProperties: false,
266
- properties: {
267
- id: { type: "string", required: true },
268
- archived: { type: "boolean", required: true }
269
- }
270
- }
271
- }
272
- },
273
- render: (_args, value) => TEXT_OUTPUT(`Memory ${value.memory.id} ${value.memory.archived ? "archived" : "restored"}.`)
274
- },
275
- async execute(args) {
276
- if (service.getById(args.id) === undefined) {
277
- throw new Error("memory not found");
278
- }
279
- const memory = service.setArchived(args.id, args.archived ?? true);
280
- return { memory: { id: memory.id, archived: memory.archived } };
281
- }
282
- })
283
- ];
284
-
285
- for (const tool of tools) {
286
- ctx.tools.register(tool);
287
- }
288
-
289
- return tools;
290
- }
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
+ session_id: { type: "string" },
20
+ created_at: { type: "string", required: true },
21
+ updated_at: { type: "string", required: true }
22
+ }
23
+ };
24
+
25
+ export function createTools(ctx, service, config, embedder) {
26
+ const tools = [
27
+ defineTool({
28
+ name: "memory_save",
29
+ description:
30
+ "Persist one memory entry for future sessions (user preferences, project state, decisions). " +
31
+ "Call this when the user states a durable preference, a project decision is made, or a lesson is learned. " +
32
+ "Merges into an existing entry of the same type when the title matches.",
33
+ parameters: {
34
+ type: { type: "string", required: true, enum: ["preference", "project", "decision", "history"], description: "preference=user profile; project=project knowledge/state; decision=key decision; history=conversation summary" },
35
+ title: { type: "string", required: true, description: "Short unique title" },
36
+ content: { type: "string", required: true, description: "Memory body" },
37
+ tags: { type: "array", items: { type: "string" }, description: "Optional tags" },
38
+ importance: { type: "integer", description: "1-5; >= threshold auto-injects into future sessions" },
39
+ source: { type: "string", description: "Optional provenance" }
40
+ },
41
+ output: {
42
+ schema: {
43
+ type: "object",
44
+ additionalProperties: false,
45
+ properties: {
46
+ action: { type: "string", required: true, enum: ["created", "merged"] },
47
+ id: { type: "string", required: true }
48
+ }
49
+ },
50
+ render: (_args, value) => TEXT_OUTPUT(`memory ${value.action}: ${value.id}`)
51
+ },
52
+ async execute(args, exec) {
53
+ const { action, memory } = service.saveWithDedupe({
54
+ type: args.type,
55
+ title: args.title,
56
+ content: args.content,
57
+ tags: args.tags ?? [],
58
+ importance: args.importance ?? 3,
59
+ source: args.source ?? "tool",
60
+ // Provenance: the session that issued the tool call. exec.agent is
61
+ // set by the agent loop (undefined in unit tests / direct calls) —
62
+ // absent a session, session_id stays null rather than fabricating one.
63
+ session_id: exec?.agent?.session?.id ?? undefined
64
+ });
65
+ return { action, id: memory.id };
66
+ }
67
+ }),
68
+
69
+ defineTool({
70
+ name: "memory_search",
71
+ 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.",
72
+ parameters: {
73
+ query: { type: "string", required: true, description: "Search text; substring match over title/content/tags" },
74
+ limit: { type: "integer", description: "Max results (default 20)" },
75
+ 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" },
76
+ semantic: { type: "boolean", description: "Shorthand: enable semantic (vector) recall (same as mode=vector when true)" },
77
+ rerank: { type: "boolean", description: "Run cross-encoder rerank over candidates when a local reranker is configured (default true)" }
78
+ },
79
+ output: {
80
+ schema: {
81
+ type: "object",
82
+ additionalProperties: false,
83
+ properties: {
84
+ items: {
85
+ type: "array", required: true,
86
+ items: MEMORY_ITEM_SCHEMA
87
+ }
88
+ }
89
+ },
90
+ render: (_args, value) => TEXT_OUTPUT(`Found ${value.items.length} memory entr${value.items.length === 1 ? "y" : "ies"}.`)
91
+ },
92
+ async execute(args) {
93
+ const limit = args.limit ?? 20;
94
+ const mode = args.semantic === true && !args.mode ? "vector" : args.mode ?? "auto";
95
+ const rows = await service.searchMemories(args.query, {
96
+ mode,
97
+ topK: limit,
98
+ useRerank: args.rerank !== false
99
+ });
100
+ return { items: service.toApiList(rows) };
101
+ }
102
+ }),
103
+
104
+ defineTool({
105
+ name: "memory_list",
106
+ description: "List memory entries by type, high-importance first, then newest, paginated. Set include_archived=true to also list archived (hidden) entries so they can be located and restored.",
107
+ parameters: {
108
+ type: { type: "string", enum: ["preference", "project", "decision", "history"], description: "Filter by type; omit for all" },
109
+ limit: { type: "integer", description: "Page size (default 50)" },
110
+ offset: { type: "integer", description: "Page offset (default 0)" },
111
+ include_archived: { type: "boolean", description: "Include archived (hidden) entries so they can be found and restored (default false)" }
112
+ },
113
+ output: {
114
+ schema: {
115
+ type: "object",
116
+ additionalProperties: false,
117
+ properties: {
118
+ items: {
119
+ type: "array", required: true,
120
+ items: MEMORY_ITEM_SCHEMA
121
+ },
122
+ total: { type: "integer", required: true }
123
+ }
124
+ },
125
+ render: (_args, value) => TEXT_OUTPUT(`${value.items.length} memory entries (of ${value.total}).`)
126
+ },
127
+ async execute(args) {
128
+ const includeArchived = args.include_archived === true;
129
+ const rows = service.toApiList(service.list({
130
+ type: args.type,
131
+ limit: args.limit ?? 50,
132
+ offset: args.offset ?? 0,
133
+ includeArchived
134
+ }));
135
+ return { items: rows, total: service.count(args.type, { includeArchived }) };
136
+ }
137
+ }),
138
+
139
+ defineTool({
140
+ name: "memory_update",
141
+ description: "Modify an existing memory entry (title, content, type, tags, importance).",
142
+ parameters: {
143
+ id: { type: "string", required: true, description: "Memory id" },
144
+ title: { type: "string" },
145
+ content: { type: "string" },
146
+ type: { type: "string", enum: ["preference", "project", "decision", "history"] },
147
+ tags: { type: "array", items: { type: "string" } },
148
+ importance: { type: "integer", description: "1-5" },
149
+ reason: { type: "string", description: "Optional context for the correction (what the user actually said/wanted), recorded for reflection" }
150
+ },
151
+ output: {
152
+ schema: {
153
+ type: "object",
154
+ additionalProperties: false,
155
+ properties: {
156
+ memory: {
157
+ type: "object",
158
+ additionalProperties: false,
159
+ properties: {
160
+ id: { type: "string", required: true },
161
+ title: { type: "string", required: true },
162
+ content: { type: "string", required: true }
163
+ }
164
+ }
165
+ }
166
+ },
167
+ render: (_args, value) => TEXT_OUTPUT(`Updated memory ${value.memory.id}: ${value.memory.title}`)
168
+ },
169
+ async execute(args) {
170
+ const memory = service.update(args.id, {
171
+ title: args.title,
172
+ content: args.content,
173
+ type: args.type,
174
+ tags: args.tags,
175
+ importance: args.importance
176
+ }, { query: args.reason });
177
+ return { memory: { id: memory.id, title: memory.title, content: memory.content } };
178
+ }
179
+ }),
180
+
181
+ defineTool({
182
+ name: "memory_delete",
183
+ description: "Permanently delete a memory entry. Pass id for exact delete, or query to delete the single best-matching entry by text — lets the agent honor 'delete the memory about X' without a prior list/search round trip.",
184
+ parameters: {
185
+ id: { type: "string", description: "Exact memory id to delete (from memory_list/memory_search output)" },
186
+ query: { type: "string", description: "Delete the best-matching entry for this text (searches title/content/tags; uses hybrid recall when an embedder is configured)" }
187
+ },
188
+ output: {
189
+ schema: {
190
+ type: "object",
191
+ additionalProperties: false,
192
+ properties: { deleted: { type: "boolean", required: true } }
193
+ },
194
+ render: (_args, value) => TEXT_OUTPUT(value.deleted ? "Memory deleted." : "Memory not found.")
195
+ },
196
+ async execute(args) {
197
+ if (args.id) {
198
+ const existed = service.getById(args.id) !== undefined;
199
+ if (existed) service.remove(args.id);
200
+ return { deleted: existed };
201
+ }
202
+ if (args.query) {
203
+ const [best] = await service.searchMemories(args.query, { mode: "auto", topK: 1, useRerank: true });
204
+ if (best) {
205
+ service.remove(best.id);
206
+ return { deleted: true };
207
+ }
208
+ }
209
+ return { deleted: false };
210
+ }
211
+ }),
212
+
213
+ defineTool({
214
+ name: "memory_forget",
215
+ description:
216
+ "Stop a memory from being auto-injected and from appearing in searches and lists without deleting it. " +
217
+ "The entry stays in storage; pass forgotten: false to restore it.",
218
+ parameters: {
219
+ id: { type: "string", required: true },
220
+ forgotten: { type: "boolean", description: "Suppress (true, default) or restore (false) the entry's visibility" }
221
+ },
222
+ output: {
223
+ schema: {
224
+ type: "object",
225
+ additionalProperties: false,
226
+ properties: {
227
+ memory: {
228
+ type: "object",
229
+ additionalProperties: false,
230
+ properties: {
231
+ id: { type: "string", required: true },
232
+ forgotten: { type: "boolean", required: true }
233
+ }
234
+ }
235
+ }
236
+ },
237
+ render: (_args, value) => TEXT_OUTPUT(`Memory ${value.memory.id} injection ${value.memory.forgotten ? "suppressed" : "restored"}.`)
238
+ },
239
+ async execute(args) {
240
+ if (service.getById(args.id) === undefined) {
241
+ throw new Error("memory not found");
242
+ }
243
+ const memory = service.setForget(args.id, args.forgotten ?? true);
244
+ return { memory: { id: memory.id, forgotten: memory.forgotten } };
245
+ }
246
+ }),
247
+
248
+ defineTool({
249
+ name: "memory_archive",
250
+ description:
251
+ "Archive a memory (hide it from active lists, search, injection and dream consolidation) or restore it. " +
252
+ "Archived entries stay in storage and are recoverable: pass archived=false to restore, and use memory_list with " +
253
+ "include_archived=true to find archived entries.",
254
+ parameters: {
255
+ id: { type: "string", required: true, description: "Memory id" },
256
+ archived: { type: "boolean", description: "Archive (true, default) or restore (false) the entry" }
257
+ },
258
+ output: {
259
+ schema: {
260
+ type: "object",
261
+ additionalProperties: false,
262
+ properties: {
263
+ memory: {
264
+ type: "object",
265
+ additionalProperties: false,
266
+ properties: {
267
+ id: { type: "string", required: true },
268
+ archived: { type: "boolean", required: true }
269
+ }
270
+ }
271
+ }
272
+ },
273
+ render: (_args, value) => TEXT_OUTPUT(`Memory ${value.memory.id} ${value.memory.archived ? "archived" : "restored"}.`)
274
+ },
275
+ async execute(args) {
276
+ if (service.getById(args.id) === undefined) {
277
+ throw new Error("memory not found");
278
+ }
279
+ const memory = service.setArchived(args.id, args.archived ?? true);
280
+ return { memory: { id: memory.id, archived: memory.archived } };
281
+ }
282
+ })
283
+ ];
284
+
285
+ for (const tool of tools) {
286
+ ctx.tools.register(tool);
287
+ }
288
+
289
+ return tools;
290
+ }