provider-llmgateway.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { Integration } from "@opencode-ai/core/integration"
  5. import { PluginV2 } from "@opencode-ai/core/plugin"
  6. import { PluginHost } from "@opencode-ai/core/plugin/host"
  7. import { ProviderPlugins } from "@opencode-ai/core/plugin/provider"
  8. import { LLMGatewayPlugin } from "@opencode-ai/core/plugin/provider/llmgateway"
  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 host = yield* PluginHost.make()
  16. yield* plugin.add({ id: LLMGatewayPlugin.id, effect: LLMGatewayPlugin.effect(host) })
  17. })
  18. describe("LLMGatewayPlugin", () => {
  19. it.effect("is registered so legacy referer headers can be applied", () =>
  20. Effect.sync(() => expect(ProviderPlugins.map((item) => item.id)).toContain(PluginV2.ID.make("llmgateway"))),
  21. )
  22. it.effect("applies legacy referer headers only to enabled llmgateway", () =>
  23. Effect.gen(function* () {
  24. const catalog = yield* Catalog.Service
  25. const integrations = yield* Integration.Service
  26. yield* integrations.transform((editor) => {
  27. editor.update(Integration.ID.make("llmgateway"), () => {})
  28. editor.update(Integration.ID.make("openrouter"), () => {})
  29. })
  30. yield* catalog.transform((catalog) => {
  31. catalog.provider.update(ProviderV2.ID.make("llmgateway"), (provider) => {
  32. provider.api = {
  33. type: "aisdk",
  34. package: "@ai-sdk/openai-compatible",
  35. url: "https://api.llmgateway.io/v1",
  36. }
  37. provider.request = { headers: { Existing: "value" }, body: {} }
  38. })
  39. catalog.provider.update(ProviderV2.ID.openrouter, () => {})
  40. })
  41. yield* addPlugin()
  42. expect((yield* catalog.provider.get(ProviderV2.ID.make("llmgateway")))?.request.headers).toEqual({
  43. Existing: "value",
  44. "HTTP-Referer": "https://opencode.ai/",
  45. "X-Title": "opencode",
  46. "X-Source": "opencode",
  47. })
  48. expect((yield* catalog.provider.get(ProviderV2.ID.openrouter))?.request.headers).toEqual({})
  49. }),
  50. )
  51. it.effect("does not apply legacy headers to a disabled llmgateway provider", () =>
  52. Effect.gen(function* () {
  53. const catalog = yield* Catalog.Service
  54. const integrations = yield* Integration.Service
  55. yield* integrations.transform((editor) => {
  56. editor.update(Integration.ID.make("llmgateway"), () => {})
  57. })
  58. yield* catalog.transform((catalog) => {
  59. catalog.provider.update(ProviderV2.ID.make("llmgateway"), (provider) => {
  60. provider.disabled = true
  61. provider.api = {
  62. type: "aisdk",
  63. package: "@ai-sdk/openai-compatible",
  64. url: "https://api.llmgateway.io/v1",
  65. }
  66. })
  67. })
  68. yield* addPlugin()
  69. expect((yield* catalog.provider.get(ProviderV2.ID.make("llmgateway")))?.disabled).toBe(true)
  70. expect((yield* catalog.provider.get(ProviderV2.ID.make("llmgateway")))?.request.headers).toEqual({})
  71. }),
  72. )
  73. })