fix(providers): accept keyless connections for providers marked optional in the registry

keyless providers declared optional in the registry were rejected by the connect form and API
This commit is contained in:
Dizzle
2026-11-18 04:06:07 +01:00
parent 5340875be2
commit ac46104a48
8 changed files with 99 additions and 26 deletions

View File

@@ -0,0 +1 @@
- **fix(providers):** accept keyless connections for providers marked optional or none in the registry ([#20458](https://github.com/cheaper-inference/OmniRoute/pull/20458)) — thanks @maxmad64bis

View File

@@ -34,14 +34,19 @@ export async function validateKilocodeProvider({
// providerSpecificData.baseUrl is `unknown` (persisted JSON) — narrow it before
// handing it to normalizeBaseUrl(string). A malformed stored value (object, number)
// must fall back to the registry base URL, not crash the probe (#2463 class).
const kiloEntry = getRegistryEntry("kilocode");
const configuredBaseUrl =
normalizeBaseUrl(
typeof providerSpecificData.baseUrl === "string" ? providerSpecificData.baseUrl : ""
) ||
getRegistryEntry("kilocode")?.baseUrl ||
kiloEntry?.baseUrl ||
"https://api.kilo.ai/api/openrouter/chat/completions";
const headers = buildBearerHeaders(apiKey, providerSpecificData);
// Kilo serves its free models with the registry's anonymous bearer token when no key is
// configured — the same fallback DefaultExecutor uses. A supplied key always wins, and
// buildBearerHeaders still applies any connection-specific custom user agent.
const effectiveApiKey = apiKey || kiloEntry?.anonymousApiKey || "";
const headers = buildBearerHeaders(effectiveApiKey, providerSpecificData);
headers["Content-Type"] = "application/json";
headers["X-KILOCODE-EDITORNAME"] = "OmniRoute";

View File

@@ -17,6 +17,10 @@ import { UPSTREAM_PROXY_PROVIDERS } from "./providers/upstream-proxy";
import { CLOUD_AGENT_PROVIDERS } from "./providers/cloud-agent";
import { SYSTEM_PROVIDERS } from "./providers/system";
import { validateProviders } from "../validation/providerSchema";
import {
getCredentialRequirement,
worksWithoutCredential,
} from "../utils/providerCredentialRequirement";
export const FREE_PROVIDERS = {};
@@ -292,19 +296,9 @@ const EXPLICIT_OPTIONAL_APIKEY_PROVIDER_IDS = new Set([
"searxng-search",
"firecrawl",
"copilot-web",
"g4f-groq",
"g4f-gemini",
"g4f-pollinations",
"g4f-ollama",
"g4f-nvidia",
"huggingchat",
"gitlawb",
"gitlawb-gmi",
"naga-ac",
// UC (uncensored.com) persona: un-metered subscription chat with NO API key —
// auth is a durable Clerk credential stored in providerSpecificData, from which
// the executor mints a short-lived session token per connect.
"uc",
]);
export function providerAllowsOptionalApiKey(providerId: unknown): boolean {
@@ -314,6 +308,10 @@ export function providerAllowsOptionalApiKey(providerId: unknown): boolean {
// ponytail: any noAuth provider auto-qualifies — no per-provider maintenance
canonicalId in NOAUTH_PROVIDERS ||
providerId in NOAUTH_PROVIDERS ||
// The credential registry is the source of truth for what is user-facing:
// anything declared optional or none works without a pasted key.
worksWithoutCredential(getCredentialRequirement(canonicalId)) ||
worksWithoutCredential(getCredentialRequirement(providerId)) ||
EXPLICIT_OPTIONAL_APIKEY_PROVIDER_IDS.has(canonicalId) ||
EXPLICIT_OPTIONAL_APIKEY_PROVIDER_IDS.has(providerId) ||
isLocalProvider(canonicalId) ||

View File

@@ -65,9 +65,20 @@ test("cline and kilocode are registered in SPECIALTY_VALIDATORS for direct chat
assert.equal(resCline.valid, false);
assert.equal(resCline.error, "Provider and API key required");
const resKilo = await validateProviderApiKey({ provider: "kilocode", apiKey: "" });
assert.equal(resKilo.valid, false);
assert.equal(resKilo.error, "Provider and API key required");
// kilocode is marked optional in the registry, so an empty key reaches the
// specialized validator instead of the missing-credential guard. Probe the local
// fake upstream (not Kilo's public endpoint) to prove the dispatch and verdict:
// the validator sends the registry's anonymous bearer token, which the fake accepts.
chatRequests = 0;
const resKilo = await validateProviderApiKey({
provider: "kilocode",
apiKey: "",
providerSpecificData: { baseUrl },
});
assert.ok(chatRequests > 0, "the empty-key validator must dispatch to the chat route");
assert.equal(lastChatAuthorization, "Bearer anonymous");
assert.equal(resKilo.valid, true);
assert.equal(resKilo.error, null);
});
// ── Validator dispatch against a fake upstream (#16298) ─────────────────────
@@ -124,7 +135,9 @@ before(async () => {
} catch {
lastChatBody = null;
}
if (auth !== `Bearer ${VALID_KEY}`) {
// Kilo also serves its free tier with the registry's anonymous bearer token;
// accept it here so the keyless-validation dispatch can be tested locally.
if (auth !== `Bearer ${VALID_KEY}` && auth !== "Bearer anonymous") {
res.writeHead(401, { "content-type": "application/json" });
res.end(JSON.stringify({ error: "Unauthorized" }));
return;

View File

@@ -0,0 +1,43 @@
import assert from "node:assert/strict";
import test from "node:test";
import { providerAllowsOptionalApiKey } from "@/shared/constants/providers.ts";
import { listNoCredentialProviders } from "@/shared/utils/providerCredentialRequirement.ts";
test("providers usable without a credential from the registry allow a keyless connection", () => {
// These entries declare optional auth: anonymous access works and a key is accepted.
assert.equal(providerAllowsOptionalApiKey("ovhcloud"), true);
// The catalog alias must resolve to the same canonical id.
assert.equal(providerAllowsOptionalApiKey("ovh"), true);
assert.equal(providerAllowsOptionalApiKey("kilo-gateway"), true);
assert.equal(providerAllowsOptionalApiKey("pollinations"), true);
});
test("providers that require a credential still refuse a keyless connection", () => {
assert.equal(providerAllowsOptionalApiKey("groq"), false);
// Signing in is still a barrier: OAuth providers must not become keyless.
assert.equal(providerAllowsOptionalApiKey("agy"), false);
});
test("providers outside the registry keep their declared keyless behavior", () => {
assert.equal(providerAllowsOptionalApiKey("searxng-search"), true);
});
test("non-string and unknown input is refused", () => {
assert.equal(providerAllowsOptionalApiKey(null), false);
assert.equal(providerAllowsOptionalApiKey(42), false);
assert.equal(providerAllowsOptionalApiKey(""), false);
assert.equal(providerAllowsOptionalApiKey("definitely-not-a-provider"), false);
});
test("every provider listed as usable without a credential passes the connect gate", () => {
const ids = listNoCredentialProviders();
assert.ok(ids.length > 0);
for (const id of ids) {
assert.equal(
providerAllowsOptionalApiKey(id),
true,
`${id} is listed as usable without a credential but the connect gate refuses it`
);
}
});

View File

@@ -2,10 +2,10 @@ import test from "node:test";
import assert from "node:assert/strict";
import { providerAllowsOptionalApiKey } from "../../src/shared/constants/providers.js";
test("pollinations provider requires an API key and does not allow optional API key", () => {
test("pollinations allows an optional API key and accepts a keyless connection", () => {
assert.equal(
providerAllowsOptionalApiKey("pollinations"),
false,
"pollinations must require an API key because anonymous completions are no longer supported"
true,
"pollinations serves free models without a credential and accepts a key when one is provided"
);
});

View File

@@ -5,19 +5,19 @@ const { createProviderSchema, providersBatchTestSchema } =
await import("../../src/shared/validation/schemas.ts");
const { providerAllowsOptionalApiKey } = await import("../../src/shared/constants/providers.ts");
// #11117: Pollinations no longer serves anonymous requests (401 without a key),
// so it left EXPLICIT_OPTIONAL_APIKEY_PROVIDER_IDS — key is now required.
test("Pollinations requires an API key", () => {
assert.equal(providerAllowsOptionalApiKey("pollinations"), false);
// The registry declares optional auth for Pollinations, so a connection without
// a pasted key is valid and a key, when supplied, is still honoured.
test("Pollinations allows an optional API key", () => {
assert.equal(providerAllowsOptionalApiKey("pollinations"), true);
});
test("createProviderSchema rejects Pollinations without apiKey", () => {
test("createProviderSchema accepts Pollinations without apiKey", () => {
const result = createProviderSchema.safeParse({
provider: "pollinations",
name: "Pollinations",
});
assert.equal(result.success, false);
assert.equal(result.success, true);
});
test("providersBatchTestSchema accepts cloud-agent batch mode", () => {

View File

@@ -6,7 +6,7 @@ import assert from "node:assert/strict";
// behind `allowNoCredential === true`, so the route only threw a ReferenceError
// (HTTP 500) for that request shape; every other create path stayed green.
import { mkdtempSync } from "node:fs";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { settleConnectionFollowUpsAfterEachTest } from "../helpers/connectionFollowUps.ts";
@@ -21,6 +21,7 @@ const route = await import("../../src/app/api/providers/route.ts");
test.after(() => {
core.resetDbInstance();
rmSync(DATA_DIR, { recursive: true, force: true });
});
function post(body: Record<string, unknown>) {
@@ -50,3 +51,15 @@ test("allowNoCredential on a provider that allows an optional key passes the opt
const text = JSON.stringify(await res.json().catch(() => ({})));
assert.doesNotMatch(text, /does not allow a connection without a credential/);
});
test("allowNoCredential on a registry provider marked optional is accepted by the route", async () => {
const res = await post({
provider: "ovhcloud",
name: "no-cred-ovhcloud",
allowNoCredential: true,
});
assert.equal(res.status, 201, "an allowed keyless connection must be created with 201");
const body = (await res.json()) as { connection?: { provider?: string }; error?: unknown };
assert.equal(body.connection?.provider, "ovhcloud");
assert.doesNotMatch(JSON.stringify(body), /does not allow a connection without a credential/);
});