@smyslenny/agent-memory 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { D as DbOptions, o as openDatabase } from './db-CMsKtBt0.js';
1
+ export { D as DbOptions, i as isCountRow, o as openDatabase } from './db-DsY3zz8f.js';
2
2
  import Database from 'better-sqlite3';
3
3
 
4
4
  type MemoryType = "identity" | "emotion" | "knowledge" | "event";
@@ -59,6 +59,7 @@ declare function countMemories(db: Database.Database, agent_id?: string): {
59
59
  interface Path {
60
60
  id: string;
61
61
  memory_id: string;
62
+ agent_id: string;
62
63
  uri: string;
63
64
  alias: string | null;
64
65
  domain: string;
@@ -68,35 +69,36 @@ declare function parseUri(uri: string): {
68
69
  domain: string;
69
70
  path: string;
70
71
  };
71
- declare function createPath(db: Database.Database, memoryId: string, uri: string, alias?: string, validDomains?: Set<string>): Path;
72
+ declare function createPath(db: Database.Database, memoryId: string, uri: string, alias?: string, validDomains?: Set<string>, agent_id?: string): Path;
72
73
  declare function getPath(db: Database.Database, id: string): Path | null;
73
- declare function getPathByUri(db: Database.Database, uri: string): Path | null;
74
+ declare function getPathByUri(db: Database.Database, uri: string, agent_id?: string): Path | null;
74
75
  declare function getPathsByMemory(db: Database.Database, memoryId: string): Path[];
75
- declare function getPathsByDomain(db: Database.Database, domain: string): Path[];
76
- declare function getPathsByPrefix(db: Database.Database, prefix: string): Path[];
76
+ declare function getPathsByDomain(db: Database.Database, domain: string, agent_id?: string): Path[];
77
+ declare function getPathsByPrefix(db: Database.Database, prefix: string, agent_id?: string): Path[];
77
78
  declare function deletePath(db: Database.Database, id: string): boolean;
78
79
 
79
80
  type RelationType = "related" | "caused" | "reminds" | "evolved" | "contradicts";
80
81
  interface Link {
82
+ agent_id: string;
81
83
  source_id: string;
82
84
  target_id: string;
83
85
  relation: RelationType;
84
86
  weight: number;
85
87
  created_at: string;
86
88
  }
87
- declare function createLink(db: Database.Database, sourceId: string, targetId: string, relation: RelationType, weight?: number): Link;
88
- declare function getLinks(db: Database.Database, memoryId: string): Link[];
89
- declare function getOutgoingLinks(db: Database.Database, sourceId: string): Link[];
89
+ declare function createLink(db: Database.Database, sourceId: string, targetId: string, relation: RelationType, weight?: number, agent_id?: string): Link;
90
+ declare function getLinks(db: Database.Database, memoryId: string, agent_id?: string): Link[];
91
+ declare function getOutgoingLinks(db: Database.Database, sourceId: string, agent_id?: string): Link[];
90
92
  /**
91
93
  * Multi-hop traversal: find all memories reachable within N hops
92
94
  * Inspired by PowerMem's knowledge graph traversal
93
95
  */
94
- declare function traverse(db: Database.Database, startId: string, maxHops?: number): Array<{
96
+ declare function traverse(db: Database.Database, startId: string, maxHops?: number, agent_id?: string): Array<{
95
97
  id: string;
96
98
  hop: number;
97
99
  relation: string;
98
100
  }>;
99
- declare function deleteLink(db: Database.Database, sourceId: string, targetId: string): boolean;
101
+ declare function deleteLink(db: Database.Database, sourceId: string, targetId: string, agent_id?: string): boolean;
100
102
 
101
103
  type SnapshotAction = "create" | "update" | "delete" | "merge";
102
104
  interface Snapshot {
@@ -133,13 +135,25 @@ interface GuardResult {
133
135
  * Pipeline:
134
136
  * 1. Hash dedup (exact content match → skip)
135
137
  * 2. URI conflict (URI exists → update path)
136
- * 3. BM25 similarity (>0.85 conflict detection → merge or update)
137
- * 4. Four-criterion gate (for P0/P1 only)
138
+ * 3. BM25 similarity (dynamic threshold → merge or update)
139
+ * 4. Four-criterion gate: Specificity, Novelty, Relevance, Coherence
138
140
  */
139
141
  declare function guard(db: Database.Database, input: CreateMemoryInput & {
140
142
  uri?: string;
141
143
  }): GuardResult;
142
144
 
145
+ interface ExportResult {
146
+ exported: number;
147
+ files: string[];
148
+ }
149
+ /**
150
+ * Export all memories to Markdown files in the given directory.
151
+ * Creates MEMORY.md for identity/emotion/knowledge and daily .md files for events.
152
+ */
153
+ declare function exportMemories(db: Database.Database, dirPath: string, opts?: {
154
+ agent_id?: string;
155
+ }): ExportResult;
156
+
143
157
  interface SearchResult {
144
158
  memory: Memory;
145
159
  score: number;
@@ -155,6 +169,14 @@ declare function searchBM25(db: Database.Database, query: string, opts?: {
155
169
  min_vitality?: number;
156
170
  }): SearchResult[];
157
171
 
172
+ /**
173
+ * Tokenize text for FTS5 queries.
174
+ * - Latin/numeric words: split on whitespace, filter len > 1
175
+ * - CJK text: use jieba cutForSearch, fallback to unigram + bigram
176
+ * Returns deduplicated token array, max 30 tokens.
177
+ */
178
+ declare function tokenize(text: string): string[];
179
+
158
180
  type SearchIntent = "factual" | "exploratory" | "temporal" | "causal";
159
181
  interface IntentResult {
160
182
  intent: SearchIntent;
@@ -162,7 +184,8 @@ interface IntentResult {
162
184
  }
163
185
  /**
164
186
  * Classify the intent of a search query.
165
- * Uses keyword scoring no LLM needed.
187
+ * Uses keyword pattern matching + structural analysis.
188
+ * Enhanced for Chinese with jieba-aware token analysis.
166
189
  */
167
190
  declare function classifyIntent(query: string): IntentResult;
168
191
  /**
@@ -174,6 +197,27 @@ declare function getStrategy(intent: SearchIntent): {
174
197
  limit: number;
175
198
  };
176
199
 
200
+ interface RerankResult {
201
+ index: number;
202
+ relevance_score: number;
203
+ }
204
+ interface RerankProvider {
205
+ id: string;
206
+ model: string;
207
+ rerank(query: string, documents: string[]): Promise<RerankResult[]>;
208
+ }
209
+ declare function getRerankerProviderFromEnv(): RerankProvider | null;
210
+ declare function createOpenAIRerankProvider(opts: {
211
+ apiKey: string;
212
+ model: string;
213
+ baseUrl?: string;
214
+ }): RerankProvider;
215
+
216
+ /**
217
+ * Optionally rerank results using an external semantic reranker provider.
218
+ * Best-effort: on failure, returns original results unchanged.
219
+ */
220
+ declare function rerankWithProvider(results: SearchResult[], query: string, provider: RerankProvider): Promise<SearchResult[]>;
177
221
  /**
178
222
  * Rerank search results based on intent strategy and priority weighting.
179
223
  */
@@ -184,13 +228,94 @@ declare function rerank(results: SearchResult[], opts: {
184
228
  limit: number;
185
229
  }): SearchResult[];
186
230
 
187
- declare function calculateVitality(stability: number, daysSinceCreation: number, priority: number): number;
231
+ interface EmbeddingProvider {
232
+ id: string;
233
+ model: string;
234
+ dimension?: number;
235
+ instructionPrefix?: string | null;
236
+ embed(text: string): Promise<number[]>;
237
+ embedQuery?(query: string): Promise<number[]>;
238
+ }
239
+ declare function getDefaultInstruction(model: string): string | null;
240
+ declare function getEmbeddingProviderFromEnv(): EmbeddingProvider | null;
241
+ declare function createOpenAIProvider(opts: {
242
+ id?: string;
243
+ apiKey: string;
244
+ model: string;
245
+ baseUrl?: string;
246
+ instruction?: string | null;
247
+ }): EmbeddingProvider;
248
+ declare function createDashScopeProvider(opts: {
249
+ apiKey: string;
250
+ model: string;
251
+ baseUrl?: string;
252
+ instruction?: string | null;
253
+ }): EmbeddingProvider;
254
+
255
+ interface HybridSearchOptions {
256
+ agent_id?: string;
257
+ limit?: number;
258
+ bm25CandidateMultiplier?: number;
259
+ semanticCandidates?: number;
260
+ rrfK?: number;
261
+ embeddingProvider?: EmbeddingProvider | null;
262
+ embeddingModel?: string;
263
+ }
264
+ declare function searchHybrid(db: Database.Database, query: string, opts?: HybridSearchOptions): Promise<SearchResult[]>;
265
+
266
+ interface StoredEmbedding {
267
+ agent_id: string;
268
+ memory_id: string;
269
+ model: string;
270
+ dim: number;
271
+ vector: Float32Array;
272
+ created_at: string;
273
+ updated_at: string;
274
+ }
275
+ declare function encodeEmbedding(vector: number[] | Float32Array): Buffer;
276
+ declare function decodeEmbedding(buf: Buffer): Float32Array;
277
+ declare function upsertEmbedding(db: Database.Database, input: {
278
+ agent_id: string;
279
+ memory_id: string;
280
+ model: string;
281
+ vector: number[] | Float32Array;
282
+ }): void;
283
+ declare function getEmbedding(db: Database.Database, agent_id: string, memory_id: string, model: string): StoredEmbedding | null;
284
+ declare function listEmbeddings(db: Database.Database, agent_id: string, model: string): Array<{
285
+ memory_id: string;
286
+ vector: Float32Array;
287
+ }>;
288
+
289
+ declare function embedMemory(db: Database.Database, memoryId: string, provider: EmbeddingProvider, opts?: {
290
+ agent_id?: string;
291
+ model?: string;
292
+ maxChars?: number;
293
+ }): Promise<boolean>;
294
+ declare function embedMissingForAgent(db: Database.Database, provider: EmbeddingProvider, opts?: {
295
+ agent_id?: string;
296
+ model?: string;
297
+ limit?: number;
298
+ maxChars?: number;
299
+ }): Promise<{
300
+ embedded: number;
301
+ scanned: number;
302
+ }>;
303
+
304
+ /**
305
+ * Calculate vitality using Ebbinghaus forgetting curve.
306
+ * @param stability - S parameter (higher = slower decay)
307
+ * @param daysSinceLastAccess - days since last recall (or creation if never accessed)
308
+ * @param priority - memory priority (0-3)
309
+ */
310
+ declare function calculateVitality(stability: number, daysSinceLastAccess: number, priority: number): number;
188
311
  /**
189
312
  * Run decay on all memories.
190
313
  * Updates vitality based on Ebbinghaus curve.
191
314
  * Returns count of memories updated.
192
315
  */
193
- declare function runDecay(db: Database.Database): {
316
+ declare function runDecay(db: Database.Database, opts?: {
317
+ agent_id?: string;
318
+ }): {
194
319
  updated: number;
195
320
  decayed: number;
196
321
  belowThreshold: number;
@@ -199,7 +324,9 @@ declare function runDecay(db: Database.Database): {
199
324
  * Get memories that are candidates for cleanup (vitality < threshold).
200
325
  * Only P3 (event) memories can be fully cleaned.
201
326
  */
202
- declare function getDecayedMemories(db: Database.Database, threshold?: number): Array<{
327
+ declare function getDecayedMemories(db: Database.Database, threshold?: number, opts?: {
328
+ agent_id?: string;
329
+ }): Array<{
203
330
  id: string;
204
331
  content: string;
205
332
  vitality: number;
@@ -244,6 +371,7 @@ interface TidyResult {
244
371
  declare function runTidy(db: Database.Database, opts?: {
245
372
  vitalityThreshold?: number;
246
373
  maxSnapshotsPerMemory?: number;
374
+ agent_id?: string;
247
375
  }): TidyResult;
248
376
 
249
377
  interface GovernResult {
@@ -257,7 +385,9 @@ interface GovernResult {
257
385
  * 2. Remove orphan links (source or target missing)
258
386
  * 3. Remove empty memories (blank content)
259
387
  */
260
- declare function runGovern(db: Database.Database): GovernResult;
388
+ declare function runGovern(db: Database.Database, opts?: {
389
+ agent_id?: string;
390
+ }): GovernResult;
261
391
 
262
392
  interface BootResult {
263
393
  identityMemories: Memory[];
@@ -272,4 +402,4 @@ declare function boot(db: Database.Database, opts?: {
272
402
  corePaths?: string[];
273
403
  }): BootResult;
274
404
 
275
- export { type BootResult, type CreateMemoryInput, type GovernResult, type GuardAction, type GuardResult, type IntentResult, type Link, type Memory, type MemoryType, type Path, type Priority, type RelationType, type SearchIntent, type SearchResult, type Snapshot, type SnapshotAction, type SyncInput, type SyncResult, type TidyResult, type UpdateMemoryInput, boot, calculateVitality, classifyIntent, contentHash, countMemories, createLink, createMemory, createPath, createSnapshot, deleteLink, deleteMemory, deletePath, getDecayedMemories, getLinks, getMemory, getOutgoingLinks, getPath, getPathByUri, getPathsByDomain, getPathsByMemory, getPathsByPrefix, getSnapshot, getSnapshots, getStrategy, guard, listMemories, parseUri, recordAccess, rerank, rollback, runDecay, runGovern, runTidy, searchBM25, syncBatch, syncOne, traverse, updateMemory };
405
+ export { type BootResult, type CreateMemoryInput, type EmbeddingProvider, type ExportResult, type GovernResult, type GuardAction, type GuardResult, type HybridSearchOptions, type IntentResult, type Link, type Memory, type MemoryType, type Path, type Priority, type RelationType, type RerankProvider, type RerankResult, type SearchIntent, type SearchResult, type Snapshot, type SnapshotAction, type StoredEmbedding, type SyncInput, type SyncResult, type TidyResult, type UpdateMemoryInput, boot, calculateVitality, classifyIntent, contentHash, countMemories, createDashScopeProvider, createLink, createMemory, createOpenAIProvider, createOpenAIRerankProvider, createPath, createSnapshot, decodeEmbedding, deleteLink, deleteMemory, deletePath, embedMemory, embedMissingForAgent, encodeEmbedding, exportMemories, getDecayedMemories, getDefaultInstruction, getEmbedding, getEmbeddingProviderFromEnv, getLinks, getMemory, getOutgoingLinks, getPath, getPathByUri, getPathsByDomain, getPathsByMemory, getPathsByPrefix, getRerankerProviderFromEnv, getSnapshot, getSnapshots, getStrategy, guard, listEmbeddings, listMemories, parseUri, recordAccess, rerank, rerankWithProvider, rollback, runDecay, runGovern, runTidy, searchBM25, searchHybrid, syncBatch, syncOne, tokenize, traverse, updateMemory, upsertEmbedding };