provider-xai.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import { EventV2 } from "@opencode-ai/core/event"
  4. import { ModelV2 } from "@opencode-ai/core/model"
  5. import { PluginV2 } from "@opencode-ai/core/plugin"
  6. import { XAIPlugin } from "@opencode-ai/core/plugin/provider/xai"
  7. import { ProviderV2 } from "@opencode-ai/core/provider"
  8. import { testEffect } from "../lib/effect"
  9. import { addPlugin, fakeSelectorSdk } from "./provider-helper"
  10. const it = testEffect(PluginV2.locationLayer.pipe(Layer.provide(EventV2.defaultLayer)))
  11. const model = new ModelV2.Info({
  12. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("grok-4")),
  13. api: {
  14. id: ModelV2.ID.make("grok-4"),
  15. type: "aisdk",
  16. package: "@ai-sdk/xai",
  17. },
  18. })
  19. describe("XAIPlugin", () => {
  20. it.effect("creates an xAI SDK only for @ai-sdk/xai", () =>
  21. Effect.gen(function* () {
  22. const plugin = yield* PluginV2.Service
  23. yield* addPlugin(plugin, XAIPlugin)
  24. const ignored = yield* plugin.trigger(
  25. "aisdk.sdk",
  26. { model, package: "@ai-sdk/openai-compatible", options: {} },
  27. {},
  28. )
  29. const result = yield* plugin.trigger("aisdk.sdk", { model, package: "@ai-sdk/xai", options: {} }, {})
  30. expect(ignored.sdk).toBeUndefined()
  31. expect(typeof result.sdk?.responses).toBe("function")
  32. }),
  33. )
  34. it.effect("creates xAI SDKs for custom provider IDs", () =>
  35. Effect.gen(function* () {
  36. const plugin = yield* PluginV2.Service
  37. const providers: string[] = []
  38. yield* addPlugin(plugin, XAIPlugin)
  39. yield* plugin.add({
  40. id: PluginV2.ID.make("xai-sdk-name-observer"),
  41. effect: Effect.gen(function* () {
  42. return {
  43. "aisdk.sdk": Effect.fn(function* (evt) {
  44. if (!evt.sdk) return
  45. providers.push(evt.sdk.responses("grok-4").provider)
  46. }),
  47. }
  48. }),
  49. })
  50. yield* plugin.trigger(
  51. "aisdk.sdk",
  52. {
  53. model: new ModelV2.Info({ ...model, providerID: ProviderV2.ID.make("custom-xai") }),
  54. package: "@ai-sdk/xai",
  55. options: {},
  56. },
  57. {},
  58. )
  59. expect(providers).toEqual(["xai.responses"])
  60. }),
  61. )
  62. it.effect("uses responses with the model api.id for xAI language models", () =>
  63. Effect.gen(function* () {
  64. const plugin = yield* PluginV2.Service
  65. const calls: string[] = []
  66. yield* addPlugin(plugin, XAIPlugin)
  67. const result = yield* plugin.trigger(
  68. "aisdk.language",
  69. {
  70. model: new ModelV2.Info({ ...model, id: ModelV2.ID.make("alias") }),
  71. sdk: fakeSelectorSdk(calls),
  72. options: {},
  73. },
  74. {},
  75. )
  76. expect(calls).toEqual(["responses:grok-4"])
  77. expect(result.language).toBeDefined()
  78. }),
  79. )
  80. it.effect("ignores non-xAI providers", () =>
  81. Effect.gen(function* () {
  82. const plugin = yield* PluginV2.Service
  83. const calls: string[] = []
  84. yield* addPlugin(plugin, XAIPlugin)
  85. const result = yield* plugin.trigger(
  86. "aisdk.language",
  87. {
  88. model: new ModelV2.Info({ ...model, providerID: ProviderV2.ID.openai }),
  89. sdk: fakeSelectorSdk(calls),
  90. options: {},
  91. },
  92. {},
  93. )
  94. expect(calls).toEqual([])
  95. expect(result.language).toBeUndefined()
  96. }),
  97. )
  98. })