114 lines
5.3 KiB
TypeScript
114 lines
5.3 KiB
TypeScript
export * as ConfigProviderPlugin from "./provider"
|
|
|
|
import { define } from "../../plugin/internal"
|
|
import { Effect } from "effect"
|
|
import { Config } from "../../config"
|
|
import { ModelV2 } from "../../model"
|
|
import { ProviderV2 } from "../../provider"
|
|
|
|
export const Plugin = define({
|
|
id: "config-provider",
|
|
effect: Effect.fn(function* (ctx) {
|
|
const config = yield* Config.Service
|
|
yield* ctx.integration.transform(
|
|
Effect.fn(function* (integrations) {
|
|
const files = (yield* config.entries()).filter((entry): entry is Config.Document => entry.type === "document")
|
|
const configuredIntegrations = new Set(
|
|
files.flatMap((file) =>
|
|
Object.entries(file.info.providers ?? {}).flatMap(([id, provider]) =>
|
|
provider.env === undefined ? [] : [id],
|
|
),
|
|
),
|
|
)
|
|
for (const file of files) {
|
|
for (const [id, item] of Object.entries(file.info.providers ?? {})) {
|
|
const integrationID = id
|
|
if (!configuredIntegrations.has(id) && !integrations.get(integrationID)) continue
|
|
integrations.update(integrationID, (integration) => {
|
|
integration.name = item.name ?? integration.name
|
|
})
|
|
if (item.env !== undefined) {
|
|
integrations.method.update({
|
|
integrationID,
|
|
method: { type: "env", names: [...item.env] },
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
)
|
|
|
|
yield* ctx.catalog.transform(
|
|
Effect.fn(function* (catalog) {
|
|
const entries = yield* config.entries()
|
|
const files = entries.filter((entry): entry is Config.Document => entry.type === "document")
|
|
const configuredDefault = Config.latest(entries, "model")
|
|
if (configuredDefault !== undefined) {
|
|
const model = ModelV2.parse(configuredDefault)
|
|
catalog.model.default.set(model.providerID, model.modelID)
|
|
}
|
|
for (const file of files) {
|
|
for (const [id, item] of Object.entries(file.info.providers ?? {})) {
|
|
const providerID = id
|
|
catalog.provider.update(providerID, (provider) => {
|
|
if (item.name !== undefined) provider.name = item.name
|
|
if (item.package !== undefined) provider.package = item.package
|
|
if (item.aisdk !== undefined) provider.aisdk = item.aisdk
|
|
if (item.settings !== undefined) provider.settings = { ...provider.settings, ...item.settings }
|
|
if (item.headers !== undefined) provider.headers = { ...provider.headers, ...item.headers }
|
|
if (item.body !== undefined) provider.body = { ...provider.body, ...item.body }
|
|
})
|
|
for (const [id, config] of Object.entries(item.models ?? {})) {
|
|
catalog.model.update(providerID, id, (model) => {
|
|
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.package !== undefined) model.package = config.package
|
|
if (config.aisdk !== undefined) model.aisdk = config.aisdk
|
|
if (config.settings !== undefined) model.settings = { ...model.settings, ...config.settings }
|
|
if (config.headers !== undefined) model.headers = { ...model.headers, ...config.headers }
|
|
if (config.body !== undefined) model.body = { ...model.body, ...config.body }
|
|
if (config.capabilities !== undefined) {
|
|
model.capabilities = {
|
|
tools: config.capabilities.tools,
|
|
input: [...config.capabilities.input],
|
|
output: [...config.capabilities.output],
|
|
}
|
|
}
|
|
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 = { ...existing.settings, ...variant.settings }
|
|
if (variant.headers !== undefined) existing.headers = { ...existing.headers, ...variant.headers }
|
|
if (variant.body !== undefined) existing.body = { ...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 ?? 0,
|
|
write: cost.cache?.write ?? 0,
|
|
},
|
|
}))
|
|
}
|
|
if (config.disabled !== undefined) model.enabled = !config.disabled
|
|
if (config.limit !== undefined) model.limit = { ...model.limit, ...config.limit }
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
)
|
|
}),
|
|
})
|