github-copilot.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Effect } from "effect"
  2. import { ModelV2 } from "../../model"
  3. import { define } from "@opencode-ai/plugin/v2/effect"
  4. import { ProviderV2 } from "../../provider"
  5. function shouldUseResponses(modelID: string) {
  6. // Copilot supports Responses for GPT-5 class models, except mini variants
  7. // which still need the chat-completions endpoint.
  8. const match = /^gpt-(\d+)/.exec(modelID)
  9. if (!match) return false
  10. return Number(match[1]) >= 5 && !modelID.startsWith("gpt-5-mini")
  11. }
  12. export const GithubCopilotPlugin = define({
  13. id: "github-copilot",
  14. effect: Effect.fn(function* (ctx) {
  15. yield* ctx.catalog.transform(
  16. Effect.fn(function* (evt) {
  17. const item = evt.provider.get(ProviderV2.ID.githubCopilot)
  18. if (!item || !item.models.has(ModelV2.ID.make("gpt-5-chat-latest"))) return
  19. evt.model.update(item.provider.id, ModelV2.ID.make("gpt-5-chat-latest"), (model) => {
  20. // This chat-only alias conflicts with the Copilot GPT-5 Responses route,
  21. // so hide it only for Copilot rather than for every provider catalog.
  22. model.enabled = false
  23. })
  24. }),
  25. )
  26. yield* ctx.aisdk.hook(
  27. "sdk",
  28. Effect.fn(function* (evt) {
  29. if (evt.package !== "@ai-sdk/github-copilot") return
  30. const mod = yield* Effect.promise(() => import("../../github-copilot/copilot-provider"))
  31. evt.sdk = mod.createOpenaiCompatible(evt.options)
  32. }),
  33. )
  34. yield* ctx.aisdk.hook(
  35. "language",
  36. Effect.fn(function* (evt) {
  37. if (evt.model.providerID !== ProviderV2.ID.githubCopilot) return
  38. if (evt.sdk.responses === undefined && evt.sdk.chat === undefined) {
  39. evt.language = evt.sdk.languageModel(evt.model.api.id)
  40. return
  41. }
  42. evt.language = shouldUseResponses(evt.model.api.id)
  43. ? evt.sdk.responses(evt.model.api.id)
  44. : evt.sdk.chat(evt.model.api.id)
  45. }),
  46. )
  47. }),
  48. })