provider-togetherai.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. modelID: ModelV2.ID.make("model"),
  41. package: "aisdk:test-provider",
  42. }),
  43. package: "@ai-sdk/togetherai",
  44. options: { name: "togetherai" },
  45. })
  46. expect(result.sdk).toBeDefined()
  47. }),
  48. )
  49. it.effect("matches the old bundled provider package exactly", () =>
  50. Effect.gen(function* () {
  51. const plugin = yield* PluginV2.Service
  52. const aisdk = yield* AISDK.Service
  53. yield* addPlugin()
  54. const ignored = yield* aisdk.runSDK({
  55. model: ModelV2.Info.make({
  56. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  57. modelID: ModelV2.ID.make("model"),
  58. package: "aisdk:test-provider",
  59. }),
  60. package: "file:///tmp/@ai-sdk/togetherai-provider.js",
  61. options: { name: "togetherai" },
  62. })
  63. expect(ignored.sdk).toBeUndefined()
  64. const result = yield* aisdk.runSDK({
  65. model: ModelV2.Info.make({
  66. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  67. modelID: ModelV2.ID.make("model"),
  68. package: "aisdk:test-provider",
  69. }),
  70. package: "@ai-sdk/togetherai",
  71. options: { name: "togetherai" },
  72. })
  73. expect(result.sdk).toBeDefined()
  74. }),
  75. )
  76. it.effect("creates bundled TogetherAI SDKs for custom provider IDs", () =>
  77. Effect.gen(function* () {
  78. const plugin = yield* PluginV2.Service
  79. const aisdk = yield* AISDK.Service
  80. yield* addPlugin()
  81. const result = yield* aisdk.runSDK({
  82. model: ModelV2.Info.make({
  83. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-togetherai"), ModelV2.ID.make("model")),
  84. modelID: ModelV2.ID.make("model"),
  85. package: "aisdk:test-provider",
  86. }),
  87. package: "@ai-sdk/togetherai",
  88. options: { name: "custom-togetherai" },
  89. })
  90. expect(result.sdk.languageModel("model").provider).toBe("togetherai.chat")
  91. }),
  92. )
  93. it.effect("defaults language selection to sdk.languageModel with the model API ID", () =>
  94. Effect.gen(function* () {
  95. const plugin = yield* PluginV2.Service
  96. const aisdk = yield* AISDK.Service
  97. const calls: string[] = []
  98. yield* addPlugin()
  99. const result = yield* aisdk.runLanguage({
  100. model: ModelV2.Info.make({
  101. ...ModelV2.Info.empty(
  102. ProviderV2.ID.make("togetherai"),
  103. ModelV2.ID.make("meta-llama/Llama-3.3-70B-Instruct-Turbo"),
  104. ),
  105. modelID: ModelV2.ID.make("meta-llama/Llama-3.3-70B-Instruct-Turbo"),
  106. package: "aisdk:test-provider",
  107. }),
  108. sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
  109. options: {},
  110. })
  111. expect(result.language).toBeUndefined()
  112. expect(calls).toEqual([])
  113. expect(
  114. result.language ?? fakeSelectorSdk(calls).languageModel(result.model.modelID ?? result.model.id),
  115. ).toBeDefined()
  116. expect(calls).toEqual(["languageModel:meta-llama/Llama-3.3-70B-Instruct-Turbo"])
  117. }),
  118. )
  119. })