provider-cohere.test.ts 4.4 KB

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