@qvac/sdk 0.9.1 → 0.10.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 (335) hide show
  1. package/NOTICE +15 -68
  2. package/dist/client/api/cancel.d.ts +1 -1
  3. package/dist/client/api/cancel.js +1 -1
  4. package/dist/client/api/completion-stream.d.ts +51 -12
  5. package/dist/client/api/completion-stream.js +129 -56
  6. package/dist/client/api/delete-cache.d.ts +1 -0
  7. package/dist/client/api/delete-cache.js +1 -0
  8. package/dist/client/api/diffusion.d.ts +17 -2
  9. package/dist/client/api/diffusion.js +21 -4
  10. package/dist/client/api/embed.d.ts +4 -0
  11. package/dist/client/api/finetune.d.ts +21 -0
  12. package/dist/client/api/get-loaded-model-info.d.ts +25 -0
  13. package/dist/client/api/get-loaded-model-info.js +36 -0
  14. package/dist/client/api/get-model-info.d.ts +9 -1
  15. package/dist/client/api/get-model-info.js +8 -0
  16. package/dist/client/api/heartbeat.d.ts +2 -2
  17. package/dist/client/api/heartbeat.js +2 -2
  18. package/dist/client/api/index.d.ts +3 -1
  19. package/dist/client/api/index.js +3 -1
  20. package/dist/client/api/invoke-plugin.d.ts +16 -0
  21. package/dist/client/api/invoke-plugin.js +16 -0
  22. package/dist/client/api/load-model.d.ts +36 -3
  23. package/dist/client/api/load-model.js +31 -10
  24. package/dist/client/api/provide.d.ts +11 -5
  25. package/dist/client/api/provide.js +11 -6
  26. package/dist/client/api/rag.d.ts +9 -0
  27. package/dist/client/api/rag.js +9 -0
  28. package/dist/client/api/registry.d.ts +26 -0
  29. package/dist/client/api/registry.js +26 -0
  30. package/dist/client/api/resume.d.ts +20 -4
  31. package/dist/client/api/resume.js +20 -4
  32. package/dist/client/api/state.d.ts +23 -0
  33. package/dist/client/api/state.js +29 -0
  34. package/dist/client/api/stop-provider.d.ts +11 -5
  35. package/dist/client/api/stop-provider.js +11 -5
  36. package/dist/client/api/suspend.d.ts +21 -3
  37. package/dist/client/api/suspend.js +21 -3
  38. package/dist/client/api/text-to-speech.d.ts +64 -6
  39. package/dist/client/api/text-to-speech.js +423 -47
  40. package/dist/client/api/transcribe.d.ts +29 -4
  41. package/dist/client/api/transcribe.js +86 -28
  42. package/dist/client/api/translate.d.ts +1 -0
  43. package/dist/client/api/translate.js +1 -0
  44. package/dist/client/rpc/bare-client.js +22 -1
  45. package/dist/client/rpc/expo-rpc-client.d.ts +1 -1
  46. package/dist/client/rpc/expo-rpc-client.js +96 -4
  47. package/dist/client/rpc/node-rpc-client.js +36 -0
  48. package/dist/examples/audio/mic-input.d.ts +9 -0
  49. package/dist/examples/audio/mic-input.js +111 -0
  50. package/dist/examples/completion-events.d.ts +18 -0
  51. package/dist/examples/completion-events.js +79 -0
  52. package/dist/examples/delegated-inference/composite.js +12 -18
  53. package/dist/examples/delegated-inference/consumer-profiled.js +4 -11
  54. package/dist/examples/delegated-inference/consumer.js +8 -13
  55. package/dist/examples/delegated-inference/provider.js +11 -16
  56. package/dist/examples/diffusion-flux2-klein-img2img.d.ts +2 -0
  57. package/dist/examples/diffusion-flux2-klein-img2img.js +60 -0
  58. package/dist/examples/diffusion-flux2-klein.js +1 -0
  59. package/dist/examples/diffusion-img2img.d.ts +2 -0
  60. package/dist/examples/diffusion-img2img.js +49 -0
  61. package/dist/examples/diffusion-txt2img.js +2 -1
  62. package/dist/examples/kv-cache-example.js +11 -12
  63. package/dist/examples/llamacpp-cache.d.ts +2 -0
  64. package/dist/examples/llamacpp-cache.js +107 -0
  65. package/dist/examples/llamacpp-dynamic-tools.d.ts +2 -0
  66. package/dist/examples/llamacpp-dynamic-tools.js +144 -0
  67. package/dist/examples/llamacpp-native-tools.js +2 -8
  68. package/dist/examples/llamacpp-structured-output.d.ts +2 -0
  69. package/dist/examples/llamacpp-structured-output.js +106 -0
  70. package/dist/examples/llamacpp-tools-pythonic.d.ts +2 -0
  71. package/dist/examples/llamacpp-tools-pythonic.js +69 -0
  72. package/dist/examples/plugins.js +4 -4
  73. package/dist/examples/suspend-resume.js +21 -4
  74. package/dist/examples/transcription/parakeet-microphone-record.js +6 -33
  75. package/dist/examples/transcription/whispercpp-filesystem-streaming.js +7 -5
  76. package/dist/examples/transcription/whispercpp-filesystem.js +12 -3
  77. package/dist/examples/transcription/whispercpp-microphone-record.js +6 -29
  78. package/dist/examples/translation/translation-stream.js +5 -5
  79. package/dist/examples/tts/llm-to-tts-streaming.d.ts +16 -0
  80. package/dist/examples/tts/llm-to-tts-streaming.js +143 -0
  81. package/dist/examples/tts/supertonic-multilingual.js +3 -3
  82. package/dist/examples/tts/supertonic.js +3 -3
  83. package/dist/examples/tts/utils.d.ts +11 -1
  84. package/dist/examples/tts/utils.js +70 -17
  85. package/dist/examples/voice-assistant/voice-assistant.d.ts +2 -0
  86. package/dist/examples/voice-assistant/voice-assistant.js +207 -0
  87. package/dist/expo/plugins/withMobileBundle.js +1 -1
  88. package/dist/index.d.ts +2 -2
  89. package/dist/index.js +2 -2
  90. package/dist/logging/logger.d.ts +11 -0
  91. package/dist/logging/logger.js +11 -0
  92. package/dist/logging/types.d.ts +18 -0
  93. package/dist/models/registry/models.d.ts +6620 -6185
  94. package/dist/models/registry/models.js +6296 -5318
  95. package/dist/models/update-models/codegen.js +84 -23
  96. package/dist/models/update-models/companions.d.ts +18 -0
  97. package/dist/models/update-models/companions.js +175 -0
  98. package/dist/models/update-models/naming.js +0 -25
  99. package/dist/models/update-models/registry.js +4 -2
  100. package/dist/models/update-models/types.d.ts +37 -9
  101. package/dist/profiling/index.d.ts +49 -0
  102. package/dist/profiling/index.js +49 -0
  103. package/dist/schemas/cancel.d.ts +6 -2
  104. package/dist/schemas/cancel.js +14 -7
  105. package/dist/schemas/common.d.ts +361 -279
  106. package/dist/schemas/common.js +26 -5
  107. package/dist/schemas/completion-event.d.ts +225 -0
  108. package/dist/schemas/completion-event.js +79 -0
  109. package/dist/schemas/completion-stream.d.ts +122 -31
  110. package/dist/schemas/completion-stream.js +137 -36
  111. package/dist/schemas/delegate.d.ts +0 -2
  112. package/dist/schemas/delegate.js +25 -6
  113. package/dist/schemas/download-asset.d.ts +5 -5
  114. package/dist/schemas/embed.js +21 -9
  115. package/dist/schemas/engine-addon-map.d.ts +11 -1
  116. package/dist/schemas/engine-addon-map.js +2 -2
  117. package/dist/schemas/finetune.d.ts +16 -16
  118. package/dist/schemas/finetune.js +8 -8
  119. package/dist/schemas/get-loaded-model-info.d.ts +52 -0
  120. package/dist/schemas/get-loaded-model-info.js +44 -0
  121. package/dist/schemas/get-model-info.d.ts +2 -2
  122. package/dist/schemas/get-model-info.js +103 -32
  123. package/dist/schemas/heartbeat.d.ts +0 -1
  124. package/dist/schemas/index.d.ts +4 -1
  125. package/dist/schemas/index.js +4 -1
  126. package/dist/schemas/llamacpp-config.d.ts +14 -4
  127. package/dist/schemas/llamacpp-config.js +5 -0
  128. package/dist/schemas/load-model.d.ts +3233 -1260
  129. package/dist/schemas/load-model.js +23 -16
  130. package/dist/schemas/model-src-utils.d.ts +7 -6
  131. package/dist/schemas/model-src-utils.js +27 -1
  132. package/dist/schemas/model-types.d.ts +4 -4
  133. package/dist/schemas/ocr.d.ts +1 -1
  134. package/dist/schemas/plugin.d.ts +27 -3
  135. package/dist/schemas/plugin.js +27 -2
  136. package/dist/schemas/profiling.js +11 -3
  137. package/dist/schemas/provide.d.ts +1 -3
  138. package/dist/schemas/provide.js +0 -3
  139. package/dist/schemas/rag.d.ts +4 -4
  140. package/dist/schemas/registry.d.ts +10 -10
  141. package/dist/schemas/registry.js +36 -14
  142. package/dist/schemas/reload-config.d.ts +73 -7
  143. package/dist/schemas/sdcpp-config.d.ts +16 -8
  144. package/dist/schemas/sdcpp-config.js +118 -25
  145. package/dist/schemas/sdk-config.d.ts +116 -90
  146. package/dist/schemas/sdk-config.js +16 -0
  147. package/dist/schemas/sdk-errors-client.d.ts +3 -0
  148. package/dist/schemas/sdk-errors-client.js +16 -1
  149. package/dist/schemas/sdk-errors-server.d.ts +3 -0
  150. package/dist/schemas/sdk-errors-server.js +23 -0
  151. package/dist/schemas/state.d.ts +23 -0
  152. package/dist/schemas/state.js +14 -0
  153. package/dist/schemas/stop-provide.d.ts +0 -5
  154. package/dist/schemas/stop-provide.js +2 -6
  155. package/dist/schemas/text-to-speech.d.ts +93 -27
  156. package/dist/schemas/text-to-speech.js +27 -0
  157. package/dist/schemas/tools.d.ts +16 -0
  158. package/dist/schemas/tools.js +15 -0
  159. package/dist/schemas/transcription-config.d.ts +8 -8
  160. package/dist/schemas/transcription.d.ts +33 -0
  161. package/dist/schemas/transcription.js +9 -0
  162. package/dist/schemas/translate.js +59 -19
  163. package/dist/schemas/translation-config.d.ts +157 -133
  164. package/dist/schemas/translation-config.js +34 -11
  165. package/dist/server/bare/delegate-rpc-client.d.ts +1 -1
  166. package/dist/server/bare/delegate-rpc-client.js +131 -122
  167. package/dist/server/bare/hyperswarm.d.ts +2 -2
  168. package/dist/server/bare/hyperswarm.js +12 -7
  169. package/dist/server/bare/ops/cancel.js +7 -0
  170. package/dist/server/bare/ops/kv-cache-utils.d.ts +7 -10
  171. package/dist/server/bare/ops/kv-cache-utils.js +31 -40
  172. package/dist/server/bare/ops/load-model.js +0 -1
  173. package/dist/server/bare/ops/transcribe.d.ts +11 -4
  174. package/dist/server/bare/ops/transcribe.js +25 -4
  175. package/dist/server/bare/ops/translate.js +42 -4
  176. package/dist/server/bare/ops/unload-model.js +21 -19
  177. package/dist/server/bare/plugins/llamacpp-completion/ops/cache-logger.js +1 -2
  178. package/dist/server/bare/plugins/llamacpp-completion/ops/completion-stream.d.ts +13 -8
  179. package/dist/server/bare/plugins/llamacpp-completion/ops/completion-stream.js +257 -85
  180. package/dist/server/bare/plugins/llamacpp-completion/ops/kv-cache-state.d.ts +101 -0
  181. package/dist/server/bare/plugins/llamacpp-completion/ops/kv-cache-state.js +135 -0
  182. package/dist/server/bare/plugins/llamacpp-completion/plugin.d.ts +83 -26
  183. package/dist/server/bare/plugins/llamacpp-completion/plugin.js +57 -32
  184. package/dist/server/bare/plugins/llamacpp-embedding/plugin.js +10 -16
  185. package/dist/server/bare/plugins/nmtcpp-translation/plugin.d.ts +69 -44
  186. package/dist/server/bare/plugins/nmtcpp-translation/plugin.js +113 -94
  187. package/dist/server/bare/plugins/onnx-ocr/plugin.d.ts +1 -1
  188. package/dist/server/bare/plugins/onnx-ocr/plugin.js +1 -2
  189. package/dist/server/bare/plugins/onnx-tts/ops/text-to-speech-stream.d.ts +7 -0
  190. package/dist/server/bare/plugins/onnx-tts/ops/text-to-speech-stream.js +116 -0
  191. package/dist/server/bare/plugins/onnx-tts/ops/text-to-speech.d.ts +2 -3
  192. package/dist/server/bare/plugins/onnx-tts/ops/text-to-speech.js +71 -9
  193. package/dist/server/bare/plugins/onnx-tts/plugin.d.ts +41 -13
  194. package/dist/server/bare/plugins/onnx-tts/plugin.js +48 -3
  195. package/dist/server/bare/plugins/parakeet-transcription/plugin.d.ts +15 -7
  196. package/dist/server/bare/plugins/parakeet-transcription/plugin.js +5 -6
  197. package/dist/server/bare/plugins/sdcpp-generation/ops/diffusion.js +6 -0
  198. package/dist/server/bare/plugins/sdcpp-generation/plugin.d.ts +8 -5
  199. package/dist/server/bare/plugins/sdcpp-generation/plugin.js +13 -13
  200. package/dist/server/bare/plugins/whispercpp-transcription/plugin.d.ts +17 -1
  201. package/dist/server/bare/plugins/whispercpp-transcription/plugin.js +40 -17
  202. package/dist/server/bare/registry/config-registry.js +14 -3
  203. package/dist/server/bare/registry/model-registry.d.ts +8 -10
  204. package/dist/server/bare/registry/model-registry.js +17 -12
  205. package/dist/server/bare/registry/registry-client.js +79 -19
  206. package/dist/server/bare/runtime-lifecycle.d.ts +2 -1
  207. package/dist/server/bare/runtime-lifecycle.js +13 -2
  208. package/dist/server/bare/types/addon-responses.d.ts +8 -3
  209. package/dist/server/bare/utils/transcribe-metadata.d.ts +21 -0
  210. package/dist/server/bare/utils/transcribe-metadata.js +28 -0
  211. package/dist/server/bare/utils/tts-stats.d.ts +22 -0
  212. package/dist/server/bare/utils/tts-stats.js +10 -0
  213. package/dist/server/rpc/create-server.d.ts +4 -1
  214. package/dist/server/rpc/create-server.js +5 -1
  215. package/dist/server/rpc/delegate-transport.js +36 -29
  216. package/dist/server/rpc/handle-request.js +28 -3
  217. package/dist/server/rpc/handler-registry.js +7 -1
  218. package/dist/server/rpc/handler-utils.d.ts +5 -0
  219. package/dist/server/rpc/handler-utils.js +22 -0
  220. package/dist/server/rpc/handlers/cancel-delegated.js +3 -5
  221. package/dist/server/rpc/handlers/cancelHandler.js +4 -5
  222. package/dist/server/rpc/handlers/completion-stream-delegated.js +14 -5
  223. package/dist/server/rpc/handlers/delete-cache.js +8 -3
  224. package/dist/server/rpc/handlers/download-asset.js +3 -24
  225. package/dist/server/rpc/handlers/get-loaded-model-info.d.ts +3 -0
  226. package/dist/server/rpc/handlers/get-loaded-model-info.js +39 -0
  227. package/dist/server/rpc/handlers/get-model-info.js +93 -84
  228. package/dist/server/rpc/handlers/heartbeat-delegated.js +2 -2
  229. package/dist/server/rpc/handlers/index.d.ts +6 -0
  230. package/dist/server/rpc/handlers/index.js +6 -0
  231. package/dist/server/rpc/handlers/load-model/download-manager.d.ts +35 -9
  232. package/dist/server/rpc/handlers/load-model/download-manager.js +142 -38
  233. package/dist/server/rpc/handlers/load-model/download-stats.d.ts +5 -4
  234. package/dist/server/rpc/handlers/load-model/download-stats.js +49 -8
  235. package/dist/server/rpc/handlers/load-model/handler.js +31 -50
  236. package/dist/server/rpc/handlers/load-model/http.d.ts +2 -2
  237. package/dist/server/rpc/handlers/load-model/http.js +97 -142
  238. package/dist/server/rpc/handlers/load-model/hyperdrive.d.ts +2 -2
  239. package/dist/server/rpc/handlers/load-model/hyperdrive.js +55 -74
  240. package/dist/server/rpc/handlers/load-model/index.d.ts +0 -1
  241. package/dist/server/rpc/handlers/load-model/index.js +0 -1
  242. package/dist/server/rpc/handlers/load-model/registry-client-options.d.ts +27 -0
  243. package/dist/server/rpc/handlers/load-model/registry-client-options.js +19 -0
  244. package/dist/server/rpc/handlers/load-model/registry-companion-set.d.ts +22 -0
  245. package/dist/server/rpc/handlers/load-model/registry-companion-set.js +208 -0
  246. package/dist/server/rpc/handlers/load-model/registry-download-utils.d.ts +24 -0
  247. package/dist/server/rpc/handlers/load-model/registry-download-utils.js +114 -0
  248. package/dist/server/rpc/handlers/load-model/registry.d.ts +2 -20
  249. package/dist/server/rpc/handlers/load-model/registry.js +109 -381
  250. package/dist/server/rpc/handlers/load-model/resolve-session.d.ts +15 -0
  251. package/dist/server/rpc/handlers/load-model/resolve-session.js +61 -0
  252. package/dist/server/rpc/handlers/load-model/resolve.d.ts +3 -3
  253. package/dist/server/rpc/handlers/load-model/resolve.js +14 -23
  254. package/dist/server/rpc/handlers/load-model/types.d.ts +11 -4
  255. package/dist/server/rpc/handlers/load-model/types.js +28 -1
  256. package/dist/server/rpc/handlers/load-model-delegated.js +2 -4
  257. package/dist/server/rpc/handlers/plugin-dispatch.js +9 -5
  258. package/dist/server/rpc/handlers/plugin-invoke.js +1 -1
  259. package/dist/server/rpc/handlers/provideHandler/connection.js +13 -2
  260. package/dist/server/rpc/handlers/provideHandler/handler.js +42 -29
  261. package/dist/server/rpc/handlers/state.d.ts +3 -0
  262. package/dist/server/rpc/handlers/state.js +7 -0
  263. package/dist/server/rpc/handlers/stopProvideHandler.d.ts +2 -2
  264. package/dist/server/rpc/handlers/stopProvideHandler.js +12 -7
  265. package/dist/server/rpc/handlers/text-to-speech-stream.d.ts +3 -0
  266. package/dist/server/rpc/handlers/text-to-speech-stream.js +4 -0
  267. package/dist/server/rpc/handlers/unload-model-delegated.js +3 -3
  268. package/dist/server/rpc/profiling/operation-metrics.js +20 -0
  269. package/dist/server/rpc/rpc-utils.js +1 -1
  270. package/dist/server/rpc/transport-selector.d.ts +3 -0
  271. package/dist/server/rpc/transport-selector.js +18 -0
  272. package/dist/server/utils/cache/auto-history.d.ts +4 -0
  273. package/dist/server/utils/cache/auto-history.js +22 -0
  274. package/dist/server/utils/{cache.d.ts → cache/paths.d.ts} +22 -5
  275. package/dist/server/utils/{cache.js → cache/paths.js} +36 -20
  276. package/dist/server/utils/cache/types.d.ts +8 -0
  277. package/dist/server/utils/cache/types.js +1 -0
  278. package/dist/server/utils/checksum.d.ts +2 -2
  279. package/dist/server/utils/checksum.js +1 -1
  280. package/dist/server/utils/clear-storage.d.ts +5 -0
  281. package/dist/server/utils/clear-storage.js +17 -0
  282. package/dist/server/utils/completion-normalizer.d.ts +17 -0
  283. package/dist/server/utils/completion-normalizer.js +301 -0
  284. package/dist/server/utils/expand-gguf-shards.d.ts +3 -0
  285. package/dist/server/utils/expand-gguf-shards.js +22 -0
  286. package/dist/server/utils/formatting.js +1 -1
  287. package/dist/server/utils/index.d.ts +4 -2
  288. package/dist/server/utils/index.js +4 -2
  289. package/dist/server/utils/qvac-paths.d.ts +2 -0
  290. package/dist/server/utils/qvac-paths.js +5 -0
  291. package/dist/server/utils/response-format.d.ts +3 -0
  292. package/dist/server/utils/response-format.js +20 -0
  293. package/dist/server/utils/tool-integration.d.ts +15 -4
  294. package/dist/server/utils/tool-integration.js +21 -41
  295. package/dist/server/utils/tools/dialect.d.ts +8 -0
  296. package/dist/server/utils/tools/dialect.js +12 -0
  297. package/dist/server/utils/tools/index.d.ts +3 -0
  298. package/dist/server/utils/tools/index.js +2 -0
  299. package/dist/server/utils/tools/parser.d.ts +6 -0
  300. package/dist/server/utils/tools/parser.js +40 -0
  301. package/dist/server/utils/tools/parsers/hermes.d.ts +4 -0
  302. package/dist/server/utils/tools/parsers/hermes.js +100 -0
  303. package/dist/server/utils/tools/parsers/json.d.ts +6 -0
  304. package/dist/server/utils/tools/parsers/json.js +115 -0
  305. package/dist/server/utils/tools/parsers/pythonic.d.ts +4 -0
  306. package/dist/server/utils/tools/parsers/pythonic.js +267 -0
  307. package/dist/server/utils/tools/shared.d.ts +18 -0
  308. package/dist/server/utils/tools/shared.js +54 -0
  309. package/dist/server/utils/worker-lock.d.ts +3 -0
  310. package/dist/server/utils/worker-lock.js +67 -0
  311. package/dist/server/worker-core.d.ts +16 -1
  312. package/dist/server/worker-core.js +114 -14
  313. package/dist/utils/aggregate-events.d.ts +16 -0
  314. package/dist/utils/aggregate-events.js +52 -0
  315. package/dist/utils/cache-normalize.d.ts +10 -0
  316. package/dist/utils/cache-normalize.js +23 -0
  317. package/dist/utils/encoding.d.ts +8 -0
  318. package/dist/utils/encoding.js +13 -0
  319. package/dist/utils/errors-client.d.ts +12 -0
  320. package/dist/utils/errors-client.js +20 -0
  321. package/dist/utils/errors-server.d.ts +14 -0
  322. package/dist/utils/errors-server.js +31 -0
  323. package/dist/utils/load-model-validation.d.ts +7 -0
  324. package/dist/utils/load-model-validation.js +18 -0
  325. package/expo/plugins/patches/android-link.mjs +1 -1
  326. package/expo/plugins/patches/ios-link.mjs +1 -1
  327. package/package.json +15 -19
  328. package/dist/examples/translation/translation-opus.d.ts +0 -2
  329. package/dist/examples/translation/translation-opus.js +0 -33
  330. package/dist/server/bare/utils/loader-adapter.d.ts +0 -7
  331. package/dist/server/bare/utils/loader-adapter.js +0 -9
  332. package/dist/server/utils/model-path.d.ts +0 -5
  333. package/dist/server/utils/model-path.js +0 -7
  334. package/dist/server/utils/tool-parser.d.ts +0 -7
  335. package/dist/server/utils/tool-parser.js +0 -248
