provider.test.ts 9.9 KB

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