summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAiden Cline <[email protected]>2025-09-05 17:25:25 -0500
committerGitHub <[email protected]>2025-09-05 17:25:25 -0500
commitdc99005e652eae9cf9234842d011e8758f996af6 (patch)
tree972cf01b11e33683204b85b0071dbcea1cd01ff0
parent8ffedbe157999d7cdc395ddad26c464ec2f87ef0 (diff)
downloadopencode-dc99005e652eae9cf9234842d011e8758f996af6.tar.gz
opencode-dc99005e652eae9cf9234842d011e8758f996af6.zip
fix: default to last used model (#2443)
-rw-r--r--packages/opencode/src/provider/provider.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts
index 184c2c950..4cc23a71d 100644
--- a/packages/opencode/src/provider/provider.ts
+++ b/packages/opencode/src/provider/provider.ts
@@ -1,4 +1,5 @@
import z from "zod"
+import path from "path"
import { Config } from "../config/config"
import { mergeDeep, sortBy } from "remeda"
import { NoSuchModelError, type LanguageModel, type Provider as SDK } from "ai"
@@ -9,6 +10,7 @@ import { ModelsDev } from "./models"
import { NamedError } from "../util/error"
import { Auth } from "../auth"
import { Instance } from "../project/instance"
+import { Global } from "../global"
export namespace Provider {
const log = Log.create({ service: "provider" })
@@ -420,6 +422,43 @@ export namespace Provider {
export async function defaultModel() {
const cfg = await Config.get()
if (cfg.model) return parseModel(cfg.model)
+
+ // this will be adjusted when migration to opentui is complete,
+ // for now we just read the tui state toml file directly
+ //
+ // NOTE: cannot just import file as toml without cleaning due to lack of
+ // support for date/time references in Bun toml parser: https://github.com/oven-sh/bun/issues/22426
+ const lastused = await Bun.file(path.join(Global.Path.state, "tui"))
+ .text()
+ .then((text) => {
+ // remove the date/time references since Bun toml parser doesn't support yet
+ const cleaned = text
+ .split("\n")
+ .filter((line) => !line.trim().startsWith("last_used ="))
+ .join("\n")
+ const state = Bun.TOML.parse(cleaned) as {
+ recently_used_models?: {
+ provider_id: string
+ model_id: string
+ }[]
+ }
+ const models = state?.recently_used_models ?? []
+ if (models.length > 0) {
+ return {
+ providerID: models[0].provider_id,
+ modelID: models[0].model_id,
+ }
+ }
+ })
+ .catch((error) => {
+ log.error("failed to find last used model", {
+ error,
+ })
+ return undefined
+ })
+
+ if (lastused) return lastused
+
const provider = await list()
.then((val) => Object.values(val))
.then((x) => x.find((p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id)))