@qvac/sdk 0.10.2 → 0.11.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 (200) hide show
  1. package/NOTICE +92 -54
  2. package/dist/client/api/cancel.d.ts +24 -32
  3. package/dist/client/api/cancel.js +58 -31
  4. package/dist/client/api/client-request-id.d.ts +17 -0
  5. package/dist/client/api/client-request-id.js +27 -0
  6. package/dist/client/api/completion-stream.d.ts +1 -1
  7. package/dist/client/api/completion-stream.js +34 -3
  8. package/dist/client/api/diffusion.d.ts +46 -0
  9. package/dist/client/api/diffusion.js +50 -1
  10. package/dist/client/api/download-asset.d.ts +12 -2
  11. package/dist/client/api/download-asset.js +21 -3
  12. package/dist/client/api/embed.d.ts +8 -4
  13. package/dist/client/api/embed.js +14 -1
  14. package/dist/client/api/get-model-info.d.ts +1 -1
  15. package/dist/client/api/index.d.ts +1 -0
  16. package/dist/client/api/index.js +1 -0
  17. package/dist/client/api/load-model.d.ts +12 -4
  18. package/dist/client/api/load-model.js +25 -1
  19. package/dist/client/api/rag.d.ts +9 -3
  20. package/dist/client/api/rag.js +23 -3
  21. package/dist/client/api/transcribe.d.ts +20 -8
  22. package/dist/client/api/transcribe.js +58 -4
  23. package/dist/client/api/unload-model.d.ts +5 -2
  24. package/dist/client/api/unload-model.js +9 -4
  25. package/dist/client/api/upscale.d.ts +45 -0
  26. package/dist/client/api/upscale.js +99 -0
  27. package/dist/client/rpc/expo-rpc-client.js +2 -1
  28. package/dist/client/rpc/node-rpc-client.js +18 -7
  29. package/dist/client/rpc/rpc-client.d.ts +2 -2
  30. package/dist/client/rpc/rpc-client.js +10 -4
  31. package/dist/client/rpc/rpc-error.d.ts +9 -0
  32. package/dist/client/rpc/rpc-error.js +92 -0
  33. package/dist/examples/cancel-by-request-id.d.ts +38 -0
  34. package/dist/examples/cancel-by-request-id.js +103 -0
  35. package/dist/examples/diffusion-esrgan-upscale.d.ts +2 -0
  36. package/dist/examples/diffusion-esrgan-upscale.js +78 -0
  37. package/dist/examples/download-with-cancel.js +10 -10
  38. package/dist/examples/embed-multi-gpu.d.ts +2 -0
  39. package/dist/examples/embed-multi-gpu.js +46 -0
  40. package/dist/examples/llamacpp-multi-gpu.d.ts +2 -0
  41. package/dist/examples/llamacpp-multi-gpu.js +57 -0
  42. package/dist/examples/rag/rag-hyperdb/cancellation.js +20 -17
  43. package/dist/examples/tools/llamacpp-tools-gemma4.d.ts +2 -0
  44. package/dist/examples/tools/llamacpp-tools-gemma4.js +76 -0
  45. package/dist/examples/tools/llamacpp-tools-qwen35.d.ts +2 -0
  46. package/dist/examples/tools/llamacpp-tools-qwen35.js +70 -0
  47. package/dist/examples/transcription/whispercpp-microphone-conversation.d.ts +2 -0
  48. package/dist/examples/transcription/whispercpp-microphone-conversation.js +102 -0
  49. package/dist/examples/tts/supertonic.js +1 -1
  50. package/dist/expo/plugins/withMobileBundle.d.ts +20 -4
  51. package/dist/expo/plugins/withMobileBundle.js +44 -6
  52. package/dist/index.d.ts +5 -2
  53. package/dist/index.js +21 -1
  54. package/dist/models/registry/models.d.ts +186 -318
  55. package/dist/models/registry/models.js +1522 -1667
  56. package/dist/pear/pre.d.ts +3 -0
  57. package/dist/pear/pre.js +3 -0
  58. package/dist/schemas/cancel.d.ts +167 -28
  59. package/dist/schemas/cancel.js +119 -19
  60. package/dist/schemas/common.d.ts +427 -208
  61. package/dist/schemas/common.js +3 -1
  62. package/dist/schemas/completion-event.d.ts +14 -0
  63. package/dist/schemas/completion-event.js +11 -1
  64. package/dist/schemas/completion-stream.d.ts +15 -1
  65. package/dist/schemas/completion-stream.js +25 -7
  66. package/dist/schemas/download-asset.d.ts +15 -9
  67. package/dist/schemas/download-asset.js +17 -6
  68. package/dist/schemas/embed.d.ts +1 -0
  69. package/dist/schemas/embed.js +5 -0
  70. package/dist/schemas/error.d.ts +31 -0
  71. package/dist/schemas/error.js +29 -1
  72. package/dist/schemas/finetune.d.ts +2 -0
  73. package/dist/schemas/finetune.js +5 -0
  74. package/dist/schemas/get-model-info.d.ts +6 -6
  75. package/dist/schemas/llamacpp-config.d.ts +54 -8
  76. package/dist/schemas/llamacpp-config.js +12 -0
  77. package/dist/schemas/load-model.d.ts +1911 -1260
  78. package/dist/schemas/load-model.js +15 -0
  79. package/dist/schemas/model-src-utils.d.ts +14 -14
  80. package/dist/schemas/model-types.d.ts +10 -10
  81. package/dist/schemas/ocr.d.ts +3 -3
  82. package/dist/schemas/plugin.d.ts +40 -1
  83. package/dist/schemas/plugin.js +10 -1
  84. package/dist/schemas/rag.d.ts +9 -0
  85. package/dist/schemas/rag.js +17 -9
  86. package/dist/schemas/registry.d.ts +30 -30
  87. package/dist/schemas/reload-config.d.ts +14 -14
  88. package/dist/schemas/sdcpp-config.d.ts +131 -17
  89. package/dist/schemas/sdcpp-config.js +163 -1
  90. package/dist/schemas/sdk-config.d.ts +649 -271
  91. package/dist/schemas/sdk-errors-client.d.ts +1 -0
  92. package/dist/schemas/sdk-errors-client.js +5 -0
  93. package/dist/schemas/sdk-errors-server.d.ts +5 -0
  94. package/dist/schemas/sdk-errors-server.js +25 -0
  95. package/dist/schemas/text-to-speech.d.ts +78 -78
  96. package/dist/schemas/transcription-config.d.ts +24 -24
  97. package/dist/schemas/transcription.d.ts +56 -2
  98. package/dist/schemas/transcription.js +27 -0
  99. package/dist/schemas/translate.d.ts +2 -0
  100. package/dist/schemas/translate.js +13 -2
  101. package/dist/schemas/translation-config.d.ts +40 -40
  102. package/dist/schemas/unload-model.d.ts +2 -1
  103. package/dist/schemas/unload-model.js +4 -1
  104. package/dist/scripts/run-unit-tests.js +15 -2
  105. package/dist/server/bare/ops/cancel.d.ts +27 -1
  106. package/dist/server/bare/ops/cancel.js +36 -10
  107. package/dist/server/bare/ops/embed.d.ts +3 -3
  108. package/dist/server/bare/ops/embed.js +144 -28
  109. package/dist/server/bare/ops/kv-cache-utils.d.ts +0 -7
  110. package/dist/server/bare/ops/kv-cache-utils.js +8 -47
  111. package/dist/server/bare/ops/transcribe.d.ts +10 -5
  112. package/dist/server/bare/ops/transcribe.js +196 -41
  113. package/dist/server/bare/ops/translate.d.ts +1 -1
  114. package/dist/server/bare/ops/translate.js +197 -88
  115. package/dist/server/bare/plugins/llamacpp-completion/ops/completion-stream.d.ts +12 -3
  116. package/dist/server/bare/plugins/llamacpp-completion/ops/completion-stream.js +170 -172
  117. package/dist/server/bare/plugins/llamacpp-completion/ops/finetune.js +125 -21
  118. package/dist/server/bare/plugins/llamacpp-completion/ops/kv-cache-session.d.ts +154 -0
  119. package/dist/server/bare/plugins/llamacpp-completion/ops/kv-cache-session.js +448 -0
  120. package/dist/server/bare/plugins/llamacpp-completion/ops/kv-cache-state.d.ts +21 -73
  121. package/dist/server/bare/plugins/llamacpp-completion/ops/kv-cache-state.js +18 -101
  122. package/dist/server/bare/plugins/llamacpp-completion/plugin.d.ts +30 -3
  123. package/dist/server/bare/plugins/llamacpp-completion/plugin.js +157 -85
  124. package/dist/server/bare/plugins/llamacpp-completion/transform.d.ts +10 -0
  125. package/dist/server/bare/plugins/llamacpp-completion/transform.js +34 -0
  126. package/dist/server/bare/plugins/llamacpp-embedding/plugin.d.ts +7 -0
  127. package/dist/server/bare/plugins/llamacpp-embedding/plugin.js +10 -1
  128. package/dist/server/bare/plugins/nmtcpp-translation/plugin.d.ts +17 -15
  129. package/dist/server/bare/plugins/nmtcpp-translation/plugin.js +5 -1
  130. package/dist/server/bare/plugins/onnx-ocr/plugin.d.ts +3 -3
  131. package/dist/server/bare/plugins/onnx-ocr/plugin.js +3 -0
  132. package/dist/server/bare/plugins/onnx-tts/plugin.d.ts +39 -39
  133. package/dist/server/bare/plugins/onnx-tts/plugin.js +6 -0
  134. package/dist/server/bare/plugins/parakeet-transcription/plugin.d.ts +29 -21
  135. package/dist/server/bare/plugins/parakeet-transcription/plugin.js +4 -1
  136. package/dist/server/bare/plugins/sdcpp-generation/ops/diffusion.js +8 -0
  137. package/dist/server/bare/plugins/sdcpp-generation/ops/upscale.d.ts +3 -0
  138. package/dist/server/bare/plugins/sdcpp-generation/ops/upscale.js +40 -0
  139. package/dist/server/bare/plugins/sdcpp-generation/plugin.d.ts +91 -16
  140. package/dist/server/bare/plugins/sdcpp-generation/plugin.js +97 -7
  141. package/dist/server/bare/plugins/whispercpp-transcription/plugin.d.ts +22 -3
  142. package/dist/server/bare/plugins/whispercpp-transcription/plugin.js +50 -14
  143. package/dist/server/bare/rag-hyperdb/index.d.ts +1 -1
  144. package/dist/server/bare/rag-hyperdb/index.js +1 -1
  145. package/dist/server/bare/rag-hyperdb/rag-operation-manager.d.ts +26 -4
  146. package/dist/server/bare/rag-hyperdb/rag-operation-manager.js +65 -28
  147. package/dist/server/bare/runtime/disposable-scope.d.ts +36 -0
  148. package/dist/server/bare/runtime/disposable-scope.js +69 -0
  149. package/dist/server/bare/runtime/index.d.ts +8 -0
  150. package/dist/server/bare/runtime/index.js +3 -0
  151. package/dist/server/bare/runtime/request-context.d.ts +49 -0
  152. package/dist/server/bare/runtime/request-context.js +1 -0
  153. package/dist/server/bare/runtime/request-id.d.ts +12 -0
  154. package/dist/server/bare/runtime/request-id.js +14 -0
  155. package/dist/server/bare/runtime/request-registry-singleton.d.ts +4 -0
  156. package/dist/server/bare/runtime/request-registry-singleton.js +27 -0
  157. package/dist/server/bare/runtime/request-registry.d.ts +136 -0
  158. package/dist/server/bare/runtime/request-registry.js +297 -0
  159. package/dist/server/bare/runtime/with-request-context.d.ts +22 -0
  160. package/dist/server/bare/runtime/with-request-context.js +53 -0
  161. package/dist/server/bare/types/addon-responses.d.ts +11 -1
  162. package/dist/server/rpc/handler-registry.js +26 -7
  163. package/dist/server/rpc/handlers/cancel-delegated.js +28 -31
  164. package/dist/server/rpc/handlers/cancelHandler.d.ts +30 -1
  165. package/dist/server/rpc/handlers/cancelHandler.js +54 -22
  166. package/dist/server/rpc/handlers/delete-cache.d.ts +9 -0
  167. package/dist/server/rpc/handlers/delete-cache.js +13 -15
  168. package/dist/server/rpc/handlers/download-asset.js +133 -40
  169. package/dist/server/rpc/handlers/index.d.ts +2 -0
  170. package/dist/server/rpc/handlers/index.js +2 -0
  171. package/dist/server/rpc/handlers/load-model/download-manager.d.ts +60 -2
  172. package/dist/server/rpc/handlers/load-model/download-manager.js +141 -8
  173. package/dist/server/rpc/handlers/load-model/handler.js +213 -98
  174. package/dist/server/rpc/handlers/load-model/http.js +3 -3
  175. package/dist/server/rpc/handlers/load-model/hyperdrive.js +1 -1
  176. package/dist/server/rpc/handlers/load-model/registry.js +1 -1
  177. package/dist/server/rpc/handlers/load-model/resolve-session.d.ts +20 -1
  178. package/dist/server/rpc/handlers/load-model/resolve-session.js +6 -5
  179. package/dist/server/rpc/handlers/load-model/resolve.d.ts +3 -2
  180. package/dist/server/rpc/handlers/load-model/resolve.js +9 -6
  181. package/dist/server/rpc/handlers/load-model/types.d.ts +24 -0
  182. package/dist/server/rpc/handlers/rag.js +133 -15
  183. package/dist/server/rpc/handlers/upscale-stream.d.ts +3 -0
  184. package/dist/server/rpc/handlers/upscale-stream.js +4 -0
  185. package/dist/server/utils/completion-normalizer.js +8 -0
  186. package/dist/server/utils/tools/dialect.js +4 -0
  187. package/dist/server/utils/tools/parser.js +16 -1
  188. package/dist/server/utils/tools/parsers/gemma4native.d.ts +4 -0
  189. package/dist/server/utils/tools/parsers/gemma4native.js +60 -0
  190. package/dist/server/utils/tools/parsers/qwen35.d.ts +4 -0
  191. package/dist/server/utils/tools/parsers/qwen35.js +120 -0
  192. package/dist/utils/aggregate-events.d.ts +9 -0
  193. package/dist/utils/aggregate-events.js +18 -3
  194. package/dist/utils/decorate-promise.d.ts +34 -0
  195. package/dist/utils/decorate-promise.js +35 -0
  196. package/dist/utils/errors-client.d.ts +3 -0
  197. package/dist/utils/errors-client.js +5 -0
  198. package/dist/utils/errors-server.d.ts +85 -0
  199. package/dist/utils/errors-server.js +97 -0
  200. package/package.json +20 -15
