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 — refactor(core): resolve configured model variants once across config layers by rekram1-node · Pull Request #60425 · anomalyco/opencode

refactor(core): resolve configured model variants once across config layers #60425

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

Closed
rekram1-node wants to merge 4 commits into dev from model-reasoning-variants

Uh oh!

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

Closed

refactor(core): resolve configured model variants once across config layers #60425

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
a4c37ef
refactor(core): retain model reasoning capabilities and resolve confi…
rekram1-node Oct 30, 2026
ae3954f
refactor(core): keep Model.Capabilities untouched and map reasoning o…
rekram1-node Oct 30, 2026
0dbed3a
refactor(core): build configured models in provider.transform and re-…
rekram1-node Oct 30, 2026
fe9ef80
fix(core): clear prior model config layers on canonical switch
rekram1-node Oct 30, 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

255 changes: 124 additions & 131 deletions 255 packages/core/src/config/plugin/provider.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * as ConfigProviderPlugin from "./provider.js"

import type { ModelEditor } from "@opencode/plugin/effect/model"
import type { ProviderEditor } from "@opencode/plugin/effect/provider"
import type { ProviderEditor, ProviderRecord } from "@opencode/plugin/effect/provider"
import { define } from "@opencode/plugin/effect/plugin"
import { Document, type Entry } from "@opencode/schema/config"
import { ConfigProvider } from "@opencode/schema/config/provider"
Expand All @@ -15,6 +15,8 @@ import { ConfigIntegration } from "../integration.js"
import { ConfigManaged } from "../managed.js"
import { ConfigEntryObserver } from "./entry-observer.js"

type ModelConfig = NonNullable<ConfigProvider.Info["models"]>[string]

