Skip to content

Commit e3c1861

Browse files
committed
get rid of models.dev macro
1 parent d9eebba commit e3c1861

File tree

7 files changed

+48
-43
lines changed

7 files changed

+48
-43
lines changed

packages/opencode/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ research
22
dist
33
gen
44
app.log
5+
src/provider/models-snapshot.ts

packages/opencode/script/build.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ process.chdir(dir)
1515
import pkg from "../package.json"
1616
import { Script } from "@opencode-ai/script"
1717

18+
// Fetch and generate models.dev snapshot
19+
const modelsData = await fetch(`https://models.dev/api.json`).then((x) => x.text())
20+
await Bun.write(
21+
path.join(dir, "src/provider/models-snapshot.ts"),
22+
`// Auto-generated by build.ts - do not edit\nexport const snapshot = ${modelsData} as const\n`,
23+
)
24+
console.log("Generated models-snapshot.ts")
25+
1826
const singleFlag = process.argv.includes("--single")
1927
const baselineFlag = process.argv.includes("--baseline")
2028
const skipInstall = process.argv.includes("--skip-install")

packages/opencode/src/flag/flag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export namespace Flag {
4646
export const OPENCODE_EXPERIMENTAL_LSP_TOOL = OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_LSP_TOOL")
4747
export const OPENCODE_DISABLE_FILETIME_CHECK = truthy("OPENCODE_DISABLE_FILETIME_CHECK")
4848
export const OPENCODE_EXPERIMENTAL_PLAN_MODE = OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_PLAN_MODE")
49+
export const OPENCODE_MODELS_URL = process.env["OPENCODE_MODELS_URL"]
4950

5051
function number(key: string) {
5152
const value = process.env[key]

packages/opencode/src/global/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ export namespace Global {
2222
cache,
2323
config,
2424
state,
25-
// Allow overriding models.dev URL for offline deployments
26-
get modelsDevUrl() {
27-
return process.env.OPENCODE_MODELS_URL || "https://models.dev"
28-
},
2925
}
3026
}
3127

packages/opencode/src/provider/models-macro.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/opencode/src/provider/models.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { Global } from "../global"
22
import { Log } from "../util/log"
33
import path from "path"
44
import z from "zod"
5-
import { data } from "./models-macro" with { type: "macro" }
65
import { Installation } from "../installation"
76
import { Flag } from "../flag/flag"
7+
import { lazy } from "@/util/lazy"
8+
9+
// Try to import bundled snapshot (generated at build time)
10+
// Falls back to undefined in dev mode when snapshot doesn't exist
11+
/* @ts-ignore */
12+
const SNAPSHOT = await import("./models-snapshot")
13+
.then((m) => m.snapshot as Record<string, unknown>)
14+
.catch(() => undefined)
815

916
export namespace ModelsDev {
1017
const log = Log.create({ service: "models.dev" })
@@ -76,18 +83,24 @@ export namespace ModelsDev {
7683

7784
export type Provider = z.infer<typeof Provider>
7885

79-
export async function get() {
80-
refresh()
86+
function url() {
87+
return Flag.OPENCODE_MODELS_URL || "https://models.dev"
88+
}
89+
90+
export const Data = lazy(async () => {
8191
const file = Bun.file(filepath)
8292
const result = await file.json().catch(() => {})
83-
if (result) return result as Record<string, Provider>
84-
if (typeof data === "function") {
85-
const json = await data()
86-
return JSON.parse(json) as Record<string, Provider>
87-
}
88-
const url = Global.Path.modelsDevUrl
89-
const json = await fetch(`${url}/api.json`).then((x) => x.text())
90-
return JSON.parse(json) as Record<string, Provider>
93+
if (result) return result
94+
if (SNAPSHOT) return SNAPSHOT
95+
if (Flag.OPENCODE_DISABLE_MODELS_FETCH) return {}
96+
const json = await fetch(`${url()}/api.json`).then((x) => x.text())
97+
return JSON.parse(json)
98+
})
99+
100+
export async function get() {
101+
refresh()
102+
const result = await Data()
103+
return result as Record<string, Provider>
91104
}
92105

93106
export async function refresh() {
@@ -96,8 +109,7 @@ export namespace ModelsDev {
96109
log.info("refreshing", {
97110
file,
98111
})
99-
const url = Global.Path.modelsDevUrl
100-
const result = await fetch(`${url}/api.json`, {
112+
const result = await fetch(`${url()}/api.json`, {
101113
headers: {
102114
"User-Agent": Installation.USER_AGENT,
103115
},
@@ -107,8 +119,18 @@ export namespace ModelsDev {
107119
error: e,
108120
})
109121
})
110-
if (result && result.ok) await Bun.write(file, await result.text())
122+
if (result && result.ok) {
123+
await Bun.write(file, await result.text())
124+
ModelsDev.Data.reset()
125+
}
111126
}
112127
}
113128

114-
setInterval(() => ModelsDev.refresh(), 60 * 1000 * 60).unref()
129+
if (!Flag.OPENCODE_DISABLE_MODELS_FETCH) {
130+
setInterval(
131+
async () => {
132+
await ModelsDev.refresh()
133+
},
134+
60 * 1000 * 60,
135+
).unref()
136+
}

packages/opencode/test/preload.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path from "path"
55
import fs from "fs/promises"
66
import fsSync from "fs"
77
import { afterAll } from "bun:test"
8-
const { Global } = await import("../src/global")
98

109
const dir = path.join(os.tmpdir(), "opencode-test-data-" + process.pid)
1110
await fs.mkdir(dir, { recursive: true })
@@ -23,18 +22,10 @@ process.env["XDG_CACHE_HOME"] = path.join(dir, "cache")
2322
process.env["XDG_CONFIG_HOME"] = path.join(dir, "config")
2423
process.env["XDG_STATE_HOME"] = path.join(dir, "state")
2524

26-
// Pre-fetch models.json so tests don't need the macro fallback
27-
// Also write the cache version file to prevent global/index.ts from clearing the cache
25+
// Write the cache version file to prevent global/index.ts from clearing the cache
2826
const cacheDir = path.join(dir, "cache", "opencode")
2927
await fs.mkdir(cacheDir, { recursive: true })
3028
await fs.writeFile(path.join(cacheDir, "version"), "14")
31-
const url = Global.Path.modelsDevUrl
32-
const response = await fetch(`${url}/api.json`)
33-
if (response.ok) {
34-
await fs.writeFile(path.join(cacheDir, "models.json"), await response.text())
35-
}
36-
// Disable models.dev refresh to avoid race conditions during tests
37-
process.env["OPENCODE_DISABLE_MODELS_FETCH"] = "true"
3829

3930
// Clear provider env vars to ensure clean test state
4031
delete process.env["ANTHROPIC_API_KEY"]

0 commit comments

Comments
 (0)