memory-lancedb-pro 1.0.23 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -0
- package/README_CN.md +47 -0
- package/index.ts +7 -4
- package/openclaw.plugin.json +9 -5
- package/package.json +1 -1
- package/src/embedder.ts +79 -11
package/README.md
CHANGED
|
@@ -710,6 +710,51 @@ On LanceDB 0.26+ (via Apache Arrow), some numeric columns may be returned as `Bi
|
|
|
710
710
|
|
|
711
711
|
upgrade to **memory-lancedb-pro >= 1.0.14**. This plugin now coerces these values using `Number(...)` before doing arithmetic (for example, when computing scores or sorting by timestamp).
|
|
712
712
|
|
|
713
|
+
## Iron Rules for AI Agents (铁律)
|
|
714
|
+
|
|
715
|
+
> **For OpenClaw users**: copy the code block below into your `AGENTS.md` so your agent enforces these rules automatically.
|
|
716
|
+
|
|
717
|
+
```markdown
|
|
718
|
+
## Rule 1 — 双层记忆存储(铁律)
|
|
719
|
+
|
|
720
|
+
Every pitfall/lesson learned → IMMEDIATELY store TWO memories to LanceDB before moving on:
|
|
721
|
+
|
|
722
|
+
- **Technical layer**: Pitfall: [symptom]. Cause: [root cause]. Fix: [solution]. Prevention: [how to avoid]
|
|
723
|
+
(category: fact, importance ≥ 0.8)
|
|
724
|
+
- **Principle layer**: Decision principle ([tag]): [behavioral rule]. Trigger: [when it applies]. Action: [what to do]
|
|
725
|
+
(category: decision, importance ≥ 0.85)
|
|
726
|
+
- After each store, immediately `memory_recall` with anchor keywords to verify retrieval.
|
|
727
|
+
If not found, rewrite and re-store.
|
|
728
|
+
- Missing either layer = incomplete.
|
|
729
|
+
Do NOT proceed to next topic until both are stored and verified.
|
|
730
|
+
- Also update relevant SKILL.md files to prevent recurrence.
|
|
731
|
+
|
|
732
|
+
## Rule 2 — LanceDB 卫生
|
|
733
|
+
|
|
734
|
+
Entries must be short and atomic (< 500 chars). Never store raw conversation summaries, large blobs, or duplicates.
|
|
735
|
+
Prefer structured format with keywords for retrieval.
|
|
736
|
+
|
|
737
|
+
## Rule 3 — Recall before retry
|
|
738
|
+
|
|
739
|
+
On ANY tool failure, repeated error, or unexpected behavior, ALWAYS `memory_recall` with relevant keywords
|
|
740
|
+
(error message, tool name, symptom) BEFORE retrying. LanceDB likely already has the fix.
|
|
741
|
+
Blind retries waste time and repeat known mistakes.
|
|
742
|
+
|
|
743
|
+
## Rule 4 — 编辑前确认目标代码库
|
|
744
|
+
|
|
745
|
+
When working on memory plugins, confirm you are editing the intended package
|
|
746
|
+
(e.g., `memory-lancedb-pro` vs built-in `memory-lancedb`) before making changes;
|
|
747
|
+
use `memory_recall` + filesystem search to avoid patching the wrong repo.
|
|
748
|
+
|
|
749
|
+
## Rule 5 — 插件代码变更必须清 jiti 缓存(MANDATORY)
|
|
750
|
+
|
|
751
|
+
After modifying ANY `.ts` file under `plugins/`, MUST run `rm -rf /tmp/jiti/` BEFORE `openclaw gateway restart`.
|
|
752
|
+
jiti caches compiled TS; restart alone loads STALE code. This has caused silent bugs multiple times.
|
|
753
|
+
Config-only changes do NOT need cache clearing.
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
---
|
|
757
|
+
|
|
713
758
|
## Dependencies
|
|
714
759
|
|
|
715
760
|
| Package | Purpose |
|
|
@@ -733,12 +778,14 @@ Top contributors (from GitHub’s contributors list, sorted by commit contributi
|
|
|
733
778
|
<a href="https://github.com/furedericca-lab"><img src="https://avatars.githubusercontent.com/u/263020793?v=4" width="48" height="48" alt="@furedericca-lab" /></a>
|
|
734
779
|
<a href="https://github.com/joe2643"><img src="https://avatars.githubusercontent.com/u/19421931?v=4" width="48" height="48" alt="@joe2643" /></a>
|
|
735
780
|
<a href="https://github.com/AliceLJY"><img src="https://avatars.githubusercontent.com/u/136287420?v=4" width="48" height="48" alt="@AliceLJY" /></a>
|
|
781
|
+
<a href="https://github.com/chenjiyong"><img src="https://avatars.githubusercontent.com/u/8199522?v=4" width="48" height="48" alt="@chenjiyong" /></a>
|
|
736
782
|
</p>
|
|
737
783
|
|
|
738
784
|
- [@win4r](https://github.com/win4r) (3 commits)
|
|
739
785
|
- [@kctony](https://github.com/kctony) (2 commits)
|
|
740
786
|
- [@Akatsuki-Ryu](https://github.com/Akatsuki-Ryu) (1 commit)
|
|
741
787
|
- [@AliceLJY](https://github.com/AliceLJY) (1 commit)
|
|
788
|
+
- [@chenjiyong](https://github.com/chenjiyong) (1 commit)
|
|
742
789
|
- [@JasonSuz](https://github.com/JasonSuz) (1 commit)
|
|
743
790
|
- [@Minidoracat](https://github.com/Minidoracat) (1 commit)
|
|
744
791
|
- [@furedericca-lab](https://github.com/furedericca-lab) (1 commit)
|
package/README_CN.md
CHANGED
|
@@ -584,6 +584,51 @@ LanceDB 表 `memories`:
|
|
|
584
584
|
|
|
585
585
|
请升级到 **memory-lancedb-pro >= 1.0.14**。插件已对这些字段统一做 `Number(...)` 转换后再参与运算(例如:计算分数、按时间排序)。
|
|
586
586
|
|
|
587
|
+
## AI Agent 铁律(Iron Rules)
|
|
588
|
+
|
|
589
|
+
> **OpenClaw 用户**:将下方代码块复制到你的 `AGENTS.md` 中,让 Agent 自动遵守这些规则。
|
|
590
|
+
|
|
591
|
+
```markdown
|
|
592
|
+
## Rule 1 — 双层记忆存储(铁律)
|
|
593
|
+
|
|
594
|
+
Every pitfall/lesson learned → IMMEDIATELY store TWO memories to LanceDB before moving on:
|
|
595
|
+
|
|
596
|
+
- **Technical layer**: Pitfall: [symptom]. Cause: [root cause]. Fix: [solution]. Prevention: [how to avoid]
|
|
597
|
+
(category: fact, importance ≥ 0.8)
|
|
598
|
+
- **Principle layer**: Decision principle ([tag]): [behavioral rule]. Trigger: [when it applies]. Action: [what to do]
|
|
599
|
+
(category: decision, importance ≥ 0.85)
|
|
600
|
+
- After each store, immediately `memory_recall` with anchor keywords to verify retrieval.
|
|
601
|
+
If not found, rewrite and re-store.
|
|
602
|
+
- Missing either layer = incomplete.
|
|
603
|
+
Do NOT proceed to next topic until both are stored and verified.
|
|
604
|
+
- Also update relevant SKILL.md files to prevent recurrence.
|
|
605
|
+
|
|
606
|
+
## Rule 2 — LanceDB 卫生
|
|
607
|
+
|
|
608
|
+
Entries must be short and atomic (< 500 chars). Never store raw conversation summaries, large blobs, or duplicates.
|
|
609
|
+
Prefer structured format with keywords for retrieval.
|
|
610
|
+
|
|
611
|
+
## Rule 3 — Recall before retry
|
|
612
|
+
|
|
613
|
+
On ANY tool failure, repeated error, or unexpected behavior, ALWAYS `memory_recall` with relevant keywords
|
|
614
|
+
(error message, tool name, symptom) BEFORE retrying. LanceDB likely already has the fix.
|
|
615
|
+
Blind retries waste time and repeat known mistakes.
|
|
616
|
+
|
|
617
|
+
## Rule 4 — 编辑前确认目标代码库
|
|
618
|
+
|
|
619
|
+
When working on memory plugins, confirm you are editing the intended package
|
|
620
|
+
(e.g., `memory-lancedb-pro` vs built-in `memory-lancedb`) before making changes;
|
|
621
|
+
use `memory_recall` + filesystem search to avoid patching the wrong repo.
|
|
622
|
+
|
|
623
|
+
## Rule 5 — 插件代码变更必须清 jiti 缓存(MANDATORY)
|
|
624
|
+
|
|
625
|
+
After modifying ANY `.ts` file under `plugins/`, MUST run `rm -rf /tmp/jiti/` BEFORE `openclaw gateway restart`.
|
|
626
|
+
jiti caches compiled TS; restart alone loads STALE code. This has caused silent bugs multiple times.
|
|
627
|
+
Config-only changes do NOT need cache clearing.
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
---
|
|
631
|
+
|
|
587
632
|
## 依赖
|
|
588
633
|
|
|
589
634
|
| 包 | 用途 |
|
|
@@ -607,12 +652,14 @@ LanceDB 表 `memories`:
|
|
|
607
652
|
<a href="https://github.com/furedericca-lab"><img src="https://avatars.githubusercontent.com/u/263020793?v=4" width="48" height="48" alt="@furedericca-lab" /></a>
|
|
608
653
|
<a href="https://github.com/joe2643"><img src="https://avatars.githubusercontent.com/u/19421931?v=4" width="48" height="48" alt="@joe2643" /></a>
|
|
609
654
|
<a href="https://github.com/AliceLJY"><img src="https://avatars.githubusercontent.com/u/136287420?v=4" width="48" height="48" alt="@AliceLJY" /></a>
|
|
655
|
+
<a href="https://github.com/chenjiyong"><img src="https://avatars.githubusercontent.com/u/8199522?v=4" width="48" height="48" alt="@chenjiyong" /></a>
|
|
610
656
|
</p>
|
|
611
657
|
|
|
612
658
|
- [@win4r](https://github.com/win4r)(3 次提交)
|
|
613
659
|
- [@kctony](https://github.com/kctony)(2 次提交)
|
|
614
660
|
- [@Akatsuki-Ryu](https://github.com/Akatsuki-Ryu)(1 次提交)
|
|
615
661
|
- [@AliceLJY](https://github.com/AliceLJY)(1 次提交)
|
|
662
|
+
- [@chenjiyong](https://github.com/chenjiyong)(1 次提交)
|
|
616
663
|
- [@JasonSuz](https://github.com/JasonSuz)(1 次提交)
|
|
617
664
|
- [@Minidoracat](https://github.com/Minidoracat)(1 次提交)
|
|
618
665
|
- [@furedericca-lab](https://github.com/furedericca-lab)(1 次提交)
|
package/index.ts
CHANGED
|
@@ -336,7 +336,7 @@ const memoryLanceDBProPlugin = {
|
|
|
336
336
|
const store = new MemoryStore({ dbPath: resolvedDbPath, vectorDim });
|
|
337
337
|
const embedder = createEmbedder({
|
|
338
338
|
provider: "openai-compatible",
|
|
339
|
-
apiKey:
|
|
339
|
+
apiKey: config.embedding.apiKey,
|
|
340
340
|
model: config.embedding.model || "text-embedding-3-small",
|
|
341
341
|
baseURL: config.embedding.baseURL,
|
|
342
342
|
dimensions: config.embedding.dimensions,
|
|
@@ -742,11 +742,14 @@ function parsePluginConfig(value: unknown): PluginConfig {
|
|
|
742
742
|
throw new Error("embedding config is required");
|
|
743
743
|
}
|
|
744
744
|
|
|
745
|
-
|
|
745
|
+
// Accept single key (string) or array of keys for round-robin rotation
|
|
746
|
+
const apiKey: string | string[] = typeof embedding.apiKey === "string"
|
|
746
747
|
? embedding.apiKey
|
|
747
|
-
:
|
|
748
|
+
: Array.isArray(embedding.apiKey) && embedding.apiKey.length > 0 && embedding.apiKey.every((k: unknown) => typeof k === "string")
|
|
749
|
+
? (embedding.apiKey as string[])
|
|
750
|
+
: process.env.OPENAI_API_KEY || "";
|
|
748
751
|
|
|
749
|
-
if (!apiKey) {
|
|
752
|
+
if (!apiKey || (Array.isArray(apiKey) && apiKey.length === 0)) {
|
|
750
753
|
throw new Error("embedding.apiKey is required (set directly or via OPENAI_API_KEY env var)");
|
|
751
754
|
}
|
|
752
755
|
|
package/openclaw.plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "memory-lancedb-pro",
|
|
3
3
|
"name": "Memory (LanceDB Pro)",
|
|
4
4
|
"description": "Enhanced LanceDB-backed long-term memory with hybrid retrieval, multi-scope isolation, long-context chunking, and management CLI",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.23",
|
|
6
6
|
"kind": "memory",
|
|
7
7
|
"configSchema": {
|
|
8
8
|
"type": "object",
|
|
@@ -17,7 +17,11 @@
|
|
|
17
17
|
"const": "openai-compatible"
|
|
18
18
|
},
|
|
19
19
|
"apiKey": {
|
|
20
|
-
"
|
|
20
|
+
"oneOf": [
|
|
21
|
+
{ "type": "string" },
|
|
22
|
+
{ "type": "array", "items": { "type": "string" }, "minItems": 1 }
|
|
23
|
+
],
|
|
24
|
+
"description": "Single API key or array of keys for round-robin rotation"
|
|
21
25
|
},
|
|
22
26
|
"model": {
|
|
23
27
|
"type": "string"
|
|
@@ -243,10 +247,10 @@
|
|
|
243
247
|
},
|
|
244
248
|
"uiHints": {
|
|
245
249
|
"embedding.apiKey": {
|
|
246
|
-
"label": "API Key",
|
|
250
|
+
"label": "API Key(s)",
|
|
247
251
|
"sensitive": true,
|
|
248
|
-
"placeholder": "sk-proj-... or
|
|
249
|
-
"help": "API key for
|
|
252
|
+
"placeholder": "sk-proj-... or [\"key1\", \"key2\"] for rotation",
|
|
253
|
+
"help": "Single API key or array of keys for round-robin rotation with automatic failover on rate limits (or use ${OPENAI_API_KEY}; use a dummy value for keyless local endpoints)"
|
|
250
254
|
},
|
|
251
255
|
"embedding.model": {
|
|
252
256
|
"label": "Embedding Model",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "memory-lancedb-pro",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.24",
|
|
4
4
|
"description": "OpenClaw enhanced LanceDB memory plugin with hybrid retrieval (Vector + BM25), cross-encoder rerank, multi-scope isolation, long-context chunking, and management CLI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.ts",
|
package/src/embedder.ts
CHANGED
|
@@ -85,7 +85,8 @@ class EmbeddingCache {
|
|
|
85
85
|
|
|
86
86
|
export interface EmbeddingConfig {
|
|
87
87
|
provider: "openai-compatible";
|
|
88
|
-
|
|
88
|
+
/** Single API key or array of keys for round-robin rotation with failover. */
|
|
89
|
+
apiKey: string | string[];
|
|
89
90
|
model: string;
|
|
90
91
|
baseURL?: string;
|
|
91
92
|
dimensions?: number;
|
|
@@ -151,7 +152,11 @@ export function getVectorDimensions(model: string, overrideDims?: number): numbe
|
|
|
151
152
|
// ============================================================================
|
|
152
153
|
|
|
153
154
|
export class Embedder {
|
|
154
|
-
|
|
155
|
+
/** Pool of OpenAI clients — one per API key for round-robin rotation. */
|
|
156
|
+
private clients: OpenAI[];
|
|
157
|
+
/** Round-robin index for client rotation. */
|
|
158
|
+
private _clientIndex: number = 0;
|
|
159
|
+
|
|
155
160
|
public readonly dimensions: number;
|
|
156
161
|
private readonly _cache: EmbeddingCache;
|
|
157
162
|
|
|
@@ -166,8 +171,9 @@ export class Embedder {
|
|
|
166
171
|
private readonly _autoChunk: boolean;
|
|
167
172
|
|
|
168
173
|
constructor(config: EmbeddingConfig & { chunking?: boolean }) {
|
|
169
|
-
//
|
|
170
|
-
const
|
|
174
|
+
// Normalize apiKey to array and resolve environment variables
|
|
175
|
+
const apiKeys = Array.isArray(config.apiKey) ? config.apiKey : [config.apiKey];
|
|
176
|
+
const resolvedKeys = apiKeys.map(k => resolveEnvVars(k));
|
|
171
177
|
|
|
172
178
|
this._model = config.model;
|
|
173
179
|
this._taskQuery = config.taskQuery;
|
|
@@ -177,15 +183,74 @@ export class Embedder {
|
|
|
177
183
|
// Enable auto-chunking by default for better handling of long documents
|
|
178
184
|
this._autoChunk = config.chunking !== false;
|
|
179
185
|
|
|
180
|
-
|
|
181
|
-
|
|
186
|
+
// Create a client pool — one OpenAI client per key
|
|
187
|
+
this.clients = resolvedKeys.map(key => new OpenAI({
|
|
188
|
+
apiKey: key,
|
|
182
189
|
...(config.baseURL ? { baseURL: config.baseURL } : {}),
|
|
183
|
-
});
|
|
190
|
+
}));
|
|
191
|
+
|
|
192
|
+
if (this.clients.length > 1) {
|
|
193
|
+
console.log(`[memory-lancedb-pro] Initialized ${this.clients.length} API keys for round-robin rotation`);
|
|
194
|
+
}
|
|
184
195
|
|
|
185
196
|
this.dimensions = getVectorDimensions(config.model, config.dimensions);
|
|
186
197
|
this._cache = new EmbeddingCache(256, 30); // 256 entries, 30 min TTL
|
|
187
198
|
}
|
|
188
199
|
|
|
200
|
+
// --------------------------------------------------------------------------
|
|
201
|
+
// Multi-key rotation helpers
|
|
202
|
+
// --------------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
/** Return the next client in round-robin order. */
|
|
205
|
+
private nextClient(): OpenAI {
|
|
206
|
+
const client = this.clients[this._clientIndex % this.clients.length];
|
|
207
|
+
this._clientIndex = (this._clientIndex + 1) % this.clients.length;
|
|
208
|
+
return client;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Check whether an error is a rate-limit / quota-exceeded error. */
|
|
212
|
+
private isRateLimitError(error: unknown): boolean {
|
|
213
|
+
// OpenAI SDK typed error
|
|
214
|
+
if (error && typeof error === "object" && "status" in error && (error as any).status === 429) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
218
|
+
return /rate.limit|quota|too many requests|insufficient.*credit|429/i.test(msg);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Call embeddings.create with automatic key rotation on rate-limit errors.
|
|
223
|
+
* Tries each key in the pool at most once before giving up.
|
|
224
|
+
*/
|
|
225
|
+
private async embedWithRetry(payload: any): Promise<any> {
|
|
226
|
+
const maxAttempts = this.clients.length;
|
|
227
|
+
let lastError: Error | undefined;
|
|
228
|
+
|
|
229
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
230
|
+
const client = this.nextClient();
|
|
231
|
+
try {
|
|
232
|
+
return await client.embeddings.create(payload);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (this.isRateLimitError(error) && attempt < maxAttempts - 1) {
|
|
235
|
+
console.log(
|
|
236
|
+
`[memory-lancedb-pro] API key ${attempt + 1}/${maxAttempts} hit rate limit, rotating to next key...`
|
|
237
|
+
);
|
|
238
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
// Non-rate-limit error or last attempt — let caller handle
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
throw lastError || new Error("All API keys exhausted (rate limited)");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Number of API keys in the rotation pool. */
|
|
250
|
+
get keyCount(): number {
|
|
251
|
+
return this.clients.length;
|
|
252
|
+
}
|
|
253
|
+
|
|
189
254
|
// --------------------------------------------------------------------------
|
|
190
255
|
// Backward-compatible API
|
|
191
256
|
// --------------------------------------------------------------------------
|
|
@@ -271,7 +336,7 @@ export class Embedder {
|
|
|
271
336
|
if (cached) return cached;
|
|
272
337
|
|
|
273
338
|
try {
|
|
274
|
-
const response = await this.
|
|
339
|
+
const response = await this.embedWithRetry(this.buildPayload(text, task));
|
|
275
340
|
const embedding = response.data[0]?.embedding as number[] | undefined;
|
|
276
341
|
if (!embedding) {
|
|
277
342
|
throw new Error("No embedding returned from provider");
|
|
@@ -361,8 +426,8 @@ export class Embedder {
|
|
|
361
426
|
}
|
|
362
427
|
|
|
363
428
|
try {
|
|
364
|
-
const response = await this.
|
|
365
|
-
this.buildPayload(validTexts, task)
|
|
429
|
+
const response = await this.embedWithRetry(
|
|
430
|
+
this.buildPayload(validTexts, task)
|
|
366
431
|
);
|
|
367
432
|
|
|
368
433
|
// Create result array with proper length
|
|
@@ -479,7 +544,10 @@ export class Embedder {
|
|
|
479
544
|
}
|
|
480
545
|
|
|
481
546
|
get cacheStats() {
|
|
482
|
-
return
|
|
547
|
+
return {
|
|
548
|
+
...this._cache.stats,
|
|
549
|
+
keyCount: this.clients.length,
|
|
550
|
+
};
|
|
483
551
|
}
|
|
484
552
|
}
|
|
485
553
|
|