provider.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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("keeps configured model variant bodies unchanged", () =>
  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. reasoningEffort: "high",
  92. reasoningSummary: "auto",
  93. include: ["reasoning.encrypted_content"],
  94. },
  95. },
  96. ])
  97. }),
  98. )
  99. it.effect("keeps layered model variant bodies unchanged", () =>
  100. Effect.gen(function* () {
  101. const catalog = yield* Catalog.Service
  102. const providerID = ProviderV2.ID.opencode
  103. const modelID = ModelV2.ID.make("alpha-gpt-next")
  104. const config = Config.Service.of({
  105. entries: () =>
  106. Effect.succeed([
  107. new Config.Document({
  108. type: "document",
  109. info: decode({
  110. providers: {
  111. opencode: {
  112. api: { type: "aisdk", package: "@ai-sdk/openai", url: "https://opencode.test/v1" },
  113. },
  114. },
  115. }),
  116. }),
  117. new Config.Document({
  118. type: "document",
  119. info: decode({
  120. providers: {
  121. opencode: {
  122. models: {
  123. "alpha-gpt-next": {
  124. variants: [{ id: "high", body: { reasoningEffort: "high" } }],
  125. },
  126. },
  127. },
  128. },
  129. }),
  130. }),
  131. ]),
  132. })
  133. yield* addPlugin(config)
  134. const model = required(yield* catalog.model.get(providerID, modelID))
  135. expect(model.variants[0]).toMatchObject({
  136. id: "high",
  137. body: { reasoningEffort: "high" },
  138. })
  139. }),
  140. )
  141. it.effect("loads configured providers and applies later model overrides", () =>
  142. withEnv({ CUSTOM_API_KEY: "secret" }, () =>
  143. Effect.gen(function* () {
  144. const catalog = yield* Catalog.Service
  145. const integrations = yield* Integration.Service
  146. const providerID = ProviderV2.ID.make("custom")
  147. const modelID = ModelV2.ID.make("chat")
  148. const config = Config.Service.of({
  149. entries: () =>
  150. Effect.succeed([
  151. new Config.Document({
  152. type: "document",
  153. info: decode({
  154. model: "custom/first",
  155. providers: {
  156. custom: {
  157. name: "Configured",
  158. env: ["CUSTOM_API_KEY"],
  159. api: { type: "native", settings: {} },
  160. request: request({ first: "first", shared: "first" }),
  161. models: {
  162. chat: {
  163. name: "First",
  164. capabilities: { tools: true, input: ["text"], output: ["text"] },
  165. disabled: true,
  166. limit: { context: 100, output: 50 },
  167. cost: { input: 1, output: 2 },
  168. request: request({ first: "first", shared: "first" }, "retained"),
  169. variants: [
  170. {
  171. id: "fast",
  172. headers: { first: "first", shared: "first" },
  173. },
  174. ],
  175. },
  176. },
  177. },
  178. },
  179. }),
  180. }),
  181. new Config.Document({
  182. type: "document",
  183. info: decode({
  184. model: "custom/default",
  185. providers: {
  186. custom: {
  187. api: { type: "aisdk", package: "custom-sdk", url: "https://example.test" },
  188. request: request({ last: "last", shared: "last" }),
  189. models: {
  190. default: {
  191. name: "Default",
  192. },
  193. chat: {
  194. api: { id: "api-chat" },
  195. name: "Last",
  196. limit: { output: 75 },
  197. request: request({ last: "last", shared: "last" }),
  198. variants: [
  199. {
  200. id: "fast",
  201. headers: { last: "last", shared: "last" },
  202. },
  203. {
  204. id: "slow",
  205. headers: { slow: "slow" },
  206. },
  207. ],
  208. },
  209. },
  210. },
  211. },
  212. }),
  213. }),
  214. new Config.Document({
  215. type: "document",
  216. info: decode({
  217. providers: {
  218. custom: { name: "Renamed" },
  219. },
  220. }),
  221. }),
  222. ]),
  223. })
  224. yield* addPlugin(config)
  225. const provider = required(yield* catalog.provider.get(providerID))
  226. const model = required(yield* catalog.model.get(providerID, modelID))
  227. expect((yield* catalog.model.default())?.id).toBe(ModelV2.ID.make("default"))
  228. expect(provider.name).toBe("Renamed")
  229. expect((yield* integrations.get(Integration.ID.make("custom")))?.methods).toContainEqual({
  230. type: "env",
  231. names: ["CUSTOM_API_KEY"],
  232. })
  233. expect((yield* integrations.get(Integration.ID.make("custom")))?.name).toBe("Renamed")
  234. expect(provider.disabled).toBeUndefined()
  235. expect(provider.api).toEqual({ type: "aisdk", package: "custom-sdk", url: "https://example.test" })
  236. expect(provider.request.headers).toEqual({ first: "first", shared: "last", last: "last" })
  237. expect(model.api.id).toBe(ModelV2.ID.make("api-chat"))
  238. expect(model.name).toBe("Last")
  239. expect(model.capabilities).toEqual({ tools: true, input: ["text"], output: ["text"] })
  240. expect(model.enabled).toBe(false)
  241. expect(model.limit).toEqual({ context: 100, output: 75 })
  242. expect(model.cost).toEqual([{ input: 1, output: 2, cache: { read: 0, write: 0 }, tier: undefined }])
  243. expect(model.request.headers).toEqual({ first: "first", shared: "last", last: "last" })
  244. expect(model.request.variant).toBe("retained")
  245. expect(model.variants.map((variant) => variant.id)).toEqual([
  246. ModelV2.VariantID.make("fast"),
  247. ModelV2.VariantID.make("slow"),
  248. ])
  249. expect(model.variants[0]?.headers).toEqual({ first: "first", shared: "last", last: "last" })
  250. expect(model.variants[1]?.headers).toEqual({ slow: "slow" })
  251. }),
  252. ),
  253. )
  254. })