@yamo/memory-mesh 2.3.2 → 3.0.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.
Files changed (102) hide show
  1. package/bin/memory_mesh.js +1 -1
  2. package/lib/llm/client.d.ts +111 -0
  3. package/lib/llm/client.js +299 -357
  4. package/lib/llm/client.ts +413 -0
  5. package/lib/llm/index.d.ts +17 -0
  6. package/lib/llm/index.js +15 -8
  7. package/lib/llm/index.ts +19 -0
  8. package/lib/memory/adapters/client.d.ts +183 -0
  9. package/lib/memory/adapters/client.js +518 -0
  10. package/lib/memory/adapters/client.ts +678 -0
  11. package/lib/memory/adapters/config.d.ts +137 -0
  12. package/lib/memory/adapters/config.js +189 -0
  13. package/lib/memory/adapters/config.ts +259 -0
  14. package/lib/memory/adapters/errors.d.ts +76 -0
  15. package/lib/memory/adapters/errors.js +128 -0
  16. package/lib/memory/adapters/errors.ts +166 -0
  17. package/lib/memory/context-manager.d.ts +44 -0
  18. package/lib/memory/context-manager.js +344 -0
  19. package/lib/memory/context-manager.ts +432 -0
  20. package/lib/memory/embeddings/factory.d.ts +59 -0
  21. package/lib/memory/embeddings/factory.js +148 -0
  22. package/lib/{embeddings/factory.js → memory/embeddings/factory.ts} +69 -28
  23. package/lib/memory/embeddings/index.d.ts +2 -0
  24. package/lib/memory/embeddings/index.js +2 -0
  25. package/lib/memory/embeddings/index.ts +2 -0
  26. package/lib/memory/embeddings/service.d.ts +164 -0
  27. package/lib/memory/embeddings/service.js +515 -0
  28. package/lib/{embeddings/service.js → memory/embeddings/service.ts} +223 -156
  29. package/lib/memory/index.d.ts +9 -0
  30. package/lib/memory/index.js +9 -1
  31. package/lib/memory/index.ts +20 -0
  32. package/lib/memory/memory-mesh.d.ts +274 -0
  33. package/lib/memory/memory-mesh.js +1469 -678
  34. package/lib/memory/memory-mesh.ts +1803 -0
  35. package/lib/memory/memory-translator.d.ts +19 -0
  36. package/lib/memory/memory-translator.js +125 -0
  37. package/lib/memory/memory-translator.ts +158 -0
  38. package/lib/memory/schema.d.ts +111 -0
  39. package/lib/memory/schema.js +183 -0
  40. package/lib/memory/schema.ts +267 -0
  41. package/lib/memory/scorer.d.ts +26 -0
  42. package/lib/memory/scorer.js +77 -0
  43. package/lib/memory/scorer.ts +95 -0
  44. package/lib/memory/search/index.d.ts +1 -0
  45. package/lib/memory/search/index.js +1 -0
  46. package/lib/memory/search/index.ts +1 -0
  47. package/lib/memory/search/keyword-search.d.ts +62 -0
  48. package/lib/memory/search/keyword-search.js +135 -0
  49. package/lib/{search/keyword-search.js → memory/search/keyword-search.ts} +66 -36
  50. package/lib/scrubber/config/defaults.d.ts +53 -0
  51. package/lib/scrubber/config/defaults.js +49 -57
  52. package/lib/scrubber/config/defaults.ts +117 -0
  53. package/lib/scrubber/index.d.ts +6 -0
  54. package/lib/scrubber/index.js +3 -23
  55. package/lib/scrubber/index.ts +7 -0
  56. package/lib/scrubber/scrubber.d.ts +61 -0
  57. package/lib/scrubber/scrubber.js +99 -121
  58. package/lib/scrubber/scrubber.ts +168 -0
  59. package/lib/scrubber/stages/chunker.d.ts +13 -0
  60. package/lib/scrubber/stages/metadata-annotator.d.ts +18 -0
  61. package/lib/scrubber/stages/normalizer.d.ts +13 -0
  62. package/lib/scrubber/stages/semantic-filter.d.ts +13 -0
  63. package/lib/scrubber/stages/structural-cleaner.d.ts +13 -0
  64. package/lib/scrubber/stages/validator.d.ts +18 -0
  65. package/lib/scrubber/telemetry.d.ts +36 -0
  66. package/lib/scrubber/telemetry.js +53 -58
  67. package/lib/scrubber/telemetry.ts +99 -0
  68. package/lib/utils/logger.d.ts +29 -0
  69. package/lib/utils/logger.js +64 -0
  70. package/lib/utils/logger.ts +85 -0
  71. package/lib/utils/skill-metadata.d.ts +32 -0
  72. package/lib/utils/skill-metadata.js +132 -0
  73. package/lib/utils/skill-metadata.ts +147 -0
  74. package/lib/yamo/emitter.d.ts +73 -0
  75. package/lib/yamo/emitter.js +78 -143
  76. package/lib/yamo/emitter.ts +249 -0
  77. package/lib/yamo/schema.d.ts +58 -0
  78. package/lib/yamo/schema.js +81 -108
  79. package/lib/yamo/schema.ts +165 -0
  80. package/package.json +11 -8
  81. package/index.d.ts +0 -111
  82. package/lib/embeddings/index.js +0 -2
  83. package/lib/index.js +0 -6
  84. package/lib/lancedb/client.js +0 -633
  85. package/lib/lancedb/config.js +0 -215
  86. package/lib/lancedb/errors.js +0 -144
  87. package/lib/lancedb/index.js +0 -4
  88. package/lib/lancedb/schema.js +0 -217
  89. package/lib/scrubber/errors/scrubber-error.js +0 -43
  90. package/lib/scrubber/stages/chunker.js +0 -103
  91. package/lib/scrubber/stages/metadata-annotator.js +0 -74
  92. package/lib/scrubber/stages/normalizer.js +0 -59
  93. package/lib/scrubber/stages/semantic-filter.js +0 -61
  94. package/lib/scrubber/stages/structural-cleaner.js +0 -82
  95. package/lib/scrubber/stages/validator.js +0 -66
  96. package/lib/scrubber/utils/hash.js +0 -39
  97. package/lib/scrubber/utils/html-parser.js +0 -45
  98. package/lib/scrubber/utils/pattern-matcher.js +0 -63
  99. package/lib/scrubber/utils/token-counter.js +0 -31
  100. package/lib/search/index.js +0 -1
  101. package/lib/utils/index.js +0 -1
  102. package/lib/yamo/index.js +0 -15
