|
| 1 | +import type { Model } from "@opencode-ai/sdk/v2" |
| 2 | +import { Schema } from "effect" |
| 3 | + |
| 4 | +const reasoningOption = Schema.Struct({ |
| 5 | + type: Schema.Literal("effort"), |
| 6 | + values: Schema.Array(Schema.NullOr(Schema.String)), |
| 7 | +}) |
| 8 | + |
| 9 | +const response = Schema.Struct({ |
| 10 | + data: Schema.Array( |
| 11 | + Schema.Struct({ |
| 12 | + id: Schema.String, |
| 13 | + base_model_id: Schema.optional(Schema.String), |
| 14 | + hugging_face_id: Schema.optional(Schema.String), |
| 15 | + name: Schema.optional(Schema.String), |
| 16 | + input_modalities: Schema.optional(Schema.Array(Schema.String)), |
| 17 | + output_modalities: Schema.optional(Schema.Array(Schema.String)), |
| 18 | + context_length: Schema.optional(Schema.Number), |
| 19 | + max_output_length: Schema.optional(Schema.Number), |
| 20 | + pricing: Schema.optional( |
| 21 | + Schema.Struct({ |
| 22 | + prompt: Schema.optional(Schema.Union([Schema.String, Schema.Number])), |
| 23 | + completion: Schema.optional(Schema.Union([Schema.String, Schema.Number])), |
| 24 | + input_cache_read: Schema.optional(Schema.Union([Schema.String, Schema.Number])), |
| 25 | + }), |
| 26 | + ), |
| 27 | + supported_sampling_parameters: Schema.optional(Schema.Array(Schema.String)), |
| 28 | + supported_features: Schema.optional(Schema.Array(Schema.String)), |
| 29 | + reasoning_options: Schema.optional(Schema.Array(reasoningOption)), |
| 30 | + interleaved: Schema.optional( |
| 31 | + Schema.Union([ |
| 32 | + Schema.Boolean, |
| 33 | + Schema.Struct({ |
| 34 | + field: Schema.Literals(["reasoning", "reasoning_content", "reasoning_details"]), |
| 35 | + }), |
| 36 | + ]), |
| 37 | + ), |
| 38 | + }), |
| 39 | + ), |
| 40 | +}) |
| 41 | + |
| 42 | +const decode = Schema.decodeUnknownSync(response) |
| 43 | + |
| 44 | +function price(value: string | number | undefined, fallback: number) { |
| 45 | + if (value === undefined) return fallback |
| 46 | + const parsed = Number(value) |
| 47 | + return Number.isFinite(parsed) ? parsed * 1_000_000 : fallback |
| 48 | +} |
| 49 | + |
| 50 | +export async function get(baseURL: string, apiKey: string, existing: Record<string, Model>) { |
| 51 | + const data = await fetch(`${baseURL.replace(/\/+$/, "")}/models`, { |
| 52 | + headers: { |
| 53 | + Authorization: `Bearer ${apiKey}`, |
| 54 | + }, |
| 55 | + signal: AbortSignal.timeout(3_000), |
| 56 | + }).then(async (res) => { |
| 57 | + if (!res.ok) throw new Error(`Failed to fetch Modal models: ${res.status}`) |
| 58 | + return decode(await res.json()) |
| 59 | + }) |
| 60 | + |
| 61 | + return Object.fromEntries( |
| 62 | + data.data.map((item) => { |
| 63 | + const template = existing[item.base_model_id ?? item.hugging_face_id ?? item.id] |
| 64 | + const model: Model = { |
| 65 | + id: item.id, |
| 66 | + providerID: "modal", |
| 67 | + name: item.name ?? template?.name ?? item.id, |
| 68 | + family: template?.family, |
| 69 | + api: { |
| 70 | + id: item.id, |
| 71 | + url: baseURL, |
| 72 | + npm: template?.api.npm ?? "@ai-sdk/openai-compatible", |
| 73 | + }, |
| 74 | + status: template?.status ?? "active", |
| 75 | + headers: { ...template?.headers }, |
| 76 | + options: { ...template?.options }, |
| 77 | + cost: { |
| 78 | + input: price(item.pricing?.prompt, template?.cost.input ?? 0), |
| 79 | + output: price(item.pricing?.completion, template?.cost.output ?? 0), |
| 80 | + cache: { |
| 81 | + read: price(item.pricing?.input_cache_read, template?.cost.cache.read ?? 0), |
| 82 | + write: template?.cost.cache.write ?? 0, |
| 83 | + }, |
| 84 | + }, |
| 85 | + limit: { |
| 86 | + context: item.context_length ?? template?.limit.context ?? 0, |
| 87 | + input: template?.limit.input, |
| 88 | + output: item.max_output_length ?? template?.limit.output ?? 0, |
| 89 | + }, |
| 90 | + capabilities: { |
| 91 | + temperature: |
| 92 | + item.supported_sampling_parameters?.includes("temperature") ?? template?.capabilities.temperature ?? false, |
| 93 | + reasoning: item.supported_features?.includes("reasoning") ?? template?.capabilities.reasoning ?? false, |
| 94 | + attachment: |
| 95 | + item.input_modalities?.some((modality) => modality !== "text") ?? |
| 96 | + template?.capabilities.attachment ?? |
| 97 | + false, |
| 98 | + toolcall: item.supported_features?.includes("tools") ?? template?.capabilities.toolcall ?? true, |
| 99 | + input: { |
| 100 | + text: item.input_modalities?.includes("text") ?? template?.capabilities.input.text ?? true, |
| 101 | + audio: item.input_modalities?.includes("audio") ?? template?.capabilities.input.audio ?? false, |
| 102 | + image: item.input_modalities?.includes("image") ?? template?.capabilities.input.image ?? false, |
| 103 | + video: item.input_modalities?.includes("video") ?? template?.capabilities.input.video ?? false, |
| 104 | + pdf: item.input_modalities?.includes("pdf") ?? template?.capabilities.input.pdf ?? false, |
| 105 | + }, |
| 106 | + output: { |
| 107 | + text: item.output_modalities?.includes("text") ?? template?.capabilities.output.text ?? true, |
| 108 | + audio: item.output_modalities?.includes("audio") ?? template?.capabilities.output.audio ?? false, |
| 109 | + image: item.output_modalities?.includes("image") ?? template?.capabilities.output.image ?? false, |
| 110 | + video: item.output_modalities?.includes("video") ?? template?.capabilities.output.video ?? false, |
| 111 | + pdf: item.output_modalities?.includes("pdf") ?? template?.capabilities.output.pdf ?? false, |
| 112 | + }, |
| 113 | + interleaved: item.interleaved ?? template?.capabilities.interleaved ?? false, |
| 114 | + }, |
| 115 | + release_date: template?.release_date ?? "", |
| 116 | + } |
| 117 | + model.variants = |
| 118 | + item.reasoning_options === undefined |
| 119 | + ? template?.variants |
| 120 | + : Object.fromEntries( |
| 121 | + item.reasoning_options.flatMap((option) => |
| 122 | + option.values.map((value) => { |
| 123 | + const effort = value ?? "none" |
| 124 | + return [effort, { reasoningEffort: effort }] |
| 125 | + }), |
| 126 | + ), |
| 127 | + ) |
| 128 | + return [item.id, model] |
| 129 | + }), |
| 130 | + ) |
| 131 | +} |
| 132 | + |
| 133 | +export * as ModalModels from "./models" |
0 commit comments