Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/memos-local-openclaw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ const memosLocalPlugin = {

const store = new SqliteStore(ctx.config.storage!.dbPath!, ctx.log);
const embedder = new Embedder(ctx.config.embedding, ctx.log, ctx.openclawAPI);
store.configureVectorIndex?.(embedder.dimensions);
const worker = new IngestWorker(store, embedder, ctx);
const engine = new RecallEngine(store, embedder, ctx);
const evidenceTag = ctx.config.capture?.evidenceWrapperTag ?? DEFAULTS.evidenceWrapperTag;
Expand Down
1 change: 1 addition & 0 deletions apps/memos-local-openclaw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"posthog-node": "^5.28.0",
"puppeteer": "^24.38.0",
"semver": "^7.7.4",
"sqlite-vec": "^0.1.9",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions apps/memos-local-openclaw/scripts/native-binding.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@ function validateNativeBinding(bindingPath, loadBinding = defaultLoadBinding) {
}
}

function validateSqliteVecExtension(
sqliteVec,
createDatabase,
fsImpl = fs,
) {
if (!sqliteVec || typeof sqliteVec.getLoadablePath !== "function" || typeof sqliteVec.load !== "function") {
return { ok: false, reason: "missing", message: "sqlite-vec package is unavailable" };
}

let db;
try {
const extensionPath = sqliteVec.getLoadablePath();
if (!extensionPath || !fsImpl.existsSync(extensionPath)) {
return {
ok: false,
reason: "missing-platform-binary",
message: `sqlite-vec platform binary not found: ${extensionPath || "unknown"}`,
};
}

db = createDatabase();
sqliteVec.load(db);
const row = db.prepare("SELECT vec_version() AS version").get();
return { ok: true, reason: "ok", message: "", version: row?.version || "unknown" };
} catch (error) {
return { ok: false, reason: "load-error", message: errorMessage(error) };
} finally {
try { db?.close(); } catch { /* best effort */ }
}
}

