Repository navigation
Expand file tree
/
Copy pathprovider.ts
More file actions
274 lines (266 loc) · 12.9 KB
/
Copy pathprovider.ts
File metadata and controls
274 lines (266 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
export * as ConfigProviderPlugin from "./provider.js"
import type { ModelEditor } from "@opencode/plugin/effect/model"
import type { ProviderEditor } 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"
import { Money } from "@opencode/schema/money"
import { Effect } from "effect"
import { Config } from "../../config.js"
import { Model } from "../../model.js"
import { Provider } from "../../provider.js"
import { Variant } from "../../variant.js"
import { ConfigIntegration } from "../integration.js"
import { ConfigEntryObserver } from "./entry-observer.js"
export const Plugin = define({
id: "opencode.config.provider",
effect: Effect.fn(function* (ctx) {
const config = yield* Config.Service
const loaded = yield* ConfigEntryObserver.observe(
config,
ctx.event,
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" },
})
}
integrations.update(integrationID, (integration) => {
integration.name = provider.name || integration.name
})
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 }>
}
>(),
}
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
})
}
// 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.
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
})
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 }
})
}
}
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 existing = models.get(providerID, id)
const hadVariants = (existing?.variants.length ?? 0) > 0
const inherit = source?.inherit || !existing
overlay(
models,
models.provider.get(providerID)?.provider.package,
item,
providerID,
id,
config,
inherit && source?.base ? { base: source.base } : 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 &&
// Custom models start without variants, so non-empty variants here came from an earlier transform.
((!source?.base && !hadVariants) || repackaged)
)
models.update(providerID, id, (model) => {
model.variants = [
...Variant.resolve({
...model,
package: model.package ?? models.provider.get(providerID)?.provider.package,
}),
]
})
}
}
})
}),
})
// Integration documents are left out: their providers authenticate with the integration's connection, so they add no
// integration or method, and the integration's own plugin applies them earlier in the provider chain (`integrate`).
function configuredProviders(entries: readonly Entry[]) {
return entries
.filter((entry): entry is Document => entry.type === "document" && !(entry instanceof ConfigIntegration.Document))
.flatMap((file) => Object.entries(file.info.providers ?? {}))
}
/**
* 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.
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 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,
models: Array.from(configured.models.values()),
sourceConnection: document.connection,
})
}
}
/** Applies one configured provider, first copying its source when `canonical` points it at another provider. */
function overlayProvider(
providers: ProviderEditor,
providerID: string,
item: ConfigProvider.Info,
integrationID?: ConfigIntegration.Document["integrationID"],
) {
const current = providers.get(providerID)
const source = providers.get(item.canonical ?? current?.provider.canonical ?? providerID)
const changed = item.canonical !== undefined && item.canonical !== current?.provider.canonical
providers.update(providerID, (provider) => {
if (changed && source && source.provider !== provider)
Object.assign(provider, structuredClone(source.provider), {
id: provider.id,
integrationID: provider.integrationID,
})
if (integrationID) provider.integrationID = integrationID
provider.activation = "enabled"
if (item.canonical !== undefined) provider.canonical = item.canonical
if (item.name) provider.name = item.name
if (item.package !== undefined) provider.package = item.package
if (item.compatibility !== undefined) provider.compatibility = { ...provider.compatibility, ...item.compatibility }
if (item.settings !== undefined) provider.settings = Provider.mergeOverlay(provider.settings, item.settings)
if (item.headers !== undefined) provider.headers = Provider.mergeHeaders(provider.headers, item.headers)
if (item.body !== undefined) provider.body = Provider.mergeOverlay(provider.body, item.body)
})
return { current, source, changed }
}
/** Applies one configured model, first resetting it to `inherit.base` when it starts again from the catalog. */
function overlay(
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,
) {
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 (config.type !== undefined) model.type = config.type
if (config.family !== undefined) model.family = config.family
if (config.name) model.name = config.name
if (config.modelID !== undefined) model.modelID = config.modelID
if (config.releaseDate !== undefined) model.time = { released: Date.parse(config.releaseDate) }
if (config.compatibility !== undefined) model.compatibility = { ...model.compatibility, ...config.compatibility }
if (config.package !== undefined) model.package = config.package
if (config.settings !== undefined) model.settings = Provider.mergeOverlay(model.settings, config.settings)
if (config.headers !== undefined) model.headers = Provider.mergeHeaders(model.headers, config.headers)
if (config.body !== undefined) model.body = Provider.mergeOverlay(model.body, config.body)
if (config.capabilities !== undefined)
model.capabilities = Model.mergeCapabilities(config.capabilities, model.capabilities)
if (config.limit !== undefined) model.limit = { ...model.limit, ...config.limit }
})
// Variants are spelled for the final package, which the update above normalizes from `aisdk:*`.
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(
{ ...model, package: model.package ?? providerPackage },
Variant.supports(config.capabilities.reasoning),
),
]
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)
}
}
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
})
}