provider-xai.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import type { LanguageModelV3 } from "@ai-sdk/provider"
  3. import { describe, expect } from "bun:test"
  4. import { Effect } from "effect"
  5. import { ModelV2 } from "@opencode-ai/core/model"
  6. import { PluginV2 } from "@opencode-ai/core/plugin"
  7. import { PluginHost } from "@opencode-ai/core/plugin/host"
  8. import { XAIPlugin } from "@opencode-ai/core/plugin/provider/xai"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const it = testEffect(PluginTestLayer)
  13. const addPlugin = Effect.fn(function* () {
  14. const plugin = yield* PluginV2.Service
  15. const aisdk = yield* AISDK.Service
  16. const host = yield* PluginHost.make(plugin)
  17. yield* XAIPlugin.effect(host)
  18. })
  19. function fakeSelectorSdk(calls: string[]) {
  20. const make = (method: string) => (id: string) => {
  21. calls.push(`${method}:${id}`)
  22. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  23. }
  24. return {
  25. responses: make("responses"),
  26. messages: make("messages"),
  27. chat: make("chat"),
  28. languageModel: make("languageModel"),
  29. }
  30. }
  31. describe("XAIPlugin", () => {
  32. it.effect("creates an xAI SDK only for @ai-sdk/xai", () =>
  33. Effect.gen(function* () {
  34. const plugin = yield* PluginV2.Service
  35. const aisdk = yield* AISDK.Service
  36. yield* addPlugin()
  37. const ignored = yield* aisdk.runSDK({
  38. model: ModelV2.Info.make({
  39. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("grok-4")),
  40. api: { id: ModelV2.ID.make("grok-4"), type: "aisdk", package: "@ai-sdk/xai" },
  41. }),
  42. package: "@ai-sdk/openai-compatible",
  43. options: {},
  44. })
  45. const result = yield* aisdk.runSDK({
  46. model: ModelV2.Info.make({
  47. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("grok-4")),
  48. api: { id: ModelV2.ID.make("grok-4"), type: "aisdk", package: "@ai-sdk/xai" },
  49. }),
  50. package: "@ai-sdk/xai",
  51. options: {},
  52. })
  53. expect(ignored.sdk).toBeUndefined()
  54. expect(typeof result.sdk?.responses).toBe("function")
  55. }),
  56. )
  57. it.effect("creates xAI SDKs for custom provider IDs", () =>
  58. Effect.gen(function* () {
  59. const plugin = yield* PluginV2.Service
  60. const aisdk = yield* AISDK.Service
  61. yield* addPlugin()
  62. const result = yield* aisdk.runSDK({
  63. model: ModelV2.Info.make({
  64. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-xai"), ModelV2.ID.make("grok-4")),
  65. api: { id: ModelV2.ID.make("grok-4"), type: "aisdk", package: "@ai-sdk/xai" },
  66. }),
  67. package: "@ai-sdk/xai",
  68. options: {},
  69. })
  70. expect(result.sdk.responses("grok-4").provider).toBe("xai.responses")
  71. }),
  72. )
  73. it.effect("uses responses with the model api.id for xAI language models", () =>
  74. Effect.gen(function* () {
  75. const plugin = yield* PluginV2.Service
  76. const aisdk = yield* AISDK.Service
  77. const calls: string[] = []
  78. yield* addPlugin()
  79. const result = yield* aisdk.runLanguage({
  80. model: ModelV2.Info.make({
  81. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("alias")),
  82. api: { id: ModelV2.ID.make("grok-4"), type: "aisdk", package: "@ai-sdk/xai" },
  83. }),
  84. sdk: fakeSelectorSdk(calls),
  85. options: {},
  86. })
  87. expect(calls).toEqual(["responses:grok-4"])
  88. expect(result.language).toBeDefined()
  89. }),
  90. )
  91. it.effect("ignores non-xAI providers", () =>
  92. Effect.gen(function* () {
  93. const plugin = yield* PluginV2.Service
  94. const aisdk = yield* AISDK.Service
  95. const calls: string[] = []
  96. yield* addPlugin()
  97. const result = yield* aisdk.runLanguage({
  98. model: ModelV2.Info.make({
  99. ...ModelV2.Info.empty(ProviderV2.ID.openai, ModelV2.ID.make("grok-4")),
  100. api: { id: ModelV2.ID.make("grok-4"), type: "aisdk", package: "@ai-sdk/xai" },
  101. }),
  102. sdk: fakeSelectorSdk(calls),
  103. options: {},
  104. })
  105. expect(calls).toEqual([])
  106. expect(result.language).toBeUndefined()
  107. }),
  108. )
  109. })