provider-openai.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { ModelV2 } from "@opencode-ai/core/model"
  4. import { PluginV2 } from "@opencode-ai/core/plugin"
  5. import { OpenAIPlugin } from "@opencode-ai/core/plugin/provider/openai"
  6. import { fakeSelectorSdk, it, model } from "./provider-helper"
  7. describe("OpenAIPlugin", () => {
  8. it.effect("creates an OpenAI SDK for @ai-sdk/openai using the provider ID as SDK name", () =>
  9. Effect.gen(function* () {
  10. const plugin = yield* PluginV2.Service
  11. yield* plugin.add(OpenAIPlugin)
  12. const result = yield* plugin.trigger(
  13. "aisdk.sdk",
  14. {
  15. model: model("custom-openai", "gpt-5"),
  16. package: "@ai-sdk/openai",
  17. options: { name: "custom-openai", apiKey: "test" },
  18. },
  19. {},
  20. )
  21. expect(result.sdk?.responses("gpt-5").provider).toBe("custom-openai.responses")
  22. }),
  23. )
  24. it.effect("ignores non-OpenAI SDK packages", () =>
  25. Effect.gen(function* () {
  26. const plugin = yield* PluginV2.Service
  27. yield* plugin.add(OpenAIPlugin)
  28. const result = yield* plugin.trigger(
  29. "aisdk.sdk",
  30. { model: model("openai", "gpt-5"), package: "@ai-sdk/openai-compatible", options: { name: "openai" } },
  31. {},
  32. )
  33. expect(result.sdk).toBeUndefined()
  34. }),
  35. )
  36. it.effect("uses the Responses API for language models", () =>
  37. Effect.gen(function* () {
  38. const plugin = yield* PluginV2.Service
  39. const calls: string[] = []
  40. yield* plugin.add(OpenAIPlugin)
  41. const result = yield* plugin.trigger(
  42. "aisdk.language",
  43. {
  44. model: model("openai", "alias", { apiID: ModelV2.ID.make("gpt-5") }),
  45. sdk: fakeSelectorSdk(calls),
  46. options: {},
  47. },
  48. {},
  49. )
  50. expect(calls).toEqual(["responses:gpt-5"])
  51. expect(result.language).toBeDefined()
  52. }),
  53. )
  54. it.effect("ignores non-OpenAI providers", () =>
  55. Effect.gen(function* () {
  56. const plugin = yield* PluginV2.Service
  57. const calls: string[] = []
  58. yield* plugin.add(OpenAIPlugin)
  59. const result = yield* plugin.trigger(
  60. "aisdk.language",
  61. { model: model("anthropic", "gpt-5"), sdk: fakeSelectorSdk(calls), options: {} },
  62. {},
  63. )
  64. expect(calls).toEqual([])
  65. expect(result.language).toBeUndefined()
  66. }),
  67. )
  68. it.effect("cancels gpt-5-chat-latest during model updates", () =>
  69. Effect.gen(function* () {
  70. const plugin = yield* PluginV2.Service
  71. yield* plugin.add(OpenAIPlugin)
  72. const normal = yield* plugin.trigger("model.update", {}, { model: model("openai", "gpt-5"), cancel: false })
  73. const filtered = yield* plugin.trigger(
  74. "model.update",
  75. {},
  76. { model: model("openai", "gpt-5-chat-latest"), cancel: false },
  77. )
  78. expect(normal.cancel).toBe(false)
  79. expect(filtered.cancel).toBe(true)
  80. }),
  81. )
  82. it.effect("does not cancel gpt-5-chat-latest for non-OpenAI providers", () =>
  83. Effect.gen(function* () {
  84. const plugin = yield* PluginV2.Service
  85. yield* plugin.add(OpenAIPlugin)
  86. const result = yield* plugin.trigger(
  87. "model.update",
  88. {},
  89. { model: model("custom-openai", "gpt-5-chat-latest"), cancel: false },
  90. )
  91. expect(result.cancel).toBe(false)
  92. }),
  93. )
  94. })