@qvac/inference 0.18.2 → 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,54 @@
1
+ import { promises as fsPromises } from 'bare-fs';
2
+ import { isHuggingFaceHost, measureChecksum } from '../../utils/index.js';
3
+ import { ChecksumUnavailableError, ChecksumValidationFailedError } from '../../errors/index.js';
4
+ import { getEngineLogger } from '../../logging/index.js';
5
+ const logger = getEngineLogger();
6
+ export function isHuggingFaceUrl(url) {
7
+ try {
8
+ return isHuggingFaceHost(new URL(url).hostname);
9
+ }
10
+ catch {
11
+ return false;
12
+ }
13
+ }
14
+ /**
15
+ * Whether an HTTP download's transport must be secure (plaintext / HTTPS→HTTP
16
+ * downgrades refused, loopback exempt). Hugging Face is always enforced so a
17
+ * downgrade can't sidestep its Hub SHA-256 attestation; `requireSecureTransport`
18
+ * extends the same enforcement to every HTTP source.
19
+ */
20
+ export function shouldEnforceSecureTransport(url, requireSecureTransport) {
21
+ return requireSecureTransport || isHuggingFaceUrl(url);
22
+ }
23
+ /**
24
+ * Enforce the download integrity policy for a freshly downloaded HTTP file.
25
+ *
26
+ * A Hugging Face download carries the Hub's SHA-256 (captured from the resolve
27
+ * hop), so it is always verified and a mismatch fails. When no trusted checksum
28
+ * is available, the behavior depends on `requireChecksum`: a source that should
29
+ * be verifiable (Hugging Face without a usable hash) is rejected under the flag;
30
+ * a source with no checksum source at all (non-Hugging-Face) always downloads,
31
+ * with a warning that it was unverified.
32
+ */
33
+ export async function verifyHttpModelFile(url, filePath, hubSha256, requireChecksum, hooks) {
34
+ if (hubSha256) {
35
+ const actual = await measureChecksum(filePath, hooks);
36
+ if (actual !== hubSha256) {
37
+ await fsPromises.unlink(filePath).catch(() => { });
38
+ logger.error(`❌ SHA-256 mismatch for ${url} (expected Hub ${hubSha256}, got ${actual})`);
39
+ throw new ChecksumValidationFailedError(url);
40
+ }
41
+ logger.info(`✅ Verified ${url} against Hugging Face Hub SHA-256`);
42
+ return;
43
+ }
44
+ if (isHuggingFaceUrl(url)) {
45
+ if (requireChecksum) {
46
+ await fsPromises.unlink(filePath).catch(() => { });
47
+ throw new ChecksumUnavailableError(url);
48
+ }
49
+ logger.warn(`⚠️ Integrity not verified: Hugging Face download exposes no Hub SHA-256 for ${url}`);
50
+ return;
51
+ }
52
+ // No trusted checksum to verify against. Allowed, but surfaced so it is never silent.
53
+ logger.warn(`⚠️ Integrity not verified: no checksum available for ${url}`);
54
+ }
@@ -1,3 +1,13 @@
1
1
  import type { ModelProgressUpdate } from '../../schemas/index.js';
2
- import type { DownloadHooks } from '../../handlers/load-model/types.js';
3
- export declare function downloadModelFromHttp(url: string, progressCallback?: (progress: ModelProgressUpdate) => void, hooks?: DownloadHooks): Promise<string>;
2
+ import type { DownloadHooks, DownloadSecurityOptions } from './types.js';
3
+ /**
4
+ * A mid-stream socket/body error, a connection failure (HTTP status 0, no
5
+ * response), or our own resume-triggered abort is recoverable by resuming from
6
+ * the partial. A real HTTP status (4xx/5xx) or a missing/unreadable body is not.
7
+ *
8
+ * `DownloadCancelledError` is intentionally NOT excluded here: this is only
9
+ * reached after the caller's consumer-cancel gate, so a cancellation at this
10
+ * point is our per-attempt abort (resume interrupt), which must be retried.
11
+ */
12
+ export declare function isResumableTransferError(error: unknown): boolean;
13
+ export declare function downloadModelFromHttp(url: string, progressCallback?: (progress: ModelProgressUpdate) => void, hooks?: DownloadHooks, security?: DownloadSecurityOptions): Promise<string>;
@@ -1,18 +1,26 @@
1
1
  import fs, { promises as fsPromises } from 'bare-fs';
2
2
  import path from 'bare-path';
