Skip to content
Draft
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ Create `~/.config/opencode/supermemory.jsonc`:
// API key (can also use SUPERMEMORY_API_KEY env var)
"apiKey": "sm_...",

// Supermemory API base URL (SUPERMEMORY_API_URL/SUPERMEMORY_BASE_URL env vars take precedence)
"baseUrl": "https://api.supermemory.ai",

// Min similarity for memory retrieval (0-1)
"similarityThreshold": 0.6,

Expand Down
31 changes: 30 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ const CONFIG_FILES = [
join(CONFIG_DIR, "supermemory.json"),
];

export const DEFAULT_BASE_URL = "https://api.supermemory.ai";

interface SupermemoryConfig {
apiKey?: string;
baseUrl?: string;
similarityThreshold?: number;
maxMemories?: number;
maxProjectMemories?: number;
Expand Down Expand Up @@ -44,7 +47,7 @@ const DEFAULT_KEYWORD_PATTERNS = [
"always\\s+remember",
];

const DEFAULTS: Required<Omit<SupermemoryConfig, "apiKey" | "userContainerTag" | "projectContainerTag">> = {
const DEFAULTS: Required<Omit<SupermemoryConfig, "apiKey" | "baseUrl" | "userContainerTag" | "projectContainerTag">> = {
similarityThreshold: 0.6,
maxMemories: 5,
maxProjectMemories: 10,
Expand Down Expand Up @@ -99,6 +102,32 @@ function getApiKey(): string | undefined {

export const SUPERMEMORY_API_KEY = getApiKey();

function normalizeBaseUrl(baseUrl: unknown): string | null {
if (typeof baseUrl !== "string" || !baseUrl.trim()) return null;

const trimmed = baseUrl.trim();
try {
const url = new URL(trimmed);
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
return trimmed;
} catch {
return null;
}
}

export function getBaseUrl(): string {
const configured =
process.env.SUPERMEMORY_API_URL ||
process.env.SUPERMEMORY_BASE_URL ||
fileConfig.baseUrl ||
DEFAULT_BASE_URL;
const normalized = normalizeBaseUrl(configured);
if (!normalized) {
throw new Error("Invalid baseUrl: expected an absolute http(s) URL");
}
return normalized;
}

export const CONFIG = {
similarityThreshold: fileConfig.similarityThreshold ?? DEFAULTS.similarityThreshold,
maxMemories: fileConfig.maxMemories ?? DEFAULTS.maxMemories,
Expand Down
4 changes: 2 additions & 2 deletions src/services/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Supermemory from "supermemory";
import { CONFIG, SUPERMEMORY_API_KEY, isConfigured } from "../config.js";
import { CONFIG, SUPERMEMORY_API_KEY, isConfigured, getBaseUrl } from "../config.js";
import { log } from "./logger.js";
import type {
ConversationIngestResponse,
Expand Down Expand Up @@ -52,7 +52,7 @@ export class SupermemoryClient {
if (!isConfigured()) {
throw new Error("SUPERMEMORY_API_KEY not set");
}
this.client = new Supermemory({ apiKey: SUPERMEMORY_API_KEY });
this.client = new Supermemory({ apiKey: SUPERMEMORY_API_KEY, baseURL: getBaseUrl() });
this.client.settings.update({
shouldLLMFilter: true,
filterPrompt: CONFIG.filterPrompt
Expand Down