provider.test.ts 9.8 KB

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