-
Notifications
You must be signed in to change notification settings - Fork 16.8k
feat: add new provider plugin hook for resolving models and sync models from github models endpoint (falls back to models.dev) #20533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
333e4bf
feat: add new provider plugin hook for resolving models and sync mode…
rekram1-node e697bbd
tweak: mv plugin hook
rekram1-node 1caa3df
tweak: ensure config models take precedence
rekram1-node 390e0f9
tweak ordering
rekram1-node 7d01c3c
tweak: fix import
rekram1-node File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { z } from "zod" | ||
| import type { Model } from "@opencode-ai/sdk/v2" | ||
|
|
||
| export namespace CopilotModels { | ||
| export const schema = z.object({ | ||
| data: z.array( | ||
| z.object({ | ||
| model_picker_enabled: z.boolean(), | ||
| id: z.string(), | ||
| name: z.string(), | ||
| // every version looks like: `{model.id}-YYYY-MM-DD` | ||
| version: z.string(), | ||
| supported_endpoints: z.array(z.string()).optional(), | ||
| capabilities: z.object({ | ||
| family: z.string(), | ||
| limits: z.object({ | ||
| max_context_window_tokens: z.number(), | ||
| max_output_tokens: z.number(), | ||
| max_prompt_tokens: z.number(), | ||
| vision: z | ||
| .object({ | ||
| max_prompt_image_size: z.number(), | ||
| max_prompt_images: z.number(), | ||
| supported_media_types: z.array(z.string()), | ||
| }) | ||
| .optional(), | ||
| }), | ||
| supports: z.object({ | ||
| adaptive_thinking: z.boolean().optional(), | ||
| max_thinking_budget: z.number().optional(), | ||
| min_thinking_budget: z.number().optional(), | ||
| reasoning_effort: z.array(z.string()).optional(), | ||
| streaming: z.boolean(), | ||
| structured_outputs: z.boolean().optional(), | ||
| tool_calls: z.boolean(), | ||
| vision: z.boolean().optional(), | ||
| }), | ||
| }), | ||
| }), | ||
| ), | ||
| }) | ||
|
|
||
| type Item = z.infer<typeof schema>["data"][number] | ||
|
|
||
| function build(key: string, remote: Item, url: string, prev?: Model): Model { | ||
| const reasoning = | ||
| !!remote.capabilities.supports.adaptive_thinking || | ||
| !!remote.capabilities.supports.reasoning_effort?.length || | ||
| remote.capabilities.supports.max_thinking_budget !== undefined || | ||
| remote.capabilities.supports.min_thinking_budget !== undefined | ||
| const image = | ||
| (remote.capabilities.supports.vision ?? false) || | ||
| (remote.capabilities.limits.vision?.supported_media_types ?? []).some((item) => item.startsWith("image/")) | ||
|
|
||
| return { | ||
| id: key, | ||
| providerID: "github-copilot", | ||
| api: { | ||
| id: remote.id, | ||
| url, | ||
| npm: "@ai-sdk/github-copilot", | ||
| }, | ||
| // API response wins | ||
| status: "active", | ||
| limit: { | ||
| context: remote.capabilities.limits.max_context_window_tokens, | ||
| input: remote.capabilities.limits.max_prompt_tokens, | ||
| output: remote.capabilities.limits.max_output_tokens, | ||
| }, | ||
| capabilities: { | ||
| temperature: prev?.capabilities.temperature ?? true, | ||
| reasoning: prev?.capabilities.reasoning ?? reasoning, | ||
| attachment: prev?.capabilities.attachment ?? true, | ||
| toolcall: remote.capabilities.supports.tool_calls, | ||
| input: { | ||
| text: true, | ||
| audio: false, | ||
| image, | ||
| video: false, | ||
| pdf: false, | ||
| }, | ||
| output: { | ||
| text: true, | ||
| audio: false, | ||
| image: false, | ||
| video: false, | ||
| pdf: false, | ||
| }, | ||
| interleaved: false, | ||
| }, | ||
| // existing wins | ||
| family: prev?.family ?? remote.capabilities.family, | ||
| name: prev?.name ?? remote.name, | ||
| cost: { | ||
| input: 0, | ||
| output: 0, | ||
| cache: { read: 0, write: 0 }, | ||
| }, | ||
| options: prev?.options ?? {}, | ||
| headers: prev?.headers ?? {}, | ||
| release_date: | ||
| prev?.release_date ?? | ||
| (remote.version.startsWith(`${remote.id}-`) ? remote.version.slice(remote.id.length + 1) : remote.version), | ||
| variants: prev?.variants ?? {}, | ||
| } | ||
| } | ||
|
|
||
| export async function get( | ||
| baseURL: string, | ||
| headers: HeadersInit = {}, | ||
| existing: Record<string, Model> = {}, | ||
| ): Promise<Record<string, Model>> { | ||
| const data = await fetch(`${baseURL}/models`, { | ||
| headers, | ||
| }).then(async (res) => { | ||
| if (!res.ok) { | ||
| throw new Error(`Failed to fetch models: ${res.status}`) | ||
| } | ||
| return schema.parse(await res.json()) | ||
| }) | ||
|
|
||
| const result = { ...existing } | ||
| const remote = new Map(data.data.filter((m) => m.model_picker_enabled).map((m) => [m.id, m] as const)) | ||
|
|
||
| // prune existing models whose api.id isn't in the endpoint response | ||
| for (const [key, model] of Object.entries(result)) { | ||
| const m = remote.get(model.api.id) | ||
| if (!m) { | ||
| delete result[key] | ||
| continue | ||
| } | ||
| result[key] = build(key, m, baseURL, model) | ||
| } | ||
|
|
||
| // add new endpoint models not already keyed in result | ||
| for (const [id, m] of remote) { | ||
| if (id in result) continue | ||
| result[id] = build(id, m, baseURL) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.