@@ -0,0 +1,137 @@
1
+ /**
2
+ * LanceDB Configuration Loader
3
+ * Loads and validates configuration from environment variables
4
+ */
5
+ /**
6
+ * Default configuration values
7
+ */
8
+ export declare const DEFAULTS: {
9
+ readonly LANCEDB_URI: "./runtime/data/lancedb";
10
+ readonly LANCEDB_MEMORY_TABLE: "memory_entries";
11
+ readonly LANCEDB_MAX_CACHE_SIZE: "2GB";
12
+ readonly EMBEDDING_MODEL_TYPE: "local";
13
+ readonly EMBEDDING_MODEL_NAME: "Xenova/all-MiniLM-L6-v2";
14
+ readonly EMBEDDING_DIMENSION: "384";
15
+ readonly EMBEDDING_BATCH_SIZE: "32";
16
+ readonly EMBEDDING_NORMALIZE: "true";
17
+ readonly OPENAI_EMBEDDING_MODEL: "text-embedding-3-small";
18
+ readonly DEFAULT_TOP_K: "10";
19
+ readonly DEFAULT_SIMILARITY_THRESHOLD: "0.7";
20
+ readonly ENABLE_HYBRID_SEARCH: "true";
21
+ readonly HYBRID_SEARCH_ALPHA: "0.5";
22
+ readonly VECTOR_INDEX_TYPE: "ivf_pq";
23
+ readonly IVF_PARTITIONS: "256";
24
+ readonly PQ_BITS: "8";
25
+ readonly ENABLE_QUERY_CACHE: "true";
26
+ readonly QUERY_CACHE_TTL: "300";
27
+ };
28
+ export type ConfigKey = keyof typeof DEFAULTS;
29
+ export type Config = Record<ConfigKey, string>;
30
+ /**
31
+ * Memory system configuration defaults
32
+ */
33
+ export declare const MEMORY_DEFAULTS: {
34
+ readonly MEMORY_ENABLED: "true";
35
+ readonly MEMORY_AUTO_CAPTURE: "true";
36
+ readonly MEMORY_AUTO_RECALL: "true";
37
+ readonly MEMORY_MAX_CONTEXT: "5";
38
+ readonly MEMORY_RELEVANCE_THRESHOLD: "0.7";
39
+ readonly MEMORY_IMPORTANCE_BOOST: "1.5";
40
+ readonly MEMORY_RECENCY_WEIGHT: "0.3";
41
+ readonly MEMORY_MIN_IMPORTANCE: "0.3";
42
+ readonly MEMORY_DEDUP_THRESHOLD: "0.9";
43
+ readonly MEMORY_CAPTURE_TOOL_RESULTS: "true";
44
+ readonly MEMORY_CAPTURE_FILE_OPS: "true";
45
+ readonly MEMORY_RETENTION_ENABLED: "true";
46
+ readonly MEMORY_RETENTION_DAYS: "90";
47
+ readonly MEMORY_MAX_PER_SESSION: "100";
48
+ readonly MEMORY_MIN_IMPORTANCE_TO_KEEP: "0.5";
49
+ readonly MEMORY_REDACT_PII: "false";
50
+ readonly MEMORY_ENCRYPTION_ENABLED: "false";
51
+ };
52
+ export interface MemoryConfig {
53
+ enabled: boolean;
54
+ autoCapture: boolean;
55
+ autoRecall: boolean;
56
+ maxContext: number;
57
+ relevanceThreshold: number;
58
+ importanceBoost: number;
59
+ recencyWeight: number;
60
+ minImportance: number;
61
+ dedupThreshold: number;
62
+ captureToolResults: boolean;
63
+ captureFileOps: boolean;
64
+ retention: {
65
+ enabled: boolean;
66
+ days: number;
67
+ maxPerSession: number;
68
+ minImportanceToKeep: number;
69
+ };
70
+ privacy: {
71
+ redactPii: boolean;
72
+ encryptionEnabled: boolean;
73
+ };
74
+ }
75
+ /**
76
+ * Load configuration with validation
77
+ */
78
+ export declare function loadConfig(): Config;
79
+ /**
80
+ * Load memory-specific configuration
81
+ * @returns {Object} Memory configuration object
82
+ */
83
+ export declare function loadMemoryConfig(): MemoryConfig;
84
+ /**
85
+ * Validate configuration
86
+ */
87
+ export declare function validateConfig(config: Config): string[];
88
+ /**
89
+ * Get validated configuration
90
+ */
91
+ export declare function getConfig(): Config;
92
+ declare const _default: {
93
+ loadConfig: typeof loadConfig;
94
+ validateConfig: typeof validateConfig;
95
+ getConfig: typeof getConfig;
96
+ loadMemoryConfig: typeof loadMemoryConfig;
97
+ DEFAULTS: {
98
+ readonly LANCEDB_URI: "./runtime/data/lancedb";
99
+ readonly LANCEDB_MEMORY_TABLE: "memory_entries";
100
+ readonly LANCEDB_MAX_CACHE_SIZE: "2GB";
101
+ readonly EMBEDDING_MODEL_TYPE: "local";
102
+ readonly EMBEDDING_MODEL_NAME: "Xenova/all-MiniLM-L6-v2";
103
+ readonly EMBEDDING_DIMENSION: "384";
104
+ readonly EMBEDDING_BATCH_SIZE: "32";
105
+ readonly EMBEDDING_NORMALIZE: "true";
106
+ readonly OPENAI_EMBEDDING_MODEL: "text-embedding-3-small";
107
+ readonly DEFAULT_TOP_K: "10";
108
+ readonly DEFAULT_SIMILARITY_THRESHOLD: "0.7";
109
+ readonly ENABLE_HYBRID_SEARCH: "true";
110
+ readonly HYBRID_SEARCH_ALPHA: "0.5";
111
+ readonly VECTOR_INDEX_TYPE: "ivf_pq";
112
+ readonly IVF_PARTITIONS: "256";
113
+ readonly PQ_BITS: "8";
114
+ readonly ENABLE_QUERY_CACHE: "true";
115
+ readonly QUERY_CACHE_TTL: "300";
116
+ };
117
+ MEMORY_DEFAULTS: {
118
+ readonly MEMORY_ENABLED: "true";
119
+ readonly MEMORY_AUTO_CAPTURE: "true";
120
+ readonly MEMORY_AUTO_RECALL: "true";
121
+ readonly MEMORY_MAX_CONTEXT: "5";
122
+ readonly MEMORY_RELEVANCE_THRESHOLD: "0.7";
123
+ readonly MEMORY_IMPORTANCE_BOOST: "1.5";
124
+ readonly MEMORY_RECENCY_WEIGHT: "0.3";
125
+ readonly MEMORY_MIN_IMPORTANCE: "0.3";
126
+ readonly MEMORY_DEDUP_THRESHOLD: "0.9";
127
+ readonly MEMORY_CAPTURE_TOOL_RESULTS: "true";
128
+ readonly MEMORY_CAPTURE_FILE_OPS: "true";
129
+ readonly MEMORY_RETENTION_ENABLED: "true";
130
+ readonly MEMORY_RETENTION_DAYS: "90";
131
+ readonly MEMORY_MAX_PER_SESSION: "100";
132
+ readonly MEMORY_MIN_IMPORTANCE_TO_KEEP: "0.5";
133
+ readonly MEMORY_REDACT_PII: "false";
134
+ readonly MEMORY_ENCRYPTION_ENABLED: "false";
135
+ };
136
+ };
137
+ export default _default;
@@ -0,0 +1,189 @@
1
+ /**
2
+ * LanceDB Configuration Loader
3
+ * Loads and validates configuration from environment variables
4
+ */
5
+ import path from "path";
6
+ /**
7
+ * Default configuration values
8
+ */
9
+ export const DEFAULTS = {
10
+ // LanceDB Configuration
11
+ LANCEDB_URI: "./runtime/data/lancedb",
12
+ LANCEDB_MEMORY_TABLE: "memory_entries",
13
+ LANCEDB_MAX_CACHE_SIZE: "2GB",
14
+ // Embedding Model Configuration
15
+ EMBEDDING_MODEL_TYPE: "local",
16
+ EMBEDDING_MODEL_NAME: "Xenova/all-MiniLM-L6-v2",
17
+ EMBEDDING_DIMENSION: "384",
18
+ EMBEDDING_BATCH_SIZE: "32",
19
+ EMBEDDING_NORMALIZE: "true",
20
+ // API-based Embeddings
21
+ OPENAI_EMBEDDING_MODEL: "text-embedding-3-small",
22
+ // Search Configuration
23
+ DEFAULT_TOP_K: "10",
24
+ DEFAULT_SIMILARITY_THRESHOLD: "0.7",
25
+ ENABLE_HYBRID_SEARCH: "true",
26
+ HYBRID_SEARCH_ALPHA: "0.5",
27
+ // Performance Tuning
28
+ VECTOR_INDEX_TYPE: "ivf_pq",
29
+ IVF_PARTITIONS: "256",
30
+ PQ_BITS: "8",
31
+ ENABLE_QUERY_CACHE: "true",
32
+ QUERY_CACHE_TTL: "300",
33
+ };
34
+ /**
35
+ * Memory system configuration defaults
36
+ */
37
+ export const MEMORY_DEFAULTS = {
38
+ // Feature flags
39
+ MEMORY_ENABLED: "true",
40
+ MEMORY_AUTO_CAPTURE: "true",
41
+ MEMORY_AUTO_RECALL: "true",
42
+ // Recall settings
43
+ MEMORY_MAX_CONTEXT: "5",
44
+ MEMORY_RELEVANCE_THRESHOLD: "0.7",
45
+ MEMORY_IMPORTANCE_BOOST: "1.5",
46
+ MEMORY_RECENCY_WEIGHT: "0.3",
47
+ // Capture settings
48
+ MEMORY_MIN_IMPORTANCE: "0.3",
49
+ MEMORY_DEDUP_THRESHOLD: "0.9",
50
+ MEMORY_CAPTURE_TOOL_RESULTS: "true",
51
+ MEMORY_CAPTURE_FILE_OPS: "true",
52
+ // Retention settings
53
+ MEMORY_RETENTION_ENABLED: "true",
54
+ MEMORY_RETENTION_DAYS: "90",
55
+ MEMORY_MAX_PER_SESSION: "100",
56
+ MEMORY_MIN_IMPORTANCE_TO_KEEP: "0.5",
57
+ // Privacy settings
58
+ MEMORY_REDACT_PII: "false",
59
+ MEMORY_ENCRYPTION_ENABLED: "false",
60
+ };
61
+ /**
62
+ * Load configuration with validation
63
+ */
64
+ export function loadConfig() {
65
+ const config = {};
66
+ for (const [key, defaultValue] of Object.entries(DEFAULTS)) {
67
+ config[key] = process.env[key] || defaultValue;
68
+ }
69
+ // Resolve relative paths to absolute relative to package root
70
+ if (config.LANCEDB_URI &&
71
+ (config.LANCEDB_URI.startsWith("./") ||
72
+ config.LANCEDB_URI.startsWith("../"))) {
73
+ const currentFileUrl = import.meta.url;
74
+ const currentFilePath = new URL(currentFileUrl).pathname;
75
+ // config.ts is in lib/brain/adapters, so package root is ../../../
76
+ const packageRoot = path.resolve(path.dirname(currentFilePath), "../../../");
77
+ config.LANCEDB_URI = path.resolve(packageRoot, config.LANCEDB_URI);
78
+ }
79
+ return config;
80
+ }
81
+ /**
82
+ * Load memory-specific configuration
83
+ * @returns {Object} Memory configuration object
84
+ */
85
+ export function loadMemoryConfig() {
86
+ return {
87
+ enabled: process.env.MEMORY_ENABLED !== "false",
88
+ autoCapture: process.env.MEMORY_AUTO_CAPTURE !== "false",
89
+ autoRecall: process.env.MEMORY_AUTO_RECALL !== "false",
90
+ maxContext: parseInt(process.env.MEMORY_MAX_CONTEXT || "5"),
91
+ relevanceThreshold: parseFloat(process.env.MEMORY_RELEVANCE_THRESHOLD || "0.7"),
92
+ importanceBoost: parseFloat(process.env.MEMORY_IMPORTANCE_BOOST || "1.5"),
93
+ recencyWeight: parseFloat(process.env.MEMORY_RECENCY_WEIGHT || "0.3"),
94
+ minImportance: parseFloat(process.env.MEMORY_MIN_IMPORTANCE || "0.3"),
95
+ dedupThreshold: parseFloat(process.env.MEMORY_DEDUP_THRESHOLD || "0.9"),
96
+ captureToolResults: process.env.MEMORY_CAPTURE_TOOL_RESULTS !== "false",
97
+ captureFileOps: process.env.MEMORY_CAPTURE_FILE_OPS !== "false",
98
+ retention: {
99
+ enabled: process.env.MEMORY_RETENTION_ENABLED !== "false",
100
+ days: parseInt(process.env.MEMORY_RETENTION_DAYS || "90"),
101
+ maxPerSession: parseInt(process.env.MEMORY_MAX_PER_SESSION || "100"),
102
+ minImportanceToKeep: parseFloat(process.env.MEMORY_MIN_IMPORTANCE_TO_KEEP || "0.5"),
103
+ },
104
+ privacy: {
105
+ redactPii: process.env.MEMORY_REDACT_PII === "true",
106
+ encryptionEnabled: process.env.MEMORY_ENCRYPTION_ENABLED === "true",
107
+ },
108
+ };
109
+ }
110
+ /**
111
+ * Validate configuration
112
+ */
113
+ export function validateConfig(config) {
114
+ const errors = [];
115
+ // Validate embedding model type
116
+ const validModelTypes = ["local", "openai", "cohere", "voyage"];
117
+ if (!validModelTypes.includes(config.EMBEDDING_MODEL_TYPE)) {
118
+ errors.push(`Invalid EMBEDDING_MODEL_TYPE: ${config.EMBEDDING_MODEL_TYPE}`);
119
+ }
120
+ // Validate numeric values
121
+ const dimension = parseInt(config.EMBEDDING_DIMENSION);
122
+ if (isNaN(dimension) || dimension <= 0) {
123
+ errors.push(`Invalid EMBEDDING_DIMENSION: ${config.EMBEDDING_DIMENSION}`);
124
+ }
125
+ const topK = parseInt(config.DEFAULT_TOP_K);
126
+ if (isNaN(topK) || topK <= 0) {
127
+ errors.push(`Invalid DEFAULT_TOP_K: ${config.DEFAULT_TOP_K}`);
128
+ }
129
+ // Validate boolean strings
130
+ const boolFields = [
131
+ "EMBEDDING_NORMALIZE",
132
+ "ENABLE_HYBRID_SEARCH",
133
+ "ENABLE_QUERY_CACHE",
134
+ ];
135
+ for (const field of boolFields) {
136
+ const value = config[field].toLowerCase();
137
+ if (value !== "true" && value !== "false") {
138
+ errors.push(`Invalid ${field}: must be 'true' or 'false'`);
139
+ }
140
+ }
141
+ // Validate similarity threshold (0-1 range)
142
+ const threshold = parseFloat(config.DEFAULT_SIMILARITY_THRESHOLD);
143
+ if (isNaN(threshold) || threshold < 0 || threshold > 1) {
144
+ errors.push(`Invalid DEFAULT_SIMILARITY_THRESHOLD: must be between 0 and 1`);
145
+ }
146
+ // Validate hybrid search alpha (0-1 range)
147
+ const alpha = parseFloat(config.HYBRID_SEARCH_ALPHA);
148
+ if (isNaN(alpha) || alpha < 0 || alpha > 1) {
149
+ errors.push(`Invalid HYBRID_SEARCH_ALPHA: must be between 0 and 1`);
150
+ }
151
+ // Validate positive integers
152
+ const positiveIntFields = [
153
+ "EMBEDDING_BATCH_SIZE",
154
+ "IVF_PARTITIONS",
155
+ "PQ_BITS",
156
+ "QUERY_CACHE_TTL",
157
+ ];
158
+ for (const field of positiveIntFields) {
159
+ const value = parseInt(config[field]);
160
+ if (isNaN(value) || value <= 0) {
161
+ errors.push(`Invalid ${field}: must be a positive integer`);
162
+ }
163
+ }
164
+ // Validate cache size format (e.g., "2GB", "500MB")
165
+ const cacheSizePattern = /^\d+(\.\d+)?(KB|MB|GB|TB)$/;
166
+ if (!cacheSizePattern.test(config.LANCEDB_MAX_CACHE_SIZE)) {
167
+ errors.push(`Invalid LANCEDB_MAX_CACHE_SIZE: must match pattern like "2GB", "500MB"`);
168
+ }
169
+ return errors;
170
+ }
171
+ /**
172
+ * Get validated configuration
173
+ */
174
+ export function getConfig() {
175
+ const config = loadConfig();
176
+ const errors = validateConfig(config);
177
+ if (errors.length > 0) {
178
+ throw new Error(`Configuration validation failed:\n${errors.join("\n")}`);
179
+ }
180
+ return config;
181
+ }
182
+ export default {
183
+ loadConfig,
184
+ validateConfig,
185
+ getConfig,
186
+ loadMemoryConfig,
187
+ DEFAULTS,
188
+ MEMORY_DEFAULTS,
189
+ };
@@ -0,0 +1,259 @@
1
+ /**
2
+ * LanceDB Configuration Loader
3
+ * Loads and validates configuration from environment variables
4
+ */
5
+
6
+ import path from "path";
7
+
8
+ /**
9
+ * Default configuration values
10
+ */
11
+ export const DEFAULTS = {
12
+ // LanceDB Configuration
13
+ LANCEDB_URI: "./runtime/data/lancedb",
14
+ LANCEDB_MEMORY_TABLE: "memory_entries",
15
+ LANCEDB_MAX_CACHE_SIZE: "2GB",
16
+
17
+ // Embedding Model Configuration
18
+ EMBEDDING_MODEL_TYPE: "local",
19
+ EMBEDDING_MODEL_NAME: "Xenova/all-MiniLM-L6-v2",
20
+ EMBEDDING_DIMENSION: "384",
21
+ EMBEDDING_BATCH_SIZE: "32",
22
+ EMBEDDING_NORMALIZE: "true",
23
+
24
+ // API-based Embeddings
25
+ OPENAI_EMBEDDING_MODEL: "text-embedding-3-small",
26
+
27
+ // Search Configuration
28
+ DEFAULT_TOP_K: "10",
29
+ DEFAULT_SIMILARITY_THRESHOLD: "0.7",
30
+ ENABLE_HYBRID_SEARCH: "true",
31
+ HYBRID_SEARCH_ALPHA: "0.5",
32
+
33
+ // Performance Tuning
34
+ VECTOR_INDEX_TYPE: "ivf_pq",
35
+ IVF_PARTITIONS: "256",
36
+ PQ_BITS: "8",
37
+ ENABLE_QUERY_CACHE: "true",
38
+ QUERY_CACHE_TTL: "300",
39
+ } as const;
40
+
41
+ export type ConfigKey = keyof typeof DEFAULTS;
42
+ export type Config = Record<ConfigKey, string>;
43
+
44
+ /**
45
+ * Memory system configuration defaults
46
+ */
47
+ export const MEMORY_DEFAULTS = {
48
+ // Feature flags
49
+ MEMORY_ENABLED: "true",
50
+ MEMORY_AUTO_CAPTURE: "true",
51
+ MEMORY_AUTO_RECALL: "true",
52
+
53
+ // Recall settings
54
+ MEMORY_MAX_CONTEXT: "5",
55
+ MEMORY_RELEVANCE_THRESHOLD: "0.7",
56
+ MEMORY_IMPORTANCE_BOOST: "1.5",
57
+ MEMORY_RECENCY_WEIGHT: "0.3",
58
+
59
+ // Capture settings
60
+ MEMORY_MIN_IMPORTANCE: "0.3",
61
+ MEMORY_DEDUP_THRESHOLD: "0.9",
62
+ MEMORY_CAPTURE_TOOL_RESULTS: "true",
63
+ MEMORY_CAPTURE_FILE_OPS: "true",
64
+
65
+ // Retention settings
66
+ MEMORY_RETENTION_ENABLED: "true",
67
+ MEMORY_RETENTION_DAYS: "90",
68
+ MEMORY_MAX_PER_SESSION: "100",
69
+ MEMORY_MIN_IMPORTANCE_TO_KEEP: "0.5",
70
+
71
+ // Privacy settings
72
+ MEMORY_REDACT_PII: "false",
73
+ MEMORY_ENCRYPTION_ENABLED: "false",
74
+ } as const;
75
+
76
+ export interface MemoryConfig {
77
+ enabled: boolean;
78
+ autoCapture: boolean;
79
+ autoRecall: boolean;
80
+ maxContext: number;
81
+ relevanceThreshold: number;
82
+ importanceBoost: number;
83
+ recencyWeight: number;
84
+ minImportance: number;
85
+ dedupThreshold: number;
86
+ captureToolResults: boolean;
87
+ captureFileOps: boolean;
88
+ retention: {
89
+ enabled: boolean;
90
+ days: number;
91
+ maxPerSession: number;
92
+ minImportanceToKeep: number;
93
+ };
94
+ privacy: {
95
+ redactPii: boolean;
96
+ encryptionEnabled: boolean;
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Load configuration with validation
102
+ */
103
+ export function loadConfig(): Config {
104
+ const config: Partial<Config> = {};
105
+
106
+ for (const [key, defaultValue] of Object.entries(DEFAULTS)) {
107
+ config[key as ConfigKey] = process.env[key] || defaultValue;
108
+ }
109
+
110
+ // Resolve relative paths to absolute relative to package root
111
+ if (
112
+ config.LANCEDB_URI &&
113
+ (config.LANCEDB_URI.startsWith("./") ||
114
+ config.LANCEDB_URI.startsWith("../"))
115
+ ) {
116
+ const currentFileUrl = import.meta.url;
117
+ const currentFilePath = new URL(currentFileUrl).pathname;
118
+ // config.ts is in lib/brain/adapters, so package root is ../../../
119
+ const packageRoot = path.resolve(
120
+ path.dirname(currentFilePath),
121
+ "../../../",
122
+ );
123
+ config.LANCEDB_URI = path.resolve(packageRoot, config.LANCEDB_URI);
124
+ }
125
+
126
+ return config as Config;
127
+ }
128
+
129
+ /**
130
+ * Load memory-specific configuration
131
+ * @returns {Object} Memory configuration object
132
+ */
133
+ export function loadMemoryConfig(): MemoryConfig {
134
+ return {
135
+ enabled: process.env.MEMORY_ENABLED !== "false",
136
+ autoCapture: process.env.MEMORY_AUTO_CAPTURE !== "false",
137
+ autoRecall: process.env.MEMORY_AUTO_RECALL !== "false",
138
+ maxContext: parseInt(process.env.MEMORY_MAX_CONTEXT || "5"),
139
+ relevanceThreshold: parseFloat(
140
+ process.env.MEMORY_RELEVANCE_THRESHOLD || "0.7",
141
+ ),
142
+ importanceBoost: parseFloat(process.env.MEMORY_IMPORTANCE_BOOST || "1.5"),
143
+ recencyWeight: parseFloat(process.env.MEMORY_RECENCY_WEIGHT || "0.3"),
144
+ minImportance: parseFloat(process.env.MEMORY_MIN_IMPORTANCE || "0.3"),
145
+ dedupThreshold: parseFloat(process.env.MEMORY_DEDUP_THRESHOLD || "0.9"),
146
+ captureToolResults: process.env.MEMORY_CAPTURE_TOOL_RESULTS !== "false",
147
+ captureFileOps: process.env.MEMORY_CAPTURE_FILE_OPS !== "false",
148
+ retention: {
149
+ enabled: process.env.MEMORY_RETENTION_ENABLED !== "false",
150
+ days: parseInt(process.env.MEMORY_RETENTION_DAYS || "90"),
151
+ maxPerSession: parseInt(process.env.MEMORY_MAX_PER_SESSION || "100"),
152
+ minImportanceToKeep: parseFloat(
153
+ process.env.MEMORY_MIN_IMPORTANCE_TO_KEEP || "0.5",
154
+ ),
155
+ },
156
+ privacy: {
157
+ redactPii: process.env.MEMORY_REDACT_PII === "true",
158
+ encryptionEnabled: process.env.MEMORY_ENCRYPTION_ENABLED === "true",
159
+ },
160
+ };
161
+ }
162
+
163
+ /**
164
+ * Validate configuration
165
+ */
166
+ export function validateConfig(config: Config): string[] {
167
+ const errors: string[] = [];
168
+
169
+ // Validate embedding model type
170
+ const validModelTypes = ["local", "openai", "cohere", "voyage"];
171
+ if (!validModelTypes.includes(config.EMBEDDING_MODEL_TYPE)) {
172
+ errors.push(`Invalid EMBEDDING_MODEL_TYPE: ${config.EMBEDDING_MODEL_TYPE}`);
173
+ }
174
+
175
+ // Validate numeric values
176
+ const dimension = parseInt(config.EMBEDDING_DIMENSION);
177
+ if (isNaN(dimension) || dimension <= 0) {
178
+ errors.push(`Invalid EMBEDDING_DIMENSION: ${config.EMBEDDING_DIMENSION}`);
179
+ }
180
+
181
+ const topK = parseInt(config.DEFAULT_TOP_K);
182
+ if (isNaN(topK) || topK <= 0) {
183
+ errors.push(`Invalid DEFAULT_TOP_K: ${config.DEFAULT_TOP_K}`);
184
+ }
185
+
186
+ // Validate boolean strings
187
+ const boolFields: (keyof Config)[] = [
188
+ "EMBEDDING_NORMALIZE",
189
+ "ENABLE_HYBRID_SEARCH",
190
+ "ENABLE_QUERY_CACHE",
191
+ ];
192
+ for (const field of boolFields) {
193
+ const value = config[field].toLowerCase();
194
+ if (value !== "true" && value !== "false") {
195
+ errors.push(`Invalid ${field}: must be 'true' or 'false'`);
196
+ }
197
+ }
198
+
199
+ // Validate similarity threshold (0-1 range)
200
+ const threshold = parseFloat(config.DEFAULT_SIMILARITY_THRESHOLD);
201
+ if (isNaN(threshold) || threshold < 0 || threshold > 1) {
202
+ errors.push(
203
+ `Invalid DEFAULT_SIMILARITY_THRESHOLD: must be between 0 and 1`,
204
+ );
205
+ }
206
+
207
+ // Validate hybrid search alpha (0-1 range)
208
+ const alpha = parseFloat(config.HYBRID_SEARCH_ALPHA);
209
+ if (isNaN(alpha) || alpha < 0 || alpha > 1) {
210
+ errors.push(`Invalid HYBRID_SEARCH_ALPHA: must be between 0 and 1`);
211
+ }
212
+
213
+ // Validate positive integers
214
+ const positiveIntFields: (keyof Config)[] = [
215
+ "EMBEDDING_BATCH_SIZE",
216
+ "IVF_PARTITIONS",
217
+ "PQ_BITS",
218
+ "QUERY_CACHE_TTL",
219
+ ];
220
+ for (const field of positiveIntFields) {
221
+ const value = parseInt(config[field]);
222
+ if (isNaN(value) || value <= 0) {
223
+ errors.push(`Invalid ${field}: must be a positive integer`);
224
+ }
225
+ }
226
+
227
+ // Validate cache size format (e.g., "2GB", "500MB")
228
+ const cacheSizePattern = /^\d+(\.\d+)?(KB|MB|GB|TB)$/;
229
+ if (!cacheSizePattern.test(config.LANCEDB_MAX_CACHE_SIZE)) {
230
+ errors.push(
231
+ `Invalid LANCEDB_MAX_CACHE_SIZE: must match pattern like "2GB", "500MB"`,
232
+ );
233
+ }
234
+
235
+ return errors;
236
+ }
237
+
238
+ /**
239
+ * Get validated configuration
240
+ */
241
+ export function getConfig(): Config {
242
+ const config = loadConfig();
243
+ const errors = validateConfig(config);
244
+
245
+ if (errors.length > 0) {
246
+ throw new Error(`Configuration validation failed:\n${errors.join("\n")}`);
247
+ }
248
+
249
+ return config;
250
+ }
251
+
252
+ export default {
253
+ loadConfig,
254
+ validateConfig,
255
+ getConfig,
256
+ loadMemoryConfig,
257
+ DEFAULTS,
258
+ MEMORY_DEFAULTS,
259
+ };
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Custom error classes for LanceDB operations
3
+ *
4
+ * Base error class for all LanceDB-related errors. Captures proper stack traces
5
+ * to ensure debugging information points to where errors are thrown, not to the
6
+ * error constructor.
7
+ */
8
+ export declare class LanceDBError extends Error {
9
+ code: string;
10
+ details: Record<string, any>;
11
+ timestamp: string;
12
+ /**
13
+ * Create a new LanceDBError
14
+ * @param {string} message - Human-readable error message
15
+ * @param {string} code - Machine-readable error code (e.g., 'EMBEDDING_ERROR')
16
+ * @param {Object} details - Additional error context and metadata
17
+ */
18
+ constructor(message: string, code: string, details?: Record<string, any>);
19
+ }
20
+ /**
21
+ * Error raised when embedding generation or comparison fails
22
+ */
23
+ export declare class EmbeddingError extends LanceDBError {
24
+ constructor(message: string, details?: Record<string, any>);
25
+ }
26
+ /**
27
+ * Error raised when storage operations (read/write/delete) fail
28
+ */
29
+ export declare class StorageError extends LanceDBError {
30
+ constructor(message: string, details?: Record<string, any>);
31
+ }
32
+ /**
33
+ * Error raised when database queries fail or return invalid results
34
+ */
35
+ export declare class QueryError extends LanceDBError {
36
+ constructor(message: string, details?: Record<string, any>);
37
+ }
38
+ /**
39
+ * Error raised when configuration is missing or invalid
40
+ */
41
+ export declare class ConfigurationError extends LanceDBError {
42
+ constructor(message: string, details?: Record<string, any>);
43
+ }
44
+ /**
45
+ * Sanitize error messages by redacting sensitive information
46
+ * @param {string} message - Error message to sanitize
47
+ * @returns {string} Sanitized error message
48
+ */
49
+ export declare function sanitizeErrorMessage(message: unknown): string;
50
+ export interface ErrorResponse {
51
+ success: boolean;
52
+ error: {
53
+ code: string;
54
+ message: string;
55
+ details?: Record<string, any>;
56
+ context?: Record<string, any>;
57
+ stack?: string;
58
+ };
59
+ }
60
+ /**
61
+ * Normalize errors into a consistent response format
62
+ * @param {Error} error - The error to handle
63
+ * @param {Object} context - Additional context about where/when the error occurred
64
+ * @returns {Object} Formatted error response with success: false
65
+ */
66
+ export declare function handleError(error: unknown, context?: Record<string, any>): ErrorResponse;
67
+ declare const _default: {
68
+ LanceDBError: typeof LanceDBError;
69
+ EmbeddingError: typeof EmbeddingError;
70
+ StorageError: typeof StorageError;
71
+ QueryError: typeof QueryError;
72
+ ConfigurationError: typeof ConfigurationError;
73
+ handleError: typeof handleError;
74
+ sanitizeErrorMessage: typeof sanitizeErrorMessage;
75
+ };
76
+ export default _default;