@mce-bt/microagents-memory 0.1.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/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/knowledge-graph.d.ts +179 -0
- package/dist/knowledge-graph.d.ts.map +1 -0
- package/dist/knowledge-graph.js +513 -0
- package/dist/knowledge-graph.js.map +1 -0
- package/dist/ltm.d.ts +82 -0
- package/dist/ltm.d.ts.map +1 -0
- package/dist/ltm.js +223 -0
- package/dist/ltm.js.map +1 -0
- package/dist/manager.d.ts +80 -0
- package/dist/manager.d.ts.map +1 -0
- package/dist/manager.js +182 -0
- package/dist/manager.js.map +1 -0
- package/dist/memory-extractor.d.ts +75 -0
- package/dist/memory-extractor.d.ts.map +1 -0
- package/dist/memory-extractor.js +321 -0
- package/dist/memory-extractor.js.map +1 -0
- package/dist/memory-tools.d.ts +10 -0
- package/dist/memory-tools.d.ts.map +1 -0
- package/dist/memory-tools.js +239 -0
- package/dist/memory-tools.js.map +1 -0
- package/dist/mtm.d.ts +55 -0
- package/dist/mtm.d.ts.map +1 -0
- package/dist/mtm.js +139 -0
- package/dist/mtm.js.map +1 -0
- package/dist/stm.d.ts +55 -0
- package/dist/stm.d.ts.map +1 -0
- package/dist/stm.js +105 -0
- package/dist/stm.js.map +1 -0
- package/dist/summarization-pipeline.d.ts +78 -0
- package/dist/summarization-pipeline.d.ts.map +1 -0
- package/dist/summarization-pipeline.js +155 -0
- package/dist/summarization-pipeline.js.map +1 -0
- package/package.json +59 -0
package/dist/ltm.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
const { Pool } = pg;
|
|
3
|
+
/**
|
|
4
|
+
* Long-Term Memory (LTM) — PostgreSQL + pgvector knowledge store.
|
|
5
|
+
*
|
|
6
|
+
* Stores persistent semantic knowledge with embeddings for RAG retrieval.
|
|
7
|
+
* Where durable learning lives. Categorized, tagged, searchable.
|
|
8
|
+
*/
|
|
9
|
+
export class LongTermMemory {
|
|
10
|
+
pool = null;
|
|
11
|
+
config;
|
|
12
|
+
agentId = '';
|
|
13
|
+
embeddingDimensions;
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.embeddingDimensions = config.embeddingDimensions ?? 1536;
|
|
17
|
+
}
|
|
18
|
+
async start(agentId) {
|
|
19
|
+
this.agentId = agentId;
|
|
20
|
+
this.pool = new Pool({ connectionString: this.config.connectionString });
|
|
21
|
+
await this.ensureSchema();
|
|
22
|
+
}
|
|
23
|
+
async stop() {
|
|
24
|
+
if (this.pool) {
|
|
25
|
+
await this.pool.end();
|
|
26
|
+
this.pool = null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async ensureSchema() {
|
|
30
|
+
if (!this.pool)
|
|
31
|
+
return;
|
|
32
|
+
// Enable pgvector extension
|
|
33
|
+
await this.pool.query('CREATE EXTENSION IF NOT EXISTS vector');
|
|
34
|
+
await this.pool.query(`
|
|
35
|
+
CREATE TABLE IF NOT EXISTS ltm_knowledge (
|
|
36
|
+
id TEXT PRIMARY KEY,
|
|
37
|
+
agent_id TEXT NOT NULL,
|
|
38
|
+
content TEXT NOT NULL,
|
|
39
|
+
category TEXT NOT NULL DEFAULT 'general',
|
|
40
|
+
tags TEXT[] DEFAULT '{}',
|
|
41
|
+
embedding vector(${this.embeddingDimensions}),
|
|
42
|
+
metadata JSONB DEFAULT '{}',
|
|
43
|
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
44
|
+
updated_at TIMESTAMPTZ DEFAULT NOW()
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
CREATE INDEX IF NOT EXISTS idx_ltm_agent_id ON ltm_knowledge(agent_id);
|
|
48
|
+
CREATE INDEX IF NOT EXISTS idx_ltm_category ON ltm_knowledge(category);
|
|
49
|
+
CREATE INDEX IF NOT EXISTS idx_ltm_tags ON ltm_knowledge USING GIN(tags);
|
|
50
|
+
`);
|
|
51
|
+
// Create vector index for similarity search (IVFFlat for performance)
|
|
52
|
+
// Only create if enough rows exist; otherwise use default sequential scan
|
|
53
|
+
try {
|
|
54
|
+
await this.pool.query(`
|
|
55
|
+
CREATE INDEX IF NOT EXISTS idx_ltm_embedding
|
|
56
|
+
ON ltm_knowledge USING ivfflat (embedding vector_cosine_ops)
|
|
57
|
+
WITH (lists = 100)
|
|
58
|
+
`);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// IVFFlat index requires data to build; will be created later
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Store a knowledge fact with optional embedding.
|
|
66
|
+
*/
|
|
67
|
+
async store(fact) {
|
|
68
|
+
if (!this.pool)
|
|
69
|
+
throw new Error('LTM not started');
|
|
70
|
+
const id = `kf-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
71
|
+
const embeddingStr = fact.embedding
|
|
72
|
+
? `[${fact.embedding.join(',')}]`
|
|
73
|
+
: null;
|
|
74
|
+
await this.pool.query(`INSERT INTO ltm_knowledge (id, agent_id, content, category, tags, embedding, metadata)
|
|
75
|
+
VALUES ($1, $2, $3, $4, $5, $6::vector, $7)`, [
|
|
76
|
+
id,
|
|
77
|
+
this.agentId,
|
|
78
|
+
fact.content,
|
|
79
|
+
fact.category ?? 'general',
|
|
80
|
+
fact.tags ?? [],
|
|
81
|
+
embeddingStr,
|
|
82
|
+
JSON.stringify(fact.metadata ?? {}),
|
|
83
|
+
]);
|
|
84
|
+
return id;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Semantic similarity search using embedding vector.
|
|
88
|
+
*/
|
|
89
|
+
async search(queryEmbedding, options = {}) {
|
|
90
|
+
if (!this.pool)
|
|
91
|
+
throw new Error('LTM not started');
|
|
92
|
+
const conditions = ['embedding IS NOT NULL'];
|
|
93
|
+
const params = [];
|
|
94
|
+
let idx = 1;
|
|
95
|
+
conditions.push(`agent_id = $${idx}`);
|
|
96
|
+
params.push(options.agentId ?? this.agentId);
|
|
97
|
+
idx++;
|
|
98
|
+
if (options.category) {
|
|
99
|
+
conditions.push(`category = $${idx}`);
|
|
100
|
+
params.push(options.category);
|
|
101
|
+
idx++;
|
|
102
|
+
}
|
|
103
|
+
if (options.tags && options.tags.length > 0) {
|
|
104
|
+
conditions.push(`tags && $${idx}`);
|
|
105
|
+
params.push(options.tags);
|
|
106
|
+
idx++;
|
|
107
|
+
}
|
|
108
|
+
const embeddingStr = `[${queryEmbedding.join(',')}]`;
|
|
109
|
+
const limit = options.limit ?? 10;
|
|
110
|
+
const threshold = options.threshold ?? 0.7;
|
|
111
|
+
const where = conditions.join(' AND ');
|
|
112
|
+
const result = await this.pool.query(`SELECT *,
|
|
113
|
+
1 - (embedding <=> $${idx}::vector) as similarity
|
|
114
|
+
FROM ltm_knowledge
|
|
115
|
+
WHERE ${where}
|
|
116
|
+
AND 1 - (embedding <=> $${idx}::vector) >= $${idx + 1}
|
|
117
|
+
ORDER BY embedding <=> $${idx}::vector
|
|
118
|
+
LIMIT $${idx + 2}`, [...params, embeddingStr, threshold, limit]);
|
|
119
|
+
return result.rows.map((row) => ({
|
|
120
|
+
...this.rowToFact(row),
|
|
121
|
+
similarity: parseFloat(row.similarity),
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Full-text search (keyword-based, no embedding needed).
|
|
126
|
+
*/
|
|
127
|
+
async searchByText(query, options = {}) {
|
|
128
|
+
if (!this.pool)
|
|
129
|
+
throw new Error('LTM not started');
|
|
130
|
+
const conditions = [];
|
|
131
|
+
const params = [];
|
|
132
|
+
let idx = 1;
|
|
133
|
+
conditions.push(`agent_id = $${idx}`);
|
|
134
|
+
params.push(this.agentId);
|
|
135
|
+
idx++;
|
|
136
|
+
conditions.push(`content ILIKE $${idx}`);
|
|
137
|
+
params.push(`%${query}%`);
|
|
138
|
+
idx++;
|
|
139
|
+
if (options.category) {
|
|
140
|
+
conditions.push(`category = $${idx}`);
|
|
141
|
+
params.push(options.category);
|
|
142
|
+
idx++;
|
|
143
|
+
}
|
|
144
|
+
if (options.tags && options.tags.length > 0) {
|
|
145
|
+
conditions.push(`tags && $${idx}`);
|
|
146
|
+
params.push(options.tags);
|
|
147
|
+
idx++;
|
|
148
|
+
}
|
|
149
|
+
const limit = options.limit ?? 10;
|
|
150
|
+
const where = conditions.join(' AND ');
|
|
151
|
+
const result = await this.pool.query(`SELECT * FROM ltm_knowledge WHERE ${where} ORDER BY updated_at DESC LIMIT $${idx}`, [...params, limit]);
|
|
152
|
+
return result.rows.map(this.rowToFact);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Get a fact by ID.
|
|
156
|
+
*/
|
|
157
|
+
async getById(id) {
|
|
158
|
+
if (!this.pool)
|
|
159
|
+
throw new Error('LTM not started');
|
|
160
|
+
const result = await this.pool.query('SELECT * FROM ltm_knowledge WHERE id = $1', [id]);
|
|
161
|
+
if (result.rows.length === 0)
|
|
162
|
+
return null;
|
|
163
|
+
return this.rowToFact(result.rows[0]);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Update a fact's content and optionally its embedding.
|
|
167
|
+
*/
|
|
168
|
+
async update(id, updates) {
|
|
169
|
+
if (!this.pool)
|
|
170
|
+
throw new Error('LTM not started');
|
|
171
|
+
const sets = ['updated_at = NOW()'];
|
|
172
|
+
const params = [];
|
|
173
|
+
let idx = 1;
|
|
174
|
+
if (updates.content !== undefined) {
|
|
175
|
+
sets.push(`content = $${idx}`);
|
|
176
|
+
params.push(updates.content);
|
|
177
|
+
idx++;
|
|
178
|
+
}
|
|
179
|
+
if (updates.category !== undefined) {
|
|
180
|
+
sets.push(`category = $${idx}`);
|
|
181
|
+
params.push(updates.category);
|
|
182
|
+
idx++;
|
|
183
|
+
}
|
|
184
|
+
if (updates.tags !== undefined) {
|
|
185
|
+
sets.push(`tags = $${idx}`);
|
|
186
|
+
params.push(updates.tags);
|
|
187
|
+
idx++;
|
|
188
|
+
}
|
|
189
|
+
if (updates.embedding !== undefined) {
|
|
190
|
+
sets.push(`embedding = $${idx}::vector`);
|
|
191
|
+
params.push(`[${updates.embedding.join(',')}]`);
|
|
192
|
+
idx++;
|
|
193
|
+
}
|
|
194
|
+
if (updates.metadata !== undefined) {
|
|
195
|
+
sets.push(`metadata = $${idx}`);
|
|
196
|
+
params.push(JSON.stringify(updates.metadata));
|
|
197
|
+
idx++;
|
|
198
|
+
}
|
|
199
|
+
params.push(id);
|
|
200
|
+
await this.pool.query(`UPDATE ltm_knowledge SET ${sets.join(', ')} WHERE id = $${idx}`, params);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Delete a fact.
|
|
204
|
+
*/
|
|
205
|
+
async forget(id) {
|
|
206
|
+
if (!this.pool)
|
|
207
|
+
throw new Error('LTM not started');
|
|
208
|
+
await this.pool.query('DELETE FROM ltm_knowledge WHERE id = $1', [id]);
|
|
209
|
+
}
|
|
210
|
+
rowToFact(row) {
|
|
211
|
+
return {
|
|
212
|
+
id: row.id,
|
|
213
|
+
agentId: row.agent_id,
|
|
214
|
+
content: row.content,
|
|
215
|
+
category: row.category,
|
|
216
|
+
tags: row.tags,
|
|
217
|
+
metadata: (row.metadata ?? {}),
|
|
218
|
+
createdAt: new Date(row.created_at),
|
|
219
|
+
updatedAt: new Date(row.updated_at),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
//# sourceMappingURL=ltm.js.map
|
package/dist/ltm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ltm.js","sourceRoot":"","sources":["../src/ltm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;AAoBpB;;;;;GAKG;AACH,MAAM,OAAO,cAAc;IACjB,IAAI,GAAmB,I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AC1B,QAAQ,EAAE,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAA4B;YACzD,SAAS,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,UAAoB,CAAC;YAC7C,SAAS,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,UAAoB,CAAC;SAC9C,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ShortTermMemory, type STMConfig } from './stm.js';
|
|
2
|
+
import { MidTermMemory, type MTMConfig } from './mtm.js';
|
|
3
|
+
import { LongTermMemory, type LTMConfig } from './ltm.js';
|
|
4
|
+
import { KnowledgeGraph, type KGConfig } from './knowledge-graph.js';
|
|
5
|
+
import type { MemoryEntry } from '@mce-bt/microagents-core';
|
|
6
|
+
export interface MemoryManagerConfig {
|
|
7
|
+
stm: STMConfig;
|
|
8
|
+
mtm: MTMConfig;
|
|
9
|
+
ltm: LTMConfig;
|
|
10
|
+
/** Optional knowledge graph config. When provided, enables KG features. */
|
|
11
|
+
kg?: KGConfig;
|
|
12
|
+
}
|
|
13
|
+
export interface ContextBlock {
|
|
14
|
+
source: 'system' | 'stm' | 'mtm' | 'ltm' | 'kg';
|
|
15
|
+
content: string;
|
|
16
|
+
metadata?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Unified Memory Manager — orchestrates all 3 memory tiers.
|
|
20
|
+
*
|
|
21
|
+
* Provides a single API for agents to interact with memory,
|
|
22
|
+
* plus a buildContext() method that assembles LLM context from
|
|
23
|
+
* all relevant memory sources.
|
|
24
|
+
*/
|
|
25
|
+
export declare class MemoryManager {
|
|
26
|
+
readonly stm: ShortTermMemory;
|
|
27
|
+
readonly mtm: MidTermMemory;
|
|
28
|
+
readonly ltm: LongTermMemory;
|
|
29
|
+
readonly kg: KnowledgeGraph | null;
|
|
30
|
+
private agentId;
|
|
31
|
+
constructor(config: MemoryManagerConfig);
|
|
32
|
+
start(agentId: string): Promise<void>;
|
|
33
|
+
stop(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Log an entry to the session's short-term memory.
|
|
36
|
+
* Convenience method that delegates to STM.
|
|
37
|
+
*/
|
|
38
|
+
log(sessionId: string, entry: Omit<MemoryEntry, 'id' | 'sessionId' | 'agentId' | 'timestamp'>): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Build the full context for an LLM call by merging:
|
|
41
|
+
* 1. STM: current session messages
|
|
42
|
+
* 2. MTM: relevant recent episodes
|
|
43
|
+
* 3. LTM: semantically relevant knowledge (if embedder provided)
|
|
44
|
+
*/
|
|
45
|
+
buildContext(options: {
|
|
46
|
+
sessionId: string;
|
|
47
|
+
queryEmbedding?: number[];
|
|
48
|
+
mtmOptions?: {
|
|
49
|
+
domain?: string;
|
|
50
|
+
tags?: string[];
|
|
51
|
+
limit?: number;
|
|
52
|
+
};
|
|
53
|
+
ltmOptions?: {
|
|
54
|
+
category?: string;
|
|
55
|
+
tags?: string[];
|
|
56
|
+
limit?: number;
|
|
57
|
+
threshold?: number;
|
|
58
|
+
};
|
|
59
|
+
kgOptions?: {
|
|
60
|
+
query?: string;
|
|
61
|
+
entityType?: string;
|
|
62
|
+
limit?: number;
|
|
63
|
+
};
|
|
64
|
+
maxStmEntries?: number;
|
|
65
|
+
}): Promise<ContextBlock[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Summarize a session's STM into an MTM episode.
|
|
68
|
+
* Called on session close or by a background process.
|
|
69
|
+
*/
|
|
70
|
+
summarizeSession(sessionId: string, summary: string, options?: {
|
|
71
|
+
tags?: string[];
|
|
72
|
+
domain?: string;
|
|
73
|
+
metadata?: Record<string, unknown>;
|
|
74
|
+
}): Promise<string>;
|
|
75
|
+
private formatStmEntries;
|
|
76
|
+
private formatMtmEpisodes;
|
|
77
|
+
private formatLtmFacts;
|
|
78
|
+
private formatKgEntities;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|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|
+
import { ShortTermMemory } from './stm.js';
|
|
2
|
+
import { MidTermMemory } from './mtm.js';
|
|
3
|
+
import { LongTermMemory } from './ltm.js';
|
|
4
|
+
import { KnowledgeGraph } from './knowledge-graph.js';
|
|
5
|
+
/**
|
|
6
|
+
* Unified Memory Manager — orchestrates all 3 memory tiers.
|
|
7
|
+
*
|
|
8
|
+
* Provides a single API for agents to interact with memory,
|
|
9
|
+
* plus a buildContext() method that assembles LLM context from
|
|
10
|
+
* all relevant memory sources.
|
|
11
|
+
*/
|
|
12
|
+
export class MemoryManager {
|
|
13
|
+
stm;
|
|
14
|
+
mtm;
|
|
15
|
+
ltm;
|
|
16
|
+
kg;
|
|
17
|
+
agentId = '';
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.stm = new ShortTermMemory(config.stm);
|
|
20
|
+
this.mtm = new MidTermMemory(config.mtm);
|
|
21
|
+
this.ltm = new LongTermMemory(config.ltm);
|
|
22
|
+
this.kg = config.kg ? new KnowledgeGraph(config.kg) : null;
|
|
23
|
+
}
|
|
24
|
+
async start(agentId) {
|
|
25
|
+
this.agentId = agentId;
|
|
26
|
+
const inits = [
|
|
27
|
+
this.stm.start(agentId),
|
|
28
|
+
this.mtm.start(agentId),
|
|
29
|
+
this.ltm.start(agentId),
|
|
30
|
+
];
|
|
31
|
+
if (this.kg)
|
|
32
|
+
inits.push(this.kg.start(agentId));
|
|
33
|
+
await Promise.all(inits);
|
|
34
|
+
}
|
|
35
|
+
async stop() {
|
|
36
|
+
const stops = [
|
|
37
|
+
this.stm.stop(),
|
|
38
|
+
this.mtm.stop(),
|
|
39
|
+
this.ltm.stop(),
|
|
40
|
+
];
|
|
41
|
+
if (this.kg)
|
|
42
|
+
stops.push(this.kg.stop());
|
|
43
|
+
await Promise.all(stops);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Log an entry to the session's short-term memory.
|
|
47
|
+
* Convenience method that delegates to STM.
|
|
48
|
+
*/
|
|
49
|
+
async log(sessionId, entry) {
|
|
50
|
+
await this.stm.log(sessionId, entry);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build the full context for an LLM call by merging:
|
|
54
|
+
* 1. STM: current session messages
|
|
55
|
+
* 2. MTM: relevant recent episodes
|
|
56
|
+
* 3. LTM: semantically relevant knowledge (if embedder provided)
|
|
57
|
+
*/
|
|
58
|
+
async buildContext(options) {
|
|
59
|
+
const blocks = [];
|
|
60
|
+
// STM: session log
|
|
61
|
+
const stmEntries = options.maxStmEntries
|
|
62
|
+
? await this.stm.getRecentEntries(options.sessionId, options.maxStmEntries)
|
|
63
|
+
: await this.stm.getSessionLog(options.sessionId);
|
|
64
|
+
if (stmEntries.length > 0) {
|
|
65
|
+
blocks.push({
|
|
66
|
+
source: 'stm',
|
|
67
|
+
content: this.formatStmEntries(stmEntries),
|
|
68
|
+
metadata: { entryCount: stmEntries.length },
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// MTM: recent episodes
|
|
72
|
+
const episodes = await this.mtm.getRecent({
|
|
73
|
+
domain: options.mtmOptions?.domain,
|
|
74
|
+
tags: options.mtmOptions?.tags,
|
|
75
|
+
limit: options.mtmOptions?.limit ?? 5,
|
|
76
|
+
});
|
|
77
|
+
if (episodes.length > 0) {
|
|
78
|
+
blocks.push({
|
|
79
|
+
source: 'mtm',
|
|
80
|
+
content: this.formatMtmEpisodes(episodes),
|
|
81
|
+
metadata: { episodeCount: episodes.length },
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// LTM: semantic knowledge
|
|
85
|
+
if (options.queryEmbedding) {
|
|
86
|
+
const facts = await this.ltm.search(options.queryEmbedding, {
|
|
87
|
+
category: options.ltmOptions?.category,
|
|
88
|
+
tags: options.ltmOptions?.tags,
|
|
89
|
+
limit: options.ltmOptions?.limit ?? 5,
|
|
90
|
+
threshold: options.ltmOptions?.threshold ?? 0.7,
|
|
91
|
+
});
|
|
92
|
+
if (facts.length > 0) {
|
|
93
|
+
blocks.push({
|
|
94
|
+
source: 'ltm',
|
|
95
|
+
content: this.formatLtmFacts(facts),
|
|
96
|
+
metadata: { factCount: facts.length },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// KG: knowledge graph entities & relations
|
|
101
|
+
if (this.kg && options.kgOptions?.query) {
|
|
102
|
+
const entities = await this.kg.searchEntities({
|
|
103
|
+
query: options.kgOptions.query,
|
|
104
|
+
entityType: options.kgOptions.entityType,
|
|
105
|
+
limit: options.kgOptions.limit ?? 5,
|
|
106
|
+
});
|
|
107
|
+
if (entities.length > 0) {
|
|
108
|
+
const entitiesWithRelations = await Promise.all(entities.map(async (entity) => {
|
|
109
|
+
const relations = await this.kg.getEntityRelations(entity.name);
|
|
110
|
+
return { entity, relations };
|
|
111
|
+
}));
|
|
112
|
+
blocks.push({
|
|
113
|
+
source: 'kg',
|
|
114
|
+
content: this.formatKgEntities(entitiesWithRelations),
|
|
115
|
+
metadata: { entityCount: entities.length },
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return blocks;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Summarize a session's STM into an MTM episode.
|
|
123
|
+
* Called on session close or by a background process.
|
|
124
|
+
*/
|
|
125
|
+
async summarizeSession(sessionId, summary, options = {}) {
|
|
126
|
+
const stmEntries = await this.stm.getSessionLog(sessionId);
|
|
127
|
+
const startedAt = stmEntries.length > 0
|
|
128
|
+
? new Date(stmEntries[0].timestamp)
|
|
129
|
+
: new Date();
|
|
130
|
+
const endedAt = stmEntries.length > 0
|
|
131
|
+
? new Date(stmEntries[stmEntries.length - 1].timestamp)
|
|
132
|
+
: new Date();
|
|
133
|
+
return this.mtm.storeSummary({
|
|
134
|
+
agentId: this.agentId,
|
|
135
|
+
sessionId,
|
|
136
|
+
summary,
|
|
137
|
+
tags: options.tags ?? [],
|
|
138
|
+
domain: options.domain ?? null,
|
|
139
|
+
startedAt,
|
|
140
|
+
endedAt,
|
|
141
|
+
metadata: options.metadata ?? {},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
// ─── Formatters ───
|
|
145
|
+
formatStmEntries(entries) {
|
|
146
|
+
return entries
|
|
147
|
+
.map((e) => {
|
|
148
|
+
const ts = new Date(e.timestamp).toISOString();
|
|
149
|
+
return `[${ts}] [${e.type}] ${e.content}`;
|
|
150
|
+
})
|
|
151
|
+
.join('\n');
|
|
152
|
+
}
|
|
153
|
+
formatMtmEpisodes(episodes) {
|
|
154
|
+
return episodes
|
|
155
|
+
.map((ep) => {
|
|
156
|
+
const date = ep.startedAt.toISOString().split('T')[0];
|
|
157
|
+
const tags = ep.tags.length > 0 ? ` [${ep.tags.join(', ')}]` : '';
|
|
158
|
+
return `[${date}]${tags} ${ep.summary}`;
|
|
159
|
+
})
|
|
160
|
+
.join('\n\n');
|
|
161
|
+
}
|
|
162
|
+
formatLtmFacts(facts) {
|
|
163
|
+
return facts
|
|
164
|
+
.map((f) => `[${f.category}] (relevance: ${(f.similarity * 100).toFixed(0)}%) ${f.content}`)
|
|
165
|
+
.join('\n\n');
|
|
166
|
+
}
|
|
167
|
+
formatKgEntities(items) {
|
|
168
|
+
return items
|
|
169
|
+
.map(({ entity, relations }) => {
|
|
170
|
+
const lines = [
|
|
171
|
+
`[${entity.entityType}] ${entity.name}`,
|
|
172
|
+
...entity.observations.map((o) => ` - ${o}`),
|
|
173
|
+
];
|
|
174
|
+
for (const r of relations) {
|
|
175
|
+
lines.push(` → ${r.fromName} -[${r.relationType}]-> ${r.toName}`);
|
|
176
|
+
}
|
|
177
|
+
return lines.join('\n');
|
|
178
|
+
})
|
|
179
|
+
.join('\n\n');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAkB,MAAM,UAAU,CA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,KAAK;aACT,GAAG,CACF,CAAC,CAAC,EAAE,EAAE,CACJ,IAAI,CAAC,CAAC,QAAQ,iBAAiB,CAAC,CAAC,CAAC,UAAU,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAClF;aACA,IAAI,CAAC,MAAM,CAAC,CAAC;IAClB,CAAC;IAEO,gBAAgB,CACtB,KAGE;QAEF,OAAO,KAAK;aACT,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,EAAE;YAC7B,MAAM,KAAK,GAAG;gBACZ,IAAI,MAAM,CAAC,UAAU,KAAK,MAAM,CAAC,IAAI,EAAE;gBACvC,GAAG,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;aAC9C,CAAC;YACF,KAAK,MAAM,CAAC,IAAI,SAAS,EAAE,CAAC;gBAC1B,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,MAAM,CAAC,CAAC,YAAY,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;YACrE,CAAC;YACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC,CAAC;aACD,IAAI,CAAC,MAAM,CAAC,CAAC;IAClB,CAAC;CACF"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { KnowledgeGraph } from './knowledge-graph.js';
|
|
2
|
+
import type { MemoryEntry } from '@mce-bt/microagents-core';
|
|
3
|
+
export interface ExtractedEntity {
|
|
4
|
+
name: string;
|
|
5
|
+
entityType: string;
|
|
6
|
+
observations: string[];
|
|
7
|
+
}
|
|
8
|
+
export interface ExtractedRelation {
|
|
9
|
+
from: string;
|
|
10
|
+
to: string;
|
|
11
|
+
relationType: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ExtractionResult {
|
|
14
|
+
entities: ExtractedEntity[];
|
|
15
|
+
relations: ExtractedRelation[];
|
|
16
|
+
}
|
|
17
|
+
export interface ExtractionStats {
|
|
18
|
+
entitiesCreated: number;
|
|
19
|
+
entitiesUpdated: number;
|
|
20
|
+
relationsCreated: number;
|
|
21
|
+
relationsSuperseded: number;
|
|
22
|
+
durationMs: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* LLM adapter interface for memory extraction.
|
|
26
|
+
* The extraction pipeline calls this to extract structured data from text.
|
|
27
|
+
*/
|
|
28
|
+
export interface ExtractionLLMAdapter {
|
|
29
|
+
/**
|
|
30
|
+
* Extract entities and relations from conversation text.
|
|
31
|
+
* Must return valid JSON matching ExtractionResult.
|
|
32
|
+
*/
|
|
33
|
+
extract(systemPrompt: string, text: string): Promise<ExtractionResult>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Simple extraction adapter that uses a completion function.
|
|
37
|
+
* Wraps any LLM completion call into the ExtractionLLMAdapter interface.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createExtractionAdapter(completeFn: (messages: Array<{
|
|
40
|
+
role: string;
|
|
41
|
+
content: string;
|
|
42
|
+
}>) => Promise<string>): ExtractionLLMAdapter;
|
|
43
|
+
/**
|
|
44
|
+
* Memory Extraction Pipeline — Extracts structured knowledge from conversations.
|
|
45
|
+
*
|
|
46
|
+
* Inspired by Mem0's automatic extraction pattern. Uses an LLM to analyze
|
|
47
|
+
* conversation logs and extract entities, observations, and relations,
|
|
48
|
+
* then stores them in the knowledge graph with temporal validity.
|
|
49
|
+
*/
|
|
50
|
+
export declare class MemoryExtractor {
|
|
51
|
+
private kg;
|
|
52
|
+
private llm;
|
|
53
|
+
private logger?;
|
|
54
|
+
constructor(kg: KnowledgeGraph, llm: ExtractionLLMAdapter, logger?: MemoryExtractor['logger']);
|
|
55
|
+
/**
|
|
56
|
+
* Extract knowledge from a list of memory entries (e.g., a session log).
|
|
57
|
+
* Formats entries into text, sends to LLM for extraction, then stores results.
|
|
58
|
+
*/
|
|
59
|
+
extractFromEntries(entries: MemoryEntry[]): Promise<ExtractionStats>;
|
|
60
|
+
/**
|
|
61
|
+
* Extract knowledge from raw text (e.g., a document or summary).
|
|
62
|
+
*/
|
|
63
|
+
extractFromText(text: string): Promise<ExtractionStats>;
|
|
64
|
+
/**
|
|
65
|
+
* Extract with contradiction handling — supersedes existing relations
|
|
66
|
+
* when the LLM finds conflicting facts.
|
|
67
|
+
*/
|
|
68
|
+
extractWithContradictions(entries: MemoryEntry[], existingContext?: string): Promise<ExtractionStats>;
|
|
69
|
+
private formatEntries;
|
|
70
|
+
/**
|
|
71
|
+
* Build a text summary of existing knowledge for contradiction detection context.
|
|
72
|
+
*/
|
|
73
|
+
buildExistingContext(entityNames: string[]): Promise<string>;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=memory-extractor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-extractor.d.ts","sourceRoot":"","sources":["../src/memory-extractor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAU,MAAM,sBAAsB,CAAC;AACnE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAI5D,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,SAAS,EAAE,iBAAiB,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC;IACxB,gBAAgB,EAAE,MAAM,CAAC;IACzB,mB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