mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(ai): resolve an OpenAI-compatible gateway as its own provider
The chain gains a seventh entry. It builds the OpenAI client against the base URL the user saved, with the bearer they saved for it and the model they named, and it reads none of the OpenAI columns — the pinned api.openai.com arm stays pinned. Host policy is the Local provider's, unchanged: a public host always, a private one only where the operator allowlisted it, which is what a LAN LiteLLM or vLLM needs. Egress keeps riding safeFetch. The JSON-mode dialect the Local client learns per endpoint moves into a shared module and now serves both clients, so a gateway that rejects response_format is detected once and retried without it. The entry stays out of the default chain: it can only resolve against a base URL somebody configured.
This commit is contained in:
1 parent
d11019aebc
commit
0c7059804b
13 files changed
+462
-64
No files matched your search
@@ -42,7 +42,13 @@ export const dynamic = "force-dynamic";
|
||||
const overrideSchema = z
|
||||
.object({
|
||||
provider: z
|
||||
.enum(["OPENAI", "ANTHROPIC", "LOCAL", "CHATGPT_OAUTH"])
|
||||
.enum([
|
||||
"OPENAI",
|
||||
"ANTHROPIC",
|
||||
"LOCAL",
|
||||
"OPENAI_COMPATIBLE",
|
||||
"CHATGPT_OAUTH",
|
||||
])
|
||||
.optional()
|
||||
.nullable(),
|
||||
model: z.string().min(1).max(120).optional().nullable(),
|
||||
@@ -50,6 +56,12 @@ const overrideSchema = z
|
||||
anthropicKey: z.string().min(1).max(500).optional().nullable(),
|
||||
localKey: z.string().min(1).max(500).optional().nullable(),
|
||||
openaiKey: z.string().min(1).max(500).optional().nullable(),
|
||||
// v1.33.1 (#470) — the OpenAI-compatible gateway's own fields, kept
|
||||
// separate from `baseUrl` / `openaiKey` so testing an unsaved gateway
|
||||
// config can never reach the pinned OpenAI arm and vice versa.
|
||||
compatBaseUrl: z.string().url().max(2048).optional().nullable(),
|
||||
compatKey: z.string().min(1).max(500).optional().nullable(),
|
||||
compatModel: z.string().min(1).max(120).optional().nullable(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
|
||||
@@ -10,7 +10,15 @@ import { annotate } from "@/lib/logging/context";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const ALLOWED = new Set(["OPENAI", "ANTHROPIC", "LOCAL", "CHATGPT_OAUTH"]);
|
||||
const ALLOWED = new Set([
|
||||
"OPENAI",
|
||||
"ANTHROPIC",
|
||||
"LOCAL",
|
||||
// v1.33.1 (#470) — the OpenAI-compatible gateway (LiteLLM / OpenRouter /
|
||||
// vLLM). Its base URL, key and model live in their own three columns.
|
||||
"OPENAI_COMPATIBLE",
|
||||
"CHATGPT_OAUTH",
|
||||
]);
|
||||
|
||||
export const GET = apiHandler(async () => {
|
||||
const { user } = await requireAuth();
|
||||
@@ -25,6 +33,10 @@ export const GET = apiHandler(async () => {
|
||||
aiAnthropicKeyEncrypted: true,
|
||||
aiLocalKeyEncrypted: true,
|
||||
aiOpenaiKeyEncrypted: true,
|
||||
// v1.33.1 (#470) — the OpenAI-compatible gateway.
|
||||
aiCompatBaseUrl: true,
|
||||
aiCompatKeyEncrypted: true,
|
||||
aiCompatModel: true,
|
||||
// v1.22 (#89)
|
||||
aiResponseTimeoutSeconds: true,
|
||||
},
|
||||
@@ -52,6 +64,11 @@ export const GET = apiHandler(async () => {
|
||||
openaiKeyPreview: u?.aiOpenaiKeyEncrypted
|
||||
? `...${decrypt(u.aiOpenaiKeyEncrypted).slice(-4)}`
|
||||
: null,
|
||||
// v1.33.1 (#470) — the gateway's own configuration. The key is reported
|
||||
// as presence only, like every other credential on this surface.
|
||||
compatBaseUrl: u?.aiCompatBaseUrl ?? null,
|
||||
compatModel: u?.aiCompatModel ?? null,
|
||||
hasCompatKey: Boolean(u?.aiCompatKeyEncrypted),
|
||||
// v1.22 (#89) — per-user response timeout, in seconds (null = default).
|
||||
responseTimeoutSeconds: u?.aiResponseTimeoutSeconds ?? null,
|
||||
});
|
||||
@@ -112,6 +129,46 @@ export const PATCH = apiHandler(async (request: NextRequest) => {
|
||||
}
|
||||
}
|
||||
|
||||
// ── v1.33.1 (#470) — the OpenAI-compatible gateway's three fields ──
|
||||
// Deliberately a separate block from `baseUrl` above: that column is shared
|
||||
// with LOCAL and is cleared when the provider switches away from it. This
|
||||
// one belongs to a single provider and to no other, which is what keeps the
|
||||
// OpenAI key from ever being sendable to a user-supplied host.
|
||||
if (body.compatBaseUrl !== undefined) {
|
||||
if (body.compatBaseUrl === null || body.compatBaseUrl === "") {
|
||||
updates.aiCompatBaseUrl = null;
|
||||
} else if (typeof body.compatBaseUrl === "string") {
|
||||
const trimmed = body.compatBaseUrl.trim();
|
||||
// Same SSRF floor as the Local provider: a public host always, a
|
||||
// private one only when the operator allowlisted it. Gateways on a LAN
|
||||
// are the normal case for LiteLLM / vLLM, so the escape hatch matters
|
||||
// here as much as it does for Ollama.
|
||||
if (!isLocalAiHostAllowed(trimmed) && !isPublicUrl(trimmed)) {
|
||||
return apiError(
|
||||
"Base URL points to an internal/private host. Ops must allow it on this instance via ALLOW_LOCAL_AI_PRIVATE_HOSTS — set it to the exact host (e.g. litellm.lan) or to true for any private host (intended for a self-hosted gateway).",
|
||||
422,
|
||||
);
|
||||
}
|
||||
updates.aiCompatBaseUrl = trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
if (body.compatModel !== undefined) {
|
||||
if (body.compatModel === null || body.compatModel === "") {
|
||||
updates.aiCompatModel = null;
|
||||
} else if (typeof body.compatModel === "string") {
|
||||
updates.aiCompatModel = body.compatModel.trim() || null;
|
||||
}
|
||||
}
|
||||
|
||||
if (body.compatKey !== undefined) {
|
||||
if (body.compatKey === null || body.compatKey === "") {
|
||||
updates.aiCompatKeyEncrypted = null;
|
||||
} else if (typeof body.compatKey === "string") {
|
||||
updates.aiCompatKeyEncrypted = encrypt(body.compatKey.trim());
|
||||
}
|
||||
}
|
||||
|
||||
if (body.anthropicKey !== undefined) {
|
||||
if (body.anthropicKey === null || body.anthropicKey === "") {
|
||||
updates.aiAnthropicKeyEncrypted = null;
|
||||
|
||||
Reference in new issue
Block a user