fix(core): send Copilot requests to the login's own endpoint until models load (#63146)

This commit is contained in:
Aiden Cline
2026-11-10 10:22:31 -06:00
committed by GitHub
Unverified
parent eccfb829bd
commit 8de2338c48
2 changed files with 47 additions and 3 deletions
@@ -15,7 +15,7 @@ import { Provider } from "../../provider.js"
import { SessionAffinity } from "../../session/affinity.js"
import type { Session } from "@opencode/schema/session"
import { SharedCache } from "../../shared-cache.js"
import { activeCredential } from "./credential-settings.js"
import { activeCredential, credentialSettings } from "./credential-settings.js"
import { ProviderRefresh } from "./refresh.js"
import type { PluginInternal } from "../internal.js"
@@ -173,6 +173,11 @@ export const GithubCopilotPlugin = define({
// Each Session keeps the model Copilot's Auto router picked until its token nears expiry, the Session is compacted,
// or the account changes. The token unlocks the pick and its Auto discount; Copilot rejects it for any other model.
const routes = new Map<Session.ID, { model: string; account: string; token?: string; until: number }>()
// The endpoint saved at login serves requests until the first refresh reads the account's current one, so an
// Enterprise login doesn't send its token to the public host while that refresh is still running.
const login = yield* credentialSettings(ctx, "github-copilot", (value) =>
value.type === "oauth" ? { baseURL: copilotBaseURL(value.metadata) } : {},
)
const discovered = yield* ProviderRefresh.start({
integrationID: "github-copilot",
initial: {} as {
@@ -254,8 +259,8 @@ export const GithubCopilotPlugin = define({
const item = evt.get(Provider.ID.githubCopilot)
if (!item) return
const loaded = discovered.current
const url = loaded.baseURL ?? (typeof login.settings.baseURL === "string" ? login.settings.baseURL : baseURL())
if (loaded.models) {
const url = loaded.baseURL ?? baseURL()
const router = CopilotModels.autoModel(url, loaded.models)
evt.add({
info: item.provider,
@@ -279,7 +284,7 @@ export const GithubCopilotPlugin = define({
: model.package === "@opencode/ai/providers/openai"
? "responses"
: undefined,
loaded.baseURL ?? baseURL(),
url,
)
model.package = route.package
model.settings = Provider.mergeOverlay(model.settings, { baseURL: route.baseURL })
@@ -199,6 +199,45 @@ describe("GithubCopilotPlugin", () => {
}),
)
const pendingRoute = Effect.fn(function* (metadata: Record<string, string>) {
const providers = yield* Provider.Service
const models = yield* Model.Service
const requests: string[] = []
yield* providers.transform((editor) => {
editor.models.update(Provider.ID.githubCopilot, Model.ID.make("gpt-4o"), (model) => {
model.package = "@opencode/ai/providers/openai-compatible"
})
})
yield* connect(metadata)
// The account lookup never answers, so the first refresh is still running when the model is read.
const pending = HttpClient.make((request) =>
Effect.sync(() => requests.push(request.url)).pipe(Effect.andThen(Effect.never)),
)
yield* addPlugin().pipe(Effect.provideService(HttpClient.HttpClient, pending))
yield* advance(() => requests.length > 0)
return {
requests,
baseURL: required(yield* models.get(Provider.ID.githubCopilot, Model.ID.make("gpt-4o"))).settings?.baseURL,
}
})
it.effect("serves an Enterprise login from its own host until the first model list arrives", () =>
Effect.gen(function* () {
expect(yield* pendingRoute({ enterpriseUrl: "company.ghe.com" })).toEqual({
requests: ["https://api.company.ghe.com/copilot_internal/user"],
baseURL: "https://copilot-api.company.ghe.com",
})
}),
)
it.effect("serves a login from the endpoint saved with it until the first model list arrives", () =>
Effect.gen(function* () {
expect(
(yield* pendingRoute({ enterpriseUrl: "company.ghe.com", apiEndpoint: "https://api.tenant.ghe.com" })).baseURL,
).toBe("https://api.tenant.ghe.com")
}),
)
it.effect("registers GitHub Copilot device OAuth", () =>
Effect.gen(function* () {
yield* addPlugin()