From 3546f61aabc9ffc9ef222cde53d03d337ade595a Mon Sep 17 00:00:00 2001 From: fiandev Date: Wed, 23 Sep 2026 22:50:42 +0700 Subject: [PATCH] Add custom base URL support for AI providers Allow configuring a custom OpenAI-compatible base URL for both Anthropic and OpenAI providers. URLs can be set per-provider and are validated (https/http only, max 2048 chars). Also render AskJournal AI responses as markdown. --- apps/web/src/app/api/settings/route.ts | 12 +++ apps/web/src/components/ai-settings.tsx | 35 +++++++ apps/web/src/components/ask-journal.tsx | 16 ++-- apps/web/src/lib/ai-settings.ts | 4 + apps/web/src/server/ai.ts | 7 +- apps/web/src/server/settings.ts | 29 ++++++ apps/web/tests/ai-settings.test.ts | 117 +++++++++++++++++++++++- 7 files changed, 210 insertions(+), 10 deletions(-) diff --git a/apps/web/src/app/api/settings/route.ts b/apps/web/src/app/api/settings/route.ts index cc0ce299..baf18419 100644 --- a/apps/web/src/app/api/settings/route.ts +++ b/apps/web/src/app/api/settings/route.ts @@ -9,6 +9,7 @@ import { aiModelSetting, getAiProvider, getAiSettings, + setAiBaseUrl, setAiKey, setSetting, } from "@/server/settings"; @@ -33,6 +34,8 @@ interface SettingsBody { openaiKey?: string | null; aiProvider?: AiProvider; aiModel?: string; + /** Custom OpenAI-compatible base URL. Blank clears it back to the provider default. */ + aiBaseUrl?: string; } export const PATCH = handler(async (request: Request) => { @@ -47,6 +50,14 @@ export const PATCH = handler(async (request: Request) => { /^[a-zA-Z0-9][a-zA-Z0-9._:/-]{0,199}$/.test(body.aiModel.trim()), "Enter a valid model ID.", ); + if (body.aiBaseUrl !== undefined) + requireValue( + typeof body.aiBaseUrl === "string" && + (body.aiBaseUrl.trim() === "" || + (body.aiBaseUrl.trim().length <= 2048 && + /^https?:\/\/\S+$/i.test(body.aiBaseUrl.trim()))), + "Enter a valid base URL.", + ); for (const id of AI_PROVIDERS) { const key = body[`${id}Key`]; if (key === undefined) continue; @@ -97,6 +108,7 @@ export const PATCH = handler(async (request: Request) => { } if (body.aiProvider !== undefined) setSetting("aiProvider", body.aiProvider); if (body.aiModel !== undefined) setSetting(aiModelSetting(provider), body.aiModel.trim()); + if (body.aiBaseUrl !== undefined) setAiBaseUrl(provider, body.aiBaseUrl.trim() || null); }); return ok({ saved: true }); }); diff --git a/apps/web/src/components/ai-settings.tsx b/apps/web/src/components/ai-settings.tsx index 8f9feca5..a206b1b0 100644 --- a/apps/web/src/components/ai-settings.tsx +++ b/apps/web/src/components/ai-settings.tsx @@ -5,6 +5,8 @@ import { AI_DEFAULT_MODELS, AI_PROVIDER_NAMES, AI_PROVIDERS, + DEFAULT_ANTHROPIC_API_BASE_URL, + DEFAULT_OPENAI_BASE_URL, type AiProvider, type AiSettingsPayload, } from "@/lib/ai-settings"; @@ -20,6 +22,7 @@ export function AiSettings() { const [provider, setProvider] = useState("anthropic"); const [model, setModel] = useState(AI_DEFAULT_MODELS.anthropic); const [apiKey, setApiKey] = useState(""); + const [baseUrl, setBaseUrl] = useState(""); const [busy, setBusy] = useState(false); const [failure, setFailure] = useState(""); const [saved, setSaved] = useState(""); @@ -28,6 +31,7 @@ export function AiSettings() { if (!data) return; setProvider(data.aiProvider); setModel(data.aiModel); + setBaseUrl(data.aiBaseUrl) }, [data]); const connection = data?.aiConnections[provider]; @@ -49,6 +53,7 @@ export function AiSettings() { : { aiProvider: provider, aiModel: model.trim(), + aiBaseUrl: baseUrl.trim(), ...(apiKey.trim() ? { [`${provider}Key`]: apiKey.trim() } : {}), }, "PATCH", @@ -90,6 +95,7 @@ export function AiSettings() { const next = value as AiProvider; setProvider(next); setModel(data?.aiConnections[next].model ?? AI_DEFAULT_MODELS[next]); + setBaseUrl(data?.aiConnections[next].baseUrl ?? ""); setApiKey(""); setSaved(""); setFailure(""); @@ -120,6 +126,35 @@ export function AiSettings() { Use a text model available to your provider account. Each provider keeps its own model and key.

+
+ + { + setBaseUrl(event.target.value); + setSaved(""); + }} + placeholder={ + connection?.configured + ? data?.aiBaseUrl + : provider === "anthropic" + ? DEFAULT_ANTHROPIC_API_BASE_URL + : DEFAULT_OPENAI_BASE_URL + } + autoComplete="off" + spellCheck={false} + /> +

+ {environment + ? `Using ${provider === "openai" ? "OPENAI_API_KEY" : "ANTHROPIC_API_KEY"} from the server environment. Change or remove that variable on the server to update the key.` + : connection?.configured + ? "Leave blank to keep your saved key, or enter a replacement." + : "Add your Base URL (support custom provider*), then save to use this provider."} +

+
(null); + const [response, setResponse] = useState<{ answer: string; scope: AiScope } | null>(null); const { run, busy, error, dismiss } = useAiRequest(); const [lastQuestion, setLastQuestion] = useState(""); @@ -33,7 +35,7 @@ function ScopedAskJournal({ filters, timeZone }: { filters: AnalysisFilters; tim if (busy || !q.trim()) return; q = q.trim(); setLastQuestion(q); - setAnswer(null); + setResponse(null); await run( () => postJson<{ answer: string; scope: AiScope }>("/api/ai/ask", { @@ -41,7 +43,7 @@ function ScopedAskJournal({ filters, timeZone }: { filters: AnalysisFilters; tim filters, timeZone, }), - setAnswer, + setResponse, ); }; @@ -90,10 +92,12 @@ function ScopedAskJournal({ filters, timeZone }: { filters: AnalysisFilters; tim {error && ( void ask(lastQuestion)} onDismiss={dismiss} /> )} - {answer && ( + {response && (
-

{answer.scope.label}

-

{answer.answer}

+

{response.scope.label}

+
+ {response.answer} +
)} diff --git a/apps/web/src/lib/ai-settings.ts b/apps/web/src/lib/ai-settings.ts index b274b4a0..58ab048b 100644 --- a/apps/web/src/lib/ai-settings.ts +++ b/apps/web/src/lib/ai-settings.ts @@ -1,6 +1,9 @@ export const AI_PROVIDERS = ["anthropic", "openai"] as const; export type AiProvider = (typeof AI_PROVIDERS)[number]; +export const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1"; +export const DEFAULT_ANTHROPIC_API_BASE_URL = "https://api.anthropic.com/v1"; + export const AI_DEFAULT_MODELS: Record = { anthropic: "claude-opus-5", openai: "gpt-4.1-mini", @@ -21,6 +24,7 @@ export interface AiSettingsPayload { aiProvider: AiProvider; aiConfigured: boolean; aiModel: string; + aiBaseUrl: string; aiConnections: Record; } diff --git a/apps/web/src/server/ai.ts b/apps/web/src/server/ai.ts index 0bd55654..e909968c 100644 --- a/apps/web/src/server/ai.ts +++ b/apps/web/src/server/ai.ts @@ -1,7 +1,7 @@ import { createAnthropic } from "@ai-sdk/anthropic"; import { createOpenAI } from "@ai-sdk/openai"; import { APICallError, RetryError, generateText } from "ai"; -import { getAiKey, getAiModel, getAiProvider } from "./settings"; +import { getAiBaseUrl, getAiKey, getAiModel, getAiProvider } from "./settings"; import { AI_PROVIDER_NAMES } from "@/lib/ai-settings"; /** @@ -27,12 +27,13 @@ export const runAi = async (prompt: string, maxOutputTokens = 1200): Promise (provider === "openai" ? process.env.OPENAI_API_KEY : process.env.ANTHROPIC_API_KEY)?.trim() || null; +export const aiBaseUrlEnvironment = (provider: AiProvider): string | null => + (provider === "openai" ? process.env.OPENAI_API_BASE_URL : process.env.ANTHROPIC_API_BASE_URL)?.trim() || + null; + /** Provider keys are stored separately and encrypted like broker credentials. */ export const getAiKey = (provider: AiProvider): string | null => { const environment = aiKeyEnvironment(provider); @@ -66,11 +72,30 @@ export const getAiKey = (provider: AiProvider): string | null => { } }; +export const getAiBaseUrl = (provider: AiProvider): string => { + const environment = aiBaseUrlEnvironment(provider); + if (environment) return environment; + const envelope = getSetting(`${provider}_baseUrl`); + + if (!envelope) { + return provider === "anthropic" ? + DEFAULT_ANTHROPIC_API_BASE_URL : + DEFAULT_OPENAI_BASE_URL; + } + + return envelope; +} + export const setAiKey = (provider: AiProvider, key: string | null): void => { if (key === null) deleteSetting(`${provider}KeyEnc`); else setSetting(`${provider}KeyEnc`, encryptJson(key.trim())); }; +export const setAiBaseUrl = (provider: AiProvider, url: string | null): void => { + if (url === null) deleteSetting(`${provider}_baseUrl`); + else setSetting(`${provider}_baseUrl`, url); +}; + export const getAnthropicKey = (): string | null => getAiKey("anthropic"); export const setAnthropicKey = (key: string | null): void => setAiKey("anthropic", key); @@ -89,6 +114,7 @@ export const getAiModel = (provider: AiProvider): string => export const getAiSettings = (): AiSettingsPayload => { const aiProvider = getAiProvider(); + const connection = (provider: AiProvider) => ({ configured: Boolean(getAiKey(provider)), source: aiKeyEnvironment(provider) @@ -96,13 +122,16 @@ export const getAiSettings = (): AiSettingsPayload => { : getAiKey(provider) ? ("saved" as const) : null, + baseUrl: getAiBaseUrl(provider), model: getAiModel(provider), }); + const aiConnections = { anthropic: connection("anthropic"), openai: connection("openai") }; return { aiProvider, aiConfigured: aiConnections[aiProvider].configured, aiModel: aiConnections[aiProvider].model, + aiBaseUrl: aiConnections[aiProvider].baseUrl, aiConnections, }; }; diff --git a/apps/web/tests/ai-settings.test.ts b/apps/web/tests/ai-settings.test.ts index 33901065..054f299b 100644 --- a/apps/web/tests/ai-settings.test.ts +++ b/apps/web/tests/ai-settings.test.ts @@ -7,8 +7,10 @@ const originalDir = process.env.JOURNAL_DATA_DIR; const scratch = mkdtempSync(join(tmpdir(), "journal-ai-test-")); process.env.JOURNAL_DATA_DIR = scratch; const { db, settings } = await import("../src/db"); -const { getAiKey, getAiModel, getAiProvider, setSetting, getSetting } = +const { getAiBaseUrl, getAiKey, getAiModel, getAiProvider, aiBaseUrlEnvironment, setAiBaseUrl, setSetting, getSetting } = await import("../src/server/settings"); +const { DEFAULT_ANTHROPIC_API_BASE_URL, DEFAULT_OPENAI_BASE_URL, AI_DEFAULT_MODELS } = + await import("../src/lib/ai-settings"); const { GET, PATCH } = await import("../src/app/api/settings/route"); const { GET: exportData } = await import("../src/app/api/export/route"); const { aiConfigured, runAi } = await import("../src/server/ai"); @@ -27,6 +29,8 @@ beforeEach(() => { db.delete(settings).run(); vi.stubEnv("ANTHROPIC_API_KEY", ""); vi.stubEnv("OPENAI_API_KEY", ""); + vi.stubEnv("ANTHROPIC_API_BASE_URL", ""); + vi.stubEnv("OPENAI_API_BASE_URL", ""); vi.stubEnv("JOURNAL_PASSWORD", ""); vi.stubGlobal( "fetch", @@ -171,6 +175,117 @@ describe("AI provider settings", () => { }); }); +describe("AI provider base URLs", () => { + it("defaults each provider to its own API base URL", async () => { + expect(aiBaseUrlEnvironment("anthropic")).toBeNull(); + expect(aiBaseUrlEnvironment("openai")).toBeNull(); + expect(getAiBaseUrl("anthropic")).toBe(DEFAULT_ANTHROPIC_API_BASE_URL); + expect(getAiBaseUrl("openai")).toBe(DEFAULT_OPENAI_BASE_URL); + expect(await state()).toMatchObject({ + aiProvider: "anthropic", + aiBaseUrl: DEFAULT_ANTHROPIC_API_BASE_URL, + aiConnections: { + anthropic: { baseUrl: DEFAULT_ANTHROPIC_API_BASE_URL }, + openai: { baseUrl: DEFAULT_OPENAI_BASE_URL }, + }, + }); + }); + + it("prefers the provider environment base URL over any saved value", async () => { + setAiBaseUrl("openai", "https://saved.example/v1"); + vi.stubEnv("OPENAI_API_BASE_URL", " https://env.example/v1 "); + expect(aiBaseUrlEnvironment("openai")).toBe("https://env.example/v1"); + expect(getAiBaseUrl("openai")).toBe("https://env.example/v1"); + expect(getAiBaseUrl("anthropic")).toBe(DEFAULT_ANTHROPIC_API_BASE_URL); + }); + + it("stores, isolates, and clears a custom base URL per provider", async () => { + await save({ + aiProvider: "openai", + openaiKey: "fixture-openai", + aiBaseUrl: "https://proxy.example/v1", + }); + expect(getAiBaseUrl("openai")).toBe("https://proxy.example/v1"); + expect(getAiBaseUrl("anthropic")).toBe(DEFAULT_ANTHROPIC_API_BASE_URL); + expect(await state()).toMatchObject({ + aiProvider: "openai", + aiBaseUrl: "https://proxy.example/v1", + aiConnections: { + openai: { baseUrl: "https://proxy.example/v1" }, + anthropic: { baseUrl: DEFAULT_ANTHROPIC_API_BASE_URL }, + }, + }); + expect((await save({ aiBaseUrl: " " })).status).toBe(200); + expect(getAiBaseUrl("openai")).toBe(DEFAULT_OPENAI_BASE_URL); + }); + + it("rejects malformed base URLs before writing any settings", async () => { + for (const aiBaseUrl of [ + "ftp://nope", + "not a url", + "https://", + 42, + `https://${"a".repeat(2049)}`, + ]) { + expect((await save({ aiBaseUrl })).status).toBe(400); + expect(db.select().from(settings).all()).toHaveLength(0); + } + }); + + it("routes OpenAI requests through a custom compatible base URL", async () => { + await save({ + aiProvider: "openai", + openaiKey: "fixture-openai", + aiBaseUrl: "https://proxy.example/v1", + }); + const fetcher = vi.fn(async () => + Response.json({ + id: "resp_fixture", + created_at: 1, + model: "gpt-4.1-mini", + status: "completed", + output: [ + { + type: "message", + id: "msg_fixture", + role: "assistant", + content: [{ type: "output_text", text: "Fixture reflection", annotations: [] }], + }, + ], + usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 }, + }), + ); + vi.stubGlobal("fetch", fetcher); + expect(await runAi("Fixture journal question", 700)).toBe("Fixture reflection"); + const [url] = (fetcher.mock.calls as unknown as [string, RequestInit][])[0]!; + expect(url).toBe("https://proxy.example/v1/responses"); + }); + + it("routes Anthropic requests through a custom compatible base URL", async () => { + await save({ + aiProvider: "anthropic", + anthropicKey: "fixture-anthropic", + aiBaseUrl: "https://proxy.example/v1", + }); + const fetcher = vi.fn(async () => + Response.json({ + id: "msg_fixture", + type: "message", + role: "assistant", + model: "claude-opus-5", + content: [{ type: "text", text: "Anthropic fixture" }], + stop_reason: "end_turn", + stop_sequence: null, + usage: { input_tokens: 10, output_tokens: 5 }, + }), + ); + vi.stubGlobal("fetch", fetcher); + expect(await runAi("Fixture prompt", 800)).toBe("Anthropic fixture"); + const [url] = (fetcher.mock.calls as unknown as [string, RequestInit][])[0]!; + expect(url).toBe("https://proxy.example/v1/messages"); + }); +}); + describe("AI provider requests through the real SDK adapters", () => { it("sends OpenAI's key and model to Responses with storage disabled", async () => { await save({