function quarantineNativeBinding(
bindingPath,
fsImpl = fs,
Expand Down Expand Up @@ -74,4 +105,5 @@ module.exports = {
defaultLoadBinding,
quarantineNativeBinding,
validateNativeBinding,
validateSqliteVecExtension,
};
23 changes: 21 additions & 2 deletions apps/memos-local-openclaw/scripts/postinstall.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
const { spawnSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const { quarantineNativeBinding, validateNativeBinding } = require("./native-binding.cjs");
const {
quarantineNativeBinding,
validateNativeBinding,
validateSqliteVecExtension,
} = require("./native-binding.cjs");

const RESET = "\x1b[0m";
const GREEN = "\x1b[32m";
Expand Down Expand Up @@ -137,7 +141,7 @@ try {
function ensureDependencies() {
phase(0, "Check core dependencies / 检测核心依赖");

const coreDeps = ["@sinclair/typebox", "uuid", "@huggingface/transformers"];
const coreDeps = ["@sinclair/typebox", "uuid", "@huggingface/transformers", "sqlite-vec"];
const missing = [];
for (const dep of coreDeps) {
try {
Expand Down Expand Up @@ -482,6 +486,21 @@ ${YELLOW}${BOLD} ╚═══════════════════
}
}

try {
const SqliteDatabase = require("better-sqlite3");
const sqliteVec = require("sqlite-vec");
const status = validateSqliteVecExtension(sqliteVec, () => new SqliteDatabase(":memory:"));
if (status.ok) {
ok(`sqlite-vec is ready (v${status.version}).`);
} else {
warn(`sqlite-vec is unavailable (${status.reason}): ${status.message}`);
log("Vector search will use the built-in brute-force fallback.");
}
} catch (e) {
warn(`sqlite-vec validation failed: ${e.message}`);
log("Vector search will use the built-in brute-force fallback.");
}

/* ═══════════════════════════════════════════════════════════
* Phase 3: Interactive LAN Sharing Setup
* ═══════════════════════════════════════════════════════════ */
Expand Down
2 changes: 2 additions & 0 deletions apps/memos-local-openclaw/scripts/re-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export async function runReembed(opts: CliOpts): Promise<{ processed: number; fa
return { processed: 0, failed: 0, planned: ids.length };
}

store.configureVectorIndex(embedder.dimensions);

let processed = 0;
let failed = 0;
for (let i = 0; i < ids.length; i += opts.batchSize) {
Expand Down
1 change: 1 addition & 0 deletions apps/memos-local-openclaw/scripts/start-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async function main() {
},
log,
);
store.configureVectorIndex(embedder.dimensions);

const port = parseInt(process.env.VIEWER_PORT ?? "18799", 10);
const ctx = buildContext(dataDir, process.cwd(), undefined, log);
Expand Down
95 changes: 95 additions & 0 deletions apps/memos-local-openclaw/src/embedding/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { createHash } from "node:crypto";
import type { Logger } from "../types";

interface CacheEntry {
vector: number[];
timestamp: number;
}

export interface CacheOptions {
maxSize: number;
ttlMs: number;
now?: () => number;
}

export class EmbeddingCache {
private readonly cache = new Map<string, CacheEntry>();
private readonly now: () => number;

constructor(
private readonly options: CacheOptions,
private readonly log?: Logger,
) {
this.now = options.now ?? Date.now;
}

async get(signature: string, text: string): Promise<number[] | null> {
const key = this.cacheKey(signature, text);
const entry = this.cache.get(key);
if (!entry) return null;

if (this.now() - entry.timestamp >= this.options.ttlMs) {
this.cache.delete(key);
this.log?.debug(`[EmbeddingCache] Entry expired for key: ${key.slice(0, 16)}...`);
return null;
}

this.cache.delete(key);
this.cache.set(key, entry);
this.log?.debug(`[EmbeddingCache] Cache hit for key: ${key.slice(0, 16)}...`);
return [...entry.vector];
}

async set(signature: string, text: string, vector: number[]): Promise<void> {
if (this.options.maxSize <= 0 || this.options.ttlMs <= 0) return;

const key = this.cacheKey(signature, text);
this.cache.delete(key);
while (this.cache.size >= this.options.maxSize) {
const oldestKey = this.cache.keys().next().value as string | undefined;
if (!oldestKey) break;
this.cache.delete(oldestKey);
this.log?.debug(`[EmbeddingCache] Evicted LRU entry: ${oldestKey.slice(0, 16)}...`);
}

this.cache.set(key, { vector: [...vector], timestamp: this.now() });
this.log?.debug(`[EmbeddingCache] Cached embedding for key: ${key.slice(0, 16)}...`);
}

async has(signature: string, text: string): Promise<boolean> {
return (await this.get(signature, text)) !== null;
}

getStats(): { size: number; maxSize: number; ttlMs: number } {
return {
size: this.cache.size,
maxSize: this.options.maxSize,
ttlMs: this.options.ttlMs,
};
}

clear(): void {
this.cache.clear();
this.log?.debug("[EmbeddingCache] Cache cleared");
}

private cacheKey(signature: string, text: string): string {
return createHash("sha256").update(signature).update("\0").update(text).digest("hex");
}
}

export const DEFAULT_CACHE_OPTIONS: CacheOptions = {
maxSize: 1000,
ttlMs: 60 * 60 * 1000,
};

let globalCache: EmbeddingCache | null = null;

export function getGlobalCache(log?: Logger): EmbeddingCache {
if (!globalCache) globalCache = new EmbeddingCache(DEFAULT_CACHE_OPTIONS, log);
return globalCache;
}

export function resetGlobalCache(): void {
globalCache = null;
}
53 changes: 52 additions & 1 deletion apps/memos-local-openclaw/src/embedding/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,65 @@ import { embedGemini } from "./providers/gemini";
import { embedCohere, embedCohereQuery } from "./providers/cohere";
import { embedVoyage } from "./providers/voyage";
import { embedMistral } from "./providers/mistral";
import { embedOllama } from "./providers/ollama";
import { embedLocal } from "./local";
import { modelHealth } from "../ingest/providers";
import { EmbeddingCache, getGlobalCache } from "./cache";

type EmbeddingInputKind = "document" | "query";

export class Embedder {
private cache: EmbeddingCache;

constructor(
private cfg: EmbeddingConfig | undefined,
private log: Logger,
private openclawAPI?: OpenClawAPI,
) {}
) {
// Use global cache singleton to share cache across instances
this.cache = getGlobalCache(log);
}

/**
* Get embedding for query with caching support
*/
async embedQueryWithCache(text: string): Promise<number[]> {
const cached = await this.cache.get(this.signature, text);
if (cached) {
this.log.debug(`[Embedder] Cache hit for query: "${text.slice(0, 50)}..."`);
return cached;
}

// Generate embedding
const startTime = Date.now();
const vector = await this.embedQuery(text);
const duration = Date.now() - startTime;

if (vector.length === this.dimensions) {
await this.cache.set(this.signature, text, vector);
this.log.debug(`[Embedder] Cached embedding (${duration}ms) for query: "${text.slice(0, 50)}..."`);
} else {
this.log.debug(
`[Embedder] Skipped query cache because expected ${this.dimensions} dimensions but received ${vector.length}`,
);
}

return vector;
}

/**
* Clear embedding cache
*/
clearCache(): void {
this.cache.clear();
}

/**
* Get cache statistics
*/
getCacheStats(): { size: number; maxSize: number; ttlMs: number } {
return this.cache.getStats();
}

get provider(): string {
if (this.cfg?.provider === "openclaw" && this.cfg.capabilities?.hostEmbedding !== true) {
Expand All @@ -25,6 +73,7 @@ export class Embedder {

get model(): string {
if (this.provider === "local") return "";
if (this.provider === "ollama") return this.cfg?.model ?? "nomic-embed-text";
return this.cfg?.model ?? "";
}

Expand Down Expand Up @@ -92,6 +141,8 @@ export class Embedder {
result = await embedMistral(texts, cfg!, this.log); break;
case "voyage":
result = await embedVoyage(texts, cfg!, this.log); break;
case "ollama":
result = await embedOllama(texts, cfg!, this.log); break;
case "local":
default:
result = await embedLocal(texts, this.log); break;
Expand Down
44 changes: 44 additions & 0 deletions apps/memos-local-openclaw/src/embedding/providers/ollama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { EmbeddingConfig, Logger } from "../../types";

interface OllamaEmbedResponse {
embeddings?: unknown;
}

export async function embedOllama(
texts: string[],
cfg: EmbeddingConfig,
log: Logger,
): Promise<number[][]> {
if (texts.length === 0) return [];

const endpoint = cfg.endpoint ?? "http://localhost:11434";
const model = cfg.model ?? "nomic-embed-text";
const baseUrl = endpoint.replace(/\/+$/, "");
const url = baseUrl.endsWith("/api/embed") ? baseUrl : `${baseUrl}/api/embed`;

log.debug(`Calling Ollama embedding API for ${texts.length} texts`);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
...cfg.headers,
},
body: JSON.stringify({ model, input: texts }),
signal: AbortSignal.timeout(cfg.timeoutMs ?? 60_000),
});

if (!response.ok) {
const body = await response.text();
throw new Error(`Ollama embedding failed (${response.status}): ${body}`);
}

const payload = await response.json() as OllamaEmbedResponse;
if (!Array.isArray(payload.embeddings)
|| payload.embeddings.length !== texts.length
|| payload.embeddings.some((vector) => !Array.isArray(vector)
|| vector.some((value) => typeof value !== "number"))) {
throw new Error("Ollama embedding response has an invalid embeddings shape");
}

return payload.embeddings as number[][];
}
1 change: 1 addition & 0 deletions apps/memos-local-openclaw/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function initPlugin(opts: PluginInitOptions = {}): MemosLocalPlugin {

const store = new SqliteStore(ctx.config.storage!.dbPath!, ctx.log);
const embedder = new Embedder(ctx.config.embedding, ctx.log, ctx.openclawAPI);
store.configureVectorIndex?.(embedder.dimensions);
warnOnEmbeddingMismatch(store, embedder, ctx.log);
const worker = new IngestWorker(store, embedder, ctx);
const engine = new RecallEngine(store, embedder, ctx);
Expand Down
Loading
Loading