Repository navigation
Expand file tree
/
Copy pathprovider.ts
More file actions
170 lines (165 loc) · 8.25 KB
/
Copy pathprovider.ts
File metadata and controls
170 lines (165 loc) · 8.25 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
export * as ConfigProviderPlugin from "./provider.js"
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 { 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 = 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,
})
provider.activation = "enabled"
if (item.canonical !== undefined) provider.canonical = item.canonical
if (item.name !== undefined) provider.name = item.name
if (item.package !== undefined) provider.package = item.package
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)
})
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
if (item.settings?.baseURL !== undefined && model.settings) delete model.settings.baseURL
})
}
next.set(item, { providerID, models: definitions })
}
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)
models.update(providerID, id, (model) => {
if (inherit && source?.base) {
Object.assign(model, structuredClone(source.base))
if (item.package !== undefined) model.package = undefined
if (item.settings?.baseURL !== undefined && model.settings) delete model.settings.baseURL
}
if (config.family !== undefined) model.family = config.family
if (config.name !== undefined) model.name = config.name
if (config.modelID !== undefined) model.modelID = config.modelID
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.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
if (config.limit !== undefined) model.limit = { ...model.limit, ...config.limit }
})
if (config.variants === undefined && !source?.base)
models.update(providerID, id, (model) => {
model.variants = [
...Variant.resolve({
...model,
package: model.package ?? models.provider.get(providerID)?.provider.package,
}),
]
})
}
}
})
}),
})
function configuredProviders(entries: readonly Entry[]) {
return entries
.filter((entry): entry is Document => entry.type === "document")
.flatMap((file) => Object.entries(file.info.providers ?? {}))
}