3
3
  import { Readable } from 'bare-stream';
4
- import fetch, { Headers } from 'bare-fetch';
5
4
  import { AbortController } from 'bare-abort-controller';
6
- import Buffer from 'bare-buffer';
7
- import { withTimeout } from '../../utils/withTimeout.js';
8
5
  import { getModelsCacheDir, getShardedModelCacheDir, generateShortHash, detectShardedModel, parsePatternBasedShardUrl, extractTensorsFromShards, calculatePercentage, isArchiveUrl, sanitizePathComponent, extractAndValidateShardedArchive, validateShardedModelCache, checkAllShardsExist, generateShardFilenames, hasValidGGUFHeader } from '../../utils/index.js';
6
+ import { safeFetch } from './safe-fetch.js';
7
+ import { shouldEnforceSecureTransport, verifyHttpModelFile } from './http-verify.js';
9
8
  import { getConfig } from '../../runtime/state.js';
10
9
  import { getLifecycleState, onResume } from '../../runtime/runtime-lifecycle.js';
11
- import { createHttpDownloadKey, startOrJoinDownload, applyJoinedDownloadStats } from '../../handlers/load-model/download-manager.js';
12
- import { DownloadCancelledError, HTTPError, NoResponseBodyError, PartialDownloadOfflineError, ResponseBodyNotReadableError } from '../../errors/index.js';
10
+ import { createHttpDownloadKey, startOrJoinDownload, applyJoinedDownloadStats } from './download-manager.js';
11
+ import { ChecksumUnavailableError, ChecksumValidationFailedError, DownloadCancelledError, HTTPError, InsecureModelSourceError, NoResponseBodyError, PartialDownloadOfflineError, ResponseBodyNotReadableError } from '../../errors/index.js';
13
12
  import { getEngineLogger } from '../../logging/index.js';
14
13
  const logger = getEngineLogger();
15
14
  const DEFAULT_CONCURRENCY = 3;
15
+ // Resolve the effective download-security flags: a per-call override wins,
16
+ // otherwise the engine config, otherwise off.
17
+ function resolveDownloadSecurity(security) {
18
+ const config = getConfig();
19
+ return {
20
+ requireChecksum: security?.requireHttpChecksum ?? config.requireHttpChecksum ?? false,
21
+ requireSecureTransport: security?.requireSecureTransport ?? config.requireSecureTransport ?? false
22
+ };
23
+ }
16
24
  const DEFAULT_HTTP_CONNECTION_TIMEOUT_MS = 10_000;
17
25
  // If no bytes arrive for this long mid-stream, treat the transfer as stalled
18
26
  // (dead socket after a suspend or network drop that didn't surface an error)
@@ -27,7 +35,13 @@ function extractFilenameFromUrl(url) {
27
35
  // Sanitize to prevent path traversal via crafted URLs
28
36
  return sanitizePathComponent(cleanFilename);
29
37
  }
