@srouterhq/server 0.0.0-stage → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1027) hide show
  1. package/LICENSE +21 -0
  2. package/client/dist/assets/am-TQ7Jmdqe.js +1 -0
  3. package/client/dist/assets/ar-Bf5a7yfC.js +1 -0
  4. package/client/dist/assets/az-CrosE1xH.js +1 -0
  5. package/client/dist/assets/bg-BvWnmcUz.js +1 -0
  6. package/client/dist/assets/bn-ams5TsJp.js +1 -0
  7. package/client/dist/assets/cs-Dh67CaJ_.js +1 -0
  8. package/client/dist/assets/da-9WlT8Iq7.js +1 -0
  9. package/client/dist/assets/de-lWhJJzz7.js +1 -0
  10. package/client/dist/assets/el-Cu7_GhNq.js +1 -0
  11. package/client/dist/assets/es-BZuzsBcP.js +1 -0
  12. package/client/dist/assets/fa-Dwdn-jKS.js +1 -0
  13. package/client/dist/assets/fi-BFrFyOTy.js +1 -0
  14. package/client/dist/assets/fr-XhbtmPpj.js +1 -0
  15. package/client/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
  16. package/client/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
  17. package/client/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
  18. package/client/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
  19. package/client/dist/assets/geist-mono-cyrillic-ext-wght-normal-I4S5GZfc.woff2 +0 -0
  20. package/client/dist/assets/geist-mono-cyrillic-wght-normal-BmXc_FBt.woff2 +0 -0
  21. package/client/dist/assets/geist-mono-latin-ext-wght-normal-DrnZ1wKl.woff2 +0 -0
  22. package/client/dist/assets/geist-mono-latin-wght-normal-B_7UjwxQ.woff2 +0 -0
  23. package/client/dist/assets/geist-mono-symbols2-wght-normal-GZpp1pK2.woff2 +0 -0
  24. package/client/dist/assets/geist-mono-vietnamese-wght-normal-D8KDMBhC.woff2 +0 -0
  25. package/client/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
  26. package/client/dist/assets/gu-D0uhLUB8.js +1 -0
  27. package/client/dist/assets/ha-CVlm9pqp.js +1 -0
  28. package/client/dist/assets/he-CMIFeoWr.js +1 -0
  29. package/client/dist/assets/hi-ENTwhBpn.js +1 -0
  30. package/client/dist/assets/hr-fc03vMy_.js +1 -0
  31. package/client/dist/assets/hu-YBo2BIYt.js +1 -0
  32. package/client/dist/assets/id-BEWXYX69.js +1 -0
  33. package/client/dist/assets/ig-BKLZFKka.js +1 -0
  34. package/client/dist/assets/index-D9I4KtvR.js +145 -0
  35. package/client/dist/assets/index-txhEmCdb.css +2 -0
  36. package/client/dist/assets/it-Bzmei1Y2.js +1 -0
  37. package/client/dist/assets/ja-BfcpuqEl.js +1 -0
  38. package/client/dist/assets/ka-CnWjRgLo.js +1 -0
  39. package/client/dist/assets/km-BAjPR3Qj.js +1 -0
  40. package/client/dist/assets/kn-B15WKU5m.js +1 -0
  41. package/client/dist/assets/ko-C61fAB-w.js +1 -0
  42. package/client/dist/assets/lt-CYTr4_YW.js +1 -0
  43. package/client/dist/assets/ml-CtqxgyH3.js +1 -0
  44. package/client/dist/assets/mr-X5TMY7aR.js +1 -0
  45. package/client/dist/assets/ms-DSg8RqPS.js +1 -0
  46. package/client/dist/assets/my-D7RqR8uE.js +1 -0
  47. package/client/dist/assets/ne-DaflxkDj.js +1 -0
  48. package/client/dist/assets/nl-BOqqtsC8.js +1 -0
  49. package/client/dist/assets/no-BSHp_StB.js +1 -0
  50. package/client/dist/assets/or-BkLR9Vub.js +1 -0
  51. package/client/dist/assets/pa-CarJ2XgI.js +1 -0
  52. package/client/dist/assets/pl-C2n0V-0k.js +1 -0
  53. package/client/dist/assets/pt-BR-CqJdNhg2.js +1 -0
  54. package/client/dist/assets/pt-PT-C_HpMyKz.js +1 -0
  55. package/client/dist/assets/ro-DVRZqSAS.js +1 -0
  56. package/client/dist/assets/ru-DwAJG7VC.js +1 -0
  57. package/client/dist/assets/si-BxUezXm0.js +1 -0
  58. package/client/dist/assets/sk-eI1VcIfY.js +1 -0
  59. package/client/dist/assets/sr-CJF3auCr.js +1 -0
  60. package/client/dist/assets/srouter-logo-C6ZfjGIi.svg +14 -0
  61. package/client/dist/assets/sv-nqvwHAq_.js +1 -0
  62. package/client/dist/assets/sw-DWynE2pW.js +1 -0
  63. package/client/dist/assets/ta-BVM12knC.js +1 -0
  64. package/client/dist/assets/te-oyzEkF7_.js +1 -0
  65. package/client/dist/assets/th-COVeE0ZQ.js +1 -0
  66. package/client/dist/assets/tl-DsJANnQz.js +1 -0
  67. package/client/dist/assets/tr-B-Fl-urO.js +1 -0
  68. package/client/dist/assets/uk-C5qketAO.js +1 -0
  69. package/client/dist/assets/ur-QU9KugfV.js +1 -0
  70. package/client/dist/assets/uz-DL9cG_mY.js +1 -0
  71. package/client/dist/assets/vi-CQiVy3Pu.js +1 -0
  72. package/client/dist/assets/yo-DqazMdrZ.js +1 -0
  73. package/client/dist/assets/zh-CN-DnYL_Kio.js +1 -0
  74. package/client/dist/assets/zh-TW-5bdbosKj.js +1 -0
  75. package/client/dist/favicon.svg +14 -0
  76. package/client/dist/icons.svg +24 -0
  77. package/client/dist/index.html +37 -0
  78. package/dist/app.d.ts +4 -0
  79. package/dist/app.d.ts.map +1 -0
  80. package/dist/app.js +372 -0
  81. package/dist/app.js.map +1 -0
  82. package/dist/db/index.d.ts +49 -0
  83. package/dist/db/index.d.ts.map +1 -0
  84. package/dist/db/index.js +203 -0
  85. package/dist/db/index.js.map +1 -0
  86. package/dist/db/migrate/TEMPLATE.d.ts +4 -0
  87. package/dist/db/migrate/TEMPLATE.d.ts.map +1 -0
  88. package/dist/db/migrate/TEMPLATE.js +18 -0
  89. package/dist/db/migrate/TEMPLATE.js.map +1 -0
  90. package/dist/db/migrate/cli.d.ts +2 -0
  91. package/dist/db/migrate/cli.d.ts.map +1 -0
  92. package/dist/db/migrate/cli.js +177 -0
  93. package/dist/db/migrate/cli.js.map +1 -0
  94. package/dist/db/migrate/defaults.d.ts +52 -0
  95. package/dist/db/migrate/defaults.d.ts.map +1 -0
  96. package/dist/db/migrate/defaults.js +126 -0
  97. package/dist/db/migrate/defaults.js.map +1 -0
  98. package/dist/db/migrate/runner.d.ts +16 -0
  99. package/dist/db/migrate/runner.d.ts.map +1 -0
  100. package/dist/db/migrate/runner.js +178 -0
  101. package/dist/db/migrate/runner.js.map +1 -0
  102. package/dist/db/migrations/20260101_000000_legacy_baseline.d.ts +4 -0
  103. package/dist/db/migrations/20260101_000000_legacy_baseline.d.ts.map +1 -0
  104. package/dist/db/migrations/20260101_000000_legacy_baseline.js +2240 -0
  105. package/dist/db/migrations/20260101_000000_legacy_baseline.js.map +1 -0
  106. package/dist/db/migrations/20260627_000001_custom_provider_modalities.d.ts +4 -0
  107. package/dist/db/migrations/20260627_000001_custom_provider_modalities.d.ts.map +1 -0
  108. package/dist/db/migrations/20260627_000001_custom_provider_modalities.js +27 -0
  109. package/dist/db/migrations/20260627_000001_custom_provider_modalities.js.map +1 -0
  110. package/dist/db/migrations/20260627_000002_catalog_model_state.d.ts +4 -0
  111. package/dist/db/migrations/20260627_000002_catalog_model_state.d.ts.map +1 -0
  112. package/dist/db/migrations/20260627_000002_catalog_model_state.js +30 -0
  113. package/dist/db/migrations/20260627_000002_catalog_model_state.js.map +1 -0
  114. package/dist/db/migrations/20260628_120000_request_aggregates.d.ts +6 -0
  115. package/dist/db/migrations/20260628_120000_request_aggregates.d.ts.map +1 -0
  116. package/dist/db/migrations/20260628_120000_request_aggregates.js +104 -0
  117. package/dist/db/migrations/20260628_120000_request_aggregates.js.map +1 -0
  118. package/dist/db/migrations/20260630_000001_github_gpt41_context.d.ts +9 -0
  119. package/dist/db/migrations/20260630_000001_github_gpt41_context.d.ts.map +1 -0
  120. package/dist/db/migrations/20260630_000001_github_gpt41_context.js +22 -0
  121. package/dist/db/migrations/20260630_000001_github_gpt41_context.js.map +1 -0
  122. package/dist/db/migrations/20260706_000001_request_client_info.d.ts +4 -0
  123. package/dist/db/migrations/20260706_000001_request_client_info.d.ts.map +1 -0
  124. package/dist/db/migrations/20260706_000001_request_client_info.js +30 -0
  125. package/dist/db/migrations/20260706_000001_request_client_info.js.map +1 -0
  126. package/dist/db/migrations/20260706_000002_custom_model_tool_support.d.ts +17 -0
  127. package/dist/db/migrations/20260706_000002_custom_model_tool_support.d.ts.map +1 -0
  128. package/dist/db/migrations/20260706_000002_custom_model_tool_support.js +20 -0
  129. package/dist/db/migrations/20260706_000002_custom_model_tool_support.js.map +1 -0
  130. package/dist/db/migrations/20260714_000001_profile_chain_backfill.d.ts +12 -0
  131. package/dist/db/migrations/20260714_000001_profile_chain_backfill.d.ts.map +1 -0
  132. package/dist/db/migrations/20260714_000001_profile_chain_backfill.js +45 -0
  133. package/dist/db/migrations/20260714_000001_profile_chain_backfill.js.map +1 -0
  134. package/dist/db/migrations/20260720_000001_key_health_error.d.ts +5 -0
  135. package/dist/db/migrations/20260720_000001_key_health_error.d.ts.map +1 -0
  136. package/dist/db/migrations/20260720_000001_key_health_error.js +16 -0
  137. package/dist/db/migrations/20260720_000001_key_health_error.js.map +1 -0
  138. package/dist/db/migrations/20260726_000001_cooldown_probe_provenance.d.ts +4 -0
  139. package/dist/db/migrations/20260726_000001_cooldown_probe_provenance.d.ts.map +1 -0
  140. package/dist/db/migrations/20260726_000001_cooldown_probe_provenance.js +38 -0
  141. package/dist/db/migrations/20260726_000001_cooldown_probe_provenance.js.map +1 -0
  142. package/dist/db/migrations/20260726_000002_request_attempts.d.ts +4 -0
  143. package/dist/db/migrations/20260726_000002_request_attempts.d.ts.map +1 -0
  144. package/dist/db/migrations/20260726_000002_request_attempts.js +43 -0
  145. package/dist/db/migrations/20260726_000002_request_attempts.js.map +1 -0
  146. package/dist/db/migrations/20260726_000003_model_source_provenance.d.ts +4 -0
  147. package/dist/db/migrations/20260726_000003_model_source_provenance.d.ts.map +1 -0
  148. package/dist/db/migrations/20260726_000003_model_source_provenance.js +80 -0
  149. package/dist/db/migrations/20260726_000003_model_source_provenance.js.map +1 -0
  150. package/dist/db/migrations/20260726_000004_media_model_meta.d.ts +4 -0
  151. package/dist/db/migrations/20260726_000004_media_model_meta.d.ts.map +1 -0
  152. package/dist/db/migrations/20260726_000004_media_model_meta.js +34 -0
  153. package/dist/db/migrations/20260726_000004_media_model_meta.js.map +1 -0
  154. package/dist/db/migrations/20260726_000005_request_served_model.d.ts +4 -0
  155. package/dist/db/migrations/20260726_000005_request_served_model.d.ts.map +1 -0
  156. package/dist/db/migrations/20260726_000005_request_served_model.js +31 -0
  157. package/dist/db/migrations/20260726_000005_request_served_model.js.map +1 -0
  158. package/dist/db/migrations/20260726_000006_attempt_error_summary.d.ts +4 -0
  159. package/dist/db/migrations/20260726_000006_attempt_error_summary.d.ts.map +1 -0
  160. package/dist/db/migrations/20260726_000006_attempt_error_summary.js +29 -0
  161. package/dist/db/migrations/20260726_000006_attempt_error_summary.js.map +1 -0
  162. package/dist/db/migrations/20260727_000001_agent_compatibility.d.ts +4 -0
  163. package/dist/db/migrations/20260727_000001_agent_compatibility.d.ts.map +1 -0
  164. package/dist/db/migrations/20260727_000001_agent_compatibility.js +41 -0
  165. package/dist/db/migrations/20260727_000001_agent_compatibility.js.map +1 -0
  166. package/dist/db/migrations/20260728_000001_tombstone_provenance.d.ts +12 -0
  167. package/dist/db/migrations/20260728_000001_tombstone_provenance.d.ts.map +1 -0
  168. package/dist/db/migrations/20260728_000001_tombstone_provenance.js +37 -0
  169. package/dist/db/migrations/20260728_000001_tombstone_provenance.js.map +1 -0
  170. package/dist/db/migrations/20260729_000001_custom_model_endpoint_identity.d.ts +4 -0
  171. package/dist/db/migrations/20260729_000001_custom_model_endpoint_identity.d.ts.map +1 -0
  172. package/dist/db/migrations/20260729_000001_custom_model_endpoint_identity.js +145 -0
  173. package/dist/db/migrations/20260729_000001_custom_model_endpoint_identity.js.map +1 -0
  174. package/dist/db/migrations/20260802_000001_custom_endpoint_host_labels.d.ts +6 -0
  175. package/dist/db/migrations/20260802_000001_custom_endpoint_host_labels.d.ts.map +1 -0
  176. package/dist/db/migrations/20260802_000001_custom_endpoint_host_labels.js +52 -0
  177. package/dist/db/migrations/20260802_000001_custom_endpoint_host_labels.js.map +1 -0
  178. package/dist/db/migrations/20260805_000001_key_model_scope.d.ts +6 -0
  179. package/dist/db/migrations/20260805_000001_key_model_scope.d.ts.map +1 -0
  180. package/dist/db/migrations/20260805_000001_key_model_scope.js +17 -0
  181. package/dist/db/migrations/20260805_000001_key_model_scope.js.map +1 -0
  182. package/dist/db/migrations/20260805_000002_client_profiles.d.ts +4 -0
  183. package/dist/db/migrations/20260805_000002_client_profiles.d.ts.map +1 -0
  184. package/dist/db/migrations/20260805_000002_client_profiles.js +33 -0
  185. package/dist/db/migrations/20260805_000002_client_profiles.js.map +1 -0
  186. package/dist/db/migrations/20260810_000001_api_key_proxy.d.ts +4 -0
  187. package/dist/db/migrations/20260810_000001_api_key_proxy.d.ts.map +1 -0
  188. package/dist/db/migrations/20260810_000001_api_key_proxy.js +33 -0
  189. package/dist/db/migrations/20260810_000001_api_key_proxy.js.map +1 -0
  190. package/dist/db/migrations/20260819_000001_custom_model_tombstones.d.ts +4 -0
  191. package/dist/db/migrations/20260819_000001_custom_model_tombstones.d.ts.map +1 -0
  192. package/dist/db/migrations/20260819_000001_custom_model_tombstones.js +19 -0
  193. package/dist/db/migrations/20260819_000001_custom_model_tombstones.js.map +1 -0
  194. package/dist/db/migrations/20260820_000001_playground_conversations.d.ts +4 -0
  195. package/dist/db/migrations/20260820_000001_playground_conversations.d.ts.map +1 -0
  196. package/dist/db/migrations/20260820_000001_playground_conversations.js +47 -0
  197. package/dist/db/migrations/20260820_000001_playground_conversations.js.map +1 -0
  198. package/dist/db/migrations/20260823_000001_server_logs.d.ts +4 -0
  199. package/dist/db/migrations/20260823_000001_server_logs.d.ts.map +1 -0
  200. package/dist/db/migrations/20260823_000001_server_logs.js +53 -0
  201. package/dist/db/migrations/20260823_000001_server_logs.js.map +1 -0
  202. package/dist/db/migrations/20260823_000002_backups_table.d.ts +8 -0
  203. package/dist/db/migrations/20260823_000002_backups_table.d.ts.map +1 -0
  204. package/dist/db/migrations/20260823_000002_backups_table.js +22 -0
  205. package/dist/db/migrations/20260823_000002_backups_table.js.map +1 -0
  206. package/dist/db/migrations/20260823_000003_attempt_key_label.d.ts +19 -0
  207. package/dist/db/migrations/20260823_000003_attempt_key_label.d.ts.map +1 -0
  208. package/dist/db/migrations/20260823_000003_attempt_key_label.js +28 -0
  209. package/dist/db/migrations/20260823_000003_attempt_key_label.js.map +1 -0
  210. package/dist/db/migrations/20260823_000004_profile_auto_include.d.ts +14 -0
  211. package/dist/db/migrations/20260823_000004_profile_auto_include.d.ts.map +1 -0
  212. package/dist/db/migrations/20260823_000004_profile_auto_include.js +25 -0
  213. package/dist/db/migrations/20260823_000004_profile_auto_include.js.map +1 -0
  214. package/dist/db/migrations/20260901_000001_idempotency_claims.d.ts +4 -0
  215. package/dist/db/migrations/20260901_000001_idempotency_claims.d.ts.map +1 -0
  216. package/dist/db/migrations/20260901_000001_idempotency_claims.js +46 -0
  217. package/dist/db/migrations/20260901_000001_idempotency_claims.js.map +1 -0
  218. package/dist/db/migrations/20260901_000002_quota_observation_lookup.d.ts +4 -0
  219. package/dist/db/migrations/20260901_000002_quota_observation_lookup.d.ts.map +1 -0
  220. package/dist/db/migrations/20260901_000002_quota_observation_lookup.js +37 -0
  221. package/dist/db/migrations/20260901_000002_quota_observation_lookup.js.map +1 -0
  222. package/dist/db/migrations/20260901_000003_request_caller.d.ts +4 -0
  223. package/dist/db/migrations/20260901_000003_request_caller.d.ts.map +1 -0
  224. package/dist/db/migrations/20260901_000003_request_caller.js +27 -0
  225. package/dist/db/migrations/20260901_000003_request_caller.js.map +1 -0
  226. package/dist/db/migrations/20260902_000001_analytics_latency_percentile_index.d.ts +4 -0
  227. package/dist/db/migrations/20260902_000001_analytics_latency_percentile_index.d.ts.map +1 -0
  228. package/dist/db/migrations/20260902_000001_analytics_latency_percentile_index.js +35 -0
  229. package/dist/db/migrations/20260902_000001_analytics_latency_percentile_index.js.map +1 -0
  230. package/dist/db/migrations/20260903_000001_mcp_enabled_default.d.ts +4 -0
  231. package/dist/db/migrations/20260903_000001_mcp_enabled_default.d.ts.map +1 -0
  232. package/dist/db/migrations/20260903_000001_mcp_enabled_default.js +37 -0
  233. package/dist/db/migrations/20260903_000001_mcp_enabled_default.js.map +1 -0
  234. package/dist/db/migrations/20260903_000002_response_cache.d.ts +4 -0
  235. package/dist/db/migrations/20260903_000002_response_cache.d.ts.map +1 -0
  236. package/dist/db/migrations/20260903_000002_response_cache.js +53 -0
  237. package/dist/db/migrations/20260903_000002_response_cache.js.map +1 -0
  238. package/dist/db/migrations/20260904_000001_key_monthly_budget.d.ts +13 -0
  239. package/dist/db/migrations/20260904_000001_key_monthly_budget.d.ts.map +1 -0
  240. package/dist/db/migrations/20260904_000001_key_monthly_budget.js +35 -0
  241. package/dist/db/migrations/20260904_000001_key_monthly_budget.js.map +1 -0
  242. package/dist/db/migrations/20260913_000001_request_model_attribution.d.ts +4 -0
  243. package/dist/db/migrations/20260913_000001_request_model_attribution.d.ts.map +1 -0
  244. package/dist/db/migrations/20260913_000001_request_model_attribution.js +68 -0
  245. package/dist/db/migrations/20260913_000001_request_model_attribution.js.map +1 -0
  246. package/dist/db/migrations/20260914_000001_key_monthly_usage.d.ts +5 -0
  247. package/dist/db/migrations/20260914_000001_key_monthly_usage.d.ts.map +1 -0
  248. package/dist/db/migrations/20260914_000001_key_monthly_usage.js +37 -0
  249. package/dist/db/migrations/20260914_000001_key_monthly_usage.js.map +1 -0
  250. package/dist/db/migrations/20260915_000001_quota_snapshot_freshness.d.ts +4 -0
  251. package/dist/db/migrations/20260915_000001_quota_snapshot_freshness.d.ts.map +1 -0
  252. package/dist/db/migrations/20260915_000001_quota_snapshot_freshness.js +28 -0
  253. package/dist/db/migrations/20260915_000001_quota_snapshot_freshness.js.map +1 -0
  254. package/dist/db/migrations/20261004_000001_unified_key_sr_prefix.d.ts +4 -0
  255. package/dist/db/migrations/20261004_000001_unified_key_sr_prefix.d.ts.map +1 -0
  256. package/dist/db/migrations/20261004_000001_unified_key_sr_prefix.js +25 -0
  257. package/dist/db/migrations/20261004_000001_unified_key_sr_prefix.js.map +1 -0
  258. package/dist/db/migrations/20261005_000001_key_budget_period.d.ts +10 -0
  259. package/dist/db/migrations/20261005_000001_key_budget_period.d.ts.map +1 -0
  260. package/dist/db/migrations/20261005_000001_key_budget_period.js +78 -0
  261. package/dist/db/migrations/20261005_000001_key_budget_period.js.map +1 -0
  262. package/dist/db/migrations/20261005_000002_key_budget_period_auto.d.ts +21 -0
  263. package/dist/db/migrations/20261005_000002_key_budget_period_auto.d.ts.map +1 -0
  264. package/dist/db/migrations/20261005_000002_key_budget_period_auto.js +41 -0
  265. package/dist/db/migrations/20261005_000002_key_budget_period_auto.js.map +1 -0
  266. package/dist/db/model-pricing.d.ts +39 -0
  267. package/dist/db/model-pricing.d.ts.map +1 -0
  268. package/dist/db/model-pricing.js +274 -0
  269. package/dist/db/model-pricing.js.map +1 -0
  270. package/dist/db/node-sqlite.d.ts +9 -0
  271. package/dist/db/node-sqlite.d.ts.map +1 -0
  272. package/dist/db/node-sqlite.js +87 -0
  273. package/dist/db/node-sqlite.js.map +1 -0
  274. package/dist/db/types.d.ts +22 -0
  275. package/dist/db/types.d.ts.map +1 -0
  276. package/dist/db/types.js +2 -0
  277. package/dist/db/types.js.map +1 -0
  278. package/dist/docs/docs-page.d.ts +2 -0
  279. package/dist/docs/docs-page.d.ts.map +1 -0
  280. package/dist/docs/docs-page.js +319 -0
  281. package/dist/docs/docs-page.js.map +1 -0
  282. package/dist/docs/openapi.d.ts +1896 -0
  283. package/dist/docs/openapi.d.ts.map +1 -0
  284. package/dist/docs/openapi.js +1096 -0
  285. package/dist/docs/openapi.js.map +1 -0
  286. package/dist/env.d.ts +2 -0
  287. package/dist/env.d.ts.map +1 -0
  288. package/dist/env.js +9 -0
  289. package/dist/env.js.map +1 -0
  290. package/dist/index.d.ts +2 -0
  291. package/dist/index.d.ts.map +1 -0
  292. package/dist/index.js +144 -0
  293. package/dist/index.js.map +1 -0
  294. package/dist/lib/anthropic-documents.d.ts +41 -0
  295. package/dist/lib/anthropic-documents.d.ts.map +1 -0
  296. package/dist/lib/anthropic-documents.js +132 -0
  297. package/dist/lib/anthropic-documents.js.map +1 -0
  298. package/dist/lib/app-version.d.ts +5 -0
  299. package/dist/lib/app-version.d.ts.map +1 -0
  300. package/dist/lib/app-version.js +61 -0
  301. package/dist/lib/app-version.js.map +1 -0
  302. package/dist/lib/attempt-trace.d.ts +23 -0
  303. package/dist/lib/attempt-trace.d.ts.map +1 -0
  304. package/dist/lib/attempt-trace.js +32 -0
  305. package/dist/lib/attempt-trace.js.map +1 -0
  306. package/dist/lib/budget.d.ts +6 -0
  307. package/dist/lib/budget.d.ts.map +1 -0
  308. package/dist/lib/budget.js +40 -0
  309. package/dist/lib/budget.js.map +1 -0
  310. package/dist/lib/client-classifier.d.ts +10 -0
  311. package/dist/lib/client-classifier.d.ts.map +1 -0
  312. package/dist/lib/client-classifier.js +126 -0
  313. package/dist/lib/client-classifier.js.map +1 -0
  314. package/dist/lib/client-context.d.ts +10 -0
  315. package/dist/lib/client-context.d.ts.map +1 -0
  316. package/dist/lib/client-context.js +39 -0
  317. package/dist/lib/client-context.js.map +1 -0
  318. package/dist/lib/config.d.ts +33 -0
  319. package/dist/lib/config.d.ts.map +1 -0
  320. package/dist/lib/config.js +94 -0
  321. package/dist/lib/config.js.map +1 -0
  322. package/dist/lib/content.d.ts +33 -0
  323. package/dist/lib/content.d.ts.map +1 -0
  324. package/dist/lib/content.js +201 -0
  325. package/dist/lib/content.js.map +1 -0
  326. package/dist/lib/credential.d.ts +11 -0
  327. package/dist/lib/credential.d.ts.map +1 -0
  328. package/dist/lib/credential.js +18 -0
  329. package/dist/lib/credential.js.map +1 -0
  330. package/dist/lib/crypto.d.ts +31 -0
  331. package/dist/lib/crypto.d.ts.map +1 -0
  332. package/dist/lib/crypto.js +206 -0
  333. package/dist/lib/crypto.js.map +1 -0
  334. package/dist/lib/custom-provider-cleanup.d.ts +13 -0
  335. package/dist/lib/custom-provider-cleanup.d.ts.map +1 -0
  336. package/dist/lib/custom-provider-cleanup.js +43 -0
  337. package/dist/lib/custom-provider-cleanup.js.map +1 -0
  338. package/dist/lib/db-backup.d.ts +31 -0
  339. package/dist/lib/db-backup.d.ts.map +1 -0
  340. package/dist/lib/db-backup.js +255 -0
  341. package/dist/lib/db-backup.js.map +1 -0
  342. package/dist/lib/endpoint-scope.d.ts +42 -0
  343. package/dist/lib/endpoint-scope.d.ts.map +1 -0
  344. package/dist/lib/endpoint-scope.js +95 -0
  345. package/dist/lib/endpoint-scope.js.map +1 -0
  346. package/dist/lib/env-drift.d.ts +17 -0
  347. package/dist/lib/env-drift.d.ts.map +1 -0
  348. package/dist/lib/env-drift.js +108 -0
  349. package/dist/lib/env-drift.js.map +1 -0
  350. package/dist/lib/error-classify.d.ts +40 -0
  351. package/dist/lib/error-classify.d.ts.map +1 -0
  352. package/dist/lib/error-classify.js +687 -0
  353. package/dist/lib/error-classify.js.map +1 -0
  354. package/dist/lib/error-redaction.d.ts +8 -0
  355. package/dist/lib/error-redaction.d.ts.map +1 -0
  356. package/dist/lib/error-redaction.js +45 -0
  357. package/dist/lib/error-redaction.js.map +1 -0
  358. package/dist/lib/fallback-loop.d.ts +306 -0
  359. package/dist/lib/fallback-loop.d.ts.map +1 -0
  360. package/dist/lib/fallback-loop.js +1336 -0
  361. package/dist/lib/fallback-loop.js.map +1 -0
  362. package/dist/lib/file-permissions.d.ts +98 -0
  363. package/dist/lib/file-permissions.d.ts.map +1 -0
  364. package/dist/lib/file-permissions.js +159 -0
  365. package/dist/lib/file-permissions.js.map +1 -0
  366. package/dist/lib/gemini-wire.d.ts +76 -0
  367. package/dist/lib/gemini-wire.d.ts.map +1 -0
  368. package/dist/lib/gemini-wire.js +392 -0
  369. package/dist/lib/gemini-wire.js.map +1 -0
  370. package/dist/lib/guardrails.d.ts +37 -0
  371. package/dist/lib/guardrails.d.ts.map +1 -0
  372. package/dist/lib/guardrails.js +106 -0
  373. package/dist/lib/guardrails.js.map +1 -0
  374. package/dist/lib/header-value.d.ts +8 -0
  375. package/dist/lib/header-value.d.ts.map +1 -0
  376. package/dist/lib/header-value.js +55 -0
  377. package/dist/lib/header-value.js.map +1 -0
  378. package/dist/lib/image-normalize.d.ts +21 -0
  379. package/dist/lib/image-normalize.d.ts.map +1 -0
  380. package/dist/lib/image-normalize.js +218 -0
  381. package/dist/lib/image-normalize.js.map +1 -0
  382. package/dist/lib/inbound-chat.d.ts +48 -0
  383. package/dist/lib/inbound-chat.d.ts.map +1 -0
  384. package/dist/lib/inbound-chat.js +406 -0
  385. package/dist/lib/inbound-chat.js.map +1 -0
  386. package/dist/lib/key-parser.d.ts +79 -0
  387. package/dist/lib/key-parser.d.ts.map +1 -0
  388. package/dist/lib/key-parser.js +701 -0
  389. package/dist/lib/key-parser.js.map +1 -0
  390. package/dist/lib/key-proxy.d.ts +49 -0
  391. package/dist/lib/key-proxy.d.ts.map +1 -0
  392. package/dist/lib/key-proxy.js +92 -0
  393. package/dist/lib/key-proxy.js.map +1 -0
  394. package/dist/lib/log-redaction.d.ts +28 -0
  395. package/dist/lib/log-redaction.d.ts.map +1 -0
  396. package/dist/lib/log-redaction.js +166 -0
  397. package/dist/lib/log-redaction.js.map +1 -0
  398. package/dist/lib/model-scope.d.ts +9 -0
  399. package/dist/lib/model-scope.d.ts.map +1 -0
  400. package/dist/lib/model-scope.js +29 -0
  401. package/dist/lib/model-scope.js.map +1 -0
  402. package/dist/lib/one-time-code.d.ts +15 -0
  403. package/dist/lib/one-time-code.d.ts.map +1 -0
  404. package/dist/lib/one-time-code.js +41 -0
  405. package/dist/lib/one-time-code.js.map +1 -0
  406. package/dist/lib/output-cap.d.ts +24 -0
  407. package/dist/lib/output-cap.d.ts.map +1 -0
  408. package/dist/lib/output-cap.js +80 -0
  409. package/dist/lib/output-cap.js.map +1 -0
  410. package/dist/lib/password.d.ts +3 -0
  411. package/dist/lib/password.d.ts.map +1 -0
  412. package/dist/lib/password.js +27 -0
  413. package/dist/lib/password.js.map +1 -0
  414. package/dist/lib/process-safety-net.d.ts +32 -0
  415. package/dist/lib/process-safety-net.d.ts.map +1 -0
  416. package/dist/lib/process-safety-net.js +123 -0
  417. package/dist/lib/process-safety-net.js.map +1 -0
  418. package/dist/lib/provider-identity.d.ts +37 -0
  419. package/dist/lib/provider-identity.d.ts.map +1 -0
  420. package/dist/lib/provider-identity.js +110 -0
  421. package/dist/lib/provider-identity.js.map +1 -0
  422. package/dist/lib/provider-size-parser.d.ts +6 -0
  423. package/dist/lib/provider-size-parser.d.ts.map +1 -0
  424. package/dist/lib/provider-size-parser.js +72 -0
  425. package/dist/lib/provider-size-parser.js.map +1 -0
  426. package/dist/lib/provider-timeout.d.ts +17 -0
  427. package/dist/lib/provider-timeout.d.ts.map +1 -0
  428. package/dist/lib/provider-timeout.js +70 -0
  429. package/dist/lib/provider-timeout.js.map +1 -0
  430. package/dist/lib/proxy.d.ts +180 -0
  431. package/dist/lib/proxy.d.ts.map +1 -0
  432. package/dist/lib/proxy.js +1002 -0
  433. package/dist/lib/proxy.js.map +1 -0
  434. package/dist/lib/request-log.d.ts +4 -0
  435. package/dist/lib/request-log.d.ts.map +1 -0
  436. package/dist/lib/request-log.js +161 -0
  437. package/dist/lib/request-log.js.map +1 -0
  438. package/dist/lib/reset-code.d.ts +5 -0
  439. package/dist/lib/reset-code.d.ts.map +1 -0
  440. package/dist/lib/reset-code.js +39 -0
  441. package/dist/lib/reset-code.js.map +1 -0
  442. package/dist/lib/retry-hint.d.ts +14 -0
  443. package/dist/lib/retry-hint.d.ts.map +1 -0
  444. package/dist/lib/retry-hint.js +36 -0
  445. package/dist/lib/retry-hint.js.map +1 -0
  446. package/dist/lib/route-live.d.ts +29 -0
  447. package/dist/lib/route-live.d.ts.map +1 -0
  448. package/dist/lib/route-live.js +97 -0
  449. package/dist/lib/route-live.js.map +1 -0
  450. package/dist/lib/sampling-params.d.ts +183 -0
  451. package/dist/lib/sampling-params.d.ts.map +1 -0
  452. package/dist/lib/sampling-params.js +386 -0
  453. package/dist/lib/sampling-params.js.map +1 -0
  454. package/dist/lib/scheduler.d.ts +13 -0
  455. package/dist/lib/scheduler.d.ts.map +1 -0
  456. package/dist/lib/scheduler.js +11 -0
  457. package/dist/lib/scheduler.js.map +1 -0
  458. package/dist/lib/served-model.d.ts +16 -0
  459. package/dist/lib/served-model.d.ts.map +1 -0
  460. package/dist/lib/served-model.js +0 -0
  461. package/dist/lib/served-model.js.map +1 -0
  462. package/dist/lib/server-logs.d.ts +132 -0
  463. package/dist/lib/server-logs.d.ts.map +1 -0
  464. package/dist/lib/server-logs.js +473 -0
  465. package/dist/lib/server-logs.js.map +1 -0
  466. package/dist/lib/setup-code.d.ts +5 -0
  467. package/dist/lib/setup-code.d.ts.map +1 -0
  468. package/dist/lib/setup-code.js +35 -0
  469. package/dist/lib/setup-code.js.map +1 -0
  470. package/dist/lib/structured-output.d.ts +9 -0
  471. package/dist/lib/structured-output.d.ts.map +1 -0
  472. package/dist/lib/structured-output.js +72 -0
  473. package/dist/lib/structured-output.js.map +1 -0
  474. package/dist/lib/system-prompt.d.ts +30 -0
  475. package/dist/lib/system-prompt.d.ts.map +1 -0
  476. package/dist/lib/system-prompt.js +66 -0
  477. package/dist/lib/system-prompt.js.map +1 -0
  478. package/dist/lib/task-type.d.ts +18 -0
  479. package/dist/lib/task-type.d.ts.map +1 -0
  480. package/dist/lib/task-type.js +92 -0
  481. package/dist/lib/task-type.js.map +1 -0
  482. package/dist/lib/think-tags.d.ts +37 -0
  483. package/dist/lib/think-tags.d.ts.map +1 -0
  484. package/dist/lib/think-tags.js +203 -0
  485. package/dist/lib/think-tags.js.map +1 -0
  486. package/dist/lib/tool-args.d.ts +36 -0
  487. package/dist/lib/tool-args.d.ts.map +1 -0
  488. package/dist/lib/tool-args.js +190 -0
  489. package/dist/lib/tool-args.js.map +1 -0
  490. package/dist/lib/tool-call-rescue.d.ts +62 -0
  491. package/dist/lib/tool-call-rescue.d.ts.map +1 -0
  492. package/dist/lib/tool-call-rescue.js +258 -0
  493. package/dist/lib/tool-call-rescue.js.map +1 -0
  494. package/dist/lib/tool-capability.d.ts +13 -0
  495. package/dist/lib/tool-capability.d.ts.map +1 -0
  496. package/dist/lib/tool-capability.js +70 -0
  497. package/dist/lib/tool-capability.js.map +1 -0
  498. package/dist/lib/tool-validate.d.ts +44 -0
  499. package/dist/lib/tool-validate.d.ts.map +1 -0
  500. package/dist/lib/tool-validate.js +165 -0
  501. package/dist/lib/tool-validate.js.map +1 -0
  502. package/dist/lib/ttfb-budget.d.ts +13 -0
  503. package/dist/lib/ttfb-budget.d.ts.map +1 -0
  504. package/dist/lib/ttfb-budget.js +153 -0
  505. package/dist/lib/ttfb-budget.js.map +1 -0
  506. package/dist/lib/url-guard.d.ts +47 -0
  507. package/dist/lib/url-guard.d.ts.map +1 -0
  508. package/dist/lib/url-guard.js +229 -0
  509. package/dist/lib/url-guard.js.map +1 -0
  510. package/dist/lib/wake-detect.d.ts +12 -0
  511. package/dist/lib/wake-detect.d.ts.map +1 -0
  512. package/dist/lib/wake-detect.js +99 -0
  513. package/dist/lib/wake-detect.js.map +1 -0
  514. package/dist/middleware/errorHandler.d.ts +3 -0
  515. package/dist/middleware/errorHandler.d.ts.map +1 -0
  516. package/dist/middleware/errorHandler.js +45 -0
  517. package/dist/middleware/errorHandler.js.map +1 -0
  518. package/dist/middleware/rateLimit.d.ts +4 -0
  519. package/dist/middleware/rateLimit.d.ts.map +1 -0
  520. package/dist/middleware/rateLimit.js +125 -0
  521. package/dist/middleware/rateLimit.js.map +1 -0
  522. package/dist/middleware/requireAuth.d.ts +3 -0
  523. package/dist/middleware/requireAuth.d.ts.map +1 -0
  524. package/dist/middleware/requireAuth.js +17 -0
  525. package/dist/middleware/requireAuth.js.map +1 -0
  526. package/dist/providers/aclide.d.ts +18 -0
  527. package/dist/providers/aclide.d.ts.map +1 -0
  528. package/dist/providers/aclide.js +174 -0
  529. package/dist/providers/aclide.js.map +1 -0
  530. package/dist/providers/aihorde.d.ts +42 -0
  531. package/dist/providers/aihorde.d.ts.map +1 -0
  532. package/dist/providers/aihorde.js +192 -0
  533. package/dist/providers/aihorde.js.map +1 -0
  534. package/dist/providers/airforce.d.ts +11 -0
  535. package/dist/providers/airforce.d.ts.map +1 -0
  536. package/dist/providers/airforce.js +32 -0
  537. package/dist/providers/airforce.js.map +1 -0
  538. package/dist/providers/base.d.ts +177 -0
  539. package/dist/providers/base.d.ts.map +1 -0
  540. package/dist/providers/base.js +354 -0
  541. package/dist/providers/base.js.map +1 -0
  542. package/dist/providers/blaze.d.ts +11 -0
  543. package/dist/providers/blaze.d.ts.map +1 -0
  544. package/dist/providers/blaze.js +33 -0
  545. package/dist/providers/blaze.js.map +1 -0
  546. package/dist/providers/blockrun.d.ts +14 -0
  547. package/dist/providers/blockrun.d.ts.map +1 -0
  548. package/dist/providers/blockrun.js +36 -0
  549. package/dist/providers/blockrun.js.map +1 -0
  550. package/dist/providers/clod.d.ts +11 -0
  551. package/dist/providers/clod.d.ts.map +1 -0
  552. package/dist/providers/clod.js +39 -0
  553. package/dist/providers/clod.js.map +1 -0
  554. package/dist/providers/cloudflare.d.ts +15 -0
  555. package/dist/providers/cloudflare.d.ts.map +1 -0
  556. package/dist/providers/cloudflare.js +175 -0
  557. package/dist/providers/cloudflare.js.map +1 -0
  558. package/dist/providers/cohere.d.ts +11 -0
  559. package/dist/providers/cohere.d.ts.map +1 -0
  560. package/dist/providers/cohere.js +117 -0
  561. package/dist/providers/cohere.js.map +1 -0
  562. package/dist/providers/dreamprompting.d.ts +11 -0
  563. package/dist/providers/dreamprompting.d.ts.map +1 -0
  564. package/dist/providers/dreamprompting.js +41 -0
  565. package/dist/providers/dreamprompting.js.map +1 -0
  566. package/dist/providers/electronhub.d.ts +10 -0
  567. package/dist/providers/electronhub.d.ts.map +1 -0
  568. package/dist/providers/electronhub.js +56 -0
  569. package/dist/providers/electronhub.js.map +1 -0
  570. package/dist/providers/experiential.d.ts +10 -0
  571. package/dist/providers/experiential.d.ts.map +1 -0
  572. package/dist/providers/experiential.js +33 -0
  573. package/dist/providers/experiential.js.map +1 -0
  574. package/dist/providers/gizmo.d.ts +15 -0
  575. package/dist/providers/gizmo.d.ts.map +1 -0
  576. package/dist/providers/gizmo.js +54 -0
  577. package/dist/providers/gizmo.js.map +1 -0
  578. package/dist/providers/google.d.ts +30 -0
  579. package/dist/providers/google.d.ts.map +1 -0
  580. package/dist/providers/google.js +766 -0
  581. package/dist/providers/google.js.map +1 -0
  582. package/dist/providers/index.d.ts +13 -0
  583. package/dist/providers/index.d.ts.map +1 -0
  584. package/dist/providers/index.js +561 -0
  585. package/dist/providers/index.js.map +1 -0
  586. package/dist/providers/llmtr.d.ts +15 -0
  587. package/dist/providers/llmtr.d.ts.map +1 -0
  588. package/dist/providers/llmtr.js +51 -0
  589. package/dist/providers/llmtr.js.map +1 -0
  590. package/dist/providers/logfare.d.ts +11 -0
  591. package/dist/providers/logfare.d.ts.map +1 -0
  592. package/dist/providers/logfare.js +33 -0
  593. package/dist/providers/logfare.js.map +1 -0
  594. package/dist/providers/lucidity.d.ts +11 -0
  595. package/dist/providers/lucidity.d.ts.map +1 -0
  596. package/dist/providers/lucidity.js +37 -0
  597. package/dist/providers/lucidity.js.map +1 -0
  598. package/dist/providers/modelscope.d.ts +50 -0
  599. package/dist/providers/modelscope.d.ts.map +1 -0
  600. package/dist/providers/modelscope.js +143 -0
  601. package/dist/providers/modelscope.js.map +1 -0
  602. package/dist/providers/moondream.d.ts +17 -0
  603. package/dist/providers/moondream.d.ts.map +1 -0
  604. package/dist/providers/moondream.js +140 -0
  605. package/dist/providers/moondream.js.map +1 -0
  606. package/dist/providers/openai-compat.d.ts +140 -0
  607. package/dist/providers/openai-compat.d.ts.map +1 -0
  608. package/dist/providers/openai-compat.js +537 -0
  609. package/dist/providers/openai-compat.js.map +1 -0
  610. package/dist/providers/opencode-free.d.ts +24 -0
  611. package/dist/providers/opencode-free.d.ts.map +1 -0
  612. package/dist/providers/opencode-free.js +262 -0
  613. package/dist/providers/opencode-free.js.map +1 -0
  614. package/dist/providers/pollinations.d.ts +32 -0
  615. package/dist/providers/pollinations.d.ts.map +1 -0
  616. package/dist/providers/pollinations.js +65 -0
  617. package/dist/providers/pollinations.js.map +1 -0
  618. package/dist/providers/router9.d.ts +16 -0
  619. package/dist/providers/router9.d.ts.map +1 -0
  620. package/dist/providers/router9.js +42 -0
  621. package/dist/providers/router9.js.map +1 -0
  622. package/dist/providers/sail.d.ts +44 -0
  623. package/dist/providers/sail.d.ts.map +1 -0
  624. package/dist/providers/sail.js +302 -0
  625. package/dist/providers/sail.js.map +1 -0
  626. package/dist/providers/septor.d.ts +10 -0
  627. package/dist/providers/septor.d.ts.map +1 -0
  628. package/dist/providers/septor.js +36 -0
  629. package/dist/providers/septor.js.map +1 -0
  630. package/dist/providers/speechify.d.ts +14 -0
  631. package/dist/providers/speechify.d.ts.map +1 -0
  632. package/dist/providers/speechify.js +28 -0
  633. package/dist/providers/speechify.js.map +1 -0
  634. package/dist/providers/speka.d.ts +12 -0
  635. package/dist/providers/speka.d.ts.map +1 -0
  636. package/dist/providers/speka.js +37 -0
  637. package/dist/providers/speka.js.map +1 -0
  638. package/dist/providers/waterfall.d.ts +11 -0
  639. package/dist/providers/waterfall.d.ts.map +1 -0
  640. package/dist/providers/waterfall.js +33 -0
  641. package/dist/providers/waterfall.js.map +1 -0
  642. package/dist/providers/xkiro.d.ts +71 -0
  643. package/dist/providers/xkiro.d.ts.map +1 -0
  644. package/dist/providers/xkiro.js +119 -0
  645. package/dist/providers/xkiro.js.map +1 -0
  646. package/dist/providers/zhipu.d.ts +43 -0
  647. package/dist/providers/zhipu.d.ts.map +1 -0
  648. package/dist/providers/zhipu.js +95 -0
  649. package/dist/providers/zhipu.js.map +1 -0
  650. package/dist/routes/analytics.d.ts +2 -0
  651. package/dist/routes/analytics.d.ts.map +1 -0
  652. package/dist/routes/analytics.js +738 -0
  653. package/dist/routes/analytics.js.map +1 -0
  654. package/dist/routes/anthropic.d.ts +7 -0
  655. package/dist/routes/anthropic.d.ts.map +1 -0
  656. package/dist/routes/anthropic.js +1076 -0
  657. package/dist/routes/anthropic.js.map +1 -0
  658. package/dist/routes/auth.d.ts +2 -0
  659. package/dist/routes/auth.d.ts.map +1 -0
  660. package/dist/routes/auth.js +310 -0
  661. package/dist/routes/auth.js.map +1 -0
  662. package/dist/routes/backups.d.ts +2 -0
  663. package/dist/routes/backups.d.ts.map +1 -0
  664. package/dist/routes/backups.js +127 -0
  665. package/dist/routes/backups.js.map +1 -0
  666. package/dist/routes/cache.d.ts +2 -0
  667. package/dist/routes/cache.d.ts.map +1 -0
  668. package/dist/routes/cache.js +40 -0
  669. package/dist/routes/cache.js.map +1 -0
  670. package/dist/routes/client-profiles.d.ts +2 -0
  671. package/dist/routes/client-profiles.d.ts.map +1 -0
  672. package/dist/routes/client-profiles.js +128 -0
  673. package/dist/routes/client-profiles.js.map +1 -0
  674. package/dist/routes/compression.d.ts +2 -0
  675. package/dist/routes/compression.d.ts.map +1 -0
  676. package/dist/routes/compression.js +82 -0
  677. package/dist/routes/compression.js.map +1 -0
  678. package/dist/routes/conversations.d.ts +3 -0
  679. package/dist/routes/conversations.d.ts.map +1 -0
  680. package/dist/routes/conversations.js +215 -0
  681. package/dist/routes/conversations.js.map +1 -0
  682. package/dist/routes/docs.d.ts +2 -0
  683. package/dist/routes/docs.d.ts.map +1 -0
  684. package/dist/routes/docs.js +21 -0
  685. package/dist/routes/docs.js.map +1 -0
  686. package/dist/routes/embeddings.d.ts +2 -0
  687. package/dist/routes/embeddings.d.ts.map +1 -0
  688. package/dist/routes/embeddings.js +255 -0
  689. package/dist/routes/embeddings.js.map +1 -0
  690. package/dist/routes/fallback.d.ts +2 -0
  691. package/dist/routes/fallback.d.ts.map +1 -0
  692. package/dist/routes/fallback.js +631 -0
  693. package/dist/routes/fallback.js.map +1 -0
  694. package/dist/routes/free-tier.d.ts +22 -0
  695. package/dist/routes/free-tier.d.ts.map +1 -0
  696. package/dist/routes/free-tier.js +175 -0
  697. package/dist/routes/free-tier.js.map +1 -0
  698. package/dist/routes/gemini.d.ts +2 -0
  699. package/dist/routes/gemini.d.ts.map +1 -0
  700. package/dist/routes/gemini.js +218 -0
  701. package/dist/routes/gemini.js.map +1 -0
  702. package/dist/routes/health.d.ts +2 -0
  703. package/dist/routes/health.d.ts.map +1 -0
  704. package/dist/routes/health.js +72 -0
  705. package/dist/routes/health.js.map +1 -0
  706. package/dist/routes/keys.d.ts +17 -0
  707. package/dist/routes/keys.d.ts.map +1 -0
  708. package/dist/routes/keys.js +1787 -0
  709. package/dist/routes/keys.js.map +1 -0
  710. package/dist/routes/logs.d.ts +2 -0
  711. package/dist/routes/logs.d.ts.map +1 -0
  712. package/dist/routes/logs.js +88 -0
  713. package/dist/routes/logs.js.map +1 -0
  714. package/dist/routes/mcp.d.ts +4 -0
  715. package/dist/routes/mcp.d.ts.map +1 -0
  716. package/dist/routes/mcp.js +563 -0
  717. package/dist/routes/mcp.js.map +1 -0
  718. package/dist/routes/media.d.ts +2 -0
  719. package/dist/routes/media.d.ts.map +1 -0
  720. package/dist/routes/media.js +180 -0
  721. package/dist/routes/media.js.map +1 -0
  722. package/dist/routes/models.d.ts +2 -0
  723. package/dist/routes/models.d.ts.map +1 -0
  724. package/dist/routes/models.js +439 -0
  725. package/dist/routes/models.js.map +1 -0
  726. package/dist/routes/ollama.d.ts +7 -0
  727. package/dist/routes/ollama.d.ts.map +1 -0
  728. package/dist/routes/ollama.js +595 -0
  729. package/dist/routes/ollama.js.map +1 -0
  730. package/dist/routes/profiles.d.ts +3 -0
  731. package/dist/routes/profiles.d.ts.map +1 -0
  732. package/dist/routes/profiles.js +431 -0
  733. package/dist/routes/profiles.js.map +1 -0
  734. package/dist/routes/proxy.d.ts +32 -0
  735. package/dist/routes/proxy.d.ts.map +1 -0
  736. package/dist/routes/proxy.js +2793 -0
  737. package/dist/routes/proxy.js.map +1 -0
  738. package/dist/routes/responses.d.ts +1161 -0
  739. package/dist/routes/responses.d.ts.map +1 -0
  740. package/dist/routes/responses.js +1521 -0
  741. package/dist/routes/responses.js.map +1 -0
  742. package/dist/routes/settings.d.ts +2 -0
  743. package/dist/routes/settings.d.ts.map +1 -0
  744. package/dist/routes/settings.js +549 -0
  745. package/dist/routes/settings.js.map +1 -0
  746. package/dist/routes/status.d.ts +3 -0
  747. package/dist/routes/status.d.ts.map +1 -0
  748. package/dist/routes/status.js +214 -0
  749. package/dist/routes/status.js.map +1 -0
  750. package/dist/routes/update.d.ts +31 -0
  751. package/dist/routes/update.d.ts.map +1 -0
  752. package/dist/routes/update.js +536 -0
  753. package/dist/routes/update.js.map +1 -0
  754. package/dist/routes/url-tokens.d.ts +2 -0
  755. package/dist/routes/url-tokens.d.ts.map +1 -0
  756. package/dist/routes/url-tokens.js +31 -0
  757. package/dist/routes/url-tokens.js.map +1 -0
  758. package/dist/scripts/export-catalog.d.ts +2 -0
  759. package/dist/scripts/export-catalog.d.ts.map +1 -0
  760. package/dist/scripts/export-catalog.js +114 -0
  761. package/dist/scripts/export-catalog.js.map +1 -0
  762. package/dist/scripts/rotate-encryption-key.d.ts +81 -0
  763. package/dist/scripts/rotate-encryption-key.d.ts.map +1 -0
  764. package/dist/scripts/rotate-encryption-key.js +232 -0
  765. package/dist/scripts/rotate-encryption-key.js.map +1 -0
  766. package/dist/scripts/routing-sim.d.ts +2 -0
  767. package/dist/scripts/routing-sim.d.ts.map +1 -0
  768. package/dist/scripts/routing-sim.js +130 -0
  769. package/dist/scripts/routing-sim.js.map +1 -0
  770. package/dist/scripts/test-all-models.d.ts +2 -0
  771. package/dist/scripts/test-all-models.d.ts.map +1 -0
  772. package/dist/scripts/test-all-models.js +56 -0
  773. package/dist/scripts/test-all-models.js.map +1 -0
  774. package/dist/services/anthropic-map.d.ts +39 -0
  775. package/dist/services/anthropic-map.d.ts.map +1 -0
  776. package/dist/services/anthropic-map.js +138 -0
  777. package/dist/services/anthropic-map.js.map +1 -0
  778. package/dist/services/auth.d.ts +30 -0
  779. package/dist/services/auth.d.ts.map +1 -0
  780. package/dist/services/auth.js +124 -0
  781. package/dist/services/auth.js.map +1 -0
  782. package/dist/services/auto-discover.d.ts +77 -0
  783. package/dist/services/auto-discover.d.ts.map +1 -0
  784. package/dist/services/auto-discover.js +1047 -0
  785. package/dist/services/auto-discover.js.map +1 -0
  786. package/dist/services/backups.d.ts +67 -0
  787. package/dist/services/backups.d.ts.map +1 -0
  788. package/dist/services/backups.js +444 -0
  789. package/dist/services/backups.js.map +1 -0
  790. package/dist/services/builtin-model-discovery.d.ts +107 -0
  791. package/dist/services/builtin-model-discovery.d.ts.map +1 -0
  792. package/dist/services/builtin-model-discovery.js +296 -0
  793. package/dist/services/builtin-model-discovery.js.map +1 -0
  794. package/dist/services/cache.d.ts +190 -0
  795. package/dist/services/cache.d.ts.map +1 -0
  796. package/dist/services/cache.js +617 -0
  797. package/dist/services/cache.js.map +1 -0
  798. package/dist/services/catalog-sync.d.ts +257 -0
  799. package/dist/services/catalog-sync.d.ts.map +1 -0
  800. package/dist/services/catalog-sync.js +1041 -0
  801. package/dist/services/catalog-sync.js.map +1 -0
  802. package/dist/services/compression/config.d.ts +33 -0
  803. package/dist/services/compression/config.d.ts.map +1 -0
  804. package/dist/services/compression/config.js +158 -0
  805. package/dist/services/compression/config.js.map +1 -0
  806. package/dist/services/compression/engines/aging.d.ts +2 -0
  807. package/dist/services/compression/engines/aging.d.ts.map +1 -0
  808. package/dist/services/compression/engines/aging.js +53 -0
  809. package/dist/services/compression/engines/aging.js.map +1 -0
  810. package/dist/services/compression/engines/custom-filters.d.ts +4 -0
  811. package/dist/services/compression/engines/custom-filters.d.ts.map +1 -0
  812. package/dist/services/compression/engines/custom-filters.js +49 -0
  813. package/dist/services/compression/engines/custom-filters.js.map +1 -0
  814. package/dist/services/compression/engines/dedup.d.ts +2 -0
  815. package/dist/services/compression/engines/dedup.d.ts.map +1 -0
  816. package/dist/services/compression/engines/dedup.js +61 -0
  817. package/dist/services/compression/engines/dedup.js.map +1 -0
  818. package/dist/services/compression/engines/filter-definitions.d.ts +75 -0
  819. package/dist/services/compression/engines/filter-definitions.d.ts.map +1 -0
  820. package/dist/services/compression/engines/filter-definitions.js +45 -0
  821. package/dist/services/compression/engines/filter-definitions.js.map +1 -0
  822. package/dist/services/compression/engines/hard-budget.d.ts +2 -0
  823. package/dist/services/compression/engines/hard-budget.d.ts.map +1 -0
  824. package/dist/services/compression/engines/hard-budget.js +52 -0
  825. package/dist/services/compression/engines/hard-budget.js.map +1 -0
  826. package/dist/services/compression/engines/index.d.ts +9 -0
  827. package/dist/services/compression/engines/index.d.ts.map +1 -0
  828. package/dist/services/compression/engines/index.js +9 -0
  829. package/dist/services/compression/engines/index.js.map +1 -0
  830. package/dist/services/compression/engines/jsoncompact.d.ts +3 -0
  831. package/dist/services/compression/engines/jsoncompact.d.ts.map +1 -0
  832. package/dist/services/compression/engines/jsoncompact.js +145 -0
  833. package/dist/services/compression/engines/jsoncompact.js.map +1 -0
  834. package/dist/services/compression/engines/lite.d.ts +2 -0
  835. package/dist/services/compression/engines/lite.d.ts.map +1 -0
  836. package/dist/services/compression/engines/lite.js +34 -0
  837. package/dist/services/compression/engines/lite.js.map +1 -0
  838. package/dist/services/compression/engines/read-lifecycle.d.ts +2 -0
  839. package/dist/services/compression/engines/read-lifecycle.d.ts.map +1 -0
  840. package/dist/services/compression/engines/read-lifecycle.js +70 -0
  841. package/dist/services/compression/engines/read-lifecycle.js.map +1 -0
  842. package/dist/services/compression/engines/relevance.d.ts +2 -0
  843. package/dist/services/compression/engines/relevance.d.ts.map +1 -0
  844. package/dist/services/compression/engines/relevance.js +74 -0
  845. package/dist/services/compression/engines/relevance.js.map +1 -0
  846. package/dist/services/compression/engines/toolfilter.d.ts +2 -0
  847. package/dist/services/compression/engines/toolfilter.d.ts.map +1 -0
  848. package/dist/services/compression/engines/toolfilter.js +195 -0
  849. package/dist/services/compression/engines/toolfilter.js.map +1 -0
  850. package/dist/services/compression/fidelity-gate.d.ts +12 -0
  851. package/dist/services/compression/fidelity-gate.d.ts.map +1 -0
  852. package/dist/services/compression/fidelity-gate.js +90 -0
  853. package/dist/services/compression/fidelity-gate.js.map +1 -0
  854. package/dist/services/compression/helpers.d.ts +9 -0
  855. package/dist/services/compression/helpers.d.ts.map +1 -0
  856. package/dist/services/compression/helpers.js +52 -0
  857. package/dist/services/compression/helpers.js.map +1 -0
  858. package/dist/services/compression/pipeline.d.ts +7 -0
  859. package/dist/services/compression/pipeline.d.ts.map +1 -0
  860. package/dist/services/compression/pipeline.js +245 -0
  861. package/dist/services/compression/pipeline.js.map +1 -0
  862. package/dist/services/compression/preservation.d.ts +27 -0
  863. package/dist/services/compression/preservation.d.ts.map +1 -0
  864. package/dist/services/compression/preservation.js +109 -0
  865. package/dist/services/compression/preservation.js.map +1 -0
  866. package/dist/services/compression/registry.d.ts +6 -0
  867. package/dist/services/compression/registry.d.ts.map +1 -0
  868. package/dist/services/compression/registry.js +16 -0
  869. package/dist/services/compression/registry.js.map +1 -0
  870. package/dist/services/compression/stats.d.ts +29 -0
  871. package/dist/services/compression/stats.d.ts.map +1 -0
  872. package/dist/services/compression/stats.js +55 -0
  873. package/dist/services/compression/stats.js.map +1 -0
  874. package/dist/services/compression/types.d.ts +90 -0
  875. package/dist/services/compression/types.d.ts.map +1 -0
  876. package/dist/services/compression/types.js +2 -0
  877. package/dist/services/compression/types.js.map +1 -0
  878. package/dist/services/context-handoff.d.ts +22 -0
  879. package/dist/services/context-handoff.d.ts.map +1 -0
  880. package/dist/services/context-handoff.js +164 -0
  881. package/dist/services/context-handoff.js.map +1 -0
  882. package/dist/services/cooldown-probe.d.ts +24 -0
  883. package/dist/services/cooldown-probe.d.ts.map +1 -0
  884. package/dist/services/cooldown-probe.js +181 -0
  885. package/dist/services/cooldown-probe.js.map +1 -0
  886. package/dist/services/custom-endpoint.d.ts +58 -0
  887. package/dist/services/custom-endpoint.d.ts.map +1 -0
  888. package/dist/services/custom-endpoint.js +167 -0
  889. package/dist/services/custom-endpoint.js.map +1 -0
  890. package/dist/services/custom-media-register.d.ts +22 -0
  891. package/dist/services/custom-media-register.d.ts.map +1 -0
  892. package/dist/services/custom-media-register.js +45 -0
  893. package/dist/services/custom-media-register.js.map +1 -0
  894. package/dist/services/custom-model-register.d.ts +50 -0
  895. package/dist/services/custom-model-register.d.ts.map +1 -0
  896. package/dist/services/custom-model-register.js +103 -0
  897. package/dist/services/custom-model-register.js.map +1 -0
  898. package/dist/services/custom-model-seed.d.ts +18 -0
  899. package/dist/services/custom-model-seed.d.ts.map +1 -0
  900. package/dist/services/custom-model-seed.js +42 -0
  901. package/dist/services/custom-model-seed.js.map +1 -0
  902. package/dist/services/custom-model-sync.d.ts +37 -0
  903. package/dist/services/custom-model-sync.d.ts.map +1 -0
  904. package/dist/services/custom-model-sync.js +118 -0
  905. package/dist/services/custom-model-sync.js.map +1 -0
  906. package/dist/services/custom-model-tombstone.d.ts +5 -0
  907. package/dist/services/custom-model-tombstone.d.ts.map +1 -0
  908. package/dist/services/custom-model-tombstone.js +30 -0
  909. package/dist/services/custom-model-tombstone.js.map +1 -0
  910. package/dist/services/declarative-config.d.ts +356 -0
  911. package/dist/services/declarative-config.d.ts.map +1 -0
  912. package/dist/services/declarative-config.js +427 -0
  913. package/dist/services/declarative-config.js.map +1 -0
  914. package/dist/services/degradation.d.ts +40 -0
  915. package/dist/services/degradation.d.ts.map +1 -0
  916. package/dist/services/degradation.js +119 -0
  917. package/dist/services/degradation.js.map +1 -0
  918. package/dist/services/embeddings.d.ts +76 -0
  919. package/dist/services/embeddings.d.ts.map +1 -0
  920. package/dist/services/embeddings.js +319 -0
  921. package/dist/services/embeddings.js.map +1 -0
  922. package/dist/services/fusion.d.ts +162 -0
  923. package/dist/services/fusion.d.ts.map +1 -0
  924. package/dist/services/fusion.js +789 -0
  925. package/dist/services/fusion.js.map +1 -0
  926. package/dist/services/gemini-map.d.ts +14 -0
  927. package/dist/services/gemini-map.d.ts.map +1 -0
  928. package/dist/services/gemini-map.js +78 -0
  929. package/dist/services/gemini-map.js.map +1 -0
  930. package/dist/services/health.d.ts +52 -0
  931. package/dist/services/health.d.ts.map +1 -0
  932. package/dist/services/health.js +352 -0
  933. package/dist/services/health.js.map +1 -0
  934. package/dist/services/idempotency.d.ts +49 -0
  935. package/dist/services/idempotency.d.ts.map +1 -0
  936. package/dist/services/idempotency.js +140 -0
  937. package/dist/services/idempotency.js.map +1 -0
  938. package/dist/services/key-budget.d.ts +74 -0
  939. package/dist/services/key-budget.d.ts.map +1 -0
  940. package/dist/services/key-budget.js +200 -0
  941. package/dist/services/key-budget.js.map +1 -0
  942. package/dist/services/media.d.ts +123 -0
  943. package/dist/services/media.d.ts.map +1 -0
  944. package/dist/services/media.js +954 -0
  945. package/dist/services/media.js.map +1 -0
  946. package/dist/services/model-discovery.d.ts +126 -0
  947. package/dist/services/model-discovery.d.ts.map +1 -0
  948. package/dist/services/model-discovery.js +662 -0
  949. package/dist/services/model-discovery.js.map +1 -0
  950. package/dist/services/model-groups.d.ts +149 -0
  951. package/dist/services/model-groups.d.ts.map +1 -0
  952. package/dist/services/model-groups.js +314 -0
  953. package/dist/services/model-groups.js.map +1 -0
  954. package/dist/services/model-listing.d.ts +18 -0
  955. package/dist/services/model-listing.d.ts.map +1 -0
  956. package/dist/services/model-listing.js +94 -0
  957. package/dist/services/model-listing.js.map +1 -0
  958. package/dist/services/model-retirement.d.ts +29 -0
  959. package/dist/services/model-retirement.d.ts.map +1 -0
  960. package/dist/services/model-retirement.js +86 -0
  961. package/dist/services/model-retirement.js.map +1 -0
  962. package/dist/services/model-state.d.ts +90 -0
  963. package/dist/services/model-state.d.ts.map +1 -0
  964. package/dist/services/model-state.js +342 -0
  965. package/dist/services/model-state.js.map +1 -0
  966. package/dist/services/model-weight-overrides.d.ts +48 -0
  967. package/dist/services/model-weight-overrides.d.ts.map +1 -0
  968. package/dist/services/model-weight-overrides.js +127 -0
  969. package/dist/services/model-weight-overrides.js.map +1 -0
  970. package/dist/services/notification-emitter.d.ts +50 -0
  971. package/dist/services/notification-emitter.d.ts.map +1 -0
  972. package/dist/services/notification-emitter.js +177 -0
  973. package/dist/services/notification-emitter.js.map +1 -0
  974. package/dist/services/opencode-free-sync.d.ts +29 -0
  975. package/dist/services/opencode-free-sync.d.ts.map +1 -0
  976. package/dist/services/opencode-free-sync.js +206 -0
  977. package/dist/services/opencode-free-sync.js.map +1 -0
  978. package/dist/services/penalty-inspector.d.ts +56 -0
  979. package/dist/services/penalty-inspector.d.ts.map +1 -0
  980. package/dist/services/penalty-inspector.js +167 -0
  981. package/dist/services/penalty-inspector.js.map +1 -0
  982. package/dist/services/profile-models.d.ts +5 -0
  983. package/dist/services/profile-models.d.ts.map +1 -0
  984. package/dist/services/profile-models.js +62 -0
  985. package/dist/services/profile-models.js.map +1 -0
  986. package/dist/services/provider-credential.d.ts +19 -0
  987. package/dist/services/provider-credential.d.ts.map +1 -0
  988. package/dist/services/provider-credential.js +51 -0
  989. package/dist/services/provider-credential.js.map +1 -0
  990. package/dist/services/provider-quota.d.ts +64 -0
  991. package/dist/services/provider-quota.d.ts.map +1 -0
  992. package/dist/services/provider-quota.js +563 -0
  993. package/dist/services/provider-quota.js.map +1 -0
  994. package/dist/services/quirks.d.ts +0 -0
  995. package/dist/services/quirks.d.ts.map +1 -0
  996. package/dist/services/quirks.js +0 -0
  997. package/dist/services/quirks.js.map +1 -0
  998. package/dist/services/quota-forecast.d.ts +50 -0
  999. package/dist/services/quota-forecast.d.ts.map +1 -0
  1000. package/dist/services/quota-forecast.js +150 -0
  1001. package/dist/services/quota-forecast.js.map +1 -0
  1002. package/dist/services/quota-outlook.d.ts +4 -0
  1003. package/dist/services/quota-outlook.d.ts.map +1 -0
  1004. package/dist/services/quota-outlook.js +92 -0
  1005. package/dist/services/quota-outlook.js.map +1 -0
  1006. package/dist/services/ratelimit.d.ts +249 -0
  1007. package/dist/services/ratelimit.d.ts.map +1 -0
  1008. package/dist/services/ratelimit.js +1182 -0
  1009. package/dist/services/ratelimit.js.map +1 -0
  1010. package/dist/services/request-retention.d.ts +60 -0
  1011. package/dist/services/request-retention.d.ts.map +1 -0
  1012. package/dist/services/request-retention.js +229 -0
  1013. package/dist/services/request-retention.js.map +1 -0
  1014. package/dist/services/router.d.ts +384 -0
  1015. package/dist/services/router.d.ts.map +1 -0
  1016. package/dist/services/router.js +1986 -0
  1017. package/dist/services/router.js.map +1 -0
  1018. package/dist/services/scoring.d.ts +156 -0
  1019. package/dist/services/scoring.d.ts.map +1 -0
  1020. package/dist/services/scoring.js +435 -0
  1021. package/dist/services/scoring.js.map +1 -0
  1022. package/dist/services/url-tokens.d.ts +15 -0
  1023. package/dist/services/url-tokens.d.ts.map +1 -0
  1024. package/dist/services/url-tokens.js +55 -0
  1025. package/dist/services/url-tokens.js.map +1 -0
  1026. package/package.json +77 -4
  1027. package/README.md +0 -3
