provider.test.ts 62 KB

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