provider-llmgateway.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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("opencode.provider.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.package = ProviderV2.aisdk("@ai-sdk/openai-compatible")
  34. provider.settings = { baseURL: "https://api.llmgateway.io/v1" }
  35. provider.headers = { Existing: "value" }
  36. })
  37. catalog.provider.update(ProviderV2.ID.openrouter, () => {})
  38. })
  39. yield* addPlugin()
  40. expect((yield* catalog.provider.get(ProviderV2.ID.make("llmgateway")))?.headers).toEqual({
  41. Existing: "value",
  42. "HTTP-Referer": "https://opencode.ai/",
  43. "X-Title": "opencode",
  44. "X-Source": "opencode",
  45. })
  46. expect((yield* catalog.provider.get(ProviderV2.ID.openrouter))?.headers).toBeUndefined()
  47. }),
  48. )
  49. it.effect("does not apply legacy headers to a disabled llmgateway provider", () =>
  50. Effect.gen(function* () {
  51. const catalog = yield* Catalog.Service
  52. const integrations = yield* Integration.Service
  53. yield* integrations.transform((editor) => {
  54. editor.update(Integration.ID.make("llmgateway"), () => {})
  55. })
  56. yield* catalog.transform((catalog) => {
  57. catalog.provider.update(ProviderV2.ID.make("llmgateway"), (provider) => {
  58. provider.disabled = true
  59. provider.package = ProviderV2.aisdk("@ai-sdk/openai-compatible")
  60. provider.settings = { baseURL: "https://api.llmgateway.io/v1" }
  61. })
  62. })
  63. yield* addPlugin()
  64. expect((yield* catalog.provider.get(ProviderV2.ID.make("llmgateway")))?.disabled).toBe(true)
  65. expect((yield* catalog.provider.get(ProviderV2.ID.make("llmgateway")))?.headers).toBeUndefined()
  66. }),
  67. )
  68. })