@framers/agentos 0.1.101 → 0.1.103
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 +16 -0
- package/dist/api/agency.js +1 -1
- package/dist/api/agency.js.map +1 -1
- package/dist/api/strategies/graph.d.ts.map +1 -1
- package/dist/api/strategies/graph.js +1 -0
- package/dist/api/strategies/graph.js.map +1 -1
- package/dist/api/strategies/sequential.d.ts.map +1 -1
- package/dist/api/strategies/sequential.js +1 -0
- package/dist/api/strategies/sequential.js.map +1 -1
- package/dist/memory/config.d.ts +39 -0
- package/dist/memory/config.d.ts.map +1 -1
- package/dist/memory/config.js.map +1 -1
- package/dist/memory/consolidation/ConsolidationLoop.d.ts +177 -0
- package/dist/memory/consolidation/ConsolidationLoop.d.ts.map +1 -0
- package/dist/memory/consolidation/ConsolidationLoop.js +517 -0
- package/dist/memory/consolidation/ConsolidationLoop.js.map +1 -0
- package/dist/memory/consolidation/ConsolidationPipeline.d.ts.map +1 -1
- package/dist/memory/consolidation/ConsolidationPipeline.js +7 -0
- package/dist/memory/consolidation/ConsolidationPipeline.js.map +1 -1
- package/dist/memory/consolidation/index.d.ts +8 -0
- package/dist/memory/consolidation/index.d.ts.map +1 -0
- package/dist/memory/consolidation/index.js +7 -0
- package/dist/memory/consolidation/index.js.map +1 -0
- package/dist/memory/decay/DecayModel.d.ts +33 -0
- package/dist/memory/decay/DecayModel.d.ts.map +1 -1
- package/dist/memory/decay/DecayModel.js +31 -0
- package/dist/memory/decay/DecayModel.js.map +1 -1
- package/dist/memory/facade/Memory.d.ts +228 -0
- package/dist/memory/facade/Memory.d.ts.map +1 -0
- package/dist/memory/facade/Memory.js +823 -0
- package/dist/memory/facade/Memory.js.map +1 -0
- package/dist/memory/facade/index.d.ts +13 -0
- package/dist/memory/facade/index.d.ts.map +1 -0
- package/dist/memory/facade/index.js +11 -0
- package/dist/memory/facade/index.js.map +1 -0
- package/dist/memory/facade/types.d.ts +606 -0
- package/dist/memory/facade/types.d.ts.map +1 -0
- package/dist/memory/facade/types.js +11 -0
- package/dist/memory/facade/types.js.map +1 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.d.ts +132 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.d.ts.map +1 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.js +178 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.js.map +1 -0
- package/dist/memory/feedback/index.d.ts +13 -0
- package/dist/memory/feedback/index.d.ts.map +1 -0
- package/dist/memory/feedback/index.js +12 -0
- package/dist/memory/feedback/index.js.map +1 -0
- package/dist/memory/index.d.ts +22 -0
- package/dist/memory/index.d.ts.map +1 -1
- package/dist/memory/index.js +24 -0
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/ingestion/ChunkingEngine.d.ts +143 -0
- package/dist/memory/ingestion/ChunkingEngine.d.ts.map +1 -0
- package/dist/memory/ingestion/ChunkingEngine.js +508 -0
- package/dist/memory/ingestion/ChunkingEngine.js.map +1 -0
- package/dist/memory/ingestion/DoclingLoader.d.ts +44 -0
- package/dist/memory/ingestion/DoclingLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/DoclingLoader.js +228 -0
- package/dist/memory/ingestion/DoclingLoader.js.map +1 -0
- package/dist/memory/ingestion/DocxLoader.d.ts +37 -0
- package/dist/memory/ingestion/DocxLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/DocxLoader.js +111 -0
- package/dist/memory/ingestion/DocxLoader.js.map +1 -0
- package/dist/memory/ingestion/FolderScanner.d.ts +116 -0
- package/dist/memory/ingestion/FolderScanner.d.ts.map +1 -0
- package/dist/memory/ingestion/FolderScanner.js +127 -0
- package/dist/memory/ingestion/FolderScanner.js.map +1 -0
- package/dist/memory/ingestion/HtmlLoader.d.ts +49 -0
- package/dist/memory/ingestion/HtmlLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/HtmlLoader.js +202 -0
- package/dist/memory/ingestion/HtmlLoader.js.map +1 -0
- package/dist/memory/ingestion/IDocumentLoader.d.ts +63 -0
- package/dist/memory/ingestion/IDocumentLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/IDocumentLoader.js +11 -0
- package/dist/memory/ingestion/IDocumentLoader.js.map +1 -0
- package/dist/memory/ingestion/LoaderRegistry.d.ts +140 -0
- package/dist/memory/ingestion/LoaderRegistry.d.ts.map +1 -0
- package/dist/memory/ingestion/LoaderRegistry.js +229 -0
- package/dist/memory/ingestion/LoaderRegistry.js.map +1 -0
- package/dist/memory/ingestion/MarkdownLoader.d.ts +50 -0
- package/dist/memory/ingestion/MarkdownLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/MarkdownLoader.js +169 -0
- package/dist/memory/ingestion/MarkdownLoader.js.map +1 -0
- package/dist/memory/ingestion/MultimodalAggregator.d.ts +88 -0
- package/dist/memory/ingestion/MultimodalAggregator.d.ts.map +1 -0
- package/dist/memory/ingestion/MultimodalAggregator.js +96 -0
- package/dist/memory/ingestion/MultimodalAggregator.js.map +1 -0
- package/dist/memory/ingestion/OcrPdfLoader.d.ts +41 -0
- package/dist/memory/ingestion/OcrPdfLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/OcrPdfLoader.js +149 -0
- package/dist/memory/ingestion/OcrPdfLoader.js.map +1 -0
- package/dist/memory/ingestion/PdfLoader.d.ts +78 -0
- package/dist/memory/ingestion/PdfLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/PdfLoader.js +179 -0
- package/dist/memory/ingestion/PdfLoader.js.map +1 -0
- package/dist/memory/ingestion/TextLoader.d.ts +66 -0
- package/dist/memory/ingestion/TextLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/TextLoader.js +207 -0
- package/dist/memory/ingestion/TextLoader.js.map +1 -0
- package/dist/memory/ingestion/UrlLoader.d.ts +95 -0
- package/dist/memory/ingestion/UrlLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/UrlLoader.js +174 -0
- package/dist/memory/ingestion/UrlLoader.js.map +1 -0
- package/dist/memory/io/ChatGptImporter.d.ts +85 -0
- package/dist/memory/io/ChatGptImporter.d.ts.map +1 -0
- package/dist/memory/io/ChatGptImporter.js +231 -0
- package/dist/memory/io/ChatGptImporter.js.map +1 -0
- package/dist/memory/io/JsonExporter.d.ts +67 -0
- package/dist/memory/io/JsonExporter.d.ts.map +1 -0
- package/dist/memory/io/JsonExporter.js +132 -0
- package/dist/memory/io/JsonExporter.js.map +1 -0
- package/dist/memory/io/JsonImporter.d.ts +84 -0
- package/dist/memory/io/JsonImporter.d.ts.map +1 -0
- package/dist/memory/io/JsonImporter.js +234 -0
- package/dist/memory/io/JsonImporter.js.map +1 -0
- package/dist/memory/io/MarkdownExporter.d.ts +95 -0
- package/dist/memory/io/MarkdownExporter.d.ts.map +1 -0
- package/dist/memory/io/MarkdownExporter.js +130 -0
- package/dist/memory/io/MarkdownExporter.js.map +1 -0
- package/dist/memory/io/MarkdownImporter.d.ts +84 -0
- package/dist/memory/io/MarkdownImporter.d.ts.map +1 -0
- package/dist/memory/io/MarkdownImporter.js +166 -0
- package/dist/memory/io/MarkdownImporter.js.map +1 -0
- package/dist/memory/io/ObsidianExporter.d.ts +80 -0
- package/dist/memory/io/ObsidianExporter.d.ts.map +1 -0
- package/dist/memory/io/ObsidianExporter.js +127 -0
- package/dist/memory/io/ObsidianExporter.js.map +1 -0
- package/dist/memory/io/ObsidianImporter.d.ts +93 -0
- package/dist/memory/io/ObsidianImporter.d.ts.map +1 -0
- package/dist/memory/io/ObsidianImporter.js +221 -0
- package/dist/memory/io/ObsidianImporter.js.map +1 -0
- package/dist/memory/io/SqliteExporter.d.ts +47 -0
- package/dist/memory/io/SqliteExporter.d.ts.map +1 -0
- package/dist/memory/io/SqliteExporter.js +56 -0
- package/dist/memory/io/SqliteExporter.js.map +1 -0
- package/dist/memory/io/SqliteImporter.d.ts +82 -0
- package/dist/memory/io/SqliteImporter.d.ts.map +1 -0
- package/dist/memory/io/SqliteImporter.js +232 -0
- package/dist/memory/io/SqliteImporter.js.map +1 -0
- package/dist/memory/io/index.d.ts +31 -0
- package/dist/memory/io/index.d.ts.map +1 -0
- package/dist/memory/io/index.js +31 -0
- package/dist/memory/io/index.js.map +1 -0
- package/dist/memory/store/SqliteBrain.d.ts +125 -0
- package/dist/memory/store/SqliteBrain.d.ts.map +1 -0
- package/dist/memory/store/SqliteBrain.js +407 -0
- package/dist/memory/store/SqliteBrain.js.map +1 -0
- package/dist/memory/store/SqliteKnowledgeGraph.d.ts +259 -0
- package/dist/memory/store/SqliteKnowledgeGraph.d.ts.map +1 -0
- package/dist/memory/store/SqliteKnowledgeGraph.js +1062 -0
- package/dist/memory/store/SqliteKnowledgeGraph.js.map +1 -0
- package/dist/memory/store/SqliteMemoryGraph.d.ts +251 -0
- package/dist/memory/store/SqliteMemoryGraph.d.ts.map +1 -0
- package/dist/memory/store/SqliteMemoryGraph.js +637 -0
- package/dist/memory/store/SqliteMemoryGraph.js.map +1 -0
- package/dist/memory/tools/MemoryAddTool.d.ts +98 -0
- package/dist/memory/tools/MemoryAddTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryAddTool.js +131 -0
- package/dist/memory/tools/MemoryAddTool.js.map +1 -0
- package/dist/memory/tools/MemoryDeleteTool.d.ts +83 -0
- package/dist/memory/tools/MemoryDeleteTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryDeleteTool.js +96 -0
- package/dist/memory/tools/MemoryDeleteTool.js.map +1 -0
- package/dist/memory/tools/MemoryMergeTool.d.ts +95 -0
- package/dist/memory/tools/MemoryMergeTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryMergeTool.js +164 -0
- package/dist/memory/tools/MemoryMergeTool.js.map +1 -0
- package/dist/memory/tools/MemoryReflectTool.d.ts +86 -0
- package/dist/memory/tools/MemoryReflectTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryReflectTool.js +102 -0
- package/dist/memory/tools/MemoryReflectTool.js.map +1 -0
- package/dist/memory/tools/MemorySearchTool.d.ts +117 -0
- package/dist/memory/tools/MemorySearchTool.d.ts.map +1 -0
- package/dist/memory/tools/MemorySearchTool.js +162 -0
- package/dist/memory/tools/MemorySearchTool.js.map +1 -0
- package/dist/memory/tools/MemoryUpdateTool.d.ts +92 -0
- package/dist/memory/tools/MemoryUpdateTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryUpdateTool.js +125 -0
- package/dist/memory/tools/MemoryUpdateTool.js.map +1 -0
- package/dist/memory/tools/index.d.ts +32 -0
- package/dist/memory/tools/index.d.ts.map +1 -0
- package/dist/memory/tools/index.js +26 -0
- package/dist/memory/tools/index.js.map +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview MemoryMergeTool — lets an agent merge multiple memory traces
|
|
3
|
+
* into a single, consolidated trace.
|
|
4
|
+
*
|
|
5
|
+
* The merge process:
|
|
6
|
+
* 1. Load all specified traces from the database.
|
|
7
|
+
* 2. Select the survivor — the trace with the highest `retrieval_count`
|
|
8
|
+
* (most-frequently accessed, i.e. most valuable). Ties broken by taking
|
|
9
|
+
* the first trace in the provided `traceIds` array.
|
|
10
|
+
* 3. Determine merged content — use `mergedContent` if the agent supplied it,
|
|
11
|
+
* otherwise concatenate all trace contents with ` | ` as separator.
|
|
12
|
+
* 4. Update the survivor's content, clear its embedding (stale after merge),
|
|
13
|
+
* and union the tags from all merged traces (deduplicated).
|
|
14
|
+
* 5. Soft-delete all non-survivor traces.
|
|
15
|
+
*
|
|
16
|
+
* @module memory/tools/MemoryMergeTool
|
|
17
|
+
*/
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// MemoryMergeTool
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
/**
|
|
22
|
+
* ITool implementation that merges multiple memory traces into one.
|
|
23
|
+
*
|
|
24
|
+
* **Usage:**
|
|
25
|
+
* ```ts
|
|
26
|
+
* const tool = new MemoryMergeTool(brain);
|
|
27
|
+
* const result = await tool.execute(
|
|
28
|
+
* {
|
|
29
|
+
* traceIds: ['mt_1_0', 'mt_2_0', 'mt_3_0'],
|
|
30
|
+
* mergedContent: 'Consolidated insight from three related observations.',
|
|
31
|
+
* },
|
|
32
|
+
* context,
|
|
33
|
+
* );
|
|
34
|
+
* // result.output → { survivorId: 'mt_1_0', deletedIds: ['mt_2_0', 'mt_3_0'] }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class MemoryMergeTool {
|
|
38
|
+
/**
|
|
39
|
+
* @param brain - The agent's shared SQLite brain database connection.
|
|
40
|
+
*/
|
|
41
|
+
constructor(brain) {
|
|
42
|
+
this.brain = brain;
|
|
43
|
+
/** Globally unique tool identifier. */
|
|
44
|
+
this.id = 'memory-merge-v1';
|
|
45
|
+
/** LLM-facing tool name. */
|
|
46
|
+
this.name = 'memory_merge';
|
|
47
|
+
/** Human-readable display name. */
|
|
48
|
+
this.displayName = 'Merge Memories';
|
|
49
|
+
/** LLM-facing description. */
|
|
50
|
+
this.description = 'Merge two or more memory traces into a single consolidated trace. ' +
|
|
51
|
+
'The trace with the highest retrieval count survives; others are soft-deleted. ' +
|
|
52
|
+
'Optionally provide pre-composed merged content; otherwise, contents are concatenated.';
|
|
53
|
+
/** Logical category for discovery and grouping. */
|
|
54
|
+
this.category = 'memory';
|
|
55
|
+
/** This tool writes to the database. */
|
|
56
|
+
this.hasSideEffects = true;
|
|
57
|
+
/** JSON schema for input validation and LLM tool-call construction. */
|
|
58
|
+
this.inputSchema = {
|
|
59
|
+
type: 'object',
|
|
60
|
+
properties: {
|
|
61
|
+
traceIds: {
|
|
62
|
+
type: 'array',
|
|
63
|
+
items: { type: 'string' },
|
|
64
|
+
minItems: 2,
|
|
65
|
+
description: 'Array of trace IDs to merge. Must contain at least 2 IDs.',
|
|
66
|
+
},
|
|
67
|
+
mergedContent: {
|
|
68
|
+
type: 'string',
|
|
69
|
+
description: 'Optional pre-composed merged content. If omitted, trace contents are joined with " | ".',
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
required: ['traceIds'],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
// execute
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
/**
|
|
79
|
+
* Merge the specified traces into one survivor.
|
|
80
|
+
*
|
|
81
|
+
* Steps:
|
|
82
|
+
* 1. Validate that at least 2 trace IDs were supplied.
|
|
83
|
+
* 2. Load all matching, non-deleted traces from the database.
|
|
84
|
+
* 3. Pick survivor by highest `retrieval_count`; fallback to first found.
|
|
85
|
+
* 4. Compute merged content (provided or concatenated).
|
|
86
|
+
* 5. Union all tags, deduplicate.
|
|
87
|
+
* 6. Update survivor: new content, cleared embedding, unioned tags.
|
|
88
|
+
* 7. Soft-delete all non-survivor traces.
|
|
89
|
+
*
|
|
90
|
+
* @param args - Merge input (traceIds, optional mergedContent).
|
|
91
|
+
* @param _context - Tool execution context (not used by this tool).
|
|
92
|
+
* @returns `{ survivorId, deletedIds }` on success, or an error result.
|
|
93
|
+
*/
|
|
94
|
+
async execute(args, _context) {
|
|
95
|
+
try {
|
|
96
|
+
const { traceIds, mergedContent } = args;
|
|
97
|
+
if (!traceIds || traceIds.length < 2) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: 'traceIds must contain at least 2 trace IDs.',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// Load all matching active traces.
|
|
104
|
+
const placeholders = traceIds.map(() => '?').join(', ');
|
|
105
|
+
const rows = this.brain.db
|
|
106
|
+
.prepare(`SELECT id, content, retrieval_count, tags
|
|
107
|
+
FROM memory_traces
|
|
108
|
+
WHERE id IN (${placeholders}) AND deleted = 0`)
|
|
109
|
+
.all(...traceIds);
|
|
110
|
+
if (rows.length < 2) {
|
|
111
|
+
return {
|
|
112
|
+
success: false,
|
|
113
|
+
error: `Found only ${rows.length} active trace(s) for the provided IDs. Need at least 2.`,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// Pick survivor — trace with highest retrieval_count.
|
|
117
|
+
// Stable sort: keeps original order for equal counts so the first traceId wins.
|
|
118
|
+
const sorted = [...rows].sort((a, b) => b.retrieval_count - a.retrieval_count);
|
|
119
|
+
const survivor = sorted[0];
|
|
120
|
+
// Determine merged content.
|
|
121
|
+
const finalContent = mergedContent !== undefined && mergedContent.trim().length > 0
|
|
122
|
+
? mergedContent
|
|
123
|
+
: rows.map((r) => r.content).join(' | ');
|
|
124
|
+
// Union tags from all traces (deduplicated).
|
|
125
|
+
const allTags = new Set();
|
|
126
|
+
for (const row of rows) {
|
|
127
|
+
try {
|
|
128
|
+
const parsed = JSON.parse(row.tags);
|
|
129
|
+
for (const tag of parsed)
|
|
130
|
+
allTags.add(tag);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// Malformed JSON tag — skip gracefully.
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Update survivor: new content, cleared embedding, unioned tags.
|
|
137
|
+
this.brain.db
|
|
138
|
+
.prepare(`UPDATE memory_traces
|
|
139
|
+
SET content = ?, embedding = NULL, tags = ?
|
|
140
|
+
WHERE id = ?`)
|
|
141
|
+
.run(finalContent, JSON.stringify([...allTags]), survivor.id);
|
|
142
|
+
// Soft-delete all non-survivors.
|
|
143
|
+
const deletedIds = [];
|
|
144
|
+
const deleteStmt = this.brain.db.prepare(`UPDATE memory_traces SET deleted = 1 WHERE id = ?`);
|
|
145
|
+
for (const row of rows) {
|
|
146
|
+
if (row.id !== survivor.id) {
|
|
147
|
+
deleteStmt.run(row.id);
|
|
148
|
+
deletedIds.push(row.id);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
success: true,
|
|
153
|
+
output: { survivorId: survivor.id, deletedIds },
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
return {
|
|
158
|
+
success: false,
|
|
159
|
+
error: err instanceof Error ? err.message : String(err),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=MemoryMergeTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryMergeTool.js","sourceRoot":"","sources":["../../../src/memory/tools/MemoryMergeTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AA4CH,8EAA8E;AAC9E,kBAAkB;AAClB,8EAA8E;AAE9E;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,eAAe;IAyC1B;;OAEG;IACH,YAA6B,KAAkB;QAAlB,UAAK,GAAL,KAAK,CAAa;QA3C/C,uCAAuC;QAC9B,OAAE,GAAG,iBAAiB,CAAC;QAEhC,4BAA4B;QACnB,SAAI,GAAG,cAAc,CAAC;QAE/B,mCAAmC;QAC1B,gBAAW,GAAG,gBAAgB,CAAC;QAExC,8BAA8B;QACrB,gBAAW,GAClB,oEAAoE;YACpE,gFAAgF;YAChF,uFAAuF,CAAC;QAE1F,mDAAmD;QAC1C,aAAQ,GAAG,QAAQ,CAAC;QAE7B,wCAAwC;QAC/B,mBAAc,GAAG,IAAI,CAAC;QAE/B,uEAAuE;QAC9D,gBAAW,GAAqB;YACvC,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,QAAQ,EAAE;oBACR,IAAI,EAAE,OAAO;oBACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;oBACzB,QAAQ,EAAE,CAAC;oBACX,WAAW,EAAE,2DAA2D;iBACzE;gBACD,aAAa,EAAE;oBACb,IAAI,EAAE,QAAQ;oBACd,WAAW,EACT,yFAAyF;iBAC5F;aACF;YACD,QAAQ,EAAE,CAAC,UAAU,CAAC;SACvB,CAAC;IAKgD,CAAC;IAEnD,8EAA8E;IAC9E,UAAU;IACV,8EAA8E;IAE9E;;;;;;;;;;;;;;;OAeG;IACH,KAAK,CAAC,OAAO,CACX,IAAsB,EACtB,QAA8B;QAE9B,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AAC,MAAM,CAAC,GAAG,CAAC;aACxD,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview MemoryReflectTool — lets an agent trigger offline memory
|
|
3
|
+
* consolidation on demand.
|
|
4
|
+
*
|
|
5
|
+
* Consolidation is the analogue of slow-wave-sleep memory processing:
|
|
6
|
+
* - **Prune** — soft-delete traces whose Ebbinghaus strength has decayed below threshold.
|
|
7
|
+
* - **Merge** — deduplicate near-identical traces (embedding similarity or hash).
|
|
8
|
+
* - **Strengthen** — record Hebbian co-activation edges from retrieval feedback.
|
|
9
|
+
* - **Derive** — synthesise higher-level insight traces from memory clusters (LLM-backed).
|
|
10
|
+
* - **Compact** — promote old, high-retrieval episodic traces to semantic type.
|
|
11
|
+
* - **Re-index** — rebuild FTS5 index and log the run to `consolidation_log`.
|
|
12
|
+
*
|
|
13
|
+
* The optional `topic` argument is accepted at the contract level but is not
|
|
14
|
+
* yet threaded into the ConsolidationLoop (which currently runs globally).
|
|
15
|
+
* It is reserved for a future topic-scoped consolidation mode.
|
|
16
|
+
*
|
|
17
|
+
* @module memory/tools/MemoryReflectTool
|
|
18
|
+
*/
|
|
19
|
+
import type { ITool, ToolExecutionResult, ToolExecutionContext, JSONSchemaObject } from '../../core/tools/ITool.js';
|
|
20
|
+
import type { ConsolidationLoop } from '../consolidation/ConsolidationLoop.js';
|
|
21
|
+
import type { ConsolidationResult } from '../facade/types.js';
|
|
22
|
+
import type { SqliteBrain } from '../store/SqliteBrain.js';
|
|
23
|
+
/**
|
|
24
|
+
* Input arguments for {@link MemoryReflectTool}.
|
|
25
|
+
*
|
|
26
|
+
* @property topic - Optional hint for the consolidation run. Currently reserved
|
|
27
|
+
* for future topic-scoped consolidation; ignored at runtime.
|
|
28
|
+
*/
|
|
29
|
+
export interface MemoryReflectInput extends Record<string, any> {
|
|
30
|
+
topic?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* ITool implementation that triggers one full memory consolidation cycle via
|
|
34
|
+
* {@link ConsolidationLoop.run()}.
|
|
35
|
+
*
|
|
36
|
+
* **Usage:**
|
|
37
|
+
* ```ts
|
|
38
|
+
* const tool = new MemoryReflectTool(brain, consolidationLoop);
|
|
39
|
+
* const result = await tool.execute({}, context);
|
|
40
|
+
* // result.output → { pruned: 3, merged: 1, derived: 0, compacted: 2, durationMs: 42 }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare class MemoryReflectTool implements ITool<MemoryReflectInput, ConsolidationResult> {
|
|
44
|
+
private readonly brain;
|
|
45
|
+
private readonly consolidation;
|
|
46
|
+
/** Globally unique tool identifier. */
|
|
47
|
+
readonly id = "memory-reflect-v1";
|
|
48
|
+
/** LLM-facing tool name. */
|
|
49
|
+
readonly name = "memory_reflect";
|
|
50
|
+
/** Human-readable display name. */
|
|
51
|
+
readonly displayName = "Reflect on Memory";
|
|
52
|
+
/**
|
|
53
|
+
* Description shown to the LLM. The consolidation steps are described
|
|
54
|
+
* explicitly so the model understands what "reflect" means operationally.
|
|
55
|
+
*/
|
|
56
|
+
readonly description: string;
|
|
57
|
+
/** Logical category for discovery and grouping. */
|
|
58
|
+
readonly category = "memory";
|
|
59
|
+
/**
|
|
60
|
+
* Consolidation writes to the database (pruning, merging, inserting insights).
|
|
61
|
+
* Mark as having side effects so callers may request confirmation if needed.
|
|
62
|
+
*/
|
|
63
|
+
readonly hasSideEffects = true;
|
|
64
|
+
/** JSON schema for input validation and LLM tool-call construction. */
|
|
65
|
+
readonly inputSchema: JSONSchemaObject;
|
|
66
|
+
/**
|
|
67
|
+
* @param brain - The agent's shared SQLite brain database connection.
|
|
68
|
+
* Accepted for symmetry with other memory tools and for
|
|
69
|
+
* future direct consolidation calls.
|
|
70
|
+
* @param consolidation - The {@link ConsolidationLoop} instance to invoke.
|
|
71
|
+
*/
|
|
72
|
+
constructor(brain: SqliteBrain, consolidation: ConsolidationLoop);
|
|
73
|
+
/**
|
|
74
|
+
* Run one full consolidation cycle and return the statistics.
|
|
75
|
+
*
|
|
76
|
+
* If a consolidation cycle is already in progress (mutex guard in
|
|
77
|
+
* {@link ConsolidationLoop}), `run()` returns immediately with zero counts —
|
|
78
|
+
* this is surfaced as a successful result with all-zero statistics.
|
|
79
|
+
*
|
|
80
|
+
* @param _args - Reflect input (optional topic hint, currently unused).
|
|
81
|
+
* @param _context - Tool execution context (not used by this tool).
|
|
82
|
+
* @returns {@link ConsolidationResult} on success, or an error result.
|
|
83
|
+
*/
|
|
84
|
+
execute(_args: MemoryReflectInput, _context: ToolExecutionContext): Promise<ToolExecutionResult<ConsolidationResult>>;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=MemoryReflectTool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryReflectTool.d.ts","sourceRoot":"","sources":["../../../src/memory/tools/MemoryReflectTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AACpH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAM3D;;;;;GAKG;AACH,MAAM,WAAW,kBAAmB,SAAQ,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAC7D,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAMD;;;;;;;;;;GAUG;AACH,qBAAa,iBAAkB,YAAW,KAAK,CAAC,kBAAkB,EAAE,mBAAmB,CAAC;IAgDpF,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,aAAa;IAhDhC,uCAAuC;IACvC,QAAQ,CAAC,EAAE,uBAAuB;IAElC,4BAA4B;IAC5B,QAAQ,CAAC,IAAI,oBAAoB;IAEjC,mCAAmC;IACnC,QAAQ,CAAC,WAAW,uBAAuB;IAE3C;;;OAGG;IACH,QAAQ,CAAC,WAAW,SAIgF;IAEpG,mDAAmD;IACnD,QAAQ,CAAC,QAAQ,YAAY;IAE7B;;;OAGG;IACH,QAAQ,CAAC,cAAc,QAAQ;IAE/B,uEAAuE;IACvE,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CASpC;IAEF;;;;;OAKG;gBAEgB,KAAK,EAAE,WAAW,EAClB,aAAa,EAAE,iBAAiB;IAOnD;;;;;;;;;;OAUG;IACG,OAAO,CACX,KAAK,EAAE,kBAAkB,EACzB,QAAQ,EAAE,oBAAoB,GAC7B,OAAO,CAAC,mBAAmB,CAAC,mBAAmB,CAAC,CAAC;CAWrD"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview MemoryReflectTool — lets an agent trigger offline memory
|
|
3
|
+
* consolidation on demand.
|
|
4
|
+
*
|
|
5
|
+
* Consolidation is the analogue of slow-wave-sleep memory processing:
|
|
6
|
+
* - **Prune** — soft-delete traces whose Ebbinghaus strength has decayed below threshold.
|
|
7
|
+
* - **Merge** — deduplicate near-identical traces (embedding similarity or hash).
|
|
8
|
+
* - **Strengthen** — record Hebbian co-activation edges from retrieval feedback.
|
|
9
|
+
* - **Derive** — synthesise higher-level insight traces from memory clusters (LLM-backed).
|
|
10
|
+
* - **Compact** — promote old, high-retrieval episodic traces to semantic type.
|
|
11
|
+
* - **Re-index** — rebuild FTS5 index and log the run to `consolidation_log`.
|
|
12
|
+
*
|
|
13
|
+
* The optional `topic` argument is accepted at the contract level but is not
|
|
14
|
+
* yet threaded into the ConsolidationLoop (which currently runs globally).
|
|
15
|
+
* It is reserved for a future topic-scoped consolidation mode.
|
|
16
|
+
*
|
|
17
|
+
* @module memory/tools/MemoryReflectTool
|
|
18
|
+
*/
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// MemoryReflectTool
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
/**
|
|
23
|
+
* ITool implementation that triggers one full memory consolidation cycle via
|
|
24
|
+
* {@link ConsolidationLoop.run()}.
|
|
25
|
+
*
|
|
26
|
+
* **Usage:**
|
|
27
|
+
* ```ts
|
|
28
|
+
* const tool = new MemoryReflectTool(brain, consolidationLoop);
|
|
29
|
+
* const result = await tool.execute({}, context);
|
|
30
|
+
* // result.output → { pruned: 3, merged: 1, derived: 0, compacted: 2, durationMs: 42 }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class MemoryReflectTool {
|
|
34
|
+
/**
|
|
35
|
+
* @param brain - The agent's shared SQLite brain database connection.
|
|
36
|
+
* Accepted for symmetry with other memory tools and for
|
|
37
|
+
* future direct consolidation calls.
|
|
38
|
+
* @param consolidation - The {@link ConsolidationLoop} instance to invoke.
|
|
39
|
+
*/
|
|
40
|
+
constructor(brain, consolidation) {
|
|
41
|
+
this.brain = brain;
|
|
42
|
+
this.consolidation = consolidation;
|
|
43
|
+
/** Globally unique tool identifier. */
|
|
44
|
+
this.id = 'memory-reflect-v1';
|
|
45
|
+
/** LLM-facing tool name. */
|
|
46
|
+
this.name = 'memory_reflect';
|
|
47
|
+
/** Human-readable display name. */
|
|
48
|
+
this.displayName = 'Reflect on Memory';
|
|
49
|
+
/**
|
|
50
|
+
* Description shown to the LLM. The consolidation steps are described
|
|
51
|
+
* explicitly so the model understands what "reflect" means operationally.
|
|
52
|
+
*/
|
|
53
|
+
this.description = 'Trigger memory consolidation — prune weak memories, merge duplicates, derive insights from patterns. ' +
|
|
54
|
+
'This runs an offline slow-wave-sleep analogue: decayed traces are pruned, near-identical traces are ' +
|
|
55
|
+
'merged, Hebbian co-activation edges are recorded, insights are derived from clusters, and old ' +
|
|
56
|
+
'high-retrieval episodic traces are promoted to semantic type. Returns consolidation statistics.';
|
|
57
|
+
/** Logical category for discovery and grouping. */
|
|
58
|
+
this.category = 'memory';
|
|
59
|
+
/**
|
|
60
|
+
* Consolidation writes to the database (pruning, merging, inserting insights).
|
|
61
|
+
* Mark as having side effects so callers may request confirmation if needed.
|
|
62
|
+
*/
|
|
63
|
+
this.hasSideEffects = true;
|
|
64
|
+
/** JSON schema for input validation and LLM tool-call construction. */
|
|
65
|
+
this.inputSchema = {
|
|
66
|
+
type: 'object',
|
|
67
|
+
properties: {
|
|
68
|
+
topic: {
|
|
69
|
+
type: 'string',
|
|
70
|
+
description: 'Optional topic hint for the consolidation run. Reserved for future topic-scoped consolidation.',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
// execute
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
/**
|
|
79
|
+
* Run one full consolidation cycle and return the statistics.
|
|
80
|
+
*
|
|
81
|
+
* If a consolidation cycle is already in progress (mutex guard in
|
|
82
|
+
* {@link ConsolidationLoop}), `run()` returns immediately with zero counts —
|
|
83
|
+
* this is surfaced as a successful result with all-zero statistics.
|
|
84
|
+
*
|
|
85
|
+
* @param _args - Reflect input (optional topic hint, currently unused).
|
|
86
|
+
* @param _context - Tool execution context (not used by this tool).
|
|
87
|
+
* @returns {@link ConsolidationResult} on success, or an error result.
|
|
88
|
+
*/
|
|
89
|
+
async execute(_args, _context) {
|
|
90
|
+
try {
|
|
91
|
+
const result = await this.consolidation.run();
|
|
92
|
+
return { success: true, output: result };
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
error: err instanceof Error ? err.message : String(err),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=MemoryReflectTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryReflectTool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AAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;aACxD,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview MemorySearchTool — lets an agent search its own memory traces
|
|
3
|
+
* using FTS5 full-text search.
|
|
4
|
+
*
|
|
5
|
+
* The tool queries the `memory_traces_fts` FTS5 virtual table (backed by the
|
|
6
|
+
* `memory_traces` content table) and joins back to `memory_traces` for
|
|
7
|
+
* metadata fields (type, scope, strength, tags). Optional `type` and `scope`
|
|
8
|
+
* filters narrow the result set via SQL WHERE clauses applied to the join.
|
|
9
|
+
*
|
|
10
|
+
* FTS5 matching uses the Porter-stemmed tokenizer configured at DDL time, so
|
|
11
|
+
* queries like `"retrieve"` will also match `"retrieval"`, `"retrieved"`, etc.
|
|
12
|
+
*
|
|
13
|
+
* @module memory/tools/MemorySearchTool
|
|
14
|
+
*/
|
|
15
|
+
import type { ITool, ToolExecutionResult, ToolExecutionContext, JSONSchemaObject } from '../../core/tools/ITool.js';
|
|
16
|
+
import type { SqliteBrain } from '../store/SqliteBrain.js';
|
|
17
|
+
/**
|
|
18
|
+
* Input arguments for {@link MemorySearchTool}.
|
|
19
|
+
*
|
|
20
|
+
* @property query - The full-text search query string. Supports FTS5 syntax
|
|
21
|
+
* (phrase queries in quotes, boolean operators, prefix matching).
|
|
22
|
+
* @property type - Optional filter: only return traces of this Tulving memory type.
|
|
23
|
+
* @property scope - Optional filter: only return traces with this visibility scope.
|
|
24
|
+
* @property limit - Maximum number of results to return. Defaults to 10.
|
|
25
|
+
*/
|
|
26
|
+
export interface MemorySearchInput extends Record<string, any> {
|
|
27
|
+
query: string;
|
|
28
|
+
type?: string;
|
|
29
|
+
scope?: string;
|
|
30
|
+
limit?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A single result item in the {@link MemorySearchOutput} results array.
|
|
34
|
+
*
|
|
35
|
+
* @property id - Trace ID.
|
|
36
|
+
* @property content - Full text content of the trace.
|
|
37
|
+
* @property type - Tulving memory type.
|
|
38
|
+
* @property scope - Visibility scope.
|
|
39
|
+
* @property strength - Current Ebbinghaus encoding strength (0–1).
|
|
40
|
+
* @property tags - Array of tag strings.
|
|
41
|
+
*/
|
|
42
|
+
export interface MemorySearchResult {
|
|
43
|
+
id: string;
|
|
44
|
+
content: string;
|
|
45
|
+
type: string;
|
|
46
|
+
scope: string;
|
|
47
|
+
strength: number;
|
|
48
|
+
tags: string[];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Output returned by {@link MemorySearchTool} on success.
|
|
52
|
+
*
|
|
53
|
+
* @property results - Array of matching memory traces ordered by FTS5 relevance (BM25).
|
|
54
|
+
*/
|
|
55
|
+
export interface MemorySearchOutput {
|
|
56
|
+
results: MemorySearchResult[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* ITool implementation that searches the agent's memory traces using FTS5.
|
|
60
|
+
*
|
|
61
|
+
* **Usage:**
|
|
62
|
+
* ```ts
|
|
63
|
+
* const tool = new MemorySearchTool(brain);
|
|
64
|
+
* const result = await tool.execute(
|
|
65
|
+
* { query: 'dark mode preference', scope: 'user', limit: 5 },
|
|
66
|
+
* context,
|
|
67
|
+
* );
|
|
68
|
+
* // result.output.results → [{ id, content, type, scope, strength, tags }, ...]
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare class MemorySearchTool implements ITool<MemorySearchInput, MemorySearchOutput> {
|
|
72
|
+
private readonly brain;
|
|
73
|
+
/** Globally unique tool identifier. */
|
|
74
|
+
readonly id = "memory-search-v1";
|
|
75
|
+
/** LLM-facing tool name. */
|
|
76
|
+
readonly name = "memory_search";
|
|
77
|
+
/** Human-readable display name. */
|
|
78
|
+
readonly displayName = "Search Memory";
|
|
79
|
+
/** LLM-facing description. */
|
|
80
|
+
readonly description: string;
|
|
81
|
+
/** Logical category for discovery and grouping. */
|
|
82
|
+
readonly category = "memory";
|
|
83
|
+
/** This tool only reads from the database — no side effects. */
|
|
84
|
+
readonly hasSideEffects = false;
|
|
85
|
+
/** JSON schema for input validation and LLM tool-call construction. */
|
|
86
|
+
readonly inputSchema: JSONSchemaObject;
|
|
87
|
+
/**
|
|
88
|
+
* @param brain - The agent's shared SQLite brain database connection.
|
|
89
|
+
*/
|
|
90
|
+
constructor(brain: SqliteBrain);
|
|
91
|
+
/**
|
|
92
|
+
* Run a full-text search against `memory_traces_fts` and join back to
|
|
93
|
+
* `memory_traces` for metadata.
|
|
94
|
+
*
|
|
95
|
+
* The SQL pattern:
|
|
96
|
+
* ```sql
|
|
97
|
+
* SELECT mt.id, mt.content, mt.type, mt.scope, mt.strength, mt.tags
|
|
98
|
+
* FROM memory_traces_fts fts
|
|
99
|
+
* JOIN memory_traces mt ON mt.rowid = fts.rowid
|
|
100
|
+
* WHERE fts.memory_traces_fts MATCH ?
|
|
101
|
+
* AND mt.deleted = 0
|
|
102
|
+
* [AND mt.type = ?] -- when type filter provided
|
|
103
|
+
* [AND mt.scope = ?] -- when scope filter provided
|
|
104
|
+
* ORDER BY rank -- FTS5 BM25 relevance (lower = more relevant)
|
|
105
|
+
* LIMIT ?
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* Tags are stored as JSON arrays; they are parsed and returned as string[].
|
|
109
|
+
* Malformed tag JSON returns an empty array rather than throwing.
|
|
110
|
+
*
|
|
111
|
+
* @param args - Search input (query, optional type/scope/limit).
|
|
112
|
+
* @param _context - Tool execution context (not used by this tool).
|
|
113
|
+
* @returns `{ results }` array on success, or an error result.
|
|
114
|
+
*/
|
|
115
|
+
execute(args: MemorySearchInput, _context: ToolExecutionContext): Promise<ToolExecutionResult<MemorySearchOutput>>;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=MemorySearchTool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemorySearchTool.d.ts","sourceRoot":"","sources":["../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CAAC,WAAW,EAAE,gBAAgB,CA0BpC;IAEF;;OAEG;gBAC0B,KAAK,EAAE,WAAW;IAM/C;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,OAAO,CACX,IAAI,EAAE,iBAAiB,EACvB,QAAQ,EAAE,oBAAoB,GAC7B,OAAO,CAAC,mBAAmB,CAAC,kBAAkB,CAAC,CAAC;CAgEpD"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview MemorySearchTool — lets an agent search its own memory traces
|
|
3
|
+
* using FTS5 full-text search.
|
|
4
|
+
*
|
|
5
|
+
* The tool queries the `memory_traces_fts` FTS5 virtual table (backed by the
|
|
6
|
+
* `memory_traces` content table) and joins back to `memory_traces` for
|
|
7
|
+
* metadata fields (type, scope, strength, tags). Optional `type` and `scope`
|
|
8
|
+
* filters narrow the result set via SQL WHERE clauses applied to the join.
|
|
9
|
+
*
|
|
10
|
+
* FTS5 matching uses the Porter-stemmed tokenizer configured at DDL time, so
|
|
11
|
+
* queries like `"retrieve"` will also match `"retrieval"`, `"retrieved"`, etc.
|
|
12
|
+
*
|
|
13
|
+
* @module memory/tools/MemorySearchTool
|
|
14
|
+
*/
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// MemorySearchTool
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
/**
|
|
19
|
+
* ITool implementation that searches the agent's memory traces using FTS5.
|
|
20
|
+
*
|
|
21
|
+
* **Usage:**
|
|
22
|
+
* ```ts
|
|
23
|
+
* const tool = new MemorySearchTool(brain);
|
|
24
|
+
* const result = await tool.execute(
|
|
25
|
+
* { query: 'dark mode preference', scope: 'user', limit: 5 },
|
|
26
|
+
* context,
|
|
27
|
+
* );
|
|
28
|
+
* // result.output.results → [{ id, content, type, scope, strength, tags }, ...]
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class MemorySearchTool {
|
|
32
|
+
/**
|
|
33
|
+
* @param brain - The agent's shared SQLite brain database connection.
|
|
34
|
+
*/
|
|
35
|
+
constructor(brain) {
|
|
36
|
+
this.brain = brain;
|
|
37
|
+
/** Globally unique tool identifier. */
|
|
38
|
+
this.id = 'memory-search-v1';
|
|
39
|
+
/** LLM-facing tool name. */
|
|
40
|
+
this.name = 'memory_search';
|
|
41
|
+
/** Human-readable display name. */
|
|
42
|
+
this.displayName = 'Search Memory';
|
|
43
|
+
/** LLM-facing description. */
|
|
44
|
+
this.description = 'Search memory traces using full-text search (FTS5 with Porter stemming). ' +
|
|
45
|
+
'Supports FTS5 query syntax: phrase queries in quotes, AND/OR operators, prefix matching (e.g. "retriev*"). ' +
|
|
46
|
+
'Filter by type (episodic/semantic/procedural/prospective) or scope (thread/user/persona/organization).';
|
|
47
|
+
/** Logical category for discovery and grouping. */
|
|
48
|
+
this.category = 'memory';
|
|
49
|
+
/** This tool only reads from the database — no side effects. */
|
|
50
|
+
this.hasSideEffects = false;
|
|
51
|
+
/** JSON schema for input validation and LLM tool-call construction. */
|
|
52
|
+
this.inputSchema = {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
query: {
|
|
56
|
+
type: 'string',
|
|
57
|
+
description: 'Full-text search query string. Supports FTS5 syntax: phrases in quotes, boolean operators, prefix matching.',
|
|
58
|
+
},
|
|
59
|
+
type: {
|
|
60
|
+
type: 'string',
|
|
61
|
+
enum: ['episodic', 'semantic', 'procedural', 'prospective'],
|
|
62
|
+
description: 'Optional filter: only return traces of this memory type.',
|
|
63
|
+
},
|
|
64
|
+
scope: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
enum: ['thread', 'user', 'persona', 'organization'],
|
|
67
|
+
description: 'Optional filter: only return traces with this scope.',
|
|
68
|
+
},
|
|
69
|
+
limit: {
|
|
70
|
+
type: 'integer',
|
|
71
|
+
minimum: 1,
|
|
72
|
+
maximum: 100,
|
|
73
|
+
description: 'Maximum number of results to return. Defaults to 10.',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
required: ['query'],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
// execute
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
/**
|
|
83
|
+
* Run a full-text search against `memory_traces_fts` and join back to
|
|
84
|
+
* `memory_traces` for metadata.
|
|
85
|
+
*
|
|
86
|
+
* The SQL pattern:
|
|
87
|
+
* ```sql
|
|
88
|
+
* SELECT mt.id, mt.content, mt.type, mt.scope, mt.strength, mt.tags
|
|
89
|
+
* FROM memory_traces_fts fts
|
|
90
|
+
* JOIN memory_traces mt ON mt.rowid = fts.rowid
|
|
91
|
+
* WHERE fts.memory_traces_fts MATCH ?
|
|
92
|
+
* AND mt.deleted = 0
|
|
93
|
+
* [AND mt.type = ?] -- when type filter provided
|
|
94
|
+
* [AND mt.scope = ?] -- when scope filter provided
|
|
95
|
+
* ORDER BY rank -- FTS5 BM25 relevance (lower = more relevant)
|
|
96
|
+
* LIMIT ?
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* Tags are stored as JSON arrays; they are parsed and returned as string[].
|
|
100
|
+
* Malformed tag JSON returns an empty array rather than throwing.
|
|
101
|
+
*
|
|
102
|
+
* @param args - Search input (query, optional type/scope/limit).
|
|
103
|
+
* @param _context - Tool execution context (not used by this tool).
|
|
104
|
+
* @returns `{ results }` array on success, or an error result.
|
|
105
|
+
*/
|
|
106
|
+
async execute(args, _context) {
|
|
107
|
+
try {
|
|
108
|
+
const limit = args.limit ?? 10;
|
|
109
|
+
// Build WHERE clause additions for optional filters.
|
|
110
|
+
const extraClauses = [];
|
|
111
|
+
const extraParams = [];
|
|
112
|
+
if (args.type !== undefined) {
|
|
113
|
+
extraClauses.push('mt.type = ?');
|
|
114
|
+
extraParams.push(args.type);
|
|
115
|
+
}
|
|
116
|
+
if (args.scope !== undefined) {
|
|
117
|
+
extraClauses.push('mt.scope = ?');
|
|
118
|
+
extraParams.push(args.scope);
|
|
119
|
+
}
|
|
120
|
+
const extraWhere = extraClauses.length > 0 ? `AND ${extraClauses.join(' AND ')}` : '';
|
|
121
|
+
const sql = `
|
|
122
|
+
SELECT mt.id, mt.content, mt.type, mt.scope, mt.strength, mt.tags
|
|
123
|
+
FROM memory_traces_fts fts
|
|
124
|
+
JOIN memory_traces mt ON mt.rowid = fts.rowid
|
|
125
|
+
WHERE fts.memory_traces_fts MATCH ?
|
|
126
|
+
AND mt.deleted = 0
|
|
127
|
+
${extraWhere}
|
|
128
|
+
ORDER BY rank
|
|
129
|
+
LIMIT ?
|
|
130
|
+
`;
|
|
131
|
+
const params = [args.query, ...extraParams, limit];
|
|
132
|
+
const rows = this.brain.db
|
|
133
|
+
.prepare(sql)
|
|
134
|
+
.all(...params);
|
|
135
|
+
const results = rows.map((row) => {
|
|
136
|
+
let tags = [];
|
|
137
|
+
try {
|
|
138
|
+
tags = JSON.parse(row.tags);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// Malformed JSON tags — return empty array.
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
id: row.id,
|
|
145
|
+
content: row.content,
|
|
146
|
+
type: row.type,
|
|
147
|
+
scope: row.scope,
|
|
148
|
+
strength: row.strength,
|
|
149
|
+
tags,
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
return { success: true, output: { results } };
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
return {
|
|
156
|
+
success: false,
|
|
157
|
+
error: err instanceof Error ? err.message : String(err),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=MemorySearchTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemorySearchTool.js","sourceRoot":"","sources":["../../../src/memory/tools/MemorySearchTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAmEH,8EAA8E;AAC9E,mBAAmB;AACnB,8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