@hazeljs/rag 0.2.0-beta.1
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 +379 -0
- package/dist/agentic/agentic-rag.service.d.ts +49 -0
- package/dist/agentic/agentic-rag.service.d.ts.map +1 -0
- package/dist/agentic/agentic-rag.service.js +149 -0
- package/dist/agentic/agentic-rag.service.js.map +1 -0
- package/dist/agentic/decorators/active-learning.decorator.d.ts +19 -0
- package/dist/agentic/decorators/active-learning.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/active-learning.decorator.js +98 -0
- package/dist/agentic/decorators/active-learning.decorator.js.map +1 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.d.ts +16 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.js +114 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.js.map +1 -0
- package/dist/agentic/decorators/cached.decorator.d.ts +18 -0
- package/dist/agentic/decorators/cached.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/cached.decorator.js +93 -0
- package/dist/agentic/decorators/cached.decorator.js.map +1 -0
- package/dist/agentic/decorators/context-aware.decorator.d.ts +16 -0
- package/dist/agentic/decorators/context-aware.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/context-aware.decorator.js +169 -0
- package/dist/agentic/decorators/context-aware.decorator.js.map +1 -0
- package/dist/agentic/decorators/corrective-rag.decorator.d.ts +15 -0
- package/dist/agentic/decorators/corrective-rag.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/corrective-rag.decorator.js +146 -0
- package/dist/agentic/decorators/corrective-rag.decorator.js.map +1 -0
- package/dist/agentic/decorators/hyde.decorator.d.ts +14 -0
- package/dist/agentic/decorators/hyde.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/hyde.decorator.js +90 -0
- package/dist/agentic/decorators/hyde.decorator.js.map +1 -0
- package/dist/agentic/decorators/index.d.ts +16 -0
- package/dist/agentic/decorators/index.d.ts.map +1 -0
- package/dist/agentic/decorators/index.js +32 -0
- package/dist/agentic/decorators/index.js.map +1 -0
- package/dist/agentic/decorators/multi-hop.decorator.d.ts +14 -0
- package/dist/agentic/decorators/multi-hop.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/multi-hop.decorator.js +120 -0
- package/dist/agentic/decorators/multi-hop.decorator.js.map +1 -0
- package/dist/agentic/decorators/query-planner.decorator.d.ts +19 -0
- package/dist/agentic/decorators/query-planner.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/query-planner.decorator.js +227 -0
- package/dist/agentic/decorators/query-planner.decorator.js.map +1 -0
- package/dist/agentic/decorators/query-rewriter.decorator.d.ts +15 -0
- package/dist/agentic/decorators/query-rewriter.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/query-rewriter.decorator.js +140 -0
- package/dist/agentic/decorators/query-rewriter.decorator.js.map +1 -0
- package/dist/agentic/decorators/self-reflective.decorator.d.ts +18 -0
- package/dist/agentic/decorators/self-reflective.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/self-reflective.decorator.js +210 -0
- package/dist/agentic/decorators/self-reflective.decorator.js.map +1 -0
- package/dist/agentic/decorators/source-verification.decorator.d.ts +15 -0
- package/dist/agentic/decorators/source-verification.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/source-verification.decorator.js +121 -0
- package/dist/agentic/decorators/source-verification.decorator.js.map +1 -0
- package/dist/agentic/index.d.ts +9 -0
- package/dist/agentic/index.d.ts.map +1 -0
- package/dist/agentic/index.js +25 -0
- package/dist/agentic/index.js.map +1 -0
- package/dist/agentic/types.d.ts +209 -0
- package/dist/agentic/types.d.ts.map +1 -0
- package/dist/agentic/types.js +7 -0
- package/dist/agentic/types.js.map +1 -0
- package/dist/decorators/embeddable.decorator.d.ts +31 -0
- package/dist/decorators/embeddable.decorator.d.ts.map +1 -0
- package/dist/decorators/embeddable.decorator.js +44 -0
- package/dist/decorators/embeddable.decorator.js.map +1 -0
- package/dist/decorators/rag.decorator.d.ts +58 -0
- package/dist/decorators/rag.decorator.d.ts.map +1 -0
- package/dist/decorators/rag.decorator.js +78 -0
- package/dist/decorators/rag.decorator.js.map +1 -0
- package/dist/decorators/semantic-search.decorator.d.ts +69 -0
- package/dist/decorators/semantic-search.decorator.d.ts.map +1 -0
- package/dist/decorators/semantic-search.decorator.js +116 -0
- package/dist/decorators/semantic-search.decorator.js.map +1 -0
- package/dist/embeddings/cohere-embeddings.d.ts +33 -0
- package/dist/embeddings/cohere-embeddings.d.ts.map +1 -0
- package/dist/embeddings/cohere-embeddings.js +91 -0
- package/dist/embeddings/cohere-embeddings.js.map +1 -0
- package/dist/embeddings/openai-embeddings.d.ts +21 -0
- package/dist/embeddings/openai-embeddings.d.ts.map +1 -0
- package/dist/embeddings/openai-embeddings.js +53 -0
- package/dist/embeddings/openai-embeddings.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/index.d.ts +11 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +31 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/memory-manager.d.ts +96 -0
- package/dist/memory/memory-manager.d.ts.map +1 -0
- package/dist/memory/memory-manager.js +369 -0
- package/dist/memory/memory-manager.js.map +1 -0
- package/dist/memory/memory-store.interface.d.ts +73 -0
- package/dist/memory/memory-store.interface.d.ts.map +1 -0
- package/dist/memory/memory-store.interface.js +6 -0
- package/dist/memory/memory-store.interface.js.map +1 -0
- package/dist/memory/stores/buffer-memory.d.ts +47 -0
- package/dist/memory/stores/buffer-memory.d.ts.map +1 -0
- package/dist/memory/stores/buffer-memory.js +280 -0
- package/dist/memory/stores/buffer-memory.js.map +1 -0
- package/dist/memory/stores/hybrid-memory.d.ts +49 -0
- package/dist/memory/stores/hybrid-memory.d.ts.map +1 -0
- package/dist/memory/stores/hybrid-memory.js +194 -0
- package/dist/memory/stores/hybrid-memory.js.map +1 -0
- package/dist/memory/stores/vector-memory.d.ts +48 -0
- package/dist/memory/stores/vector-memory.d.ts.map +1 -0
- package/dist/memory/stores/vector-memory.js +312 -0
- package/dist/memory/stores/vector-memory.js.map +1 -0
- package/dist/memory/types.d.ts +119 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +18 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/rag-pipeline-with-memory.d.ts +68 -0
- package/dist/rag-pipeline-with-memory.d.ts.map +1 -0
- package/dist/rag-pipeline-with-memory.js +186 -0
- package/dist/rag-pipeline-with-memory.js.map +1 -0
- package/dist/rag-pipeline.d.ts +59 -0
- package/dist/rag-pipeline.d.ts.map +1 -0
- package/dist/rag-pipeline.js +181 -0
- package/dist/rag-pipeline.js.map +1 -0
- package/dist/rag.module.d.ts +26 -0
- package/dist/rag.module.d.ts.map +1 -0
- package/dist/rag.module.js +40 -0
- package/dist/rag.module.js.map +1 -0
- package/dist/rag.service.d.ts +95 -0
- package/dist/rag.service.d.ts.map +1 -0
- package/dist/rag.service.js +174 -0
- package/dist/rag.service.js.map +1 -0
- package/dist/retrieval/bm25.d.ts +57 -0
- package/dist/retrieval/bm25.d.ts.map +1 -0
- package/dist/retrieval/bm25.js +106 -0
- package/dist/retrieval/bm25.js.map +1 -0
- package/dist/retrieval/hybrid-search.d.ts +48 -0
- package/dist/retrieval/hybrid-search.d.ts.map +1 -0
- package/dist/retrieval/hybrid-search.js +123 -0
- package/dist/retrieval/hybrid-search.js.map +1 -0
- package/dist/retrieval/multi-query.d.ts +38 -0
- package/dist/retrieval/multi-query.d.ts.map +1 -0
- package/dist/retrieval/multi-query.js +135 -0
- package/dist/retrieval/multi-query.js.map +1 -0
- package/dist/text-splitters/recursive-text-splitter.d.ts +21 -0
- package/dist/text-splitters/recursive-text-splitter.d.ts.map +1 -0
- package/dist/text-splitters/recursive-text-splitter.js +95 -0
- package/dist/text-splitters/recursive-text-splitter.js.map +1 -0
- package/dist/types/index.d.ts +144 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +16 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/similarity.d.ts +16 -0
- package/dist/utils/similarity.d.ts.map +1 -0
- package/dist/utils/similarity.js +58 -0
- package/dist/utils/similarity.js.map +1 -0
- package/dist/vector-stores/chroma.store.d.ts +42 -0
- package/dist/vector-stores/chroma.store.d.ts.map +1 -0
- package/dist/vector-stores/chroma.store.js +242 -0
- package/dist/vector-stores/chroma.store.js.map +1 -0
- package/dist/vector-stores/memory-vector-store.d.ts +20 -0
- package/dist/vector-stores/memory-vector-store.d.ts.map +1 -0
- package/dist/vector-stores/memory-vector-store.js +94 -0
- package/dist/vector-stores/memory-vector-store.js.map +1 -0
- package/dist/vector-stores/pinecone.store.d.ts +34 -0
- package/dist/vector-stores/pinecone.store.d.ts.map +1 -0
- package/dist/vector-stores/pinecone.store.js +146 -0
- package/dist/vector-stores/pinecone.store.js.map +1 -0
- package/dist/vector-stores/qdrant.store.d.ts +31 -0
- package/dist/vector-stores/qdrant.store.d.ts.map +1 -0
- package/dist/vector-stores/qdrant.store.js +173 -0
- package/dist/vector-stores/qdrant.store.js.map +1 -0
- package/dist/vector-stores/weaviate.store.d.ts +37 -0
- package/dist/vector-stores/weaviate.store.d.ts.map +1 -0
- package/dist/vector-stores/weaviate.store.js +226 -0
- package/dist/vector-stores/weaviate.store.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Vector Memory Store - Stores memories as embeddings for semantic search
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.VectorMemory = void 0;
|
|
7
|
+
const types_1 = require("../types");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
/**
|
|
10
|
+
* Memory store that uses vector embeddings for semantic search
|
|
11
|
+
* Leverages existing vector store infrastructure
|
|
12
|
+
*/
|
|
13
|
+
class VectorMemory {
|
|
14
|
+
constructor(vectorStore, embeddings, config = {}) {
|
|
15
|
+
this.vectorStore = vectorStore;
|
|
16
|
+
this.embeddings = embeddings;
|
|
17
|
+
this.config = config;
|
|
18
|
+
}
|
|
19
|
+
async initialize() {
|
|
20
|
+
await this.vectorStore.initialize();
|
|
21
|
+
}
|
|
22
|
+
async save(memory) {
|
|
23
|
+
const id = memory.id || (0, crypto_1.randomUUID)();
|
|
24
|
+
// Generate embedding if not provided
|
|
25
|
+
let embedding = memory.embedding;
|
|
26
|
+
if (!embedding) {
|
|
27
|
+
embedding = await this.embeddings.embed(memory.content);
|
|
28
|
+
}
|
|
29
|
+
// Store as document in vector store
|
|
30
|
+
await this.vectorStore.addDocuments([
|
|
31
|
+
{
|
|
32
|
+
id,
|
|
33
|
+
content: memory.content,
|
|
34
|
+
metadata: {
|
|
35
|
+
...memory.metadata,
|
|
36
|
+
memoryType: memory.type,
|
|
37
|
+
timestamp: memory.metadata.timestamp.toISOString(),
|
|
38
|
+
},
|
|
39
|
+
embedding,
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
return id;
|
|
43
|
+
}
|
|
44
|
+
async saveBatch(memories) {
|
|
45
|
+
const ids = [];
|
|
46
|
+
const documents = [];
|
|
47
|
+
for (const memory of memories) {
|
|
48
|
+
const id = memory.id || (0, crypto_1.randomUUID)();
|
|
49
|
+
ids.push(id);
|
|
50
|
+
let embedding = memory.embedding;
|
|
51
|
+
if (!embedding) {
|
|
52
|
+
embedding = await this.embeddings.embed(memory.content);
|
|
53
|
+
}
|
|
54
|
+
documents.push({
|
|
55
|
+
id,
|
|
56
|
+
content: memory.content,
|
|
57
|
+
metadata: {
|
|
58
|
+
...memory.metadata,
|
|
59
|
+
memoryType: memory.type,
|
|
60
|
+
timestamp: memory.metadata.timestamp.toISOString(),
|
|
61
|
+
},
|
|
62
|
+
embedding,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
await this.vectorStore.addDocuments(documents);
|
|
66
|
+
return ids;
|
|
67
|
+
}
|
|
68
|
+
async retrieve(query) {
|
|
69
|
+
// Build metadata filter
|
|
70
|
+
const filter = {};
|
|
71
|
+
if (query.sessionId) {
|
|
72
|
+
filter.sessionId = query.sessionId;
|
|
73
|
+
}
|
|
74
|
+
if (query.userId) {
|
|
75
|
+
filter.userId = query.userId;
|
|
76
|
+
}
|
|
77
|
+
if (query.types && query.types.length > 0) {
|
|
78
|
+
filter.memoryType = query.types;
|
|
79
|
+
}
|
|
80
|
+
// For date range and other filters, we'll need to fetch and filter
|
|
81
|
+
const results = await this.vectorStore.search('', {
|
|
82
|
+
topK: query.limit || 100,
|
|
83
|
+
filter,
|
|
84
|
+
includeMetadata: true,
|
|
85
|
+
includeEmbedding: true,
|
|
86
|
+
});
|
|
87
|
+
// Convert search results to memories
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
+
let memories = results.map((result) => this.searchResultToMemory(result));
|
|
90
|
+
// Apply additional filters
|
|
91
|
+
if (query.startDate) {
|
|
92
|
+
memories = memories.filter((m) => m.metadata.timestamp >= query.startDate);
|
|
93
|
+
}
|
|
94
|
+
if (query.endDate) {
|
|
95
|
+
memories = memories.filter((m) => m.metadata.timestamp <= query.endDate);
|
|
96
|
+
}
|
|
97
|
+
if (query.minImportance !== undefined) {
|
|
98
|
+
memories = memories.filter((m) => (m.metadata.importance || 0) >= query.minImportance);
|
|
99
|
+
}
|
|
100
|
+
if (query.entities && query.entities.length > 0) {
|
|
101
|
+
memories = memories.filter((m) => m.metadata.entities?.some((e) => query.entities.includes(e)));
|
|
102
|
+
}
|
|
103
|
+
// Sort by timestamp
|
|
104
|
+
memories.sort((a, b) => b.metadata.timestamp.getTime() - a.metadata.timestamp.getTime());
|
|
105
|
+
return memories.slice(0, query.limit || 100);
|
|
106
|
+
}
|
|
107
|
+
async search(query, options) {
|
|
108
|
+
// Build metadata filter
|
|
109
|
+
const filter = {};
|
|
110
|
+
if (options.sessionId) {
|
|
111
|
+
filter.sessionId = options.sessionId;
|
|
112
|
+
}
|
|
113
|
+
if (options.userId) {
|
|
114
|
+
filter.userId = options.userId;
|
|
115
|
+
}
|
|
116
|
+
if (options.types && options.types.length > 0) {
|
|
117
|
+
filter.memoryType = options.types;
|
|
118
|
+
}
|
|
119
|
+
// Perform semantic search
|
|
120
|
+
const results = await this.vectorStore.search(query, {
|
|
121
|
+
topK: options.topK || 10,
|
|
122
|
+
filter,
|
|
123
|
+
includeMetadata: true,
|
|
124
|
+
includeEmbedding: options.includeEmbedding,
|
|
125
|
+
minScore: options.minScore,
|
|
126
|
+
});
|
|
127
|
+
// Convert to memories
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
|
+
const memories = results.map((result) => this.searchResultToMemory(result));
|
|
130
|
+
// Apply additional filters
|
|
131
|
+
let filtered = memories;
|
|
132
|
+
if (options.startDate) {
|
|
133
|
+
filtered = filtered.filter((m) => m.metadata.timestamp >= options.startDate);
|
|
134
|
+
}
|
|
135
|
+
if (options.endDate) {
|
|
136
|
+
filtered = filtered.filter((m) => m.metadata.timestamp <= options.endDate);
|
|
137
|
+
}
|
|
138
|
+
if (options.minImportance !== undefined) {
|
|
139
|
+
filtered = filtered.filter((m) => (m.metadata.importance || 0) >= options.minImportance);
|
|
140
|
+
}
|
|
141
|
+
return filtered;
|
|
142
|
+
}
|
|
143
|
+
async get(id) {
|
|
144
|
+
const doc = await this.vectorStore.getDocument(id);
|
|
145
|
+
if (!doc)
|
|
146
|
+
return null;
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
148
|
+
return this.documentToMemory(doc);
|
|
149
|
+
}
|
|
150
|
+
async update(id, updates) {
|
|
151
|
+
const existing = await this.get(id);
|
|
152
|
+
if (!existing) {
|
|
153
|
+
throw new Error(`Memory with id ${id} not found`);
|
|
154
|
+
}
|
|
155
|
+
const updated = {
|
|
156
|
+
...existing,
|
|
157
|
+
...updates,
|
|
158
|
+
metadata: {
|
|
159
|
+
...existing.metadata,
|
|
160
|
+
...updates.metadata,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
// Re-generate embedding if content changed
|
|
164
|
+
let embedding = updated.embedding;
|
|
165
|
+
if (updates.content && updates.content !== existing.content) {
|
|
166
|
+
embedding = await this.embeddings.embed(updated.content);
|
|
167
|
+
}
|
|
168
|
+
await this.vectorStore.updateDocument(id, {
|
|
169
|
+
content: updated.content,
|
|
170
|
+
metadata: {
|
|
171
|
+
...updated.metadata,
|
|
172
|
+
memoryType: updated.type,
|
|
173
|
+
timestamp: updated.metadata.timestamp.toISOString(),
|
|
174
|
+
},
|
|
175
|
+
embedding,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
async delete(id) {
|
|
179
|
+
await this.vectorStore.deleteDocuments([id]);
|
|
180
|
+
}
|
|
181
|
+
async deleteBatch(ids) {
|
|
182
|
+
await this.vectorStore.deleteDocuments(ids);
|
|
183
|
+
}
|
|
184
|
+
async clearSession(sessionId) {
|
|
185
|
+
// Retrieve all memories for session
|
|
186
|
+
const memories = await this.retrieve({ sessionId });
|
|
187
|
+
const ids = memories.map((m) => m.id);
|
|
188
|
+
if (ids.length > 0) {
|
|
189
|
+
await this.deleteBatch(ids);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async clear() {
|
|
193
|
+
await this.vectorStore.clear();
|
|
194
|
+
}
|
|
195
|
+
async summarize(options) {
|
|
196
|
+
const memories = await this.retrieve({
|
|
197
|
+
sessionId: options.sessionId,
|
|
198
|
+
limit: options.maxLength || 50,
|
|
199
|
+
});
|
|
200
|
+
if (memories.length === 0) {
|
|
201
|
+
return 'No memories to summarize.';
|
|
202
|
+
}
|
|
203
|
+
const contents = memories.map((m) => m.content).join('\n');
|
|
204
|
+
return contents.slice(0, options.maxLength || 1000);
|
|
205
|
+
}
|
|
206
|
+
async consolidate(memories) {
|
|
207
|
+
if (memories.length === 0) {
|
|
208
|
+
throw new Error('No memories to consolidate');
|
|
209
|
+
}
|
|
210
|
+
const consolidated = {
|
|
211
|
+
id: (0, crypto_1.randomUUID)(),
|
|
212
|
+
type: memories[0].type,
|
|
213
|
+
content: memories.map((m) => m.content).join('\n\n'),
|
|
214
|
+
metadata: {
|
|
215
|
+
...memories[0].metadata,
|
|
216
|
+
timestamp: new Date(),
|
|
217
|
+
consolidated: true,
|
|
218
|
+
sourceIds: memories.map((m) => m.id),
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
// Save consolidated memory
|
|
222
|
+
await this.save(consolidated);
|
|
223
|
+
// Optionally delete source memories
|
|
224
|
+
// await this.deleteBatch(memories.map(m => m.id));
|
|
225
|
+
return consolidated;
|
|
226
|
+
}
|
|
227
|
+
async getStats(sessionId) {
|
|
228
|
+
const memories = await this.retrieve({
|
|
229
|
+
sessionId,
|
|
230
|
+
limit: 10000, // Large limit to get all
|
|
231
|
+
});
|
|
232
|
+
const byType = {
|
|
233
|
+
[types_1.MemoryType.CONVERSATION]: 0,
|
|
234
|
+
[types_1.MemoryType.ENTITY]: 0,
|
|
235
|
+
[types_1.MemoryType.FACT]: 0,
|
|
236
|
+
[types_1.MemoryType.EVENT]: 0,
|
|
237
|
+
[types_1.MemoryType.WORKING]: 0,
|
|
238
|
+
};
|
|
239
|
+
let totalImportance = 0;
|
|
240
|
+
let oldestTime = Date.now();
|
|
241
|
+
let newestTime = 0;
|
|
242
|
+
for (const memory of memories) {
|
|
243
|
+
byType[memory.type]++;
|
|
244
|
+
totalImportance += memory.metadata.importance || 0;
|
|
245
|
+
const time = memory.metadata.timestamp.getTime();
|
|
246
|
+
if (time < oldestTime)
|
|
247
|
+
oldestTime = time;
|
|
248
|
+
if (time > newestTime)
|
|
249
|
+
newestTime = time;
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
totalMemories: memories.length,
|
|
253
|
+
byType,
|
|
254
|
+
oldestMemory: new Date(oldestTime),
|
|
255
|
+
newestMemory: new Date(newestTime),
|
|
256
|
+
averageImportance: memories.length > 0 ? totalImportance / memories.length : 0,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
async prune(options) {
|
|
260
|
+
const memories = await this.retrieve({ limit: 10000 });
|
|
261
|
+
const toDelete = [];
|
|
262
|
+
for (const memory of memories) {
|
|
263
|
+
let shouldPrune = false;
|
|
264
|
+
if (options?.olderThan && memory.metadata.timestamp < options.olderThan) {
|
|
265
|
+
shouldPrune = true;
|
|
266
|
+
}
|
|
267
|
+
if (options?.minImportance !== undefined &&
|
|
268
|
+
(memory.metadata.importance || 0) < options.minImportance) {
|
|
269
|
+
shouldPrune = true;
|
|
270
|
+
}
|
|
271
|
+
if (shouldPrune) {
|
|
272
|
+
toDelete.push(memory.id);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (toDelete.length > 0) {
|
|
276
|
+
await this.deleteBatch(toDelete);
|
|
277
|
+
}
|
|
278
|
+
return toDelete.length;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Convert search result to memory
|
|
282
|
+
*/
|
|
283
|
+
searchResultToMemory(result) {
|
|
284
|
+
return {
|
|
285
|
+
id: result.id,
|
|
286
|
+
type: result.metadata.memoryType,
|
|
287
|
+
content: result.content,
|
|
288
|
+
metadata: {
|
|
289
|
+
...result.metadata,
|
|
290
|
+
timestamp: new Date(result.metadata.timestamp),
|
|
291
|
+
},
|
|
292
|
+
embedding: result.embedding,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Convert document to memory
|
|
297
|
+
*/
|
|
298
|
+
documentToMemory(doc) {
|
|
299
|
+
return {
|
|
300
|
+
id: doc.id,
|
|
301
|
+
type: doc.metadata.memoryType,
|
|
302
|
+
content: doc.content,
|
|
303
|
+
metadata: {
|
|
304
|
+
...doc.metadata,
|
|
305
|
+
timestamp: new Date(doc.metadata.timestamp),
|
|
306
|
+
},
|
|
307
|
+
embedding: doc.embedding,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
exports.VectorMemory = VectorMemory;
|
|
312
|
+
//# sourceMappingURL=vector-memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector-memory.js","sourceRoot":"","sources":["../../../src/memory/stores/vector-memory.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAGH,oCAOkB;AAGlB,mCAAoC;AAOpC;;;GAGG;AACH,MAAa,YAAY;IAKvB,YACE,WAAwB,EACxB,UAA6B,EAC7B,SAA6B,EAAE;QAE/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,MAAc;QACvB,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,IAAI,IAAA,mBAAU,GAAE,CAAC;QAErC,qCAAqC;QACrC,IAAI,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QACjC,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,SAAS,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1D,CAAC;QAED,oCAAoC;QACpC,MAAM,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC;YAClC;gBACE,EAAE;gBACF,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,QAAQ,EAAE;oBACR,GAAG,MAAM,CAAC,QAAQ;oBAClB,UAAU,EAAE,MAAM,CAAC,IAAI;oBACvB,SAAS,EAAE,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,WAAW,EAAE;iBACnD;gBACD,SAAS;aACV;SACF,CAAC,CAAC;QAEH,OAAO,EAAE,C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G,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,QAAQ,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;OAEG;IACK,oBAAoB,CAAC,MAK5B;QACC,OAAO;YACL,EAAE,EAAE,MAAM,CAAC,EAAE;YACb,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,UAAwB;YAC9C,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,QAAQ,EAAE;gBACR,GAAG,MAAM,CAAC,QAAQ;gBAClB,SAAS,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC;aAC/C;YACD,SAAS,EAAE,MAAM,CAAC,SAAS;SAC5B,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,gBAAgB,CAAC,GAKxB;QACC,OAAO;YACL,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,IAAI,EAAE,GAAG,CAAC,QAAQ,CAAC,UAAwB;YAC3C,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,QAAQ,EAAE;gBACR,GAAG,GAAG,CAAC,QAAQ;gBACf,SAAS,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC;aAC5C;YACD,SAAS,EAAE,GAAG,CAAC,SAAS;SACzB,CAAC;IACJ,CAAC;CACF;AArXD,oCAqXC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory types and interfaces for persistent context management
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Types of memory
|
|
6
|
+
*/
|
|
7
|
+
export declare enum MemoryType {
|
|
8
|
+
CONVERSATION = "conversation",
|
|
9
|
+
ENTITY = "entity",
|
|
10
|
+
FACT = "fact",
|
|
11
|
+
EVENT = "event",
|
|
12
|
+
WORKING = "working"
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Message in a conversation
|
|
16
|
+
*/
|
|
17
|
+
export interface Message {
|
|
18
|
+
role: 'user' | 'assistant' | 'system';
|
|
19
|
+
content: string;
|
|
20
|
+
timestamp?: Date;
|
|
21
|
+
metadata?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Memory object
|
|
25
|
+
*/
|
|
26
|
+
export interface Memory {
|
|
27
|
+
id: string;
|
|
28
|
+
type: MemoryType;
|
|
29
|
+
content: string;
|
|
30
|
+
metadata: {
|
|
31
|
+
timestamp: Date;
|
|
32
|
+
userId?: string;
|
|
33
|
+
sessionId?: string;
|
|
34
|
+
importance?: number;
|
|
35
|
+
entities?: string[];
|
|
36
|
+
expiresAt?: Date;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
};
|
|
39
|
+
embedding?: number[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Entity tracked in memory
|
|
43
|
+
*/
|
|
44
|
+
export interface Entity {
|
|
45
|
+
name: string;
|
|
46
|
+
type: string;
|
|
47
|
+
attributes: Record<string, unknown>;
|
|
48
|
+
relationships: Array<{
|
|
49
|
+
type: string;
|
|
50
|
+
target: string;
|
|
51
|
+
}>;
|
|
52
|
+
firstSeen: Date;
|
|
53
|
+
lastSeen: Date;
|
|
54
|
+
mentions: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Memory query options
|
|
58
|
+
*/
|
|
59
|
+
export interface MemoryQuery {
|
|
60
|
+
sessionId?: string;
|
|
61
|
+
userId?: string;
|
|
62
|
+
types?: MemoryType[];
|
|
63
|
+
startDate?: Date;
|
|
64
|
+
endDate?: Date;
|
|
65
|
+
limit?: number;
|
|
66
|
+
minImportance?: number;
|
|
67
|
+
entities?: string[];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Memory search options
|
|
71
|
+
*/
|
|
72
|
+
export interface MemorySearchOptions extends MemoryQuery {
|
|
73
|
+
topK?: number;
|
|
74
|
+
minScore?: number;
|
|
75
|
+
includeEmbedding?: boolean;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Summarization options
|
|
79
|
+
*/
|
|
80
|
+
export interface SummarizeOptions {
|
|
81
|
+
sessionId?: string;
|
|
82
|
+
maxLength?: number;
|
|
83
|
+
strategy?: 'extractive' | 'abstractive';
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Memory configuration
|
|
87
|
+
*/
|
|
88
|
+
export interface MemoryConfig {
|
|
89
|
+
maxConversationLength?: number;
|
|
90
|
+
summarizeAfter?: number;
|
|
91
|
+
entityExtraction?: boolean;
|
|
92
|
+
importanceScoring?: boolean;
|
|
93
|
+
memoryDecay?: boolean;
|
|
94
|
+
decayRate?: number;
|
|
95
|
+
maxWorkingMemorySize?: number;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Conversation summary
|
|
99
|
+
*/
|
|
100
|
+
export interface ConversationSummary {
|
|
101
|
+
sessionId: string;
|
|
102
|
+
summary: string;
|
|
103
|
+
messageCount: number;
|
|
104
|
+
startTime: Date;
|
|
105
|
+
endTime: Date;
|
|
106
|
+
entities: string[];
|
|
107
|
+
topics: string[];
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Memory statistics
|
|
111
|
+
*/
|
|
112
|
+
export interface MemoryStats {
|
|
113
|
+
totalMemories: number;
|
|
114
|
+
byType: Record<MemoryType, number>;
|
|
115
|
+
oldestMemory: Date;
|
|
116
|
+
newestMemory: Date;
|
|
117
|
+
averageImportance: number;
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/memory/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,oBAAY,UAAU;IACpB,YAAY,iBAAiB;IAC7B,MAAM,WAAW;IACjB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,OAAO,YAAY;CACpB;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B,YAAY,EAAE,IAAI,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;CAC3B"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Memory types and interfaces for persistent context management
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MemoryType = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Types of memory
|
|
9
|
+
*/
|
|
10
|
+
var MemoryType;
|
|
11
|
+
(function (MemoryType) {
|
|
12
|
+
MemoryType["CONVERSATION"] = "conversation";
|
|
13
|
+
MemoryType["ENTITY"] = "entity";
|
|
14
|
+
MemoryType["FACT"] = "fact";
|
|
15
|
+
MemoryType["EVENT"] = "event";
|
|
16
|
+
MemoryType["WORKING"] = "working";
|
|
17
|
+
})(MemoryType || (exports.MemoryType = MemoryType = {}));
|
|
18
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/memory/types.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH;;GAEG;AACH,IAAY,UAMX;AAND,WAAY,UAAU;IACpB,2CAA6B,CAAA;IAC7B,+BAAiB,CAAA;IACjB,2BAAa,CAAA;IACb,6BAAe,CAAA;IACf,iCAAmB,CAAA;AACrB,CAAC,EANW,UAAU,0BAAV,UAAU,QAMrB"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RAG Pipeline with Memory Integration
|
|
3
|
+
*/
|
|
4
|
+
import { RAGPipeline } from './rag-pipeline';
|
|
5
|
+
import { RAGConfig, RAGQueryOptions, RAGResponse } from './types';
|
|
6
|
+
import { MemoryManager } from './memory';
|
|
7
|
+
import { Message } from './memory/types';
|
|
8
|
+
/**
|
|
9
|
+
* Extended RAG response with memory context
|
|
10
|
+
*/
|
|
11
|
+
export interface RAGResponseWithMemory extends RAGResponse {
|
|
12
|
+
memories: import('./memory/types').Memory[];
|
|
13
|
+
conversationHistory: Message[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* RAG Pipeline enhanced with memory capabilities
|
|
17
|
+
* Combines document retrieval with conversation history and persistent memory
|
|
18
|
+
*/
|
|
19
|
+
export declare class RAGPipelineWithMemory extends RAGPipeline {
|
|
20
|
+
private memoryManager;
|
|
21
|
+
protected llmFunc?: (prompt: string) => Promise<string>;
|
|
22
|
+
constructor(config: RAGConfig, memoryManager: MemoryManager, llmFunction?: (prompt: string) => Promise<string>);
|
|
23
|
+
/**
|
|
24
|
+
* Initialize both RAG and memory systems
|
|
25
|
+
*/
|
|
26
|
+
initialize(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Query with memory-enhanced context
|
|
29
|
+
*/
|
|
30
|
+
queryWithMemory(query: string, sessionId: string, userId?: string, options?: RAGQueryOptions): Promise<RAGResponseWithMemory>;
|
|
31
|
+
/**
|
|
32
|
+
* Query with automatic fact extraction and storage
|
|
33
|
+
*/
|
|
34
|
+
queryWithLearning(query: string, sessionId: string, userId?: string, options?: RAGQueryOptions): Promise<RAGResponseWithMemory>;
|
|
35
|
+
/**
|
|
36
|
+
* Clear all memory for a session
|
|
37
|
+
*/
|
|
38
|
+
clearSessionMemory(sessionId: string): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Get conversation summary
|
|
41
|
+
*/
|
|
42
|
+
getConversationSummary(sessionId: string): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Store a fact in memory
|
|
45
|
+
*/
|
|
46
|
+
storeFact(fact: string, sessionId?: string, userId?: string): Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Recall facts related to a query
|
|
49
|
+
*/
|
|
50
|
+
recallFacts(query: string, topK?: number): Promise<string[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Get memory statistics
|
|
53
|
+
*/
|
|
54
|
+
getMemoryStats(sessionId?: string): Promise<import('./memory/types').MemoryStats>;
|
|
55
|
+
/**
|
|
56
|
+
* Build enhanced context combining memories, conversation, and RAG results
|
|
57
|
+
*/
|
|
58
|
+
private buildEnhancedContext;
|
|
59
|
+
/**
|
|
60
|
+
* Build prompt with memory context
|
|
61
|
+
*/
|
|
62
|
+
private buildPromptWithMemory;
|
|
63
|
+
/**
|
|
64
|
+
* Extract and store important facts from text
|
|
65
|
+
*/
|
|
66
|
+
private extractAndStoreFacts;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=rag-pipeline-with-memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rag-pipeline-with-memory.d.ts","sourceRoot":"","sources":["../src/rag-pipeline-with-memory.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,WAAW,EAAgB,MAAM,SAAS,CAAC;AAChF,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAc,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,qBAAsB,SAAQ,WAAW;IACxD,QAAQ,EAAE,OAAO,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC5C,mBAAmB,EAAE,OAAO,EAAE,CAAC;CAChC;AAED;;;GAGG;AACH,qBAAa,qBAAsB,SAAQ,WAAW;IACpD,OAAO,CAAC,aAAa,CAAgB;IACrC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;gBAGtD,MAAM,EAAE,SAAS,EACjB,aAAa,EAAE,aAAa,EAC5B,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC;IAOnD;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAKjC;;OAEG;IACG,e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