@qvac/inference 0.18.1 → 0.19.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 (388) hide show
  1. package/NOTICE +11 -2
  2. package/README.md +1 -5
  3. package/dist/api/assess-model-fit.d.ts +16 -0
  4. package/dist/api/assess-model-fit.js +27 -0
  5. package/dist/api/audio-gen.d.ts +4 -4
  6. package/dist/api/audio-gen.js +17 -6
  7. package/dist/api/completion-stream.d.ts +1 -1
  8. package/dist/api/completion-stream.js +1 -1
  9. package/dist/api/download-asset.d.ts +1 -1
  10. package/dist/api/download-asset.js +2 -2
  11. package/dist/api/finetune.js +1 -1
  12. package/dist/api/get-loaded-model-info.d.ts +6 -10
  13. package/dist/api/get-loaded-model-info.js +6 -10
  14. package/dist/api/heartbeat.d.ts +5 -22
  15. package/dist/api/heartbeat.js +6 -23
  16. package/dist/api/index.d.ts +36 -35
  17. package/dist/api/index.js +35 -35
  18. package/dist/api/load-model.d.ts +5 -1
  19. package/dist/api/load-model.js +3 -5
  20. package/dist/api/logging-stream-registry.js +1 -1
  21. package/dist/api/resume.d.ts +0 -3
  22. package/dist/api/resume.js +0 -3
  23. package/dist/api/subscribe-logs.js +1 -1
  24. package/dist/api/suspend.d.ts +0 -4
  25. package/dist/api/suspend.js +0 -4
  26. package/dist/api/translate.d.ts +3 -1
  27. package/dist/api/translate.js +16 -8
  28. package/dist/api/unload-model.js +3 -5
  29. package/dist/api/video-request.d.ts +207 -0
  30. package/dist/api/video-request.js +18 -0
  31. package/dist/api/video.d.ts +22 -2
  32. package/dist/api/video.js +25 -15
  33. package/dist/api/vla-helpers.d.ts +1 -2
  34. package/dist/api/vla-helpers.js +1 -2
  35. package/dist/api/vla.js +1 -1
  36. package/dist/api/world-result.d.ts +71 -0
  37. package/dist/api/world-result.js +293 -0
  38. package/dist/api/world.d.ts +143 -0
  39. package/dist/api/world.js +71 -0
  40. package/dist/config/resolve-config.d.ts +1 -1
  41. package/dist/config/resolve-config.js +1 -1
  42. package/dist/constants/index.d.ts +2 -2
  43. package/dist/constants/index.js +2 -2
  44. package/dist/dispatch.js +15 -28
  45. package/dist/errors/index.d.ts +35 -31
  46. package/dist/errors/index.js +66 -56
  47. package/dist/handlers/assess-model-fit.d.ts +10 -0
  48. package/dist/handlers/assess-model-fit.js +75 -0
  49. package/dist/handlers/cancelHandler.js +1 -1
  50. package/dist/handlers/completion-orchestrate.js +1 -1
  51. package/dist/handlers/download-asset.js +6 -5
  52. package/dist/handlers/finetune.js +1 -1
  53. package/dist/handlers/get-loaded-model-info.js +0 -12
  54. package/dist/handlers/get-model-info.js +1 -1
  55. package/dist/handlers/get-system-resources.js +8 -0
  56. package/dist/handlers/load-model/download-manager.d.ts +1 -1
  57. package/dist/handlers/load-model/download-stats.d.ts +3 -3
  58. package/dist/handlers/load-model/download-stats.js +5 -5
  59. package/dist/handlers/load-model/handler.js +6 -8
  60. package/dist/handlers/load-model/http-verify.d.ts +20 -0
  61. package/dist/handlers/load-model/http-verify.js +54 -0
  62. package/dist/handlers/load-model/http.d.ts +12 -2
  63. package/dist/handlers/load-model/http.js +119 -230
  64. package/dist/handlers/load-model/hyperdrive.d.ts +1 -1
  65. package/dist/handlers/load-model/hyperdrive.js +1 -1
  66. package/dist/handlers/load-model/index.d.ts +2 -2
  67. package/dist/handlers/load-model/index.js +2 -2
  68. package/dist/handlers/load-model/registry-companion-set.d.ts +1 -1
  69. package/dist/handlers/load-model/registry-companion-set.js +1 -1
  70. package/dist/handlers/load-model/registry-download-utils.d.ts +3 -3
  71. package/dist/handlers/load-model/registry-download-utils.js +2 -2
  72. package/dist/handlers/load-model/registry.d.ts +2 -2
  73. package/dist/handlers/load-model/registry.js +4 -4
  74. package/dist/handlers/load-model/resolve-session.d.ts +8 -1
  75. package/dist/handlers/load-model/resolve-session.js +12 -9
  76. package/dist/handlers/load-model/resolve.d.ts +4 -4
  77. package/dist/handlers/load-model/resolve.js +18 -16
  78. package/dist/handlers/load-model/safe-fetch.d.ts +44 -0
  79. package/dist/handlers/load-model/safe-fetch.js +143 -0
  80. package/dist/handlers/load-model/types.d.ts +9 -0
  81. package/dist/handlers/plugin-dispatch.js +1 -4
  82. package/dist/handlers/plugin-invoke.js +1 -4
  83. package/dist/handlers/types.d.ts +0 -2
  84. package/dist/handlers/unload-model.js +1 -4
  85. package/dist/index.d.ts +1 -1
  86. package/dist/index.js +1 -1
  87. package/dist/logging/addon.d.ts +1 -1
  88. package/dist/logging/addon.js +1 -1
  89. package/dist/logging/index.d.ts +5 -5
  90. package/dist/logging/index.js +4 -4
  91. package/dist/logging/logger.d.ts +1 -1
  92. package/dist/logging/logger.js +2 -2
  93. package/dist/models/registry/index.d.ts +1 -1
  94. package/dist/models/registry/index.js +2 -2
  95. package/dist/models/registry/models.d.ts +250 -0
  96. package/dist/models/registry/models.js +2071 -1806
  97. package/dist/models/registry/resource-profiles.d.ts +18 -0
  98. package/dist/models/registry/resource-profiles.js +4934 -0
  99. package/dist/plugins/builtin/audiogen-ggml/config.d.ts +14 -0
  100. package/dist/plugins/builtin/audiogen-ggml/config.js +23 -0
  101. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.js +47 -5
  102. package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +134 -4
  103. package/dist/plugins/builtin/audiogen-ggml/plugin.js +47 -28
  104. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +1 -1
  105. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +3 -3
  106. package/dist/plugins/builtin/ggml-classification/plugin.js +1 -1
  107. package/dist/plugins/builtin/ggml-ocr/plugin.js +2 -2
  108. package/dist/plugins/builtin/ggml-vla/plugin.d.ts +1 -1
  109. package/dist/plugins/builtin/ggml-vla/plugin.js +3 -3
  110. package/dist/plugins/builtin/index.d.ts +12 -12
  111. package/dist/plugins/builtin/index.js +12 -12
  112. package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +2 -2
  113. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +37 -26
  114. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.d.ts +12 -11
  115. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.js +133 -41
  116. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.d.ts +7 -1
  117. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.js +26 -4
  118. package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +19 -8
  119. package/dist/plugins/builtin/llamacpp-completion/plugin.js +10 -13
  120. package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +4 -0
  121. package/dist/plugins/builtin/llamacpp-embedding/plugin.js +13 -2
  122. package/dist/plugins/builtin/nmtcpp-translation/plugin.js +2 -2
  123. package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +2 -2
  124. package/dist/plugins/builtin/parakeet-transcription/plugin.js +3 -3
  125. package/dist/plugins/builtin/sdcpp-generation/ops/upscale.js +1 -1
  126. package/dist/plugins/builtin/sdcpp-generation/ops/video.js +32 -2
  127. package/dist/plugins/builtin/sdcpp-generation/ops/world.d.ts +126 -0
  128. package/dist/plugins/builtin/sdcpp-generation/ops/world.js +820 -0
  129. package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +153 -4
  130. package/dist/plugins/builtin/sdcpp-generation/plugin.js +134 -13
  131. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.js +1 -1
  132. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +1 -1
  133. package/dist/plugins/builtin/tts-ggml/plugin.js +2 -2
  134. package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +2 -2
  135. package/dist/plugins/builtin/whispercpp-transcription/plugin.js +3 -3
  136. package/dist/plugins/index.d.ts +1 -1
  137. package/dist/plugins/index.js +2 -2
  138. package/dist/plugins/ops/kv-cache-utils.js +1 -1
  139. package/dist/plugins/ops/load-model.js +1 -1
  140. package/dist/plugins/ops/transcribe.js +2 -2
  141. package/dist/plugins/ops/translate.js +4 -12
  142. package/dist/plugins/ops/unload-model.js +19 -21
  143. package/dist/plugins/registry.d.ts +1 -0
  144. package/dist/plugins/registry.js +12 -0
  145. package/dist/profiling/aggregator.d.ts +1 -1
  146. package/dist/profiling/aggregator.js +1 -1
  147. package/dist/profiling/controller.d.ts +1 -1
  148. package/dist/profiling/controller.js +2 -2
  149. package/dist/profiling/envelope.d.ts +0 -1
  150. package/dist/profiling/envelope.js +0 -10
  151. package/dist/profiling/events.d.ts +2 -3
  152. package/dist/profiling/events.js +2 -9
  153. package/dist/profiling/exporters.d.ts +1 -1
  154. package/dist/profiling/exporters.js +3 -3
  155. package/dist/profiling/index.d.ts +13 -14
  156. package/dist/profiling/index.js +12 -13
  157. package/dist/profiling/operation-metrics.d.ts +1 -1
  158. package/dist/profiling/operation-metrics.js +35 -2
  159. package/dist/profiling/operation-wrappers.js +12 -15
  160. package/dist/profiling/profiler.d.ts +1 -2
  161. package/dist/profiling/profiler.js +7 -26
  162. package/dist/profiling/types.d.ts +1 -1
  163. package/dist/rag/handler.js +1 -1
  164. package/dist/rag/handlers/close-workspace.js +1 -1
  165. package/dist/rag/handlers/delete-embeddings.js +1 -1
  166. package/dist/rag/handlers/delete-workspace.js +2 -6
  167. package/dist/rag/handlers/ingest.js +1 -1
  168. package/dist/rag/handlers/list-workspaces.d.ts +1 -1
  169. package/dist/rag/handlers/list-workspaces.js +1 -1
  170. package/dist/rag/handlers/reindex.js +1 -1
  171. package/dist/rag/handlers/save-embeddings.js +1 -1
  172. package/dist/rag/handlers/search.js +1 -1
  173. package/dist/rag/index.d.ts +11 -11
  174. package/dist/rag/index.js +11 -11
  175. package/dist/rag/rag-workspace-manager.d.ts +4 -2
  176. package/dist/rag/rag-workspace-manager.js +268 -15
  177. package/dist/registry.js +8 -58
  178. package/dist/resources/collector.d.ts +3 -1
  179. package/dist/resources/collector.js +91 -9
  180. package/dist/resources/instance.d.ts +1 -1
  181. package/dist/resources/instance.js +1 -1
  182. package/dist/resources/model-fit/assess.d.ts +36 -0
  183. package/dist/resources/model-fit/assess.js +578 -0
  184. package/dist/resources/model-fit/calibration/darwin-arm64.d.ts +8 -0
  185. package/dist/resources/model-fit/calibration/darwin-arm64.js +36 -0
  186. package/dist/resources/model-fit/calibration/darwin-x64.d.ts +8 -0
  187. package/dist/resources/model-fit/calibration/darwin-x64.js +38 -0
  188. package/dist/resources/model-fit/calibration/fit.d.ts +73 -0
  189. package/dist/resources/model-fit/calibration/fit.js +152 -0
  190. package/dist/resources/model-fit/calibration/index.d.ts +37 -0
  191. package/dist/resources/model-fit/calibration/index.js +75 -0
  192. package/dist/resources/model-fit/calibration/linux-arm64.d.ts +8 -0
  193. package/dist/resources/model-fit/calibration/linux-arm64.js +38 -0
  194. package/dist/resources/model-fit/calibration/linux-x64-vulkan.d.ts +8 -0
  195. package/dist/resources/model-fit/calibration/linux-x64-vulkan.js +39 -0
  196. package/dist/resources/model-fit/calibration/linux-x64.d.ts +8 -0
  197. package/dist/resources/model-fit/calibration/linux-x64.js +38 -0
  198. package/dist/resources/model-fit/calibration/win32-x64-vulkan-shared.d.ts +8 -0
  199. package/dist/resources/model-fit/calibration/win32-x64-vulkan-shared.js +39 -0
  200. package/dist/resources/model-fit/calibration/win32-x64-vulkan.d.ts +8 -0
  201. package/dist/resources/model-fit/calibration/win32-x64-vulkan.js +36 -0
  202. package/dist/resources/model-fit/calibration/win32-x64.d.ts +8 -0
  203. package/dist/resources/model-fit/calibration/win32-x64.js +38 -0
  204. package/dist/resources/model-fit/estimators/llm.d.ts +45 -0
  205. package/dist/resources/model-fit/estimators/llm.js +220 -0
  206. package/dist/resources/model-fit/estimators/whisper.d.ts +10 -0
  207. package/dist/resources/model-fit/estimators/whisper.js +77 -0
  208. package/dist/resources/model-fit/types.d.ts +120 -0
  209. package/dist/resources/model-fit/types.js +1 -0
  210. package/dist/resources/native.d.ts +1 -1
  211. package/dist/resources/native.js +14 -0
  212. package/dist/resources/normalize.d.ts +1 -1
  213. package/dist/resources/types.d.ts +16 -0
  214. package/dist/runtime/index.d.ts +5 -5
  215. package/dist/runtime/index.js +2 -2
  216. package/dist/runtime/lifecycle.js +5 -7
  217. package/dist/runtime/model-registry.d.ts +0 -14
  218. package/dist/runtime/model-registry.js +17 -40
  219. package/dist/runtime/registry-client.js +1 -1
  220. package/dist/runtime/request-context.d.ts +3 -3
  221. package/dist/runtime/request-context.js +13 -1
  222. package/dist/runtime/request-registry.d.ts +1 -1
  223. package/dist/runtime/request-registry.js +4 -5
  224. package/dist/runtime/state.d.ts +1 -1
  225. package/dist/runtime/state.js +18 -3
  226. package/dist/schemas/assess-model-fit.d.ts +271 -0
  227. package/dist/schemas/assess-model-fit.js +153 -0
  228. package/dist/schemas/audio-gen.d.ts +434 -4
  229. package/dist/schemas/audio-gen.js +131 -19
  230. package/dist/schemas/batch-completion-stream.d.ts +2 -2
  231. package/dist/schemas/batch-completion-stream.js +3 -3
  232. package/dist/schemas/bci-config.js +73 -27
  233. package/dist/schemas/bci.d.ts +2 -2
  234. package/dist/schemas/bci.js +5 -5
  235. package/dist/schemas/cancel.d.ts +5 -0
  236. package/dist/schemas/cancel.js +1 -0
  237. package/dist/schemas/classification.js +13 -6
  238. package/dist/schemas/common.d.ts +662 -331
  239. package/dist/schemas/common.js +33 -31
  240. package/dist/schemas/completion-event.d.ts +2 -2
  241. package/dist/schemas/completion-event.js +1 -1
  242. package/dist/schemas/completion-stream.d.ts +1 -5
  243. package/dist/schemas/completion-stream.js +4 -8
  244. package/dist/schemas/config.d.ts +640 -18
  245. package/dist/schemas/config.js +50 -8
  246. package/dist/schemas/download-asset.d.ts +19 -1
  247. package/dist/schemas/download-asset.js +20 -4
  248. package/dist/schemas/engine-addon-map.d.ts +1 -1
  249. package/dist/schemas/engine-addon-map.js +3 -3
  250. package/dist/schemas/error.d.ts +2 -2
  251. package/dist/schemas/error.js +2 -2
  252. package/dist/schemas/errors.d.ts +2 -6
  253. package/dist/schemas/errors.js +30 -40
  254. package/dist/schemas/heartbeat.d.ts +10 -0
  255. package/dist/schemas/heartbeat.js +8 -0
  256. package/dist/schemas/index.d.ts +46 -44
  257. package/dist/schemas/index.js +45 -43
  258. package/dist/schemas/llamacpp-config.d.ts +32 -10
  259. package/dist/schemas/llamacpp-config.js +11 -8
  260. package/dist/schemas/load-model.d.ts +2933 -1948
  261. package/dist/schemas/load-model.js +52 -52
  262. package/dist/schemas/mcp-adapter.d.ts +1 -1
  263. package/dist/schemas/model-config-schemas.d.ts +2354 -0
  264. package/dist/schemas/model-config-schemas.js +46 -0
  265. package/dist/schemas/model-info.d.ts +10 -20
  266. package/dist/schemas/model-info.js +4 -25
  267. package/dist/schemas/model-ops.d.ts +0 -1
  268. package/dist/schemas/model-ops.js +1 -2
  269. package/dist/schemas/model-resource-profile.d.ts +109 -0
  270. package/dist/schemas/model-resource-profile.js +83 -0
  271. package/dist/schemas/model-src-utils.d.ts +1 -0
  272. package/dist/schemas/model-src-utils.js +33 -13
  273. package/dist/schemas/ocr.js +48 -13
  274. package/dist/schemas/path-validation.js +8 -2
  275. package/dist/schemas/plugin.d.ts +14 -1
  276. package/dist/schemas/plugin.js +11 -0
  277. package/dist/schemas/profiling.d.ts +138 -43
  278. package/dist/schemas/profiling.js +1 -22
  279. package/dist/schemas/rag.js +1 -1
  280. package/dist/schemas/registry.js +1 -1
  281. package/dist/schemas/reload-config.d.ts +0 -2
  282. package/dist/schemas/reload-config.js +2 -3
  283. package/dist/schemas/sdcpp-config.d.ts +344 -4
  284. package/dist/schemas/sdcpp-config.js +504 -17
  285. package/dist/schemas/system-resources.d.ts +138 -0
  286. package/dist/schemas/system-resources.js +3 -1
  287. package/dist/schemas/text-to-speech.js +183 -76
  288. package/dist/schemas/transcription-config.js +253 -85
  289. package/dist/schemas/transcription.d.ts +3 -3
  290. package/dist/schemas/translate.d.ts +2 -2
  291. package/dist/schemas/translate.js +1 -1
  292. package/dist/schemas/translation-config.js +53 -25
  293. package/dist/schemas/vla.d.ts +1 -1
  294. package/dist/selection.d.ts +1 -5
  295. package/dist/selection.js +2 -6
  296. package/dist/surface.d.ts +1 -1
  297. package/dist/utils/aggregate-events.d.ts +1 -1
  298. package/dist/utils/aggregate-events.js +2 -2
  299. package/dist/utils/archive.js +2 -2
  300. package/dist/utils/audio-input.js +1 -1
  301. package/dist/utils/cache/auto-history.d.ts +1 -1
  302. package/dist/utils/cache/auto-history.js +1 -1
  303. package/dist/utils/cache/paths.js +5 -5
  304. package/dist/utils/completion-normalizer.js +1 -1
  305. package/dist/utils/gguf-tensor-extractor.js +1 -1
  306. package/dist/utils/image-dimensions.d.ts +26 -0
  307. package/dist/utils/image-dimensions.js +46 -0
  308. package/dist/utils/index.d.ts +13 -10
  309. package/dist/utils/index.js +13 -10
  310. package/dist/utils/mcp-adapter.d.ts +1 -1
  311. package/dist/utils/mcp-adapter.js +1 -1
  312. package/dist/utils/path-sanitize.d.ts +7 -0
  313. package/dist/utils/path-sanitize.js +36 -0
  314. package/dist/utils/path-security.d.ts +0 -7
  315. package/dist/utils/path-security.js +1 -36
  316. package/dist/utils/shard-utils.js +3 -3
  317. package/dist/utils/tool-integration.js +1 -1
  318. package/dist/utils/tools/dialect.js +1 -1
  319. package/dist/utils/tools/index.d.ts +2 -2
  320. package/dist/utils/tools/index.js +2 -2
  321. package/dist/utils/tools/parser.js +8 -8
  322. package/dist/utils/tools/parsers/dsml.d.ts +1 -1
  323. package/dist/utils/tools/parsers/dsml.js +1 -1
  324. package/dist/utils/tools/parsers/gemma4native.d.ts +1 -1
  325. package/dist/utils/tools/parsers/gemma4native.js +1 -1
  326. package/dist/utils/tools/parsers/harmony.d.ts +1 -1
  327. package/dist/utils/tools/parsers/harmony.js +1 -1
  328. package/dist/utils/tools/parsers/hermes.d.ts +1 -1
  329. package/dist/utils/tools/parsers/hermes.js +1 -1
  330. package/dist/utils/tools/parsers/json.d.ts +1 -1
  331. package/dist/utils/tools/parsers/json.js +1 -1
  332. package/dist/utils/tools/parsers/pythonic.d.ts +1 -1
  333. package/dist/utils/tools/parsers/pythonic.js +1 -1
  334. package/dist/utils/tools/parsers/qwen35.d.ts +1 -1
  335. package/dist/utils/tools/parsers/qwen35.js +3 -3
  336. package/dist/utils/url-security.d.ts +17 -0
  337. package/dist/utils/url-security.js +75 -0
  338. package/package.json +31 -26
  339. package/dist/api/provide.d.ts +0 -23
  340. package/dist/api/provide.js +0 -32
  341. package/dist/api/stop-provider.d.ts +0 -20
  342. package/dist/api/stop-provider.js +0 -30
  343. package/dist/p2p/cancel-delegated.d.ts +0 -3
  344. package/dist/p2p/cancel-delegated.js +0 -64
  345. package/dist/p2p/completion-stream-delegated.d.ts +0 -4
  346. package/dist/p2p/completion-stream-delegated.js +0 -51
  347. package/dist/p2p/delegate-client.d.ts +0 -9
  348. package/dist/p2p/delegate-client.js +0 -219
  349. package/dist/p2p/delegate-connect-diagnostics.d.ts +0 -8
  350. package/dist/p2p/delegate-connect-diagnostics.js +0 -38
  351. package/dist/p2p/delegate-transport.d.ts +0 -18
  352. package/dist/p2p/delegate-transport.js +0 -237
  353. package/dist/p2p/error-handlers.d.ts +0 -4
  354. package/dist/p2p/error-handlers.js +0 -31
  355. package/dist/p2p/handle-request.d.ts +0 -2
  356. package/dist/p2p/handle-request.js +0 -171
  357. package/dist/p2p/handler-utils.d.ts +0 -19
  358. package/dist/p2p/handler-utils.js +0 -180
  359. package/dist/p2p/heartbeat-delegated.d.ts +0 -3
  360. package/dist/p2p/heartbeat-delegated.js +0 -32
  361. package/dist/p2p/load-model-delegated.d.ts +0 -6
  362. package/dist/p2p/load-model-delegated.js +0 -132
  363. package/dist/p2p/progress-throttle.d.ts +0 -7
  364. package/dist/p2p/progress-throttle.js +0 -47
  365. package/dist/p2p/provideHandler/connection.d.ts +0 -2
  366. package/dist/p2p/provideHandler/connection.js +0 -38
  367. package/dist/p2p/provideHandler/handler.d.ts +0 -2
  368. package/dist/p2p/provideHandler/handler.js +0 -66
  369. package/dist/p2p/provideHandler/proxy.d.ts +0 -2
  370. package/dist/p2p/provideHandler/proxy.js +0 -24
  371. package/dist/p2p/provideHandler.d.ts +0 -1
  372. package/dist/p2p/provideHandler.js +0 -2
  373. package/dist/p2p/rpc-utils.d.ts +0 -4
  374. package/dist/p2p/rpc-utils.js +0 -8
  375. package/dist/p2p/stopProvideHandler.d.ts +0 -2
  376. package/dist/p2p/stopProvideHandler.js +0 -29
  377. package/dist/p2p/swarm.d.ts +0 -9
  378. package/dist/p2p/swarm.js +0 -103
  379. package/dist/p2p/transport-selector.d.ts +0 -2
  380. package/dist/p2p/transport-selector.js +0 -18
  381. package/dist/p2p/unload-model-delegated.d.ts +0 -2
  382. package/dist/p2p/unload-model-delegated.js +0 -31
  383. package/dist/profiling/delegation-profiler.d.ts +0 -31
  384. package/dist/profiling/delegation-profiler.js +0 -155
  385. package/dist/schemas/delegate.d.ts +0 -29
  386. package/dist/schemas/delegate.js +0 -40
  387. package/dist/schemas/provide.d.ts +0 -50
  388. package/dist/schemas/provide.js +0 -32
