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)
main
Clawd 3 weeks ago
parent 1ae3e2a0b6
commit a02f3e9124

@ -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<Response> {
// 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(),

Loading…
Cancel
Save

Powered by TurnKey Linux.