@@ -1,22 +1,4 @@
1
1
  import type { ModelProgressUpdate } from "../../../../schemas/index";
2
- import { type RegistryItem } from "../../../../models/registry/index";
3
- import type { DownloadMetricsHooks } from "./types";
4
- /**
5
- * Find companion ONNX data file in registry.
6
- * ONNX models with external data may use either .onnx_data or .onnx.data.
7
- */
8
- export declare function findOnnxCompanionDataFile(registryPath: string): RegistryItem | undefined;
9
- /**
10
- * Resolve the main ONNX registry path from a companion data registry path.
11
- * Supports both .onnx_data and .onnx.data naming conventions.
12
- */
13
- export declare function getPairedOnnxRegistryPath(registryPath: string): string | undefined;
14
- /**
15
- * Download a model from the QVAC Registry.
16
- *
17
- * For known models (present in models.ts), uses direct blob download which
18
- * skips the registry metadata sync entirely. Falls back to metadata-based
19
- * download for unknown models.
20
- */
21
- export declare function downloadModelFromRegistry(registryPath: string, registrySource: string, progressCallback?: (progress: ModelProgressUpdate) => void, expectedChecksum?: string, hooks?: DownloadMetricsHooks): Promise<string>;
2
+ import type { DownloadHooks } from "./types";
3
+ export declare function downloadModelFromRegistry(registryPath: string, registrySource: string, progressCallback?: (progress: ModelProgressUpdate) => void, expectedChecksum?: string, hooks?: DownloadHooks): Promise<string>;
22
4
  //# sourceMappingURL=registry.d.ts.map
