diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77fc70bf7..c1862eb71 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -219,6 +219,7 @@ jobs: - run: npm run test:navigation - run: npm run test:chat-filter - run: npm run test:bash + - run: npm run test:openai-compatible - run: npm run build:static python: diff --git a/CHANGELOG.md b/CHANGELOG.md index f92d3d631..5b3257d4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project are documented in this file. The format is b ### Added +- **Custom OpenAI-compatible preset in the LLM profile form** (#677): the format selector offers "Custom OpenAI-compatible", which selects the existing `openai` format and shows the expected base-URL shape (`https:///v1`) with a one-line hint. It is UI-only: no vendor host, no schema change, and nothing is prefilled, so an empty base URL keeps the documented public fallback. The preset is re-derived from the saved base URL when a profile reopens, and tests pin the base-URL normalization (bare `/v1`, trailing slash, a pasted `/chat/completions`, whitespace, path prefix, host:port). - **Opt-in SELinux overlay for bind mounts**: `docker-compose.selinux.yml` layers private `:Z` labels on `./data`, `./keys` and `./skills` for hosts with SELinux enforcing. The base file keeps its unlabelled mounts, so the overlay is opt-in and a shared `:z` label never lets another container read the encryption keys (#632). - **Browser back and forward across console views** (#641): page navigation is represented in query parameters (`tab`, `view`, `c`, `path`, and the inspector record parameters), so a user action pushes a history entry and back and forward restore the view. The native history API integrates with the Next router and a stable server snapshot keeps the static export working. Inspector payloads stay in memory and are never written to the URL or to browser storage. `npm run test:navigation` covers the history logic and now runs in CI. - **Remote approvals over a chat bridge** (#642): a bridged chat can now carry an operator approval as one-time buttons instead of waiting for the console. The nonce is stored as a SHA-256 hash in the new `chat_bridge_approvals` table (migration 0019), a button is consumed atomically so a double tap cannot approve twice, and a group chat only sees the approvals of its own bridge. diff --git a/agent/provider_openai_compatible_test.go b/agent/provider_openai_compatible_test.go new file mode 100644 index 000000000..a74483a86 --- /dev/null +++ b/agent/provider_openai_compatible_test.go @@ -0,0 +1,71 @@ +package agent + +import ( + "testing" + + "github.com/Autumn-27/norma/llm" +) + +// TestConfigFromOpenAICompatibleBaseURL locks the base-URL handling the +// "Custom OpenAI-compatible" profile preset relies on: a self-hosted or relayed +// endpoint is configured through the plain openai format, so ConfigFrom must +// keep a custom host untouched while tolerating the two shapes users paste from +// a provider's docs — a trailing slash and a full /chat/completions endpoint. +// The preset only pre-fills the form, so if this normalization regressed the +// preset would start producing dead profiles. +func TestConfigFromOpenAICompatibleBaseURL(t *testing.T) { + for _, tc := range []struct { + name string + in string + want string + }{ + {"bare /v1 base", "https://gw.example/v1", "https://gw.example/v1"}, + {"trailing slash", "https://gw.example/v1/", "https://gw.example/v1"}, + {"full endpoint pasted", "https://gw.example/v1/chat/completions", "https://gw.example/v1"}, + {"full endpoint with trailing slash", "https://gw.example/v1/chat/completions/", "https://gw.example/v1"}, + {"surrounding whitespace", " https://gw.example/v1 ", "https://gw.example/v1"}, + {"path-prefixed gateway", "https://gw.example/llm/openai/v1", "https://gw.example/llm/openai/v1"}, + {"host and port", "http://127.0.0.1:4000/v1", "http://127.0.0.1:4000/v1"}, + } { + t.Run(tc.name, func(t *testing.T) { + c := ConfigFrom("openai", "custom-model-name", tc.in, "sk-x", "") + if c.Format != llm.FormatOpenAI { + t.Fatalf("format=%v, want FormatOpenAI", c.Format) + } + if c.BaseURL != tc.want { + t.Fatalf("base_url=%q, want %q", c.BaseURL, tc.want) + } + if c.Model != "custom-model-name" { + t.Fatalf("model=%q, want the custom model preserved", c.Model) + } + if c.Provider() != "openai" { + t.Fatalf("Provider()=%q, want openai", c.Provider()) + } + if _, err := c.NewProvider(); err != nil { + t.Fatalf("NewProvider: %v", err) + } + }) + } +} + +// TestConfigFromOpenAIEmptyBaseURLKeepsPublicDefault pins the documented +// fallback the preset must not change: an openai profile saved without a base +// URL keeps today's behaviour and resolves against the SDK's public endpoint. +// Existing profiles depend on it, so the preset pre-fills a placeholder only +// and never writes a value. +// +// Note that this is also why the preset cannot by itself prevent a misconfigured +// self-hosted profile from reaching the public endpoint; see the follow-up filed +// against this behaviour. +func TestConfigFromOpenAIEmptyBaseURLKeepsPublicDefault(t *testing.T) { + c := ConfigFrom("openai", "custom-model-name", "", "sk-x", "") + if c.Format != llm.FormatOpenAI { + t.Fatalf("format=%v, want FormatOpenAI", c.Format) + } + if c.BaseURL != "" { + t.Fatalf("base_url=%q, want it left empty so the SDK default applies", c.BaseURL) + } + if c.Model != "custom-model-name" { + t.Fatalf("model=%q, want the custom model preserved", c.Model) + } +} diff --git a/web/package.json b/web/package.json index b44d0b06c..721a63da4 100644 --- a/web/package.json +++ b/web/package.json @@ -23,7 +23,8 @@ "test:sse": "node --test scripts/sse.test.mjs", "test:auth": "node --test scripts/auth.test.mjs", "test:navigation": "node --test src/lib/navigation.test.mjs", - "test:chat-filter": "node --test src/lib/chat-agent-filter.test.mjs" + "test:chat-filter": "node --test src/lib/chat-agent-filter.test.mjs", + "test:openai-compatible": "node --test src/lib/openai-compatible.test.mjs" }, "lint-staged": { "*.{js,ts,jsx,tsx}": [ diff --git a/web/src/app/(main)/system/llm/page.tsx b/web/src/app/(main)/system/llm/page.tsx index a64d92761..760084f8f 100644 --- a/web/src/app/(main)/system/llm/page.tsx +++ b/web/src/app/(main)/system/llm/page.tsx @@ -30,6 +30,11 @@ import { Switch } from "@/components/ui/switch"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { t } from "@/i18n"; import { api } from "@/lib/api"; +import { + OPENAI_COMPATIBLE_BASE_PLACEHOLDER, + OPENAI_COMPATIBLE_PRESET, + openAICompatiblePresetSelected, +} from "@/lib/openai-compatible"; import type { LLMPoolMember, LLMPoolStatus, LLMProfile, LLMRetryOverride } from "@/lib/types"; import { cn } from "@/lib/utils"; @@ -339,6 +344,10 @@ function ProfileSheet({ const isNew = !profile; const [name, setName] = React.useState(""); const [format, setFormat] = React.useState("anthropic"); + // The "Custom OpenAI-compatible" preset. UI-only: it selects the openai + // format and the expected base-URL shape, and is never stored, so it is + // re-derived from the saved base URL when a profile reopens. + const [compatPreset, setCompatPreset] = React.useState(false); const [model, setModel] = React.useState(""); const [baseUrl, setBaseUrl] = React.useState(""); const [proxy, setProxy] = React.useState(""); @@ -377,6 +386,7 @@ function ProfileSheet({ ? profile.format : "anthropic", ); + setCompatPreset(openAICompatiblePresetSelected(profile?.format, profile?.base_url)); setModel(profile?.model ?? ""); setBaseUrl(profile?.base_url ?? ""); setProxy(profile?.proxy ?? ""); @@ -540,6 +550,11 @@ function ProfileSheet({ } else if (keyHint) { keyPlaceholder = t("Set ({hint}); leave empty to keep it", { hint: keyHint }); } + if (compatPreset) { + // Shape only — no vendor host is prefilled, and an empty field still saves + // as an empty base URL. + basePlaceholder = OPENAI_COMPATIBLE_BASE_PLACEHOLDER; + } return ( @@ -576,7 +591,16 @@ function ProfileSheet({
- { + // The preset is not a wire format: it selects openai and is + // remembered only for the placeholder and the hint below. + const preset = v === OPENAI_COMPATIBLE_PRESET; + setCompatPreset(preset); + setFormat(preset ? "openai" : (v as ProfileFormat)); + }} + > @@ -584,6 +608,7 @@ function ProfileSheet({ Anthropic OpenAI (Chat Completions) OpenAI (Responses API) + Custom OpenAI-compatible {t("Ollama (local, no key needed)")} Bedrock gateway @@ -673,6 +698,13 @@ function ProfileSheet({ value={baseUrl} onChange={(e) => setBaseUrl(e.target.value)} /> + {compatPreset && ( +

+ End the base URL at /v1 — the provider appends{" "} + /chat/completions itself. A full endpoint URL is accepted and + trimmed. Left empty, this profile uses OpenAI's public endpoint rather than your own. +

+ )}
{format === "bedrock-gateway" && ( diff --git a/web/src/lib/openai-compatible.test.mjs b/web/src/lib/openai-compatible.test.mjs new file mode 100644 index 000000000..4235f297c --- /dev/null +++ b/web/src/lib/openai-compatible.test.mjs @@ -0,0 +1,55 @@ +import { + isCustomOpenAIBaseURL, + OPENAI_COMPATIBLE_BASE_PLACEHOLDER, + openAICompatiblePresetSelected, +} from "./openai-compatible.ts"; +import assert from "node:assert/strict"; +import test from "node:test"; + +test("isCustomOpenAIBaseURL treats a self-hosted or relayed host as custom", () => { + assert.equal(isCustomOpenAIBaseURL("https://gw.example/v1"), true); + assert.equal(isCustomOpenAIBaseURL("http://127.0.0.1:4000/v1"), true); + assert.equal(isCustomOpenAIBaseURL("https://gw.example/llm/openai/v1"), true); + assert.equal(isCustomOpenAIBaseURL(" https://gw.example/v1 "), true); +}); + +test("isCustomOpenAIBaseURL does not treat the public endpoint as custom", () => { + assert.equal(isCustomOpenAIBaseURL("https://api.openai.com/v1"), false); + assert.equal(isCustomOpenAIBaseURL("https://API.OpenAI.com/v1"), false); + assert.equal(isCustomOpenAIBaseURL("https://api.openai.com/v1/chat/completions"), false); +}); + +test("isCustomOpenAIBaseURL treats an empty base URL as the public fallback", () => { + assert.equal(isCustomOpenAIBaseURL(""), false); + assert.equal(isCustomOpenAIBaseURL(" "), false); + assert.equal(isCustomOpenAIBaseURL(null), false); + assert.equal(isCustomOpenAIBaseURL(undefined), false); +}); + +test("isCustomOpenAIBaseURL treats an unparseable non-empty value as custom", () => { + // Not the public endpoint, so the preset hint is the useful thing to show. + assert.equal(isCustomOpenAIBaseURL("gw.example/v1"), true); + assert.equal(isCustomOpenAIBaseURL("not a url"), true); +}); + +test("openAICompatiblePresetSelected re-derives the preset for a saved openai profile", () => { + assert.equal(openAICompatiblePresetSelected("openai", "https://gw.example/v1"), true); + assert.equal(openAICompatiblePresetSelected("openai", "https://api.openai.com/v1"), false); + assert.equal(openAICompatiblePresetSelected("openai", ""), false); +}); + +test("openAICompatiblePresetSelected leaves every other format alone", () => { + for (const format of ["anthropic", "openai-responses", "ollama", "bedrock-gateway", "", null, undefined]) { + assert.equal( + openAICompatiblePresetSelected(format, "https://gw.example/v1"), + false, + `format ${String(format)} should not select the preset`, + ); + } +}); + +test("the preset placeholder names no vendor and bakes in no endpoint", () => { + assert.equal(OPENAI_COMPATIBLE_BASE_PLACEHOLDER, "https:///v1"); + assert.ok(!OPENAI_COMPATIBLE_BASE_PLACEHOLDER.includes("openai.com")); + assert.ok(OPENAI_COMPATIBLE_BASE_PLACEHOLDER.endsWith("/v1")); +}); diff --git a/web/src/lib/openai-compatible.ts b/web/src/lib/openai-compatible.ts new file mode 100644 index 000000000..f500ef128 --- /dev/null +++ b/web/src/lib/openai-compatible.ts @@ -0,0 +1,55 @@ +// Helpers for the "Custom OpenAI-compatible" preset in the model profile form +// (System -> LLM). The preset is UI-only: it selects the existing `openai` +// format and shows the expected base-URL shape, so nothing about it is stored +// and no wire format or schema changes. Because it is not stored, reopening a +// saved profile re-derives it from the saved base URL. + +/** Value of the preset entry in the format selector. Never sent to the API. */ +export const OPENAI_COMPATIBLE_PRESET = "openai-compatible"; + +/** + * Base-URL shape shown as a placeholder for the preset. Deliberately a shape and + * not a vendor endpoint: no vendor name, no baked host, nothing prefilled into + * the value. + */ +export const OPENAI_COMPATIBLE_BASE_PLACEHOLDER = "https:///v1"; + +/** + * The endpoint the OpenAI SDK resolves against when a profile saves no base URL. + * An `openai` profile with an empty base URL keeps that behaviour, which is why + * the preset fills a placeholder rather than a value. + */ +export const PUBLIC_OPENAI_HOST = "api.openai.com"; + +/** + * isCustomOpenAIBaseURL reports whether raw addresses something other than + * OpenAI's public endpoint. + * + * An empty value is not custom: it is the documented fallback to the public + * endpoint. A non-empty value that does not parse as a URL is treated as custom, + * since it is certainly not the public endpoint — the form should then show the + * preset's hint rather than hide it behind a parse failure. + */ +export function isCustomOpenAIBaseURL(raw: string | null | undefined): boolean { + const s = (raw ?? "").trim(); + if (s === "") return false; + let host: string; + try { + host = new URL(s).hostname.toLowerCase(); + } catch { + return true; + } + return host !== PUBLIC_OPENAI_HOST; +} + +/** + * openAICompatiblePresetSelected reports whether a saved profile should reopen + * with the preset selected: the `openai` format pointed at a custom host. Any + * other format, and the public endpoint, show their own format entry. + */ +export function openAICompatiblePresetSelected( + format: string | null | undefined, + baseURL: string | null | undefined, +): boolean { + return format === "openai" && isCustomOpenAIBaseURL(baseURL); +}