From a02f3e91248ca3239044de6de105ac9eabcb8bf6 Mon Sep 17 00:00:00 2001 From: Clawd Date: Fri, 6 Mar 2026 08:30:43 +0000 Subject: [PATCH] Support both /api suffix and non-suffix API URLs - Auto-detect if SECOND_BRAIN_API_URL ends with /api - Only add /api prefix when base URL doesn't have it - Works with: internal (192.168.1.16:8001), external (2brain.coer.nl), and external with /api suffix (2brain.coer.nl/api) --- src/index.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index d186e5a..53463a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,9 +19,13 @@ import * as fs from "fs"; import * as path from "path"; // Configuration - can be overridden via environment variables -// API_BASE_URL should point to the base (e.g., http://192.168.1.16:8001) -// The /api/v1 prefix is added by apiRequest -const API_BASE_URL = (process.env.SECOND_BRAIN_API_URL || "http://192.168.1.16:8001").replace(/\/+$/, ""); +// API_BASE_URL can be: +// - http://192.168.1.16:8001 (internal, /api/v1 prefix added) +// - https://2brain.coer.nl (external, /api/v1 prefix added) +// - https://2brain.coer.nl/api (external with /api, only /v1 added) +const RAW_API_URL = (process.env.SECOND_BRAIN_API_URL || "http://192.168.1.16:8001").replace(/\/+$/, ""); +const API_BASE_URL = RAW_API_URL.endsWith("/api") ? RAW_API_URL : RAW_API_URL; +const API_HAS_PREFIX = RAW_API_URL.endsWith("/api"); const API_USERNAME = process.env.SECOND_BRAIN_USERNAME || ""; const API_PASSWORD = process.env.SECOND_BRAIN_PASSWORD || ""; @@ -42,10 +46,19 @@ async function apiRequest( endpoint: string, options: RequestInit = {} ): Promise { - // Ensure endpoint starts with /api/v1 - const normalizedEndpoint = endpoint.startsWith("/api/v1") - ? endpoint - : `/api${endpoint.startsWith("/v1") ? endpoint : `/v1${endpoint}`}`; + // Normalize endpoint to /v1/... format + let normalizedEndpoint = endpoint; + if (endpoint.startsWith("/api/v1")) { + normalizedEndpoint = endpoint.substring(4); // Remove /api prefix + } else if (!endpoint.startsWith("/v1")) { + normalizedEndpoint = `/v1${endpoint}`; + } + + // Add /api prefix only if base URL doesn't already have it + if (!API_HAS_PREFIX) { + normalizedEndpoint = `/api${normalizedEndpoint}`; + } + const url = `${API_BASE_URL}${normalizedEndpoint}`; const headers = { ...getAuthHeader(),