@@ -1,124 +1,14 @@
1
1
  import { promises as fsPromises } from "bare-fs";
2
- import path from "bare-path";
3
- import { AbortController } from "bare-abort-controller";
4
- import { getModelsCacheDir, generateShortHash, detectShardedModel, getShardedModelCacheDir, getShardPath, getOnnxModelPath, measureChecksum, extractTensorsFromShards, calculatePercentage, } from "../../../utils/index.js";
2
+ import { generateShortHash, detectShardedModel, getShardedModelCacheDir, getShardPath, extractTensorsFromShards, calculatePercentage, } from "../../../utils/index.js";
3
+ import { getSingleFileCachePath } from "../../../utils/cache/paths.js";
5
4
  import { getModelByPath } from "../../../../models/registry/index.js";
6
5
  import { getRegistryClient } from "../../../bare/registry/registry-client.js";
7
- import { getActiveDownload, registerDownload, unregisterDownload, createRegistryDownloadKey, clearClearCacheFlag, } from "./download-manager.js";
8
- import { ChecksumValidationFailedError, DownloadCancelledError, ModelNotFoundError, RegistryDownloadFailedError, } from "../../../../utils/errors-server.js";
6
+ import { createRegistryDownloadKey, startOrJoinDownload, applyJoinedDownloadStats, } from "./download-manager.js";
7
+ import { buildBlobBinding, validateCachedFile, downloadSingleFileFromRegistry, } from "./registry-download-utils.js";
8
+ import { downloadCompanionSetFromRegistry } from "./registry-companion-set.js";
9
+ import { DownloadCancelledError, ModelNotFoundError, RegistryDownloadFailedError, } from "../../../../utils/errors-server.js";
9
10
  import { getServerLogger } from "../../../../logging/index.js";
