feat(core): port GitHub Copilot OAuth (#36336)

Co-authored-by: Dax Raad <d@ironbay.co>
This commit is contained in:
opencode-agent[bot] 2026-07-10 21:11:12 -04:00 committed by GitHub
commit d1550cb599
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 643 additions and 10 deletions

View file

@ -0,0 +1,76 @@
import { expect, test } from "bun:test"
import { CopilotModels } from "@opencode-ai/core/github-copilot/models"
import { ModelV2 } from "@opencode-ai/core/model"
import { ProviderV2 } from "@opencode-ai/core/provider"
test("defensively syncs advertised Copilot models", async () => {
const server = Bun.serve({
port: 0,
fetch: () =>
Response.json({
data: [
{
model_picker_enabled: true,
id: "gpt-5",
name: "GPT-5 remote",
version: "gpt-5-2026-06-01",
supported_endpoints: ["/responses"],
billing: {
token_prices: {
batch_size: 0,
default: { input_price: 10, output_price: 20, cache_price: 5 },
},
},
capabilities: {
family: "gpt",
limits: {
max_context_window_tokens: 200000,
max_output_tokens: 16384,
max_prompt_tokens: 180000,
},
supports: { tool_calls: true, reasoning_effort: ["low", "high"] },
},
},
{
model_picker_enabled: false,
id: "utility",
name: "Utility",
version: "utility-2026-06-01",
capabilities: {
family: "utility",
limits: { max_output_tokens: 1000, max_prompt_tokens: 8000 },
supports: { tool_calls: false },
},
},
{ model_picker_enabled: true, id: "incomplete" },
],
}),
})
try {
const existing = ModelV2.Info.make({
...ModelV2.Info.empty(ProviderV2.ID.githubCopilot, ModelV2.ID.make("gpt-5")),
modelID: ModelV2.ID.make("gpt-5"),
name: "GPT-5 local",
})
const stale = ModelV2.Info.make({
...ModelV2.Info.empty(ProviderV2.ID.githubCopilot, ModelV2.ID.make("stale")),
modelID: ModelV2.ID.make("stale"),
})
const models = await CopilotModels.get(server.url.origin, {}, [existing, stale])
const model = models.get(ModelV2.ID.make("gpt-5"))
expect(model?.name).toBe("GPT-5 local")
expect(model?.settings).toMatchObject({ baseURL: server.url.origin, endpoint: "responses" })
expect(model?.cost[0]).toMatchObject({ input: 0, output: 0, cache: { read: 0, write: 0 } })
expect(model?.variants.map((variant) => variant.id)).toEqual([
ModelV2.VariantID.make("low"),
ModelV2.VariantID.make("high"),
])
expect(models.get(ModelV2.ID.make("utility"))?.enabled).toBe(false)
expect(models.has(ModelV2.ID.make("stale"))).toBe(false)
expect(models.has(ModelV2.ID.make("incomplete"))).toBe(false)
} finally {
await server.stop(true)
}
})

View file

@ -5,8 +5,9 @@ import { Catalog } from "@opencode-ai/core/catalog"
import { ModelV2 } from "@opencode-ai/core/model"
import { PluginV2 } from "@opencode-ai/core/plugin"
import { PluginHost } from "@opencode-ai/core/plugin/host"
import { GithubCopilotPlugin } from "@opencode-ai/core/plugin/provider/github-copilot"
import { copilotFetch, GithubCopilotPlugin } from "@opencode-ai/core/plugin/provider/github-copilot"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { Integration } from "@opencode-ai/core/integration"
import type { LanguageModelV3 } from "@ai-sdk/provider"
import { testEffect } from "../lib/effect"
import { PluginTestLayer } from "./fixture"
@ -39,6 +40,46 @@ function fakeSelectorSdk(calls: string[]) {
}
describe("GithubCopilotPlugin", () => {
it.effect("registers GitHub Copilot device OAuth", () =>
Effect.gen(function* () {
yield* addPlugin()
expect((yield* (yield* Integration.Service).get(Integration.ID.make("github-copilot")))?.methods).toContainEqual({
id: Integration.MethodID.make("device"),
type: "oauth",
label: "Login with GitHub Copilot",
prompts: expect.any(Array),
})
}),
)
it.live("adds Copilot authentication and request metadata headers", () =>
Effect.gen(function* () {
const requests: Headers[] = []
const send = copilotFetch(
"token",
async (_input: Parameters<typeof fetch>[0], init?: RequestInit) => {
requests.push(new Headers(init?.headers))
return Response.json({ ok: true })
},
false,
)
yield* Effect.promise(() =>
send("https://api.githubcopilot.com/chat/completions", {
method: "POST",
headers: { "x-api-key": "old" },
body: JSON.stringify({
messages: [{ role: "user", content: [{ type: "image_url", image_url: { url: "data:image/png" } }] }],
}),
}),
)
expect(requests[0]?.get("authorization")).toBe("Bearer token")
expect(requests[0]?.has("x-api-key")).toBe(false)
expect(requests[0]?.get("x-initiator")).toBe("user")
expect(requests[0]?.get("copilot-vision-request")).toBe("true")
expect(requests[0]?.get("x-github-api-version")).toBe("2026-06-01")
}),
)
it.effect("creates the bundled Copilot SDK for the GitHub Copilot package", () =>
Effect.gen(function* () {
const plugin = yield* PluginV2.Service