Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

5.6 KB
Raw Blame
// Regression test for #6457 — huggingface/stabilityai/stable-diffusion-xl-base-1.0
// (an image/diffusion model) was listed as a CHAT model in GET /v1/models, so firing
// POST /v1/chat/completions with it hit the upstream and returned a raw HuggingFace
// "[400] The requested model '...' is not a chat model." error.
//
// Root cause: the synced-provider-models loop in catalog.ts (fed by live discovery —
// e.g. HuggingFace's own `/v1/models`) defaults a model's `endpoints` to `["chat"]`
// whenever the upstream discovery payload carries no modality/endpoint info, which is
// exactly what HuggingFace's live catalog returns for image models. That produced a
// second, bogus chat-typed entry for the SAME id already correctly listed with
// `type: "image"` by the imageRegistry loop — and catalogDedupe.ts keys on
// (id, type, subtype), so the two distinct-`type` entries both survived.
//
// Fix: skip an exact-provider registered image model from the chat-catalog loop only
// when synced metadata does not explicitly advertise `chat` or `responses`. The image
// registry loop still adds the correctly typed image entry, while multi-capability
// models keep both entries.

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-image-chat-6457-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "catalog-test-secret-6457";

const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const v1ModelsCatalog = await import("../../src/app/api/v1/models/catalog.ts");

async function resetStorage() {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}

test.beforeEach(async () => {
  v1ModelsCatalog.__resetCatalogBuilderRunsForTest();
  await resetStorage();
});

test.after(async () => {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});

async function seedProviderConnection(provider: string) {
  return providersDb.createProviderConnection({
    provider,
    authType: "apikey",
    name: `${provider}-${Math.random().toString(16).slice(2, 8)}`,
    apiKey: `${provider}-key`,
    isActive: true,
    testStatus: "active",
  });
}

test("#6457 image/diffusion model discovered via live sync is NOT listed as a chat model", async () => {
  const connection = await seedProviderConnection("huggingface");

  // Simulate what HuggingFace's live `/v1/models` discovery persists for an
  // image/diffusion model: no supportedEndpoints/modality info at all — the exact
  // upstream shape that made the synced-models loop default to `["chat"]`.
  await modelsDb.replaceSyncedAvailableModelsForConnection("huggingface", connection.id, [
    { id: "stabilityai/stable-diffusion-xl-base-1.0", name: "Stable Diffusion XL (HF)" },
    { id: "meta-llama/llama-3.1-8b-instruct", name: "Llama 3.1 8B" },
  ]);

  const response = await v1ModelsCatalog.getUnifiedModelsResponse(
    new Request("http://localhost/api/v1/models")
  );
  assert.equal(response.status, 200);

  const body = (await response.json()) as {
    data: Array<{ id: string; type?: string }>;
  };

  const imageModelEntries = body.data.filter((m) =>
    m.id.includes("stabilityai/stable-diffusion-xl-base-1.0")
  );

  assert.ok(imageModelEntries.length > 0, "the image model must still be listed somewhere");
  for (const entry of imageModelEntries) {
    assert.equal(
      entry.type,
      "image",
      `every listing of the diffusion model must be type:"image", got ${JSON.stringify(entry)}`
    );
  }

  // A real chat model synced alongside it must still be listed as chat (no `type`,
  // per the OpenAI-compatible convention used throughout this catalog).
  const chatModelEntries = body.data.filter((m) =>
    m.id.includes("meta-llama/llama-3.1-8b-instruct")
  );
  assert.ok(chatModelEntries.length > 0, "the real chat model must still be listed");
  for (const entry of chatModelEntries) {
    assert.equal(entry.type, undefined, "the real chat model must not carry a non-chat type");
  }
});

test("Codex Responses and image entries keep distinct public IDs for the same upstream model", async () => {
  const connection = await seedProviderConnection("codex");

  await modelsDb.replaceSyncedAvailableModelsForConnection("codex", connection.id, [
    {
      id: "gpt-5.6-sol",
      name: "GPT 5.6 Sol",
      supportedEndpoints: ["responses"],
    },
  ]);

  const response = await v1ModelsCatalog.getUnifiedModelsResponse(
    new Request("http://localhost/api/v1/models?prefix=alias")
  );
  assert.equal(response.status, 200);

  const body = (await response.json()) as {
    data: Array<{ id: string; type?: string; supported_endpoints?: string[] }>;
  };
  const entries = body.data.filter((model) => model.id.endsWith("/gpt-5.6-sol"));

  assert.ok(
    entries.some(
      (model) => model.type !== "image" && model.supported_endpoints?.includes("responses")
    ),
    "explicit responses support must keep the synced chat entry"
  );
  assert.ok(
    entries.every((model) => model.type !== "image"),
    "an image entry must not shadow the Responses model ID"
  );
  assert.ok(
    body.data.some((model) => model.id === "codex/gpt-5.6-sol-image" && model.type === "image"),
    "the image surface must remain available under its distinct catalog ID"
  );
});