diff --git a/packages/core/src/plugin/provider/github-copilot.ts b/packages/core/src/plugin/provider/github-copilot.ts index d39dee394c3..2cbe319c7ef 100644 --- a/packages/core/src/plugin/provider/github-copilot.ts +++ b/packages/core/src/plugin/provider/github-copilot.ts @@ -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() + // 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 }) diff --git a/packages/core/test/plugin/provider-github-copilot.test.ts b/packages/core/test/plugin/provider-github-copilot.test.ts index f1c0d9dda6c..841b03d3861 100644 --- a/packages/core/test/plugin/provider-github-copilot.test.ts +++ b/packages/core/test/plugin/provider-github-copilot.test.ts @@ -199,6 +199,45 @@ describe("GithubCopilotPlugin", () => { }), ) + const pendingRoute = Effect.fn(function* (metadata: Record) { + 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()