@qvac/inference 0.19.1 → 0.20.1

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 (152) hide show
  1. package/NOTICE +57 -17
  2. package/dist/api/assess-model-fit.d.ts +9 -4
  3. package/dist/api/assess-model-fit.js +9 -4
  4. package/dist/api/audio-gen.d.ts +42 -1
  5. package/dist/api/audio-gen.js +137 -51
  6. package/dist/api/index.d.ts +2 -1
  7. package/dist/api/index.js +2 -1
  8. package/dist/api/text-to-speech.d.ts +31 -4
  9. package/dist/api/text-to-speech.js +117 -21
  10. package/dist/api/vector-index.d.ts +142 -0
  11. package/dist/api/vector-index.js +168 -0
  12. package/dist/api/world.d.ts +2 -2
  13. package/dist/errors/index.d.ts +12 -0
  14. package/dist/errors/index.js +21 -0
  15. package/dist/handlers/assess-model-fit.d.ts +30 -6
  16. package/dist/handlers/assess-model-fit.js +68 -6
  17. package/dist/handlers/load-model/registry-download-utils.d.ts +1 -0
  18. package/dist/handlers/load-model/registry-download-utils.js +2 -1
  19. package/dist/handlers/vector-index.d.ts +2 -0
  20. package/dist/handlers/vector-index.js +40 -0
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/models/registry/models.d.ts +351 -12
  24. package/dist/models/registry/models.js +2271 -1957
  25. package/dist/models/registry/resource-profiles.js +135 -0
  26. package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.d.ts +2 -0
  27. package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.js +65 -0
  28. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-input.d.ts +9 -1
  29. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-input.js +24 -7
  30. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.d.ts +46 -0
  31. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.js +218 -0
  32. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.d.ts +1 -1
  33. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.js +56 -225
  34. package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.d.ts +8 -0
  35. package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.js +28 -0
  36. package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +248 -0
  37. package/dist/plugins/builtin/audiogen-ggml/plugin.js +17 -1
  38. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +5 -2
  39. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +1 -2
  40. package/dist/plugins/builtin/ggml-ocr/plugin.d.ts +1 -1
  41. package/dist/plugins/builtin/ggml-ocr/plugin.js +1 -2
  42. package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +6 -4
  43. package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.d.ts +1 -2
  44. package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.js +0 -4
  45. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.d.ts +1 -0
  46. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.js +3 -0
  47. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.d.ts +9 -0
  48. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +96 -89
  49. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.d.ts +0 -1
  50. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.js +0 -31
  51. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.d.ts +40 -24
  52. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.js +187 -123
  53. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-state.d.ts +11 -4
  54. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-state.js +18 -16
  55. package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +69 -4
  56. package/dist/plugins/builtin/llamacpp-completion/plugin.js +1 -2
  57. package/dist/plugins/builtin/llamacpp-completion/transform.js +19 -0
  58. package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +1 -2
  59. package/dist/plugins/builtin/llamacpp-embedding/plugin.js +2 -38
  60. package/dist/plugins/builtin/llamacpp-embedding/transform.d.ts +9 -0
  61. package/dist/plugins/builtin/llamacpp-embedding/transform.js +42 -0
  62. package/dist/plugins/builtin/nmtcpp-translation/plugin.d.ts +1 -1
  63. package/dist/plugins/builtin/nmtcpp-translation/plugin.js +1 -2
  64. package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +1 -1
  65. package/dist/plugins/builtin/parakeet-transcription/plugin.js +1 -2
  66. package/dist/plugins/builtin/sdcpp-generation/ops/video.d.ts +1 -0
  67. package/dist/plugins/builtin/sdcpp-generation/ops/video.js +8 -2
  68. package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +9 -4
  69. package/dist/plugins/builtin/sdcpp-generation/plugin.js +51 -19
  70. package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.d.ts +29 -0
  71. package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.js +66 -0
  72. package/dist/plugins/builtin/tts-ggml/ops/parler-options.js +10 -0
  73. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.d.ts +3 -5
  74. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.js +143 -28
  75. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.d.ts +5 -2
  76. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +182 -66
  77. package/dist/plugins/builtin/tts-ggml/plugin.d.ts +267 -22
  78. package/dist/plugins/builtin/tts-ggml/plugin.js +94 -35
  79. package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +1 -1
  80. package/dist/plugins/builtin/whispercpp-transcription/plugin.js +1 -2
  81. package/dist/plugins/index.d.ts +1 -1
  82. package/dist/plugins/index.js +1 -1
  83. package/dist/plugins/ops/load-model.js +22 -2
  84. package/dist/plugins/ops/translate.js +58 -12
  85. package/dist/plugins/ops/translatepsy-utils.d.ts +17 -0
  86. package/dist/plugins/ops/translatepsy-utils.js +32 -0
  87. package/dist/plugins/registry.d.ts +15 -0
  88. package/dist/plugins/registry.js +133 -15
  89. package/dist/plugins/turbovec-backend.d.ts +17 -0
  90. package/dist/plugins/turbovec-backend.js +99 -0
  91. package/dist/registry.js +4 -0
  92. package/dist/resources/model-fit/assess.d.ts +10 -1
  93. package/dist/resources/model-fit/assess.js +43 -0
  94. package/dist/resources/model-fit/fit-stub/fetch-fit-stub.d.ts +83 -0
  95. package/dist/resources/model-fit/fit-stub/fetch-fit-stub.js +133 -0
  96. package/dist/resources/model-fit/fit-stub/index.d.ts +2 -0
  97. package/dist/resources/model-fit/fit-stub/index.js +2 -0
  98. package/dist/resources/model-fit/fit-stub/project-fit-from-stub.d.ts +31 -0
  99. package/dist/resources/model-fit/fit-stub/project-fit-from-stub.js +36 -0
  100. package/dist/resources/model-fit/native-probe/advisory-fit.d.ts +46 -0
  101. package/dist/resources/model-fit/native-probe/advisory-fit.js +214 -0
  102. package/dist/resources/model-fit/native-probe/create-llama-fit-request.d.ts +26 -0
  103. package/dist/resources/model-fit/native-probe/create-llama-fit-request.js +191 -0
  104. package/dist/resources/model-fit/native-probe/run-in-process-fit.d.ts +42 -0
  105. package/dist/resources/model-fit/native-probe/run-in-process-fit.js +192 -0
  106. package/dist/resources/model-fit/native-probe/run-isolated-fit.d.ts +70 -0
  107. package/dist/resources/model-fit/native-probe/run-isolated-fit.js +372 -0
  108. package/dist/runtime/env.d.ts +2 -0
  109. package/dist/runtime/env.js +9 -2
  110. package/dist/runtime/lifecycle.js +2 -0
  111. package/dist/runtime/model-registry.d.ts +3 -1
  112. package/dist/runtime/model-registry.js +2 -1
  113. package/dist/runtime/request-context.js +17 -0
  114. package/dist/runtime/vector-index-backend.d.ts +25 -0
  115. package/dist/runtime/vector-index-backend.js +1 -0
  116. package/dist/runtime/vector-index-registry.d.ts +76 -0
  117. package/dist/runtime/vector-index-registry.js +161 -0
  118. package/dist/schemas/assess-model-fit.d.ts +67 -0
  119. package/dist/schemas/assess-model-fit.js +56 -2
  120. package/dist/schemas/audio-gen.d.ts +479 -4
  121. package/dist/schemas/audio-gen.js +376 -17
  122. package/dist/schemas/batch-completion-stream.d.ts +7 -0
  123. package/dist/schemas/batch-completion-stream.js +5 -3
  124. package/dist/schemas/common.d.ts +816 -15
  125. package/dist/schemas/common.js +8 -1
  126. package/dist/schemas/completion-event.d.ts +11 -1
  127. package/dist/schemas/completion-event.js +3 -0
  128. package/dist/schemas/completion-stream.d.ts +20 -0
  129. package/dist/schemas/completion-stream.js +41 -4
  130. package/dist/schemas/config.d.ts +186 -30
  131. package/dist/schemas/errors.d.ts +4 -0
  132. package/dist/schemas/errors.js +22 -0
  133. package/dist/schemas/index.d.ts +1 -0
  134. package/dist/schemas/index.js +1 -0
  135. package/dist/schemas/llamacpp-config.d.ts +88 -8
  136. package/dist/schemas/llamacpp-config.js +108 -8
  137. package/dist/schemas/load-model.d.ts +2906 -206
  138. package/dist/schemas/model-config-schemas.d.ts +251 -5
  139. package/dist/schemas/plugin.d.ts +10 -0
  140. package/dist/schemas/sdcpp-config.d.ts +221 -4
  141. package/dist/schemas/sdcpp-config.js +128 -22
  142. package/dist/schemas/text-to-speech.d.ts +780 -19
  143. package/dist/schemas/text-to-speech.js +298 -44
  144. package/dist/schemas/transcription-config.js +7 -4
  145. package/dist/schemas/vector-index.d.ts +194 -0
  146. package/dist/schemas/vector-index.js +185 -0
  147. package/dist/utils/addon-responses.d.ts +29 -3
  148. package/dist/utils/aggregate-events.d.ts +3 -1
  149. package/dist/utils/aggregate-events.js +7 -1
  150. package/dist/utils/tts-stats.d.ts +15 -11
  151. package/dist/utils/tts-stats.js +33 -11
  152. package/package.json +29 -26