10
11
  const logger = getServerLogger();
11
- const REGISTRY_STREAM_TIMEOUT_MS = 60_000;
12
- function buildBlobBinding(meta) {
13
- return {
14
- coreKey: meta.blobCoreKey,
15
- blockOffset: meta.blobBlockOffset,
16
- blockLength: meta.blobBlockLength,
17
- byteOffset: meta.blobByteOffset,
18
- byteLength: meta.expectedSize,
19
- };
20
- }
21
- /**
22
- * Validate a cached file against expected size and checksum.
23
- */
24
- async function validateCachedFile(modelPath, modelFileName, expectedSize, expectedChecksum, hooks) {
25
- try {
26
- await fsPromises.access(modelPath);
27
- const localStats = await fsPromises.stat(modelPath);
28
- const localSize = localStats.size;
29
- if (localSize === expectedSize) {
30
- logger.info(`✅ Model cached with correct size: ${modelPath}`);
31
- // Validate checksum if provided
32
- if (expectedChecksum && expectedChecksum.length === 64) {
33
- const checksum = await measureChecksum(modelPath, hooks);
34
- if (checksum !== expectedChecksum) {
35
- throw new ChecksumValidationFailedError(`${modelFileName}. Expected: ${expectedChecksum}. Actual: ${checksum}. File may be corrupted`);
36
- }
37
- }
38
- logger.info(`✅ Model already cached and validated: ${modelPath}`);
39
- return modelPath;
40
- }
41
- // File exists but wrong size (incomplete/corrupted) - remove it
42
- logger.warn(`🗑️ Removing incomplete cached file (expected ${expectedSize}, got ${localSize}): ${modelPath}`);
43
- await fsPromises.unlink(modelPath);
44
- return null;
45
- }
46
- catch (error) {
47
- if (error instanceof ChecksumValidationFailedError) {
48
- // Corrupted file - remove it so next download starts fresh
49
- logger.warn(`🗑️ Removing corrupted cached file: ${modelPath}`);
50
- await fsPromises.unlink(modelPath).catch(() => { });
51
- }
52
- // File doesn't exist or was cleaned up - need to download
53
- return null;
54
- }
55
- }
56
- /**
57
- * Download a single file from the registry to filesystem.
58
- * When blobBinding is provided, uses direct blob download (skips metadata sync).
59
- * Otherwise falls back to metadata-based downloadModel.
60
- */
61
- async function downloadSingleFileFromRegistry(registryPath, registrySource, modelPath, modelFileName, downloadKey, expectedSize, expectedChecksum, progressCallback, signal, blobBinding, hooks) {
62
- if (signal?.aborted) {
63
- throw new DownloadCancelledError();
64
- }
65
- const client = await getRegistryClient();
66
- const dir = path.dirname(modelPath);
67
- await fsPromises.mkdir(dir, { recursive: true });
68
- // Adapt registry client's core.on("download") progress to SDK ModelProgressUpdate.
69
- // This reports network-layer bytes, decoupled from disk I/O backpressure.
70
- const onProgress = progressCallback
71
- ? (progress) => {
72
- const total = progress.total || expectedSize || progress.downloaded;
73
- progressCallback({
74
- type: "modelProgress",
75
- downloaded: progress.downloaded,
76
- total,
77
- percentage: total > 0
78
- ? calculatePercentage(progress.downloaded, total)
79
- : 0,
80
- downloadKey,
81
- });
82
- }
83
- : undefined;
84
- const clientOptions = {
85
- timeout: REGISTRY_STREAM_TIMEOUT_MS,
86
- outputFile: modelPath,
87
- ...(onProgress && { onProgress }),
88
- ...(signal && { signal: signal }),
89
- };
90
- if (blobBinding) {
91
- logger.info(`📥 Downloading blob directly: ${modelFileName}`);
92
- await client.downloadBlob(blobBinding, clientOptions);
93
- }
94
- else {
95
- logger.info(`📥 Downloading from registry: ${registryPath}`);
96
- await client.downloadModel(registryPath, registrySource, clientOptions);
97
- }
98
- logger.info(`✅ Downloaded to ${modelPath}`);
99
- const stats = await fsPromises.stat(modelPath);
100
- if (expectedSize && stats.size !== expectedSize) {
101
- await fsPromises.unlink(modelPath).catch(() => { });
102
- throw new ChecksumValidationFailedError(`${modelFileName}. File size mismatch: expected ${expectedSize}, got ${stats.size}`);
103
- }
104
- if (expectedChecksum && expectedChecksum.length === 64) {
105
- const checksum = await measureChecksum(modelPath, hooks);
106
- if (checksum !== expectedChecksum) {
107
- await fsPromises.unlink(modelPath);
108
- throw new ChecksumValidationFailedError(`${modelFileName}. Expected: ${expectedChecksum}. Actual: ${checksum}`);
109
- }
110
- logger.info(`✅ Checksum validated for ${modelFileName}`);
111
- }
112
- if (progressCallback) {
113
- progressCallback({
114
- type: "modelProgress",
115
- downloaded: stats.size,
116
- total: stats.size,
117
- percentage: 100,
118
- downloadKey,
119
- });
120
- }
121
- }
122
12
  /**
123
13
  * Find all shards for a model using path prefix query.
124
14
  */
