























@@ -228,6 +228,36 @@ export abstract class MemoryManagerEmbeddingOps extends MemoryManagerSyncOps {
228228.run(excess);
229229}
230230231+private upsertEmbeddingCacheEntries(
232+entries: Array<{ hash: string; embedding: number[] }>,
233+provider: { id: string; model: string } | null = this.provider,
234+): void {
235+upsertMemoryEmbeddingCache({
236+db: this.db,
237+enabled: this.cache.enabled,
238+ provider,
239+providerKey: this.providerKey,
240+ entries,
241+tableName: EMBEDDING_CACHE_TABLE,
242+});
243+if (this.embeddingCacheMirrorDb && this.embeddingCacheMirrorDb !== this.db) {
244+try {
245+upsertMemoryEmbeddingCache({
246+db: this.embeddingCacheMirrorDb,
247+enabled: this.cache.enabled,
248+ provider,
249+providerKey: this.providerKey,
250+ entries,
251+tableName: EMBEDDING_CACHE_TABLE,
252+});
253+} catch (err) {
254+log.warn("memory embeddings: failed to mirror safe-reindex cache batch", {
255+error: formatErrorMessage(err),
256+});
257+}
258+}
259+}
260+231261private async embedChunksInBatches(chunks: MemoryChunk[]): Promise<number[][]> {
232262if (chunks.length === 0) {
233263return [];
@@ -240,7 +270,6 @@ export abstract class MemoryManagerEmbeddingOps extends MemoryManagerSyncOps {
240270241271const missingChunks = missing.map((m) => m.chunk);
242272const batches = buildMemoryEmbeddingBatches(missingChunks, EMBEDDING_BATCH_MAX_TOKENS);
243-const toCache: Array<{ hash: string; embedding: number[] }> = [];
244273const provider = this.provider;
245274if (!provider) {
246275throw new Error("Cannot embed batch in FTS-only mode (no embedding provider)");
@@ -261,24 +290,18 @@ export abstract class MemoryManagerEmbeddingOps extends MemoryManagerSyncOps {
261290const batchEmbeddings = hasStructuredInputs
262291 ? await this.embedBatchInputsWithRetry(inputs)
263292 : await this.embedBatchWithRetry(batch.map((chunk) => chunk.text));
293+const batchCacheEntries: Array<{ hash: string; embedding: number[] }> = [];
264294for (let i = 0; i < batch.length; i += 1) {
265295const item = missing[cursor + i];
266296const embedding = batchEmbeddings[i] ?? [];
267297if (item) {
268298embeddings[item.index] = embedding;
269-toCache.push({ hash: item.chunk.hash, embedding });
299+batchCacheEntries.push({ hash: item.chunk.hash, embedding });
270300}
271301}
302+this.upsertEmbeddingCacheEntries(batchCacheEntries);
272303cursor += batch.length;
273304}
274-upsertMemoryEmbeddingCache({
275-db: this.db,
276-enabled: this.cache.enabled,
277-provider: this.provider,
278-providerKey: this.providerKey,
279-entries: toCache,
280-tableName: EMBEDDING_CACHE_TABLE,
281-});
282305return embeddings;
283306}
284307@@ -354,14 +377,7 @@ export abstract class MemoryManagerEmbeddingOps extends MemoryManagerSyncOps {
354377embeddings[item.index] = embedding;
355378toCache.push({ hash: item.chunk.hash, embedding });
356379}
357-upsertMemoryEmbeddingCache({
358-db: this.db,
359-enabled: this.cache.enabled,
360- provider,
361-providerKey: this.providerKey,
362-entries: toCache,
363-tableName: EMBEDDING_CACHE_TABLE,
364-});
380+this.upsertEmbeddingCacheEntries(toCache, provider);
365381return embeddings;
366382}
367383此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。