Skip to content
Open
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
18 changes: 11 additions & 7 deletions packages/app/src/context/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSync } from "./sync"
import { base64Encode } from "@opencode-ai/util/encode"
import { useProviders } from "@/hooks/use-providers"
import { useModels } from "@/context/models"
import { Persist, persisted } from "@/utils/persist"
import { cycleModelVariant, getConfiguredAgentVariant, resolveModelVariant } from "./model-variant"

export type ModelKey = { providerID: string; modelID: string }
Expand Down Expand Up @@ -93,11 +94,14 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
const model = (() => {
const models = useModels()

const [ephemeral, setEphemeral] = createStore<{
model: Record<string, ModelKey | undefined>
}>({
model: {},
})
const [store, setStore] = persisted(
Persist.workspace(sdk.directory, "local-model"),
createStore<{
model: Record<string, ModelKey | undefined>
}>({
model: {},
}),
)

const resolveConfigured = () => {
if (!sync.data.config.model) return
Expand Down Expand Up @@ -136,7 +140,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
const a = agent.current()
if (!a) return undefined
const key = getFirstValidModel(
() => ephemeral.model[a.name],
() => store.model[a.name],
() => a.model,
fallbackModel,
)
Expand Down Expand Up @@ -173,7 +177,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
batch(() => {
const currentAgent = agent.current()
const next = model ?? fallbackModel()
if (currentAgent) setEphemeral("model", currentAgent.name, next)
if (currentAgent) setStore("model", currentAgent.name, next)
if (model) models.setVisibility(model, true)
if (options?.recent && model) models.recent.push(model)
})
Expand Down
31 changes: 31 additions & 0 deletions packages/app/src/pages/session/session-model-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ describe("syncSessionModel", () => {
})

describe("resetSessionModel", () => {
test("keeps the current model selection for new sessions", () => {
const calls: unknown[] = []

resetSessionModel({
agent: {
current() {
return {
model: { providerID: "anthropic", modelID: "claude-sonnet-4" },
variant: "high",
}
},
set() {},
},
model: {
set(value) {
calls.push(["model", value])
},
current() {
return { id: "gpt-5", provider: { id: "openai" } }
},
variant: {
set(value) {
calls.push(["variant", value])
},
},
},
})

expect(calls).toEqual([])
})

test("restores the current agent defaults", () => {
const calls: unknown[] = []

Expand Down
1 change: 1 addition & 0 deletions packages/app/src/pages/session/session-model-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Local = {
}

export const resetSessionModel = (local: Local) => {
if (local.model.current()) return
const agent = local.agent.current()
if (!agent) return
batch(() => {
Expand Down
Loading