refactor(plugin): edit the model in place in experimental.model.resolve (#63015)

This commit is contained in:
Aiden Cline
2026-11-09 20:58:26 -06:00
committed by GitHub
Unverified
parent 37729b1df1
commit b88b867b6e
7 changed files with 34 additions and 35 deletions
@@ -370,7 +370,7 @@ export const GithubCopilotPlugin = define({
if (!current) return
for (const [id, route] of routes) if (route.until <= now) routes.delete(id)
routes.set(evt.sessionID, current)
evt.target = Model.Ref.make({ providerID: Provider.ID.githubCopilot, id: Model.ID.make(current.model) })
evt.model = Model.Ref.make({ providerID: Provider.ID.githubCopilot, id: Model.ID.make(current.model) })
}),
{ providerID: Provider.ID.githubCopilot },
)
+13 -12
View File
@@ -101,22 +101,23 @@ const layer = Layer.effect(
return Service.of({
resolve: Effect.fn("SessionRunnerModel.resolve")(function* (session, available, request) {
const selected = yield* select(session, available)
const event = yield* hooks.trigger("session", "experimental.model.resolve", {
const ref = Model.Ref.make({
providerID: selected.info.providerID,
id: selected.info.id,
...(selected.variant === undefined ? {} : { variant: selected.variant }),
})
const model = (yield* hooks.trigger("session", "experimental.model.resolve", {
sessionID: session.id,
agent: request.agent,
kind: request.kind,
model: Model.Ref.make({
providerID: selected.info.providerID,
id: selected.info.id,
...(selected.variant === undefined ? {} : { variant: selected.variant }),
}),
})
const target = event.target
if (!target) return yield* resolver.resolveModel(selected.info, selected.variant)
// Same provider only: the target may be hidden from the picker, and its credentials and hooks stay the selection's.
const resolved = target.providerID === selected.info.providerID ? yield* resolver.resolve(target) : undefined
model: ref,
})).model
if (model.providerID === ref.providerID && model.id === ref.id && model.variant === ref.variant)
return yield* resolver.resolveModel(selected.info, selected.variant)
// Same provider only: the model may be hidden from the picker, and its credentials and hooks stay the selection's.
const resolved = model.providerID === ref.providerID ? yield* resolver.resolve(model) : undefined
if (resolved) return resolved
return yield* new ModelUnavailableError({ providerID: target.providerID, modelID: target.id })
return yield* new ModelUnavailableError({ providerID: model.providerID, modelID: model.id })
}),
})
}),
+2 -2
View File
@@ -780,7 +780,7 @@ describe("LocationServiceMap", () => {
Effect.sync(() => {
seen.push({ kind: event.kind, agent: event.agent, model: event.model.id })
if (event.model.id === "auto")
event.target = Model.Ref.make({ providerID: target.providerID, id: Model.ID.make("hidden") })
event.model = Model.Ref.make({ providerID: target.providerID, id: Model.ID.make("hidden") })
}),
)
const modelState = yield* Model.Service
@@ -809,7 +809,7 @@ describe("LocationServiceMap", () => {
expect(result.seen[0]).toEqual({ kind: "compaction", agent: "compaction", model: "auto" })
expect(result.resolved.ref).toEqual(Model.Ref.make({ id: Model.ID.make("hidden"), providerID: router }))
expect(String(result.resolved.model.id)).toBe("hidden")
// A target from another provider would bypass the selection's credentials and provider hooks.
// A model from another provider would bypass the selection's credentials and provider hooks.
expect(result.crossed).toMatchObject({
_tag: "SessionRunnerModel.ModelUnavailableError",
providerID: "other",
@@ -689,7 +689,7 @@ describe("GithubCopilotPlugin", () => {
kind,
model: Model.Ref.make({ providerID: Provider.ID.githubCopilot, id: Model.ID.make("auto") }),
})
.pipe(Effect.map((event) => event.target && String(event.target.id)))
.pipe(Effect.map((event) => (event.model.id === "auto" ? undefined : String(event.model.id))))
const token = (model: string) =>
hooks
.trigger("session", "model.request", {
+5 -6
View File
@@ -66,17 +66,16 @@ export interface SessionTitle extends SessionRequest {
export type SessionRequestKind = "primary" | "compaction" | "title" | "generate"
/**
* Runs whenever a Session resolves the model for a request, before anything is built for it. Set `target` to serve
* this request with another model from the same provider, including models hidden from the picker; the Session keeps
* its selection, and the request and its records use the target. Router models such as GitHub Copilot's Auto use it.
* Experimental.
* Runs whenever a Session resolves the model for a request, before anything is built for it. `model` starts as the
* Session's or agent's selection; change it to serve this request with another model from the same provider, including
* models hidden from the picker. The Session keeps its selection, and the request and its records use the new model.
* Router models such as GitHub Copilot's Auto use it. Experimental.
*/
export interface SessionModelResolve {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly kind: SessionRequestKind
readonly model: Model.Ref
target?: Model.Ref
model: Model.Ref
}
export interface SessionModelRequest {
+5 -6
View File
@@ -66,17 +66,16 @@ export interface SessionTitle extends SessionRequest {
export type SessionRequestKind = "primary" | "compaction" | "title" | "generate"
/**
* Runs whenever a Session resolves the model for a request, before anything is built for it. Set `target` to serve
* this request with another model from the same provider, including models hidden from the picker; the Session keeps
* its selection, and the request and its records use the target. Router models such as GitHub Copilot's Auto use it.
* Experimental.
* Runs whenever a Session resolves the model for a request, before anything is built for it. `model` starts as the
* Session's or agent's selection; change it to serve this request with another model from the same provider, including
* models hidden from the picker. The Session keeps its selection, and the request and its records use the new model.
* Router models such as GitHub Copilot's Auto use it. Experimental.
*/
export interface SessionModelResolve {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly kind: SessionRequestKind
readonly model: Model.Ref
target?: Model.Ref
model: Model.Ref
}
export interface SessionModelRequest {
@@ -1469,17 +1469,18 @@ Generation options depend on the selected protocol and model:
#### Model resolve
Experimental. Runs whenever a Session resolves the model for a request, before anything is built for it. Set `target` to
serve this request with another model from the same provider, including one hidden from the model picker. The Session
keeps its selected model; the request and the assistant message use the target. `kind` says which request is resolving,
so a router can skip titles or route again after compaction. A target from another provider fails as unavailable.
Experimental. Runs whenever a Session resolves the model for a request, before anything is built for it. `model` starts
as the selected model; change it to serve this request with another model from the same provider, including one hidden
from the model picker. The Session keeps its selected model; the request and the assistant message use the new one.
`kind` says which request is resolving, so a router can skip titles or route again after compaction. A model from
another provider fails as unavailable.
```ts
await ctx.session.hook(
"experimental.model.resolve",
(event) => {
if (event.model.id === "auto" && event.kind !== "title")
event.target = { providerID: event.model.providerID, id: pick(event.sessionID) }
event.model = { providerID: event.model.providerID, id: pick(event.sessionID) }
},
{ providerID: "acme" },
)
@@ -1626,8 +1627,7 @@ interface SessionModelResolveHook {
readonly sessionID: string
readonly agent: string
readonly kind: "primary" | "compaction" | "title" | "generate"
readonly model: { providerID: string; id: string; variant?: string }
target?: { providerID: string; id: string; variant?: string }
model: { providerID: string; id: string; variant?: string }
}
interface SessionWebSocketHandshakeHook {