provider.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 options(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. directories: () => Effect.succeed([]),
  26. get: () =>
  27. Effect.succeed([
  28. new Config.Loaded({
  29. source: { type: "memory" },
  30. info: decode({
  31. providers: {
  32. custom: {
  33. name: "Configured",
  34. env: ["CUSTOM_API_KEY"],
  35. endpoint: { type: "unknown" },
  36. options: options({ first: "first", shared: "first" }),
  37. models: {
  38. chat: {
  39. name: "First",
  40. capabilities: { tools: true, input: ["text"], output: ["text"] },
  41. disabled: true,
  42. limit: { context: 100, output: 50 },
  43. cost: { input: 1, output: 2 },
  44. options: options({ first: "first", shared: "first" }, "retained"),
  45. variants: [
  46. {
  47. id: "fast",
  48. headers: { first: "first", shared: "first" },
  49. },
  50. ],
  51. },
  52. },
  53. },
  54. },
  55. }),
  56. }),
  57. new Config.Loaded({
  58. source: { type: "memory" },
  59. info: decode({
  60. providers: {
  61. custom: {
  62. endpoint: { type: "aisdk", package: "custom-sdk", url: "https://example.test" },
  63. options: options({ last: "last", shared: "last" }),
  64. models: {
  65. chat: {
  66. api_id: "api-chat",
  67. name: "Last",
  68. limit: { output: 75 },
  69. options: options({ last: "last", shared: "last" }),
  70. variants: [
  71. {
  72. id: "fast",
  73. headers: { last: "last", shared: "last" },
  74. },
  75. {
  76. id: "slow",
  77. headers: { slow: "slow" },
  78. },
  79. ],
  80. },
  81. },
  82. },
  83. },
  84. }),
  85. }),
  86. new Config.Loaded({
  87. source: { type: "memory" },
  88. info: decode({
  89. providers: {
  90. custom: { name: "Renamed" },
  91. },
  92. }),
  93. }),
  94. ]),
  95. })
  96. yield* plugin.add({
  97. ...ConfigProviderPlugin.Plugin,
  98. effect: ConfigProviderPlugin.Plugin.effect.pipe(
  99. Effect.provideService(Config.Service, config),
  100. Effect.provideService(Catalog.Service, catalog),
  101. ),
  102. })
  103. const provider = yield* catalog.provider.get(providerID)
  104. const model = yield* catalog.model.get(providerID, modelID)
  105. expect(provider.name).toBe("Renamed")
  106. expect(provider.env).toEqual(["CUSTOM_API_KEY"])
  107. expect(provider.enabled).toEqual({ via: "custom", data: {} })
  108. expect(provider.endpoint).toEqual({ type: "aisdk", package: "custom-sdk", url: "https://example.test" })
  109. expect(provider.options.headers).toEqual({ first: "first", shared: "last", last: "last" })
  110. expect(model.apiID).toBe(ModelV2.ID.make("api-chat"))
  111. expect(model.name).toBe("Last")
  112. expect(model.capabilities).toEqual({ tools: true, input: ["text"], output: ["text"] })
  113. expect(model.enabled).toBe(false)
  114. expect(model.limit).toEqual({ context: 100, output: 75 })
  115. expect(model.cost).toEqual([{ input: 1, output: 2, cache: { read: 0, write: 0 }, tier: undefined }])
  116. expect(model.options.headers).toEqual({ first: "first", shared: "last", last: "last" })
  117. expect(model.options.variant).toBe("retained")
  118. expect(model.variants.map((variant) => variant.id)).toEqual([
  119. ModelV2.VariantID.make("fast"),
  120. ModelV2.VariantID.make("slow"),
  121. ])
  122. expect(model.variants[0]?.headers).toEqual({ first: "first", shared: "last", last: "last" })
  123. expect(model.variants[1]?.headers).toEqual({ slow: "slow" })
  124. }),
  125. )
  126. })