@qvac/inference 0.23.0 → 0.24.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 (426) hide show
  1. package/NOTICE +60 -28
  2. package/README.md +3 -1
  3. package/dist/api/assess-model-fit.d.ts +14 -14
  4. package/dist/api/assess-model-fit.js +14 -14
  5. package/dist/api/audio-gen.d.ts +2 -2
  6. package/dist/api/audio-gen.js +135 -44
  7. package/dist/api/batch-completion.js +2 -1
  8. package/dist/api/calc-tokens.d.ts +30 -0
  9. package/dist/api/calc-tokens.js +41 -0
  10. package/dist/api/classify.d.ts +5 -2
  11. package/dist/api/classify.js +5 -2
  12. package/dist/api/completion-stream.d.ts +1 -1
  13. package/dist/api/completion-stream.js +2 -1
  14. package/dist/api/delete-cache.d.ts +7 -1
  15. package/dist/api/delete-cache.js +11 -1
  16. package/dist/api/diffusion.d.ts +14 -1
  17. package/dist/api/diffusion.js +14 -1
  18. package/dist/api/embed.d.ts +82 -29
  19. package/dist/api/embed.js +64 -18
  20. package/dist/api/get-model-info.d.ts +1 -1
  21. package/dist/api/index.d.ts +2 -1
  22. package/dist/api/index.js +2 -1
  23. package/dist/api/rag.d.ts +1 -1
  24. package/dist/api/rag.js +1 -1
  25. package/dist/api/registry.d.ts +16 -6
  26. package/dist/api/registry.js +32 -9
  27. package/dist/api/text-to-speech.d.ts +14 -5
  28. package/dist/api/text-to-speech.js +83 -29
  29. package/dist/api/transcribe.d.ts +6 -2
  30. package/dist/api/transcribe.js +7 -1
  31. package/dist/api/vector-index.d.ts +25 -3
  32. package/dist/api/vector-index.js +17 -2
  33. package/dist/api/video-request.d.ts +15 -15
  34. package/dist/dispatch.d.ts +10 -4
  35. package/dist/dispatch.js +65 -15
  36. package/dist/engine.d.ts +1 -1
  37. package/dist/errors/index.d.ts +70 -0
  38. package/dist/errors/index.js +128 -0
  39. package/dist/handlers/assess-model-fit.d.ts +3 -21
  40. package/dist/handlers/assess-model-fit.js +4 -303
  41. package/dist/handlers/cancelHandler.d.ts +10 -9
  42. package/dist/handlers/cancelHandler.js +17 -13
  43. package/dist/handlers/completion-orchestrate.d.ts +14 -2
  44. package/dist/handlers/completion-orchestrate.js +164 -22
  45. package/dist/handlers/delete-cache.d.ts +2 -1
  46. package/dist/handlers/delete-cache.js +6 -1
  47. package/dist/handlers/download-asset.js +11 -4
  48. package/dist/handlers/get-loaded-model-info.js +1 -0
  49. package/dist/handlers/get-model-info.js +71 -2
  50. package/dist/handlers/load-model/coreml-sidecars.d.ts +2 -13
  51. package/dist/handlers/load-model/coreml-sidecars.js +16 -119
  52. package/dist/handlers/load-model/download-lock.d.ts +20 -0
  53. package/dist/handlers/load-model/download-lock.js +213 -0
  54. package/dist/handlers/load-model/download-manager.d.ts +10 -2
  55. package/dist/handlers/load-model/download-manager.js +62 -11
  56. package/dist/handlers/load-model/handler.js +88 -35
  57. package/dist/handlers/load-model/http.js +327 -205
  58. package/dist/handlers/load-model/hyperdrive.d.ts +15 -0
  59. package/dist/handlers/load-model/hyperdrive.js +123 -50
  60. package/dist/handlers/load-model/registry-client-options.d.ts +2 -0
  61. package/dist/handlers/load-model/registry-client-options.js +4 -2
  62. package/dist/handlers/load-model/registry-companion-set.d.ts +5 -0
  63. package/dist/handlers/load-model/registry-companion-set.js +19 -1
  64. package/dist/handlers/load-model/registry-delete.d.ts +5 -0
  65. package/dist/handlers/load-model/registry-delete.js +269 -0
  66. package/dist/handlers/load-model/registry-download-utils.d.ts +4 -2
  67. package/dist/handlers/load-model/registry-download-utils.js +93 -22
  68. package/dist/handlers/load-model/registry.d.ts +20 -0
  69. package/dist/handlers/load-model/registry.js +308 -175
  70. package/dist/handlers/load-model/resolve.js +4 -1
  71. package/dist/handlers/load-model/types.d.ts +2 -0
  72. package/dist/handlers/logging-stream.js +17 -5
  73. package/dist/handlers/plugin-invoke.js +152 -39
  74. package/dist/handlers/registry.d.ts +2 -2
  75. package/dist/handlers/registry.js +105 -28
  76. package/dist/handlers/rpc-server.js +7 -7
  77. package/dist/handlers/session.d.ts +11 -0
  78. package/dist/handlers/session.js +40 -0
  79. package/dist/handlers/unload-model.js +8 -3
  80. package/dist/handlers/vector-index.js +118 -19
  81. package/dist/index.d.ts +1 -1
  82. package/dist/index.js +1 -1
  83. package/dist/logging/utils.js +10 -2
  84. package/dist/model-cache-layout.d.ts +1 -0
  85. package/dist/model-cache-layout.js +2 -0
  86. package/dist/model-fit/assess.d.ts +18 -67
  87. package/dist/model-fit/assess.js +96 -665
  88. package/dist/model-fit/describe.d.ts +12 -0
  89. package/dist/model-fit/describe.js +29 -0
  90. package/dist/model-fit/description/fetch.d.ts +1 -1
  91. package/dist/model-fit/description/fetch.js +95 -16
  92. package/dist/model-fit/description/resolve-load.d.ts +5 -3
  93. package/dist/model-fit/description/resolve-load.js +14 -14
  94. package/dist/model-fit/fit-load.d.ts +23 -0
  95. package/dist/model-fit/fit-load.js +29 -0
  96. package/dist/model-fit/load-check.d.ts +15 -33
  97. package/dist/model-fit/load-check.js +50 -195
  98. package/dist/model-fit/run/engine.d.ts +12 -72
  99. package/dist/model-fit/run/engine.js +4 -8
  100. package/dist/model-fit/run/run.d.ts +10 -7
  101. package/dist/model-fit/run/run.js +22 -18
  102. package/dist/model-fit/sum.d.ts +32 -0
  103. package/dist/model-fit/sum.js +60 -0
  104. package/dist/models/registry/models.d.ts +865 -128
  105. package/dist/models/registry/models.js +3737 -2963
  106. package/dist/plugins/builtin/asr-ggml/config.d.ts +19 -4
  107. package/dist/plugins/builtin/asr-ggml/config.js +39 -3
  108. package/dist/plugins/builtin/asr-ggml/fit.d.ts +18 -0
  109. package/dist/{model-fit/request/engines/asr.js → plugins/builtin/asr-ggml/fit.js} +55 -71
  110. package/dist/plugins/builtin/audiogen-ggml/config.d.ts +6 -0
  111. package/dist/plugins/builtin/audiogen-ggml/config.js +9 -0
  112. package/dist/plugins/builtin/audiogen-ggml/fit.d.ts +3 -0
  113. package/dist/{model-fit/request/engines/audiogen.js → plugins/builtin/audiogen-ggml/fit.js} +28 -18
  114. package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.d.ts +3 -1
  115. package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.js +2 -1
  116. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-encode.d.ts +17 -0
  117. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-encode.js +68 -0
  118. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.d.ts +11 -3
  119. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.js +47 -11
  120. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.d.ts +3 -1
  121. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.js +2 -1
  122. package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.d.ts +3 -1
  123. package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.js +2 -1
  124. package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +143 -18
  125. package/dist/plugins/builtin/audiogen-ggml/plugin.js +13 -1
  126. package/dist/plugins/builtin/bci-whispercpp-transcription/fit.d.ts +3 -0
  127. package/dist/plugins/builtin/bci-whispercpp-transcription/fit.js +22 -0
  128. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +45 -4
  129. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +8 -6
  130. package/dist/plugins/builtin/ggml-classification/plugin.d.ts +4 -1
  131. package/dist/plugins/builtin/ggml-classification/plugin.js +88 -8
  132. package/dist/plugins/builtin/ggml-image-embedding/ops/embed.d.ts +24 -0
  133. package/dist/plugins/builtin/ggml-image-embedding/ops/embed.js +184 -0
  134. package/dist/plugins/builtin/ggml-image-embedding/plugin.d.ts +120 -0
  135. package/dist/plugins/builtin/ggml-image-embedding/plugin.js +39 -0
  136. package/dist/plugins/builtin/ggml-ocr/fit.d.ts +6 -0
  137. package/dist/plugins/builtin/ggml-ocr/fit.js +19 -0
  138. package/dist/plugins/builtin/ggml-ocr/params.d.ts +1 -1
  139. package/dist/plugins/builtin/ggml-ocr/plugin.d.ts +9 -3
  140. package/dist/plugins/builtin/ggml-ocr/plugin.js +107 -21
  141. package/dist/plugins/builtin/ggml-vla/ops/vla-run.d.ts +2 -2
  142. package/dist/plugins/builtin/ggml-vla/ops/vla-run.js +18 -2
  143. package/dist/plugins/builtin/ggml-vla/plugin.d.ts +3 -3
  144. package/dist/plugins/builtin/index.d.ts +1 -0
  145. package/dist/plugins/builtin/index.js +1 -0
  146. package/dist/plugins/builtin/llamacpp-completion/fit.d.ts +3 -0
  147. package/dist/plugins/builtin/llamacpp-completion/fit.js +28 -0
  148. package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +1 -1
  149. package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.js +15 -4
  150. package/dist/plugins/builtin/llamacpp-completion/ops/calc-tokens.d.ts +2 -0
  151. package/dist/plugins/builtin/llamacpp-completion/ops/calc-tokens.js +32 -0
  152. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.d.ts +1 -1
  153. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.d.ts +17 -3
  154. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +61 -5
  155. package/dist/plugins/builtin/llamacpp-completion/ops/finetune.d.ts +5 -4
  156. package/dist/plugins/builtin/llamacpp-completion/ops/finetune.js +54 -19
  157. package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +148 -19
  158. package/dist/plugins/builtin/llamacpp-completion/plugin.js +26 -9
  159. package/dist/plugins/builtin/llamacpp-decision/ops/decide.d.ts +3 -1
  160. package/dist/plugins/builtin/llamacpp-decision/ops/decide.js +5 -2
  161. package/dist/plugins/builtin/llamacpp-decision/plugin.d.ts +4 -2
  162. package/dist/plugins/builtin/llamacpp-decision/plugin.js +2 -2
  163. package/dist/plugins/builtin/llamacpp-embedding/fit.d.ts +3 -0
  164. package/dist/plugins/builtin/llamacpp-embedding/fit.js +23 -0
  165. package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +194 -4
  166. package/dist/plugins/builtin/llamacpp-embedding/plugin.js +37 -11
  167. package/dist/plugins/builtin/llamacpp-embedding/transform.d.ts +2 -2
  168. package/dist/plugins/builtin/llamacpp-embedding/transform.js +23 -2
  169. package/dist/plugins/builtin/llamacpp-rerank/ops/rerank.d.ts +3 -1
  170. package/dist/plugins/builtin/llamacpp-rerank/ops/rerank.js +5 -2
  171. package/dist/plugins/builtin/llamacpp-rerank/plugin.d.ts +6 -5
  172. package/dist/plugins/builtin/llamacpp-rerank/plugin.js +2 -3
  173. package/dist/plugins/builtin/moss-transcribe/plugin.d.ts +30 -3
  174. package/dist/plugins/builtin/moss-transcribe/plugin.js +5 -3
  175. package/dist/plugins/builtin/nmtcpp-translation/fit.d.ts +3 -0
  176. package/dist/plugins/builtin/nmtcpp-translation/fit.js +11 -0
  177. package/dist/plugins/builtin/nmtcpp-translation/plugin.d.ts +14 -5
  178. package/dist/plugins/builtin/nmtcpp-translation/plugin.js +4 -2
  179. package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +205 -9
  180. package/dist/plugins/builtin/parakeet-transcription/plugin.js +39 -13
  181. package/dist/plugins/builtin/sdcpp-generation/fit.d.ts +3 -0
  182. package/dist/plugins/builtin/sdcpp-generation/fit.js +68 -0
  183. package/dist/plugins/builtin/sdcpp-generation/load-files.d.ts +30 -0
  184. package/dist/plugins/builtin/sdcpp-generation/load-files.js +121 -0
  185. package/dist/plugins/builtin/sdcpp-generation/ops/diffusion.d.ts +4 -1
  186. package/dist/plugins/builtin/sdcpp-generation/ops/diffusion.js +8 -3
  187. package/dist/plugins/builtin/sdcpp-generation/ops/upscale.d.ts +4 -1
  188. package/dist/plugins/builtin/sdcpp-generation/ops/upscale.js +107 -22
  189. package/dist/plugins/builtin/sdcpp-generation/ops/video.d.ts +3 -1
  190. package/dist/plugins/builtin/sdcpp-generation/ops/video.js +4 -1
  191. package/dist/plugins/builtin/sdcpp-generation/ops/world.d.ts +4 -2
  192. package/dist/plugins/builtin/sdcpp-generation/ops/world.js +7 -2
  193. package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +79 -16
  194. package/dist/plugins/builtin/sdcpp-generation/plugin.js +29 -117
  195. package/dist/plugins/builtin/tts-ggml/fit.d.ts +3 -0
  196. package/dist/plugins/builtin/tts-ggml/fit.js +162 -0
  197. package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.d.ts +2 -1
  198. package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.js +4 -1
  199. package/dist/plugins/builtin/tts-ggml/ops/moss-options.d.ts +1 -1
  200. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.d.ts +2 -1
  201. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.js +22 -9
  202. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.d.ts +2 -1
  203. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +51 -16
  204. package/dist/plugins/builtin/tts-ggml/ops/voice-options.d.ts +50 -0
  205. package/dist/plugins/builtin/tts-ggml/ops/voice-options.js +74 -0
  206. package/dist/plugins/builtin/tts-ggml/plugin.d.ts +368 -25
  207. package/dist/plugins/builtin/tts-ggml/plugin.js +180 -31
  208. package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +70 -7
  209. package/dist/plugins/builtin/whispercpp-transcription/plugin.js +8 -6
  210. package/dist/plugins/ops/bci-transcribe.d.ts +5 -4
  211. package/dist/plugins/ops/bci-transcribe.js +18 -4
  212. package/dist/plugins/ops/cancel.d.ts +1 -0
  213. package/dist/plugins/ops/cancel.js +11 -2
  214. package/dist/plugins/ops/embed.d.ts +5 -3
  215. package/dist/plugins/ops/embed.js +89 -34
  216. package/dist/plugins/ops/kv-cache-utils.d.ts +1 -0
  217. package/dist/plugins/ops/kv-cache-utils.js +6 -2
  218. package/dist/plugins/ops/load-model.d.ts +2 -0
  219. package/dist/plugins/ops/load-model.js +33 -16
  220. package/dist/plugins/ops/transcribe.d.ts +5 -4
  221. package/dist/plugins/ops/transcribe.js +62 -27
  222. package/dist/plugins/ops/translate.d.ts +2 -1
  223. package/dist/plugins/ops/translate.js +4 -1
  224. package/dist/plugins/ops/unload-model.d.ts +17 -1
  225. package/dist/plugins/ops/unload-model.js +31 -2
  226. package/dist/plugins/turbovec-backend.d.ts +1 -1
  227. package/dist/plugins/turbovec-backend.js +16 -1
  228. package/dist/profiling/operation-metrics.js +22 -19
  229. package/dist/rag/embedding-scope.d.ts +16 -0
  230. package/dist/rag/embedding-scope.js +81 -0
  231. package/dist/rag/handler.d.ts +25 -0
  232. package/dist/rag/handler.js +50 -16
  233. package/dist/rag/handlers/ingest.js +2 -6
  234. package/dist/rag/handlers/search.d.ts +10 -1
  235. package/dist/rag/handlers/search.js +16 -7
  236. package/dist/rag/rag-operation-manager.d.ts +27 -3
  237. package/dist/rag/rag-operation-manager.js +10 -27
  238. package/dist/registry.js +5 -0
  239. package/dist/resources/collector.d.ts +70 -36
  240. package/dist/resources/collector.js +8 -1
  241. package/dist/resources/types.d.ts +2 -0
  242. package/dist/rpc/inventory.js +1 -12
  243. package/dist/runtime/env.d.ts +5 -1
  244. package/dist/runtime/env.js +6 -21
  245. package/dist/runtime/index.d.ts +4 -1
  246. package/dist/runtime/index.js +2 -0
  247. package/dist/runtime/lifecycle.d.ts +6 -4
  248. package/dist/runtime/lifecycle.js +9 -5
  249. package/dist/runtime/model-config-utils.js +5 -1
  250. package/dist/runtime/model-registry.d.ts +28 -4
  251. package/dist/runtime/model-registry.js +46 -1
  252. package/dist/runtime/model-work-lock.d.ts +13 -0
  253. package/dist/runtime/model-work-lock.js +48 -0
  254. package/dist/runtime/registry-client.d.ts +47 -1
  255. package/dist/runtime/registry-client.js +446 -88
  256. package/dist/runtime/request-context.d.ts +9 -1
  257. package/dist/runtime/request-context.js +25 -1
  258. package/dist/runtime/request-registry.d.ts +55 -7
  259. package/dist/runtime/request-registry.js +189 -71
  260. package/dist/runtime/runtime-lifecycle.js +10 -1
  261. package/dist/runtime/session-id.d.ts +18 -0
  262. package/dist/runtime/session-id.js +20 -0
  263. package/dist/runtime/session-registry.d.ts +39 -0
  264. package/dist/runtime/session-registry.js +114 -0
  265. package/dist/runtime/vector-index-backend.d.ts +4 -0
  266. package/dist/runtime/vector-index-registry.d.ts +21 -0
  267. package/dist/runtime/vector-index-registry.js +56 -18
  268. package/dist/schemas/abort-signal.d.ts +8 -0
  269. package/dist/schemas/abort-signal.js +1 -0
  270. package/dist/schemas/assess-model-fit.d.ts +132 -282
  271. package/dist/schemas/assess-model-fit.js +82 -216
  272. package/dist/schemas/audio-gen.d.ts +278 -24
  273. package/dist/schemas/audio-gen.js +127 -23
  274. package/dist/schemas/batch-completion-stream.d.ts +105 -13
  275. package/dist/schemas/batch-completion-stream.js +8 -4
  276. package/dist/schemas/bci-config.d.ts +2 -1
  277. package/dist/schemas/bci.d.ts +54 -3
  278. package/dist/schemas/bci.js +11 -16
  279. package/dist/schemas/calc-tokens.d.ts +141 -0
  280. package/dist/schemas/calc-tokens.js +53 -0
  281. package/dist/schemas/cancel.d.ts +26 -0
  282. package/dist/schemas/cancel.js +10 -2
  283. package/dist/schemas/classification.d.ts +3 -0
  284. package/dist/schemas/classification.js +6 -2
  285. package/dist/schemas/common.d.ts +1831 -348
  286. package/dist/schemas/common.js +18 -2
  287. package/dist/schemas/completion-event.d.ts +3 -0
  288. package/dist/schemas/completion-event.js +1 -1
  289. package/dist/schemas/completion-stream.d.ts +162 -17
  290. package/dist/schemas/completion-stream.js +83 -13
  291. package/dist/schemas/config.d.ts +1212 -169
  292. package/dist/schemas/config.js +9 -13
  293. package/dist/schemas/decision.d.ts +4 -2
  294. package/dist/schemas/decision.js +2 -1
  295. package/dist/schemas/download-asset.d.ts +10 -5
  296. package/dist/schemas/download-asset.js +3 -6
  297. package/dist/schemas/embed.d.ts +286 -3
  298. package/dist/schemas/embed.js +162 -16
  299. package/dist/schemas/engine-addon-map.d.ts +1 -0
  300. package/dist/schemas/engine-addon-map.js +1 -0
  301. package/dist/schemas/errors.d.ts +12 -0
  302. package/dist/schemas/errors.js +66 -1
  303. package/dist/schemas/finetune.d.ts +14 -4
  304. package/dist/schemas/finetune.js +5 -6
  305. package/dist/schemas/heartbeat.d.ts +1 -0
  306. package/dist/schemas/heartbeat.js +5 -2
  307. package/dist/schemas/image-embedding.d.ts +27 -0
  308. package/dist/schemas/image-embedding.js +107 -0
  309. package/dist/schemas/index.d.ts +7 -3
  310. package/dist/schemas/index.js +7 -3
  311. package/dist/schemas/lifecycle.d.ts +3 -0
  312. package/dist/schemas/lifecycle.js +13 -6
  313. package/dist/schemas/llamacpp-config.d.ts +269 -22
  314. package/dist/schemas/llamacpp-config.js +86 -7
  315. package/dist/schemas/load-model.d.ts +6418 -1960
  316. package/dist/schemas/load-model.js +39 -8
  317. package/dist/schemas/logging-stream.d.ts +1 -0
  318. package/dist/schemas/logging-stream.js +5 -2
  319. package/dist/schemas/model-config-schemas.d.ts +542 -116
  320. package/dist/schemas/model-config-schemas.js +2 -0
  321. package/dist/schemas/model-fit-policy.d.ts +2 -2
  322. package/dist/schemas/model-info.d.ts +54 -56
  323. package/dist/schemas/model-info.js +17 -6
  324. package/dist/schemas/model-ops.d.ts +12 -0
  325. package/dist/schemas/model-ops.js +33 -5
  326. package/dist/schemas/model-src-utils.d.ts +10 -6
  327. package/dist/schemas/model-types.d.ts +8 -4
  328. package/dist/schemas/model-types.js +1 -0
  329. package/dist/schemas/ocr.d.ts +9 -4
  330. package/dist/schemas/ocr.js +6 -2
  331. package/dist/schemas/plugin.d.ts +62 -6
  332. package/dist/schemas/plugin.js +26 -4
  333. package/dist/schemas/profiling.d.ts +32 -32
  334. package/dist/schemas/rag.d.ts +9 -0
  335. package/dist/schemas/rag.js +10 -16
  336. package/dist/schemas/registry.d.ts +30 -10
  337. package/dist/schemas/registry.js +14 -6
  338. package/dist/schemas/reload-config.d.ts +41 -6
  339. package/dist/schemas/reload-config.js +11 -4
  340. package/dist/schemas/request-base.d.ts +39 -0
  341. package/dist/schemas/request-base.js +64 -0
  342. package/dist/schemas/rerank.d.ts +8 -7
  343. package/dist/schemas/rerank.js +6 -4
  344. package/dist/schemas/rpc-server.d.ts +3 -0
  345. package/dist/schemas/rpc-server.js +15 -15
  346. package/dist/schemas/sdcpp-config.d.ts +90 -20
  347. package/dist/schemas/sdcpp-config.js +96 -19
  348. package/dist/schemas/session.d.ts +37 -0
  349. package/dist/schemas/session.js +53 -0
  350. package/dist/schemas/system-resources.d.ts +364 -179
  351. package/dist/schemas/system-resources.js +8 -3
  352. package/dist/schemas/text-to-speech.d.ts +1343 -271
  353. package/dist/schemas/text-to-speech.js +547 -33
  354. package/dist/schemas/tools.d.ts +7 -0
  355. package/dist/schemas/tools.js +29 -0
  356. package/dist/schemas/transcription-config.d.ts +262 -2
  357. package/dist/schemas/transcription-config.js +122 -6
  358. package/dist/schemas/transcription.d.ts +93 -6
  359. package/dist/schemas/transcription.js +98 -21
  360. package/dist/schemas/translate.d.ts +2 -0
  361. package/dist/schemas/translate.js +5 -9
  362. package/dist/schemas/translation-config.d.ts +30 -20
  363. package/dist/schemas/tts-pcm.d.ts +35 -0
  364. package/dist/schemas/tts-pcm.js +69 -0
  365. package/dist/schemas/vector-index.d.ts +47 -1
  366. package/dist/schemas/vector-index.js +37 -10
  367. package/dist/schemas/vla.d.ts +3 -3
  368. package/dist/surface.d.ts +3 -3
  369. package/dist/surface.js +3 -3
  370. package/dist/utils/addon-responses.d.ts +69 -2
  371. package/dist/utils/archive.d.ts +7 -0
  372. package/dist/utils/archive.js +88 -29
  373. package/dist/utils/asr-events.d.ts +5 -0
  374. package/dist/utils/asr-events.js +10 -1
  375. package/dist/utils/cache/auto-history.js +3 -1
  376. package/dist/utils/cache/layout.d.ts +48 -0
  377. package/dist/utils/cache/layout.js +190 -0
  378. package/dist/utils/cache/paths.js +2 -4
  379. package/dist/utils/cache/types.d.ts +2 -1
  380. package/dist/utils/checksum.d.ts +5 -0
  381. package/dist/utils/checksum.js +18 -0
  382. package/dist/utils/completion-normalizer.js +286 -87
  383. package/dist/utils/gguf-tensor-extractor.d.ts +0 -6
  384. package/dist/utils/gguf-tensor-extractor.js +1 -50
  385. package/dist/utils/shard-utils.d.ts +0 -21
  386. package/dist/utils/shard-utils.js +12 -62
  387. package/dist/utils/tool-integration.d.ts +2 -2
  388. package/dist/utils/tools/defer.d.ts +39 -10
  389. package/dist/utils/tools/defer.js +72 -17
  390. package/dist/utils/tools/index.d.ts +1 -1
  391. package/dist/utils/tools/index.js +1 -1
  392. package/dist/utils/transcribe-metadata.d.ts +28 -2
  393. package/dist/utils/transcribe-metadata.js +16 -0
  394. package/dist/utils/tts-stats.d.ts +42 -0
  395. package/dist/utils/tts-stats.js +120 -22
  396. package/package.json +43 -39
  397. package/dist/model-fit/classify.d.ts +0 -6
  398. package/dist/model-fit/classify.js +0 -166
  399. package/dist/model-fit/description/project.d.ts +0 -27
  400. package/dist/model-fit/description/project.js +0 -33
  401. package/dist/model-fit/floor.d.ts +0 -31
  402. package/dist/model-fit/floor.js +0 -65
  403. package/dist/model-fit/kv-cache.d.ts +0 -23
  404. package/dist/model-fit/kv-cache.js +0 -85
  405. package/dist/model-fit/platform.d.ts +0 -14
  406. package/dist/model-fit/platform.js +0 -26
  407. package/dist/model-fit/request/create.d.ts +0 -31
  408. package/dist/model-fit/request/create.js +0 -61
  409. package/dist/model-fit/request/engines/asr.d.ts +0 -14
  410. package/dist/model-fit/request/engines/audiogen.d.ts +0 -8
  411. package/dist/model-fit/request/engines/diffusion.d.ts +0 -7
  412. package/dist/model-fit/request/engines/diffusion.js +0 -93
  413. package/dist/model-fit/request/engines/llama.d.ts +0 -12
  414. package/dist/model-fit/request/engines/llama.js +0 -44
  415. package/dist/model-fit/request/engines/nmt.d.ts +0 -8
  416. package/dist/model-fit/request/engines/nmt.js +0 -11
  417. package/dist/model-fit/request/engines/ocr.d.ts +0 -9
  418. package/dist/model-fit/request/engines/ocr.js +0 -13
  419. package/dist/model-fit/request/engines/tts.d.ts +0 -10
  420. package/dist/model-fit/request/engines/tts.js +0 -152
  421. package/dist/models/registry/resource-profiles.d.ts +0 -17
  422. package/dist/models/registry/resource-profiles.js +0 -5859
  423. package/dist/schemas/model-resource-profile.d.ts +0 -109
  424. package/dist/schemas/model-resource-profile.js +0 -80
  425. /package/dist/model-fit/{request/gpu.d.ts → gpu.d.ts} +0 -0
  426. /package/dist/model-fit/{request/gpu.js → gpu.js} +0 -0
