@qvac/inference 0.18.2 → 0.19.1

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