provider.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import { describe, expect } from "bun:test"
  2. import { Money } from "@opencode-ai/schema/money"
  3. import { Document, Info, type Entry } from "@opencode-ai/schema/config"
  4. import { Effect, Schema, Stream } from "effect"
  5. import { Catalog } from "@opencode-ai/core/catalog"
  6. import { Config } from "@opencode-ai/core/config"
  7. import { ConfigProviderPlugin } from "@opencode-ai/core/config/plugin/provider"
  8. import { Integration } from "@opencode-ai/core/integration"
  9. import { Model } from "@opencode-ai/core/model"
  10. import { Plugin } from "@opencode-ai/core/plugin"
  11. import { PluginHost } from "@opencode-ai/core/plugin/host"
  12. import { Provider } from "@opencode-ai/core/provider"
  13. import { testEffect } from "../lib/effect"
  14. import { PluginTestLayer } from "../plugin/fixture"
  15. const it = testEffect(PluginTestLayer)
  16. const addPlugin = Effect.fn(function* (entries: Entry[]) {
  17. const plugin = yield* Plugin.Service
  18. const host = yield* PluginHost.make(plugin)
  19. yield* ConfigProviderPlugin.Plugin.effect(host).pipe(Effect.provide(Config.testLayer(entries)))
  20. })
  21. function required<T>(value: T | undefined): T {
  22. if (value === undefined) throw new Error("Expected value")
  23. return value
  24. }
  25. function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
  26. return Effect.acquireUseRelease(
  27. Effect.sync(() => {
  28. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  29. Object.entries(vars).forEach(([key, value]) => {
  30. if (value === undefined) delete process.env[key]
  31. else process.env[key] = value
  32. })
  33. return previous
  34. }),
  35. effect,
  36. (previous) =>
  37. Effect.sync(() =>
  38. Object.entries(previous).forEach(([key, value]) => {
  39. if (value === undefined) delete process.env[key]
  40. else process.env[key] = value
  41. }),
  42. ),
  43. )
  44. }
  45. const decode = Schema.decodeUnknownSync(Info)
  46. describe("ConfigProviderPlugin.Plugin", () => {
  47. it.effect("adds key auth for custom providers without env credentials", () =>
  48. Effect.gen(function* () {
  49. const integrations = yield* Integration.Service
  50. const entries = [
  51. new Document({
  52. type: "document",
  53. info: decode({
  54. providers: {
  55. litellm: {
  56. package: "aisdk:@ai-sdk/openai-compatible",
  57. models: { chat: {} },
  58. },
  59. },
  60. }),
  61. }),
  62. ]
  63. yield* addPlugin(entries)
  64. expect(yield* integrations.get(Integration.ID.make("litellm"))).toMatchObject({
  65. id: "litellm",
  66. name: "litellm",
  67. methods: [{ type: "key", label: "Manually enter API Key" }],
  68. })
  69. }),
  70. )
  71. it.effect("defaults custom models to agent capabilities", () =>
  72. Effect.gen(function* () {
  73. const catalog = yield* Catalog.Service
  74. const providerID = Provider.ID.make("custom")
  75. const modelID = Model.ID.make("chat")
  76. const entries = [
  77. new Document({
  78. type: "document",
  79. info: decode({
  80. providers: {
  81. custom: {
  82. package: "aisdk:@ai-sdk/openai-compatible",
  83. models: { chat: {} },
  84. },
  85. },
  86. }),
  87. }),
  88. ]
  89. yield* addPlugin(entries)
  90. const model = required(yield* catalog.model.get(providerID, modelID))
  91. expect(model.capabilities).toEqual({ tools: true, input: ["text", "image"], output: ["text"] })
  92. }),
  93. )
  94. it.effect("preserves catalog capabilities unless config overrides them", () =>
  95. Effect.gen(function* () {
  96. const catalog = yield* Catalog.Service
  97. const providerID = Provider.ID.make("custom")
  98. const inheritedID = Model.ID.make("inherited")
  99. const overriddenID = Model.ID.make("overridden")
  100. yield* catalog.transform((draft) => {
  101. draft.model.update(providerID, inheritedID, (model) => {
  102. model.capabilities = { tools: false, input: ["text"], output: ["text"] }
  103. })
  104. draft.model.update(providerID, overriddenID, (model) => {
  105. model.capabilities = { tools: false, input: ["text"], output: ["text"] }
  106. })
  107. })
  108. const entries = [
  109. new Document({
  110. type: "document",
  111. info: decode({
  112. providers: {
  113. custom: {
  114. package: "aisdk:@ai-sdk/openai-compatible",
  115. models: {
  116. inherited: { name: "Inherited" },
  117. overridden: {
  118. capabilities: { tools: true, input: ["text", "image"], output: ["text"] },
  119. },
  120. },
  121. },
  122. },
  123. }),
  124. }),
  125. ]
  126. yield* addPlugin(entries)
  127. expect((yield* catalog.model.get(providerID, inheritedID))?.capabilities).toEqual({
  128. tools: false,
  129. input: ["text"],
  130. output: ["text"],
  131. })
  132. expect((yield* catalog.model.get(providerID, overriddenID))?.capabilities).toEqual({
  133. tools: true,
  134. input: ["text", "image"],
  135. output: ["text"],
  136. })
  137. }),
  138. )
  139. it.effect("keeps configured model variant bodies unchanged", () =>
  140. Effect.gen(function* () {
  141. const catalog = yield* Catalog.Service
  142. const providerID = Provider.ID.opencode
  143. const modelID = Model.ID.make("alpha-gpt-next")
  144. const entries = [
  145. new Document({
  146. type: "document",
  147. info: decode({
  148. providers: {
  149. opencode: {
  150. package: "aisdk:@ai-sdk/openai",
  151. settings: { baseURL: "https://opencode.test/v1" },
  152. models: {
  153. "alpha-gpt-next": {
  154. variants: [
  155. {
  156. id: "high",
  157. body: {
  158. reasoningEffort: "high",
  159. reasoningSummary: "auto",
  160. include: ["reasoning.encrypted_content"],
  161. },
  162. },
  163. ],
  164. },
  165. },
  166. },
  167. },
  168. }),
  169. }),
  170. ]
  171. yield* addPlugin(entries)
  172. const model = required(yield* catalog.model.get(providerID, modelID))
  173. expect(model.variants).toMatchObject([
  174. {
  175. id: "high",
  176. body: {
  177. reasoningEffort: "high",
  178. reasoningSummary: "auto",
  179. include: ["reasoning.encrypted_content"],
  180. },
  181. },
  182. ])
  183. }),
  184. )
  185. it.effect("keeps layered model variant bodies unchanged", () =>
  186. Effect.gen(function* () {
  187. const catalog = yield* Catalog.Service
  188. const providerID = Provider.ID.opencode
  189. const modelID = Model.ID.make("alpha-gpt-next")
  190. const entries = [
  191. new Document({
  192. type: "document",
  193. info: decode({
  194. providers: {
  195. opencode: {
  196. package: "aisdk:@ai-sdk/openai",
  197. settings: { baseURL: "https://opencode.test/v1" },
  198. },
  199. },
  200. }),
  201. }),
  202. new Document({
  203. type: "document",
  204. info: decode({
  205. providers: {
  206. opencode: {
  207. models: {
  208. "alpha-gpt-next": {
  209. variants: [{ id: "high", body: { reasoningEffort: "high" } }],
  210. },
  211. },
  212. },
  213. },
  214. }),
  215. }),
  216. ]
  217. yield* addPlugin(entries)
  218. const model = required(yield* catalog.model.get(providerID, modelID))
  219. expect(model.variants?.[0]).toMatchObject({
  220. id: "high",
  221. body: { reasoningEffort: "high" },
  222. })
  223. }),
  224. )
  225. it.effect("loads configured providers and applies later model overrides", () =>
  226. withEnv({ CUSTOM_API_KEY: "secret" }, () =>
  227. Effect.gen(function* () {
  228. const catalog = yield* Catalog.Service
  229. const integrations = yield* Integration.Service
  230. const providerID = Provider.ID.make("custom")
  231. const modelID = Model.ID.make("chat")
  232. const entries = [
  233. new Document({
  234. type: "document",
  235. info: decode({
  236. model: "custom/first",
  237. providers: {
  238. custom: {
  239. name: "Configured",
  240. env: ["CUSTOM_API_KEY"],
  241. package: "native",
  242. headers: { first: "first", shared: "first" },
  243. models: {
  244. chat: {
  245. name: "First",
  246. compatibility: {
  247. reasoningField: "vendor_reasoning",
  248. maxTokensField: "max_completion_tokens",
  249. requireFinishReason: false,
  250. },
  251. capabilities: { tools: true, input: ["text"], output: ["text"] },
  252. disabled: true,
  253. limit: { context: 100, output: 50 },
  254. cost: { input: 1, output: 2 },
  255. settings: { retained: true },
  256. headers: { first: "first", shared: "first" },
  257. variants: [
  258. {
  259. id: "fast",
  260. headers: { first: "first", shared: "first" },
  261. },
  262. ],
  263. },
  264. },
  265. },
  266. },
  267. }),
  268. }),
  269. new Document({
  270. type: "document",
  271. info: decode({
  272. model: "custom/default",
  273. providers: {
  274. custom: {
  275. package: "aisdk:custom-sdk",
  276. settings: { baseURL: "https://example.test" },
  277. headers: { last: "last", shared: "last" },
  278. models: {
  279. default: {
  280. name: "Default",
  281. },
  282. chat: {
  283. modelID: "api-chat",
  284. name: "Last",
  285. limit: { output: 75 },
  286. headers: { last: "last", shared: "last" },
  287. variants: [
  288. {
  289. id: "fast",
  290. headers: { last: "last", shared: "last" },
  291. },
  292. {
  293. id: "slow",
  294. headers: { slow: "slow" },
  295. },
  296. ],
  297. },
  298. },
  299. },
  300. },
  301. }),
  302. }),
  303. new Document({
  304. type: "document",
  305. info: decode({
  306. providers: {
  307. custom: { name: "Renamed" },
  308. },
  309. }),
  310. }),
  311. ]
  312. yield* addPlugin(entries)
  313. const provider = required(yield* catalog.provider.get(providerID))
  314. const model = required(yield* catalog.model.get(providerID, modelID))
  315. expect((yield* catalog.model.default())?.id).toBe(Model.ID.make("default"))
  316. expect(provider.name).toBe("Renamed")
  317. expect((yield* integrations.get(Integration.ID.make("custom")))?.methods).toContainEqual({
  318. type: "env",
  319. names: ["CUSTOM_API_KEY"],
  320. })
  321. expect((yield* integrations.get(Integration.ID.make("custom")))?.name).toBe("Renamed")
  322. expect(provider.disabled).toBeUndefined()
  323. expect(provider.package).toBe("aisdk:custom-sdk")
  324. expect(provider.settings).toEqual({ baseURL: "https://example.test" })
  325. expect(provider.headers).toEqual({ first: "first", shared: "last", last: "last" })
  326. expect(model.id).toBe(modelID)
  327. expect(model.modelID).toBe(Model.ID.make("api-chat"))
  328. expect(model.name).toBe("Last")
  329. expect(model.compatibility).toEqual({
  330. reasoningField: "vendor_reasoning",
  331. maxTokensField: "max_completion_tokens",
  332. requireFinishReason: false,
  333. })
  334. expect(model.capabilities).toEqual({ tools: true, input: ["text"], output: ["text"] })
  335. expect(model.enabled).toBe(false)
  336. expect(model.limit).toEqual({ context: 100, output: 75 })
  337. expect(model.cost).toEqual([
  338. {
  339. input: Money.USDPerMillionTokens.make(1),
  340. output: Money.USDPerMillionTokens.make(2),
  341. cache: {
  342. read: Money.USDPerMillionTokens.zero,
  343. write: Money.USDPerMillionTokens.zero,
  344. },
  345. tier: undefined,
  346. },
  347. ])
  348. expect(model.settings).toEqual({ baseURL: "https://example.test", retained: true })
  349. expect(model.headers).toEqual({ first: "first", shared: "last", last: "last" })
  350. expect(model.variants?.map((variant) => variant.id)).toEqual([
  351. Model.VariantID.make("fast"),
  352. Model.VariantID.make("slow"),
  353. ])
  354. expect(model.variants?.[0]?.headers).toEqual({ first: "first", shared: "last", last: "last" })
  355. expect(model.variants?.[1]?.headers).toEqual({ slow: "slow" })
  356. }),
  357. ),
  358. )
  359. })