provider-cohere.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect, mock } 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 { CoherePlugin } from "@opencode-ai/core/plugin/provider/cohere"
  8. import { ProviderV2 } from "@opencode-ai/core/provider"
  9. import type { LanguageModelV3 } from "@ai-sdk/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const cohereOptions: Record<string, any>[] = []
  13. const it = testEffect(PluginTestLayer)
  14. const addPlugin = Effect.fn(function* () {
  15. const plugin = yield* PluginV2.Service
  16. const aisdk = yield* AISDK.Service
  17. const host = yield* PluginHost.make(plugin)
  18. yield* CoherePlugin.effect(host)
  19. })
  20. function fakeSelectorSdk(calls: string[]) {
  21. const make = (method: string) => (id: string) => {
  22. calls.push(`${method}:${id}`)
  23. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  24. }
  25. return {
  26. responses: make("responses"),
  27. messages: make("messages"),
  28. chat: make("chat"),
  29. languageModel: make("languageModel"),
  30. }
  31. }
  32. void mock.module("@ai-sdk/cohere", () => ({
  33. createCohere: (options: Record<string, any>) => {
  34. cohereOptions.push({ ...options })
  35. return {
  36. languageModel: (modelID: string) => ({
  37. modelID,
  38. provider: `${options.name ?? "cohere"}.chat`,
  39. specificationVersion: "v3",
  40. }),
  41. }
  42. },
  43. }))
  44. describe("CoherePlugin", () => {
  45. it.effect("creates a Cohere SDK only for @ai-sdk/cohere", () =>
  46. Effect.gen(function* () {
  47. const plugin = yield* PluginV2.Service
  48. const aisdk = yield* AISDK.Service
  49. yield* addPlugin()
  50. const ignored = yield* aisdk.runSDK({
  51. model: ModelV2.Info.make({
  52. ...ModelV2.Info.empty(ProviderV2.ID.make("cohere"), ModelV2.ID.make("command")),
  53. api: { id: ModelV2.ID.make("command"), type: "aisdk", package: "test-provider" },
  54. }),
  55. package: "@ai-sdk/openai-compatible",
  56. options: { name: "cohere" },
  57. })
  58. expect(ignored.sdk).toBeUndefined()
  59. const result = yield* aisdk.runSDK({
  60. model: ModelV2.Info.make({
  61. ...ModelV2.Info.empty(ProviderV2.ID.make("cohere"), ModelV2.ID.make("command")),
  62. api: { id: ModelV2.ID.make("command"), type: "aisdk", package: "test-provider" },
  63. }),
  64. package: "@ai-sdk/cohere",
  65. options: { name: "cohere" },
  66. })
  67. expect(result.sdk).toBeDefined()
  68. }),
  69. )
  70. it.effect("uses the model provider ID as the bundled SDK name", () =>
  71. Effect.gen(function* () {
  72. const plugin = yield* PluginV2.Service
  73. const aisdk = yield* AISDK.Service
  74. yield* addPlugin()
  75. const result = yield* aisdk.runSDK({
  76. model: ModelV2.Info.make({
  77. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-cohere"), ModelV2.ID.make("command-r-plus")),
  78. api: { id: ModelV2.ID.make("command-r-plus"), type: "aisdk", package: "test-provider" },
  79. }),
  80. package: "@ai-sdk/cohere",
  81. options: { name: "custom-cohere", apiKey: "test", baseURL: "https://cohere.example" },
  82. })
  83. expect(cohereOptions.at(-1)).toEqual({
  84. name: "custom-cohere",
  85. apiKey: "test",
  86. baseURL: "https://cohere.example",
  87. })
  88. expect(result.sdk?.languageModel("command-r-plus").provider).toBe("custom-cohere.chat")
  89. }),
  90. )
  91. it.effect("leaves language selection to the default languageModel fallback", () =>
  92. Effect.gen(function* () {
  93. const plugin = yield* PluginV2.Service
  94. const aisdk = yield* AISDK.Service
  95. const calls: string[] = []
  96. const sdk = fakeSelectorSdk(calls)
  97. yield* addPlugin()
  98. const result = yield* aisdk.runLanguage({
  99. model: ModelV2.Info.make({
  100. ...ModelV2.Info.empty(ProviderV2.ID.make("cohere"), ModelV2.ID.make("alias")),
  101. api: { id: ModelV2.ID.make("command-r-plus"), type: "aisdk", package: "test-provider" },
  102. }),
  103. sdk,
  104. options: {},
  105. })
  106. expect(result.language).toBeUndefined()
  107. expect(calls).toEqual([])
  108. expect(result.language ?? sdk.languageModel("command-r-plus")).toBeDefined()
  109. expect(calls).toEqual(["languageModel:command-r-plus"])
  110. }),
  111. )
  112. })