30
- async function validateCachedFile(modelPath, url, signal) {
38
+ // A cache hit is served after a freshness check only — size against the server,
39
+ // or a GGUF-header check when offline — and is not re-hashed (that would cost a
40
+ // full-file SHA-256 on every load). Verification happens at download time, so a
41
+ // cache entry from before this shipped, or one written down the warn-and-proceed
42
+ // path, is served as-is: tightening requireHttpChecksum does not retroactively
43
+ // verify the cache — clear it to force a fresh, verified download.
44
+ async function validateCachedFile(modelPath, url, requireSecureTransport, signal) {
31
45
  try {
32
46
  await fsPromises.access(modelPath);
33
47
  const localStats = await fsPromises.stat(modelPath);
@@ -36,13 +50,19 @@ async function validateCachedFile(modelPath, url, signal) {
36
50
  const connectionTimeout = config.httpConnectionTimeoutMs ?? DEFAULT_HTTP_CONNECTION_TIMEOUT_MS;
37
51
  let expectedSize = 0;
38
52
  try {
39
- const response = await withTimeout(fetch(url, {
53
+ const response = await safeFetch(url, {
40
54
  method: 'HEAD',
55
+ timeoutMs: connectionTimeout,
56
+ enforceSecureTransport: shouldEnforceSecureTransport(url, requireSecureTransport),
41
57
  ...(signal && { signal })
42
- }), connectionTimeout);
43
- expectedSize = parseInt(response.headers.get('content-length') || '0');
58
+ });
59
+ response.body.resume();
60
+ expectedSize = parseInt(String(response.headers['content-length'] ?? '0') || '0');
44
61
  }
45
62
  catch (headError) {
63
+ // A refused insecure Hugging Face hop is terminal, not an offline fallback.
64
+ if (headError instanceof InsecureModelSourceError)
65
+ throw headError;
46
66
  logger.warn(`⚠️ HEAD request failed: ${headError instanceof Error ? headError.message : String(headError)}`);
47
67
  logger.info(`📴 Falling back to GGUF header validation...`);
48
68
  const hasValidHeader = await hasValidGGUFHeader(modelPath);
@@ -65,15 +85,15 @@ async function validateCachedFile(modelPath, url, signal) {
65
85
  return modelPath;
66
86
  }
67
87
  catch (error) {
68
- // Re-throw PartialDownloadOfflineError
69
- if (error instanceof PartialDownloadOfflineError) {
88
+ // A refused insecure hop and an offline partial are terminal; everything
89
+ // else (missing file, access error) just means "no usable cache" → re-download.
90
+ if (error instanceof PartialDownloadOfflineError || error instanceof InsecureModelSourceError) {
70
91
  throw error;
71
92
  }
72
- // File doesn't exist or other access error
73
93
  return null;
74
94
  }
75
95
  }
76
- async function performHttpDownload(url, modelPath, downloadKey, progressCallback, signal) {
96
+ async function performHttpDownload(url, modelPath, downloadKey, requireSecureTransport, progressCallback, signal) {
77
97
  if (signal?.aborted) {
78
98
  throw new DownloadCancelledError();
79
99
  }
@@ -90,36 +110,45 @@ async function performHttpDownload(url, modelPath, downloadKey, progressCallback
90
110
  catch {
91
111
  logger.info(`📥 Starting fresh download`);
92
112
  }
93
- // Prepare headers for resume if needed
94
- const headers = new Headers({
95
- 'User-Agent': 'qvac'
96
- });
113
+ // Prepare headers for resume if needed. Request identity encoding: the raw
114
+ // client streams bytes straight to disk with no Content-Encoding decode, and
115
+ // Range resume assumes unencoded bytes.
116
+ const headers = { 'User-Agent': 'qvac', 'Accept-Encoding': 'identity' };
97
117
  if (startOffset > 0) {
98
- headers.append('Range', `bytes=${startOffset}-`);
118
+ headers['Range'] = `bytes=${startOffset}-`;
99
119
  }
100
120
  const config = getConfig();
101
121
  const connectionTimeout = config.httpConnectionTimeoutMs ?? DEFAULT_HTTP_CONNECTION_TIMEOUT_MS;
122
+ const enforceSecureTransport = shouldEnforceSecureTransport(url, requireSecureTransport);
102
123
  let response;
103
124
  try {
104
- response = await withTimeout(fetch(url, {
125
+ response = await safeFetch(url, {
105
126
  method: 'GET',
106
127
  headers,
128
+ timeoutMs: connectionTimeout,
129
+ enforceSecureTransport,
107
130
  ...(signal && { signal })
108
- }), connectionTimeout);
131
+ });
109
132
  }
110
133
  catch (error) {
111
- // Check if it was parent abort
112
- if (signal?.aborted) {
113
- throw new DownloadCancelledError();
134
+ // A refused insecure hop or a real cancel is terminal; everything else is a
135
+ // connection/network failure the caller can resume from.
136
+ if (signal?.aborted || error instanceof DownloadCancelledError) {
137
+ throw error instanceof DownloadCancelledError ? error : new DownloadCancelledError();
138
+ }
139
+ if (error instanceof InsecureModelSourceError) {
140
+ throw error;
114
141
  }
115
- // Connection timeout or network error
116
142
  const errorMsg = error instanceof Error ? error.message : String(error);
117
143
  logger.error(`❌ Connection failed: ${errorMsg}. URL: ${url}`);
118
144
  throw new HTTPError(0, `Connection failed: ${errorMsg}`, error);
119
145
  }
120
- if (!response.ok) {
121
- // Check if it's a 416 (Range Not Satisfiable) - file already complete
146
+ const meta = { hubSha256: response.hubSha256 };
147
+ const isOk = response.status >= 200 && response.status <= 299;
148
+ if (!isOk) {
149
+ // 416 Range Not Satisfiable — the partial on disk is already the whole file.
122
150
  if (response.status === 416 && startOffset > 0) {
151
+ response.body.destroy();
123
152
  logger.info(`✅ File already completely downloaded`);
124
153
  // Send 100% progress for already complete file
125
154
  if (progressCallback) {
@@ -131,36 +160,30 @@ async function performHttpDownload(url, modelPath, downloadKey, progressCallback
131
160
  downloadKey
132
161
  });
133
162
  }
134
- return;
135
- }
136
- // Check if server doesn't support range requests
137
- if (response.status === 200 && startOffset > 0) {
138
- logger.warn(`⚠️ Server doesn't support resume, starting fresh download`);
139
- startOffset = 0;
140
- downloadedBytes = 0;
141
- // Retry without Range header
142
- response = await fetch(url, {
143
- method: 'GET',
144
- headers: new Headers({
145
- 'User-Agent': 'qvac'
146
- }),
147
- ...(signal && { signal })
148
- });
149
- if (!response.ok) {
150
- throw new HTTPError(response.status, response.statusText);
151
- }
152
- }
153
- else if (response.status !== 206) {
154
- // 206 is Partial Content (successful resume)
155
- throw new HTTPError(response.status, response.statusText);
163
+ return meta;
156
164
  }
165
+ response.body.destroy();
166
+ throw new HTTPError(response.status, response.statusText);
167
+ }
168
+ // A resume sent a Range header but the server ignored it and returned the
169
+ // whole file (200 instead of 206). The write stream truncates in that case
170
+ // (append is keyed on 206 below), so reset the byte counter to 0 or progress
171
+ // would be measured from the discarded partial's size.
172
+ if (startOffset > 0 && response.status !== 206) {
173
+ logger.warn(`⚠️ Server doesn't support resume, starting fresh download`);
174
+ startOffset = 0;
175
+ downloadedBytes = 0;
157
176
  }
158
177
  // Get total size from headers
159
178
  let totalBytes = 0;
160
- const contentLength = response.headers.get('content-length');
179
+ const contentLength = response.headers['content-length'] === undefined
180
+ ? null
181
+ : String(response.headers['content-length']);
161
182
  if (response.status === 206) {
162
183
  // For resumed downloads, parse Content-Range header
163
- const contentRange = response.headers.get('content-range');
184
+ const contentRange = response.headers['content-range'] === undefined
185
+ ? null
186
+ : String(response.headers['content-range']);
164
187
  if (contentRange) {
165
188
  const match = contentRange.match(/bytes \d+-\d+\/(\d+)/);
166
189
  if (match && match[1]) {
@@ -255,164 +278,8 @@ async function performHttpDownload(url, modelPath, downloadKey, progressCallback
255
278
  });
256
279
  });
257
280
  }
258
- else if (body[Symbol.asyncIterator]) {
259
- // Body is an async iterable. Drive the iterator manually so each pull can
260
- // race a stall timeout: a dead socket after suspend/drop produces neither
261
- // a chunk nor an error, so the timeout converts it into a retriable
262
- // failure and we abandon the iterator (which cancels the stream).
263
- const iterator = body[Symbol.asyncIterator]();
264
- let stalled = false;
265
- try {
266
- for (;;) {
267
- let stallTimer;
268
- const nextPromise = iterator.next();
269
- nextPromise.catch(() => { });
270
- let step;
271
- try {
272
- step = await Promise.race([
273
- nextPromise,
274
- new Promise((_, reject) => {
275
- stallTimer = setTimeout(() => {
276
- stalled = true;
277
- reject(new Error(`HTTP stream stalled: no data for ${HTTP_STREAM_STALL_TIMEOUT_MS}ms`));
278
- }, HTTP_STREAM_STALL_TIMEOUT_MS);
279
- })
280
- ]);
281
- }
282
- finally {
283
- if (stallTimer)
284
- clearTimeout(stallTimer);
285
- }
286
- if (step.done)
287
- break;
288
- if (signal?.aborted) {
289
- writeStream.destroy();
290
- throw new DownloadCancelledError();
291
- }
292
- const buffer = Buffer.isBuffer(step.value) ? step.value : Buffer.from(step.value);
293
- downloadedBytes += buffer.length;
294
- if (progressCallback) {
295
- progressCallback({
296
- type: 'modelProgress',
297
- downloaded: downloadedBytes,
298
- total: totalBytes,
299
- percentage: calculatePercentage(downloadedBytes, totalBytes),
300
- downloadKey
301
- });
302
- }
303
- // Write chunk to file
304
- await new Promise((resolve, reject) => {
305
- writeStream.write(buffer, (err) => {
306
- if (err)
307
- reject(new Error(err instanceof Error ? err.message : String(err)));
308
- else
309
- resolve();
310
- });
311
- });
312
- }
313
- }
314
- finally {
315
- if (stalled) {
316
- try {
317
- await iterator.return?.();
318
- }
319
- catch {
320
- /* best effort: free the dead socket */
321
- }
322
- }
323
- }
324
- // Close the write stream
325
- await new Promise((resolve, reject) => {
326
- writeStream.end(() => {
327
- logger.info(`✅ Model downloaded successfully to ${modelPath}`);
328
- resolve();
329
- });
330
- writeStream.on('error', reject);
331
- });
332
- }
333
281
  else {
334
- // Fallback: getReader() for a WHATWG ReadableStream (bare-fetch body).
335
- const readableStreamBody = body;
336
- const reader = readableStreamBody.getReader ? readableStreamBody.getReader() : null;
337
- if (reader) {
338
- let cancelled = false;
339
- try {
340
- for (;;) {
341
- // Race each read against a stall timeout. A dead socket after a
342
- // suspend/drop yields no data and no error; the timeout converts
343
- // that into a retriable failure so the caller resumes via Range.
344
- let stallTimer;
345
- const readPromise = reader.read();
346
- // Avoid an unhandled rejection if the read loses the race.
347
- readPromise.catch(() => { });
348
- let result;
349
- try {
350
- result = await Promise.race([
351
- readPromise,
352
- new Promise((_, reject) => {
353
- stallTimer = setTimeout(() => {
354
- cancelled = true;
355
- reject(new Error(`HTTP stream stalled: no data for ${HTTP_STREAM_STALL_TIMEOUT_MS}ms`));
356
- }, HTTP_STREAM_STALL_TIMEOUT_MS);
357
- })
358
- ]);
359
- }
360
- finally {
361
- if (stallTimer)
362
- clearTimeout(stallTimer);
363
- }
364
- const { done, value } = result;
365
- if (done)
366
- break;
367
- if (signal?.aborted) {
368
- cancelled = true;
369
- throw new DownloadCancelledError();
370
- }
371
- const buffer = Buffer.from(value);
372
- downloadedBytes += buffer.length;
373
- if (progressCallback) {
374
- progressCallback({
375
- type: 'modelProgress',
376
- downloaded: downloadedBytes,
377
- total: totalBytes,
378
- percentage: calculatePercentage(downloadedBytes, totalBytes),
379
- downloadKey
380
- });
381
- }
382
- // Write chunk to file
383
- await new Promise((resolve, reject) => {
384
- writeStream.write(buffer, (err) => {
385
- if (err)
386
- reject(new Error(err instanceof Error ? err.message : String(err)));
387
- else
388
- resolve();
389
- });
390
- });
391
- }
392
- }
393
- finally {
394
- if (cancelled) {
395
- try {
396
- await reader.cancel();
397
- }
398
- catch {
399
- /* best effort: free the dead socket */
400
- }
401
- }
402
- reader.releaseLock();
403
- }
404
- // Close the write stream
405
- await new Promise((resolve, reject) => {
406
- writeStream.end(() => {
407
- logger.info(`✅ Model downloaded successfully to ${modelPath}`);
408
- resolve();
409
- });
410
- writeStream.on('error', reject);
411
- });
412
- }
413
- else {
414
- throw new ResponseBodyNotReadableError();
415
- }
282
+ throw new ResponseBodyNotReadableError();
416
283
  }
417
284
  }
418
285
  catch (error) {
@@ -420,6 +287,7 @@ async function performHttpDownload(url, modelPath, downloadKey, progressCallback
420
287
  logger.error('Error during download:', error instanceof Error ? error.message : String(error));
421
288
  throw error instanceof Error ? error : new Error(String(error));
422
289
  }
290
+ return meta;
423
291
  }
424
292
  const DEFAULT_HTTP_DOWNLOAD_MAX_RETRIES = 5;
425
293
  const HTTP_RETRY_BASE_DELAY_MS = 500;
@@ -435,10 +303,16 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
435
303
  * reached after the caller's consumer-cancel gate, so a cancellation at this
436
304
  * point is our per-attempt abort (resume interrupt), which must be retried.
437
305
  */
438
- function isResumableTransferError(error) {
306
+ export function isResumableTransferError(error) {
439
307
  if (error instanceof NoResponseBodyError ||
440
308
  error instanceof ResponseBodyNotReadableError ||
441
- error instanceof PartialDownloadOfflineError) {
309
+ error instanceof PartialDownloadOfflineError ||
310
+ error instanceof InsecureModelSourceError ||
311
+ // Integrity failures are terminal — resuming would range-append onto bad
312
+ // bytes. Verification runs after the retry loop today, so these are belt-and-
313
+ // suspenders, but they keep the guarantee if verification ever moves inline.
314
+ error instanceof ChecksumValidationFailedError ||
315
+ error instanceof ChecksumUnavailableError) {
442
316
  return false;
443
317
  }
444
318
  if (error instanceof HTTPError)
@@ -473,7 +347,7 @@ async function waitForLifecycleActive(signal) {
473
347
  * recovery — the in-flight socket is dead after a background, so abort and
474
348
  * range-resume immediately instead of waiting for the stall watchdog).
475
349
  */
476
- async function performHttpDownloadWithResume(url, modelPath, downloadKey, progressCallback, signal) {
350
+ async function performHttpDownloadWithResume(url, modelPath, downloadKey, requireSecureTransport, progressCallback, signal) {
477
351
  const maxRetries = DEFAULT_HTTP_DOWNLOAD_MAX_RETRIES;
478
352
  let attempt = 0;
479
353
  for (;;) {
@@ -488,8 +362,7 @@ async function performHttpDownloadWithResume(url, modelPath, downloadKey, progre
488
362
  // resume() → abort this attempt so it range-resumes now, not after the stall.
489
363
  const offResume = onResume(() => attemptController.abort(undefined));
490
364
  try {
491
- await performHttpDownload(url, modelPath, downloadKey, progressCallback, attemptController.signal);
492
- return;
365
+ return await performHttpDownload(url, modelPath, downloadKey, requireSecureTransport, progressCallback, attemptController.signal);
493
366
  }
494
367
  catch (error) {
495
368
  // A real consumer cancel is terminal; anything else (resume abort, stall,
@@ -511,29 +384,30 @@ async function performHttpDownloadWithResume(url, modelPath, downloadKey, progre
511
384
  }
512
385
  }
513
386
  }
514
- export async function downloadModelFromHttp(url, progressCallback, hooks) {
387
+ export async function downloadModelFromHttp(url, progressCallback, hooks, security) {
515
388
  const filename = extractFilenameFromUrl(url);
516
389
  if (isArchiveUrl(url)) {
517
- return downloadShardedModelFromArchive(url, progressCallback, hooks);
390
+ return downloadShardedModelFromArchive(url, progressCallback, hooks, security);
518
391
  }
519
392
  const shardInfo = detectShardedModel(filename);
520
393
  if (shardInfo.isSharded && shardInfo.totalShards) {
521
- return downloadShardedModelFromHttp(url, progressCallback, hooks);
394
+ return downloadShardedModelFromHttp(url, progressCallback, hooks, security);
522
395
  }
523
396
  const downloadKey = createHttpDownloadKey(url);
524
397
  hooks?.onDownloadKey?.(downloadKey);
525
398
  const cacheDir = getModelsCacheDir();
526
399
  const sourceHash = generateShortHash(url);
527
400
  const modelPath = `${cacheDir}/${sourceHash}_${filename}`;
401
+ const { requireChecksum, requireSecureTransport } = resolveDownloadSecurity(security);
528
402
  const result = startOrJoinDownload(downloadKey, async (ctx) => {
529
403
  try {
530
404
  // Check if already cached
531
- const cachedPath = await validateCachedFile(modelPath, url, ctx.signal);
532
- if (cachedPath) {
405
+ const cached = await validateCachedFile(modelPath, url, requireSecureTransport, ctx.signal);
406
+ if (cached) {
533
407
  hooks?.markCacheHit?.();
534
408
  ctx.setCacheHit(true);
535
409
  try {
536
- const stats = await fsPromises.stat(cachedPath);
410
+ const stats = await fsPromises.stat(cached);
537
411
  ctx.broadcastProgress({
538
412
  type: 'modelProgress',
539
413
  downloaded: stats.size,
@@ -544,16 +418,17 @@ export async function downloadModelFromHttp(url, progressCallback, hooks) {
544
418
  }
545
419
  catch (error) {
546
420
  logger.debug('Failed to get file stats for progress callback', {
547
- path: cachedPath,
421
+ path: cached,
548
422
  error
549
423
  });
550
424
  }
551
- return cachedPath;
425
+ return cached;
552
426
  }
553
427
  // Download the file
554
428
  hooks?.markCacheMiss?.();
555
429
  ctx.setCacheHit(false);
556
- await performHttpDownloadWithResume(url, modelPath, downloadKey, ctx.broadcastProgress, ctx.signal);
430
+ const meta = await performHttpDownloadWithResume(url, modelPath, downloadKey, requireSecureTransport, ctx.broadcastProgress, ctx.signal);
431
+ await verifyHttpModelFile(url, modelPath, meta.hubSha256, requireChecksum, hooks);
557
432
  try {
558
433
  const stats = await fsPromises.stat(modelPath);
559
434
  ctx.broadcastProgress({
@@ -599,9 +474,11 @@ export async function downloadModelFromHttp(url, progressCallback, hooks) {
599
474
  }, progressCallback, hooks?.requestBinding);
600
475
  return applyJoinedDownloadStats(result, hooks);
601
476
  }
602
- async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks) {
477
+ async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks, security) {
603
478
  const config = getConfig();
604
479
  const concurrency = config.httpDownloadConcurrency ?? DEFAULT_CONCURRENCY;
480
+ const connectionTimeout = config.httpConnectionTimeoutMs ?? DEFAULT_HTTP_CONNECTION_TIMEOUT_MS;
481
+ const { requireChecksum, requireSecureTransport } = resolveDownloadSecurity(security);
605
482
  const { shardUrls: shardInfos, cacheKey } = parsePatternBasedShardUrl(shardUrl);
606
483
  const downloadKey = `http-sharded:${cacheKey}`;
607
484
  hooks?.onDownloadKey?.(downloadKey);
@@ -613,13 +490,20 @@ async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks) {
613
490
  const shardPath = path.join(shardDir, shard.filename);
614
491
  let expectedSize = 0;
615
492
  try {
616
- const response = await fetch(shard.url, {
493
+ const response = await safeFetch(shard.url, {
617
494
  method: 'HEAD',
495
+ timeoutMs: connectionTimeout,
496
+ enforceSecureTransport: shouldEnforceSecureTransport(shard.url, requireSecureTransport),
618
497
  signal: ctx.signal
619
498
  });
620
- expectedSize = parseInt(response.headers.get('content-length') || '0');
499
+ response.body.resume();
500
+ expectedSize = parseInt(String(response.headers['content-length'] ?? '0') || '0');
621
501
  }
622
502
  catch (error) {
503
+ // A refused insecure hop is terminal; fail fast instead of logging a
504
+ // misleading "failed to get shard size" and hitting it again on GET.
505
+ if (error instanceof InsecureModelSourceError)
506
+ throw error;
623
507
  logger.warn('Failed to get shard size via HEAD request', {
624
508
  url: shard.url,
625
509
  error
@@ -637,7 +521,7 @@ async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks) {
637
521
  const overallTotal = shardStates.reduce((sum, s) => sum + s.expectedSize, 0);
638
522
  logger.info(`📏 Total size: ${overallTotal} bytes (${(overallTotal / 1024 / 1024).toFixed(2)} MB)`);
639
523
  const cacheChecks = await Promise.all(shardStates.map(async (state) => {
640
- const cached = await validateCachedFile(state.shardPath, state.shard.url, ctx.signal);
524
+ const cached = await validateCachedFile(state.shardPath, state.shard.url, requireSecureTransport, ctx.signal);
641
525
  return { state, isCached: cached !== null };
642
526
  }));
643
527
  const shardsToDownload = cacheChecks.filter((c) => !c.isCached).map((c) => c.state);
@@ -656,7 +540,7 @@ async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks) {
656
540
  hooks?.markCacheMiss?.();
657
541
  ctx.setCacheHit(false);
658
542
  }
659
- await downloadShardsWithConcurrency(shardsToDownload, shardStates, concurrency, ctx.signal, downloadKey, overallTotal, ctx.broadcastProgress);
543
+ await downloadShardsWithConcurrency(shardsToDownload, shardStates, concurrency, ctx.signal, downloadKey, overallTotal, requireSecureTransport, ctx.broadcastProgress, requireChecksum, hooks);
660
544
  logger.info(`✅ All ${shardInfos.length} shards downloaded successfully`);
661
545
  await extractTensorsFromShards(shardDir, shardInfos[0].filename);
662
546
  return path.join(shardDir, shardInfos[0].filename);
@@ -683,11 +567,12 @@ async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks) {
683
567
  }, progressCallback, hooks?.requestBinding);
684
568
  return applyJoinedDownloadStats(result, hooks);
685
569
  }
686
- async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hooks) {
570
+ async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hooks, security) {
687
571
  const filename = extractFilenameFromUrl(archiveUrl);
688
572
  const sourceHash = generateShortHash(archiveUrl);
689
573
  const downloadKey = `http-archive:${sourceHash}`;
690
574
  hooks?.onDownloadKey?.(downloadKey);
575
+ const { requireChecksum, requireSecureTransport } = resolveDownloadSecurity(security);
691
576
  logger.info(`📦 HTTP archive download: ${filename}`);
692
577
  const extractDir = getShardedModelCacheDir(sourceHash);
693
578
  const archivePath = path.join(extractDir, `${sourceHash}_${filename}`);
@@ -772,7 +657,10 @@ async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hoo
772
657
  throw error;
773
658
  }
774
659
  async function downloadAndExtractArchive() {
775
- await performHttpDownloadWithResume(archiveUrl, archivePath, downloadKey, ctx.broadcastProgress, ctx.signal);
660
+ const meta = await performHttpDownloadWithResume(archiveUrl, archivePath, downloadKey, requireSecureTransport, ctx.broadcastProgress, ctx.signal);
661
+ // Verify the downloaded archive before extraction so tampered bytes
662
+ // never reach the extractor.
663
+ await verifyHttpModelFile(archiveUrl, archivePath, meta.hubSha256, requireChecksum, hooks);
776
664
  logger.info(`✅ Archive downloaded, extracting to: ${extractDir}`);
777
665
  const firstShardPath = await extractAndValidateShardedArchive(archivePath, extractDir, ctx.signal);
778
666
  try {
@@ -790,7 +678,7 @@ async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hoo
790
678
  }, progressCallback, hooks?.requestBinding);
791
679
  return applyJoinedDownloadStats(result, hooks);
792
680
  }
793
- async function downloadShardsWithConcurrency(shardsToDownload, allShards, concurrency, signal, downloadKey, overallTotal, progressCallback) {
681
+ async function downloadShardsWithConcurrency(shardsToDownload, allShards, concurrency, signal, downloadKey, overallTotal, requireSecureTransport, progressCallback, requireChecksum = false, hooks) {
794
682
  const queue = [...shardsToDownload];
795
683
  const inFlight = new Set();
796
684
  while (queue.length > 0 || inFlight.size > 0) {
@@ -801,7 +689,7 @@ async function downloadShardsWithConcurrency(shardsToDownload, allShards, concur
801
689
  const state = queue.shift();
802
690
  const downloadPromise = (async () => {
803
691
  logger.info(`📥 Downloading shard ${state.index + 1}: ${state.shard.filename}`);
804
- await performHttpDownloadWithResume(state.shard.url, state.shardPath, downloadKey, (progress) => {
692
+ const meta = await performHttpDownloadWithResume(state.shard.url, state.shardPath, downloadKey, requireSecureTransport, (progress) => {
805
693
  state.downloadedBytes = progress.downloaded;
806
694
  if (progressCallback) {
807
695
  const overallDownloaded = allShards.reduce((sum, s) => sum + s.downloadedBytes, 0);
@@ -822,6 +710,7 @@ async function downloadShardsWithConcurrency(shardsToDownload, allShards, concur
822
710
  });
823
711
  }
824
712
  }, signal);
713
+ await verifyHttpModelFile(state.shard.url, state.shardPath, meta.hubSha256, requireChecksum, hooks);
825
714
  logger.info(`✅ Shard ${state.index + 1} complete: ${state.shard.filename}`);
826
715
  })().finally(() => {
827
716
  inFlight.delete(downloadPromise);
@@ -1,4 +1,4 @@
1
1
  import type { ModelProgressUpdate } from '../../schemas/index.js';
2
- import type { DownloadHooks } from '../../handlers/load-model/types.js';
2
+ import type { DownloadHooks } from './types.js';
3
3
  export declare function deleteCorestoreDirectory(corestoreDir: string): Promise<void>;
4
4
  export declare function downloadModelFromHyperdrive(hyperdriveKey: string, modelFileName: string, seed?: boolean, progressCallback?: (progress: ModelProgressUpdate) => void, hooks?: DownloadHooks, expectedChecksum?: string): Promise<string>;