@@ -1,3 +1,32 @@
1
1
  import type { CancelRequest, CancelResponse } from "../../../schemas/cancel";
2
- export declare function cancelHandler(request: CancelRequest): Promise<CancelResponse>;
2
+ /**
3
+ * Cancel RPC entry point. The 5-arm `switch (request.operation)`
4
+ * dispatcher that lived here through 0.10.x was retired in 0.11.0:
5
+ * every long-running handler now registers itself on the
6
+ * worker-singleton `RequestRegistry`, so the cancel surface narrows to
7
+ * two paths that route through the same registry primitive:
8
+ *
9
+ * - `{ operation: "request", requestId, clearCache? }` — targeted
10
+ * cancel by client-generated id. Looks up the registry entry,
11
+ * fires its abort signal, optionally marks the underlying download
12
+ * transfer for cache clear. The "stop-button race" case (client
13
+ * cancel beats its own begin to the worker) is handled inside the
14
+ * registry via the cancel-before-begin tripwire.
15
+ *
16
+ * - `{ operation: "broad", modelId, kind? }` — abort every in-flight
17
+ * request on a model (optionally narrowed by `kind`). Used for
18
+ * model unload, app shutdown, and admin sweeps where the caller
19
+ * has no `requestId`. Delegates to the `cancel` bare op so the
20
+ * `ModelNotLoadedError` validation is shared with internal
21
+ * server-side broad cancels.
22
+ *
23
+ * Always returns `success: true` plus a `cancelled` count (the number
24
+ * of contexts this call flipped to `cancelling` — already-cancelled
25
+ * contexts are not counted). A targeted cancel with no in-flight
26
+ * match still returns `success: true, cancelled: 0`; the
27
+ * cancel-before-begin tripwire ensures the cancel is applied
28
+ * retroactively if a matching begin arrives within the registry's
29
+ * race window.
30
+ */
31
+ export declare function cancelHandler(request: CancelRequest): CancelResponse;
3
32
  //# sourceMappingURL=cancelHandler.d.ts.map