@@ -126,7 +16,7 @@ async function findModelShards(registryPath) {
126
16
  const client = await getRegistryClient();
127
17
  const shardInfo = detectShardedModel(registryPath.split("/").pop() || "");
128
18
  if (!shardInfo.isSharded) {
129
- throw new Error(`Not a sharded model path: ${registryPath}`);
19
+ throw new RegistryDownloadFailedError(`Not a sharded model path: ${registryPath}`);
130
20
  }
131
21
  const pathPrefix = registryPath.replace(/-\d{5}-of-\d{5}\./, ".");
132
22
  const basePath = pathPrefix.substring(0, pathPrefix.lastIndexOf("."));
@@ -198,7 +88,7 @@ async function downloadShardedFilesFromRegistry(registryPath, registrySource, ca
198
88
  }));
199
89
  }
200
90
  const shardDir = getShardedModelCacheDir(cacheKey);
201
- const downloadKey = createRegistryDownloadKey(registryPath);
91
+ const downloadKey = createRegistryDownloadKey(registrySource, registryPath);
202
92
  logger.info(`📥 Downloading sharded model: ${shards.length} shards to ${shardDir}`);
203
93
  const overallTotal = shards.reduce((sum, s) => sum + s.size, 0);
204
94
  let overallDownloaded = 0;
@@ -275,209 +165,35 @@ async function downloadShardedFilesFromRegistry(registryPath, registrySource, ca
275
165
  }
276
166
  return getShardPath(cacheKey, firstShardFilename);
277
167
  }
