provider.test.ts 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. import { test, expect, mock } from "bun:test"
  2. import path from "path"
  3. // Mock BunProc and default plugins to prevent actual installations during tests
  4. mock.module("../../src/bun/index", () => ({
  5. BunProc: {
  6. install: async (pkg: string, _version?: string) => {
  7. // Return package name without version for mocking
  8. const lastAtIndex = pkg.lastIndexOf("@")
  9. return lastAtIndex > 0 ? pkg.substring(0, lastAtIndex) : pkg
  10. },
  11. run: async () => {
  12. throw new Error("BunProc.run should not be called in tests")
  13. },
  14. which: () => process.execPath,
  15. InstallFailedError: class extends Error {},
  16. },
  17. }))
  18. const mockPlugin = () => ({})
  19. mock.module("opencode-copilot-auth", () => ({ default: mockPlugin }))
  20. mock.module("opencode-anthropic-auth", () => ({ default: mockPlugin }))
  21. mock.module("@gitlab/opencode-gitlab-auth", () => ({ default: mockPlugin }))
  22. import { tmpdir } from "../fixture/fixture"
  23. import { Instance } from "../../src/project/instance"
  24. import { Provider } from "../../src/provider/provider"
  25. import { Env } from "../../src/env"
  26. test("provider loaded from env variable", async () => {
  27. await using tmp = await tmpdir({
  28. init: async (dir) => {
  29. await Bun.write(
  30. path.join(dir, "opencode.json"),
  31. JSON.stringify({
  32. $schema: "https://opencode.ai/config.json",
  33. }),
  34. )
  35. },
  36. })
  37. await Instance.provide({
  38. directory: tmp.path,
  39. init: async () => {
  40. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  41. },
  42. fn: async () => {
  43. const providers = await Provider.list()
  44. expect(providers["anthropic"]).toBeDefined()
  45. // Provider should retain its connection source even if custom loaders
  46. // merge additional options.
  47. expect(providers["anthropic"].source).toBe("env")
  48. expect(providers["anthropic"].options.headers["anthropic-beta"]).toBeDefined()
  49. },
  50. })
  51. })
  52. test("provider loaded from config with apiKey option", async () => {
  53. await using tmp = await tmpdir({
  54. init: async (dir) => {
  55. await Bun.write(
  56. path.join(dir, "opencode.json"),
  57. JSON.stringify({
  58. $schema: "https://opencode.ai/config.json",
  59. provider: {
  60. anthropic: {
  61. options: {
  62. apiKey: "config-api-key",
  63. },
  64. },
  65. },
  66. }),
  67. )
  68. },
  69. })
  70. await Instance.provide({
  71. directory: tmp.path,
  72. fn: async () => {
  73. const providers = await Provider.list()
  74. expect(providers["anthropic"]).toBeDefined()
  75. },
  76. })
  77. })
  78. test("disabled_providers excludes provider", async () => {
  79. await using tmp = await tmpdir({
  80. init: async (dir) => {
  81. await Bun.write(
  82. path.join(dir, "opencode.json"),
  83. JSON.stringify({
  84. $schema: "https://opencode.ai/config.json",
  85. disabled_providers: ["anthropic"],
  86. }),
  87. )
  88. },
  89. })
  90. await Instance.provide({
  91. directory: tmp.path,
  92. init: async () => {
  93. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  94. },
  95. fn: async () => {
  96. const providers = await Provider.list()
  97. expect(providers["anthropic"]).toBeUndefined()
  98. },
  99. })
  100. })
  101. test("enabled_providers restricts to only listed providers", async () => {
  102. await using tmp = await tmpdir({
  103. init: async (dir) => {
  104. await Bun.write(
  105. path.join(dir, "opencode.json"),
  106. JSON.stringify({
  107. $schema: "https://opencode.ai/config.json",
  108. enabled_providers: ["anthropic"],
  109. }),
  110. )
  111. },
  112. })
  113. await Instance.provide({
  114. directory: tmp.path,
  115. init: async () => {
  116. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  117. Env.set("OPENAI_API_KEY", "test-openai-key")
  118. },
  119. fn: async () => {
  120. const providers = await Provider.list()
  121. expect(providers["anthropic"]).toBeDefined()
  122. expect(providers["openai"]).toBeUndefined()
  123. },
  124. })
  125. })
  126. test("model whitelist filters models for provider", async () => {
  127. await using tmp = await tmpdir({
  128. init: async (dir) => {
  129. await Bun.write(
  130. path.join(dir, "opencode.json"),
  131. JSON.stringify({
  132. $schema: "https://opencode.ai/config.json",
  133. provider: {
  134. anthropic: {
  135. whitelist: ["claude-sonnet-4-20250514"],
  136. },
  137. },
  138. }),
  139. )
  140. },
  141. })
  142. await Instance.provide({
  143. directory: tmp.path,
  144. init: async () => {
  145. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  146. },
  147. fn: async () => {
  148. const providers = await Provider.list()
  149. expect(providers["anthropic"]).toBeDefined()
  150. const models = Object.keys(providers["anthropic"].models)
  151. expect(models).toContain("claude-sonnet-4-20250514")
  152. expect(models.length).toBe(1)
  153. },
  154. })
  155. })
  156. test("model blacklist excludes specific models", async () => {
  157. await using tmp = await tmpdir({
  158. init: async (dir) => {
  159. await Bun.write(
  160. path.join(dir, "opencode.json"),
  161. JSON.stringify({
  162. $schema: "https://opencode.ai/config.json",
  163. provider: {
  164. anthropic: {
  165. blacklist: ["claude-sonnet-4-20250514"],
  166. },
  167. },
  168. }),
  169. )
  170. },
  171. })
  172. await Instance.provide({
  173. directory: tmp.path,
  174. init: async () => {
  175. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  176. },
  177. fn: async () => {
  178. const providers = await Provider.list()
  179. expect(providers["anthropic"]).toBeDefined()
  180. const models = Object.keys(providers["anthropic"].models)
  181. expect(models).not.toContain("claude-sonnet-4-20250514")
  182. },
  183. })
  184. })
  185. test("custom model alias via config", async () => {
  186. await using tmp = await tmpdir({
  187. init: async (dir) => {
  188. await Bun.write(
  189. path.join(dir, "opencode.json"),
  190. JSON.stringify({
  191. $schema: "https://opencode.ai/config.json",
  192. provider: {
  193. anthropic: {
  194. models: {
  195. "my-alias": {
  196. id: "claude-sonnet-4-20250514",
  197. name: "My Custom Alias",
  198. },
  199. },
  200. },
  201. },
  202. }),
  203. )
  204. },
  205. })
  206. await Instance.provide({
  207. directory: tmp.path,
  208. init: async () => {
  209. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  210. },
  211. fn: async () => {
  212. const providers = await Provider.list()
  213. expect(providers["anthropic"]).toBeDefined()
  214. expect(providers["anthropic"].models["my-alias"]).toBeDefined()
  215. expect(providers["anthropic"].models["my-alias"].name).toBe("My Custom Alias")
  216. },
  217. })
  218. })
  219. test("custom provider with npm package", async () => {
  220. await using tmp = await tmpdir({
  221. init: async (dir) => {
  222. await Bun.write(
  223. path.join(dir, "opencode.json"),
  224. JSON.stringify({
  225. $schema: "https://opencode.ai/config.json",
  226. provider: {
  227. "custom-provider": {
  228. name: "Custom Provider",
  229. npm: "@ai-sdk/openai-compatible",
  230. api: "https://api.custom.com/v1",
  231. env: ["CUSTOM_API_KEY"],
  232. models: {
  233. "custom-model": {
  234. name: "Custom Model",
  235. tool_call: true,
  236. limit: {
  237. context: 128000,
  238. output: 4096,
  239. },
  240. },
  241. },
  242. options: {
  243. apiKey: "custom-key",
  244. },
  245. },
  246. },
  247. }),
  248. )
  249. },
  250. })
  251. await Instance.provide({
  252. directory: tmp.path,
  253. fn: async () => {
  254. const providers = await Provider.list()
  255. expect(providers["custom-provider"]).toBeDefined()
  256. expect(providers["custom-provider"].name).toBe("Custom Provider")
  257. expect(providers["custom-provider"].models["custom-model"]).toBeDefined()
  258. },
  259. })
  260. })
  261. test("env variable takes precedence, config merges options", async () => {
  262. await using tmp = await tmpdir({
  263. init: async (dir) => {
  264. await Bun.write(
  265. path.join(dir, "opencode.json"),
  266. JSON.stringify({
  267. $schema: "https://opencode.ai/config.json",
  268. provider: {
  269. anthropic: {
  270. options: {
  271. timeout: 60000,
  272. },
  273. },
  274. },
  275. }),
  276. )
  277. },
  278. })
  279. await Instance.provide({
  280. directory: tmp.path,
  281. init: async () => {
  282. Env.set("ANTHROPIC_API_KEY", "env-api-key")
  283. },
  284. fn: async () => {
  285. const providers = await Provider.list()
  286. expect(providers["anthropic"]).toBeDefined()
  287. // Config options should be merged
  288. expect(providers["anthropic"].options.timeout).toBe(60000)
  289. },
  290. })
  291. })
  292. test("getModel returns model for valid provider/model", async () => {
  293. await using tmp = await tmpdir({
  294. init: async (dir) => {
  295. await Bun.write(
  296. path.join(dir, "opencode.json"),
  297. JSON.stringify({
  298. $schema: "https://opencode.ai/config.json",
  299. }),
  300. )
  301. },
  302. })
  303. await Instance.provide({
  304. directory: tmp.path,
  305. init: async () => {
  306. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  307. },
  308. fn: async () => {
  309. const model = await Provider.getModel("anthropic", "claude-sonnet-4-20250514")
  310. expect(model).toBeDefined()
  311. expect(model.providerID).toBe("anthropic")
  312. expect(model.id).toBe("claude-sonnet-4-20250514")
  313. const language = await Provider.getLanguage(model)
  314. expect(language).toBeDefined()
  315. },
  316. })
  317. })
  318. test("getModel throws ModelNotFoundError for invalid model", async () => {
  319. await using tmp = await tmpdir({
  320. init: async (dir) => {
  321. await Bun.write(
  322. path.join(dir, "opencode.json"),
  323. JSON.stringify({
  324. $schema: "https://opencode.ai/config.json",
  325. }),
  326. )
  327. },
  328. })
  329. await Instance.provide({
  330. directory: tmp.path,
  331. init: async () => {
  332. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  333. },
  334. fn: async () => {
  335. expect(Provider.getModel("anthropic", "nonexistent-model")).rejects.toThrow()
  336. },
  337. })
  338. })
  339. test("getModel throws ModelNotFoundError for invalid provider", async () => {
  340. await using tmp = await tmpdir({
  341. init: async (dir) => {
  342. await Bun.write(
  343. path.join(dir, "opencode.json"),
  344. JSON.stringify({
  345. $schema: "https://opencode.ai/config.json",
  346. }),
  347. )
  348. },
  349. })
  350. await Instance.provide({
  351. directory: tmp.path,
  352. fn: async () => {
  353. expect(Provider.getModel("nonexistent-provider", "some-model")).rejects.toThrow()
  354. },
  355. })
  356. })
  357. test("parseModel correctly parses provider/model string", () => {
  358. const result = Provider.parseModel("anthropic/claude-sonnet-4")
  359. expect(result.providerID).toBe("anthropic")
  360. expect(result.modelID).toBe("claude-sonnet-4")
  361. })
  362. test("parseModel handles model IDs with slashes", () => {
  363. const result = Provider.parseModel("openrouter/anthropic/claude-3-opus")
  364. expect(result.providerID).toBe("openrouter")
  365. expect(result.modelID).toBe("anthropic/claude-3-opus")
  366. })
  367. test("defaultModel returns first available model when no config set", async () => {
  368. await using tmp = await tmpdir({
  369. init: async (dir) => {
  370. await Bun.write(
  371. path.join(dir, "opencode.json"),
  372. JSON.stringify({
  373. $schema: "https://opencode.ai/config.json",
  374. }),
  375. )
  376. },
  377. })
  378. await Instance.provide({
  379. directory: tmp.path,
  380. init: async () => {
  381. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  382. },
  383. fn: async () => {
  384. const model = await Provider.defaultModel()
  385. expect(model.providerID).toBeDefined()
  386. expect(model.modelID).toBeDefined()
  387. },
  388. })
  389. })
  390. test("defaultModel respects config model setting", async () => {
  391. await using tmp = await tmpdir({
  392. init: async (dir) => {
  393. await Bun.write(
  394. path.join(dir, "opencode.json"),
  395. JSON.stringify({
  396. $schema: "https://opencode.ai/config.json",
  397. model: "anthropic/claude-sonnet-4-20250514",
  398. }),
  399. )
  400. },
  401. })
  402. await Instance.provide({
  403. directory: tmp.path,
  404. init: async () => {
  405. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  406. },
  407. fn: async () => {
  408. const model = await Provider.defaultModel()
  409. expect(model.providerID).toBe("anthropic")
  410. expect(model.modelID).toBe("claude-sonnet-4-20250514")
  411. },
  412. })
  413. })
  414. test("provider with baseURL from config", async () => {
  415. await using tmp = await tmpdir({
  416. init: async (dir) => {
  417. await Bun.write(
  418. path.join(dir, "opencode.json"),
  419. JSON.stringify({
  420. $schema: "https://opencode.ai/config.json",
  421. provider: {
  422. "custom-openai": {
  423. name: "Custom OpenAI",
  424. npm: "@ai-sdk/openai-compatible",
  425. env: [],
  426. models: {
  427. "gpt-4": {
  428. name: "GPT-4",
  429. tool_call: true,
  430. limit: { context: 128000, output: 4096 },
  431. },
  432. },
  433. options: {
  434. apiKey: "test-key",
  435. baseURL: "https://custom.openai.com/v1",
  436. },
  437. },
  438. },
  439. }),
  440. )
  441. },
  442. })
  443. await Instance.provide({
  444. directory: tmp.path,
  445. fn: async () => {
  446. const providers = await Provider.list()
  447. expect(providers["custom-openai"]).toBeDefined()
  448. expect(providers["custom-openai"].options.baseURL).toBe("https://custom.openai.com/v1")
  449. },
  450. })
  451. })
  452. test("model cost defaults to zero when not specified", async () => {
  453. await using tmp = await tmpdir({
  454. init: async (dir) => {
  455. await Bun.write(
  456. path.join(dir, "opencode.json"),
  457. JSON.stringify({
  458. $schema: "https://opencode.ai/config.json",
  459. provider: {
  460. "test-provider": {
  461. name: "Test Provider",
  462. npm: "@ai-sdk/openai-compatible",
  463. env: [],
  464. models: {
  465. "test-model": {
  466. name: "Test Model",
  467. tool_call: true,
  468. limit: { context: 128000, output: 4096 },
  469. },
  470. },
  471. options: {
  472. apiKey: "test-key",
  473. },
  474. },
  475. },
  476. }),
  477. )
  478. },
  479. })
  480. await Instance.provide({
  481. directory: tmp.path,
  482. fn: async () => {
  483. const providers = await Provider.list()
  484. const model = providers["test-provider"].models["test-model"]
  485. expect(model.cost.input).toBe(0)
  486. expect(model.cost.output).toBe(0)
  487. expect(model.cost.cache.read).toBe(0)
  488. expect(model.cost.cache.write).toBe(0)
  489. },
  490. })
  491. })
  492. test("model options are merged from existing model", async () => {
  493. await using tmp = await tmpdir({
  494. init: async (dir) => {
  495. await Bun.write(
  496. path.join(dir, "opencode.json"),
  497. JSON.stringify({
  498. $schema: "https://opencode.ai/config.json",
  499. provider: {
  500. anthropic: {
  501. models: {
  502. "claude-sonnet-4-20250514": {
  503. options: {
  504. customOption: "custom-value",
  505. },
  506. },
  507. },
  508. },
  509. },
  510. }),
  511. )
  512. },
  513. })
  514. await Instance.provide({
  515. directory: tmp.path,
  516. init: async () => {
  517. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  518. },
  519. fn: async () => {
  520. const providers = await Provider.list()
  521. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  522. expect(model.options.customOption).toBe("custom-value")
  523. },
  524. })
  525. })
  526. test("provider removed when all models filtered out", async () => {
  527. await using tmp = await tmpdir({
  528. init: async (dir) => {
  529. await Bun.write(
  530. path.join(dir, "opencode.json"),
  531. JSON.stringify({
  532. $schema: "https://opencode.ai/config.json",
  533. provider: {
  534. anthropic: {
  535. whitelist: ["nonexistent-model"],
  536. },
  537. },
  538. }),
  539. )
  540. },
  541. })
  542. await Instance.provide({
  543. directory: tmp.path,
  544. init: async () => {
  545. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  546. },
  547. fn: async () => {
  548. const providers = await Provider.list()
  549. expect(providers["anthropic"]).toBeUndefined()
  550. },
  551. })
  552. })
  553. test("closest finds model by partial match", async () => {
  554. await using tmp = await tmpdir({
  555. init: async (dir) => {
  556. await Bun.write(
  557. path.join(dir, "opencode.json"),
  558. JSON.stringify({
  559. $schema: "https://opencode.ai/config.json",
  560. }),
  561. )
  562. },
  563. })
  564. await Instance.provide({
  565. directory: tmp.path,
  566. init: async () => {
  567. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  568. },
  569. fn: async () => {
  570. const result = await Provider.closest("anthropic", ["sonnet-4"])
  571. expect(result).toBeDefined()
  572. expect(result?.providerID).toBe("anthropic")
  573. expect(result?.modelID).toContain("sonnet-4")
  574. },
  575. })
  576. })
  577. test("closest returns undefined for nonexistent provider", async () => {
  578. await using tmp = await tmpdir({
  579. init: async (dir) => {
  580. await Bun.write(
  581. path.join(dir, "opencode.json"),
  582. JSON.stringify({
  583. $schema: "https://opencode.ai/config.json",
  584. }),
  585. )
  586. },
  587. })
  588. await Instance.provide({
  589. directory: tmp.path,
  590. fn: async () => {
  591. const result = await Provider.closest("nonexistent", ["model"])
  592. expect(result).toBeUndefined()
  593. },
  594. })
  595. })
  596. test("getModel uses realIdByKey for aliased models", async () => {
  597. await using tmp = await tmpdir({
  598. init: async (dir) => {
  599. await Bun.write(
  600. path.join(dir, "opencode.json"),
  601. JSON.stringify({
  602. $schema: "https://opencode.ai/config.json",
  603. provider: {
  604. anthropic: {
  605. models: {
  606. "my-sonnet": {
  607. id: "claude-sonnet-4-20250514",
  608. name: "My Sonnet Alias",
  609. },
  610. },
  611. },
  612. },
  613. }),
  614. )
  615. },
  616. })
  617. await Instance.provide({
  618. directory: tmp.path,
  619. init: async () => {
  620. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  621. },
  622. fn: async () => {
  623. const providers = await Provider.list()
  624. expect(providers["anthropic"].models["my-sonnet"]).toBeDefined()
  625. const model = await Provider.getModel("anthropic", "my-sonnet")
  626. expect(model).toBeDefined()
  627. expect(model.id).toBe("my-sonnet")
  628. expect(model.name).toBe("My Sonnet Alias")
  629. },
  630. })
  631. })
  632. test("provider api field sets model api.url", async () => {
  633. await using tmp = await tmpdir({
  634. init: async (dir) => {
  635. await Bun.write(
  636. path.join(dir, "opencode.json"),
  637. JSON.stringify({
  638. $schema: "https://opencode.ai/config.json",
  639. provider: {
  640. "custom-api": {
  641. name: "Custom API",
  642. npm: "@ai-sdk/openai-compatible",
  643. api: "https://api.example.com/v1",
  644. env: [],
  645. models: {
  646. "model-1": {
  647. name: "Model 1",
  648. tool_call: true,
  649. limit: { context: 8000, output: 2000 },
  650. },
  651. },
  652. options: {
  653. apiKey: "test-key",
  654. },
  655. },
  656. },
  657. }),
  658. )
  659. },
  660. })
  661. await Instance.provide({
  662. directory: tmp.path,
  663. fn: async () => {
  664. const providers = await Provider.list()
  665. // api field is stored on model.api.url, used by getSDK to set baseURL
  666. expect(providers["custom-api"].models["model-1"].api.url).toBe("https://api.example.com/v1")
  667. },
  668. })
  669. })
  670. test("explicit baseURL overrides api field", async () => {
  671. await using tmp = await tmpdir({
  672. init: async (dir) => {
  673. await Bun.write(
  674. path.join(dir, "opencode.json"),
  675. JSON.stringify({
  676. $schema: "https://opencode.ai/config.json",
  677. provider: {
  678. "custom-api": {
  679. name: "Custom API",
  680. npm: "@ai-sdk/openai-compatible",
  681. api: "https://api.example.com/v1",
  682. env: [],
  683. models: {
  684. "model-1": {
  685. name: "Model 1",
  686. tool_call: true,
  687. limit: { context: 8000, output: 2000 },
  688. },
  689. },
  690. options: {
  691. apiKey: "test-key",
  692. baseURL: "https://custom.override.com/v1",
  693. },
  694. },
  695. },
  696. }),
  697. )
  698. },
  699. })
  700. await Instance.provide({
  701. directory: tmp.path,
  702. fn: async () => {
  703. const providers = await Provider.list()
  704. expect(providers["custom-api"].options.baseURL).toBe("https://custom.override.com/v1")
  705. },
  706. })
  707. })
  708. test("model inherits properties from existing database model", async () => {
  709. await using tmp = await tmpdir({
  710. init: async (dir) => {
  711. await Bun.write(
  712. path.join(dir, "opencode.json"),
  713. JSON.stringify({
  714. $schema: "https://opencode.ai/config.json",
  715. provider: {
  716. anthropic: {
  717. models: {
  718. "claude-sonnet-4-20250514": {
  719. name: "Custom Name for Sonnet",
  720. },
  721. },
  722. },
  723. },
  724. }),
  725. )
  726. },
  727. })
  728. await Instance.provide({
  729. directory: tmp.path,
  730. init: async () => {
  731. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  732. },
  733. fn: async () => {
  734. const providers = await Provider.list()
  735. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  736. expect(model.name).toBe("Custom Name for Sonnet")
  737. expect(model.capabilities.toolcall).toBe(true)
  738. expect(model.capabilities.attachment).toBe(true)
  739. expect(model.limit.context).toBeGreaterThan(0)
  740. },
  741. })
  742. })
  743. test("disabled_providers prevents loading even with env var", async () => {
  744. await using tmp = await tmpdir({
  745. init: async (dir) => {
  746. await Bun.write(
  747. path.join(dir, "opencode.json"),
  748. JSON.stringify({
  749. $schema: "https://opencode.ai/config.json",
  750. disabled_providers: ["openai"],
  751. }),
  752. )
  753. },
  754. })
  755. await Instance.provide({
  756. directory: tmp.path,
  757. init: async () => {
  758. Env.set("OPENAI_API_KEY", "test-openai-key")
  759. },
  760. fn: async () => {
  761. const providers = await Provider.list()
  762. expect(providers["openai"]).toBeUndefined()
  763. },
  764. })
  765. })
  766. test("enabled_providers with empty array allows no providers", async () => {
  767. await using tmp = await tmpdir({
  768. init: async (dir) => {
  769. await Bun.write(
  770. path.join(dir, "opencode.json"),
  771. JSON.stringify({
  772. $schema: "https://opencode.ai/config.json",
  773. enabled_providers: [],
  774. }),
  775. )
  776. },
  777. })
  778. await Instance.provide({
  779. directory: tmp.path,
  780. init: async () => {
  781. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  782. Env.set("OPENAI_API_KEY", "test-openai-key")
  783. },
  784. fn: async () => {
  785. const providers = await Provider.list()
  786. expect(Object.keys(providers).length).toBe(0)
  787. },
  788. })
  789. })
  790. test("whitelist and blacklist can be combined", async () => {
  791. await using tmp = await tmpdir({
  792. init: async (dir) => {
  793. await Bun.write(
  794. path.join(dir, "opencode.json"),
  795. JSON.stringify({
  796. $schema: "https://opencode.ai/config.json",
  797. provider: {
  798. anthropic: {
  799. whitelist: ["claude-sonnet-4-20250514", "claude-opus-4-20250514"],
  800. blacklist: ["claude-opus-4-20250514"],
  801. },
  802. },
  803. }),
  804. )
  805. },
  806. })
  807. await Instance.provide({
  808. directory: tmp.path,
  809. init: async () => {
  810. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  811. },
  812. fn: async () => {
  813. const providers = await Provider.list()
  814. expect(providers["anthropic"]).toBeDefined()
  815. const models = Object.keys(providers["anthropic"].models)
  816. expect(models).toContain("claude-sonnet-4-20250514")
  817. expect(models).not.toContain("claude-opus-4-20250514")
  818. expect(models.length).toBe(1)
  819. },
  820. })
  821. })
  822. test("model modalities default correctly", async () => {
  823. await using tmp = await tmpdir({
  824. init: async (dir) => {
  825. await Bun.write(
  826. path.join(dir, "opencode.json"),
  827. JSON.stringify({
  828. $schema: "https://opencode.ai/config.json",
  829. provider: {
  830. "test-provider": {
  831. name: "Test",
  832. npm: "@ai-sdk/openai-compatible",
  833. env: [],
  834. models: {
  835. "test-model": {
  836. name: "Test Model",
  837. tool_call: true,
  838. limit: { context: 8000, output: 2000 },
  839. },
  840. },
  841. options: { apiKey: "test" },
  842. },
  843. },
  844. }),
  845. )
  846. },
  847. })
  848. await Instance.provide({
  849. directory: tmp.path,
  850. fn: async () => {
  851. const providers = await Provider.list()
  852. const model = providers["test-provider"].models["test-model"]
  853. expect(model.capabilities.input.text).toBe(true)
  854. expect(model.capabilities.output.text).toBe(true)
  855. },
  856. })
  857. })
  858. test("model with custom cost values", async () => {
  859. await using tmp = await tmpdir({
  860. init: async (dir) => {
  861. await Bun.write(
  862. path.join(dir, "opencode.json"),
  863. JSON.stringify({
  864. $schema: "https://opencode.ai/config.json",
  865. provider: {
  866. "test-provider": {
  867. name: "Test",
  868. npm: "@ai-sdk/openai-compatible",
  869. env: [],
  870. models: {
  871. "test-model": {
  872. name: "Test Model",
  873. tool_call: true,
  874. limit: { context: 8000, output: 2000 },
  875. cost: {
  876. input: 5,
  877. output: 15,
  878. cache_read: 2.5,
  879. cache_write: 7.5,
  880. },
  881. },
  882. },
  883. options: { apiKey: "test" },
  884. },
  885. },
  886. }),
  887. )
  888. },
  889. })
  890. await Instance.provide({
  891. directory: tmp.path,
  892. fn: async () => {
  893. const providers = await Provider.list()
  894. const model = providers["test-provider"].models["test-model"]
  895. expect(model.cost.input).toBe(5)
  896. expect(model.cost.output).toBe(15)
  897. expect(model.cost.cache.read).toBe(2.5)
  898. expect(model.cost.cache.write).toBe(7.5)
  899. },
  900. })
  901. })
  902. test("getSmallModel returns appropriate small model", async () => {
  903. await using tmp = await tmpdir({
  904. init: async (dir) => {
  905. await Bun.write(
  906. path.join(dir, "opencode.json"),
  907. JSON.stringify({
  908. $schema: "https://opencode.ai/config.json",
  909. }),
  910. )
  911. },
  912. })
  913. await Instance.provide({
  914. directory: tmp.path,
  915. init: async () => {
  916. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  917. },
  918. fn: async () => {
  919. const model = await Provider.getSmallModel("anthropic")
  920. expect(model).toBeDefined()
  921. expect(model?.id).toContain("haiku")
  922. },
  923. })
  924. })
  925. test("getSmallModel respects config small_model override", async () => {
  926. await using tmp = await tmpdir({
  927. init: async (dir) => {
  928. await Bun.write(
  929. path.join(dir, "opencode.json"),
  930. JSON.stringify({
  931. $schema: "https://opencode.ai/config.json",
  932. small_model: "anthropic/claude-sonnet-4-20250514",
  933. }),
  934. )
  935. },
  936. })
  937. await Instance.provide({
  938. directory: tmp.path,
  939. init: async () => {
  940. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  941. },
  942. fn: async () => {
  943. const model = await Provider.getSmallModel("anthropic")
  944. expect(model).toBeDefined()
  945. expect(model?.providerID).toBe("anthropic")
  946. expect(model?.id).toBe("claude-sonnet-4-20250514")
  947. },
  948. })
  949. })
  950. test("provider.sort prioritizes preferred models", () => {
  951. const models = [
  952. { id: "random-model", name: "Random" },
  953. { id: "claude-sonnet-4-latest", name: "Claude Sonnet 4" },
  954. { id: "gpt-5-turbo", name: "GPT-5 Turbo" },
  955. { id: "other-model", name: "Other" },
  956. ] as any[]
  957. const sorted = Provider.sort(models)
  958. expect(sorted[0].id).toContain("sonnet-4")
  959. expect(sorted[0].id).toContain("latest")
  960. expect(sorted[sorted.length - 1].id).not.toContain("gpt-5")
  961. expect(sorted[sorted.length - 1].id).not.toContain("sonnet-4")
  962. })
  963. test("multiple providers can be configured simultaneously", async () => {
  964. await using tmp = await tmpdir({
  965. init: async (dir) => {
  966. await Bun.write(
  967. path.join(dir, "opencode.json"),
  968. JSON.stringify({
  969. $schema: "https://opencode.ai/config.json",
  970. provider: {
  971. anthropic: {
  972. options: { timeout: 30000 },
  973. },
  974. openai: {
  975. options: { timeout: 60000 },
  976. },
  977. },
  978. }),
  979. )
  980. },
  981. })
  982. await Instance.provide({
  983. directory: tmp.path,
  984. init: async () => {
  985. Env.set("ANTHROPIC_API_KEY", "test-anthropic-key")
  986. Env.set("OPENAI_API_KEY", "test-openai-key")
  987. },
  988. fn: async () => {
  989. const providers = await Provider.list()
  990. expect(providers["anthropic"]).toBeDefined()
  991. expect(providers["openai"]).toBeDefined()
  992. expect(providers["anthropic"].options.timeout).toBe(30000)
  993. expect(providers["openai"].options.timeout).toBe(60000)
  994. },
  995. })
  996. })
  997. test("provider with custom npm package", 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. provider: {
  1005. "local-llm": {
  1006. name: "Local LLM",
  1007. npm: "@ai-sdk/openai-compatible",
  1008. env: [],
  1009. models: {
  1010. "llama-3": {
  1011. name: "Llama 3",
  1012. tool_call: true,
  1013. limit: { context: 8192, output: 2048 },
  1014. },
  1015. },
  1016. options: {
  1017. apiKey: "not-needed",
  1018. baseURL: "http://localhost:11434/v1",
  1019. },
  1020. },
  1021. },
  1022. }),
  1023. )
  1024. },
  1025. })
  1026. await Instance.provide({
  1027. directory: tmp.path,
  1028. fn: async () => {
  1029. const providers = await Provider.list()
  1030. expect(providers["local-llm"]).toBeDefined()
  1031. expect(providers["local-llm"].models["llama-3"].api.npm).toBe("@ai-sdk/openai-compatible")
  1032. expect(providers["local-llm"].options.baseURL).toBe("http://localhost:11434/v1")
  1033. },
  1034. })
  1035. })
  1036. // Edge cases for model configuration
  1037. test("model alias name defaults to alias key when id differs", async () => {
  1038. await using tmp = await tmpdir({
  1039. init: async (dir) => {
  1040. await Bun.write(
  1041. path.join(dir, "opencode.json"),
  1042. JSON.stringify({
  1043. $schema: "https://opencode.ai/config.json",
  1044. provider: {
  1045. anthropic: {
  1046. models: {
  1047. sonnet: {
  1048. id: "claude-sonnet-4-20250514",
  1049. // no name specified - should default to "sonnet" (the key)
  1050. },
  1051. },
  1052. },
  1053. },
  1054. }),
  1055. )
  1056. },
  1057. })
  1058. await Instance.provide({
  1059. directory: tmp.path,
  1060. init: async () => {
  1061. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1062. },
  1063. fn: async () => {
  1064. const providers = await Provider.list()
  1065. expect(providers["anthropic"].models["sonnet"].name).toBe("sonnet")
  1066. },
  1067. })
  1068. })
  1069. test("provider with multiple env var options only includes apiKey when single env", async () => {
  1070. await using tmp = await tmpdir({
  1071. init: async (dir) => {
  1072. await Bun.write(
  1073. path.join(dir, "opencode.json"),
  1074. JSON.stringify({
  1075. $schema: "https://opencode.ai/config.json",
  1076. provider: {
  1077. "multi-env": {
  1078. name: "Multi Env Provider",
  1079. npm: "@ai-sdk/openai-compatible",
  1080. env: ["MULTI_ENV_KEY_1", "MULTI_ENV_KEY_2"],
  1081. models: {
  1082. "model-1": {
  1083. name: "Model 1",
  1084. tool_call: true,
  1085. limit: { context: 8000, output: 2000 },
  1086. },
  1087. },
  1088. options: {
  1089. baseURL: "https://api.example.com/v1",
  1090. },
  1091. },
  1092. },
  1093. }),
  1094. )
  1095. },
  1096. })
  1097. await Instance.provide({
  1098. directory: tmp.path,
  1099. init: async () => {
  1100. Env.set("MULTI_ENV_KEY_1", "test-key")
  1101. },
  1102. fn: async () => {
  1103. const providers = await Provider.list()
  1104. expect(providers["multi-env"]).toBeDefined()
  1105. // When multiple env options exist, key should NOT be auto-set
  1106. expect(providers["multi-env"].key).toBeUndefined()
  1107. },
  1108. })
  1109. })
  1110. test("provider with single env var includes apiKey automatically", async () => {
  1111. await using tmp = await tmpdir({
  1112. init: async (dir) => {
  1113. await Bun.write(
  1114. path.join(dir, "opencode.json"),
  1115. JSON.stringify({
  1116. $schema: "https://opencode.ai/config.json",
  1117. provider: {
  1118. "single-env": {
  1119. name: "Single Env Provider",
  1120. npm: "@ai-sdk/openai-compatible",
  1121. env: ["SINGLE_ENV_KEY"],
  1122. models: {
  1123. "model-1": {
  1124. name: "Model 1",
  1125. tool_call: true,
  1126. limit: { context: 8000, output: 2000 },
  1127. },
  1128. },
  1129. options: {
  1130. baseURL: "https://api.example.com/v1",
  1131. },
  1132. },
  1133. },
  1134. }),
  1135. )
  1136. },
  1137. })
  1138. await Instance.provide({
  1139. directory: tmp.path,
  1140. init: async () => {
  1141. Env.set("SINGLE_ENV_KEY", "my-api-key")
  1142. },
  1143. fn: async () => {
  1144. const providers = await Provider.list()
  1145. expect(providers["single-env"]).toBeDefined()
  1146. // Single env option should auto-set key
  1147. expect(providers["single-env"].key).toBe("my-api-key")
  1148. },
  1149. })
  1150. })
  1151. test("model cost overrides existing cost values", async () => {
  1152. await using tmp = await tmpdir({
  1153. init: async (dir) => {
  1154. await Bun.write(
  1155. path.join(dir, "opencode.json"),
  1156. JSON.stringify({
  1157. $schema: "https://opencode.ai/config.json",
  1158. provider: {
  1159. anthropic: {
  1160. models: {
  1161. "claude-sonnet-4-20250514": {
  1162. cost: {
  1163. input: 999,
  1164. output: 888,
  1165. },
  1166. },
  1167. },
  1168. },
  1169. },
  1170. }),
  1171. )
  1172. },
  1173. })
  1174. await Instance.provide({
  1175. directory: tmp.path,
  1176. init: async () => {
  1177. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1178. },
  1179. fn: async () => {
  1180. const providers = await Provider.list()
  1181. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1182. expect(model.cost.input).toBe(999)
  1183. expect(model.cost.output).toBe(888)
  1184. },
  1185. })
  1186. })
  1187. test("completely new provider not in database can be configured", async () => {
  1188. await using tmp = await tmpdir({
  1189. init: async (dir) => {
  1190. await Bun.write(
  1191. path.join(dir, "opencode.json"),
  1192. JSON.stringify({
  1193. $schema: "https://opencode.ai/config.json",
  1194. provider: {
  1195. "brand-new-provider": {
  1196. name: "Brand New",
  1197. npm: "@ai-sdk/openai-compatible",
  1198. env: [],
  1199. api: "https://new-api.com/v1",
  1200. models: {
  1201. "new-model": {
  1202. name: "New Model",
  1203. tool_call: true,
  1204. reasoning: true,
  1205. attachment: true,
  1206. temperature: true,
  1207. limit: { context: 32000, output: 8000 },
  1208. modalities: {
  1209. input: ["text", "image"],
  1210. output: ["text"],
  1211. },
  1212. },
  1213. },
  1214. options: {
  1215. apiKey: "new-key",
  1216. },
  1217. },
  1218. },
  1219. }),
  1220. )
  1221. },
  1222. })
  1223. await Instance.provide({
  1224. directory: tmp.path,
  1225. fn: async () => {
  1226. const providers = await Provider.list()
  1227. expect(providers["brand-new-provider"]).toBeDefined()
  1228. expect(providers["brand-new-provider"].name).toBe("Brand New")
  1229. const model = providers["brand-new-provider"].models["new-model"]
  1230. expect(model.capabilities.reasoning).toBe(true)
  1231. expect(model.capabilities.attachment).toBe(true)
  1232. expect(model.capabilities.input.image).toBe(true)
  1233. },
  1234. })
  1235. })
  1236. test("disabled_providers and enabled_providers interaction", async () => {
  1237. await using tmp = await tmpdir({
  1238. init: async (dir) => {
  1239. await Bun.write(
  1240. path.join(dir, "opencode.json"),
  1241. JSON.stringify({
  1242. $schema: "https://opencode.ai/config.json",
  1243. // enabled_providers takes precedence - only these are considered
  1244. enabled_providers: ["anthropic", "openai"],
  1245. // Then disabled_providers filters from the enabled set
  1246. disabled_providers: ["openai"],
  1247. }),
  1248. )
  1249. },
  1250. })
  1251. await Instance.provide({
  1252. directory: tmp.path,
  1253. init: async () => {
  1254. Env.set("ANTHROPIC_API_KEY", "test-anthropic")
  1255. Env.set("OPENAI_API_KEY", "test-openai")
  1256. Env.set("GOOGLE_GENERATIVE_AI_API_KEY", "test-google")
  1257. },
  1258. fn: async () => {
  1259. const providers = await Provider.list()
  1260. // anthropic: in enabled, not in disabled = allowed
  1261. expect(providers["anthropic"]).toBeDefined()
  1262. // openai: in enabled, but also in disabled = NOT allowed
  1263. expect(providers["openai"]).toBeUndefined()
  1264. // google: not in enabled = NOT allowed (even though not disabled)
  1265. expect(providers["google"]).toBeUndefined()
  1266. },
  1267. })
  1268. })
  1269. test("model with tool_call false", async () => {
  1270. await using tmp = await tmpdir({
  1271. init: async (dir) => {
  1272. await Bun.write(
  1273. path.join(dir, "opencode.json"),
  1274. JSON.stringify({
  1275. $schema: "https://opencode.ai/config.json",
  1276. provider: {
  1277. "no-tools": {
  1278. name: "No Tools Provider",
  1279. npm: "@ai-sdk/openai-compatible",
  1280. env: [],
  1281. models: {
  1282. "basic-model": {
  1283. name: "Basic Model",
  1284. tool_call: false,
  1285. limit: { context: 4000, output: 1000 },
  1286. },
  1287. },
  1288. options: { apiKey: "test" },
  1289. },
  1290. },
  1291. }),
  1292. )
  1293. },
  1294. })
  1295. await Instance.provide({
  1296. directory: tmp.path,
  1297. fn: async () => {
  1298. const providers = await Provider.list()
  1299. expect(providers["no-tools"].models["basic-model"].capabilities.toolcall).toBe(false)
  1300. },
  1301. })
  1302. })
  1303. test("model defaults tool_call to true when not specified", async () => {
  1304. await using tmp = await tmpdir({
  1305. init: async (dir) => {
  1306. await Bun.write(
  1307. path.join(dir, "opencode.json"),
  1308. JSON.stringify({
  1309. $schema: "https://opencode.ai/config.json",
  1310. provider: {
  1311. "default-tools": {
  1312. name: "Default Tools Provider",
  1313. npm: "@ai-sdk/openai-compatible",
  1314. env: [],
  1315. models: {
  1316. model: {
  1317. name: "Model",
  1318. // tool_call not specified
  1319. limit: { context: 4000, output: 1000 },
  1320. },
  1321. },
  1322. options: { apiKey: "test" },
  1323. },
  1324. },
  1325. }),
  1326. )
  1327. },
  1328. })
  1329. await Instance.provide({
  1330. directory: tmp.path,
  1331. fn: async () => {
  1332. const providers = await Provider.list()
  1333. expect(providers["default-tools"].models["model"].capabilities.toolcall).toBe(true)
  1334. },
  1335. })
  1336. })
  1337. test("model headers are preserved", async () => {
  1338. await using tmp = await tmpdir({
  1339. init: async (dir) => {
  1340. await Bun.write(
  1341. path.join(dir, "opencode.json"),
  1342. JSON.stringify({
  1343. $schema: "https://opencode.ai/config.json",
  1344. provider: {
  1345. "headers-provider": {
  1346. name: "Headers Provider",
  1347. npm: "@ai-sdk/openai-compatible",
  1348. env: [],
  1349. models: {
  1350. model: {
  1351. name: "Model",
  1352. tool_call: true,
  1353. limit: { context: 4000, output: 1000 },
  1354. headers: {
  1355. "X-Custom-Header": "custom-value",
  1356. Authorization: "Bearer special-token",
  1357. },
  1358. },
  1359. },
  1360. options: { apiKey: "test" },
  1361. },
  1362. },
  1363. }),
  1364. )
  1365. },
  1366. })
  1367. await Instance.provide({
  1368. directory: tmp.path,
  1369. fn: async () => {
  1370. const providers = await Provider.list()
  1371. const model = providers["headers-provider"].models["model"]
  1372. expect(model.headers).toEqual({
  1373. "X-Custom-Header": "custom-value",
  1374. Authorization: "Bearer special-token",
  1375. })
  1376. },
  1377. })
  1378. })
  1379. test("provider env fallback - second env var used if first missing", async () => {
  1380. await using tmp = await tmpdir({
  1381. init: async (dir) => {
  1382. await Bun.write(
  1383. path.join(dir, "opencode.json"),
  1384. JSON.stringify({
  1385. $schema: "https://opencode.ai/config.json",
  1386. provider: {
  1387. "fallback-env": {
  1388. name: "Fallback Env Provider",
  1389. npm: "@ai-sdk/openai-compatible",
  1390. env: ["PRIMARY_KEY", "FALLBACK_KEY"],
  1391. models: {
  1392. model: {
  1393. name: "Model",
  1394. tool_call: true,
  1395. limit: { context: 4000, output: 1000 },
  1396. },
  1397. },
  1398. options: { baseURL: "https://api.example.com" },
  1399. },
  1400. },
  1401. }),
  1402. )
  1403. },
  1404. })
  1405. await Instance.provide({
  1406. directory: tmp.path,
  1407. init: async () => {
  1408. // Only set fallback, not primary
  1409. Env.set("FALLBACK_KEY", "fallback-api-key")
  1410. },
  1411. fn: async () => {
  1412. const providers = await Provider.list()
  1413. // Provider should load because fallback env var is set
  1414. expect(providers["fallback-env"]).toBeDefined()
  1415. },
  1416. })
  1417. })
  1418. test("getModel returns consistent results", async () => {
  1419. await using tmp = await tmpdir({
  1420. init: async (dir) => {
  1421. await Bun.write(
  1422. path.join(dir, "opencode.json"),
  1423. JSON.stringify({
  1424. $schema: "https://opencode.ai/config.json",
  1425. }),
  1426. )
  1427. },
  1428. })
  1429. await Instance.provide({
  1430. directory: tmp.path,
  1431. init: async () => {
  1432. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1433. },
  1434. fn: async () => {
  1435. const model1 = await Provider.getModel("anthropic", "claude-sonnet-4-20250514")
  1436. const model2 = await Provider.getModel("anthropic", "claude-sonnet-4-20250514")
  1437. expect(model1.providerID).toEqual(model2.providerID)
  1438. expect(model1.id).toEqual(model2.id)
  1439. expect(model1).toEqual(model2)
  1440. },
  1441. })
  1442. })
  1443. test("provider name defaults to id when not in database", async () => {
  1444. await using tmp = await tmpdir({
  1445. init: async (dir) => {
  1446. await Bun.write(
  1447. path.join(dir, "opencode.json"),
  1448. JSON.stringify({
  1449. $schema: "https://opencode.ai/config.json",
  1450. provider: {
  1451. "my-custom-id": {
  1452. // no name specified
  1453. npm: "@ai-sdk/openai-compatible",
  1454. env: [],
  1455. models: {
  1456. model: {
  1457. name: "Model",
  1458. tool_call: true,
  1459. limit: { context: 4000, output: 1000 },
  1460. },
  1461. },
  1462. options: { apiKey: "test" },
  1463. },
  1464. },
  1465. }),
  1466. )
  1467. },
  1468. })
  1469. await Instance.provide({
  1470. directory: tmp.path,
  1471. fn: async () => {
  1472. const providers = await Provider.list()
  1473. expect(providers["my-custom-id"].name).toBe("my-custom-id")
  1474. },
  1475. })
  1476. })
  1477. test("ModelNotFoundError includes suggestions for typos", async () => {
  1478. await using tmp = await tmpdir({
  1479. init: async (dir) => {
  1480. await Bun.write(
  1481. path.join(dir, "opencode.json"),
  1482. JSON.stringify({
  1483. $schema: "https://opencode.ai/config.json",
  1484. }),
  1485. )
  1486. },
  1487. })
  1488. await Instance.provide({
  1489. directory: tmp.path,
  1490. init: async () => {
  1491. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1492. },
  1493. fn: async () => {
  1494. try {
  1495. await Provider.getModel("anthropic", "claude-sonet-4") // typo: sonet instead of sonnet
  1496. expect(true).toBe(false) // Should not reach here
  1497. } catch (e: any) {
  1498. expect(e.data.suggestions).toBeDefined()
  1499. expect(e.data.suggestions.length).toBeGreaterThan(0)
  1500. }
  1501. },
  1502. })
  1503. })
  1504. test("ModelNotFoundError for provider includes suggestions", async () => {
  1505. await using tmp = await tmpdir({
  1506. init: async (dir) => {
  1507. await Bun.write(
  1508. path.join(dir, "opencode.json"),
  1509. JSON.stringify({
  1510. $schema: "https://opencode.ai/config.json",
  1511. }),
  1512. )
  1513. },
  1514. })
  1515. await Instance.provide({
  1516. directory: tmp.path,
  1517. init: async () => {
  1518. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1519. },
  1520. fn: async () => {
  1521. try {
  1522. await Provider.getModel("antropic", "claude-sonnet-4") // typo: antropic
  1523. expect(true).toBe(false) // Should not reach here
  1524. } catch (e: any) {
  1525. expect(e.data.suggestions).toBeDefined()
  1526. expect(e.data.suggestions).toContain("anthropic")
  1527. }
  1528. },
  1529. })
  1530. })
  1531. test("getProvider returns undefined for nonexistent provider", async () => {
  1532. await using tmp = await tmpdir({
  1533. init: async (dir) => {
  1534. await Bun.write(
  1535. path.join(dir, "opencode.json"),
  1536. JSON.stringify({
  1537. $schema: "https://opencode.ai/config.json",
  1538. }),
  1539. )
  1540. },
  1541. })
  1542. await Instance.provide({
  1543. directory: tmp.path,
  1544. fn: async () => {
  1545. const provider = await Provider.getProvider("nonexistent")
  1546. expect(provider).toBeUndefined()
  1547. },
  1548. })
  1549. })
  1550. test("getProvider returns provider info", async () => {
  1551. await using tmp = await tmpdir({
  1552. init: async (dir) => {
  1553. await Bun.write(
  1554. path.join(dir, "opencode.json"),
  1555. JSON.stringify({
  1556. $schema: "https://opencode.ai/config.json",
  1557. }),
  1558. )
  1559. },
  1560. })
  1561. await Instance.provide({
  1562. directory: tmp.path,
  1563. init: async () => {
  1564. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1565. },
  1566. fn: async () => {
  1567. const provider = await Provider.getProvider("anthropic")
  1568. expect(provider).toBeDefined()
  1569. expect(provider?.id).toBe("anthropic")
  1570. },
  1571. })
  1572. })
  1573. test("closest returns undefined when no partial match found", async () => {
  1574. await using tmp = await tmpdir({
  1575. init: async (dir) => {
  1576. await Bun.write(
  1577. path.join(dir, "opencode.json"),
  1578. JSON.stringify({
  1579. $schema: "https://opencode.ai/config.json",
  1580. }),
  1581. )
  1582. },
  1583. })
  1584. await Instance.provide({
  1585. directory: tmp.path,
  1586. init: async () => {
  1587. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1588. },
  1589. fn: async () => {
  1590. const result = await Provider.closest("anthropic", ["nonexistent-xyz-model"])
  1591. expect(result).toBeUndefined()
  1592. },
  1593. })
  1594. })
  1595. test("closest checks multiple query terms in order", async () => {
  1596. await using tmp = await tmpdir({
  1597. init: async (dir) => {
  1598. await Bun.write(
  1599. path.join(dir, "opencode.json"),
  1600. JSON.stringify({
  1601. $schema: "https://opencode.ai/config.json",
  1602. }),
  1603. )
  1604. },
  1605. })
  1606. await Instance.provide({
  1607. directory: tmp.path,
  1608. init: async () => {
  1609. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1610. },
  1611. fn: async () => {
  1612. // First term won't match, second will
  1613. const result = await Provider.closest("anthropic", ["nonexistent", "haiku"])
  1614. expect(result).toBeDefined()
  1615. expect(result?.modelID).toContain("haiku")
  1616. },
  1617. })
  1618. })
  1619. test("model limit defaults to zero when not specified", async () => {
  1620. await using tmp = await tmpdir({
  1621. init: async (dir) => {
  1622. await Bun.write(
  1623. path.join(dir, "opencode.json"),
  1624. JSON.stringify({
  1625. $schema: "https://opencode.ai/config.json",
  1626. provider: {
  1627. "no-limit": {
  1628. name: "No Limit Provider",
  1629. npm: "@ai-sdk/openai-compatible",
  1630. env: [],
  1631. models: {
  1632. model: {
  1633. name: "Model",
  1634. tool_call: true,
  1635. // no limit specified
  1636. },
  1637. },
  1638. options: { apiKey: "test" },
  1639. },
  1640. },
  1641. }),
  1642. )
  1643. },
  1644. })
  1645. await Instance.provide({
  1646. directory: tmp.path,
  1647. fn: async () => {
  1648. const providers = await Provider.list()
  1649. const model = providers["no-limit"].models["model"]
  1650. expect(model.limit.context).toBe(0)
  1651. expect(model.limit.output).toBe(0)
  1652. },
  1653. })
  1654. })
  1655. test("provider options are deeply merged", async () => {
  1656. await using tmp = await tmpdir({
  1657. init: async (dir) => {
  1658. await Bun.write(
  1659. path.join(dir, "opencode.json"),
  1660. JSON.stringify({
  1661. $schema: "https://opencode.ai/config.json",
  1662. provider: {
  1663. anthropic: {
  1664. options: {
  1665. headers: {
  1666. "X-Custom": "custom-value",
  1667. },
  1668. timeout: 30000,
  1669. },
  1670. },
  1671. },
  1672. }),
  1673. )
  1674. },
  1675. })
  1676. await Instance.provide({
  1677. directory: tmp.path,
  1678. init: async () => {
  1679. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1680. },
  1681. fn: async () => {
  1682. const providers = await Provider.list()
  1683. // Custom options should be merged
  1684. expect(providers["anthropic"].options.timeout).toBe(30000)
  1685. expect(providers["anthropic"].options.headers["X-Custom"]).toBe("custom-value")
  1686. // anthropic custom loader adds its own headers, they should coexist
  1687. expect(providers["anthropic"].options.headers["anthropic-beta"]).toBeDefined()
  1688. },
  1689. })
  1690. })
  1691. test("custom model inherits npm package from models.dev provider config", async () => {
  1692. await using tmp = await tmpdir({
  1693. init: async (dir) => {
  1694. await Bun.write(
  1695. path.join(dir, "opencode.json"),
  1696. JSON.stringify({
  1697. $schema: "https://opencode.ai/config.json",
  1698. provider: {
  1699. openai: {
  1700. models: {
  1701. "my-custom-model": {
  1702. name: "My Custom Model",
  1703. tool_call: true,
  1704. limit: { context: 8000, output: 2000 },
  1705. },
  1706. },
  1707. },
  1708. },
  1709. }),
  1710. )
  1711. },
  1712. })
  1713. await Instance.provide({
  1714. directory: tmp.path,
  1715. init: async () => {
  1716. Env.set("OPENAI_API_KEY", "test-api-key")
  1717. },
  1718. fn: async () => {
  1719. const providers = await Provider.list()
  1720. const model = providers["openai"].models["my-custom-model"]
  1721. expect(model).toBeDefined()
  1722. expect(model.api.npm).toBe("@ai-sdk/openai")
  1723. },
  1724. })
  1725. })
  1726. test("custom model inherits api.url from models.dev provider", async () => {
  1727. await using tmp = await tmpdir({
  1728. init: async (dir) => {
  1729. await Bun.write(
  1730. path.join(dir, "opencode.json"),
  1731. JSON.stringify({
  1732. $schema: "https://opencode.ai/config.json",
  1733. provider: {
  1734. openrouter: {
  1735. models: {
  1736. "prime-intellect/intellect-3": {},
  1737. "deepseek/deepseek-r1-0528": {
  1738. name: "DeepSeek R1",
  1739. },
  1740. },
  1741. },
  1742. },
  1743. }),
  1744. )
  1745. },
  1746. })
  1747. await Instance.provide({
  1748. directory: tmp.path,
  1749. init: async () => {
  1750. Env.set("OPENROUTER_API_KEY", "test-api-key")
  1751. },
  1752. fn: async () => {
  1753. const providers = await Provider.list()
  1754. expect(providers["openrouter"]).toBeDefined()
  1755. // New model not in database should inherit api.url from provider
  1756. const intellect = providers["openrouter"].models["prime-intellect/intellect-3"]
  1757. expect(intellect).toBeDefined()
  1758. expect(intellect.api.url).toBe("https://openrouter.ai/api/v1")
  1759. // Another new model should also inherit api.url
  1760. const deepseek = providers["openrouter"].models["deepseek/deepseek-r1-0528"]
  1761. expect(deepseek).toBeDefined()
  1762. expect(deepseek.api.url).toBe("https://openrouter.ai/api/v1")
  1763. expect(deepseek.name).toBe("DeepSeek R1")
  1764. },
  1765. })
  1766. })
  1767. test("model variants are generated for reasoning models", async () => {
  1768. await using tmp = await tmpdir({
  1769. init: async (dir) => {
  1770. await Bun.write(
  1771. path.join(dir, "opencode.json"),
  1772. JSON.stringify({
  1773. $schema: "https://opencode.ai/config.json",
  1774. }),
  1775. )
  1776. },
  1777. })
  1778. await Instance.provide({
  1779. directory: tmp.path,
  1780. init: async () => {
  1781. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1782. },
  1783. fn: async () => {
  1784. const providers = await Provider.list()
  1785. // Claude sonnet 4 has reasoning capability
  1786. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1787. expect(model.capabilities.reasoning).toBe(true)
  1788. expect(model.variants).toBeDefined()
  1789. expect(Object.keys(model.variants!).length).toBeGreaterThan(0)
  1790. },
  1791. })
  1792. })
  1793. test("model variants can be disabled via config", async () => {
  1794. await using tmp = await tmpdir({
  1795. init: async (dir) => {
  1796. await Bun.write(
  1797. path.join(dir, "opencode.json"),
  1798. JSON.stringify({
  1799. $schema: "https://opencode.ai/config.json",
  1800. provider: {
  1801. anthropic: {
  1802. models: {
  1803. "claude-sonnet-4-20250514": {
  1804. variants: {
  1805. high: { disabled: true },
  1806. },
  1807. },
  1808. },
  1809. },
  1810. },
  1811. }),
  1812. )
  1813. },
  1814. })
  1815. await Instance.provide({
  1816. directory: tmp.path,
  1817. init: async () => {
  1818. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1819. },
  1820. fn: async () => {
  1821. const providers = await Provider.list()
  1822. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1823. expect(model.variants).toBeDefined()
  1824. expect(model.variants!["high"]).toBeUndefined()
  1825. // max variant should still exist
  1826. expect(model.variants!["max"]).toBeDefined()
  1827. },
  1828. })
  1829. })
  1830. test("model variants can be customized via config", async () => {
  1831. await using tmp = await tmpdir({
  1832. init: async (dir) => {
  1833. await Bun.write(
  1834. path.join(dir, "opencode.json"),
  1835. JSON.stringify({
  1836. $schema: "https://opencode.ai/config.json",
  1837. provider: {
  1838. anthropic: {
  1839. models: {
  1840. "claude-sonnet-4-20250514": {
  1841. variants: {
  1842. high: {
  1843. thinking: {
  1844. type: "enabled",
  1845. budgetTokens: 20000,
  1846. },
  1847. },
  1848. },
  1849. },
  1850. },
  1851. },
  1852. },
  1853. }),
  1854. )
  1855. },
  1856. })
  1857. await Instance.provide({
  1858. directory: tmp.path,
  1859. init: async () => {
  1860. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1861. },
  1862. fn: async () => {
  1863. const providers = await Provider.list()
  1864. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1865. expect(model.variants!["high"]).toBeDefined()
  1866. expect(model.variants!["high"].thinking.budgetTokens).toBe(20000)
  1867. },
  1868. })
  1869. })
  1870. test("disabled key is stripped from variant config", async () => {
  1871. await using tmp = await tmpdir({
  1872. init: async (dir) => {
  1873. await Bun.write(
  1874. path.join(dir, "opencode.json"),
  1875. JSON.stringify({
  1876. $schema: "https://opencode.ai/config.json",
  1877. provider: {
  1878. anthropic: {
  1879. models: {
  1880. "claude-sonnet-4-20250514": {
  1881. variants: {
  1882. max: {
  1883. disabled: false,
  1884. customField: "test",
  1885. },
  1886. },
  1887. },
  1888. },
  1889. },
  1890. },
  1891. }),
  1892. )
  1893. },
  1894. })
  1895. await Instance.provide({
  1896. directory: tmp.path,
  1897. init: async () => {
  1898. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1899. },
  1900. fn: async () => {
  1901. const providers = await Provider.list()
  1902. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1903. expect(model.variants!["max"]).toBeDefined()
  1904. expect(model.variants!["max"].disabled).toBeUndefined()
  1905. expect(model.variants!["max"].customField).toBe("test")
  1906. },
  1907. })
  1908. })
  1909. test("all variants can be disabled via config", async () => {
  1910. await using tmp = await tmpdir({
  1911. init: async (dir) => {
  1912. await Bun.write(
  1913. path.join(dir, "opencode.json"),
  1914. JSON.stringify({
  1915. $schema: "https://opencode.ai/config.json",
  1916. provider: {
  1917. anthropic: {
  1918. models: {
  1919. "claude-sonnet-4-20250514": {
  1920. variants: {
  1921. high: { disabled: true },
  1922. max: { disabled: true },
  1923. },
  1924. },
  1925. },
  1926. },
  1927. },
  1928. }),
  1929. )
  1930. },
  1931. })
  1932. await Instance.provide({
  1933. directory: tmp.path,
  1934. init: async () => {
  1935. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1936. },
  1937. fn: async () => {
  1938. const providers = await Provider.list()
  1939. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1940. expect(model.variants).toBeDefined()
  1941. expect(Object.keys(model.variants!).length).toBe(0)
  1942. },
  1943. })
  1944. })
  1945. test("variant config merges with generated variants", async () => {
  1946. await using tmp = await tmpdir({
  1947. init: async (dir) => {
  1948. await Bun.write(
  1949. path.join(dir, "opencode.json"),
  1950. JSON.stringify({
  1951. $schema: "https://opencode.ai/config.json",
  1952. provider: {
  1953. anthropic: {
  1954. models: {
  1955. "claude-sonnet-4-20250514": {
  1956. variants: {
  1957. high: {
  1958. extraOption: "custom-value",
  1959. },
  1960. },
  1961. },
  1962. },
  1963. },
  1964. },
  1965. }),
  1966. )
  1967. },
  1968. })
  1969. await Instance.provide({
  1970. directory: tmp.path,
  1971. init: async () => {
  1972. Env.set("ANTHROPIC_API_KEY", "test-api-key")
  1973. },
  1974. fn: async () => {
  1975. const providers = await Provider.list()
  1976. const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
  1977. expect(model.variants!["high"]).toBeDefined()
  1978. // Should have both the generated thinking config and the custom option
  1979. expect(model.variants!["high"].thinking).toBeDefined()
  1980. expect(model.variants!["high"].extraOption).toBe("custom-value")
  1981. },
  1982. })
  1983. })
  1984. test("variants filtered in second pass for database models", async () => {
  1985. await using tmp = await tmpdir({
  1986. init: async (dir) => {
  1987. await Bun.write(
  1988. path.join(dir, "opencode.json"),
  1989. JSON.stringify({
  1990. $schema: "https://opencode.ai/config.json",
  1991. provider: {
  1992. openai: {
  1993. models: {
  1994. "gpt-5": {
  1995. variants: {
  1996. high: { disabled: true },
  1997. },
  1998. },
  1999. },
  2000. },
  2001. },
  2002. }),
  2003. )
  2004. },
  2005. })
  2006. await Instance.provide({
  2007. directory: tmp.path,
  2008. init: async () => {
  2009. Env.set("OPENAI_API_KEY", "test-api-key")
  2010. },
  2011. fn: async () => {
  2012. const providers = await Provider.list()
  2013. const model = providers["openai"].models["gpt-5"]
  2014. expect(model.variants).toBeDefined()
  2015. expect(model.variants!["high"]).toBeUndefined()
  2016. // Other variants should still exist
  2017. expect(model.variants!["medium"]).toBeDefined()
  2018. },
  2019. })
  2020. })
  2021. test("custom model with variants enabled and disabled", async () => {
  2022. await using tmp = await tmpdir({
  2023. init: async (dir) => {
  2024. await Bun.write(
  2025. path.join(dir, "opencode.json"),
  2026. JSON.stringify({
  2027. $schema: "https://opencode.ai/config.json",
  2028. provider: {
  2029. "custom-reasoning": {
  2030. name: "Custom Reasoning Provider",
  2031. npm: "@ai-sdk/openai-compatible",
  2032. env: [],
  2033. models: {
  2034. "reasoning-model": {
  2035. name: "Reasoning Model",
  2036. tool_call: true,
  2037. reasoning: true,
  2038. limit: { context: 128000, output: 16000 },
  2039. variants: {
  2040. low: { reasoningEffort: "low" },
  2041. medium: { reasoningEffort: "medium" },
  2042. high: { reasoningEffort: "high", disabled: true },
  2043. custom: { reasoningEffort: "custom", budgetTokens: 5000 },
  2044. },
  2045. },
  2046. },
  2047. options: { apiKey: "test-key" },
  2048. },
  2049. },
  2050. }),
  2051. )
  2052. },
  2053. })
  2054. await Instance.provide({
  2055. directory: tmp.path,
  2056. fn: async () => {
  2057. const providers = await Provider.list()
  2058. const model = providers["custom-reasoning"].models["reasoning-model"]
  2059. expect(model.variants).toBeDefined()
  2060. // Enabled variants should exist
  2061. expect(model.variants!["low"]).toBeDefined()
  2062. expect(model.variants!["low"].reasoningEffort).toBe("low")
  2063. expect(model.variants!["medium"]).toBeDefined()
  2064. expect(model.variants!["medium"].reasoningEffort).toBe("medium")
  2065. expect(model.variants!["custom"]).toBeDefined()
  2066. expect(model.variants!["custom"].reasoningEffort).toBe("custom")
  2067. expect(model.variants!["custom"].budgetTokens).toBe(5000)
  2068. // Disabled variant should not exist
  2069. expect(model.variants!["high"]).toBeUndefined()
  2070. // disabled key should be stripped from all variants
  2071. expect(model.variants!["low"].disabled).toBeUndefined()
  2072. expect(model.variants!["medium"].disabled).toBeUndefined()
  2073. expect(model.variants!["custom"].disabled).toBeUndefined()
  2074. },
  2075. })
  2076. })