provider.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Option, 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 { Integration } from "@opencode-ai/core/integration"
  7. import { ModelV2 } from "@opencode-ai/core/model"
  8. import { PluginV2 } from "@opencode-ai/core/plugin"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { it, required, withEnv } from "../plugin/provider-helper"
  11. import { catalogHost, host, integrationHost } from "../plugin/host"
  12. function request(headers: Record<string, string>, variant?: string) {
  13. return {
  14. headers,
  15. variant,
  16. }
  17. }
  18. const decode = Schema.decodeUnknownSync(Config.Info)
  19. describe("ConfigProviderPlugin.Plugin", () => {
  20. it.effect("partitions existing model variant bodies without changing config shape", () =>
  21. Effect.gen(function* () {
  22. const catalog = yield* Catalog.Service
  23. const integrations = yield* Integration.Service
  24. const plugin = yield* PluginV2.Service
  25. const providerID = ProviderV2.ID.opencode
  26. const modelID = ModelV2.ID.make("alpha-gpt-next")
  27. const config = Config.Service.of({
  28. entries: () =>
  29. Effect.succeed([
  30. new Config.Document({
  31. type: "document",
  32. info: decode({
  33. providers: {
  34. opencode: {
  35. api: { type: "aisdk", package: "@ai-sdk/openai", url: "https://opencode.test/v1" },
  36. models: {
  37. "alpha-gpt-next": {
  38. variants: [
  39. {
  40. id: "high",
  41. body: {
  42. reasoningEffort: "high",
  43. reasoningSummary: "auto",
  44. include: ["reasoning.encrypted_content"],
  45. },
  46. },
  47. ],
  48. },
  49. },
  50. },
  51. },
  52. }),
  53. }),
  54. ]),
  55. })
  56. yield* plugin.add({
  57. ...ConfigProviderPlugin.Plugin,
  58. effect: ConfigProviderPlugin.Plugin.effect(
  59. host({ catalog: catalogHost(catalog), integration: integrationHost(integrations) }),
  60. ).pipe(Effect.provideService(Config.Service, config)),
  61. })
  62. const model = required(yield* catalog.model.get(providerID, modelID))
  63. expect(model.variants).toMatchObject([
  64. {
  65. id: "high",
  66. body: {},
  67. options: {
  68. reasoningEffort: "high",
  69. reasoningSummary: "auto",
  70. include: ["reasoning.encrypted_content"],
  71. },
  72. },
  73. ])
  74. }),
  75. )
  76. it.effect("uses the effective provider package across layered config", () =>
  77. Effect.gen(function* () {
  78. const catalog = yield* Catalog.Service
  79. const integrations = yield* Integration.Service
  80. const plugin = yield* PluginV2.Service
  81. const providerID = ProviderV2.ID.opencode
  82. const modelID = ModelV2.ID.make("alpha-gpt-next")
  83. const config = Config.Service.of({
  84. entries: () =>
  85. Effect.succeed([
  86. new Config.Document({
  87. type: "document",
  88. info: decode({
  89. providers: {
  90. opencode: {
  91. api: { type: "aisdk", package: "@ai-sdk/openai", url: "https://opencode.test/v1" },
  92. },
  93. },
  94. }),
  95. }),
  96. new Config.Document({
  97. type: "document",
  98. info: decode({
  99. providers: {
  100. opencode: {
  101. models: {
  102. "alpha-gpt-next": {
  103. variants: [{ id: "high", body: { reasoningEffort: "high" } }],
  104. },
  105. },
  106. },
  107. },
  108. }),
  109. }),
  110. ]),
  111. })
  112. yield* plugin.add({
  113. ...ConfigProviderPlugin.Plugin,
  114. effect: ConfigProviderPlugin.Plugin.effect(
  115. host({ catalog: catalogHost(catalog), integration: integrationHost(integrations) }),
  116. ).pipe(Effect.provideService(Config.Service, config)),
  117. })
  118. const model = required(yield* catalog.model.get(providerID, modelID))
  119. expect(model.variants[0]).toMatchObject({
  120. id: "high",
  121. body: {},
  122. options: { reasoningEffort: "high" },
  123. })
  124. }),
  125. )
  126. it.effect("loads configured providers and applies later model overrides", () =>
  127. withEnv({ CUSTOM_API_KEY: "secret" }, () =>
  128. Effect.gen(function* () {
  129. const catalog = yield* Catalog.Service
  130. const integrations = yield* Integration.Service
  131. const plugin = yield* PluginV2.Service
  132. const providerID = ProviderV2.ID.make("custom")
  133. const modelID = ModelV2.ID.make("chat")
  134. const config = Config.Service.of({
  135. entries: () =>
  136. Effect.succeed([
  137. new Config.Document({
  138. type: "document",
  139. info: decode({
  140. model: "custom/first",
  141. providers: {
  142. custom: {
  143. name: "Configured",
  144. env: ["CUSTOM_API_KEY"],
  145. api: { type: "native", settings: {} },
  146. request: request({ first: "first", shared: "first" }),
  147. models: {
  148. chat: {
  149. name: "First",
  150. capabilities: { tools: true, input: ["text"], output: ["text"] },
  151. disabled: true,
  152. limit: { context: 100, output: 50 },
  153. cost: { input: 1, output: 2 },
  154. request: request({ first: "first", shared: "first" }, "retained"),
  155. variants: [
  156. {
  157. id: "fast",
  158. headers: { first: "first", shared: "first" },
  159. },
  160. ],
  161. },
  162. },
  163. },
  164. },
  165. }),
  166. }),
  167. new Config.Document({
  168. type: "document",
  169. info: decode({
  170. model: "custom/default",
  171. providers: {
  172. custom: {
  173. api: { type: "aisdk", package: "custom-sdk", url: "https://example.test" },
  174. request: request({ last: "last", shared: "last" }),
  175. models: {
  176. default: {
  177. name: "Default",
  178. },
  179. chat: {
  180. api: { id: "api-chat" },
  181. name: "Last",
  182. limit: { output: 75 },
  183. request: request({ last: "last", shared: "last" }),
  184. variants: [
  185. {
  186. id: "fast",
  187. headers: { last: "last", shared: "last" },
  188. },
  189. {
  190. id: "slow",
  191. headers: { slow: "slow" },
  192. },
  193. ],
  194. },
  195. },
  196. },
  197. },
  198. }),
  199. }),
  200. new Config.Document({
  201. type: "document",
  202. info: decode({
  203. providers: {
  204. custom: { name: "Renamed" },
  205. },
  206. }),
  207. }),
  208. ]),
  209. })
  210. yield* plugin.add({
  211. ...ConfigProviderPlugin.Plugin,
  212. effect: ConfigProviderPlugin.Plugin.effect(
  213. host({ catalog: catalogHost(catalog), integration: integrationHost(integrations) }),
  214. ).pipe(Effect.provideService(Config.Service, config)),
  215. })
  216. const provider = required(yield* catalog.provider.get(providerID))
  217. const model = required(yield* catalog.model.get(providerID, modelID))
  218. expect((yield* catalog.model.default())?.id).toBe(ModelV2.ID.make("default"))
  219. expect(provider.name).toBe("Renamed")
  220. expect((yield* integrations.get(Integration.ID.make("custom")))?.methods).toContainEqual({
  221. type: "env",
  222. names: ["CUSTOM_API_KEY"],
  223. })
  224. expect((yield* integrations.get(Integration.ID.make("custom")))?.name).toBe("Renamed")
  225. expect(provider.disabled).toBeUndefined()
  226. expect(provider.api).toEqual({ type: "aisdk", package: "custom-sdk", url: "https://example.test" })
  227. expect(provider.request.headers).toEqual({ first: "first", shared: "last", last: "last" })
  228. expect(model.api.id).toBe(ModelV2.ID.make("api-chat"))
  229. expect(model.name).toBe("Last")
  230. expect(model.capabilities).toEqual({ tools: true, input: ["text"], output: ["text"] })
  231. expect(model.enabled).toBe(false)
  232. expect(model.limit).toEqual({ context: 100, output: 75 })
  233. expect(model.cost).toEqual([{ input: 1, output: 2, cache: { read: 0, write: 0 }, tier: undefined }])
  234. expect(model.request.headers).toEqual({ first: "first", shared: "last", last: "last" })
  235. expect(model.request.variant).toBe("retained")
  236. expect(model.variants.map((variant) => variant.id)).toEqual([
  237. ModelV2.VariantID.make("fast"),
  238. ModelV2.VariantID.make("slow"),
  239. ])
  240. expect(model.variants[0]?.headers).toEqual({ first: "first", shared: "last", last: "last" })
  241. expect(model.variants[1]?.headers).toEqual({ slow: "slow" })
  242. }),
  243. ),
  244. )
  245. })