@@ -1,13 +1,66 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
1
53
  import fs, { promises as fsPromises } from 'bare-fs';
2
54
  import path from 'bare-path';
3
55
  import { Readable } from 'bare-stream';
4
56
  import { AbortController } from 'bare-abort-controller';
5
- import { getModelsCacheDir, getShardedModelCacheDir, generateShortHash, detectShardedModel, parsePatternBasedShardUrl, extractTensorsFromShards, calculatePercentage, isArchiveUrl, sanitizePathComponent, extractAndValidateShardedArchive, validateShardedModelCache, checkAllShardsExist, generateShardFilenames, hasValidGGUFHeader } from '../../utils/index.js';
57
+ import { getModelsCacheDir, getShardedModelCacheDir, generateShortHash, detectShardedModel, parsePatternBasedShardUrl, calculatePercentage, isArchiveUrl, sanitizePathComponent, extractAndValidateShardedArchive, isShardedExtractionComplete, generateShardFilenames, hasValidGGUFHeader } from '../../utils/index.js';
6
58
  import { safeFetch } from '../../handlers/load-model/safe-fetch.js';
7
59
  import { shouldEnforceSecureTransport, verifyHttpModelFile } from '../../handlers/load-model/http-verify.js';
8
60
  import { getConfig } from '../../runtime/state.js';
