|
|
@@ -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
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.
|
|
|
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 |
|
|
}) |
|
|
} |