jeopi-mnemopi 16.2.13

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.
Files changed (135) hide show
  1. package/CHANGELOG.md +172 -0
  2. package/README.md +107 -0
  3. package/dist/types/cli.d.ts +35 -0
  4. package/dist/types/config.d.ts +108 -0
  5. package/dist/types/core/aaak.d.ts +55 -0
  6. package/dist/types/core/annotations.d.ts +75 -0
  7. package/dist/types/core/banks.d.ts +33 -0
  8. package/dist/types/core/beam/consolidate.d.ts +32 -0
  9. package/dist/types/core/beam/helpers.d.ts +69 -0
  10. package/dist/types/core/beam/index.d.ts +59 -0
  11. package/dist/types/core/beam/recall.d.ts +32 -0
  12. package/dist/types/core/beam/schema.d.ts +2 -0
  13. package/dist/types/core/beam/store.d.ts +56 -0
  14. package/dist/types/core/beam/types.d.ts +245 -0
  15. package/dist/types/core/binary-vectors.d.ts +54 -0
  16. package/dist/types/core/chat-normalize.d.ts +13 -0
  17. package/dist/types/core/content-sanitizer.d.ts +18 -0
  18. package/dist/types/core/cost-log.d.ts +13 -0
  19. package/dist/types/core/embeddings.d.ts +54 -0
  20. package/dist/types/core/entities.d.ts +9 -0
  21. package/dist/types/core/episodic-graph.d.ts +89 -0
  22. package/dist/types/core/extraction/client.d.ts +35 -0
  23. package/dist/types/core/extraction/diagnostics.d.ts +51 -0
  24. package/dist/types/core/extraction/prompts.d.ts +2 -0
  25. package/dist/types/core/extraction.d.ts +7 -0
  26. package/dist/types/core/fastembed-model-cache.d.ts +2 -0
  27. package/dist/types/core/fastembed-runtime.d.ts +14 -0
  28. package/dist/types/core/index.d.ts +6 -0
  29. package/dist/types/core/llm-backends.d.ts +23 -0
  30. package/dist/types/core/local-llm.d.ts +20 -0
  31. package/dist/types/core/memory.d.ts +178 -0
  32. package/dist/types/core/migrations/e6-triplestore-split.d.ts +17 -0
  33. package/dist/types/core/migrations/index.d.ts +1 -0
  34. package/dist/types/core/mmr.d.ts +8 -0
  35. package/dist/types/core/orchestrator.d.ts +20 -0
  36. package/dist/types/core/patterns.d.ts +61 -0
  37. package/dist/types/core/plugins.d.ts +109 -0
  38. package/dist/types/core/polyphonic-recall.d.ts +66 -0
  39. package/dist/types/core/query-cache.d.ts +45 -0
  40. package/dist/types/core/query-intent.d.ts +20 -0
  41. package/dist/types/core/recall-diagnostics.d.ts +48 -0
  42. package/dist/types/core/runtime-options.d.ts +76 -0
  43. package/dist/types/core/shmr.d.ts +62 -0
  44. package/dist/types/core/streaming.d.ts +136 -0
  45. package/dist/types/core/synonyms.d.ts +46 -0
  46. package/dist/types/core/temporal-parser.d.ts +16 -0
  47. package/dist/types/core/token-counter.d.ts +8 -0
  48. package/dist/types/core/triples.d.ts +63 -0
  49. package/dist/types/core/typed-memory.d.ts +39 -0
  50. package/dist/types/core/vector-index.d.ts +16 -0
  51. package/dist/types/core/vector-math.d.ts +1 -0
  52. package/dist/types/core/veracity-consolidation.d.ts +60 -0
  53. package/dist/types/core/weibull.d.ts +96 -0
  54. package/dist/types/db.d.ts +16 -0
  55. package/dist/types/diagnose.d.ts +24 -0
  56. package/dist/types/dr/index.d.ts +1 -0
  57. package/dist/types/dr/recovery.d.ts +68 -0
  58. package/dist/types/index.d.ts +6 -0
  59. package/dist/types/mcp-server.d.ts +40 -0
  60. package/dist/types/mcp-tools.d.ts +484 -0
  61. package/dist/types/migrations/e6-triplestore-split.d.ts +1 -0
  62. package/dist/types/migrations/index.d.ts +1 -0
  63. package/dist/types/types.d.ts +145 -0
  64. package/dist/types/util/datetime.d.ts +8 -0
  65. package/dist/types/util/env.d.ts +10 -0
  66. package/dist/types/util/ids.d.ts +3 -0
  67. package/dist/types/util/lru.d.ts +12 -0
  68. package/dist/types/util/regex.d.ts +11 -0
  69. package/package.json +108 -0
  70. package/src/cli.ts +398 -0
  71. package/src/config.ts +373 -0
  72. package/src/core/aaak.ts +142 -0
  73. package/src/core/annotations.ts +457 -0
  74. package/src/core/banks.ts +133 -0
  75. package/src/core/beam/consolidate.ts +1068 -0
  76. package/src/core/beam/helpers.ts +831 -0
  77. package/src/core/beam/index.ts +358 -0
  78. package/src/core/beam/recall.ts +1173 -0
  79. package/src/core/beam/schema.ts +423 -0
  80. package/src/core/beam/store.ts +936 -0
  81. package/src/core/beam/types.ts +280 -0
  82. package/src/core/binary-vectors.ts +317 -0
  83. package/src/core/chat-normalize.ts +160 -0
  84. package/src/core/content-sanitizer.ts +136 -0
  85. package/src/core/cost-log.ts +103 -0
  86. package/src/core/embeddings.ts +531 -0
  87. package/src/core/entities.ts +263 -0
  88. package/src/core/episodic-graph.ts +708 -0
  89. package/src/core/extraction/client.ts +187 -0
  90. package/src/core/extraction/diagnostics.ts +193 -0
  91. package/src/core/extraction/prompts.ts +31 -0
  92. package/src/core/extraction.ts +338 -0
  93. package/src/core/fastembed-model-cache.ts +39 -0
  94. package/src/core/fastembed-runtime.ts +108 -0
  95. package/src/core/index.ts +38 -0
  96. package/src/core/llm-backends.ts +54 -0
  97. package/src/core/local-llm.ts +474 -0
  98. package/src/core/memory.ts +667 -0
  99. package/src/core/migrations/e6-triplestore-split.ts +211 -0
  100. package/src/core/migrations/index.ts +1 -0
  101. package/src/core/mmr.ts +71 -0
  102. package/src/core/orchestrator.ts +62 -0
  103. package/src/core/patterns.ts +484 -0
  104. package/src/core/plugins.ts +375 -0
  105. package/src/core/polyphonic-recall.ts +563 -0
  106. package/src/core/query-cache.ts +353 -0
  107. package/src/core/query-intent.ts +139 -0
  108. package/src/core/recall-diagnostics.ts +157 -0
  109. package/src/core/runtime-options.ts +130 -0
  110. package/src/core/shmr.ts +560 -0
  111. package/src/core/streaming.ts +419 -0
  112. package/src/core/synonyms.ts +197 -0
  113. package/src/core/temporal-parser.ts +363 -0
  114. package/src/core/token-counter.ts +30 -0
  115. package/src/core/triples.ts +452 -0
  116. package/src/core/typed-memory.ts +407 -0
  117. package/src/core/vector-index.ts +84 -0
  118. package/src/core/vector-math.ts +23 -0
  119. package/src/core/veracity-consolidation.ts +477 -0
  120. package/src/core/weibull.ts +124 -0
  121. package/src/db.ts +128 -0
  122. package/src/diagnose.ts +174 -0
  123. package/src/dr/index.ts +1 -0
  124. package/src/dr/recovery.ts +405 -0
  125. package/src/index.ts +33 -0
  126. package/src/mcp-server.ts +155 -0
  127. package/src/mcp-tools.ts +970 -0
  128. package/src/migrations/e6-triplestore-split.ts +1 -0
  129. package/src/migrations/index.ts +1 -0
  130. package/src/types.ts +157 -0
  131. package/src/util/datetime.ts +69 -0
  132. package/src/util/env.ts +65 -0
  133. package/src/util/ids.ts +19 -0
  134. package/src/util/lru.ts +48 -0
  135. package/src/util/regex.ts +165 -0
