Skip to content

Commit c254ba8

Browse files
authored
core: embed models.dev snapshot instead of compile-time define (#41838)
1 parent d8e126b commit c254ba8

10 files changed

Lines changed: 87 additions & 30 deletions

File tree

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
packages/core/migration/**/snapshot.json linguist-generated
22
packages/core/src/database/migration.gen.ts linguist-generated
3+
packages/core/src/models-dev/snapshot.txt linguist-generated
34
packages/core/src/**/*.txt text eol=lf

‎packages/cli/script/build-node.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import { spawnSync } from "node:child_process"
44
import { createHash } from "node:crypto"
5-
import { chmod, copyFile, mkdir, mkdtemp, realpath, rename, rm, stat, writeFile } from "node:fs/promises"
5+
import { chmod, copyFile, mkdir, mkdtemp, readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises"
66
import os from "node:os"
77
import path from "node:path"
88
import { build } from "vite"
99
import { Script } from "@opencode-ai/script"
1010
import pkg from "../package.json"
11-
import { modelsData } from "./generate"
1211
import { collectNodeAssets, copyNodeAssets, hashNodeAssets, seaAssetMap } from "./node-assets"
1312
import { mainConfig } from "../vite.node.config"
1413
import { nodeExecArgv, nodeTarget, type NodeTarget } from "../src/node/target"
@@ -58,6 +57,25 @@ const builder =
5857
: undefined
5958
const appArchive = await buildAppArchive(Script.channel)
6059

60+
// Vite silently rewrites text imports of known asset types (.txt) to asset
61+
// URL strings when the raw-text plugin doesn't intercept them first — the
62+
// bundle still builds and `--help` still runs, so only content assertions
63+
// catch it. Guards the models.dev snapshot and the prompt/tool description
64+
// text that ships inside the bundle.
65+
async function assertTextImportsInlined(bundlePath: string) {
66+
const bundle = await readFile(bundlePath, "utf8")
67+
const markers = [
68+
{ marker: '"zhipuai"', source: "models-dev snapshot" },
69+
{ marker: "/assets/snapshot", source: "models-dev snapshot inlined as asset URL", forbidden: true },
70+
{ marker: '="/assets/', source: "text import inlined as asset URL", forbidden: true },
71+
]
72+
for (const { marker, source, forbidden } of markers) {
73+
const present = bundle.includes(marker)
74+
if (forbidden ? present : !present)
75+
throw new Error(`${bundlePath}: ${source} — text imports are not inlined as content (marker ${marker})`)
76+
}
77+
}
78+
6179
for (const target of targets) {
6280
console.log(`building cli-node-${targetName(target)}`)
6381
const assets = await collectNodeAssets(target)
@@ -66,13 +84,13 @@ for (const target of targets) {
6684
const input = {
6785
version: Script.version,
6886
channel: Script.channel,
69-
models: modelsData,
7087
assetHash,
7188
target,
7289
appArchive,
7390
}
7491
await copyNodeAssets(assets)
7592
await build(mainConfig(input))
93+
await assertTextImportsInlined("dist-node/opencode.mjs")
7694

7795
const host = target.platform === process.platform && target.arch === process.arch
7896
if (host) {

‎packages/cli/script/build.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { Script } from "@opencode-ai/script"
77
import { createSolidTransformPlugin } from "@opentui/solid/bun-plugin"
88
import type { BunPlugin } from "bun"
99
import pkg from "../package.json"
10-
import { modelsData } from "./generate"
1110
import { buildAppArchive } from "./app-assets"
1211

1312
const dir = path.resolve(import.meta.dirname, "..")
@@ -115,7 +114,6 @@ for (const item of targets) {
115114
define: {
116115
OPENCODE_VERSION: `'${Script.version}'`,
117116
OPENCODE_CLI_NAME: `'${binary}'`,
118-
OPENCODE_MODELS_DEV: modelsData,
119117
OPENCODE_CHANNEL: `'${Script.channel}'`,
120118
OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "undefined",
121119
// FFF_LIBC selects the fff native lib variant: "musl" or "gnu".

‎packages/cli/script/generate.ts‎

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

‎packages/cli/vite.node.config.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ const dir = import.meta.dirname
1010
function rawTextPlugin(): Plugin {
1111
return {
1212
name: "opencode:raw-text",
13+
// "pre" is load-bearing for .txt: Vite's built-in asset plugin claims
14+
// known asset types (.txt among them) ahead of normal-priority plugins,
15+
// replacing the import with an asset URL string instead of the content.
16+
// .md only ever worked without it because .md is not a known asset type.
17+
enforce: "pre",
1318
async load(id) {
14-
if (!id.endsWith(".md")) return
19+
if (!id.endsWith(".md") && !id.endsWith(".txt")) return
1520
return `export default ${JSON.stringify(await readFile(id, "utf8"))}`
1621
},
1722
}
@@ -222,7 +227,6 @@ if (process.platform === "linux") process.env.OPENTUI_LIBC = "glibc"`
222227
export type NodeBuildInput = {
223228
readonly version: string
224229
readonly channel: string
225-
readonly models: string
226230
readonly assetHash: string
227231
readonly target: NodeTarget
228232
readonly appArchive: string
@@ -248,7 +252,6 @@ export function mainConfig(input: NodeBuildInput): UserConfig {
248252
define: {
249253
OPENCODE_VERSION: JSON.stringify(input.version),
250254
OPENCODE_CLI_NAME: JSON.stringify("opencode2-node"),
251-
OPENCODE_MODELS_DEV: input.models,
252255
OPENCODE_CHANNEL: JSON.stringify(input.channel),
253256
OPENCODE_LIBC: input.target.platform === "linux" ? JSON.stringify("glibc") : "undefined",
254257
FFF_LIBC: input.target.platform === "linux" ? JSON.stringify("gnu") : "undefined",
@@ -271,7 +274,6 @@ export function mainConfig(input: NodeBuildInput): UserConfig {
271274
export default mainConfig({
272275
version: process.env.OPENCODE_VERSION ?? "local",
273276
channel: process.env.OPENCODE_CHANNEL ?? "local",
274-
models: "undefined",
275277
assetHash: "local",
276278
target: nodeTarget(process.platform, process.arch),
277279
appArchive: "",

‎packages/core/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"migration": "bun run script/migration.ts",
1111
"fix-node-pty": "bun run script/fix-node-pty.ts",
1212
"benchmark:location": "bun run script/benchmark-location.ts",
13+
"update-models-snapshot": "bun run script/update-models-snapshot.ts",
1314
"test": "bun test --only-failures",
1415
"typecheck": "tsgo -b tsconfig.json tsconfig.tests.json"
1516
},
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bun
2+
/**
3+
* Refreshes the bundled models.dev catalog snapshot at src/models-dev/snapshot.txt.
4+
* The snapshot is the boot-time floor for the catalog when no cache entry exists
5+
* and fetching is disabled or unavailable; live fetch still refreshes on top.
6+
*/
7+
const source = process.env.OPENCODE_MODELS_URL || "https://models.opencode.ai"
8+
const response = await fetch(`${source}/api.json`)
9+
if (!response.ok) {
10+
console.error(`Failed to fetch ${source}/api.json: ${response.status} ${response.statusText}`)
11+
process.exit(1)
12+
}
13+
const text = await response.text()
14+
const parsed: unknown = JSON.parse(text)
15+
// A floor, not equality: guards against committing an error page or a
16+
// truncated body that still parses as a small object.
17+
const MINIMUM_PROVIDERS = 100
18+
if (typeof parsed !== "object" || parsed === null || Object.keys(parsed).length < MINIMUM_PROVIDERS) {
19+
console.error(`Fetched catalog has fewer than ${MINIMUM_PROVIDERS} providers; refusing to write snapshot`)
20+
process.exit(1)
21+
}
22+
const target = new URL("../src/models-dev/snapshot.txt", import.meta.url)
23+
await Bun.write(target, text)
24+
console.log(`Wrote ${Object.keys(parsed).length} providers (${text.length} bytes) to ${Bun.fileURLToPath(target)}`)

‎packages/core/src/models-dev.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { httpClient } from "@opencode-ai/util/effect/app-node-platform"
1111
import { Model } from "./model.js"
1212
import { Provider } from "./provider.js"
1313
import { KV } from "./kv.js"
14+
import snapshotText from "./models-dev/snapshot.txt" with { type: "text" }
1415

1516
export const CatalogModelStatus = Schema.Literals(["alpha", "beta", "deprecated"])
1617
export type CatalogModelStatus = typeof CatalogModelStatus.Type
@@ -519,8 +520,6 @@ function modelInfo(
519520

520521
export { Event } from "@opencode-ai/schema/models-dev"
521522

522-
declare const OPENCODE_MODELS_DEV: Record<string, SourceProvider> | undefined
523-
524523
export interface Interface {
525524
readonly get: () => Effect.Effect<readonly Snapshot[]>
526525
readonly refresh: (force?: boolean) => Effect.Effect<void>
@@ -530,12 +529,17 @@ export const Options = Schema.Struct({
530529
url: Schema.optional(Schema.String),
531530
file: Schema.optional(Schema.String),
532531
fetch: Schema.optional(Schema.Boolean),
532+
snapshot: Schema.optional(Schema.Boolean),
533533
})
534534
export type Options = typeof Options.Type
535535

536536
export class Service extends Context.Service<Service, Interface>()("@opencode/ModelsDev") {}
537537

538538
const CatalogJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown))
539+
const decodeCatalog = (text: string) =>
540+
Schema.decodeUnknownEffect(CatalogJson)(text).pipe(
541+
Effect.map((catalog) => catalog as Record<string, SourceProvider>),
542+
)
539543
const Cache = Schema.Struct({
540544
updatedAt: Schema.Number,
541545
body: CatalogJson,
@@ -605,13 +609,15 @@ export const layer = (options?: Options) =>
605609
)
606610
: Effect.succeed(undefined)
607611

608-
const loadSnapshot = Effect.sync(() =>
609-
typeof OPENCODE_MODELS_DEV === "undefined" ? undefined : OPENCODE_MODELS_DEV,
610-
)
612+
// Bundled snapshot of https://models.opencode.ai/api.json, committed at
613+
// packages/core/src/models-dev/snapshot.txt and refreshed via
614+
// `bun run script/update-models-snapshot.ts`. It is the boot-time floor
615+
// for the catalog; the periodic fetch below still refreshes on top.
616+
const loadSnapshot = options?.snapshot === false ? Effect.succeed(undefined) : decodeCatalog(snapshotText)
611617

612618
const fetchAndWrite = Effect.fn("ModelsDev.fetchAndWrite")(function* () {
613619
const text = yield* fetchApi()
614-
const catalog = (yield* Schema.decodeUnknownEffect(CatalogJson)(text)) as Record<string, SourceProvider>
620+
const catalog = yield* decodeCatalog(text)
615621
// Best-effort: a cache-write failure must never kill catalog
616622
// population. The payload has outgrown some KV backends' per-value
617623
// limits (Durable Object SQLite caps values at 2 MB and api.json

‎packages/core/src/models-dev/snapshot.txt‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/core/test/models.test.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,20 @@ describe("ModelsDev Service", () => {
228228
}),
229229
)
230230

231-
it.live("get() returns empty catalog when KV is empty, fetch disabled, and no bundled snapshot is injected", () =>
231+
it.live("get() returns empty catalog when KV is empty, fetch disabled, and the bundled snapshot is disabled", () =>
232+
Effect.gen(function* () {
233+
const cache = makeCache()
234+
const state = yield* Ref.make(initialState)
235+
const result = yield* ModelsDev.Service.use((s) => s.get()).pipe(
236+
Effect.provide(buildLayer(state, cache, { fetch: false, snapshot: false })),
237+
)
238+
expect(result).toEqual([])
239+
const final = yield* Ref.get(state)
240+
expect(final.calls).toEqual([])
241+
}),
242+
)
243+
244+
it.live("get() falls back to the bundled snapshot when KV is empty and fetch is disabled", () =>
232245
Effect.gen(function* () {
233246
const cache = makeCache()
234247
const state = yield* Ref.make(initialState)
@@ -237,7 +250,9 @@ describe("ModelsDev Service", () => {
237250
cache,
238251
ModelsDev.Service.use((s) => s.get()),
239252
)
240-
expect(result).toEqual([])
253+
expect(result.length).toBeGreaterThan(0)
254+
const anthropic = result.find((snapshot) => snapshot.info.id === "anthropic")
255+
expect(anthropic?.environment).toContain("ANTHROPIC_API_KEY")
241256
const final = yield* Ref.get(state)
242257
expect(final.calls).toEqual([])
243258
}),
@@ -248,7 +263,7 @@ describe("ModelsDev Service", () => {
248263
const cache = makeCache()
249264
writeCacheText(cache, "{")
250265
const state = yield* Ref.make({ ...initialState, body: JSON.stringify(fixture2) })
251-
const context = yield* Layer.build(buildLayer(state, cache, { fetch: true }))
266+
const context = yield* Layer.build(buildLayer(state, cache, { fetch: true, snapshot: false }))
252267
const result = yield* ModelsDev.Service.use((s) => s.get()).pipe(Effect.provide(context))
253268
expect(result).toEqual(fixture2Snapshot)
254269
expect(cache.values.get(cacheKey)).toMatchObject({ body: JSON.stringify(fixture2) })
@@ -263,7 +278,7 @@ describe("ModelsDev Service", () => {
263278
const state = yield* Ref.make({ ...initialState, body: JSON.stringify(fixture2) })
264279
const layer = Layer.fresh(
265280
AppNodeBuilder.build(ModelsDev.node, [
266-
[ModelsDev.node, ModelsDev.configured({ fetch: true })],
281+
[ModelsDev.node, ModelsDev.configured({ fetch: true, snapshot: false })],
267282
[LayerNodePlatform.httpClient, Layer.succeed(HttpClient.HttpClient, makeMockClient(state))],
268283
[KV.node, makeFailingWriteKV(cache)],
269284
]),
@@ -281,7 +296,7 @@ describe("ModelsDev Service", () => {
281296
const cache = makeCache()
282297
const state = yield* Ref.make(initialState)
283298
yield* ModelsDev.Service.use((service) => service.get()).pipe(
284-
Effect.provide(buildLayer(state, cache, { url: "", fetch: true })),
299+
Effect.provide(buildLayer(state, cache, { url: "", fetch: true, snapshot: false })),
285300
)
286301
expect((yield* Ref.get(state)).calls[0]?.url).toBe("https://models.opencode.ai/api.json")
287302
}),
@@ -296,7 +311,7 @@ describe("ModelsDev Service", () => {
296311
return yield* Effect.all([svc.get(), svc.get(), svc.get(), svc.get(), svc.get()], {
297312
concurrency: "unbounded",
298313
})
299-
}).pipe(Effect.provide(buildLayer(state, cache, { fetch: true })))
314+
}).pipe(Effect.provide(buildLayer(state, cache, { fetch: true, snapshot: false })))
300315
for (const result of results) expect(result).toEqual(fixtureSnapshot)
301316
expect((yield* Ref.get(state)).calls.length).toBe(1)
302317
}),

0 commit comments

Comments
 (0)