@@ -1,30 +1,62 @@
1
- import { cancel } from "../../bare/ops/cancel.js";
2
- import { cancelTransfer } from "./load-model/download-manager.js";
3
- import { cancelRagOperation, DEFAULT_WORKSPACE, } from "../../bare/rag-hyperdb/index.js";
1
+ import { cancel as cancelByModelId } from "../../bare/ops/cancel.js";
2
+ import { getRequestRegistry } from "../../bare/runtime/index.js";
3
+ import { markClearCacheForRequest } from "./load-model/download-manager.js";
4
4
  import { getServerLogger } from "../../../logging/index.js";
5
5
  const logger = getServerLogger();
6
- export async function cancelHandler(request) {
6
+ /**
7
+ * Cancel RPC entry point. The 5-arm `switch (request.operation)`
8
+ * dispatcher that lived here through 0.10.x was retired in 0.11.0:
9
+ * every long-running handler now registers itself on the
10
+ * worker-singleton `RequestRegistry`, so the cancel surface narrows to
11
+ * two paths that route through the same registry primitive:
12
+ *
13
+ * - `{ operation: "request", requestId, clearCache? }` — targeted
14
+ * cancel by client-generated id. Looks up the registry entry,
15
+ * fires its abort signal, optionally marks the underlying download
16
+ * transfer for cache clear. The "stop-button race" case (client
17
+ * cancel beats its own begin to the worker) is handled inside the
18
+ * registry via the cancel-before-begin tripwire.
19
+ *
20
+ * - `{ operation: "broad", modelId, kind? }` — abort every in-flight
21
+ * request on a model (optionally narrowed by `kind`). Used for
22
+ * model unload, app shutdown, and admin sweeps where the caller
23
+ * has no `requestId`. Delegates to the `cancel` bare op so the
24
+ * `ModelNotLoadedError` validation is shared with internal
25
+ * server-side broad cancels.
26
+ *
27
+ * Always returns `success: true` plus a `cancelled` count (the number
28
+ * of contexts this call flipped to `cancelling` — already-cancelled
29
+ * contexts are not counted). A targeted cancel with no in-flight
30
+ * match still returns `success: true, cancelled: 0`; the
31
+ * cancel-before-begin tripwire ensures the cancel is applied
32
+ * retroactively if a matching begin arrives within the registry's
33
+ * race window.
34
+ */
35
+ export function cancelHandler(request) {
7
36
  try {
8
- switch (request.operation) {
9
- case "inference":
10
- case "embeddings":
11
- await cancel({ modelId: request.modelId });
12
- break;
13
- case "downloadAsset":
14
- cancelTransfer(request.downloadKey, request.clearCache);
15
- break;
16
- case "rag": {
17
- const cancelled = cancelRagOperation(request.workspace);
18
- if (!cancelled) {
19
- logger.warn(`No active RAG operation to cancel for workspace: ${request.workspace ?? DEFAULT_WORKSPACE}`);
20
- }
21
- break;
37
+ if (request.operation === "request") {
38
+ if (request.clearCache) {
39
+ markClearCacheForRequest(request.requestId);
22
40
  }
41
+ const cancelled = getRequestRegistry().cancel({
42
+ requestId: request.requestId,
43
+ });
44
+ if (cancelled === 0) {
45
+ // info-level (not debug) because the decorated-promise pattern
46
+ // makes "no in-flight match" a common and user-visible case:
47
+ // a Stop button fired after the request settled but before
48
+ // the UI cleared lands here. The cancel-before-begin tripwire
49
+ // inside the registry already captured the cancel for any
50
+ // matching begin in flight; this log just helps operators
51
+ // debugging "my Stop button isn't working" without lowering
52
+ // the log level.
53
+ logger.info(`[cancel] no in-flight request matched requestId=${request.requestId}`);
54
+ }
55
+ return { type: "cancel", success: true, cancelled };
23
56
  }
24
- return {
25
- type: "cancel",
26
- success: true,
27
- };
57
+ // operation === "broad"
58
+ const cancelled = cancelByModelId({ modelId: request.modelId }, request.kind ? { kind: request.kind } : undefined);
59
+ return { type: "cancel", success: true, cancelled };
28
60
  }
29
61
  catch (error) {
30
62
  logger.error("Error during cancellation:", error);
@@ -1,3 +1,12 @@
1
1
  import type { DeleteCacheRequest, DeleteCacheResponse } from "../../../schemas/index";
2
+ /**
3
+ * RPC handler for `deleteCache(...)`. The three KV-cache bookkeeping
4
+ * layers (on-disk `.bin`, `initializedCaches`, `cachedMessageCounts`)
5
+ * are private to `kv-cache-session.ts`; this handler delegates to that
6
+ * module's single administrative entry point (`deleteKvCacheState`).
7
+ * The handler must have **zero** direct references to
8
+ * `fsPromises.unlink`, the `initializedCaches` set, or the
9
+ * `cachedMessageCounts` map.
10
+ */
2
11
  export declare function handleDeleteCache(request: DeleteCacheRequest): Promise<DeleteCacheResponse>;
3
12
  //# sourceMappingURL=delete-cache.d.ts.map
@@ -1,26 +1,24 @@
1
- import { clearCacheRegistry, deleteCache as deleteCacheUtil, } from "../../bare/ops/kv-cache-utils.js";
2
- import { clearCachedMessageCounts } from "../../bare/plugins/llamacpp-completion/ops/completion-stream.js";
1
+ import { deleteKvCacheState } from "../../bare/plugins/llamacpp-completion/ops/kv-cache-session.js";
3
2
  import { getServerLogger } from "../../../logging/index.js";
4
3
  const logger = getServerLogger();
4
+ /**
5
+ * RPC handler for `deleteCache(...)`. The three KV-cache bookkeeping
6
+ * layers (on-disk `.bin`, `initializedCaches`, `cachedMessageCounts`)
7
+ * are private to `kv-cache-session.ts`; this handler delegates to that
8
+ * module's single administrative entry point (`deleteKvCacheState`).
9
+ * The handler must have **zero** direct references to
10
+ * `fsPromises.unlink`, the `initializedCaches` set, or the
11
+ * `cachedMessageCounts` map.
12
+ */
5
13
  export async function handleDeleteCache(request) {
6
14
  try {
7
15
  if ("all" in request && request.all) {
8
- await deleteCacheUtil({ all: true });
9
- clearCachedMessageCounts();
10
- clearCacheRegistry();
16
+ await deleteKvCacheState({ all: true });
11
17
  }
12
18
  else if ("kvCacheKey" in request) {
13
- const params = {
19
+ await deleteKvCacheState({
14
20
  kvCacheKey: request.kvCacheKey,
15
- };
16
- if (request.modelId !== undefined) {
17
- params.modelId = request.modelId;
18
- }
19
- const removedPath = await deleteCacheUtil(params);
20
- clearCachedMessageCounts(removedPath);
21
- clearCacheRegistry({
22
- cacheKey: request.kvCacheKey,
23
- modelId: request.modelId,
21
+ ...(request.modelId !== undefined && { modelId: request.modelId }),
24
22
  });
25
23
  }
26
24
  return {
@@ -1,53 +1,146 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
1
53
  import { PROFILING_KEY, OPERATION_EVENT_KEY, } from "../../../schemas/index.js";
2
54
  import { resolveModelPath, resolveModelPathWithStats, } from "./load-model/resolve.js";
3
- import { buildDownloadProfilingFields } from "./load-model/types.js";
55
+ import { buildDownloadProfilingFields, } from "./load-model/types.js";
4
56
  import { nowMs, generateProfileId } from "../../../profiling/clock.js";
5
57
  import { getServerLogger } from "../../../logging/index.js";
58
+ import { getRequestRegistry, withRequestContext, } from "../../bare/runtime/index.js";
59
+ import { generateServerRequestId } from "../../bare/runtime/request-id.js";
60
+ import { InferenceCancelledError } from "../../../utils/errors-server.js";
6
61
  const logger = getServerLogger();
7
62
  export async function handleDownloadAsset(request, progressCallback) {
8
- const { assetSrc, seed } = request;
9
- const profilingMeta = request[PROFILING_KEY];
10
- const profilingEnabled = profilingMeta?.enabled !== false && !!profilingMeta;
63
+ const env_1 = { stack: [], error: void 0, hasError: false };
11
64
  try {
12
- const totalDownloadStart = profilingEnabled ? nowMs() : 0;
13
- let sourceType;
14
- let downloadStats;
15
- if (profilingEnabled) {
16
- const result = await resolveModelPathWithStats(assetSrc, progressCallback, seed);
17
- sourceType = result.sourceType;
18
- downloadStats = result.downloadStats;
19
- }
20
- else {
21
- await resolveModelPath(assetSrc, progressCallback, seed);
22
- }
23
- const response = {
24
- type: "downloadAsset",
25
- success: true,
26
- assetId: assetSrc,
65
+ const { assetSrc, seed } = request;
66
+ const profilingMeta = request[PROFILING_KEY];
67
+ const profilingEnabled = profilingMeta?.enabled !== false && !!profilingMeta;
68
+ const requestId = request.requestId ?? generateServerRequestId();
69
+ // `downloadAsset` is artifact-shaped, not model-shaped — there is no
70
+ // `modelId` to register on the registry entry. Cancel by `requestId`
71
+ // is the primary path; `cancel({ modelId })` is intentionally a
72
+ // non-match for this kind.
73
+ const ctx = __addDisposableResource(env_1, getRequestRegistry().begin({
74
+ requestId,
75
+ kind: "downloadAsset",
76
+ }), true);
77
+ const log = withRequestContext(getServerLogger(), ctx);
78
+ log.debug(`downloadAsset start assetSrc=${assetSrc}`);
79
+ const hooks = {
80
+ requestBinding: {
81
+ signal: ctx.signal,
82
+ scope: ctx.scope,
83
+ requestId,
84
+ },
27
85
  };
28
- if (profilingEnabled) {
29
- const totalDownloadTimeMs = nowMs() - totalDownloadStart;
30
- const profileId = profilingMeta?.id ?? generateProfileId();
31
- const { gauges, tags } = buildDownloadProfilingFields(downloadStats, sourceType);
32
- gauges["totalDownloadTime"] = totalDownloadTimeMs;
33
- const operationEvent = {
34
- op: "downloadAsset",
35
- kind: "handler",
36
- ms: totalDownloadTimeMs,
37
- profileId,
38
- gauges: Object.keys(gauges).length > 0 ? gauges : undefined,
39
- tags: Object.keys(tags).length > 0 ? tags : undefined,
86
+ try {
87
+ const totalDownloadStart = profilingEnabled ? nowMs() : 0;
88
+ let sourceType;
89
+ let downloadStats;
90
+ if (profilingEnabled) {
91
+ const result = await resolveModelPathWithStats(assetSrc, progressCallback, seed, ctx.signal, hooks);
92
+ sourceType = result.sourceType;
93
+ downloadStats = result.downloadStats;
94
+ }
95
+ else {
96
+ await resolveModelPath(assetSrc, progressCallback, seed, ctx.signal, hooks);
97
+ }
98
+ const response = {
99
+ type: "downloadAsset",
100
+ success: true,
101
+ assetId: assetSrc,
102
+ };
103
+ if (profilingEnabled) {
104
+ const totalDownloadTimeMs = nowMs() - totalDownloadStart;
105
+ const profileId = profilingMeta?.id ?? generateProfileId();
106
+ const { gauges, tags } = buildDownloadProfilingFields(downloadStats, sourceType);
107
+ gauges["totalDownloadTime"] = totalDownloadTimeMs;
108
+ const operationEvent = {
109
+ op: "downloadAsset",
110
+ kind: "handler",
111
+ ms: totalDownloadTimeMs,
112
+ profileId,
113
+ gauges: Object.keys(gauges).length > 0 ? gauges : undefined,
114
+ tags: Object.keys(tags).length > 0 ? tags : undefined,
115
+ };
116
+ response[OPERATION_EVENT_KEY] = operationEvent;
117
+ }
118
+ return response;
119
+ }
120
+ catch (error) {
121
+ // Mirror the load-model handler's cancel contract: a typed cancel
122
+ // bubbles up as the rejection so client-side callers can branch on
123
+ // `instanceof InferenceCancelledError`. Every other error keeps
124
+ // the legacy `success: false` envelope.
125
+ if (error instanceof InferenceCancelledError) {
126
+ log.info(`downloadAsset cancelled requestId=${requestId}`);
127
+ throw error;
128
+ }
129
+ logger.error("Error downloading asset:", error);
130
+ return {
131
+ type: "downloadAsset",
132
+ success: false,
133
+ error: error instanceof Error ? error.message : "Unknown error",
40
134
  };
41
- response[OPERATION_EVENT_KEY] = operationEvent;
42
135
  }
43
- return response;
44
136
  }
45
- catch (error) {
46
- logger.error("Error downloading asset:", error);
47
- return {
48
- type: "downloadAsset",
49
- success: false,
50
- error: error instanceof Error ? error.message : "Unknown error",
51
- };
137
+ catch (e_1) {
138
+ env_1.error = e_1;
139
+ env_1.hasError = true;
140
+ }
141
+ finally {
142
+ const result_1 = __disposeResources(env_1);
143
+ if (result_1)
144
+ await result_1;
52
145
  }
53
146
  }
@@ -20,6 +20,7 @@ import { handleFinetune } from "./finetune";
20
20
  import { handleOCRStream } from "./ocr-stream";
21
21
  import { handleHeartbeat } from "./heartbeat";
22
22
  import { handleDiffusionStream } from "./diffusion-stream";
23
+ import { handleUpscaleStream } from "./upscale-stream";
23
24
  import { handlePluginInvoke, handlePluginInvokeStream } from "./plugin-invoke";
24
25
  import { handleModelRegistryList, handleModelRegistrySearch, handleModelRegistryGetModel } from "./registry";
25
26
  import { handleSuspend } from "./suspend";
@@ -48,6 +49,7 @@ export declare const handlers: {
48
49
  finetune: typeof handleFinetune;
49
50
  ocrStream: typeof handleOCRStream;
50
51
  diffusionStream: typeof handleDiffusionStream;
52
+ upscaleStream: typeof handleUpscaleStream;
51
53
  pluginInvoke: typeof handlePluginInvoke;
52
54
  pluginInvokeStream: typeof handlePluginInvokeStream;
53
55
  modelRegistryList: typeof handleModelRegistryList;
@@ -20,6 +20,7 @@ import { handleFinetune } from "./finetune.js";
20
20
  import { handleOCRStream } from "./ocr-stream.js";
21
21
  import { handleHeartbeat } from "./heartbeat.js";
22
22
  import { handleDiffusionStream } from "./diffusion-stream.js";
23
+ import { handleUpscaleStream } from "./upscale-stream.js";
23
24
  import { handlePluginInvoke, handlePluginInvokeStream } from "./plugin-invoke.js";
24
25
  import { handleModelRegistryList, handleModelRegistrySearch, handleModelRegistryGetModel, } from "./registry.js";
25
26
  import { handleSuspend } from "./suspend.js";
@@ -48,6 +49,7 @@ export const handlers = {
48
49
  finetune: handleFinetune,
49
50
  ocrStream: handleOCRStream,
50
51
  diffusionStream: handleDiffusionStream,
52
+ upscaleStream: handleUpscaleStream,
51
53
  pluginInvoke: handlePluginInvoke,
52
54
  pluginInvokeStream: handlePluginInvokeStream,
53
55
  modelRegistryList: handleModelRegistryList,
@@ -1,6 +1,29 @@
1
1
  import type { ModelProgressUpdate } from "../../../../schemas/index";
2
- import { AbortController } from "bare-abort-controller";
2
+ import { AbortController, type AbortSignal } from "bare-abort-controller";
3
+ import type { DisposableScope } from "../../../bare/runtime/disposable-scope";
3
4
  import type { DownloadHooks } from "./types";
5
+ /**
6
+ * Per-subscriber binding to a registry-tracked request.
7
+ * `startOrJoinDownload` is request-aware: each caller (the
8
+ * `await using ctx = registry.begin(...)` inside `handleLoadModel` /
9
+ * `handleDownloadAsset`) registers a subscriber bound to its
10
+ * `requestId`. A `cancel({ requestId })` against the registry aborts
11
+ * the subscriber's `ctx.signal`, which:
12
+ * - rejects this subscriber's promise so the awaiting handler unwinds;
13
+ * - removes this subscriber from `transfer.subscribers`;
14
+ * - tears down the transfer iff this was the last subscriber.
15
+ *
16
+ * The shared-transfer dedup logic in `startOrJoinDownload` is preserved
17
+ * — two callers requesting the same `downloadKey` still share one
18
+ * underlying download — but cancel is per-`requestId`-honest:
19
+ * cancelling one subscriber does not affect siblings on the same
20
+ * `downloadKey`.
21
+ */
22
+ export interface SubscriberRequestBinding {
23
+ signal: AbortSignal;
24
+ scope: DisposableScope;
25
+ requestId: string;
26
+ }
4
27
  export interface Subscriber {
5
28
  id: string;
6
29
  onProgress?: ((progress: ModelProgressUpdate) => void) | undefined;
@@ -8,6 +31,8 @@ export interface Subscriber {
8
31
  resolve: (path: string) => void;
9
32
  reject: (error: unknown) => void;
10
33
  promise: Promise<string>;
34
+ /** Identity of the registry request this subscriber belongs to, if any. */
35
+ requestId?: string | undefined;
11
36
  }
12
37
  export interface Transfer {
13
38
  downloadKey: string;
@@ -29,7 +54,40 @@ export interface StartOrJoinResult {
29
54
  joined: boolean;
30
55
  getCacheHit: () => boolean | undefined;
31
56
  }
32
- export declare function startOrJoinDownload(downloadKey: string, startDownload: (ctx: DownloadContext) => Promise<string>, onProgress?: (progress: ModelProgressUpdate) => void): StartOrJoinResult;
57
+ export declare function startOrJoinDownload(downloadKey: string, startDownload: (ctx: DownloadContext) => Promise<string>, onProgress?: (progress: ModelProgressUpdate) => void, request?: SubscriberRequestBinding): StartOrJoinResult;
58
+ /**
59
+ * Legacy cancel entry point. Callers (`cancelHandler`'s deprecated
60
+ * `case "downloadAsset"` arm, the shutdown sweep, intra-resolve
61
+ * cleanup) call this with a `downloadKey`. The single source of cancel
62
+ * routing is the request registry, so this function resolves each
63
+ * subscriber's request via `registry.cancel({ requestId })`.
64
+ *
65
+ * Subscribers without a `requestId` (legacy callers that didn't pass a
66
+ * registry binding) are settled directly with `DownloadCancelledError`
67
+ * so we don't leak the transfer if a legacy code path holds the only
68
+ * reference.
69
+ */
70
+ /**
71
+ * Set `clearCache=true` on the transfer that owns the subscriber bound
72
+ * to `requestId`, so when the registry's targeted cancel removes that
73
+ * subscriber and we reach last-subscriber teardown, the partial
74
+ * download file is deleted instead of preserved for automatic resume.
75
+ *
76
+ * Lookup is O(transfers * subscribers); transfers are short-lived and
77
+ * subscriber counts per transfer are tiny in practice, so this is
78
+ * fine. Returns `true` if a matching subscriber was found, `false`
79
+ * otherwise — the cancel handler treats both cases identically (the
80
+ * registry cancel still fires) and the return value is informational.
81
+ *
82
+ * Added in 0.11.0 to support `cancel({ requestId, clearCache: true })`
83
+ * for download requests after the wire schema collapse removed the
84
+ * `{ operation: "downloadAsset", downloadKey, clearCache }` arm. The
85
+ * subscriber is the unit of `clearCache` even though the flag lives on
86
+ * the shared transfer: if any subscriber on the transfer asks for
87
+ * clearCache, the partial file is deleted when the last subscriber
88
+ * leaves, matching the pre-collapse behaviour.
89
+ */
90
+ export declare function markClearCacheForRequest(requestId: string): boolean;
33
91
  export declare function cancelTransfer(downloadKey: string, clearCache?: boolean): void;
34
92
  export declare function createHyperdriveDownloadKey(hyperdriveKey: string, modelFileName: string): string;
35
93
  export declare function createHttpDownloadKey(url: string): string;