Files
mem0/integrations/opencode-plugin/api-key.ts
kartik-mem0 71dc0cae07 fix(plugins): read the mem0 CLI key and ignore unexpanded host placeholders
The Python plugin core and the opencode, pi and deepseek plugins now fall back to the key `mem0 init` saves in ~/.mem0/config.json, so a configured CLI is enough to authenticate. The Python core also stops using or caching a literal ${api_key} left behind when a host (Cursor) does not expand its plugin variables, which is what kept asking for auth after the key was set.

Fixes #7346
2026-09-25 10:24:04 +05:30

32 lines
1.0 KiB
TypeScript

import {readFileSync} from "fs";
import {homedir} from "os";
import {join} from "path";
import {mem0CliApiKey} from "../agent-plugin-core/typescript/src/credentials.ts";
const PROFILE_FILES = [".zshrc", ".bashrc", ".zprofile", ".bash_profile", ".profile"];
export function parseApiKeyLine(line: string): string | undefined {
const match = line.match(/^\s*(?:export\s+)?MEM0_API_KEY=(.*)$/);
if (!match) return undefined;
const value = match[1].replace(/#.*$/, "").trim().replace(/^("|')(.*)\1$/, "$2").trim();
return value && !value.startsWith("$") ? value : undefined;
}
export function resolveApiKey(env: NodeJS.ProcessEnv = process.env, homeDir = homedir()): string {
const explicit = env.MEM0_API_KEY?.trim();
if (explicit) return explicit;
for (const profile of PROFILE_FILES) {
try {
for (const line of readFileSync(join(homeDir, profile), "utf8").split(/\r?\n/)) {
const key = parseApiKeyLine(line);
if (key) return key;
}
} catch {
}
}
return mem0CliApiKey(homeDir);
}