@@ -0,0 +1,578 @@
1
+ import { getModelResourceProfile } from '../../models/registry/resource-profiles.js';
2
+ import { getGpuCalibration, getSharedGpuCalibration } from './calibration/index.js';
3
+ import { estimateLlm } from './estimators/llm.js';
4
+ import { estimateWhisper } from './estimators/whisper.js';
5
+ const GIB = 1024 * 1024 * 1024;
6
+ /** Engines phase 1 can estimate, and which estimator owns each. */
7
+ const ESTIMATORS = {
8
+ 'llamacpp-completion': estimateLlm,
9
+ 'llamacpp-embedding': estimateLlm,
10
+ 'whispercpp-transcription': estimateWhisper
11
+ };
12
+ const MOBILE_PLATFORMS = ['android-arm64', 'ios-arm64'];
13
+ /**
14
+ * Platforms whose fixture describes CPU-resident execution (`device: 'cpu'`),
15
+ * so a reported GPU sends the host through `resolveGpuPlacement` instead.
16
+ * Apple silicon and mobile calibrate on their GPU already.
17
+ */
18
+ const CPU_CALIBRATED_PLATFORMS = [
19
+ 'darwin-x64',
20
+ 'linux-arm64',
21
+ 'linux-x64',
22
+ 'win32-x64'
23
+ ];
24
+ /**
25
+ * Turns catalog profiles plus a fresh system-memory sample into an advisory
26
+ * verdict.
27
+ *
28
+ * Pure: every input is passed in, so the same call is testable without a worker,
29
+ * a device, or a network. Sampling, platform detection and calibration lookup
30
+ * happen in the handler.
31
+ */
32
+ export function assessModelFitFromResources(options) {
33
+ const { models, execution, resources, platform, calibration } = options;
34
+ const resolveProfile = options.resolveProfile ?? getModelResourceProfile;
35
+ const resolveGpuCalibration = options.resolveGpuCalibration ?? getGpuCalibration;
36
+ const resolveSharedGpuCalibration = options.resolveSharedGpuCalibration ?? getSharedGpuCalibration;
37
+ // Where the model would execute decides which evidence can bound it, and
38
+ // which fixture describes the load. See METHODOLOGY.md, "Which fixture a
39
+ // host gets".
40
+ const mustPlaceGpu = platform !== undefined && CPU_CALIBRATED_PLATFORMS.includes(platform) && hasGpu(resources);
41
+ const placement = mustPlaceGpu && platform ? resolveGpuPlacement(resources, platform) : undefined;
42
+ const gpuCalibration = placement && platform
43
+ ? placement.kind === 'device'
44
+ ? resolveGpuCalibration(platform, placement.backend)
45
+ : resolveSharedGpuCalibration(platform, placement.backend)
46
+ : undefined;
47
+ // A mode only engages with coefficients measured for it.
48
+ const onDevice = placement?.kind === 'device' && gpuCalibration ? placement : undefined;
49
+ const onIntegrated = placement?.kind === 'shared' && gpuCalibration ? placement : undefined;
50
+ const gpuMode = onDevice !== undefined || onIntegrated !== undefined;
51
+ const effectiveCalibration = gpuMode ? gpuCalibration : calibration;
52
+ const basis = onDevice
53
+ ? onDevice.targets[0].scope === 'budget'
54
+ ? 'device-budget'
55
+ : 'device-memory'
56
+ : resolveBasis(platform);
57
+ const reasons = [];
58
+ const assumptions = [
59
+ `execution mode '${execution}' is a declared assumption used for aggregation only; the SDK does not schedule, serialize, or reserve anything`,
60
+ `the verdict is advisory and based on ${basisEvidence(basis)} alone; it does not block loadModel and makes no performance claim`
61
+ ];
62
+ if (onDevice) {
63
+ const { targets, backend } = onDevice;
64
+ assumptions.push(targets.length === 1
65
+ ? `the model is assumed to execute on ${targets[0].device ?? 'the discrete GPU'} via ${backend}, and the budget is that device's own memory`
66
+ : `${targets.length} usable GPUs are reported and the engine pins the model to one of them, an order this side cannot observe; the verdict holds for whichever it picks, and the budget shown is the tightest of them`);
67
+ }
68
+ if (onIntegrated) {
69
+ assumptions.push(`the model is assumed to execute on ${onIntegrated.device ?? 'the integrated GPU'} via ${onIntegrated.backend}; an integrated GPU allocates out of system RAM, so system memory is the budget it draws on`);
70
+ }
71
+ if (platform === 'android-arm64') {
72
+ assumptions.push('android budgets deliberately use system memory: the low-memory killer acts system-wide and native allocations carry no per-process cap like iOS jetsam');
73
+ }
74
+ // Every candidate card carries its own budget; the tightest is reported and
75
+ // the verdict is taken across all of them.
76
+ const deviceBudgets = onDevice?.targets.map((target) => gpuBudget(target, platform));
77
+ const budget = deviceBudgets
78
+ ? tightest(deviceBudgets)
79
+ : resolveBudget(resources, platform, basis, reasons);
80
+ // A discrete-GPU load is paid for in system RAM too. In shared mode the
81
+ // system budget already *is* the budget.
82
+ const alsoBoundBy = onDevice ? resolveBudget(resources, platform, 'system-memory', []) : undefined;
83
+ if (!platform) {
84
+ reasons.push('the runtime platform is not one this assessment covers');
85
+ }
86
+ else if (mustPlaceGpu && !gpuMode) {
87
+ // Name the missing fixture: the platform's own coefficients exist here,
88
+ // they are just the wrong ones.
89
+ reasons.push(!placement
90
+ ? 'a GPU is reported but its readings cannot say where the model would execute, so no estimate can be defended'
91
+ : placement.kind === 'device'
92
+ ? `no validated calibration for ${platform} on ${placement.backend}, so no estimate can be defended`
93
+ : `no validated calibration for ${platform} on an integrated ${placement.backend} GPU, so no estimate can be defended`);
94
+ }
95
+ else if (!effectiveCalibration) {
96
+ reasons.push(`no validated calibration for ${platform}, so no estimate can be defended`);
97
+ }
98
+ else if (effectiveCalibration.measuredAt) {
99
+ assumptions.push(calibrationAssumption(platform, effectiveCalibration));
100
+ }
101
+ const evaluated = models.map((candidate) => evaluate(candidate, platform, effectiveCalibration, resources, resolveProfile, gpuMode));
102
+ for (const { result } of evaluated) {
103
+ for (const assumption of result.assumptions ?? []) {
104
+ if (!assumptions.includes(assumption))
105
+ assumptions.push(assumption);
106
+ }
107
+ }
108
+ const modelResults = evaluated.map(({ candidate, result }) => toModelResult(candidate, result, budget, deviceBudgets, alsoBoundBy));
109
+ const estimates = evaluated
110
+ .map(({ result }) => result)
111
+ .filter((result) => {
112
+ return result.kind === 'estimate';
113
+ });
114
+ const anyUnknown = estimates.length !== evaluated.length;
115
+ const combined = anyUnknown ? undefined : aggregate(estimates, execution);
116
+ if (anyUnknown) {
117
+ reasons.push('at least one model could not be estimated, so the combined verdict is unknown');
118
+ }
119
+ const verdict = !budget || !combined
120
+ ? 'unknown'
121
+ : verdictAgainst(combined, deviceBudgets ?? [budget], alsoBoundBy);
122
+ if (onDevice && combined && alsoBoundBy) {
123
+ reasons.push('a GPU load is also paid for in system RAM, so every verdict is the more pessimistic of the GPU and system budgets');
124
+ }
125
+ if (deviceBudgets && deviceBudgets.length > 1 && combined) {
126
+ reasons.push('more than one usable GPU is reported, so a fit has to hold on the smallest of them and a refusal on the largest');
127
+ }
128
+ if (budget && combined) {
129
+ reasons.push(execution === 'concurrent'
130
+ ? 'all models counted as resident with every working peak added'
131
+ : 'all models counted as resident with only the largest working peak added');
132
+ }
133
+ return {
134
+ verdict,
135
+ basis,
136
+ execution,
137
+ ...(budget && { budget }),
138
+ ...(combined && {
139
+ estimate: { lowerBoundBytes: combined.lower, upperBoundBytes: combined.upper }
140
+ }),
141
+ models: modelResults,
142
+ reasons,
143
+ assumptions
144
+ };
145
+ }
146
+ function evaluate(candidate, platform, calibration, resources, resolveProfile, gpuMode) {
147
+ if (!calibration) {
148
+ return {
149
+ candidate,
150
+ result: {
151
+ kind: 'unknown',
152
+ estimatorVersion: 'none',
153
+ reasons: [
154
+ platform
155
+ ? `no validated calibration for ${platform}`
156
+ : 'the runtime platform is not one this assessment covers'
157
+ ]
158
+ }
159
+ };
160
+ }
161
+ // A GPU is present and neither GPU mode engaged, so the only coefficients
162
+ // left were measured with the offload disabled — not how this host runs.
163
+ if (!gpuMode && platform && CPU_CALIBRATED_PLATFORMS.includes(platform) && hasGpu(resources)) {
164
+ return {
165
+ candidate,
166
+ result: {
167
+ kind: 'unknown',
168
+ estimatorVersion: 'none',
169
+ reasons: [
170
+ 'a GPU is present, so the model executes on it rather than on the CPU this platform’s coefficients were measured against'
171
+ ]
172
+ }
173
+ };
174
+ }
175
+ const profile = resolveProfile(candidate.model.sha256Checksum);
176
+ if (!profile) {
177
+ return {
178
+ candidate,
179
+ result: {
180
+ kind: 'unknown',
181
+ estimatorVersion: 'none',
182
+ reasons: ['no resource profile in the catalog for this checksum']
183
+ }
184
+ };
185
+ }
186
+ const estimator = ESTIMATORS[profile.engine];
187
+ if (!estimator) {
188
+ return {
189
+ candidate,
190
+ result: {
191
+ kind: 'unknown',
192
+ estimatorVersion: 'none',
193
+ reasons: [`engine '${profile.engine}' has no estimator in this phase`]
194
+ }
195
+ };
196
+ }
197
+ const extra = extraArtifactBytes(candidate, resolveProfile);
198
+ if (extra === undefined) {
199
+ return {
200
+ candidate,
201
+ result: {
202
+ kind: 'unknown',
203
+ estimatorVersion: 'none',
204
+ reasons: ['an entry in `artifacts` has no resource profile in the catalog']
205
+ }
206
+ };
207
+ }
208
+ return {
209
+ candidate,
210
+ result: estimator({
211
+ profile,
212
+ workload: candidate.workload,
213
+ extraArtifactBytes: extra,
214
+ calibration,
215
+ hasGpu: hasGpu(resources)
216
+ })
217
+ };
218
+ }
219
+ /**
220
+ * Sums the artifact bytes of companion constants.
221
+ *
222
+ * @returns The total, or `undefined` when any companion is not in the catalog —
223
+ * an incomplete artifact set must not be silently under-counted.
224
+ */
225
+ function extraArtifactBytes(candidate, resolveProfile) {
226
+ if (!candidate.artifacts || candidate.artifacts.length === 0)
227
+ return 0;
228
+ let total = 0;
229
+ for (const artifact of candidate.artifacts) {
230
+ const profile = resolveProfile(artifact.sha256Checksum);
231
+ if (!profile)
232
+ return undefined;
233
+ total += profile.artifactBytes;
234
+ }
235
+ return total;
236
+ }
237
+ /**
238
+ * States when, and under what conditions, this platform's coefficients were
239
+ * measured.
240
+ *
241
+ * The backend belongs in the result because the buffers these coefficients
242
+ * cover are allocated by it, while the coefficients themselves are keyed by
243
+ * platform alone — so a fixture measured on one backend is being applied to
244
+ * every backend on that platform. That is a real caveat, and the caller is
245
+ * entitled to see it rather than read the fixture.
246
+ */
247
+ function calibrationAssumption(platform, calibration) {
248
+ const base = `${platform} coefficients were calibrated on ${calibration.measuredAt}`;
249
+ const on = calibration.measuredOn;
250
+ if (!on)
251
+ return base;
252
+ const device = on.device ? ` (${on.device})` : '';
253
+ return `${base} against a ${on.backend} backend${device}, at ${on.kvElementBytes} bytes per KV element`;
254
+ }
255
+ /**
256
+ * The GPUs the engine could actually execute on: `chooseBackend` passes over a
257
+ * paravirtual adapter and a device with no graphics API this build talks to,
258
+ * and falls back to the CPU. The driver flags are library-presence checks, and
259
+ * ggml's own backend needs the same libraries to load.
260
+ */
261
+ function usableGpus(resources) {
262
+ const gpus = resources.capabilities.gpus;
263
+ if (gpus.status !== 'supported')
264
+ return [];
265
+ return gpus.value.filter((gpu) => !isVirtualDisplayAdapter(gpu) && backendOf(gpu) !== undefined);
266
+ }
267
+ /** Whether a GPU the engine would use is present. Sets the KV-cache default. */
268
+ function hasGpu(resources) {
269
+ return usableGpus(resources).length > 0;
270
+ }
271
+ // An adapter too small to hold any model we assess is not a candidate for one.
272
+ // Windows classifies the Intel iGPU as dedicated because it declares 128 MiB of
273
+ // its own, which is otherwise indistinguishable from a real card by type.
274
+ const MIN_USABLE_GPU_BYTES = GIB;
275
+ // Only excludes a card when the declared memory says so. An unreported total
276
+ // leaves it counted, so an unknown device makes the choice ambiguous rather
277
+ // than silently disappearing from it.
278
+ function tooSmallToHostAModel(gpu) {
279
+ return (gpu.memoryTotalBytes.status === 'supported' && gpu.memoryTotalBytes.value < MIN_USABLE_GPU_BYTES);
280
+ }
281
+ // `gpuType.VIRTUAL`: the virtio / VMware / Hyper-V adapter a VM exposes. It
282
+ // has no compute backend, so counting it as a GPU would deny every VM the
283
+ // platform's own coefficients.
284
+ const GPU_TYPE_VIRTUAL = 3;
285
+ function isVirtualDisplayAdapter(gpu) {
286
+ return gpu.type.status === 'supported' && gpu.type.value === GPU_TYPE_VIRTUAL;
287
+ }
288
+ /**
289
+ * Whether this GPU's allocations come out of system RAM. `unifiedMemory` says
290
+ * so directly; the Windows iGPU does not, being typed dedicated for a 128 MiB
291
+ * carve-out, so the usable-memory floor is what identifies it.
292
+ */
293
+ function allocatesFromSystemMemory(gpu) {
294
+ if (gpu.unifiedMemory.status === 'supported' && gpu.unifiedMemory.value)
295
+ return true;
296
+ return tooSmallToHostAModel(gpu);
297
+ }
298
+ /**
299
+ * Whether the collector's dedicated/integrated call is trustworthy for this
300
+ * device. On linux it is inferred from amdgpu's `mem_info_vram_total`, which an
301
+ * APU also exposes for its carve-out — a Ryzen 5000U reported `dedicated` with
302
+ * over a gigabyte of "VRAM". Vulkan calls the same device INTEGRATED_GPU, but
303
+ * that is not in the collector, so an AMD GPU on linux cannot be placed and
304
+ * assesses as `unknown` until the engine's own device type is exposed.
305
+ */
306
+ function integratedIsIndistinguishable(gpu, platform) {
307
+ if (!platform.startsWith('linux'))
308
+ return false;
309
+ return gpu.driverName.status === 'supported' && gpu.driverName.value === 'amdgpu';
310
+ }
311
+ // Ordered by the backends the addon actually builds, not by what the device's
312
+ // drivers advertise: the NVIDIA calibration host advertises both CUDA and
313
+ // Vulkan, and every load on it reports `ggml_vulkan`, never `ggml_cuda`. The
314
+ // engine's own choice is not observable from here (`chooseBackend` is C++ and
315
+ // only reaches the llama log), so this order has to track the addon's build.
316
+ const GPU_BACKENDS = ['metal', 'vulkan', 'rocm', 'cuda', 'levelZero', 'opencl'];
317
+ function backendOf(gpu) {
318
+ for (const name of GPU_BACKENDS) {
319
+ const driver = gpu.drivers[name];
320
+ if (driver.status === 'supported' && driver.value)
321
+ return name;
322
+ }
323
+ return undefined;
324
+ }
325
+ /**
326
+ * Classifies the reported GPUs: any card with its own memory ⇒ `device`,
327
+ * otherwise ⇒ `shared`. That preference is the engine's own.
328
+ *
329
+ * @returns `undefined` when the readings support neither, or when the cards
330
+ * disagree on backend or scope.
331
+ */
332
+ function resolveGpuPlacement(resources, platform) {
333
+ const gpus = usableGpus(resources);
334
+ if (gpus.length === 0)
335
+ return undefined;
336
+ if (gpus.some((gpu) => integratedIsIndistinguishable(gpu, platform)))
337
+ return undefined;
338
+ // Classify on device properties only, never on a reading. A card whose
339
+ // sample failed must still count as a card the engine could choose;
340
+ // filtering it out here would leave its neighbour looking unambiguous.
341
+ const deviceBacked = gpus.filter((gpu) => !allocatesFromSystemMemory(gpu));
342
+ if (deviceBacked.length === 0) {
343
+ const shared = gpus[0];
344
+ const backend = backendOf(shared);
345
+ // Two integrated devices on different backends would need two fixtures and
346
+ // the engine picks one, so the same rule applies as for cards: no single
347
+ // set of coefficients, no estimate. They share the budget either way.
348
+ if (gpus.some((gpu) => backendOf(gpu) !== backend))
349
+ return undefined;
350
+ return {
351
+ kind: 'shared',
352
+ backend,
353
+ ...(shared.name.status === 'supported' && { device: shared.name.value })
354
+ };
355
+ }
356
+ const samples = resources.sample?.gpus;
357
+ if (samples?.status !== 'supported')
358
+ return undefined;
359
+ const targets = [];
360
+ for (const gpu of deviceBacked) {
361
+ const backend = backendOf(gpu);
362
+ if (!backend)
363
+ return undefined;
364
+ const sample = samples.value.find((entry) => entry.id === gpu.id);
365
+ if (!sample)
366
+ return undefined;
367
+ if (sample.memoryTotalBytes.status !== 'supported')
368
+ return undefined;
369
+ if (sample.memoryUsedBytes.status !== 'supported')
370
+ return undefined;
371
+ if (sample.memoryTotalBytes.value <= 0)
372
+ return undefined;
373
+ if (sample.memoryUsedBytes.value > sample.memoryTotalBytes.value)
374
+ return undefined;
375
+ targets.push({
376
+ backend,
377
+ totalBytes: sample.memoryTotalBytes.value,
378
+ usedBytes: sample.memoryUsedBytes.value,
379
+ scope: sample.memoryTotalBytes.provenance.scope === 'budget' ? 'budget' : 'device',
380
+ ...(gpu.name.status === 'supported' && { device: gpu.name.value })
381
+ });
382
+ }
383
+ // One fixture covers one backend, and one basis: cards that disagree on
384
+ // either cannot be assessed under a single set of coefficients.
385
+ const backend = targets[0].backend;
386
+ if (targets.some((target) => target.backend !== backend))
387
+ return undefined;
388
+ if (targets.some((target) => target.scope !== targets[0].scope))
389
+ return undefined;
390
+ return { kind: 'device', backend, targets };
391
+ }
392
+ function gpuBudget(target, platform) {
393
+ const available = target.totalBytes - target.usedBytes;
394
+ const reserved = reserveBytes(available, platform);
395
+ return {
396
+ totalBytes: target.totalBytes,
397
+ usedBytes: target.usedBytes,
398
+ availableBytes: available,
399
+ reservedBytes: reserved,
400
+ availableAfterReserveBytes: available - reserved
401
+ };
402
+ }
403
+ /** The budget with the least room left, which is the one worth reporting. */
404
+ function tightest(budgets) {
405
+ return budgets.reduce((least, budget) => budget.availableAfterReserveBytes < least.availableAfterReserveBytes ? budget : least);
406
+ }
407
+ /**
408
+ * `candidates` are alternatives — the engine pins the model to one of them, and
409
+ * which one is not observable here — so a fit has to hold on the smallest and a
410
+ * refusal on the largest. `alsoBoundBy` is a conjunction: a GPU load is paid
411
+ * for in system RAM too, so both bounds apply.
412
+ */
413
+ function verdictAgainst(estimate, candidates, alsoBoundBy) {
414
+ const room = candidates.map((budget) => budget.availableAfterReserveBytes);
415
+ const primary = estimate.upper <= Math.min(...room)
416
+ ? 'likely-fits'
417
+ : estimate.lower > Math.max(...room)
418
+ ? 'likely-too-large'
419
+ : 'unknown';
420
+ if (!alsoBoundBy)
421
+ return primary;
422
+ return worst(primary, compare(estimate, alsoBoundBy.availableAfterReserveBytes));
423
+ }
424
+ /** The more pessimistic of two verdicts. */
425
+ function worst(a, b) {
426
+ if (a === 'likely-too-large' || b === 'likely-too-large')
427
+ return 'likely-too-large';
428
+ if (a === 'unknown' || b === 'unknown')
429
+ return 'unknown';
430
+ return 'likely-fits';
431
+ }
432
+ /**
433
+ * Picks the budget basis for a platform.
434
+ *
435
+ * iOS is the exception: jetsam terminates an app on its own footprint against
436
+ * a per-process limit well below device RAM, so a system-memory budget there
437
+ * would defend `likely-fits` verdicts the OS does not honor. Android's
438
+ * low-memory killer acts system-wide and native allocations carry no
439
+ * per-process cap, so it deliberately keeps the system basis with the mobile
440
+ * reserve.
441
+ */
442
+ function resolveBasis(platform) {
443
+ return platform === 'ios-arm64' ? 'process-memory' : 'system-memory';
444
+ }
445
+ function basisEvidence(basis) {
446
+ if (basis === 'process-memory')
447
+ return 'this process’s own memory ceiling';
448
+ if (basis === 'device-memory')
449
+ return 'the GPU’s own memory';
450
+ if (basis === 'device-budget')
451
+ return 'the GPU memory this process is budgeted';
452
+ return 'system memory';
453
+ }
454
+ /**
455
+ * Derives the memory budget from the sample, under the platform's basis.
456
+ *
457
+ * Only `sample.memory` is used: capabilities-only totals say nothing about what
458
+ * is free right now, and a verdict without that is not worth giving. Under the
459
+ * process basis the ceiling is reconstructed as allowance + footprint — the
460
+ * relation the OS enforces — so every budget field keeps the same meaning
461
+ * under either basis.
462
+ */
463
+ function resolveBudget(resources, platform, basis, reasons) {
464
+ const sample = resources.sample;
465
+ if (!sample) {
466
+ reasons.push('no memory sample was available');
467
+ return undefined;
468
+ }
469
+ if (basis === 'process-memory') {
470
+ const available = sample.memory.processAvailableBytes;
471
+ const used = sample.memory.processUsedBytes;
472
+ if (available.status !== 'supported' || used.status !== 'supported') {
473
+ reasons.push('iOS budgets are per-process (jetsam terminates on the app’s own footprint), and the per-process allowance metric is not available on this build');
474
+ return undefined;
475
+ }
476
+ const total = available.value + used.value;
477
+ if (total <= 0) {
478
+ reasons.push('process-memory metrics are inconsistent');
479
+ return undefined;
480
+ }
481
+ const reserved = reserveBytes(available.value, platform);
482
+ return {
483
+ totalBytes: total,
484
+ usedBytes: used.value,
485
+ availableBytes: available.value,
486
+ reservedBytes: reserved,
487
+ availableAfterReserveBytes: available.value - reserved
488
+ };
489
+ }
490
+ const total = sample.memory.totalBytes;
491
+ const used = sample.memory.usedBytes;
492
+ if (total.status !== 'supported' || used.status !== 'supported') {
493
+ reasons.push('system-memory metrics are not supported on this platform');
494
+ return undefined;
495
+ }
496
+ if (total.value <= 0 || used.value > total.value) {
497
+ reasons.push('system-memory metrics are inconsistent');
498
+ return undefined;
499
+ }
500
+ const available = total.value - used.value;
501
+ const reserved = reserveBytes(available, platform);
502
+ return {
503
+ totalBytes: total.value,
504
+ usedBytes: used.value,
505
+ availableBytes: available,
506
+ reservedBytes: reserved,
507
+ availableAfterReserveBytes: available - reserved
508
+ };
509
+ }
510
+ /**
511
+ * `interactive-v1`: 20% of what is available right now, capped at 2 GiB on
512
+ * desktop and 1 GiB on mobile.
513
+ *
514
+ * The reserve is taken from available memory, not total: a share of total is
515
+ * subtracted from a figure that already excludes what is in use, so on a busy
516
+ * host it double-counts and can exceed the whole headroom — a 24 GiB Mac with
517
+ * 3.3 GiB free ended up with a zero budget and called a 2 GiB model too large.
518
+ */
519
+ function reserveBytes(availableBytes, platform) {
520
+ const mobile = platform !== undefined && MOBILE_PLATFORMS.includes(platform);
521
+ return Math.min(mobile ? 1 * GIB : 2 * GIB, Math.floor(availableBytes * 0.2));
522
+ }
523
+ /**
524
+ * Combines per-model bounds under the declared execution mode.
525
+ *
526
+ * Every model is resident either way — what differs is the working peak:
527
+ * `sequential` counts only the largest, `concurrent` counts them all.
528
+ */
529
+ function aggregate(estimates, execution) {
530
+ let persistentLower = 0;
531
+ let persistentUpper = 0;
532
+ let workingLower = 0;
533
+ let workingUpper = 0;
534
+ for (const estimate of estimates) {
535
+ persistentLower += estimate.persistent.lower;
536
+ persistentUpper += estimate.persistent.upper;
537
+ if (execution === 'concurrent') {
538
+ workingLower += estimate.working.lower;
539
+ workingUpper += estimate.working.upper;
540
+ }
541
+ else {
542
+ workingLower = Math.max(workingLower, estimate.working.lower);
543
+ workingUpper = Math.max(workingUpper, estimate.working.upper);
544
+ }
545
+ }
546
+ return { lower: persistentLower + workingLower, upper: persistentUpper + workingUpper };
547
+ }
548
+ function compare(estimate, budget) {
549
+ if (estimate.lower > budget)
550
+ return 'likely-too-large';
551
+ if (estimate.upper <= budget)
552
+ return 'likely-fits';
553
+ return 'unknown';
554
+ }
555
+ function toModelResult(candidate, result, budget,
556
+ /** Every candidate GPU budget, when the host has more than one. */
557
+ deviceBudgets, alsoBoundBy) {
558
+ if (result.kind === 'unknown') {
559
+ return {
560
+ name: candidate.model.name,
561
+ verdict: 'unknown',
562
+ reasons: [...result.reasons]
563
+ };
564
+ }
565
+ const total = {
566
+ lower: result.persistent.lower + result.working.lower,
567
+ upper: result.persistent.upper + result.working.upper
568
+ };
569
+ return {
570
+ name: candidate.model.name,
571
+ verdict: budget ? verdictAgainst(total, deviceBudgets ?? [budget], alsoBoundBy) : 'unknown',
572
+ estimate: { lowerBoundBytes: total.lower, upperBoundBytes: total.upper },
573
+ estimatorVersion: result.estimatorVersion,
574
+ reasons: budget
575
+ ? [...result.reasons]
576
+ : [...result.reasons, 'no usable system-memory sample, so this model has no verdict']
577
+ };
578
+ }
@@ -0,0 +1,8 @@
1
+ import type { PlatformCalibration } from '../types.js';
2
+ /**
3
+ * darwin-arm64 coefficients.
4
+ *
5
+ * Generated by `scripts/calibrate-model-fit.ts`; see `METHODOLOGY.md` next
6
+ * to this file for how the numbers are derived and validated.
7
+ */
8
+ export declare const DARWIN_ARM64_CALIBRATION: PlatformCalibration;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * darwin-arm64 coefficients.
3
+ *
4
+ * Generated by `scripts/calibrate-model-fit.ts`; see `METHODOLOGY.md` next
5
+ * to this file for how the numbers are derived and validated.
6
+ */
7
+ export const DARWIN_ARM64_CALIBRATION = {
8
+ weightUpperCoeff: 1.01,
9
+ workingPeakBytes: {
10
+ lower: 0,
11
+ upper: 2870477
12
+ },
13
+ fixedOverheadBytes: {
14
+ lower: 5703296,
15
+ upper: 85542755
16
+ },
17
+ computeBufferBytesPerToken: {
18
+ lower: 92,
19
+ upper: 138
20
+ },
21
+ audioWindowBytes: {
22
+ lower: 0,
23
+ upper: 0
24
+ },
25
+ audioStreamingBytes: {
26
+ lower: 0,
27
+ upper: 0
28
+ },
29
+ validated: true,
30
+ measuredAt: '2026-09-03',
31
+ measuredOn: {
32
+ backend: 'metal',
33
+ device: 'Apple M4 Max',
34
+ kvElementBytes: 1.0625
35
+ }
36
+ };
@@ -0,0 +1,8 @@
1
+ import type { PlatformCalibration } from '../types.js';
2
+ /**
3
+ * darwin-x64 coefficients.
4
+ *
5
+ * Generated by `scripts/calibrate-model-fit.ts`; see `METHODOLOGY.md` next
6
+ * to this file for how the numbers are derived and validated.
7
+ */
8
+ export declare const DARWIN_X64_CALIBRATION: PlatformCalibration;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * darwin-x64 coefficients.
3
+ *
4
+ * Generated by `scripts/calibrate-model-fit.ts`; see `METHODOLOGY.md` next
5
+ * to this file for how the numbers are derived and validated.
6
+ */
7
+ export const DARWIN_X64_CALIBRATION = {
8
+ weightUpperCoeff: 1.027,
9
+ workingPeakBytes: {
10
+ lower: 0,
11
+ upper: 92474573
12
+ },
13
+ fixedOverheadBytes: {
14
+ lower: 7161979,
15
+ upper: 54888742
16
+ },
17
+ computeBufferBytesPerToken: {
18
+ lower: 0,
19
+ upper: 0
20
+ },
21
+ audioWindowBytes: {
22
+ lower: 0,
23
+ upper: 0
24
+ },
25
+ audioStreamingBytes: {
26
+ lower: 0,
27
+ upper: 0
28
+ },
29
+ validated: true,
30
+ notes: [
31
+ "weights were loaded with load_mode 'none' so RSS counted them in full at load; a mapped weight set keeps at most the artifact size resident, so weightUpperCoeff remains an upper bound for the default mmap load"
32
+ ],
33
+ measuredAt: '2026-09-03',
34
+ measuredOn: {
35
+ backend: 'cpu',
36
+ kvElementBytes: 2
37
+ }
38
+ };