provider-cerebras.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect, mock } from "bun:test"
  3. import { Effect } from "effect"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  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 { CerebrasPlugin } from "@opencode-ai/core/plugin/provider/cerebras"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const cerebrasOptions: Record<string, unknown>[] = []
  13. const it = testEffect(PluginTestLayer)
  14. const addPlugin = Effect.fn(function* () {
  15. const plugin = yield* PluginV2.Service
  16. const aisdk = yield* AISDK.Service
  17. const host = yield* PluginHost.make(plugin)
  18. yield* CerebrasPlugin.effect(host)
  19. })
  20. void mock.module("@ai-sdk/cerebras", () => ({
  21. createCerebras: (options: Record<string, unknown>) => {
  22. const snapshot = { ...options }
  23. cerebrasOptions.push(snapshot)
  24. return {
  25. languageModel: (modelID: string) => ({ modelID, provider: snapshot.name, specificationVersion: "v3" }),
  26. }
  27. },
  28. }))
  29. describe("CerebrasPlugin", () => {
  30. it.effect("applies the legacy integration header", () =>
  31. Effect.gen(function* () {
  32. const catalog = yield* Catalog.Service
  33. yield* catalog.transform((catalog) => {
  34. catalog.provider.update(ProviderV2.ID.make("cerebras"), (item) => {
  35. item.api = { type: "aisdk", package: "@ai-sdk/cerebras" }
  36. item.request.headers.Existing = "1"
  37. })
  38. })
  39. yield* addPlugin()
  40. expect((yield* catalog.provider.get(ProviderV2.ID.make("cerebras")))?.request.headers).toEqual({
  41. Existing: "1",
  42. "X-Cerebras-3rd-Party-Integration": "opencode",
  43. })
  44. }),
  45. )
  46. it.effect("ignores non-Cerebras providers", () =>
  47. Effect.gen(function* () {
  48. const catalog = yield* Catalog.Service
  49. yield* catalog.transform((catalog) => catalog.provider.update(ProviderV2.ID.make("groq"), () => {}))
  50. yield* addPlugin()
  51. expect((yield* catalog.provider.get(ProviderV2.ID.make("groq")))?.request.headers).toEqual({})
  52. }),
  53. )
  54. it.effect("creates a bundled Cerebras SDK with the model provider ID as the SDK name", () =>
  55. Effect.gen(function* () {
  56. cerebrasOptions.length = 0
  57. const plugin = yield* PluginV2.Service
  58. const aisdk = yield* AISDK.Service
  59. yield* addPlugin()
  60. const result = yield* aisdk.runSDK({
  61. model: ModelV2.Info.make({
  62. ...ModelV2.Info.empty(
  63. ProviderV2.ID.make("custom-cerebras"),
  64. ModelV2.ID.make("llama-4-scout-17b-16e-instruct"),
  65. ),
  66. api: {
  67. id: ModelV2.ID.make("llama-4-scout-17b-16e-instruct"),
  68. type: "aisdk",
  69. package: "test-provider",
  70. },
  71. }),
  72. package: "@ai-sdk/cerebras",
  73. options: { name: "custom-cerebras", apiKey: "test" },
  74. })
  75. expect(cerebrasOptions).toEqual([{ name: "custom-cerebras", apiKey: "test" }])
  76. expect(result.sdk.languageModel("llama-4-scout-17b-16e-instruct").provider).toBe("custom-cerebras")
  77. }),
  78. )
  79. it.effect("preserves an explicit bundled Cerebras SDK name option", () =>
  80. Effect.gen(function* () {
  81. cerebrasOptions.length = 0
  82. const plugin = yield* PluginV2.Service
  83. const aisdk = yield* AISDK.Service
  84. yield* addPlugin()
  85. yield* aisdk.runSDK({
  86. model: ModelV2.Info.make({
  87. ...ModelV2.Info.empty(
  88. ProviderV2.ID.make("custom-cerebras"),
  89. ModelV2.ID.make("llama-4-scout-17b-16e-instruct"),
  90. ),
  91. api: {
  92. id: ModelV2.ID.make("llama-4-scout-17b-16e-instruct"),
  93. type: "aisdk",
  94. package: "test-provider",
  95. },
  96. }),
  97. package: "@ai-sdk/cerebras",
  98. options: { name: "configured-cerebras", apiKey: "test" },
  99. })
  100. expect(cerebrasOptions).toEqual([{ name: "configured-cerebras", apiKey: "test" }])
  101. }),
  102. )
  103. it.effect("ignores non-Cerebras SDK packages", () =>
  104. Effect.gen(function* () {
  105. cerebrasOptions.length = 0
  106. const plugin = yield* PluginV2.Service
  107. const aisdk = yield* AISDK.Service
  108. yield* addPlugin()
  109. const result = yield* aisdk.runSDK({
  110. model: ModelV2.Info.make({
  111. ...ModelV2.Info.empty(
  112. ProviderV2.ID.make("custom-cerebras"),
  113. ModelV2.ID.make("llama-4-scout-17b-16e-instruct"),
  114. ),
  115. api: {
  116. id: ModelV2.ID.make("llama-4-scout-17b-16e-instruct"),
  117. type: "aisdk",
  118. package: "test-provider",
  119. },
  120. }),
  121. package: "@ai-sdk/groq",
  122. options: { name: "custom-cerebras", apiKey: "test" },
  123. })
  124. expect(cerebrasOptions).toEqual([])
  125. expect(result.sdk).toBeUndefined()
  126. }),
  127. )
  128. })