@qvac/inference 0.18.2 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (388) hide show
  1. package/NOTICE +11 -2
  2. package/README.md +1 -5
  3. package/dist/api/assess-model-fit.d.ts +16 -0
  4. package/dist/api/assess-model-fit.js +27 -0
  5. package/dist/api/audio-gen.d.ts +4 -4
  6. package/dist/api/audio-gen.js +17 -6
  7. package/dist/api/completion-stream.d.ts +1 -1
  8. package/dist/api/completion-stream.js +1 -1
  9. package/dist/api/download-asset.d.ts +1 -1
  10. package/dist/api/download-asset.js +2 -2
  11. package/dist/api/finetune.js +1 -1
  12. package/dist/api/get-loaded-model-info.d.ts +6 -10
  13. package/dist/api/get-loaded-model-info.js +6 -10
  14. package/dist/api/heartbeat.d.ts +5 -22
  15. package/dist/api/heartbeat.js +6 -23
  16. package/dist/api/index.d.ts +36 -35
  17. package/dist/api/index.js +35 -35
  18. package/dist/api/load-model.d.ts +5 -1
  19. package/dist/api/load-model.js +3 -5
  20. package/dist/api/logging-stream-registry.js +1 -1
  21. package/dist/api/resume.d.ts +0 -3
  22. package/dist/api/resume.js +0 -3
  23. package/dist/api/subscribe-logs.js +1 -1
  24. package/dist/api/suspend.d.ts +0 -4
  25. package/dist/api/suspend.js +0 -4
  26. package/dist/api/translate.d.ts +3 -1
  27. package/dist/api/translate.js +16 -8
  28. package/dist/api/unload-model.js +3 -5
  29. package/dist/api/video-request.d.ts +207 -0
  30. package/dist/api/video-request.js +18 -0
  31. package/dist/api/video.d.ts +22 -2
  32. package/dist/api/video.js +25 -15
  33. package/dist/api/vla-helpers.d.ts +1 -2
  34. package/dist/api/vla-helpers.js +1 -2
  35. package/dist/api/vla.js +1 -1
  36. package/dist/api/world-result.d.ts +71 -0
  37. package/dist/api/world-result.js +293 -0
  38. package/dist/api/world.d.ts +143 -0
  39. package/dist/api/world.js +71 -0
  40. package/dist/config/resolve-config.d.ts +1 -1
  41. package/dist/config/resolve-config.js +1 -1
  42. package/dist/constants/index.d.ts +2 -2
  43. package/dist/constants/index.js +2 -2
  44. package/dist/dispatch.js +15 -28
  45. package/dist/errors/index.d.ts +35 -31
  46. package/dist/errors/index.js +66 -56
  47. package/dist/handlers/assess-model-fit.d.ts +10 -0
  48. package/dist/handlers/assess-model-fit.js +75 -0
  49. package/dist/handlers/cancelHandler.js +1 -1
  50. package/dist/handlers/completion-orchestrate.js +1 -1
  51. package/dist/handlers/download-asset.js +6 -5
  52. package/dist/handlers/finetune.js +1 -1
  53. package/dist/handlers/get-loaded-model-info.js +0 -12
  54. package/dist/handlers/get-model-info.js +1 -1
  55. package/dist/handlers/get-system-resources.js +8 -0
  56. package/dist/handlers/load-model/download-manager.d.ts +1 -1
  57. package/dist/handlers/load-model/download-stats.d.ts +3 -3
  58. package/dist/handlers/load-model/download-stats.js +5 -5
  59. package/dist/handlers/load-model/handler.js +6 -8
  60. package/dist/handlers/load-model/http-verify.d.ts +20 -0
  61. package/dist/handlers/load-model/http-verify.js +54 -0
  62. package/dist/handlers/load-model/http.d.ts +12 -2
  63. package/dist/handlers/load-model/http.js +119 -230
  64. package/dist/handlers/load-model/hyperdrive.d.ts +1 -1
  65. package/dist/handlers/load-model/hyperdrive.js +1 -1
  66. package/dist/handlers/load-model/index.d.ts +2 -2
  67. package/dist/handlers/load-model/index.js +2 -2
  68. package/dist/handlers/load-model/registry-companion-set.d.ts +1 -1
  69. package/dist/handlers/load-model/registry-companion-set.js +1 -1
  70. package/dist/handlers/load-model/registry-download-utils.d.ts +3 -3
  71. package/dist/handlers/load-model/registry-download-utils.js +2 -2
  72. package/dist/handlers/load-model/registry.d.ts +2 -2
  73. package/dist/handlers/load-model/registry.js +4 -4
  74. package/dist/handlers/load-model/resolve-session.d.ts +8 -1
  75. package/dist/handlers/load-model/resolve-session.js +12 -9
  76. package/dist/handlers/load-model/resolve.d.ts +4 -4
  77. package/dist/handlers/load-model/resolve.js +18 -16
  78. package/dist/handlers/load-model/safe-fetch.d.ts +44 -0
  79. package/dist/handlers/load-model/safe-fetch.js +143 -0
  80. package/dist/handlers/load-model/types.d.ts +9 -0
  81. package/dist/handlers/plugin-dispatch.js +1 -4
  82. package/dist/handlers/plugin-invoke.js +1 -4
  83. package/dist/handlers/types.d.ts +0 -2
  84. package/dist/handlers/unload-model.js +1 -4
  85. package/dist/index.d.ts +1 -1
  86. package/dist/index.js +1 -1
  87. package/dist/logging/addon.d.ts +1 -1
  88. package/dist/logging/addon.js +1 -1
  89. package/dist/logging/index.d.ts +5 -5
  90. package/dist/logging/index.js +4 -4
  91. package/dist/logging/logger.d.ts +1 -1
  92. package/dist/logging/logger.js +2 -2
  93. package/dist/models/registry/index.d.ts +1 -1
  94. package/dist/models/registry/index.js +2 -2
  95. package/dist/models/registry/models.d.ts +250 -0
  96. package/dist/models/registry/models.js +2071 -1806
  97. package/dist/models/registry/resource-profiles.d.ts +18 -0
  98. package/dist/models/registry/resource-profiles.js +4934 -0
  99. package/dist/plugins/builtin/audiogen-ggml/config.d.ts +14 -0
  100. package/dist/plugins/builtin/audiogen-ggml/config.js +23 -0
  101. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.js +47 -5
  102. package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +134 -4
  103. package/dist/plugins/builtin/audiogen-ggml/plugin.js +47 -28
  104. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +1 -1
  105. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +3 -3
  106. package/dist/plugins/builtin/ggml-classification/plugin.js +1 -1
  107. package/dist/plugins/builtin/ggml-ocr/plugin.js +2 -2
  108. package/dist/plugins/builtin/ggml-vla/plugin.d.ts +1 -1
  109. package/dist/plugins/builtin/ggml-vla/plugin.js +3 -3
  110. package/dist/plugins/builtin/index.d.ts +12 -12
  111. package/dist/plugins/builtin/index.js +12 -12
  112. package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +2 -2
  113. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +37 -26
  114. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.d.ts +12 -11
  115. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.js +133 -41
  116. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.d.ts +7 -1
  117. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.js +26 -4
  118. package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +19 -8
  119. package/dist/plugins/builtin/llamacpp-completion/plugin.js +10 -13
  120. package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +4 -0
  121. package/dist/plugins/builtin/llamacpp-embedding/plugin.js +13 -2
  122. package/dist/plugins/builtin/nmtcpp-translation/plugin.js +2 -2
  123. package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +2 -2
  124. package/dist/plugins/builtin/parakeet-transcription/plugin.js +3 -3
  125. package/dist/plugins/builtin/sdcpp-generation/ops/upscale.js +1 -1
  126. package/dist/plugins/builtin/sdcpp-generation/ops/video.js +32 -2
  127. package/dist/plugins/builtin/sdcpp-generation/ops/world.d.ts +126 -0
  128. package/dist/plugins/builtin/sdcpp-generation/ops/world.js +820 -0
  129. package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +153 -4
  130. package/dist/plugins/builtin/sdcpp-generation/plugin.js +134 -13
  131. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.js +1 -1
  132. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +1 -1
  133. package/dist/plugins/builtin/tts-ggml/plugin.js +2 -2
  134. package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +2 -2
  135. package/dist/plugins/builtin/whispercpp-transcription/plugin.js +3 -3
  136. package/dist/plugins/index.d.ts +1 -1
  137. package/dist/plugins/index.js +2 -2
  138. package/dist/plugins/ops/kv-cache-utils.js +1 -1
  139. package/dist/plugins/ops/load-model.js +1 -1
  140. package/dist/plugins/ops/transcribe.js +2 -2
  141. package/dist/plugins/ops/translate.js +4 -12
  142. package/dist/plugins/ops/unload-model.js +19 -21
  143. package/dist/plugins/registry.d.ts +1 -0
  144. package/dist/plugins/registry.js +12 -0
  145. package/dist/profiling/aggregator.d.ts +1 -1
  146. package/dist/profiling/aggregator.js +1 -1
  147. package/dist/profiling/controller.d.ts +1 -1
  148. package/dist/profiling/controller.js +2 -2
  149. package/dist/profiling/envelope.d.ts +0 -1
  150. package/dist/profiling/envelope.js +0 -10
  151. package/dist/profiling/events.d.ts +2 -3
  152. package/dist/profiling/events.js +2 -9
  153. package/dist/profiling/exporters.d.ts +1 -1
  154. package/dist/profiling/exporters.js +3 -3
  155. package/dist/profiling/index.d.ts +13 -14
  156. package/dist/profiling/index.js +12 -13
  157. package/dist/profiling/operation-metrics.d.ts +1 -1
  158. package/dist/profiling/operation-metrics.js +35 -2
  159. package/dist/profiling/operation-wrappers.js +12 -15
  160. package/dist/profiling/profiler.d.ts +1 -2
  161. package/dist/profiling/profiler.js +7 -26
  162. package/dist/profiling/types.d.ts +1 -1
  163. package/dist/rag/handler.js +1 -1
  164. package/dist/rag/handlers/close-workspace.js +1 -1
  165. package/dist/rag/handlers/delete-embeddings.js +1 -1
  166. package/dist/rag/handlers/delete-workspace.js +2 -6
  167. package/dist/rag/handlers/ingest.js +1 -1
  168. package/dist/rag/handlers/list-workspaces.d.ts +1 -1
  169. package/dist/rag/handlers/list-workspaces.js +1 -1
  170. package/dist/rag/handlers/reindex.js +1 -1
  171. package/dist/rag/handlers/save-embeddings.js +1 -1
  172. package/dist/rag/handlers/search.js +1 -1
  173. package/dist/rag/index.d.ts +11 -11
  174. package/dist/rag/index.js +11 -11
  175. package/dist/rag/rag-workspace-manager.d.ts +4 -2
  176. package/dist/rag/rag-workspace-manager.js +268 -15
  177. package/dist/registry.js +8 -58
  178. package/dist/resources/collector.d.ts +3 -1
  179. package/dist/resources/collector.js +91 -9
  180. package/dist/resources/instance.d.ts +1 -1
  181. package/dist/resources/instance.js +1 -1
  182. package/dist/resources/model-fit/assess.d.ts +36 -0
  183. package/dist/resources/model-fit/assess.js +578 -0
  184. package/dist/resources/model-fit/calibration/darwin-arm64.d.ts +8 -0
  185. package/dist/resources/model-fit/calibration/darwin-arm64.js +36 -0
  186. package/dist/resources/model-fit/calibration/darwin-x64.d.ts +8 -0
  187. package/dist/resources/model-fit/calibration/darwin-x64.js +38 -0
  188. package/dist/resources/model-fit/calibration/fit.d.ts +73 -0
  189. package/dist/resources/model-fit/calibration/fit.js +152 -0
  190. package/dist/resources/model-fit/calibration/index.d.ts +37 -0
  191. package/dist/resources/model-fit/calibration/index.js +75 -0
  192. package/dist/resources/model-fit/calibration/linux-arm64.d.ts +8 -0
  193. package/dist/resources/model-fit/calibration/linux-arm64.js +38 -0
  194. package/dist/resources/model-fit/calibration/linux-x64-vulkan.d.ts +8 -0
  195. package/dist/resources/model-fit/calibration/linux-x64-vulkan.js +39 -0
  196. package/dist/resources/model-fit/calibration/linux-x64.d.ts +8 -0
  197. package/dist/resources/model-fit/calibration/linux-x64.js +38 -0
  198. package/dist/resources/model-fit/calibration/win32-x64-vulkan-shared.d.ts +8 -0
  199. package/dist/resources/model-fit/calibration/win32-x64-vulkan-shared.js +39 -0
  200. package/dist/resources/model-fit/calibration/win32-x64-vulkan.d.ts +8 -0
  201. package/dist/resources/model-fit/calibration/win32-x64-vulkan.js +36 -0
  202. package/dist/resources/model-fit/calibration/win32-x64.d.ts +8 -0
  203. package/dist/resources/model-fit/calibration/win32-x64.js +38 -0
  204. package/dist/resources/model-fit/estimators/llm.d.ts +45 -0
  205. package/dist/resources/model-fit/estimators/llm.js +220 -0
  206. package/dist/resources/model-fit/estimators/whisper.d.ts +10 -0
  207. package/dist/resources/model-fit/estimators/whisper.js +77 -0
  208. package/dist/resources/model-fit/types.d.ts +120 -0
  209. package/dist/resources/model-fit/types.js +1 -0
  210. package/dist/resources/native.d.ts +1 -1
  211. package/dist/resources/native.js +14 -0
  212. package/dist/resources/normalize.d.ts +1 -1
  213. package/dist/resources/types.d.ts +16 -0
  214. package/dist/runtime/index.d.ts +5 -5
  215. package/dist/runtime/index.js +2 -2
  216. package/dist/runtime/lifecycle.js +5 -7
  217. package/dist/runtime/model-registry.d.ts +0 -14
  218. package/dist/runtime/model-registry.js +17 -40
  219. package/dist/runtime/registry-client.js +1 -1
  220. package/dist/runtime/request-context.d.ts +3 -3
  221. package/dist/runtime/request-context.js +13 -1
  222. package/dist/runtime/request-registry.d.ts +1 -1
  223. package/dist/runtime/request-registry.js +4 -5
  224. package/dist/runtime/state.d.ts +1 -1
  225. package/dist/runtime/state.js +18 -3
  226. package/dist/schemas/assess-model-fit.d.ts +271 -0
  227. package/dist/schemas/assess-model-fit.js +153 -0
  228. package/dist/schemas/audio-gen.d.ts +434 -4
  229. package/dist/schemas/audio-gen.js +131 -19
  230. package/dist/schemas/batch-completion-stream.d.ts +2 -2
  231. package/dist/schemas/batch-completion-stream.js +3 -3
  232. package/dist/schemas/bci-config.js +73 -27
  233. package/dist/schemas/bci.d.ts +2 -2
  234. package/dist/schemas/bci.js +5 -5
  235. package/dist/schemas/cancel.d.ts +5 -0
  236. package/dist/schemas/cancel.js +1 -0
  237. package/dist/schemas/classification.js +13 -6
  238. package/dist/schemas/common.d.ts +662 -331
  239. package/dist/schemas/common.js +33 -31
  240. package/dist/schemas/completion-event.d.ts +2 -2
  241. package/dist/schemas/completion-event.js +1 -1
  242. package/dist/schemas/completion-stream.d.ts +1 -5
  243. package/dist/schemas/completion-stream.js +4 -8
  244. package/dist/schemas/config.d.ts +640 -18
  245. package/dist/schemas/config.js +50 -8
  246. package/dist/schemas/download-asset.d.ts +19 -1
  247. package/dist/schemas/download-asset.js +20 -4
  248. package/dist/schemas/engine-addon-map.d.ts +1 -1
  249. package/dist/schemas/engine-addon-map.js +3 -3
  250. package/dist/schemas/error.d.ts +2 -2
  251. package/dist/schemas/error.js +2 -2
  252. package/dist/schemas/errors.d.ts +2 -6
  253. package/dist/schemas/errors.js +30 -40
  254. package/dist/schemas/heartbeat.d.ts +10 -0
  255. package/dist/schemas/heartbeat.js +8 -0
  256. package/dist/schemas/index.d.ts +46 -44
  257. package/dist/schemas/index.js +45 -43
  258. package/dist/schemas/llamacpp-config.d.ts +32 -10
  259. package/dist/schemas/llamacpp-config.js +11 -8
  260. package/dist/schemas/load-model.d.ts +2933 -1948
  261. package/dist/schemas/load-model.js +52 -52
  262. package/dist/schemas/mcp-adapter.d.ts +1 -1
  263. package/dist/schemas/model-config-schemas.d.ts +2354 -0
  264. package/dist/schemas/model-config-schemas.js +46 -0
  265. package/dist/schemas/model-info.d.ts +10 -20
  266. package/dist/schemas/model-info.js +4 -25
  267. package/dist/schemas/model-ops.d.ts +0 -1
  268. package/dist/schemas/model-ops.js +1 -2
  269. package/dist/schemas/model-resource-profile.d.ts +109 -0
  270. package/dist/schemas/model-resource-profile.js +83 -0
  271. package/dist/schemas/model-src-utils.d.ts +1 -0
  272. package/dist/schemas/model-src-utils.js +33 -13
  273. package/dist/schemas/ocr.js +48 -13
  274. package/dist/schemas/path-validation.js +8 -2
  275. package/dist/schemas/plugin.d.ts +14 -1
  276. package/dist/schemas/plugin.js +11 -0
  277. package/dist/schemas/profiling.d.ts +138 -43
  278. package/dist/schemas/profiling.js +1 -22
  279. package/dist/schemas/rag.js +1 -1
  280. package/dist/schemas/registry.js +1 -1
  281. package/dist/schemas/reload-config.d.ts +0 -2
  282. package/dist/schemas/reload-config.js +2 -3
  283. package/dist/schemas/sdcpp-config.d.ts +344 -4
  284. package/dist/schemas/sdcpp-config.js +504 -17
  285. package/dist/schemas/system-resources.d.ts +138 -0
  286. package/dist/schemas/system-resources.js +3 -1
  287. package/dist/schemas/text-to-speech.js +183 -76
  288. package/dist/schemas/transcription-config.js +253 -85
  289. package/dist/schemas/transcription.d.ts +3 -3
  290. package/dist/schemas/translate.d.ts +2 -2
  291. package/dist/schemas/translate.js +1 -1
  292. package/dist/schemas/translation-config.js +53 -25
  293. package/dist/schemas/vla.d.ts +1 -1
  294. package/dist/selection.d.ts +1 -5
  295. package/dist/selection.js +2 -6
  296. package/dist/surface.d.ts +1 -1
  297. package/dist/utils/aggregate-events.d.ts +1 -1
  298. package/dist/utils/aggregate-events.js +2 -2
  299. package/dist/utils/archive.js +2 -2
  300. package/dist/utils/audio-input.js +1 -1
  301. package/dist/utils/cache/auto-history.d.ts +1 -1
  302. package/dist/utils/cache/auto-history.js +1 -1
  303. package/dist/utils/cache/paths.js +5 -5
  304. package/dist/utils/completion-normalizer.js +1 -1
  305. package/dist/utils/gguf-tensor-extractor.js +1 -1
  306. package/dist/utils/image-dimensions.d.ts +26 -0
  307. package/dist/utils/image-dimensions.js +46 -0
  308. package/dist/utils/index.d.ts +13 -10
  309. package/dist/utils/index.js +13 -10
  310. package/dist/utils/mcp-adapter.d.ts +1 -1
  311. package/dist/utils/mcp-adapter.js +1 -1
  312. package/dist/utils/path-sanitize.d.ts +7 -0
  313. package/dist/utils/path-sanitize.js +36 -0
  314. package/dist/utils/path-security.d.ts +0 -7
  315. package/dist/utils/path-security.js +1 -36
  316. package/dist/utils/shard-utils.js +3 -3
  317. package/dist/utils/tool-integration.js +1 -1
  318. package/dist/utils/tools/dialect.js +1 -1
  319. package/dist/utils/tools/index.d.ts +2 -2
  320. package/dist/utils/tools/index.js +2 -2
  321. package/dist/utils/tools/parser.js +8 -8
  322. package/dist/utils/tools/parsers/dsml.d.ts +1 -1
  323. package/dist/utils/tools/parsers/dsml.js +1 -1
  324. package/dist/utils/tools/parsers/gemma4native.d.ts +1 -1
  325. package/dist/utils/tools/parsers/gemma4native.js +1 -1
  326. package/dist/utils/tools/parsers/harmony.d.ts +1 -1
  327. package/dist/utils/tools/parsers/harmony.js +1 -1
  328. package/dist/utils/tools/parsers/hermes.d.ts +1 -1
  329. package/dist/utils/tools/parsers/hermes.js +1 -1
  330. package/dist/utils/tools/parsers/json.d.ts +1 -1
  331. package/dist/utils/tools/parsers/json.js +1 -1
  332. package/dist/utils/tools/parsers/pythonic.d.ts +1 -1
  333. package/dist/utils/tools/parsers/pythonic.js +1 -1
  334. package/dist/utils/tools/parsers/qwen35.d.ts +1 -1
  335. package/dist/utils/tools/parsers/qwen35.js +3 -3
  336. package/dist/utils/url-security.d.ts +17 -0
  337. package/dist/utils/url-security.js +75 -0
  338. package/package.json +31 -26
  339. package/dist/api/provide.d.ts +0 -23
  340. package/dist/api/provide.js +0 -32
  341. package/dist/api/stop-provider.d.ts +0 -20
  342. package/dist/api/stop-provider.js +0 -30
  343. package/dist/p2p/cancel-delegated.d.ts +0 -3
  344. package/dist/p2p/cancel-delegated.js +0 -64
  345. package/dist/p2p/completion-stream-delegated.d.ts +0 -4
  346. package/dist/p2p/completion-stream-delegated.js +0 -51
  347. package/dist/p2p/delegate-client.d.ts +0 -9
  348. package/dist/p2p/delegate-client.js +0 -219
  349. package/dist/p2p/delegate-connect-diagnostics.d.ts +0 -8
  350. package/dist/p2p/delegate-connect-diagnostics.js +0 -38
  351. package/dist/p2p/delegate-transport.d.ts +0 -18
  352. package/dist/p2p/delegate-transport.js +0 -237
  353. package/dist/p2p/error-handlers.d.ts +0 -4
  354. package/dist/p2p/error-handlers.js +0 -31
  355. package/dist/p2p/handle-request.d.ts +0 -2
  356. package/dist/p2p/handle-request.js +0 -171
  357. package/dist/p2p/handler-utils.d.ts +0 -19
  358. package/dist/p2p/handler-utils.js +0 -180
  359. package/dist/p2p/heartbeat-delegated.d.ts +0 -3
  360. package/dist/p2p/heartbeat-delegated.js +0 -32
  361. package/dist/p2p/load-model-delegated.d.ts +0 -6
  362. package/dist/p2p/load-model-delegated.js +0 -132
  363. package/dist/p2p/progress-throttle.d.ts +0 -7
  364. package/dist/p2p/progress-throttle.js +0 -47
  365. package/dist/p2p/provideHandler/connection.d.ts +0 -2
  366. package/dist/p2p/provideHandler/connection.js +0 -38
  367. package/dist/p2p/provideHandler/handler.d.ts +0 -2
  368. package/dist/p2p/provideHandler/handler.js +0 -66
  369. package/dist/p2p/provideHandler/proxy.d.ts +0 -2
  370. package/dist/p2p/provideHandler/proxy.js +0 -24
  371. package/dist/p2p/provideHandler.d.ts +0 -1
  372. package/dist/p2p/provideHandler.js +0 -2
  373. package/dist/p2p/rpc-utils.d.ts +0 -4
  374. package/dist/p2p/rpc-utils.js +0 -8
  375. package/dist/p2p/stopProvideHandler.d.ts +0 -2
  376. package/dist/p2p/stopProvideHandler.js +0 -29
  377. package/dist/p2p/swarm.d.ts +0 -9
  378. package/dist/p2p/swarm.js +0 -103
  379. package/dist/p2p/transport-selector.d.ts +0 -2
  380. package/dist/p2p/transport-selector.js +0 -18
  381. package/dist/p2p/unload-model-delegated.d.ts +0 -2
  382. package/dist/p2p/unload-model-delegated.js +0 -31
  383. package/dist/profiling/delegation-profiler.d.ts +0 -31
  384. package/dist/profiling/delegation-profiler.js +0 -155
  385. package/dist/schemas/delegate.d.ts +0 -29
  386. package/dist/schemas/delegate.js +0 -40
  387. package/dist/schemas/provide.d.ts +0 -50
  388. package/dist/schemas/provide.js +0 -32
