From 92455642744c26a00a4345156bca1d9e7836e2ae Mon Sep 17 00:00:00 2001 From: Sreeram Sreedhar Date: Mon, 1 Jun 2026 21:25:03 -0400 Subject: [PATCH] add baseUrl to config --- README.md | 2 ++ src/config.ts | 28 ++++++++++++++++++++++++++++ src/services/client.ts | 12 +++++------- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7ab7b0d..b2c5a0b 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ anything else fails, they exit cleanly without breaking your Codex session. | Variable | Purpose | | ------------------------------ | ------------------------------------------------------ | | `SUPERMEMORY_CODEX_API_KEY` | Your Supermemory API key (browser auth is preferred). | +| `SUPERMEMORY_API_URL` | Override the Supermemory API base URL (takes precedence over config). | | `SUPERMEMORY_DEBUG` | Set to any truthy value to enable debug logging to `~/.codex-supermemory.log`. | ### `~/.codex/supermemory.json` (optional) @@ -86,6 +87,7 @@ Drop this file in to override defaults: | Key | Type | Default | Description | | ------------------------ | ---------- | -------------- | -------------------------------------------------------------------------------------------- | | `apiKey` | `string` | — | API key (env var takes precedence, browser auth is preferred). | +| `baseUrl` | `string` | `https://api.supermemory.ai` | Supermemory API base URL (`SUPERMEMORY_API_URL`/`SUPERMEMORY_BASE_URL` env vars take precedence). | | `similarityThreshold` | `number` | `0.6` | Minimum similarity score for retrieved memories. | | `maxMemories` | `number` | `5` | Max memories injected per prompt. | | `maxProfileItems` | `number` | `5` | Max profile items considered. | diff --git a/src/config.ts b/src/config.ts index dfcf5d6..8c91885 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,6 +5,7 @@ import { loadCredentials } from "./services/auth.js"; export const CONFIG_FILE = join(homedir(), ".codex", "supermemory.json"); export const PLUGIN_VERSION = "1.0.7"; +export const DEFAULT_BASE_URL = "https://api.supermemory.ai"; export interface CustomContainer { tag: string; @@ -13,6 +14,7 @@ export interface CustomContainer { interface CodexSupermemoryConfig { apiKey?: string; + baseUrl?: string; similarityThreshold?: number; maxMemories?: number; maxProfileItems?: number; @@ -132,6 +134,32 @@ export function getApiKeyValue(): string | undefined { return SUPERMEMORY_API_KEY; } +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 function getSignalConfig(): { enabled: boolean; keywords: string[]; diff --git a/src/services/client.ts b/src/services/client.ts index c8f4eff..8435468 100644 --- a/src/services/client.ts +++ b/src/services/client.ts @@ -1,14 +1,10 @@ import Supermemory from "supermemory"; -import { CONFIG, isConfigured, getApiKeyValue, PLUGIN_VERSION } from "../config.js"; +import { CONFIG, isConfigured, getApiKeyValue, getBaseUrl, PLUGIN_VERSION } from "../config.js"; import { log } from "./logger.js"; import type { MemoryType } from "../types/index.js"; const TIMEOUT_MS = 30000; const SPACE_NAME_TIMEOUT_MS = 5000; -const API_URL = - process.env.SUPERMEMORY_API_URL || - process.env.SUPERMEMORY_BASE_URL || - "https://api.supermemory.ai"; const CODEX_SOURCE = "codex"; function withTimeout(promise: Promise, ms: number): Promise { @@ -74,6 +70,7 @@ export class SupermemoryClient { // writes to the Codex plugin in PostHog / `document.source`. this.client = new Supermemory({ apiKey: getApiKeyValue(), + baseURL: getBaseUrl(), defaultHeaders: { "x-sm-source": CODEX_SOURCE }, }); } @@ -237,8 +234,9 @@ export class SupermemoryClient { async updateContainerTagName(containerTag: string, name: string) { log("updateContainerTagName: start", { containerTag, name }); try { + const baseUrl = getBaseUrl(); const currentResponse = await withTimeout( - fetch(`${API_URL}/v3/container-tags/${encodeURIComponent(containerTag)}`, { + fetch(`${baseUrl}/v3/container-tags/${encodeURIComponent(containerTag)}`, { headers: { Authorization: `Bearer ${getApiKeyValue()}`, }, @@ -270,7 +268,7 @@ export class SupermemoryClient { } const response = await withTimeout( - fetch(`${API_URL}/v3/container-tags/${encodeURIComponent(containerTag)}`, { + fetch(`${baseUrl}/v3/container-tags/${encodeURIComponent(containerTag)}`, { method: "PATCH", headers: { Authorization: `Bearer ${getApiKeyValue()}`,