provider-llmgateway.test.ts 3.2 KB

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