Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opencode/src/cli/cmd/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const ModelsCommand = cmd({
},
handler: async (args) => {
if (args.refresh) {
await ModelsDev.refresh()
await ModelsDev.refresh(true)
UI.println(UI.Style.TEXT_SUCCESS_BOLD + "Models cache refreshed" + UI.Style.TEXT_NORMAL)
}

Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,10 @@ export namespace Config {
chatMaxRetries: z.number().optional().describe("Number of retries for chat completions on failure"),
disable_paste_summary: z.boolean().optional(),
batch_tool: z.boolean().optional().describe("Enable the batch tool"),
skip_models_fetch: z
.boolean()
.optional()
.describe("Skip automatic fetching of models from models.dev on startup"),
})
.optional(),
})
Expand Down
24 changes: 21 additions & 3 deletions packages/opencode/src/provider/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path"
import z from "zod"
import { data } from "./models-macro" with { type: "macro" }
import { Installation } from "../installation"
import { Config } from "../config/config"

export namespace ModelsDev {
const log = Log.create({ service: "models.dev" })
Expand Down Expand Up @@ -69,15 +70,24 @@ export namespace ModelsDev {
export type Provider = z.infer<typeof Provider>

export async function get() {
refresh()
const config = await Config.get()
if (!config.experimental?.skip_models_fetch) {
refresh()
}
const file = Bun.file(filepath)
const result = await file.json().catch(() => {})
if (result) return result as Record<string, Provider>
const json = await data()
return JSON.parse(json) as Record<string, Provider>
}

export async function refresh() {
export async function refresh(force = false) {
const config = await Config.get()
if (!force && config.experimental?.skip_models_fetch) {
log.debug("skipping models.dev fetch due to skip_models_fetch config")
return
}

const file = Bun.file(filepath)
log.info("refreshing", {
file,
Expand All @@ -96,4 +106,12 @@ export namespace ModelsDev {
}
}

setInterval(() => ModelsDev.refresh(), 60 * 1000 * 60).unref()
setInterval(
async () => {
const config = await Config.get()
if (!config.experimental?.skip_models_fetch) {
ModelsDev.refresh()
}
},
60 * 1000 * 60,
).unref()
4 changes: 4 additions & 0 deletions packages/sdk/js/src/gen/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,10 @@ export type Config = {
* Enable the batch tool
*/
batch_tool?: boolean
/**
* Skip automatic fetching of models from models.dev on startup
*/
skip_models_fetch?: boolean
}
}

Expand Down