provider-cerebras.test.ts 4.9 KB

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