provider-xai.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. modelID: ModelV2.ID.make("grok-4"),
  41. package: "aisdk:@ai-sdk/xai",
  42. }),
  43. package: "@ai-sdk/openai-compatible",
  44. options: {},
  45. })
  46. const result = yield* aisdk.runSDK({
  47. model: ModelV2.Info.make({
  48. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("grok-4")),
  49. modelID: ModelV2.ID.make("grok-4"),
  50. package: "aisdk:@ai-sdk/xai",
  51. }),
  52. package: "@ai-sdk/xai",
  53. options: {},
  54. })
  55. expect(ignored.sdk).toBeUndefined()
  56. expect(typeof result.sdk?.responses).toBe("function")
  57. }),
  58. )
  59. it.effect("creates xAI SDKs for custom provider IDs", () =>
  60. Effect.gen(function* () {
  61. const plugin = yield* PluginV2.Service
  62. const aisdk = yield* AISDK.Service
  63. yield* addPlugin()
  64. const result = yield* aisdk.runSDK({
  65. model: ModelV2.Info.make({
  66. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-xai"), ModelV2.ID.make("grok-4")),
  67. modelID: ModelV2.ID.make("grok-4"),
  68. package: "aisdk:@ai-sdk/xai",
  69. }),
  70. package: "@ai-sdk/xai",
  71. options: {},
  72. })
  73. expect(result.sdk.responses("grok-4").provider).toBe("xai.responses")
  74. }),
  75. )
  76. it.effect("uses responses with the model modelID for xAI language models", () =>
  77. Effect.gen(function* () {
  78. const plugin = yield* PluginV2.Service
  79. const aisdk = yield* AISDK.Service
  80. const calls: string[] = []
  81. yield* addPlugin()
  82. const result = yield* aisdk.runLanguage({
  83. model: ModelV2.Info.make({
  84. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("alias")),
  85. modelID: ModelV2.ID.make("grok-4"),
  86. package: "aisdk:@ai-sdk/xai",
  87. }),
  88. sdk: fakeSelectorSdk(calls),
  89. options: {},
  90. })
  91. expect(calls).toEqual(["responses:grok-4"])
  92. expect(result.language).toBeDefined()
  93. }),
  94. )
  95. it.effect("ignores non-xAI providers", () =>
  96. Effect.gen(function* () {
  97. const plugin = yield* PluginV2.Service
  98. const aisdk = yield* AISDK.Service
  99. const calls: string[] = []
  100. yield* addPlugin()
  101. const result = yield* aisdk.runLanguage({
  102. model: ModelV2.Info.make({
  103. ...ModelV2.Info.empty(ProviderV2.ID.openai, ModelV2.ID.make("grok-4")),
  104. modelID: ModelV2.ID.make("grok-4"),
  105. package: "aisdk:@ai-sdk/xai",
  106. }),
  107. sdk: fakeSelectorSdk(calls),
  108. options: {},
  109. })
  110. expect(calls).toEqual([])
  111. expect(result.language).toBeUndefined()
  112. }),
  113. )
  114. })