@@ -0,0 +1,820 @@
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
+ });
53
+ import fs, { promises as fsPromises } from 'bare-fs';
54
+ import path from 'bare-path';
55
+ import WorldStableDiffusion from '@qvac/diffusion-cpp/world';
56
+ import { getEngineLogger } from '../../../../logging/index.js';
57
+ import { getModel, getModelEntry } from '../../../../runtime/model-registry.js';
58
+ import { getRequestRegistry, withRequestContext } from '../../../../runtime/index.js';
59
+ import { generateRandomRequestId } from '../../../../runtime/request-id.js';
60
+ import { getCacheDir, generateShortHash, readImageDimensions } from '../../../../utils/index.js';
61
+ import { InferenceCancelledError, ModelLoadFailedError, ModelNotLoadedError, ModelOperationNotSupportedError, PluginRequestValidationFailedError } from '../../../../errors/index.js';
62
+ import { ModelType } from '../../../../schemas/index.js';
63
+ // A step is ~1.8s and a scene is bounded by its encoder loads, so anything past
64
+ // this means the native job is not progressing. Warn only — see settleInFlight.
65
+ const INFLIGHT_WARN_MS = 120_000;
66
+ // A pack is ~10 MB at 832x480 and scales with pixel area; 128 MB is far above
67
+ // any real resolution and well under what would hurt a worker to hold, base64
68
+ // included. A pack past it means something went wrong natively, not a bigger
69
+ // world.
70
+ const MAX_SCENE_PACK_BYTES = 128 * 1024 * 1024;
71
+ /**
72
+ * Ceiling on the first frame's DECLARED pixel count, read from its header before
73
+ * anything decodes it.
74
+ *
75
+ * 8192x8192. Sized off what people actually send, not off the output: the frame
76
+ * is cover-scaled and cropped, so a phone photo is a perfectly reasonable source
77
+ * for an 832x480 world, and a 12 MP camera image (4000x3000) must not be
78
+ * refused. This bounds the decode at roughly 200 MB while still refusing a
79
+ * gigapixel bomb by more than an order of magnitude.
80
+ */
81
+ const MAX_SCENE_IMAGE_PIXELS = 8192 * 8192;
82
+ /**
83
+ * Ceiling on ONE block of decoded frames, held in memory at once.
84
+ *
85
+ * The per-axis and total-pixel limits bound a single frame; `numFramePerBlock`
86
+ * bounds how many. Neither alone bounds the product, and the product is what is
87
+ * allocated: at the 1920x1088 ceiling a frame is ~6 MiB, so the 64-frame maximum
88
+ * would reach ~1.49 GiB. Enforced where both numbers are known, which is scene
89
+ * creation — the resolution arrives per request, the block shape at load.
90
+ */
91
+ const MAX_BLOCK_BYTES = 512 * 1024 * 1024;
92
+ /** Frames a block delivers: ~4x the configured latent frames, 12 at the default of 3. */
93
+ function projectedFramesPerBlock(numFramePerBlock) {
94
+ const latent = numFramePerBlock === undefined || numFramePerBlock === 0 ? 3 : numFramePerBlock;
95
+ return latent * 4;
96
+ }
97
+ const worldSessions = new WeakSet();
98
+ /**
99
+ * Server-side path for one session's scene pack. Derived from a hash of the
100
+ * caller-supplied `modelId` rather than the id itself, so the id can never
101
+ * steer the write.
102
+ *
103
+ * Deliberately unique per call, not per `modelId`. A pack keyed only on the
104
+ * model id is shared state across every process using the same cache dir: two
105
+ * workers loading the same model would write each other's world, and a pack
106
+ * orphaned by a crashed session would be adopted by the next `load()` as if the
107
+ * caller had built it — walking a world they never created. Neither is
108
+ * detectable from inside the session.
109
+ *
110
+ * Uniqueness costs nothing here because the managed pack is not a cache: it
111
+ * lives for exactly one loaded session, and callers who want to revisit a world
112
+ * pass the bytes back as `modelConfig.sceneSrc`. The one invariant that does
113
+ * matter — `createScene` writes exactly where the native session was
114
+ * constructed to read — comes from calling this ONCE per session and storing
115
+ * the result in `files.scene`, not from the path being reproducible.
116
+ *
117
+ * No garbage collection, deliberately. `unload()` removes the pack, and so does
118
+ * worker shutdown, so only a crash or SIGKILL leaks one — bounded at roughly
119
+ * 10 MB per lost session, and never adopted by a later run because the name is
120
+ * unique. The obvious mtime-based sweep is NOT safe here: a pack is written once
121
+ * at promotion and never touched again, so a session walking for hours looks
122
+ * exactly as old as an orphan, and ~/.qvac is shared across worker processes.
123
+ * Doing this properly needs a liveness marker like the one
124
+ * `planAutoCacheEvictions` gets from `activeCachePaths` in
125
+ * `server/bare/ops/kv-cache-retention.ts`. Tracked as a follow-up rather than
126
+ * guessed at here.
127
+ */
128
+ export function worldScenePath(modelId) {
129
+ const session = generateShortHash(generateRandomRequestId());
130
+ return path.join(getCacheDir('world-scenes'), `${generateShortHash(modelId)}-${session}.safetensors`);
131
+ }
132
+ /**
133
+ * Wraps `WorldStableDiffusion` so it satisfies the plugin `load()` contract.
134
+ *
135
+ * Two things make the raw addon unusable as a plugin model. First, `load()`
136
+ * reads the scene pack, which does not exist yet when the caller intends to
137
+ * build a world with `worldCreateScene` — so activation is deferred until a
138
+ * scene is present. Second, the addon's `unload()` is synchronous down to a
139
+ * native thread join, so calling it while a job runs blocks the worker's event
140
+ * loop for the rest of that job (a walk block, or a whole scene encode plus its
141
+ * ~6.9 GB of encoder loads). We therefore track the in-flight job and wait for
142
+ * it before tearing down.
143
+ */
144
+ export function createWorldSession(args) {
145
+ const { modelId, files, config, encoders, seedScenePath, logger } = args;
146
+ const maxScenePackBytes = args.maxScenePackBytes ?? MAX_SCENE_PACK_BYTES;
147
+ const world = args.world ?? new WorldStableDiffusion({ files, config, logger, opts: { stats: true } });
148
+ const stagingScenePath = `${files.scene}.staging`;
149
+ let activated = false;
150
+ let inFlight = null;
151
+ let torn = false;
152
+ let lockTail = Promise.resolve();
153
+ /**
154
+ * Serialises everything that touches the native session or its pack files
155
+ * against teardown. `unloadModel` is a plain RPC — it does not pass through
156
+ * the request registry, so nothing else stops it landing between this
157
+ * session's own awaits. Same promise-chain idiom as `withCacheStateLock` in
158
+ * llamacpp-completion/ops/kv-cache-session.ts.
159
+ */
160
+ async function withSessionLock(operation) {
161
+ const previous = lockTail;
162
+ let releaseLock = () => { };
163
+ lockTail = new Promise((resolve) => {
164
+ releaseLock = resolve;
165
+ });
166
+ await previous;
167
+ try {
168
+ return await operation();
169
+ }
170
+ finally {
171
+ releaseLock();
172
+ }
173
+ }
174
+ /**
175
+ * `torn` is set synchronously by `unload()` before it awaits anything, so a
176
+ * caller that has not yet yielded cannot dispatch into a session that is
177
+ * already being torn down.
178
+ */
179
+ function assertLive() {
180
+ if (torn)
181
+ throw new ModelNotLoadedError(modelId);
182
+ }
183
+ function hasScene() {
184
+ return fs.existsSync(files.scene);
185
+ }
186
+ // `force` makes an already-absent pack a no-op, so anything reaching the
187
+ // catch is a real failure: teardown must not fail over it, but swallowing it
188
+ // silently would make an ephemeral-scene leak invisible.
189
+ async function removeIfPresent(target) {
190
+ try {
191
+ await fsPromises.rm(target, { force: true });
192
+ }
193
+ catch (error) {
194
+ logger.warn(`Could not remove world scene pack "${target}" for "${modelId}": ` +
195
+ `${error instanceof Error ? error.message : String(error)}`);
196
+ }
197
+ }
198
+ async function activate() {
199
+ await world.load();
200
+ activated = true;
201
+ }
202
+ /**
203
+ * Native teardown runs synchronously down to a thread join, so calling it
204
+ * while a job is running blocks the worker's event loop — every model and
205
+ * every RPC on it — until that job finishes on its own. Waiting here costs
206
+ * the same wall-clock and keeps the loop free.
207
+ *
208
+ * Deliberately unbounded. A deadline would have to either give up (leaking
209
+ * the session) or tear down anyway, and tearing down early is precisely the
210
+ * freeze this exists to avoid. The native job always terminates.
211
+ */
212
+ async function settleInFlight() {
213
+ if (!inFlight)
214
+ return;
215
+ const timer = setTimeout(() => {
216
+ logger.warn(`World job on "${modelId}" still running after ${INFLIGHT_WARN_MS}ms; teardown is waiting for it to finish`);
217
+ }, INFLIGHT_WARN_MS);
218
+ try {
219
+ await inFlight;
220
+ }
221
+ catch {
222
+ // A failed job is still a finished job — that is all we need here.
223
+ }
224
+ finally {
225
+ clearTimeout(timer);
226
+ }
227
+ }
228
+ const session = {
229
+ scenePath: files.scene,
230
+ stagingScenePath,
231
+ encoders,
232
+ numFramePerBlock: typeof config.numFramePerBlock === 'number' ? config.numFramePerBlock : undefined,
233
+ settle: settleInFlight,
234
+ // Only here does a new world replace the old one. `rename` within the same
235
+ // directory is atomic, so a reader never sees a half-written pack and a
236
+ // crash mid-generation cannot leave the model pointing at one.
237
+ //
238
+ // Promotion and the read that follows it are one locked step, and the bytes
239
+ // come back from here rather than the caller re-reading `scenePath`: an
240
+ // `unloadModel` landing between the two deletes the pack, which would turn
241
+ // a finished world into an ENOENT with no explanation.
242
+ async promoteStagedScene(readBytes) {
243
+ return withSessionLock(async () => {
244
+ assertLive();
245
+ // Sized BEFORE promotion, so an implausible pack cannot replace the
246
+ // world the caller already had — and, when the bytes were asked for,
247
+ // cannot be base64'd into ~4/3 of its size in worker memory either.
248
+ const { size } = await fsPromises.stat(stagingScenePath);
249
+ if (size > maxScenePackBytes) {
250
+ throw new PluginRequestValidationFailedError('worldSceneStream', `Generated world scene pack is ${size} bytes, over the ${maxScenePackBytes}-byte ceiling. ` +
251
+ 'The previous world was left in place; lower worldCreateScene width/height and create it again.');
252
+ }
253
+ // Read BEFORE the rename, so the rename is the last thing that can fail
254
+ // and the only thing that commits. Reading after it meant a failed read
255
+ // — ENOENT, EIO, or simply not enough memory for a large pack — handed
256
+ // the caller an error against a world that had already been replaced,
257
+ // with the previous one gone and no way back.
258
+ //
259
+ // 'buffer' picks the Buffer overload; the bare-arg form is typed
260
+ // `string | Buffer`, which the pack is never read as text.
261
+ const bytes = readBytes ? await fsPromises.readFile(stagingScenePath, 'buffer') : undefined;
262
+ await fsPromises.rename(stagingScenePath, files.scene);
263
+ return bytes;
264
+ });
265
+ },
266
+ // Generation failed: drop the staged file and leave the previous pack in
267
+ // place, so the next step re-activates the world the caller already had
268
+ // rather than finding the model worldless.
269
+ async discardStagedScene() {
270
+ // No liveness check: dropping a staged file is safe whether or not the
271
+ // session survived, and a discard that threw would mask the failure that
272
+ // triggered it.
273
+ await withSessionLock(() => removeIfPresent(stagingScenePath));
274
+ },
275
+ // Eager when a scene pack is already present, so a bad pack or an
276
+ // oversubscribed GPU fails at loadModel like every other model type.
277
+ // Deferred otherwise — the caller is going to build the world first.
278
+ async load() {
279
+ // The native session reads the pack from the single path it was
280
+ // constructed with, so a caller-supplied pack is copied into that slot
281
+ // rather than pointed at — unconditionally, so changing sceneSrc between
282
+ // loads of the same modelId cannot leave a stale world in place.
283
+ if (seedScenePath) {
284
+ // The same ceiling `promoteStagedScene` applies to a pack we generate.
285
+ // Both paths end at `files.scene` and are loaded by the same native
286
+ // session, so capping only the generated one just means an oversized
287
+ // pack has to arrive via `sceneSrc` instead. Checked before the copy, so
288
+ // an implausible file is refused rather than duplicated onto disk first.
289
+ const seedStat = await fsPromises.stat(seedScenePath);
290
+ // A directory or device node stats fine and would fail later inside
291
+ // copyFile with a bare errno. Say what is wrong while we still know.
292
+ if (!seedStat.isFile()) {
293
+ throw new ModelLoadFailedError('modelConfig.sceneSrc must be a file. Scene packs are the .safetensors ' +
294
+ 'files worldCreateScene produces.');
295
+ }
296
+ if (seedStat.size > maxScenePackBytes) {
297
+ throw new ModelLoadFailedError(`modelConfig.sceneSrc is ${seedStat.size} bytes, over the ` +
298
+ `${maxScenePackBytes}-byte scene-pack ceiling. Scene packs are produced ` +
299
+ 'by worldCreateScene; a file this large is not one.');
300
+ }
301
+ try {
302
+ await fsPromises.copyFile(seedScenePath, files.scene);
303
+ // Re-check what actually landed rather than trusting the pre-copy
304
+ // stat: the two are separate syscalls, so the source can change in
305
+ // between and the ceiling would have been measured on a file we did
306
+ // not copy. This is the size that matters — it is the one the native
307
+ // session will load.
308
+ const { size: copiedSize } = await fsPromises.stat(files.scene);
309
+ if (copiedSize > maxScenePackBytes) {
310
+ throw new ModelLoadFailedError(`modelConfig.sceneSrc copied as ${copiedSize} bytes, over the ` +
311
+ `${maxScenePackBytes}-byte scene-pack ceiling.`);
312
+ }
313
+ }
314
+ catch (error) {
315
+ // A copy that died partway leaves a truncated pack that `hasScene()`
316
+ // would happily treat as a world — and so does an oversized one we
317
+ // just rejected. Drop it so the failure is the load error, not a
318
+ // corrupt or outsized world discovered on the first step.
319
+ await removeIfPresent(files.scene);
320
+ throw error;
321
+ }
322
+ }
323
+ if (!hasScene()) {
324
+ logger.info('No scene pack yet — the walk session will activate on the first step after worldCreateScene');
325
+ return;
326
+ }
327
+ try {
328
+ await activate();
329
+ }
330
+ catch (error) {
331
+ // The model never registers when load throws, so nothing else will ever
332
+ // come back to clean this up. The pack is our own copy — the caller's
333
+ // `sceneSrc` original is untouched.
334
+ await removeIfPresent(files.scene);
335
+ throw error;
336
+ }
337
+ },
338
+ async ensureActivated() {
339
+ assertLive();
340
+ if (activated)
341
+ return;
342
+ await withSessionLock(async () => {
343
+ assertLive();
344
+ if (activated)
345
+ return;
346
+ if (!hasScene()) {
347
+ // A request-time precondition on a model that loaded fine, so this is
348
+ // a bad request rather than a load failure.
349
+ throw new PluginRequestValidationFailedError('worldStepStream', `No world exists for model "${modelId}". Create one with worldCreateScene({ modelId, prompt, image }), ` +
350
+ 'or load the model with modelConfig.sceneSrc pointing at an existing scene pack.');
351
+ }
352
+ await activate();
353
+ });
354
+ },
355
+ // The native session caches the scene it was activated with, and `load()`
356
+ // is a no-op once loaded — so a rewritten pack would be ignored and the
357
+ // walk would silently continue in the old world. Drop the session first.
358
+ async deactivate() {
359
+ if (!activated)
360
+ return;
361
+ await withSessionLock(async () => {
362
+ if (!activated)
363
+ return;
364
+ await settleInFlight();
365
+ try {
366
+ await world.unload();
367
+ activated = false;
368
+ }
369
+ catch (error) {
370
+ // A rejecting native unload is NOT recoverable by clearing our own
371
+ // bookkeeping, and clearing it is actively harmful. The addon wrapper
372
+ // sets `addon = null` and `configLoaded = false` only AFTER its
373
+ // `addon.unload()` resolves (diffusion-cpp/src/world.ts), so a
374
+ // rejection leaves it still believing it is loaded. If we then said
375
+ // `activated = false`, the next `ensureActivated()` would call
376
+ // `world.load()`, hit its `if (configLoaded) return` guard, no-op, and
377
+ // report an active session sitting on native state we already tried to
378
+ // destroy.
379
+ //
380
+ // Mark the session unusable instead. `assertLive` then fails every
381
+ // later call with ModelNotLoadedError, which tells the caller the truth
382
+ // — reload the model — rather than silently walking a dead world.
383
+ torn = true;
384
+ // Spell out the recovery, because the obvious one does not work:
385
+ // `loadModel` returns success without doing anything while the id is
386
+ // still registered (plugins/ops/load-model.ts), so a direct reload
387
+ // hands back this same torn session. `unloadModel` first — it
388
+ // unregisters before teardown, so it clears the id even when the
389
+ // native unload throws again.
390
+ logger.error(`World session teardown failed for "${modelId}"; the session is unusable. ` +
391
+ 'Recover with unloadModel({ modelId }) THEN loadModel(...) — loading ' +
392
+ 'again on its own is a no-op while the id stays registered. Cause: ' +
393
+ `${error instanceof Error ? error.message : String(error)}`);
394
+ throw error;
395
+ }
396
+ });
397
+ },
398
+ // Tracks the job for `unload()` to wait on. It must follow the response to
399
+ // its terminal state, not just the dispatch: `step()` and `createScene()`
400
+ // resolve as soon as the native scheduler admits the job, while the block
401
+ // or encode this guard exists for runs on well after that.
402
+ //
403
+ // Registration happens at dispatch, before the first await, because the
404
+ // admission itself is part of the job: `await start()` yields the event
405
+ // loop, and a teardown landing in that window would find `inFlight` empty
406
+ // and take the native session down underneath a job the scheduler had
407
+ // already accepted.
408
+ async run(start) {
409
+ assertLive();
410
+ // `start` is typed as a thunk, but this method is only reachable through
411
+ // `getModel(modelId)`, which is untyped — and `ops/diffusion.ts` calls
412
+ // `model.run({ prompt, ... })` with NO model-type guard (unlike
413
+ // `asVideoModel` / `asUpscalerModel` in its sibling ops). A diffusion
414
+ // request aimed at a world model therefore lands here with an options
415
+ // object. Refuse it BEFORE `inFlight` is touched: assigning the guard
416
+ // first and failing afterwards would leave an already-settled promise in
417
+ // it, making `settleInFlight()` a no-op while a native block is still
418
+ // running — and teardown would then enter the addon's synchronous unload
419
+ // mid-job and freeze the worker's event loop for every model on it.
420
+ if (typeof start !== 'function') {
421
+ throw new ModelOperationNotSupportedError(modelId, ModelType.sdcppGeneration, 'diffusion', ['worldStep', 'worldCreateScene'], []);
422
+ }
423
+ const dispatch = (async () => start())();
424
+ // A dispatch that rejects is a finished job as far as teardown cares, so
425
+ // both arms settle the guard. Failures still reach the caller — from the
426
+ // returned promise here, and from `response.iterate()` after it.
427
+ const settled = dispatch.then((response) => response.await().then(() => { }, () => { }), () => { });
428
+ inFlight = settled;
429
+ void settled.finally(() => {
430
+ if (inFlight === settled)
431
+ inFlight = null;
432
+ });
433
+ return dispatch;
434
+ },
435
+ step: (keys) => world.step(keys),
436
+ createScene: (params) => world.createScene(params),
437
+ cancel: () => world.cancel(),
438
+ async unload() {
439
+ // Set before the first await so `assertLive` can refuse new work from a
440
+ // caller that has not yielded yet. Taking the lock is not enough on its
441
+ // own: acquiring it is itself an await.
442
+ torn = true;
443
+ await withSessionLock(async () => {
444
+ await settleInFlight();
445
+ try {
446
+ await world.unload();
447
+ }
448
+ finally {
449
+ // In a `finally` because this is the ONLY chance to remove the pack. A
450
+ // rejecting native unload used to skip both removals, and nothing ever
451
+ // comes back: `unload-model.ts` unregisters the entry before calling
452
+ // this, and `unloadAllModels` swallows the error and drops the entry
453
+ // too. With per-session names and no GC by design, that orphan is
454
+ // never adopted and never swept — so repeated load/unload cycles would
455
+ // accumulate ~10 MB each, unbounded.
456
+ activated = false;
457
+ await removeIfPresent(files.scene);
458
+ await removeIfPresent(stagingScenePath);
459
+ }
460
+ });
461
+ }
462
+ };
463
+ worldSessions.add(session);
464
+ return session;
465
+ }
466
+ function asWorldSession(model, modelId, operation) {
467
+ if (model !== null && typeof model === 'object' && worldSessions.has(model)) {
468
+ return model;
469
+ }
470
+ const entry = getModelEntry(modelId);
471
+ const modelType = entry ? entry.local.modelType : ModelType.sdcppGeneration;
472
+ throw new ModelOperationNotSupportedError(modelId, modelType, operation, ['diffusion'], []);
473
+ }
474
+ export async function* worldStep(request) {
475
+ const env_1 = { stack: [], error: void 0, hasError: false };
476
+ try {
477
+ const ctx = __addDisposableResource(env_1, await getRequestRegistry().begin({
478
+ requestId: request.requestId ?? generateRandomRequestId(),
479
+ kind: 'world',
480
+ modelId: request.modelId
481
+ }), true);
482
+ const requestLogger = withRequestContext(getEngineLogger(), ctx);
483
+ // Before ANYTHING touches the session. A cancel that landed before `begin(...)`
484
+ // resolved makes the registry skip admission, so this context holds no world
485
+ // slot and another step may legitimately own the lane right now. Everything
486
+ // below reaches across to that owner: `ensureActivated()` can re-activate a
487
+ // session it is using, and `onAbort` fires `session.cancel()`, which is a
488
+ // MODEL-WIDE native cancel — it would kill the owner's in-flight block, not
489
+ // ours, since we have none. The later pre-dispatch check cannot help; by then
490
+ // the listener has already run.
491
+ if (ctx.signal.aborted) {
492
+ throw new InferenceCancelledError(ctx.requestId);
493
+ }
494
+ const session = asWorldSession(getModel(request.modelId), request.modelId, 'worldStep');
495
+ await session.ensureActivated();
496
+ // Block-granular by design: the engine exposes no mid-block abort, so this
497
+ // stops frame delivery and makes the step reject rather than resolve with a
498
+ // silently truncated block.
499
+ const onAbort = () => {
500
+ session.cancel().catch((err) => {
501
+ requestLogger.warn(`[cancel] world cancel rejected for modelId=${request.modelId}: ${err instanceof Error ? err.message : String(err)}`);
502
+ });
503
+ };
504
+ ctx.signal.addEventListener('abort', onAbort, { once: true });
505
+ if (ctx.signal.aborted)
506
+ onAbort();
507
+ ctx.scope.defer(() => {
508
+ ctx.signal.removeEventListener('abort', onAbort);
509
+ });
510
+ // An abort can land while `ensureActivated()` is still running. `onAbort` has
511
+ // then already fired, but the native cancel flag it sets is CLEARED by
512
+ // `processWalkStep()` when the next block begins — so dispatching now would
513
+ // run a full block, advance the session history, and deliver none of it. The
514
+ // caller would see a rejection while the world silently moved on. Refuse
515
+ // before dispatch instead.
516
+ if (ctx.signal.aborted) {
517
+ throw new InferenceCancelledError(ctx.requestId);
518
+ }
519
+ // The dispatch sits INSIDE the guard, not in front of it: `step()` rejects at
520
+ // admission too (`runStep` throwing on VRAM exhaustion, the addon's busy
521
+ // guard), and the addon's `_stepInternal` fails the native job on its way
522
+ // out. A rejection in front of the `try` would skip both `settle()` and the
523
+ // terminal teardown below and leave `activated` true over a dead session —
524
+ // the exact wedge that teardown exists to prevent. `worldCreateScene` puts
525
+ // its dispatch inside its guard for the same reason.
526
+ let response;
527
+ let frameIndex = 0;
528
+ // Set only when `iterate()` runs to its own end. Every other way out of the
529
+ // block below leaves the native session advanced past frames the caller never
530
+ // saw, which is the same hazard as a cancel and needs the same teardown. A
531
+ // consumer that stops early — a dropped transport, a `break`, an explicit
532
+ // `.return()` on the generator — unwinds through `finally` as a RETURN
533
+ // completion: `catch` does not run and `ctx.signal.aborted` is false, so
534
+ // neither of the other two flags can stand in for this one.
535
+ let drained = false;
536
+ try {
537
+ response = (await session.run(() => session.step(request.keys ?? [])));
538
+ for await (const chunk of response.iterate()) {
539
+ if (ctx.signal.aborted)
540
+ break;
541
+ if (chunk instanceof Uint8Array) {
542
+ yield {
543
+ type: 'worldStepStream',
544
+ data: Buffer.from(chunk).toString('base64'),
545
+ frameIndex: frameIndex++
546
+ };
547
+ }
548
+ else if (typeof chunk === 'string') {
549
+ // The addon emits `{"step":N,"frames":M,"elapsed_ms":T}` ONCE per block,
550
+ // after every frame has been delivered — WorldSessionModel.cpp runs the
551
+ // step to completion, loops the frames out, then fires the callback. So
552
+ // this is an end-of-block summary, not the mid-block liveness it would
553
+ // be if the engine ticked per denoise step. Forwarded anyway, for the
554
+ // same reason ops/video.ts and ops/diffusion.ts forward theirs: the wire
555
+ // shape stays identical across the three, and if the engine ever ticks
556
+ // during a block this needs no change to carry it.
557
+ //
558
+ // `frames` maps onto `totalSteps` rather than a field of its own so the
559
+ // wire shape stays identical to its two siblings — the engine counts a
560
+ // world block's progress in decoded frames, not in sampler steps.
561
+ try {
562
+ const tick = JSON.parse(chunk);
563
+ if ('step' in tick) {
564
+ yield {
565
+ type: 'worldStepStream',
566
+ step: tick['step'],
567
+ totalSteps: tick['frames'],
568
+ elapsedMs: tick['elapsed_ms']
569
+ };
570
+ }
571
+ }
572
+ catch {
573
+ // Non-JSON string output — skip, as the sibling ops do.
574
+ }
575
+ }
576
+ }
577
+ drained = !ctx.signal.aborted;
578
+ }
579
+ catch (error) {
580
+ // Native cancellation surfaces as the addon's own `Diffusion/Cancelled`
581
+ // error out of `iterate()`. Rethrowing it raw hands the client a generic RPC
582
+ // error for what this API promises as a typed cancellation — and the
583
+ // conversion below the try is unreachable once we throw from here. Only an
584
+ // aborted request is relabelled; every other failure keeps its own error.
585
+ // `finally` still runs before this propagates, so the settle and teardown
586
+ // happen first either way.
587
+ if (ctx.signal.aborted) {
588
+ throw new InferenceCancelledError(ctx.requestId, {}, error);
589
+ }
590
+ throw error;
591
+ }
592
+ finally {
593
+ // Cancel is block-granular, so the DiT keeps going after we stop reading.
594
+ // Hold the model's slot until it stops, or the next step is admitted into a
595
+ // session the addon will reject as busy. Runs on early consumer exit too.
596
+ await session.settle();
597
+ // Both a failure AND a cancellation are terminal for the native session.
598
+ // The addon contract is explicit that the engine's RNG/history cannot be
599
+ // resumed after either — of cancellation it says "Treat it like any failed
600
+ // step: reload the session" — so leaving `activated` true would make every
601
+ // later step dispatch into dead state and surface opaque native errors,
602
+ // recoverable only by unloadModel + loadModel and a re-resolve of the
603
+ // artifacts. Dropping it here lets the next step rebuild transparently from
604
+ // the same promoted pack.
605
+ //
606
+ // A cancel does not reach the `catch` above: the loop breaks on the abort
607
+ // flag rather than throwing, so the abort has to be tested here too. And an
608
+ // early consumer exit reaches neither, which is what `drained` covers.
609
+ if (!drained) {
610
+ try {
611
+ await session.deactivate();
612
+ }
613
+ catch (teardownError) {
614
+ // Never mask the failure that got us here.
615
+ requestLogger.warn(`World session teardown failed for modelId=${request.modelId} after a terminal step: ` +
616
+ `${teardownError instanceof Error ? teardownError.message : String(teardownError)}`);
617
+ }
618
+ }
619
+ }
620
+ // A cancelled block must not be reported as a finished one. Native compute for
621
+ // the block may well finish, but its history is discarded with the session in
622
+ // the teardown above, so the frames we did not deliver are gone for good —
623
+ // yielding `done` here would hand the caller a silent gap dressed up as
624
+ // success. The next step rebuilds from the promoted pack and the walk restarts
625
+ // from the world's beginning. The addon makes the step itself reject for the
626
+ // same reason; this covers the race where we stop iterating before that
627
+ // rejection lands.
628
+ if (ctx.signal.aborted) {
629
+ throw new InferenceCancelledError(ctx.requestId);
630
+ }
631
+ const { stats } = response;
632
+ yield {
633
+ type: 'worldStepStream',
634
+ done: true,
635
+ ...(stats && { stats })
636
+ };
637
+ }
638
+ catch (e_1) {
639
+ env_1.error = e_1;
640
+ env_1.hasError = true;
641
+ }
642
+ finally {
643
+ const result_1 = __disposeResources(env_1);
644
+ if (result_1)
645
+ await result_1;
646
+ }
647
+ }
648
+ export async function* worldCreateScene(request) {
649
+ const env_2 = { stack: [], error: void 0, hasError: false };
650
+ try {
651
+ const ctx = __addDisposableResource(env_2, await getRequestRegistry().begin({
652
+ requestId: request.requestId ?? generateRandomRequestId(),
653
+ kind: 'world',
654
+ modelId: request.modelId
655
+ }), true);
656
+ const requestLogger = withRequestContext(getEngineLogger(), ctx);
657
+ const session = asWorldSession(getModel(request.modelId), request.modelId, 'worldCreateScene');
658
+ const { t5, vae } = session.encoders;
659
+ if (!t5 || !vae) {
660
+ throw new PluginRequestValidationFailedError('worldSceneStream', 'Creating a world requires the prompt and image encoders. Load the model with ' +
661
+ 'modelConfig.t5XxlModelSrc (umT5-XXL) and modelConfig.vaeModelSrc (Wan2.2 VAE).');
662
+ }
663
+ // Sits HERE, with the other pure preconditions, and not further down: every
664
+ // statement below `deactivate()` costs the caller their live session, and a
665
+ // request rejected on its own input must not be what takes it. Nothing in this
666
+ // check needs the session.
667
+ //
668
+ // The base64 ceiling on `image` bounds what crosses the WIRE, not what the
669
+ // decoder allocates — wildly different numbers for a compressed format. A
670
+ // 1.5 MB PNG of uniform scanlines can declare 40000x40000: 1.6 gigapixels, and
671
+ // roughly 4.8 GB in a single native allocation, all of it before the
672
+ // cover-scale and crop that would have brought it down to width x height.
673
+ // `readImageDimensions` is a header read — big-endian IHDR for PNG, the first
674
+ // SOFx segment for JPEG — so this costs microseconds and happens before any of
675
+ // it.
676
+ //
677
+ // Only enforced when the header is actually readable. The addon documents
678
+ // PNG/JPEG, which is exactly what this reads, and refusing everything else here
679
+ // would reject formats the native decoder may accept today.
680
+ // Count x resolution is what actually gets allocated, and neither ceiling
681
+ // bounds the product on its own. Checked here because this is the only point
682
+ // where both are known: the block shape came from modelConfig at load, the
683
+ // resolution arrives with this request.
684
+ const blockWidth = request.width ?? 832;
685
+ const blockHeight = request.height ?? 480;
686
+ const blockBytes = projectedFramesPerBlock(session.numFramePerBlock) * blockWidth * blockHeight * 3;
687
+ if (blockBytes > MAX_BLOCK_BYTES) {
688
+ throw new PluginRequestValidationFailedError('worldSceneStream', `A ${blockWidth}x${blockHeight} world at world.numFramePerBlock=` +
689
+ `${session.numFramePerBlock ?? 3} would hold ${blockBytes} bytes of decoded ` +
690
+ `frames per block, over the ${MAX_BLOCK_BYTES}-byte ceiling. Lower the ` +
691
+ 'resolution, lower world.numFramePerBlock, or both.');
692
+ }
693
+ const image = Buffer.from(request.image, 'base64');
694
+ const declared = readImageDimensions(image);
695
+ if (!declared) {
696
+ // Fail CLOSED. The sender picks the format, so treating "cannot size this"
697
+ // as "allow it" makes the ceiling below trivially skippable — deliver the
698
+ // same bomb as a BMP and nothing checks it. The schema documents PNG/JPEG,
699
+ // so this refuses exactly what it already says it does not accept.
700
+ throw new PluginRequestValidationFailedError('worldSceneStream', 'The first-frame image is not a readable PNG or JPEG. Its dimensions have ' +
701
+ 'to be checked before anything decodes it, so a frame whose header ' +
702
+ 'cannot be read is refused rather than allocated.');
703
+ }
704
+ if (declared.width * declared.height > MAX_SCENE_IMAGE_PIXELS) {
705
+ throw new PluginRequestValidationFailedError('worldSceneStream', `The first-frame image declares ${declared.width}x${declared.height} ` +
706
+ `(${declared.width * declared.height} pixels), over the ` +
707
+ `${MAX_SCENE_IMAGE_PIXELS}-pixel ceiling. It is cover-scaled and cropped to ` +
708
+ 'width x height anyway, so send one closer to the target resolution.');
709
+ }
710
+ // A cancel that lands BEFORE `begin(...)` resolves is not merely early: the
711
+ // registry consumes the marker and SKIPS admission entirely (see the
712
+ // `preCancel` branch in request-registry.ts), so this context holds NO world
713
+ // slot. Another request may own it and be mid-generation right now. Carrying
714
+ // on would tear its session down under it, overwrite the `inFlight` guard
715
+ // teardown waits on, and — via `discardStagedScene` in the `finally` below —
716
+ // delete the staging file it is generating into, since that path is shared per
717
+ // session. Refuse before touching any of it.
718
+ if (ctx.signal.aborted) {
719
+ throw new InferenceCancelledError(ctx.requestId);
720
+ }
721
+ // Replacing the world of a live session: the native session pins the scene it
722
+ // was activated with, and `load()` is a no-op once loaded, so a rewritten pack
723
+ // underneath it would be ignored and the walk would silently continue in the
724
+ // old world. Dropping the session first also frees its ~16 GB before the
725
+ // encoders (~6.9 GB) load, which a 24 GB card needs.
726
+ await session.deactivate();
727
+ // `deactivate()` awaits the session lock and any in-flight native job, so a
728
+ // cancel can land across it. Re-check: the same slot-less race applies to
729
+ // everything below, and unlike `worldStep` there is no abort hook to fall back
730
+ // on — `createScene` takes no abort predicate, so once dispatched it runs to
731
+ // completion whatever the caller does.
732
+ if (ctx.signal.aborted) {
733
+ throw new InferenceCancelledError(ctx.requestId);
734
+ }
735
+ // Generated into a staging file and promoted only on success: a failed
736
+ // generation must leave the caller with the world they already had, not with
737
+ // a model that has none.
738
+ //
739
+ // Every exit that is not a completed promotion drops the staged file, which
740
+ // takes three shapes and needs all three covered. A `catch` alone misses the
741
+ // consumer abandoning the generator — that unwinds as a return completion, so
742
+ // `finally` runs and `catch` does not. A `finally` alone cannot tell success
743
+ // from failure. Hence the flag. The native side can leave a partial file
744
+ // behind on a dispatch rejection too, so the dispatch sits inside the guard
745
+ // rather than in front of it.
746
+ let promoted = false;
747
+ let response;
748
+ let scene;
749
+ try {
750
+ response = (await session.run(() => session.createScene({
751
+ prompt: request.prompt,
752
+ image,
753
+ t5,
754
+ vae,
755
+ output: session.stagingScenePath,
756
+ ...(request.width !== undefined && { width: request.width }),
757
+ ...(request.height !== undefined && { height: request.height })
758
+ })));
759
+ try {
760
+ // The completion payload carries the pack's absolute path on this machine
761
+ // and a timing already reported by stats.sceneCreateMs, so nothing from
762
+ // the stream is forwarded — the caller may be a remote peer and has no
763
+ // business knowing our filesystem layout.
764
+ for await (const chunk of response.iterate()) {
765
+ void chunk;
766
+ }
767
+ }
768
+ finally {
769
+ // Scene creation takes no abort predicate, so cancelling or disconnecting
770
+ // stops delivery but not the encode. This runs even when the consumer
771
+ // abandons the generator, and it sits inside the `await using ctx` scope —
772
+ // so the model's concurrency slot is held until the native job is really
773
+ // done. Releasing it earlier would admit the next request into a session
774
+ // that is still busy.
775
+ await session.settle();
776
+ }
777
+ if (ctx.signal.aborted) {
778
+ throw new InferenceCancelledError(ctx.requestId);
779
+ }
780
+ scene = await session.promoteStagedScene(request.returnPack === true);
781
+ promoted = true;
782
+ // Promotion is not instantaneous: it awaits the session lock, a stat, a
783
+ // rename and optionally a whole readFile. A cancel accepted across any of
784
+ // those would otherwise fall through to the terminal `done` payload below
785
+ // and report success for a request the caller already withdrew.
786
+ //
787
+ // Checked AFTER `promoted = true` on purpose. The rename has happened, so
788
+ // the world is real and the caller's previous one is already gone —
789
+ // discarding now would destroy a valid world to honour a cancel. The
790
+ // soft-cancel contract is that delivery stops, not that the encode is
791
+ // undone; this suppresses the payload and leaves the world in place for the
792
+ // next step to activate.
793
+ if (ctx.signal.aborted) {
794
+ throw new InferenceCancelledError(ctx.requestId);
795
+ }
796
+ }
797
+ finally {
798
+ if (!promoted)
799
+ await session.discardStagedScene();
800
+ }
801
+ requestLogger.info(`World scene pack created for ${request.modelId}` +
802
+ (scene ? ` (${scene.length} bytes returned)` : ' (pack retained server-side)'));
803
+ const { stats } = response;
804
+ yield {
805
+ type: 'worldSceneStream',
806
+ ...(scene && { data: Buffer.from(scene).toString('base64') }),
807
+ done: true,
808
+ ...(stats && { stats })
809
+ };
810
+ }
811
+ catch (e_2) {
812
+ env_2.error = e_2;
813
+ env_2.hasError = true;
814
+ }
815
+ finally {
816
+ const result_2 = __disposeResources(env_2);
817
+ if (result_2)
818
+ await result_2;
819
+ }
820
+ }