provider-xai.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 { 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* plugin.add(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* plugin.add(XAIPlugin)
  39. yield* plugin.add(
  40. PluginV2.define({
  41. id: PluginV2.ID.make("xai-sdk-name-observer"),
  42. effect: Effect.gen(function* () {
  43. return {
  44. "aisdk.sdk": Effect.fn(function* (evt) {
  45. if (!evt.sdk) return
  46. providers.push(evt.sdk.responses("grok-4").provider)
  47. }),
  48. }
  49. }),
  50. }),
  51. )
  52. yield* plugin.trigger(
  53. "aisdk.sdk",
  54. {
  55. model: new ModelV2.Info({ ...model, providerID: ProviderV2.ID.make("custom-xai") }),
  56. package: "@ai-sdk/xai",
  57. options: {},
  58. },
  59. {},
  60. )
  61. expect(providers).toEqual(["xai.responses"])
  62. }),
  63. )
  64. it.effect("uses responses with the model api.id for xAI language models", () =>
  65. Effect.gen(function* () {
  66. const plugin = yield* PluginV2.Service
  67. const calls: string[] = []
  68. yield* plugin.add(XAIPlugin)
  69. const result = yield* plugin.trigger(
  70. "aisdk.language",
  71. {
  72. model: new ModelV2.Info({ ...model, id: ModelV2.ID.make("alias") }),
  73. sdk: fakeSelectorSdk(calls),
  74. options: {},
  75. },
  76. {},
  77. )
  78. expect(calls).toEqual(["responses:grok-4"])
  79. expect(result.language).toBeDefined()
  80. }),
  81. )
  82. it.effect("ignores non-xAI providers", () =>
  83. Effect.gen(function* () {
  84. const plugin = yield* PluginV2.Service
  85. const calls: string[] = []
  86. yield* plugin.add(XAIPlugin)
  87. const result = yield* plugin.trigger(
  88. "aisdk.language",
  89. {
  90. model: new ModelV2.Info({ ...model, providerID: ProviderV2.ID.openai }),
  91. sdk: fakeSelectorSdk(calls),
  92. options: {},
  93. },
  94. {},
  95. )
  96. expect(calls).toEqual([])
  97. expect(result.language).toBeUndefined()
  98. }),
  99. )
  100. })