@jskit-ai/assistant-voice 0.2.9 → 0.2.10
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/examples/conversation/main.js +1 -1
- package/examples/conversation/package.json +8 -8
- package/package.json +3 -3
- package/src/client/ConversationDialog.vue +2 -1
- package/src/client/VoiceConversation.vue +12 -6
- package/src/client/VoiceConversationControls.vue +6 -6
- package/src/client/VoiceConversationHost.vue +2 -1
- package/src/client/VoiceConversationSettings.vue +2 -1
- package/src/client/VoiceSpeakingVoiceSelect.vue +1 -1
- package/src/client/audioCapture.js +57 -36
- package/src/client/browser.js +89 -0
- package/src/client/conversationNarration.js +4 -1
- package/src/client/conversationVoiceState.js +6 -2
- package/src/client/i18n.js +7 -5
- package/src/client/types.js +104 -0
- package/src/client/voiceAvatar.js +13 -5
- package/src/client/voiceController.js +31 -7
- package/src/client/voiceConversation.js +66 -355
- package/src/client/voiceLauncher.js +18 -5
- package/src/client/voiceReplies.js +389 -0
- package/src/client/voiceSpeech.js +520 -0
- package/src/client/voiceTransport.js +55 -464
- package/src/server/voiceAccessToken.js +4 -1
- package/src/server/voiceDaemon.js +105 -47
- package/src/server/voiceProxy.js +3 -1
- package/src/server/voiceProxyConfig.js +5 -1
- package/src/shared/protocol.js +27 -5
|
@@ -80,40 +80,44 @@ function createBoundedSerialQueue({ maximumQueued = 4 } = {}) {
|
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
function close() {
|
|
84
|
+
closed = true;
|
|
85
|
+
const error = Object.assign(new Error("Voice speech queue is shutting down."), {
|
|
86
|
+
code: "voice_queue_closed", messageCode: "assistant.voice.daemon.queueClosing", messageParams: {}
|
|
87
|
+
});
|
|
88
|
+
for (const entry of pending.splice(0)) {
|
|
89
|
+
entry.reject(error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function run(operation) {
|
|
94
|
+
if (closed) {
|
|
95
|
+
const error = Object.assign(new Error("Voice speech queue is unavailable."), {
|
|
96
|
+
code: "voice_queue_closed", messageCode: "assistant.voice.daemon.queueUnavailable", messageParams: {}
|
|
88
97
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
return Promise.reject(error);
|
|
99
|
+
}
|
|
100
|
+
if (typeof operation !== "function") {
|
|
101
|
+
return Promise.reject(new TypeError("Voice speech queue requires an operation."));
|
|
102
|
+
}
|
|
103
|
+
if (pending.length >= maximumQueued) {
|
|
104
|
+
const error = Object.assign(new Error("Voice speech capacity is currently busy. Try again shortly."), {
|
|
105
|
+
code: "voice_busy", messageCode: "assistant.voice.daemon.capacityBusy", messageParams: {}
|
|
106
|
+
});
|
|
107
|
+
return Promise.reject(error);
|
|
108
|
+
}
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
pending.push({ operation, reject, resolve });
|
|
111
|
+
void drain();
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return Object.freeze({
|
|
116
|
+
close,
|
|
93
117
|
get depth() {
|
|
94
118
|
return pending.length + (running ? 1 : 0);
|
|
95
119
|
},
|
|
96
|
-
run
|
|
97
|
-
if (closed) {
|
|
98
|
-
const error = Object.assign(new Error("Voice speech queue is unavailable."), {
|
|
99
|
-
code: "voice_queue_closed", messageCode: "assistant.voice.daemon.queueUnavailable", messageParams: {}
|
|
100
|
-
});
|
|
101
|
-
return Promise.reject(error);
|
|
102
|
-
}
|
|
103
|
-
if (typeof operation !== "function") {
|
|
104
|
-
return Promise.reject(new TypeError("Voice speech queue requires an operation."));
|
|
105
|
-
}
|
|
106
|
-
if (pending.length >= maximumQueued) {
|
|
107
|
-
const error = Object.assign(new Error("Voice speech capacity is currently busy. Try again shortly."), {
|
|
108
|
-
code: "voice_busy", messageCode: "assistant.voice.daemon.capacityBusy", messageParams: {}
|
|
109
|
-
});
|
|
110
|
-
return Promise.reject(error);
|
|
111
|
-
}
|
|
112
|
-
return new Promise((resolve, reject) => {
|
|
113
|
-
pending.push({ operation, reject, resolve });
|
|
114
|
-
void drain();
|
|
115
|
-
});
|
|
116
|
-
}
|
|
120
|
+
run
|
|
117
121
|
});
|
|
118
122
|
}
|
|
119
123
|
|
|
@@ -135,10 +139,60 @@ function writeJsonResponse(response, statusCode, payload) {
|
|
|
135
139
|
response.end(body);
|
|
136
140
|
}
|
|
137
141
|
|
|
142
|
+
/**
|
|
143
|
+
* A speech engine's recognition of one recording.
|
|
144
|
+
* @typedef {object} VoiceRecognizer
|
|
145
|
+
* @property {(frame: Buffer) => string} acceptPcm Recognizes a slice of 16 kHz PCM and answers the words so far.
|
|
146
|
+
* @property {() => string} finish Ends the recording and answers its final words.
|
|
147
|
+
* @property {() => string} reset Starts the next utterance of a continuous recording and answers the last one's words.
|
|
148
|
+
* @property {() => boolean} isEndpoint Whether the speaker has paused at the end of an utterance.
|
|
149
|
+
* @property {() => void} cancel Drops the recording.
|
|
150
|
+
* @property {number} acceptedSamples The samples recognized so far.
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The speech engine a voice host runs: its recognition, synthesis and voices.
|
|
155
|
+
* @typedef {object} VoiceSpeechEngine
|
|
156
|
+
* @property {(options: { continuous: boolean, onPartial: (text: string) => void, onEndpoint: (text: string) => void }) => VoiceRecognizer} createListeningSession
|
|
157
|
+
* @property {(text: string, options: { onAudio: (frame: Uint8Array) => void, signal: AbortSignal, voiceId: string }) => Promise<{ sampleRate?: number }>} synthesize
|
|
158
|
+
* @property {() => Promise<unknown>} warmup Loads the models before the host accepts connections.
|
|
159
|
+
* @property {number} sampleRate
|
|
160
|
+
* @property {number} [synthesisTimeoutMs] How long one phrase's synthesis may take, when longer than the protocol's default.
|
|
161
|
+
* @property {{ id: string, label: string, language?: string }[]} [voices]
|
|
162
|
+
* @property {string} [defaultVoice]
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The principal a request's access token names. Each of its ids has its own
|
|
167
|
+
* connection quota.
|
|
168
|
+
* @typedef {{ tenant: string, parentTenant?: string }} VoicePrincipal
|
|
169
|
+
*/
|
|
170
|
+
|
|
171
|
+
/** A host without extra HTTP routes answers no request itself, so it falls through to 404. */
|
|
172
|
+
function answerNoHostRoute() {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A voice host: an HTTP server whose WebSocket connections recognize the speaker's
|
|
178
|
+
* words and speak the answer, with the health and voice-catalogue routes.
|
|
179
|
+
* @param {object} options
|
|
180
|
+
* @param {Buffer | string} [options.accessKey] The key that signs voice access tokens, when authorize is the default.
|
|
181
|
+
* @param {(token: string, request: import("node:http").IncomingMessage) => VoicePrincipal | null} [options.authorize]
|
|
182
|
+
* @param {(request: import("node:http").IncomingMessage, response: import("node:http").ServerResponse) => boolean | Promise<boolean>} [options.handleRequest] The host's own HTTP routes; true when it answered.
|
|
183
|
+
* @param {VoiceSpeechEngine} options.engine
|
|
184
|
+
* @param {string} [options.host]
|
|
185
|
+
* @param {number} [options.idleTimeoutMs]
|
|
186
|
+
* @param {{ warn: (fields: object, message: string) => void }} [options.logger]
|
|
187
|
+
* @param {number} [options.maximumConnections]
|
|
188
|
+
* @param {number} [options.maximumConnectionsPerTenant]
|
|
189
|
+
* @param {number} [options.maximumQueuedSpeech]
|
|
190
|
+
* @param {number} [options.port]
|
|
191
|
+
*/
|
|
138
192
|
function createVoiceDaemon({
|
|
139
193
|
accessKey,
|
|
140
194
|
authorize = token => verifyVoiceAccessToken(token, { key: accessKey }),
|
|
141
|
-
handleRequest =
|
|
195
|
+
handleRequest = answerNoHostRoute,
|
|
142
196
|
engine,
|
|
143
197
|
host = "127.0.0.1",
|
|
144
198
|
idleTimeoutMs = 90_000,
|
|
@@ -147,11 +201,13 @@ function createVoiceDaemon({
|
|
|
147
201
|
maximumConnectionsPerTenant = 2,
|
|
148
202
|
maximumQueuedSpeech = 4,
|
|
149
203
|
port = 3092
|
|
150
|
-
}
|
|
151
|
-
|
|
204
|
+
}) {
|
|
205
|
+
// readable-check: allow method-probe - input validation: an application hands over the speech engine its host runs
|
|
206
|
+
if (!engine || typeof engine.createListeningSession !== "function" || typeof engine.synthesize !== "function" || typeof engine.warmup !== "function") {
|
|
152
207
|
throw new TypeError("createVoiceDaemon requires a speech engine.");
|
|
153
208
|
}
|
|
154
|
-
|
|
209
|
+
// readable-check: allow method-probe - input validation: an application hands over its own authorization and routes
|
|
210
|
+
if (typeof authorize !== "function" || typeof handleRequest !== "function") throw new TypeError("Voice host authorization and request handlers must be functions.");
|
|
155
211
|
const tenantConnections = new Map();
|
|
156
212
|
const sockets = new Set();
|
|
157
213
|
const principals = new WeakMap();
|
|
@@ -188,7 +244,7 @@ function createVoiceDaemon({
|
|
|
188
244
|
writeJsonResponse(response, 200, voiceCatalogue());
|
|
189
245
|
return;
|
|
190
246
|
}
|
|
191
|
-
if (await handleRequest
|
|
247
|
+
if (await handleRequest(request, response)) return;
|
|
192
248
|
writeJsonResponse(response, 404, { ok: false });
|
|
193
249
|
} catch {
|
|
194
250
|
if (!response.headersSent) writeJsonResponse(response, 500, { ok: false, error: "Voice host request failed." });
|
|
@@ -227,7 +283,7 @@ function createVoiceDaemon({
|
|
|
227
283
|
function revokeTenant(tenant) {
|
|
228
284
|
for (const socket of sockets) {
|
|
229
285
|
if (quotaKeys(principals.get(socket)).includes(tenant)) {
|
|
230
|
-
principals.get(socket).cancel
|
|
286
|
+
principals.get(socket).cancel();
|
|
231
287
|
sendJson(socket, "error", { code: "voice_access_revoked", message: "This application's voice access was revoked." });
|
|
232
288
|
socket.close(1008, "Voice access was revoked");
|
|
233
289
|
}
|
|
@@ -244,7 +300,7 @@ function createVoiceDaemon({
|
|
|
244
300
|
|
|
245
301
|
const cancelListening = () => {
|
|
246
302
|
clearImmediate(listening?.decodeTimer);
|
|
247
|
-
listening
|
|
303
|
+
if (listening) listening.recognizer.cancel();
|
|
248
304
|
listening = null;
|
|
249
305
|
};
|
|
250
306
|
const cancelSpeaking = () => {
|
|
@@ -260,7 +316,7 @@ function createVoiceDaemon({
|
|
|
260
316
|
}
|
|
261
317
|
socket.close(1000, "Voice connection idle");
|
|
262
318
|
}, Math.min(idleTimeoutMs, 15_000));
|
|
263
|
-
idleTimer.unref
|
|
319
|
+
idleTimer.unref();
|
|
264
320
|
|
|
265
321
|
const failMessage = (error) => {
|
|
266
322
|
invalidMessages += 1;
|
|
@@ -518,7 +574,7 @@ function createVoiceDaemon({
|
|
|
518
574
|
releaseConnection(socket);
|
|
519
575
|
});
|
|
520
576
|
socket.once("error", (error) => {
|
|
521
|
-
logger
|
|
577
|
+
logger.warn({ error: String(error?.message || error), tenantId }, "Voice WebSocket failed.");
|
|
522
578
|
});
|
|
523
579
|
sendJson(socket, "voice.ready", {
|
|
524
580
|
inputSampleRate: VOICE_INPUT_SAMPLE_RATE,
|
|
@@ -553,7 +609,7 @@ function createVoiceDaemon({
|
|
|
553
609
|
if (ready) {
|
|
554
610
|
return address();
|
|
555
611
|
}
|
|
556
|
-
await engine.warmup
|
|
612
|
+
await engine.warmup();
|
|
557
613
|
await new Promise((resolve, reject) => {
|
|
558
614
|
const fail = (error) => {
|
|
559
615
|
server.off("listening", listen);
|
|
@@ -600,17 +656,19 @@ function createVoiceDaemon({
|
|
|
600
656
|
});
|
|
601
657
|
}
|
|
602
658
|
|
|
659
|
+
function metrics() {
|
|
660
|
+
return Object.freeze({
|
|
661
|
+
connections: sockets.size,
|
|
662
|
+
queueDepth: speechQueue.depth,
|
|
663
|
+
tenants: tenantConnections.size
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
|
|
603
667
|
return Object.freeze({
|
|
604
668
|
address,
|
|
605
669
|
close,
|
|
606
670
|
revokeTenant,
|
|
607
|
-
metrics
|
|
608
|
-
return Object.freeze({
|
|
609
|
-
connections: sockets.size,
|
|
610
|
-
queueDepth: speechQueue.depth,
|
|
611
|
-
tenants: tenantConnections.size
|
|
612
|
-
});
|
|
613
|
-
},
|
|
671
|
+
metrics,
|
|
614
672
|
start
|
|
615
673
|
});
|
|
616
674
|
}
|
package/src/server/voiceProxy.js
CHANGED
|
@@ -52,6 +52,7 @@ async function readVoiceCatalogue(config, { fetchImpl = fetch } = {}) {
|
|
|
52
52
|
if (!Array.isArray(catalogue.voices) || typeof catalogue.defaultVoice !== "string") throw new Error("Invalid voice catalogue.");
|
|
53
53
|
return catalogue;
|
|
54
54
|
} catch (cause) {
|
|
55
|
+
// readable-check: allow patched-error - safety invariant: a speech-service failure is a plain 503 error, which the HTTP error handler answers without its text, like every server failure
|
|
55
56
|
throw Object.assign(new Error("Speaking voices could not load. Check the speech service and retry."), {
|
|
56
57
|
code: "voice_unavailable", statusCode: 503, cause,
|
|
57
58
|
messageCode: "assistant.voice.proxy.voicesUnavailable", messageParams: {}
|
|
@@ -79,9 +80,10 @@ function sendSocketError(socket, code, message, { params, messageCode, messagePa
|
|
|
79
80
|
function registerVoiceProxyRoute(fastify, {
|
|
80
81
|
proxyConfig,
|
|
81
82
|
route,
|
|
82
|
-
authorize
|
|
83
|
+
authorize,
|
|
83
84
|
WebSocketCtor = WebSocket
|
|
84
85
|
} = {}) {
|
|
86
|
+
// readable-check: allow method-probe - platform object: the application's Fastify instance registers the proxy route
|
|
85
87
|
if (!fastify || typeof fastify.get !== "function") {
|
|
86
88
|
throw new TypeError("registerVoiceProxyRoute requires Fastify get().");
|
|
87
89
|
}
|
|
@@ -49,7 +49,11 @@ function resolveVoiceProxyConfig({ endpoint: value = "", accessTokenFile = "" }
|
|
|
49
49
|
const endpoint = voiceEndpoint(value);
|
|
50
50
|
const tokenFile = String(accessTokenFile || "").trim();
|
|
51
51
|
if (!endpoint && !tokenFile) {
|
|
52
|
-
return Object.freeze({
|
|
52
|
+
return Object.freeze({
|
|
53
|
+
available: false,
|
|
54
|
+
endpoint: "",
|
|
55
|
+
token: ""
|
|
56
|
+
});
|
|
53
57
|
}
|
|
54
58
|
if (!endpoint || !tokenFile) {
|
|
55
59
|
throw new Error(
|
package/src/shared/protocol.js
CHANGED
|
@@ -104,15 +104,28 @@ function parseVoiceClientControl(raw = "") {
|
|
|
104
104
|
{ sampleRate: VOICE_INPUT_SAMPLE_RATE }
|
|
105
105
|
);
|
|
106
106
|
}
|
|
107
|
-
return Object.freeze({
|
|
107
|
+
return Object.freeze({
|
|
108
|
+
sampleRate,
|
|
109
|
+
turnId,
|
|
110
|
+
type,
|
|
111
|
+
...(input.continuous === true ? { continuous: true } : {})
|
|
112
|
+
});
|
|
108
113
|
}
|
|
109
114
|
if (type === "speak.stop-after") {
|
|
110
115
|
if (!Number.isSafeInteger(input.segmentIndex) || input.segmentIndex < 0) throw voiceProtocolError("voice_segment_invalid", "Choose the current spoken phrase.");
|
|
111
|
-
return Object.freeze({
|
|
116
|
+
return Object.freeze({
|
|
117
|
+
type,
|
|
118
|
+
turnId,
|
|
119
|
+
segmentIndex: input.segmentIndex
|
|
120
|
+
});
|
|
112
121
|
}
|
|
113
122
|
if (type === "listen.commit" || type === "listen.discard") {
|
|
114
123
|
if (!Number.isSafeInteger(input.revision) || input.revision < 1) throw voiceProtocolError("voice_revision_invalid", "Choose the current recognized utterance.");
|
|
115
|
-
return Object.freeze({
|
|
124
|
+
return Object.freeze({
|
|
125
|
+
type,
|
|
126
|
+
turnId,
|
|
127
|
+
revision: input.revision
|
|
128
|
+
});
|
|
116
129
|
}
|
|
117
130
|
if (type === "speak.start" || type === "speak.append") {
|
|
118
131
|
if (input.voiceId !== undefined && (type !== "speak.start" || typeof input.voiceId !== "string" || !VOICE_ID_PATTERN.test(input.voiceId))) {
|
|
@@ -128,9 +141,18 @@ function parseVoiceClientControl(raw = "") {
|
|
|
128
141
|
{ maximum: VOICE_MAX_SPEECH_TEXT_CHARACTERS }
|
|
129
142
|
);
|
|
130
143
|
}
|
|
131
|
-
return Object.freeze({
|
|
144
|
+
return Object.freeze({
|
|
145
|
+
text,
|
|
146
|
+
turnId,
|
|
147
|
+
type,
|
|
148
|
+
...(input.stream === true ? { stream: true } : {}),
|
|
149
|
+
...(input.voiceId ? { voiceId: input.voiceId } : {})
|
|
150
|
+
});
|
|
132
151
|
}
|
|
133
|
-
return Object.freeze({
|
|
152
|
+
return Object.freeze({
|
|
153
|
+
turnId,
|
|
154
|
+
type
|
|
155
|
+
});
|
|
134
156
|
}
|
|
135
157
|
|
|
136
158
|
function assertVoiceAudioFrame(frame = null) {
|