diff --git a/src/llm.ts b/src/llm.ts index 21b74dd..44b7917 100644 --- a/src/llm.ts +++ b/src/llm.ts @@ -254,6 +254,11 @@ export type LlamaCppConfig = { * Env: CLAWMEM_LLM_NO_THINK */ remoteLlmNoThink?: boolean; + /** + * API key for remote LLM server. Sent as Bearer token. + * Env: CLAWMEM_LLM_API_KEY + */ + remoteLlmApiKey?: string; /** * Inactivity timeout in ms before unloading contexts (default: 2 minutes, 0 to disable). * @@ -313,6 +318,7 @@ export class LlamaCpp implements LLM { private remoteLlmModel: string; private remoteLlmReasoningEffort: string | null; private remoteLlmNoThink: boolean; + private remoteLlmApiKey: string | null; // Ensure we don't load the same model concurrently (which can allocate duplicate VRAM). private embedModelLoadPromise: Promise | null = null; @@ -349,6 +355,7 @@ export class LlamaCpp implements LLM { this.remoteLlmModel = normalizedRemoteLlmModel || "qwen3"; this.remoteLlmReasoningEffort = normalizeRemoteLlmReasoningEffort(config.remoteLlmReasoningEffort); this.remoteLlmNoThink = config.remoteLlmNoThink ?? true; + this.remoteLlmApiKey = config.remoteLlmApiKey || null; this.inactivityTimeoutMs = config.inactivityTimeoutMs ?? DEFAULT_INACTIVITY_TIMEOUT_MS; this.disposeModelsOnInactivity = config.disposeModelsOnInactivity ?? false; } @@ -1035,9 +1042,13 @@ export class LlamaCpp implements LLM { if (this.remoteLlmReasoningEffort) { body.reasoning_effort = this.remoteLlmReasoningEffort; } + const headers: Record = { "Content-Type": "application/json" }; + if (this.remoteLlmApiKey) { + headers["Authorization"] = `Bearer ${this.remoteLlmApiKey}`; + } const resp = await fetch(buildRemoteChatCompletionsUrl(this.remoteLlmUrl!), { method: "POST", - headers: { "Content-Type": "application/json" }, + headers, body: JSON.stringify(body), signal, }); @@ -1374,6 +1385,7 @@ export function getDefaultLlamaCpp(): LlamaCpp { if (!raw) return undefined; return !["0", "false", "no", "off"].includes(raw); })(), + remoteLlmApiKey: process.env.CLAWMEM_LLM_API_KEY || undefined, }); } return defaultLlamaCpp; diff --git a/src/openclaw/index.ts b/src/openclaw/index.ts index 7b1f7a9..2390baf 100644 --- a/src/openclaw/index.ts +++ b/src/openclaw/index.ts @@ -114,6 +114,9 @@ const clawmemPlugin = { ...(pluginCfg.gpuLlmNoThink !== undefined ? { CLAWMEM_LLM_NO_THINK: String(pluginCfg.gpuLlmNoThink) } : {}), + ...(pluginCfg.gpuLlmApiKey + ? { CLAWMEM_LLM_API_KEY: pluginCfg.gpuLlmApiKey as string } + : {}), ...(pluginCfg.gpuRerank ? { CLAWMEM_RERANK_URL: pluginCfg.gpuRerank as string } : {}), CLAWMEM_PROFILE: profile, },