@qvac/inference 0.19.0 → 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.
- package/NOTICE +95 -39
- package/dist/api/audio-gen.d.ts +42 -1
- package/dist/api/audio-gen.js +137 -51
- package/dist/api/delete-cache.d.ts +7 -1
- package/dist/api/delete-cache.js +11 -1
- package/dist/api/index.d.ts +2 -1
- package/dist/api/index.js +2 -1
- package/dist/api/text-to-speech.d.ts +31 -4
- package/dist/api/text-to-speech.js +117 -21
- package/dist/api/vector-index.d.ts +142 -0
- package/dist/api/vector-index.js +168 -0
- package/dist/api/world.d.ts +2 -2
- package/dist/errors/index.d.ts +12 -0
- package/dist/errors/index.js +21 -0
- package/dist/handlers/delete-cache.js +3 -0
- package/dist/handlers/vector-index.d.ts +2 -0
- package/dist/handlers/vector-index.js +40 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/models/registry/models.d.ts +351 -12
- package/dist/models/registry/models.js +2271 -1957
- package/dist/models/registry/resource-profiles.js +135 -0
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.d.ts +2 -0
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-edit-stream.js +65 -0
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-input.d.ts +9 -1
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-input.js +24 -7
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.d.ts +46 -0
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-run.js +218 -0
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.d.ts +1 -1
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-gen-stream.js +56 -225
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.d.ts +8 -0
- package/dist/plugins/builtin/audiogen-ggml/ops/audio-understand.js +28 -0
- package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +248 -0
- package/dist/plugins/builtin/audiogen-ggml/plugin.js +17 -1
- package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +5 -2
- package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +1 -2
- package/dist/plugins/builtin/ggml-ocr/plugin.d.ts +1 -1
- package/dist/plugins/builtin/ggml-ocr/plugin.js +1 -2
- package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +6 -4
- package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.d.ts +1 -2
- package/dist/plugins/builtin/llamacpp-completion/ops/cache-logger.js +0 -4
- package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.d.ts +2 -0
- package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.js +6 -0
- package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.d.ts +9 -0
- package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +96 -89
- package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.d.ts +0 -1
- package/dist/plugins/builtin/llamacpp-completion/ops/context-overflow.js +0 -31
- package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.d.ts +48 -27
- package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-session.js +239 -135
- package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-state.d.ts +11 -4
- package/dist/plugins/builtin/llamacpp-completion/ops/kv-cache-state.js +18 -16
- package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +11 -4
- package/dist/plugins/builtin/llamacpp-completion/plugin.js +1 -2
- package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +1 -2
- package/dist/plugins/builtin/llamacpp-embedding/plugin.js +2 -38
- package/dist/plugins/builtin/llamacpp-embedding/transform.d.ts +9 -0
- package/dist/plugins/builtin/llamacpp-embedding/transform.js +42 -0
- package/dist/plugins/builtin/nmtcpp-translation/plugin.d.ts +1 -1
- package/dist/plugins/builtin/nmtcpp-translation/plugin.js +1 -2
- package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +1 -1
- package/dist/plugins/builtin/parakeet-transcription/plugin.js +1 -2
- package/dist/plugins/builtin/sdcpp-generation/ops/video.d.ts +1 -0
- package/dist/plugins/builtin/sdcpp-generation/ops/video.js +8 -2
- package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +9 -4
- package/dist/plugins/builtin/sdcpp-generation/plugin.js +51 -19
- package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.d.ts +29 -0
- package/dist/plugins/builtin/tts-ggml/ops/cancel-binding.js +66 -0
- package/dist/plugins/builtin/tts-ggml/ops/parler-options.js +10 -0
- package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.d.ts +3 -5
- package/dist/plugins/builtin/tts-ggml/ops/text-to-speech-stream.js +143 -28
- package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.d.ts +5 -2
- package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +182 -66
- package/dist/plugins/builtin/tts-ggml/plugin.d.ts +267 -22
- package/dist/plugins/builtin/tts-ggml/plugin.js +94 -35
- package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +1 -1
- package/dist/plugins/builtin/whispercpp-transcription/plugin.js +1 -2
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/ops/load-model.js +22 -2
- package/dist/plugins/ops/translate.js +58 -12
- package/dist/plugins/ops/translatepsy-utils.d.ts +17 -0
- package/dist/plugins/ops/translatepsy-utils.js +32 -0
- package/dist/plugins/registry.d.ts +15 -0
- package/dist/plugins/registry.js +133 -15
- package/dist/plugins/turbovec-backend.d.ts +17 -0
- package/dist/plugins/turbovec-backend.js +99 -0
- package/dist/registry.js +4 -0
- package/dist/resources/model-fit/assess.js +169 -68
- package/dist/resources/model-fit/estimators/llm.d.ts +1 -0
- package/dist/resources/model-fit/estimators/llm.js +3 -1
- package/dist/resources/model-fit/floor.d.ts +31 -0
- package/dist/resources/model-fit/floor.js +65 -0
- package/dist/resources/model-fit/native-probe/advisory-fit.d.ts +46 -0
- package/dist/resources/model-fit/native-probe/advisory-fit.js +214 -0
- package/dist/resources/model-fit/native-probe/create-llama-fit-request.d.ts +26 -0
- package/dist/resources/model-fit/native-probe/create-llama-fit-request.js +171 -0
- package/dist/resources/model-fit/native-probe/run-in-process-fit.d.ts +42 -0
- package/dist/resources/model-fit/native-probe/run-in-process-fit.js +192 -0
- package/dist/resources/model-fit/native-probe/run-isolated-fit.d.ts +70 -0
- package/dist/resources/model-fit/native-probe/run-isolated-fit.js +372 -0
- package/dist/resources/native.js +5 -4
- package/dist/resources/types.d.ts +1 -3
- package/dist/runtime/env.d.ts +2 -0
- package/dist/runtime/env.js +9 -2
- package/dist/runtime/lifecycle.js +2 -0
- package/dist/runtime/model-registry.d.ts +3 -1
- package/dist/runtime/model-registry.js +2 -1
- package/dist/runtime/request-context.js +17 -0
- package/dist/runtime/vector-index-backend.d.ts +25 -0
- package/dist/runtime/vector-index-backend.js +1 -0
- package/dist/runtime/vector-index-registry.d.ts +76 -0
- package/dist/runtime/vector-index-registry.js +161 -0
- package/dist/schemas/assess-model-fit.d.ts +84 -0
- package/dist/schemas/assess-model-fit.js +72 -3
- package/dist/schemas/audio-gen.d.ts +482 -7
- package/dist/schemas/audio-gen.js +376 -17
- package/dist/schemas/batch-completion-stream.d.ts +10 -0
- package/dist/schemas/batch-completion-stream.js +5 -3
- package/dist/schemas/bci.d.ts +1 -1
- package/dist/schemas/common.d.ts +782 -19
- package/dist/schemas/common.js +8 -1
- package/dist/schemas/completion-event.d.ts +14 -1
- package/dist/schemas/completion-event.js +4 -0
- package/dist/schemas/completion-stream.d.ts +22 -0
- package/dist/schemas/completion-stream.js +41 -4
- package/dist/schemas/config.d.ts +48 -30
- package/dist/schemas/errors.d.ts +4 -0
- package/dist/schemas/errors.js +22 -0
- package/dist/schemas/index.d.ts +1 -0
- package/dist/schemas/index.js +1 -0
- package/dist/schemas/llamacpp-config.d.ts +4 -8
- package/dist/schemas/llamacpp-config.js +4 -4
- package/dist/schemas/load-model.d.ts +2604 -180
- package/dist/schemas/model-config-schemas.d.ts +228 -5
- package/dist/schemas/model-ops.d.ts +3 -0
- package/dist/schemas/model-ops.js +6 -0
- package/dist/schemas/plugin.d.ts +21 -11
- package/dist/schemas/sdcpp-config.d.ts +221 -4
- package/dist/schemas/sdcpp-config.js +128 -22
- package/dist/schemas/text-to-speech.d.ts +780 -19
- package/dist/schemas/text-to-speech.js +298 -44
- package/dist/schemas/transcription-config.js +7 -4
- package/dist/schemas/vector-index.d.ts +194 -0
- package/dist/schemas/vector-index.js +185 -0
- package/dist/utils/addon-responses.d.ts +30 -3
- package/dist/utils/aggregate-events.d.ts +3 -1
- package/dist/utils/aggregate-events.js +7 -1
- package/dist/utils/tts-stats.d.ts +15 -11
- package/dist/utils/tts-stats.js +33 -11
- package/package.json +29 -26
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { type TextToSpeechStreamRequest
|
|
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
|
-
|
|
5
|
-
|
|
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
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
...(
|
|
98
|
-
...(
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
36
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
66
|
-
|
|
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
|
-
|
|
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
|
-
|
|
86
|
-
|
|
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
|
}
|