278
- /**
279
- * Find companion ONNX data file in registry.
280
- * ONNX models with external data may use either .onnx_data or .onnx.data.
281
- */
282
- export function findOnnxCompanionDataFile(registryPath) {
283
- if (!registryPath.endsWith(".onnx"))
284
- return undefined;
285
- return (getModelByPath(`${registryPath}_data`) ??
286
- getModelByPath(`${registryPath}.data`));
287
- }
288
- /**
289
- * Resolve the main ONNX registry path from a companion data registry path.
290
- * Supports both .onnx_data and .onnx.data naming conventions.
291
- */
292
- export function getPairedOnnxRegistryPath(registryPath) {
293
- if (registryPath.endsWith(".onnx_data")) {
294
- return registryPath.slice(0, -"_data".length);
295
- }
296
- if (registryPath.endsWith(".onnx.data")) {
297
- return registryPath.slice(0, -".data".length);
298
- }
299
- return undefined;
300
- }
301
- /**
302
- * Download ONNX model with companion data file from registry.
303
- * Both files are placed in the same directory to satisfy ONNX Runtime requirements.
304
- * When blob bindings are provided, uses direct blob download (skips metadata sync).
305
- */
306
- async function downloadOnnxWithDataFromRegistry(registryPath, registrySource, companionDataFile, cacheKey, progressCallback, signal, mainBlobBinding, dataBlobBinding, hooks) {
307
- if (signal?.aborted) {
308
- throw new DownloadCancelledError();
309
- }
310
- const mainFilename = registryPath.split("/").pop() || registryPath;
311
- const dataFilename = companionDataFile.registryPath.split("/").pop() || "";
312
- const downloadKey = createRegistryDownloadKey(registryPath);
313
- const mainModelMetadata = getModelByPath(registryPath);
314
- const mainPath = getOnnxModelPath(cacheKey, mainFilename);
315
- const dataPath = getOnnxModelPath(cacheKey, dataFilename);
316
- logger.info(`📥 Downloading ONNX model with external data: ${mainFilename} + ${dataFilename}`);
317
- const mainExpectedSize = mainModelMetadata?.expectedSize || 0;
318
- const mainChecksum = mainModelMetadata?.sha256Checksum || "";
319
- const dataExpectedSize = companionDataFile.expectedSize || 0;
320
- const dataChecksum = companionDataFile.sha256Checksum || "";
321
- const overallTotal = mainExpectedSize + dataExpectedSize;
322
- let overallDownloaded = 0;
323
- // Check if main file already cached
324
- const cachedMainPath = await validateCachedFile(mainPath, mainFilename, mainExpectedSize, mainChecksum, hooks);
325
- if (cachedMainPath) {
326
- logger.info(`✅ Main ONNX file already cached: ${mainFilename}`);
327
- overallDownloaded += mainExpectedSize;
328
- }
329
- else {
330
- // Download main ONNX file
331
- const mainProgressCallback = progressCallback
332
- ? (progress) => {
333
- const currentOverall = overallDownloaded + progress.downloaded;
334
- progressCallback({
335
- ...progress,
336
- downloadKey,
337
- onnxInfo: {
338
- currentFile: mainFilename,
339
- fileIndex: 1,
340
- totalFiles: 2,
341
- overallDownloaded: currentOverall,
342
- overallTotal,
343
- overallPercentage: calculatePercentage(currentOverall, overallTotal),
344
- },
345
- });
346
- }
347
- : undefined;
348
- await downloadSingleFileFromRegistry(registryPath, registrySource, mainPath, mainFilename, downloadKey, mainExpectedSize, mainChecksum, mainProgressCallback, signal, mainBlobBinding, hooks);
349
- overallDownloaded += mainExpectedSize;
350
- logger.info(`✅ Main ONNX file downloaded: ${mainFilename}`);
351
- }
352
- // Check if data file already cached
353
- const cachedDataPath = await validateCachedFile(dataPath, dataFilename, dataExpectedSize, dataChecksum, hooks);
354
- if (cachedDataPath) {
355
- logger.info(`✅ ONNX data file already cached: ${dataFilename}`);
356
- overallDownloaded = overallTotal;
357
- }
358
- else {
359
- // Download companion data file
360
- const dataProgressCallback = progressCallback
361
- ? (progress) => {
362
- const currentOverall = overallDownloaded + progress.downloaded;
363
- progressCallback({
364
- ...progress,
365
- downloadKey,
366
- onnxInfo: {
367
- currentFile: dataFilename,
368
- fileIndex: 2,
369
- totalFiles: 2,
370
- overallDownloaded: currentOverall,
371
- overallTotal,
372
- overallPercentage: calculatePercentage(currentOverall, overallTotal),
373
- },
374
- });
375
- }
376
- : undefined;
377
- await downloadSingleFileFromRegistry(companionDataFile.registryPath, companionDataFile.registrySource, dataPath, dataFilename, downloadKey, dataExpectedSize, dataChecksum, dataProgressCallback, signal, dataBlobBinding, hooks);
378
- logger.info(`✅ ONNX data file downloaded: ${dataFilename}`);
379
- }
380
- // Send final 100% progress
381
- if (progressCallback) {
382
- progressCallback({
383
- type: "modelProgress",
384
- downloaded: overallTotal,
385
- total: overallTotal,
386
- percentage: 100,
387
- downloadKey,
388
- onnxInfo: {
389
- currentFile: dataFilename,
390
- fileIndex: 2,
391
- totalFiles: 2,
392
- overallDownloaded: overallTotal,
393
- overallTotal,
394
- overallPercentage: 100,
395
- },
396
- });
397
- }
398
- return mainPath;
399
- }
400
- /**
401
- * Create a managed download with abort controller support.
402
- */
403
- function createManagedDownload(downloadKey, registryPath, downloadFn, progressCallback) {
404
- const abortController = new AbortController();
405
- const downloadPromise = (async () => {
406
- try {
407
- return await downloadFn(abortController.signal);
408
- }
409
- finally {
410
- unregisterDownload(downloadKey);
411
- clearClearCacheFlag(downloadKey);
412
- }
413
- })();
414
- const downloadEntry = {
415
- key: downloadKey,
416
- promise: downloadPromise,
417
- abortController,
418
- startTime: Date.now(),
419
- type: "registry",
420
- registryPath,
421
- ...(progressCallback && { onProgress: progressCallback }),
422
- };
423
- registerDownload(downloadKey, downloadEntry);
424
- return downloadPromise;
425
- }
426
- /**
427
- * Download a model from the QVAC Registry.
428
- *
429
- * For known models (present in models.ts), uses direct blob download which
430
- * skips the registry metadata sync entirely. Falls back to metadata-based
431
- * download for unknown models.
432
- */
433
168
  export async function downloadModelFromRegistry(registryPath, registrySource, progressCallback, expectedChecksum, hooks) {
434
- const downloadKey = createRegistryDownloadKey(registryPath);
435
- // Check if already downloading
436
- const existing = getActiveDownload(downloadKey);
437
- if (existing) {
438
- logger.info(`📥 Reusing existing download for: ${downloadKey}`);
439
- hooks?.markCacheMiss();
440
- return existing.promise;
441
- }
169
+ const downloadKey = createRegistryDownloadKey(registrySource, registryPath);
170
+ hooks?.onDownloadKey?.(downloadKey);
442
171
  const filename = registryPath.split("/").pop() || registryPath;
443
172
  const shardInfo = detectShardedModel(filename);
444
173
  // Look up model metadata from our generated models.ts
445
174
  const modelMetadata = getModelByPath(registryPath);
446
- // ONNX external data: check if already present in paired ONNX cache directory.
447
- // Avoids redundant single-file downloads when the companion .onnx download already placed it.
448
- const pairedOnnxRegistryPath = getPairedOnnxRegistryPath(registryPath);
449
- if (pairedOnnxRegistryPath) {
450
- const onnxRegistryPath = pairedOnnxRegistryPath;
451
- const onnxCacheKey = generateShortHash(onnxRegistryPath);
452
- const pairedPath = getOnnxModelPath(onnxCacheKey, filename);
453
- const validated = await validateCachedFile(pairedPath, filename, modelMetadata?.expectedSize || 0, modelMetadata?.sha256Checksum, hooks);
454
- if (validated) {
455
- logger.info(`✅ ONNX data file found in paired cache: ${validated}`);
456
- hooks?.markCacheHit();
457
- return validated;
458
- }
459
- }
460
- if (shardInfo.isSharded) {
461
- const cacheKey = generateShortHash(registryPath);
462
- const localShardMeta = modelMetadata?.shardMetadata;
463
- // FS pre-check for known models: if all shards cached, return immediately
464
- if (localShardMeta?.length) {
465
- let allCached = true;
466
- for (const shard of localShardMeta) {
467
- const shardPath = getShardPath(cacheKey, shard.filename);
468
- const cached = await validateCachedFile(shardPath, shard.filename, shard.expectedSize, shard.sha256Checksum, hooks);
469
- if (!cached) {
470
- allCached = false;
471
- break;
175
+ const result = startOrJoinDownload(downloadKey, async (ctx) => {
176
+ if (shardInfo.isSharded) {
177
+ const cacheKey = generateShortHash(registryPath);
178
+ const localShardMeta = modelMetadata?.shardMetadata;
179
+ // FS pre-check for known models: if all shards cached, return immediately
180
+ if (localShardMeta?.length) {
181
+ let allCached = true;
182
+ for (const shard of localShardMeta) {
183
+ const shardPath = getShardPath(cacheKey, shard.filename);
184
+ const cached = await validateCachedFile(shardPath, shard.filename, shard.expectedSize, shard.sha256Checksum, hooks);
185
+ if (!cached) {
186
+ allCached = false;
187
+ break;
188
+ }
472
189
  }
473
- }
474
- if (allCached) {
475
- const firstShardFilename = localShardMeta[0].filename;
476
- logger.info(`✅ All ${localShardMeta.length} shards cached`);
477
- hooks?.markCacheHit();
478
- if (progressCallback) {
190
+ if (allCached) {
191
+ const firstShardFilename = localShardMeta[0].filename;
192
+ logger.info(`✅ All ${localShardMeta.length} shards cached`);
193
+ hooks?.markCacheHit?.();
194
+ ctx.setCacheHit(true);
479
195
  const overallTotal = localShardMeta.reduce((sum, s) => sum + s.expectedSize, 0);
480
- progressCallback({
196
+ ctx.broadcastProgress({
481
197
  type: "modelProgress",
482
198
  downloaded: overallTotal,
483
199
  total: overallTotal,
@@ -492,84 +208,96 @@ export async function downloadModelFromRegistry(registryPath, registrySource, pr
492
208
  overallPercentage: 100,
493
209
  },
494
210
  });
211
+ return getShardPath(cacheKey, firstShardFilename);
495
212
  }
496
- return getShardPath(cacheKey, firstShardFilename);
497
213
  }
498
- }
499
- hooks?.markCacheMiss();
500
- return createManagedDownload(downloadKey, registryPath, (signal) => downloadShardedFilesFromRegistry(registryPath, registrySource, cacheKey, progressCallback, signal, localShardMeta, hooks), progressCallback);
501
- }
502
- // Check for ONNX with companion data file
503
- const companionDataFile = findOnnxCompanionDataFile(registryPath);
504
- if (companionDataFile) {
505
- const cacheKey = generateShortHash(registryPath);
506
- // FS pre-check: if both files cached, return immediately
507
- const mainFilename = registryPath.split("/").pop() || registryPath;
508
- const dataFilename = companionDataFile.registryPath.split("/").pop() || "";
509
- const mainPath = getOnnxModelPath(cacheKey, mainFilename);
510
- const dataPath = getOnnxModelPath(cacheKey, dataFilename);
511
- const mainCached = await validateCachedFile(mainPath, mainFilename, modelMetadata?.expectedSize || 0, modelMetadata?.sha256Checksum, hooks);
512
- const dataCached = await validateCachedFile(dataPath, dataFilename, companionDataFile.expectedSize, companionDataFile.sha256Checksum, hooks);
513
- if (mainCached && dataCached) {
514
- logger.info(`✅ ONNX model and data file both cached`);
515
- hooks?.markCacheHit();
516
- if (progressCallback) {
517
- const total = (modelMetadata?.expectedSize || 0) + companionDataFile.expectedSize;
518
- progressCallback({
519
- type: "modelProgress",
520
- downloaded: total,
521
- total,
522
- percentage: 100,
523
- downloadKey,
524
- onnxInfo: {
525
- currentFile: dataFilename,
526
- fileIndex: 2,
527
- totalFiles: 2,
528
- overallDownloaded: total,
529
- overallTotal: total,
530
- overallPercentage: 100,
531
- },
532
- });
214
+ hooks?.markCacheMiss?.();
215
+ ctx.setCacheHit(false);
216
+ try {
217
+ return await downloadShardedFilesFromRegistry(registryPath, registrySource, cacheKey, ctx.broadcastProgress, ctx.signal, localShardMeta, hooks);
218
+ }
219
+ catch (error) {
220
+ if (error instanceof DownloadCancelledError &&
221
+ ctx.shouldClearCache()) {
222
+ try {
223
+ await fsPromises.rm(getShardedModelCacheDir(cacheKey), {
224
+ recursive: true,
225
+ force: true,
226
+ });
227
+ }
228
+ catch (cleanupError) {
229
+ logger.debug("Failed to delete shard cache dir during cleanup", {
230
+ cacheKey,
231
+ error: cleanupError,
232
+ });
233
+ }
234
+ }
235
+ throw error;
533
236
  }
534
- return mainCached;
535
237
  }
536
- const mainBlobBinding = modelMetadata
537
- ? buildBlobBinding(modelMetadata)
538
- : undefined;
539
- const dataBlobBinding = companionDataFile.blobCoreKey
540
- ? buildBlobBinding(companionDataFile)
541
- : undefined;
542
- hooks?.markCacheMiss();
543
- return createManagedDownload(downloadKey, registryPath, (signal) => downloadOnnxWithDataFromRegistry(registryPath, registrySource, companionDataFile, cacheKey, progressCallback, signal, mainBlobBinding, dataBlobBinding, hooks), progressCallback);
544
- }
545
- // Single file download
546
- const cacheDir = getModelsCacheDir();
547
- const sourceHash = generateShortHash(registryPath);
548
- const modelPath = path.join(cacheDir, `${sourceHash}_${filename}`);
549
- // Check if already cached
550
- const expectedSize = modelMetadata?.expectedSize || 0;
551
- const checksum = expectedChecksum || modelMetadata?.sha256Checksum || "";
552
- const cachedPath = await validateCachedFile(modelPath, filename, expectedSize, checksum, hooks);
553
- if (cachedPath) {
554
- logger.info(`✅ Using cached model: ${cachedPath}`);
555
- hooks?.markCacheHit();
556
- if (progressCallback) {
557
- progressCallback({
238
+ // Generic companion set via generated metadata
239
+ if (modelMetadata?.companionSet) {
240
+ const companionHooks = {
241
+ ...hooks,
242
+ markCacheHit: () => {
243
+ hooks?.markCacheHit?.();
244
+ ctx.setCacheHit(true);
245
+ },
246
+ markCacheMiss: () => {
247
+ hooks?.markCacheMiss?.();
248
+ ctx.setCacheHit(false);
249
+ },
250
+ };
251
+ return await downloadCompanionSetFromRegistry({
252
+ companionSet: modelMetadata.companionSet,
253
+ downloadKey,
254
+ progressCallback: ctx.broadcastProgress,
255
+ signal: ctx.signal,
256
+ hooks: companionHooks,
257
+ shouldClearCache: ctx.shouldClearCache,
258
+ });
259
+ }
260
+ const modelPath = getSingleFileCachePath(registryPath);
261
+ const expectedSize = modelMetadata?.expectedSize || 0;
262
+ const checksum = expectedChecksum || modelMetadata?.sha256Checksum || "";
263
+ const cachedPath = await validateCachedFile(modelPath, filename, expectedSize, checksum, hooks);
264
+ if (cachedPath) {
265
+ logger.info(`✅ Using cached model: ${cachedPath}`);
266
+ hooks?.markCacheHit?.();
267
+ ctx.setCacheHit(true);
268
+ ctx.broadcastProgress({
558
269
  type: "modelProgress",
559
270
  downloaded: expectedSize,
560
271
  total: expectedSize,
561
272
  percentage: 100,
562
273
  downloadKey,
563
274
  });
275
+ return cachedPath;
276
+ }
277
+ const blobBinding = modelMetadata
278
+ ? buildBlobBinding(modelMetadata)
279
+ : undefined;
280
+ hooks?.markCacheMiss?.();
281
+ ctx.setCacheHit(false);
282
+ try {
283
+ await downloadSingleFileFromRegistry(registryPath, registrySource, modelPath, filename, downloadKey, expectedSize, checksum, ctx.broadcastProgress, ctx.signal, blobBinding, hooks);
284
+ }
285
+ catch (error) {
286
+ if (error instanceof DownloadCancelledError &&
287
+ ctx.shouldClearCache()) {
288
+ try {
289
+ await fsPromises.unlink(modelPath);
290
+ }
291
+ catch (cleanupError) {
292
+ logger.debug("Failed to delete model file during cleanup", {
293
+ path: modelPath,
294
+ error: cleanupError,
295
+ });
296
+ }
297
+ }
298
+ throw error;
564
299
  }
565
- return cachedPath;
566
- }
567
- const blobBinding = modelMetadata
568
- ? buildBlobBinding(modelMetadata)
569
- : undefined;
570
- hooks?.markCacheMiss();
571
- return createManagedDownload(downloadKey, registryPath, async (signal) => {
572
- await downloadSingleFileFromRegistry(registryPath, registrySource, modelPath, filename, downloadKey, expectedSize, checksum, progressCallback, signal, blobBinding, hooks);
573
300
  return modelPath;
574
301
  }, progressCallback);
302
+ return applyJoinedDownloadStats(result, hooks);
575
303
  }
@@ -0,0 +1,15 @@
1
+ import type { ModelProgressUpdate, ResolveContext } from "../../../../schemas/index";
2
+ import type { ResolveResult } from "./types";
3
+ export interface ResolveSessionOptions {
4
+ progressCallback?: ((update: ModelProgressUpdate) => void) | undefined;
5
+ seed?: boolean | undefined;
6
+ profilingEnabled: boolean;
7
+ }
8
+ export interface ResolveSession {
9
+ resolvePrimaryModelPath(modelSrc: unknown): Promise<string>;
10
+ createResolveContext(modelSrc: string, modelType: string, modelName?: string): ResolveContext;
11
+ getAggregateResult(): ResolveResult | undefined;
12
+ cancelAll(): void;
13
+ }
14
+ export declare function createResolveSession(options: ResolveSessionOptions): ResolveSession;
15
+ //# sourceMappingURL=resolve-session.d.ts.map
@@ -0,0 +1,61 @@
1
+ import { resolveModelPath, resolveModelPathWithStats, } from "./resolve.js";
2
+ import { cancelTransfer } from "./download-manager.js";
3
+ import { mergeDownloadStats } from "./download-stats.js";
4
+ export function createResolveSession(options) {
5
+ const { progressCallback, seed, profilingEnabled } = options;
6
+ let primaryResult;
7
+ const resolveResults = [];
8
+ const activeDownloadKeys = new Set();
9
+ const downloadHooks = {
10
+ onDownloadKey(key) {
11
+ activeDownloadKeys.add(key);
12
+ },
13
+ };
14
+ async function resolvePrimaryModelPath(modelSrc) {
15
+ if (profilingEnabled) {
16
+ const result = await resolveModelPathWithStats(modelSrc, progressCallback, seed, downloadHooks);
17
+ primaryResult = result;
18
+ resolveResults.push(result);
19
+ return result.path;
20
+ }
21
+ return resolveModelPath(modelSrc, progressCallback, seed, downloadHooks);
22
+ }
23
+ async function resolveForPlugin(src) {
24
+ if (profilingEnabled) {
25
+ const result = await resolveModelPathWithStats(src, progressCallback, seed, downloadHooks);
26
+ resolveResults.push(result);
27
+ return result.path;
28
+ }
29
+ return resolveModelPath(src, progressCallback, seed, downloadHooks);
30
+ }
31
+ function createResolveContext(modelSrc, modelType, modelName) {
32
+ return {
33
+ resolveModelPath: resolveForPlugin,
34
+ modelSrc,
35
+ modelType,
36
+ ...(modelName !== undefined && { modelName }),
37
+ };
38
+ }
39
+ function getAggregateResult() {
40
+ if (!profilingEnabled || resolveResults.length === 0)
41
+ return undefined;
42
+ const downloadStats = mergeDownloadStats(resolveResults);
43
+ return {
44
+ path: primaryResult?.path ?? resolveResults[0].path,
45
+ sourceType: primaryResult?.sourceType ?? resolveResults[0].sourceType,
46
+ ...(downloadStats !== undefined && { downloadStats }),
47
+ };
48
+ }
49
+ function cancelAll() {
50
+ for (const key of activeDownloadKeys) {
51
+ cancelTransfer(key);
52
+ }
53
+ activeDownloadKeys.clear();
54
+ }
55
+ return {
56
+ resolvePrimaryModelPath,
57
+ createResolveContext,
58
+ getAggregateResult,
59
+ cancelAll,
60
+ };
61
+ }
@@ -1,5 +1,5 @@
1
1
  import { type ModelProgressUpdate } from "../../../../schemas/index";
2
- import type { ResolveResult } from "./types";
3
- export declare function resolveModelPath(modelSrc: unknown, progressCallback?: (progress: ModelProgressUpdate) => void, seed?: boolean): Promise<string>;
4
- export declare function resolveModelPathWithStats(modelSrc: unknown, progressCallback?: (progress: ModelProgressUpdate) => void, seed?: boolean): Promise<ResolveResult>;
2
+ import type { ResolveResult, DownloadHooks } from "./types";
3
+ export declare function resolveModelPath(modelSrc: unknown, progressCallback?: (progress: ModelProgressUpdate) => void, seed?: boolean, hooks?: DownloadHooks): Promise<string>;
4
+ export declare function resolveModelPathWithStats(modelSrc: unknown, progressCallback?: (progress: ModelProgressUpdate) => void, seed?: boolean, hooks?: DownloadHooks): Promise<ResolveResult>;
5
5
  //# sourceMappingURL=resolve.d.ts.map