provider-mistral.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 { MistralPlugin } from "@opencode-ai/core/plugin/provider/mistral"
  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* MistralPlugin.effect(host)
  18. })
  19. describe("MistralPlugin", () => {
  20. it.effect("creates a Mistral SDK for @ai-sdk/mistral", () =>
  21. Effect.gen(function* () {
  22. const plugin = yield* PluginV2.Service
  23. const aisdk = yield* AISDK.Service
  24. yield* addPlugin()
  25. const result = yield* aisdk.runSDK({
  26. model: ModelV2.Info.make({
  27. ...ModelV2.Info.empty(ProviderV2.ID.make("mistral"), ModelV2.ID.make("mistral-large")),
  28. api: { id: ModelV2.ID.make("mistral-large"), type: "aisdk", package: "test-provider" },
  29. }),
  30. package: "@ai-sdk/mistral",
  31. options: { name: "mistral" },
  32. })
  33. expect(result.sdk).toBeDefined()
  34. }),
  35. )
  36. it.effect("ignores non-Mistral SDK packages", () =>
  37. Effect.gen(function* () {
  38. const plugin = yield* PluginV2.Service
  39. const aisdk = yield* AISDK.Service
  40. yield* addPlugin()
  41. const result = yield* aisdk.runSDK({
  42. model: ModelV2.Info.make({
  43. ...ModelV2.Info.empty(ProviderV2.ID.make("mistral"), ModelV2.ID.make("mistral-large")),
  44. api: { id: ModelV2.ID.make("mistral-large"), type: "aisdk", package: "test-provider" },
  45. }),
  46. package: "@ai-sdk/openai-compatible",
  47. options: { name: "mistral" },
  48. })
  49. expect(result.sdk).toBeUndefined()
  50. }),
  51. )
  52. it.effect("matches the old bundled Mistral SDK provider name for the bundled provider ID", () =>
  53. Effect.gen(function* () {
  54. const plugin = yield* PluginV2.Service
  55. const aisdk = yield* AISDK.Service
  56. const providers: string[] = []
  57. yield* addPlugin()
  58. yield* aisdk.hook.sdk((event) =>
  59. Effect.sync(() => {
  60. providers.push(event.sdk.languageModel("mistral-large").provider)
  61. }),
  62. )
  63. const result = yield* aisdk.runSDK({
  64. model: ModelV2.Info.make({
  65. ...ModelV2.Info.empty(ProviderV2.ID.make("mistral"), ModelV2.ID.make("mistral-large")),
  66. api: { id: ModelV2.ID.make("mistral-large"), type: "aisdk", package: "test-provider" },
  67. }),
  68. package: "@ai-sdk/mistral",
  69. options: { name: "mistral" },
  70. })
  71. expect(result.sdk).toBeDefined()
  72. expect(providers).toEqual(["mistral.chat"])
  73. }),
  74. )
  75. it.effect("matches the old bundled Mistral SDK provider name for custom provider IDs", () =>
  76. Effect.gen(function* () {
  77. const plugin = yield* PluginV2.Service
  78. const aisdk = yield* AISDK.Service
  79. const providers: string[] = []
  80. yield* addPlugin()
  81. yield* aisdk.hook.sdk((event) =>
  82. Effect.sync(() => {
  83. providers.push(event.sdk.languageModel("mistral-large").provider)
  84. }),
  85. )
  86. yield* aisdk.runSDK({
  87. model: ModelV2.Info.make({
  88. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-mistral"), ModelV2.ID.make("mistral-large")),
  89. api: { id: ModelV2.ID.make("mistral-large"), type: "aisdk", package: "test-provider" },
  90. }),
  91. package: "@ai-sdk/mistral",
  92. options: { name: "custom-mistral" },
  93. })
  94. expect(providers).toEqual(["mistral.chat"])
  95. }),
  96. )
  97. it.effect("leaves Mistral language selection on the default sdk.languageModel(api.id) path", () =>
  98. Effect.gen(function* () {
  99. const plugin = yield* PluginV2.Service
  100. const aisdk = yield* AISDK.Service
  101. const calls: string[] = []
  102. const sdk = {
  103. languageModel: (id: string) => {
  104. calls.push(`languageModel:${id}`)
  105. return { modelId: id, provider: "languageModel", specificationVersion: "v3" } as unknown as LanguageModelV3
  106. },
  107. }
  108. yield* addPlugin()
  109. const result = yield* aisdk.runLanguage({
  110. model: ModelV2.Info.make({
  111. ...ModelV2.Info.empty(ProviderV2.ID.make("mistral"), ModelV2.ID.make("alias")),
  112. api: { id: ModelV2.ID.make("mistral-large"), type: "aisdk", package: "test-provider" },
  113. }),
  114. sdk,
  115. options: {},
  116. })
  117. const language = result.language ?? sdk.languageModel(result.model.api.id)
  118. expect(calls).toEqual(["languageModel:mistral-large"])
  119. expect(language).toBeDefined()
  120. }),
  121. )
  122. })