CH 04 · Model-Provider Abstraction and Multi-Model Strategy · 11/15
Read as one pageReal code: a small model writes the chat title
Title generation is the low-tier job from the previous section: short, cheap, and a poor use of the model that is drafting the reply. OpenCode keeps a hidden agent whose id is title for that call. selectTitle picks the model. The session's own model is the fallback. The function is written with Effect, a library where a function* pauses at yield* until the operation returns a value. Read yield* as "run this, then continue with the result."
const selectTitle = Effect.fn("SessionContext.selectTitle")(function* (session: SessionSchema.Info) {
const agent = yield* agents.get(Agent.ID.make("title"))
if (!agent) return
const primary = yield* resolveModel(session).pipe(Effect.orElseSucceed(() => undefined)) // [1]
const info = yield* Effect.gen(function* () {
if (agent.model) return yield* model.get(agent.model.providerID, agent.model.id) // [2]
if (!primary) return
return yield* model.small(primary.ref.providerID) // [3]
})
const variant =
agent.model?.variant ?? MINIMAL_REASONING_VARIANTS.find((id) => info?.variants.some((item) => item.id === id)) // [4]
const preferred =
info &&
(yield* resolveModel({
...session,
model: Model.Ref.make({
providerID: info.providerID,
id: info.id,
...(variant ? { variant } : {}),
}),
}).pipe(Effect.orElseSucceed(() => undefined)))
const selected = preferred ?? primary // [5]
if (!selected) return
return { agent, primary, selected }
})
Source: anomalyco/opencode, packages/core/src/session/context.ts (MIT)
What to notice
- [1]
primaryis the model already chosen for the session.Effect.orElseSucceed(() => undefined)turns a failure into "no model", so a title problem does not fail the session. If the title agent is missing, the previous line returns and no title is generated. - [2] When that agent has its own
model,model.getloads that provider and id. The configured choice wins over the search below. - [3] Otherwise
model.small(packages/core/src/model.ts) stays on the primary model's provider. It looks throughavailable, the enabled models, and keeps those that areactive, with text in and text out. It then returns the first present family amonggpt-luna,gemini-flash-lite,gemini-flash, andclaude-haiku. If the provider has none of them, the title stays on the primary model. One hard-coded small id would fail for a user who does not have that provider. - [4]
MINIMAL_REASONING_VARIANTS, further down this file, isnone, thenminimal, thenlow. The agent's own variant wins when set. Otherwise this line takes the first of the three listed on the model. The title asks for the smallest reasoning setting available. - [5]
selectedis that model when the secondresolveModelsucceeds, and the primary model otherwise.Model.Refis the pointer to a provider, a model id, and an optional variant, andresolveModelturns that pointer into the model the title call will use.SessionTitle.generatetriesselected, thenprimaryif the attempt returned nothing and the refs differ. The title agent is hidden, and its registration pushes a deny-all permission. This call still has no tools for a simpler reason:request.titleis invoked without a tool list, andprepareuses an empty definition list. The text is truncated to at most 100 characters.
Read it yourself: Read small. Among enabled, active, text-in and text-out models, a provider that has families gemini-flash-lite and claude-haiku, and does not have gpt-luna, yields the flash-lite model, because that family is earlier in the list. In agent.ts, confirm the title agent is hidden and a deny-all permission is pushed. title.ts calls request.title with no tool list, and title.ts is the fallback from selected to primary.
Finished this idea?
Your learning progress stays in this browser.