provider-cohere.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { describe, expect, mock } from "bun:test"
  2. import { Effect } from "effect"
  3. import { ModelV2 } from "@opencode-ai/core/model"
  4. import { PluginV2 } from "@opencode-ai/core/plugin"
  5. import { CoherePlugin } from "@opencode-ai/core/plugin/provider/cohere"
  6. import { fakeSelectorSdk, it, model } from "./provider-helper"
  7. const cohereOptions: Record<string, any>[] = []
  8. void mock.module("@ai-sdk/cohere", () => ({
  9. createCohere: (options: Record<string, any>) => {
  10. cohereOptions.push({ ...options })
  11. return {
  12. languageModel: (modelID: string) => ({
  13. modelID,
  14. provider: `${options.name ?? "cohere"}.chat`,
  15. specificationVersion: "v3",
  16. }),
  17. }
  18. },
  19. }))
  20. describe("CoherePlugin", () => {
  21. it.effect("creates a Cohere SDK only for @ai-sdk/cohere", () =>
  22. Effect.gen(function* () {
  23. const plugin = yield* PluginV2.Service
  24. yield* plugin.add(CoherePlugin)
  25. const ignored = yield* plugin.trigger(
  26. "aisdk.sdk",
  27. { model: model("cohere", "command"), package: "@ai-sdk/openai-compatible", options: { name: "cohere" } },
  28. {},
  29. )
  30. expect(ignored.sdk).toBeUndefined()
  31. const result = yield* plugin.trigger(
  32. "aisdk.sdk",
  33. { model: model("cohere", "command"), package: "@ai-sdk/cohere", options: { name: "cohere" } },
  34. {},
  35. )
  36. expect(result.sdk).toBeDefined()
  37. }),
  38. )
  39. it.effect("uses the model provider ID as the bundled SDK name", () =>
  40. Effect.gen(function* () {
  41. const plugin = yield* PluginV2.Service
  42. yield* plugin.add(CoherePlugin)
  43. const result = yield* plugin.trigger(
  44. "aisdk.sdk",
  45. {
  46. model: model("custom-cohere", "command-r-plus"),
  47. package: "@ai-sdk/cohere",
  48. options: { name: "custom-cohere", apiKey: "test", baseURL: "https://cohere.example" },
  49. },
  50. {},
  51. )
  52. expect(cohereOptions.at(-1)).toEqual({
  53. name: "custom-cohere",
  54. apiKey: "test",
  55. baseURL: "https://cohere.example",
  56. })
  57. expect(result.sdk?.languageModel("command-r-plus").provider).toBe("custom-cohere.chat")
  58. }),
  59. )
  60. it.effect("leaves language selection to the default languageModel fallback", () =>
  61. Effect.gen(function* () {
  62. const plugin = yield* PluginV2.Service
  63. const calls: string[] = []
  64. const sdk = fakeSelectorSdk(calls)
  65. yield* plugin.add(CoherePlugin)
  66. const result = yield* plugin.trigger(
  67. "aisdk.language",
  68. { model: model("cohere", "alias", { api: { id: ModelV2.ID.make("command-r-plus") } }), sdk, options: {} },
  69. {},
  70. )
  71. expect(result.language).toBeUndefined()
  72. expect(calls).toEqual([])
  73. expect(result.language ?? sdk.languageModel("command-r-plus")).toBeDefined()
  74. expect(calls).toEqual(["languageModel:command-r-plus"])
  75. }),
  76. )
  77. })