export const Plugin = define({
id: "opencode.config.provider",
effect: Effect.fn(function* (ctx) {
Expand All @@ -25,114 +27,108 @@ export const Plugin = define({
ctx.integration.reload().pipe(Effect.andThen(ctx.provider.reload())),
)
yield* ctx.integration.transform((integrations) => {
for (const [id, provider] of configuredProviders(loaded.entries)) {
const integrationID = id
if (!integrations.get(integrationID)) {
integrations.method.update({
integrationID,
method: { type: "key", label: "Manually enter API Key" },
})
}
for (const [integrationID, provider] of configuredProviders(loaded.entries)) {
if (!integrations.get(integrationID))
integrations.method.update({ integrationID, method: { type: "key", label: "Manually enter API Key" } })
integrations.update(integrationID, (integration) => {
integration.name = provider.name || integration.name
})
if (provider.env !== undefined) {
integrations.method.update({
integrationID,
method: { type: "env", names: [...provider.env] },
})
}
if (provider.env !== undefined)
integrations.method.update({ integrationID, method: { type: "env", names: [...provider.env] } })
}
})

const sources = {
defaultModel: undefined as Document["info"]["model"],
models: new Map<
ConfigProvider.Info,
{
readonly providerID: string
readonly models: ReadonlyMap<string, { readonly inherit: boolean; readonly base?: Model.Info }>
}
>(),
configured: [] as readonly { providerID: string; id: string; configs: readonly ModelConfig[] }[],
}
yield* ctx.provider.transform((providers) => {
const next: typeof sources.models = new Map()
for (const [id, item] of configuredProviders(loaded.entries)) {
const providerID = id
const { current, source, changed } = overlayProvider(providers, providerID, item)
const definitions = new Map<string, { readonly inherit: boolean; readonly base?: Model.Info }>()
for (const [id, config] of Object.entries(item.models ?? {})) {
const base = source?.models.get(config.modelID ?? id) ?? source?.models.get(id)
const inherit = changed || !current?.models.has(id)
// Bind the source at this point in the provider fold. Later source edits/removal
// and its credential availability must not change an already-defined alias.
definitions.set(id, { inherit, base: base && structuredClone(base) })
if (!inherit) continue
providers.models.update(providerID, id, (model) => {
if (base) Object.assign(model, structuredClone(base))
if (item.package !== undefined) model.package = undefined
})
const snapshots = new Map<string, Pick<ProviderRecord, "provider" | "models"> | undefined>()
const origins = new Map<string, Pick<ProviderRecord, "provider" | "models"> | undefined>()
const configured = new Map<string, { providerID: string; id: string; base?: Model.Info; configs: ModelConfig[] }>()
const snapshot = (id: string) => {
if (!snapshots.has(id)) {
const existing = providers.get(id)
snapshots.set(id, existing && structuredClone({ provider: existing.provider, models: existing.models }))
}
return snapshots.get(id)
}

for (const [providerID, item] of configuredProviders(loaded.entries)) {
if (!origins.has(providerID)) origins.set(providerID, snapshot(providerID))
const current = providers.get(providerID)
const sourceOrigin = snapshot(item.canonical ?? current?.provider.canonical ?? providerID)
const { changed } = overlayProvider(providers, providerID, item)
if (changed) {
Comment thread

@opencodejr opencodejr Bot Oct 30, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This drops every configured entry for the provider, but models created by earlier files stay on the provider. A custom model declared only in an earlier file therefore survives with that file's fields and gets no default variants. The late pass also stops re-applying its config.

Repro, from a temporary test:

  • File 1: custom: { canonical: "openai", models: { extra: { name: "Extra" } } }.
  • File 2: custom: { canonical: "anthropic" }.
  • Base: custom/extra has low..max.
  • This PR: it has name: "Extra" and variants: [].

Either remove those models along with their entries, or keep tracking the ones the later file doesn't list, so they still get default variants.

Sorry, something went wrong.

Uh oh!

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

All reactions
origins.set(providerID, sourceOrigin)
for (const [key, entry] of configured) if (entry.providerID === providerID) configured.delete(key)
}
// A configured provider baseURL replaces every catalog per-model endpoint, not only those of configured
// models. Configured per-model settings are applied later and still win.
const origin = origins.get(providerID)
const updated = providers.get(providerID)?.provider
const clearPackage = item.package !== undefined || updated?.package !== (origin?.provider.package ?? "")
const clearBaseURL =
item.settings?.baseURL !== undefined || updated?.settings?.baseURL !== origin?.provider.settings?.baseURL
// A configured provider baseURL replaces every catalog per-model endpoint, not only those of configured models.
if (item.settings?.baseURL !== undefined)
for (const [id, model] of providers.get(providerID)?.models ?? [])
if (model.settings?.baseURL !== undefined)
providers.models.update(providerID, id, (model) => {
delete model.settings?.baseURL
providers.models.update(providerID, id, (draft) => {
delete draft.settings?.baseURL
})
next.set(item, { providerID, models: definitions })
}
for (const [item, definition] of next) {

for (const [id, config] of Object.entries(item.models ?? {})) {
if (config.limit === undefined) continue
providers.models.update(definition.providerID, id, (model) => {
model.limit = { ...model.limit, ...config.limit }
})
const base = sourceOrigin?.models.get(config.modelID ?? id) ?? sourceOrigin?.models.get(id)
const inherit = changed || !current?.models.has(id)
const key = `${providerID}\0${id}`
const existing = !inherit ? configured.get(key) : undefined
if (existing) existing.configs.push(config)
if (!existing) configured.set(key, { providerID, id, base, configs: [config] })
overlayFields(providers.models, providerID, id, config, inherit ? { base, clearPackage, clearBaseURL } : undefined)
}
}
sources.defaultModel = Config.latest(loaded.entries, "model")
sources.models = next
})

// Keep explicit model overrides in their late registration position, after external
// model transforms. They can recreate or re-enable a model within an available provider.
yield* ctx.model.transform((models) => {
const configuredDefault = sources.defaultModel
if (configuredDefault !== undefined) models.default.set(configuredDefault.providerID, configuredDefault.model)
for (const [item, definition] of sources.models) {
const providerID = definition.providerID
for (const [id, config] of Object.entries(item.models ?? {})) {
const source = definition.models.get(id)
const inherit = source?.inherit || !models.get(providerID, id)
overlay(
models,
models.provider.get(providerID)?.provider.package,
item,
providerID,
id,
config,
inherit && source?.base ? { base: source.base } : undefined,
for (const [providerID, origin] of origins) {
const record = providers.get(providerID)
if (!record) continue
for (const [id, model] of record.models) {
const entry = configured.get(`${providerID}\0${id}`)
const base = entry ? entry.base : origin?.models.get(id)
const finalPackage = model.package ?? record.provider.package
const hasExplicitVariants = entry?.configs.some(
(config) => config.capabilities?.reasoning !== undefined || config.variants !== undefined,
)
// Inherited variants are spelled for the base model's package, so a package override needs fresh ones.
// A base without variants has no reasoning support to respell.
const repackaged =
source?.base !== undefined &&
source.base.variants.length > 0 &&
models.get(providerID, id)?.package !== source.base.package
if (
config.variants === undefined &&
config.capabilities?.reasoning === undefined &&
(!source?.base || repackaged)
)
models.update(providerID, id, (model) => {
model.variants = [
...Variant.resolve({
...model,
package: model.package ?? models.provider.get(providerID)?.provider.package,
}),
]
base !== undefined &&
base.variants.length > 0 &&
finalPackage !== (base.package ?? origin?.provider.package) &&
Variant.canResolve(finalPackage)
if (repackaged || (entry !== undefined && !base && !hasExplicitVariants))
providers.models.update(providerID, id, (draft) => {
draft.variants = [...Variant.resolve({ ...draft, package: finalPackage })]
})
for (const config of entry?.configs ?? [])
applyExplicitVariants(providers.models, record.provider.package, providerID, id, config)
}
}

sources.defaultModel = Config.latest(loaded.entries, "model")
sources.configured = Array.from(configured.values())
})

// Re-apply surviving explicit config fields after external model transforms so configured values win
// while leaving plugin-supplied fields (including custom model variants) intact when omitted in config.
yield* ctx.model.transform((models) => {
if (sources.defaultModel !== undefined)
models.default.set(sources.defaultModel.providerID, sources.defaultModel.model)
for (const entry of sources.configured) {
const seeded = models.provider.get(entry.providerID)
if (!models.get(entry.providerID, entry.id)) {
const base = seeded?.models.get(entry.id)
if (base) models.update(entry.providerID, entry.id, (model) => Object.assign(model, structuredClone(base)))
}
for (const config of entry.configs) {
overlayFields(models, entry.providerID, entry.id, config)
applyExplicitVariants(models, seeded?.provider.package, entry.providerID, entry.id, config)
}
}
})
Expand All @@ -158,23 +154,20 @@ function configuredProviders(entries: readonly Entry[]) {
)
}

/**
* Applies an integration document's providers. The integration's plugin calls this at its own position in the
* provider chain, so provider plugins registered after it treat these providers as configured ones rather than
* as their canonical provider, while user and project config still overlay them in this plugin's late transform.
*/
export function integrate(providers: ProviderEditor, document: ConfigIntegration.Document) {
for (const [providerID, item] of Object.entries(document.info.providers ?? {})) {
const { source } = overlayProvider(providers, providerID, item, document.integrationID)
// An integration lists the models it routes: each starts again from the catalog and takes the provider's
// package and URL. Its models are complete here, so later model transforms and config apply on top.
const providerPackage = providers.get(providerID)?.provider.package
for (const [id, config] of Object.entries(item.models ?? {})) {
overlay(providers.models, providers.get(providerID)?.provider.package, item, providerID, id, config, {
base: source?.models.get(config.modelID ?? id) ?? source?.models.get(id),
const base = source?.models.get(config.modelID ?? id) ?? source?.models.get(id)
overlayFields(providers.models, providerID, id, config, {
base,
clearPackage: base !== undefined && item.package !== undefined,
clearBaseURL: base !== undefined && item.settings?.baseURL !== undefined,
})
applyExplicitVariants(providers.models, providerPackage, providerID, id, config)
}
const configured = providers.get(providerID)
// Bound to the connection so its providers disappear as soon as the account changes or signs out.
if (configured)
providers.add({
info: configured.provider,
Expand All @@ -184,7 +177,6 @@ export function integrate(providers: ProviderEditor, document: ConfigIntegration
}
}

/** Applies one configured provider, first copying its source when `canonical` points it at another provider. */
function overlayProvider(
providers: ProviderEditor,
providerID: string,
Expand Down Expand Up @@ -213,21 +205,18 @@ function overlayProvider(
return { current, source, changed }
}

/** Applies one configured model, first resetting it to `inherit.base` when it starts again from the catalog. */
function overlay(
function overlayFields(
models: Pick<ModelEditor, "update">,
providerPackage: string | undefined,
item: ConfigProvider.Info,
providerID: string,
id: string,
config: NonNullable<ConfigProvider.Info["models"]>[string],
inherit: { readonly base: Model.Info | undefined } | undefined,
config: ModelConfig,
reset?: { readonly base: Model.Info | undefined; readonly clearPackage: boolean; readonly clearBaseURL: boolean },
) {
models.update(providerID, id, (model) => {
if (inherit) {
if (inherit.base) Object.assign(model, structuredClone(inherit.base))
if (item.package !== undefined) model.package = undefined
if (item.settings?.baseURL !== undefined && model.settings) delete model.settings.baseURL
if (reset) {
if (reset.base) Object.assign(model, structuredClone(reset.base))
if (reset.clearPackage) model.package = undefined
if (reset.clearBaseURL && model.settings) delete model.settings.baseURL
}
if (config.type !== undefined) model.type = config.type
if (config.family !== undefined) model.family = config.family
Expand All @@ -242,10 +231,29 @@ function overlay(
if (config.capabilities !== undefined)
model.capabilities = Model.mergeCapabilities(config.capabilities, model.capabilities)
if (config.limit !== undefined) model.limit = { ...model.limit, ...config.limit }
if (config.cost !== undefined)
model.cost = (Array.isArray(config.cost) ? config.cost : [config.cost]).map((cost) => ({
tier: cost.tier && { ...cost.tier },
input: cost.input,
output: cost.output,
cache: {
read: cost.cache?.read ?? Money.USDPerMillionTokens.zero,
write: cost.cache?.write ?? Money.USDPerMillionTokens.zero,
},
}))
if (config.disabled !== undefined) model.enabled = !config.disabled
})
// Variants are spelled for the final package, which the update above normalizes from `aisdk:*`.
}

function applyExplicitVariants(
models: Pick<ModelEditor, "update">,
providerPackage: string | undefined,
providerID: string,
id: string,
config: ModelConfig,
) {
if (config.capabilities?.reasoning === undefined && config.variants === undefined) return
models.update(providerID, id, (model) => {
// Configured reasoning replaces the base variants; configured variants still overlay them below.
if (config.capabilities?.reasoning !== undefined)
model.variants = [
...Variant.resolve(
Expand All @@ -256,28 +264,13 @@ function overlay(
if (config.variants !== undefined) {
model.variants ??= []
for (const variant of config.variants) {
let existing = model.variants.find((item) => item.id === variant.id)
if (!existing) {
existing = { id: variant.id }
model.variants.push(existing)
}
if (variant.settings !== undefined)
existing.settings = Provider.mergeOverlay(existing.settings, variant.settings)
if (variant.headers !== undefined) existing.headers = Provider.mergeHeaders(existing.headers, variant.headers)
if (variant.body !== undefined) existing.body = Provider.mergeOverlay(existing.body, variant.body)
const found = model.variants.find((item) => item.id === variant.id)
const target = found ?? { id: variant.id }
if (!found) model.variants.push(target)
if (variant.settings !== undefined) target.settings = Provider.mergeOverlay(target.settings, variant.settings)
if (variant.headers !== undefined) target.headers = Provider.mergeHeaders(target.headers, variant.headers)
if (variant.body !== undefined) target.body = Provider.mergeOverlay(target.body, variant.body)
}
}
if (config.cost !== undefined) {
model.cost = (Array.isArray(config.cost) ? config.cost : [config.cost]).map((cost) => ({
tier: cost.tier && { ...cost.tier },
input: cost.input,
output: cost.output,
cache: {
read: cost.cache?.read ?? Money.USDPerMillionTokens.zero,
write: cost.cache?.write ?? Money.USDPerMillionTokens.zero,
},
}))
}
if (config.disabled !== undefined) model.enabled = !config.disabled
})
}
4 changes: 4 additions & 0 deletions 4 packages/core/src/variant.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export function supports(reasoning: ConfigProvider.Reasoning): readonly Support[
]
}

export function canResolve(packageName: string | undefined) {
return packageName !== undefined && packageName in PROTOCOLS
}

type Variants = Model.Info["variants"]
type Overlay = Omit<Variants[number], "id">

Expand Down
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 01402775 — 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.