|
| 1 | +/** |
| 2 | + * Generic OpenAI-compatible model discovery. |
| 3 | + * |
| 4 | + * Many providers (Volcengine Ark, BytePlus, MiniMax, DeepSeek, etc.) expose an |
| 5 | + * OpenAI-compatible `GET /models` endpoint. This module fetches that endpoint |
| 6 | + * and merges the results with catalog templates from models.dev so the |
| 7 | + * discovered models inherit family, cost, capabilities, and compatibility |
| 8 | + * metadata from the catalog while their availability is determined live. |
| 9 | + */ |
| 10 | +export * as OpenAICompatibleModels from "./models.js" |
| 11 | + |
| 12 | +import { Money } from "@opencode/schema/money" |
| 13 | +import { Effect, Option, Schema } from "effect" |
| 14 | +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" |
| 15 | +import { Model } from "../model.js" |
| 16 | +import { Provider } from "../provider.js" |
| 17 | + |
| 18 | +const Response = Schema.Struct({ data: Schema.Array(Schema.Unknown) }) |
| 19 | + |
| 20 | +/** A single entry returned by `GET /models`. */ |
| 21 | +const RemoteModel = Schema.Struct({ |
| 22 | + id: Schema.String, |
| 23 | +}) |
| 24 | +const decodeModel = Schema.decodeUnknownOption(RemoteModel) |
| 25 | + |
| 26 | +type RemoteModel = typeof RemoteModel.Type |
| 27 | + |
| 28 | +export type Snapshot = readonly RemoteModel[] |
| 29 | + |
| 30 | +/** |
| 31 | + * Fetch the model list from `GET <baseURL>/models`. |
| 32 | + * Each entry in `data` is decoded tolerantly so one malformed item cannot |
| 33 | + * discard the whole inventory. |
| 34 | + */ |
| 35 | +export const load = Effect.fn("OpenAICompatibleModels.load")(function* ( |
| 36 | + http: HttpClient.HttpClient, |
| 37 | + baseURL: string, |
| 38 | + apiKey: string, |
| 39 | +) { |
| 40 | + const response = yield* HttpClient.filterStatusOk(http) |
| 41 | + .execute( |
| 42 | + HttpClientRequest.get(`${baseURL.replace(/\/+$/, "")}/models`).pipe( |
| 43 | + HttpClientRequest.bearerToken(apiKey), |
| 44 | + ), |
| 45 | + ) |
| 46 | + .pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Response)), Effect.timeout("3 seconds")) |
| 47 | + return response.data.flatMap((raw) => { |
| 48 | + const model = Option.getOrUndefined(decodeModel(raw)) |
| 49 | + return model ? [model] : [] |
| 50 | + }) |
| 51 | +}) |
| 52 | + |
| 53 | +/** |
| 54 | + * Combine remote facts with source templates. A discovered model whose ID |
| 55 | + * matches a catalog template inherits the template's metadata (family, cost, |
| 56 | + * capabilities, compatibility, variants, etc.). A discovered model with no |
| 57 | + * template gets sensible defaults from `Model.Info.default`. |
| 58 | + */ |
| 59 | +export function derive( |
| 60 | + providerID: Provider.ID, |
| 61 | + baseURL: string, |
| 62 | + remote: Snapshot, |
| 63 | + existing: readonly Model.Info[], |
| 64 | +) { |
| 65 | + const templates = new Map(existing.map((model) => [model.id, model])) |
| 66 | + const result = new Map<Model.ID, Model.Info>() |
| 67 | + for (const item of remote) { |
| 68 | + const id = Model.ID.make(item.id) |
| 69 | + const template = templates.get(id) |
| 70 | + result.set(id, build(providerID, id, baseURL, template)) |
| 71 | + } |
| 72 | + return result |
| 73 | +} |
| 74 | + |
| 75 | +function build(providerID: Provider.ID, id: Model.ID, baseURL: string, previous?: Model.Info) { |
| 76 | + return Model.Info.make({ |
| 77 | + ...Model.Info.default(providerID, id), |
| 78 | + id, |
| 79 | + modelID: id, |
| 80 | + providerID, |
| 81 | + name: previous?.name ?? id, |
| 82 | + family: previous?.family, |
| 83 | + compatibility: previous?.compatibility, |
| 84 | + package: previous?.package, |
| 85 | + settings: Provider.mergeOverlay(previous?.settings, { baseURL, provider: providerID }), |
| 86 | + headers: previous?.headers, |
| 87 | + body: previous?.body, |
| 88 | + capabilities: { |
| 89 | + tools: previous?.capabilities.tools ?? true, |
| 90 | + input: previous?.capabilities.input ?? ["text"], |
| 91 | + output: previous?.capabilities.output ?? ["text"], |
| 92 | + }, |
| 93 | + variants: previous?.variants ?? [], |
| 94 | + time: previous?.time ?? { released: 0 }, |
| 95 | + cost: previous ? previous.cost : [{ input: Money.USDPerMillionTokens.zero, output: Money.USDPerMillionTokens.zero, cache: { read: Money.USDPerMillionTokens.zero, write: Money.USDPerMillionTokens.zero } }], |
| 96 | + status: previous?.status ?? "active", |
| 97 | + enabled: previous?.enabled ?? true, |
| 98 | + limit: previous?.limit ?? { context: 0, output: 0 }, |
| 99 | + }) |
| 100 | +} |