@@ -0,0 +1,66 @@
1
+ import { getEngineLogger } from '../../../../logging/index.js';
2
+ import { getRequestRegistry, withRequestContext } from '../../../../runtime/index.js';
3
+ import { generateRequestId } from '../../../../runtime/request-id.js';
4
+ /**
5
+ * The abort is persistent state, not a one-shot poke. The addon's `cancel()`
6
+ * only reaches a native job that is live at that instant, and every chunked
7
+ * path (`stream: true`, `sentenceStream`, the duplex session) runs one native
8
+ * job per sentence — so a cancel that lands between two jobs is a native
9
+ * no-op and synthesis would carry on. Ops call this on every chunk: once the
10
+ * signal is aborted it re-issues the cancel (the next job is live by then),
11
+ * and returns true so the caller drops the output instead of forwarding it.
12
+ */
13
+ export async function cancelIfAborted(model, signal) {
14
+ if (!signal.aborted)
15
+ return false;
16
+ const cancellable = model;
17
+ if (typeof cancellable.cancel === 'function') {
18
+ await cancellable.cancel().catch(() => { });
19
+ }
20
+ return true;
21
+ }
22
+ /**
23
+ * Open the request-registry context that makes the model-scoped hard cancel
24
+ * both TTS handlers advertise actually work, and bind its abort signal to the
25
+ * addon's `cancel()`.
26
+ *
27
+ * `cancel: { scope: 'model', hard: true }` is a claim the runtime does not
28
+ * enforce on its own: `cancelByModelId` walks the request registry, so a run
29
+ * that never opened a context is invisible to it — a caller's `cancel()` would
30
+ * resolve with `cancelled: 0` while the native job ran to completion. Same
31
+ * shape as the transcription and audiogen ops.
32
+ *
33
+ * The returned context is an async disposable: callers must take it with
34
+ * `await using`, and must bail out when `ctx.signal.aborted` is already true on
35
+ * return — a queued request can resume from `begin()` already aborted, and it
36
+ * never owned the model slot, so cancelling then would interrupt the run that
37
+ * does.
38
+ */
39
+ export async function bindTtsCancel(model, modelId, requestId) {
40
+ const ctx = await getRequestRegistry().begin({
41
+ requestId: requestId ?? generateRequestId(),
42
+ kind: 'tts',
43
+ modelId
44
+ });
45
+ if (ctx.signal.aborted)
46
+ return ctx;
47
+ const logger = withRequestContext(getEngineLogger(), ctx);
48
+ const cancellable = model;
49
+ let cancelPromise;
50
+ const onAbort = () => {
51
+ if (typeof cancellable.cancel !== 'function')
52
+ return;
53
+ cancelPromise ??= cancellable.cancel().catch((error) => {
54
+ logger.warn(`[cancel] model.cancel() rejected during abort for modelId=${modelId}: ${error instanceof Error ? error.message : String(error)}`);
55
+ });
56
+ };
57
+ ctx.signal.addEventListener('abort', onAbort, { once: true });
58
+ // Closes the window between the check above and the listener being attached.
59
+ if (ctx.signal.aborted)
60
+ onAbort();
61
+ ctx.scope.defer(async () => {
62
+ ctx.signal.removeEventListener('abort', onAbort);
63
+ await cancelPromise;
64
+ });
65
+ return ctx;
66
+ }
@@ -1,3 +1,4 @@
1
+ import { TTS_COSYVOICE3_EMOTIONS } from '../../../../schemas/index.js';
1
2
  import { PluginRequestValidationFailedError } from '../../../../errors/index.js';
