provider-alibaba.test.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect } from "bun:test"
  3. import { createAlibaba } from "@ai-sdk/alibaba"
  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 { AlibabaPlugin } from "@opencode-ai/core/plugin/provider/alibaba"
  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* AlibabaPlugin.effect(host)
  18. })
  19. describe("AlibabaPlugin", () => {
  20. it.effect("creates an Alibaba SDK for @ai-sdk/alibaba", () =>
  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("alibaba"), ModelV2.ID.make("qwen")),
  28. api: { id: ModelV2.ID.make("qwen"), type: "aisdk", package: "test-provider" },
  29. }),
  30. package: "@ai-sdk/alibaba",
  31. options: { name: "alibaba" },
  32. })
  33. expect(result.sdk).toBeDefined()
  34. }),
  35. )
  36. it.effect("ignores non-Alibaba 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("alibaba"), ModelV2.ID.make("qwen")),
  44. api: { id: ModelV2.ID.make("qwen"), type: "aisdk", package: "test-provider" },
  45. }),
  46. package: "@ai-sdk/openai-compatible",
  47. options: { name: "alibaba" },
  48. })
  49. expect(result.sdk).toBeUndefined()
  50. }),
  51. )
  52. it.effect("matches the old bundled Alibaba SDK provider naming", () =>
  53. Effect.gen(function* () {
  54. const plugin = yield* PluginV2.Service
  55. const aisdk = yield* AISDK.Service
  56. yield* addPlugin()
  57. const result = yield* aisdk.runSDK({
  58. model: ModelV2.Info.make({
  59. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-alibaba"), ModelV2.ID.make("qwen")),
  60. api: { id: ModelV2.ID.make("qwen"), type: "aisdk", package: "test-provider" },
  61. }),
  62. package: "@ai-sdk/alibaba",
  63. options: { name: "custom-alibaba", apiKey: "test" },
  64. })
  65. const expected = createAlibaba({ apiKey: "test", ...{ name: "custom-alibaba" } }).languageModel("qwen")
  66. const actual = result.sdk?.languageModel("qwen")
  67. expect(actual?.provider).toBe(expected.provider)
  68. expect(actual?.modelId).toBe(expected.modelId)
  69. }),
  70. )
  71. it.effect("uses the old default languageModel(api.id) behavior", () =>
  72. Effect.gen(function* () {
  73. const plugin = yield* PluginV2.Service
  74. const aisdk = yield* AISDK.Service
  75. yield* addPlugin()
  76. const item = ModelV2.Info.make({
  77. ...ModelV2.Info.empty(ProviderV2.ID.make("alibaba"), ModelV2.ID.make("alias")),
  78. api: { id: ModelV2.ID.make("qwen-plus"), type: "aisdk", package: "test-provider" },
  79. })
  80. const result = yield* aisdk.runSDK({ model: item, package: "@ai-sdk/alibaba", options: {} })
  81. const language = result.sdk?.languageModel(item.api.id)
  82. expect(language?.modelId).toBe("qwen-plus")
  83. expect(language?.provider).toBe("alibaba.chat")
  84. }),
  85. )
  86. })