@smyslenny/agent-memory 2.0.0 → 2.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 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
  /**
@@ -184,13 +207,88 @@ declare function rerank(results: SearchResult[], opts: {
184
207
  limit: number;
185
208
  }): SearchResult[];
186
209
 
187
- declare function calculateVitality(stability: number, daysSinceCreation: number, priority: number): number;
210
+ interface EmbeddingProvider {
211
+ id: string;
212
+ model: string;
213
+ dimension?: number;
214
+ embed(text: string): Promise<number[]>;
215
+ }
216
+ declare function getEmbeddingProviderFromEnv(): EmbeddingProvider | null;
217
+ declare function createOpenAIProvider(opts: {
218
+ apiKey: string;
219
+ model: string;
220
+ baseUrl?: string;
221
+ }): EmbeddingProvider;
222
+ declare function createDashScopeProvider(opts: {
223
+ apiKey: string;
224
+ model: string;
225
+ baseUrl?: string;
226
+ }): EmbeddingProvider;
227
+
228
+ interface HybridSearchOptions {
229
+ agent_id?: string;
230
+ limit?: number;
231
+ bm25CandidateMultiplier?: number;
232
+ semanticCandidates?: number;
233
+ rrfK?: number;
234
+ embeddingProvider?: EmbeddingProvider | null;
235
+ embeddingModel?: string;
236
+ }
237
+ declare function searchHybrid(db: Database.Database, query: string, opts?: HybridSearchOptions): Promise<SearchResult[]>;
238
+
239
+ interface StoredEmbedding {
240
+ agent_id: string;
241
+ memory_id: string;
242
+ model: string;
243
+ dim: number;
244
+ vector: Float32Array;
245
+ created_at: string;
246
+ updated_at: string;
247
+ }
248
+ declare function encodeEmbedding(vector: number[] | Float32Array): Buffer;
249
+ declare function decodeEmbedding(buf: Buffer): Float32Array;
250
+ declare function upsertEmbedding(db: Database.Database, input: {
251
+ agent_id: string;
252
+ memory_id: string;
253
+ model: string;
254
+ vector: number[] | Float32Array;
255
+ }): void;
256
+ declare function getEmbedding(db: Database.Database, agent_id: string, memory_id: string, model: string): StoredEmbedding | null;
257
+ declare function listEmbeddings(db: Database.Database, agent_id: string, model: string): Array<{
258
+ memory_id: string;
259
+ vector: Float32Array;
260
+ }>;
261
+
262
+ declare function embedMemory(db: Database.Database, memoryId: string, provider: EmbeddingProvider, opts?: {
263
+ agent_id?: string;
264
+ model?: string;
265
+ maxChars?: number;
266
+ }): Promise<boolean>;
267
+ declare function embedMissingForAgent(db: Database.Database, provider: EmbeddingProvider, opts?: {
268
+ agent_id?: string;
269
+ model?: string;
270
+ limit?: number;
271
+ maxChars?: number;
272
+ }): Promise<{
273
+ embedded: number;
274
+ scanned: number;
275
+ }>;
276
+
277
+ /**
278
+ * Calculate vitality using Ebbinghaus forgetting curve.
279
+ * @param stability - S parameter (higher = slower decay)
280
+ * @param daysSinceLastAccess - days since last recall (or creation if never accessed)
281
+ * @param priority - memory priority (0-3)
282
+ */
283
+ declare function calculateVitality(stability: number, daysSinceLastAccess: number, priority: number): number;
188
284
  /**
189
285
  * Run decay on all memories.
190
286
  * Updates vitality based on Ebbinghaus curve.
191
287
  * Returns count of memories updated.
192
288
  */
193
- declare function runDecay(db: Database.Database): {
289
+ declare function runDecay(db: Database.Database, opts?: {
290
+ agent_id?: string;
291
+ }): {
194
292
  updated: number;
195
293
  decayed: number;
196
294
  belowThreshold: number;
@@ -199,7 +297,9 @@ declare function runDecay(db: Database.Database): {
199
297
  * Get memories that are candidates for cleanup (vitality < threshold).
200
298
  * Only P3 (event) memories can be fully cleaned.
201
299
  */
202
- declare function getDecayedMemories(db: Database.Database, threshold?: number): Array<{
300
+ declare function getDecayedMemories(db: Database.Database, threshold?: number, opts?: {
301
+ agent_id?: string;
302
+ }): Array<{
203
303
  id: string;
204
304
  content: string;
205
305
  vitality: number;
@@ -244,6 +344,7 @@ interface TidyResult {
244
344
  declare function runTidy(db: Database.Database, opts?: {
245
345
  vitalityThreshold?: number;
246
346
  maxSnapshotsPerMemory?: number;
347
+ agent_id?: string;
247
348
  }): TidyResult;
248
349
 
249
350
  interface GovernResult {
@@ -257,7 +358,9 @@ interface GovernResult {
257
358
  * 2. Remove orphan links (source or target missing)
258
359
  * 3. Remove empty memories (blank content)
259
360
  */
260
- declare function runGovern(db: Database.Database): GovernResult;
361
+ declare function runGovern(db: Database.Database, opts?: {
362
+ agent_id?: string;
363
+ }): GovernResult;
261
364
 
262
365
  interface BootResult {
263
366
  identityMemories: Memory[];
@@ -272,4 +375,4 @@ declare function boot(db: Database.Database, opts?: {
272
375
  corePaths?: string[];
273
376
  }): BootResult;
274
377
 
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 };
378
+ 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 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, createPath, createSnapshot, decodeEmbedding, deleteLink, deleteMemory, deletePath, embedMemory, embedMissingForAgent, encodeEmbedding, exportMemories, getDecayedMemories, getEmbedding, getEmbeddingProviderFromEnv, getLinks, getMemory, getOutgoingLinks, getPath, getPathByUri, getPathsByDomain, getPathsByMemory, getPathsByPrefix, getSnapshot, getSnapshots, getStrategy, guard, listEmbeddings, listMemories, parseUri, recordAccess, rerank, rollback, runDecay, runGovern, runTidy, searchBM25, searchHybrid, syncBatch, syncOne, tokenize, traverse, updateMemory, upsertEmbedding };