provider-groq.test.ts 4.5 KB

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