@prometheus-ai/memory 0.5.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/README.md +107 -0
- package/dist/types/cli.d.ts +35 -0
- package/dist/types/config.d.ts +77 -0
- package/dist/types/core/aaak.d.ts +55 -0
- package/dist/types/core/annotations.d.ts +75 -0
- package/dist/types/core/banks.d.ts +33 -0
- package/dist/types/core/beam/consolidate.d.ts +32 -0
- package/dist/types/core/beam/helpers.d.ts +76 -0
- package/dist/types/core/beam/index.d.ts +59 -0
- package/dist/types/core/beam/recall.d.ts +32 -0
- package/dist/types/core/beam/schema.d.ts +2 -0
- package/dist/types/core/beam/store.d.ts +35 -0
- package/dist/types/core/beam/types.d.ts +233 -0
- package/dist/types/core/binary-vectors.d.ts +54 -0
- package/dist/types/core/chat-normalize.d.ts +13 -0
- package/dist/types/core/content-sanitizer.d.ts +18 -0
- package/dist/types/core/cost-log.d.ts +13 -0
- package/dist/types/core/embeddings.d.ts +44 -0
- package/dist/types/core/entities.d.ts +7 -0
- package/dist/types/core/episodic-graph.d.ts +89 -0
- package/dist/types/core/extraction/client.d.ts +31 -0
- package/dist/types/core/extraction/diagnostics.d.ts +51 -0
- package/dist/types/core/extraction/prompts.d.ts +2 -0
- package/dist/types/core/extraction.d.ts +6 -0
- package/dist/types/core/index.d.ts +4 -0
- package/dist/types/core/llm-backends.d.ts +21 -0
- package/dist/types/core/local-llm.d.ts +15 -0
- package/dist/types/core/memory.d.ts +160 -0
- package/dist/types/core/migrations/e6-triplestore-split.d.ts +17 -0
- package/dist/types/core/migrations/index.d.ts +1 -0
- package/dist/types/core/mmr.d.ts +8 -0
- package/dist/types/core/orchestrator.d.ts +20 -0
- package/dist/types/core/patterns.d.ts +61 -0
- package/dist/types/core/plugins.d.ts +109 -0
- package/dist/types/core/polyphonic-recall.d.ts +66 -0
- package/dist/types/core/query-cache.d.ts +46 -0
- package/dist/types/core/query-intent.d.ts +20 -0
- package/dist/types/core/recall-diagnostics.d.ts +48 -0
- package/dist/types/core/runtime-options.d.ts +68 -0
- package/dist/types/core/shmr.d.ts +56 -0
- package/dist/types/core/streaming.d.ts +136 -0
- package/dist/types/core/synonyms.d.ts +46 -0
- package/dist/types/core/temporal-parser.d.ts +16 -0
- package/dist/types/core/token-counter.d.ts +8 -0
- package/dist/types/core/triples.d.ts +63 -0
- package/dist/types/core/typed-memory.d.ts +39 -0
- package/dist/types/core/vector-math.d.ts +1 -0
- package/dist/types/core/veracity-consolidation.d.ts +60 -0
- package/dist/types/core/weibull.d.ts +96 -0
- package/dist/types/db.d.ts +16 -0
- package/dist/types/diagnose.d.ts +24 -0
- package/dist/types/dr/index.d.ts +1 -0
- package/dist/types/dr/recovery.d.ts +68 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/mcp-server.d.ts +40 -0
- package/dist/types/mcp-tools.d.ts +484 -0
- package/dist/types/migrations/e6-triplestore-split.d.ts +1 -0
- package/dist/types/migrations/index.d.ts +1 -0
- package/dist/types/types.d.ts +145 -0
- package/dist/types/util/datetime.d.ts +8 -0
- package/dist/types/util/env.d.ts +10 -0
- package/dist/types/util/ids.d.ts +3 -0
- package/dist/types/util/lru.d.ts +12 -0
- package/dist/types/util/regex.d.ts +10 -0
- package/package.json +85 -0
- package/src/cli.ts +398 -0
- package/src/config.ts +326 -0
- package/src/core/aaak.ts +142 -0
- package/src/core/annotations.ts +457 -0
- package/src/core/banks.ts +133 -0
- package/src/core/beam/consolidate.ts +965 -0
- package/src/core/beam/helpers.ts +977 -0
- package/src/core/beam/index.ts +353 -0
- package/src/core/beam/recall.ts +1100 -0
- package/src/core/beam/schema.ts +423 -0
- package/src/core/beam/store.ts +829 -0
- package/src/core/beam/types.ts +268 -0
- package/src/core/binary-vectors.ts +317 -0
- package/src/core/chat-normalize.ts +160 -0
- package/src/core/content-sanitizer.ts +136 -0
- package/src/core/cost-log.ts +103 -0
- package/src/core/embeddings.ts +423 -0
- package/src/core/entities.ts +259 -0
- package/src/core/episodic-graph.ts +708 -0
- package/src/core/extraction/client.ts +162 -0
- package/src/core/extraction/diagnostics.ts +193 -0
- package/src/core/extraction/prompts.ts +31 -0
- package/src/core/extraction.ts +335 -0
- package/src/core/index.ts +30 -0
- package/src/core/llm-backends.ts +51 -0
- package/src/core/local-llm.ts +436 -0
- package/src/core/memory.ts +630 -0
- package/src/core/migrations/e6-triplestore-split.ts +211 -0
- package/src/core/migrations/index.ts +1 -0
- package/src/core/mmr.ts +71 -0
- package/src/core/orchestrator.ts +62 -0
- package/src/core/patterns.ts +484 -0
- package/src/core/plugins.ts +375 -0
- package/src/core/polyphonic-recall.ts +563 -0
- package/src/core/query-cache.ts +354 -0
- package/src/core/query-intent.ts +139 -0
- package/src/core/recall-diagnostics.ts +157 -0
- package/src/core/runtime-options.ts +119 -0
- package/src/core/shmr.ts +460 -0
- package/src/core/streaming.ts +419 -0
- package/src/core/synonyms.ts +197 -0
- package/src/core/temporal-parser.ts +363 -0
- package/src/core/token-counter.ts +30 -0
- package/src/core/triples.ts +454 -0
- package/src/core/typed-memory.ts +407 -0
- package/src/core/vector-math.ts +23 -0
- package/src/core/veracity-consolidation.ts +477 -0
- package/src/core/weibull.ts +124 -0
- package/src/db.ts +128 -0
- package/src/diagnose.ts +174 -0
- package/src/dr/index.ts +1 -0
- package/src/dr/recovery.ts +405 -0
- package/src/index.ts +33 -0
- package/src/mcp-server.ts +155 -0
- package/src/mcp-tools.ts +970 -0
- package/src/migrations/e6-triplestore-split.ts +1 -0
- package/src/migrations/index.ts +1 -0
- package/src/types.ts +157 -0
- package/src/util/datetime.ts +69 -0
- package/src/util/env.ts +65 -0
- package/src/util/ids.ts +19 -0
- package/src/util/lru.ts +48 -0
- package/src/util/regex.ts +165 -0
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
import type { Database } from "bun:sqlite";
|
|
2
|
+
import type { Api, Model } from "@prometheus-ai/ai";
|
|
3
|
+
|
|
4
|
+
import { dbPath as configuredDbPath } from "../config";
|
|
5
|
+
import { closeQuietly } from "../db";
|
|
6
|
+
import type { MemoryInput, Metadata } from "../types";
|
|
7
|
+
import { AnnotationStore } from "./annotations";
|
|
8
|
+
import { BankManager } from "./banks";
|
|
9
|
+
import { BeamMemory, initBeam } from "./beam/index";
|
|
10
|
+
import type { RecallEnhancedOptions, RecallOptions, RecallResult, SleepResult } from "./beam/types";
|
|
11
|
+
import { EpisodicGraph } from "./episodic-graph";
|
|
12
|
+
import {
|
|
13
|
+
isPiAiModel,
|
|
14
|
+
type MnemopiEmbeddingRuntimeOptions,
|
|
15
|
+
type MnemopiLlmCompletion,
|
|
16
|
+
type MnemopiLlmRuntimeOptions,
|
|
17
|
+
type ResolvedMnemopiRuntimeOptions,
|
|
18
|
+
resolveEmbeddingProvider,
|
|
19
|
+
withMnemopiRuntimeOptions,
|
|
20
|
+
} from "./runtime-options";
|
|
21
|
+
|
|
22
|
+
export interface MnemopiOptions {
|
|
23
|
+
readonly db?: Database;
|
|
24
|
+
readonly dbPath?: string;
|
|
25
|
+
readonly db_path?: string;
|
|
26
|
+
readonly sessionId?: string;
|
|
27
|
+
readonly session_id?: string;
|
|
28
|
+
readonly bank?: string | null;
|
|
29
|
+
readonly authorId?: string | null;
|
|
30
|
+
readonly author_id?: string | null;
|
|
31
|
+
readonly authorType?: string | null;
|
|
32
|
+
readonly author_type?: string | null;
|
|
33
|
+
readonly channelId?: string | null;
|
|
34
|
+
readonly channel_id?: string | null;
|
|
35
|
+
readonly noEmbeddings?: boolean;
|
|
36
|
+
readonly embeddingModel?: string;
|
|
37
|
+
readonly embeddingApiUrl?: string;
|
|
38
|
+
readonly embeddingApiKey?: string;
|
|
39
|
+
readonly embeddings?: false | MnemopiEmbeddingRuntimeOptions;
|
|
40
|
+
readonly llmEnabled?: boolean;
|
|
41
|
+
readonly llmBaseUrl?: string;
|
|
42
|
+
readonly llmApiKey?: string;
|
|
43
|
+
readonly llmModel?: string | Model<Api>;
|
|
44
|
+
readonly llm?: false | MnemopiLlmRuntimeOptions | Model<Api> | MnemopiLlmCompletion;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RememberInput extends MemoryInput {
|
|
48
|
+
readonly extract?: boolean;
|
|
49
|
+
readonly extractEntities?: boolean;
|
|
50
|
+
readonly extract_entities?: boolean;
|
|
51
|
+
readonly trustTier?: string | null;
|
|
52
|
+
readonly trust_tier?: string | null;
|
|
53
|
+
readonly memoryType?: string | null;
|
|
54
|
+
readonly memory_type?: string | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface RememberFacadeOptions {
|
|
58
|
+
readonly source?: string | null;
|
|
59
|
+
readonly importance?: number;
|
|
60
|
+
readonly metadata?: Metadata | null;
|
|
61
|
+
readonly validUntil?: string | Date | null;
|
|
62
|
+
readonly valid_until?: string | Date | null;
|
|
63
|
+
readonly scope?: string | null;
|
|
64
|
+
readonly extractEntities?: boolean;
|
|
65
|
+
readonly extract_entities?: boolean;
|
|
66
|
+
readonly extract?: boolean;
|
|
67
|
+
readonly trustTier?: string | null;
|
|
68
|
+
readonly trust_tier?: string | null;
|
|
69
|
+
readonly timestamp?: string | Date | null;
|
|
70
|
+
readonly veracity?: string | null;
|
|
71
|
+
readonly memoryType?: string | null;
|
|
72
|
+
readonly memory_type?: string | null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface RecallFacadeOptions
|
|
76
|
+
extends Omit<RecallOptions, "temporalHalflife" | "vecWeight" | "ftsWeight" | "importanceWeight"> {
|
|
77
|
+
readonly from_date?: string | null;
|
|
78
|
+
readonly to_date?: string | null;
|
|
79
|
+
readonly source?: string | null;
|
|
80
|
+
readonly topic?: string | null;
|
|
81
|
+
readonly temporalWeight?: number;
|
|
82
|
+
readonly temporal_weight?: number;
|
|
83
|
+
readonly query_time?: string | Date | null;
|
|
84
|
+
readonly temporalHalflife?: number | null;
|
|
85
|
+
readonly temporal_halflife?: number | null;
|
|
86
|
+
readonly vecWeight?: number | null;
|
|
87
|
+
readonly vec_weight?: number | null;
|
|
88
|
+
readonly ftsWeight?: number | null;
|
|
89
|
+
readonly fts_weight?: number | null;
|
|
90
|
+
readonly importanceWeight?: number | null;
|
|
91
|
+
readonly importance_weight?: number | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface MemoryFacadeStats {
|
|
95
|
+
total_memories: number;
|
|
96
|
+
total_sessions: number;
|
|
97
|
+
sources: Record<string, number>;
|
|
98
|
+
last_memory: string | null;
|
|
99
|
+
database: string;
|
|
100
|
+
mode: "beam";
|
|
101
|
+
banks: string[];
|
|
102
|
+
beam: {
|
|
103
|
+
working_memory: unknown;
|
|
104
|
+
episodic_memory: unknown;
|
|
105
|
+
triples: { total: number };
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type Row = Record<string, unknown>;
|
|
110
|
+
type BeamRecallFacadeOptions = RecallOptions & {
|
|
111
|
+
source?: string | null;
|
|
112
|
+
topic?: string | null;
|
|
113
|
+
temporalWeight?: number;
|
|
114
|
+
temporalHalflife?: number;
|
|
115
|
+
vecWeight?: number;
|
|
116
|
+
ftsWeight?: number;
|
|
117
|
+
importanceWeight?: number;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
type ModuleRememberOptions = RememberFacadeOptions & { readonly bank?: string | null };
|
|
121
|
+
type ModuleRecallOptions = RecallFacadeOptions & { readonly bank?: string | null };
|
|
122
|
+
type ModuleRecallEnhancedOptions = RecallFacadeOptions & RecallEnhancedOptions & { readonly bank?: string | null };
|
|
123
|
+
type FacadeRememberOptions = {
|
|
124
|
+
source: string;
|
|
125
|
+
importance: number;
|
|
126
|
+
metadata: Metadata | null;
|
|
127
|
+
valid_until: string | null | undefined;
|
|
128
|
+
scope: string;
|
|
129
|
+
extractEntities: boolean;
|
|
130
|
+
extract: boolean;
|
|
131
|
+
trustTier: string | undefined;
|
|
132
|
+
veracity: string | undefined;
|
|
133
|
+
memoryType: string | undefined;
|
|
134
|
+
timestamp?: string;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
function hasOwn(options: MnemopiOptions, key: keyof MnemopiOptions): boolean {
|
|
138
|
+
return Object.hasOwn(options, key);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function resolveRuntimeOptions(options: MnemopiOptions): ResolvedMnemopiRuntimeOptions | undefined {
|
|
142
|
+
const nestedEmbeddings =
|
|
143
|
+
options.embeddings !== false && options.embeddings !== undefined ? options.embeddings : undefined;
|
|
144
|
+
const embeddingDisabled =
|
|
145
|
+
options.embeddings === false
|
|
146
|
+
? true
|
|
147
|
+
: hasOwn(options, "noEmbeddings")
|
|
148
|
+
? options.noEmbeddings
|
|
149
|
+
: nestedEmbeddings?.disabled;
|
|
150
|
+
const embeddingModel = options.embeddingModel ?? nestedEmbeddings?.model;
|
|
151
|
+
const embeddingApiUrl = options.embeddingApiUrl ?? nestedEmbeddings?.apiUrl;
|
|
152
|
+
const embeddingApiKey = options.embeddingApiKey ?? nestedEmbeddings?.apiKey;
|
|
153
|
+
const embeddingProvider = resolveEmbeddingProvider(nestedEmbeddings?.provider);
|
|
154
|
+
|
|
155
|
+
const embeddings =
|
|
156
|
+
embeddingDisabled !== undefined ||
|
|
157
|
+
embeddingModel !== undefined ||
|
|
158
|
+
embeddingApiUrl !== undefined ||
|
|
159
|
+
embeddingApiKey !== undefined ||
|
|
160
|
+
embeddingProvider !== undefined
|
|
161
|
+
? {
|
|
162
|
+
disabled: embeddingDisabled,
|
|
163
|
+
model: embeddingModel,
|
|
164
|
+
apiUrl: embeddingApiUrl,
|
|
165
|
+
apiKey: embeddingApiKey,
|
|
166
|
+
provider: embeddingProvider,
|
|
167
|
+
}
|
|
168
|
+
: undefined;
|
|
169
|
+
|
|
170
|
+
let llm: ResolvedMnemopiRuntimeOptions["llm"];
|
|
171
|
+
if (options.llm === false) {
|
|
172
|
+
llm = { enabled: false };
|
|
173
|
+
} else if (typeof options.llm === "function") {
|
|
174
|
+
llm = { enabled: true, complete: options.llm };
|
|
175
|
+
} else if (isPiAiModel(options.llm)) {
|
|
176
|
+
llm = { enabled: true, model: options.llm };
|
|
177
|
+
} else {
|
|
178
|
+
const nestedLlm = options.llm !== undefined && !isPiAiModel(options.llm) ? options.llm : undefined;
|
|
179
|
+
const llmModel = nestedLlm?.model ?? options.llmModel;
|
|
180
|
+
const llmEnabled = hasOwn(options, "llmEnabled")
|
|
181
|
+
? options.llmEnabled
|
|
182
|
+
: (nestedLlm?.enabled ??
|
|
183
|
+
(nestedLlm?.baseUrl !== undefined ||
|
|
184
|
+
nestedLlm?.apiKey !== undefined ||
|
|
185
|
+
nestedLlm?.maxTokens !== undefined ||
|
|
186
|
+
nestedLlm?.complete !== undefined ||
|
|
187
|
+
llmModel !== undefined ||
|
|
188
|
+
hasOwn(options, "llmBaseUrl") ||
|
|
189
|
+
hasOwn(options, "llmApiKey") ||
|
|
190
|
+
hasOwn(options, "llmModel")))
|
|
191
|
+
? true
|
|
192
|
+
: undefined;
|
|
193
|
+
const llmBaseUrl = options.llmBaseUrl ?? nestedLlm?.baseUrl;
|
|
194
|
+
const llmApiKey = options.llmApiKey ?? nestedLlm?.apiKey;
|
|
195
|
+
const llmMaxTokens = nestedLlm?.maxTokens;
|
|
196
|
+
const llmComplete = nestedLlm?.complete;
|
|
197
|
+
const llmExtractionPrompt = nestedLlm?.extractionPrompt;
|
|
198
|
+
const llmConsolidationPrompt = nestedLlm?.consolidationPrompt;
|
|
199
|
+
if (
|
|
200
|
+
llmEnabled !== undefined ||
|
|
201
|
+
llmBaseUrl !== undefined ||
|
|
202
|
+
llmApiKey !== undefined ||
|
|
203
|
+
llmModel !== undefined ||
|
|
204
|
+
llmMaxTokens !== undefined ||
|
|
205
|
+
llmComplete !== undefined ||
|
|
206
|
+
llmExtractionPrompt !== undefined ||
|
|
207
|
+
llmConsolidationPrompt !== undefined
|
|
208
|
+
) {
|
|
209
|
+
llm = {
|
|
210
|
+
enabled: llmEnabled,
|
|
211
|
+
baseUrl: llmBaseUrl,
|
|
212
|
+
apiKey: llmApiKey,
|
|
213
|
+
model: llmModel,
|
|
214
|
+
maxTokens: llmMaxTokens,
|
|
215
|
+
complete: llmComplete,
|
|
216
|
+
extractionPrompt: llmExtractionPrompt,
|
|
217
|
+
consolidationPrompt: llmConsolidationPrompt,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (embeddings === undefined && llm === undefined) {
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
return { embeddings, llm };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let defaultInstance: Mnemopi | null = null;
|
|
229
|
+
let defaultBank = "default";
|
|
230
|
+
|
|
231
|
+
function normalizeDate(value: string | Date | null | undefined): string | null | undefined {
|
|
232
|
+
if (value instanceof Date) return value.toISOString();
|
|
233
|
+
return value ?? undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function resolveDbPath(options: MnemopiOptions, bank: string): string | undefined {
|
|
237
|
+
const explicit = options.dbPath ?? options.db_path;
|
|
238
|
+
if (explicit !== undefined) return explicit;
|
|
239
|
+
if (options.db !== undefined) return undefined;
|
|
240
|
+
if (bank !== "default") return new BankManager().getBankDbPath(bank);
|
|
241
|
+
return configuredDbPath();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function toRememberOptions(input: string | RememberInput, options: RememberFacadeOptions) {
|
|
245
|
+
const memory = typeof input === "string" ? null : input;
|
|
246
|
+
const timestamp = normalizeDate(options.timestamp ?? memory?.timestamp);
|
|
247
|
+
const rememberOptions: FacadeRememberOptions = {
|
|
248
|
+
source: options.source ?? memory?.source ?? "conversation",
|
|
249
|
+
importance: options.importance ?? memory?.importance ?? 0.5,
|
|
250
|
+
metadata: options.metadata ?? memory?.metadata ?? null,
|
|
251
|
+
valid_until: normalizeDate(options.valid_until ?? options.validUntil ?? memory?.valid_until),
|
|
252
|
+
scope: options.scope ?? memory?.scope ?? "session",
|
|
253
|
+
extractEntities:
|
|
254
|
+
options.extractEntities ??
|
|
255
|
+
options.extract_entities ??
|
|
256
|
+
memory?.extractEntities ??
|
|
257
|
+
memory?.extract_entities ??
|
|
258
|
+
false,
|
|
259
|
+
extract: options.extract ?? memory?.extract ?? false,
|
|
260
|
+
trustTier: options.trustTier ?? options.trust_tier ?? memory?.trustTier ?? memory?.trust_tier ?? undefined,
|
|
261
|
+
veracity: options.veracity ?? memory?.veracity ?? undefined,
|
|
262
|
+
memoryType: options.memoryType ?? options.memory_type ?? memory?.memoryType ?? memory?.memory_type ?? undefined,
|
|
263
|
+
};
|
|
264
|
+
if (timestamp !== null && timestamp !== undefined) rememberOptions.timestamp = timestamp;
|
|
265
|
+
return rememberOptions;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function toRecallOptions(options: RecallFacadeOptions): BeamRecallFacadeOptions {
|
|
269
|
+
const beamOptions: BeamRecallFacadeOptions = {
|
|
270
|
+
fromDate: options.fromDate ?? options.from_date ?? null,
|
|
271
|
+
toDate: options.toDate ?? options.to_date ?? null,
|
|
272
|
+
authorId: options.authorId ?? null,
|
|
273
|
+
authorType: options.authorType ?? null,
|
|
274
|
+
channelId: options.channelId ?? null,
|
|
275
|
+
includeWorking: options.includeWorking,
|
|
276
|
+
queryTime: options.queryTime ?? options.query_time ?? null,
|
|
277
|
+
source: options.source ?? null,
|
|
278
|
+
topic: options.topic ?? null,
|
|
279
|
+
temporalWeight: options.temporalWeight ?? options.temporal_weight ?? undefined,
|
|
280
|
+
temporalHalflife: options.temporalHalflife ?? options.temporal_halflife ?? undefined,
|
|
281
|
+
vecWeight: options.vecWeight ?? options.vec_weight ?? undefined,
|
|
282
|
+
ftsWeight: options.ftsWeight ?? options.fts_weight ?? undefined,
|
|
283
|
+
importanceWeight: options.importanceWeight ?? options.importance_weight ?? undefined,
|
|
284
|
+
};
|
|
285
|
+
// Preserve the three-state semantics (`undefined` = auto-derive, `null` = explicitly
|
|
286
|
+
// FTS-only, `number[]` = caller-supplied) so callers can opt out of `recall()`'s
|
|
287
|
+
// auto-embed without being forced into it.
|
|
288
|
+
if ("queryEmbedding" in options) beamOptions.queryEmbedding = options.queryEmbedding;
|
|
289
|
+
return beamOptions;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function countRows(db: Database, sql: string, ...params: (string | number | null)[]): number {
|
|
293
|
+
const row = db.prepare(sql).get(...params) as { total?: number; count?: number } | null;
|
|
294
|
+
return row?.total ?? row?.count ?? 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function dataDirForDbPath(path: string): string | undefined {
|
|
298
|
+
const slash = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
299
|
+
if (slash < 0) return undefined;
|
|
300
|
+
const parent = path.slice(0, slash);
|
|
301
|
+
const marker = `${parent.includes("\\") ? "\\" : "/"}banks${parent.includes("\\") ? "\\" : "/"}`;
|
|
302
|
+
const bankIndex = parent.lastIndexOf(marker);
|
|
303
|
+
return bankIndex < 0 ? parent : parent.slice(0, bankIndex);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function sourceCounts(db: Database): Record<string, number> {
|
|
307
|
+
const counts: Record<string, number> = {};
|
|
308
|
+
for (const row of db
|
|
309
|
+
.prepare("SELECT source, COUNT(*) AS total FROM working_memory GROUP BY source")
|
|
310
|
+
.all() as Row[]) {
|
|
311
|
+
counts[String(row.source ?? "") || "conversation"] = Number(row.total ?? 0);
|
|
312
|
+
}
|
|
313
|
+
return counts;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function buildBeamAnnotations(db: Database, dbPath: string | undefined): BeamMemory["annotations"] {
|
|
317
|
+
const annotationStore = dbPath === undefined ? new AnnotationStore({ db }) : new AnnotationStore({ db, dbPath });
|
|
318
|
+
return {
|
|
319
|
+
add: (memoryId, kind, value, writeOptions) =>
|
|
320
|
+
annotationStore.add(memoryId, kind, value, writeOptions?.source, writeOptions?.confidence),
|
|
321
|
+
addMany: (memoryId, kind, values, writeOptions) =>
|
|
322
|
+
annotationStore.addMany(memoryId, kind, values, writeOptions?.source, writeOptions?.confidence),
|
|
323
|
+
queryByMemory: (memoryId, kind) => annotationStore.queryByMemory(memoryId, kind),
|
|
324
|
+
queryByKind: (kind, value) => annotationStore.queryByKind(kind, { value }),
|
|
325
|
+
getDistinctValues: kind => annotationStore.getDistinctValues(kind),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function buildEpisodicGraph(db: Database, dbPath: string | undefined): EpisodicGraph {
|
|
330
|
+
return dbPath === undefined ? new EpisodicGraph({ db }) : new EpisodicGraph({ db, dbPath });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function defaultFor(bank: string | null | undefined = null): Mnemopi {
|
|
334
|
+
const targetBank = bank ?? defaultBank ?? "default";
|
|
335
|
+
if (defaultInstance === null || defaultInstance.bank !== targetBank) {
|
|
336
|
+
defaultInstance?.close();
|
|
337
|
+
defaultBank = targetBank;
|
|
338
|
+
defaultInstance = new Mnemopi({ bank: targetBank });
|
|
339
|
+
}
|
|
340
|
+
return defaultInstance;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export class Mnemopi {
|
|
344
|
+
readonly sessionId: string;
|
|
345
|
+
readonly bank: string;
|
|
346
|
+
readonly dbPath?: string;
|
|
347
|
+
readonly authorId: string | null;
|
|
348
|
+
readonly authorType: string | null;
|
|
349
|
+
readonly channelId: string;
|
|
350
|
+
readonly beam: BeamMemory;
|
|
351
|
+
readonly conn: Database;
|
|
352
|
+
readonly db: Database;
|
|
353
|
+
readonly runtimeOptions?: ResolvedMnemopiRuntimeOptions;
|
|
354
|
+
#ownsDb: boolean;
|
|
355
|
+
#closed = false;
|
|
356
|
+
|
|
357
|
+
constructor(options: MnemopiOptions = {}) {
|
|
358
|
+
this.sessionId = options.sessionId ?? options.session_id ?? "default";
|
|
359
|
+
this.bank = options.bank ?? "default";
|
|
360
|
+
this.authorId = options.authorId ?? options.author_id ?? null;
|
|
361
|
+
this.authorType = options.authorType ?? options.author_type ?? null;
|
|
362
|
+
this.channelId = options.channelId ?? options.channel_id ?? this.sessionId;
|
|
363
|
+
this.dbPath = resolveDbPath(options, this.bank);
|
|
364
|
+
this.runtimeOptions = resolveRuntimeOptions(options);
|
|
365
|
+
|
|
366
|
+
this.beam = new BeamMemory({
|
|
367
|
+
sessionId: this.sessionId,
|
|
368
|
+
dbPath: options.db === undefined ? this.dbPath : ":memory:",
|
|
369
|
+
authorId: this.authorId,
|
|
370
|
+
authorType: this.authorType,
|
|
371
|
+
channelId: this.channelId,
|
|
372
|
+
});
|
|
373
|
+
this.#ownsDb = options.db === undefined;
|
|
374
|
+
if (options.db !== undefined) {
|
|
375
|
+
const opened = this.beam.db;
|
|
376
|
+
initBeam(options.db);
|
|
377
|
+
Object.defineProperty(this.beam, "db", { value: options.db });
|
|
378
|
+
Object.defineProperty(this.beam, "annotations", {
|
|
379
|
+
value: buildBeamAnnotations(options.db, this.dbPath),
|
|
380
|
+
});
|
|
381
|
+
Object.defineProperty(this.beam, "episodicGraph", {
|
|
382
|
+
value: buildEpisodicGraph(options.db, this.dbPath),
|
|
383
|
+
});
|
|
384
|
+
closeQuietly(opened);
|
|
385
|
+
}
|
|
386
|
+
this.conn = this.beam.db;
|
|
387
|
+
this.db = this.beam.db;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
close(): void {
|
|
391
|
+
if (this.#closed) return;
|
|
392
|
+
this.#closed = true;
|
|
393
|
+
if (this.#ownsDb) this.beam.close();
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
async flushExtractions(): Promise<void> {
|
|
397
|
+
await this.beam.flushExtractions();
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
remember(memory: string | RememberInput, options: RememberFacadeOptions = {}): string {
|
|
401
|
+
const content = typeof memory === "string" ? memory : memory.content;
|
|
402
|
+
return this.#withRuntimeOptions(() => this.beam.remember(content, toRememberOptions(memory, options)));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
recall(query: string, topK = 5, options: RecallFacadeOptions = {}): Promise<RecallResult[]> {
|
|
406
|
+
return this.#withRuntimeOptions(() => this.beam.recall(query, topK, toRecallOptions(options)));
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
recallEnhanced(
|
|
410
|
+
query: string,
|
|
411
|
+
topK = 5,
|
|
412
|
+
options: RecallFacadeOptions & RecallEnhancedOptions = {},
|
|
413
|
+
): Promise<RecallResult[]> {
|
|
414
|
+
return this.#withRuntimeOptions(() =>
|
|
415
|
+
this.beam.recallEnhanced(query, topK, {
|
|
416
|
+
...toRecallOptions(options),
|
|
417
|
+
useCache: options.useCache,
|
|
418
|
+
includeFacts: options.includeFacts,
|
|
419
|
+
}),
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
getContext(limit = 10): unknown[] {
|
|
424
|
+
return this.#withRuntimeOptions(() => this.beam.getContext(limit));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
getStats(
|
|
428
|
+
authorId: string | null = null,
|
|
429
|
+
authorType: string | null = null,
|
|
430
|
+
channelId: string | null = null,
|
|
431
|
+
): MemoryFacadeStats {
|
|
432
|
+
const working = this.#withRuntimeOptions(() => this.beam.getWorkingStats(authorId, authorType, channelId));
|
|
433
|
+
const episodic = this.#withRuntimeOptions(() => this.beam.getEpisodicStats(authorId, authorType, channelId));
|
|
434
|
+
const totalMemories = countRows(this.conn, "SELECT COUNT(*) AS total FROM working_memory");
|
|
435
|
+
const totalSessions = countRows(this.conn, "SELECT COUNT(DISTINCT session_id) AS total FROM working_memory");
|
|
436
|
+
const last = this.conn.prepare("SELECT timestamp FROM working_memory ORDER BY timestamp DESC LIMIT 1").get() as {
|
|
437
|
+
timestamp: string | null;
|
|
438
|
+
} | null;
|
|
439
|
+
const tripleTotal = countRows(this.conn, "SELECT COUNT(*) AS total FROM triples");
|
|
440
|
+
let banks = ["default"];
|
|
441
|
+
if (this.dbPath !== undefined && this.dbPath !== ":memory:") {
|
|
442
|
+
const dataDir = dataDirForDbPath(this.dbPath);
|
|
443
|
+
banks = new BankManager(dataDir).listBanks();
|
|
444
|
+
}
|
|
445
|
+
return {
|
|
446
|
+
total_memories: totalMemories,
|
|
447
|
+
total_sessions: totalSessions,
|
|
448
|
+
sources: sourceCounts(this.conn),
|
|
449
|
+
last_memory: last?.timestamp ?? null,
|
|
450
|
+
database: this.dbPath ?? ":memory:",
|
|
451
|
+
mode: "beam",
|
|
452
|
+
banks,
|
|
453
|
+
beam: { working_memory: working, episodic_memory: episodic, triples: { total: tripleTotal } },
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
get(memoryId: string): unknown | null {
|
|
458
|
+
return this.#withRuntimeOptions(() => this.beam.get(memoryId));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
forget(memoryId: string): boolean {
|
|
462
|
+
return this.#withRuntimeOptions(() => this.beam.forgetWorking(memoryId));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
update(memoryId: string, content: string | null = null, importance: number | null = null): boolean {
|
|
466
|
+
return this.#withRuntimeOptions(() => this.beam.updateWorking(memoryId, content, importance));
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
sleep(dryRun = false): SleepResult {
|
|
470
|
+
return this.#withRuntimeOptions(() => this.beam.sleep(dryRun));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
sleepAllSessions(dryRun = false): SleepResult {
|
|
474
|
+
return this.#withRuntimeOptions(() => this.beam.sleepAllSessions(dryRun));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
scratchpadWrite(content: string): string {
|
|
478
|
+
return this.#withRuntimeOptions(() => this.beam.scratchpadWrite(content));
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
scratchpadRead(): unknown[] {
|
|
482
|
+
return this.#withRuntimeOptions(() => this.beam.scratchpadRead());
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
scratchpadClear(): void {
|
|
486
|
+
this.#withRuntimeOptions(() => this.beam.scratchpadClear());
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
addMemory(memory: string | RememberInput, options: RememberFacadeOptions = {}): string {
|
|
490
|
+
return this.remember(memory, options);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
saveMemory(memory: string | RememberInput, options: RememberFacadeOptions = {}): string {
|
|
494
|
+
return this.remember(memory, options);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
storeMemory(memory: string | RememberInput, options: RememberFacadeOptions = {}): string {
|
|
498
|
+
return this.remember(memory, options);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
search(query: string, topK = 5, options: RecallFacadeOptions = {}): Promise<RecallResult[]> {
|
|
502
|
+
return this.recall(query, topK, options);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
query(query: string, topK = 5, options: RecallFacadeOptions = {}): Promise<RecallResult[]> {
|
|
506
|
+
return this.recall(query, topK, options);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
consolidate(dryRun = false): SleepResult {
|
|
510
|
+
return this.sleep(dryRun);
|
|
511
|
+
}
|
|
512
|
+
#withRuntimeOptions<T>(fn: () => T): T {
|
|
513
|
+
return withMnemopiRuntimeOptions(this.runtimeOptions, fn);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
export function setBank(bank: string): void {
|
|
518
|
+
defaultBank = bank;
|
|
519
|
+
defaultInstance?.close();
|
|
520
|
+
defaultInstance = null;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export function getBank(): string {
|
|
524
|
+
return defaultBank || "default";
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export function getDefaultInstance(bank: string | null = null): Mnemopi {
|
|
528
|
+
return defaultFor(bank);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export function remember(content: string | RememberInput, options: ModuleRememberOptions = {}): string {
|
|
532
|
+
return defaultFor(options.bank).remember(content, options);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export function recall(query: string, topK = 5, options: ModuleRecallOptions = {}): Promise<RecallResult[]> {
|
|
536
|
+
return defaultFor(options.bank).recall(query, topK, options);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function recallEnhanced(
|
|
540
|
+
query: string,
|
|
541
|
+
topK = 5,
|
|
542
|
+
options: ModuleRecallEnhancedOptions = {},
|
|
543
|
+
): Promise<RecallResult[]> {
|
|
544
|
+
return defaultFor(options.bank).recallEnhanced(query, topK, options);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export function getContext(limit = 10, bank: string | null = null): unknown[] {
|
|
548
|
+
return defaultFor(bank).getContext(limit);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
export function getStats(bank: string | null = null): MemoryFacadeStats {
|
|
552
|
+
return defaultFor(bank).getStats();
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export function get(memoryId: string, bank: string | null = null): unknown | null {
|
|
556
|
+
return defaultFor(bank).get(memoryId);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export function forget(memoryId: string, bank: string | null = null): boolean {
|
|
560
|
+
return defaultFor(bank).forget(memoryId);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export function update(
|
|
564
|
+
memoryId: string,
|
|
565
|
+
content: string | null = null,
|
|
566
|
+
importance: number | null = null,
|
|
567
|
+
bank: string | null = null,
|
|
568
|
+
): boolean {
|
|
569
|
+
return defaultFor(bank).update(memoryId, content, importance);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
export function sleep(dryRun = false, bank: string | null = null): SleepResult {
|
|
573
|
+
return defaultFor(bank).sleep(dryRun);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export function sleepAllSessions(dryRun = false, bank: string | null = null): SleepResult {
|
|
577
|
+
return defaultFor(bank).sleepAllSessions(dryRun);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
export function flushExtractions(bank: string | null = null): Promise<void> {
|
|
581
|
+
return defaultFor(bank).flushExtractions();
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export function scratchpadWrite(content: string, bank: string | null = null): string {
|
|
585
|
+
return defaultFor(bank).scratchpadWrite(content);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export function scratchpadRead(bank: string | null = null): unknown[] {
|
|
589
|
+
return defaultFor(bank).scratchpadRead();
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
export function scratchpadClear(bank: string | null = null): void {
|
|
593
|
+
defaultFor(bank).scratchpadClear();
|
|
594
|
+
}
|
|
595
|
+
export function addMemory(memory: string | RememberInput, options: ModuleRememberOptions = {}): string {
|
|
596
|
+
return remember(memory, options);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
export function saveMemory(memory: string | RememberInput, options: ModuleRememberOptions = {}): string {
|
|
600
|
+
return remember(memory, options);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function storeMemory(memory: string | RememberInput, options: ModuleRememberOptions = {}): string {
|
|
604
|
+
return remember(memory, options);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
export function search(query: string, topK = 5, options: ModuleRecallOptions = {}): Promise<RecallResult[]> {
|
|
608
|
+
return recall(query, topK, options);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export function query(query: string, topK = 5, options: ModuleRecallOptions = {}): Promise<RecallResult[]> {
|
|
612
|
+
return recall(query, topK, options);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
export function resetDefaultInstanceForTests(): void {
|
|
616
|
+
defaultInstance?.close();
|
|
617
|
+
defaultInstance = null;
|
|
618
|
+
defaultBank = "default";
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
export function resetMemoryForTests(): void {
|
|
622
|
+
resetDefaultInstanceForTests();
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
export function resetModuleStateForTests(): void {
|
|
626
|
+
resetDefaultInstanceForTests();
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export type { MemoryInput, MemoryStats } from "../types";
|
|
630
|
+
export default Mnemopi;
|