@kodelyth/memory-lancedb 2026.5.39 → 2026.5.42
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/api.ts +2 -0
- package/cli-metadata.ts +18 -0
- package/config.test.ts +178 -0
- package/config.ts +283 -0
- package/dist/api.js +3 -0
- package/dist/cli-metadata.js +16 -0
- package/dist/config.js +202 -0
- package/dist/index.js +730 -0
- package/dist/lancedb-runtime.js +46 -0
- package/dist/test-helpers.js +3205 -0
- package/index.test.ts +2370 -0
- package/index.ts +1158 -0
- package/klaw.plugin.json +4 -13
- package/lancedb-runtime.ts +77 -0
- package/memory-lancedb.live.test.ts +121 -0
- package/package.json +2 -2
- package/test-helpers.ts +25 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/cli-metadata.js +0 -7
- package/config.js +0 -7
- package/index.js +0 -7
- package/lancedb-runtime.js +0 -7
- package/test-helpers.js +0 -7
package/dist/config.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
//#region extensions/memory-lancedb/config.ts
|
|
5
|
+
const MEMORY_CATEGORIES = [
|
|
6
|
+
"preference",
|
|
7
|
+
"fact",
|
|
8
|
+
"decision",
|
|
9
|
+
"entity",
|
|
10
|
+
"other"
|
|
11
|
+
];
|
|
12
|
+
const DEFAULT_MODEL = "text-embedding-3-small";
|
|
13
|
+
const DEFAULT_CAPTURE_MAX_CHARS = 500;
|
|
14
|
+
const DEFAULT_RECALL_MAX_CHARS = 1e3;
|
|
15
|
+
const LEGACY_STATE_DIRS = [];
|
|
16
|
+
function resolveDefaultDbPath() {
|
|
17
|
+
const home = homedir();
|
|
18
|
+
const preferred = join(home, ".klaw", "memory", "lancedb");
|
|
19
|
+
try {
|
|
20
|
+
if (fs.existsSync(preferred)) return preferred;
|
|
21
|
+
} catch {}
|
|
22
|
+
for (const legacy of LEGACY_STATE_DIRS) {
|
|
23
|
+
const candidate = join(home, legacy, "memory", "lancedb");
|
|
24
|
+
try {
|
|
25
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
26
|
+
} catch {}
|
|
27
|
+
}
|
|
28
|
+
return preferred;
|
|
29
|
+
}
|
|
30
|
+
const DEFAULT_DB_PATH = resolveDefaultDbPath();
|
|
31
|
+
const EMBEDDING_DIMENSIONS = {
|
|
32
|
+
"text-embedding-3-small": 1536,
|
|
33
|
+
"text-embedding-3-large": 3072
|
|
34
|
+
};
|
|
35
|
+
const EMBEDDING_CONFIG_KEYS = [
|
|
36
|
+
"provider",
|
|
37
|
+
"apiKey",
|
|
38
|
+
"model",
|
|
39
|
+
"baseUrl",
|
|
40
|
+
"dimensions"
|
|
41
|
+
];
|
|
42
|
+
function assertAllowedKeys(value, allowed, label) {
|
|
43
|
+
const unknown = Object.keys(value).filter((key) => !allowed.includes(key));
|
|
44
|
+
if (unknown.length === 0) return;
|
|
45
|
+
throw new Error(`${label} has unknown keys: ${unknown.join(", ")}`);
|
|
46
|
+
}
|
|
47
|
+
function vectorDimsForModel(model) {
|
|
48
|
+
const dims = EMBEDDING_DIMENSIONS[model];
|
|
49
|
+
if (!dims) throw new Error(`Unsupported embedding model: ${model}`);
|
|
50
|
+
return dims;
|
|
51
|
+
}
|
|
52
|
+
function resolveEnvVars(value) {
|
|
53
|
+
return value.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
|
|
54
|
+
const envValue = process.env[envVar];
|
|
55
|
+
if (!envValue) throw new Error(`Environment variable ${envVar} is not set`);
|
|
56
|
+
return envValue;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function resolveEmbeddingModel(embedding) {
|
|
60
|
+
const model = typeof embedding.model === "string" ? embedding.model : DEFAULT_MODEL;
|
|
61
|
+
if (typeof embedding.dimensions !== "number") vectorDimsForModel(model);
|
|
62
|
+
return model;
|
|
63
|
+
}
|
|
64
|
+
const memoryConfigSchema = {
|
|
65
|
+
parse(value) {
|
|
66
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("memory config required");
|
|
67
|
+
const cfg = value;
|
|
68
|
+
assertAllowedKeys(cfg, [
|
|
69
|
+
"embedding",
|
|
70
|
+
"dreaming",
|
|
71
|
+
"dbPath",
|
|
72
|
+
"autoCapture",
|
|
73
|
+
"autoRecall",
|
|
74
|
+
"captureMaxChars",
|
|
75
|
+
"customTriggers",
|
|
76
|
+
"recallMaxChars",
|
|
77
|
+
"storageOptions"
|
|
78
|
+
], "memory config");
|
|
79
|
+
const embedding = cfg.embedding;
|
|
80
|
+
if (!embedding || typeof embedding !== "object" || Array.isArray(embedding)) throw new Error("embedding config required");
|
|
81
|
+
assertAllowedKeys(embedding, [...EMBEDDING_CONFIG_KEYS], "embedding config");
|
|
82
|
+
if (Object.keys(embedding).length === 0) throw new Error("embedding config must include at least one setting");
|
|
83
|
+
const model = resolveEmbeddingModel(embedding);
|
|
84
|
+
const provider = typeof embedding.provider === "string" ? embedding.provider.trim() : "openai";
|
|
85
|
+
if (!provider) throw new Error("embedding.provider must not be empty");
|
|
86
|
+
const captureMaxChars = typeof cfg.captureMaxChars === "number" ? Math.floor(cfg.captureMaxChars) : void 0;
|
|
87
|
+
const recallMaxChars = typeof cfg.recallMaxChars === "number" ? Math.floor(cfg.recallMaxChars) : void 0;
|
|
88
|
+
if (typeof captureMaxChars === "number" && (captureMaxChars < 100 || captureMaxChars > 1e4)) throw new Error("captureMaxChars must be between 100 and 10000");
|
|
89
|
+
if (typeof recallMaxChars === "number" && (recallMaxChars < 100 || recallMaxChars > 1e4)) throw new Error("recallMaxChars must be between 100 and 10000");
|
|
90
|
+
let customTriggers;
|
|
91
|
+
if (cfg.customTriggers !== void 0) {
|
|
92
|
+
if (!Array.isArray(cfg.customTriggers)) throw new Error("customTriggers must be an array of strings");
|
|
93
|
+
customTriggers = cfg.customTriggers.map((trigger, index) => {
|
|
94
|
+
if (typeof trigger !== "string") throw new Error(`customTriggers.${index} must be a string`);
|
|
95
|
+
const normalized = trigger.trim();
|
|
96
|
+
if (!normalized) throw new Error(`customTriggers.${index} must not be empty`);
|
|
97
|
+
if (normalized.length > 100) throw new Error(`customTriggers.${index} must be at most 100 characters`);
|
|
98
|
+
return normalized;
|
|
99
|
+
});
|
|
100
|
+
if (customTriggers.length > 50) throw new Error("customTriggers must include at most 50 entries");
|
|
101
|
+
}
|
|
102
|
+
const dreaming = cfg.dreaming === void 0 ? void 0 : cfg.dreaming && typeof cfg.dreaming === "object" && !Array.isArray(cfg.dreaming) ? cfg.dreaming : (() => {
|
|
103
|
+
throw new Error("dreaming config must be an object");
|
|
104
|
+
})();
|
|
105
|
+
let storageOptions;
|
|
106
|
+
const storageOpts = cfg.storageOptions;
|
|
107
|
+
if (storageOpts !== void 0 && storageOpts !== null) {
|
|
108
|
+
if (!storageOpts || typeof storageOpts !== "object" || Array.isArray(storageOpts)) throw new Error("storageOptions must be an object");
|
|
109
|
+
storageOptions = {};
|
|
110
|
+
for (const [key, value] of Object.entries(storageOpts)) {
|
|
111
|
+
if (typeof value !== "string") throw new Error(`storageOptions.${key} must be a string`);
|
|
112
|
+
storageOptions[key] = resolveEnvVars(value);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
embedding: {
|
|
117
|
+
provider,
|
|
118
|
+
model,
|
|
119
|
+
apiKey: typeof embedding.apiKey === "string" ? resolveEnvVars(embedding.apiKey) : void 0,
|
|
120
|
+
baseUrl: typeof embedding.baseUrl === "string" ? resolveEnvVars(embedding.baseUrl) : void 0,
|
|
121
|
+
dimensions: typeof embedding.dimensions === "number" ? embedding.dimensions : void 0
|
|
122
|
+
},
|
|
123
|
+
dreaming,
|
|
124
|
+
dbPath: typeof cfg.dbPath === "string" ? cfg.dbPath : DEFAULT_DB_PATH,
|
|
125
|
+
autoCapture: cfg.autoCapture === true,
|
|
126
|
+
autoRecall: cfg.autoRecall !== false,
|
|
127
|
+
captureMaxChars: captureMaxChars ?? 500,
|
|
128
|
+
...customTriggers ? { customTriggers } : {},
|
|
129
|
+
recallMaxChars: recallMaxChars ?? 1e3,
|
|
130
|
+
...storageOptions ? { storageOptions } : {}
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
uiHints: {
|
|
134
|
+
"embedding.provider": {
|
|
135
|
+
label: "Embedding Provider",
|
|
136
|
+
placeholder: "openai",
|
|
137
|
+
help: "Memory embedding provider adapter to use (for example openai, github-copilot, ollama)"
|
|
138
|
+
},
|
|
139
|
+
"embedding.apiKey": {
|
|
140
|
+
label: "OpenAI API Key",
|
|
141
|
+
sensitive: true,
|
|
142
|
+
placeholder: "sk-proj-...",
|
|
143
|
+
help: "Optional API key override for OpenAI-compatible embeddings; omit to use configured provider auth"
|
|
144
|
+
},
|
|
145
|
+
"embedding.baseUrl": {
|
|
146
|
+
label: "Base URL",
|
|
147
|
+
placeholder: "https://api.openai.com/v1",
|
|
148
|
+
help: "Optional provider or OpenAI-compatible embedding endpoint base URL",
|
|
149
|
+
advanced: true
|
|
150
|
+
},
|
|
151
|
+
"embedding.dimensions": {
|
|
152
|
+
label: "Dimensions",
|
|
153
|
+
placeholder: "1536",
|
|
154
|
+
help: "Vector dimensions for custom models (required for non-standard models)",
|
|
155
|
+
advanced: true
|
|
156
|
+
},
|
|
157
|
+
"embedding.model": {
|
|
158
|
+
label: "Embedding Model",
|
|
159
|
+
placeholder: DEFAULT_MODEL,
|
|
160
|
+
help: "OpenAI embedding model to use"
|
|
161
|
+
},
|
|
162
|
+
dbPath: {
|
|
163
|
+
label: "Database Path",
|
|
164
|
+
placeholder: "~/.klaw/memory/lancedb",
|
|
165
|
+
advanced: true,
|
|
166
|
+
help: "Local filesystem path or cloud storage URI (s3://, gs://) for LanceDB database"
|
|
167
|
+
},
|
|
168
|
+
autoCapture: {
|
|
169
|
+
label: "Auto-Capture",
|
|
170
|
+
help: "Automatically capture important information from conversations"
|
|
171
|
+
},
|
|
172
|
+
autoRecall: {
|
|
173
|
+
label: "Auto-Recall",
|
|
174
|
+
help: "Automatically inject relevant memories into context"
|
|
175
|
+
},
|
|
176
|
+
captureMaxChars: {
|
|
177
|
+
label: "Capture Max Chars",
|
|
178
|
+
help: "Maximum message length eligible for auto-capture",
|
|
179
|
+
advanced: true,
|
|
180
|
+
placeholder: String(500)
|
|
181
|
+
},
|
|
182
|
+
customTriggers: {
|
|
183
|
+
label: "Custom Triggers",
|
|
184
|
+
help: "Literal phrases that should make auto-capture consider a message memory-worthy",
|
|
185
|
+
advanced: true
|
|
186
|
+
},
|
|
187
|
+
recallMaxChars: {
|
|
188
|
+
label: "Recall Query Max Chars",
|
|
189
|
+
help: "Maximum prompt/query length embedded for memory recall. Lower for small local embedding models.",
|
|
190
|
+
advanced: true,
|
|
191
|
+
placeholder: String(DEFAULT_RECALL_MAX_CHARS)
|
|
192
|
+
},
|
|
193
|
+
storageOptions: {
|
|
194
|
+
label: "Storage Options",
|
|
195
|
+
sensitive: true,
|
|
196
|
+
advanced: true,
|
|
197
|
+
help: "Storage configuration options (access_key, secret_key, endpoint, etc.); supports ${ENV_VAR} values"
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
//#endregion
|
|
202
|
+
export { DEFAULT_CAPTURE_MAX_CHARS, DEFAULT_RECALL_MAX_CHARS, MEMORY_CATEGORIES, memoryConfigSchema, vectorDimsForModel };
|