GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(core): discover models for configured OpenAI-compatible providers by Gr33ndev · Pull Request #57161 · anomalyco/opencode

feat(core): discover models for configured OpenAI-compatible providers #57161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Gr33ndev wants to merge 1 commit into anomalyco:dev
base: dev
Loading
Loading
from Gr33ndev:compatible-model-discovery
+605 −0
Open

feat(core): discover models for configured OpenAI-compatible providers #57161

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
e4569b4
feat(core): discover models for configured OpenAI-compatible providers
Gr33ndev Oct 19, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 packages/client/src/promise/generated/types.ts
Show comments
View file
Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

3 changes: 3 additions & 0 deletions 3 packages/core/src/plugin/internal.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import { McpCodeModeDefaultsPlugin } from "./mcp-codemode-defaults.js"
import { ProviderPlugins } from "./provider.js"
import { DynamicProviderPlugin } from "./provider/dynamic.js"
import { OpencodePlugin } from "./provider/opencode.js"
import { OpenAICompatiblePlugin } from "./provider/openai-compatible.js"
import { WebSearchPlugins } from "./websearch/index.js"
import { SkillPlugin } from "./skill.js"
import { McpSkillPlugin } from "./mcp-skill.js"
Expand Down Expand Up @@ -341,6 +342,8 @@ const post = [
ConfigProviderPlugin.Plugin,
// Run the dynamic npm SDK loader after SDK, instance, and external plugins register their `sdk` hooks.
DynamicProviderPlugin,
// Fills configured providers with discovered models, so it must see their config applied first.
OpenAICompatiblePlugin,
ConfigWebSearchPlugin.Plugin,
ConfigPolicyPlugin.Plugin,
] as const satisfies readonly InternalPlugin[]
Expand Down
178 changes: 178 additions & 0 deletions 178 packages/core/src/plugin/provider/openai-compatible.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { define } from "@opencode/plugin/effect/plugin"
import type { Document, Entry } from "@opencode/schema/config"
import { type Duration, Effect, Schedule, Schema, Semaphore, Stream } from "effect"
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
import { Bus } from "../../bus.js"
import { Config } from "../../config.js"
import { Credential } from "../../credential.js"
import { Flag } from "../../flag.js"
import { Model } from "../../model.js"
import { Provider } from "../../provider.js"
import type { PluginInternal } from "../internal.js"
import { foldSettings } from "./configured.js"

const Strings = Schema.NullOr(Schema.Array(Schema.String)).pipe(Schema.optional)

const RemoteModel = Schema.Struct({
id: Schema.String,
mode: Schema.NullOr(Schema.String).pipe(Schema.optional),
// Gateways report modalities at the top level or under `architecture`, as OpenRouter does.
input_modalities: Strings,
output_modalities: Strings,
architecture: Schema.NullOr(Schema.Struct({ input_modalities: Strings, output_modalities: Strings })).pipe(
Schema.optional,
),
supported_parameters: Strings,
})

const Models = Schema.Struct({ data: Schema.Array(RemoteModel) })

// Gateways such as LiteLLM also list embedding, image, and audio models.
const chat = new Set(["chat", "responses"])

const packages = new Set(["@ai-sdk/openai-compatible", "@opencode/ai/providers/openai-compatible"])

// These providers have dedicated discovery plugins that own their whole model list.
const dedicated = new Set(["ollama", "lmstudio", "vllm"])

type Target = ReturnType<typeof targets>[number]

