audrey 0.16.1 → 0.17.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/LICENSE +21 -21
- package/README.md +310 -643
- package/benchmarks/baselines.js +169 -0
- package/benchmarks/cases.js +421 -0
- package/benchmarks/reference-results.js +70 -0
- package/benchmarks/report.js +255 -0
- package/benchmarks/run.js +514 -0
- package/docs/assets/benchmarks/local-benchmark.svg +45 -0
- package/docs/assets/benchmarks/operations-benchmark.svg +45 -0
- package/docs/assets/benchmarks/published-memory-standards.svg +50 -0
- package/docs/benchmarking.md +151 -0
- package/docs/production-readiness.md +96 -0
- package/examples/fintech-ops-demo.js +67 -0
- package/examples/healthcare-ops-demo.js +67 -0
- package/examples/stripe-demo.js +105 -0
- package/mcp-server/config.js +80 -27
- package/mcp-server/index.js +611 -75
- package/mcp-server/serve.js +482 -0
- package/package.json +24 -5
- package/src/audrey.js +51 -13
- package/src/consolidate.js +70 -54
- package/src/db.js +22 -1
- package/src/embedding.js +16 -12
- package/src/encode.js +8 -2
- package/src/fts.js +134 -0
- package/src/import.js +28 -0
- package/src/llm.js +6 -3
- package/src/migrate.js +2 -2
- package/src/recall.js +253 -32
- package/src/utils.js +25 -0
- package/types/index.d.ts +434 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import type { Database } from 'better-sqlite3';
|
|
3
|
+
|
|
4
|
+
// === Configuration ===
|
|
5
|
+
|
|
6
|
+
export interface EmbeddingConfig {
|
|
7
|
+
provider: 'mock' | 'local' | 'gemini' | 'openai';
|
|
8
|
+
dimensions?: number;
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
device?: 'gpu' | 'cpu';
|
|
11
|
+
model?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface LLMConfig {
|
|
15
|
+
provider: 'mock' | 'anthropic' | 'openai';
|
|
16
|
+
apiKey?: string;
|
|
17
|
+
model?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ConfidenceWeights {
|
|
21
|
+
source?: number;
|
|
22
|
+
evidence?: number;
|
|
23
|
+
recency?: number;
|
|
24
|
+
retrieval?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface HalfLives {
|
|
28
|
+
episodic?: number;
|
|
29
|
+
semantic?: number;
|
|
30
|
+
procedural?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ConfidenceConfig {
|
|
34
|
+
weights?: ConfidenceWeights;
|
|
35
|
+
halfLives?: HalfLives;
|
|
36
|
+
sourceReliability?: Record<string, number>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ConsolidationConfig {
|
|
40
|
+
minEpisodes?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface DecayConfig {
|
|
44
|
+
dormantThreshold?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ContextConfig {
|
|
48
|
+
enabled?: boolean;
|
|
49
|
+
weight?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface AffectConfig {
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
weight?: number;
|
|
55
|
+
arousalWeight?: number;
|
|
56
|
+
resonance?: {
|
|
57
|
+
enabled?: boolean;
|
|
58
|
+
threshold?: number;
|
|
59
|
+
maxResults?: number;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface InterferenceConfig {
|
|
64
|
+
enabled?: boolean;
|
|
65
|
+
k?: number;
|
|
66
|
+
threshold?: number;
|
|
67
|
+
weight?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface AudreyConfig {
|
|
71
|
+
dataDir: string;
|
|
72
|
+
agent?: string;
|
|
73
|
+
embedding: EmbeddingConfig;
|
|
74
|
+
llm?: LLMConfig;
|
|
75
|
+
confidence?: ConfidenceConfig;
|
|
76
|
+
consolidation?: ConsolidationConfig;
|
|
77
|
+
decay?: DecayConfig;
|
|
78
|
+
context?: ContextConfig;
|
|
79
|
+
affect?: AffectConfig;
|
|
80
|
+
interference?: InterferenceConfig;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// === Encode ===
|
|
84
|
+
|
|
85
|
+
export type SourceType = 'direct-observation' | 'told-by-user' | 'tool-result' | 'inference' | 'model-generated';
|
|
86
|
+
|
|
87
|
+
export interface AffectParams {
|
|
88
|
+
valence?: number;
|
|
89
|
+
arousal?: number;
|
|
90
|
+
label?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface EncodeParams {
|
|
94
|
+
content: string;
|
|
95
|
+
source: SourceType;
|
|
96
|
+
salience?: number;
|
|
97
|
+
tags?: string[];
|
|
98
|
+
context?: Record<string, unknown>;
|
|
99
|
+
affect?: AffectParams;
|
|
100
|
+
causal?: { trigger?: string; consequence?: string };
|
|
101
|
+
supersedes?: string;
|
|
102
|
+
private?: boolean;
|
|
103
|
+
agent?: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// === Recall ===
|
|
107
|
+
|
|
108
|
+
export interface RecallOptions {
|
|
109
|
+
limit?: number;
|
|
110
|
+
minConfidence?: number;
|
|
111
|
+
types?: Array<'episodic' | 'semantic' | 'procedural'>;
|
|
112
|
+
includeProvenance?: boolean;
|
|
113
|
+
includeDormant?: boolean;
|
|
114
|
+
includePrivate?: boolean;
|
|
115
|
+
tags?: string[];
|
|
116
|
+
sources?: SourceType[];
|
|
117
|
+
after?: string;
|
|
118
|
+
before?: string;
|
|
119
|
+
context?: Record<string, unknown>;
|
|
120
|
+
affect?: AffectParams;
|
|
121
|
+
scope?: 'shared' | 'agent';
|
|
122
|
+
agent?: string;
|
|
123
|
+
retrieval?: 'hybrid' | 'vector' | 'keyword';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface RecallResult {
|
|
127
|
+
id: string;
|
|
128
|
+
content: string;
|
|
129
|
+
type: 'episodic' | 'semantic' | 'procedural';
|
|
130
|
+
confidence: number;
|
|
131
|
+
score: number;
|
|
132
|
+
source: string;
|
|
133
|
+
createdAt: string;
|
|
134
|
+
agent: string;
|
|
135
|
+
state?: string;
|
|
136
|
+
contextMatch?: number;
|
|
137
|
+
moodCongruence?: number;
|
|
138
|
+
provenance?: {
|
|
139
|
+
tags?: string[];
|
|
140
|
+
context?: Record<string, unknown>;
|
|
141
|
+
affect?: AffectParams;
|
|
142
|
+
evidenceEpisodeIds?: string[];
|
|
143
|
+
};
|
|
144
|
+
_recallErrors?: Array<{ type: string; message: string }>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export type RecallResults = RecallResult[] & {
|
|
148
|
+
partialFailure?: boolean;
|
|
149
|
+
errors?: Array<{ type: string; message: string }>;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// === Consolidation ===
|
|
153
|
+
|
|
154
|
+
export interface ConsolidateOptions {
|
|
155
|
+
minClusterSize?: number;
|
|
156
|
+
similarityThreshold?: number;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface ConsolidationResult {
|
|
160
|
+
runId: string;
|
|
161
|
+
episodesEvaluated: number;
|
|
162
|
+
clustersFound: number;
|
|
163
|
+
semanticsCreated: number;
|
|
164
|
+
proceduresCreated: number;
|
|
165
|
+
inputIds: string[];
|
|
166
|
+
outputIds: string[];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// === Dream ===
|
|
170
|
+
|
|
171
|
+
export interface DreamOptions {
|
|
172
|
+
dormantThreshold?: number;
|
|
173
|
+
minClusterSize?: number;
|
|
174
|
+
similarityThreshold?: number;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface DreamResult {
|
|
178
|
+
consolidation: ConsolidationResult;
|
|
179
|
+
decay: {
|
|
180
|
+
totalEvaluated: number;
|
|
181
|
+
transitionedToDormant: number;
|
|
182
|
+
timestamp: string;
|
|
183
|
+
};
|
|
184
|
+
stats: IntrospectResult;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// === Introspect ===
|
|
188
|
+
|
|
189
|
+
export interface IntrospectResult {
|
|
190
|
+
episodic: number;
|
|
191
|
+
semantic: number;
|
|
192
|
+
procedural: number;
|
|
193
|
+
causalLinks: number;
|
|
194
|
+
dormant: number;
|
|
195
|
+
contradictions: {
|
|
196
|
+
open: number;
|
|
197
|
+
resolved: number;
|
|
198
|
+
context_dependent: number;
|
|
199
|
+
reopened: number;
|
|
200
|
+
};
|
|
201
|
+
lastConsolidation: string | null;
|
|
202
|
+
totalConsolidationRuns: number;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// === Export / Import ===
|
|
206
|
+
|
|
207
|
+
export interface Snapshot {
|
|
208
|
+
version: string;
|
|
209
|
+
exportedAt: string;
|
|
210
|
+
episodes: unknown[];
|
|
211
|
+
semantics: unknown[];
|
|
212
|
+
procedures: unknown[];
|
|
213
|
+
causalLinks: unknown[];
|
|
214
|
+
contradictions: unknown[];
|
|
215
|
+
consolidationRuns: unknown[];
|
|
216
|
+
consolidationMetrics: unknown[];
|
|
217
|
+
config: Record<string, string>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// === Forget ===
|
|
221
|
+
|
|
222
|
+
export interface ForgetOptions {
|
|
223
|
+
purge?: boolean;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface ForgetResult {
|
|
227
|
+
id: string;
|
|
228
|
+
type: 'episodic' | 'semantic' | 'procedural';
|
|
229
|
+
purged: boolean;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export interface ForgetByQueryOptions {
|
|
233
|
+
minSimilarity?: number;
|
|
234
|
+
purge?: boolean;
|
|
235
|
+
limit?: number;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export interface PurgeResult {
|
|
239
|
+
episodesRemoved: number;
|
|
240
|
+
semanticsRemoved: number;
|
|
241
|
+
proceduresRemoved: number;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// === Greeting / Reflect ===
|
|
245
|
+
|
|
246
|
+
export interface GreetingOptions {
|
|
247
|
+
context?: string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export interface GreetingResult {
|
|
251
|
+
principles: string[];
|
|
252
|
+
recentMemories: RecallResult[];
|
|
253
|
+
mood: { valence: number; arousal: number; label: string } | null;
|
|
254
|
+
stats: IntrospectResult;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export interface ReflectResult {
|
|
258
|
+
encoded: number;
|
|
259
|
+
memories: Array<{ id: string; content: string }>;
|
|
260
|
+
skipped?: string;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// === Embedding Providers ===
|
|
264
|
+
|
|
265
|
+
export interface EmbeddingProvider {
|
|
266
|
+
readonly dimensions: number;
|
|
267
|
+
readonly modelName: string;
|
|
268
|
+
readonly modelVersion: string;
|
|
269
|
+
embed(text: string): Promise<number[]>;
|
|
270
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
271
|
+
vectorToBuffer(vector: number[]): Buffer;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export class MockEmbeddingProvider implements EmbeddingProvider {
|
|
275
|
+
readonly dimensions: number;
|
|
276
|
+
readonly modelName: string;
|
|
277
|
+
readonly modelVersion: string;
|
|
278
|
+
constructor(dimensions?: number);
|
|
279
|
+
embed(text: string): Promise<number[]>;
|
|
280
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
281
|
+
vectorToBuffer(vector: number[]): Buffer;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export class LocalEmbeddingProvider implements EmbeddingProvider {
|
|
285
|
+
readonly dimensions: number;
|
|
286
|
+
readonly modelName: string;
|
|
287
|
+
readonly modelVersion: string;
|
|
288
|
+
embed(text: string): Promise<number[]>;
|
|
289
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
290
|
+
vectorToBuffer(vector: number[]): Buffer;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export class OpenAIEmbeddingProvider implements EmbeddingProvider {
|
|
294
|
+
readonly dimensions: number;
|
|
295
|
+
readonly modelName: string;
|
|
296
|
+
readonly modelVersion: string;
|
|
297
|
+
constructor(options: { apiKey: string; dimensions?: number; model?: string });
|
|
298
|
+
embed(text: string): Promise<number[]>;
|
|
299
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
300
|
+
vectorToBuffer(vector: number[]): Buffer;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export class GeminiEmbeddingProvider implements EmbeddingProvider {
|
|
304
|
+
readonly dimensions: number;
|
|
305
|
+
readonly modelName: string;
|
|
306
|
+
readonly modelVersion: string;
|
|
307
|
+
constructor(options: { apiKey: string; dimensions?: number; model?: string });
|
|
308
|
+
embed(text: string): Promise<number[]>;
|
|
309
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
310
|
+
vectorToBuffer(vector: number[]): Buffer;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export function createEmbeddingProvider(config: EmbeddingConfig): EmbeddingProvider;
|
|
314
|
+
|
|
315
|
+
// === LLM Providers ===
|
|
316
|
+
|
|
317
|
+
export interface LLMProvider {
|
|
318
|
+
readonly modelName: string;
|
|
319
|
+
generate(prompt: string): Promise<string>;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export class MockLLMProvider implements LLMProvider {
|
|
323
|
+
readonly modelName: string;
|
|
324
|
+
generate(prompt: string): Promise<string>;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export class AnthropicLLMProvider implements LLMProvider {
|
|
328
|
+
readonly modelName: string;
|
|
329
|
+
constructor(options: { apiKey: string; model?: string });
|
|
330
|
+
generate(prompt: string): Promise<string>;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export class OpenAILLMProvider implements LLMProvider {
|
|
334
|
+
readonly modelName: string;
|
|
335
|
+
constructor(options: { apiKey: string; model?: string });
|
|
336
|
+
generate(prompt: string): Promise<string>;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function createLLMProvider(config: LLMConfig): LLMProvider;
|
|
340
|
+
|
|
341
|
+
// === Core Class ===
|
|
342
|
+
|
|
343
|
+
export class Audrey extends EventEmitter {
|
|
344
|
+
readonly agent: string;
|
|
345
|
+
readonly dataDir: string;
|
|
346
|
+
readonly db: Database;
|
|
347
|
+
readonly embeddingProvider: EmbeddingProvider;
|
|
348
|
+
readonly llmProvider: LLMProvider | null;
|
|
349
|
+
|
|
350
|
+
constructor(config: AudreyConfig);
|
|
351
|
+
|
|
352
|
+
encode(params: EncodeParams): Promise<string>;
|
|
353
|
+
recall(query: string, options?: RecallOptions): Promise<RecallResults>;
|
|
354
|
+
recallStream(query: string, options?: RecallOptions): AsyncGenerator<RecallResult>;
|
|
355
|
+
consolidate(options?: ConsolidateOptions): Promise<ConsolidationResult>;
|
|
356
|
+
dream(options?: DreamOptions): Promise<DreamResult>;
|
|
357
|
+
introspect(): IntrospectResult;
|
|
358
|
+
export(): Snapshot;
|
|
359
|
+
import(snapshot: Snapshot): Promise<void>;
|
|
360
|
+
forget(id: string, options?: ForgetOptions): ForgetResult;
|
|
361
|
+
forgetByQuery(query: string, options?: ForgetByQueryOptions): Promise<ForgetResult | null>;
|
|
362
|
+
purge(): PurgeResult;
|
|
363
|
+
greeting(options?: GreetingOptions): Promise<GreetingResult>;
|
|
364
|
+
reflect(turns: string): Promise<ReflectResult>;
|
|
365
|
+
startAutoConsolidate(intervalMs: number, options?: ConsolidateOptions): void;
|
|
366
|
+
stopAutoConsolidate(): void;
|
|
367
|
+
waitForIdle(): Promise<void>;
|
|
368
|
+
close(): void;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// === Database ===
|
|
372
|
+
|
|
373
|
+
export function createDatabase(dataDir: string, options?: { dimensions?: number }): { db: Database; migrated: boolean };
|
|
374
|
+
export function closeDatabase(db: Database): void;
|
|
375
|
+
export function readStoredDimensions(dataDir: string): number | null;
|
|
376
|
+
|
|
377
|
+
// === Standalone Functions ===
|
|
378
|
+
|
|
379
|
+
export function recall(db: Database, embeddingProvider: EmbeddingProvider, query: string, options?: RecallOptions): Promise<RecallResults>;
|
|
380
|
+
export function recallStream(db: Database, embeddingProvider: EmbeddingProvider, query: string, options?: RecallOptions): AsyncGenerator<RecallResult>;
|
|
381
|
+
export function exportMemories(db: Database): Snapshot;
|
|
382
|
+
export function importMemories(db: Database, embeddingProvider: EmbeddingProvider, snapshot: Snapshot): Promise<void>;
|
|
383
|
+
export function forgetMemory(db: Database, id: string, options?: ForgetOptions): ForgetResult;
|
|
384
|
+
export function forgetByQuery(db: Database, embeddingProvider: EmbeddingProvider, query: string, options?: ForgetByQueryOptions): Promise<ForgetResult | null>;
|
|
385
|
+
export function purgeMemories(db: Database): PurgeResult;
|
|
386
|
+
export function reembedAll(db: Database, embeddingProvider: EmbeddingProvider): Promise<{ reembedded: number }>;
|
|
387
|
+
export function suggestConsolidationParams(db: Database): { minClusterSize: number; similarityThreshold: number } | null;
|
|
388
|
+
|
|
389
|
+
// === Confidence ===
|
|
390
|
+
|
|
391
|
+
export function computeConfidence(params: {
|
|
392
|
+
sourceType: SourceType;
|
|
393
|
+
supportingCount?: number;
|
|
394
|
+
contradictingCount?: number;
|
|
395
|
+
ageDays?: number;
|
|
396
|
+
halfLifeDays?: number;
|
|
397
|
+
retrievalCount?: number;
|
|
398
|
+
daysSinceRetrieval?: number;
|
|
399
|
+
}): number;
|
|
400
|
+
export function sourceReliability(source: SourceType): number;
|
|
401
|
+
export function salienceModifier(salience: number): number;
|
|
402
|
+
export const DEFAULT_SOURCE_RELIABILITY: Record<SourceType, number>;
|
|
403
|
+
export const DEFAULT_WEIGHTS: ConfidenceWeights;
|
|
404
|
+
export const DEFAULT_HALF_LIVES: HalfLives;
|
|
405
|
+
|
|
406
|
+
// === Causal ===
|
|
407
|
+
|
|
408
|
+
export function addCausalLink(db: Database, params: { causeId: string; effectId: string; strength?: number; description?: string }): string;
|
|
409
|
+
export function getCausalChain(db: Database, id: string, options?: { depth?: number; direction?: 'forward' | 'backward' | 'both' }): unknown[];
|
|
410
|
+
export function articulateCausalLink(db: Database, llmProvider: LLMProvider, linkId: string): Promise<string>;
|
|
411
|
+
|
|
412
|
+
// === Prompts ===
|
|
413
|
+
|
|
414
|
+
export function buildPrincipleExtractionPrompt(episodes: Array<{ content: string }>): string;
|
|
415
|
+
export function buildContradictionDetectionPrompt(memory: string, candidate: string): string;
|
|
416
|
+
export function buildCausalArticulationPrompt(cause: string, effect: string): string;
|
|
417
|
+
export function buildContextResolutionPrompt(contradiction: string, contextA: string, contextB: string): string;
|
|
418
|
+
|
|
419
|
+
// === Affect ===
|
|
420
|
+
|
|
421
|
+
export function arousalSalienceBoost(arousal?: number): number;
|
|
422
|
+
export function affectSimilarity(a: AffectParams, b: AffectParams): number;
|
|
423
|
+
export function moodCongruenceModifier(memoryAffect: AffectParams, queryAffect: AffectParams, weight?: number): number;
|
|
424
|
+
export function detectResonance(db: Database, embeddingProvider: EmbeddingProvider, episodeId: string, params: EncodeParams, config: { threshold?: number; maxResults?: number }): Promise<unknown[]>;
|
|
425
|
+
|
|
426
|
+
// === Interference ===
|
|
427
|
+
|
|
428
|
+
export function applyInterference(db: Database, embeddingProvider: EmbeddingProvider, episodeId: string, params: EncodeParams, config: InterferenceConfig): Promise<unknown[]>;
|
|
429
|
+
export function interferenceModifier(interferenceCount: number): number;
|
|
430
|
+
|
|
431
|
+
// === Context ===
|
|
432
|
+
|
|
433
|
+
export function contextMatchRatio(encodingContext: Record<string, unknown>, retrievalContext: Record<string, unknown>): number;
|
|
434
|
+
export function contextModifier(encodingContext: Record<string, unknown>, retrievalContext: Record<string, unknown>, weight?: number): number;
|