feat(core): expose session model switching (#31011)

This commit is contained in:
Kit Langton 2026-06-05 15:55:36 -04:00 committed by GitHub
commit a57fb32d95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 1 deletions

View file

@ -1,6 +1,6 @@
import { describe, expect } from "bun:test"
import { Effect, Schema } from "effect"
import { OpenCode, Session, Tool } from "@opencode-ai/core/public"
import { AbsolutePath, Location, Model, OpenCode, Session, Tool } from "@opencode-ai/core/public"
import { testEffect } from "./lib/effect"
const it = testEffect(OpenCode.layer)
@ -22,6 +22,7 @@ describe("public native OpenCode API", () => {
"message",
"messages",
"prompt",
"switchModel",
])
expect(Session.ID.create()).toStartWith("ses_")
expect(Session.MessageID.create()).toStartWith("msg_")
@ -36,4 +37,41 @@ describe("public native OpenCode API", () => {
})
}),
)
it.effect("switches the exact Session to the exact model through the durable facade", () =>
Effect.gen(function* () {
const opencode = yield* OpenCode.Service
const targetID = Session.ID.make("ses_public_switch_target")
const otherID = Session.ID.make("ses_public_switch_other")
const model = Schema.decodeUnknownSync(Model.Ref)({
id: "claude-sonnet-4-5",
providerID: "anthropic",
variant: "high",
})
const location = Location.Ref.make({ directory: AbsolutePath.make("/public-session-switch-model") })
yield* opencode.sessions.create({ id: targetID, location })
yield* opencode.sessions.create({ id: otherID, location })
yield* opencode.sessions.switchModel({ sessionID: targetID, model })
expect((yield* opencode.sessions.get(targetID)).model).toEqual(model)
expect((yield* opencode.sessions.get(otherID)).model).toBeUndefined()
}),
)
it.effect("preserves the typed not-found error for a missing Session", () =>
Effect.gen(function* () {
const opencode = yield* OpenCode.Service
const sessionID = Session.ID.make("ses_public_switch_missing")
const error = yield* opencode.sessions
.switchModel({
sessionID,
model: Schema.decodeUnknownSync(Model.Ref)({ id: "claude-sonnet-4-5", providerID: "anthropic" }),
})
.pipe(Effect.flip)
expect(error).toBeInstanceOf(Session.NotFoundError)
expect(error.sessionID).toBe(sessionID)
}),
)
})