@@ -0,0 +1,69 @@
1
+ import type { Database } from "bun:sqlite";
2
+ import { sha256Hex16 } from "../../util/ids";
3
+ import { cjkFtsTerms, containsSpacelessCjk, ftsQueryTerms, recallTokens } from "../../util/regex";
4
+ import type { BeamMemoryState, Metadata } from "./types";
5
+ export type Vector = number[];
6
+ export type HybridWeights = readonly [vecWeight: number, ftsWeight: number, importanceWeight: number];
7
+ export interface VectorDistanceResult {
8
+ rowid: number;
9
+ distance: number;
10
+ }
11
+ export interface WorkingVectorResult {
12
+ id: string;
13
+ sim: number;
14
+ }
15
+ export interface FtsRankResult {
16
+ rowid: number;
17
+ rank: number;
18
+ }
19
+ export interface WorkingFtsRankResult {
20
+ id: string;
21
+ rank: number;
22
+ }
23
+ export declare function generateId(content: string, now?: Date): string;
24
+ export declare function generateStableId(content: string, source?: string): string;
25
+ export declare function normalizeWeights(vecWeight: number | null | undefined, ftsWeight: number | null | undefined, importanceWeight: number | null | undefined): HybridWeights;
26
+ export declare function normalizeImportance(importance: number | null | undefined, fallback?: number): number;
27
+ export declare function normalizeDateUtc(dt: Date): Date;
28
+ export declare function parseIsoDateTimeUtc(value: string): Date;
29
+ export declare function parseQueryTime(queryTime?: string | Date | null): Date;
30
+ export declare function parseTimestampFast(ts: string | null | undefined, beam?: Pick<BeamMemoryState, "caches"> | null): Date | null;
31
+ export declare function recencyDecay(timestamp: string | null | undefined, halflifeHours?: number, now?: Date): number;
32
+ export declare function temporalBoost(memoryTimestamp: string | null | undefined, queryTime: Date | string, halflifeHours?: number, beam?: Pick<BeamMemoryState, "caches"> | null): number;
33
+ export declare function lexicalRelevance(queryTokens: readonly string[], content: string, queryLower?: string): number;
34
+ export declare function strictFactMatches(query: string, factText: string): boolean;
35
+ export declare function buildFtsQuery(query: string): string;
36
+ export declare function cjkLikeSearch(db: Database, query: string, k?: number, working?: boolean): Array<FtsRankResult | WorkingFtsRankResult>;
37
+ export declare function ftsSearch(db: Database, query: string, k?: number): FtsRankResult[];
38
+ export declare function ftsSearchWorking(db: Database, query: string, k?: number): WorkingFtsRankResult[];
39
+ export declare function encodeVector(embedding: readonly number[]): string;
40
+ export declare function decodeVector(value: string | null | undefined): Vector | null;
41
+ export declare function vecAvailable(db: Database): boolean;
42
+ export declare function effectiveVecType(db: Database): "float32" | "int8" | "bit";
43
+ export declare function vecInsert(db: Database, rowid: number, embedding: readonly number[]): void;
44
+ export declare function vecSearch(db: Database, embedding: readonly number[], k?: number): VectorDistanceResult[];
45
+ export declare function inMemoryVecSearch(db: Database, queryEmbedding: readonly number[], k?: number): VectorDistanceResult[];
46
+ export declare function workingMemoryVecSearch(db: Database, queryEmbedding: readonly number[], k?: number, now?: Date): WorkingVectorResult[];
47
+ export declare function normalizeMetadata(input: unknown): Metadata;
48
+ export declare function metadataJson(input: unknown): string;
49
+ export declare function detectLanguage(text: string): string;
50
+ export declare function memoryRowMetadata(row: unknown): Metadata;
51
+ export { cosineSimilarity, hammingDistance, informationTheoreticScore, maximallyInformativeBinarization, quantizeInt8, } from "../binary-vectors";
52
+ export { cjkFtsTerms, containsSpacelessCjk, ftsQueryTerms, recallTokens, sha256Hex16 };
53
+ /** Identifies one freshly stored memory whose embedding still needs to be derived. */
54
+ export interface EmbedItem {
55
+ readonly memoryId: string;
56
+ readonly content: string;
57
+ }
58
+ /**
59
+ * Schedule background embedding generation for one or more freshly stored memories.
60
+ *
61
+ * Mirrors the `scheduleFactExtraction` pattern in `beam/store.ts`: `remember()`,
62
+ * `rememberBatch()`, and `consolidateToEpisodic()` are synchronous, but `embed()` is
63
+ * async (it may hit an HTTP provider), so the task is fired-and-forgotten and tracked
64
+ * on `beam.pendingExtractions` so tests and graceful shutdown can drain it via
65
+ * `flushExtractions()`. The active runtime options (provider, model, API URL/key) are
66
+ * captured here and re-entered inside the task because the `AsyncLocalStorage` scope
67
+ * set by `Mnemopi.#withRuntimeOptions` has already exited by the time the task runs.
68
+ */
69
+ export declare function scheduleEmbedding(beam: BeamMemoryState, items: readonly EmbedItem[]): void;
@@ -0,0 +1,59 @@
1
+ import type { Database } from "bun:sqlite";
2
+ import type { BeamCaches, BeamConfig, BeamEvent, BeamMemoryOptions, BeamMemoryState, BeamStats, ImportStats, MemoriaRetrieveResult, Metadata, RecallEnhancedOptions, RecallOptions, RecallResult, RememberBatchItem, RememberBatchOptions, RememberOptions, SleepResult } from "./types";
3
+ export { initBeam } from "./schema";
4
+ export type * from "./types";
5
+ export declare class BeamMemory implements BeamMemoryState {
6
+ #private;
7
+ readonly db: Database;
8
+ readonly dbPath?: string;
9
+ readonly sessionId: string;
10
+ readonly authorId: string | null;
11
+ readonly authorType: string | null;
12
+ readonly channelId: string;
13
+ readonly useCloud: boolean;
14
+ readonly eventEmitter?: (event: BeamEvent) => void;
15
+ readonly pluginManager: BeamMemoryState["pluginManager"];
16
+ readonly annotations: BeamMemoryState["annotations"];
17
+ readonly triples: BeamMemoryState["triples"];
18
+ readonly episodicGraph: unknown | null;
19
+ readonly veracityConsolidator: unknown | null;
20
+ readonly caches: BeamCaches;
21
+ readonly config: BeamConfig;
22
+ readonly pendingExtractions: Set<Promise<void>>;
23
+ constructor(options?: BeamMemoryOptions);
24
+ constructor(sessionId?: string, dbPath?: string, authorId?: string | null, authorType?: string | null, channelId?: string | null, useCloud?: boolean, eventEmitter?: (event: BeamEvent) => void);
25
+ close(): void;
26
+ flushExtractions(): Promise<void>;
27
+ remember(content: string, options?: RememberOptions): string;
28
+ rememberBatch(items: readonly RememberBatchItem[], options?: RememberBatchOptions): string[];
29
+ getContext(limit?: number): unknown[];
30
+ invalidate(memoryId: string, replacementId?: string | null): boolean;
31
+ getWorkingStats(authorId?: string | null, authorType?: string | null, channelId?: string | null): BeamStats;
32
+ getGlobalWorkingStats(): BeamStats;
33
+ updateWorking(memoryId: string, content?: string | null, importance?: number | null): boolean;
34
+ get(memoryId: string): unknown | null;
35
+ forgetWorking(memoryId: string): boolean;
36
+ consolidateToEpisodic(summary: string, sourceWmIds: readonly string[], source?: string, importance?: number): string;
37
+ detectLanguage(text: string): string;
38
+ extractAndStoreFacts(content: string, messageIdx?: number, sourceMemoryId?: string | null): Record<string, unknown>;
39
+ memoriaRetrieve(query: string, ability?: string | null, topK?: number): MemoriaRetrieveResult;
40
+ recall(query: string, topK?: number, options?: RecallOptions): Promise<RecallResult[]>;
41
+ recallEnhanced(query: string, topK?: number, options?: RecallEnhancedOptions): Promise<RecallResult[]>;
42
+ formatContext(results: readonly RecallResult[], format?: string): string;
43
+ factRecall(query: string, topK?: number): RecallResult[];
44
+ getEpisodicStats(authorId?: string | null, authorType?: string | null, channelId?: string | null): BeamStats;
45
+ getMemoriaStats(): BeamStats;
46
+ scratchpadWrite(content: string): string;
47
+ scratchpadRead(): unknown[];
48
+ scratchpadClear(): void;
49
+ degradeEpisodic(dryRun?: boolean): Record<string, unknown>;
50
+ getContaminated(limit?: number, minImportance?: number): unknown[];
51
+ health(staleThresholdHours?: number): Record<string, unknown>;
52
+ sleep(dryRun?: boolean): SleepResult;
53
+ sleepAllSessions(dryRun?: boolean): SleepResult;
54
+ getConsolidationLog(limit?: number): unknown[];
55
+ exportToDict(): Record<string, unknown>;
56
+ importFromDict(data: Record<string, unknown>, force?: boolean): ImportStats;
57
+ protected emitEvent(type: string, data?: Omit<BeamEvent, "type" | "sessionId" | "timestamp">): void;
58
+ protected metadataJson(metadata: Metadata | null | undefined): string | null;
59
+ }
@@ -0,0 +1,32 @@
1
+ import type { BeamMemoryState, RecallEnhancedOptions, RecallOptions, RecallResult } from "./types";
2
+ type RecallOptionsInternal = RecallOptions & {
3
+ source?: string | null;
4
+ topic?: string | null;
5
+ veracity?: string | null;
6
+ memoryType?: string | null;
7
+ temporalWeight?: number;
8
+ temporalHalflife?: number;
9
+ vecWeight?: number;
10
+ ftsWeight?: number;
11
+ importanceWeight?: number;
12
+ queryEmbedding?: readonly number[] | null;
13
+ useSynonyms?: boolean;
14
+ useIntent?: boolean;
15
+ useMmr?: boolean;
16
+ mmrLambda?: number;
17
+ ignoreSessionScope?: boolean;
18
+ currentSensitive?: boolean;
19
+ updateRecallCounts?: boolean;
20
+ };
21
+ type FactRecallResult = RecallResult & {
22
+ fact_id?: string;
23
+ subject?: string;
24
+ predicate?: string;
25
+ };
26
+ export declare function parseQueryTime(value: RecallOptionsInternal["queryTime"]): Date;
27
+ export declare function temporalBoost(timestamp: unknown, queryTime: Date, halfLifeHours: number): number;
28
+ export declare function recall(beam: BeamMemoryState, query: string, topK?: number, options?: RecallOptionsInternal): Promise<RecallResult[]>;
29
+ export declare function recallEnhanced(beam: BeamMemoryState, query: string, topK?: number, options?: RecallEnhancedOptions & RecallOptionsInternal): Promise<RecallResult[]>;
30
+ export declare function formatContext(beam: BeamMemoryState, results: readonly RecallResult[], format?: string): string;
31
+ export declare function factRecall(beam: BeamMemoryState, query: string, topK?: number): FactRecallResult[];
32
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { Database } from "bun:sqlite";
2
+ export declare function initBeam(db: Database): void;
@@ -0,0 +1,56 @@
1
+ import type { BeamMemoryState, BeamStats, ImportStats, RememberBatchItem, RememberBatchOptions, RememberOptions } from "./types";
2
+ type Row = Record<string, unknown>;
3
+ type StoreRememberOptions = RememberOptions & {
4
+ memoryId?: string;
5
+ memory_id?: string;
6
+ validUntil?: string | null;
7
+ valid_until?: string | null;
8
+ authorId?: string | null;
9
+ author_id?: string | null;
10
+ authorType?: string | null;
11
+ author_type?: string | null;
12
+ extractEntities?: boolean;
13
+ extract_entities?: boolean;
14
+ extract_text?: string;
15
+ channelId?: string | null;
16
+ channel_id?: string | null;
17
+ };
18
+ type StoreRememberBatchOptions = RememberBatchOptions & {
19
+ forceVeracity?: boolean;
20
+ force_veracity?: boolean;
21
+ };
22
+ /**
23
+ * Reconcile stored embeddings against the active embedding model at store open.
24
+ *
25
+ * Every `memory_embeddings` row is stamped with the model that produced it (see
26
+ * `runEmbedding` in `helpers.ts`). When the configured embedding model changes,
27
+ * its vector dimension changes too, so the previously-stored vectors are no
28
+ * longer comparable. On a mismatch we wipe every stored vector — the
29
+ * `memory_embeddings` table, the `episodic_memory.binary_vector` column, and the
30
+ * sqlite-vec `vec_episodes` index — then enqueue all live memories for
31
+ * background re-embedding under the new model via `scheduleEmbedding`.
32
+ *
33
+ * Runs once per store open; a fresh store (no embeddings) or an already-current
34
+ * store is a no-op. The destructive wipe is skipped whenever it could not be
35
+ * rebuilt — embeddings disabled via the runtime option OR the
36
+ * `MNEMOPI_NO_EMBEDDINGS` env, or an unresolved (empty) active model — so a
37
+ * stale-but-valid corpus is never destroyed without a replacement. MUST run
38
+ * inside the active runtime-options scope so `currentEmbeddingModel()` /
39
+ * `embeddingsDisabled()` reflect the per-instance configuration.
40
+ */
41
+ export declare function reconcileEmbeddingModel(beam: BeamMemoryState): void;
42
+ export declare function remember(beam: BeamMemoryState, content: string, options?: StoreRememberOptions): string;
43
+ export declare function rememberBatch(beam: BeamMemoryState, items: readonly RememberBatchItem[], options?: StoreRememberBatchOptions): string[];
44
+ export declare function getContext(beam: BeamMemoryState, limit?: number): Row[];
45
+ export declare function invalidate(beam: BeamMemoryState, memoryId: string, replacementId?: string | null): boolean;
46
+ export declare function getWorkingStats(beam: BeamMemoryState, authorId?: string | null, authorType?: string | null, channelId?: string | null): BeamStats;
47
+ export declare function getGlobalWorkingStats(beam: BeamMemoryState): BeamStats;
48
+ export declare function updateWorking(beam: BeamMemoryState, memoryId: string, content?: string | null, importance?: number | null): boolean;
49
+ export declare function get(beam: BeamMemoryState, memoryId: string): Row | null;
50
+ export declare function forgetWorking(beam: BeamMemoryState, memoryId: string): boolean;
51
+ export declare function scratchpadWrite(beam: BeamMemoryState, content: string): string;
52
+ export declare function scratchpadRead(beam: BeamMemoryState): Row[];
53
+ export declare function scratchpadClear(beam: BeamMemoryState): void;
54
+ export declare function exportToDict(beam: BeamMemoryState): Record<string, unknown>;
55
+ export declare function importFromDict(beam: BeamMemoryState, data: Record<string, unknown>, force?: boolean): ImportStats;
56
+ export {};
@@ -0,0 +1,245 @@
1
+ import type { Database } from "bun:sqlite";
2
+ export type JsonPrimitive = string | number | boolean | null;
3
+ export type JsonValue = JsonPrimitive | JsonValue[] | {
4
+ [key: string]: JsonValue;
5
+ };
6
+ export type Metadata = Record<string, JsonValue>;
7
+ export type MemoryScope = "global" | "session" | "channel" | string;
8
+ export type TrustTier = "STATED" | "OBSERVED" | "INFERRED" | "SYSTEM" | string;
9
+ export type Veracity = "unknown" | "likely_true" | "true" | "false" | "stated" | "inferred" | "tool" | "imported" | "contested" | string;
10
+ export interface BeamPluginManager {
11
+ emit?(event: BeamEvent): void | Promise<void>;
12
+ close?(): void | Promise<void>;
13
+ }
14
+ export interface AnnotationStoreLike {
15
+ add?(memoryId: string, kind: string, value: string, options?: AnnotationWriteOptions): unknown;
16
+ addMany?(memoryId: string, kind: string, values: readonly string[], options?: AnnotationWriteOptions): unknown;
17
+ queryByMemory?(memoryId: string, kind?: string): unknown;
18
+ queryByKind?(kind: string, value?: string): unknown;
19
+ getDistinctValues?(kind: string): string[];
20
+ }
21
+ export interface TripleStoreLike {
22
+ add?(subject: string, predicate: string, object: string, options?: TripleWriteOptions): unknown;
23
+ query?(subject?: string, predicate?: string, asOf?: string): unknown;
24
+ }
25
+ export interface BeamCaches {
26
+ timestampParse: Map<string, Date>;
27
+ polyphonicEngine?: unknown;
28
+ extractionClient?: unknown;
29
+ extractionBuffer: unknown[];
30
+ [key: string]: unknown;
31
+ }
32
+ export interface BeamConfig {
33
+ workingMemoryLimit: number;
34
+ workingMemoryTtlHours: number;
35
+ recencyHalflifeHours: number;
36
+ vecWeight: number;
37
+ ftsWeight: number;
38
+ importanceWeight: number;
39
+ useCloud: boolean;
40
+ localLlmEnabled: boolean;
41
+ maxEpisodeChars: number;
42
+ proactiveLinking?: boolean;
43
+ }
44
+ export interface BeamMemoryOptions {
45
+ sessionId?: string;
46
+ dbPath?: string;
47
+ authorId?: string | null;
48
+ authorType?: string | null;
49
+ channelId?: string | null;
50
+ useCloud?: boolean;
51
+ proactiveLinking?: boolean;
52
+ eventEmitter?: (event: BeamEvent) => void;
53
+ pluginManager?: BeamPluginManager | null;
54
+ annotations?: AnnotationStoreLike | null;
55
+ triples?: TripleStoreLike | null;
56
+ config?: Partial<BeamConfig>;
57
+ }
58
+ export interface BeamMemoryState {
59
+ db: Database;
60
+ dbPath?: string;
61
+ sessionId: string;
62
+ authorId: string | null;
63
+ authorType: string | null;
64
+ channelId: string;
65
+ useCloud: boolean;
66
+ eventEmitter?: (event: BeamEvent) => void;
67
+ pluginManager: BeamPluginManager | null;
68
+ annotations: AnnotationStoreLike | null;
69
+ triples: TripleStoreLike | null;
70
+ episodicGraph: unknown | null;
71
+ veracityConsolidator: unknown | null;
72
+ caches: BeamCaches;
73
+ config: BeamConfig;
74
+ /** Tracks in-flight background fact-extraction tasks scheduled by `remember(..., { extract: true })`. */
75
+ pendingExtractions?: Set<Promise<void>>;
76
+ }
77
+ export interface AnnotationWriteOptions {
78
+ source?: string;
79
+ confidence?: number;
80
+ }
81
+ export interface TripleWriteOptions {
82
+ validFrom?: string;
83
+ source?: string;
84
+ confidence?: number;
85
+ }
86
+ export interface BeamEvent {
87
+ type: string;
88
+ memoryId?: string;
89
+ content?: string;
90
+ source?: string;
91
+ importance?: number;
92
+ sessionId: string;
93
+ timestamp: string;
94
+ metadata?: Metadata;
95
+ }
96
+ export interface RememberOptions {
97
+ source?: string;
98
+ importance?: number;
99
+ metadata?: Metadata | null;
100
+ extract?: boolean;
101
+ extractEntities?: boolean;
102
+ /**
103
+ * Override the text passed to fact and graph extraction. When unset
104
+ * (default) extraction runs over the stored `content`. Use this when the
105
+ * stored memory is a multi-author transcript but only a subset (e.g. the
106
+ * user-authored turns) should drive deterministic fact extraction — see
107
+ * coding-agent issue #3372 where assistant prose was being mis-attributed
108
+ * as user `Instruction:` memories.
109
+ */
110
+ extractText?: string;
111
+ veracity?: Veracity;
112
+ memoryType?: string;
113
+ scope?: MemoryScope;
114
+ trustTier?: TrustTier;
115
+ timestamp?: string;
116
+ }
117
+ export interface RememberBatchOptions {
118
+ extract?: boolean;
119
+ extractEntities?: boolean;
120
+ veracity?: Veracity;
121
+ memoryType?: string;
122
+ scope?: MemoryScope;
123
+ trustTier?: TrustTier;
124
+ }
125
+ export interface RememberBatchItem extends RememberOptions {
126
+ content: string;
127
+ }
128
+ export interface RecallOptions {
129
+ fromDate?: string | null;
130
+ toDate?: string | null;
131
+ authorId?: string | null;
132
+ authorType?: string | null;
133
+ channelId?: string | null;
134
+ includeWorking?: boolean;
135
+ queryTime?: string | Date | null;
136
+ temporalWeight?: number;
137
+ temporalHalflife?: number;
138
+ vecWeight?: number;
139
+ ftsWeight?: number;
140
+ importanceWeight?: number;
141
+ queryEmbedding?: readonly number[] | null;
142
+ useSynonyms?: boolean;
143
+ useIntent?: boolean;
144
+ useMmr?: boolean;
145
+ mmrLambda?: number;
146
+ }
147
+ export interface RecallEnhancedOptions extends RecallOptions {
148
+ useCache?: boolean;
149
+ includeFacts?: boolean;
150
+ }
151
+ export interface MemoryRow {
152
+ id: string;
153
+ content: string;
154
+ source: string | null;
155
+ timestamp: string | null;
156
+ session_id: string;
157
+ importance: number;
158
+ metadata_json: string | null;
159
+ veracity: Veracity;
160
+ memory_type?: string | null;
161
+ recall_count?: number;
162
+ last_recalled?: string | null;
163
+ valid_until?: string | null;
164
+ superseded_by?: string | null;
165
+ scope?: MemoryScope;
166
+ author_id?: string | null;
167
+ author_type?: string | null;
168
+ channel_id?: string | null;
169
+ trust_tier?: TrustTier;
170
+ validator?: string | null;
171
+ validated_at?: string | null;
172
+ validation_count?: number;
173
+ event_date?: string | null;
174
+ event_date_precision?: string | null;
175
+ temporal_tags?: string | null;
176
+ corrected_by?: number | null;
177
+ created_at: string;
178
+ }
179
+ export interface WorkingMemoryRow extends MemoryRow {
180
+ consolidated_at?: string | null;
181
+ }
182
+ export interface EpisodicMemoryRow extends MemoryRow {
183
+ rowid: number;
184
+ summary_of: string;
185
+ tier: number;
186
+ degraded_at?: string | null;
187
+ binary_vector?: Uint8Array | null;
188
+ }
189
+ export type RecallTierLabel = "working" | "episodic" | "fact" | string;
190
+ export interface RecallVoiceScores {
191
+ vec?: number;
192
+ fts?: number;
193
+ keyword?: number;
194
+ importance?: number;
195
+ recency_decay?: number;
196
+ temporal?: number;
197
+ [key: string]: number | undefined;
198
+ }
199
+ type RecallRowFields = Omit<Partial<EpisodicMemoryRow>, "tier"> & Partial<WorkingMemoryRow>;
200
+ export type RecallResult = RecallRowFields & {
201
+ [key: string]: unknown;
202
+ id: string;
203
+ content: string;
204
+ score?: number;
205
+ distance?: number;
206
+ rank?: number;
207
+ tier?: RecallTierLabel;
208
+ tier_label?: RecallTierLabel;
209
+ degradation_tier?: number;
210
+ keyword_score?: number;
211
+ dense_score?: number;
212
+ fts_score?: number;
213
+ importance_score?: number;
214
+ recency_score?: number;
215
+ temporal_score?: number;
216
+ explanation?: string;
217
+ voice_scores?: RecallVoiceScores;
218
+ metadata?: Metadata;
219
+ };
220
+ export interface BeamStats {
221
+ count: number;
222
+ by_source?: Record<string, number>;
223
+ by_session?: Record<string, number>;
224
+ oldest?: string | null;
225
+ newest?: string | null;
226
+ [key: string]: JsonValue | Record<string, number> | undefined;
227
+ }
228
+ export interface MemoriaRetrieveResult {
229
+ ability: string;
230
+ query: string;
231
+ results: unknown[];
232
+ }
233
+ export interface SleepResult {
234
+ dry_run: boolean;
235
+ sessions?: Record<string, unknown>;
236
+ items_consolidated?: number;
237
+ [key: string]: unknown;
238
+ }
239
+ export interface ImportStats {
240
+ working_memory: Record<string, number>;
241
+ episodic_memory: Record<string, number>;
242
+ scratchpad: Record<string, number>;
243
+ consolidation_log: Record<string, number>;
244
+ }
245
+ export {};
@@ -0,0 +1,54 @@
1
+ import type { Database } from "bun:sqlite";
2
+ import { type VecType } from "../config";
3
+ import { type DatabasePath } from "../db";
4
+ export { cosineSimilarity } from "./vector-math";
5
+ export declare const BITS_PER_BYTE = 8;
6
+ export declare const EMBEDDING_DIM: number;
7
+ export declare const BYTES_PER_VECTOR: number;
8
+ export interface BinaryVectorSearchResult {
9
+ memory_id: string;
10
+ distance: number;
11
+ score: number;
12
+ }
13
+ export interface BinaryVectorStats {
14
+ total_vectors: number;
15
+ avg_bytes_per_vector: number;
16
+ max_bytes: number;
17
+ min_bytes: number;
18
+ compression_ratio: number;
19
+ theoretical_size_mb: number;
20
+ }
21
+ export interface BinaryVectorStoreOptions {
22
+ dbPath?: DatabasePath;
23
+ tableName?: string;
24
+ conn?: Database;
25
+ }
26
+ export declare function getVecType(env?: NodeJS.ProcessEnv): VecType;
27
+ export declare const VEC_TYPE: VecType;
28
+ export declare function quantizeInt8(embedding: readonly number[]): Int8Array;
29
+ export declare function maximallyInformativeBinarization(embedding: readonly number[]): Uint8Array;
30
+ export declare function hammingDistance(binaryA: Uint8Array | ArrayBuffer, binaryB: Uint8Array | ArrayBuffer): number;
31
+ export declare function informationTheoreticScore(distance: number, dim?: number): number;
32
+ export declare class BinaryVectorStore {
33
+ readonly conn: Database;
34
+ readonly dbPath: DatabasePath;
35
+ readonly tableName: string;
36
+ private readonly ownsConnection;
37
+ constructor(options?: BinaryVectorStoreOptions);
38
+ private initTable;
39
+ static maximallyInformativeBinarization(embedding: readonly number[]): Uint8Array;
40
+ static hammingDistance(binaryA: Uint8Array | ArrayBuffer, binaryB: Uint8Array | ArrayBuffer): number;
41
+ static informationTheoreticScore(distance: number, dim?: number): number;
42
+ storeVector(memoryId: string, embedding: readonly number[]): void;
43
+ search(queryEmbedding: readonly number[], topK?: number): BinaryVectorSearchResult[];
44
+ searchBatch(queryEmbeddings: readonly (readonly number[])[], topK?: number): BinaryVectorSearchResult[][];
45
+ deleteVector(memoryId: string): void;
46
+ getStats(): BinaryVectorStats;
47
+ close(): void;
48
+ }
49
+ export declare class FastBinarySearch {
50
+ private readonly memoryIds;
51
+ private readonly vectors;
52
+ constructor(binaryVectors: ReadonlyMap<string, Uint8Array | ArrayBuffer> | Record<string, Uint8Array | ArrayBuffer>);
53
+ search(queryBinary: Uint8Array | ArrayBuffer, topK?: number): BinaryVectorSearchResult[];
54
+ }
@@ -0,0 +1,13 @@
1
+ type ExtractionRate = {
2
+ total: number;
3
+ survived: number;
4
+ dropped: number;
5
+ rate: number;
6
+ dropped_samples: string[];
7
+ };
8
+ export declare function normalizeChat(text: string, options?: {
9
+ add_implicit_subjects?: boolean;
10
+ }): string | null;
11
+ export declare function normalizeBatch(messages: string[]): (string | null)[];
12
+ export declare function extractionRate(messages: string[]): ExtractionRate;
13
+ export {};
@@ -0,0 +1,18 @@
1
+ export declare const SIZE_HARD_CAP = 1000000;
2
+ export declare const SIZE_BASE64_CHECK = 100000;
3
+ export declare const ENTROPY_THRESHOLD = 5;
4
+ export interface BlobMetadata {
5
+ blob_ref?: string;
6
+ original_size?: number;
7
+ mime?: string;
8
+ extraction_reason?: "data_uri" | "size_cap" | "high_entropy";
9
+ entropy?: number;
10
+ }
11
+ export declare function blobRoot(env?: NodeJS.ProcessEnv): string;
12
+ export declare function computeSha256(data: Uint8Array | string): string;
13
+ export declare function isDataUri(content: string): boolean;
14
+ export declare function parseDataUri(content: string): [mimeType: string, raw: Buffer] | null;
15
+ export declare function shannonEntropy(text: string): number;
16
+ export declare function looksLikeBase64Blob(content: string): boolean;
17
+ export declare function storeBlob(rawBytes: Uint8Array): string;
18
+ export declare function sanitizeContent(content: string): [sanitizedContent: string, blobMetadata: BlobMetadata];
@@ -0,0 +1,13 @@
1
+ import { Database } from "bun:sqlite";
2
+ export declare const DEFAULT_LOG_DIR: string;
3
+ export declare const DEFAULT_LOG_DB: string;
4
+ export interface CostStats {
5
+ total_calls: number;
6
+ total_memories_injected: number;
7
+ total_tokens: number;
8
+ total_estimated_cost_usd: number;
9
+ }
10
+ export declare function getConn(dbPath?: string): Database;
11
+ export declare function initCostLog(dbPath?: string): void;
12
+ export declare function logCost(sessionId: string, memoryCount: number, tokenCount: number, estimatedCostUsd: number, model?: string, dbPath?: string): void;
13
+ export declare function getCostStats(sessionId?: string, dbPath?: string): CostStats;
@@ -0,0 +1,54 @@
1
+ import type { EmbeddingModel } from "fastembed";
2
+ import { type EmbeddingOutput } from "./runtime-options";
3
+ export type { EmbeddingOutput } from "./runtime-options";
4
+ export { cosineSimilarity } from "./vector-math";
5
+ export type Vector = Float32Array;
6
+ export type EmbeddingMatrix = Vector[];
7
+ export interface EmbeddingProvider {
8
+ embed(texts: readonly string[]): EmbeddingOutput | Promise<EmbeddingOutput>;
9
+ available?(): boolean | Promise<boolean>;
10
+ }
11
+ export type StandardEmbeddingModel = Exclude<EmbeddingModel, EmbeddingModel.CUSTOM>;
12
+ export interface LocalEmbeddingModel {
13
+ embed(texts: string[], batchSize?: number): EmbeddingOutput;
14
+ queryEmbed?(query: string): Promise<number[]>;
15
+ }
16
+ export type LocalModelInitOptions = {
17
+ model: StandardEmbeddingModel;
18
+ cacheDir?: string;
19
+ showDownloadProgress?: boolean;
20
+ };
21
+ export type LocalModelInitializer = (options: LocalModelInitOptions) => Promise<LocalEmbeddingModel>;
22
+ export declare function embeddingsDisabled(): boolean;
23
+ /**
24
+ * Resolve the embedding model name for the currently active runtime scope.
25
+ *
26
+ * Reads (in order): the active provider's `model` from `withMnemopiRuntimeOptions`,
27
+ * the `MNEMOPI_EMBEDDING_MODEL` env var, then the bundled fastembed default. Stored
28
+ * alongside each row in `memory_embeddings.model` so migrations can re-embed when
29
+ * the active model changes.
30
+ */
31
+ export declare function currentEmbeddingModel(): string;
32
+ export declare function isApiModel(modelName: string): boolean;
33
+ export declare function embeddingDimFor(modelName: string): number;
34
+ export declare function setEmbeddingProviderForTests(provider: EmbeddingProvider | null | undefined): void;
35
+ export declare const setEmbeddingProvider: typeof setEmbeddingProviderForTests;
36
+ export declare function setLocalModelInitializerForTests(initializer: LocalModelInitializer | null | undefined): void;
37
+ /**
38
+ * Override the function used to construct the local fastembed model the next
39
+ * time `embed()` is called. Lets a host (e.g. the agent CLI) keep
40
+ * `onnxruntime-node` out of its own address space by routing every fastembed
41
+ * load + inference through a dedicated subprocess. Same wipe semantics as the
42
+ * `*ForTests` form: clears the cached model promise and the query cache so
43
+ * subsequent embeds run through the new initializer immediately.
44
+ */
45
+ export declare const setLocalModelInitializer: typeof setLocalModelInitializerForTests;
46
+ export declare function resetEmbeddingProviderForTests(): void;
47
+ export declare const resetEmbeddingStateForTests: typeof resetEmbeddingProviderForTests;
48
+ export declare function available(): Promise<boolean>;
49
+ export declare function availableApi(): boolean;
50
+ export declare function embedQuery(text: string): Promise<Vector | null>;
51
+ export declare function embed(texts: readonly string[]): Promise<EmbeddingMatrix | null>;
52
+ export declare function getEmbeddingApiCallCountForTests(): number;
53
+ export declare const DEFAULT_MODEL: string;
54
+ export declare const EMBEDDING_DIM: number;
@@ -0,0 +1,9 @@
1
+ export declare const ENTITY_EXTRACTION_STOP_WORDS: ReadonlySet<string>;
2
+ /** Maximum raw text size for regex-only entity extraction. */
3
+ export declare const REGEX_EXTRACTION_MAX_INPUT_CHARS = 50000;
4
+ export declare function levenshteinDistance(s1: string, s2: string): number;
5
+ export declare function similarity(s1: string, s2: string): number;
6
+ export declare function extractEntitiesRegex(text: string): string[];
7
+ export type SimilarEntity = readonly [entity: string, score: number];
8
+ export declare function findSimilarEntities(entity: string, knownEntities: readonly string[], threshold?: number): SimilarEntity[];
9
+ export declare function entityExtractionPerformance(text: string, iterations?: number): number;