@@ -0,0 +1,2240 @@
1
+ import crypto from 'crypto';
2
+ import { initEncryptionKey } from '../../lib/crypto.js';
3
+ import { applyModelPricing } from '../model-pricing.js';
4
+ export function up(db) {
5
+ createTables(db);
6
+ initEncryptionKey(db);
7
+ seedModels(db);
8
+ migrateModels(db);
9
+ migrateModelsV2(db);
10
+ migrateModelsV3Ranks(db);
11
+ migrateModelsV4(db);
12
+ migrateModelsV5(db);
13
+ migrateModelsV6(db);
14
+ migrateModelsV7(db);
15
+ migrateModelsV8(db);
16
+ migrateModelsV9(db);
17
+ migrateModelsV10(db);
18
+ migrateModelsV11(db);
19
+ migrateModelsV12(db);
20
+ migrateModelsV13(db);
21
+ migrateModelsV14(db);
22
+ migrateModelsV15(db);
23
+ migrateModelsV16Vision(db);
24
+ migrateModelsV17IntelligenceTiers(db);
25
+ migrateModelsV18OpenCodeZen(db);
26
+ migrateModelsV19Gemma4(db);
27
+ migrateModelsV20KiloFree(db);
28
+ migrateModelsV21PruneDead(db);
29
+ migrateModelsV22Tools(db);
30
+ migrateModelsV23FreeTierAudit(db);
31
+ migrateModelsV24ZenRefresh(db);
32
+ migrateModelsV25ZenDeadPromos(db);
33
+ migrateModelsV26OpenCodeFree(db);
34
+ // V26 is the LAST model-data migration. Since the Premium live catalog
35
+ // shipped (June 2026), model/limit DATA is maintained in the published
36
+ // catalog (served signed by the catalog service) and reaches installs via
37
+ // catalog-sync — premium on the live tier within ~12h, free at the monthly
38
+ // promote. Shipping model data as a
39
+ // migration would hand it to free users on their next binary update,
40
+ // bypassing the tier gate. Migrations from here on are baseline/code-level
41
+ // only (schema, family rules, provider plumbing, quirk-seed corrections).
42
+ // After all model migrations: add/refresh paid-equivalent pricing
43
+ // (drives the realistic "Est. savings" analytics stat).
44
+ applyModelPricing(db);
45
+ migrateEmbeddingsV1(db);
46
+ migrateMediaV1(db);
47
+ migrateQuirksV1(db);
48
+ ensureUnifiedKey(db);
49
+ migrateProfilesInit(db);
50
+ }
51
+ export function down(_db) {
52
+ throw new Error('Legacy baseline is irreversible - restore from backup');
53
+ }
54
+ function createTables(db) {
55
+ db.exec(`
56
+ CREATE TABLE IF NOT EXISTS models (
57
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
58
+ platform TEXT NOT NULL,
59
+ model_id TEXT NOT NULL,
60
+ display_name TEXT NOT NULL,
61
+ intelligence_rank INTEGER NOT NULL,
62
+ speed_rank INTEGER NOT NULL,
63
+ size_label TEXT NOT NULL DEFAULT '',
64
+ rpm_limit INTEGER,
65
+ rpd_limit INTEGER,
66
+ tpm_limit INTEGER,
67
+ tpd_limit INTEGER,
68
+ monthly_token_budget TEXT NOT NULL DEFAULT '',
69
+ context_window INTEGER,
70
+ enabled INTEGER NOT NULL DEFAULT 1,
71
+ supports_vision INTEGER NOT NULL DEFAULT 0,
72
+ UNIQUE(platform, model_id)
73
+ );
74
+
75
+ CREATE TABLE IF NOT EXISTS api_keys (
76
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
77
+ platform TEXT NOT NULL,
78
+ label TEXT NOT NULL DEFAULT '',
79
+ encrypted_key TEXT NOT NULL,
80
+ iv TEXT NOT NULL,
81
+ auth_tag TEXT NOT NULL,
82
+ status TEXT NOT NULL DEFAULT 'unknown',
83
+ enabled INTEGER NOT NULL DEFAULT 1,
84
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
85
+ last_checked_at TEXT
86
+ );
87
+
88
+ CREATE TABLE IF NOT EXISTS requests (
89
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
90
+ platform TEXT NOT NULL,
91
+ model_id TEXT NOT NULL,
92
+ key_id INTEGER,
93
+ status TEXT NOT NULL,
94
+ input_tokens INTEGER NOT NULL DEFAULT 0,
95
+ output_tokens INTEGER NOT NULL DEFAULT 0,
96
+ latency_ms INTEGER NOT NULL DEFAULT 0,
97
+ error TEXT,
98
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
99
+ );
100
+
101
+ CREATE TABLE IF NOT EXISTS rate_limit_usage (
102
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
103
+ platform TEXT NOT NULL,
104
+ model_id TEXT NOT NULL,
105
+ key_id INTEGER NOT NULL,
106
+ kind TEXT NOT NULL CHECK (kind IN ('request', 'tokens')),
107
+ tokens INTEGER NOT NULL DEFAULT 0,
108
+ created_at_ms INTEGER NOT NULL,
109
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
110
+ );
111
+
112
+ CREATE TABLE IF NOT EXISTS rate_limit_cooldowns (
113
+ platform TEXT NOT NULL,
114
+ model_id TEXT NOT NULL,
115
+ key_id INTEGER NOT NULL,
116
+ expires_at_ms INTEGER NOT NULL,
117
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
118
+ PRIMARY KEY (platform, model_id, key_id)
119
+ );
120
+
121
+ CREATE TABLE IF NOT EXISTS fallback_config (
122
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
123
+ model_db_id INTEGER NOT NULL REFERENCES models(id),
124
+ priority INTEGER NOT NULL,
125
+ enabled INTEGER NOT NULL DEFAULT 1,
126
+ UNIQUE(model_db_id)
127
+ );
128
+
129
+ CREATE TABLE IF NOT EXISTS profiles (
130
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
131
+ name TEXT NOT NULL,
132
+ emoji TEXT NOT NULL DEFAULT '',
133
+ color TEXT NOT NULL DEFAULT '#6366f1',
134
+ type TEXT NOT NULL DEFAULT 'custom',
135
+ is_favorite INTEGER NOT NULL DEFAULT 0,
136
+ sort_order INTEGER NOT NULL DEFAULT 0,
137
+ auto_sort TEXT,
138
+ layout_config TEXT,
139
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
140
+ );
141
+
142
+ CREATE TABLE IF NOT EXISTS profile_models (
143
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
144
+ profile_id INTEGER NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
145
+ model_db_id INTEGER NOT NULL REFERENCES models(id) ON DELETE CASCADE,
146
+ priority INTEGER NOT NULL,
147
+ enabled INTEGER NOT NULL DEFAULT 1,
148
+ UNIQUE(profile_id, model_db_id)
149
+ );
150
+
151
+ CREATE TABLE IF NOT EXISTS settings (
152
+ key TEXT PRIMARY KEY,
153
+ value TEXT NOT NULL
154
+ );
155
+
156
+ -- Dashboard accounts (email + password) gating the /api/* admin surface (#35).
157
+ CREATE TABLE IF NOT EXISTS users (
158
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
159
+ email TEXT NOT NULL UNIQUE,
160
+ password_hash TEXT NOT NULL,
161
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
162
+ );
163
+
164
+ CREATE TABLE IF NOT EXISTS sessions (
165
+ token_hash TEXT PRIMARY KEY,
166
+ user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
167
+ expires_at_ms INTEGER NOT NULL,
168
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
169
+ );
170
+ CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions(user_id);
171
+
172
+ CREATE INDEX IF NOT EXISTS idx_requests_created_at ON requests(created_at);
173
+ CREATE INDEX IF NOT EXISTS idx_requests_platform ON requests(platform);
174
+ CREATE INDEX IF NOT EXISTS idx_rate_limit_usage_lookup ON rate_limit_usage(platform, model_id, key_id, kind, created_at_ms);
175
+ CREATE INDEX IF NOT EXISTS idx_rate_limit_cooldowns_expires ON rate_limit_cooldowns(expires_at_ms);
176
+ CREATE INDEX IF NOT EXISTS idx_api_keys_platform ON api_keys(platform);
177
+
178
+ CREATE TABLE IF NOT EXISTS provider_quota_state (
179
+ platform TEXT NOT NULL,
180
+ key_id INTEGER NOT NULL,
181
+ quota_pool_key TEXT NOT NULL,
182
+ metric TEXT NOT NULL,
183
+ limit_value INTEGER,
184
+ remaining_value INTEGER,
185
+ reset_at TEXT,
186
+ reset_strategy TEXT NOT NULL DEFAULT 'unknown',
187
+ source TEXT NOT NULL DEFAULT 'probe',
188
+ confidence REAL NOT NULL DEFAULT 0,
189
+ notes TEXT,
190
+ observed_at TEXT NOT NULL DEFAULT (datetime('now')),
191
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
192
+ PRIMARY KEY (platform, key_id, quota_pool_key, metric)
193
+ );
194
+ CREATE INDEX IF NOT EXISTS idx_provider_quota_state_platform ON provider_quota_state(platform, key_id, updated_at);
195
+ CREATE INDEX IF NOT EXISTS idx_provider_quota_state_reset_at ON provider_quota_state(reset_at);
196
+
197
+ CREATE TABLE IF NOT EXISTS provider_quota_observations (
198
+ id TEXT PRIMARY KEY,
199
+ platform TEXT NOT NULL,
200
+ key_id INTEGER NOT NULL,
201
+ provider_account_id TEXT,
202
+ model_id TEXT,
203
+ quota_pool_key TEXT NOT NULL,
204
+ metric TEXT NOT NULL,
205
+ status_code INTEGER,
206
+ limit_value INTEGER,
207
+ remaining_value INTEGER,
208
+ reset_at TEXT,
209
+ retry_after_ms INTEGER,
210
+ reset_strategy TEXT NOT NULL DEFAULT 'unknown',
211
+ source TEXT NOT NULL DEFAULT 'probe',
212
+ confidence REAL NOT NULL DEFAULT 0,
213
+ notes TEXT,
214
+ raw_json TEXT,
215
+ endpoint TEXT,
216
+ observed_at TEXT NOT NULL DEFAULT (datetime('now')),
217
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
218
+ );
219
+ CREATE INDEX IF NOT EXISTS idx_provider_quota_observations_platform ON provider_quota_observations(platform, key_id, observed_at);
220
+ CREATE INDEX IF NOT EXISTS idx_provider_quota_observations_reset_at ON provider_quota_observations(reset_at);
221
+ `);
222
+ ensureRequestKeyIdColumn(db);
223
+ ensureApiKeysBaseUrlColumn(db);
224
+ ensureModelsKeyIdColumn(db);
225
+ ensureRequestTtfbColumn(db);
226
+ ensureRequestRequestedModelColumn(db);
227
+ }
228
+ // `requested_model` is the model id the CLIENT pinned in the request body.
229
+ // NULL when the request was auto-routed ('auto' or omitted model field).
230
+ // requested_model = model_id means the pin was honored; a different model_id
231
+ // means rate limits or failures forced a failover to another model.
232
+ function ensureRequestRequestedModelColumn(db) {
233
+ const columns = db.prepare('PRAGMA table_info(requests)').all();
234
+ if (!columns.some(col => col.name === 'requested_model')) {
235
+ db.prepare('ALTER TABLE requests ADD COLUMN requested_model TEXT').run();
236
+ }
237
+ }
238
+ // `ttfb_ms` is the time-to-first-byte for streaming responses (ms from dispatch
239
+ // to the first chunk). NULL for non-streaming or pre-existing rows. Feeds the
240
+ // bandit router's latency axis (server/src/services/scoring.ts).
241
+ function ensureRequestTtfbColumn(db) {
242
+ const columns = db.prepare('PRAGMA table_info(requests)').all();
243
+ if (!columns.some(col => col.name === 'ttfb_ms')) {
244
+ db.prepare('ALTER TABLE requests ADD COLUMN ttfb_ms INTEGER').run();
245
+ }
246
+ }
247
+ function ensureRequestKeyIdColumn(db) {
248
+ const columns = db.prepare('PRAGMA table_info(requests)').all();
249
+ if (!columns.some(col => col.name === 'key_id')) {
250
+ db.prepare('ALTER TABLE requests ADD COLUMN key_id INTEGER').run();
251
+ }
252
+ db.prepare('CREATE INDEX IF NOT EXISTS idx_requests_key_id ON requests(key_id)').run();
253
+ }
254
+ // `base_url` is the upstream endpoint for the user-configured 'custom' provider
255
+ // (#117). NULL for every built-in platform — they use their hardcoded base URL.
256
+ function ensureApiKeysBaseUrlColumn(db) {
257
+ const columns = db.prepare('PRAGMA table_info(api_keys)').all();
258
+ if (!columns.some(col => col.name === 'base_url')) {
259
+ db.prepare('ALTER TABLE api_keys ADD COLUMN base_url TEXT').run();
260
+ }
261
+ }
262
+ // `key_id` binds a custom model to the api_keys row that carries ITS endpoint,
263
+ // so several custom providers can coexist (#212). NULL for built-in platforms
264
+ // (any key of the platform serves any of its models).
265
+ function ensureModelsKeyIdColumn(db) {
266
+ const columns = db.prepare('PRAGMA table_info(models)').all();
267
+ if (!columns.some(col => col.name === 'key_id')) {
268
+ db.prepare('ALTER TABLE models ADD COLUMN key_id INTEGER').run();
269
+ // Backfill: bind pre-existing custom models to the (single) legacy custom
270
+ // endpoint key so they keep routing to the URL they were created for.
271
+ db.prepare(`
272
+ UPDATE models
273
+ SET key_id = (SELECT id FROM api_keys WHERE platform = 'custom' ORDER BY id LIMIT 1)
274
+ WHERE platform = 'custom' AND key_id IS NULL
275
+ `).run();
276
+ }
277
+ }
278
+ function seedModels(db) {
279
+ const count = db.prepare('SELECT COUNT(*) as cnt FROM models').get();
280
+ if (count.cnt > 0)
281
+ return;
282
+ const insert = db.prepare(`
283
+ INSERT INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
284
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
285
+ `);
286
+ // NOTE: Limits current as of April 2026. See migrateModels() for in-place updates.
287
+ const models = [
288
+ // Google — gemini-2.5-flash free quotas were cut Dec 2025 (now ~20 RPD, budget much lower than before)
289
+ ['google', 'gemini-2.5-pro', 'Gemini 2.5 Pro', 1, 8, 'Frontier', 5, 100, 250000, null, '~12M', 1048576],
290
+ ['google', 'gemini-2.5-flash', 'Gemini 2.5 Flash', 4, 5, 'Large', 10, 20, 250000, null, '~3M', 1048576],
291
+ ['google', 'gemini-2.5-flash-lite', 'Gemini 2.5 Flash-Lite', 8, 3, 'Medium', 15, 1000, 250000, null, '~120M', 1048576],
292
+ // OpenRouter — upgraded DeepSeek R1 -> V3.1 (stronger reasoning); default RPD ~200
293
+ ['openrouter', 'deepseek/deepseek-v3.1:free', 'DeepSeek V3.1 (free)', 2, 10, 'Frontier', 20, 200, null, null, '~6M', 131072],
294
+ ['openrouter', 'moonshotai/kimi-k2:free', 'Kimi K2 (free)', 2, 9, 'Frontier', 20, 200, null, null, '~6M', 131072],
295
+ ['openrouter', 'qwen/qwen3-coder:free', 'Qwen3 Coder (free)', 3, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
296
+ ['openrouter', 'z-ai/glm-4.5-air:free', 'GLM-4.5 Air (free)', 4, 9, 'Large', 20, 200, null, null, '~6M', 131072],
297
+ // Cerebras — same 30 RPM / 1M TPD free pool; adding frontier coder, Llama 4 Maverick, GPT-OSS
298
+ ['cerebras', 'qwen-3-coder-480b', 'Qwen3-Coder 480B', 2, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
299
+ ['cerebras', 'llama-4-maverick-17b-128e-instruct', 'Llama 4 Maverick', 3, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
300
+ ['cerebras', 'qwen3-235b', 'Qwen3 235B', 3, 1, 'Large', 30, null, 60000, 1000000, '~30M', 8192],
301
+ ['cerebras', 'gpt-oss-120b', 'GPT-OSS 120B', 3, 1, 'Large', 30, null, 60000, 1000000, '~30M', 131072],
302
+ // GitHub Models — GPT-4o replaced with GPT-5 (same free tier key)
303
+ ['github', 'openai/gpt-5', 'GPT-5 (GitHub)', 1, 7, 'Frontier', 10, 50, null, null, '~18M', 128000],
304
+ // SambaNova — 70B RPM bumped to 20
305
+ ['sambanova', 'Meta-Llama-3.3-70B-Instruct', 'Llama 3.3 70B', 6, 9, 'Large', 20, null, null, 200000, '~6M', 8192],
306
+ // Mistral — Experiment pool ~1B tokens/mo shared across all models
307
+ ['mistral', 'mistral-large-latest', 'Mistral Large 3', 7, 8, 'Large', 2, null, 500000, null, '~50-100M', 131072],
308
+ ['mistral', 'magistral-medium-latest', 'Magistral Medium', 4, 8, 'Large', 2, null, 500000, null, '~50-100M', 40000],
309
+ ['mistral', 'codestral-latest', 'Codestral', 6, 6, 'Medium', 2, null, 500000, null, '~50-100M', 32000],
310
+ // Groq — scout TPM corrected to 6k (not 30k)
311
+ ['groq', 'llama-3.3-70b-versatile', 'Llama 3.3 70B', 9, 2, 'Medium', 30, 1000, 6000, 500000, '~15M', 131072],
312
+ ['groq', 'llama-4-scout-17b-16e-instruct', 'Llama 4 Scout', 10, 2, 'Medium', 30, 1000, 6000, 1000000, '~30M', 131072],
313
+ // NVIDIA NIM — moved to credit-based model in 2025; no longer truly recurring monthly. Disabled by default.
314
+ ['nvidia', 'meta/llama-3.1-70b-instruct', 'Llama 3.1 70B (NV)', 11, 6, 'Large', 40, null, null, null, 'credits-based', 131072],
315
+ // Cohere — trial tier is 1000 calls/mo total → realistic budget 1-2M
316
+ ['cohere', 'command-r-plus-08-2024', 'Command R+ (08-2024)', 12, 11, 'Large', 20, 33, null, null, '~1-2M', 131072],
317
+ ['cloudflare', '@cf/meta/llama-3.1-70b-instruct', 'Llama 3.1 70B (CF)', 13, 11, 'Medium', null, null, null, null, '~18-45M', 131072],
318
+ // Hugging Face — free Inference credits are ~$0.10/mo → budget closer to 1-3M on a 70B model
319
+ ['huggingface', 'accounts/fireworks/models/llama-v3p3-70b-instruct', 'Llama 3.3 70B (HF)', 14, 11, 'Medium', null, null, null, null, '~1-3M', 131072],
320
+ // New providers — recurring monthly free tiers, no card required
321
+ ['zhipu', 'glm-4.5-flash', 'GLM-4.5 Flash', 5, 4, 'Large', null, null, null, 1000000, '~30M', 131072],
322
+ ['moonshot', 'kimi-latest', 'Kimi Latest', 4, 8, 'Large', 60, null, null, 500000, '~15M', 200000],
323
+ ['minimax', 'MiniMax-M1', 'MiniMax M1', 5, 8, 'Large', 20, null, 1000000, null, '~30M', 200000],
324
+ ];
325
+ const insertMany = db.transaction(() => {
326
+ for (const m of models) {
327
+ insert.run(...m);
328
+ }
329
+ });
330
+ insertMany();
331
+ // Seed default fallback config from models
332
+ const allModels = db.prepare('SELECT id, intelligence_rank FROM models ORDER BY intelligence_rank ASC').all();
333
+ const insertFallback = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
334
+ const insertFallbacks = db.transaction(() => {
335
+ for (let i = 0; i < allModels.length; i++) {
336
+ insertFallback.run(allModels[i].id, i + 1);
337
+ }
338
+ });
339
+ insertFallbacks();
340
+ console.log(`Seeded ${models.length} models and fallback config`);
341
+ }
342
+ /**
343
+ * Idempotent migration to bring existing DBs up to the April 2026 pool.
344
+ * Covers: replaces outdated models (DeepSeek R1 → V3.1, GPT-4o → GPT-5),
345
+ * corrects stale rate-limits / monthly budgets, adds new smarter models
346
+ * and three new providers (Zhipu, Moonshot, MiniMax).
347
+ */
348
+ function migrateModels(db) {
349
+ // 1) Replace outdated models in-place (preserves fallback_config & any references)
350
+ const renameStmt = db.prepare(`
351
+ UPDATE models
352
+ SET model_id = ?, display_name = ?, intelligence_rank = ?,
353
+ monthly_token_budget = ?, rpd_limit = COALESCE(?, rpd_limit),
354
+ context_window = COALESCE(?, context_window),
355
+ size_label = COALESCE(?, size_label)
356
+ WHERE platform = ? AND model_id = ?
357
+ `);
358
+ // DeepSeek R1 (free) -> DeepSeek V3.1 (free)
359
+ renameStmt.run('deepseek/deepseek-v3.1:free', 'DeepSeek V3.1 (free)', 2, '~6M', 200, 131072, 'Frontier', 'openrouter', 'deepseek/deepseek-r1:free');
360
+ // GitHub GPT-4o -> GPT-5
361
+ renameStmt.run('openai/gpt-5', 'GPT-5 (GitHub)', 1, '~18M', null, 128000, 'Frontier', 'github', 'gpt-4o');
362
+ // 2) Correct stale limits / budgets on existing rows
363
+ db.prepare(`UPDATE models SET rpd_limit = 20, monthly_token_budget = '~3M' WHERE platform = 'google' AND model_id = 'gemini-2.5-flash'`).run();
364
+ db.prepare(`UPDATE models SET rpm_limit = 20 WHERE platform = 'sambanova' AND model_id = 'Meta-Llama-3.3-70B-Instruct'`).run();
365
+ db.prepare(`UPDATE models SET tpm_limit = 6000 WHERE platform = 'groq' AND model_id = 'llama-4-scout-17b-16e-instruct'`).run();
366
+ db.prepare(`UPDATE models SET monthly_token_budget = '~1-2M' WHERE platform = 'cohere' AND model_id = 'command-r-plus-08-2024'`).run();
367
+ db.prepare(`UPDATE models SET monthly_token_budget = '~1-3M' WHERE platform = 'huggingface' AND model_id = 'accounts/fireworks/models/llama-v3p3-70b-instruct'`).run();
368
+ // NVIDIA moved to credit model — disable and label accordingly
369
+ db.prepare(`UPDATE models SET monthly_token_budget = 'credits-based', enabled = 0 WHERE platform = 'nvidia' AND model_id = 'meta/llama-3.1-70b-instruct'`).run();
370
+ // 3) Insert new models (UNIQUE(platform, model_id) makes this idempotent)
371
+ const insert = db.prepare(`
372
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
373
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
374
+ `);
375
+ const newModels = [
376
+ // Cerebras — same free pool as qwen3-235b
377
+ ['cerebras', 'qwen-3-coder-480b', 'Qwen3-Coder 480B', 2, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
378
+ ['cerebras', 'llama-4-maverick-17b-128e-instruct', 'Llama 4 Maverick', 3, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
379
+ ['cerebras', 'gpt-oss-120b', 'GPT-OSS 120B', 3, 1, 'Large', 30, null, 60000, 1000000, '~30M', 131072],
380
+ // OpenRouter free tier
381
+ ['openrouter', 'deepseek/deepseek-v3.1:free', 'DeepSeek V3.1 (free)', 2, 10, 'Frontier', 20, 200, null, null, '~6M', 131072],
382
+ ['openrouter', 'moonshotai/kimi-k2:free', 'Kimi K2 (free)', 2, 9, 'Frontier', 20, 200, null, null, '~6M', 131072],
383
+ ['openrouter', 'qwen/qwen3-coder:free', 'Qwen3 Coder (free)', 3, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
384
+ ['openrouter', 'z-ai/glm-4.5-air:free', 'GLM-4.5 Air (free)', 4, 9, 'Large', 20, 200, null, null, '~6M', 131072],
385
+ // Mistral Experiment pool — shared ~1B/mo across models
386
+ ['mistral', 'magistral-medium-latest', 'Magistral Medium', 4, 8, 'Large', 2, null, 500000, null, '~50-100M', 40000],
387
+ ['mistral', 'codestral-latest', 'Codestral', 6, 6, 'Medium', 2, null, 500000, null, '~50-100M', 32000],
388
+ // New providers
389
+ ['zhipu', 'glm-4.5-flash', 'GLM-4.5 Flash', 5, 4, 'Large', null, null, null, 1000000, '~30M', 131072],
390
+ ['moonshot', 'kimi-latest', 'Kimi Latest', 4, 8, 'Large', 60, null, null, 500000, '~15M', 200000],
391
+ ['minimax', 'MiniMax-M1', 'MiniMax M1', 5, 8, 'Large', 20, null, 1000000, null, '~30M', 200000],
392
+ ];
393
+ const apply = db.transaction(() => {
394
+ for (const m of newModels)
395
+ insert.run(...m);
396
+ // Ensure every model has a fallback_config row (new inserts + any orphans)
397
+ const missing = db.prepare(`
398
+ SELECT m.id FROM models m
399
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
400
+ WHERE f.id IS NULL
401
+ ORDER BY m.intelligence_rank ASC
402
+ `).all();
403
+ if (missing.length > 0) {
404
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
405
+ const addFallback = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
406
+ for (let i = 0; i < missing.length; i++) {
407
+ addFallback.run(missing[i].id, maxPriority + i + 1);
408
+ }
409
+ }
410
+ });
411
+ apply();
412
+ }
413
+ /**
414
+ * Second-pass migration after live-testing every model against its provider.
415
+ * Corrects model IDs verified wrong, removes models not actually available on
416
+ * the current free tier, and adds real :free OpenRouter models found in the
417
+ * live catalog (April 2026).
418
+ */
419
+ function migrateModelsV2(db) {
420
+ // Helper: delete a model and its fallback_config entry (FK is RESTRICT-by-default)
421
+ const deleteModel = db.prepare(`DELETE FROM models WHERE platform = ? AND model_id = ?`);
422
+ const deleteFallback = db.prepare(`
423
+ DELETE FROM fallback_config WHERE model_db_id IN (
424
+ SELECT id FROM models WHERE platform = ? AND model_id = ?
425
+ )
426
+ `);
427
+ const removals = [
428
+ // GitHub free tier does NOT include GPT-5 (only catalog-listed). Revert handled below.
429
+ // Cerebras: qwen-3-coder-480b and llama-4-maverick not on free tier; gpt-oss-120b is listed
430
+ // but requires special access — our key gets 404. Remove all three.
431
+ ['cerebras', 'qwen-3-coder-480b'],
432
+ ['cerebras', 'llama-4-maverick-17b-128e-instruct'],
433
+ ['cerebras', 'gpt-oss-120b'],
434
+ // These OpenRouter :free variants do not exist in the live catalog (April 2026)
435
+ ['openrouter', 'deepseek/deepseek-v3.1:free'],
436
+ ['openrouter', 'moonshotai/kimi-k2:free'],
437
+ ];
438
+ const applyRemovals = db.transaction(() => {
439
+ for (const [p, m] of removals) {
440
+ deleteFallback.run(p, m);
441
+ deleteModel.run(p, m);
442
+ }
443
+ });
444
+ applyRemovals();
445
+ // GitHub: gpt-5 is in the model catalog but returns "unavailable_model" on free tier
446
+ // inference. Revert to gpt-4o which works. This only runs if the gpt-5 row exists.
447
+ db.prepare(`
448
+ UPDATE models
449
+ SET model_id = 'gpt-4o', display_name = 'GPT-4o', intelligence_rank = 5,
450
+ size_label = 'Large', context_window = 8000, monthly_token_budget = '~18M'
451
+ WHERE platform = 'github' AND model_id = 'openai/gpt-5'
452
+ `).run();
453
+ // Groq: scout requires the meta-llama/ publisher prefix
454
+ db.prepare(`
455
+ UPDATE models SET model_id = 'meta-llama/llama-4-scout-17b-16e-instruct'
456
+ WHERE platform = 'groq' AND model_id = 'llama-4-scout-17b-16e-instruct'
457
+ `).run();
458
+ // Add real OpenRouter :free models that exist in the live catalog
459
+ const insert = db.prepare(`
460
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
461
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
462
+ `);
463
+ const additions = [
464
+ // Frontier-tier free models verified in OR catalog 2026-04
465
+ ['openrouter', 'nvidia/nemotron-3-super-120b-a12b:free', 'Nemotron 3 Super 120B (free)', 2, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
466
+ ['openrouter', 'qwen/qwen3-next-80b-a3b-instruct:free', 'Qwen3-Next 80B (free)', 3, 9, 'Large', 20, 200, null, null, '~6M', 262144],
467
+ ['openrouter', 'minimax/minimax-m2.5:free', 'MiniMax M2.5 (free)', 3, 9, 'Large', 20, 200, null, null, '~6M', 196608],
468
+ ['openrouter', 'google/gemma-4-31b-it:free', 'Gemma 4 31B (free)', 5, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
469
+ ];
470
+ const applyAdditions = db.transaction(() => {
471
+ for (const a of additions)
472
+ insert.run(...a);
473
+ // Fallback entries for new models
474
+ const missing = db.prepare(`
475
+ SELECT m.id FROM models m
476
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
477
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
478
+ `).all();
479
+ if (missing.length > 0) {
480
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
481
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
482
+ for (let i = 0; i < missing.length; i++)
483
+ addFb.run(missing[i].id, maxPriority + i + 1);
484
+ }
485
+ });
486
+ applyAdditions();
487
+ }
488
+ /**
489
+ * Re-rank intelligence based on April 2026 coding + agentic tool-use benchmarks:
490
+ * SWE-bench Verified, Terminal-Bench 2, TAU-Bench, Aider Polyglot.
491
+ * Higher rank = weaker. Ties are allowed (same weights across providers).
492
+ */
493
+ function migrateModelsV3Ranks(db) {
494
+ const setRank = db.prepare(`UPDATE models SET intelligence_rank = ? WHERE platform = ? AND model_id = ?`);
495
+ const ranks = [
496
+ // #1-10 frontier coders / agents
497
+ [1, 'openrouter', 'minimax/minimax-m2.5:free'], // SWE-V ~80%, TB2 ~57%
498
+ [2, 'openrouter', 'qwen/qwen3-coder:free'], // SWE-V ~70%
499
+ [3, 'openrouter', 'qwen/qwen3-next-80b-a3b-instruct:free'], // SWE-V ~70.6%
500
+ [4, 'moonshot', 'kimi-latest'], // K2: SWE-V ~71%
501
+ [5, 'cerebras', 'qwen-3-235b-a22b-instruct-2507'], // SWE-V ~65-72%
502
+ [6, 'google', 'gemini-2.5-pro'], // SWE-V 63.8%, Aider 83%
503
+ [7, 'openrouter', 'z-ai/glm-4.5-air:free'], // ~58% SWE-V (distill of 4.5)
504
+ [8, 'openrouter', 'openai/gpt-oss-120b:free'], // SWE-V 62.4%
505
+ [9, 'openrouter', 'nvidia/nemotron-3-super-120b-a12b:free'], // SWE-V 53.7%
506
+ [10, 'minimax', 'MiniMax-M1'], // M1 predecessor, ~45-55%
507
+ // #11-15 mid-tier specialists
508
+ [11, 'mistral', 'codestral-latest'], // HumanEval 86.6%
509
+ [12, 'mistral', 'mistral-large-latest'],
510
+ [13, 'mistral', 'magistral-medium-latest'], // reasoning, not code-tuned
511
+ [14, 'google', 'gemini-2.5-flash'],
512
+ [15, 'zhipu', 'glm-4.5-flash'],
513
+ // #16 Llama 3.3 70B — identical weights across providers (tie)
514
+ [16, 'groq', 'llama-3.3-70b-versatile'],
515
+ [16, 'sambanova', 'Meta-Llama-3.3-70B-Instruct'],
516
+ [16, 'openrouter', 'meta-llama/llama-3.3-70b-instruct:free'],
517
+ [16, 'huggingface', 'accounts/fireworks/models/llama-v3p3-70b-instruct'],
518
+ // #17-23 weaker
519
+ [17, 'openrouter', 'nousresearch/hermes-3-llama-3.1-405b:free'], // L3.1 base with tool-use tune
520
+ [18, 'groq', 'meta-llama/llama-4-scout-17b-16e-instruct'], // multimodal focus
521
+ [19, 'openrouter', 'google/gemma-4-31b-it:free'],
522
+ [20, 'google', 'gemini-2.5-flash-lite'],
523
+ [21, 'github', 'gpt-4o'], // Aug 2024, SWE-V ~33%
524
+ [22, 'nvidia', 'meta/llama-3.1-70b-instruct'], // older Llama 3.1 tune
525
+ [22, 'cloudflare', '@cf/meta/llama-3.1-70b-instruct'], // same base weights
526
+ [23, 'cohere', 'command-r-plus-08-2024'], // RAG-focused, weakest on code
527
+ ];
528
+ const apply = db.transaction(() => {
529
+ for (const [rank, platform, modelId] of ranks) {
530
+ setRank.run(rank, platform, modelId);
531
+ }
532
+ });
533
+ apply();
534
+ }
535
+ /**
536
+ * V4: Agentic-tool-use focus. Live-probed every candidate against real free-tier
537
+ * keys (April 2026) with a weather-tool function-calling test. Keeps only models
538
+ * that return a structured tool_calls response and are reachable on the free tier.
539
+ *
540
+ * Adds SambaNova DeepSeek/Llama-4/gpt-oss, Groq gpt-oss & qwen3-32b, OpenRouter
541
+ * ling-2.6-flash + nemotron-nano + gpt-oss + trinity, Mistral devstral/medium,
542
+ * GitHub gpt-4.1, Cohere command-a, Cloudflare llama-4/gpt-oss/glm-4.7. Removes
543
+ * moonshot/kimi (paid-only now), minimax/M1 (superseded), HF/Fireworks route
544
+ * (no structured tools), OR/gemma-4 (weak at tools). Renames CF llama-3.1 → 3.3
545
+ * fp8-fast. Corrects stale limits.
546
+ */
547
+ function migrateModelsV4(db) {
548
+ // 1) Remove entries that are unavailable or fail agentic tool use
549
+ const deleteModel = db.prepare(`DELETE FROM models WHERE platform = ? AND model_id = ?`);
550
+ const deleteFallback = db.prepare(`
551
+ DELETE FROM fallback_config WHERE model_db_id IN (
552
+ SELECT id FROM models WHERE platform = ? AND model_id = ?
553
+ )
554
+ `);
555
+ const removals = [
556
+ ['moonshot', 'kimi-latest'], // paid-only now ($1 min deposit)
557
+ ['minimax', 'MiniMax-M1'], // superseded; use OR minimax-m2.5:free
558
+ ['openrouter', 'google/gemma-4-31b-it:free'], // weak at tool use
559
+ ['huggingface', 'accounts/fireworks/models/llama-v3p3-70b-instruct'], // emits tool call as text content, not structured
560
+ ];
561
+ const applyRemovals = db.transaction(() => {
562
+ for (const [p, m] of removals) {
563
+ deleteFallback.run(p, m);
564
+ deleteModel.run(p, m);
565
+ }
566
+ });
567
+ applyRemovals();
568
+ // 2) Cloudflare: replace Llama 3.1 70B with the current-gen 3.3 70B fp8-fast
569
+ db.prepare(`
570
+ UPDATE models
571
+ SET model_id = '@cf/meta/llama-3.3-70b-instruct-fp8-fast',
572
+ display_name = 'Llama 3.3 70B fp8-fast (CF)',
573
+ context_window = 131072
574
+ WHERE platform = 'cloudflare' AND model_id = '@cf/meta/llama-3.1-70b-instruct'
575
+ `).run();
576
+ // 3) Field corrections verified via primary sources + live probe
577
+ db.prepare(`UPDATE models SET tpm_limit = 12000 WHERE platform = 'groq' AND model_id = 'llama-3.3-70b-versatile'`).run();
578
+ db.prepare(`UPDATE models SET rpd_limit = 20 WHERE platform = 'sambanova' AND model_id = 'Meta-Llama-3.3-70B-Instruct'`).run();
579
+ db.prepare(`UPDATE models SET rpd_limit = 14400 WHERE platform = 'cerebras' AND model_id = 'qwen-3-235b-a22b-instruct-2507'`).run();
580
+ db.prepare(`UPDATE models SET rpd_limit = 250, monthly_token_budget = '~25M' WHERE platform = 'google' AND model_id = 'gemini-2.5-flash'`).run();
581
+ // gemini-2.5-pro is at-risk: April 2026 Google moved Pro-class off free tier in practice.
582
+ // Our live probe hit "quota exceeded" immediately. Cut rpd in half to reduce 429 blast radius.
583
+ db.prepare(`UPDATE models SET rpd_limit = 50, monthly_token_budget = '~6M' WHERE platform = 'google' AND model_id = 'gemini-2.5-pro'`).run();
584
+ // 4) Add live-probed, tool-capable models
585
+ const insert = db.prepare(`
586
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
587
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
588
+ `);
589
+ const additions = [
590
+ // OpenRouter :free — shared 20 RPM / 200 RPD / ~6M tokens across :free pool
591
+ ['openrouter', 'inclusionai/ling-2.6-flash:free', 'Ling 2.6 Flash (free)', 7, 9, 'Large', 20, 200, null, null, '~6M', 262144],
592
+ ['openrouter', 'arcee-ai/trinity-large-preview:free', 'Trinity Large Preview (free)', 13, 9, 'Frontier', 20, 200, null, null, '~6M', 131072],
593
+ ['openrouter', 'nvidia/nemotron-3-nano-30b-a3b:free', 'Nemotron 3 Nano 30B (free)', 22, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
594
+ ['openrouter', 'openai/gpt-oss-120b:free', 'GPT-OSS 120B (free)', 6, 9, 'Large', 20, 200, null, null, '~6M', 131072],
595
+ ['openrouter', 'openai/gpt-oss-20b:free', 'GPT-OSS 20B (free)', 18, 9, 'Medium', 20, 200, null, null, '~6M', 131072],
596
+ ['openrouter', 'meta-llama/llama-3.3-70b-instruct:free', 'Llama 3.3 70B (free)', 17, 9, 'Medium', 20, 200, null, null, '~6M', 131072],
597
+ // SambaNova — 20 RPM / 20 RPD / 200K TPD shared free Developer tier
598
+ ['sambanova', 'DeepSeek-V3.1', 'DeepSeek V3.1', 5, 9, 'Frontier', 20, 20, null, 200000, '~3M', 131072],
599
+ ['sambanova', 'DeepSeek-V3.2', 'DeepSeek V3.2', 4, 9, 'Frontier', 20, 20, null, 200000, '~3M', 131072],
600
+ ['sambanova', 'Llama-4-Maverick-17B-128E-Instruct', 'Llama 4 Maverick', 11, 9, 'Large', 20, 20, null, 200000, '~3M', 8192],
601
+ ['sambanova', 'gpt-oss-120b', 'GPT-OSS 120B (SambaNova)', 6, 9, 'Large', 20, 20, null, 200000, '~3M', 131072],
602
+ // Groq — very fast; 30 RPM per model, 1000 RPD on most, 14.4k on the 8B
603
+ ['groq', 'openai/gpt-oss-120b', 'GPT-OSS 120B (Groq)', 6, 2, 'Large', 30, 1000, 8000, 200000, '~6M', 131072],
604
+ ['groq', 'openai/gpt-oss-20b', 'GPT-OSS 20B (Groq)', 18, 2, 'Medium', 30, 1000, 8000, 200000, '~6M', 131072],
605
+ ['groq', 'qwen/qwen3-32b', 'Qwen3 32B (Groq)', 19, 2, 'Medium', 60, 1000, 6000, 500000, '~15M', 131072],
606
+ ['groq', 'llama-3.1-8b-instant', 'Llama 3.1 8B Instant', 28, 2, 'Small', 30, 14400, 6000, 500000, '~15M', 131072],
607
+ // Mistral Experiment tier — shared 2 RPM / 500k TPM / 1B tokens/mo across all models
608
+ ['mistral', 'devstral-latest', 'Devstral', 16, 8, 'Medium', 2, null, 500000, null, '~50-100M', 131072],
609
+ ['mistral', 'mistral-medium-latest', 'Mistral Medium 3.5', 14, 8, 'Large', 2, null, 500000, null, '~50-100M', 131072],
610
+ // GitHub Models — Low-tier category (15 RPM / 150 RPD, 8K in / 4K out per call)
611
+ ['github', 'openai/gpt-4.1', 'GPT-4.1 (GitHub)', 20, 7, 'Large', 10, 50, null, null, '~9M', 8000],
612
+ // Cohere — shared 1000 calls/mo trial pool, 20 RPM Chat
613
+ ['cohere', 'command-a-03-2025', 'Command-A (03-2025)', 27, 11, 'Large', 20, 33, null, null, '~1-2M', 131072],
614
+ // Cloudflare Workers AI — shared 10K Neurons/day free pool across all @cf/* models
615
+ ['cloudflare', '@cf/openai/gpt-oss-120b', 'GPT-OSS 120B (CF)', 6, 11, 'Large', null, null, null, null, '~18-45M', 131072],
616
+ ['cloudflare', '@cf/zai-org/glm-4.7-flash', 'GLM-4.7 Flash (CF)', 10, 11, 'Large', null, null, null, null, '~18-45M', 131072],
617
+ ['cloudflare', '@cf/meta/llama-4-scout-17b-16e-instruct', 'Llama 4 Scout (CF)', 12, 11, 'Large', null, null, null, null, '~18-45M', 131072],
618
+ ];
619
+ const apply = db.transaction(() => {
620
+ for (const a of additions)
621
+ insert.run(...a);
622
+ const missing = db.prepare(`
623
+ SELECT m.id FROM models m
624
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
625
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
626
+ `).all();
627
+ if (missing.length > 0) {
628
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
629
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
630
+ for (let i = 0; i < missing.length; i++)
631
+ addFb.run(missing[i].id, maxPriority + i + 1);
632
+ }
633
+ });
634
+ apply();
635
+ // 5) Re-rank the live catalog by agentic tool-use capability (lower = smarter).
636
+ // Grounded in April 2026 SWE-Bench Verified + BFCL v3 + Tau-Bench numbers.
637
+ const setRank = db.prepare(`UPDATE models SET intelligence_rank = ? WHERE platform = ? AND model_id = ?`);
638
+ const ranks = [
639
+ [1, 'openrouter', 'minimax/minimax-m2.5:free'],
640
+ [2, 'openrouter', 'qwen/qwen3-coder:free'],
641
+ [3, 'openrouter', 'qwen/qwen3-next-80b-a3b-instruct:free'],
642
+ [4, 'sambanova', 'DeepSeek-V3.2'],
643
+ [5, 'sambanova', 'DeepSeek-V3.1'],
644
+ [6, 'cerebras', 'qwen-3-235b-a22b-instruct-2507'],
645
+ [6, 'openrouter', 'openai/gpt-oss-120b:free'],
646
+ [6, 'groq', 'openai/gpt-oss-120b'],
647
+ [6, 'sambanova', 'gpt-oss-120b'],
648
+ [6, 'cloudflare', '@cf/openai/gpt-oss-120b'],
649
+ [7, 'openrouter', 'inclusionai/ling-2.6-flash:free'],
650
+ [8, 'openrouter', 'z-ai/glm-4.5-air:free'],
651
+ [10, 'cloudflare', '@cf/zai-org/glm-4.7-flash'],
652
+ [11, 'sambanova', 'Llama-4-Maverick-17B-128E-Instruct'],
653
+ [12, 'groq', 'meta-llama/llama-4-scout-17b-16e-instruct'],
654
+ [12, 'cloudflare', '@cf/meta/llama-4-scout-17b-16e-instruct'],
655
+ [13, 'openrouter', 'arcee-ai/trinity-large-preview:free'],
656
+ [14, 'google', 'gemini-2.5-pro'],
657
+ [14, 'mistral', 'mistral-large-latest'],
658
+ [14, 'mistral', 'mistral-medium-latest'],
659
+ [16, 'mistral', 'devstral-latest'],
660
+ [16, 'mistral', 'codestral-latest'],
661
+ [17, 'groq', 'llama-3.3-70b-versatile'],
662
+ [17, 'sambanova', 'Meta-Llama-3.3-70B-Instruct'],
663
+ [17, 'cloudflare', '@cf/meta/llama-3.3-70b-instruct-fp8-fast'],
664
+ [17, 'openrouter', 'meta-llama/llama-3.3-70b-instruct:free'],
665
+ [17, 'nvidia', 'meta/llama-3.1-70b-instruct'],
666
+ [18, 'openrouter', 'openai/gpt-oss-20b:free'],
667
+ [18, 'groq', 'openai/gpt-oss-20b'],
668
+ [19, 'groq', 'qwen/qwen3-32b'],
669
+ [20, 'google', 'gemini-2.5-flash'],
670
+ [20, 'github', 'openai/gpt-4.1'],
671
+ [21, 'mistral', 'magistral-medium-latest'],
672
+ [22, 'openrouter', 'nvidia/nemotron-3-super-120b-a12b:free'],
673
+ [23, 'openrouter', 'nvidia/nemotron-3-nano-30b-a3b:free'],
674
+ [24, 'zhipu', 'glm-4.5-flash'],
675
+ [25, 'github', 'gpt-4o'],
676
+ [26, 'google', 'gemini-2.5-flash-lite'],
677
+ [27, 'cohere', 'command-a-03-2025'],
678
+ [27, 'cohere', 'command-r-plus-08-2024'],
679
+ [28, 'groq', 'llama-3.1-8b-instant'],
680
+ ];
681
+ const applyRanks = db.transaction(() => {
682
+ for (const [r, p, m] of ranks)
683
+ setRank.run(r, p, m);
684
+ });
685
+ applyRanks();
686
+ }
687
+ /**
688
+ * V5: Google moved all Pro-tier Gemini off the free tier on 2026-04-01 — disable
689
+ * gemini-2.5-pro. Add Cerebras `zai-glm-4.7` (355B z.ai GLM preview, newly on
690
+ * free tier but throttled to 10 RPM / 100 RPD due to high demand; context capped
691
+ * at 8192 on free tier).
692
+ */
693
+ function migrateModelsV5(db) {
694
+ db.prepare(`UPDATE models SET enabled = 0 WHERE platform = 'google' AND model_id = 'gemini-2.5-pro'`).run();
695
+ const insert = db.prepare(`
696
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
697
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
698
+ `);
699
+ const apply = db.transaction(() => {
700
+ insert.run('cerebras', 'zai-glm-4.7', 'GLM-4.7 (Cerebras)', 7, 1, 'Frontier', 10, 100, null, null, '~3M', 8192);
701
+ const missing = db.prepare(`
702
+ SELECT m.id FROM models m
703
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
704
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
705
+ `).all();
706
+ if (missing.length > 0) {
707
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
708
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
709
+ for (let i = 0; i < missing.length; i++)
710
+ addFb.run(missing[i].id, maxPriority + i + 1);
711
+ }
712
+ });
713
+ apply();
714
+ }
715
+ /**
716
+ * V6: Live-probed against real free-tier keys on 2026-04-25.
717
+ *
718
+ * Corrections (Google free-tier RPD): the documented "250" / "1000" RPD numbers
719
+ * for gemini-2.5-flash and gemini-2.5-flash-lite are stale — both share a 20
720
+ * RPD per-model-per-project free pool now. Confirmed by the
721
+ * `generate_content_free_tier_requests` quota error, limit 20.
722
+ *
723
+ * Removals: arcee-ai/trinity-large-preview:free returns 404 "No endpoints found"
724
+ * — pulled from OpenRouter's free pool. (Other previously-suspected dead OR :free
725
+ * IDs are still live in /api/v1/models, so they stay.)
726
+ *
727
+ * Additions (all probe-verified to return 200 with content on the user's keys):
728
+ * - 3 Cloudflare Workers AI reasoning models
729
+ * - 3 Google preview models, including Pro (which returned a free-tier 429
730
+ * against the same 20 RPD pool, confirming free-tier eligibility)
731
+ * - 2 OpenRouter :free models with no expiration_date
732
+ */
733
+ function migrateModelsV6(db) {
734
+ // 1) Remove confirmed-dead OR route
735
+ const deleteModel = db.prepare(`DELETE FROM models WHERE platform = ? AND model_id = ?`);
736
+ const deleteFallback = db.prepare(`
737
+ DELETE FROM fallback_config WHERE model_db_id IN (
738
+ SELECT id FROM models WHERE platform = ? AND model_id = ?
739
+ )
740
+ `);
741
+ const removals = [
742
+ ['openrouter', 'arcee-ai/trinity-large-preview:free'],
743
+ ];
744
+ const applyRemovals = db.transaction(() => {
745
+ for (const [p, m] of removals) {
746
+ deleteFallback.run(p, m);
747
+ deleteModel.run(p, m);
748
+ }
749
+ });
750
+ applyRemovals();
751
+ // 2) Correct stale Google free-tier RPD numbers
752
+ db.prepare(`
753
+ UPDATE models SET rpd_limit = 20, monthly_token_budget = '~3M'
754
+ WHERE platform = 'google' AND model_id = 'gemini-2.5-flash'
755
+ `).run();
756
+ db.prepare(`
757
+ UPDATE models SET rpd_limit = 20, monthly_token_budget = '~3M'
758
+ WHERE platform = 'google' AND model_id = 'gemini-2.5-flash-lite'
759
+ `).run();
760
+ // 3) Add live-probed models
761
+ const insert = db.prepare(`
762
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
763
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
764
+ `);
765
+ const additions = [
766
+ // Cloudflare Workers AI — 10K Neurons/day shared free pool. Reasoning traces
767
+ // burn output tokens fast, so per-call effective budget is small. Estimates
768
+ // assume 1K-in/500-out typical: kimi-k2.5 ≈ 50/day, qwen3-30b ≈ 200/day,
769
+ // r1-distill ≈ 5/day on the reasoning-heavy path.
770
+ ['cloudflare', '@cf/moonshotai/kimi-k2.5', 'Kimi K2.5 (CF)', 3, 11, 'Frontier', null, null, null, null, '~10-20M', 262144],
771
+ ['cloudflare', '@cf/qwen/qwen3-30b-a3b-fp8', 'Qwen3 30B-A3B fp8 (CF)', 7, 11, 'Large', null, null, null, null, '~18-45M', 131072],
772
+ ['cloudflare', '@cf/deepseek-ai/deepseek-r1-distill-qwen-32b', 'DeepSeek R1 Distill Qwen 32B (CF)', 9, 11, 'Large', null, null, null, null, '~3-5M', 131072],
773
+ // Google preview tier — shares the 20 RPD per-model free pool. Pro confirmed
774
+ // free-tier-eligible by the `free_tier_requests` quota metric in 429 errors.
775
+ ['google', 'gemini-3.1-flash-lite-preview', 'Gemini 3.1 Flash-Lite Preview', 18, 3, 'Medium', 15, 20, 250000, null, '~3M', 1048576],
776
+ ['google', 'gemini-3-flash-preview', 'Gemini 3 Flash Preview', 11, 5, 'Large', 10, 20, 250000, null, '~3M', 1048576],
777
+ ['google', 'gemini-3.1-pro-preview', 'Gemini 3.1 Pro Preview', 1, 8, 'Frontier', 5, 20, 250000, null, '~3M', 1048576],
778
+ // OpenRouter :free pool — 20 RPM / 50 RPD (1000 once $10 credits bought).
779
+ ['openrouter', 'google/gemma-4-31b-it:free', 'Gemma 4 31B (free)', 19, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
780
+ ['openrouter', 'liquid/lfm-2.5-1.2b-instruct:free', 'Liquid LFM 2.5 1.2B (free)', 30, 10, 'Small', 20, 200, null, null, '~6M', 32768],
781
+ ];
782
+ const apply = db.transaction(() => {
783
+ for (const a of additions)
784
+ insert.run(...a);
785
+ const missing = db.prepare(`
786
+ SELECT m.id FROM models m
787
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
788
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
789
+ `).all();
790
+ if (missing.length > 0) {
791
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
792
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
793
+ for (let i = 0; i < missing.length; i++)
794
+ addFb.run(missing[i].id, maxPriority + i + 1);
795
+ }
796
+ });
797
+ apply();
798
+ }
799
+ /**
800
+ * V7 (April 2026): live-probed delta against OpenRouter's free pool + Z.ai.
801
+ * - Removes inclusionai/ling-2.6-flash:free (transitioned to paid, 404 on chat).
802
+ * - Adds 8 new :free routes confirmed via /v1/models + chat-completion probe.
803
+ * - Adds zhipu/glm-4.7-flash (probe: 429 "overloaded" — free-pool throttle, not
804
+ * "insufficient balance" which paid models return). Same baseUrl works for both
805
+ * api.z.ai and open.bigmodel.cn keys.
806
+ * HF and NVIDIA left as-is: HF still serves chat with current key; NVIDIA already disabled.
807
+ */
808
+ function migrateModelsV7(db) {
809
+ const deleteModel = db.prepare(`DELETE FROM models WHERE platform = ? AND model_id = ?`);
810
+ const deleteFallback = db.prepare(`
811
+ DELETE FROM fallback_config WHERE model_db_id IN (
812
+ SELECT id FROM models WHERE platform = ? AND model_id = ?
813
+ )
814
+ `);
815
+ const removals = [
816
+ ['openrouter', 'inclusionai/ling-2.6-flash:free'],
817
+ ];
818
+ const applyRemovals = db.transaction(() => {
819
+ for (const [p, m] of removals) {
820
+ deleteFallback.run(p, m);
821
+ deleteModel.run(p, m);
822
+ }
823
+ });
824
+ applyRemovals();
825
+ const insert = db.prepare(`
826
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
827
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
828
+ `);
829
+ // OpenRouter :free quotas: 20 RPM / 50 RPD without credits, 1000 RPD with $10 lifetime topup.
830
+ // Catalog convention is rpd=200 (matches existing rows).
831
+ const additions = [
832
+ ['openrouter', 'inclusionai/ling-2.6-1t:free', 'Ling 2.6 1T (free)', 4, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
833
+ ['openrouter', 'tencent/hy3-preview:free', 'Tencent HY3 Preview (free)', 7, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
834
+ ['openrouter', 'poolside/laguna-m.1:free', 'Poolside Laguna M.1 (free)', 13, 9, 'Large', 20, 200, null, null, '~6M', 131072],
835
+ ['openrouter', 'google/gemma-4-26b-a4b-it:free', 'Gemma 4 26B-A4B (free)', 22, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
836
+ ['openrouter', 'nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free', 'Nemotron 3 Nano 30B Reasoning (free)', 23, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
837
+ ['openrouter', 'poolside/laguna-xs.2:free', 'Poolside Laguna XS.2 (free)', 26, 10, 'Medium', 20, 200, null, null, '~6M', 131072],
838
+ ['openrouter', 'nvidia/nemotron-nano-9b-v2:free', 'Nemotron Nano 9B v2 (free)', 28, 10, 'Medium', 20, 200, null, null, '~6M', 128000],
839
+ ['openrouter', 'liquid/lfm-2.5-1.2b-thinking:free', 'Liquid LFM 2.5 1.2B Thinking (free)', 30, 10, 'Small', 20, 200, null, null, '~6M', 32768],
840
+ // Zhipu (Z.ai) — free pool. glm-4.7-flash quotas unpublished; mirror glm-4.5-flash row shape.
841
+ ['zhipu', 'glm-4.7-flash', 'GLM-4.7 Flash', 18, 4, 'Large', null, null, null, 1000000, '~30M', 131072],
842
+ ];
843
+ const apply = db.transaction(() => {
844
+ for (const a of additions)
845
+ insert.run(...a);
846
+ const missing = db.prepare(`
847
+ SELECT m.id FROM models m
848
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
849
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
850
+ `).all();
851
+ if (missing.length > 0) {
852
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
853
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
854
+ for (let i = 0; i < missing.length; i++)
855
+ addFb.run(missing[i].id, maxPriority + i + 1);
856
+ }
857
+ });
858
+ apply();
859
+ }
860
+ /**
861
+ * V8 (May 2026): 3-day delta. SambaNova's /v1/models added two free-tier models;
862
+ * Cloudflare's @cf catalog added two new text models. All four probe-verified 200
863
+ * with the user's keys. SambaNova's paid-only MiniMax-M2.5 explicitly returns 422
864
+ * "Couldn't find valid service tier", so the 200s on these rows confirm free-tier
865
+ * access. Cloudflare's @cf/* models share the 10K Neurons/day free pool.
866
+ */
867
+ function migrateModelsV8(db) {
868
+ const insert = db.prepare(`
869
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
870
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
871
+ `);
872
+ const additions = [
873
+ // SambaNova free pool: 20 RPM / 20 RPD / 200K TPD shared across all free models.
874
+ ['sambanova', 'DeepSeek-V3.1-cb', 'DeepSeek V3.1 (CB)', 5, 9, 'Frontier', 20, 20, null, 200000, '~3M', 131072],
875
+ ['sambanova', 'gemma-3-12b-it', 'Gemma 3 12B (SambaNova)', 22, 9, 'Medium', 20, 20, null, 200000, '~3M', 131072],
876
+ // Cloudflare @cf — 10K Neurons/day shared pool.
877
+ ['cloudflare', '@cf/moonshotai/kimi-k2.6', 'Kimi K2.6 (CF)', 2, 11, 'Frontier', null, null, null, null, '~10-20M', 262144],
878
+ ['cloudflare', '@cf/ibm-granite/granite-4.0-h-micro', 'Granite 4.0 H Micro (CF)', 29, 11, 'Small', null, null, null, null, '~5-10M', 131072],
879
+ ];
880
+ const apply = db.transaction(() => {
881
+ for (const a of additions)
882
+ insert.run(...a);
883
+ const missing = db.prepare(`
884
+ SELECT m.id FROM models m
885
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
886
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
887
+ `).all();
888
+ if (missing.length > 0) {
889
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
890
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
891
+ for (let i = 0; i < missing.length; i++)
892
+ addFb.run(missing[i].id, maxPriority + i + 1);
893
+ }
894
+ });
895
+ apply();
896
+ }
897
+ /**
898
+ * V9 (May 2026): disable cerebras/zai-glm-4.7. The model still appears in
899
+ * Cerebras's /v1/models listing but the chat-completions endpoint returns
900
+ * 404 "Model does not exist or you do not have access" for free-tier keys —
901
+ * matches their docs note about temporarily reducing free-tier access on
902
+ * zai-glm-4.7 due to high demand. Row kept (not deleted) so it can be
903
+ * re-enabled later without losing fallback_config history.
904
+ */
905
+ function migrateModelsV9(db) {
906
+ db.prepare("UPDATE models SET enabled = 0 WHERE platform = 'cerebras' AND model_id = 'zai-glm-4.7'").run();
907
+ }
908
+ /**
909
+ * V10 (May 2026): Ollama Cloud — first new platform since Z.ai/Zhipu in V7.
910
+ * Free plan: GPU-time-based quota (not per-token), 1 concurrent model,
911
+ * 5h session caps, no card required. /v1/models lists 39 SKUs but only 28
912
+ * respond on the Free tier — paid models return 403 with an explicit
913
+ * "this model requires a subscription" message.
914
+ *
915
+ * Curated to ~10 representative free models that either (a) aren't reachable
916
+ * elsewhere in the catalog or (b) provide a useful alternate route through
917
+ * Ollama's independent rate-limit pool. Probe-verified May 2 2026.
918
+ *
919
+ * Quota shape: GPU-time, not tokens. monthly_token_budget reflects rough
920
+ * Free-tier "session" capacity rather than a hard token cap.
921
+ */
922
+ function migrateModelsV10(db) {
923
+ const insert = db.prepare(`
924
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
925
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
926
+ `);
927
+ const additions = [
928
+ // Budget strings are estimates: Ollama publishes no token cap (quota is GPU-time +
929
+ // 7-day rolling). Frontier ~5-10M, Large ~10-20M, Medium ~20-30M reflect that
930
+ // heavier models burn quota faster. Numeric limits stay null — real provider
931
+ // throttling is the source of truth, not these display strings.
932
+ ['ollama', 'qwen3-coder:480b', 'Qwen3-Coder 480B (Ollama)', 2, 9, 'Frontier', null, null, null, null, '~5-10M', 262144],
933
+ ['ollama', 'mistral-large-3:675b', 'Mistral Large 3 675B (Ollama)', 3, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
934
+ ['ollama', 'deepseek-v3.2', 'DeepSeek V3.2 (Ollama)', 4, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
935
+ ['ollama', 'cogito-2.1:671b', 'Cogito 2.1 671B (Ollama)', 4, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
936
+ ['ollama', 'kimi-k2-thinking', 'Kimi K2 Thinking (Ollama)', 5, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
937
+ ['ollama', 'glm-4.7', 'GLM-4.7 (Ollama)', 6, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
938
+ ['ollama', 'gpt-oss:120b', 'GPT-OSS 120B (Ollama)', 6, 9, 'Large', null, null, null, null, '~10-20M', 131072],
939
+ ['ollama', 'devstral-2:123b', 'Devstral 2 123B (Ollama)', 8, 10, 'Large', null, null, null, null, '~10-20M', 131072],
940
+ ['ollama', 'gpt-oss:20b', 'GPT-OSS 20B (Ollama)', 18, 10, 'Medium', null, null, null, null, '~20-30M', 131072],
941
+ ['ollama', 'gemma4:31b', 'Gemma 4 31B (Ollama)', 22, 10, 'Medium', null, null, null, null, '~20-30M', 131072],
942
+ ];
943
+ const apply = db.transaction(() => {
944
+ for (const a of additions)
945
+ insert.run(...a);
946
+ const missing = db.prepare(`
947
+ SELECT m.id FROM models m
948
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
949
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
950
+ `).all();
951
+ if (missing.length > 0) {
952
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
953
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
954
+ for (let i = 0; i < missing.length; i++)
955
+ addFb.run(missing[i].id, maxPriority + i + 1);
956
+ }
957
+ });
958
+ apply();
959
+ }
960
+ /**
961
+ * V11 (May 2026):
962
+ * 1. Fix long-standing bug: Cerebras `qwen3-235b` was inserted with the
963
+ * wrong model_id in the original seed (real id is
964
+ * `qwen-3-235b-a22b-instruct-2507`). Subsequent rank/limit updates that
965
+ * target the correct id have been silent no-ops since V0 on fresh deploys.
966
+ * 2. Re-enable NVIDIA NIM — `meta/llama-3.1-70b-instruct` was disabled in V2
967
+ * when NIM moved to credits. Per May 2026 audit it's free again (~1,000
968
+ * starter credits never expire, 40 RPM/model).
969
+ * 3. Add three new aggregator/anon-friendly platforms confirmed live May 2026:
970
+ * Kilo Gateway, Pollinations, LLM7.io — all three accept anonymous
971
+ * requests on at least one model.
972
+ * - The user still needs a placeholder key entry (any non-empty string
973
+ * works) because the router filters on `keys.length === 0` to decide
974
+ * whether a platform is routable.
975
+ * Chutes was evaluated and dropped: probe with a free-tier key returned
976
+ * 402 on every model — "Quota exceeded and account balance is $0.0,
977
+ * please pay with fiat or send tao". The "free" tier requires a paid
978
+ * balance, which conflicts with the no-card criterion.
979
+ */
980
+ function migrateModelsV11(db) {
981
+ // 1) Rename cerebras qwen3-235b → qwen-3-235b-a22b-instruct-2507 if the
982
+ // old id still exists on this DB. Safe to re-run because of the WHERE.
983
+ db.prepare(`
984
+ UPDATE models SET model_id = 'qwen-3-235b-a22b-instruct-2507'
985
+ WHERE platform = 'cerebras' AND model_id = 'qwen3-235b'
986
+ `).run();
987
+ // 2) Re-enable NVIDIA NIM (still has 1,000+ starter credits free-tier).
988
+ db.prepare(`
989
+ UPDATE models SET enabled = 1, monthly_token_budget = '~3M (1k credits)'
990
+ WHERE platform = 'nvidia' AND model_id = 'meta/llama-3.1-70b-instruct'
991
+ `).run();
992
+ // 3) Add catalog rows for the four new platforms. Numeric limits are
993
+ // conservative — provider docs publish best-effort bounds that fluctuate.
994
+ const insert = db.prepare(`
995
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
996
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
997
+ `);
998
+ const additions = [
999
+ // NVIDIA NIM — live-probed May 2026 with a free-tier key. All 8 returned
1000
+ // 200 + content. Limits are per-model: 40 RPM, shared 1k starter credits
1001
+ // (never-expire) used for the rough budget estimate. The existing
1002
+ // meta/llama-3.1-70b-instruct row stays (re-enabled above).
1003
+ ['nvidia', 'meta/llama-3.3-70b-instruct', 'Llama 3.3 70B (NV)', 17, 6, 'Large', 40, null, null, null, '~3M (credits)', 131072],
1004
+ ['nvidia', 'meta/llama-4-maverick-17b-128e-instruct', 'Llama 4 Maverick (NV)', 11, 6, 'Large', 40, null, null, null, '~3M (credits)', 131072],
1005
+ ['nvidia', 'deepseek-ai/deepseek-v4-pro', 'DeepSeek V4 Pro (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 131072],
1006
+ ['nvidia', 'mistralai/mistral-large-3-675b-instruct-2512', 'Mistral Large 3 675B (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 131072],
1007
+ ['nvidia', 'minimaxai/minimax-m2.7', 'MiniMax M2.7 (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 196608],
1008
+ ['nvidia', 'nvidia/nemotron-3-super-120b-a12b', 'Nemotron 3 Super 120B (NV)', 22, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 262144],
1009
+ ['nvidia', 'nvidia/nemotron-3-nano-30b-a3b', 'Nemotron 3 Nano 30B (NV)', 22, 9, 'Medium', 40, null, null, null, '~3M (credits)', 262144],
1010
+ ['nvidia', 'google/gemma-4-31b-it', 'Gemma 4 31B (NV)', 19, 9, 'Medium', 40, null, null, null, '~3M (credits)', 262144],
1011
+ ['nvidia', 'moonshotai/kimi-k2.6', 'Kimi K2.6 (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 131072],
1012
+ // Cerebras — live-probed May 2026 with a free-tier key. Both 200 + content.
1013
+ // gpt-oss-120b was removed in V2 ("requires special access, 404 on our
1014
+ // key") but is reachable on the current free tier — re-add. llama3.1-8b
1015
+ // is the fast small-model alternative (no hyphen, distinct from Groq's
1016
+ // llama-3.1-8b-instant id). Free-pool limits match qwen-3-235b row.
1017
+ ['cerebras', 'gpt-oss-120b', 'GPT-OSS 120B (Cerebras)', 6, 1, 'Large', 30, 1000, 60000, 1000000, '~30M', 131072],
1018
+ ['cerebras', 'llama3.1-8b', 'Llama 3.1 8B (Cerebras)', 28, 1, 'Small', 30, 1000, 60000, 1000000, '~30M', 131072],
1019
+ // Groq compound — agent system that internally routes through gpt-oss
1020
+ // models and exposes the trace in usage metadata. Standard chat-completions
1021
+ // shape works (200 + content). Same free-tier limits as other Groq rows.
1022
+ ['groq', 'groq/compound', 'Compound (Groq)', 6, 2, 'Large', 30, 1000, 8000, 200000, '~6M', 131072],
1023
+ ['groq', 'groq/compound-mini', 'Compound Mini (Groq)', 18, 2, 'Medium', 30, 1000, 8000, 200000, '~6M', 131072],
1024
+ // Kilo Gateway — 200 req/hr per IP anon. Most named :free routes have
1025
+ // transitioned to paid ("free period ended"); probe-confirmed live:
1026
+ ['kilo', 'nvidia/nemotron-3-super-120b-a12b:free', 'Nemotron 3 Super 120B (Kilo)', 22, 9, 'Frontier', null, null, null, null, '~2-3M (200/hr)', 262144],
1027
+ // Pollinations — anonymous /openai endpoint. Public model list returns
1028
+ // just one anonymous-tier entry. Tool calls supported per their metadata.
1029
+ ['pollinations', 'openai-fast', 'GPT-OSS 20B (Pollinations)', 18, 10, 'Medium', null, null, null, null, '~? (anon)', 131072],
1030
+ // LLM7.io — 100 req/hr free (anonymous works). Probe-confirmed list:
1031
+ ['llm7', 'gpt-oss-20b', 'GPT-OSS 20B (LLM7)', 18, 10, 'Medium', 100, null, null, null, '~2-3M (100/hr)', 131072],
1032
+ ['llm7', 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo', 'Llama 3.1 8B Turbo (LLM7)', 28, 10, 'Small', 100, null, null, null, '~2-3M (100/hr)', 131072],
1033
+ ['llm7', 'codestral-latest', 'Codestral (LLM7)', 16, 8, 'Medium', 100, null, null, null, '~2-3M (100/hr)', 32000],
1034
+ ['llm7', 'ministral-8b-2512', 'Ministral 8B (LLM7)', 28, 10, 'Small', 100, null, null, null, '~2-3M (100/hr)', 131072],
1035
+ ['llm7', 'GLM-4.6V-Flash', 'GLM-4.6V Flash (LLM7)', 15, 9, 'Large', 100, null, null, null, '~2-3M (100/hr)', 131072],
1036
+ ];
1037
+ const apply = db.transaction(() => {
1038
+ for (const a of additions)
1039
+ insert.run(...a);
1040
+ const missing = db.prepare(`
1041
+ SELECT m.id FROM models m
1042
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
1043
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
1044
+ `).all();
1045
+ if (missing.length > 0) {
1046
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
1047
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
1048
+ for (let i = 0; i < missing.length; i++)
1049
+ addFb.run(missing[i].id, maxPriority + i + 1);
1050
+ }
1051
+ });
1052
+ apply();
1053
+ }
1054
+ /**
1055
+ * V12 (May 2026): live-probed delta against OpenRouter's free pool.
1056
+ *
1057
+ * Removals (both confirmed 404 "no longer available as a free model" — moved
1058
+ * to paid SKUs at the same id without the :free suffix):
1059
+ * - inclusionai/ling-2.6-1t:free
1060
+ * - tencent/hy3-preview:free
1061
+ *
1062
+ * Additions (probe-verified 200 + tool_calls with tool_choice=auto on the
1063
+ * user's OR key; tool_choice=required is rejected by all three providers,
1064
+ * but the router's tool requests use the OpenAI default of auto):
1065
+ * - arcee-ai/trinity-large-thinking:free — Arcee's Trinity *Thinking*
1066
+ * successor to the trinity-large-preview:free row pulled in V6 (404 then).
1067
+ * - baidu/cobuddy:free — Baidu Qianfan coding/agent
1068
+ * model with native tool use + reasoning.
1069
+ * - openrouter/owl-alpha — OR-house agentic foundation
1070
+ * model. Zero-priced but no :free suffix, so it sits on a different rate
1071
+ * pool than the shared :free 20 RPM / 200 RPD bucket. 1M ctx.
1072
+ * - nousresearch/hermes-3-llama-3.1-405b:free — 405B route ranked in V3
1073
+ * but never inserted. Currently 429 on probe (upstream throttle), not
1074
+ * gone. No tools support listed; router falls past it on tool requests.
1075
+ *
1076
+ * Dropped from the add list:
1077
+ * - deepseek/deepseek-v4-flash:free — listed at $0 in /v1/models but the
1078
+ * Crucible provider returns 402 "Out of credits" — not actually free.
1079
+ *
1080
+ * Context-window corrections — OR raised these to match upstream provider
1081
+ * caps; our seeded values were stale:
1082
+ * - nvidia/nemotron-3-super-120b-a12b:free 262144 → 1000000
1083
+ * - qwen/qwen3-coder:free 262144 → 1048576
1084
+ */
1085
+ function migrateModelsV12(db) {
1086
+ const deleteModel = db.prepare(`DELETE FROM models WHERE platform = ? AND model_id = ?`);
1087
+ const deleteFallback = db.prepare(`
1088
+ DELETE FROM fallback_config WHERE model_db_id IN (
1089
+ SELECT id FROM models WHERE platform = ? AND model_id = ?
1090
+ )
1091
+ `);
1092
+ const removals = [
1093
+ ['openrouter', 'inclusionai/ling-2.6-1t:free'],
1094
+ ['openrouter', 'tencent/hy3-preview:free'],
1095
+ ];
1096
+ const applyRemovals = db.transaction(() => {
1097
+ for (const [p, m] of removals) {
1098
+ deleteFallback.run(p, m);
1099
+ deleteModel.run(p, m);
1100
+ }
1101
+ });
1102
+ applyRemovals();
1103
+ // Context-window upgrades for existing rows.
1104
+ db.prepare(`
1105
+ UPDATE models SET context_window = 1000000
1106
+ WHERE platform = 'openrouter' AND model_id = 'nvidia/nemotron-3-super-120b-a12b:free'
1107
+ `).run();
1108
+ db.prepare(`
1109
+ UPDATE models SET context_window = 1048576
1110
+ WHERE platform = 'openrouter' AND model_id = 'qwen/qwen3-coder:free'
1111
+ `).run();
1112
+ const insert = db.prepare(`
1113
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
1114
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1115
+ `);
1116
+ // :free pool quotas as elsewhere in catalog: 20 RPM / 200 RPD / ~6M tokens.
1117
+ // openrouter/owl-alpha sits on the non-:free zero-priced pool — quotas
1118
+ // unpublished; mirror :free numbers conservatively so it cools down on 429.
1119
+ const additions = [
1120
+ ['openrouter', 'arcee-ai/trinity-large-thinking:free', 'Trinity Large Thinking (free)', 5, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
1121
+ ['openrouter', 'baidu/cobuddy:free', 'CoBuddy (free)', 6, 9, 'Large', 20, 200, null, null, '~6M', 131072],
1122
+ ['openrouter', 'openrouter/owl-alpha', 'Owl Alpha (OR-house)', 5, 9, 'Frontier', 20, 200, null, null, '~6M', 1048576],
1123
+ ['openrouter', 'nousresearch/hermes-3-llama-3.1-405b:free', 'Hermes 3 405B (free)', 17, 9, 'Large', 20, 200, null, null, '~6M', 131072],
1124
+ ];
1125
+ const apply = db.transaction(() => {
1126
+ for (const a of additions)
1127
+ insert.run(...a);
1128
+ const missing = db.prepare(`
1129
+ SELECT m.id FROM models m
1130
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
1131
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
1132
+ `).all();
1133
+ if (missing.length > 0) {
1134
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
1135
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
1136
+ for (let i = 0; i < missing.length; i++)
1137
+ addFb.run(missing[i].id, maxPriority + i + 1);
1138
+ }
1139
+ });
1140
+ apply();
1141
+ }
1142
+ /**
1143
+ * V13 (May 2026): cross-provider catalog refresh — live-probed against the
1144
+ * user's keys for Cerebras, Groq, SambaNova, Cloudflare, Google, NVIDIA,
1145
+ * Mistral, Cohere, Ollama, HuggingFace, Z.ai, Chutes. Z.ai/Chutes confirmed
1146
+ * no-change (non-flash Z.ai SKUs require recharge; Chutes still gated on
1147
+ * $0 balance).
1148
+ *
1149
+ * DISABLES (row kept for re-enable history; pattern follows V5/V9):
1150
+ * - google/gemini-3.1-pro-preview — 429 with quotaMetric `free_tier_requests`
1151
+ * limit=0; moved off free tier. (V6 inferred Pro was free; lapsed since.)
1152
+ * - ollama/kimi-k2-thinking, ollama/mistral-large-3:675b, ollama/deepseek-v3.2
1153
+ * — all return 403 "this model requires a subscription" on the Free plan
1154
+ * now. (V10 catalogued them as Free; Ollama paywalled them since.)
1155
+ *
1156
+ * HARD REMOVALS (model is gone, not paywalled):
1157
+ * - sambanova/DeepSeek-V3.1-cb — absent from live /v1/models; V8 was over-eager.
1158
+ * - cloudflare/@cf/moonshotai/kimi-k2.5 — CF changelog: aliases to k2.6 on
1159
+ * 2026-05-30. Two rows for one backend wastes a fallback slot.
1160
+ *
1161
+ * UPDATES (numbers verified from live response headers or /v1/models):
1162
+ * - cerebras: all 3 enabled rows → 5 RPM / 30K TPM / 2400 RPD / 1M TPD
1163
+ * (V4's 30/14400/60K/1M was stale; current free pool is 5 RPM shared).
1164
+ * - groq/llama-3.3-70b-versatile: TPD 500K → 100K (docs).
1165
+ * - groq/llama-4-scout: TPM 6K → 30K (live header).
1166
+ * - groq/compound + groq/compound-mini: RPD 1000→250, TPM 8K→70K, TPD→null
1167
+ * (compound systems run on a separate budget per docs + live header).
1168
+ * - sambanova/DeepSeek-V3.2: ctx 131K → 32K (/v1/models reports 32768).
1169
+ * - cloudflare/@cf/meta/llama-3.3-70b-instruct-fp8-fast: ctx 131K → 24K
1170
+ * (CF model page: fp8-fast variant capped at 24K).
1171
+ * - mistral context windows (all stale vs /v1/models):
1172
+ * codestral-latest 32K → 256K
1173
+ * devstral-latest 131K → 262K
1174
+ * magistral-medium 40K → 131K
1175
+ * mistral-large-latest 131K → 262K
1176
+ *
1177
+ * ADDITIONS (all chat-probed; tools verified with tool_choice=auto):
1178
+ * - groq: openai/gpt-oss-safeguard-20b (tool-tuned 20B; same pool as gpt-oss-20b)
1179
+ * - cloudflare: @cf/nvidia/nemotron-3-120b-a12b, @cf/google/gemma-4-26b-a4b-it
1180
+ * (both 256K ctx, function_calling=true; 429'd on probe due to daily
1181
+ * neuron exhaustion, not model-level — same Free pool as other CF rows)
1182
+ * - google: gemini-3.5-flash (2026-05 release; 1M ctx; tool calls verified)
1183
+ * - nvidia: deepseek-ai/deepseek-v4-flash, z-ai/glm-5.1 (~2min cold start),
1184
+ * qwen/qwen3-coder-480b-a35b-instruct (full id — discovered via /v1/models)
1185
+ * - mistral: mistral-small-latest (Small 4), ministral-8b-latest (edge 8B)
1186
+ * - cohere: command-a-reasoning-08-2025, command-r-08-2024. README ToS
1187
+ * table marks Cohere "❌ Avoid for personal use" — these rows expand
1188
+ * fallback coverage but inherit the same caveat.
1189
+ * - ollama: qwen3-coder-next (~80B-A3B coder)
1190
+ *
1191
+ * NEW PLATFORM (huggingface):
1192
+ * - V4 removed HF for "tool-call format issues" on the legacy serverless
1193
+ * endpoint that emitted tool calls as text. The new router.huggingface.co
1194
+ * meta-router uses each backend's native protocol and normalizes the
1195
+ * response — chat-probed clean tool_calls on DeepSeek-V4-Flash, Kimi-K2.6,
1196
+ * Qwen3-Coder-Next, GLM-4.7, Qwen3-235B. Recurring $0.10/mo router credit
1197
+ * on the free tier (no card, no expiry). Budget ~1-3M tokens/mo depending
1198
+ * on backend. Seeded with 3 frontier rows.
1199
+ * - Provider registration lives in server/src/providers/index.ts.
1200
+ *
1201
+ * NO-CHANGE PROVIDERS (probed, nothing to update):
1202
+ * - openrouter (V12 just shipped)
1203
+ * - z.ai/zhipu — non-flash SKUs all 1113 "insufficient balance"; flash-only
1204
+ * pool is correctly catalogued.
1205
+ * - chutes — every probe 402 on $0 balance; V11 drop decision stands.
1206
+ *
1207
+ * DEFERRED:
1208
+ * - cerebras/qwen-3-235b-a22b-instruct-2507 + llama3.1-8b: docs flag
1209
+ * hard-deprecation 2026-05-27 (4d from migration), but both still 200
1210
+ * today. Disable in V14 on/after that date.
1211
+ * - sambanova: every chat probe returned 402 PAYMENT_METHOD_REQUIRED on the
1212
+ * user's account. /v1/models still lists rows so REMOVE/UPDATE above is
1213
+ * valid, but the SambaNova free tier may have lapsed account-wide.
1214
+ * Investigate before adding new SambaNova rows.
1215
+ * - github: gpt-5 family + xai/grok-3 both 400 "unavailable_model" on free
1216
+ * tier — keep gpt-4o (V2 verdict still holds).
1217
+ */
1218
+ function migrateModelsV13(db) {
1219
+ // 1) Disables (row kept; can be re-enabled without losing fallback history).
1220
+ const disable = db.prepare(`UPDATE models SET enabled = 0 WHERE platform = ? AND model_id = ?`);
1221
+ const disables = [
1222
+ ['google', 'gemini-3.1-pro-preview'],
1223
+ ['ollama', 'kimi-k2-thinking'],
1224
+ ['ollama', 'mistral-large-3:675b'],
1225
+ ['ollama', 'deepseek-v3.2'],
1226
+ ];
1227
+ for (const [p, m] of disables)
1228
+ disable.run(p, m);
1229
+ // 2) Hard removals.
1230
+ const deleteModel = db.prepare(`DELETE FROM models WHERE platform = ? AND model_id = ?`);
1231
+ const deleteFallback = db.prepare(`
1232
+ DELETE FROM fallback_config WHERE model_db_id IN (
1233
+ SELECT id FROM models WHERE platform = ? AND model_id = ?
1234
+ )
1235
+ `);
1236
+ const removals = [
1237
+ ['sambanova', 'DeepSeek-V3.1-cb'],
1238
+ ['cloudflare', '@cf/moonshotai/kimi-k2.5'],
1239
+ ];
1240
+ const applyRemovals = db.transaction(() => {
1241
+ for (const [p, m] of removals) {
1242
+ deleteFallback.run(p, m);
1243
+ deleteModel.run(p, m);
1244
+ }
1245
+ });
1246
+ applyRemovals();
1247
+ // 3) Cerebras free-pool limit correction (3 enabled rows; zai-glm-4.7 stays
1248
+ // on its V5-set per-model 10/100 cap since it's gated separately).
1249
+ db.prepare(`
1250
+ UPDATE models
1251
+ SET rpm_limit = 5, rpd_limit = 2400, tpm_limit = 30000, tpd_limit = 1000000
1252
+ WHERE platform = 'cerebras'
1253
+ AND model_id IN ('qwen-3-235b-a22b-instruct-2507', 'gpt-oss-120b', 'llama3.1-8b')
1254
+ `).run();
1255
+ // 4) Groq limit corrections.
1256
+ db.prepare(`UPDATE models SET tpd_limit = 100000 WHERE platform = 'groq' AND model_id = 'llama-3.3-70b-versatile'`).run();
1257
+ db.prepare(`UPDATE models SET tpm_limit = 30000 WHERE platform = 'groq' AND model_id = 'meta-llama/llama-4-scout-17b-16e-instruct'`).run();
1258
+ db.prepare(`
1259
+ UPDATE models SET rpd_limit = 250, tpm_limit = 70000, tpd_limit = NULL
1260
+ WHERE platform = 'groq' AND model_id IN ('groq/compound', 'groq/compound-mini')
1261
+ `).run();
1262
+ // 5) Single-row context-window corrections.
1263
+ db.prepare(`UPDATE models SET context_window = 32768 WHERE platform = 'sambanova' AND model_id = 'DeepSeek-V3.2'`).run();
1264
+ db.prepare(`UPDATE models SET context_window = 24000 WHERE platform = 'cloudflare' AND model_id = '@cf/meta/llama-3.3-70b-instruct-fp8-fast'`).run();
1265
+ // 6) Mistral context-window corrections.
1266
+ db.prepare(`UPDATE models SET context_window = 256000 WHERE platform = 'mistral' AND model_id = 'codestral-latest'`).run();
1267
+ db.prepare(`UPDATE models SET context_window = 262144 WHERE platform = 'mistral' AND model_id = 'devstral-latest'`).run();
1268
+ db.prepare(`UPDATE models SET context_window = 131072 WHERE platform = 'mistral' AND model_id = 'magistral-medium-latest'`).run();
1269
+ db.prepare(`UPDATE models SET context_window = 262144 WHERE platform = 'mistral' AND model_id = 'mistral-large-latest'`).run();
1270
+ // 7) Additions across providers (chat-probed; tools verified where claimed).
1271
+ const insert = db.prepare(`
1272
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
1273
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1274
+ `);
1275
+ const additions = [
1276
+ // Groq — shared 30 RPM / 1K RPD / 8K TPM / 200K TPD per-model pool.
1277
+ ['groq', 'openai/gpt-oss-safeguard-20b', 'GPT-OSS Safeguard 20B (Groq)', 18, 2, 'Medium', 30, 1000, 8000, 200000, '~6M', 131072],
1278
+ // Cloudflare — 10K Neurons/day shared free pool.
1279
+ ['cloudflare', '@cf/nvidia/nemotron-3-120b-a12b', 'Nemotron 3 120B (CF)', 9, 11, 'Frontier', null, null, null, null, '~5-10M', 262144],
1280
+ ['cloudflare', '@cf/google/gemma-4-26b-a4b-it', 'Gemma 4 26B-A4B it (CF)', 22, 11, 'Medium', null, null, null, null, '~10-20M', 262144],
1281
+ // Google — same 20 RPD per-model free pool. 3.5 Flash is the current Flash flagship.
1282
+ ['google', 'gemini-3.5-flash', 'Gemini 3.5 Flash', 3, 5, 'Large', 10, 20, 250000, null, '~3M', 1048576],
1283
+ // NVIDIA NIM — credits-based; per-model 40 RPM.
1284
+ ['nvidia', 'deepseek-ai/deepseek-v4-flash', 'DeepSeek V4 Flash (NV)', 4, 9, 'Frontier', 40, null, null, null, '~3M (credits)', 131072],
1285
+ ['nvidia', 'z-ai/glm-5.1', 'GLM-5.1 (NV, slow cold-start)', 5, 9, 'Frontier', 40, null, null, null, '~3M (credits)', 200000],
1286
+ ['nvidia', 'qwen/qwen3-coder-480b-a35b-instruct', 'Qwen3-Coder 480B (NV)', 2, 9, 'Frontier', 40, null, null, null, '~3M (credits)', 262144],
1287
+ // Mistral — Experiment plan 2 RPM / 500K TPM / shared ~1B/mo.
1288
+ ['mistral', 'mistral-small-latest', 'Mistral Small 4', 14, 8, 'Medium', 2, null, 500000, null, '~50-100M', 262144],
1289
+ ['mistral', 'ministral-8b-latest', 'Ministral 3 8B', 28, 8, 'Small', 2, null, 500000, null, '~50-100M', 262144],
1290
+ // Cohere — trial 20 RPM / 1000 RPM total. ToS table marks ❌ Avoid for personal use.
1291
+ ['cohere', 'command-a-reasoning-08-2025', 'Command A Reasoning (08-2025)', 13, 11, 'Large', 20, 33, null, null, '~1-2M', 256000],
1292
+ ['cohere', 'command-r-08-2024', 'Command R (08-2024)', 25, 11, 'Medium', 20, 33, null, null, '~1-2M', 131072],
1293
+ // Ollama Cloud — GPU-time quota.
1294
+ ['ollama', 'qwen3-coder-next', 'Qwen3-Coder Next (Ollama)', 3, 9, 'Large', null, null, null, null, '~10-20M', 262144],
1295
+ // HuggingFace router (new platform) — recurring $0.10/mo credit, no card.
1296
+ ['huggingface', 'deepseek-ai/DeepSeek-V4-Flash', 'DeepSeek V4 Flash (HF)', 4, 9, 'Frontier', null, null, null, null, '~1-3M', 131072],
1297
+ ['huggingface', 'moonshotai/Kimi-K2.6', 'Kimi K2.6 (HF)', 3, 9, 'Frontier', null, null, null, null, '~1-3M', 262144],
1298
+ ['huggingface', 'Qwen/Qwen3-Coder-Next', 'Qwen3-Coder Next (HF)', 3, 9, 'Large', null, null, null, null, '~1-3M', 262144],
1299
+ ];
1300
+ const apply = db.transaction(() => {
1301
+ for (const a of additions)
1302
+ insert.run(...a);
1303
+ const missing = db.prepare(`
1304
+ SELECT m.id FROM models m
1305
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
1306
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
1307
+ `).all();
1308
+ if (missing.length > 0) {
1309
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
1310
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
1311
+ for (let i = 0; i < missing.length; i++)
1312
+ addFb.run(missing[i].id, maxPriority + i + 1);
1313
+ }
1314
+ });
1315
+ apply();
1316
+ }
1317
+ /**
1318
+ * V14 (May 2026): Cerebras hard-deprecation 2026-05-27.
1319
+ *
1320
+ * Per inference-docs.cerebras.ai/models/overview, both
1321
+ * `qwen-3-235b-a22b-instruct-2507` and `llama3.1-8b` hit a hard deprecation
1322
+ * on 2026-05-27 with no announced free-tier replacement at the same
1323
+ * parameter class. Disable both ahead of the cutover so the router stops
1324
+ * sending traffic to them. Row kept (not deleted) so it can be re-enabled
1325
+ * if Cerebras restores or renames either model — same pattern as V9's
1326
+ * disable of `zai-glm-4.7`.
1327
+ *
1328
+ * Cerebras `gpt-oss-120b` is NOT in the deprecation list and stays enabled
1329
+ * as the sole free-tier Cerebras route.
1330
+ */
1331
+ function migrateModelsV14(db) {
1332
+ db.prepare(`
1333
+ UPDATE models SET enabled = 0
1334
+ WHERE platform = 'cerebras'
1335
+ AND model_id IN ('qwen-3-235b-a22b-instruct-2507', 'llama3.1-8b')
1336
+ `).run();
1337
+ }
1338
+ /**
1339
+ * V15 (May 2026): purge SiliconFlow.
1340
+ *
1341
+ * SiliconFlow was briefly added (#131) on the belief Qwen/Qwen3-8B was a $0
1342
+ * "free model". Re-verification showed it is PAID ($0.06/M in+out): the
1343
+ * account balance dropped 0.9999 -> 0.9998 across ~2.7K tokens, and the
1344
+ * official pricing page lists it at $0.06/M. The earlier "zero-cost" read was
1345
+ * a 4-decimal rounding artifact on a tiny call. SiliconFlow's .com endpoint is
1346
+ * a one-time $1 trial credit, not a recurring free tier — same disqualifier
1347
+ * as Chutes — so the provider was reverted. This removes any orphaned row from
1348
+ * a DB that already ran the original V15. No-op on DBs that never had it.
1349
+ */
1350
+ function migrateModelsV15(db) {
1351
+ db.prepare(`
1352
+ DELETE FROM fallback_config WHERE model_db_id IN (
1353
+ SELECT id FROM models WHERE platform = 'siliconflow'
1354
+ )
1355
+ `).run();
1356
+ db.prepare(`DELETE FROM models WHERE platform = 'siliconflow'`).run();
1357
+ }
1358
+ // Adds the supports_vision column to existing DBs and (re)applies the vision
1359
+ // flags by rule. Rule-based rather than a hardcoded id list because the catalog
1360
+ // churns through migrations (model ids get renamed/replaced) — rules survive
1361
+ // that. Two constraints decide a model can accept images:
1362
+ // 1. The model is genuinely multimodal (every Gemini is; Llama 4 Scout/
1363
+ // Maverick are; GitHub's GPT-4o/4.1/5 are).
1364
+ // 2. Its provider adapter forwards image content. OpenAICompat and Google do;
1365
+ // Cohere and Cloudflare flatten content to text, so models on those
1366
+ // platforms are excluded even when the underlying model can see.
1367
+ // Conservative on purpose: with hard-fail routing a false negative is just a
1368
+ // clear "no vision model" error, while a false positive routes an image to a
1369
+ // model that chokes. Idempotent — safe on fresh seeds and upgrades alike.
1370
+ function migrateModelsV16Vision(db) {
1371
+ const columns = db.prepare('PRAGMA table_info(models)').all();
1372
+ if (!columns.some(col => col.name === 'supports_vision')) {
1373
+ db.prepare('ALTER TABLE models ADD COLUMN supports_vision INTEGER NOT NULL DEFAULT 0').run();
1374
+ }
1375
+ const apply = db.transaction(() => {
1376
+ // Reset first so de-flagged models (e.g. an id that moved to Cloudflare)
1377
+ // don't keep a stale flag across re-runs.
1378
+ db.prepare('UPDATE models SET supports_vision = 0').run();
1379
+ // Every Gemini is multimodal (the 'google' platform is all Gemini).
1380
+ db.prepare("UPDATE models SET supports_vision = 1 WHERE platform = 'google'").run();
1381
+ // Llama 4 (Scout/Maverick) is natively multimodal — but only where the
1382
+ // adapter forwards images (exclude the text-flattening providers).
1383
+ db.prepare(`
1384
+ UPDATE models SET supports_vision = 1
1385
+ WHERE LOWER(model_id) LIKE '%llama-4%'
1386
+ AND platform NOT IN ('cloudflare', 'cohere')
1387
+ `).run();
1388
+ // GitHub's OpenAI vision models.
1389
+ db.prepare(`
1390
+ UPDATE models SET supports_vision = 1
1391
+ WHERE platform = 'github'
1392
+ AND (model_id LIKE '%gpt-4o%' OR model_id LIKE '%gpt-4.1%' OR model_id LIKE '%gpt-5%')
1393
+ `).run();
1394
+ // V23 vision additions, both live-probed 2026-06-07 (answered a color
1395
+ // question about an inline data-URL PNG): Zhipu's free GLM-4.6V Flash and
1396
+ // NVIDIA's Nemotron Nano 12B v2 VL (via OpenRouter).
1397
+ db.prepare(`
1398
+ UPDATE models SET supports_vision = 1
1399
+ WHERE LOWER(model_id) LIKE '%glm-4.6v%'
1400
+ OR LOWER(model_id) LIKE '%nemotron-nano-12b-v2-vl%'
1401
+ `).run();
1402
+ });
1403
+ apply();
1404
+ }
1405
+ // ── V17: intelligence tier audit (2026-06) ──
1406
+ // `size_label` is the cross-provider capability tier that DOMINATES the router's
1407
+ // intelligence axis (intelligenceComposite = tier*1000 - intelligence_rank in
1408
+ // services/router.ts), so it must track real benchmarks rather than the
1409
+ // release-day guesses several rows were seeded with. This normalizes every model
1410
+ // family that has a published Artificial Analysis Intelligence Index v4.0 score
1411
+ // (served/default mode, June 2026) to the correct tier — and as a side effect
1412
+ // fixes cross-provider inconsistencies where the same model family had landed in
1413
+ // different tiers per provider (e.g. Llama 4 Scout, Llama 3.3 70B).
1414
+ //
1415
+ // Tier bands by AA Index v4.0: Frontier ≥45 · Large 26–44 · Medium 13–25 · Small ≤12.
1416
+ //
1417
+ // Rules are keyed by model_id LIKE patterns so one rule covers a family across
1418
+ // all providers. Models with NO published AA Index are intentionally left at
1419
+ // their seeded tier (Cogito 2.1, Owl Alpha, Poolside Laguna, Hermes 3 405B,
1420
+ // Baidu CoBuddy, Groq Compound, GLM-4.6V Flash, GLM-4.5 Flash, Mistral Small 4,
1421
+ // Devstral). intelligence_rank (the within-tier tiebreak, low impact) is left
1422
+ // untouched. Idempotent — every statement is an absolute SET, safe to re-run.
1423
+ function migrateModelsV17IntelligenceTiers(db) {
1424
+ const apply = db.transaction(() => {
1425
+ // Frontier (AA ≥ 45): genuine frontier-class. Promotes Gemini 3.5 Flash (55)
1426
+ // and Gemini 3 Flash Preview (46) up from Large.
1427
+ db.prepare(`
1428
+ UPDATE models SET size_label = 'Frontier' WHERE
1429
+ LOWER(model_id) LIKE '%gemini-3.1-pro%'
1430
+ OR LOWER(model_id) LIKE '%gemini-3.5-flash%'
1431
+ OR LOWER(model_id) LIKE '%gemini-3-flash%'
1432
+ OR LOWER(model_id) LIKE '%kimi-k2.6%'
1433
+ OR LOWER(model_id) LIKE '%kimi-k2-thinking%'
1434
+ OR LOWER(model_id) LIKE '%deepseek-v4-pro%'
1435
+ OR LOWER(model_id) LIKE '%deepseek-v4-flash%'
1436
+ OR LOWER(model_id) LIKE '%glm-5.1%'
1437
+ OR LOWER(model_id) LIKE '%minimax-m2.7%'
1438
+ `).run();
1439
+ // Large (AA 26–44). Demotes Gemini 2.5 Pro (35), Nemotron 3 Super/120B (36),
1440
+ // GLM-4.7 (42), DeepSeek V3.1/V3.2 (28/32), Trinity (32) down from Frontier;
1441
+ // promotes Gemma 4 31B (39) / 26B (31) and Gemini 3.1 Flash-Lite (34) up.
1442
+ db.prepare(`
1443
+ UPDATE models SET size_label = 'Large' WHERE
1444
+ LOWER(model_id) LIKE '%minimax-m2.5%'
1445
+ OR LOWER(model_id) LIKE '%qwen3-next%'
1446
+ OR LOWER(model_id) LIKE '%qwen3-coder-next%'
1447
+ OR LOWER(model_id) LIKE '%gpt-oss-120b%' OR LOWER(model_id) LIKE '%gpt-oss:120b%'
1448
+ OR LOWER(model_id) LIKE '%glm-4.7%'
1449
+ OR LOWER(model_id) LIKE '%nemotron-3-super%' OR LOWER(model_id) LIKE '%nemotron-3-120b%'
1450
+ OR LOWER(model_id) LIKE '%gemini-2.5-pro%'
1451
+ OR LOWER(model_id) LIKE '%deepseek-v3.2%'
1452
+ OR LOWER(model_id) LIKE '%deepseek-v3.1%'
1453
+ OR LOWER(model_id) LIKE '%trinity-large%'
1454
+ OR LOWER(model_id) LIKE '%mistral-medium%'
1455
+ OR LOWER(model_id) LIKE '%magistral-medium%'
1456
+ OR LOWER(model_id) LIKE '%gpt-4.1%'
1457
+ OR LOWER(model_id) LIKE '%gemma-4-31b%' OR LOWER(model_id) LIKE '%gemma4:31b%'
1458
+ OR LOWER(model_id) LIKE '%gemma-4-26b%'
1459
+ OR LOWER(model_id) LIKE '%gemini-3.1-flash-lite%'
1460
+ `).run();
1461
+ // Medium (AA 13–25). Demotes Qwen3-Coder 480B (25) and Mistral Large 3 (23)
1462
+ // down from Frontier; Llama 4 Maverick (18), GPT-4o (17), Gemini 2.5 Flash
1463
+ // (21), GLM-4.5 Air (23), DeepSeek R1 Distill (17), Command A/R+ down from
1464
+ // Large; unifies Llama 4 Scout (14) and Llama 3.3 70B (14) across providers.
1465
+ db.prepare(`
1466
+ UPDATE models SET size_label = 'Medium' WHERE
1467
+ (LOWER(model_id) LIKE '%qwen3-coder%' AND LOWER(model_id) NOT LIKE '%qwen3-coder-next%')
1468
+ OR LOWER(model_id) LIKE '%qwen-3-235b%' OR LOWER(model_id) LIKE '%qwen3-235b%'
1469
+ OR LOWER(model_id) LIKE '%mistral-large%'
1470
+ OR LOWER(model_id) LIKE '%gpt-oss-20b%' OR LOWER(model_id) LIKE '%gpt-oss:20b%'
1471
+ OR LOWER(model_id) LIKE '%gpt-oss-safeguard-20b%' OR model_id = 'openai-fast'
1472
+ OR LOWER(model_id) LIKE '%glm-4.5-air%'
1473
+ OR LOWER(model_id) LIKE '%devstral-2%'
1474
+ OR LOWER(model_id) LIKE '%deepseek-r1-distill%'
1475
+ OR LOWER(model_id) LIKE '%qwen3-30b%'
1476
+ OR LOWER(model_id) LIKE '%qwen3-32b%'
1477
+ OR LOWER(model_id) LIKE '%llama-4-maverick%'
1478
+ OR LOWER(model_id) LIKE '%llama-4-scout%'
1479
+ OR LOWER(model_id) LIKE '%llama-3.3-70b%'
1480
+ OR LOWER(model_id) LIKE '%llama-3.1-70b%'
1481
+ OR (LOWER(model_id) LIKE '%gemini-2.5-flash%' AND LOWER(model_id) NOT LIKE '%flash-lite%')
1482
+ OR LOWER(model_id) LIKE '%gemini-2.5-flash-lite%'
1483
+ OR LOWER(model_id) LIKE '%gpt-4o%'
1484
+ OR LOWER(model_id) LIKE '%command-a-03-2025%'
1485
+ OR LOWER(model_id) LIKE '%command-r-plus%'
1486
+ OR LOWER(model_id) LIKE '%nemotron-3-nano%'
1487
+ OR LOWER(model_id) LIKE '%nemotron-nano-9b%'
1488
+ `).run();
1489
+ // Small (AA ≤ 12). Demotes Gemma 3 12B (9), Command R 08-2024 (legacy ~7),
1490
+ // and Codestral (8) down from Medium.
1491
+ db.prepare(`
1492
+ UPDATE models SET size_label = 'Small' WHERE
1493
+ LOWER(model_id) LIKE '%gemma-3-12b%'
1494
+ OR LOWER(model_id) LIKE '%command-r-08-2024%'
1495
+ OR LOWER(model_id) LIKE '%codestral%'
1496
+ OR LOWER(model_id) LIKE '%llama-3.1-8b%' OR LOWER(model_id) LIKE '%llama3.1-8b%'
1497
+ OR LOWER(model_id) LIKE '%meta-llama-3.1-8b%'
1498
+ OR LOWER(model_id) LIKE '%ministral-8b%'
1499
+ OR LOWER(model_id) LIKE '%granite-4.0-h-micro%'
1500
+ OR LOWER(model_id) LIKE '%lfm-2.5-1.2b%'
1501
+ `).run();
1502
+ });
1503
+ apply();
1504
+ }
1505
+ // ── V18: OpenCode Zen provider (2026-06) ──
1506
+ // Adds the OpenCode Zen gateway (#128, originally contributed by @Aldo-f). Zen is
1507
+ // an OpenAI-compatible service whose paid models bill pay-as-you-go but which
1508
+ // also exposes a rotating set of *promotional* free models. Access is via a FREE
1509
+ // account key from https://opencode.ai/auth — no credit card; billing only
1510
+ // applies if you call paid models. We require that key like any other provider
1511
+ // (we do NOT use Zen's unauthenticated path).
1512
+ //
1513
+ // We seed only the four models the Zen docs explicitly label free — big-pickle,
1514
+ // deepseek-v4-flash-free, mimo-v2.5-free, nemotron-3-super-free. The live
1515
+ // /v1/models list also surfaces qwen3.6-plus-free and minimax-m3-free, but those
1516
+ // are NOT documented as free, so they're intentionally omitted (same "verified
1517
+ // only" bar as V12-V14). Tiers follow V17's bands (deepseek-v4-flash → Frontier,
1518
+ // nemotron-3-super → Large). Caveats per docs: promotional/limited-time, "trial
1519
+ // use only — not for production", and prompts/outputs may be used to improve the
1520
+ // models (NVIDIA logs Nemotron traffic). Conservative shared 20 RPM / 200 RPD,
1521
+ // matching the OpenRouter :free pool pattern. Idempotent (INSERT OR IGNORE +
1522
+ // fallback_config backfill), safe to re-run.
1523
+ function migrateModelsV18OpenCodeZen(db) {
1524
+ const insert = db.prepare(`
1525
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
1526
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1527
+ `);
1528
+ const additions = [
1529
+ ['opencode', 'big-pickle', 'Big Pickle (OpenCode Zen, stealth)', 10, 4, 'Large', 20, 200, null, null, 'promo (trial)', 131072],
1530
+ ['opencode', 'deepseek-v4-flash-free', 'DeepSeek V4 Flash Free (OpenCode Zen)', 4, 4, 'Frontier', 20, 200, null, null, 'promo (trial)', 131072],
1531
+ ['opencode', 'mimo-v2.5-free', 'MiMo-V2.5 Free (OpenCode Zen)', 14, 4, 'Medium', 20, 200, null, null, 'promo (trial)', 131072],
1532
+ ['opencode', 'nemotron-3-super-free', 'Nemotron 3 Super Free (OpenCode Zen)', 12, 4, 'Large', 20, 200, null, null, 'promo (trial)', 131072],
1533
+ ];
1534
+ const apply = db.transaction(() => {
1535
+ for (const a of additions)
1536
+ insert.run(...a);
1537
+ const missing = db.prepare(`
1538
+ SELECT m.id FROM models m
1539
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
1540
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
1541
+ `).all();
1542
+ if (missing.length > 0) {
1543
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
1544
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
1545
+ for (let i = 0; i < missing.length; i++)
1546
+ addFb.run(missing[i].id, maxPriority + i + 1);
1547
+ }
1548
+ });
1549
+ apply();
1550
+ }
1551
+ /**
1552
+ * V19 (June 2026): Google Gemma 4 (released Mar 2026) on the AI Studio free tier.
1553
+ * Both IDs are reachable with the same key as Gemini (live-probed 200). The 26B
1554
+ * is an MoE model (~3.8B active) — Google's real id is `gemma-4-26b-a4b-it`, not
1555
+ * a plain "26B". Tiers match V17's bands (both Large; V17 re-asserts on every
1556
+ * boot). Free limits are now AI-Studio-dashboard-driven and were cut 50-80% in
1557
+ * Dec 2025, so rpd/tpm are conservative. Idempotent (INSERT OR IGNORE + fallback
1558
+ * backfill), safe to re-run.
1559
+ */
1560
+ function migrateModelsV19Gemma4(db) {
1561
+ const insert = db.prepare(`
1562
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
1563
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1564
+ `);
1565
+ const additions = [
1566
+ ['google', 'gemma-4-31b-it', 'Gemma 4 31B IT', 19, 4, 'Large', 15, 1000, 250000, null, '~30M', 32768],
1567
+ ['google', 'gemma-4-26b-a4b-it', 'Gemma 4 26B IT', 20, 4, 'Large', 15, 1000, 250000, null, '~30M', 32768],
1568
+ ];
1569
+ const apply = db.transaction(() => {
1570
+ for (const a of additions)
1571
+ insert.run(...a);
1572
+ backfillFallback(db);
1573
+ });
1574
+ apply();
1575
+ }
1576
+ /**
1577
+ * V20 (June 2026): Kilo Gateway anonymous free models. Live-probed keyless
1578
+ * (no API key, cost:0); Kilo documents anonymous access for `:free` routes,
1579
+ * rate-limited 200 req/hr per IP shared across ALL free models. Per-model rate
1580
+ * limits are left null on purpose — the 200/hr budget is per-IP, not per-model,
1581
+ * so we rely on Kilo's own 429s + gateway failover rather than guessing a split.
1582
+ * Prompts/outputs are logged for training (don't send sensitive data); the 120B
1583
+ * Nemotron is additionally flagged trial-use by Kilo. Tiers follow V17 bands
1584
+ * (V17 re-asserts nemotron-3-super → Large on every boot). Routing also needs a
1585
+ * 'kilo' api_keys sentinel row, added via the keyless Keys-page flow. Idempotent
1586
+ * (INSERT OR IGNORE + fallback backfill), safe to re-run.
1587
+ */
1588
+ function migrateModelsV20KiloFree(db) {
1589
+ const insert = db.prepare(`
1590
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window)
1591
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1592
+ `);
1593
+ const additions = [
1594
+ ['kilo', 'poolside/laguna-m.1:free', 'Poolside Laguna M.1 (Kilo)', 13, 8, 'Large', null, null, null, null, 'free · 200/hr per IP', 262144],
1595
+ ['kilo', 'poolside/laguna-xs.2:free', 'Poolside Laguna XS.2 (Kilo)', 16, 4, 'Medium', null, null, null, null, 'free · 200/hr per IP', 262144],
1596
+ ['kilo', 'nvidia/nemotron-3-super-120b-a12b:free', 'Nemotron 3 Super 120B (Kilo)', 12, 5, 'Large', null, null, null, null, 'free · 200/hr per IP (trial)', 1000000],
1597
+ ['kilo', 'stepfun/step-3.7-flash:free', 'StepFun Step 3.7 Flash (Kilo)', 14, 3, 'Medium', null, null, null, null, 'free · 200/hr per IP', 262144],
1598
+ ];
1599
+ const apply = db.transaction(() => {
1600
+ for (const a of additions)
1601
+ insert.run(...a);
1602
+ backfillFallback(db);
1603
+ });
1604
+ apply();
1605
+ }
1606
+ /**
1607
+ * V21 (June 2026): Remove models confirmed DEAD by live probing (2026-06-03 with
1608
+ * production keys), and re-enable Cerebras zai-glm-4.7 which serves free again.
1609
+ *
1610
+ * Deleted (fallback_config row removed first — foreign_keys=ON forbids deleting a
1611
+ * referenced models row):
1612
+ * - llm7/gpt-oss-20b, llm7/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo,
1613
+ * llm7/ministral-8b-2512 — LLM7 silently serves `mistral-small-3.2` for these
1614
+ * (200 OK but wrong model — a routing footgun).
1615
+ * - llm7/GLM-4.6V-Flash — now pro-gated (402 "upgrade required").
1616
+ * - openrouter/arcee-ai/trinity-large-thinking:free,
1617
+ * openrouter/minimax/minimax-m2.5:free, openrouter/baidu/cobuddy:free —
1618
+ * 404 "no endpoints found" (delisted / moved to paid).
1619
+ *
1620
+ * Re-enabled: cerebras/zai-glm-4.7 (V9 disabled it; live-probed 200 free again).
1621
+ * These ids are re-inserted by their original migrations on each boot, so this
1622
+ * later DELETE is what keeps them out. Idempotent, safe to re-run.
1623
+ */
1624
+ function migrateModelsV21PruneDead(db) {
1625
+ const dead = [
1626
+ ['llm7', 'gpt-oss-20b'],
1627
+ ['llm7', 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo'],
1628
+ ['llm7', 'ministral-8b-2512'],
1629
+ ['llm7', 'GLM-4.6V-Flash'],
1630
+ ['openrouter', 'arcee-ai/trinity-large-thinking:free'],
1631
+ ['openrouter', 'minimax/minimax-m2.5:free'],
1632
+ ['openrouter', 'baidu/cobuddy:free'],
1633
+ ];
1634
+ const apply = db.transaction(() => {
1635
+ const getId = db.prepare('SELECT id FROM models WHERE platform = ? AND model_id = ?');
1636
+ const delFb = db.prepare('DELETE FROM fallback_config WHERE model_db_id = ?');
1637
+ const delModel = db.prepare('DELETE FROM models WHERE id = ?');
1638
+ for (const [platform, modelId] of dead) {
1639
+ const row = getId.get(platform, modelId);
1640
+ if (!row)
1641
+ continue;
1642
+ delFb.run(row.id); // remove the fallback chain entry first (FK)
1643
+ delModel.run(row.id);
1644
+ }
1645
+ // Re-enable Cerebras zai-glm-4.7 (model + its fallback chain entry).
1646
+ db.prepare("UPDATE models SET enabled = 1 WHERE platform = 'cerebras' AND model_id = 'zai-glm-4.7'").run();
1647
+ db.prepare(`
1648
+ UPDATE fallback_config SET enabled = 1
1649
+ WHERE model_db_id = (SELECT id FROM models WHERE platform = 'cerebras' AND model_id = 'zai-glm-4.7')
1650
+ `).run();
1651
+ });
1652
+ apply();
1653
+ }
1654
+ // ── V22: tools-aware routing (2026-06-04) ──
1655
+ // Adds the supports_tools column and flags the models that reliably emit
1656
+ // STRUCTURED tool_calls. Motivation: an OpenAI-compatible agent client
1657
+ // (Paperclip/Codex via /v1/responses) sent tool-bearing requests that the
1658
+ // chain cascaded down to models with no real function-calling support —
1659
+ // nemotron-3-nano answered 72 requests averaging ~38 output tokens, and the
1660
+ // tool call leaked into chat text as literal `</tool_call>` XML, so the agent
1661
+ // harness never saw a status update and every issue dead-ended in manual
1662
+ // recovery. The router now keeps tool-bearing requests on this flagged subset
1663
+ // (see routeRequest's requireTools).
1664
+ //
1665
+ // Rule-based by model family rather than a hardcoded id list, same reasoning
1666
+ // as V16Vision: the catalog churns through migrations and LIKE rules survive
1667
+ // renames. A family is flagged only when (a) it has native, documented
1668
+ // function calling AND (b) we've seen structured tool_calls from it through
1669
+ // this gateway (the 2026-06-01 live tool-calling benchmark, V4's probe pass,
1670
+ // or production traffic). Conservative on purpose: a false negative just
1671
+ // narrows the pool, while a false positive reproduces the silent-garbage
1672
+ // failure above. Deliberately NOT flagged: gemma (weak at tools — V4),
1673
+ // nemotron nano/9b (the incident model), poolside laguna (returns ~2 tokens),
1674
+ // hermes-3 (emits tool calls as text — V4), groq compound (built-in tools
1675
+ // only, rejects user functions), r1-distills, and the small/stealth/unknown
1676
+ // tail (granite, lfm, stepfun, big-pickle, mimo, owl-alpha, cogito,
1677
+ // pollinations). Idempotent — reset-then-set, safe on fresh seeds and
1678
+ // upgrades alike.
1679
+ function migrateModelsV22Tools(db) {
1680
+ const columns = db.prepare('PRAGMA table_info(models)').all();
1681
+ if (!columns.some(col => col.name === 'supports_tools')) {
1682
+ db.prepare('ALTER TABLE models ADD COLUMN supports_tools INTEGER NOT NULL DEFAULT 0').run();
1683
+ }
1684
+ const apply = db.transaction(() => {
1685
+ // Reset first so a de-flagged model doesn't keep a stale flag across re-runs.
1686
+ db.prepare('UPDATE models SET supports_tools = 0').run();
1687
+ db.prepare(`
1688
+ UPDATE models SET supports_tools = 1
1689
+ WHERE (
1690
+ LOWER(model_id) LIKE '%gpt-oss%' -- groq/OR/cerebras/CF/sambanova/ollama; incl. safeguard (tool-tuned)
1691
+ OR ((LOWER(model_id) LIKE '%llama-3%' OR LOWER(model_id) LIKE '%llama-4%')
1692
+ AND LOWER(model_id) NOT LIKE '%hermes%' -- hermes-3-llama emits text tool calls
1693
+ AND LOWER(model_id) NOT LIKE '%llama-3.2%') -- 3B route declares no tool support (V23)
1694
+ OR LOWER(model_id) LIKE '%gemini-%' -- every Gemini; gemma intentionally NOT matched
1695
+ OR LOWER(model_id) LIKE '%glm-%' -- GLM 4.5+/5.x are agentic-tuned (zai/zhipu/CF/ollama)
1696
+ OR LOWER(model_id) LIKE '%qwen3%'
1697
+ OR LOWER(model_id) LIKE '%qwen-3%' -- Qwen3 incl. coder/next variants
1698
+ OR LOWER(model_id) LIKE '%deepseek-v%' -- V3.x/V4 function calling; excludes r1-distill
1699
+ OR LOWER(model_id) LIKE '%kimi-k2%' -- K2 family is tool-native
1700
+ OR LOWER(model_id) LIKE '%minimax-m2%' -- M2.x is agent-focused
1701
+ OR LOWER(model_id) LIKE '%mistral-large%' -- Mistral API function calling (whole family)
1702
+ OR LOWER(model_id) LIKE '%mistral-medium%'
1703
+ OR LOWER(model_id) LIKE '%mistral-small%'
1704
+ OR LOWER(model_id) LIKE '%magistral%'
1705
+ OR LOWER(model_id) LIKE '%codestral%'
1706
+ OR LOWER(model_id) LIKE '%devstral%'
1707
+ OR LOWER(model_id) LIKE '%ministral%'
1708
+ OR LOWER(model_id) LIKE '%command-a%' -- Cohere native tool use (benchmarked top-20)
1709
+ OR LOWER(model_id) LIKE '%command-r%'
1710
+ OR LOWER(model_id) LIKE '%gpt-4o%' -- GitHub's OpenAI models
1711
+ OR LOWER(model_id) LIKE '%gpt-4.1%'
1712
+ OR LOWER(model_id) LIKE '%gpt-5%'
1713
+ OR LOWER(model_id) LIKE '%nemotron-3-super%' -- benchmarked #8 with real tool calls; nano stays excluded
1714
+ OR LOWER(model_id) LIKE '%nemotron-nano-12b-v2-vl%' -- unlike the 30B nano: live-probed structured tool_calls (V23, 2026-06-07)
1715
+ OR LOWER(model_id) LIKE '%nemotron-3-ultra%' -- structured tool_calls live-verified via Zen's dedicated endpoint (V24, 2026-06-07); covers the disabled OR row too
1716
+ OR LOWER(model_id) LIKE '%minimax-m3%' -- finish_reason:tool_calls live-verified on Zen (V24)
1717
+ OR LOWER(model_id) LIKE '%north-mini-code%' -- structured tool_calls + reasoning_content live-verified on Zen (V26, 2026-06-10)
1718
+ )
1719
+ `).run();
1720
+ });
1721
+ apply();
1722
+ }
1723
+ /**
1724
+ * V23 (June 2026): recurring-free audit — drop SambaNova + Chutes, add
1725
+ * live-verified free models (all probed 2026-06-07 with production keys).
1726
+ *
1727
+ * Removed platforms (models, fallback rows, AND key rows; earlier migrations
1728
+ * re-insert the model rows on every boot, so this later DELETE is what keeps
1729
+ * them out — same pattern as V21):
1730
+ * - sambanova: free tier is gone for good. The always-free tier was retired
1731
+ * in early 2025 ("We do not have any current plans to maintain the free
1732
+ * tier in any state" — staff, community.sambanova.ai/t/847) in favor of a
1733
+ * one-time $5 trial credit that expires in 3 months. Once it lapses with
1734
+ * no card on file, every chat call 402s "payment method required" — live
1735
+ * probe confirmed on all 6 models, while /v1/models still 200s (billing
1736
+ * gate, not a key problem). No recurring no-card path remains.
1737
+ * - chutes: never a registered provider — rows were hand-added during the
1738
+ * V11 evaluation. The Early Access free plan (200 req/day) fully retired
1739
+ * 2026-03-15 (chutes.ai/news/community-announcement-february); PAYG and
1740
+ * paid subs only now, so V11's drop verdict is permanent.
1741
+ *
1742
+ * Added:
1743
+ * - openrouter kimi-k2.6:free — chat probed 200; tools flagged via the V22
1744
+ * %kimi-k2% family rule (the tool probe itself only hit upstream 429s —
1745
+ * it's a popular new route — but K2 is tool-native everywhere we run it).
1746
+ * - openrouter nemotron-nano-12b-v2-vl:free — structured tool_calls AND
1747
+ * vision both live-verified; V16/V22 rules added.
1748
+ * - zhipu glm-4.6v-flash — listed "Free" on Z.AI pricing; 200 with our
1749
+ * existing bigmodel.cn key; structured tool_calls + vision verified.
1750
+ * - openrouter nemotron-3-ultra-550b-a55b:free — 1M ctx, currently the
1751
+ * biggest free model anywhere, but generation takes 180s+ even on trivial
1752
+ * prompts (heavily congested), so it's seeded enabled=0. Flip it on when
1753
+ * it serves sanely. (Tools were unverifiable on the OR route while it
1754
+ * hangs; V24 verified the model family's structured tool_calls via Zen's
1755
+ * dedicated endpoint and added the V22 rule, so this seed is now 1.)
1756
+ * - openrouter llama-3.2-3b-instruct:free and
1757
+ * dolphin-mistral-24b-venice-edition:free — both heavily contended
1758
+ * (persistent upstream 429s at probe time) but real routes; no tools.
1759
+ * - NOT added: nvidia/nemotron-3.5-content-safety:free — it's a safety
1760
+ * classifier, not a chat model: probe returned content:null with a "User
1761
+ * Safety: safe/unsafe" verdict in reasoning, which would surface as empty
1762
+ * responses whenever the fallback chain landed on it.
1763
+ *
1764
+ * Idempotent: DELETEs re-run harmlessly; INSERT OR IGNORE means the ultra
1765
+ * row's enabled=0 is seed-only, so a dashboard re-enable sticks across boots.
1766
+ * vision/tools seeds match the V16/V22 rules (which re-assert next boot);
1767
+ * tiers match V17's bands.
1768
+ */
1769
+ function migrateModelsV23FreeTierAudit(db) {
1770
+ const apply = db.transaction(() => {
1771
+ for (const platform of ['sambanova', 'chutes']) {
1772
+ db.prepare(`
1773
+ DELETE FROM fallback_config WHERE model_db_id IN (
1774
+ SELECT id FROM models WHERE platform = ?
1775
+ )
1776
+ `).run(platform);
1777
+ db.prepare('DELETE FROM models WHERE platform = ?').run(platform);
1778
+ db.prepare('DELETE FROM api_keys WHERE platform = ?').run(platform);
1779
+ }
1780
+ const insert = db.prepare(`
1781
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window, enabled, supports_vision, supports_tools)
1782
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1783
+ `);
1784
+ const additions = [
1785
+ ['openrouter', 'moonshotai/kimi-k2.6:free', 'Kimi K2.6 (OR free)', 3, 9, 'Frontier', 20, 200, null, null, '~6M', 262144, 1, 0, 1],
1786
+ ['openrouter', 'nvidia/nemotron-3-ultra-550b-a55b:free', 'Nemotron 3 Ultra 550B (free, slow)', 7, 11, 'Frontier', 20, 200, null, null, '~6M', 1000000, 0, 0, 1],
1787
+ ['openrouter', 'nvidia/nemotron-nano-12b-v2-vl:free', 'Nemotron Nano 12B VL (free)', 26, 9, 'Medium', 20, 200, null, null, '~6M', 128000, 1, 1, 1],
1788
+ ['openrouter', 'meta-llama/llama-3.2-3b-instruct:free', 'Llama 3.2 3B (free)', 30, 9, 'Small', 20, 200, null, null, '~6M', 131072, 1, 0, 0],
1789
+ ['openrouter', 'cognitivecomputations/dolphin-mistral-24b-venice-edition:free', 'Dolphin Mistral 24B Venice (free)', 25, 9, 'Medium', 20, 200, null, null, '~6M', 32768, 1, 0, 0],
1790
+ ['zhipu', 'glm-4.6v-flash', 'GLM-4.6V Flash', 21, 4, 'Large', null, null, null, null, '~30M', 131072, 1, 1, 1],
1791
+ ];
1792
+ for (const a of additions)
1793
+ insert.run(...a);
1794
+ backfillFallback(db);
1795
+ });
1796
+ apply();
1797
+ }
1798
+ /**
1799
+ * V24 (June 2026): OpenCode Zen roster refresh + NIM gemma pause. All claims
1800
+ * live-probed 2026-06-07 with a real Zen account key through the gateway.
1801
+ *
1802
+ * Zen's promo roster rotated since V18:
1803
+ * - ADDED nemotron-3-ultra-free — newly docs-confirmed free, and unlike the
1804
+ * OpenRouter ultra route (which hangs: 0 tokens in 140s even streaming),
1805
+ * Zen serves it from a dedicated vLLM endpoint in ~2s WITH structured
1806
+ * tool_calls. Currently the biggest usable free model anywhere.
1807
+ * - ADDED minimax-m3-free — requested in #242 (thanks @Naster17). Live
1808
+ * probe: 2s chat, finish_reason:tool_calls. CAVEAT: not in Zen's docs
1809
+ * free table (same undocumented status qwen3.6-plus-free had before its
1810
+ * promo ended), so it may transition without notice — watchlist.
1811
+ * - qwen3.6-plus-free now errors "Free promotion has ended" — confirms the
1812
+ * docs-confirmed-only bar; never added, nothing to remove.
1813
+ * - nemotron-3-super-free DROPPED OUT of Zen's docs free table but still
1814
+ * serves (routes to the same congested NVIDIA :free upstream as
1815
+ * OpenRouter, ~15s). Kept enabled; prune if it starts billing or 402s.
1816
+ * - big-pickle unmasked: responses report model "deepseek-v4-flash".
1817
+ *
1818
+ * Also pauses nvidia/google/gemma-4-31b-it: still listed on NIM's /v1/models
1819
+ * (only gemma-4 there) but every chat probe hangs 90s+ even on tiny prompts —
1820
+ * NIM free-tier serverless capacity starvation (forum 504 reports, May–Jun
1821
+ * 2026) compounded by a known gemma-4-31b dense Flash-Attention prefill
1822
+ * deadlock upstream. Disabled like the V13 disables (re-asserted each boot);
1823
+ * re-enable via a future migration when NIM serves it again. Gemma-4 coverage
1824
+ * remains via google/openrouter/cloudflare rows.
1825
+ *
1826
+ * Idempotent: INSERT OR IGNORE + always-run UPDATEs, safe to re-run.
1827
+ */
1828
+ function migrateModelsV24ZenRefresh(db) {
1829
+ const apply = db.transaction(() => {
1830
+ const insert = db.prepare(`
1831
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window, enabled, supports_vision, supports_tools)
1832
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1833
+ `);
1834
+ const additions = [
1835
+ ['opencode', 'nemotron-3-ultra-free', 'Nemotron 3 Ultra Free (OpenCode Zen)', 7, 4, 'Frontier', 20, 200, null, null, 'promo (trial)', 131072, 1, 0, 1],
1836
+ ['opencode', 'minimax-m3-free', 'MiniMax M3 Free (OpenCode Zen)', 4, 4, 'Frontier', 20, 200, null, null, 'promo (trial)', 131072, 1, 0, 1],
1837
+ ];
1838
+ for (const a of additions)
1839
+ insert.run(...a);
1840
+ backfillFallback(db);
1841
+ db.prepare(`
1842
+ UPDATE models SET enabled = 0
1843
+ WHERE platform = 'nvidia' AND model_id = 'google/gemma-4-31b-it'
1844
+ `).run();
1845
+ });
1846
+ apply();
1847
+ }
1848
+ /**
1849
+ * V25 (2026-06-09): retire two OpenCode Zen free models that went dead.
1850
+ * Observed on the production proxy — together they accounted for ~55% of a
1851
+ * day's error rows, each 401ing on every route attempt (a 401 is non-retryable,
1852
+ * so when one was picked first it hard-failed the request, not just logged):
1853
+ * - nemotron-3-super-free → `401: Model nemotron-3-super-free is not supported`.
1854
+ * V24 kept it enabled with "prune if it starts billing or 402s"; Zen has now
1855
+ * removed it outright.
1856
+ * - minimax-m3-free → `401: Free promotion has ended for MiniMax M3 Free`.
1857
+ * Added in V24 while its promo was live; the promo has since lapsed (same
1858
+ * fate qwen3.6-plus-free hit). These are server-side, account-independent
1859
+ * verdicts — dead for everyone, not a per-key issue.
1860
+ * Disabled, not removed (row kept so fallback history survives and a future
1861
+ * promo can re-enable). Idempotent: re-asserted each boot like the V13/V24
1862
+ * disables.
1863
+ */
1864
+ function migrateModelsV25ZenDeadPromos(db) {
1865
+ const disable = db.prepare(`UPDATE models SET enabled = 0 WHERE platform = ? AND model_id = ?`);
1866
+ const disables = [
1867
+ ['opencode', 'nemotron-3-super-free'],
1868
+ ['opencode', 'minimax-m3-free'],
1869
+ ];
1870
+ const apply = db.transaction(() => {
1871
+ for (const [p, m] of disables)
1872
+ disable.run(p, m);
1873
+ });
1874
+ apply();
1875
+ }
1876
+ // V26 NOTE (June 2026, recurring-free audit pass 2): the model-data changes
1877
+ // from this audit (4 OVH rows, opencode/north-mini-code-free, Cerebras
1878
+ // 5 RPM/30K TPM/1M TPD limits, NVIDIA "free · 40 RPM" labels, LLM7 60-100/hr
1879
+ // label) were briefly shipped as a data migration and then MOVED to the
1880
+ // published catalog — catalog data is distributed via catalog-sync, never
1881
+ // via migrations, so the premium tier gate holds. What remains in code from that audit: the 'ovh'
1882
+ // keyless provider, Pollinations keyless, the V22 north-mini-code tools rule,
1883
+ // and the corrected quirk seeds in migrateQuirksV1 (incl. the
1884
+ // 'nvidia-credits-based' → 'nvidia-rate-limited' replacement; the stale slug
1885
+ // is cleaned up below in migrateQuirksV1 itself).
1886
+ /**
1887
+ * V26 (2026-09): seed the opencode-free platform — keyless, identity-spoofed
1888
+ * access to OpenCode's zen free tier. Since 2026-09 the free roster is gated
1889
+ * on OpenCode CLI headers; the opencode-free provider forges them so the proxy
1890
+ * can serve free models without requiring users to hold an OpenCode account key.
1891
+ *
1892
+ * Models: the roster mirrors the V18/V24 OpenCode Zen seeds (same upstream
1893
+ * model IDs), but on the new 'opencode-free' platform. Limits are conservative
1894
+ * (20 RPM / 200 RPD — same as the original Zen seeds); the upstream has not
1895
+ * published official limits for the CLI-bypass path. Idempotent (INSERT OR IGNORE).
1896
+ */
1897
+ function migrateModelsV26OpenCodeFree(db) {
1898
+ const insert = db.prepare(`
1899
+ INSERT OR IGNORE INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window, enabled, supports_vision, supports_tools)
1900
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1901
+ `);
1902
+ const additions = [
1903
+ ['opencode-free', 'big-pickle', 'Big Pickle (OpenCode Free)', 10, 4, 'Large', 20, 200, null, null, 'promo (free)', 131072, 1, 0, 1],
1904
+ ['opencode-free', 'mimo-v2.5-free', 'MiMo-V2.5 Free (OpenCode Free)', 14, 4, 'Medium', 20, 200, null, null, 'promo (free)', 131072, 1, 0, 1],
1905
+ ['opencode-free', 'deepseek-v4-flash-free', 'DeepSeek V4 Flash Free (OpenCode Free)', 4, 4, 'Frontier', 20, 200, null, null, 'promo (free)', 131072, 1, 0, 1],
1906
+ ];
1907
+ const apply = db.transaction(() => {
1908
+ for (const a of additions)
1909
+ insert.run(...a);
1910
+ backfillFallback(db);
1911
+ });
1912
+ apply();
1913
+ }
1914
+ // Embeddings V1 (2026-06): per-family embedding catalog. A "family" is one
1915
+ // model identity + dimension — vectors from different families live in
1916
+ // incompatible spaces, so /v1/embeddings only ever fails over WITHIN a family
1917
+ // (same model served by another provider), never across families.
1918
+ // Every entry was live-verified against the provider on 2026-06-04.
1919
+ function migrateEmbeddingsV1(db) {
1920
+ db.exec(`
1921
+ CREATE TABLE IF NOT EXISTS embedding_models (
1922
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1923
+ family TEXT NOT NULL,
1924
+ platform TEXT NOT NULL,
1925
+ model_id TEXT NOT NULL,
1926
+ display_name TEXT NOT NULL,
1927
+ dimensions INTEGER NOT NULL,
1928
+ max_input_tokens INTEGER,
1929
+ priority INTEGER NOT NULL DEFAULT 0,
1930
+ enabled INTEGER NOT NULL DEFAULT 1,
1931
+ quota_label TEXT NOT NULL DEFAULT '',
1932
+ UNIQUE(platform, model_id)
1933
+ );
1934
+ `);
1935
+ // Tag request rows so embeddings traffic doesn't pollute the chat token
1936
+ // budget / headroom math. Existing rows backfill to 'chat' via the default.
1937
+ const columns = db.prepare('PRAGMA table_info(requests)').all();
1938
+ if (!columns.some(col => col.name === 'request_type')) {
1939
+ db.prepare("ALTER TABLE requests ADD COLUMN request_type TEXT NOT NULL DEFAULT 'chat'").run();
1940
+ }
1941
+ const seed = db.prepare(`
1942
+ INSERT OR IGNORE INTO embedding_models
1943
+ (family, platform, model_id, display_name, dimensions, max_input_tokens, priority, enabled, quota_label)
1944
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1945
+ `);
1946
+ const rows = [
1947
+ // family, platform, provider model id, display name, dims, max input, priority, enabled, quota label
1948
+ ['gemini-embedding-001', 'google', 'gemini-embedding-001', 'Gemini Embedding', 3072, 2048, 1, 1, '100 rpm · 1K req/day'],
1949
+ ['llama-nemotron-embed-vl-1b-v2', 'nvidia', 'nvidia/llama-nemotron-embed-vl-1b-v2', 'Nemotron Embed VL 1B', 2048, 8192, 1, 1, '~40 rpm'],
1950
+ ['llama-nemotron-embed-vl-1b-v2', 'openrouter', 'nvidia/llama-nemotron-embed-vl-1b-v2', 'Nemotron Embed VL 1B (OR)', 2048, 8192, 2, 1, '$0/M tok'],
1951
+ ['llama-nemotron-embed-1b-v2', 'nvidia', 'nvidia/llama-nemotron-embed-1b-v2', 'Nemotron Embed 1B', 2048, 8192, 1, 1, '~40 rpm'],
1952
+ ['nv-embedqa-e5-v5', 'nvidia', 'nvidia/nv-embedqa-e5-v5', 'NV-EmbedQA E5 v5', 1024, 512, 1, 1, '~40 rpm'],
1953
+ ['text-embedding-3-small', 'github', 'openai/text-embedding-3-small', 'Text Embedding 3 Small', 1536, 8191, 1, 1, 'rate-limited free'],
1954
+ ['text-embedding-3-large', 'github', 'openai/text-embedding-3-large', 'Text Embedding 3 Large', 3072, 8191, 1, 1, 'rate-limited free'],
1955
+ ['bge-m3', 'cloudflare', '@cf/baai/bge-m3', 'BGE-M3', 1024, 8192, 1, 1, '10K neurons/day (shared)'],
1956
+ ['bge-m3', 'huggingface', 'BAAI/bge-m3', 'BGE-M3 (HF)', 1024, 8192, 2, 1, '$0.10/mo credits'],
1957
+ ['embeddinggemma-300m', 'cloudflare', '@cf/google/embeddinggemma-300m', 'EmbeddingGemma 300M', 768, 2048, 1, 1, '10K neurons/day (shared)'],
1958
+ ['qwen3-embedding-0.6b', 'cloudflare', '@cf/qwen/qwen3-embedding-0.6b', 'Qwen3 Embedding 0.6B', 1024, 4096, 1, 1, '10K neurons/day (shared)'],
1959
+ // Cohere trial keys allow 1,000 calls/month TOTAL shared with chat —
1960
+ // disabled by default so embedding traffic can't silently eat chat quota.
1961
+ ['embed-v4.0', 'cohere', 'embed-v4.0', 'Cohere Embed v4', 1536, 128000, 1, 0, '1K calls/mo (shared w/ chat)'],
1962
+ ];
1963
+ const apply = db.transaction(() => { for (const r of rows)
1964
+ seed.run(...r); });
1965
+ apply();
1966
+ const def = db.prepare("SELECT value FROM settings WHERE key = 'embeddings_default_family'").get();
1967
+ if (!def) {
1968
+ db.prepare("INSERT INTO settings (key, value) VALUES ('embeddings_default_family', 'gemini-embedding-001')").run();
1969
+ }
1970
+ }
1971
+ /**
1972
+ * Media (image + audio/TTS) models V1 (June 2026): SCHEMA ONLY.
1973
+ *
1974
+ * Generative-media models live in their OWN table — exactly like embeddings —
1975
+ * so they never enter the chat router's candidate pool (a chat request can't
1976
+ * misroute to an image model) and never pollute the chat token budget. This is
1977
+ * schema only: per the no-model-data-in-migrations rule (see migrateDbSchema),
1978
+ * the rows are maintained in the published catalog and arrive via catalog-sync
1979
+ * (premium on the live tier within ~12h, free once each model is 30 days old). `modality`
1980
+ * is 'image' | 'audio'; `quota_label` mirrors the catalog's display note. The
1981
+ * request_type column (added by migrateEmbeddingsV1) tags media traffic 'image'
1982
+ * / 'audio' so it stays out of the chat budget math.
1983
+ */
1984
+ function migrateMediaV1(db) {
1985
+ db.exec(`
1986
+ CREATE TABLE IF NOT EXISTS media_models (
1987
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1988
+ platform TEXT NOT NULL,
1989
+ model_id TEXT NOT NULL,
1990
+ display_name TEXT NOT NULL,
1991
+ modality TEXT NOT NULL,
1992
+ priority INTEGER NOT NULL DEFAULT 0,
1993
+ enabled INTEGER NOT NULL DEFAULT 1,
1994
+ quota_label TEXT NOT NULL DEFAULT '',
1995
+ UNIQUE(platform, model_id)
1996
+ );
1997
+ `);
1998
+ }
1999
+ /**
2000
+ * Quirks V1 (June 2026): promote the catalog's free-form "quirks" out of code
2001
+ * comments into structured, reusable data so the catalog server can ship them.
2002
+ *
2003
+ * A quirk is defined ONCE (slug, title, body, severity) and applied to models
2004
+ * by SELECTOR PARAMETERS rather than hand-attached per model — because one
2005
+ * quirk routinely covers many models (a whole platform's keyless access, an
2006
+ * entire model family that hangs upstream, etc.). Each row in quirk_targets is
2007
+ * one selector; a NULL field is a wildcard:
2008
+ *
2009
+ * platform NULL, model_glob NULL -> every model (global note)
2010
+ * platform set, model_glob NULL -> every model on that platform
2011
+ * platform set, model_glob set -> that platform's models whose id GLOBs
2012
+ * platform NULL, model_glob set -> any model whose id GLOBs (cross-platform)
2013
+ *
2014
+ * Resolution (see services/quirks.ts) is therefore:
2015
+ * (platform IS NULL OR platform = m.platform)
2016
+ * AND (model_glob IS NULL OR m.model_id GLOB model_glob)
2017
+ *
2018
+ * GLOB (not LIKE) so the selectors read like the model-family rules elsewhere
2019
+ * in this file ('*nemotron-3-ultra*'). Severity drives client display:
2020
+ * 'info' (neutral note), 'warning' (works but caveated), 'blocker' (knowingly
2021
+ * unusable / disabled). Idempotent: tables are IF NOT EXISTS and the seed is a
2022
+ * reset-then-insert of the curated set, so editing a seeded quirk in code wins
2023
+ * on next boot while operator-added quirks (new slugs) are left untouched.
2024
+ */
2025
+ function migrateQuirksV1(db) {
2026
+ db.exec(`
2027
+ CREATE TABLE IF NOT EXISTS quirks (
2028
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
2029
+ slug TEXT NOT NULL UNIQUE,
2030
+ title TEXT NOT NULL,
2031
+ body TEXT NOT NULL DEFAULT '',
2032
+ severity TEXT NOT NULL DEFAULT 'info',
2033
+ created_at_ms INTEGER NOT NULL,
2034
+ updated_at_ms INTEGER NOT NULL
2035
+ );
2036
+ CREATE TABLE IF NOT EXISTS quirk_targets (
2037
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
2038
+ quirk_id INTEGER NOT NULL REFERENCES quirks(id) ON DELETE CASCADE,
2039
+ platform TEXT,
2040
+ model_glob TEXT
2041
+ );
2042
+ CREATE INDEX IF NOT EXISTS idx_quirk_targets_quirk ON quirk_targets(quirk_id);
2043
+ `);
2044
+ // Superseded curated slugs: removed from the seed below, so the upsert no
2045
+ // longer refreshes them — but the old rows would linger as (apparently)
2046
+ // operator-added quirks. Drop them explicitly; targets cascade.
2047
+ // 'nvidia-credits-based' → replaced by 'nvidia-rate-limited' (June 2026:
2048
+ // NVIDIA dropped the credit system for per-account rate limits).
2049
+ db.prepare("DELETE FROM quirks WHERE slug = 'nvidia-credits-based'").run();
2050
+ const seeds = [
2051
+ {
2052
+ slug: 'keyless-anonymous',
2053
+ title: 'No API key required',
2054
+ body: 'Routes anonymously — the catalog ships a keyless sentinel row and calls work with no account or key.',
2055
+ severity: 'info',
2056
+ targets: [{ platform: 'kilo' }, { platform: 'llm7' }, { platform: 'ovh' }],
2057
+ },
2058
+ {
2059
+ slug: 'ovh-anon-trickle',
2060
+ title: 'Anonymous tier is 2 req/min',
2061
+ body: 'OVH AI Endpoints anonymous mode is documented at 2 req/min per IP per model (observed even stricter across models). The 400 req/min authenticated tier requires a Public Cloud project with a payment method, so the catalog ships the keyless path. Treat as a breadth/fallback tier, not a throughput tier.',
2062
+ severity: 'warning',
2063
+ targets: [{ platform: 'ovh' }],
2064
+ },
2065
+ {
2066
+ slug: 'pollinations-degraded',
2067
+ title: 'Publishable key uses recurring shared capacity',
2068
+ body: 'Pollinations chat uses https://gen.pollinations.ai/v1 with a free publishable API key. Shared free capacity currently accrues at one pollen per IP per hour; the legacy text host is no longer used.',
2069
+ severity: 'warning',
2070
+ targets: [{ platform: 'pollinations' }],
2071
+ },
2072
+ {
2073
+ slug: 'or-free-cap-account-wide',
2074
+ title: 'Daily :free cap is account-wide',
2075
+ body: 'OpenRouter’s :free daily cap (50/day, or 1000/day once you have ever bought $10 of credits) is shared across ALL :free models on the account, not per model. Per-row rpd values here are therefore optimistic; the router’s cooldown handling absorbs the shared 429s.',
2076
+ severity: 'info',
2077
+ targets: [{ platform: 'openrouter', modelGlob: '*:free' }],
2078
+ },
2079
+ {
2080
+ slug: 'zen-promo-roster',
2081
+ title: 'Limited-time promo, roster rotates',
2082
+ body: 'OpenCode Zen free models are explicitly limited-time promotional access ("available for a limited time" per the docs), not a recurring quota. The roster rotates: qwen3.6-plus and minimax-m3 promos already ended. Expect any row here to die without notice; prompts/outputs may be used for model improvement.',
2083
+ severity: 'warning',
2084
+ targets: [{ platform: 'opencode' }],
2085
+ },
2086
+ {
2087
+ slug: 'cloudflare-key-format',
2088
+ title: 'Key is account_id:token',
2089
+ body: 'Cloudflare Workers AI authenticates with a combined credential in the form "account_id:token", not a bare token.',
2090
+ severity: 'info',
2091
+ targets: [{ platform: 'cloudflare' }],
2092
+ },
2093
+ {
2094
+ // Replaces 'nvidia-credits-based' (deleted in V26): NVIDIA staff
2095
+ // confirmed on the dev forums (~Sept 2025) that the credit system was
2096
+ // discontinued in favor of per-account rate limits, so NIM IS
2097
+ // recurring-free again. The ToS caution survives the correction.
2098
+ slug: 'nvidia-rate-limited',
2099
+ title: 'Recurring free, 40 RPM, eval-only ToS',
2100
+ body: 'NVIDIA NIM replaced its depleting trial credits with a recurring per-account rate limit (40 RPM default, varies by model), verified June 2026. The trial ToS still scopes usage to evaluation/prototyping, not production.',
2101
+ severity: 'info',
2102
+ targets: [{ platform: 'nvidia' }],
2103
+ },
2104
+ {
2105
+ slug: 'nim-gemma-hung',
2106
+ title: 'NIM gemma route hangs',
2107
+ body: 'The NVIDIA NIM gemma endpoint is listed but hangs (capacity starvation plus an upstream FlashAttention bug). Paused; probe with a 120s timeout before re-enabling.',
2108
+ severity: 'blocker',
2109
+ targets: [{ platform: 'nvidia', modelGlob: '*gemma*' }],
2110
+ },
2111
+ {
2112
+ slug: 'or-ultra-hangs',
2113
+ title: 'OpenRouter ultra route hangs',
2114
+ body: 'nemotron-3-ultra (550B) on OpenRouter takes 180s+ even on trivial prompts (heavily congested), so its OR row is seeded disabled. Use the OpenCode Zen route instead.',
2115
+ severity: 'warning',
2116
+ targets: [{ platform: 'openrouter', modelGlob: '*nemotron-3-ultra*' }],
2117
+ },
2118
+ {
2119
+ slug: 'zen-serves-ultra-fast',
2120
+ title: 'Zen serves the 550B fast',
2121
+ body: 'OpenCode Zen serves nemotron-3-ultra in ~2s with working tool calls where the OpenRouter route hangs — the live-verified path for this model.',
2122
+ severity: 'info',
2123
+ targets: [{ platform: 'opencode', modelGlob: '*nemotron-3-ultra*' }],
2124
+ },
2125
+ {
2126
+ slug: 'zhipu-shared-key',
2127
+ title: 'Works with existing Zhipu key',
2128
+ body: 'glm-4.6v-flash is listed Free on Z.AI and answers 200 with the existing bigmodel.cn key; vision and structured tool calls both live-verified.',
2129
+ severity: 'info',
2130
+ targets: [{ platform: 'zhipu', modelGlob: '*glm-4.6v*' }],
2131
+ },
2132
+ ];
2133
+ const now = Date.now();
2134
+ const upsertQuirk = db.prepare(`
2135
+ INSERT INTO quirks (slug, title, body, severity, created_at_ms, updated_at_ms)
2136
+ VALUES (@slug, @title, @body, @severity, @now, @now)
2137
+ ON CONFLICT(slug) DO UPDATE SET
2138
+ title = excluded.title,
2139
+ body = excluded.body,
2140
+ severity = excluded.severity,
2141
+ updated_at_ms = excluded.updated_at_ms
2142
+ `);
2143
+ const getId = db.prepare('SELECT id FROM quirks WHERE slug = ?');
2144
+ const clearTargets = db.prepare('DELETE FROM quirk_targets WHERE quirk_id = ?');
2145
+ const addTarget = db.prepare('INSERT INTO quirk_targets (quirk_id, platform, model_glob) VALUES (?, ?, ?)');
2146
+ const apply = db.transaction(() => {
2147
+ for (const s of seeds) {
2148
+ upsertQuirk.run({ slug: s.slug, title: s.title, body: s.body, severity: s.severity, now });
2149
+ const { id } = getId.get(s.slug);
2150
+ // Reset the curated quirk's selectors so edits in code take effect, but
2151
+ // leave quirks/targets with unknown slugs (operator-added) alone.
2152
+ clearTargets.run(id);
2153
+ for (const t of s.targets)
2154
+ addTarget.run(id, t.platform ?? null, t.modelGlob ?? null);
2155
+ }
2156
+ });
2157
+ apply();
2158
+ }
2159
+ /** Append any models not yet in the fallback chain, lowest priority, ordered by
2160
+ * intelligence_rank. Shared by the recent model migrations (V18–V20). */
2161
+ function backfillFallback(db) {
2162
+ const missing = db.prepare(`
2163
+ SELECT m.id FROM models m
2164
+ LEFT JOIN fallback_config f ON m.id = f.model_db_id
2165
+ WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC
2166
+ `).all();
2167
+ if (missing.length > 0) {
2168
+ const maxPriority = db.prepare('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config').get().mx;
2169
+ const addFb = db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)');
2170
+ for (let i = 0; i < missing.length; i++)
2171
+ addFb.run(missing[i].id, maxPriority + i + 1);
2172
+ }
2173
+ }
2174
+ function ensureUnifiedKey(db) {
2175
+ const existing = db.prepare("SELECT value FROM settings WHERE key = 'unified_api_key'").get();
2176
+ if (!existing) {
2177
+ const key = `sr-${crypto.randomBytes(24).toString('hex')}`;
2178
+ db.prepare("INSERT INTO settings (key, value) VALUES ('unified_api_key', ?)").run(key);
2179
+ // A direct stdout write bypasses the process-wide console redaction, so it
2180
+ // is only safe for a deliberate local-development session with an attached
2181
+ // terminal. Production, CI, and container startup must never put this
2182
+ // credential in a log stream; the dashboard is the safe retrieval path.
2183
+ if (isInteractiveLocalDevelopment()) {
2184
+ process.stdout.write(`\n Your unified API key: ${key}\n\n`);
2185
+ }
2186
+ else {
2187
+ console.log('\n A unified API key was generated. Open the dashboard and retrieve it from the Keys page.\n' +
2188
+ ' The key is intentionally not printed to logs.\n');
2189
+ }
2190
+ }
2191
+ }
2192
+ function isInteractiveLocalDevelopment() {
2193
+ const nodeEnv = process.env.NODE_ENV?.trim().toLowerCase() || 'development';
2194
+ return nodeEnv === 'development'
2195
+ && process.stdout.isTTY === true
2196
+ && !process.env.CI
2197
+ && !process.env.KUBERNETES_SERVICE_HOST;
2198
+ }
2199
+ /**
2200
+ * Migration helper to ensure Default profile exists, legacy profiles converted,
2201
+ * and fallback_config synced.
2202
+ */
2203
+ function migrateProfilesInit(db) {
2204
+ // 1. Convert any legacy built-in profiles to custom profiles (type = 'custom')
2205
+ db.prepare(`
2206
+ UPDATE profiles
2207
+ SET type = 'custom'
2208
+ WHERE type = 'builtin'
2209
+ `).run();
2210
+ // 2. Ensure Default profile exists
2211
+ const hasDefault = db.prepare("SELECT COUNT(*) as cnt FROM profiles WHERE type = 'default'").get();
2212
+ if (hasDefault.cnt === 0) {
2213
+ const minOrder = db.prepare('SELECT COALESCE(MIN(sort_order), 0) AS mn FROM profiles').get().mn;
2214
+ const targetOrder = Math.min(-1, minOrder - 1);
2215
+ const result = db.prepare("INSERT INTO profiles (name, emoji, color, type, sort_order) VALUES ('Default', '⚙️', '#6366f1', 'default', ?)").run(targetOrder);
2216
+ const profileId = result.lastInsertRowid;
2217
+ // Seed profile models from fallback_config
2218
+ db.prepare(`
2219
+ INSERT INTO profile_models (profile_id, model_db_id, priority, enabled)
2220
+ SELECT ?, model_db_id, priority, enabled
2221
+ FROM fallback_config
2222
+ ORDER BY priority ASC
2223
+ `).run(profileId);
2224
+ // Make it the active profile if none is set
2225
+ db.prepare(`
2226
+ INSERT INTO settings (key, value) VALUES ('active_profile_id', ?)
2227
+ ON CONFLICT(key) DO NOTHING
2228
+ `).run(String(profileId));
2229
+ console.log('Created Default profile');
2230
+ }
2231
+ else {
2232
+ // If it exists, ensure its emoji is '⚙️'
2233
+ db.prepare(`
2234
+ UPDATE profiles
2235
+ SET emoji = '⚙️'
2236
+ WHERE type = 'default' AND emoji != '⚙️'
2237
+ `).run();
2238
+ }
2239
+ }
2240
+ //# sourceMappingURL=20260101_000000_legacy_baseline.js.map