fix(core): skip catalog entrypoints this build can't load (#59475)

This commit is contained in:
Aiden Cline
2026-10-26 22:01:16 -05:00
committed by GitHub
Unverified
parent 09b4275bc7
commit 8d26861fe8
2 changed files with 82 additions and 9 deletions
+20 -7
View File
@@ -164,6 +164,7 @@ const BUILDS_OWN_URL = new Set([
const normalize = Effect.fnUntraced(function* (catalog: Record<string, unknown>) {
const failed: { readonly model: string; readonly issue: unknown }[] = []
const unknown = new Set<string>()
const snapshots = Object.values(isV1(catalog) ? toV2(catalog) : catalog).flatMap((value) => {
const item = Option.getOrUndefined(decodeProvider(value))
if (!item) return []
@@ -173,7 +174,7 @@ const normalize = Effect.fnUntraced(function* (catalog: Record<string, unknown>)
if (Result.isFailure(decoded)) failed.push({ model: `${item.id}/${key}`, issue: decoded.failure })
const model = Result.isSuccess(decoded) ? decoded.success : undefined
// The fetch already asks for these types, but custom files and v1 catalogs may hold any type.
const target = model && TYPES.includes(model.type) && route(providerID, model)
const target = model && TYPES.includes(model.type) && route(providerID, model, unknown)
return model && target ? [{ model, route: target }] : []
})
if (sources.length === 0) return []
@@ -218,6 +219,10 @@ const normalize = Effect.fnUntraced(function* (catalog: Record<string, unknown>)
models: failed.slice(0, 10).map((item) => item.model),
issue: String(failed[0]?.issue),
})
if (unknown.size > 0)
yield* Effect.logDebug("Skipped models.dev APIs with entrypoints this build doesn't have", {
entrypoints: [...unknown],
})
return snapshots as readonly Snapshot[]
})
@@ -227,17 +232,17 @@ function entries<A>(record: Record<string, unknown> | undefined, decode: (input:
)
}
function route(providerID: Provider.ID, model: SourceModel): Route | undefined {
function route(providerID: Provider.ID, model: SourceModel, unknown: Set<string>): Route | undefined {
// A chat model uses its first API with a package. Other types (image, decision…) only run through
// opencode's own packages, so they use their first API that has one, and are skipped without one.
const [protocol, api] =
entries(model.api, decodeApi).find(([, entry]) =>
model.type === "chat" ? entry.sdk?.opencodeai || entry.sdk?.aisdk : entry.sdk?.opencodeai,
) ?? []
const [protocol, api, opencodeai] =
entries(model.api, decodeApi)
.map(([protocol, entry]) => [protocol, entry, loadable(entry.sdk?.opencodeai, unknown)] as const)
.find(([, entry, opencodeai]) => (model.type === "chat" ? opencodeai || entry.sdk?.aisdk : opencodeai)) ?? []
if (!protocol || !api) return undefined
const aisdk = api.sdk?.aisdk
const packageName =
api.sdk?.opencodeai ??
opencodeai ??
(aisdk &&
(AISDKNative.native(aisdk, {
providerID,
@@ -259,6 +264,14 @@ function route(providerID: Provider.ID, model: SourceModel): Route | undefined {
}
}
// Newer catalogs may name @opencode/ai entrypoints that older builds don't ship, so those count as no package.
function loadable(opencodeai: string | undefined, unknown: Set<string>) {
const name = Provider.packageName(opencodeai)
if (!name?.startsWith("@opencode/ai/") || Provider.builtinPackages.has(name)) return opencodeai
unknown.add(name)
return undefined
}
function mostCommon<T>(values: readonly T[]) {
const counts = new Map<T, number>()
for (const value of values) counts.set(value, (counts.get(value) ?? 0) + 1)
+62 -2
View File
@@ -352,7 +352,10 @@ describe("ModelsDev Service", () => {
},
images: {
base_url: "https://api.example.test/v1",
sdk: { aisdk: "@acme/ai-sdk-provider", opencodeai: "@opencode/ai/protocols/openai-images" },
sdk: {
aisdk: "@acme/ai-sdk-provider",
opencodeai: "@opencode/ai/providers/openai-compatible/images",
},
},
},
experimental: { modes: { fast: {} } },
@@ -378,7 +381,7 @@ describe("ModelsDev Service", () => {
["acme-image-fast", "image"],
])
expect(acme?.models[1]?.capabilities).toMatchObject({ input: ["text", "image", "pdf"], output: ["image"] })
expect(acme?.models[1]?.package).toBe("@opencode/ai/protocols/openai-images")
expect(acme?.models[1]?.package).toBe("@opencode/ai/providers/openai-compatible/images")
// Without an opencode package a non-chat model is left out ("acme-sdk-image" above).
}),
)
@@ -436,6 +439,63 @@ describe("ModelsDev Service", () => {
}),
)
it.effect("skips APIs whose opencode entrypoint this build doesn't have", () =>
Effect.gen(function* () {
const unknown = (path: string) => ({
base_url: "https://new.acme.test/v1",
sdk: { opencodeai: `@opencode/ai/providers/acme/${path}` },
})
const image = (id: string, api: Record<string, unknown>) =>
sourceModel(id, { type: "image", modalities: { input: ["text"], output: ["image/png"] }, api })
const [acme] = yield* ModelsDev.decodeSnapshot(
JSON.stringify({
acme: {
...fixture.acme,
models: {
next: sourceModel("next", {
api: { "new-api": unknown("new-api"), ...sourceModel("next").api },
}),
"sdk-fallback": sourceModel("sdk-fallback", {
api: {
"new-api": {
base_url: "https://claude.acme.test/v1",
sdk: { aisdk: "@ai-sdk/anthropic", opencodeai: "@opencode/ai/providers/acme/new-api" },
},
},
}),
"unknown-only": sourceModel("unknown-only", { api: { "new-api": unknown("new-api") } }),
"image-unknown": image("image-unknown", { images: unknown("images") }),
"image-known": image("image-known", {
images: unknown("images"),
"openai-images": {
base_url: "https://api.acme.test/v1",
sdk: { opencodeai: "@opencode/ai/providers/openai-compatible/images" },
},
}),
},
},
}),
)
const model = (id: string) => acme?.models.find((item) => item.id === id)
expect(acme?.models.map((item) => item.id)).toEqual(
["next", "sdk-fallback", "image-known"].map((id) => Model.ID.make(id)),
)
expect(acme?.info).toMatchObject({
package: "@opencode/ai/providers/openai-compatible",
settings: { baseURL: "https://api.example.test/v1" },
})
expect(model("next")).toMatchObject({ package: undefined, settings: { provider: "acme" } })
expect(model("sdk-fallback")).toMatchObject({
package: "@opencode/ai/providers/anthropic",
settings: { baseURL: "https://claude.acme.test/v1" },
})
expect(model("image-known")).toMatchObject({
package: "@opencode/ai/providers/openai-compatible/images",
settings: { baseURL: "https://api.acme.test/v1" },
})
}),
)
it.live("drops model types opencode doesn't keep", () =>
Effect.gen(function* () {
const cache = makeCache()