Skip to content
4 changes: 4 additions & 0 deletions packages/opencode/src/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export namespace Global {
cache,
config,
state,
// Allow overriding models.dev URL for offline deployments
get modelsDevUrl() {
return process.env.OPENCODE_MODELS_URL || "https://models.dev"
},
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/opencode/src/provider/models-macro.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Global } from "../global"

export async function data() {
const path = Bun.env.MODELS_DEV_API_JSON
if (path) {
Expand All @@ -6,6 +8,7 @@ export async function data() {
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
}
6 changes: 4 additions & 2 deletions packages/opencode/src/provider/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export namespace ModelsDev {
const json = await data()
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>
}

Expand All @@ -95,7 +96,8 @@ export namespace ModelsDev {
log.info("refreshing", {
file,
})
const result = await fetch("https://models.dev/api.json", {
const url = Global.Path.modelsDevUrl
const result = await fetch(`${url}/api.json`, {
headers: {
"User-Agent": Installation.USER_AGENT,
},
Expand Down
4 changes: 3 additions & 1 deletion packages/opencode/test/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "path"
import fs from "fs/promises"
import fsSync from "fs"
import { afterAll } from "bun:test"
const { Global } = await import("../src/global")

const dir = path.join(os.tmpdir(), "opencode-test-data-" + process.pid)
await fs.mkdir(dir, { recursive: true })
Expand All @@ -27,7 +28,8 @@ process.env["XDG_STATE_HOME"] = path.join(dir, "state")
const cacheDir = path.join(dir, "cache", "opencode")
await fs.mkdir(cacheDir, { recursive: true })
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) {
await fs.writeFile(path.join(cacheDir, "models.json"), await response.text())
}
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ function providerIconsPlugin() {
}

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((json) => Object.keys(json))
await Promise.all(
providers.map((provider) =>
fetch(`https://models.dev/logos/${provider}.svg`)
fetch(`${url}/logos/${provider}.svg`)
.then((res) => res.text())
.then((svg) => fs.writeFileSync(`./src/assets/icons/provider/${provider}.svg`, svg)),
),
Expand Down