provider-xai.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 { Integration } from "@opencode-ai/core/integration"
  6. import { ModelV2 } from "@opencode-ai/core/model"
  7. import { PluginV2 } from "@opencode-ai/core/plugin"
  8. import { PluginHost } from "@opencode-ai/core/plugin/host"
  9. import { XAIPlugin } from "@opencode-ai/core/plugin/provider/xai"
  10. import { ProviderV2 } from "@opencode-ai/core/provider"
  11. import { testEffect } from "../lib/effect"
  12. import { PluginTestLayer } from "./fixture"
  13. const it = testEffect(PluginTestLayer)
  14. const addPlugin = Effect.fn(function* () {
  15. const plugin = yield* PluginV2.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("registers browser OAuth, device OAuth, and API key methods", () =>
  33. Effect.gen(function* () {
  34. yield* addPlugin()
  35. const integrations = yield* Integration.Service
  36. const integration = yield* integrations.get(Integration.ID.make("xai"))
  37. expect(integration?.name).toBe("xAI")
  38. expect(integration?.methods).toEqual([
  39. {
  40. id: Integration.MethodID.make("browser"),
  41. type: "oauth",
  42. label: "xAI Grok OAuth (SuperGrok Subscription)",
  43. },
  44. {
  45. id: Integration.MethodID.make("device"),
  46. type: "oauth",
  47. label: "xAI Grok OAuth (Headless / Remote / VPS)",
  48. },
  49. { type: "key", label: "Manually enter API Key" },
  50. ])
  51. }),
  52. )
  53. it.effect("creates an xAI SDK only for @ai-sdk/xai", () =>
  54. Effect.gen(function* () {
  55. const aisdk = yield* AISDK.Service
  56. yield* addPlugin()
  57. const ignored = yield* aisdk.runSDK({
  58. model: ModelV2.Info.make({
  59. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("grok-4")),
  60. modelID: ModelV2.ID.make("grok-4"),
  61. package: "aisdk:@ai-sdk/xai",
  62. }),
  63. package: "@ai-sdk/openai-compatible",
  64. options: {},
  65. })
  66. const result = yield* aisdk.runSDK({
  67. model: ModelV2.Info.make({
  68. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("grok-4")),
  69. modelID: ModelV2.ID.make("grok-4"),
  70. package: "aisdk:@ai-sdk/xai",
  71. }),
  72. package: "@ai-sdk/xai",
  73. options: {},
  74. })
  75. expect(ignored.sdk).toBeUndefined()
  76. expect(typeof result.sdk?.responses).toBe("function")
  77. }),
  78. )
  79. it.effect("creates xAI SDKs for custom provider IDs", () =>
  80. Effect.gen(function* () {
  81. const aisdk = yield* AISDK.Service
  82. yield* addPlugin()
  83. const result = yield* aisdk.runSDK({
  84. model: ModelV2.Info.make({
  85. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-xai"), ModelV2.ID.make("grok-4")),
  86. modelID: ModelV2.ID.make("grok-4"),
  87. package: "aisdk:@ai-sdk/xai",
  88. }),
  89. package: "@ai-sdk/xai",
  90. options: {},
  91. })
  92. expect(result.sdk.responses("grok-4").provider).toBe("xai.responses")
  93. }),
  94. )
  95. it.effect("uses responses with the model modelID for xAI language models", () =>
  96. Effect.gen(function* () {
  97. const aisdk = yield* AISDK.Service
  98. const calls: string[] = []
  99. yield* addPlugin()
  100. const result = yield* aisdk.runLanguage({
  101. model: ModelV2.Info.make({
  102. ...ModelV2.Info.empty(ProviderV2.ID.make("xai"), ModelV2.ID.make("alias")),
  103. modelID: ModelV2.ID.make("grok-4"),
  104. package: "aisdk:@ai-sdk/xai",
  105. }),
  106. sdk: fakeSelectorSdk(calls),
  107. options: {},
  108. })
  109. expect(calls).toEqual(["responses:grok-4"])
  110. expect(result.language).toBeDefined()
  111. }),
  112. )
  113. it.effect("ignores non-xAI providers", () =>
  114. Effect.gen(function* () {
  115. const aisdk = yield* AISDK.Service
  116. const calls: string[] = []
  117. yield* addPlugin()
  118. const result = yield* aisdk.runLanguage({
  119. model: ModelV2.Info.make({
  120. ...ModelV2.Info.empty(ProviderV2.ID.openai, ModelV2.ID.make("grok-4")),
  121. modelID: ModelV2.ID.make("grok-4"),
  122. package: "aisdk:@ai-sdk/xai",
  123. }),
  124. sdk: fakeSelectorSdk(calls),
  125. options: {},
  126. })
  127. expect(calls).toEqual([])
  128. expect(result.language).toBeUndefined()
  129. }),
  130. )
  131. })