provider.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Schema } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { Config } from "@opencode-ai/core/config"
  5. import { ConfigProviderPlugin } from "@opencode-ai/core/config/plugin/provider"
  6. import { ModelV2 } from "@opencode-ai/core/model"
  7. import { PluginV2 } from "@opencode-ai/core/plugin"
  8. import { ProviderV2 } from "@opencode-ai/core/provider"
  9. import { it } from "../plugin/provider-helper"
  10. function request(headers: Record<string, string>, variant?: string) {
  11. return {
  12. headers,
  13. variant,
  14. }
  15. }
  16. const decode = Schema.decodeUnknownSync(Config.Info)
  17. describe("ConfigProviderPlugin.Plugin", () => {
  18. it.effect("loads configured providers and applies later model overrides", () =>
  19. Effect.gen(function* () {
  20. const catalog = yield* Catalog.Service
  21. const plugin = yield* PluginV2.Service
  22. const providerID = ProviderV2.ID.make("custom")
  23. const modelID = ModelV2.ID.make("chat")
  24. const config = Config.Service.of({
  25. entries: () =>
  26. Effect.succeed([
  27. new Config.Document({
  28. type: "document",
  29. info: decode({
  30. providers: {
  31. custom: {
  32. name: "Configured",
  33. env: ["CUSTOM_API_KEY"],
  34. api: { type: "native", settings: {} },
  35. request: request({ first: "first", shared: "first" }),
  36. models: {
  37. chat: {
  38. name: "First",
  39. capabilities: { tools: true, input: ["text"], output: ["text"] },
  40. disabled: true,
  41. limit: { context: 100, output: 50 },
  42. cost: { input: 1, output: 2 },
  43. request: request({ first: "first", shared: "first" }, "retained"),
  44. variants: [
  45. {
  46. id: "fast",
  47. headers: { first: "first", shared: "first" },
  48. },
  49. ],
  50. },
  51. },
  52. },
  53. },
  54. }),
  55. }),
  56. new Config.Document({
  57. type: "document",
  58. info: decode({
  59. providers: {
  60. custom: {
  61. api: { type: "aisdk", package: "custom-sdk", url: "https://example.test" },
  62. request: request({ last: "last", shared: "last" }),
  63. models: {
  64. chat: {
  65. api: { id: "api-chat" },
  66. name: "Last",
  67. limit: { output: 75 },
  68. request: request({ last: "last", shared: "last" }),
  69. variants: [
  70. {
  71. id: "fast",
  72. headers: { last: "last", shared: "last" },
  73. },
  74. {
  75. id: "slow",
  76. headers: { slow: "slow" },
  77. },
  78. ],
  79. },
  80. },
  81. },
  82. },
  83. }),
  84. }),
  85. new Config.Document({
  86. type: "document",
  87. info: decode({
  88. providers: {
  89. custom: { name: "Renamed" },
  90. },
  91. }),
  92. }),
  93. ]),
  94. })
  95. yield* plugin.add({
  96. ...ConfigProviderPlugin.Plugin,
  97. effect: ConfigProviderPlugin.Plugin.effect.pipe(
  98. Effect.provideService(Config.Service, config),
  99. Effect.provideService(Catalog.Service, catalog),
  100. ),
  101. })
  102. const provider = yield* catalog.provider.get(providerID)
  103. const model = yield* catalog.model.get(providerID, modelID)
  104. expect(provider.name).toBe("Renamed")
  105. expect(provider.env).toEqual(["CUSTOM_API_KEY"])
  106. expect(provider.enabled).toEqual({ via: "custom", data: {} })
  107. expect(provider.api).toEqual({ type: "aisdk", package: "custom-sdk", url: "https://example.test" })
  108. expect(provider.request.headers).toEqual({ first: "first", shared: "last", last: "last" })
  109. expect(model.api.id).toBe(ModelV2.ID.make("api-chat"))
  110. expect(model.name).toBe("Last")
  111. expect(model.capabilities).toEqual({ tools: true, input: ["text"], output: ["text"] })
  112. expect(model.enabled).toBe(false)
  113. expect(model.limit).toEqual({ context: 100, output: 75 })
  114. expect(model.cost).toEqual([{ input: 1, output: 2, cache: { read: 0, write: 0 }, tier: undefined }])
  115. expect(model.request.headers).toEqual({ first: "first", shared: "last", last: "last" })
  116. expect(model.request.variant).toBe("retained")
  117. expect(model.variants.map((variant) => variant.id)).toEqual([
  118. ModelV2.VariantID.make("fast"),
  119. ModelV2.VariantID.make("slow"),
  120. ])
  121. expect(model.variants[0]?.headers).toEqual({ first: "first", shared: "last", last: "last" })
  122. expect(model.variants[1]?.headers).toEqual({ slow: "slow" })
  123. }),
  124. )
  125. })