export function make(interval: Duration.Input = "5 minutes") {
return define({
id: "opencode.provider.openai-compatible",
effect: Effect.fn(function* (ctx) {
const http = HttpClient.filterStatusOk(yield* HttpClient.HttpClient)
const bus = yield* Bus.Service
const config = yield* Config.Service
const flags = yield* Flag.Service
const loading = Semaphore.makeUnsafe(1)
const loaded = {
inventory: new Map<
Provider.ID,
{ source: string; models: readonly { id: string; capabilities: Model.Capabilities }[] }
>(),
hash: "[]",
}

yield* ctx.provider.transform((providers) => {
for (const [providerID, item] of loaded.inventory) {
const provider = providers.get(providerID)
if (!provider) continue
for (const model of item.models) {
// Configured models keep their own capabilities.
if (provider.models.has(Model.ID.make(model.id))) continue
providers.models.update(providerID, model.id, (draft) => {
draft.capabilities = {
tools: model.capabilities.tools,
input: [...model.capabilities.input],
output: [...model.capabilities.output],
}
})
}
}
})

const inventory = Effect.fnUntraced(function* (target: Target) {
const connection = yield* ctx.integration.connection.active(target.providerID)
const credential = connection
? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined))
: undefined
// Same precedence as requests: a stored key replaces the configured one.
const apiKey = credential?.type === "key" ? credential.key : target.apiKey
const source = JSON.stringify([target.baseURL, apiKey, target.headers])
const previous = loaded.inventory.get(target.providerID)
const request = HttpClientRequest.get(`${target.baseURL}/models`).pipe(
HttpClientRequest.acceptJson,
HttpClientRequest.setHeaders(target.headers ?? {}),
)
const models = yield* http.execute(apiKey ? request.pipe(HttpClientRequest.bearerToken(apiKey)) : request).pipe(
Effect.flatMap(HttpClientResponse.schemaBodyJson(Models)),
Effect.timeout("5 seconds"),
Effect.map((response) =>
response.data
.filter((model) => model.id.length > 0 && (!model.mode || chat.has(model.mode)))
.map((model) => ({ id: model.id, capabilities: capabilities(model) }))
.toSorted((a, b) => a.id.localeCompare(b.id)),
),
// Log the message only: the request in the cause carries configured headers, which may hold secrets.
Effect.tapError((error) =>
Effect.logDebug("openai-compatible model discovery failed", {
providerID: target.providerID,
message: error.message,
}),
),
// Keep the last successful inventory through transient outages while the endpoint and key are unchanged.
Effect.orElseSucceed(() => (previous?.source === source ? previous.models : undefined)),
)
return models === undefined ? [] : [[target.providerID, { source, models }] as const]
})

const load = Effect.fn("OpenAICompatiblePlugin.load")(function* () {
const entries = yield* config.entries()
// Internal plugins are not re-run on config changes, so read the flag on every refresh; turning it off drops
// the discovered models.
const results = flags.resolve(entries).openai_compatible_discovery.enabled
? yield* Effect.forEach(targets(entries), inventory, { concurrency: 4 })
: []
const next = new Map(results.flat())
const hash = JSON.stringify([...next])
if (hash === loaded.hash) return
loaded.inventory = next
loaded.hash = hash
yield* ctx.provider.reload()
})
const refresh = () => loading.withPermit(load())

yield* ctx.event.subscribe().pipe(
Stream.filter((event) => event.type === "config.updated"),
Stream.runForEach(refresh),
Effect.forkScoped({ startImmediately: true }),
)
yield* bus.subscribe(Credential.Event.Switched).pipe(
Stream.runForEach(refresh),
Effect.forkScoped({ startImmediately: true }),
)
yield* refresh().pipe(Effect.ignore, Effect.repeat(Schedule.spaced(interval)), Effect.forkScoped)
}),
} satisfies PluginInternal.InternalPlugin)
}

export const OpenAICompatiblePlugin = make()

function targets(entries: readonly Entry[]) {
const documents = entries.filter((entry): entry is Document => entry.type === "document")
const ids = new Set(documents.flatMap((file) => Object.keys(file.info.providers ?? {})))
return [...ids].flatMap((id) => {
if (dedicated.has(id)) return []
const configured = documents.flatMap((file) => file.info.providers?.[id] ?? [])
const pkg = configured.findLast((item) => item.package !== undefined)?.package
// Migrated V1 configs store the package as `aisdk:@ai-sdk/openai-compatible`.
if (pkg === undefined || !packages.has(Provider.packageName(pkg))) return []
const settings = foldSettings(entries, id, undefined)
const baseURL = typeof settings?.baseURL === "string" ? settings.baseURL.replace(/\/+$/, "") : ""
if (!URL.canParse(baseURL) || !["http:", "https:"].includes(new URL(baseURL).protocol)) return []
return [
{
providerID: Provider.ID.make(id),
baseURL,
apiKey: typeof settings?.apiKey === "string" && settings.apiKey !== "" ? settings.apiKey : undefined,
headers: configured.reduce(
(headers: Record<string, string> | undefined, item) => Provider.mergeHeaders(headers, item.headers),
undefined,
),
},
]
})
}

// Without metadata, assume text-only input: an image sent to a text-only model fails every later request in the
// session, while a missing image capability only blocks attachments.
function capabilities(model: typeof RemoteModel.Type): Model.Capabilities {
const input = model.input_modalities ?? model.architecture?.input_modalities ?? ["text"]
return {
tools: model.supported_parameters?.includes("tools") ?? true,
// OpenRouter calls PDF input `file`.
input: input.map((modality) => (modality === "file" ? "pdf" : modality)),
output: model.output_modalities ?? model.architecture?.output_modalities ?? ["text"],
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.