provider.test.ts 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. import { afterEach, expect, test } from "bun:test"
  2. import { mkdir, unlink } from "fs/promises"
  3. import path from "path"
  4. import { Effect, Layer } from "effect"
  5. import { ModelsDev } from "@opencode-ai/core/models-dev"
  6. import { AppFileSystem } from "@opencode-ai/core/filesystem"
  7. import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
  8. import { Global } from "@opencode-ai/core/global"
  9. import { disposeAllInstances, provideInstanceEffect, tmpdirScoped, TestInstance } from "../fixture/fixture"
  10. import { markPluginDependenciesReady } from "../fixture/plugin"
  11. import { Auth } from "@/auth"
  12. import { Config } from "@/config/config"
  13. import { Env } from "../../src/env"
  14. import { Plugin } from "../../src/plugin/index"
  15. import { Provider } from "@/provider/provider"
  16. import { ProviderID, ModelID } from "../../src/provider/schema"
  17. import { RuntimeFlags } from "@/effect/runtime-flags"
  18. import { Filesystem } from "@/util/filesystem"
  19. import { InstanceLayer } from "@/project/instance-layer"
  20. import { testEffect } from "../lib/effect"
  21. const originalEnv = new Map<string, string | undefined>()
  22. const rememberEnv = (k: string) => {
  23. if (!originalEnv.has(k)) originalEnv.set(k, process.env[k])
  24. }
  25. const setProcessEnv = (k: string, v: string) =>
  26. Effect.sync(() => {
  27. rememberEnv(k)
  28. process.env[k] = v
  29. })
  30. const set = (k: string, v: string) =>
  31. Effect.gen(function* () {
  32. rememberEnv(k)
  33. process.env[k] = v
  34. yield* Env.use.set(k, v)
  35. })
  36. const remove = (k: string) =>
  37. Effect.gen(function* () {
  38. rememberEnv(k)
  39. delete process.env[k]
  40. yield* Env.use.remove(k)
  41. })
  42. afterEach(async () => {
  43. for (const [key, value] of originalEnv) {
  44. if (value === undefined) delete process.env[key]
  45. else process.env[key] = value
  46. }
  47. originalEnv.clear()
  48. await disposeAllInstances()
  49. })
  50. const providerLayer = (flags: Partial<RuntimeFlags.Info> = {}) =>
  51. Provider.layer.pipe(
  52. Layer.provide(AppFileSystem.defaultLayer),
  53. Layer.provide(Env.defaultLayer),
  54. Layer.provide(Config.defaultLayer),
  55. Layer.provide(Auth.defaultLayer),
  56. Layer.provide(Plugin.defaultLayer),
  57. Layer.provide(ModelsDev.defaultLayer),
  58. Layer.provide(RuntimeFlags.layer(flags)),
  59. )
  60. const list = Provider.use.list()
  61. const paid = (providers: Record<string, { models: Record<string, { cost: { input: number } }> }>) => {
  62. const item = providers[ProviderID.make("opencode")]
  63. expect(item).toBeDefined()
  64. return Object.values(item.models).filter((model) => model.cost.input > 0).length
  65. }
  66. const languageBaseURL = (language: unknown) => (language as { config: { baseURL: string } }).config.baseURL
  67. const it = testEffect(Layer.mergeAll(Provider.defaultLayer, Env.defaultLayer, Plugin.defaultLayer))
  68. const experimentalModels = testEffect(providerLayer({ enableExperimentalModels: true }))
  69. const alphaProviderConfig = {
  70. provider: {
  71. "custom-provider": {
  72. name: "Custom Provider",
  73. npm: "@ai-sdk/openai-compatible",
  74. api: "https://api.custom.com/v1",
  75. models: {
  76. "active-model": {
  77. name: "Active Model",
  78. },
  79. "alpha-model": {
  80. name: "Alpha Model",
  81. status: "alpha" as const,
  82. },
  83. },
  84. options: {
  85. apiKey: "custom-key",
  86. },
  87. },
  88. },
  89. }
  90. it.instance("provider loaded from env variable", () =>
  91. Effect.gen(function* () {
  92. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  93. const providers = yield* list
  94. expect(providers[ProviderID.anthropic]).toBeDefined()
  95. // Provider should retain its connection source even if custom loaders
  96. // merge additional options.
  97. expect(providers[ProviderID.anthropic].source).toBe("env")
  98. expect(providers[ProviderID.anthropic].options.headers["anthropic-beta"]).toBeDefined()
  99. }),
  100. )
  101. it.instance(
  102. "provider loaded from config with apiKey option",
  103. Effect.gen(function* () {
  104. const providers = yield* list
  105. expect(providers[ProviderID.anthropic]).toBeDefined()
  106. }),
  107. { config: { provider: { anthropic: { options: { apiKey: "config-api-key" } } } } },
  108. )
  109. it.instance(
  110. "disabled_providers excludes provider",
  111. Effect.gen(function* () {
  112. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  113. const providers = yield* list
  114. expect(providers[ProviderID.anthropic]).toBeUndefined()
  115. }),
  116. { config: { disabled_providers: ["anthropic"] } },
  117. )
  118. it.instance(
  119. "enabled_providers restricts to only listed providers",
  120. Effect.gen(function* () {
  121. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  122. yield* setProcessEnv("OPENAI_API_KEY", "test-openai-key")
  123. const providers = yield* list
  124. expect(providers[ProviderID.anthropic]).toBeDefined()
  125. expect(providers[ProviderID.openai]).toBeUndefined()
  126. }),
  127. { config: { enabled_providers: ["anthropic"] } },
  128. )
  129. it.instance(
  130. "model whitelist filters models for provider",
  131. Effect.gen(function* () {
  132. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  133. const providers = yield* list
  134. expect(providers[ProviderID.anthropic]).toBeDefined()
  135. const models = Object.keys(providers[ProviderID.anthropic].models)
  136. expect(models).toContain("claude-sonnet-4-20250514")
  137. expect(models.length).toBe(1)
  138. }),
  139. { config: { provider: { anthropic: { whitelist: ["claude-sonnet-4-20250514"] } } } },
  140. )
  141. it.instance(
  142. "model blacklist excludes specific models",
  143. Effect.gen(function* () {
  144. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  145. const providers = yield* list
  146. expect(providers[ProviderID.anthropic]).toBeDefined()
  147. const models = Object.keys(providers[ProviderID.anthropic].models)
  148. expect(models).not.toContain("claude-sonnet-4-20250514")
  149. }),
  150. { config: { provider: { anthropic: { blacklist: ["claude-sonnet-4-20250514"] } } } },
  151. )
  152. it.instance(
  153. "custom model alias via config",
  154. Effect.gen(function* () {
  155. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  156. const providers = yield* list
  157. expect(providers[ProviderID.anthropic]).toBeDefined()
  158. expect(providers[ProviderID.anthropic].models["my-alias"]).toBeDefined()
  159. expect(providers[ProviderID.anthropic].models["my-alias"].name).toBe("My Custom Alias")
  160. }),
  161. {
  162. config: {
  163. provider: {
  164. anthropic: { models: { "my-alias": { id: "claude-sonnet-4-20250514", name: "My Custom Alias" } } },
  165. },
  166. },
  167. },
  168. )
  169. it.instance(
  170. "custom provider with npm package",
  171. Effect.gen(function* () {
  172. const providers = yield* list
  173. expect(providers[ProviderID.make("custom-provider")]).toBeDefined()
  174. expect(providers[ProviderID.make("custom-provider")].name).toBe("Custom Provider")
  175. expect(providers[ProviderID.make("custom-provider")].models["custom-model"]).toBeDefined()
  176. }),
  177. {
  178. config: {
  179. provider: {
  180. "custom-provider": {
  181. name: "Custom Provider",
  182. npm: "@ai-sdk/openai-compatible",
  183. api: "https://api.custom.com/v1",
  184. env: ["CUSTOM_API_KEY"],
  185. models: {
  186. "custom-model": {
  187. name: "Custom Model",
  188. tool_call: true,
  189. limit: { context: 128000, output: 4096 },
  190. },
  191. },
  192. options: { apiKey: "custom-key" },
  193. },
  194. },
  195. },
  196. },
  197. )
  198. it.instance(
  199. "filters alpha provider models by default",
  200. Effect.gen(function* () {
  201. const providers = yield* list
  202. expect(providers[ProviderID.make("custom-provider")].models["active-model"]).toBeDefined()
  203. expect(providers[ProviderID.make("custom-provider")].models["alpha-model"]).toBeUndefined()
  204. }),
  205. { config: alphaProviderConfig },
  206. )
  207. experimentalModels.instance(
  208. "includes alpha provider models when experimental models are enabled",
  209. Effect.gen(function* () {
  210. const providers = yield* list
  211. expect(providers[ProviderID.make("custom-provider")].models["active-model"]).toBeDefined()
  212. expect(providers[ProviderID.make("custom-provider")].models["alpha-model"]).toBeDefined()
  213. }),
  214. { config: alphaProviderConfig },
  215. )
  216. it.instance(
  217. "custom DeepSeek openai-compatible model defaults interleaved reasoning field",
  218. Effect.gen(function* () {
  219. const providers = yield* list
  220. const provider = providers[ProviderID.make("custom-provider")]
  221. expect(provider.models["deepseek-r1"].capabilities.interleaved).toEqual({ field: "reasoning_content" })
  222. expect(provider.models["deepseek-details"].capabilities.interleaved).toEqual({ field: "reasoning_details" })
  223. expect(provider.models["custom-model"].capabilities.interleaved).toBe(false)
  224. expect(providers[ProviderID.make("custom-anthropic-provider")].models["deepseek-r1"].capabilities.interleaved).toBe(
  225. false,
  226. )
  227. }),
  228. {
  229. config: {
  230. provider: {
  231. "custom-provider": {
  232. name: "Custom Provider",
  233. npm: "@ai-sdk/openai-compatible",
  234. api: "https://api.custom.com/v1",
  235. models: {
  236. "deepseek-r1": { name: "DeepSeek R1" },
  237. "deepseek-details": { name: "DeepSeek Details", interleaved: { field: "reasoning_details" } },
  238. "custom-model": { name: "Custom Model" },
  239. },
  240. options: { apiKey: "custom-key" },
  241. },
  242. "custom-anthropic-provider": {
  243. name: "Custom Anthropic Provider",
  244. npm: "@ai-sdk/anthropic",
  245. api: "https://api.custom.com/v1",
  246. models: { "deepseek-r1": { name: "DeepSeek R1" } },
  247. options: { apiKey: "custom-key" },
  248. },
  249. },
  250. },
  251. },
  252. )
  253. it.instance(
  254. "env variable takes precedence, config merges options",
  255. Effect.gen(function* () {
  256. yield* setProcessEnv("ANTHROPIC_API_KEY", "env-api-key")
  257. const providers = yield* list
  258. expect(providers[ProviderID.anthropic]).toBeDefined()
  259. // Config options should be merged
  260. expect(providers[ProviderID.anthropic].options.timeout).toBe(60000)
  261. expect(providers[ProviderID.anthropic].options.headerTimeout).toBe(10000)
  262. expect(providers[ProviderID.anthropic].options.chunkTimeout).toBe(15000)
  263. }),
  264. { config: { provider: { anthropic: { options: { timeout: 60000, headerTimeout: 10000, chunkTimeout: 15000 } } } } },
  265. )
  266. it.instance("getModel returns model for valid provider/model", () =>
  267. Effect.gen(function* () {
  268. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  269. const provider = yield* Provider.Service
  270. const model = yield* provider.getModel(ProviderID.anthropic, ModelID.make("claude-sonnet-4-20250514"))
  271. expect(model).toBeDefined()
  272. expect(String(model.providerID)).toBe("anthropic")
  273. expect(String(model.id)).toBe("claude-sonnet-4-20250514")
  274. const language = yield* provider.getLanguage(model)
  275. expect(language).toBeDefined()
  276. }),
  277. )
  278. it.instance("getModel throws ModelNotFoundError for invalid model", () =>
  279. Effect.gen(function* () {
  280. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  281. const exit = yield* Provider.use.getModel(ProviderID.anthropic, ModelID.make("nonexistent-model")).pipe(Effect.exit)
  282. expect(exit._tag).toBe("Failure")
  283. }),
  284. )
  285. it.instance("getModel throws ModelNotFoundError for invalid provider", () =>
  286. Effect.gen(function* () {
  287. const exit = yield* Provider.use
  288. .getModel(ProviderID.make("nonexistent-provider"), ModelID.make("some-model"))
  289. .pipe(Effect.exit)
  290. expect(exit._tag).toBe("Failure")
  291. }),
  292. )
  293. // Pure synchronous unit tests — no Effect runtime needed.
  294. test("parseModel correctly parses provider/model string", () => {
  295. const result = Provider.parseModel("anthropic/claude-sonnet-4")
  296. expect(String(result.providerID)).toBe("anthropic")
  297. expect(String(result.modelID)).toBe("claude-sonnet-4")
  298. })
  299. test("parseModel handles model IDs with slashes", () => {
  300. const result = Provider.parseModel("openrouter/anthropic/claude-3-opus")
  301. expect(String(result.providerID)).toBe("openrouter")
  302. expect(String(result.modelID)).toBe("anthropic/claude-3-opus")
  303. })
  304. it.instance("defaultModel returns first available model when no config set", () =>
  305. Effect.gen(function* () {
  306. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  307. const model = yield* Provider.use.defaultModel()
  308. expect(model.providerID).toBeDefined()
  309. expect(model.modelID).toBeDefined()
  310. }),
  311. )
  312. it.instance(
  313. "defaultModel respects config model setting",
  314. Effect.gen(function* () {
  315. yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key")
  316. const model = yield* Provider.use.defaultModel()
  317. expect(String(model.providerID)).toBe("anthropic")
  318. expect(String(model.modelID)).toBe("claude-sonnet-4-20250514")
  319. }),
  320. { config: { model: "anthropic/claude-sonnet-4-20250514" } },
  321. )
  322. it.instance(
  323. "defaultModel returns a typed error when config excludes every provider",
  324. Effect.gen(function* () {
  325. const error = yield* Provider.use.defaultModel().pipe(Effect.flip)
  326. expect(error).toBeInstanceOf(Provider.NoProvidersError)
  327. expect(error._tag).toBe("ProviderNoProvidersError")
  328. }),
  329. { config: { enabled_providers: [] } },
  330. )
  331. it.instance(
  332. "provider with baseURL from config",
  333. Effect.gen(function* () {
  334. const providers = yield* list
  335. expect(providers[ProviderID.make("custom-openai")]).toBeDefined()
  336. expect(providers[ProviderID.make("custom-openai")].options.baseURL).toBe("https://custom.openai.com/v1")
  337. }),
  338. {
  339. config: {
  340. provider: {
  341. "custom-openai": {
  342. name: "Custom OpenAI",
  343. npm: "@ai-sdk/openai-compatible",
  344. env: [],
  345. models: { "gpt-4": { name: "GPT-4", tool_call: true, limit: { context: 128000, output: 4096 } } },
  346. options: { apiKey: "test-key", baseURL: "https://custom.openai.com/v1" },
  347. },
  348. },
  349. },
  350. },
  351. )
  352. it.instance(
  353. "model cost defaults to zero when not specified",
  354. Effect.gen(function* () {
  355. const providers = yield* list
  356. const model = providers[ProviderID.make("test-provider")].models["test-model"]
  357. expect(model.cost.input).toBe(0)
  358. expect(model.cost.output).toBe(0)
  359. expect(model.cost.cache.read).toBe(0)
  360. expect(model.cost.cache.write).toBe(0)
  361. }),
  362. {
  363. config: {
  364. provider: {
  365. "test-provider": {
  366. name: "Test Provider",
  367. npm: "@ai-sdk/openai-compatible",
  368. env: [],
  369. models: { "test-model": { name: "Test Model", tool_call: true, limit: { context: 128000, output: 4096 } } },
  370. options: { apiKey: "test-key" },
  371. },
  372. },
  373. },
  374. },
  375. )
  376. it.instance(
  377. "model options are merged from existing model",
  378. Effect.gen(function* () {
  379. const providers = yield* list
  380. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  381. expect(model.options.customOption).toBe("custom-value")
  382. }),
  383. {
  384. config: {
  385. provider: {
  386. anthropic: {
  387. options: { apiKey: "test-api-key" },
  388. models: { "claude-sonnet-4-20250514": { options: { customOption: "custom-value" } } },
  389. },
  390. },
  391. },
  392. },
  393. )
  394. it.instance(
  395. "provider removed when all models filtered out",
  396. Effect.gen(function* () {
  397. const providers = yield* list
  398. expect(providers[ProviderID.anthropic]).toBeUndefined()
  399. }),
  400. { config: { provider: { anthropic: { options: { apiKey: "test-api-key" }, whitelist: ["nonexistent-model"] } } } },
  401. )
  402. it.instance("closest finds model by partial match", () =>
  403. Effect.gen(function* () {
  404. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  405. const result = yield* Provider.use.closest(ProviderID.anthropic, ["sonnet-4"])
  406. expect(result).toBeDefined()
  407. expect(String(result?.providerID)).toBe("anthropic")
  408. expect(String(result?.modelID)).toContain("sonnet-4")
  409. }),
  410. )
  411. it.instance("closest returns undefined for nonexistent provider", () =>
  412. Effect.gen(function* () {
  413. const result = yield* Provider.use.closest(ProviderID.make("nonexistent"), ["model"])
  414. expect(result).toBeUndefined()
  415. }),
  416. )
  417. it.instance(
  418. "getModel uses realIdByKey for aliased models",
  419. Effect.gen(function* () {
  420. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  421. const providers = yield* list
  422. expect(providers[ProviderID.anthropic].models["my-sonnet"]).toBeDefined()
  423. const model = yield* Provider.use.getModel(ProviderID.anthropic, ModelID.make("my-sonnet"))
  424. expect(model).toBeDefined()
  425. expect(String(model.id)).toBe("my-sonnet")
  426. expect(model.name).toBe("My Sonnet Alias")
  427. }),
  428. {
  429. config: {
  430. provider: {
  431. anthropic: {
  432. models: { "my-sonnet": { id: "claude-sonnet-4-20250514", name: "My Sonnet Alias" } },
  433. },
  434. },
  435. },
  436. },
  437. )
  438. it.instance(
  439. "provider api field sets model api.url",
  440. Effect.gen(function* () {
  441. const providers = yield* list
  442. // api field is stored on model.api.url, used by getSDK to set baseURL
  443. expect(providers[ProviderID.make("custom-api")].models["model-1"].api.url).toBe("https://api.example.com/v1")
  444. }),
  445. {
  446. config: {
  447. provider: {
  448. "custom-api": {
  449. name: "Custom API",
  450. npm: "@ai-sdk/openai-compatible",
  451. api: "https://api.example.com/v1",
  452. env: [],
  453. models: { "model-1": { name: "Model 1", tool_call: true, limit: { context: 8000, output: 2000 } } },
  454. options: { apiKey: "test-key" },
  455. },
  456. },
  457. },
  458. },
  459. )
  460. it.instance(
  461. "explicit baseURL overrides api field",
  462. Effect.gen(function* () {
  463. const providers = yield* list
  464. expect(providers[ProviderID.make("custom-api")].options.baseURL).toBe("https://custom.override.com/v1")
  465. }),
  466. {
  467. config: {
  468. provider: {
  469. "custom-api": {
  470. name: "Custom API",
  471. npm: "@ai-sdk/openai-compatible",
  472. api: "https://api.example.com/v1",
  473. env: [],
  474. models: { "model-1": { name: "Model 1", tool_call: true, limit: { context: 8000, output: 2000 } } },
  475. options: { apiKey: "test-key", baseURL: "https://custom.override.com/v1" },
  476. },
  477. },
  478. },
  479. },
  480. )
  481. it.instance(
  482. "model inherits properties from existing database model",
  483. Effect.gen(function* () {
  484. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  485. const providers = yield* list
  486. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  487. expect(model.name).toBe("Custom Name for Sonnet")
  488. expect(model.capabilities.toolcall).toBe(true)
  489. expect(model.capabilities.attachment).toBe(true)
  490. expect(model.limit.context).toBeGreaterThan(0)
  491. }),
  492. {
  493. config: {
  494. provider: { anthropic: { models: { "claude-sonnet-4-20250514": { name: "Custom Name for Sonnet" } } } },
  495. },
  496. },
  497. )
  498. it.instance(
  499. "disabled_providers prevents loading even with env var",
  500. Effect.gen(function* () {
  501. yield* set("OPENAI_API_KEY", "test-openai-key")
  502. const providers = yield* list
  503. expect(providers[ProviderID.openai]).toBeUndefined()
  504. }),
  505. { config: { disabled_providers: ["openai"] } },
  506. )
  507. it.instance(
  508. "enabled_providers with empty array allows no providers",
  509. Effect.gen(function* () {
  510. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  511. yield* set("OPENAI_API_KEY", "test-openai-key")
  512. const providers = yield* list
  513. expect(Object.keys(providers).length).toBe(0)
  514. }),
  515. { config: { enabled_providers: [] } },
  516. )
  517. it.instance(
  518. "whitelist and blacklist can be combined",
  519. Effect.gen(function* () {
  520. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  521. const providers = yield* list
  522. expect(providers[ProviderID.anthropic]).toBeDefined()
  523. const models = Object.keys(providers[ProviderID.anthropic].models)
  524. expect(models).toContain("claude-sonnet-4-20250514")
  525. expect(models).not.toContain("claude-opus-4-20250514")
  526. expect(models.length).toBe(1)
  527. }),
  528. {
  529. config: {
  530. provider: {
  531. anthropic: {
  532. whitelist: ["claude-sonnet-4-20250514", "claude-opus-4-20250514"],
  533. blacklist: ["claude-opus-4-20250514"],
  534. },
  535. },
  536. },
  537. },
  538. )
  539. it.instance(
  540. "model modalities default correctly",
  541. Effect.gen(function* () {
  542. const providers = yield* list
  543. const model = providers[ProviderID.make("test-provider")].models["test-model"]
  544. expect(model.capabilities.input.text).toBe(true)
  545. expect(model.capabilities.output.text).toBe(true)
  546. }),
  547. {
  548. config: {
  549. provider: {
  550. "test-provider": {
  551. name: "Test",
  552. npm: "@ai-sdk/openai-compatible",
  553. env: [],
  554. models: { "test-model": { name: "Test Model", tool_call: true, limit: { context: 8000, output: 2000 } } },
  555. options: { apiKey: "test" },
  556. },
  557. },
  558. },
  559. },
  560. )
  561. it.instance(
  562. "model with custom cost values",
  563. Effect.gen(function* () {
  564. const providers = yield* list
  565. const model = providers[ProviderID.make("test-provider")].models["test-model"]
  566. expect(model.cost.input).toBe(5)
  567. expect(model.cost.output).toBe(15)
  568. expect(model.cost.cache.read).toBe(2.5)
  569. expect(model.cost.cache.write).toBe(7.5)
  570. }),
  571. {
  572. config: {
  573. provider: {
  574. "test-provider": {
  575. name: "Test",
  576. npm: "@ai-sdk/openai-compatible",
  577. env: [],
  578. models: {
  579. "test-model": {
  580. name: "Test Model",
  581. tool_call: true,
  582. limit: { context: 8000, output: 2000 },
  583. cost: { input: 5, output: 15, cache_read: 2.5, cache_write: 7.5 },
  584. },
  585. },
  586. options: { apiKey: "test" },
  587. },
  588. },
  589. },
  590. },
  591. )
  592. it.instance("getSmallModel returns appropriate small model", () =>
  593. Effect.gen(function* () {
  594. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  595. const model = yield* Provider.use.getSmallModel(ProviderID.anthropic)
  596. expect(model).toBeDefined()
  597. expect(model?.id).toContain("haiku")
  598. }),
  599. )
  600. it.instance(
  601. "getSmallModel respects config small_model override",
  602. Effect.gen(function* () {
  603. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  604. const model = yield* Provider.use.getSmallModel(ProviderID.anthropic)
  605. expect(model).toBeDefined()
  606. expect(String(model?.providerID)).toBe("anthropic")
  607. expect(String(model?.id)).toBe("claude-sonnet-4-20250514")
  608. }),
  609. { config: { small_model: "anthropic/claude-sonnet-4-20250514" } },
  610. )
  611. it.instance(
  612. "getSmallModel ignores invalid config small_model",
  613. Effect.gen(function* () {
  614. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  615. const model = yield* Provider.use.getSmallModel(ProviderID.anthropic)
  616. expect(model).toBeUndefined()
  617. }),
  618. { config: { small_model: "anthropic/not-a-real-model" } },
  619. )
  620. test("provider.sort prioritizes preferred models", () => {
  621. const models = [
  622. { id: "random-model", name: "Random" },
  623. { id: "claude-sonnet-4-latest", name: "Claude Sonnet 4" },
  624. { id: "gpt-5-turbo", name: "GPT-5 Turbo" },
  625. { id: "other-model", name: "Other" },
  626. ] as any[]
  627. const sorted = Provider.sort(models)
  628. expect(sorted[0].id).toContain("sonnet-4")
  629. expect(sorted[0].id).toContain("latest")
  630. expect(sorted[sorted.length - 1].id).not.toContain("gpt-5")
  631. expect(sorted[sorted.length - 1].id).not.toContain("sonnet-4")
  632. })
  633. it.instance(
  634. "multiple providers can be configured simultaneously",
  635. Effect.gen(function* () {
  636. yield* set("ANTHROPIC_API_KEY", "test-anthropic-key")
  637. yield* set("OPENAI_API_KEY", "test-openai-key")
  638. const providers = yield* list
  639. expect(providers[ProviderID.anthropic]).toBeDefined()
  640. expect(providers[ProviderID.openai]).toBeDefined()
  641. expect(providers[ProviderID.anthropic].options.timeout).toBe(30000)
  642. expect(providers[ProviderID.openai].options.timeout).toBe(60000)
  643. }),
  644. {
  645. config: {
  646. provider: {
  647. anthropic: { options: { timeout: 30000 } },
  648. openai: { options: { timeout: 60000 } },
  649. },
  650. },
  651. },
  652. )
  653. it.instance(
  654. "provider with custom npm package",
  655. Effect.gen(function* () {
  656. const providers = yield* list
  657. expect(providers[ProviderID.make("local-llm")]).toBeDefined()
  658. expect(providers[ProviderID.make("local-llm")].models["llama-3"].api.npm).toBe("@ai-sdk/openai-compatible")
  659. expect(providers[ProviderID.make("local-llm")].options.baseURL).toBe("http://localhost:11434/v1")
  660. }),
  661. {
  662. config: {
  663. provider: {
  664. "local-llm": {
  665. name: "Local LLM",
  666. npm: "@ai-sdk/openai-compatible",
  667. env: [],
  668. models: { "llama-3": { name: "Llama 3", tool_call: true, limit: { context: 8192, output: 2048 } } },
  669. options: { apiKey: "not-needed", baseURL: "http://localhost:11434/v1" },
  670. },
  671. },
  672. },
  673. },
  674. )
  675. // Edge cases for model configuration
  676. it.instance(
  677. "model alias name defaults to alias key when id differs",
  678. Effect.gen(function* () {
  679. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  680. const providers = yield* list
  681. expect(providers[ProviderID.anthropic].models["sonnet"].name).toBe("sonnet")
  682. }),
  683. {
  684. config: {
  685. provider: {
  686. anthropic: {
  687. models: { sonnet: { id: "claude-sonnet-4-20250514" } },
  688. },
  689. },
  690. },
  691. },
  692. )
  693. it.instance(
  694. "provider with multiple env var options only includes apiKey when single env",
  695. Effect.gen(function* () {
  696. yield* set("MULTI_ENV_KEY_1", "test-key")
  697. const providers = yield* list
  698. expect(providers[ProviderID.make("multi-env")]).toBeDefined()
  699. // When multiple env options exist, key should NOT be auto-set
  700. expect(providers[ProviderID.make("multi-env")].key).toBeUndefined()
  701. }),
  702. {
  703. config: {
  704. provider: {
  705. "multi-env": {
  706. name: "Multi Env Provider",
  707. npm: "@ai-sdk/openai-compatible",
  708. env: ["MULTI_ENV_KEY_1", "MULTI_ENV_KEY_2"],
  709. models: { "model-1": { name: "Model 1", tool_call: true, limit: { context: 8000, output: 2000 } } },
  710. options: { baseURL: "https://api.example.com/v1" },
  711. },
  712. },
  713. },
  714. },
  715. )
  716. it.instance(
  717. "provider with single env var includes apiKey automatically",
  718. Effect.gen(function* () {
  719. yield* set("SINGLE_ENV_KEY", "my-api-key")
  720. const providers = yield* list
  721. expect(providers[ProviderID.make("single-env")]).toBeDefined()
  722. // Single env option should auto-set key
  723. expect(providers[ProviderID.make("single-env")].key).toBe("my-api-key")
  724. }),
  725. {
  726. config: {
  727. provider: {
  728. "single-env": {
  729. name: "Single Env Provider",
  730. npm: "@ai-sdk/openai-compatible",
  731. env: ["SINGLE_ENV_KEY"],
  732. models: { "model-1": { name: "Model 1", tool_call: true, limit: { context: 8000, output: 2000 } } },
  733. options: { baseURL: "https://api.example.com/v1" },
  734. },
  735. },
  736. },
  737. },
  738. )
  739. it.instance(
  740. "model cost overrides existing cost values",
  741. Effect.gen(function* () {
  742. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  743. const providers = yield* list
  744. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  745. expect(model.cost.input).toBe(999)
  746. expect(model.cost.output).toBe(888)
  747. }),
  748. {
  749. config: {
  750. provider: {
  751. anthropic: {
  752. models: { "claude-sonnet-4-20250514": { cost: { input: 999, output: 888 } } },
  753. },
  754. },
  755. },
  756. },
  757. )
  758. it.instance(
  759. "completely new provider not in database can be configured",
  760. Effect.gen(function* () {
  761. const providers = yield* list
  762. expect(providers[ProviderID.make("brand-new-provider")]).toBeDefined()
  763. expect(providers[ProviderID.make("brand-new-provider")].name).toBe("Brand New")
  764. const model = providers[ProviderID.make("brand-new-provider")].models["new-model"]
  765. expect(model.capabilities.reasoning).toBe(true)
  766. expect(model.capabilities.attachment).toBe(true)
  767. expect(model.capabilities.input.image).toBe(true)
  768. }),
  769. {
  770. config: {
  771. provider: {
  772. "brand-new-provider": {
  773. name: "Brand New",
  774. npm: "@ai-sdk/openai-compatible",
  775. env: [],
  776. api: "https://new-api.com/v1",
  777. models: {
  778. "new-model": {
  779. name: "New Model",
  780. tool_call: true,
  781. reasoning: true,
  782. attachment: true,
  783. temperature: true,
  784. limit: { context: 32000, output: 8000 },
  785. modalities: { input: ["text", "image"], output: ["text"] },
  786. },
  787. },
  788. options: { apiKey: "new-key" },
  789. },
  790. },
  791. },
  792. },
  793. )
  794. it.instance(
  795. "disabled_providers and enabled_providers interaction",
  796. Effect.gen(function* () {
  797. yield* set("ANTHROPIC_API_KEY", "test-anthropic")
  798. yield* set("OPENAI_API_KEY", "test-openai")
  799. yield* set("GOOGLE_GENERATIVE_AI_API_KEY", "test-google")
  800. const providers = yield* list
  801. // anthropic: in enabled, not in disabled = allowed
  802. expect(providers[ProviderID.anthropic]).toBeDefined()
  803. // openai: in enabled, but also in disabled = NOT allowed
  804. expect(providers[ProviderID.openai]).toBeUndefined()
  805. // google: not in enabled = NOT allowed (even though not disabled)
  806. expect(providers[ProviderID.google]).toBeUndefined()
  807. }),
  808. {
  809. // enabled_providers takes precedence — only these are considered
  810. // Then disabled_providers filters from the enabled set
  811. config: { enabled_providers: ["anthropic", "openai"], disabled_providers: ["openai"] },
  812. },
  813. )
  814. it.instance(
  815. "model with tool_call false",
  816. Effect.gen(function* () {
  817. const providers = yield* list
  818. expect(providers[ProviderID.make("no-tools")].models["basic-model"].capabilities.toolcall).toBe(false)
  819. }),
  820. {
  821. config: {
  822. provider: {
  823. "no-tools": {
  824. name: "No Tools Provider",
  825. npm: "@ai-sdk/openai-compatible",
  826. env: [],
  827. models: { "basic-model": { name: "Basic Model", tool_call: false, limit: { context: 4000, output: 1000 } } },
  828. options: { apiKey: "test" },
  829. },
  830. },
  831. },
  832. },
  833. )
  834. it.instance(
  835. "model defaults tool_call to true when not specified",
  836. Effect.gen(function* () {
  837. const providers = yield* list
  838. expect(providers[ProviderID.make("default-tools")].models["model"].capabilities.toolcall).toBe(true)
  839. }),
  840. {
  841. config: {
  842. provider: {
  843. "default-tools": {
  844. name: "Default Tools Provider",
  845. npm: "@ai-sdk/openai-compatible",
  846. env: [],
  847. models: { model: { name: "Model", limit: { context: 4000, output: 1000 } } },
  848. options: { apiKey: "test" },
  849. },
  850. },
  851. },
  852. },
  853. )
  854. it.instance(
  855. "model headers are preserved",
  856. Effect.gen(function* () {
  857. const providers = yield* list
  858. const model = providers[ProviderID.make("headers-provider")].models["model"]
  859. expect(model.headers).toEqual({
  860. "X-Custom-Header": "custom-value",
  861. Authorization: "Bearer special-token",
  862. })
  863. }),
  864. {
  865. config: {
  866. provider: {
  867. "headers-provider": {
  868. name: "Headers Provider",
  869. npm: "@ai-sdk/openai-compatible",
  870. env: [],
  871. models: {
  872. model: {
  873. name: "Model",
  874. tool_call: true,
  875. limit: { context: 4000, output: 1000 },
  876. headers: { "X-Custom-Header": "custom-value", Authorization: "Bearer special-token" },
  877. },
  878. },
  879. options: { apiKey: "test" },
  880. },
  881. },
  882. },
  883. },
  884. )
  885. it.instance(
  886. "provider env fallback - second env var used if first missing",
  887. Effect.gen(function* () {
  888. // Only set fallback, not primary
  889. yield* set("FALLBACK_KEY", "fallback-api-key")
  890. const providers = yield* list
  891. // Provider should load because fallback env var is set
  892. expect(providers[ProviderID.make("fallback-env")]).toBeDefined()
  893. }),
  894. {
  895. config: {
  896. provider: {
  897. "fallback-env": {
  898. name: "Fallback Env Provider",
  899. npm: "@ai-sdk/openai-compatible",
  900. env: ["PRIMARY_KEY", "FALLBACK_KEY"],
  901. models: { model: { name: "Model", tool_call: true, limit: { context: 4000, output: 1000 } } },
  902. options: { baseURL: "https://api.example.com" },
  903. },
  904. },
  905. },
  906. },
  907. )
  908. it.instance("getModel returns consistent results", () =>
  909. Effect.gen(function* () {
  910. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  911. const model1 = yield* Provider.use.getModel(ProviderID.anthropic, ModelID.make("claude-sonnet-4-20250514"))
  912. const model2 = yield* Provider.use.getModel(ProviderID.anthropic, ModelID.make("claude-sonnet-4-20250514"))
  913. expect(model1.providerID).toEqual(model2.providerID)
  914. expect(model1.id).toEqual(model2.id)
  915. expect(model1).toEqual(model2)
  916. }),
  917. )
  918. it.instance(
  919. "provider name defaults to id when not in database",
  920. Effect.gen(function* () {
  921. const providers = yield* list
  922. expect(providers[ProviderID.make("my-custom-id")].name).toBe("my-custom-id")
  923. }),
  924. {
  925. config: {
  926. provider: {
  927. "my-custom-id": {
  928. npm: "@ai-sdk/openai-compatible",
  929. env: [],
  930. models: { model: { name: "Model", tool_call: true, limit: { context: 4000, output: 1000 } } },
  931. options: { apiKey: "test" },
  932. },
  933. },
  934. },
  935. },
  936. )
  937. it.instance("ModelNotFoundError includes suggestions for typos", () =>
  938. Effect.gen(function* () {
  939. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  940. const error = yield* Provider.use.getModel(ProviderID.anthropic, ModelID.make("claude-sonet-4")).pipe(Effect.flip)
  941. expect(error.suggestions).toBeDefined()
  942. expect((error.suggestions ?? []).length).toBeGreaterThan(0)
  943. }),
  944. )
  945. it.instance("ModelNotFoundError for provider includes suggestions", () =>
  946. Effect.gen(function* () {
  947. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  948. const error = yield* Provider.use
  949. .getModel(ProviderID.make("antropic"), ModelID.make("claude-sonnet-4"))
  950. .pipe(Effect.flip)
  951. expect(error.suggestions).toBeDefined()
  952. expect(error.suggestions).toContain("anthropic")
  953. }),
  954. )
  955. it.instance("ModelNotFoundError suggests catalog models for unloaded providers", () =>
  956. Effect.gen(function* () {
  957. yield* remove("OPENCODE_API_KEY")
  958. const error = yield* Provider.use
  959. .getModel(ProviderID.opencode, ModelID.make("claude-haiku-fake-model"))
  960. .pipe(Effect.flip)
  961. if (!Provider.ModelNotFoundError.isInstance(error)) throw error
  962. expect(error.suggestions ?? []).toContain("claude-haiku-4-5")
  963. }),
  964. )
  965. it.instance("getProvider returns undefined for nonexistent provider", () =>
  966. Effect.gen(function* () {
  967. const provider = yield* Provider.Service.use((svc) => svc.getProvider(ProviderID.make("nonexistent")))
  968. expect(provider).toBeUndefined()
  969. }),
  970. )
  971. it.instance("getProvider returns provider info", () =>
  972. Effect.gen(function* () {
  973. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  974. const provider = yield* Provider.use.getProvider(ProviderID.anthropic)
  975. expect(provider).toBeDefined()
  976. expect(String(provider?.id)).toBe("anthropic")
  977. }),
  978. )
  979. it.instance("closest returns undefined when no partial match found", () =>
  980. Effect.gen(function* () {
  981. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  982. const result = yield* Provider.use.closest(ProviderID.anthropic, ["nonexistent-xyz-model"])
  983. expect(result).toBeUndefined()
  984. }),
  985. )
  986. it.instance("closest checks multiple query terms in order", () =>
  987. Effect.gen(function* () {
  988. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  989. // First term won't match, second will
  990. const result = yield* Provider.use.closest(ProviderID.anthropic, ["nonexistent", "haiku"])
  991. expect(result).toBeDefined()
  992. expect(result?.modelID).toContain("haiku")
  993. }),
  994. )
  995. it.instance(
  996. "model limit defaults to zero when not specified",
  997. Effect.gen(function* () {
  998. const providers = yield* list
  999. const model = providers[ProviderID.make("no-limit")].models["model"]
  1000. expect(model.limit.context).toBe(0)
  1001. expect(model.limit.output).toBe(0)
  1002. }),
  1003. {
  1004. config: {
  1005. provider: {
  1006. "no-limit": {
  1007. name: "No Limit Provider",
  1008. npm: "@ai-sdk/openai-compatible",
  1009. env: [],
  1010. models: { model: { name: "Model", tool_call: true } },
  1011. options: { apiKey: "test" },
  1012. },
  1013. },
  1014. },
  1015. },
  1016. )
  1017. it.instance(
  1018. "provider options are deeply merged",
  1019. Effect.gen(function* () {
  1020. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1021. const providers = yield* list
  1022. // Custom options should be merged
  1023. expect(providers[ProviderID.anthropic].options.timeout).toBe(30000)
  1024. expect(providers[ProviderID.anthropic].options.headers["X-Custom"]).toBe("custom-value")
  1025. // anthropic custom loader adds its own headers, they should coexist
  1026. expect(providers[ProviderID.anthropic].options.headers["anthropic-beta"]).toBeDefined()
  1027. }),
  1028. {
  1029. config: {
  1030. provider: { anthropic: { options: { headers: { "X-Custom": "custom-value" }, timeout: 30000 } } },
  1031. },
  1032. },
  1033. )
  1034. it.instance(
  1035. "hosted nvidia provider adds billing origin header",
  1036. Effect.gen(function* () {
  1037. const providers = yield* list
  1038. expect(providers[ProviderID.make("nvidia")].options.headers).toEqual({
  1039. "HTTP-Referer": "https://opencode.ai/",
  1040. "X-Title": "opencode",
  1041. "X-BILLING-INVOKE-ORIGIN": "OpenCode",
  1042. })
  1043. }),
  1044. { config: { provider: { nvidia: { options: { apiKey: "test-api-key" } } } } },
  1045. )
  1046. it.instance(
  1047. "custom nvidia baseURL adds billing origin header",
  1048. Effect.gen(function* () {
  1049. const providers = yield* list
  1050. expect(providers[ProviderID.make("nvidia")].options.headers).toEqual({
  1051. "HTTP-Referer": "https://opencode.ai/",
  1052. "X-Title": "opencode",
  1053. "X-BILLING-INVOKE-ORIGIN": "OpenCode",
  1054. })
  1055. }),
  1056. { config: { provider: { nvidia: { options: { apiKey: "test-api-key", baseURL: "http://localhost:8000/v1" } } } } },
  1057. )
  1058. it.instance(
  1059. "explicit nvidia billing origin header is preserved",
  1060. Effect.gen(function* () {
  1061. const providers = yield* list
  1062. expect(providers[ProviderID.make("nvidia")].options.headers["X-BILLING-INVOKE-ORIGIN"]).toBe("CustomOrigin")
  1063. }),
  1064. {
  1065. config: {
  1066. provider: {
  1067. nvidia: {
  1068. options: {
  1069. apiKey: "test-api-key",
  1070. baseURL: "http://localhost:8000/v1",
  1071. headers: { "X-BILLING-INVOKE-ORIGIN": "CustomOrigin" },
  1072. },
  1073. },
  1074. },
  1075. },
  1076. },
  1077. )
  1078. it.instance(
  1079. "custom model inherits npm package from models.dev provider config",
  1080. Effect.gen(function* () {
  1081. yield* set("OPENAI_API_KEY", "test-api-key")
  1082. const providers = yield* list
  1083. const model = providers[ProviderID.openai].models["my-custom-model"]
  1084. expect(model).toBeDefined()
  1085. expect(model.api.npm).toBe("@ai-sdk/openai")
  1086. }),
  1087. {
  1088. config: {
  1089. provider: {
  1090. openai: {
  1091. models: {
  1092. "my-custom-model": {
  1093. name: "My Custom Model",
  1094. tool_call: true,
  1095. limit: { context: 8000, output: 2000 },
  1096. },
  1097. },
  1098. },
  1099. },
  1100. },
  1101. },
  1102. )
  1103. it.instance(
  1104. "custom model inherits api.url from models.dev provider",
  1105. Effect.gen(function* () {
  1106. yield* set("OPENROUTER_API_KEY", "test-api-key")
  1107. const providers = yield* list
  1108. expect(providers[ProviderID.openrouter]).toBeDefined()
  1109. // New model not in database should inherit api.url from provider
  1110. const intellect = providers[ProviderID.openrouter].models["prime-intellect/intellect-3"]
  1111. expect(intellect).toBeDefined()
  1112. expect(intellect.api.url).toBe("https://openrouter.ai/api/v1")
  1113. // Another new model should also inherit api.url
  1114. const deepseek = providers[ProviderID.openrouter].models["deepseek/deepseek-r1-0528"]
  1115. expect(deepseek).toBeDefined()
  1116. expect(deepseek.api.url).toBe("https://openrouter.ai/api/v1")
  1117. expect(deepseek.name).toBe("DeepSeek R1")
  1118. }),
  1119. {
  1120. config: {
  1121. provider: {
  1122. openrouter: {
  1123. models: {
  1124. "prime-intellect/intellect-3": {},
  1125. "deepseek/deepseek-r1-0528": { name: "DeepSeek R1" },
  1126. },
  1127. },
  1128. },
  1129. },
  1130. },
  1131. )
  1132. test("mode cost preserves over-200k pricing from base model", () => {
  1133. const provider = {
  1134. id: "openai",
  1135. name: "OpenAI",
  1136. env: [],
  1137. api: "https://api.openai.com/v1",
  1138. models: {
  1139. "gpt-5.4": {
  1140. id: "gpt-5.4",
  1141. name: "GPT-5.4",
  1142. family: "gpt",
  1143. release_date: "2026-03-05",
  1144. attachment: true,
  1145. reasoning: true,
  1146. temperature: false,
  1147. tool_call: true,
  1148. cost: {
  1149. input: 2.5,
  1150. output: 15,
  1151. cache_read: 0.25,
  1152. context_over_200k: {
  1153. input: 5,
  1154. output: 22.5,
  1155. cache_read: 0.5,
  1156. },
  1157. },
  1158. limit: {
  1159. context: 1_050_000,
  1160. input: 922_000,
  1161. output: 128_000,
  1162. },
  1163. experimental: {
  1164. modes: {
  1165. fast: {
  1166. cost: {
  1167. input: 5,
  1168. output: 30,
  1169. cache_read: 0.5,
  1170. },
  1171. provider: {
  1172. body: {
  1173. service_tier: "priority",
  1174. },
  1175. },
  1176. },
  1177. },
  1178. },
  1179. },
  1180. },
  1181. } as unknown as ModelsDev.Provider
  1182. const model = Provider.fromModelsDevProvider(provider).models["gpt-5.4-fast"]
  1183. expect(model.cost.input).toEqual(5)
  1184. expect(model.cost.output).toEqual(30)
  1185. expect(model.cost.cache.read).toEqual(0.5)
  1186. expect(model.cost.cache.write).toEqual(0)
  1187. expect(model.options["serviceTier"]).toEqual("priority")
  1188. expect(model.cost.experimentalOver200K).toEqual({
  1189. input: 5,
  1190. output: 22.5,
  1191. cache: { read: 0.5, write: 0 },
  1192. })
  1193. })
  1194. test("models.dev normalization fills required response fields", () => {
  1195. const provider = {
  1196. id: "gateway",
  1197. name: "Gateway",
  1198. env: [],
  1199. models: {
  1200. "gpt-5.4": {
  1201. id: "gpt-5.4",
  1202. name: "GPT-5.4",
  1203. family: "gpt",
  1204. cost: { input: 2.5, output: 15 },
  1205. limit: { context: 1_050_000, input: 922_000, output: 128_000 },
  1206. },
  1207. },
  1208. } as unknown as ModelsDev.Provider
  1209. const model = Provider.fromModelsDevProvider(provider).models["gpt-5.4"]
  1210. expect(model.api.url).toBe("")
  1211. expect(model.capabilities.temperature).toBe(false)
  1212. expect(model.capabilities.reasoning).toBe(false)
  1213. expect(model.capabilities.attachment).toBe(false)
  1214. expect(model.capabilities.toolcall).toBe(true)
  1215. expect(model.release_date).toBe("")
  1216. })
  1217. it.instance("model variants are generated for reasoning models", () =>
  1218. Effect.gen(function* () {
  1219. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1220. const providers = yield* list
  1221. // Claude sonnet 4 has reasoning capability
  1222. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1223. expect(model.capabilities.reasoning).toBe(true)
  1224. expect(model.variants).toBeDefined()
  1225. expect(Object.keys(model.variants!).length).toBeGreaterThan(0)
  1226. }),
  1227. )
  1228. it.instance(
  1229. "model variants can be disabled via config",
  1230. Effect.gen(function* () {
  1231. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1232. const providers = yield* list
  1233. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1234. expect(model.variants).toBeDefined()
  1235. expect(model.variants!["high"]).toBeUndefined()
  1236. // max variant should still exist
  1237. expect(model.variants!["max"]).toBeDefined()
  1238. }),
  1239. {
  1240. config: {
  1241. provider: {
  1242. anthropic: {
  1243. models: { "claude-sonnet-4-20250514": { variants: { high: { disabled: true } } } },
  1244. },
  1245. },
  1246. },
  1247. },
  1248. )
  1249. it.instance(
  1250. "model variants can be customized via config",
  1251. Effect.gen(function* () {
  1252. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1253. const providers = yield* list
  1254. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1255. expect(model.variants!["high"]).toBeDefined()
  1256. expect(model.variants!["high"].thinking.budgetTokens).toBe(20000)
  1257. }),
  1258. {
  1259. config: {
  1260. provider: {
  1261. anthropic: {
  1262. models: {
  1263. "claude-sonnet-4-20250514": {
  1264. variants: { high: { thinking: { type: "enabled", budgetTokens: 20000 } } },
  1265. },
  1266. },
  1267. },
  1268. },
  1269. },
  1270. },
  1271. )
  1272. it.instance(
  1273. "disabled key is stripped from variant config",
  1274. Effect.gen(function* () {
  1275. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1276. const providers = yield* list
  1277. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1278. expect(model.variants!["max"]).toBeDefined()
  1279. expect(model.variants!["max"].disabled).toBeUndefined()
  1280. expect(model.variants!["max"].customField).toBe("test")
  1281. }),
  1282. {
  1283. config: {
  1284. provider: {
  1285. anthropic: {
  1286. models: {
  1287. "claude-sonnet-4-20250514": {
  1288. variants: { max: { disabled: false, customField: "test" } },
  1289. },
  1290. },
  1291. },
  1292. },
  1293. },
  1294. },
  1295. )
  1296. it.instance(
  1297. "all variants can be disabled via config",
  1298. Effect.gen(function* () {
  1299. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1300. const providers = yield* list
  1301. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1302. expect(model.variants).toBeDefined()
  1303. expect(Object.keys(model.variants!).length).toBe(0)
  1304. }),
  1305. {
  1306. config: {
  1307. provider: {
  1308. anthropic: {
  1309. models: {
  1310. "claude-sonnet-4-20250514": {
  1311. variants: { high: { disabled: true }, max: { disabled: true } },
  1312. },
  1313. },
  1314. },
  1315. },
  1316. },
  1317. },
  1318. )
  1319. it.instance(
  1320. "variant config merges with generated variants",
  1321. Effect.gen(function* () {
  1322. yield* set("ANTHROPIC_API_KEY", "test-api-key")
  1323. const providers = yield* list
  1324. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1325. expect(model.variants!["high"]).toBeDefined()
  1326. // Should have both the generated thinking config and the custom option
  1327. expect(model.variants!["high"].thinking).toBeDefined()
  1328. expect(model.variants!["high"].extraOption).toBe("custom-value")
  1329. }),
  1330. {
  1331. config: {
  1332. provider: {
  1333. anthropic: {
  1334. models: {
  1335. "claude-sonnet-4-20250514": { variants: { high: { extraOption: "custom-value" } } },
  1336. },
  1337. },
  1338. },
  1339. },
  1340. },
  1341. )
  1342. it.instance(
  1343. "variants filtered in second pass for database models",
  1344. Effect.gen(function* () {
  1345. yield* set("OPENAI_API_KEY", "test-api-key")
  1346. const providers = yield* list
  1347. const model = providers[ProviderID.openai].models["gpt-5"]
  1348. expect(model.variants).toBeDefined()
  1349. expect(model.variants!["high"]).toBeUndefined()
  1350. // Other variants should still exist
  1351. expect(model.variants!["medium"]).toBeDefined()
  1352. }),
  1353. {
  1354. config: {
  1355. provider: { openai: { models: { "gpt-5": { variants: { high: { disabled: true } } } } } },
  1356. },
  1357. },
  1358. )
  1359. it.instance(
  1360. "custom model with variants enabled and disabled",
  1361. Effect.gen(function* () {
  1362. const providers = yield* list
  1363. const model = providers[ProviderID.make("custom-reasoning")].models["reasoning-model"]
  1364. expect(model.variants).toBeDefined()
  1365. // Enabled variants should exist
  1366. expect(model.variants!["low"]).toBeDefined()
  1367. expect(model.variants!["low"].reasoningEffort).toBe("low")
  1368. expect(model.variants!["medium"]).toBeDefined()
  1369. expect(model.variants!["medium"].reasoningEffort).toBe("medium")
  1370. expect(model.variants!["custom"]).toBeDefined()
  1371. expect(model.variants!["custom"].reasoningEffort).toBe("custom")
  1372. expect(model.variants!["custom"].budgetTokens).toBe(5000)
  1373. // Disabled variant should not exist
  1374. expect(model.variants!["high"]).toBeUndefined()
  1375. // disabled key should be stripped from all variants
  1376. expect(model.variants!["low"].disabled).toBeUndefined()
  1377. expect(model.variants!["medium"].disabled).toBeUndefined()
  1378. expect(model.variants!["custom"].disabled).toBeUndefined()
  1379. }),
  1380. {
  1381. config: {
  1382. provider: {
  1383. "custom-reasoning": {
  1384. name: "Custom Reasoning Provider",
  1385. npm: "@ai-sdk/openai-compatible",
  1386. env: [],
  1387. models: {
  1388. "reasoning-model": {
  1389. name: "Reasoning Model",
  1390. tool_call: true,
  1391. reasoning: true,
  1392. limit: { context: 128000, output: 16000 },
  1393. variants: {
  1394. low: { reasoningEffort: "low" },
  1395. medium: { reasoningEffort: "medium" },
  1396. high: { reasoningEffort: "high", disabled: true },
  1397. custom: { reasoningEffort: "custom", budgetTokens: 5000 },
  1398. },
  1399. },
  1400. },
  1401. options: { apiKey: "test-key" },
  1402. },
  1403. },
  1404. },
  1405. },
  1406. )
  1407. it.instance(
  1408. "Google Vertex: retains baseURL for custom proxy",
  1409. Effect.gen(function* () {
  1410. yield* set("GOOGLE_APPLICATION_CREDENTIALS", "test-creds")
  1411. const providers = yield* list
  1412. expect(providers[ProviderID.make("vertex-proxy")]).toBeDefined()
  1413. expect(providers[ProviderID.make("vertex-proxy")].options.baseURL).toBe("https://my-proxy.com/v1")
  1414. }),
  1415. {
  1416. config: {
  1417. provider: {
  1418. "vertex-proxy": {
  1419. name: "Vertex Proxy",
  1420. npm: "@ai-sdk/google-vertex",
  1421. api: "https://my-proxy.com/v1",
  1422. env: ["GOOGLE_APPLICATION_CREDENTIALS"],
  1423. models: { "gemini-pro": { name: "Gemini Pro", tool_call: true } },
  1424. options: {
  1425. project: "test-project",
  1426. location: "us-central1",
  1427. baseURL: "https://my-proxy.com/v1",
  1428. },
  1429. },
  1430. },
  1431. },
  1432. },
  1433. )
  1434. it.instance(
  1435. "Google Vertex: supports OpenAI compatible models",
  1436. Effect.gen(function* () {
  1437. yield* set("GOOGLE_APPLICATION_CREDENTIALS", "test-creds")
  1438. const providers = yield* list
  1439. const model = providers[ProviderID.make("vertex-openai")].models["gpt-4"]
  1440. expect(model).toBeDefined()
  1441. expect(model.api.npm).toBe("@ai-sdk/openai-compatible")
  1442. }),
  1443. {
  1444. config: {
  1445. provider: {
  1446. "vertex-openai": {
  1447. name: "Vertex OpenAI",
  1448. npm: "@ai-sdk/google-vertex",
  1449. env: ["GOOGLE_APPLICATION_CREDENTIALS"],
  1450. models: {
  1451. "gpt-4": {
  1452. name: "GPT-4",
  1453. provider: { npm: "@ai-sdk/openai-compatible", api: "https://api.openai.com/v1" },
  1454. },
  1455. },
  1456. options: { project: "test-project", location: "us-central1" },
  1457. },
  1458. },
  1459. },
  1460. },
  1461. )
  1462. it.instance("Google Vertex: uses REP endpoint for Claude continental multi-regions", () =>
  1463. Effect.gen(function* () {
  1464. yield* set("GOOGLE_CLOUD_PROJECT", "test-project")
  1465. yield* set("VERTEX_LOCATION", "eu")
  1466. const provider = yield* Provider.Service
  1467. const model = yield* provider.getModel(ProviderID.make("google-vertex"), ModelID.make("claude-sonnet-4-6@default"))
  1468. const language = yield* provider.getLanguage(model)
  1469. expect(languageBaseURL(language)).toBe(
  1470. "https://aiplatform.eu.rep.googleapis.com/v1/projects/test-project/locations/eu/publishers/anthropic/models",
  1471. )
  1472. }),
  1473. )
  1474. it.instance("Google Vertex Anthropic: uses REP endpoint for continental multi-regions", () =>
  1475. Effect.gen(function* () {
  1476. yield* set("GOOGLE_CLOUD_PROJECT", "test-project")
  1477. yield* set("VERTEX_LOCATION", "us")
  1478. const provider = yield* Provider.Service
  1479. const model = yield* provider.getModel(
  1480. ProviderID.make("google-vertex-anthropic"),
  1481. ModelID.make("claude-sonnet-4-6@default"),
  1482. )
  1483. const language = yield* provider.getLanguage(model)
  1484. expect(languageBaseURL(language)).toBe(
  1485. "https://aiplatform.us.rep.googleapis.com/v1/projects/test-project/locations/us/publishers/anthropic/models",
  1486. )
  1487. }),
  1488. )
  1489. it.instance("Google Vertex: keeps regional Claude endpoints unchanged", () =>
  1490. Effect.gen(function* () {
  1491. yield* set("GOOGLE_CLOUD_PROJECT", "test-project")
  1492. yield* set("VERTEX_LOCATION", "europe-west1")
  1493. const provider = yield* Provider.Service
  1494. const model = yield* provider.getModel(ProviderID.make("google-vertex"), ModelID.make("claude-sonnet-4-6@default"))
  1495. const language = yield* provider.getLanguage(model)
  1496. expect(languageBaseURL(language)).toBe(
  1497. "https://europe-west1-aiplatform.googleapis.com/v1/projects/test-project/locations/europe-west1/publishers/anthropic/models",
  1498. )
  1499. }),
  1500. )
  1501. it.instance("cloudflare-ai-gateway loads with env variables", () =>
  1502. Effect.gen(function* () {
  1503. yield* set("CLOUDFLARE_ACCOUNT_ID", "test-account")
  1504. yield* set("CLOUDFLARE_GATEWAY_ID", "test-gateway")
  1505. yield* set("CLOUDFLARE_API_TOKEN", "test-token")
  1506. const providers = yield* list
  1507. expect(providers[ProviderID.make("cloudflare-ai-gateway")]).toBeDefined()
  1508. }),
  1509. )
  1510. it.instance(
  1511. "cloudflare-ai-gateway forwards config metadata options",
  1512. Effect.gen(function* () {
  1513. yield* set("CLOUDFLARE_ACCOUNT_ID", "test-account")
  1514. yield* set("CLOUDFLARE_GATEWAY_ID", "test-gateway")
  1515. yield* set("CLOUDFLARE_API_TOKEN", "test-token")
  1516. const providers = yield* list
  1517. expect(providers[ProviderID.make("cloudflare-ai-gateway")]).toBeDefined()
  1518. expect(providers[ProviderID.make("cloudflare-ai-gateway")].options.metadata).toEqual({
  1519. invoked_by: "test",
  1520. project: "opencode",
  1521. })
  1522. }),
  1523. {
  1524. config: {
  1525. provider: { "cloudflare-ai-gateway": { options: { metadata: { invoked_by: "test", project: "opencode" } } } },
  1526. },
  1527. },
  1528. )
  1529. // Tests that need plugin file setup or multi-instance flows fall back to a
  1530. // scoped tmpdir + provideInstance pattern via it.effect.
  1531. const provideMultiInstance = <A, E, R>(eff: Effect.Effect<A, E, R>) =>
  1532. eff.pipe(Effect.provide(InstanceLayer.layer), Effect.provide(CrossSpawnSpawner.defaultLayer))
  1533. it.effect("plugin config providers persist after instance dispose", () =>
  1534. Effect.gen(function* () {
  1535. const dir = yield* tmpdirScoped()
  1536. const configDir = path.join(dir, ".opencode")
  1537. const root = path.join(configDir, "plugin")
  1538. yield* Effect.promise(() => mkdir(root, { recursive: true }))
  1539. yield* Effect.promise(() => markPluginDependenciesReady(configDir))
  1540. yield* Effect.promise(() => markPluginDependenciesReady(Global.Path.config))
  1541. yield* Effect.promise(() =>
  1542. Bun.write(
  1543. path.join(root, "demo-provider.ts"),
  1544. [
  1545. "export default {",
  1546. ' id: "demo.plugin-provider",',
  1547. " server: async () => ({",
  1548. " async config(cfg) {",
  1549. " cfg.provider ??= {}",
  1550. " cfg.provider.demo = {",
  1551. ' name: "Demo Provider",',
  1552. ' npm: "@ai-sdk/openai-compatible",',
  1553. ' api: "https://example.com/v1",',
  1554. " models: {",
  1555. " chat: {",
  1556. ' name: "Demo Chat",',
  1557. " tool_call: true,",
  1558. " limit: { context: 128000, output: 4096 },",
  1559. " },",
  1560. " },",
  1561. " }",
  1562. " },",
  1563. " }),",
  1564. "}",
  1565. "",
  1566. ].join("\n"),
  1567. ),
  1568. )
  1569. const loadAndList = Effect.gen(function* () {
  1570. const plugin = yield* Plugin.Service
  1571. const provider = yield* Provider.Service
  1572. yield* plugin.init()
  1573. return yield* provider.list()
  1574. }).pipe(provideInstanceEffect(dir))
  1575. const first = yield* loadAndList
  1576. expect(first[ProviderID.make("demo")]).toBeDefined()
  1577. expect(first[ProviderID.make("demo")].models[ModelID.make("chat")]).toBeDefined()
  1578. yield* Effect.promise(() => disposeAllInstances())
  1579. const second = yield* loadAndList
  1580. expect(second[ProviderID.make("demo")]).toBeDefined()
  1581. expect(second[ProviderID.make("demo")].models[ModelID.make("chat")]).toBeDefined()
  1582. }).pipe(provideMultiInstance),
  1583. )
  1584. it.instance(
  1585. "plugin config enabled and disabled providers are honored",
  1586. Effect.gen(function* () {
  1587. const instance = yield* TestInstance
  1588. const configDir = path.join(instance.directory, ".opencode")
  1589. const root = path.join(configDir, "plugin")
  1590. yield* Effect.promise(() => mkdir(root, { recursive: true }))
  1591. yield* Effect.promise(() => markPluginDependenciesReady(configDir))
  1592. yield* Effect.promise(() =>
  1593. Bun.write(
  1594. path.join(root, "provider-filter.ts"),
  1595. [
  1596. "export default {",
  1597. ' id: "demo.provider-filter",',
  1598. " server: async () => ({",
  1599. " async config(cfg) {",
  1600. ' cfg.enabled_providers = ["anthropic", "openai"]',
  1601. ' cfg.disabled_providers = ["openai"]',
  1602. " },",
  1603. " }),",
  1604. "}",
  1605. "",
  1606. ].join("\n"),
  1607. ),
  1608. )
  1609. yield* set("ANTHROPIC_API_KEY", "test-anthropic-key")
  1610. yield* set("OPENAI_API_KEY", "test-openai-key")
  1611. const providers = yield* list
  1612. expect(providers[ProviderID.anthropic]).toBeDefined()
  1613. expect(providers[ProviderID.openai]).toBeUndefined()
  1614. }),
  1615. )
  1616. it.effect("opencode loader keeps paid models when config apiKey is present", () =>
  1617. Effect.gen(function* () {
  1618. const noneDir = yield* tmpdirScoped()
  1619. const keyedDir = yield* tmpdirScoped({
  1620. config: { provider: { opencode: { options: { apiKey: "test-key" } } } },
  1621. })
  1622. const listIn = (directory: string) =>
  1623. Provider.use
  1624. .list()
  1625. .pipe(provideInstanceEffect(directory))
  1626. .pipe(Effect.provide(InstanceLayer.layer), Effect.provide(CrossSpawnSpawner.defaultLayer))
  1627. const none = paid(yield* listIn(noneDir))
  1628. const keyedCount = paid(yield* listIn(keyedDir))
  1629. expect(none).toBe(0)
  1630. expect(keyedCount).toBeGreaterThan(0)
  1631. }).pipe(provideMultiInstance),
  1632. )
  1633. it.effect("opencode loader keeps paid models when auth exists", () =>
  1634. Effect.gen(function* () {
  1635. const noneDir = yield* tmpdirScoped()
  1636. const keyedDir = yield* tmpdirScoped()
  1637. const listIn = (directory: string) =>
  1638. Provider.use
  1639. .list()
  1640. .pipe(provideInstanceEffect(directory))
  1641. .pipe(Effect.provide(InstanceLayer.layer), Effect.provide(CrossSpawnSpawner.defaultLayer))
  1642. const none = paid(yield* listIn(noneDir))
  1643. const authPath = path.join(Global.Path.data, "auth.json")
  1644. const original = yield* Effect.promise(() => Filesystem.readText(authPath).catch(() => undefined))
  1645. yield* Effect.acquireRelease(
  1646. Effect.promise(() => Filesystem.write(authPath, JSON.stringify({ opencode: { type: "api", key: "test-key" } }))),
  1647. () =>
  1648. Effect.promise(async () => {
  1649. if (original !== undefined) await Filesystem.write(authPath, original)
  1650. else await unlink(authPath).catch(() => undefined)
  1651. }),
  1652. )
  1653. const keyedCount = paid(yield* listIn(keyedDir))
  1654. expect(none).toBe(0)
  1655. expect(keyedCount).toBeGreaterThan(0)
  1656. }).pipe(provideMultiInstance),
  1657. )