provider-openai.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { Integration } from "@opencode-ai/core/integration"
  5. import { ModelV2 } from "@opencode-ai/core/model"
  6. import { PluginV2 } from "@opencode-ai/core/plugin"
  7. import { OpenAIPlugin } from "@opencode-ai/core/plugin/provider/openai"
  8. import { ProviderV2 } from "@opencode-ai/core/provider"
  9. import { fakeSelectorSdk, it, model, provider } from "./provider-helper"
  10. function add(plugin: PluginV2.Interface, integrations: Integration.Interface) {
  11. return plugin.add({
  12. ...OpenAIPlugin,
  13. effect: OpenAIPlugin.effect.pipe(Effect.provideService(Integration.Service, integrations)),
  14. })
  15. }
  16. describe("OpenAIPlugin", () => {
  17. it.effect("registers browser and headless ChatGPT OAuth methods", () =>
  18. Effect.gen(function* () {
  19. const plugin = yield* PluginV2.Service
  20. yield* add(plugin, yield* Integration.Service)
  21. expect((yield* (yield* Integration.Service).get(Integration.ID.make("openai")))?.methods).toEqual([
  22. new Integration.OAuthMethod({
  23. id: Integration.MethodID.make("chatgpt-browser"),
  24. type: "oauth",
  25. label: "ChatGPT Pro/Plus (browser)",
  26. }),
  27. new Integration.OAuthMethod({
  28. id: Integration.MethodID.make("chatgpt-headless"),
  29. type: "oauth",
  30. label: "ChatGPT Pro/Plus (headless)",
  31. }),
  32. ])
  33. }),
  34. )
  35. it.effect("creates an OpenAI SDK for @ai-sdk/openai using the provider ID as SDK name", () =>
  36. Effect.gen(function* () {
  37. const plugin = yield* PluginV2.Service
  38. yield* add(plugin, yield* Integration.Service)
  39. const result = yield* plugin.trigger(
  40. "aisdk.sdk",
  41. {
  42. model: model("custom-openai", "gpt-5"),
  43. package: "@ai-sdk/openai",
  44. options: { name: "custom-openai", apiKey: "test" },
  45. },
  46. {},
  47. )
  48. expect(result.sdk?.responses("gpt-5").provider).toBe("custom-openai.responses")
  49. }),
  50. )
  51. it.effect("ignores non-OpenAI SDK packages", () =>
  52. Effect.gen(function* () {
  53. const plugin = yield* PluginV2.Service
  54. yield* add(plugin, yield* Integration.Service)
  55. const result = yield* plugin.trigger(
  56. "aisdk.sdk",
  57. { model: model("openai", "gpt-5"), package: "@ai-sdk/openai-compatible", options: { name: "openai" } },
  58. {},
  59. )
  60. expect(result.sdk).toBeUndefined()
  61. }),
  62. )
  63. it.effect("uses the Responses API for language models", () =>
  64. Effect.gen(function* () {
  65. const plugin = yield* PluginV2.Service
  66. const calls: string[] = []
  67. yield* add(plugin, yield* Integration.Service)
  68. const result = yield* plugin.trigger(
  69. "aisdk.language",
  70. {
  71. model: model("openai", "alias", {
  72. api: { id: ModelV2.ID.make("gpt-5"), type: "aisdk", package: "test-provider" },
  73. }),
  74. sdk: fakeSelectorSdk(calls),
  75. options: {},
  76. },
  77. {},
  78. )
  79. expect(calls).toEqual(["responses:gpt-5"])
  80. expect(result.language).toBeDefined()
  81. }),
  82. )
  83. it.effect("ignores non-OpenAI providers", () =>
  84. Effect.gen(function* () {
  85. const plugin = yield* PluginV2.Service
  86. const calls: string[] = []
  87. yield* add(plugin, yield* Integration.Service)
  88. const result = yield* plugin.trigger(
  89. "aisdk.language",
  90. { model: model("anthropic", "gpt-5"), sdk: fakeSelectorSdk(calls), options: {} },
  91. {},
  92. )
  93. expect(calls).toEqual([])
  94. expect(result.language).toBeUndefined()
  95. }),
  96. )
  97. it.effect("disables gpt-5-chat-latest during catalog transforms", () =>
  98. Effect.gen(function* () {
  99. const plugin = yield* PluginV2.Service
  100. const catalog = yield* Catalog.Service
  101. yield* add(plugin, yield* Integration.Service)
  102. const transform = yield* catalog.transform()
  103. yield* transform((catalog) => {
  104. const item = provider("openai", { api: { type: "aisdk", package: "@ai-sdk/openai" } })
  105. catalog.provider.update(item.id, (draft) => {
  106. draft.api = item.api
  107. })
  108. catalog.model.update(item.id, ModelV2.ID.make("gpt-5"), () => {})
  109. catalog.model.update(item.id, ModelV2.ID.make("gpt-5-chat-latest"), () => {})
  110. })
  111. expect((yield* catalog.model.get(ProviderV2.ID.openai, ModelV2.ID.make("gpt-5"))).enabled).toBe(true)
  112. expect((yield* catalog.model.get(ProviderV2.ID.openai, ModelV2.ID.make("gpt-5-chat-latest"))).enabled).toBe(false)
  113. }),
  114. )
  115. it.effect("does not disable gpt-5-chat-latest for non-OpenAI providers", () =>
  116. Effect.gen(function* () {
  117. const plugin = yield* PluginV2.Service
  118. const catalog = yield* Catalog.Service
  119. yield* add(plugin, yield* Integration.Service)
  120. const transform = yield* catalog.transform()
  121. yield* transform((catalog) => {
  122. const item = provider("custom-openai")
  123. catalog.provider.update(item.id, () => {})
  124. catalog.model.update(item.id, ModelV2.ID.make("gpt-5-chat-latest"), () => {})
  125. })
  126. expect(
  127. (yield* catalog.model.get(ProviderV2.ID.make("custom-openai"), ModelV2.ID.make("gpt-5-chat-latest"))).enabled,
  128. ).toBe(true)
  129. }),
  130. )
  131. })