|
| 1 | +import { z } from "zod" |
| 2 | +import type { Model } from "@opencode-ai/sdk/v2" |
| 3 | + |
| 4 | +export namespace CopilotModels { |
| 5 | + export const schema = z.object({ |
| 6 | + data: z.array( |
| 7 | + z.object({ |
| 8 | + model_picker_enabled: z.boolean(), |
| 9 | + id: z.string(), |
| 10 | + name: z.string(), |
| 11 | + // every version looks like: `{model.id}-YYYY-MM-DD` |
| 12 | + version: z.string(), |
| 13 | + supported_endpoints: z.array(z.string()).optional(), |
| 14 | + capabilities: z.object({ |
| 15 | + family: z.string(), |
| 16 | + limits: z.object({ |
| 17 | + max_context_window_tokens: z.number(), |
| 18 | + max_output_tokens: z.number(), |
| 19 | + max_prompt_tokens: z.number(), |
| 20 | + vision: z |
| 21 | + .object({ |
| 22 | + max_prompt_image_size: z.number(), |
| 23 | + max_prompt_images: z.number(), |
| 24 | + supported_media_types: z.array(z.string()), |
| 25 | + }) |
| 26 | + .optional(), |
| 27 | + }), |
| 28 | + supports: z.object({ |
| 29 | + adaptive_thinking: z.boolean().optional(), |
| 30 | + max_thinking_budget: z.number().optional(), |
| 31 | + min_thinking_budget: z.number().optional(), |
| 32 | + reasoning_effort: z.array(z.string()).optional(), |
| 33 | + streaming: z.boolean(), |
| 34 | + structured_outputs: z.boolean().optional(), |
| 35 | + tool_calls: z.boolean(), |
| 36 | + vision: z.boolean().optional(), |
| 37 | + }), |
| 38 | + }), |
| 39 | + }), |
| 40 | + ), |
| 41 | + }) |
| 42 | + |
| 43 | + type Item = z.infer<typeof schema>["data"][number] |
| 44 | + |
| 45 | + function build(key: string, remote: Item, url: string, prev?: Model): Model { |
| 46 | + const reasoning = |
| 47 | + !!remote.capabilities.supports.adaptive_thinking || |
| 48 | + !!remote.capabilities.supports.reasoning_effort?.length || |
| 49 | + remote.capabilities.supports.max_thinking_budget !== undefined || |
| 50 | + remote.capabilities.supports.min_thinking_budget !== undefined |
| 51 | + const image = |
| 52 | + (remote.capabilities.supports.vision ?? false) || |
| 53 | + (remote.capabilities.limits.vision?.supported_media_types ?? []).some((item) => item.startsWith("image/")) |
| 54 | + |
| 55 | + return { |
| 56 | + id: key, |
| 57 | + providerID: "github-copilot", |
| 58 | + api: { |
| 59 | + id: remote.id, |
| 60 | + url, |
| 61 | + npm: "@ai-sdk/github-copilot", |
| 62 | + }, |
| 63 | + // API response wins |
| 64 | + status: "active", |
| 65 | + limit: { |
| 66 | + context: remote.capabilities.limits.max_context_window_tokens, |
| 67 | + input: remote.capabilities.limits.max_prompt_tokens, |
| 68 | + output: remote.capabilities.limits.max_output_tokens, |
| 69 | + }, |
| 70 | + capabilities: { |
| 71 | + temperature: prev?.capabilities.temperature ?? true, |
| 72 | + reasoning: prev?.capabilities.reasoning ?? reasoning, |
| 73 | + attachment: prev?.capabilities.attachment ?? true, |
| 74 | + toolcall: remote.capabilities.supports.tool_calls, |
| 75 | + input: { |
| 76 | + text: true, |
| 77 | + audio: false, |
| 78 | + image, |
| 79 | + video: false, |
| 80 | + pdf: false, |
| 81 | + }, |
| 82 | + output: { |
| 83 | + text: true, |
| 84 | + audio: false, |
| 85 | + image: false, |
| 86 | + video: false, |
| 87 | + pdf: false, |
| 88 | + }, |
| 89 | + interleaved: false, |
| 90 | + }, |
| 91 | + // existing wins |
| 92 | + family: prev?.family ?? remote.capabilities.family, |
| 93 | + name: prev?.name ?? remote.name, |
| 94 | + cost: { |
| 95 | + input: 0, |
| 96 | + output: 0, |
| 97 | + cache: { read: 0, write: 0 }, |
| 98 | + }, |
| 99 | + options: prev?.options ?? {}, |
| 100 | + headers: prev?.headers ?? {}, |
| 101 | + release_date: |
| 102 | + prev?.release_date ?? |
| 103 | + (remote.version.startsWith(`${remote.id}-`) ? remote.version.slice(remote.id.length + 1) : remote.version), |
| 104 | + variants: prev?.variants ?? {}, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + export async function get( |
| 109 | + baseURL: string, |
| 110 | + headers: HeadersInit = {}, |
| 111 | + existing: Record<string, Model> = {}, |
| 112 | + ): Promise<Record<string, Model>> { |
| 113 | + const data = await fetch(`${baseURL}/models`, { |
| 114 | + headers, |
| 115 | + }).then(async (res) => { |
| 116 | + if (!res.ok) { |
| 117 | + throw new Error(`Failed to fetch models: ${res.status}`) |
| 118 | + } |
| 119 | + return schema.parse(await res.json()) |
| 120 | + }) |
| 121 | + |
| 122 | + const result = { ...existing } |
| 123 | + const remote = new Map(data.data.filter((m) => m.model_picker_enabled).map((m) => [m.id, m] as const)) |
| 124 | + |
| 125 | + // prune existing models whose api.id isn't in the endpoint response |
| 126 | + for (const [key, model] of Object.entries(result)) { |
| 127 | + const m = remote.get(model.api.id) |
| 128 | + if (!m) { |
| 129 | + delete result[key] |
| 130 | + continue |
| 131 | + } |
| 132 | + result[key] = build(key, m, baseURL, model) |
| 133 | + } |
| 134 | + |
| 135 | + // add new endpoint models not already keyed in result |
| 136 | + for (const [id, m] of remote) { |
| 137 | + if (id in result) continue |
| 138 | + result[id] = build(id, m, baseURL) |
| 139 | + } |
| 140 | + |
| 141 | + return result |
| 142 | + } |
| 143 | +} |
0 commit comments