provider.test.ts 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714
  1. import { test, expect } from "bun:test"
  2. import { mkdir, unlink } from "fs/promises"
  3. import path from "path"
  4. import { tmpdir } from "../fixture/fixture"
  5. import { Global } from "@opencode-ai/core/global"
  6. import { Instance } from "../../src/project/instance"
  7. import { Plugin } from "../../src/plugin/index"
  8. import { ModelsDev } from "../../src/provider"
  9. import { Provider } from "../../src/provider"
  10. import { ProviderID, ModelID } from "../../src/provider/schema"
  11. import { Filesystem } from "../../src/util"
  12. import { Env } from "../../src/env"
  13. import { Effect } from "effect"
  14. import { AppRuntime } from "../../src/effect/app-runtime"
  15. import { makeRuntime } from "../../src/effect/run-service"
  16. const env = makeRuntime(Env.Service, Env.defaultLayer)
  17. const set = (k: string, v: string) => env.runSync((svc) => svc.set(k, v))
  18. async function run<A, E>(fn: (provider: Provider.Interface) => Effect.Effect<A, E, never>) {
  19. return AppRuntime.runPromise(
  20. Effect.gen(function* () {
  21. const provider = yield* Provider.Service
  22. return yield* fn(provider)
  23. }),
  24. )
  25. }
  26. async function list() {
  27. return run((provider) => provider.list())
  28. }
  29. async function getProvider(providerID: ProviderID) {
  30. return run((provider) => provider.getProvider(providerID))
  31. }
  32. async function getModel(providerID: ProviderID, modelID: ModelID) {
  33. return run((provider) => provider.getModel(providerID, modelID))
  34. }
  35. async function getLanguage(model: Provider.Model) {
  36. return run((provider) => provider.getLanguage(model))
  37. }
  38. async function closest(providerID: ProviderID, query: string[]) {
  39. return run((provider) => provider.closest(providerID, query))
  40. }
  41. async function getSmallModel(providerID: ProviderID) {
  42. return run((provider) => provider.getSmallModel(providerID))
  43. }
  44. async function defaultModel() {
  45. return run((provider) => provider.defaultModel())
  46. }
  47. async function markPluginDependenciesReady(dir: string) {
  48. await mkdir(path.join(dir, "node_modules"), { recursive: true })
  49. await Bun.write(
  50. path.join(dir, "package-lock.json"),
  51. JSON.stringify({ packages: { "": { dependencies: { "@opencode-ai/plugin": "0.0.0" } } } }),
  52. )
  53. }
  54. function paid(providers: Awaited<ReturnType<typeof list>>) {
  55. const item = providers[ProviderID.make("opencode")]
  56. expect(item).toBeDefined()
  57. return Object.values(item.models).filter((model) => model.cost.input > 0).length
  58. }
  59. test("provider loaded from env variable", async () => {
  60. await using tmp = await tmpdir({
  61. init: async (dir) => {
  62. await Bun.write(
  63. path.join(dir, "opencode.json"),
  64. JSON.stringify({
  65. $schema: "https://opencode.ai/config.json",
  66. }),
  67. )
  68. },
  69. })
  70. await Instance.provide({
  71. directory: tmp.path,
  72. init: async () => {
  73. set("ANTHROPIC_API_KEY", "test-api-key")
  74. },
  75. fn: async () => {
  76. const providers = await list()
  77. expect(providers[ProviderID.anthropic]).toBeDefined()
  78. // Provider should retain its connection source even if custom loaders
  79. // merge additional options.
  80. expect(providers[ProviderID.anthropic].source).toBe("env")
  81. expect(providers[ProviderID.anthropic].options.headers["anthropic-beta"]).toBeDefined()
  82. },
  83. })
  84. })
  85. test("provider loaded from config with apiKey option", async () => {
  86. await using tmp = await tmpdir({
  87. init: async (dir) => {
  88. await Bun.write(
  89. path.join(dir, "opencode.json"),
  90. JSON.stringify({
  91. $schema: "https://opencode.ai/config.json",
  92. provider: {
  93. anthropic: {
  94. options: {
  95. apiKey: "config-api-key",
  96. },
  97. },
  98. },
  99. }),
  100. )
  101. },
  102. })
  103. await Instance.provide({
  104. directory: tmp.path,
  105. fn: async () => {
  106. const providers = await list()
  107. expect(providers[ProviderID.anthropic]).toBeDefined()
  108. },
  109. })
  110. })
  111. test("disabled_providers excludes provider", async () => {
  112. await using tmp = await tmpdir({
  113. init: async (dir) => {
  114. await Bun.write(
  115. path.join(dir, "opencode.json"),
  116. JSON.stringify({
  117. $schema: "https://opencode.ai/config.json",
  118. disabled_providers: ["anthropic"],
  119. }),
  120. )
  121. },
  122. })
  123. await Instance.provide({
  124. directory: tmp.path,
  125. init: async () => {
  126. set("ANTHROPIC_API_KEY", "test-api-key")
  127. },
  128. fn: async () => {
  129. const providers = await list()
  130. expect(providers[ProviderID.anthropic]).toBeUndefined()
  131. },
  132. })
  133. })
  134. test("enabled_providers restricts to only listed providers", async () => {
  135. await using tmp = await tmpdir({
  136. init: async (dir) => {
  137. await Bun.write(
  138. path.join(dir, "opencode.json"),
  139. JSON.stringify({
  140. $schema: "https://opencode.ai/config.json",
  141. enabled_providers: ["anthropic"],
  142. }),
  143. )
  144. },
  145. })
  146. await Instance.provide({
  147. directory: tmp.path,
  148. init: async () => {
  149. set("ANTHROPIC_API_KEY", "test-api-key")
  150. set("OPENAI_API_KEY", "test-openai-key")
  151. },
  152. fn: async () => {
  153. const providers = await list()
  154. expect(providers[ProviderID.anthropic]).toBeDefined()
  155. expect(providers[ProviderID.openai]).toBeUndefined()
  156. },
  157. })
  158. })
  159. test("model whitelist filters models for provider", async () => {
  160. await using tmp = await tmpdir({
  161. init: async (dir) => {
  162. await Bun.write(
  163. path.join(dir, "opencode.json"),
  164. JSON.stringify({
  165. $schema: "https://opencode.ai/config.json",
  166. provider: {
  167. anthropic: {
  168. whitelist: ["claude-sonnet-4-20250514"],
  169. },
  170. },
  171. }),
  172. )
  173. },
  174. })
  175. await Instance.provide({
  176. directory: tmp.path,
  177. init: async () => {
  178. set("ANTHROPIC_API_KEY", "test-api-key")
  179. },
  180. fn: async () => {
  181. const providers = await list()
  182. expect(providers[ProviderID.anthropic]).toBeDefined()
  183. const models = Object.keys(providers[ProviderID.anthropic].models)
  184. expect(models).toContain("claude-sonnet-4-20250514")
  185. expect(models.length).toBe(1)
  186. },
  187. })
  188. })
  189. test("model blacklist excludes specific models", async () => {
  190. await using tmp = await tmpdir({
  191. init: async (dir) => {
  192. await Bun.write(
  193. path.join(dir, "opencode.json"),
  194. JSON.stringify({
  195. $schema: "https://opencode.ai/config.json",
  196. provider: {
  197. anthropic: {
  198. blacklist: ["claude-sonnet-4-20250514"],
  199. },
  200. },
  201. }),
  202. )
  203. },
  204. })
  205. await Instance.provide({
  206. directory: tmp.path,
  207. init: async () => {
  208. set("ANTHROPIC_API_KEY", "test-api-key")
  209. },
  210. fn: async () => {
  211. const providers = await list()
  212. expect(providers[ProviderID.anthropic]).toBeDefined()
  213. const models = Object.keys(providers[ProviderID.anthropic].models)
  214. expect(models).not.toContain("claude-sonnet-4-20250514")
  215. },
  216. })
  217. })
  218. test("custom model alias via config", async () => {
  219. await using tmp = await tmpdir({
  220. init: async (dir) => {
  221. await Bun.write(
  222. path.join(dir, "opencode.json"),
  223. JSON.stringify({
  224. $schema: "https://opencode.ai/config.json",
  225. provider: {
  226. anthropic: {
  227. models: {
  228. "my-alias": {
  229. id: "claude-sonnet-4-20250514",
  230. name: "My Custom Alias",
  231. },
  232. },
  233. },
  234. },
  235. }),
  236. )
  237. },
  238. })
  239. await Instance.provide({
  240. directory: tmp.path,
  241. init: async () => {
  242. set("ANTHROPIC_API_KEY", "test-api-key")
  243. },
  244. fn: async () => {
  245. const providers = await list()
  246. expect(providers[ProviderID.anthropic]).toBeDefined()
  247. expect(providers[ProviderID.anthropic].models["my-alias"]).toBeDefined()
  248. expect(providers[ProviderID.anthropic].models["my-alias"].name).toBe("My Custom Alias")
  249. },
  250. })
  251. })
  252. test("custom provider with npm package", async () => {
  253. await using tmp = await tmpdir({
  254. init: async (dir) => {
  255. await Bun.write(
  256. path.join(dir, "opencode.json"),
  257. JSON.stringify({
  258. $schema: "https://opencode.ai/config.json",
  259. provider: {
  260. "custom-provider": {
  261. name: "Custom Provider",
  262. npm: "@ai-sdk/openai-compatible",
  263. api: "https://api.custom.com/v1",
  264. env: ["CUSTOM_API_KEY"],
  265. models: {
  266. "custom-model": {
  267. name: "Custom Model",
  268. tool_call: true,
  269. limit: {
  270. context: 128000,
  271. output: 4096,
  272. },
  273. },
  274. },
  275. options: {
  276. apiKey: "custom-key",
  277. },
  278. },
  279. },
  280. }),
  281. )
  282. },
  283. })
  284. await Instance.provide({
  285. directory: tmp.path,
  286. fn: async () => {
  287. const providers = await list()
  288. expect(providers[ProviderID.make("custom-provider")]).toBeDefined()
  289. expect(providers[ProviderID.make("custom-provider")].name).toBe("Custom Provider")
  290. expect(providers[ProviderID.make("custom-provider")].models["custom-model"]).toBeDefined()
  291. },
  292. })
  293. })
  294. test("custom DeepSeek openai-compatible model defaults interleaved reasoning field", async () => {
  295. await using tmp = await tmpdir({
  296. init: async (dir) => {
  297. await Bun.write(
  298. path.join(dir, "opencode.json"),
  299. JSON.stringify({
  300. $schema: "https://opencode.ai/config.json",
  301. provider: {
  302. "custom-provider": {
  303. name: "Custom Provider",
  304. npm: "@ai-sdk/openai-compatible",
  305. api: "https://api.custom.com/v1",
  306. models: {
  307. "deepseek-r1": {
  308. name: "DeepSeek R1",
  309. },
  310. "deepseek-details": {
  311. name: "DeepSeek Details",
  312. interleaved: { field: "reasoning_details" },
  313. },
  314. "custom-model": {
  315. name: "Custom Model",
  316. },
  317. },
  318. options: {
  319. apiKey: "custom-key",
  320. },
  321. },
  322. "custom-anthropic-provider": {
  323. name: "Custom Anthropic Provider",
  324. npm: "@ai-sdk/anthropic",
  325. api: "https://api.custom.com/v1",
  326. models: {
  327. "deepseek-r1": {
  328. name: "DeepSeek R1",
  329. },
  330. },
  331. options: {
  332. apiKey: "custom-key",
  333. },
  334. },
  335. },
  336. }),
  337. )
  338. },
  339. })
  340. await Instance.provide({
  341. directory: tmp.path,
  342. fn: async () => {
  343. const providers = await list()
  344. const provider = providers[ProviderID.make("custom-provider")]
  345. expect(provider.models["deepseek-r1"].capabilities.interleaved).toEqual({ field: "reasoning_content" })
  346. expect(provider.models["deepseek-details"].capabilities.interleaved).toEqual({ field: "reasoning_details" })
  347. expect(provider.models["custom-model"].capabilities.interleaved).toBe(false)
  348. expect(
  349. providers[ProviderID.make("custom-anthropic-provider")].models["deepseek-r1"].capabilities.interleaved,
  350. ).toBe(false)
  351. },
  352. })
  353. })
  354. test("env variable takes precedence, config merges options", async () => {
  355. await using tmp = await tmpdir({
  356. init: async (dir) => {
  357. await Bun.write(
  358. path.join(dir, "opencode.json"),
  359. JSON.stringify({
  360. $schema: "https://opencode.ai/config.json",
  361. provider: {
  362. anthropic: {
  363. options: {
  364. timeout: 60000,
  365. chunkTimeout: 15000,
  366. },
  367. },
  368. },
  369. }),
  370. )
  371. },
  372. })
  373. await Instance.provide({
  374. directory: tmp.path,
  375. init: async () => {
  376. set("ANTHROPIC_API_KEY", "env-api-key")
  377. },
  378. fn: async () => {
  379. const providers = await list()
  380. expect(providers[ProviderID.anthropic]).toBeDefined()
  381. // Config options should be merged
  382. expect(providers[ProviderID.anthropic].options.timeout).toBe(60000)
  383. expect(providers[ProviderID.anthropic].options.chunkTimeout).toBe(15000)
  384. },
  385. })
  386. })
  387. test("getModel returns model for valid provider/model", async () => {
  388. await using tmp = await tmpdir({
  389. init: async (dir) => {
  390. await Bun.write(
  391. path.join(dir, "opencode.json"),
  392. JSON.stringify({
  393. $schema: "https://opencode.ai/config.json",
  394. }),
  395. )
  396. },
  397. })
  398. await Instance.provide({
  399. directory: tmp.path,
  400. init: async () => {
  401. set("ANTHROPIC_API_KEY", "test-api-key")
  402. },
  403. fn: async () => {
  404. const model = await getModel(ProviderID.anthropic, ModelID.make("claude-sonnet-4-20250514"))
  405. expect(model).toBeDefined()
  406. expect(String(model.providerID)).toBe("anthropic")
  407. expect(String(model.id)).toBe("claude-sonnet-4-20250514")
  408. const language = await getLanguage(model)
  409. expect(language).toBeDefined()
  410. },
  411. })
  412. })
  413. test("getModel throws ModelNotFoundError for invalid model", async () => {
  414. await using tmp = await tmpdir({
  415. init: async (dir) => {
  416. await Bun.write(
  417. path.join(dir, "opencode.json"),
  418. JSON.stringify({
  419. $schema: "https://opencode.ai/config.json",
  420. }),
  421. )
  422. },
  423. })
  424. await Instance.provide({
  425. directory: tmp.path,
  426. init: async () => {
  427. set("ANTHROPIC_API_KEY", "test-api-key")
  428. },
  429. fn: async () => {
  430. expect(getModel(ProviderID.anthropic, ModelID.make("nonexistent-model"))).rejects.toThrow()
  431. },
  432. })
  433. })
  434. test("getModel throws ModelNotFoundError for invalid provider", async () => {
  435. await using tmp = await tmpdir({
  436. init: async (dir) => {
  437. await Bun.write(
  438. path.join(dir, "opencode.json"),
  439. JSON.stringify({
  440. $schema: "https://opencode.ai/config.json",
  441. }),
  442. )
  443. },
  444. })
  445. await Instance.provide({
  446. directory: tmp.path,
  447. fn: async () => {
  448. expect(getModel(ProviderID.make("nonexistent-provider"), ModelID.make("some-model"))).rejects.toThrow()
  449. },
  450. })
  451. })
  452. test("parseModel correctly parses provider/model string", () => {
  453. const result = Provider.parseModel("anthropic/claude-sonnet-4")
  454. expect(String(result.providerID)).toBe("anthropic")
  455. expect(String(result.modelID)).toBe("claude-sonnet-4")
  456. })
  457. test("parseModel handles model IDs with slashes", () => {
  458. const result = Provider.parseModel("openrouter/anthropic/claude-3-opus")
  459. expect(String(result.providerID)).toBe("openrouter")
  460. expect(String(result.modelID)).toBe("anthropic/claude-3-opus")
  461. })
  462. test("defaultModel returns first available model when no config set", async () => {
  463. await using tmp = await tmpdir({
  464. init: async (dir) => {
  465. await Bun.write(
  466. path.join(dir, "opencode.json"),
  467. JSON.stringify({
  468. $schema: "https://opencode.ai/config.json",
  469. }),
  470. )
  471. },
  472. })
  473. await Instance.provide({
  474. directory: tmp.path,
  475. init: async () => {
  476. set("ANTHROPIC_API_KEY", "test-api-key")
  477. },
  478. fn: async () => {
  479. const model = await defaultModel()
  480. expect(model.providerID).toBeDefined()
  481. expect(model.modelID).toBeDefined()
  482. },
  483. })
  484. })
  485. test("defaultModel respects config model setting", async () => {
  486. await using tmp = await tmpdir({
  487. init: async (dir) => {
  488. await Bun.write(
  489. path.join(dir, "opencode.json"),
  490. JSON.stringify({
  491. $schema: "https://opencode.ai/config.json",
  492. model: "anthropic/claude-sonnet-4-20250514",
  493. }),
  494. )
  495. },
  496. })
  497. await Instance.provide({
  498. directory: tmp.path,
  499. init: async () => {
  500. set("ANTHROPIC_API_KEY", "test-api-key")
  501. },
  502. fn: async () => {
  503. const model = await defaultModel()
  504. expect(String(model.providerID)).toBe("anthropic")
  505. expect(String(model.modelID)).toBe("claude-sonnet-4-20250514")
  506. },
  507. })
  508. })
  509. test("provider with baseURL from config", async () => {
  510. await using tmp = await tmpdir({
  511. init: async (dir) => {
  512. await Bun.write(
  513. path.join(dir, "opencode.json"),
  514. JSON.stringify({
  515. $schema: "https://opencode.ai/config.json",
  516. provider: {
  517. "custom-openai": {
  518. name: "Custom OpenAI",
  519. npm: "@ai-sdk/openai-compatible",
  520. env: [],
  521. models: {
  522. "gpt-4": {
  523. name: "GPT-4",
  524. tool_call: true,
  525. limit: { context: 128000, output: 4096 },
  526. },
  527. },
  528. options: {
  529. apiKey: "test-key",
  530. baseURL: "https://custom.openai.com/v1",
  531. },
  532. },
  533. },
  534. }),
  535. )
  536. },
  537. })
  538. await Instance.provide({
  539. directory: tmp.path,
  540. fn: async () => {
  541. const providers = await list()
  542. expect(providers[ProviderID.make("custom-openai")]).toBeDefined()
  543. expect(providers[ProviderID.make("custom-openai")].options.baseURL).toBe("https://custom.openai.com/v1")
  544. },
  545. })
  546. })
  547. test("model cost defaults to zero when not specified", async () => {
  548. await using tmp = await tmpdir({
  549. init: async (dir) => {
  550. await Bun.write(
  551. path.join(dir, "opencode.json"),
  552. JSON.stringify({
  553. $schema: "https://opencode.ai/config.json",
  554. provider: {
  555. "test-provider": {
  556. name: "Test Provider",
  557. npm: "@ai-sdk/openai-compatible",
  558. env: [],
  559. models: {
  560. "test-model": {
  561. name: "Test Model",
  562. tool_call: true,
  563. limit: { context: 128000, output: 4096 },
  564. },
  565. },
  566. options: {
  567. apiKey: "test-key",
  568. },
  569. },
  570. },
  571. }),
  572. )
  573. },
  574. })
  575. await Instance.provide({
  576. directory: tmp.path,
  577. fn: async () => {
  578. const providers = await list()
  579. const model = providers[ProviderID.make("test-provider")].models["test-model"]
  580. expect(model.cost.input).toBe(0)
  581. expect(model.cost.output).toBe(0)
  582. expect(model.cost.cache.read).toBe(0)
  583. expect(model.cost.cache.write).toBe(0)
  584. },
  585. })
  586. })
  587. test("model options are merged from existing model", async () => {
  588. await using tmp = await tmpdir({
  589. init: async (dir) => {
  590. await Bun.write(
  591. path.join(dir, "opencode.json"),
  592. JSON.stringify({
  593. $schema: "https://opencode.ai/config.json",
  594. provider: {
  595. anthropic: {
  596. models: {
  597. "claude-sonnet-4-20250514": {
  598. options: {
  599. customOption: "custom-value",
  600. },
  601. },
  602. },
  603. },
  604. },
  605. }),
  606. )
  607. },
  608. })
  609. await Instance.provide({
  610. directory: tmp.path,
  611. init: async () => {
  612. set("ANTHROPIC_API_KEY", "test-api-key")
  613. },
  614. fn: async () => {
  615. const providers = await list()
  616. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  617. expect(model.options.customOption).toBe("custom-value")
  618. },
  619. })
  620. })
  621. test("provider removed when all models filtered out", async () => {
  622. await using tmp = await tmpdir({
  623. init: async (dir) => {
  624. await Bun.write(
  625. path.join(dir, "opencode.json"),
  626. JSON.stringify({
  627. $schema: "https://opencode.ai/config.json",
  628. provider: {
  629. anthropic: {
  630. whitelist: ["nonexistent-model"],
  631. },
  632. },
  633. }),
  634. )
  635. },
  636. })
  637. await Instance.provide({
  638. directory: tmp.path,
  639. init: async () => {
  640. set("ANTHROPIC_API_KEY", "test-api-key")
  641. },
  642. fn: async () => {
  643. const providers = await list()
  644. expect(providers[ProviderID.anthropic]).toBeUndefined()
  645. },
  646. })
  647. })
  648. test("closest finds model by partial match", async () => {
  649. await using tmp = await tmpdir({
  650. init: async (dir) => {
  651. await Bun.write(
  652. path.join(dir, "opencode.json"),
  653. JSON.stringify({
  654. $schema: "https://opencode.ai/config.json",
  655. }),
  656. )
  657. },
  658. })
  659. await Instance.provide({
  660. directory: tmp.path,
  661. init: async () => {
  662. set("ANTHROPIC_API_KEY", "test-api-key")
  663. },
  664. fn: async () => {
  665. const result = await closest(ProviderID.anthropic, ["sonnet-4"])
  666. expect(result).toBeDefined()
  667. expect(String(result?.providerID)).toBe("anthropic")
  668. expect(String(result?.modelID)).toContain("sonnet-4")
  669. },
  670. })
  671. })
  672. test("closest returns undefined for nonexistent provider", async () => {
  673. await using tmp = await tmpdir({
  674. init: async (dir) => {
  675. await Bun.write(
  676. path.join(dir, "opencode.json"),
  677. JSON.stringify({
  678. $schema: "https://opencode.ai/config.json",
  679. }),
  680. )
  681. },
  682. })
  683. await Instance.provide({
  684. directory: tmp.path,
  685. fn: async () => {
  686. const result = await closest(ProviderID.make("nonexistent"), ["model"])
  687. expect(result).toBeUndefined()
  688. },
  689. })
  690. })
  691. test("getModel uses realIdByKey for aliased models", async () => {
  692. await using tmp = await tmpdir({
  693. init: async (dir) => {
  694. await Bun.write(
  695. path.join(dir, "opencode.json"),
  696. JSON.stringify({
  697. $schema: "https://opencode.ai/config.json",
  698. provider: {
  699. anthropic: {
  700. models: {
  701. "my-sonnet": {
  702. id: "claude-sonnet-4-20250514",
  703. name: "My Sonnet Alias",
  704. },
  705. },
  706. },
  707. },
  708. }),
  709. )
  710. },
  711. })
  712. await Instance.provide({
  713. directory: tmp.path,
  714. init: async () => {
  715. set("ANTHROPIC_API_KEY", "test-api-key")
  716. },
  717. fn: async () => {
  718. const providers = await list()
  719. expect(providers[ProviderID.anthropic].models["my-sonnet"]).toBeDefined()
  720. const model = await getModel(ProviderID.anthropic, ModelID.make("my-sonnet"))
  721. expect(model).toBeDefined()
  722. expect(String(model.id)).toBe("my-sonnet")
  723. expect(model.name).toBe("My Sonnet Alias")
  724. },
  725. })
  726. })
  727. test("provider api field sets model api.url", async () => {
  728. await using tmp = await tmpdir({
  729. init: async (dir) => {
  730. await Bun.write(
  731. path.join(dir, "opencode.json"),
  732. JSON.stringify({
  733. $schema: "https://opencode.ai/config.json",
  734. provider: {
  735. "custom-api": {
  736. name: "Custom API",
  737. npm: "@ai-sdk/openai-compatible",
  738. api: "https://api.example.com/v1",
  739. env: [],
  740. models: {
  741. "model-1": {
  742. name: "Model 1",
  743. tool_call: true,
  744. limit: { context: 8000, output: 2000 },
  745. },
  746. },
  747. options: {
  748. apiKey: "test-key",
  749. },
  750. },
  751. },
  752. }),
  753. )
  754. },
  755. })
  756. await Instance.provide({
  757. directory: tmp.path,
  758. fn: async () => {
  759. const providers = await list()
  760. // api field is stored on model.api.url, used by getSDK to set baseURL
  761. expect(providers[ProviderID.make("custom-api")].models["model-1"].api.url).toBe("https://api.example.com/v1")
  762. },
  763. })
  764. })
  765. test("explicit baseURL overrides api field", async () => {
  766. await using tmp = await tmpdir({
  767. init: async (dir) => {
  768. await Bun.write(
  769. path.join(dir, "opencode.json"),
  770. JSON.stringify({
  771. $schema: "https://opencode.ai/config.json",
  772. provider: {
  773. "custom-api": {
  774. name: "Custom API",
  775. npm: "@ai-sdk/openai-compatible",
  776. api: "https://api.example.com/v1",
  777. env: [],
  778. models: {
  779. "model-1": {
  780. name: "Model 1",
  781. tool_call: true,
  782. limit: { context: 8000, output: 2000 },
  783. },
  784. },
  785. options: {
  786. apiKey: "test-key",
  787. baseURL: "https://custom.override.com/v1",
  788. },
  789. },
  790. },
  791. }),
  792. )
  793. },
  794. })
  795. await Instance.provide({
  796. directory: tmp.path,
  797. fn: async () => {
  798. const providers = await list()
  799. expect(providers[ProviderID.make("custom-api")].options.baseURL).toBe("https://custom.override.com/v1")
  800. },
  801. })
  802. })
  803. test("model inherits properties from existing database model", async () => {
  804. await using tmp = await tmpdir({
  805. init: async (dir) => {
  806. await Bun.write(
  807. path.join(dir, "opencode.json"),
  808. JSON.stringify({
  809. $schema: "https://opencode.ai/config.json",
  810. provider: {
  811. anthropic: {
  812. models: {
  813. "claude-sonnet-4-20250514": {
  814. name: "Custom Name for Sonnet",
  815. },
  816. },
  817. },
  818. },
  819. }),
  820. )
  821. },
  822. })
  823. await Instance.provide({
  824. directory: tmp.path,
  825. init: async () => {
  826. set("ANTHROPIC_API_KEY", "test-api-key")
  827. },
  828. fn: async () => {
  829. const providers = await list()
  830. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  831. expect(model.name).toBe("Custom Name for Sonnet")
  832. expect(model.capabilities.toolcall).toBe(true)
  833. expect(model.capabilities.attachment).toBe(true)
  834. expect(model.limit.context).toBeGreaterThan(0)
  835. },
  836. })
  837. })
  838. test("disabled_providers prevents loading even with env var", async () => {
  839. await using tmp = await tmpdir({
  840. init: async (dir) => {
  841. await Bun.write(
  842. path.join(dir, "opencode.json"),
  843. JSON.stringify({
  844. $schema: "https://opencode.ai/config.json",
  845. disabled_providers: ["openai"],
  846. }),
  847. )
  848. },
  849. })
  850. await Instance.provide({
  851. directory: tmp.path,
  852. init: async () => {
  853. set("OPENAI_API_KEY", "test-openai-key")
  854. },
  855. fn: async () => {
  856. const providers = await list()
  857. expect(providers[ProviderID.openai]).toBeUndefined()
  858. },
  859. })
  860. })
  861. test("enabled_providers with empty array allows no providers", async () => {
  862. await using tmp = await tmpdir({
  863. init: async (dir) => {
  864. await Bun.write(
  865. path.join(dir, "opencode.json"),
  866. JSON.stringify({
  867. $schema: "https://opencode.ai/config.json",
  868. enabled_providers: [],
  869. }),
  870. )
  871. },
  872. })
  873. await Instance.provide({
  874. directory: tmp.path,
  875. init: async () => {
  876. set("ANTHROPIC_API_KEY", "test-api-key")
  877. set("OPENAI_API_KEY", "test-openai-key")
  878. },
  879. fn: async () => {
  880. const providers = await list()
  881. expect(Object.keys(providers).length).toBe(0)
  882. },
  883. })
  884. })
  885. test("whitelist and blacklist can be combined", async () => {
  886. await using tmp = await tmpdir({
  887. init: async (dir) => {
  888. await Bun.write(
  889. path.join(dir, "opencode.json"),
  890. JSON.stringify({
  891. $schema: "https://opencode.ai/config.json",
  892. provider: {
  893. anthropic: {
  894. whitelist: ["claude-sonnet-4-20250514", "claude-opus-4-20250514"],
  895. blacklist: ["claude-opus-4-20250514"],
  896. },
  897. },
  898. }),
  899. )
  900. },
  901. })
  902. await Instance.provide({
  903. directory: tmp.path,
  904. init: async () => {
  905. set("ANTHROPIC_API_KEY", "test-api-key")
  906. },
  907. fn: async () => {
  908. const providers = await list()
  909. expect(providers[ProviderID.anthropic]).toBeDefined()
  910. const models = Object.keys(providers[ProviderID.anthropic].models)
  911. expect(models).toContain("claude-sonnet-4-20250514")
  912. expect(models).not.toContain("claude-opus-4-20250514")
  913. expect(models.length).toBe(1)
  914. },
  915. })
  916. })
  917. test("model modalities default correctly", async () => {
  918. await using tmp = await tmpdir({
  919. init: async (dir) => {
  920. await Bun.write(
  921. path.join(dir, "opencode.json"),
  922. JSON.stringify({
  923. $schema: "https://opencode.ai/config.json",
  924. provider: {
  925. "test-provider": {
  926. name: "Test",
  927. npm: "@ai-sdk/openai-compatible",
  928. env: [],
  929. models: {
  930. "test-model": {
  931. name: "Test Model",
  932. tool_call: true,
  933. limit: { context: 8000, output: 2000 },
  934. },
  935. },
  936. options: { apiKey: "test" },
  937. },
  938. },
  939. }),
  940. )
  941. },
  942. })
  943. await Instance.provide({
  944. directory: tmp.path,
  945. fn: async () => {
  946. const providers = await list()
  947. const model = providers[ProviderID.make("test-provider")].models["test-model"]
  948. expect(model.capabilities.input.text).toBe(true)
  949. expect(model.capabilities.output.text).toBe(true)
  950. },
  951. })
  952. })
  953. test("model with custom cost values", async () => {
  954. await using tmp = await tmpdir({
  955. init: async (dir) => {
  956. await Bun.write(
  957. path.join(dir, "opencode.json"),
  958. JSON.stringify({
  959. $schema: "https://opencode.ai/config.json",
  960. provider: {
  961. "test-provider": {
  962. name: "Test",
  963. npm: "@ai-sdk/openai-compatible",
  964. env: [],
  965. models: {
  966. "test-model": {
  967. name: "Test Model",
  968. tool_call: true,
  969. limit: { context: 8000, output: 2000 },
  970. cost: {
  971. input: 5,
  972. output: 15,
  973. cache_read: 2.5,
  974. cache_write: 7.5,
  975. },
  976. },
  977. },
  978. options: { apiKey: "test" },
  979. },
  980. },
  981. }),
  982. )
  983. },
  984. })
  985. await Instance.provide({
  986. directory: tmp.path,
  987. fn: async () => {
  988. const providers = await list()
  989. const model = providers[ProviderID.make("test-provider")].models["test-model"]
  990. expect(model.cost.input).toBe(5)
  991. expect(model.cost.output).toBe(15)
  992. expect(model.cost.cache.read).toBe(2.5)
  993. expect(model.cost.cache.write).toBe(7.5)
  994. },
  995. })
  996. })
  997. test("getSmallModel returns appropriate small model", async () => {
  998. await using tmp = await tmpdir({
  999. init: async (dir) => {
  1000. await Bun.write(
  1001. path.join(dir, "opencode.json"),
  1002. JSON.stringify({
  1003. $schema: "https://opencode.ai/config.json",
  1004. }),
  1005. )
  1006. },
  1007. })
  1008. await Instance.provide({
  1009. directory: tmp.path,
  1010. init: async () => {
  1011. set("ANTHROPIC_API_KEY", "test-api-key")
  1012. },
  1013. fn: async () => {
  1014. const model = await getSmallModel(ProviderID.anthropic)
  1015. expect(model).toBeDefined()
  1016. expect(model?.id).toContain("haiku")
  1017. },
  1018. })
  1019. })
  1020. test("getSmallModel respects config small_model override", async () => {
  1021. await using tmp = await tmpdir({
  1022. init: async (dir) => {
  1023. await Bun.write(
  1024. path.join(dir, "opencode.json"),
  1025. JSON.stringify({
  1026. $schema: "https://opencode.ai/config.json",
  1027. small_model: "anthropic/claude-sonnet-4-20250514",
  1028. }),
  1029. )
  1030. },
  1031. })
  1032. await Instance.provide({
  1033. directory: tmp.path,
  1034. init: async () => {
  1035. set("ANTHROPIC_API_KEY", "test-api-key")
  1036. },
  1037. fn: async () => {
  1038. const model = await getSmallModel(ProviderID.anthropic)
  1039. expect(model).toBeDefined()
  1040. expect(String(model?.providerID)).toBe("anthropic")
  1041. expect(String(model?.id)).toBe("claude-sonnet-4-20250514")
  1042. },
  1043. })
  1044. })
  1045. test("provider.sort prioritizes preferred models", () => {
  1046. const models = [
  1047. { id: "random-model", name: "Random" },
  1048. { id: "claude-sonnet-4-latest", name: "Claude Sonnet 4" },
  1049. { id: "gpt-5-turbo", name: "GPT-5 Turbo" },
  1050. { id: "other-model", name: "Other" },
  1051. ] as any[]
  1052. const sorted = Provider.sort(models)
  1053. expect(sorted[0].id).toContain("sonnet-4")
  1054. expect(sorted[0].id).toContain("latest")
  1055. expect(sorted[sorted.length - 1].id).not.toContain("gpt-5")
  1056. expect(sorted[sorted.length - 1].id).not.toContain("sonnet-4")
  1057. })
  1058. test("multiple providers can be configured simultaneously", async () => {
  1059. await using tmp = await tmpdir({
  1060. init: async (dir) => {
  1061. await Bun.write(
  1062. path.join(dir, "opencode.json"),
  1063. JSON.stringify({
  1064. $schema: "https://opencode.ai/config.json",
  1065. provider: {
  1066. anthropic: {
  1067. options: { timeout: 30000 },
  1068. },
  1069. openai: {
  1070. options: { timeout: 60000 },
  1071. },
  1072. },
  1073. }),
  1074. )
  1075. },
  1076. })
  1077. await Instance.provide({
  1078. directory: tmp.path,
  1079. init: async () => {
  1080. set("ANTHROPIC_API_KEY", "test-anthropic-key")
  1081. set("OPENAI_API_KEY", "test-openai-key")
  1082. },
  1083. fn: async () => {
  1084. const providers = await list()
  1085. expect(providers[ProviderID.anthropic]).toBeDefined()
  1086. expect(providers[ProviderID.openai]).toBeDefined()
  1087. expect(providers[ProviderID.anthropic].options.timeout).toBe(30000)
  1088. expect(providers[ProviderID.openai].options.timeout).toBe(60000)
  1089. },
  1090. })
  1091. })
  1092. test("provider with custom npm package", async () => {
  1093. await using tmp = await tmpdir({
  1094. init: async (dir) => {
  1095. await Bun.write(
  1096. path.join(dir, "opencode.json"),
  1097. JSON.stringify({
  1098. $schema: "https://opencode.ai/config.json",
  1099. provider: {
  1100. "local-llm": {
  1101. name: "Local LLM",
  1102. npm: "@ai-sdk/openai-compatible",
  1103. env: [],
  1104. models: {
  1105. "llama-3": {
  1106. name: "Llama 3",
  1107. tool_call: true,
  1108. limit: { context: 8192, output: 2048 },
  1109. },
  1110. },
  1111. options: {
  1112. apiKey: "not-needed",
  1113. baseURL: "http://localhost:11434/v1",
  1114. },
  1115. },
  1116. },
  1117. }),
  1118. )
  1119. },
  1120. })
  1121. await Instance.provide({
  1122. directory: tmp.path,
  1123. fn: async () => {
  1124. const providers = await list()
  1125. expect(providers[ProviderID.make("local-llm")]).toBeDefined()
  1126. expect(providers[ProviderID.make("local-llm")].models["llama-3"].api.npm).toBe("@ai-sdk/openai-compatible")
  1127. expect(providers[ProviderID.make("local-llm")].options.baseURL).toBe("http://localhost:11434/v1")
  1128. },
  1129. })
  1130. })
  1131. // Edge cases for model configuration
  1132. test("model alias name defaults to alias key when id differs", async () => {
  1133. await using tmp = await tmpdir({
  1134. init: async (dir) => {
  1135. await Bun.write(
  1136. path.join(dir, "opencode.json"),
  1137. JSON.stringify({
  1138. $schema: "https://opencode.ai/config.json",
  1139. provider: {
  1140. anthropic: {
  1141. models: {
  1142. sonnet: {
  1143. id: "claude-sonnet-4-20250514",
  1144. // no name specified - should default to "sonnet" (the key)
  1145. },
  1146. },
  1147. },
  1148. },
  1149. }),
  1150. )
  1151. },
  1152. })
  1153. await Instance.provide({
  1154. directory: tmp.path,
  1155. init: async () => {
  1156. set("ANTHROPIC_API_KEY", "test-api-key")
  1157. },
  1158. fn: async () => {
  1159. const providers = await list()
  1160. expect(providers[ProviderID.anthropic].models["sonnet"].name).toBe("sonnet")
  1161. },
  1162. })
  1163. })
  1164. test("provider with multiple env var options only includes apiKey when single env", async () => {
  1165. await using tmp = await tmpdir({
  1166. init: async (dir) => {
  1167. await Bun.write(
  1168. path.join(dir, "opencode.json"),
  1169. JSON.stringify({
  1170. $schema: "https://opencode.ai/config.json",
  1171. provider: {
  1172. "multi-env": {
  1173. name: "Multi Env Provider",
  1174. npm: "@ai-sdk/openai-compatible",
  1175. env: ["MULTI_ENV_KEY_1", "MULTI_ENV_KEY_2"],
  1176. models: {
  1177. "model-1": {
  1178. name: "Model 1",
  1179. tool_call: true,
  1180. limit: { context: 8000, output: 2000 },
  1181. },
  1182. },
  1183. options: {
  1184. baseURL: "https://api.example.com/v1",
  1185. },
  1186. },
  1187. },
  1188. }),
  1189. )
  1190. },
  1191. })
  1192. await Instance.provide({
  1193. directory: tmp.path,
  1194. init: async () => {
  1195. set("MULTI_ENV_KEY_1", "test-key")
  1196. },
  1197. fn: async () => {
  1198. const providers = await list()
  1199. expect(providers[ProviderID.make("multi-env")]).toBeDefined()
  1200. // When multiple env options exist, key should NOT be auto-set
  1201. expect(providers[ProviderID.make("multi-env")].key).toBeUndefined()
  1202. },
  1203. })
  1204. })
  1205. test("provider with single env var includes apiKey automatically", async () => {
  1206. await using tmp = await tmpdir({
  1207. init: async (dir) => {
  1208. await Bun.write(
  1209. path.join(dir, "opencode.json"),
  1210. JSON.stringify({
  1211. $schema: "https://opencode.ai/config.json",
  1212. provider: {
  1213. "single-env": {
  1214. name: "Single Env Provider",
  1215. npm: "@ai-sdk/openai-compatible",
  1216. env: ["SINGLE_ENV_KEY"],
  1217. models: {
  1218. "model-1": {
  1219. name: "Model 1",
  1220. tool_call: true,
  1221. limit: { context: 8000, output: 2000 },
  1222. },
  1223. },
  1224. options: {
  1225. baseURL: "https://api.example.com/v1",
  1226. },
  1227. },
  1228. },
  1229. }),
  1230. )
  1231. },
  1232. })
  1233. await Instance.provide({
  1234. directory: tmp.path,
  1235. init: async () => {
  1236. set("SINGLE_ENV_KEY", "my-api-key")
  1237. },
  1238. fn: async () => {
  1239. const providers = await list()
  1240. expect(providers[ProviderID.make("single-env")]).toBeDefined()
  1241. // Single env option should auto-set key
  1242. expect(providers[ProviderID.make("single-env")].key).toBe("my-api-key")
  1243. },
  1244. })
  1245. })
  1246. test("model cost overrides existing cost values", async () => {
  1247. await using tmp = await tmpdir({
  1248. init: async (dir) => {
  1249. await Bun.write(
  1250. path.join(dir, "opencode.json"),
  1251. JSON.stringify({
  1252. $schema: "https://opencode.ai/config.json",
  1253. provider: {
  1254. anthropic: {
  1255. models: {
  1256. "claude-sonnet-4-20250514": {
  1257. cost: {
  1258. input: 999,
  1259. output: 888,
  1260. },
  1261. },
  1262. },
  1263. },
  1264. },
  1265. }),
  1266. )
  1267. },
  1268. })
  1269. await Instance.provide({
  1270. directory: tmp.path,
  1271. init: async () => {
  1272. set("ANTHROPIC_API_KEY", "test-api-key")
  1273. },
  1274. fn: async () => {
  1275. const providers = await list()
  1276. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1277. expect(model.cost.input).toBe(999)
  1278. expect(model.cost.output).toBe(888)
  1279. },
  1280. })
  1281. })
  1282. test("completely new provider not in database can be configured", async () => {
  1283. await using tmp = await tmpdir({
  1284. init: async (dir) => {
  1285. await Bun.write(
  1286. path.join(dir, "opencode.json"),
  1287. JSON.stringify({
  1288. $schema: "https://opencode.ai/config.json",
  1289. provider: {
  1290. "brand-new-provider": {
  1291. name: "Brand New",
  1292. npm: "@ai-sdk/openai-compatible",
  1293. env: [],
  1294. api: "https://new-api.com/v1",
  1295. models: {
  1296. "new-model": {
  1297. name: "New Model",
  1298. tool_call: true,
  1299. reasoning: true,
  1300. attachment: true,
  1301. temperature: true,
  1302. limit: { context: 32000, output: 8000 },
  1303. modalities: {
  1304. input: ["text", "image"],
  1305. output: ["text"],
  1306. },
  1307. },
  1308. },
  1309. options: {
  1310. apiKey: "new-key",
  1311. },
  1312. },
  1313. },
  1314. }),
  1315. )
  1316. },
  1317. })
  1318. await Instance.provide({
  1319. directory: tmp.path,
  1320. fn: async () => {
  1321. const providers = await list()
  1322. expect(providers[ProviderID.make("brand-new-provider")]).toBeDefined()
  1323. expect(providers[ProviderID.make("brand-new-provider")].name).toBe("Brand New")
  1324. const model = providers[ProviderID.make("brand-new-provider")].models["new-model"]
  1325. expect(model.capabilities.reasoning).toBe(true)
  1326. expect(model.capabilities.attachment).toBe(true)
  1327. expect(model.capabilities.input.image).toBe(true)
  1328. },
  1329. })
  1330. })
  1331. test("disabled_providers and enabled_providers interaction", async () => {
  1332. await using tmp = await tmpdir({
  1333. init: async (dir) => {
  1334. await Bun.write(
  1335. path.join(dir, "opencode.json"),
  1336. JSON.stringify({
  1337. $schema: "https://opencode.ai/config.json",
  1338. // enabled_providers takes precedence - only these are considered
  1339. enabled_providers: ["anthropic", "openai"],
  1340. // Then disabled_providers filters from the enabled set
  1341. disabled_providers: ["openai"],
  1342. }),
  1343. )
  1344. },
  1345. })
  1346. await Instance.provide({
  1347. directory: tmp.path,
  1348. init: async () => {
  1349. set("ANTHROPIC_API_KEY", "test-anthropic")
  1350. set("OPENAI_API_KEY", "test-openai")
  1351. set("GOOGLE_GENERATIVE_AI_API_KEY", "test-google")
  1352. },
  1353. fn: async () => {
  1354. const providers = await list()
  1355. // anthropic: in enabled, not in disabled = allowed
  1356. expect(providers[ProviderID.anthropic]).toBeDefined()
  1357. // openai: in enabled, but also in disabled = NOT allowed
  1358. expect(providers[ProviderID.openai]).toBeUndefined()
  1359. // google: not in enabled = NOT allowed (even though not disabled)
  1360. expect(providers[ProviderID.google]).toBeUndefined()
  1361. },
  1362. })
  1363. })
  1364. test("model with tool_call false", async () => {
  1365. await using tmp = await tmpdir({
  1366. init: async (dir) => {
  1367. await Bun.write(
  1368. path.join(dir, "opencode.json"),
  1369. JSON.stringify({
  1370. $schema: "https://opencode.ai/config.json",
  1371. provider: {
  1372. "no-tools": {
  1373. name: "No Tools Provider",
  1374. npm: "@ai-sdk/openai-compatible",
  1375. env: [],
  1376. models: {
  1377. "basic-model": {
  1378. name: "Basic Model",
  1379. tool_call: false,
  1380. limit: { context: 4000, output: 1000 },
  1381. },
  1382. },
  1383. options: { apiKey: "test" },
  1384. },
  1385. },
  1386. }),
  1387. )
  1388. },
  1389. })
  1390. await Instance.provide({
  1391. directory: tmp.path,
  1392. fn: async () => {
  1393. const providers = await list()
  1394. expect(providers[ProviderID.make("no-tools")].models["basic-model"].capabilities.toolcall).toBe(false)
  1395. },
  1396. })
  1397. })
  1398. test("model defaults tool_call to true when not specified", async () => {
  1399. await using tmp = await tmpdir({
  1400. init: async (dir) => {
  1401. await Bun.write(
  1402. path.join(dir, "opencode.json"),
  1403. JSON.stringify({
  1404. $schema: "https://opencode.ai/config.json",
  1405. provider: {
  1406. "default-tools": {
  1407. name: "Default Tools Provider",
  1408. npm: "@ai-sdk/openai-compatible",
  1409. env: [],
  1410. models: {
  1411. model: {
  1412. name: "Model",
  1413. // tool_call not specified
  1414. limit: { context: 4000, output: 1000 },
  1415. },
  1416. },
  1417. options: { apiKey: "test" },
  1418. },
  1419. },
  1420. }),
  1421. )
  1422. },
  1423. })
  1424. await Instance.provide({
  1425. directory: tmp.path,
  1426. fn: async () => {
  1427. const providers = await list()
  1428. expect(providers[ProviderID.make("default-tools")].models["model"].capabilities.toolcall).toBe(true)
  1429. },
  1430. })
  1431. })
  1432. test("model headers are preserved", async () => {
  1433. await using tmp = await tmpdir({
  1434. init: async (dir) => {
  1435. await Bun.write(
  1436. path.join(dir, "opencode.json"),
  1437. JSON.stringify({
  1438. $schema: "https://opencode.ai/config.json",
  1439. provider: {
  1440. "headers-provider": {
  1441. name: "Headers Provider",
  1442. npm: "@ai-sdk/openai-compatible",
  1443. env: [],
  1444. models: {
  1445. model: {
  1446. name: "Model",
  1447. tool_call: true,
  1448. limit: { context: 4000, output: 1000 },
  1449. headers: {
  1450. "X-Custom-Header": "custom-value",
  1451. Authorization: "Bearer special-token",
  1452. },
  1453. },
  1454. },
  1455. options: { apiKey: "test" },
  1456. },
  1457. },
  1458. }),
  1459. )
  1460. },
  1461. })
  1462. await Instance.provide({
  1463. directory: tmp.path,
  1464. fn: async () => {
  1465. const providers = await list()
  1466. const model = providers[ProviderID.make("headers-provider")].models["model"]
  1467. expect(model.headers).toEqual({
  1468. "X-Custom-Header": "custom-value",
  1469. Authorization: "Bearer special-token",
  1470. })
  1471. },
  1472. })
  1473. })
  1474. test("provider env fallback - second env var used if first missing", async () => {
  1475. await using tmp = await tmpdir({
  1476. init: async (dir) => {
  1477. await Bun.write(
  1478. path.join(dir, "opencode.json"),
  1479. JSON.stringify({
  1480. $schema: "https://opencode.ai/config.json",
  1481. provider: {
  1482. "fallback-env": {
  1483. name: "Fallback Env Provider",
  1484. npm: "@ai-sdk/openai-compatible",
  1485. env: ["PRIMARY_KEY", "FALLBACK_KEY"],
  1486. models: {
  1487. model: {
  1488. name: "Model",
  1489. tool_call: true,
  1490. limit: { context: 4000, output: 1000 },
  1491. },
  1492. },
  1493. options: { baseURL: "https://api.example.com" },
  1494. },
  1495. },
  1496. }),
  1497. )
  1498. },
  1499. })
  1500. await Instance.provide({
  1501. directory: tmp.path,
  1502. init: async () => {
  1503. // Only set fallback, not primary
  1504. set("FALLBACK_KEY", "fallback-api-key")
  1505. },
  1506. fn: async () => {
  1507. const providers = await list()
  1508. // Provider should load because fallback env var is set
  1509. expect(providers[ProviderID.make("fallback-env")]).toBeDefined()
  1510. },
  1511. })
  1512. })
  1513. test("getModel returns consistent results", async () => {
  1514. await using tmp = await tmpdir({
  1515. init: async (dir) => {
  1516. await Bun.write(
  1517. path.join(dir, "opencode.json"),
  1518. JSON.stringify({
  1519. $schema: "https://opencode.ai/config.json",
  1520. }),
  1521. )
  1522. },
  1523. })
  1524. await Instance.provide({
  1525. directory: tmp.path,
  1526. init: async () => {
  1527. set("ANTHROPIC_API_KEY", "test-api-key")
  1528. },
  1529. fn: async () => {
  1530. const model1 = await getModel(ProviderID.anthropic, ModelID.make("claude-sonnet-4-20250514"))
  1531. const model2 = await getModel(ProviderID.anthropic, ModelID.make("claude-sonnet-4-20250514"))
  1532. expect(model1.providerID).toEqual(model2.providerID)
  1533. expect(model1.id).toEqual(model2.id)
  1534. expect(model1).toEqual(model2)
  1535. },
  1536. })
  1537. })
  1538. test("provider name defaults to id when not in database", async () => {
  1539. await using tmp = await tmpdir({
  1540. init: async (dir) => {
  1541. await Bun.write(
  1542. path.join(dir, "opencode.json"),
  1543. JSON.stringify({
  1544. $schema: "https://opencode.ai/config.json",
  1545. provider: {
  1546. "my-custom-id": {
  1547. // no name specified
  1548. npm: "@ai-sdk/openai-compatible",
  1549. env: [],
  1550. models: {
  1551. model: {
  1552. name: "Model",
  1553. tool_call: true,
  1554. limit: { context: 4000, output: 1000 },
  1555. },
  1556. },
  1557. options: { apiKey: "test" },
  1558. },
  1559. },
  1560. }),
  1561. )
  1562. },
  1563. })
  1564. await Instance.provide({
  1565. directory: tmp.path,
  1566. fn: async () => {
  1567. const providers = await list()
  1568. expect(providers[ProviderID.make("my-custom-id")].name).toBe("my-custom-id")
  1569. },
  1570. })
  1571. })
  1572. test("ModelNotFoundError includes suggestions for typos", async () => {
  1573. await using tmp = await tmpdir({
  1574. init: async (dir) => {
  1575. await Bun.write(
  1576. path.join(dir, "opencode.json"),
  1577. JSON.stringify({
  1578. $schema: "https://opencode.ai/config.json",
  1579. }),
  1580. )
  1581. },
  1582. })
  1583. await Instance.provide({
  1584. directory: tmp.path,
  1585. init: async () => {
  1586. set("ANTHROPIC_API_KEY", "test-api-key")
  1587. },
  1588. fn: async () => {
  1589. try {
  1590. await getModel(ProviderID.anthropic, ModelID.make("claude-sonet-4")) // typo: sonet instead of sonnet
  1591. expect(true).toBe(false) // Should not reach here
  1592. } catch (e: any) {
  1593. expect(e.data.suggestions).toBeDefined()
  1594. expect(e.data.suggestions.length).toBeGreaterThan(0)
  1595. }
  1596. },
  1597. })
  1598. })
  1599. test("ModelNotFoundError for provider includes suggestions", async () => {
  1600. await using tmp = await tmpdir({
  1601. init: async (dir) => {
  1602. await Bun.write(
  1603. path.join(dir, "opencode.json"),
  1604. JSON.stringify({
  1605. $schema: "https://opencode.ai/config.json",
  1606. }),
  1607. )
  1608. },
  1609. })
  1610. await Instance.provide({
  1611. directory: tmp.path,
  1612. init: async () => {
  1613. set("ANTHROPIC_API_KEY", "test-api-key")
  1614. },
  1615. fn: async () => {
  1616. try {
  1617. await getModel(ProviderID.make("antropic"), ModelID.make("claude-sonnet-4")) // typo: antropic
  1618. expect(true).toBe(false) // Should not reach here
  1619. } catch (e: any) {
  1620. expect(e.data.suggestions).toBeDefined()
  1621. expect(e.data.suggestions).toContain("anthropic")
  1622. }
  1623. },
  1624. })
  1625. })
  1626. test("getProvider returns undefined for nonexistent provider", async () => {
  1627. await using tmp = await tmpdir({
  1628. init: async (dir) => {
  1629. await Bun.write(
  1630. path.join(dir, "opencode.json"),
  1631. JSON.stringify({
  1632. $schema: "https://opencode.ai/config.json",
  1633. }),
  1634. )
  1635. },
  1636. })
  1637. await Instance.provide({
  1638. directory: tmp.path,
  1639. fn: async () => {
  1640. const provider = await getProvider(ProviderID.make("nonexistent"))
  1641. expect(provider).toBeUndefined()
  1642. },
  1643. })
  1644. })
  1645. test("getProvider returns provider info", async () => {
  1646. await using tmp = await tmpdir({
  1647. init: async (dir) => {
  1648. await Bun.write(
  1649. path.join(dir, "opencode.json"),
  1650. JSON.stringify({
  1651. $schema: "https://opencode.ai/config.json",
  1652. }),
  1653. )
  1654. },
  1655. })
  1656. await Instance.provide({
  1657. directory: tmp.path,
  1658. init: async () => {
  1659. set("ANTHROPIC_API_KEY", "test-api-key")
  1660. },
  1661. fn: async () => {
  1662. const provider = await getProvider(ProviderID.anthropic)
  1663. expect(provider).toBeDefined()
  1664. expect(String(provider?.id)).toBe("anthropic")
  1665. },
  1666. })
  1667. })
  1668. test("closest returns undefined when no partial match found", async () => {
  1669. await using tmp = await tmpdir({
  1670. init: async (dir) => {
  1671. await Bun.write(
  1672. path.join(dir, "opencode.json"),
  1673. JSON.stringify({
  1674. $schema: "https://opencode.ai/config.json",
  1675. }),
  1676. )
  1677. },
  1678. })
  1679. await Instance.provide({
  1680. directory: tmp.path,
  1681. init: async () => {
  1682. set("ANTHROPIC_API_KEY", "test-api-key")
  1683. },
  1684. fn: async () => {
  1685. const result = await closest(ProviderID.anthropic, ["nonexistent-xyz-model"])
  1686. expect(result).toBeUndefined()
  1687. },
  1688. })
  1689. })
  1690. test("closest checks multiple query terms in order", async () => {
  1691. await using tmp = await tmpdir({
  1692. init: async (dir) => {
  1693. await Bun.write(
  1694. path.join(dir, "opencode.json"),
  1695. JSON.stringify({
  1696. $schema: "https://opencode.ai/config.json",
  1697. }),
  1698. )
  1699. },
  1700. })
  1701. await Instance.provide({
  1702. directory: tmp.path,
  1703. init: async () => {
  1704. set("ANTHROPIC_API_KEY", "test-api-key")
  1705. },
  1706. fn: async () => {
  1707. // First term won't match, second will
  1708. const result = await closest(ProviderID.anthropic, ["nonexistent", "haiku"])
  1709. expect(result).toBeDefined()
  1710. expect(result?.modelID).toContain("haiku")
  1711. },
  1712. })
  1713. })
  1714. test("model limit defaults to zero when not specified", async () => {
  1715. await using tmp = await tmpdir({
  1716. init: async (dir) => {
  1717. await Bun.write(
  1718. path.join(dir, "opencode.json"),
  1719. JSON.stringify({
  1720. $schema: "https://opencode.ai/config.json",
  1721. provider: {
  1722. "no-limit": {
  1723. name: "No Limit Provider",
  1724. npm: "@ai-sdk/openai-compatible",
  1725. env: [],
  1726. models: {
  1727. model: {
  1728. name: "Model",
  1729. tool_call: true,
  1730. // no limit specified
  1731. },
  1732. },
  1733. options: { apiKey: "test" },
  1734. },
  1735. },
  1736. }),
  1737. )
  1738. },
  1739. })
  1740. await Instance.provide({
  1741. directory: tmp.path,
  1742. fn: async () => {
  1743. const providers = await list()
  1744. const model = providers[ProviderID.make("no-limit")].models["model"]
  1745. expect(model.limit.context).toBe(0)
  1746. expect(model.limit.output).toBe(0)
  1747. },
  1748. })
  1749. })
  1750. test("provider options are deeply merged", async () => {
  1751. await using tmp = await tmpdir({
  1752. init: async (dir) => {
  1753. await Bun.write(
  1754. path.join(dir, "opencode.json"),
  1755. JSON.stringify({
  1756. $schema: "https://opencode.ai/config.json",
  1757. provider: {
  1758. anthropic: {
  1759. options: {
  1760. headers: {
  1761. "X-Custom": "custom-value",
  1762. },
  1763. timeout: 30000,
  1764. },
  1765. },
  1766. },
  1767. }),
  1768. )
  1769. },
  1770. })
  1771. await Instance.provide({
  1772. directory: tmp.path,
  1773. init: async () => {
  1774. set("ANTHROPIC_API_KEY", "test-api-key")
  1775. },
  1776. fn: async () => {
  1777. const providers = await list()
  1778. // Custom options should be merged
  1779. expect(providers[ProviderID.anthropic].options.timeout).toBe(30000)
  1780. expect(providers[ProviderID.anthropic].options.headers["X-Custom"]).toBe("custom-value")
  1781. // anthropic custom loader adds its own headers, they should coexist
  1782. expect(providers[ProviderID.anthropic].options.headers["anthropic-beta"]).toBeDefined()
  1783. },
  1784. })
  1785. })
  1786. test("custom model inherits npm package from models.dev provider config", async () => {
  1787. await using tmp = await tmpdir({
  1788. init: async (dir) => {
  1789. await Bun.write(
  1790. path.join(dir, "opencode.json"),
  1791. JSON.stringify({
  1792. $schema: "https://opencode.ai/config.json",
  1793. provider: {
  1794. openai: {
  1795. models: {
  1796. "my-custom-model": {
  1797. name: "My Custom Model",
  1798. tool_call: true,
  1799. limit: { context: 8000, output: 2000 },
  1800. },
  1801. },
  1802. },
  1803. },
  1804. }),
  1805. )
  1806. },
  1807. })
  1808. await Instance.provide({
  1809. directory: tmp.path,
  1810. init: async () => {
  1811. set("OPENAI_API_KEY", "test-api-key")
  1812. },
  1813. fn: async () => {
  1814. const providers = await list()
  1815. const model = providers[ProviderID.openai].models["my-custom-model"]
  1816. expect(model).toBeDefined()
  1817. expect(model.api.npm).toBe("@ai-sdk/openai")
  1818. },
  1819. })
  1820. })
  1821. test("custom model inherits api.url from models.dev provider", async () => {
  1822. await using tmp = await tmpdir({
  1823. init: async (dir) => {
  1824. await Bun.write(
  1825. path.join(dir, "opencode.json"),
  1826. JSON.stringify({
  1827. $schema: "https://opencode.ai/config.json",
  1828. provider: {
  1829. openrouter: {
  1830. models: {
  1831. "prime-intellect/intellect-3": {},
  1832. "deepseek/deepseek-r1-0528": {
  1833. name: "DeepSeek R1",
  1834. },
  1835. },
  1836. },
  1837. },
  1838. }),
  1839. )
  1840. },
  1841. })
  1842. await Instance.provide({
  1843. directory: tmp.path,
  1844. init: async () => {
  1845. set("OPENROUTER_API_KEY", "test-api-key")
  1846. },
  1847. fn: async () => {
  1848. const providers = await list()
  1849. expect(providers[ProviderID.openrouter]).toBeDefined()
  1850. // New model not in database should inherit api.url from provider
  1851. const intellect = providers[ProviderID.openrouter].models["prime-intellect/intellect-3"]
  1852. expect(intellect).toBeDefined()
  1853. expect(intellect.api.url).toBe("https://openrouter.ai/api/v1")
  1854. // Another new model should also inherit api.url
  1855. const deepseek = providers[ProviderID.openrouter].models["deepseek/deepseek-r1-0528"]
  1856. expect(deepseek).toBeDefined()
  1857. expect(deepseek.api.url).toBe("https://openrouter.ai/api/v1")
  1858. expect(deepseek.name).toBe("DeepSeek R1")
  1859. },
  1860. })
  1861. })
  1862. test("mode cost preserves over-200k pricing from base model", () => {
  1863. const provider = {
  1864. id: "openai",
  1865. name: "OpenAI",
  1866. env: [],
  1867. api: "https://api.openai.com/v1",
  1868. models: {
  1869. "gpt-5.4": {
  1870. id: "gpt-5.4",
  1871. name: "GPT-5.4",
  1872. family: "gpt",
  1873. release_date: "2026-03-05",
  1874. attachment: true,
  1875. reasoning: true,
  1876. temperature: false,
  1877. tool_call: true,
  1878. cost: {
  1879. input: 2.5,
  1880. output: 15,
  1881. cache_read: 0.25,
  1882. context_over_200k: {
  1883. input: 5,
  1884. output: 22.5,
  1885. cache_read: 0.5,
  1886. },
  1887. },
  1888. limit: {
  1889. context: 1_050_000,
  1890. input: 922_000,
  1891. output: 128_000,
  1892. },
  1893. experimental: {
  1894. modes: {
  1895. fast: {
  1896. cost: {
  1897. input: 5,
  1898. output: 30,
  1899. cache_read: 0.5,
  1900. },
  1901. provider: {
  1902. body: {
  1903. service_tier: "priority",
  1904. },
  1905. },
  1906. },
  1907. },
  1908. },
  1909. },
  1910. },
  1911. } as unknown as ModelsDev.Provider
  1912. const model = Provider.fromModelsDevProvider(provider).models["gpt-5.4-fast"]
  1913. expect(model.cost.input).toEqual(5)
  1914. expect(model.cost.output).toEqual(30)
  1915. expect(model.cost.cache.read).toEqual(0.5)
  1916. expect(model.cost.cache.write).toEqual(0)
  1917. expect(model.options["serviceTier"]).toEqual("priority")
  1918. expect(model.cost.experimentalOver200K).toEqual({
  1919. input: 5,
  1920. output: 22.5,
  1921. cache: {
  1922. read: 0.5,
  1923. write: 0,
  1924. },
  1925. })
  1926. })
  1927. test("models.dev normalization fills required response fields", () => {
  1928. const provider = {
  1929. id: "gateway",
  1930. name: "Gateway",
  1931. env: [],
  1932. models: {
  1933. "gpt-5.4": {
  1934. id: "gpt-5.4",
  1935. name: "GPT-5.4",
  1936. family: "gpt",
  1937. cost: {
  1938. input: 2.5,
  1939. output: 15,
  1940. },
  1941. limit: {
  1942. context: 1_050_000,
  1943. input: 922_000,
  1944. output: 128_000,
  1945. },
  1946. },
  1947. },
  1948. } as unknown as ModelsDev.Provider
  1949. const model = Provider.fromModelsDevProvider(provider).models["gpt-5.4"]
  1950. expect(model.api.url).toBe("")
  1951. expect(model.capabilities.temperature).toBe(false)
  1952. expect(model.capabilities.reasoning).toBe(false)
  1953. expect(model.capabilities.attachment).toBe(false)
  1954. expect(model.capabilities.toolcall).toBe(true)
  1955. expect(model.release_date).toBe("")
  1956. })
  1957. test("model variants are generated for reasoning models", async () => {
  1958. await using tmp = await tmpdir({
  1959. init: async (dir) => {
  1960. await Bun.write(
  1961. path.join(dir, "opencode.json"),
  1962. JSON.stringify({
  1963. $schema: "https://opencode.ai/config.json",
  1964. }),
  1965. )
  1966. },
  1967. })
  1968. await Instance.provide({
  1969. directory: tmp.path,
  1970. init: async () => {
  1971. set("ANTHROPIC_API_KEY", "test-api-key")
  1972. },
  1973. fn: async () => {
  1974. const providers = await list()
  1975. // Claude sonnet 4 has reasoning capability
  1976. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  1977. expect(model.capabilities.reasoning).toBe(true)
  1978. expect(model.variants).toBeDefined()
  1979. expect(Object.keys(model.variants!).length).toBeGreaterThan(0)
  1980. },
  1981. })
  1982. })
  1983. test("model variants can be disabled via config", async () => {
  1984. await using tmp = await tmpdir({
  1985. init: async (dir) => {
  1986. await Bun.write(
  1987. path.join(dir, "opencode.json"),
  1988. JSON.stringify({
  1989. $schema: "https://opencode.ai/config.json",
  1990. provider: {
  1991. anthropic: {
  1992. models: {
  1993. "claude-sonnet-4-20250514": {
  1994. variants: {
  1995. high: { disabled: true },
  1996. },
  1997. },
  1998. },
  1999. },
  2000. },
  2001. }),
  2002. )
  2003. },
  2004. })
  2005. await Instance.provide({
  2006. directory: tmp.path,
  2007. init: async () => {
  2008. set("ANTHROPIC_API_KEY", "test-api-key")
  2009. },
  2010. fn: async () => {
  2011. const providers = await list()
  2012. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  2013. expect(model.variants).toBeDefined()
  2014. expect(model.variants!["high"]).toBeUndefined()
  2015. // max variant should still exist
  2016. expect(model.variants!["max"]).toBeDefined()
  2017. },
  2018. })
  2019. })
  2020. test("model variants can be customized via config", async () => {
  2021. await using tmp = await tmpdir({
  2022. init: async (dir) => {
  2023. await Bun.write(
  2024. path.join(dir, "opencode.json"),
  2025. JSON.stringify({
  2026. $schema: "https://opencode.ai/config.json",
  2027. provider: {
  2028. anthropic: {
  2029. models: {
  2030. "claude-sonnet-4-20250514": {
  2031. variants: {
  2032. high: {
  2033. thinking: {
  2034. type: "enabled",
  2035. budgetTokens: 20000,
  2036. },
  2037. },
  2038. },
  2039. },
  2040. },
  2041. },
  2042. },
  2043. }),
  2044. )
  2045. },
  2046. })
  2047. await Instance.provide({
  2048. directory: tmp.path,
  2049. init: async () => {
  2050. set("ANTHROPIC_API_KEY", "test-api-key")
  2051. },
  2052. fn: async () => {
  2053. const providers = await list()
  2054. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  2055. expect(model.variants!["high"]).toBeDefined()
  2056. expect(model.variants!["high"].thinking.budgetTokens).toBe(20000)
  2057. },
  2058. })
  2059. })
  2060. test("disabled key is stripped from variant config", async () => {
  2061. await using tmp = await tmpdir({
  2062. init: async (dir) => {
  2063. await Bun.write(
  2064. path.join(dir, "opencode.json"),
  2065. JSON.stringify({
  2066. $schema: "https://opencode.ai/config.json",
  2067. provider: {
  2068. anthropic: {
  2069. models: {
  2070. "claude-sonnet-4-20250514": {
  2071. variants: {
  2072. max: {
  2073. disabled: false,
  2074. customField: "test",
  2075. },
  2076. },
  2077. },
  2078. },
  2079. },
  2080. },
  2081. }),
  2082. )
  2083. },
  2084. })
  2085. await Instance.provide({
  2086. directory: tmp.path,
  2087. init: async () => {
  2088. set("ANTHROPIC_API_KEY", "test-api-key")
  2089. },
  2090. fn: async () => {
  2091. const providers = await list()
  2092. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  2093. expect(model.variants!["max"]).toBeDefined()
  2094. expect(model.variants!["max"].disabled).toBeUndefined()
  2095. expect(model.variants!["max"].customField).toBe("test")
  2096. },
  2097. })
  2098. })
  2099. test("all variants can be disabled via config", async () => {
  2100. await using tmp = await tmpdir({
  2101. init: async (dir) => {
  2102. await Bun.write(
  2103. path.join(dir, "opencode.json"),
  2104. JSON.stringify({
  2105. $schema: "https://opencode.ai/config.json",
  2106. provider: {
  2107. anthropic: {
  2108. models: {
  2109. "claude-sonnet-4-20250514": {
  2110. variants: {
  2111. high: { disabled: true },
  2112. max: { disabled: true },
  2113. },
  2114. },
  2115. },
  2116. },
  2117. },
  2118. }),
  2119. )
  2120. },
  2121. })
  2122. await Instance.provide({
  2123. directory: tmp.path,
  2124. init: async () => {
  2125. set("ANTHROPIC_API_KEY", "test-api-key")
  2126. },
  2127. fn: async () => {
  2128. const providers = await list()
  2129. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  2130. expect(model.variants).toBeDefined()
  2131. expect(Object.keys(model.variants!).length).toBe(0)
  2132. },
  2133. })
  2134. })
  2135. test("variant config merges with generated variants", async () => {
  2136. await using tmp = await tmpdir({
  2137. init: async (dir) => {
  2138. await Bun.write(
  2139. path.join(dir, "opencode.json"),
  2140. JSON.stringify({
  2141. $schema: "https://opencode.ai/config.json",
  2142. provider: {
  2143. anthropic: {
  2144. models: {
  2145. "claude-sonnet-4-20250514": {
  2146. variants: {
  2147. high: {
  2148. extraOption: "custom-value",
  2149. },
  2150. },
  2151. },
  2152. },
  2153. },
  2154. },
  2155. }),
  2156. )
  2157. },
  2158. })
  2159. await Instance.provide({
  2160. directory: tmp.path,
  2161. init: async () => {
  2162. set("ANTHROPIC_API_KEY", "test-api-key")
  2163. },
  2164. fn: async () => {
  2165. const providers = await list()
  2166. const model = providers[ProviderID.anthropic].models["claude-sonnet-4-20250514"]
  2167. expect(model.variants!["high"]).toBeDefined()
  2168. // Should have both the generated thinking config and the custom option
  2169. expect(model.variants!["high"].thinking).toBeDefined()
  2170. expect(model.variants!["high"].extraOption).toBe("custom-value")
  2171. },
  2172. })
  2173. })
  2174. test("variants filtered in second pass for database models", async () => {
  2175. await using tmp = await tmpdir({
  2176. init: async (dir) => {
  2177. await Bun.write(
  2178. path.join(dir, "opencode.json"),
  2179. JSON.stringify({
  2180. $schema: "https://opencode.ai/config.json",
  2181. provider: {
  2182. openai: {
  2183. models: {
  2184. "gpt-5": {
  2185. variants: {
  2186. high: { disabled: true },
  2187. },
  2188. },
  2189. },
  2190. },
  2191. },
  2192. }),
  2193. )
  2194. },
  2195. })
  2196. await Instance.provide({
  2197. directory: tmp.path,
  2198. init: async () => {
  2199. set("OPENAI_API_KEY", "test-api-key")
  2200. },
  2201. fn: async () => {
  2202. const providers = await list()
  2203. const model = providers[ProviderID.openai].models["gpt-5"]
  2204. expect(model.variants).toBeDefined()
  2205. expect(model.variants!["high"]).toBeUndefined()
  2206. // Other variants should still exist
  2207. expect(model.variants!["medium"]).toBeDefined()
  2208. },
  2209. })
  2210. })
  2211. test("custom model with variants enabled and disabled", async () => {
  2212. await using tmp = await tmpdir({
  2213. init: async (dir) => {
  2214. await Bun.write(
  2215. path.join(dir, "opencode.json"),
  2216. JSON.stringify({
  2217. $schema: "https://opencode.ai/config.json",
  2218. provider: {
  2219. "custom-reasoning": {
  2220. name: "Custom Reasoning Provider",
  2221. npm: "@ai-sdk/openai-compatible",
  2222. env: [],
  2223. models: {
  2224. "reasoning-model": {
  2225. name: "Reasoning Model",
  2226. tool_call: true,
  2227. reasoning: true,
  2228. limit: { context: 128000, output: 16000 },
  2229. variants: {
  2230. low: { reasoningEffort: "low" },
  2231. medium: { reasoningEffort: "medium" },
  2232. high: { reasoningEffort: "high", disabled: true },
  2233. custom: { reasoningEffort: "custom", budgetTokens: 5000 },
  2234. },
  2235. },
  2236. },
  2237. options: { apiKey: "test-key" },
  2238. },
  2239. },
  2240. }),
  2241. )
  2242. },
  2243. })
  2244. await Instance.provide({
  2245. directory: tmp.path,
  2246. fn: async () => {
  2247. const providers = await list()
  2248. const model = providers[ProviderID.make("custom-reasoning")].models["reasoning-model"]
  2249. expect(model.variants).toBeDefined()
  2250. // Enabled variants should exist
  2251. expect(model.variants!["low"]).toBeDefined()
  2252. expect(model.variants!["low"].reasoningEffort).toBe("low")
  2253. expect(model.variants!["medium"]).toBeDefined()
  2254. expect(model.variants!["medium"].reasoningEffort).toBe("medium")
  2255. expect(model.variants!["custom"]).toBeDefined()
  2256. expect(model.variants!["custom"].reasoningEffort).toBe("custom")
  2257. expect(model.variants!["custom"].budgetTokens).toBe(5000)
  2258. // Disabled variant should not exist
  2259. expect(model.variants!["high"]).toBeUndefined()
  2260. // disabled key should be stripped from all variants
  2261. expect(model.variants!["low"].disabled).toBeUndefined()
  2262. expect(model.variants!["medium"].disabled).toBeUndefined()
  2263. expect(model.variants!["custom"].disabled).toBeUndefined()
  2264. },
  2265. })
  2266. })
  2267. test("Google Vertex: retains baseURL for custom proxy", async () => {
  2268. await using tmp = await tmpdir({
  2269. init: async (dir) => {
  2270. await Bun.write(
  2271. path.join(dir, "opencode.json"),
  2272. JSON.stringify({
  2273. $schema: "https://opencode.ai/config.json",
  2274. provider: {
  2275. "vertex-proxy": {
  2276. name: "Vertex Proxy",
  2277. npm: "@ai-sdk/google-vertex",
  2278. api: "https://my-proxy.com/v1",
  2279. env: ["GOOGLE_APPLICATION_CREDENTIALS"], // Mock env var requirement
  2280. models: {
  2281. "gemini-pro": {
  2282. name: "Gemini Pro",
  2283. tool_call: true,
  2284. },
  2285. },
  2286. options: {
  2287. project: "test-project",
  2288. location: "us-central1",
  2289. baseURL: "https://my-proxy.com/v1", // Should be retained
  2290. },
  2291. },
  2292. },
  2293. }),
  2294. )
  2295. },
  2296. })
  2297. await Instance.provide({
  2298. directory: tmp.path,
  2299. init: async () => {
  2300. set("GOOGLE_APPLICATION_CREDENTIALS", "test-creds")
  2301. },
  2302. fn: async () => {
  2303. const providers = await list()
  2304. expect(providers[ProviderID.make("vertex-proxy")]).toBeDefined()
  2305. expect(providers[ProviderID.make("vertex-proxy")].options.baseURL).toBe("https://my-proxy.com/v1")
  2306. },
  2307. })
  2308. })
  2309. test("Google Vertex: supports OpenAI compatible models", async () => {
  2310. await using tmp = await tmpdir({
  2311. init: async (dir) => {
  2312. await Bun.write(
  2313. path.join(dir, "opencode.json"),
  2314. JSON.stringify({
  2315. $schema: "https://opencode.ai/config.json",
  2316. provider: {
  2317. "vertex-openai": {
  2318. name: "Vertex OpenAI",
  2319. npm: "@ai-sdk/google-vertex",
  2320. env: ["GOOGLE_APPLICATION_CREDENTIALS"],
  2321. models: {
  2322. "gpt-4": {
  2323. name: "GPT-4",
  2324. provider: {
  2325. npm: "@ai-sdk/openai-compatible",
  2326. api: "https://api.openai.com/v1",
  2327. },
  2328. },
  2329. },
  2330. options: {
  2331. project: "test-project",
  2332. location: "us-central1",
  2333. },
  2334. },
  2335. },
  2336. }),
  2337. )
  2338. },
  2339. })
  2340. await Instance.provide({
  2341. directory: tmp.path,
  2342. init: async () => {
  2343. set("GOOGLE_APPLICATION_CREDENTIALS", "test-creds")
  2344. },
  2345. fn: async () => {
  2346. const providers = await list()
  2347. const model = providers[ProviderID.make("vertex-openai")].models["gpt-4"]
  2348. expect(model).toBeDefined()
  2349. expect(model.api.npm).toBe("@ai-sdk/openai-compatible")
  2350. },
  2351. })
  2352. })
  2353. test("cloudflare-ai-gateway loads with env variables", async () => {
  2354. await using tmp = await tmpdir({
  2355. init: async (dir) => {
  2356. await Bun.write(
  2357. path.join(dir, "opencode.json"),
  2358. JSON.stringify({
  2359. $schema: "https://opencode.ai/config.json",
  2360. }),
  2361. )
  2362. },
  2363. })
  2364. await Instance.provide({
  2365. directory: tmp.path,
  2366. init: async () => {
  2367. set("CLOUDFLARE_ACCOUNT_ID", "test-account")
  2368. set("CLOUDFLARE_GATEWAY_ID", "test-gateway")
  2369. set("CLOUDFLARE_API_TOKEN", "test-token")
  2370. },
  2371. fn: async () => {
  2372. const providers = await list()
  2373. expect(providers[ProviderID.make("cloudflare-ai-gateway")]).toBeDefined()
  2374. },
  2375. })
  2376. })
  2377. test("cloudflare-ai-gateway forwards config metadata options", async () => {
  2378. await using tmp = await tmpdir({
  2379. init: async (dir) => {
  2380. await Bun.write(
  2381. path.join(dir, "opencode.json"),
  2382. JSON.stringify({
  2383. $schema: "https://opencode.ai/config.json",
  2384. provider: {
  2385. "cloudflare-ai-gateway": {
  2386. options: {
  2387. metadata: { invoked_by: "test", project: "opencode" },
  2388. },
  2389. },
  2390. },
  2391. }),
  2392. )
  2393. },
  2394. })
  2395. await Instance.provide({
  2396. directory: tmp.path,
  2397. init: async () => {
  2398. set("CLOUDFLARE_ACCOUNT_ID", "test-account")
  2399. set("CLOUDFLARE_GATEWAY_ID", "test-gateway")
  2400. set("CLOUDFLARE_API_TOKEN", "test-token")
  2401. },
  2402. fn: async () => {
  2403. const providers = await list()
  2404. expect(providers[ProviderID.make("cloudflare-ai-gateway")]).toBeDefined()
  2405. expect(providers[ProviderID.make("cloudflare-ai-gateway")].options.metadata).toEqual({
  2406. invoked_by: "test",
  2407. project: "opencode",
  2408. })
  2409. },
  2410. })
  2411. })
  2412. test("plugin config providers persist after instance dispose", async () => {
  2413. await using tmp = await tmpdir({
  2414. init: async (dir) => {
  2415. const configDir = path.join(dir, ".opencode")
  2416. const root = path.join(configDir, "plugin")
  2417. await mkdir(root, { recursive: true })
  2418. await markPluginDependenciesReady(configDir)
  2419. await markPluginDependenciesReady(Global.Path.config)
  2420. await Bun.write(
  2421. path.join(root, "demo-provider.ts"),
  2422. [
  2423. "export default {",
  2424. ' id: "demo.plugin-provider",',
  2425. " server: async () => ({",
  2426. " async config(cfg) {",
  2427. " cfg.provider ??= {}",
  2428. " cfg.provider.demo = {",
  2429. ' name: "Demo Provider",',
  2430. ' npm: "@ai-sdk/openai-compatible",',
  2431. ' api: "https://example.com/v1",',
  2432. " models: {",
  2433. " chat: {",
  2434. ' name: "Demo Chat",',
  2435. " tool_call: true,",
  2436. " limit: { context: 128000, output: 4096 },",
  2437. " },",
  2438. " },",
  2439. " }",
  2440. " },",
  2441. " }),",
  2442. "}",
  2443. "",
  2444. ].join("\n"),
  2445. )
  2446. },
  2447. })
  2448. const first = await Instance.provide({
  2449. directory: tmp.path,
  2450. fn: async () =>
  2451. AppRuntime.runPromise(
  2452. Effect.gen(function* () {
  2453. const plugin = yield* Plugin.Service
  2454. const provider = yield* Provider.Service
  2455. yield* plugin.init()
  2456. return yield* provider.list()
  2457. }),
  2458. ),
  2459. })
  2460. expect(first[ProviderID.make("demo")]).toBeDefined()
  2461. expect(first[ProviderID.make("demo")].models[ModelID.make("chat")]).toBeDefined()
  2462. await Instance.disposeAll()
  2463. const second = await Instance.provide({
  2464. directory: tmp.path,
  2465. fn: async () => list(),
  2466. })
  2467. expect(second[ProviderID.make("demo")]).toBeDefined()
  2468. expect(second[ProviderID.make("demo")].models[ModelID.make("chat")]).toBeDefined()
  2469. })
  2470. test("plugin config enabled and disabled providers are honored", async () => {
  2471. await using tmp = await tmpdir({
  2472. init: async (dir) => {
  2473. const root = path.join(dir, ".opencode", "plugin")
  2474. await mkdir(root, { recursive: true })
  2475. await Bun.write(
  2476. path.join(root, "provider-filter.ts"),
  2477. [
  2478. "export default {",
  2479. ' id: "demo.provider-filter",',
  2480. " server: async () => ({",
  2481. " async config(cfg) {",
  2482. ' cfg.enabled_providers = ["anthropic", "openai"]',
  2483. ' cfg.disabled_providers = ["openai"]',
  2484. " },",
  2485. " }),",
  2486. "}",
  2487. "",
  2488. ].join("\n"),
  2489. )
  2490. },
  2491. })
  2492. await Instance.provide({
  2493. directory: tmp.path,
  2494. init: async () => {
  2495. set("ANTHROPIC_API_KEY", "test-anthropic-key")
  2496. set("OPENAI_API_KEY", "test-openai-key")
  2497. },
  2498. fn: async () => {
  2499. const providers = await list()
  2500. expect(providers[ProviderID.anthropic]).toBeDefined()
  2501. expect(providers[ProviderID.openai]).toBeUndefined()
  2502. },
  2503. })
  2504. })
  2505. test("opencode loader keeps paid models when config apiKey is present", async () => {
  2506. await using base = await tmpdir({
  2507. init: async (dir) => {
  2508. await Bun.write(
  2509. path.join(dir, "opencode.json"),
  2510. JSON.stringify({
  2511. $schema: "https://opencode.ai/config.json",
  2512. }),
  2513. )
  2514. },
  2515. })
  2516. const none = await Instance.provide({
  2517. directory: base.path,
  2518. fn: async () => paid(await list()),
  2519. })
  2520. await using keyed = await tmpdir({
  2521. init: async (dir) => {
  2522. await Bun.write(
  2523. path.join(dir, "opencode.json"),
  2524. JSON.stringify({
  2525. $schema: "https://opencode.ai/config.json",
  2526. provider: {
  2527. opencode: {
  2528. options: {
  2529. apiKey: "test-key",
  2530. },
  2531. },
  2532. },
  2533. }),
  2534. )
  2535. },
  2536. })
  2537. const keyedCount = await Instance.provide({
  2538. directory: keyed.path,
  2539. fn: async () => paid(await list()),
  2540. })
  2541. expect(none).toBe(0)
  2542. expect(keyedCount).toBeGreaterThan(0)
  2543. })
  2544. test("opencode loader keeps paid models when auth exists", async () => {
  2545. await using base = await tmpdir({
  2546. init: async (dir) => {
  2547. await Bun.write(
  2548. path.join(dir, "opencode.json"),
  2549. JSON.stringify({
  2550. $schema: "https://opencode.ai/config.json",
  2551. }),
  2552. )
  2553. },
  2554. })
  2555. const none = await Instance.provide({
  2556. directory: base.path,
  2557. fn: async () => paid(await list()),
  2558. })
  2559. await using keyed = await tmpdir({
  2560. init: async (dir) => {
  2561. await Bun.write(
  2562. path.join(dir, "opencode.json"),
  2563. JSON.stringify({
  2564. $schema: "https://opencode.ai/config.json",
  2565. }),
  2566. )
  2567. },
  2568. })
  2569. const authPath = path.join(Global.Path.data, "auth.json")
  2570. let prev: string | undefined
  2571. try {
  2572. prev = await Filesystem.readText(authPath)
  2573. } catch {}
  2574. try {
  2575. await Filesystem.write(
  2576. authPath,
  2577. JSON.stringify({
  2578. opencode: {
  2579. type: "api",
  2580. key: "test-key",
  2581. },
  2582. }),
  2583. )
  2584. const keyedCount = await Instance.provide({
  2585. directory: keyed.path,
  2586. fn: async () => paid(await list()),
  2587. })
  2588. expect(none).toBe(0)
  2589. expect(keyedCount).toBeGreaterThan(0)
  2590. } finally {
  2591. if (prev !== undefined) {
  2592. await Filesystem.write(authPath, prev)
  2593. }
  2594. if (prev === undefined) {
  2595. try {
  2596. await unlink(authPath)
  2597. } catch {}
  2598. }
  2599. }
  2600. })