GeezerVison is showing github.com — Add local server provider with automatic model discovery · hmblair/opencode@a77a75d
File treeExpand file treeCollapse file tree Open diff view settings packages/opencode/src/provider
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/provider/provider.ts +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -668,6 +668,49 @@ export namespace Provider { | 668 | 668 | }, | 669 | 669 | } | 670 | 670 | }, | | 671 | + async local(input) { | | 672 | + const baseURL = input.options?.baseURL | | 673 | + if (!baseURL) return { autoload: false } | | 674 | + try { | | 675 | + const url = `${String(baseURL).replace(/\/+$/, "")}/models` | | 676 | + const headers: Record<string, string> = { Accept: "application/json" } | | 677 | + const apiKey = input.options?.apiKey ?? input.key | | 678 | + if (apiKey) headers["Authorization"] = `Bearer ${apiKey}` | | 679 | + const res = await fetch(url, { headers, signal: AbortSignal.timeout(3000) }) | | 680 | + if (!res.ok) return { autoload: false } | | 681 | + const json = (await res.json()) as { data?: Array<{ id: string }> } | | 682 | + if (!json.data?.length) return { autoload: false } | | 683 | + for (const model of json.data) { | | 684 | + if (!model.id || input.models[model.id]) continue | | 685 | + input.models[model.id] = { | | 686 | + id: ModelID.make(model.id), | | 687 | + providerID: ProviderID.make("local"), | | 688 | + name: model.id, | | 689 | + api: { id: model.id, url: baseURL, npm: "@ai-sdk/openai-compatible" }, | | 690 | + status: "active", | | 691 | + headers: {}, | | 692 | + options: {}, | | 693 | + cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, | | 694 | + limit: { context: 0, output: 0 }, | | 695 | + capabilities: { | | 696 | + temperature: true, | | 697 | + reasoning: false, | | 698 | + attachment: false, | | 699 | + toolcall: true, | | 700 | + input: { text: true, audio: false, image: false, video: false, pdf: false }, | | 701 | + output: { text: true, audio: false, image: false, video: false, pdf: false }, | | 702 | + interleaved: false, | | 703 | + }, | | 704 | + family: "", | | 705 | + release_date: "", | | 706 | + } | | 707 | + } | | 708 | + log.info("auto-discovered local models", { count: json.data.length }) | | 709 | + return { autoload: true } | | 710 | + } catch { | | 711 | + return { autoload: false } | | 712 | + } | | 713 | + }, | 671 | 714 | } | 672 | 715 |
| 673 | 716 | export const Model = z | |
|