@qvac/inference 0.19.1 → 0.20.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 (137) hide show
  1. package/NOTICE +43 -11
  2. package/dist/api/audio-gen.d.ts +42 -1
  3. package/dist/api/audio-gen.js +137 -51
  4. package/dist/api/index.d.ts +2 -1
  5. package/dist/api/index.js +2 -1
  6. package/dist/api/text-to-speech.d.ts +31 -4
  7. package/dist/api/text-to-speech.js +117 -21
  8. package/dist/api/vector-index.d.ts +142 -0
  9. package/dist/api/vector-index.js +168 -0
  10. package/dist/api/world.d.ts +2 -2
  11. package/dist/errors/index.d.ts +12 -0
  12. package/dist/errors/index.js +21 -0
  13. package/dist/handlers/vector-index.d.ts +2 -0
  14. package/dist/handlers/vector-index.js +40 -0
  15. package/dist/index.d.ts +1 -1
  16. package/dist/index.js +1 -1
  17. package/dist/models/registry/models.d.ts +351 -12
  18. package/dist/models/registry/models.js +2271 -1957
  19. package/dist/models/registry/resource-profiles.js +135 -0
  20. package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.d.ts +2 -0
  21. package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.js +65 -0
  22. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-input.d.ts +9 -1
  23. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-input.js +24 -7
  24. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.d.ts +46 -0
  25. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.js +218 -0
  26. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.d.ts +1 -1
  27. package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.js +56 -225
  28. package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.d.ts +8 -0
  29. package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.js +28 -0
  30. package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +248 -0
  31. package/dist/plugins/builtin/audiogen-ggml/plugin.js +17 -1
  32. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +5 -2
  33. package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +1 -2
  34. package/dist/plugins/builtin/ggml-ocr/plugin.d.ts +1 -1
  35. package/dist/plugins/builtin/ggml-ocr/plugin.js +1 -2
  36. package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +6 -4
  37. package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.d.ts +1 -2
  38. package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.js +0 -4
  39. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.d.ts +1 -0
  40. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.js +3 -0
  41. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.d.ts +9 -0
  42. package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +96 -89
  43. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.d.ts +0 -1
  44. package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.js +0 -31
  45. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.d.ts +40 -24
  46. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.js +187 -123
  47. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-state.d.ts +11 -4
  48. package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-state.js +18 -16
  49. package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +8 -4
  50. package/dist/plugins/builtin/llamacpp-completion/plugin.js +1 -2
  51. package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +1 -2
  52. package/dist/plugins/builtin/llamacpp-embedding/plugin.js +2 -38
  53. package/dist/plugins/builtin/llamacpp-embedding/transform.d.ts +9 -0
  54. package/dist/plugins/builtin/llamacpp-embedding/transform.js +42 -0
  55. package/dist/plugins/builtin/nmtcpp-translation/plugin.d.ts +1 -1
  56. package/dist/plugins/builtin/nmtcpp-translation/plugin.js +1 -2
  57. package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +1 -1
  58. package/dist/plugins/builtin/parakeet-transcription/plugin.js +1 -2
  59. package/dist/plugins/builtin/sdcpp-generation/ops/video.d.ts +1 -0
  60. package/dist/plugins/builtin/sdcpp-generation/ops/video.js +8 -2
  61. package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +9 -4
  62. package/dist/plugins/builtin/sdcpp-generation/plugin.js +51 -19
  63. package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.d.ts +29 -0
  64. package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.js +66 -0
  65. package/dist/plugins/builtin/tts-ggml/ops/parler-options.js +10 -0
  66. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.d.ts +3 -5
  67. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.js +143 -28
  68. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.d.ts +5 -2
  69. package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +182 -66
  70. package/dist/plugins/builtin/tts-ggml/plugin.d.ts +267 -22
  71. package/dist/plugins/builtin/tts-ggml/plugin.js +94 -35
  72. package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +1 -1
  73. package/dist/plugins/builtin/whispercpp-transcription/plugin.js +1 -2
  74. package/dist/plugins/index.d.ts +1 -1
  75. package/dist/plugins/index.js +1 -1
  76. package/dist/plugins/ops/load-model.js +22 -2
  77. package/dist/plugins/ops/translate.js +58 -12
  78. package/dist/plugins/ops/translatepsy-utils.d.ts +17 -0
  79. package/dist/plugins/ops/translatepsy-utils.js +32 -0
  80. package/dist/plugins/registry.d.ts +15 -0
  81. package/dist/plugins/registry.js +133 -15
  82. package/dist/plugins/turbovec-backend.d.ts +17 -0
  83. package/dist/plugins/turbovec-backend.js +99 -0
  84. package/dist/registry.js +4 -0
  85. package/dist/resources/model-fit/native-probe/advisory-fit.d.ts +46 -0
  86. package/dist/resources/model-fit/native-probe/advisory-fit.js +214 -0
  87. package/dist/resources/model-fit/native-probe/create-llama-fit-request.d.ts +26 -0
  88. package/dist/resources/model-fit/native-probe/create-llama-fit-request.js +171 -0
  89. package/dist/resources/model-fit/native-probe/run-in-process-fit.d.ts +42 -0
  90. package/dist/resources/model-fit/native-probe/run-in-process-fit.js +192 -0
  91. package/dist/resources/model-fit/native-probe/run-isolated-fit.d.ts +70 -0
  92. package/dist/resources/model-fit/native-probe/run-isolated-fit.js +372 -0
  93. package/dist/runtime/env.d.ts +2 -0
  94. package/dist/runtime/env.js +9 -2
  95. package/dist/runtime/lifecycle.js +2 -0
  96. package/dist/runtime/model-registry.d.ts +3 -1
  97. package/dist/runtime/model-registry.js +2 -1
  98. package/dist/runtime/request-context.js +17 -0
  99. package/dist/runtime/vector-index-backend.d.ts +25 -0
  100. package/dist/runtime/vector-index-backend.js +1 -0
  101. package/dist/runtime/vector-index-registry.d.ts +76 -0
  102. package/dist/runtime/vector-index-registry.js +161 -0
  103. package/dist/schemas/assess-model-fit.d.ts +44 -0
  104. package/dist/schemas/assess-model-fit.js +40 -0
  105. package/dist/schemas/audio-gen.d.ts +479 -4
  106. package/dist/schemas/audio-gen.js +376 -17
  107. package/dist/schemas/batch-completion-stream.d.ts +7 -0
  108. package/dist/schemas/batch-completion-stream.js +5 -3
  109. package/dist/schemas/common.d.ts +749 -15
  110. package/dist/schemas/common.js +8 -1
  111. package/dist/schemas/completion-event.d.ts +11 -1
  112. package/dist/schemas/completion-event.js +3 -0
  113. package/dist/schemas/completion-stream.d.ts +20 -0
  114. package/dist/schemas/completion-stream.js +41 -4
  115. package/dist/schemas/config.d.ts +48 -30
  116. package/dist/schemas/errors.d.ts +4 -0
  117. package/dist/schemas/errors.js +22 -0
  118. package/dist/schemas/index.d.ts +1 -0
  119. package/dist/schemas/index.js +1 -0
  120. package/dist/schemas/llamacpp-config.d.ts +4 -8
  121. package/dist/schemas/llamacpp-config.js +4 -4
  122. package/dist/schemas/load-model.d.ts +2503 -139
  123. package/dist/schemas/model-config-schemas.d.ts +228 -5
  124. package/dist/schemas/plugin.d.ts +10 -0
  125. package/dist/schemas/sdcpp-config.d.ts +221 -4
  126. package/dist/schemas/sdcpp-config.js +128 -22
  127. package/dist/schemas/text-to-speech.d.ts +780 -19
  128. package/dist/schemas/text-to-speech.js +298 -44
  129. package/dist/schemas/transcription-config.js +7 -4
  130. package/dist/schemas/vector-index.d.ts +194 -0
  131. package/dist/schemas/vector-index.js +185 -0
  132. package/dist/utils/addon-responses.d.ts +29 -3
  133. package/dist/utils/aggregate-events.d.ts +3 -1
  134. package/dist/utils/aggregate-events.js +7 -1
  135. package/dist/utils/tts-stats.d.ts +15 -11
  136. package/dist/utils/tts-stats.js +33 -11
  137. package/package.json +28 -25
@@ -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
  }