provider-togetherai.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { describe, expect } from "bun:test"
  2. import type { LanguageModelV3 } from "@ai-sdk/provider"
  3. import { Effect } from "effect"
  4. import { ModelV2 } from "@opencode-ai/core/model"
  5. import { PluginV2 } from "@opencode-ai/core/plugin"
  6. import { PluginHost } from "@opencode-ai/core/plugin/host"
  7. import { TogetherAIPlugin } from "@opencode-ai/core/plugin/provider/togetherai"
  8. import { ProviderV2 } from "@opencode-ai/core/provider"
  9. import { testEffect } from "../lib/effect"
  10. import { PluginTestLayer } from "./fixture"
  11. const it = testEffect(PluginTestLayer)
  12. const addPlugin = Effect.fn(function* () {
  13. const plugin = yield* PluginV2.Service
  14. const host = yield* PluginHost.make()
  15. yield* plugin.add({ id: TogetherAIPlugin.id, effect: TogetherAIPlugin.effect(host) })
  16. })
  17. function fakeSelectorSdk(calls: string[]) {
  18. const make = (method: string) => (id: string) => {
  19. calls.push(`${method}:${id}`)
  20. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  21. }
  22. return {
  23. responses: make("responses"),
  24. messages: make("messages"),
  25. chat: make("chat"),
  26. languageModel: make("languageModel"),
  27. }
  28. }
  29. describe("TogetherAIPlugin", () => {
  30. it.effect("creates a TogetherAI SDK for @ai-sdk/togetherai", () =>
  31. Effect.gen(function* () {
  32. const plugin = yield* PluginV2.Service
  33. yield* addPlugin()
  34. const result = yield* plugin.trigger(
  35. "aisdk.sdk",
  36. {
  37. model: new ModelV2.Info({
  38. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  39. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  40. }),
  41. package: "@ai-sdk/togetherai",
  42. options: { name: "togetherai" },
  43. },
  44. {},
  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. yield* addPlugin()
  53. const ignored = yield* plugin.trigger(
  54. "aisdk.sdk",
  55. {
  56. model: new ModelV2.Info({
  57. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  58. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  59. }),
  60. package: "file:///tmp/@ai-sdk/togetherai-provider.js",
  61. options: { name: "togetherai" },
  62. },
  63. {},
  64. )
  65. expect(ignored.sdk).toBeUndefined()
  66. const result = yield* plugin.trigger(
  67. "aisdk.sdk",
  68. {
  69. model: new ModelV2.Info({
  70. ...ModelV2.Info.empty(ProviderV2.ID.make("togetherai"), ModelV2.ID.make("model")),
  71. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  72. }),
  73. package: "@ai-sdk/togetherai",
  74. options: { name: "togetherai" },
  75. },
  76. {},
  77. )
  78. expect(result.sdk).toBeDefined()
  79. }),
  80. )
  81. it.effect("creates bundled TogetherAI SDKs for custom provider IDs", () =>
  82. Effect.gen(function* () {
  83. const plugin = yield* PluginV2.Service
  84. yield* addPlugin()
  85. const result = yield* plugin.trigger(
  86. "aisdk.sdk",
  87. {
  88. model: new ModelV2.Info({
  89. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-togetherai"), ModelV2.ID.make("model")),
  90. api: { id: ModelV2.ID.make("model"), type: "aisdk", package: "test-provider" },
  91. }),
  92. package: "@ai-sdk/togetherai",
  93. options: { name: "custom-togetherai" },
  94. },
  95. {},
  96. )
  97. expect(result.sdk.languageModel("model").provider).toBe("togetherai.chat")
  98. }),
  99. )
  100. it.effect("defaults language selection to sdk.languageModel with the model API ID", () =>
  101. Effect.gen(function* () {
  102. const plugin = yield* PluginV2.Service
  103. const calls: string[] = []
  104. yield* addPlugin()
  105. const result = yield* plugin.trigger(
  106. "aisdk.language",
  107. {
  108. model: new ModelV2.Info({
  109. ...ModelV2.Info.empty(
  110. ProviderV2.ID.make("togetherai"),
  111. ModelV2.ID.make("meta-llama/Llama-3.3-70B-Instruct-Turbo"),
  112. ),
  113. api: {
  114. id: ModelV2.ID.make("meta-llama/Llama-3.3-70B-Instruct-Turbo"),
  115. type: "aisdk",
  116. package: "test-provider",
  117. },
  118. }),
  119. sdk: { languageModel: fakeSelectorSdk(calls).languageModel },
  120. options: {},
  121. },
  122. {},
  123. )
  124. expect(result.language).toBeUndefined()
  125. expect(calls).toEqual([])
  126. expect(result.language ?? fakeSelectorSdk(calls).languageModel(result.model.api.id)).toBeDefined()
  127. expect(calls).toEqual(["languageModel:meta-llama/Llama-3.3-70B-Instruct-Turbo"])
  128. }),
  129. )
  130. })