9
61
  import { getLifecycleState, onResume } from '../../runtime/runtime-lifecycle.js';
10
62
  import { createHttpDownloadKey, startOrJoinDownload, applyJoinedDownloadStats } from '../../handlers/load-model/download-manager.js';
63
+ import { acquireDownloadLock, partialDownloadPath, publishDownload } from '../../handlers/load-model/download-lock.js';
11
64
  import { ChecksumUnavailableError, ChecksumValidationFailedError, DownloadCancelledError, HTTPError, InsecureModelSourceError, NoResponseBodyError, PartialDownloadOfflineError, ResponseBodyNotReadableError } from '../../errors/index.js';
12
65
  import { getEngineLogger } from '../../logging/index.js';
13
66
  const logger = getEngineLogger();
@@ -41,9 +94,18 @@ function extractFilenameFromUrl(url) {
41
94
  // cache entry from before this shipped, or one written down the warn-and-proceed
42
95
  // path, is served as-is: tightening requireHttpChecksum does not retroactively
43
96
  // verify the cache — clear it to force a fresh, verified download.
97
+ //
98
+ // Downloads land here only once verified (see `publishDownload`), but a partial
99
+ // an older SDK wrote in place can still pass the offline header check.
44
100
  async function validateCachedFile(modelPath, url, requireSecureTransport, signal) {
45
101
  try {
46
102
  await fsPromises.access(modelPath);
103
+ }
104
+ catch {
105
+ await assertPartialCanResume(modelPath, url, requireSecureTransport, signal);
106
+ return null;
107
+ }
108
+ try {
47
109
  const localStats = await fsPromises.stat(modelPath);
48
110
  const localSize = localStats.size;
49
111
  const config = getConfig();
@@ -93,6 +155,28 @@ async function validateCachedFile(modelPath, url, requireSecureTransport, signal
93
155
  return null;
94
156
  }
95
157
  }
158
+ // Only the `.part` of an interrupted download is on disk. Resuming it needs the
159
+ // network, so offline this fails as an offline partial instead of retrying.
160
+ async function assertPartialCanResume(modelPath, url, requireSecureTransport, signal) {
161
+ const partialSize = await fsPromises.stat(partialDownloadPath(modelPath)).then((stats) => stats.size, () => 0);
162
+ if (partialSize === 0)
163
+ return;
164
+ try {
165
+ const response = await safeFetch(url, {
166
+ method: 'HEAD',
167
+ timeoutMs: getConfig().httpConnectionTimeoutMs ?? DEFAULT_HTTP_CONNECTION_TIMEOUT_MS,
168
+ enforceSecureTransport: shouldEnforceSecureTransport(url, requireSecureTransport),
169
+ ...(signal && { signal })
170
+ });
171
+ response.body.resume();
172
+ }
173
+ catch (error) {
174
+ if (signal?.aborted || error instanceof InsecureModelSourceError)
175
+ throw error;
176
+ logger.error(`❌ Offline with partial download (${partialSize} bytes). Cannot resume without network.`);
177
+ throw new PartialDownloadOfflineError(url, partialSize);
178
+ }
179
+ }
96
180
  async function performHttpDownload(url, modelPath, downloadKey, requireSecureTransport, progressCallback, signal) {
97
181
  if (signal?.aborted) {
98
182
  throw new DownloadCancelledError();
@@ -199,6 +283,7 @@ async function performHttpDownload(url, modelPath, downloadKey, requireSecureTra
199
283
  // Create write stream (append if resuming)
200
284
  const writeStreamOptions = startOffset > 0 && response.status === 206 ? { flags: 'a' } : {};
201
285
  const writeStream = fs.createWriteStream(modelPath, writeStreamOptions);
286
+ const writeStreamClosed = new Promise((resolve) => writeStream.once('close', resolve));
202
287
  // Get the response body
203
288
  const body = response.body;
204
289
  if (!body) {
@@ -283,7 +368,9 @@ async function performHttpDownload(url, modelPath, downloadKey, requireSecureTra
283
368
  }
284
369
  }
285
370
  catch (error) {
371
+ // Let an in-flight write land before the caller can release its lock.
286
372
  writeStream.destroy();
373
+ await writeStreamClosed;
287
374
  logger.error('Error during download:', error instanceof Error ? error.message : String(error));
288
375
  throw error instanceof Error ? error : new Error(String(error));
289
376
  }
@@ -384,6 +471,13 @@ async function performHttpDownloadWithResume(url, modelPath, downloadKey, requir
384
471
  }
385
472
  }
386
473
  }
474
+ /** Download to the partial path, resuming it, then verify and publish. Callers hold the lock. */
475
+ async function downloadVerifyAndPublish(url, finalPath, downloadKey, requireSecureTransport, requireChecksum, progressCallback, signal, hooks) {
476
+ const partialPath = partialDownloadPath(finalPath);
477
+ const meta = await performHttpDownloadWithResume(url, partialPath, downloadKey, requireSecureTransport, progressCallback, signal);
478
+ await verifyHttpModelFile(url, partialPath, meta.hubSha256, requireChecksum, hooks);
479
+ await publishDownload(finalPath);
480
+ }
387
481
  export async function downloadModelFromHttp(url, progressCallback, hooks, security) {
388
482
  const filename = extractFilenameFromUrl(url);
389
483
  if (isArchiveUrl(url)) {
@@ -400,14 +494,39 @@ export async function downloadModelFromHttp(url, progressCallback, hooks, securi
400
494
  const modelPath = `${cacheDir}/${sourceHash}_${filename}`;
401
495
  const { requireChecksum, requireSecureTransport } = resolveDownloadSecurity(security);
402
496
  const result = startOrJoinDownload(downloadKey, async (ctx) => {
497
+ const env_1 = { stack: [], error: void 0, hasError: false };
403
498
  try {
404
- // Check if already cached
405
- const cached = await validateCachedFile(modelPath, url, requireSecureTransport, ctx.signal);
406
- if (cached) {
407
- hooks?.markCacheHit?.();
408
- ctx.setCacheHit(true);
499
+ const _downloadLock = __addDisposableResource(env_1, await acquireDownloadLock(downloadKey, ctx.signal), true);
500
+ try {
501
+ // Check if already cached
502
+ const cached = await validateCachedFile(modelPath, url, requireSecureTransport, ctx.signal);
503
+ if (cached) {
504
+ hooks?.markCacheHit?.();
505
+ ctx.setCacheHit(true);
506
+ try {
507
+ const stats = await fsPromises.stat(cached);
508
+ ctx.broadcastProgress({
509
+ type: 'modelProgress',
510
+ downloaded: stats.size,
511
+ total: stats.size,
512
+ percentage: 100,
513
+ downloadKey
514
+ });
515
+ }
516
+ catch (error) {
517
+ logger.debug('Failed to get file stats for progress callback', {
518
+ path: cached,
519
+ error
520
+ });
521
+ }
522
+ return cached;
523
+ }
524
+ // Download the file
525
+ hooks?.markCacheMiss?.();
526
+ ctx.setCacheHit(false);
527
+ await downloadVerifyAndPublish(url, modelPath, downloadKey, requireSecureTransport, requireChecksum, ctx.broadcastProgress, ctx.signal, hooks);
409
528
  try {
410
- const stats = await fsPromises.stat(cached);
529
+ const stats = await fsPromises.stat(modelPath);
411
530
  ctx.broadcastProgress({
412
531
  type: 'modelProgress',
413
532
  downloaded: stats.size,
@@ -417,59 +536,47 @@ export async function downloadModelFromHttp(url, progressCallback, hooks, securi
417
536
  });
418
537
  }
419
538
  catch (error) {
420
- logger.debug('Failed to get file stats for progress callback', {
421
- path: cached,
539
+ logger.debug('Failed to get file stats for final progress update', {
540
+ path: modelPath,
422
541
  error
423
542
  });
424
543
  }
425
- return cached;
426
- }
427
- // Download the file
428
- hooks?.markCacheMiss?.();
429
- ctx.setCacheHit(false);
430
- const meta = await performHttpDownloadWithResume(url, modelPath, downloadKey, requireSecureTransport, ctx.broadcastProgress, ctx.signal);
431
- await verifyHttpModelFile(url, modelPath, meta.hubSha256, requireChecksum, hooks);
432
- try {
433
- const stats = await fsPromises.stat(modelPath);
434
- ctx.broadcastProgress({
435
- type: 'modelProgress',
436
- downloaded: stats.size,
437
- total: stats.size,
438
- percentage: 100,
439
- downloadKey
440
- });
544
+ return modelPath;
441
545
  }
442
546
  catch (error) {
443
- logger.debug('Failed to get file stats for final progress update', {
444
- path: modelPath,
445
- error
446
- });
447
- }
448
- return modelPath;
449
- }
450
- catch (error) {
451
- logger.error('❌ Error downloading model:', error instanceof Error ? error.message : String(error));
452
- // Check if we should delete the partial file (clearCache was requested)
453
- if (error instanceof DownloadCancelledError) {
454
- if (ctx.shouldClearCache()) {
455
- logger.info('🗑️ Clearing cache - deleting partial file');
456
- try {
457
- await fsPromises.unlink(modelPath);
458
- logger.info(`✅ Deleted partial file: ${modelPath}`);
547
+ logger.error('❌ Error downloading model:', error instanceof Error ? error.message : String(error));
548
+ // Check if we should delete the partial file (clearCache was requested)
549
+ if (error instanceof DownloadCancelledError) {
550
+ if (ctx.shouldClearCache()) {
551
+ logger.info('🗑️ Clearing cache - deleting partial file');
552
+ const partialPath = partialDownloadPath(modelPath);
553
+ try {
554
+ await fsPromises.unlink(partialPath);
555
+ logger.info(`✅ Deleted partial file: ${partialPath}`);
556
+ }
557
+ catch (error) {
558
+ logger.debug('Failed to delete partial file during cleanup', {
559
+ path: partialPath,
560
+ error
561
+ });
562
+ }
459
563
  }
460
- catch (error) {
461
- logger.debug('Failed to delete partial file during cleanup', {
462
- path: modelPath,
463
- error
464
- });
564
+ else {
565
+ logger.info('📥 Download paused - partial file preserved for resume');
465
566
  }
466
567
  }
467
- else {
468
- logger.info('📥 Download paused - partial file preserved for resume');
469
- }
568
+ const errorToThrow = error instanceof Error ? error : new Error(String(error));
569
+ throw errorToThrow;
470
570
  }
471
- const errorToThrow = error instanceof Error ? error : new Error(String(error));
472
- throw errorToThrow;
571
+ }
572
+ catch (e_1) {
573
+ env_1.error = e_1;
574
+ env_1.hasError = true;
575
+ }
576
+ finally {
577
+ const result_1 = __disposeResources(env_1);
578
+ if (result_1)
579
+ await result_1;
473
580
  }
474
581
  }, progressCallback, hooks?.requestBinding);
475
582
  return applyJoinedDownloadStats(result, hooks);
@@ -485,84 +592,98 @@ async function downloadShardedModelFromHttp(shardUrl, progressCallback, hooks, s
485
592
  logger.info(`📥 HTTP sharded download: ${shardInfos.length} shards detected from ${shardUrl}`);
486
593
  const shardDir = getShardedModelCacheDir(cacheKey);
487
594
  const result = startOrJoinDownload(downloadKey, async (ctx) => {
595
+ const env_2 = { stack: [], error: void 0, hasError: false };
488
596
  try {
489
- const shardStates = await Promise.all(shardInfos.map(async (shard, index) => {
490
- const shardPath = path.join(shardDir, shard.filename);
491
- let expectedSize = 0;
492
- try {
493
- const response = await safeFetch(shard.url, {
494
- method: 'HEAD',
495
- timeoutMs: connectionTimeout,
496
- enforceSecureTransport: shouldEnforceSecureTransport(shard.url, requireSecureTransport),
497
- signal: ctx.signal
498
- });
499
- response.body.resume();
500
- expectedSize = parseInt(String(response.headers['content-length'] ?? '0') || '0');
597
+ const _downloadLock = __addDisposableResource(env_2, await acquireDownloadLock(downloadKey, ctx.signal), true);
598
+ try {
599
+ // The previous holder may have cleared the cache while this one waited.
600
+ await fsPromises.mkdir(shardDir, { recursive: true });
601
+ const shardStates = await Promise.all(shardInfos.map(async (shard, index) => {
602
+ const shardPath = path.join(shardDir, shard.filename);
603
+ let expectedSize = 0;
604
+ try {
605
+ const response = await safeFetch(shard.url, {
606
+ method: 'HEAD',
607
+ timeoutMs: connectionTimeout,
608
+ enforceSecureTransport: shouldEnforceSecureTransport(shard.url, requireSecureTransport),
609
+ signal: ctx.signal
610
+ });
611
+ response.body.resume();
612
+ expectedSize = parseInt(String(response.headers['content-length'] ?? '0') || '0');
613
+ }
614
+ catch (error) {
615
+ // A refused insecure hop is terminal; fail fast instead of logging a
616
+ // misleading "failed to get shard size" and hitting it again on GET.
617
+ if (error instanceof InsecureModelSourceError)
618
+ throw error;
619
+ logger.warn('Failed to get shard size via HEAD request', {
620
+ url: shard.url,
621
+ error
622
+ });
623
+ }
624
+ return {
625
+ index,
626
+ shard,
627
+ shardPath,
628
+ expectedSize,
629
+ downloadedBytes: 0,
630
+ isComplete: false
631
+ };
632
+ }));
633
+ const overallTotal = shardStates.reduce((sum, s) => sum + s.expectedSize, 0);
634
+ logger.info(`📏 Total size: ${overallTotal} bytes (${(overallTotal / 1024 / 1024).toFixed(2)} MB)`);
635
+ const cacheChecks = await Promise.all(shardStates.map(async (state) => {
636
+ const cached = await validateCachedFile(state.shardPath, state.shard.url, requireSecureTransport, ctx.signal);
637
+ return { state, isCached: cached !== null };
638
+ }));
639
+ const shardsToDownload = cacheChecks.filter((c) => !c.isCached).map((c) => c.state);
640
+ for (const check of cacheChecks) {
641
+ if (check.isCached) {
642
+ check.state.isComplete = true;
643
+ check.state.downloadedBytes = check.state.expectedSize;
644
+ }
501
645
  }
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;
507
- logger.warn('Failed to get shard size via HEAD request', {
508
- url: shard.url,
509
- error
510
- });
646
+ logger.info(`📥 ${shardsToDownload.length} of ${shardInfos.length} shards need downloading`);
647
+ if (shardsToDownload.length === 0) {
648
+ hooks?.markCacheHit?.();
649
+ ctx.setCacheHit(true);
511
650
  }
512
- return {
513
- index,
514
- shard,
515
- shardPath,
516
- expectedSize,
517
- downloadedBytes: 0,
518
- isComplete: false
519
- };
520
- }));
521
- const overallTotal = shardStates.reduce((sum, s) => sum + s.expectedSize, 0);
522
- logger.info(`📏 Total size: ${overallTotal} bytes (${(overallTotal / 1024 / 1024).toFixed(2)} MB)`);
523
- const cacheChecks = await Promise.all(shardStates.map(async (state) => {
524
- const cached = await validateCachedFile(state.shardPath, state.shard.url, requireSecureTransport, ctx.signal);
525
- return { state, isCached: cached !== null };
526
- }));
527
- const shardsToDownload = cacheChecks.filter((c) => !c.isCached).map((c) => c.state);
528
- for (const check of cacheChecks) {
529
- if (check.isCached) {
530
- check.state.isComplete = true;
531
- check.state.downloadedBytes = check.state.expectedSize;
651
+ else {
652
+ hooks?.markCacheMiss?.();
653
+ ctx.setCacheHit(false);
532
654
  }
655
+ await downloadShardsWithConcurrency(shardsToDownload, shardStates, concurrency, ctx.signal, downloadKey, overallTotal, requireSecureTransport, ctx.broadcastProgress, requireChecksum, hooks);
656
+ logger.info(`✅ All ${shardInfos.length} shards downloaded successfully`);
657
+ return path.join(shardDir, shardInfos[0].filename);
533
658
  }
534
- logger.info(`📥 ${shardsToDownload.length} of ${shardInfos.length} shards need downloading`);
535
- if (shardsToDownload.length === 0) {
536
- hooks?.markCacheHit?.();
537
- ctx.setCacheHit(true);
538
- }
539
- else {
540
- hooks?.markCacheMiss?.();
541
- ctx.setCacheHit(false);
542
- }
543
- await downloadShardsWithConcurrency(shardsToDownload, shardStates, concurrency, ctx.signal, downloadKey, overallTotal, requireSecureTransport, ctx.broadcastProgress, requireChecksum, hooks);
544
- logger.info(`✅ All ${shardInfos.length} shards downloaded successfully`);
545
- await extractTensorsFromShards(shardDir, shardInfos[0].filename);
546
- return path.join(shardDir, shardInfos[0].filename);
547
- }
548
- catch (error) {
549
- logger.error('❌ Error during sharded download:', error instanceof Error ? error.message : String(error));
550
- if (error instanceof DownloadCancelledError) {
551
- if (ctx.shouldClearCache()) {
552
- logger.info('🗑️ Clearing cache - deleting partial shard files');
553
- try {
554
- await fsPromises.rm(shardDir, { recursive: true, force: true });
555
- logger.info(`✅ Deleted shard directory: ${shardDir}`);
556
- }
557
- catch (cleanupError) {
558
- logger.debug('Failed to delete shard directory during cleanup', {
559
- path: shardDir,
560
- error: cleanupError
561
- });
659
+ catch (error) {
660
+ logger.error('❌ Error during sharded download:', error instanceof Error ? error.message : String(error));
661
+ if (error instanceof DownloadCancelledError) {
662
+ if (ctx.shouldClearCache()) {
663
+ logger.info('🗑️ Clearing cache - deleting partial shard files');
664
+ try {
665
+ await fsPromises.rm(shardDir, { recursive: true, force: true });
666
+ logger.info(`✅ Deleted shard directory: ${shardDir}`);
667
+ }
668
+ catch (cleanupError) {
669
+ logger.debug('Failed to delete shard directory during cleanup', {
670
+ path: shardDir,
671
+ error: cleanupError
672
+ });
673
+ }
562
674
  }
563
675
  }
676
+ throw error;
564
677
  }
565
- throw error;
678
+ }
679
+ catch (e_2) {
680
+ env_2.error = e_2;
681
+ env_2.hasError = true;
682
+ }
683
+ finally {
684
+ const result_2 = __disposeResources(env_2);
685
+ if (result_2)
686
+ await result_2;
566
687
  }
567
688
  }, progressCallback, hooks?.requestBinding);
568
689
  return applyJoinedDownloadStats(result, hooks);
@@ -577,43 +698,27 @@ async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hoo
577
698
  const extractDir = getShardedModelCacheDir(sourceHash);
578
699
  const archivePath = path.join(extractDir, `${sourceHash}_${filename}`);
579
700
  const result = startOrJoinDownload(downloadKey, async (ctx) => {
701
+ const env_3 = { stack: [], error: void 0, hasError: false };
580
702
  try {
581
- await fsPromises.mkdir(extractDir, { recursive: true });
582
- const files = await fsPromises.readdir(extractDir);
583
- const shardedFile = files.find((f) => detectShardedModel(String(f)).isSharded);
584
- if (!shardedFile) {
585
- hooks?.markCacheMiss?.();
586
- ctx.setCacheHit(false);
587
- return downloadAndExtractArchive();
588
- }
589
- const shardFilename = String(shardedFile);
590
- const allShardsExist = await checkAllShardsExist(extractDir, shardFilename);
591
- if (!allShardsExist) {
592
- logger.warn(`⚠️ Incomplete shards found, re-downloading archive`);
593
- hooks?.markCacheMiss?.();
594
- ctx.setCacheHit(false);
595
- return downloadAndExtractArchive();
596
- }
597
- const shardFilenames = generateShardFilenames(shardFilename);
598
- const firstShard = path.join(extractDir, shardFilenames[0]);
599
- const isComplete = await validateShardedModelCache(extractDir, shardFilename);
600
- if (isComplete) {
601
- logger.info(`✅ Archive already extracted: ${extractDir}`);
602
- hooks?.markCacheHit?.();
603
- ctx.setCacheHit(true);
604
- ctx.broadcastProgress({
605
- type: 'modelProgress',
606
- downloaded: 1,
607
- total: 1,
608
- percentage: 100,
609
- downloadKey
610
- });
611
- return firstShard;
612
- }
613
- logger.info(`📝 All shards present but tensors.txt missing, extracting tensors...`);
703
+ const _downloadLock = __addDisposableResource(env_3, await acquireDownloadLock(downloadKey, ctx.signal), true);
614
704
  try {
615
- await extractTensorsFromShards(extractDir, shardFilename);
616
- logger.info(`✅ Tensors extracted successfully`);
705
+ await fsPromises.mkdir(extractDir, { recursive: true });
706
+ const files = await fsPromises.readdir(extractDir);
707
+ const shardedFile = files.find((f) => detectShardedModel(String(f)).isSharded);
708
+ if (!shardedFile) {
709
+ hooks?.markCacheMiss?.();
710
+ ctx.setCacheHit(false);
711
+ return await downloadAndExtractArchive();
712
+ }
713
+ const shardFilename = String(shardedFile);
714
+ if (!(await isShardedExtractionComplete(extractDir, shardFilename))) {
715
+ logger.warn(`⚠️ Incomplete extraction found, re-downloading archive`);
716
+ hooks?.markCacheMiss?.();
717
+ ctx.setCacheHit(false);
718
+ return await downloadAndExtractArchive();
719
+ }
720
+ const shardFilenames = generateShardFilenames(shardFilename);
721
+ logger.info(`✅ Archive already extracted: ${extractDir}`);
617
722
  hooks?.markCacheHit?.();
618
723
  ctx.setCacheHit(true);
619
724
  ctx.broadcastProgress({
@@ -623,57 +728,57 @@ async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hoo
623
728
  percentage: 100,
624
729
  downloadKey
625
730
  });
626
- return firstShard;
731
+ return path.join(extractDir, shardFilenames[0]);
627
732
  }
628
733
  catch (error) {
629
- logger.warn(`Failed to extract tensors, will re-download archive`, {
630
- error
631
- });
632
- hooks?.markCacheMiss?.();
633
- ctx.setCacheHit(false);
634
- return downloadAndExtractArchive();
635
- }
636
- }
637
- catch (error) {
638
- logger.error('❌ Error downloading/extracting archive:', error);
639
- if (error instanceof DownloadCancelledError) {
640
- if (ctx.shouldClearCache()) {
641
- logger.info('🗑️ Clearing cache - deleting archive extract directory');
642
- try {
643
- await fsPromises.rm(extractDir, {
644
- recursive: true,
645
- force: true
646
- });
647
- logger.info(`✅ Deleted extract directory: ${extractDir}`);
648
- }
649
- catch (cleanupError) {
650
- logger.debug('Failed to delete extract directory during cleanup', {
651
- path: extractDir,
652
- error: cleanupError
653
- });
734
+ logger.error('❌ Error downloading/extracting archive:', error);
735
+ if (error instanceof DownloadCancelledError) {
736
+ if (ctx.shouldClearCache()) {
737
+ logger.info('🗑️ Clearing cache - deleting archive extract directory');
738
+ try {
739
+ await fsPromises.rm(extractDir, {
740
+ recursive: true,
741
+ force: true
742
+ });
743
+ logger.info(`✅ Deleted extract directory: ${extractDir}`);
744
+ }
745
+ catch (cleanupError) {
746
+ logger.debug('Failed to delete extract directory during cleanup', {
747
+ path: extractDir,
748
+ error: cleanupError
749
+ });
750
+ }
654
751
  }
655
752
  }
753
+ throw error;
656
754
  }
657
- throw error;
658
- }
659
- async function downloadAndExtractArchive() {
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);
664
- logger.info(`✅ Archive downloaded, extracting to: ${extractDir}`);
665
- const firstShardPath = await extractAndValidateShardedArchive(archivePath, extractDir, ctx.signal);
666
- try {
667
- await fsPromises.unlink(archivePath);
668
- logger.info(`🗑️ Cleaned up archive file: ${archivePath}`);
669
- }
670
- catch (cleanupError) {
671
- logger.debug('Failed to delete archive file during cleanup', {
672
- path: archivePath,
673
- error: cleanupError
674
- });
755
+ async function downloadAndExtractArchive() {
756
+ // Verify the downloaded archive before extraction so tampered bytes
757
+ // never reach the extractor.
758
+ await downloadVerifyAndPublish(archiveUrl, archivePath, downloadKey, requireSecureTransport, requireChecksum, ctx.broadcastProgress, ctx.signal, hooks);
759
+ logger.info(`✅ Archive downloaded, extracting to: ${extractDir}`);
760
+ const firstShardPath = await extractAndValidateShardedArchive(archivePath, extractDir, ctx.signal);
761
+ try {
762
+ await fsPromises.unlink(archivePath);
763
+ logger.info(`🗑️ Cleaned up archive file: ${archivePath}`);
764
+ }
765
+ catch (cleanupError) {
766
+ logger.debug('Failed to delete archive file during cleanup', {
767
+ path: archivePath,
768
+ error: cleanupError
769
+ });
770
+ }
771
+ return firstShardPath;
675
772
  }
676
- return firstShardPath;
773
+ }
774
+ catch (e_3) {
775
+ env_3.error = e_3;
776
+ env_3.hasError = true;
777
+ }
778
+ finally {
779
+ const result_3 = __disposeResources(env_3);
780
+ if (result_3)
781
+ await result_3;
677
782
  }
678
783
  }, progressCallback, hooks?.requestBinding);
679
784
  return applyJoinedDownloadStats(result, hooks);
@@ -681,15 +786,26 @@ async function downloadShardedModelFromArchive(archiveUrl, progressCallback, hoo
681
786
  async function downloadShardsWithConcurrency(shardsToDownload, allShards, concurrency, signal, downloadKey, overallTotal, requireSecureTransport, progressCallback, requireChecksum = false, hooks) {
682
787
  const queue = [...shardsToDownload];
683
788
  const inFlight = new Set();
789
+ // A failed or cancelled shard stops the rest, and all settle before this
790
+ // returns, so nothing writes after the caller releases its lock.
791
+ const shardsController = new AbortController();
792
+ const stopShards = () => shardsController.abort(undefined);
793
+ signal.addEventListener('abort', stopShards, { once: true });
794
+ const stopAndSettle = async () => {
795
+ stopShards();
796
+ signal.removeEventListener('abort', stopShards);
797
+ await Promise.allSettled([...inFlight]);
798
+ };
684
799
  while (queue.length > 0 || inFlight.size > 0) {
685
800
  if (signal.aborted) {
801
+ await stopAndSettle();
686
802
  throw new DownloadCancelledError();
687
803
  }
688
804
  while (queue.length > 0 && inFlight.size < concurrency) {
689
805
  const state = queue.shift();
690
806
  const downloadPromise = (async () => {
691
807
  logger.info(`📥 Downloading shard ${state.index + 1}: ${state.shard.filename}`);
692
- const meta = await performHttpDownloadWithResume(state.shard.url, state.shardPath, downloadKey, requireSecureTransport, (progress) => {
808
+ await downloadVerifyAndPublish(state.shard.url, state.shardPath, downloadKey, requireSecureTransport, requireChecksum, (progress) => {
693
809
  state.downloadedBytes = progress.downloaded;
694
810
  if (progressCallback) {
695
811
  const overallDownloaded = allShards.reduce((sum, s) => sum + s.downloadedBytes, 0);
@@ -709,8 +825,7 @@ async function downloadShardsWithConcurrency(shardsToDownload, allShards, concur
709
825
  }
710
826
  });
711
827
  }
712
- }, signal);
713
- await verifyHttpModelFile(state.shard.url, state.shardPath, meta.hubSha256, requireChecksum, hooks);
828
+ }, shardsController.signal, hooks);
714
829
  logger.info(`✅ Shard ${state.index + 1} complete: ${state.shard.filename}`);
715
830
  })().finally(() => {
716
831
  inFlight.delete(downloadPromise);
@@ -718,9 +833,16 @@ async function downloadShardsWithConcurrency(shardsToDownload, allShards, concur
718
833
  inFlight.add(downloadPromise);
719
834
  }
720
835
  if (inFlight.size > 0) {
721
- await Promise.race(inFlight);
836
+ try {
837
+ await Promise.race(inFlight);
838
+ }
839
+ catch (error) {
840
+ await stopAndSettle();
841
+ throw error;
842
+ }
722
843
  }
723
844
  }
845
+ signal.removeEventListener('abort', stopShards);
724
846
  // Mark all downloaded shards as complete
725
847
  for (const state of shardsToDownload) {
726
848
  state.isComplete = true;