@openclaw/memory-lancedb 2026.7.2-beta.5 → 2026.8.1-beta.2
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/dist/config.js +76 -145
- package/dist/doctor-contract-api.js +4 -6
- package/dist/embeddings.js +93 -36
- package/dist/index.js +28 -11
- package/dist/memory-cli.js +1 -1
- package/openclaw.plugin.json +3 -0
- package/package.json +6 -6
package/dist/config.js
CHANGED
|
@@ -62,153 +62,84 @@ function resolveEmbeddingDimensions(embedding) {
|
|
|
62
62
|
if (dimensions === void 0 || !Number.isInteger(dimensions) || dimensions < 1) throw new Error("embedding.dimensions must be a positive integer");
|
|
63
63
|
return dimensions;
|
|
64
64
|
}
|
|
65
|
-
const memoryConfigSchema = {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
65
|
+
const memoryConfigSchema = { 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 dimensions = resolveEmbeddingDimensions(embedding);
|
|
84
|
+
const model = resolveEmbeddingModel(embedding, dimensions);
|
|
85
|
+
const provider = typeof embedding.provider === "string" ? embedding.provider.trim() : "openai";
|
|
86
|
+
if (!provider) throw new Error("embedding.provider must not be empty");
|
|
87
|
+
const captureMaxChars = resolveBoundedIntegerConfig({
|
|
88
|
+
value: cfg.captureMaxChars,
|
|
89
|
+
fallback: 500,
|
|
90
|
+
min: 100,
|
|
91
|
+
max: 1e4,
|
|
92
|
+
label: "captureMaxChars"
|
|
93
|
+
});
|
|
94
|
+
const recallMaxChars = resolveBoundedIntegerConfig({
|
|
95
|
+
value: cfg.recallMaxChars,
|
|
96
|
+
fallback: DEFAULT_RECALL_MAX_CHARS,
|
|
97
|
+
min: 100,
|
|
98
|
+
max: 1e4,
|
|
99
|
+
label: "recallMaxChars"
|
|
100
|
+
});
|
|
101
|
+
let customTriggers;
|
|
102
|
+
if (cfg.customTriggers !== void 0) {
|
|
103
|
+
if (!Array.isArray(cfg.customTriggers)) throw new Error("customTriggers must be an array of strings");
|
|
104
|
+
customTriggers = cfg.customTriggers.map((trigger, index) => {
|
|
105
|
+
if (typeof trigger !== "string") throw new Error(`customTriggers.${index} must be a string`);
|
|
106
|
+
const normalized = trigger.trim();
|
|
107
|
+
if (!normalized) throw new Error(`customTriggers.${index} must not be empty`);
|
|
108
|
+
if (normalized.length > 100) throw new Error(`customTriggers.${index} must be at most 100 characters`);
|
|
109
|
+
return normalized;
|
|
101
110
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
throw new Error("dreaming config must be an object");
|
|
116
|
-
})();
|
|
117
|
-
let storageOptions;
|
|
118
|
-
const storageOpts = cfg.storageOptions;
|
|
119
|
-
if (storageOpts !== void 0 && storageOpts !== null) {
|
|
120
|
-
if (!storageOpts || typeof storageOpts !== "object" || Array.isArray(storageOpts)) throw new Error("storageOptions must be an object");
|
|
121
|
-
storageOptions = {};
|
|
122
|
-
for (const [key, valueLocal] of Object.entries(storageOpts)) {
|
|
123
|
-
if (typeof valueLocal !== "string") throw new Error(`storageOptions.${key} must be a string`);
|
|
124
|
-
storageOptions[key] = resolveEnvVars(valueLocal);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
embedding: {
|
|
129
|
-
provider,
|
|
130
|
-
model,
|
|
131
|
-
apiKey: typeof embedding.apiKey === "string" ? resolveEnvVars(embedding.apiKey) : void 0,
|
|
132
|
-
baseUrl: typeof embedding.baseUrl === "string" ? resolveEnvVars(embedding.baseUrl) : void 0,
|
|
133
|
-
dimensions
|
|
134
|
-
},
|
|
135
|
-
dreaming,
|
|
136
|
-
dbPath: typeof cfg.dbPath === "string" ? cfg.dbPath : DEFAULT_DB_PATH,
|
|
137
|
-
autoCapture: cfg.autoCapture === true,
|
|
138
|
-
autoRecall: cfg.autoRecall !== false,
|
|
139
|
-
captureMaxChars,
|
|
140
|
-
...customTriggers ? { customTriggers } : {},
|
|
141
|
-
recallMaxChars,
|
|
142
|
-
...storageOptions ? { storageOptions } : {}
|
|
143
|
-
};
|
|
144
|
-
},
|
|
145
|
-
uiHints: {
|
|
146
|
-
"embedding.provider": {
|
|
147
|
-
label: "Embedding Provider",
|
|
148
|
-
placeholder: "openai",
|
|
149
|
-
help: "Memory embedding provider adapter to use (for example openai, github-copilot, ollama)"
|
|
150
|
-
},
|
|
151
|
-
"embedding.apiKey": {
|
|
152
|
-
label: "OpenAI API Key",
|
|
153
|
-
sensitive: true,
|
|
154
|
-
placeholder: "sk-proj-...",
|
|
155
|
-
help: "Optional API key override for OpenAI-compatible embeddings; omit to use configured provider auth"
|
|
156
|
-
},
|
|
157
|
-
"embedding.baseUrl": {
|
|
158
|
-
label: "Base URL",
|
|
159
|
-
placeholder: "https://api.openai.com/v1",
|
|
160
|
-
help: "Optional provider or OpenAI-compatible embedding endpoint base URL",
|
|
161
|
-
advanced: true
|
|
162
|
-
},
|
|
163
|
-
"embedding.dimensions": {
|
|
164
|
-
label: "Dimensions",
|
|
165
|
-
placeholder: "1536",
|
|
166
|
-
help: "Vector dimensions for custom models (required for non-standard models)",
|
|
167
|
-
advanced: true
|
|
168
|
-
},
|
|
169
|
-
"embedding.model": {
|
|
170
|
-
label: "Embedding Model",
|
|
171
|
-
placeholder: DEFAULT_MODEL,
|
|
172
|
-
help: "OpenAI embedding model to use"
|
|
173
|
-
},
|
|
174
|
-
dbPath: {
|
|
175
|
-
label: "Database Path",
|
|
176
|
-
placeholder: "~/.openclaw/memory/lancedb",
|
|
177
|
-
advanced: true,
|
|
178
|
-
help: "Local filesystem path or cloud storage URI (s3://, gs://) for LanceDB database"
|
|
179
|
-
},
|
|
180
|
-
autoCapture: {
|
|
181
|
-
label: "Auto-Capture",
|
|
182
|
-
help: "Automatically capture important information from conversations"
|
|
183
|
-
},
|
|
184
|
-
autoRecall: {
|
|
185
|
-
label: "Auto-Recall",
|
|
186
|
-
help: "Automatically inject relevant memories into context"
|
|
187
|
-
},
|
|
188
|
-
captureMaxChars: {
|
|
189
|
-
label: "Capture Max Chars",
|
|
190
|
-
help: "Maximum message length eligible for auto-capture",
|
|
191
|
-
advanced: true,
|
|
192
|
-
placeholder: String(500)
|
|
193
|
-
},
|
|
194
|
-
customTriggers: {
|
|
195
|
-
label: "Custom Triggers",
|
|
196
|
-
help: "Literal phrases that should make auto-capture consider a message memory-worthy",
|
|
197
|
-
advanced: true
|
|
198
|
-
},
|
|
199
|
-
recallMaxChars: {
|
|
200
|
-
label: "Recall Query Max Chars",
|
|
201
|
-
help: "Maximum prompt/query length embedded for memory recall. Lower for small local embedding models.",
|
|
202
|
-
advanced: true,
|
|
203
|
-
placeholder: String(DEFAULT_RECALL_MAX_CHARS)
|
|
204
|
-
},
|
|
205
|
-
storageOptions: {
|
|
206
|
-
label: "Storage Options",
|
|
207
|
-
sensitive: true,
|
|
208
|
-
advanced: true,
|
|
209
|
-
help: "Storage configuration options (access_key, secret_key, endpoint, etc.); supports ${ENV_VAR} values"
|
|
111
|
+
if (customTriggers.length > 50) throw new Error("customTriggers must include at most 50 entries");
|
|
112
|
+
}
|
|
113
|
+
const dreaming = cfg.dreaming === void 0 ? void 0 : cfg.dreaming && typeof cfg.dreaming === "object" && !Array.isArray(cfg.dreaming) ? cfg.dreaming : (() => {
|
|
114
|
+
throw new Error("dreaming config must be an object");
|
|
115
|
+
})();
|
|
116
|
+
let storageOptions;
|
|
117
|
+
const storageOpts = cfg.storageOptions;
|
|
118
|
+
if (storageOpts !== void 0 && storageOpts !== null) {
|
|
119
|
+
if (!storageOpts || typeof storageOpts !== "object" || Array.isArray(storageOpts)) throw new Error("storageOptions must be an object");
|
|
120
|
+
storageOptions = {};
|
|
121
|
+
for (const [key, valueLocal] of Object.entries(storageOpts)) {
|
|
122
|
+
if (typeof valueLocal !== "string") throw new Error(`storageOptions.${key} must be a string`);
|
|
123
|
+
storageOptions[key] = resolveEnvVars(valueLocal);
|
|
210
124
|
}
|
|
211
125
|
}
|
|
212
|
-
|
|
126
|
+
return {
|
|
127
|
+
embedding: {
|
|
128
|
+
provider,
|
|
129
|
+
model,
|
|
130
|
+
apiKey: typeof embedding.apiKey === "string" ? resolveEnvVars(embedding.apiKey) : void 0,
|
|
131
|
+
baseUrl: typeof embedding.baseUrl === "string" ? resolveEnvVars(embedding.baseUrl) : void 0,
|
|
132
|
+
dimensions
|
|
133
|
+
},
|
|
134
|
+
dreaming,
|
|
135
|
+
dbPath: typeof cfg.dbPath === "string" ? cfg.dbPath : DEFAULT_DB_PATH,
|
|
136
|
+
autoCapture: cfg.autoCapture === true,
|
|
137
|
+
autoRecall: cfg.autoRecall !== false,
|
|
138
|
+
captureMaxChars,
|
|
139
|
+
...customTriggers ? { customTriggers } : {},
|
|
140
|
+
recallMaxChars,
|
|
141
|
+
...storageOptions ? { storageOptions } : {}
|
|
142
|
+
};
|
|
143
|
+
} };
|
|
213
144
|
//#endregion
|
|
214
145
|
export { DEFAULT_CAPTURE_MAX_CHARS, DEFAULT_RECALL_MAX_CHARS, MEMORY_CATEGORIES, memoryConfigSchema, vectorDimsForModel };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { MEMORY_AGENT_ID_COLUMN, MEMORY_TABLE_NAME, hasAgentScopeColumn, memoryAgentPredicate, quoteLanceSqlString } from "./lancedb-schema.js";
|
|
2
|
-
import {
|
|
2
|
+
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import fs from "node:fs";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-scope-runtime";
|
|
7
8
|
//#region extensions/memory-lancedb/doctor-contract-api.ts
|
|
8
9
|
const LEGACY_ENVELOPE_DELETE_BATCH_SIZE = 500;
|
|
9
10
|
const LEGACY_ENVELOPE_SENTINEL_LINE_RE = new RegExp(`^(?:${[
|
|
@@ -37,14 +38,11 @@ function resolveMemoryLanceDbPluginRoot(moduleUrl) {
|
|
|
37
38
|
return path.basename(artifactDir) === "dist" ? path.dirname(artifactDir) : artifactDir;
|
|
38
39
|
}
|
|
39
40
|
const DEFAULT_PLUGIN_ROOT = resolveMemoryLanceDbPluginRoot(import.meta.url);
|
|
40
|
-
function asRecord(value) {
|
|
41
|
-
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
42
|
-
}
|
|
43
41
|
function resolveHome(env) {
|
|
44
42
|
return env.HOME?.trim() || os.homedir();
|
|
45
43
|
}
|
|
46
44
|
function resolveConfiguredDbPath(config, env, pluginRoot) {
|
|
47
|
-
const pluginConfig =
|
|
45
|
+
const pluginConfig = asOptionalRecord(config.plugins?.entries?.["memory-lancedb"]?.config);
|
|
48
46
|
const configured = typeof pluginConfig?.dbPath === "string" ? pluginConfig.dbPath.trim() : "";
|
|
49
47
|
if (!configured) return path.join(resolveHome(env), ".openclaw", "memory", "lancedb");
|
|
50
48
|
if (configured.includes("://")) return configured;
|
|
@@ -52,7 +50,7 @@ function resolveConfiguredDbPath(config, env, pluginRoot) {
|
|
|
52
50
|
return path.resolve(pluginRoot, configured);
|
|
53
51
|
}
|
|
54
52
|
function resolveStorageOptions(config, env) {
|
|
55
|
-
const rawOptions =
|
|
53
|
+
const rawOptions = asOptionalRecord(asOptionalRecord(config.plugins?.entries?.["memory-lancedb"]?.config)?.storageOptions);
|
|
56
54
|
if (!rawOptions) return;
|
|
57
55
|
return Object.fromEntries(Object.entries(rawOptions).map(([key, value]) => {
|
|
58
56
|
if (typeof value !== "string") throw new Error(`memory-lancedb storageOptions.${key} must be a string`);
|
package/dist/embeddings.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import { formatErrorMessage, toErrorObject } from "openclaw/plugin-sdk/error-runtime";
|
|
1
2
|
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
|
|
3
|
+
import { normalizeAgentId } from "openclaw/plugin-sdk/routing";
|
|
2
4
|
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
5
|
+
import { resolve } from "node:path";
|
|
3
6
|
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
|
|
4
7
|
import { Buffer } from "node:buffer";
|
|
5
|
-
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
|
|
6
8
|
import { resolveGlobalSingleton } from "openclaw/plugin-sdk/global-singleton";
|
|
7
9
|
import { canonicalizeBase64 } from "openclaw/plugin-sdk/media-runtime";
|
|
8
10
|
import { ensureGlobalUndiciEnvProxyDispatcher } from "openclaw/plugin-sdk/runtime-env";
|
|
9
11
|
//#region extensions/memory-lancedb/embeddings.ts
|
|
10
12
|
const loadOpenAiModule = createLazyRuntimeModule(() => import("openai"));
|
|
11
13
|
const loadMemoryEmbeddingProviderModule = createLazyRuntimeModule(() => import("openclaw/plugin-sdk/memory-core-host-engine-embeddings"));
|
|
12
|
-
const loadMemoryHostCoreModule = createLazyRuntimeModule(() => import("openclaw/plugin-sdk/memory-host-core"));
|
|
13
14
|
const PROVIDER_ADAPTER_LIFECYCLE = resolveGlobalSingleton(Symbol.for("openclaw.memoryLanceDbEmbeddingProviderLifecycle.v1"), () => ({
|
|
14
15
|
retainedProviders: /* @__PURE__ */ new Set(),
|
|
15
16
|
tail: Promise.resolve()
|
|
@@ -40,7 +41,7 @@ var OpenAiCompatibleEmbeddings = class {
|
|
|
40
41
|
baseURL: baseUrl
|
|
41
42
|
}));
|
|
42
43
|
}
|
|
43
|
-
async embed(text, options) {
|
|
44
|
+
async embed(_agentId, text, options) {
|
|
44
45
|
const dimensions = this.dimensions;
|
|
45
46
|
const startedAtMs = options?.timeoutMs && Number.isFinite(options.timeoutMs) ? Date.now() : null;
|
|
46
47
|
try {
|
|
@@ -112,17 +113,51 @@ var ProviderAdapterEmbeddings = class {
|
|
|
112
113
|
constructor(api, embedding) {
|
|
113
114
|
this.api = api;
|
|
114
115
|
this.embedding = embedding;
|
|
116
|
+
this.providers = /* @__PURE__ */ new Map();
|
|
115
117
|
this.closePromise = null;
|
|
116
118
|
this.closed = false;
|
|
117
119
|
this.activeUses = 0;
|
|
118
120
|
this.idleWaiters = /* @__PURE__ */ new Set();
|
|
119
121
|
}
|
|
120
|
-
getProvider() {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
getProvider(agentId) {
|
|
123
|
+
const config = this.api.runtime.config?.current?.() ?? this.api.config;
|
|
124
|
+
const agentDir = this.api.runtime.agent.resolveAgentDir(config, agentId);
|
|
125
|
+
const existing = this.providers.get(agentId);
|
|
126
|
+
if (existing?.config === config && existing.agentDir === agentDir) return existing;
|
|
127
|
+
if (existing) {
|
|
128
|
+
this.providers.delete(agentId);
|
|
129
|
+
this.retireProvider(existing);
|
|
130
|
+
}
|
|
131
|
+
const entry = {
|
|
132
|
+
config,
|
|
133
|
+
agentDir,
|
|
134
|
+
promise: this.createProvider(config, agentDir).catch((err) => {
|
|
135
|
+
if (this.providers.get(agentId) === entry) this.providers.delete(agentId);
|
|
136
|
+
throw err;
|
|
137
|
+
}),
|
|
138
|
+
activeUses: 0,
|
|
139
|
+
idleWaiters: /* @__PURE__ */ new Set()
|
|
140
|
+
};
|
|
141
|
+
this.providers.set(agentId, entry);
|
|
142
|
+
return entry;
|
|
143
|
+
}
|
|
144
|
+
retireProvider(entry) {
|
|
145
|
+
runProviderAdapterLifecycle(async () => {
|
|
146
|
+
if (entry.activeUses > 0) await new Promise((resolve) => {
|
|
147
|
+
entry.idleWaiters.add(resolve);
|
|
148
|
+
});
|
|
149
|
+
const provider = await entry.promise.catch(() => null);
|
|
150
|
+
if (provider) PROVIDER_ADAPTER_LIFECYCLE.retainedProviders.add(provider);
|
|
151
|
+
await drainRetainedProviders();
|
|
152
|
+
}).catch(() => void 0);
|
|
153
|
+
}
|
|
154
|
+
invalidateProvidersForAuthMutation(event) {
|
|
155
|
+
const changedAgentDir = event.agentDir ? resolve(event.agentDir) : void 0;
|
|
156
|
+
for (const [agentId, entry] of this.providers) {
|
|
157
|
+
if (!event.affectsInheritedStores && resolve(entry.agentDir) !== changedAgentDir) continue;
|
|
158
|
+
this.providers.delete(agentId);
|
|
159
|
+
this.retireProvider(entry);
|
|
160
|
+
}
|
|
126
161
|
}
|
|
127
162
|
acquireUse() {
|
|
128
163
|
if (this.closed) throw new Error("memory-lancedb embeddings are closed");
|
|
@@ -145,27 +180,24 @@ var ProviderAdapterEmbeddings = class {
|
|
|
145
180
|
this.idleWaiters.add(resolve);
|
|
146
181
|
});
|
|
147
182
|
}
|
|
148
|
-
async createProvider() {
|
|
183
|
+
async createProvider(config, agentDir) {
|
|
149
184
|
return await runProviderAdapterLifecycle(async () => {
|
|
150
185
|
await drainRetainedProviders();
|
|
151
|
-
return await this.createProviderAfterRetirement();
|
|
186
|
+
return await this.createProviderAfterRetirement(config, agentDir);
|
|
152
187
|
});
|
|
153
188
|
}
|
|
154
|
-
async createProviderAfterRetirement() {
|
|
155
|
-
const cfg = this.api.runtime.config?.current?.() ?? this.api.config;
|
|
189
|
+
async createProviderAfterRetirement(config, agentDir) {
|
|
156
190
|
const providerId = this.embedding.provider;
|
|
157
|
-
const { getMemoryEmbeddingProvider } = await loadMemoryEmbeddingProviderModule();
|
|
158
|
-
|
|
191
|
+
const { getMemoryEmbeddingProvider, registerRuntimeAuthProfileStoreMutationListener } = await loadMemoryEmbeddingProviderModule();
|
|
192
|
+
if (!this.closed && !this.unregisterAuthMutationListener) this.unregisterAuthMutationListener = registerRuntimeAuthProfileStoreMutationListener((event) => this.invalidateProvidersForAuthMutation(event));
|
|
193
|
+
const adapter = getMemoryEmbeddingProvider(providerId, config);
|
|
159
194
|
if (!adapter) throw new Error(`Unknown memory embedding provider: ${providerId}`);
|
|
160
|
-
const { resolveDefaultAgentId } = await loadMemoryHostCoreModule();
|
|
161
|
-
const defaultAgentId = resolveDefaultAgentId(cfg);
|
|
162
|
-
const agentDir = this.api.runtime.agent.resolveAgentDir(cfg, defaultAgentId);
|
|
163
195
|
const remote = this.embedding.apiKey || this.embedding.baseUrl ? {
|
|
164
196
|
...this.embedding.apiKey ? { apiKey: this.embedding.apiKey } : {},
|
|
165
197
|
...this.embedding.baseUrl ? { baseUrl: this.embedding.baseUrl } : {}
|
|
166
198
|
} : void 0;
|
|
167
199
|
const result = await adapter.create({
|
|
168
|
-
config
|
|
200
|
+
config,
|
|
169
201
|
agentDir,
|
|
170
202
|
provider: providerId,
|
|
171
203
|
fallback: "none",
|
|
@@ -176,19 +208,30 @@ var ProviderAdapterEmbeddings = class {
|
|
|
176
208
|
if (!result.provider) throw new Error(`Memory embedding provider ${providerId} is unavailable.`);
|
|
177
209
|
return result.provider;
|
|
178
210
|
}
|
|
179
|
-
async embed(text, options) {
|
|
211
|
+
async embed(agentId, text, options) {
|
|
180
212
|
const releaseUse = this.acquireUse();
|
|
181
213
|
try {
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
const controller = new AbortController();
|
|
185
|
-
let timer;
|
|
214
|
+
const entry = this.getProvider(normalizeAgentId(agentId));
|
|
215
|
+
entry.activeUses += 1;
|
|
186
216
|
try {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
217
|
+
const provider = await entry.promise;
|
|
218
|
+
if (!options?.timeoutMs) return await provider.embedQuery(text);
|
|
219
|
+
const controller = new AbortController();
|
|
220
|
+
let timer;
|
|
221
|
+
try {
|
|
222
|
+
timer = setTimeout(() => controller.abort(/* @__PURE__ */ new Error("memory-lancedb embedding timed out")), resolveTimerTimeoutMs(options.timeoutMs, 1));
|
|
223
|
+
timer.unref?.();
|
|
224
|
+
return await provider.embedQuery(text, { signal: controller.signal });
|
|
225
|
+
} finally {
|
|
226
|
+
if (timer) clearTimeout(timer);
|
|
227
|
+
}
|
|
190
228
|
} finally {
|
|
191
|
-
|
|
229
|
+
entry.activeUses -= 1;
|
|
230
|
+
if (entry.activeUses === 0) {
|
|
231
|
+
const waiters = Array.from(entry.idleWaiters);
|
|
232
|
+
entry.idleWaiters.clear();
|
|
233
|
+
for (const resolve of waiters) resolve();
|
|
234
|
+
}
|
|
192
235
|
}
|
|
193
236
|
} finally {
|
|
194
237
|
releaseUse();
|
|
@@ -211,15 +254,19 @@ var ProviderAdapterEmbeddings = class {
|
|
|
211
254
|
}
|
|
212
255
|
async closeOnce() {
|
|
213
256
|
this.closed = true;
|
|
214
|
-
|
|
257
|
+
this.unregisterAuthMutationListener?.();
|
|
258
|
+
this.unregisterAuthMutationListener = void 0;
|
|
259
|
+
const providers = Array.from(this.providers.entries());
|
|
215
260
|
await runProviderAdapterLifecycle(async () => {
|
|
216
261
|
await this.awaitIdle();
|
|
217
|
-
const
|
|
218
|
-
|
|
262
|
+
for (const [, entry] of providers) {
|
|
263
|
+
const provider = await entry.promise.catch(() => null);
|
|
264
|
+
if (provider) PROVIDER_ADAPTER_LIFECYCLE.retainedProviders.add(provider);
|
|
265
|
+
}
|
|
219
266
|
try {
|
|
220
267
|
await drainRetainedProviders();
|
|
221
268
|
} finally {
|
|
222
|
-
if (this.
|
|
269
|
+
for (const [agentId, entry] of providers) if (this.providers.get(agentId) === entry) this.providers.delete(agentId);
|
|
223
270
|
}
|
|
224
271
|
});
|
|
225
272
|
}
|
|
@@ -244,8 +291,17 @@ async function runWithTimeout(params) {
|
|
|
244
291
|
if (timeout) clearTimeout(timeout);
|
|
245
292
|
}
|
|
246
293
|
}
|
|
247
|
-
function
|
|
248
|
-
|
|
294
|
+
function isMemoryRecallTimeoutError(error) {
|
|
295
|
+
let current = error;
|
|
296
|
+
for (let depth = 0; depth < 3 && current !== void 0; depth += 1) {
|
|
297
|
+
const record = asOptionalRecord(current);
|
|
298
|
+
const name = current instanceof Error ? current.name : typeof record?.name === "string" ? record.name : "";
|
|
299
|
+
const message = current instanceof Error ? current.message : typeof record?.message === "string" ? record.message : "";
|
|
300
|
+
const code = typeof record?.code === "string" ? record.code : "";
|
|
301
|
+
if (name === "APIConnectionTimeoutError" || name === "TimeoutError" || code === "ETIMEDOUT" || /^UND_ERR_.*_TIMEOUT$/.test(code) || /\btimed out\b/i.test(message)) return true;
|
|
302
|
+
current = record?.cause;
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
249
305
|
}
|
|
250
306
|
function buildMemoryRecallUnavailableResult(error) {
|
|
251
307
|
return {
|
|
@@ -263,13 +319,14 @@ function buildMemoryRecallUnavailableResult(error) {
|
|
|
263
319
|
}
|
|
264
320
|
var MemoryRecallEmbeddingError = class extends Error {
|
|
265
321
|
constructor(originalError) {
|
|
266
|
-
super(
|
|
322
|
+
super(formatErrorMessage(originalError));
|
|
267
323
|
this.originalError = originalError;
|
|
268
324
|
this.name = "MemoryRecallEmbeddingError";
|
|
269
325
|
}
|
|
270
326
|
};
|
|
271
327
|
const testing = {
|
|
272
328
|
isEmbeddingDimensionsRejectedError,
|
|
329
|
+
isMemoryRecallTimeoutError,
|
|
273
330
|
runWithTimeout,
|
|
274
331
|
truncateEmbeddingVector
|
|
275
332
|
};
|
|
@@ -296,4 +353,4 @@ function normalizeEmbeddingVector(value) {
|
|
|
296
353
|
throw new Error("Embedding response is missing a vector");
|
|
297
354
|
}
|
|
298
355
|
//#endregion
|
|
299
|
-
export { MemoryRecallEmbeddingError, buildMemoryRecallUnavailableResult, createEmbeddings,
|
|
356
|
+
export { MemoryRecallEmbeddingError, buildMemoryRecallUnavailableResult, createEmbeddings, isMemoryRecallTimeoutError, normalizeEmbeddingVector, runWithTimeout, testing };
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { definePluginEntry } from "./api.js";
|
|
2
2
|
import { MEMORY_CATEGORIES, memoryConfigSchema, vectorDimsForModel } from "./config.js";
|
|
3
|
-
import { MemoryRecallEmbeddingError, buildMemoryRecallUnavailableResult, createEmbeddings,
|
|
3
|
+
import { MemoryRecallEmbeddingError, buildMemoryRecallUnavailableResult, createEmbeddings, isMemoryRecallTimeoutError, normalizeEmbeddingVector, runWithTimeout, testing } from "./embeddings.js";
|
|
4
4
|
import { MemoryDB } from "./lancedb-store.js";
|
|
5
5
|
import { dropMediaNoteLines, looksLikeEnvelopeSludge, sanitizeForMemoryCapture } from "./memory-capture-sanitization.js";
|
|
6
6
|
import { cleanMemorySearchResults, detectCategory, escapeMemoryForPrompt, extractLatestUserText, extractUserTextContent, findCleanDuplicateMemory, formatRelevantMemoriesContext, looksLikePromptInjection, messageFingerprint, normalizeRecallQuery, resolveAutoCaptureStartIndex, shouldCapture } from "./memory-policy.js";
|
|
7
7
|
import { parseMemoryCliFilter, registerMemoryCli } from "./memory-cli.js";
|
|
8
8
|
import { resolveAgentConfig, resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
|
|
9
9
|
import { optionalFiniteNumberSchema, optionalPositiveIntegerSchema } from "openclaw/plugin-sdk/channel-actions";
|
|
10
|
+
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
|
|
10
11
|
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
|
|
11
12
|
import { readFiniteNumberParam, readPositiveIntegerParam } from "openclaw/plugin-sdk/param-readers";
|
|
12
13
|
import { resolveLivePluginConfigObject } from "openclaw/plugin-sdk/plugin-config-runtime";
|
|
@@ -18,7 +19,7 @@ import { Type } from "typebox";
|
|
|
18
19
|
const loadMemoryHostCoreModule = createLazyRuntimeModule(() => import("openclaw/plugin-sdk/memory-host-core"));
|
|
19
20
|
const DEFAULT_AUTO_RECALL_TIMEOUT_MS = 15e3;
|
|
20
21
|
const DEFAULT_TOOL_RECALL_TIMEOUT_MS = 15e3;
|
|
21
|
-
const
|
|
22
|
+
const DEFAULT_RECALL_COOLDOWN_MS = 6e4;
|
|
22
23
|
const DEFAULT_TOOL_RECALL_OVERFETCH_EXTRA = 10;
|
|
23
24
|
const DEFAULT_AUTO_RECALL_OVERFETCH_LIMIT = 10;
|
|
24
25
|
const DEFAULT_AUTO_RECALL_RESULT_CAP = 3;
|
|
@@ -97,7 +98,7 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
97
98
|
};
|
|
98
99
|
const recordMemoryRecallCooldown = (agentId, error) => {
|
|
99
100
|
memoryRecallCooldowns.set(agentId, {
|
|
100
|
-
until: Date.now() +
|
|
101
|
+
until: Date.now() + DEFAULT_RECALL_COOLDOWN_MS,
|
|
101
102
|
error
|
|
102
103
|
});
|
|
103
104
|
};
|
|
@@ -124,6 +125,7 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
124
125
|
const currentCfg = resolveCurrentHookConfig();
|
|
125
126
|
const cooldown = readMemoryRecallCooldown(agentId);
|
|
126
127
|
if (cooldown) return buildMemoryRecallUnavailableResult(cooldown.error);
|
|
128
|
+
let recallPhase = "embedding";
|
|
127
129
|
let recall;
|
|
128
130
|
try {
|
|
129
131
|
recall = await runWithTimeout({
|
|
@@ -131,23 +133,24 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
131
133
|
task: async () => {
|
|
132
134
|
let vector;
|
|
133
135
|
try {
|
|
134
|
-
vector = await embeddings.embed(normalizeRecallQuery(query, currentCfg.recallMaxChars), { timeoutMs: DEFAULT_TOOL_RECALL_TIMEOUT_MS });
|
|
136
|
+
vector = await embeddings.embed(agentId, normalizeRecallQuery(query, currentCfg.recallMaxChars), { timeoutMs: DEFAULT_TOOL_RECALL_TIMEOUT_MS });
|
|
135
137
|
} catch (error) {
|
|
136
138
|
throw new MemoryRecallEmbeddingError(error);
|
|
137
139
|
}
|
|
140
|
+
recallPhase = "search";
|
|
138
141
|
return await db.search(agentId, vector, limit + DEFAULT_TOOL_RECALL_OVERFETCH_EXTRA, .1);
|
|
139
142
|
}
|
|
140
143
|
});
|
|
141
144
|
} catch (error) {
|
|
142
145
|
if (!(error instanceof MemoryRecallEmbeddingError)) throw error;
|
|
143
|
-
const message =
|
|
144
|
-
recordMemoryRecallCooldown(agentId, message);
|
|
146
|
+
const message = formatErrorMessage(error.originalError);
|
|
147
|
+
if (isMemoryRecallTimeoutError(error.originalError)) recordMemoryRecallCooldown(agentId, message);
|
|
145
148
|
api.logger.warn?.(`memory-lancedb: memory_recall failed: ${message}; returning unavailable memory result`);
|
|
146
149
|
return buildMemoryRecallUnavailableResult(message);
|
|
147
150
|
}
|
|
148
151
|
if (recall.status === "timeout") {
|
|
149
152
|
const message = `memory_recall timed out after ${Math.round(DEFAULT_TOOL_RECALL_TIMEOUT_MS / 1e3)}s`;
|
|
150
|
-
recordMemoryRecallCooldown(agentId, message);
|
|
153
|
+
if (recallPhase === "embedding") recordMemoryRecallCooldown(agentId, message);
|
|
151
154
|
api.logger.warn?.(`memory-lancedb: memory_recall timed out after ${DEFAULT_TOOL_RECALL_TIMEOUT_MS}ms; returning unavailable memory result`);
|
|
152
155
|
return buildMemoryRecallUnavailableResult(message);
|
|
153
156
|
}
|
|
@@ -225,7 +228,7 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
225
228
|
reason: "prompt_injection_detected"
|
|
226
229
|
}
|
|
227
230
|
};
|
|
228
|
-
const vector = await embeddings.embed(text);
|
|
231
|
+
const vector = await embeddings.embed(agentId, text);
|
|
229
232
|
const existing = await findCleanDuplicateMemory(db, agentId, vector);
|
|
230
233
|
if (existing) return {
|
|
231
234
|
content: [{
|
|
@@ -294,7 +297,7 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
294
297
|
}
|
|
295
298
|
if (query) {
|
|
296
299
|
const currentCfg = resolveCurrentHookConfig();
|
|
297
|
-
const vector = await embeddings.embed(normalizeRecallQuery(query, currentCfg.recallMaxChars));
|
|
300
|
+
const vector = await embeddings.embed(agentId, normalizeRecallQuery(query, currentCfg.recallMaxChars));
|
|
298
301
|
const results = await db.search(agentId, vector, 5, .7);
|
|
299
302
|
if (results.length === 0) return {
|
|
300
303
|
content: [{
|
|
@@ -352,17 +355,30 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
352
355
|
const agentId = resolveEnabledAgentId(ctx.agentId);
|
|
353
356
|
if (!agentId) return;
|
|
354
357
|
if (!event.prompt || event.prompt.length < 5) return;
|
|
358
|
+
const cooldown = readMemoryRecallCooldown(agentId);
|
|
359
|
+
if (cooldown) {
|
|
360
|
+
api.logger.debug?.(`memory-lancedb: auto-recall skipped during recall cooldown: ${cooldown.error}`);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
355
363
|
try {
|
|
356
364
|
const recallQuery = normalizeRecallQuery(dropMediaNoteLines(extractLatestUserText(Array.isArray(event.messages) ? event.messages : []) ?? event.prompt), currentCfg.recallMaxChars);
|
|
357
365
|
if (!recallQuery) return;
|
|
366
|
+
let recallPhase = "embedding";
|
|
358
367
|
const recall = await runWithTimeout({
|
|
359
368
|
timeoutMs: DEFAULT_AUTO_RECALL_TIMEOUT_MS,
|
|
360
369
|
task: async () => {
|
|
361
|
-
|
|
370
|
+
let vector;
|
|
371
|
+
try {
|
|
372
|
+
vector = await embeddings.embed(agentId, recallQuery, { timeoutMs: DEFAULT_AUTO_RECALL_TIMEOUT_MS });
|
|
373
|
+
} catch (error) {
|
|
374
|
+
throw new MemoryRecallEmbeddingError(error);
|
|
375
|
+
}
|
|
376
|
+
recallPhase = "search";
|
|
362
377
|
return await db.search(agentId, vector, DEFAULT_AUTO_RECALL_OVERFETCH_LIMIT, .3);
|
|
363
378
|
}
|
|
364
379
|
});
|
|
365
380
|
if (recall.status === "timeout") {
|
|
381
|
+
if (recallPhase === "embedding") recordMemoryRecallCooldown(agentId, `auto-recall timed out after ${Math.round(DEFAULT_AUTO_RECALL_TIMEOUT_MS / 1e3)}s`);
|
|
366
382
|
api.logger.warn?.(`memory-lancedb: auto-recall timed out after ${DEFAULT_AUTO_RECALL_TIMEOUT_MS}ms; skipping memory injection to avoid stalling agent startup`);
|
|
367
383
|
return;
|
|
368
384
|
}
|
|
@@ -376,6 +392,7 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
376
392
|
if (!context) return;
|
|
377
393
|
return { prependContext: context };
|
|
378
394
|
} catch (err) {
|
|
395
|
+
if (err instanceof MemoryRecallEmbeddingError && isMemoryRecallTimeoutError(err.originalError)) recordMemoryRecallCooldown(agentId, formatErrorMessage(err.originalError));
|
|
379
396
|
api.logger.warn(`memory-lancedb: recall failed: ${String(err)}`);
|
|
380
397
|
}
|
|
381
398
|
});
|
|
@@ -404,7 +421,7 @@ var memory_lancedb_default = definePluginEntry({
|
|
|
404
421
|
capturableSeen++;
|
|
405
422
|
if (capturableSeen > 3) continue;
|
|
406
423
|
const category = detectCategory(sanitized);
|
|
407
|
-
const vector = await embeddings.embed(sanitized);
|
|
424
|
+
const vector = await embeddings.embed(agentId, sanitized);
|
|
408
425
|
if (await findCleanDuplicateMemory(db, agentId, vector)) continue;
|
|
409
426
|
await db.store(agentId, {
|
|
410
427
|
text: sanitized,
|
package/dist/memory-cli.js
CHANGED
|
@@ -65,8 +65,8 @@ function registerMemoryCli(api, db, embeddings, resolveCliAgentId, recallMaxChar
|
|
|
65
65
|
let operationFailed = false;
|
|
66
66
|
try {
|
|
67
67
|
const agentId = resolveCliAgentId(opts.agent);
|
|
68
|
-
const vector = await embeddings.embed(normalizeRecallQuery(query, recallMaxChars));
|
|
69
68
|
const limit = parsePositiveIntegerOption(opts.limit, "--limit");
|
|
69
|
+
const vector = await embeddings.embed(agentId, normalizeRecallQuery(query, recallMaxChars));
|
|
70
70
|
const output = (await db.search(agentId, vector, limit, .3)).map((r) => ({
|
|
71
71
|
id: r.entry.id,
|
|
72
72
|
text: r.entry.text,
|
package/openclaw.plugin.json
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "memory-lancedb",
|
|
3
|
+
"doctorContract": {
|
|
4
|
+
"stateMigrations": true
|
|
5
|
+
},
|
|
3
6
|
"name": "Memory LanceDB",
|
|
4
7
|
"description": "OpenClaw LanceDB-backed long-term memory plugin with auto-recall, auto-capture, and vector search.",
|
|
5
8
|
"catalog": { "featured": true, "order": 70 },
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openclaw/memory-lancedb",
|
|
3
|
-
"version": "2026.
|
|
3
|
+
"version": "2026.8.1-beta.2",
|
|
4
4
|
"description": "OpenClaw LanceDB-backed long-term memory plugin with auto-recall, auto-capture, and vector search.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@lancedb/lancedb": "0.
|
|
11
|
+
"@lancedb/lancedb": "0.31.0",
|
|
12
12
|
"apache-arrow": "18.1.0",
|
|
13
|
-
"openai": "6.
|
|
13
|
+
"openai": "6.49.0",
|
|
14
14
|
"typebox": "1.3.6"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"minHostVersion": ">=2026.5.31"
|
|
27
27
|
},
|
|
28
28
|
"compat": {
|
|
29
|
-
"pluginApi": ">=2026.
|
|
29
|
+
"pluginApi": ">=2026.8.1-beta.2"
|
|
30
30
|
},
|
|
31
31
|
"build": {
|
|
32
|
-
"openclawVersion": "2026.
|
|
32
|
+
"openclawVersion": "2026.8.1-beta.2"
|
|
33
33
|
},
|
|
34
34
|
"release": {
|
|
35
35
|
"bundleRuntimeDependencies": false,
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"README.md"
|
|
47
47
|
],
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"openclaw": ">=2026.
|
|
49
|
+
"openclaw": ">=2026.8.1-beta.2"
|
|
50
50
|
},
|
|
51
51
|
"peerDependenciesMeta": {
|
|
52
52
|
"openclaw": {
|