feat: Make the models.dev domain configurable for offline environments (#9258)

pull/5339/head
Bart Broere 2026-01-21 18:23:07 +01:00 committed by GitHub
parent 95b17bcf5e
commit 8df09abb1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 6 deletions

View File

@ -22,6 +22,10 @@ export namespace Global {
cache, cache,
config, config,
state, state,
// Allow overriding models.dev URL for offline deployments
get modelsDevUrl() {
return process.env.OPENCODE_MODELS_URL || "https://models.dev"
},
} }
} }

View File

@ -1,3 +1,5 @@
import { Global } from "../global"
export async function data() { export async function data() {
const path = Bun.env.MODELS_DEV_API_JSON const path = Bun.env.MODELS_DEV_API_JSON
if (path) { if (path) {
@ -6,6 +8,7 @@ export async function data() {
return await file.text() return await file.text()
} }
} }
const json = await fetch("https://models.dev/api.json").then((x) => x.text()) const url = Global.Path.modelsDevUrl
const json = await fetch(`${url}/api.json`).then((x) => x.text())
return json return json
} }

View File

@ -85,7 +85,8 @@ export namespace ModelsDev {
const json = await data() const json = await data()
return JSON.parse(json) as Record<string, Provider> return JSON.parse(json) as Record<string, Provider>
} }
const json = await fetch("https://models.dev/api.json").then((x) => x.text()) const url = Global.Path.modelsDevUrl
const json = await fetch(`${url}/api.json`).then((x) => x.text())
return JSON.parse(json) as Record<string, Provider> return JSON.parse(json) as Record<string, Provider>
} }
@ -95,7 +96,8 @@ export namespace ModelsDev {
log.info("refreshing", { log.info("refreshing", {
file, file,
}) })
const result = await fetch("https://models.dev/api.json", { const url = Global.Path.modelsDevUrl
const result = await fetch(`${url}/api.json`, {
headers: { headers: {
"User-Agent": Installation.USER_AGENT, "User-Agent": Installation.USER_AGENT,
}, },

View File

@ -5,6 +5,7 @@ import path from "path"
import fs from "fs/promises" import fs from "fs/promises"
import fsSync from "fs" import fsSync from "fs"
import { afterAll } from "bun:test" import { afterAll } from "bun:test"
const { Global } = await import("../src/global")
const dir = path.join(os.tmpdir(), "opencode-test-data-" + process.pid) const dir = path.join(os.tmpdir(), "opencode-test-data-" + process.pid)
await fs.mkdir(dir, { recursive: true }) await fs.mkdir(dir, { recursive: true })
@ -27,7 +28,8 @@ process.env["XDG_STATE_HOME"] = path.join(dir, "state")
const cacheDir = path.join(dir, "cache", "opencode") const cacheDir = path.join(dir, "cache", "opencode")
await fs.mkdir(cacheDir, { recursive: true }) await fs.mkdir(cacheDir, { recursive: true })
await fs.writeFile(path.join(cacheDir, "version"), "14") await fs.writeFile(path.join(cacheDir, "version"), "14")
const response = await fetch("https://models.dev/api.json") const url = Global.Path.modelsDevUrl
const response = await fetch(`${url}/api.json`)
if (response.ok) { if (response.ok) {
await fs.writeFile(path.join(cacheDir, "models.json"), await response.text()) await fs.writeFile(path.join(cacheDir, "models.json"), await response.text())
} }

View File

@ -45,12 +45,13 @@ function providerIconsPlugin() {
} }
async function fetchProviderIcons() { async function fetchProviderIcons() {
const providers = await fetch("https://models.dev/api.json") const url = process.env.OPENCODE_MODELS_URL || "https://models.dev"
const providers = await fetch(`${url}/api.json`)
.then((res) => res.json()) .then((res) => res.json())
.then((json) => Object.keys(json)) .then((json) => Object.keys(json))
await Promise.all( await Promise.all(
providers.map((provider) => providers.map((provider) =>
fetch(`https://models.dev/logos/${provider}.svg`) fetch(`${url}/logos/${provider}.svg`)
.then((res) => res.text()) .then((res) => res.text())
.then((svg) => fs.writeFileSync(`./src/assets/icons/provider/${provider}.svg`, svg)), .then((svg) => fs.writeFileSync(`./src/assets/icons/provider/${provider}.svg`, svg)),
), ),