provider-groq.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect } from "bun:test"
  3. import { createGroq } from "@ai-sdk/groq"
  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 { GroqPlugin } from "@opencode-ai/core/plugin/provider/groq"
  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* GroqPlugin.effect(host)
  18. })
  19. describe("GroqPlugin", () => {
  20. it.effect("creates a Groq SDK for @ai-sdk/groq", () =>
  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("groq"), ModelV2.ID.make("llama")),
  28. modelID: ModelV2.ID.make("llama"),
  29. package: "aisdk:@ai-sdk/groq",
  30. }),
  31. package: "@ai-sdk/groq",
  32. options: { name: "groq" },
  33. })
  34. expect(result.sdk).toBeDefined()
  35. }),
  36. )
  37. it.effect("ignores non-Groq SDK packages", () =>
  38. Effect.gen(function* () {
  39. const plugin = yield* PluginV2.Service
  40. const aisdk = yield* AISDK.Service
  41. yield* addPlugin()
  42. const result = yield* aisdk.runSDK({
  43. model: ModelV2.Info.make({
  44. ...ModelV2.Info.empty(ProviderV2.ID.make("groq"), ModelV2.ID.make("llama")),
  45. modelID: ModelV2.ID.make("llama"),
  46. package: "aisdk:@ai-sdk/groq",
  47. }),
  48. package: "@ai-sdk/openai-compatible",
  49. options: { name: "groq" },
  50. })
  51. expect(result.sdk).toBeUndefined()
  52. }),
  53. )
  54. it.effect("only matches the bundled @ai-sdk/groq package exactly", () =>
  55. Effect.gen(function* () {
  56. const plugin = yield* PluginV2.Service
  57. const aisdk = yield* AISDK.Service
  58. yield* addPlugin()
  59. const result = yield* aisdk.runSDK({
  60. model: ModelV2.Info.make({
  61. ...ModelV2.Info.empty(ProviderV2.ID.make("groq"), ModelV2.ID.make("llama")),
  62. modelID: ModelV2.ID.make("llama"),
  63. package: "aisdk:@ai-sdk/groq",
  64. }),
  65. package: "@ai-sdk/groq/compat",
  66. options: { name: "groq" },
  67. })
  68. expect(result.sdk).toBeUndefined()
  69. }),
  70. )
  71. it.effect("matches the old bundled Groq SDK provider naming", () =>
  72. Effect.gen(function* () {
  73. const plugin = yield* PluginV2.Service
  74. const aisdk = yield* AISDK.Service
  75. yield* addPlugin()
  76. const result = yield* aisdk.runSDK({
  77. model: ModelV2.Info.make({
  78. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-groq"), ModelV2.ID.make("llama")),
  79. modelID: ModelV2.ID.make("llama"),
  80. package: "aisdk:@ai-sdk/groq",
  81. }),
  82. package: "@ai-sdk/groq",
  83. options: { name: "custom-groq", apiKey: "test" },
  84. })
  85. const expected = createGroq({ name: "custom-groq", apiKey: "test" } as Parameters<typeof createGroq>[0] & {
  86. name: string
  87. }).languageModel("llama")
  88. const actual = result.sdk?.languageModel("llama")
  89. expect(actual?.provider).toBe(expected.provider)
  90. expect(actual?.modelId).toBe(expected.modelId)
  91. }),
  92. )
  93. it.effect("uses the default languageModel(modelID) behavior", () =>
  94. Effect.gen(function* () {
  95. const plugin = yield* PluginV2.Service
  96. const aisdk = yield* AISDK.Service
  97. yield* addPlugin()
  98. const sdk = createGroq({ name: "groq", apiKey: "test" } as Parameters<typeof createGroq>[0] & {
  99. name: string
  100. })
  101. const result = yield* aisdk.runLanguage({
  102. model: ModelV2.Info.make({
  103. ...ModelV2.Info.empty(ProviderV2.ID.make("groq"), ModelV2.ID.make("alias")),
  104. modelID: ModelV2.ID.make("llama-api"),
  105. package: "aisdk:@ai-sdk/groq",
  106. }),
  107. sdk,
  108. options: { name: "groq", apiKey: "test" },
  109. })
  110. const language = result.language ?? sdk.languageModel(result.model.modelID ?? result.model.id)
  111. expect(language.modelId).toBe("llama-api")
  112. expect(language.provider).toBe("groq.chat")
  113. }),
  114. )
  115. })