1
0

provider.test.ts 10 KB

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