2
3
  function hasEngineType(model) {
3
4
  return (typeof model === 'object' &&
@@ -37,6 +38,15 @@ export function assertParlerJobOptionsSupported(model, options, handlerName) {
37
38
  if (parlerOnlyKeys.length > 0) {
38
39
  throw new PluginRequestValidationFailedError(handlerName, `${parlerOnlyKeys.join(', ')} ${parlerOnlyKeys.length === 1 ? 'is' : 'are'} only supported by Parler TTS models; CosyVoice3 supports emotion and pace`);
39
40
  }
41
+ // The request schema validates `emotion` against Parler's 12-value
42
+ // vocabulary because a request carries only an opaque modelId. Now that
43
+ // the engine is known, narrow it to the four CosyVoice3 has a trained
44
+ // instruction for — the addon rejects the other eight, and rejecting them
45
+ // here names the supported set instead of surfacing a raw addon throw.
46
+ if (options.emotion !== undefined &&
47
+ !TTS_COSYVOICE3_EMOTIONS.includes(options.emotion)) {
48
+ throw new PluginRequestValidationFailedError(handlerName, `emotion "${options.emotion}" is not supported by CosyVoice3; use one of ${TTS_COSYVOICE3_EMOTIONS.join(', ')}`);
49
+ }
40
50
  // CosyVoice3 is trained on one instruction per synthesis; a request that
41
51
  // engages both channels always conflicts regardless of the loaded config.
42
52
  if (options.emotion !== undefined &&
@@ -1,7 +1,5 @@
1
- import { type TextToSpeechStreamRequest, type TtsStats } from '../../../../schemas/index.js';
1
+ import { type TextToSpeechStreamRequest } from '../../../../schemas/index.js';
2
2
  import Buffer from 'bare-buffer';
3
3
  import { type TtsOpYield } from '../../../../utils/tts-stats.js';
4
- export declare function textToSpeechStream(params: TextToSpeechStreamRequest, inputStream: AsyncIterable<Buffer>): AsyncGenerator<TtsOpYield, {
5
- modelExecutionMs: number;
6
- stats?: TtsStats;
7
- }, unknown>;
4
+ import type { TtsOpResult } from './text-to-speech.js';
5
+ export declare function textToSpeechStream(params: TextToSpeechStreamRequest, inputStream: AsyncIterable<Buffer>): AsyncGenerator<TtsOpYield, TtsOpResult, unknown>;
@@ -1,11 +1,105 @@
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 { getModel } from '../../../../runtime/model-registry.js';
2
54
  import { textToSpeechStreamRequestSchema } from '../../../../schemas/index.js';
3
55
  import Buffer from 'bare-buffer';
4
56
  import { nowMs } from '../../../../profiling/index.js';
5
57
  import { buildStreamResult, hasDefinedValues } from '../../../../profiling/model-execution.js';
6
58
  import { TextToSpeechStreamFailedError } from '../../../../errors/index.js';
7
- import { collectTtsStats } from '../../../../utils/tts-stats.js';
59
+ import { collectTtsStats, chunkMetadata } from '../../../../utils/tts-stats.js';
8
60
  import { assertParlerJobOptionsSupported, getParlerJobOptions } from './parler-options.js';
61
+ import { bindTtsCancel, cancelIfAborted } from './cancel-binding.js';
62
+ // Ends the text source the moment the run is cancelled. Without this an idle
63
+ // duplex session — cancelled while the accumulator is waiting for the next
64
+ // fragment, with no native job live — would sit in `runStreaming` holding the
65
+ // model's slot until the client wrote more text, and then synthesise it.
66
+ async function* untilAborted(source, signal) {
67
+ const iterator = source[Symbol.asyncIterator]();
68
+ const aborted = new Promise((resolve) => {
69
+ const end = () => resolve({ done: true, value: undefined });
70
+ if (signal.aborted)
71
+ end();
72
+ else
73
+ signal.addEventListener('abort', end, { once: true });
74
+ });
75
+ try {
76
+ while (true) {
77
+ const next = await Promise.race([iterator.next(), aborted]);
78
+ if (next.done)
79
+ return;
80
+ yield next.value;
81
+ }
82
+ }
83
+ finally {
84
+ const returned = iterator.return?.();
85
+ if (returned !== undefined) {
86
+ // When the abort won the race above, the `iterator.next()` it raced is
87
+ // still pending — the client is idle inside `for await (const buf of
88
+ // inputStream)`. An async generator queues `return()` behind that
89
+ // outstanding `next()`, so awaiting here would not settle until the
90
+ // client sends more text or closes the stream, which is exactly what an
91
+ // idle cancel cannot count on. Blocking would keep this generator alive,
92
+ // so `runStreaming`'s text source would never end, the op would never
93
+ // return, and the model's single concurrency slot would stay held. Let
94
+ // the cleanup settle on its own in that case, and keep awaiting it on
95
+ // the ordinary paths, where no `next()` is outstanding.
96
+ if (signal.aborted)
97
+ void Promise.resolve(returned).catch(() => { });
98
+ else
99
+ await returned;
100
+ }
101
+ }
102
+ }
9
103
  function hasRunStreaming(model) {
10
104
  return (typeof model === 'object' &&
11
105
  model !== null &&
@@ -72,35 +166,56 @@ function buildRunStreamingOptions(request, parlerJobOptions) {
72
166
  return { ...o, ...parlerJobOptions };
73
167
  }
74
168
  export async function* textToSpeechStream(params, inputStream) {
75
- const request = textToSpeechStreamRequestSchema.parse(params);
76
- const model = getModel(request.modelId);
77
- const parlerJobOptions = getParlerJobOptions(request);
78
- assertParlerJobOptionsSupported(model, parlerJobOptions, 'textToSpeechStream');
79
- const modelStart = nowMs();
80
- if (!hasRunStreaming(model)) {
81
- throw new TextToSpeechStreamFailedError('textToSpeechStream requires a TTS model with runStreaming');
82
- }
83
- const textSource = buffersToUtf8Fragments(inputStream);
84
- const streamOpts = buildRunStreamingOptions(request, parlerJobOptions);
85
- const response = await model.runStreaming(textSource, Object.keys(streamOpts).length > 0 ? streamOpts : undefined);
86
- for await (const data of response.iterate()) {
87
- // lunte-disable-next-line eqeqeq -- `== null` intentionally matches null and undefined
88
- if (data.outputArray == null) {
89
- continue;
169
+ const env_1 = { stack: [], error: void 0, hasError: false };
170
+ try {
171
+ const request = textToSpeechStreamRequestSchema.parse(params);
172
+ const model = getModel(request.modelId);
173
+ const parlerJobOptions = getParlerJobOptions(request);
174
+ assertParlerJobOptionsSupported(model, parlerJobOptions, 'textToSpeechStream');
175
+ const ctx = __addDisposableResource(env_1, await bindTtsCancel(model, request.modelId, request.requestId), true);
176
+ if (ctx.signal.aborted)
177
+ return { ...buildStreamResult(0), cancelled: true };
178
+ const modelStart = nowMs();
179
+ if (!hasRunStreaming(model)) {
180
+ throw new TextToSpeechStreamFailedError('textToSpeechStream requires a TTS model with runStreaming');
90
181
  }
91
- const buf = Array.from(data.outputArray);
92
- if (buf.length === 0) {
93
- continue;
182
+ const textSource = untilAborted(buffersToUtf8Fragments(inputStream), ctx.signal);
183
+ const streamOpts = buildRunStreamingOptions(request, parlerJobOptions);
184
+ const response = await model.runStreaming(textSource, Object.keys(streamOpts).length > 0 ? streamOpts : undefined);
185
+ try {
186
+ for await (const data of response.iterate()) {
187
+ if (await cancelIfAborted(model, ctx.signal))
188
+ continue;
189
+ // lunte-disable-next-line eqeqeq -- `== null` intentionally matches null and undefined
190
+ if (data.outputArray == null) {
191
+ continue;
192
+ }
193
+ const buf = Array.from(data.outputArray);
194
+ if (buf.length === 0) {
195
+ continue;
196
+ }
197
+ yield { buffer: buf, ...chunkMetadata(data) };
198
+ }
199
+ }
200
+ catch (error) {
201
+ // The addon fails the response with its own 'Job cancelled' error once a
202
+ // cancel lands on a live job; after an abort that is the expected exit.
203
+ if (!ctx.signal.aborted)
204
+ throw error;
94
205
  }
95
- yield {
96
- buffer: buf,
97
- ...(data.chunkIndex !== undefined ? { chunkIndex: data.chunkIndex } : {}),
98
- ...(typeof data.sentenceChunk === 'string' && data.sentenceChunk.length > 0
99
- ? { sentenceChunk: data.sentenceChunk }
100
- : {})
206
+ const stats = collectTtsStats(response);
207
+ return {
208
+ ...buildStreamResult(nowMs() - modelStart, hasDefinedValues(stats) ? stats : undefined),
209
+ ...(ctx.signal.aborted ? { cancelled: true } : {})
101
210
  };
102
211
  }
103
- const modelExecutionMs = nowMs() - modelStart;
104
- const stats = collectTtsStats(response);
105
- return buildStreamResult(modelExecutionMs, hasDefinedValues(stats) ? stats : undefined);
212
+ catch (e_1) {
213
+ env_1.error = e_1;
214
+ env_1.hasError = true;
215
+ }
216
+ finally {
217
+ const result_1 = __disposeResources(env_1);
218
+ if (result_1)
219
+ await result_1;
220
+ }
106
221
  }
@@ -1,6 +1,9 @@
1
1
  import { type TtsRequest, type TtsStats } from '../../../../schemas/index.js';
2
2
  import { type TtsOpYield } from '../../../../utils/tts-stats.js';
3
- export declare function textToSpeech(params: TtsRequest): AsyncGenerator<TtsOpYield, {
3
+ /** What the handler turns into the terminal frame. */
4
+ export type TtsOpResult = {
4
5
  modelExecutionMs: number;
5
6
  stats?: TtsStats;
6
- }>;
7
+ cancelled?: boolean;
8
+ };
9
+ export declare function textToSpeech(params: TtsRequest): AsyncGenerator<TtsOpYield, TtsOpResult>;
@@ -1,91 +1,207 @@
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 { getModel } from '../../../../runtime/model-registry.js';
2
54
  import { ttsRequestSchema } from '../../../../schemas/index.js';
3
55
  import { nowMs } from '../../../../profiling/index.js';
4
56
  import { buildStreamResult, hasDefinedValues } from '../../../../profiling/model-execution.js';
5
57
  import { TextToSpeechFailedError } from '../../../../errors/index.js';
6
- import { collectTtsStats } from '../../../../utils/tts-stats.js';
58
+ import { collectTtsStats, chunkMetadata } from '../../../../utils/tts-stats.js';
7
59
  import { assertParlerJobOptionsSupported, getParlerJobOptions } from './parler-options.js';
60
+ import { bindTtsCancel, cancelIfAborted } from './cancel-binding.js';
8
61
  function hasRunStream(model) {
9
62
  return (typeof model === 'object' &&
10
63
  model !== null &&
11
64
  'runStream' in model &&
12
65
  typeof model.runStream === 'function');
13
66
  }
67
+ function finish(modelStart, response, cancelled) {
68
+ const stats = collectTtsStats(response);
69
+ return {
70
+ ...buildStreamResult(nowMs() - modelStart, hasDefinedValues(stats) ? stats : undefined),
71
+ ...(cancelled ? { cancelled: true } : {})
72
+ };
73
+ }
74
+ // The addon fails the response with its own 'Job cancelled' error once a
75
+ // cancel lands on a live job. After an abort that is the expected way out,
76
+ // not a failure to surface.
77
+ function rethrowUnlessCancelled(error, signal) {
78
+ if (!signal.aborted)
79
+ throw error;
80
+ }
14
81
  export async function* textToSpeech(params) {
15
- const request = ttsRequestSchema.parse(params);
16
- const { modelId, inputType, text, stream, sentenceStream, sentenceStreamLocale, sentenceStreamMaxChunkScalars } = request;
17
- const parlerJobOptions = getParlerJobOptions(request);
18
- const model = getModel(modelId);
19
- assertParlerJobOptionsSupported(model, parlerJobOptions, 'textToSpeech');
20
- const modelStart = nowMs();
21
- if (sentenceStream) {
22
- if (!hasRunStream(model)) {
23
- throw new TextToSpeechFailedError('sentenceStream requires a TTS model with runStream');
24
- }
25
- const streamOpts = sentenceStreamLocale !== undefined ||
26
- sentenceStreamMaxChunkScalars !== undefined ||
27
- Object.keys(parlerJobOptions).length > 0
28
- ? {
29
- ...(sentenceStreamLocale !== undefined ? { locale: sentenceStreamLocale } : {}),
30
- ...(sentenceStreamMaxChunkScalars !== undefined
31
- ? { maxChunkScalars: sentenceStreamMaxChunkScalars }
32
- : {}),
33
- ...parlerJobOptions
82
+ const env_1 = { stack: [], error: void 0, hasError: false };
83
+ try {
84
+ const request = ttsRequestSchema.parse(params);
85
+ const { modelId, text, stream, sentenceStream, sentenceStreamLocale, sentenceStreamMaxChunkScalars } = request;
86
+ const parlerJobOptions = getParlerJobOptions(request);
87
+ const model = getModel(modelId);
88
+ assertParlerJobOptionsSupported(model, parlerJobOptions, 'textToSpeech');
89
+ const ctx = __addDisposableResource(env_1, await bindTtsCancel(model, modelId, request.requestId), true);
90
+ if (ctx.signal.aborted)
91
+ return { ...buildStreamResult(0), cancelled: true };
92
+ const modelStart = nowMs();
93
+ if (sentenceStream) {
94
+ if (!hasRunStream(model)) {
95
+ throw new TextToSpeechFailedError('sentenceStream requires a TTS model with runStream');
96
+ }
97
+ const streamOpts = sentenceStreamLocale !== undefined ||
98
+ sentenceStreamMaxChunkScalars !== undefined ||
99
+ Object.keys(parlerJobOptions).length > 0
100
+ ? {
101
+ ...(sentenceStreamLocale !== undefined ? { locale: sentenceStreamLocale } : {}),
102
+ ...(sentenceStreamMaxChunkScalars !== undefined
103
+ ? { maxChunkScalars: sentenceStreamMaxChunkScalars }
104
+ : {}),
105
+ ...parlerJobOptions
106
+ }
107
+ : undefined;
108
+ const response = await model.runStream(text, streamOpts);
109
+ if (!stream) {
110
+ let completeBuffer = [];
111
+ let sampleRate;
112
+ try {
113
+ for await (const data of response.iterate()) {
114
+ if (await cancelIfAborted(model, ctx.signal))
115
+ continue;
116
+ // lunte-disable-next-line eqeqeq -- `!= null` intentionally matches null and undefined
117
+ if (data.outputArray != null) {
118
+ sampleRate ??= data.sampleRate;
119
+ completeBuffer = completeBuffer.concat(Array.from(data.outputArray));
120
+ }
121
+ }
122
+ }
123
+ catch (error) {
124
+ rethrowUnlessCancelled(error, ctx.signal);
125
+ }
126
+ // A cancelled collect must not hand back a partial buffer as if complete.
127
+ if (ctx.signal.aborted)
128
+ return finish(modelStart, response, true);
129
+ yield { buffer: completeBuffer, ...(sampleRate !== undefined ? { sampleRate } : {}) };
130
+ return finish(modelStart, response, false);
34
131
  }
35
- : undefined;
36
- const response = await model.runStream(text, streamOpts);
132
+ try {
133
+ for await (const data of response.iterate()) {
134
+ if (await cancelIfAborted(model, ctx.signal))
135
+ continue;
136
+ // lunte-disable-next-line eqeqeq -- `== null` intentionally matches null and undefined
137
+ if (data.outputArray == null)
138
+ continue;
139
+ const buf = Array.from(data.outputArray);
140
+ if (buf.length === 0)
141
+ continue;
142
+ yield { buffer: buf, ...chunkMetadata(data) };
143
+ }
144
+ }
145
+ catch (error) {
146
+ rethrowUnlessCancelled(error, ctx.signal);
147
+ }
148
+ return finish(modelStart, response, ctx.signal.aborted);
149
+ }
150
+ const response = (await model.run({
151
+ input: text,
152
+ // The addon's job field is `type`, and its native layer accepts only
153
+ // 'text' (AddonJs.hpp runJob) — the addon's own streaming paths hard-code
154
+ // it. Pin it here too, so the request's `inputType` (which the schema
155
+ // does not constrain) behaves the same on every path.
156
+ type: 'text',
157
+ ...(stream ? { streamOutput: true } : {}),
158
+ // `run({ streamOutput: true })` runs the same chunker as `runStream()`, so
159
+ // the chunking knobs apply here too — they used to be honoured only on the
160
+ // sentenceStream path.
161
+ ...(stream && sentenceStreamLocale !== undefined ? { locale: sentenceStreamLocale } : {}),
162
+ ...(stream && sentenceStreamMaxChunkScalars !== undefined
163
+ ? { maxChunkScalars: sentenceStreamMaxChunkScalars }
164
+ : {}),
165
+ ...parlerJobOptions
166
+ }));
37
167
  if (!stream) {
38
168
  let completeBuffer = [];
39
- for await (const data of response.iterate()) {
40
- // lunte-disable-next-line eqeqeq -- `!= null` intentionally matches null and undefined
41
- if (data.outputArray != null) {
169
+ let sampleRate;
170
+ try {
171
+ for await (const data of response.iterate()) {
172
+ if (await cancelIfAborted(model, ctx.signal))
173
+ continue;
174
+ sampleRate ??= data.sampleRate;
42
175
  completeBuffer = completeBuffer.concat(Array.from(data.outputArray));
43
176
  }
44
177
  }
45
- const modelExecutionMs = nowMs() - modelStart;
46
- const stats = collectTtsStats(response);
47
- yield { buffer: completeBuffer };
48
- return buildStreamResult(modelExecutionMs, hasDefinedValues(stats) ? stats : undefined);
178
+ catch (error) {
179
+ rethrowUnlessCancelled(error, ctx.signal);
180
+ }
181
+ if (ctx.signal.aborted)
182
+ return finish(modelStart, response, true);
183
+ yield { buffer: completeBuffer, ...(sampleRate !== undefined ? { sampleRate } : {}) };
184
+ return finish(modelStart, response, false);
49
185
  }
50
- for await (const data of response.iterate()) {
51
- // lunte-disable-next-line eqeqeq -- `== null` intentionally matches null and undefined
52
- if (data.outputArray == null)
53
- continue;
54
- const buf = Array.from(data.outputArray);
55
- if (buf.length === 0)
56
- continue;
57
- yield {
58
- buffer: buf,
59
- ...(data.chunkIndex !== undefined ? { chunkIndex: data.chunkIndex } : {}),
60
- ...(typeof data.sentenceChunk === 'string' && data.sentenceChunk.length > 0
61
- ? { sentenceChunk: data.sentenceChunk }
62
- : {})
63
- };
186
+ try {
187
+ for await (const data of response.iterate()) {
188
+ if (await cancelIfAborted(model, ctx.signal))
189
+ continue;
190
+ yield { buffer: Array.from(data.outputArray), ...chunkMetadata(data) };
191
+ }
64
192
  }
65
- const modelExecutionMs = nowMs() - modelStart;
66
- const stats = collectTtsStats(response);
67
- return buildStreamResult(modelExecutionMs, hasDefinedValues(stats) ? stats : undefined);
68
- }
69
- const response = (await model.run({
70
- input: text,
71
- inputType,
72
- ...(stream ? { streamOutput: true } : {}),
73
- ...parlerJobOptions
74
- }));
75
- if (!stream) {
76
- let completeBuffer = [];
77
- for await (const data of response.iterate()) {
78
- completeBuffer = completeBuffer.concat(Array.from(data.outputArray));
193
+ catch (error) {
194
+ rethrowUnlessCancelled(error, ctx.signal);
79
195
  }
80
- const modelExecutionMs = nowMs() - modelStart;
81
- const stats = collectTtsStats(response);
82
- yield { buffer: completeBuffer };
83
- return buildStreamResult(modelExecutionMs, hasDefinedValues(stats) ? stats : undefined);
196
+ return finish(modelStart, response, ctx.signal.aborted);
84
197
  }
85
- for await (const data of response.iterate()) {
86
- yield { buffer: Array.from(data.outputArray) };
198
+ catch (e_1) {
199
+ env_1.error = e_1;
200
+ env_1.hasError = true;
201
+ }
202
+ finally {
203
+ const result_1 = __disposeResources(env_1);
204
+ if (result_1)
205
+ await result_1;
87
206
  }
88
- const modelExecutionMs = nowMs() - modelStart;
89
- const stats = collectTtsStats(response);
90
- return buildStreamResult(modelExecutionMs, hasDefinedValues(stats) ? stats : undefined);
91
207
  }