provider-mistral.test.ts 4.9 KB

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