provider-togetherai.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect } from "bun:test"
  3. import type { LanguageModelV3 } from "@ai-sdk/provider"
  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 { TogetherAIPlugin } from "@opencode-ai/core/plugin/provider/togetherai"
  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* TogetherAIPlugin.effect(host)
  18. })
  19. function fakeSelectorSdk(calls: string[]) {
  20. const make = (method: string) => (id: string) => {
  21. calls.push(`${method}:${id}`)
  22. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  23. }
  24. return {
  25. responses: make("responses"),
  26. messages: make("messages"),
  27. chat: make("chat"),
  28. languageModel: make("languageModel"),
  29. }
  30. }
  31. describe("TogetherAIPlugin", () => {
  32. it.effect("creates a TogetherAI SDK for @ai-sdk/togetherai", () =>
  33. Effect.gen(function* () {
  34. const plugin = yield* PluginV2.Service
  35. const aisdk = yield* AISDK.Service
  36. yield* addPlugin()
  37. const result = yield* aisdk.runSDK({
  38. model: ModelV2.Info.make({
  39. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  40. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  41. }),
  42. package: "@ai-sdk/togetherai",
  43. options: { name: "togetherai" },
  44. })
  45. expect(result.sdk).toBeDefined()
  46. }),
  47. )
  48. it.effect("matches the old bundled provider package exactly", () =>
  49. Effect.gen(function* () {
  50. const plugin = yield* PluginV2.Service
  51. const aisdk = yield* AISDK.Service
  52. yield* addPlugin()
  53. const ignored = yield* aisdk.runSDK({
  54. model: ModelV2.Info.make({
  55. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  56. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  57. }),
  58. package: "file:///tmp/@ai-sdk/togetherai-provider.js",
  59. options: { name: "togetherai" },
  60. })
  61. expect(ignored.sdk).toBeUndefined()
  62. const result = yield* aisdk.runSDK({
  63. model: ModelV2.Info.make({
  64. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  65. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  66. }),
  67. package: "@ai-sdk/togetherai",
  68. options: { name: "togetherai" },
  69. })
  70. expect(result.sdk).toBeDefined()
  71. }),
  72. )
  73. it.effect("creates bundled TogetherAI SDKs for custom provider IDs", () =>
  74. Effect.gen(function* () {
  75. const plugin = yield* PluginV2.Service
  76. const aisdk = yield* AISDK.Service
  77. yield* addPlugin()
  78. const result = yield* aisdk.runSDK({
  79. model: ModelV2.Info.make({
  80. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-togetherai"), ModelV2.ID.make("model")),
  81. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  82. }),
  83. package: "@ai-sdk/togetherai",
  84. options: { name: "custom-togetherai" },
  85. })
  86. expect(result.sdk.languageModel("model").provider).toBe("togetherai.chat")
  87. }),
  88. )
  89. it.effect("defaults language selection to sdk.languageModel with the model API ID", () =>
  90. Effect.gen(function* () {
  91. const plugin = yield* PluginV2.Service
  92. const aisdk = yield* AISDK.Service
  93. const calls: string[] = []
  94. yield* addPlugin()
  95. const result = yield* aisdk.runLanguage({
  96. model: ModelV2.Info.make({
  97. ...ModelV2.Info.empty(
  98. ProviderV2.ID.make("togetherai"),
  99. ModelV2.ID.make("meta-llama/Llama-3.3-70B-Instruct-Turbo"),
  100. ),
  101. api: {
  102. id: ModelV2.ID.make("meta-llama/Llama-3.3-70B-Instruct-Turbo"),
  103. type: "aisdk",
  104. package: "test-provider",
  105. },
  106. }),
  107. sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
  108. options: {},
  109. })
  110. expect(result.language).toBeUndefined()
  111. expect(calls).toEqual([])
  112. expect(result.language ?? fakeSelectorSdk(calls).languageModel(result.model.api.id)).toBeDefined()
  113. expect(calls).toEqual(["languageModel:meta-llama/Llama-3.3-70B-Instruct-Turbo"])
  114. }),
  115. )
  116. })