GeezerVison is showing github.com — feat(provider): add dynamic model discovery for custom providers · anomalyco/opencode@7c5145c
File treeExpand file treeCollapse file tree Open diff view settings server/routes/instance/httpapi
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/dialog-custom-provider-form.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -154,5 +154,6 @@ let row = 0 | 154 | 154 |
| 155 | 155 | const nextRow = () => `row-${row++}` | 156 | 156 |
| 157 | | -export const modelRow = (): ModelRow => ({ row: nextRow(), id: "", name: "", err: {} }) | | 157 | +export const modelRow = (id = "", name = ""): ModelRow => ({ row: nextRow(), id, name, err: {} }) | 158 | 158 | export const headerRow = (): HeaderRow => ({ row: nextRow(), key: "", value: "", err: {} }) | | 159 | +export const modelsFromIDs = (ids: string[]): ModelRow[] => ids.map((id) => modelRow(id, id)) |
Collapse file Copy file name to clipboard +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, test } from "bun:test" | | 2 | +import { parseDiscoveryResult } from "./dialog-custom-provider-models" | | 3 | + | | 4 | +describe("parseDiscoveryResult", () => { | | 5 | + test("returns trimmed, deduped ids for a successful result", () => { | | 6 | + const result = parseDiscoveryResult({ ok: true, ids: ["model-a", "model-a", " ", " b "] }) | | 7 | + expect(result).toEqual({ ok: true, models: ["model-a", "b"] }) | | 8 | + }) | | 9 | + | | 10 | + test("translates every error kind", () => { | | 11 | + for (const kind of ["invalidUrl", "unauthorized", "invalidFormat", "timeout", "failed"] as const) { | | 12 | + expect(parseDiscoveryResult({ ok: false, kind })).toEqual({ ok: false, error: kind }) | | 13 | + } | | 14 | + }) | | 15 | + | | 16 | + test("preserves the optional message field without surfacing it", () => { | | 17 | + expect(parseDiscoveryResult({ ok: false, kind: "unauthorized", message: "bad key" })).toEqual({ | | 18 | + ok: false, | | 19 | + error: "unauthorized", | | 20 | + }) | | 21 | + }) | | 22 | + | | 23 | + test("reports invalidFormat when the ok payload lacks a string id array", () => { | | 24 | + expect(parseDiscoveryResult({ ok: true, ids: "models" })).toEqual({ ok: false, error: "invalidFormat" }) | | 25 | + expect(parseDiscoveryResult({ ok: true, ids: [42] })).toEqual({ ok: false, error: "invalidFormat" }) | | 26 | + }) | | 27 | + | | 28 | + test("reports failed for an unknown error kind", () => { | | 29 | + expect(parseDiscoveryResult({ ok: false, kind: "unexpected" })).toEqual({ ok: false, error: "failed" }) | | 30 | + }) | | 31 | + | | 32 | + test("reports failed for an unexpected payload shape", () => { | | 33 | + expect(parseDiscoveryResult(null)).toEqual({ ok: false, error: "failed" }) | | 34 | + expect(parseDiscoveryResult({ object: "list" })).toEqual({ ok: false, error: "failed" }) | | 35 | + }) | | 36 | +}) |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export type LoadModelsError = "invalidUrl" | "unauthorized" | "invalidFormat" | "timeout" | "failed" | | 2 | + | | 3 | +export type LoadModelsResult = { ok: true; models: string[] } | { ok: false; error: LoadModelsError } | | 4 | + | | 5 | +export function parseDiscoveryResult(data: unknown): LoadModelsResult { | | 6 | + if (data !== null && typeof data === "object" && "ok" in data) { | | 7 | + if (data.ok === true) { | | 8 | + const ids = (data as { ids?: unknown }).ids | | 9 | + if (Array.isArray(ids) && ids.every((id) => typeof id === "string")) { | | 10 | + return { ok: true, models: [...new Set(ids.map((id) => id.trim()).filter(Boolean))] } | | 11 | + } | | 12 | + return { ok: false, error: "invalidFormat" } | | 13 | + } | | 14 | + if (data.ok === false) { | | 15 | + const kind = (data as { kind?: unknown }).kind | | 16 | + if (typeof kind === "string" && isLoadModelError(kind)) return { ok: false, error: kind } | | 17 | + } | | 18 | + } | | 19 | + return { ok: false, error: "failed" } | | 20 | +} | | 21 | + | | 22 | +function isLoadModelError(value: string): value is LoadModelsError { | | 23 | + return ( | | 24 | + value === "invalidUrl" || | | 25 | + value === "unauthorized" || | | 26 | + value === "invalidFormat" || | | 27 | + value === "timeout" || | | 28 | + value === "failed" | | 29 | + ) | | 30 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/dialog-custom-provider.test.ts +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, test } from "bun:test" | 2 | | -import { validateCustomProvider } from "./dialog-custom-provider-form" | | 2 | +import { modelsFromIDs, validateCustomProvider } from "./dialog-custom-provider-form" | 3 | 3 |
| 4 | 4 | const t = (key: string) => key | 5 | 5 |
| | 6 | +describe("modelsFromIDs", () => { | | 7 | + test("builds one editable row per id with id as default name", () => { | | 8 | + const rows = modelsFromIDs(["model-a", "model-b"]) | | 9 | + | | 10 | + expect(rows).toHaveLength(2) | | 11 | + expect(rows[0]!.id).toBe("model-a") | | 12 | + expect(rows[0]!.name).toBe("model-a") | | 13 | + expect(rows[0]!.err).toEqual({}) | | 14 | + expect(rows[1]!.id).toBe("model-b") | | 15 | + expect(rows[1]!.name).toBe("model-b") | | 16 | + }) | | 17 | +}) | | 18 | + | 6 | 19 | describe("validateCustomProvider", () => { | 7 | 20 | test("builds trimmed config payload", () => { | 8 | 21 | const result = validateCustomProvider({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/dialog-custom-provider.tsx +68-2Lines changed: 68 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,24 @@ import { ExternalLink } from "@/components/external-link" | 12 | 12 | import { useServerSDK } from "@/context/server-sdk" | 13 | 13 | import { useServerSync } from "@/context/server-sync" | 14 | 14 | import { useLanguage } from "@/context/language" | 15 | | -import { type FormState, headerRow, modelRow, validateCustomProvider } from "./dialog-custom-provider-form" | | 15 | +import { parseDiscoveryResult } from "./dialog-custom-provider-models" | | 16 | +import { | | 17 | + type FormState, | | 18 | + headerRow, | | 19 | + modelRow, | | 20 | + modelsFromIDs, | | 21 | + validateCustomProvider, | | 22 | +} from "./dialog-custom-provider-form" | | 23 | + | | 24 | +function headerMap(rows: FormState["headers"]): Record<string, string> { | | 25 | + const out: Record<string, string> = {} | | 26 | + for (const row of rows) { | | 27 | + const key = row.key.trim() | | 28 | + const value = row.value.trim() | | 29 | + if (key && value) out[key] = value | | 30 | + } | | 31 | + return out | | 32 | +} | 16 | 33 |
| 17 | 34 | type Props = { | 18 | 35 | onBack: () => void | @@ -175,6 +192,35 @@ export function CustomProviderForm(props: { autofocus?: boolean } = {}) { | 175 | 192 | saveMutation.mutate(result) | 176 | 193 | } | 177 | 194 |
| | 195 | + const loadModelsMutation = useMutation(() => ({ | | 196 | + mutationFn: async (input: { baseURL: string; apiKey: string; headers?: Record<string, string> }) => { | | 197 | + const res = await serverSDK().client.provider.discover({ | | 198 | + discoverModelsPayload: { | | 199 | + baseURL: input.baseURL, | | 200 | + ...(input.apiKey ? { apiKey: input.apiKey } : {}), | | 201 | + ...(input.headers ? { headers: input.headers } : {}), | | 202 | + }, | | 203 | + }) | | 204 | + return parseDiscoveryResult(res.data) | | 205 | + }, | | 206 | + onSuccess: (result) => { | | 207 | + if (!result.ok) { | | 208 | + showToast({ | | 209 | + title: language.t(`provider.custom.models.load.${result.error}` as Parameters<typeof language.t>[0]), | | 210 | + }) | | 211 | + return | | 212 | + } | | 213 | + if (result.models.length === 0) { | | 214 | + showToast({ title: language.t("provider.custom.models.load.empty") }) | | 215 | + return | | 216 | + } | | 217 | + setForm("models", modelsFromIDs(result.models)) | | 218 | + }, | | 219 | + onError: () => { | | 220 | + showToast({ title: language.t("provider.custom.models.load.failed") }) | | 221 | + }, | | 222 | + })) | | 223 | + | 178 | 224 | return ( | 179 | 225 | <div class="flex flex-col gap-6 px-2.5 pb-3 overflow-y-auto max-h-[60vh]"> | 180 | 226 | <div class="px-2.5 flex gap-4 items-center"> | @@ -228,7 +274,27 @@ export function CustomProviderForm(props: { autofocus?: boolean } = {}) { | 228 | 274 | </div> | 229 | 275 |
| 230 | 276 | <div class="flex flex-col gap-3"> | 231 | | - <label class="text-12-medium text-text-weak">{language.t("provider.custom.models.label")}</label> | | 277 | + <div class="flex items-center justify-between gap-2"> | | 278 | + <label class="text-12-medium text-text-weak">{language.t("provider.custom.models.label")}</label> | | 279 | + <Button | | 280 | + type="button" | | 281 | + size="small" | | 282 | + variant="ghost" | | 283 | + icon="download" | | 284 | + onClick={() => | | 285 | + void loadModelsMutation.mutate({ | | 286 | + baseURL: form.baseURL, | | 287 | + apiKey: form.apiKey, | | 288 | + headers: headerMap(form.headers), | | 289 | + }) | | 290 | + } | | 291 | + disabled={loadModelsMutation.isPending} | | 292 | + > | | 293 | + {loadModelsMutation.isPending | | 294 | + ? language.t("provider.custom.models.loading") | | 295 | + : language.t("provider.custom.models.load")} | | 296 | + </Button> | | 297 | + </div> | 232 | 298 | <For each={form.models}> | 233 | 299 | {(m, i) => ( | 234 | 300 | <div class="flex gap-2 items-start" data-row={m.row}> | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/i18n/am.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -265,6 +265,14 @@ export const dict = { | 265 | 265 | "provider.custom.models.name.placeholder": "የማሳያ ስም", | 266 | 266 | "provider.custom.models.remove": "ሞዴሉን አስወግድ", | 267 | 267 | "provider.custom.models.add": "ሞዴል አክል", | | 268 | + "provider.custom.models.load": "በራስ-ግምት ይጫን", | | 269 | + "provider.custom.models.loading": "በመጫን ላይ...", | | 270 | + "provider.custom.models.load.invalidUrl": "የሚሰራ መሰረት URL ያስገቡ", | | 271 | + "provider.custom.models.load.unauthorized": "መዳረሻ የለም። የAPI ቁልፍ ያረጋግጡ", | | 272 | + "provider.custom.models.load.invalidFormat": "የሰርቨሩ ምላሽ ትክክለኛ የሞዴል ዝርዝር አልነበረውም", | | 273 | + "provider.custom.models.load.timeout": "ሞዴሎችን በመጫን ላይ ጊዜው አልፏል", | | 274 | + "provider.custom.models.load.failed": "ሞዴሎችን መጫን አልተሳካም", | | 275 | + "provider.custom.models.load.empty": "ሞዴሎች አልተገኙም", | 268 | 276 | "provider.custom.headers.label": "ራስጌዎች (አማራጭ)", | 269 | 277 | "provider.custom.headers.key.label": "ራስጌ", | 270 | 278 | "provider.custom.headers.key.placeholder": "ራስጌ-ስም", | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/i18n/ar.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -275,6 +275,14 @@ export const dict = { | 275 | 275 | "provider.custom.models.name.placeholder": "اسم العرض", | 276 | 276 | "provider.custom.models.remove": "إزالة النموذج", | 277 | 277 | "provider.custom.models.add": "إضافة نموذج", | | 278 | + "provider.custom.models.load": "تحميل ديناميكي", | | 279 | + "provider.custom.models.loading": "جارٍ التحميل...", | | 280 | + "provider.custom.models.load.invalidUrl": "أدخل عنوان URL أساسي صالح", | | 281 | + "provider.custom.models.load.unauthorized": "غير مصرح. تحقق من مفتاح API", | | 282 | + "provider.custom.models.load.invalidFormat": "استجابة الخادم لا تحتوي على قائمة نماذج صالحة", | | 283 | + "provider.custom.models.load.timeout": "انتهت مهلة تحميل النماذج", | | 284 | + "provider.custom.models.load.failed": "فشل تحميل النماذج", | | 285 | + "provider.custom.models.load.empty": "لم يتم العثور على نماذج", | 278 | 286 | "provider.custom.headers.label": "الترويسات (اختياري)", | 279 | 287 | "provider.custom.headers.key.label": "ترويسة", | 280 | 288 | "provider.custom.headers.key.placeholder": "Header-Name", | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/i18n/az.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -272,6 +272,14 @@ export const dict = { | 272 | 272 | "provider.custom.models.name.placeholder": "Görünən ad", | 273 | 273 | "provider.custom.models.remove": "Modeli sil", | 274 | 274 | "provider.custom.models.add": "Model əlavə et", | | 275 | + "provider.custom.models.load": "Dinamik yüklə", | | 276 | + "provider.custom.models.loading": "Yüklənir...", | | 277 | + "provider.custom.models.load.invalidUrl": "Düzgün əsas URL daxil edin", | | 278 | + "provider.custom.models.load.unauthorized": "İcazə yoxdur. API açarını yoxlayın", | | 279 | + "provider.custom.models.load.invalidFormat": "Server cavabı düzgün model siyahısı deyil", | | 280 | + "provider.custom.models.load.timeout": "Modelləri yükləyərkən vaxt bitdi", | | 281 | + "provider.custom.models.load.failed": "Modelləri yükləmək mümkün olmadı", | | 282 | + "provider.custom.models.load.empty": "Model tapılmadı", | 275 | 283 | "provider.custom.headers.label": "Başlıqlar (istəyə bağlı)", | 276 | 284 | "provider.custom.headers.key.label": "Başlıq", | 277 | 285 | "provider.custom.headers.key.placeholder": "Başlıq-Adı", | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/i18n/bg.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -272,6 +272,14 @@ export const dict = { | 272 | 272 | "provider.custom.models.name.placeholder": "Екранно име", | 273 | 273 | "provider.custom.models.remove": "Премахване на модела", | 274 | 274 | "provider.custom.models.add": "Добавете модел", | | 275 | + "provider.custom.models.load": "Зареди динамично", | | 276 | + "provider.custom.models.loading": "Зарежда се...", | | 277 | + "provider.custom.models.load.invalidUrl": "Въведете валидна основа URL", | | 278 | + "provider.custom.models.load.unauthorized": "Неоторизиран. Проверете API ключа", | | 279 | + "provider.custom.models.load.invalidFormat": "Отговорът на сървъра не съдържаше валиден списък с модели", | | 280 | + "provider.custom.models.load.timeout": "Изтече време при зареждане на модели", | | 281 | + "provider.custom.models.load.failed": "Неуспешно зареждане на модели", | | 282 | + "provider.custom.models.load.empty": "Не са намерени модели", | 275 | 283 | "provider.custom.headers.label": "Заглавки (по избор)", | 276 | 284 | "provider.custom.headers.key.label": "Заглавка", | 277 | 285 | "provider.custom.headers.key.placeholder": "Заглавие-Име", | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/i18n/bn.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -269,6 +269,14 @@ export const dict: Record<string, string> = { | 269 | 269 | "provider.custom.models.name.placeholder": "প্রদর্শনের নাম", | 270 | 270 | "provider.custom.models.remove": "মডেল সরান", | 271 | 271 | "provider.custom.models.add": "মডেল যোগ করুন", | | 272 | + "provider.custom.models.load": "গতিশীলভাবে লোড করুন", | | 273 | + "provider.custom.models.loading": "লোড হচ্ছে...", | | 274 | + "provider.custom.models.load.invalidUrl": "একটি বৈধ ভিত্তি URL লিখুন", | | 275 | + "provider.custom.models.load.unauthorized": "অননুমোদিত। API কী পরীক্ষা করুন", | | 276 | + "provider.custom.models.load.invalidFormat": "সার্ভারের প্রতিক্রিয়ায় বৈধ মডেল তালিকা নেই", | | 277 | + "provider.custom.models.load.timeout": "মডেল লোড করার সময় শেষ হয়ে গেছে", | | 278 | + "provider.custom.models.load.failed": "মডেল লোড করতে ব্যর্থ", | | 279 | + "provider.custom.models.load.empty": "কোনো মডেল পাওয়া যায়নি", | 272 | 280 | "provider.custom.headers.label": "হেডার (ঐচ্ছিক)", | 273 | 281 | "provider.custom.headers.key.label": "হেডার", | 274 | 282 | "provider.custom.headers.key.placeholder": "হেডার-নাম", | |
|