dsh-live-voice 0.6.2 → 0.7.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/deploy/attendee/README.md +34 -0
- package/deploy/attendee/audio_bridge.py +186 -0
- package/deploy/attendee/compose.dsh-audio.yaml +54 -0
- package/deploy/attendee/cross_runtime_fixture.py +46 -0
- package/deploy/attendee/test_audio_bridge.py +170 -0
- package/lib/asr/asr.worker.js +1 -1
- package/lib/asr/manifest.json +1 -1
- package/lib/client.js +14848 -11796
- package/lib/server.js +1051 -14
- package/lib/vad/manifest.json +2 -2
- package/lib/vad/vad.worker.js +1 -1
- package/package.json +5 -3
- package/src/app/ARCHITECTURE.md +2 -0
- package/src/app/client/apply.tsx +74 -15
- package/src/app/client/i18n/catalogs/base.ts +87 -1
- package/src/app/client/i18n/catalogs/en.ts +119 -3
- package/src/app/client/i18n/catalogs/es.ts +120 -4
- package/src/app/client/i18n/catalogs/fr.ts +121 -4
- package/src/app/client/i18n/catalogs/hi.ts +118 -4
- package/src/app/client/i18n/catalogs/pt-BR.ts +119 -3
- package/src/app/client/i18n/catalogs/zh.ts +114 -3
- package/src/app/server/apply.ts +5 -0
- package/src/app/server/attendeeConnection.ts +133 -0
- package/src/app/server/attendeeCredentials.ts +144 -0
- package/src/app/server/attendeeRelay.ts +104 -0
- package/src/app/server/attendeeRoutes.ts +476 -0
- package/src/app/server/chatterboxRoutes.ts +73 -0
- package/src/modules/conversation/ARCHITECTURE.md +1 -0
- package/src/modules/conversation/components/AttendeeControls.tsx +376 -0
- package/src/modules/conversation/components/AutoPlaybackToggle.tsx +32 -13
- package/src/modules/conversation/components/ConversationControls.tsx +8 -5
- package/src/modules/conversation/components/ConversationStatusBar.tsx +18 -2
- package/src/modules/conversation/components/DevicePopover.tsx +31 -0
- package/src/modules/conversation/components/MeetingControls.tsx +102 -15
- package/src/modules/conversation/components/MicrophoneButton.tsx +1 -1
- package/src/modules/conversation/components/RecoveryFeedback.tsx +39 -0
- package/src/modules/conversation/components/SpeechStatusBar.tsx +15 -3
- package/src/modules/conversation/components/conversationStatus.ts +7 -0
- package/src/modules/conversation/components/createConversationComponents.tsx +6 -1
- package/src/modules/conversation/models/AttendeeClient.ts +742 -0
- package/src/modules/conversation/models/AttendeeHub.ts +98 -0
- package/src/modules/conversation/models/ParticipantPcmMeter.ts +58 -0
- package/src/modules/conversation/models/attendeeProtocol.ts +53 -0
- package/src/modules/conversation/models/meeting.ts +244 -41
- package/src/modules/conversation/models/meetingTranscript.ts +14 -3
- package/src/modules/core/ARCHITECTURE.md +2 -0
- package/src/modules/core/coordinator.ts +305 -22
- package/src/modules/core/engineSelection.ts +2 -2
- package/src/modules/core/networkFailure.ts +10 -0
- package/src/modules/core/recovery.ts +26 -0
- package/src/modules/core/settings.ts +16 -1
- package/src/modules/recognition/engines/browser/BrowserRecognitionEngine.ts +1 -1
- package/src/modules/recognition/engines/browser/transcribeRecording.ts +241 -0
- package/src/modules/recognition/engines/whisper/WhisperRecognitionEngine.ts +5 -1
- package/src/modules/recognition/engines/whisper/whisperRecognitionHost.ts +5 -2
- package/src/modules/recognition/recordingTranscriber.ts +143 -0
- package/src/modules/recording/ARCHITECTURE.md +12 -1
- package/src/modules/recording/RecordingRecognition.ts +95 -16
- package/src/modules/recording/repository.ts +279 -16
- package/src/modules/recording/requestMetadata.ts +30 -0
- package/src/modules/recording/storage.ts +61 -0
- package/src/modules/recording/types.ts +63 -1
- package/src/modules/settings/ARCHITECTURE.md +2 -0
- package/src/modules/settings/components/AudioDeviceField.tsx +43 -0
- package/src/modules/settings/models/browserVoiceSelection.ts +15 -0
- package/src/modules/settings/sections/conversation/ConversationSettingsSection.tsx +3 -0
- package/src/modules/settings/sections/conversation/MeetingSourceSettings.tsx +14 -2
- package/src/modules/settings/sections/recognition/RecognitionEngineSettings.tsx +4 -11
- package/src/modules/settings/sections/recognition/RecognitionSettingsSection.tsx +0 -2
- package/src/modules/settings/sections/speak/SpeechAdvancedSettings.tsx +6 -20
- package/src/modules/settings/sections/speak/SpeechEngineSettings.tsx +5 -0
- package/src/modules/speak/ARCHITECTURE.md +3 -1
- package/src/modules/speak/chatterbox/ChatterboxVoiceSettings.tsx +60 -0
- package/src/modules/speak/engines/audio/HostAudioEngine.ts +105 -38
- package/src/modules/speak/engines/chatterbox/ChatterboxHttpHost.ts +130 -0
- package/src/modules/speak/engines/chatterbox/ChatterboxSpeakingEngine.ts +14 -0
- package/src/modules/speak/index.ts +1 -0
- package/src/shared/ARCHITECTURE.md +2 -0
- package/src/shared/design-system/feedback/ContextPopover.tsx +107 -0
- package/src/styles/index.ts +6 -1
package/lib/server.js
CHANGED
|
@@ -138,6 +138,11 @@ var defaultSettings = Object.freeze({
|
|
|
138
138
|
sharedTimestampsEnabled: false,
|
|
139
139
|
microphoneSourceName: "Me",
|
|
140
140
|
sharedSourceName: "Them",
|
|
141
|
+
meetingSource: "shared",
|
|
142
|
+
attendeeApiUrl: "http://localhost:8000",
|
|
143
|
+
attendeeIngressUrl: "",
|
|
144
|
+
attendeeBotId: "",
|
|
145
|
+
attendeeBotName: "DSH Live Voice",
|
|
141
146
|
voiceCommandSend: "send, send message",
|
|
142
147
|
voiceCommandQueue: "queue, queue message",
|
|
143
148
|
voiceCommandEnd: "end, end conversation",
|
|
@@ -158,6 +163,7 @@ import { readFile, mkdir, writeFile, rename, rm } from "node:fs/promises";
|
|
|
158
163
|
import { homedir } from "node:os";
|
|
159
164
|
import { dirname, join } from "node:path";
|
|
160
165
|
import { randomUUID } from "node:crypto";
|
|
166
|
+
var WHISPER_MAX_AUDIO_BYTES = 1e8;
|
|
161
167
|
var LOOPBACK = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
|
|
162
168
|
function validateWhisperConfig(value) {
|
|
163
169
|
if (!value || typeof value.url !== "string" || typeof value.healthUrl !== "string")
|
|
@@ -202,7 +208,7 @@ function resolveWhisperUrl(value = process.env.DSH_LIVE_VOICE_WHISPER_URL || "ht
|
|
|
202
208
|
throw new Error("Whisper HTTP URL must be an unauthenticated loopback http URL.");
|
|
203
209
|
return url;
|
|
204
210
|
}
|
|
205
|
-
function validateMonoPcm16Wav(input, { maxBytes =
|
|
211
|
+
function validateMonoPcm16Wav(input, { maxBytes = WHISPER_MAX_AUDIO_BYTES } = {}) {
|
|
206
212
|
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
207
213
|
if (bytes.byteLength < 44 || bytes.byteLength > maxBytes)
|
|
208
214
|
throw new Error("Invalid or oversized WAV recording.");
|
|
@@ -233,7 +239,7 @@ var WhisperHttpHost = class {
|
|
|
233
239
|
healthUrl = "/health",
|
|
234
240
|
timeoutMs = 3e4,
|
|
235
241
|
fetchImpl = globalThis.fetch,
|
|
236
|
-
maxBytes =
|
|
242
|
+
maxBytes = WHISPER_MAX_AUDIO_BYTES,
|
|
237
243
|
store
|
|
238
244
|
} = {}) {
|
|
239
245
|
this.config = validateWhisperConfig({ url: resolveWhisperUrl(url).href, healthUrl, timeoutMs });
|
|
@@ -805,7 +811,7 @@ var SayEngine = class {
|
|
|
805
811
|
return result;
|
|
806
812
|
}
|
|
807
813
|
#waitForClose(request, child) {
|
|
808
|
-
return new Promise((
|
|
814
|
+
return new Promise((resolve2, reject) => {
|
|
809
815
|
let closed = false;
|
|
810
816
|
let processError;
|
|
811
817
|
let killTimer;
|
|
@@ -834,7 +840,7 @@ var SayEngine = class {
|
|
|
834
840
|
reject(
|
|
835
841
|
failure("Speech process exited unsuccessfully", "SAY_EXIT_FAILED", { code, signal })
|
|
836
842
|
);
|
|
837
|
-
else
|
|
843
|
+
else resolve2();
|
|
838
844
|
});
|
|
839
845
|
request.cancelChild = () => {
|
|
840
846
|
if (closed) return;
|
|
@@ -989,10 +995,10 @@ function registerSettingsRoute(ctx, store) {
|
|
|
989
995
|
const value = request.method === "GET" ? await store.load() : await store.save(patch);
|
|
990
996
|
return Response.json({ ok: true, value, hostPlatform: process.platform }, { headers });
|
|
991
997
|
} catch (error) {
|
|
992
|
-
const
|
|
998
|
+
const invalid2 = error instanceof InvalidSettingsDocument && request.method === "PUT";
|
|
993
999
|
return Response.json(
|
|
994
|
-
{ ok: false, error: { code:
|
|
995
|
-
{ status:
|
|
1000
|
+
{ ok: false, error: { code: invalid2 ? "invalid-settings" : "settings-unavailable" } },
|
|
1001
|
+
{ status: invalid2 ? 400 : 500, headers }
|
|
996
1002
|
);
|
|
997
1003
|
}
|
|
998
1004
|
}
|
|
@@ -1001,9 +1007,1037 @@ function registerSettingsRoute(ctx, store) {
|
|
|
1001
1007
|
}
|
|
1002
1008
|
var APP_VOICE_CONTEXT_PATH = API_ROOT + "/voice-context";
|
|
1003
1009
|
|
|
1010
|
+
// src/app/server/attendeeRelay.ts
|
|
1011
|
+
import { X509Certificate } from "node:crypto";
|
|
1012
|
+
async function connectAttendeeRelay(apiKey, hub, session, loadWs, fetcher, abort) {
|
|
1013
|
+
const timeout = AbortSignal.timeout(5e3);
|
|
1014
|
+
const signal = AbortSignal.any([timeout, abort.signal]);
|
|
1015
|
+
const response = await fetcher("http://127.0.0.1:18766/v1/descriptor", {
|
|
1016
|
+
redirect: "error",
|
|
1017
|
+
signal
|
|
1018
|
+
});
|
|
1019
|
+
if (!response.ok) throw new Error("relay-unavailable");
|
|
1020
|
+
const reader = response.body.getReader();
|
|
1021
|
+
let bytes = 0;
|
|
1022
|
+
const chunks = [];
|
|
1023
|
+
try {
|
|
1024
|
+
while (true) {
|
|
1025
|
+
const { value, done } = await reader.read();
|
|
1026
|
+
if (done) break;
|
|
1027
|
+
bytes += value.byteLength;
|
|
1028
|
+
if (bytes > 16e3) throw Error("relay-unavailable");
|
|
1029
|
+
chunks.push(Buffer.from(value));
|
|
1030
|
+
}
|
|
1031
|
+
} finally {
|
|
1032
|
+
await reader.cancel().catch(() => {
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
const descriptor = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
1036
|
+
if (descriptor.protocol !== "dsh-attendee-relay-v1" || typeof descriptor.certificate !== "string")
|
|
1037
|
+
throw Error("relay-unavailable");
|
|
1038
|
+
const cert = new X509Certificate(descriptor.certificate);
|
|
1039
|
+
if (!cert.checkIP("127.0.0.1") || Date.parse(cert.validTo) <= Date.now())
|
|
1040
|
+
throw Error("relay-unavailable");
|
|
1041
|
+
const module = await loadWs();
|
|
1042
|
+
const WebSocket = module.WebSocket || module.default;
|
|
1043
|
+
if (abort.signal.aborted || hub.session !== session) throw Error("closed");
|
|
1044
|
+
const ws = new WebSocket("wss://127.0.0.1:18765/v1/receive", {
|
|
1045
|
+
ca: descriptor.certificate,
|
|
1046
|
+
rejectUnauthorized: true,
|
|
1047
|
+
headers: { Authorization: "Token " + apiKey },
|
|
1048
|
+
maxPayload: 128e3,
|
|
1049
|
+
perMessageDeflate: false,
|
|
1050
|
+
handshakeTimeout: 5e3
|
|
1051
|
+
});
|
|
1052
|
+
session.socket = ws;
|
|
1053
|
+
return await new Promise((resolve2, reject) => {
|
|
1054
|
+
let ready = false, settled = false;
|
|
1055
|
+
const stop = () => {
|
|
1056
|
+
clearTimeout(timer);
|
|
1057
|
+
abort.signal.removeEventListener("abort", stop);
|
|
1058
|
+
ws.terminate();
|
|
1059
|
+
if (!settled) {
|
|
1060
|
+
settled = true;
|
|
1061
|
+
reject(Error("closed"));
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
abort.signal.addEventListener("abort", stop, { once: true });
|
|
1065
|
+
const timer = setTimeout(() => {
|
|
1066
|
+
stop();
|
|
1067
|
+
}, 6e3);
|
|
1068
|
+
const failed = () => {
|
|
1069
|
+
clearTimeout(timer);
|
|
1070
|
+
abort.signal.removeEventListener("abort", stop);
|
|
1071
|
+
if (!settled) {
|
|
1072
|
+
settled = true;
|
|
1073
|
+
reject(Error("relay-unavailable"));
|
|
1074
|
+
} else if (hub.session === session && !session.error) hub.fail("connection");
|
|
1075
|
+
};
|
|
1076
|
+
ws.on("error", failed);
|
|
1077
|
+
ws.on("close", failed);
|
|
1078
|
+
ws.on("message", (data, binary) => {
|
|
1079
|
+
if (hub.session !== session) return;
|
|
1080
|
+
try {
|
|
1081
|
+
if (binary) throw Error("invalid-audio");
|
|
1082
|
+
const value = JSON.parse(data.toString());
|
|
1083
|
+
if (!ready) {
|
|
1084
|
+
const url = new URL(value.ingestUrl);
|
|
1085
|
+
if (value.type !== "ready" || url.origin !== "wss://dsh-audio-bridge:18765" || url.pathname !== "/ingest" || !/^\?token=[a-f0-9]{64}$/.test(url.search) || url.username || url.password || url.hash)
|
|
1086
|
+
throw Error("relay-unavailable");
|
|
1087
|
+
ready = true;
|
|
1088
|
+
settled = true;
|
|
1089
|
+
clearTimeout(timer);
|
|
1090
|
+
resolve2(url.href);
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
hub.ingest(value);
|
|
1094
|
+
} catch {
|
|
1095
|
+
hub.fail("invalid-audio");
|
|
1096
|
+
if (!settled) {
|
|
1097
|
+
settled = true;
|
|
1098
|
+
clearTimeout(timer);
|
|
1099
|
+
reject(Error("relay-unavailable"));
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// src/app/server/attendeeCredentials.ts
|
|
1107
|
+
import { open, mkdir as mkdir4, rename as rename4, rm as rm4 } from "node:fs/promises";
|
|
1108
|
+
import { constants as constants2 } from "node:fs";
|
|
1109
|
+
import { homedir as homedir4 } from "node:os";
|
|
1110
|
+
import { dirname as dirname4, join as join6, resolve } from "node:path";
|
|
1111
|
+
import { randomUUID as randomUUID4 } from "node:crypto";
|
|
1112
|
+
|
|
1113
|
+
// src/app/server/attendeeConnection.ts
|
|
1114
|
+
var ATTENDEE_ROOT = "/api/dsh-live-voice/attendee";
|
|
1115
|
+
function attendeeReceiverUrl(webServer) {
|
|
1116
|
+
try {
|
|
1117
|
+
const origin = new URL(webServer.getOrigin());
|
|
1118
|
+
if (origin.protocol !== "https:") return "";
|
|
1119
|
+
origin.protocol = "wss:";
|
|
1120
|
+
origin.pathname = ATTENDEE_ROOT + "/ingest";
|
|
1121
|
+
origin.search = "";
|
|
1122
|
+
origin.hash = "";
|
|
1123
|
+
return origin.href;
|
|
1124
|
+
} catch {
|
|
1125
|
+
return "";
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
function validateAttendeeApi(value) {
|
|
1129
|
+
const api = new URL(value);
|
|
1130
|
+
if (!["http:", "https:"].includes(api.protocol) || !["localhost", "127.0.0.1", "[::1]"].includes(api.hostname) || api.username || api.password || api.search || api.hash || !["", "/"].includes(api.pathname))
|
|
1131
|
+
throw new Error("invalid-api");
|
|
1132
|
+
return api.origin;
|
|
1133
|
+
}
|
|
1134
|
+
function validateAttendeeIngress(value) {
|
|
1135
|
+
const url = new URL(value);
|
|
1136
|
+
if (url.protocol !== "wss:" || url.username || url.password || url.search || url.hash)
|
|
1137
|
+
throw new Error("tls-required");
|
|
1138
|
+
if (url.pathname !== ATTENDEE_ROOT + "/ingest") throw new Error("invalid-ingress");
|
|
1139
|
+
return url;
|
|
1140
|
+
}
|
|
1141
|
+
async function attendeeParticipantNames(api, origin, botId, signal) {
|
|
1142
|
+
const path = "bots/" + encodeURIComponent(botId) + "/participants";
|
|
1143
|
+
const endpoint = new URL(origin + "/api/v1/" + path);
|
|
1144
|
+
const names = /* @__PURE__ */ new Map();
|
|
1145
|
+
const cursors = /* @__PURE__ */ new Set([""]);
|
|
1146
|
+
const responseBudget = { remaining: 64e3 };
|
|
1147
|
+
let query = "";
|
|
1148
|
+
let count = 0;
|
|
1149
|
+
for (let page = 0; page < 16; page++) {
|
|
1150
|
+
signal.throwIfAborted();
|
|
1151
|
+
const data = await api(path + query, { signal, responseBudget });
|
|
1152
|
+
signal.throwIfAborted();
|
|
1153
|
+
if (!data || Array.isArray(data) || !Array.isArray(data.results) || !(data.next === null || typeof data.next === "string") || !(data.previous === null || typeof data.previous === "string") || (count += data.results.length) > 256)
|
|
1154
|
+
throw new Error("participants");
|
|
1155
|
+
for (const row of data.results) {
|
|
1156
|
+
if (!row || typeof row.uuid !== "string" || !row.uuid.length || row.uuid.length > 256 || /[\x00-\x1f]/.test(row.uuid) || typeof row.name !== "string")
|
|
1157
|
+
throw new Error("participants");
|
|
1158
|
+
names.set(row.uuid, row.name.replace(/[\x00-\x1f]/g, "").slice(0, 80));
|
|
1159
|
+
}
|
|
1160
|
+
if (data.next === null) return names;
|
|
1161
|
+
if (!data.results.length || data.next.length > 2048) throw new Error("participants");
|
|
1162
|
+
const next = new URL(data.next, endpoint);
|
|
1163
|
+
const cursor = next.searchParams.get("cursor");
|
|
1164
|
+
if (next.origin !== endpoint.origin || next.pathname !== endpoint.pathname || next.username || next.password || next.hash || [...next.searchParams.keys()].length !== 1 || !cursor || cursor.length > 1024 || /[\x00-\x1f]/.test(cursor) || cursors.has(cursor))
|
|
1165
|
+
throw new Error("participants");
|
|
1166
|
+
cursors.add(cursor);
|
|
1167
|
+
query = "?cursor=" + encodeURIComponent(cursor);
|
|
1168
|
+
}
|
|
1169
|
+
throw new Error("participants");
|
|
1170
|
+
}
|
|
1171
|
+
function attendeeApiRequest(origin, key, fetcher) {
|
|
1172
|
+
return async (path, options = {}) => {
|
|
1173
|
+
const abort = new AbortController();
|
|
1174
|
+
const timer = setTimeout(() => abort.abort(), 5e3);
|
|
1175
|
+
const { responseBudget, ...requestOptions } = options;
|
|
1176
|
+
try {
|
|
1177
|
+
const response = await fetcher(origin + "/api/v1/" + path, {
|
|
1178
|
+
...requestOptions,
|
|
1179
|
+
headers: {
|
|
1180
|
+
Authorization: "Token " + key,
|
|
1181
|
+
...options.body ? { "content-type": "application/json" } : {}
|
|
1182
|
+
},
|
|
1183
|
+
signal: options.signal ? AbortSignal.any([abort.signal, options.signal]) : abort.signal,
|
|
1184
|
+
redirect: "error"
|
|
1185
|
+
});
|
|
1186
|
+
if (!response.ok) throw new Error("api-failed");
|
|
1187
|
+
if (response.status === 204) return {};
|
|
1188
|
+
const reader = response.body.getReader();
|
|
1189
|
+
const chunks = [];
|
|
1190
|
+
let length = 0;
|
|
1191
|
+
try {
|
|
1192
|
+
while (true) {
|
|
1193
|
+
const { done, value } = await reader.read();
|
|
1194
|
+
if (done) break;
|
|
1195
|
+
length += value.byteLength;
|
|
1196
|
+
if (responseBudget) responseBudget.remaining -= value.byteLength;
|
|
1197
|
+
if (length > 64e3 || responseBudget && responseBudget.remaining < 0)
|
|
1198
|
+
throw new Error("api-failed");
|
|
1199
|
+
chunks.push(Buffer.from(value));
|
|
1200
|
+
}
|
|
1201
|
+
} finally {
|
|
1202
|
+
await reader.cancel().catch(() => {
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
1206
|
+
} finally {
|
|
1207
|
+
clearTimeout(timer);
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
// src/app/server/attendeeCredentials.ts
|
|
1213
|
+
var queues = /* @__PURE__ */ new Map();
|
|
1214
|
+
var invalid = () => new Error("credential-storage-invalid");
|
|
1215
|
+
function validateAttendeeKey(value) {
|
|
1216
|
+
if (typeof value !== "string" || value.length < 8 || value.length > 512 || /[\x00-\x20\x7f]/.test(value))
|
|
1217
|
+
throw new Error("invalid-api");
|
|
1218
|
+
return value;
|
|
1219
|
+
}
|
|
1220
|
+
function createAttendeeCredentialStore(path = join6(homedir4(), ".dsh", "dsh-live-voice.attendee-credentials.json")) {
|
|
1221
|
+
path = resolve(path);
|
|
1222
|
+
const serial = (run) => {
|
|
1223
|
+
const next = (queues.get(path) ?? Promise.resolve()).then(run);
|
|
1224
|
+
const tail = next.then(
|
|
1225
|
+
() => {
|
|
1226
|
+
},
|
|
1227
|
+
() => {
|
|
1228
|
+
}
|
|
1229
|
+
);
|
|
1230
|
+
queues.set(path, tail);
|
|
1231
|
+
void tail.then(() => {
|
|
1232
|
+
if (queues.get(path) === tail) queues.delete(path);
|
|
1233
|
+
});
|
|
1234
|
+
return next;
|
|
1235
|
+
};
|
|
1236
|
+
async function load() {
|
|
1237
|
+
let file;
|
|
1238
|
+
try {
|
|
1239
|
+
file = await open(path, constants2.O_RDONLY | constants2.O_NOFOLLOW | constants2.O_NONBLOCK);
|
|
1240
|
+
const stat = await file.stat();
|
|
1241
|
+
if (!stat.isFile() || stat.size > 65536 || (stat.mode & 511) !== 384 || process.getuid && stat.uid !== process.getuid())
|
|
1242
|
+
throw invalid();
|
|
1243
|
+
const buffer = Buffer.alloc(65537);
|
|
1244
|
+
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
|
|
1245
|
+
if (bytesRead > 65536) throw invalid();
|
|
1246
|
+
const doc = JSON.parse(buffer.subarray(0, bytesRead).toString("utf8"));
|
|
1247
|
+
if (!doc || typeof doc !== "object" || Array.isArray(doc) || Object.keys(doc).sort().join(",") !== "origins,version" || doc.version !== 1 || !doc.origins || typeof doc.origins !== "object" || Array.isArray(doc.origins))
|
|
1248
|
+
throw invalid();
|
|
1249
|
+
const entries = Object.entries(doc.origins);
|
|
1250
|
+
if (entries.length > 32) throw invalid();
|
|
1251
|
+
const origins = /* @__PURE__ */ Object.create(null);
|
|
1252
|
+
for (const [origin2, key] of entries) {
|
|
1253
|
+
try {
|
|
1254
|
+
if (origin2.length > 256 || validateAttendeeApi(origin2) !== origin2) throw invalid();
|
|
1255
|
+
origins[origin2] = validateAttendeeKey(key);
|
|
1256
|
+
} catch {
|
|
1257
|
+
throw invalid();
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return origins;
|
|
1261
|
+
} catch (error) {
|
|
1262
|
+
if (error.code === "ENOENT") return /* @__PURE__ */ Object.create(null);
|
|
1263
|
+
if (error.message === "credential-storage-invalid" || error instanceof SyntaxError || error.message === "invalid-api" || error.code === "ELOOP")
|
|
1264
|
+
throw invalid();
|
|
1265
|
+
throw new Error("credential-storage-unavailable");
|
|
1266
|
+
} finally {
|
|
1267
|
+
await file?.close().catch(() => {
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
function origin(value) {
|
|
1272
|
+
try {
|
|
1273
|
+
const normalized = validateAttendeeApi(value);
|
|
1274
|
+
if (normalized.length > 256) throw Error();
|
|
1275
|
+
return normalized;
|
|
1276
|
+
} catch {
|
|
1277
|
+
throw new Error("invalid-api");
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
async function mutate(apiUrl, key) {
|
|
1281
|
+
const normalized = origin(apiUrl);
|
|
1282
|
+
const validated = key === void 0 ? void 0 : validateAttendeeKey(key);
|
|
1283
|
+
return serial(async () => {
|
|
1284
|
+
const origins = await load();
|
|
1285
|
+
if (validated === void 0) delete origins[normalized];
|
|
1286
|
+
else origins[normalized] = validated;
|
|
1287
|
+
if (Object.keys(origins).length > 32) throw invalid();
|
|
1288
|
+
const data = JSON.stringify({ version: 1, origins }) + "\n";
|
|
1289
|
+
if (Buffer.byteLength(data) > 65536) throw invalid();
|
|
1290
|
+
const temporary = path + "." + randomUUID4() + ".tmp";
|
|
1291
|
+
try {
|
|
1292
|
+
await mkdir4(dirname4(path), { recursive: true, mode: 448 });
|
|
1293
|
+
const file = await open(temporary, "wx", 384);
|
|
1294
|
+
try {
|
|
1295
|
+
await file.writeFile(data);
|
|
1296
|
+
await file.sync();
|
|
1297
|
+
} finally {
|
|
1298
|
+
await file.close();
|
|
1299
|
+
}
|
|
1300
|
+
await rename4(temporary, path);
|
|
1301
|
+
} catch {
|
|
1302
|
+
throw new Error("credential-storage-unavailable");
|
|
1303
|
+
} finally {
|
|
1304
|
+
await rm4(temporary, { force: true }).catch(() => {
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
return { hasSavedKey: validated !== void 0 };
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
return {
|
|
1311
|
+
metadata(apiUrl) {
|
|
1312
|
+
const normalized = origin(apiUrl);
|
|
1313
|
+
return serial(async () => ({ hasSavedKey: Object.hasOwn(await load(), normalized) }));
|
|
1314
|
+
},
|
|
1315
|
+
get(apiUrl) {
|
|
1316
|
+
const normalized = origin(apiUrl);
|
|
1317
|
+
return serial(async () => {
|
|
1318
|
+
const key = (await load())[normalized];
|
|
1319
|
+
if (!key) throw new Error("credential-missing");
|
|
1320
|
+
return key;
|
|
1321
|
+
});
|
|
1322
|
+
},
|
|
1323
|
+
save: (apiUrl, key) => mutate(apiUrl, key),
|
|
1324
|
+
remove: (apiUrl) => mutate(apiUrl)
|
|
1325
|
+
};
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// src/modules/conversation/models/AttendeeHub.ts
|
|
1329
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
1330
|
+
|
|
1331
|
+
// src/modules/conversation/models/attendeeProtocol.ts
|
|
1332
|
+
var ATTENDEE_MAX_MESSAGE = 128e3;
|
|
1333
|
+
var ATTENDEE_MAX_PARTICIPANTS = 16;
|
|
1334
|
+
function attendeeAudio(value, botId) {
|
|
1335
|
+
const d = value?.data;
|
|
1336
|
+
if (value?.trigger !== "realtime_audio.per_participant" || typeof value.bot_id !== "string" || !/^bot_[a-zA-Z0-9]{1,100}$/.test(value.bot_id) || botId && value.bot_id !== botId || typeof d?.participant_uuid !== "string" || !d.participant_uuid.length || d.participant_uuid.length > 256 || /[\x00-\x1f]/.test(d.participant_uuid) || ![8e3, 16e3].includes(d.sample_rate) || !Number.isSafeInteger(d.timestamp_ms) || d.timestamp_ms < 0 || typeof d.chunk !== "string" || !d.chunk.length || d.chunk.length > 9e4 || d.chunk.length % 4 !== 0 || !/^[A-Za-z0-9+/]+={0,2}$/.test(d.chunk))
|
|
1337
|
+
throw new Error("invalid-audio");
|
|
1338
|
+
const bytes = d.chunk.length / 4 * 3 - (d.chunk.endsWith("==") ? 2 : d.chunk.endsWith("=") ? 1 : 0);
|
|
1339
|
+
if (bytes % 2 || bytes > d.sample_rate * 2) throw new Error("invalid-audio");
|
|
1340
|
+
return {
|
|
1341
|
+
botId: value.bot_id,
|
|
1342
|
+
participantId: d.participant_uuid,
|
|
1343
|
+
chunk: d.chunk,
|
|
1344
|
+
sampleRate: d.sample_rate,
|
|
1345
|
+
timestamp: d.timestamp_ms
|
|
1346
|
+
};
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
// src/modules/conversation/models/AttendeeHub.ts
|
|
1350
|
+
var AttendeeHub = class {
|
|
1351
|
+
session = null;
|
|
1352
|
+
open() {
|
|
1353
|
+
if (this.session) throw new Error("busy");
|
|
1354
|
+
const s = {
|
|
1355
|
+
owner: randomBytes(32).toString("hex"),
|
|
1356
|
+
ingest: randomBytes(32).toString("hex"),
|
|
1357
|
+
botId: "",
|
|
1358
|
+
events: [],
|
|
1359
|
+
sequence: 0,
|
|
1360
|
+
bytes: 0,
|
|
1361
|
+
names: /* @__PURE__ */ new Map(),
|
|
1362
|
+
participants: /* @__PURE__ */ new Set(),
|
|
1363
|
+
status: "waiting",
|
|
1364
|
+
error: "",
|
|
1365
|
+
lastRead: Date.now(),
|
|
1366
|
+
socket: null
|
|
1367
|
+
};
|
|
1368
|
+
this.session = s;
|
|
1369
|
+
return s;
|
|
1370
|
+
}
|
|
1371
|
+
owns(token, ingest = false) {
|
|
1372
|
+
const expected = this.session?.[ingest ? "ingest" : "owner"];
|
|
1373
|
+
return this.ownsControl(token, expected);
|
|
1374
|
+
}
|
|
1375
|
+
ownsControl(token, expected) {
|
|
1376
|
+
return typeof token === "string" && /^[a-f0-9]{64}$/.test(token) && expected && token.length === expected.length && timingSafeEqual(Buffer.from(token), Buffer.from(expected));
|
|
1377
|
+
}
|
|
1378
|
+
ingest(value) {
|
|
1379
|
+
const s = this.session;
|
|
1380
|
+
if (!s) throw new Error("closed");
|
|
1381
|
+
if (s.error) throw new Error("closed");
|
|
1382
|
+
const a = attendeeAudio(value, s.botId || void 0);
|
|
1383
|
+
if (!s.botId) s.botId = a.botId;
|
|
1384
|
+
if (!s.participants.has(a.participantId) && s.participants.size >= ATTENDEE_MAX_PARTICIPANTS)
|
|
1385
|
+
throw new Error("participants-limit");
|
|
1386
|
+
s.participants.add(a.participantId);
|
|
1387
|
+
a.name = s.names.get(a.participantId);
|
|
1388
|
+
s.events.push({ sequence: ++s.sequence, ...a });
|
|
1389
|
+
s.bytes += a.chunk.length;
|
|
1390
|
+
if (s.bytes > 2e6 || s.events.length > 1500 || Date.now() - s.lastRead > 15e3)
|
|
1391
|
+
throw new Error("overloaded");
|
|
1392
|
+
s.status = "receiving";
|
|
1393
|
+
}
|
|
1394
|
+
read(cursor) {
|
|
1395
|
+
const s = this.session;
|
|
1396
|
+
if (!s) throw new Error("closed");
|
|
1397
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0 || cursor > s.sequence)
|
|
1398
|
+
throw new Error("invalid-cursor");
|
|
1399
|
+
if (s.events.length && cursor < s.events[0].sequence - 1) throw new Error("audio-gap");
|
|
1400
|
+
s.lastRead = Date.now();
|
|
1401
|
+
while (s.events.length && s.events[0].sequence <= cursor)
|
|
1402
|
+
s.bytes -= s.events.shift().chunk.length;
|
|
1403
|
+
const events = [];
|
|
1404
|
+
let batchBytes = 0;
|
|
1405
|
+
for (const event of s.events) {
|
|
1406
|
+
if (events.length >= 100 || events.length && batchBytes + event.chunk.length > 128e3)
|
|
1407
|
+
break;
|
|
1408
|
+
events.push(event);
|
|
1409
|
+
batchBytes += event.chunk.length;
|
|
1410
|
+
}
|
|
1411
|
+
return {
|
|
1412
|
+
status: s.status,
|
|
1413
|
+
error: s.error,
|
|
1414
|
+
botId: s.botId,
|
|
1415
|
+
participants: [...s.names].map(([id, name2]) => ({ id, name: name2 })),
|
|
1416
|
+
events,
|
|
1417
|
+
hasMore: s.events.length > events.length,
|
|
1418
|
+
cursor: events.at(-1)?.sequence ?? cursor
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
fail(code) {
|
|
1422
|
+
if (!this.session) return;
|
|
1423
|
+
this.session.error = code;
|
|
1424
|
+
this.session.status = "failed";
|
|
1425
|
+
this.session.socket?.close(1008, "stream stopped");
|
|
1426
|
+
}
|
|
1427
|
+
close() {
|
|
1428
|
+
const s = this.session;
|
|
1429
|
+
this.session = null;
|
|
1430
|
+
s?.socket?.close(1e3, "receiver closed");
|
|
1431
|
+
if (s) {
|
|
1432
|
+
s.events.length = 0;
|
|
1433
|
+
s.names.clear();
|
|
1434
|
+
s.participants.clear();
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
};
|
|
1438
|
+
|
|
1439
|
+
// src/app/server/attendeeRoutes.ts
|
|
1440
|
+
function registerAttendeeRoutes(ctx, loadWs = () => import("ws"), fetcher = (...args) => globalThis.fetch(...args), credentials = createAttendeeCredentialStore()) {
|
|
1441
|
+
const hub = new AttendeeHub();
|
|
1442
|
+
let disposed = false;
|
|
1443
|
+
let disposeUpgrade, server, timer, pollAbort, controlTimer;
|
|
1444
|
+
let control = null;
|
|
1445
|
+
const clear = () => {
|
|
1446
|
+
const s = hub.session;
|
|
1447
|
+
if (s && control?.owner === s.owner && !control.botId && s.botId) control.botId = s.botId;
|
|
1448
|
+
clearInterval(timer);
|
|
1449
|
+
pollAbort?.abort();
|
|
1450
|
+
pollAbort = null;
|
|
1451
|
+
hub.session?.relayAbort?.abort();
|
|
1452
|
+
if (hub.session) hub.session.api = null;
|
|
1453
|
+
hub.close();
|
|
1454
|
+
};
|
|
1455
|
+
const unresolved = (c) => c && (c.botId || c.joining || c.joinUncertain);
|
|
1456
|
+
const controlValue = () => ({
|
|
1457
|
+
hasBotControl: Boolean(control),
|
|
1458
|
+
botId: control?.botId || "",
|
|
1459
|
+
botState: control?.botState || "",
|
|
1460
|
+
joining: Boolean(control?.joining),
|
|
1461
|
+
joinUncertain: Boolean(control?.joinUncertain),
|
|
1462
|
+
leaving: Boolean(control?.leaving),
|
|
1463
|
+
controlExpiresAt: control ? Math.min(control.lastSeen + 3e5, control.createdAt + 72e5) : 0,
|
|
1464
|
+
controlError: control?.error || null
|
|
1465
|
+
});
|
|
1466
|
+
const release = () => {
|
|
1467
|
+
clear();
|
|
1468
|
+
clearInterval(controlTimer);
|
|
1469
|
+
const c = control;
|
|
1470
|
+
control = null;
|
|
1471
|
+
c?.abort.abort();
|
|
1472
|
+
if (c) c.api = null;
|
|
1473
|
+
};
|
|
1474
|
+
const expire = () => {
|
|
1475
|
+
if (control && Date.now() >= controlValue().controlExpiresAt) release();
|
|
1476
|
+
};
|
|
1477
|
+
const ownsControl = (owner) => {
|
|
1478
|
+
expire();
|
|
1479
|
+
return hub.ownsControl(owner, control?.owner);
|
|
1480
|
+
};
|
|
1481
|
+
const register = ctx.connection.fetch.register({
|
|
1482
|
+
path: ATTENDEE_ROOT,
|
|
1483
|
+
methods: ["GET", "POST"],
|
|
1484
|
+
requestBody: "buffered",
|
|
1485
|
+
fetch: async (request) => {
|
|
1486
|
+
const headers = { "cache-control": "no-store" };
|
|
1487
|
+
try {
|
|
1488
|
+
if (disposed) throw new Error("closed");
|
|
1489
|
+
if (request.method === "GET") {
|
|
1490
|
+
const url = new URL(request.url);
|
|
1491
|
+
if (url.searchParams.get("action") === "credential")
|
|
1492
|
+
return Response.json(
|
|
1493
|
+
{
|
|
1494
|
+
ok: true,
|
|
1495
|
+
value: await credentials.metadata(
|
|
1496
|
+
validateAttendeeApi(url.searchParams.get("apiUrl"))
|
|
1497
|
+
)
|
|
1498
|
+
},
|
|
1499
|
+
{ headers }
|
|
1500
|
+
);
|
|
1501
|
+
if (url.searchParams.get("action") === "capabilities")
|
|
1502
|
+
return Response.json(
|
|
1503
|
+
{
|
|
1504
|
+
ok: true,
|
|
1505
|
+
value: {
|
|
1506
|
+
receiverUrl: attendeeReceiverUrl(ctx.webServer),
|
|
1507
|
+
tlsRequired: !attendeeReceiverUrl(ctx.webServer)
|
|
1508
|
+
}
|
|
1509
|
+
},
|
|
1510
|
+
{ headers }
|
|
1511
|
+
);
|
|
1512
|
+
const token = request.headers.get("x-dlv-attendee-owner");
|
|
1513
|
+
if (!ownsControl(token) || url.searchParams.get("action") !== "control" && !hub.session)
|
|
1514
|
+
return Response.json(
|
|
1515
|
+
{ ok: false, error: { code: "ownership" } },
|
|
1516
|
+
{ status: 403, headers }
|
|
1517
|
+
);
|
|
1518
|
+
control.lastSeen = Date.now();
|
|
1519
|
+
if (!control.botId && hub.session?.botId) control.botId = hub.session.botId;
|
|
1520
|
+
if (url.searchParams.get("action") === "control")
|
|
1521
|
+
return Response.json({ ok: true, value: controlValue() }, { headers });
|
|
1522
|
+
return Response.json(
|
|
1523
|
+
{
|
|
1524
|
+
ok: true,
|
|
1525
|
+
value: {
|
|
1526
|
+
...hub.read(Number(url.searchParams.get("cursor") || 0)),
|
|
1527
|
+
...controlValue()
|
|
1528
|
+
}
|
|
1529
|
+
},
|
|
1530
|
+
{ headers }
|
|
1531
|
+
);
|
|
1532
|
+
}
|
|
1533
|
+
const text = await request.text();
|
|
1534
|
+
if (text.length > 12e3) throw new Error("invalid-request");
|
|
1535
|
+
const p = JSON.parse(text);
|
|
1536
|
+
if (p.action === "credential-save" || p.action === "credential-remove") {
|
|
1537
|
+
const origin = validateAttendeeApi(p.apiUrl);
|
|
1538
|
+
const value = p.action === "credential-save" ? await credentials.save(origin, validateAttendeeKey(p.apiKey)) : await credentials.remove(origin);
|
|
1539
|
+
return Response.json({ ok: true, value }, { headers });
|
|
1540
|
+
}
|
|
1541
|
+
if (p.action === "disconnect") {
|
|
1542
|
+
if (!ownsControl(p.owner))
|
|
1543
|
+
return Response.json(
|
|
1544
|
+
{ ok: false, error: { code: "ownership" } },
|
|
1545
|
+
{ status: 403, headers }
|
|
1546
|
+
);
|
|
1547
|
+
const c = control;
|
|
1548
|
+
if (!c.disconnectPromise) {
|
|
1549
|
+
if (!c.botId && hub.session?.botId) c.botId = hub.session.botId;
|
|
1550
|
+
const api2 = c.api;
|
|
1551
|
+
clear();
|
|
1552
|
+
c.disconnectPromise = (async () => {
|
|
1553
|
+
const abort = new AbortController();
|
|
1554
|
+
let timer2;
|
|
1555
|
+
let leaveRequested = false;
|
|
1556
|
+
let warning = null;
|
|
1557
|
+
const timeout = new Promise((_, reject) => {
|
|
1558
|
+
timer2 = setTimeout(() => {
|
|
1559
|
+
abort.abort();
|
|
1560
|
+
reject(Error("disconnect-timeout"));
|
|
1561
|
+
}, 3e3);
|
|
1562
|
+
});
|
|
1563
|
+
try {
|
|
1564
|
+
await Promise.race([
|
|
1565
|
+
(async () => {
|
|
1566
|
+
let id = c.botId;
|
|
1567
|
+
if (!id && c.joinPromise) {
|
|
1568
|
+
const result = await c.joinPromise;
|
|
1569
|
+
if (/^bot_[a-zA-Z0-9]{1,100}$/.test(result?.id)) id = result.id;
|
|
1570
|
+
}
|
|
1571
|
+
abort.signal.throwIfAborted();
|
|
1572
|
+
if (id) {
|
|
1573
|
+
await api2("bots/" + id + "/leave", { method: "POST", signal: abort.signal });
|
|
1574
|
+
leaveRequested = true;
|
|
1575
|
+
} else if (c.joining || c.joinUncertain) warning = "control-unknown";
|
|
1576
|
+
})(),
|
|
1577
|
+
timeout
|
|
1578
|
+
]);
|
|
1579
|
+
} catch {
|
|
1580
|
+
warning = c.botId ? "leave-failed" : "control-unknown";
|
|
1581
|
+
} finally {
|
|
1582
|
+
clearTimeout(timer2);
|
|
1583
|
+
abort.abort();
|
|
1584
|
+
if (control === c) release();
|
|
1585
|
+
}
|
|
1586
|
+
return { disconnected: true, leaveRequested, warning };
|
|
1587
|
+
})();
|
|
1588
|
+
}
|
|
1589
|
+
return Response.json({ ok: true, value: await c.disconnectPromise }, { headers });
|
|
1590
|
+
}
|
|
1591
|
+
if (["close", "release"].includes(p.action)) {
|
|
1592
|
+
if (!ownsControl(p.owner))
|
|
1593
|
+
return Response.json(
|
|
1594
|
+
{ ok: false, error: { code: "ownership" } },
|
|
1595
|
+
{ status: 403, headers }
|
|
1596
|
+
);
|
|
1597
|
+
if (!control.botId && hub.session?.botId) control.botId = hub.session.botId;
|
|
1598
|
+
if (p.action === "release" || !unresolved(control)) release();
|
|
1599
|
+
else clear();
|
|
1600
|
+
return Response.json({ ok: true, value: controlValue() }, { headers });
|
|
1601
|
+
}
|
|
1602
|
+
if (["join", "leave"].includes(p.action)) {
|
|
1603
|
+
if (!ownsControl(p.owner))
|
|
1604
|
+
return Response.json(
|
|
1605
|
+
{ ok: false, error: { code: "ownership" } },
|
|
1606
|
+
{ status: 403, headers }
|
|
1607
|
+
);
|
|
1608
|
+
const c = control;
|
|
1609
|
+
c.lastSeen = Date.now();
|
|
1610
|
+
if (!c.botId && hub.session?.botId) c.botId = hub.session.botId;
|
|
1611
|
+
const s2 = hub.session;
|
|
1612
|
+
if (p.action === "leave") {
|
|
1613
|
+
if (!c.botId) throw new Error("invalid-bot");
|
|
1614
|
+
if (c.leaving) throw new Error("busy");
|
|
1615
|
+
c.leaving = true;
|
|
1616
|
+
try {
|
|
1617
|
+
const result = await c.api("bots/" + encodeURIComponent(c.botId) + "/leave", {
|
|
1618
|
+
method: "POST",
|
|
1619
|
+
body: "{}",
|
|
1620
|
+
signal: c.abort.signal
|
|
1621
|
+
});
|
|
1622
|
+
if (control !== c) throw new Error("ownership");
|
|
1623
|
+
if (result?.state && /^[a-z_]{1,80}$/.test(result.state)) c.botState = result.state;
|
|
1624
|
+
c.error = null;
|
|
1625
|
+
clear();
|
|
1626
|
+
return Response.json(
|
|
1627
|
+
{ ok: true, value: { ...controlValue(), leaving: false, leaveRequested: true } },
|
|
1628
|
+
{ headers }
|
|
1629
|
+
);
|
|
1630
|
+
} catch (error) {
|
|
1631
|
+
if (control === c) c.error = "leave-failed";
|
|
1632
|
+
throw error;
|
|
1633
|
+
} finally {
|
|
1634
|
+
c.leaving = false;
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
if (!s2 || c.botId || c.joining || c.joinUncertain) throw new Error("busy");
|
|
1638
|
+
if (!s2.ingestUrl) throw new Error("tls-required");
|
|
1639
|
+
const meeting = new URL(p.meetingUrl);
|
|
1640
|
+
if (meeting.protocol !== "https:" || meeting.hostname !== "meet.google.com" || meeting.port || !/^\/[a-z]{3}-[a-z]{4}-[a-z]{3}$/.test(meeting.pathname) || meeting.username || meeting.password)
|
|
1641
|
+
throw new Error("invalid-meeting");
|
|
1642
|
+
if (typeof p.botName !== "string" || !p.botName.trim() || p.botName.length > 80 || /[\x00-\x1f]/.test(p.botName))
|
|
1643
|
+
throw new Error("invalid-bot");
|
|
1644
|
+
c.joining = true;
|
|
1645
|
+
try {
|
|
1646
|
+
c.joinPromise = c.api("bots", {
|
|
1647
|
+
method: "POST",
|
|
1648
|
+
signal: c.abort.signal,
|
|
1649
|
+
body: JSON.stringify({
|
|
1650
|
+
meeting_url: meeting.origin + meeting.pathname,
|
|
1651
|
+
bot_name: p.botName.trim(),
|
|
1652
|
+
recording_settings: { format: "none" },
|
|
1653
|
+
transcription_settings: { meeting_closed_captions: {} },
|
|
1654
|
+
websocket_settings: {
|
|
1655
|
+
per_participant_audio: { url: s2.ingestUrl, sample_rate: 16e3 }
|
|
1656
|
+
}
|
|
1657
|
+
})
|
|
1658
|
+
});
|
|
1659
|
+
const result = await c.joinPromise;
|
|
1660
|
+
if (!/^bot_[a-zA-Z0-9]{1,100}$/.test(result?.id) || control !== c || c.botId && c.botId !== result.id)
|
|
1661
|
+
throw new Error("join-uncertain");
|
|
1662
|
+
c.botId = result.id;
|
|
1663
|
+
c.botState = /^[a-z_]{1,80}$/.test(result.state || "") ? result.state : "";
|
|
1664
|
+
if (hub.session === s2) {
|
|
1665
|
+
s2.botId = c.botId;
|
|
1666
|
+
void s2.refresh?.();
|
|
1667
|
+
}
|
|
1668
|
+
return Response.json(
|
|
1669
|
+
{ ok: true, value: { ...controlValue(), joining: false } },
|
|
1670
|
+
{ headers }
|
|
1671
|
+
);
|
|
1672
|
+
} catch {
|
|
1673
|
+
if (control === c) {
|
|
1674
|
+
c.joinUncertain = true;
|
|
1675
|
+
c.error = "control-unknown";
|
|
1676
|
+
}
|
|
1677
|
+
throw new Error("join-uncertain");
|
|
1678
|
+
} finally {
|
|
1679
|
+
c.joining = false;
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
expire();
|
|
1683
|
+
if (p.action !== "open" || hub.session || control)
|
|
1684
|
+
throw new Error(hub.session || control ? "busy" : "invalid-request");
|
|
1685
|
+
if (!ctx.webServer?.registerUpgrade) throw new Error("web-unavailable");
|
|
1686
|
+
const api = validateAttendeeApi(p.apiUrl);
|
|
1687
|
+
const explicitKey = p.apiKey !== void 0 && p.apiKey !== "";
|
|
1688
|
+
if (explicitKey) validateAttendeeKey(p.apiKey);
|
|
1689
|
+
if (p.botId && !/^bot_[a-zA-Z0-9]{1,100}$/.test(p.botId)) throw new Error("invalid-bot");
|
|
1690
|
+
const ingressValue = p.ingressUrl || attendeeReceiverUrl(ctx.webServer);
|
|
1691
|
+
const ingress = ingressValue ? validateAttendeeIngress(ingressValue) : null;
|
|
1692
|
+
const key = explicitKey ? validateAttendeeKey(p.apiKey) : await credentials.get(api);
|
|
1693
|
+
if (explicitKey) await credentials.save(api, key);
|
|
1694
|
+
if (disposed || hub.session || control) throw new Error(disposed ? "closed" : "busy");
|
|
1695
|
+
const s = hub.open();
|
|
1696
|
+
s.botId = p.botId || "";
|
|
1697
|
+
s.api = attendeeApiRequest(api, key, fetcher);
|
|
1698
|
+
control = {
|
|
1699
|
+
owner: s.owner,
|
|
1700
|
+
api: s.api,
|
|
1701
|
+
botId: s.botId,
|
|
1702
|
+
botState: "",
|
|
1703
|
+
createdAt: Date.now(),
|
|
1704
|
+
lastSeen: Date.now(),
|
|
1705
|
+
abort: new AbortController()
|
|
1706
|
+
};
|
|
1707
|
+
controlTimer = setInterval(expire, 2e3);
|
|
1708
|
+
controlTimer.unref?.();
|
|
1709
|
+
s.ingressUrl = ingress?.href || "";
|
|
1710
|
+
if (p.verify === true) {
|
|
1711
|
+
try {
|
|
1712
|
+
await s.api("bots?limit=1");
|
|
1713
|
+
} catch {
|
|
1714
|
+
if (hub.session === s) release();
|
|
1715
|
+
throw new Error("invalid-api");
|
|
1716
|
+
}
|
|
1717
|
+
if (hub.session !== s) throw new Error("closed");
|
|
1718
|
+
}
|
|
1719
|
+
try {
|
|
1720
|
+
if (!ingress && p.verify === true) {
|
|
1721
|
+
s.relayAbort = new AbortController();
|
|
1722
|
+
try {
|
|
1723
|
+
s.relayUrl = await connectAttendeeRelay(key, hub, s, loadWs, fetcher, s.relayAbort);
|
|
1724
|
+
} catch {
|
|
1725
|
+
throw new Error("relay-unavailable");
|
|
1726
|
+
}
|
|
1727
|
+
if (disposed || hub.session !== s) {
|
|
1728
|
+
s.relayAbort.abort();
|
|
1729
|
+
throw new Error("closed");
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
if (ingress && !server) {
|
|
1733
|
+
const { WebSocketServer } = await loadWs();
|
|
1734
|
+
if (disposed || hub.session !== s) throw new Error("closed");
|
|
1735
|
+
server = new WebSocketServer({
|
|
1736
|
+
noServer: true,
|
|
1737
|
+
maxPayload: ATTENDEE_MAX_MESSAGE,
|
|
1738
|
+
perMessageDeflate: false
|
|
1739
|
+
});
|
|
1740
|
+
try {
|
|
1741
|
+
disposeUpgrade = ctx.webServer.registerUpgrade({
|
|
1742
|
+
path: ATTENDEE_ROOT + "/ingest",
|
|
1743
|
+
handler: (req, socket, head) => {
|
|
1744
|
+
const u = new URL(req.url, "http://receiver");
|
|
1745
|
+
if (req.headers.origin || !hub.owns(u.searchParams.get("token"), true) || hub.session.socket) {
|
|
1746
|
+
socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n");
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
const s2 = hub.session;
|
|
1750
|
+
server.handleUpgrade(req, socket, head, (ws) => {
|
|
1751
|
+
s2.socket = ws;
|
|
1752
|
+
ws.on("error", () => {
|
|
1753
|
+
if (hub.session === s2) hub.fail("connection");
|
|
1754
|
+
});
|
|
1755
|
+
ws.on("close", () => {
|
|
1756
|
+
if (hub.session === s2 && !s2.error) {
|
|
1757
|
+
s2.status = "disconnected";
|
|
1758
|
+
}
|
|
1759
|
+
});
|
|
1760
|
+
ws.on("message", (data, binary) => {
|
|
1761
|
+
if (hub.session !== s2) return;
|
|
1762
|
+
try {
|
|
1763
|
+
if (binary || s2.error) throw new Error("invalid-audio");
|
|
1764
|
+
hub.ingest(JSON.parse(data.toString()));
|
|
1765
|
+
} catch (e) {
|
|
1766
|
+
hub.fail(
|
|
1767
|
+
["overloaded", "participants-limit"].includes(e.message) ? e.message : "invalid-audio"
|
|
1768
|
+
);
|
|
1769
|
+
}
|
|
1770
|
+
});
|
|
1771
|
+
});
|
|
1772
|
+
}
|
|
1773
|
+
});
|
|
1774
|
+
} catch (error) {
|
|
1775
|
+
server.close();
|
|
1776
|
+
server = null;
|
|
1777
|
+
throw error;
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
} catch (e) {
|
|
1781
|
+
if (hub.session === s) release();
|
|
1782
|
+
throw e;
|
|
1783
|
+
}
|
|
1784
|
+
const refresh = async () => {
|
|
1785
|
+
if (hub.session !== s) return;
|
|
1786
|
+
if (Date.now() - s.lastRead > 15e3) {
|
|
1787
|
+
clear();
|
|
1788
|
+
return;
|
|
1789
|
+
}
|
|
1790
|
+
if (!s.botId || s.error || pollAbort) return;
|
|
1791
|
+
pollAbort = new AbortController();
|
|
1792
|
+
const abort = pollAbort;
|
|
1793
|
+
const deadline = setTimeout(() => abort.abort(), 5e3);
|
|
1794
|
+
try {
|
|
1795
|
+
const names = await attendeeParticipantNames(s.api, api, s.botId, abort.signal);
|
|
1796
|
+
if (hub.session !== s || abort.signal.aborted) return;
|
|
1797
|
+
s.names = names;
|
|
1798
|
+
} catch {
|
|
1799
|
+
if (hub.session === s) hub.fail("participants");
|
|
1800
|
+
} finally {
|
|
1801
|
+
clearTimeout(deadline);
|
|
1802
|
+
if (pollAbort === abort) pollAbort = null;
|
|
1803
|
+
}
|
|
1804
|
+
};
|
|
1805
|
+
s.refresh = refresh;
|
|
1806
|
+
timer = setInterval(() => void refresh(), 2e3);
|
|
1807
|
+
timer.unref?.();
|
|
1808
|
+
ingress?.searchParams.set("token", s.ingest);
|
|
1809
|
+
s.ingestUrl = s.relayUrl || ingress?.href || "";
|
|
1810
|
+
return Response.json(
|
|
1811
|
+
{
|
|
1812
|
+
ok: true,
|
|
1813
|
+
value: {
|
|
1814
|
+
...controlValue(),
|
|
1815
|
+
owner: s.owner,
|
|
1816
|
+
ingestUrl: s.ingestUrl,
|
|
1817
|
+
tlsRequired: !s.ingestUrl,
|
|
1818
|
+
botId: s.botId,
|
|
1819
|
+
hasSavedKey: true
|
|
1820
|
+
}
|
|
1821
|
+
},
|
|
1822
|
+
{ headers }
|
|
1823
|
+
);
|
|
1824
|
+
} catch (e) {
|
|
1825
|
+
const code = e?.code === "ERR_MODULE_NOT_FOUND" ? "dependency" : [
|
|
1826
|
+
"ownership",
|
|
1827
|
+
"busy",
|
|
1828
|
+
"web-unavailable",
|
|
1829
|
+
"invalid-api",
|
|
1830
|
+
"credential-missing",
|
|
1831
|
+
"credential-storage-invalid",
|
|
1832
|
+
"credential-storage-unavailable",
|
|
1833
|
+
"invalid-bot",
|
|
1834
|
+
"tls-required",
|
|
1835
|
+
"invalid-ingress",
|
|
1836
|
+
"invalid-request",
|
|
1837
|
+
"invalid-meeting",
|
|
1838
|
+
"join-uncertain",
|
|
1839
|
+
"relay-unavailable"
|
|
1840
|
+
].includes(e.message) ? e.message : "failed";
|
|
1841
|
+
return Response.json(
|
|
1842
|
+
{ ok: false, error: { code } },
|
|
1843
|
+
{ status: code === "busy" && control ? 409 : 400, headers }
|
|
1844
|
+
);
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
});
|
|
1848
|
+
ctx.effect(
|
|
1849
|
+
() => () => {
|
|
1850
|
+
disposed = true;
|
|
1851
|
+
release();
|
|
1852
|
+
disposeUpgrade?.();
|
|
1853
|
+
server?.close();
|
|
1854
|
+
return register();
|
|
1855
|
+
},
|
|
1856
|
+
"dsh-live-voice: remove Attendee receiver"
|
|
1857
|
+
);
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
// src/modules/speak/engines/chatterbox/ChatterboxHttpHost.ts
|
|
1861
|
+
var ChatterboxHttpHost = class {
|
|
1862
|
+
constructor(fetchImpl = globalThis.fetch) {
|
|
1863
|
+
this.fetchImpl = fetchImpl;
|
|
1864
|
+
}
|
|
1865
|
+
fetchImpl;
|
|
1866
|
+
controllers = /* @__PURE__ */ new Set();
|
|
1867
|
+
closed = false;
|
|
1868
|
+
base = "http://127.0.0.1:7865";
|
|
1869
|
+
async json(path, init = {}) {
|
|
1870
|
+
const response = await this.fetchImpl(this.base + path, { ...init, redirect: "error" });
|
|
1871
|
+
const data = await response.json();
|
|
1872
|
+
if (!response.ok) throw new Error(data.error || "Chatterbox service request failed.");
|
|
1873
|
+
return data;
|
|
1874
|
+
}
|
|
1875
|
+
async voices(signal) {
|
|
1876
|
+
const data = await this.json("/v1/voices", {
|
|
1877
|
+
signal: AbortSignal.any([signal || new AbortController().signal, AbortSignal.timeout(5e3)])
|
|
1878
|
+
});
|
|
1879
|
+
if (!Array.isArray(data.voices) || data.voices.length > 1e3)
|
|
1880
|
+
throw new Error("Invalid Chatterbox voice catalog.");
|
|
1881
|
+
return data.voices.filter(
|
|
1882
|
+
(v) => v !== null && typeof v === "object" && typeof v.id === "string" && /^[a-f0-9]{24}$/.test(v.id) && typeof v.name === "string" && v.name.length <= 200
|
|
1883
|
+
).map((v) => ({ id: v.id, name: v.name }));
|
|
1884
|
+
}
|
|
1885
|
+
async capability(signal) {
|
|
1886
|
+
const data = await this.json("/v1/health", {
|
|
1887
|
+
signal: AbortSignal.any([signal || new AbortController().signal, AbortSignal.timeout(5e3)])
|
|
1888
|
+
});
|
|
1889
|
+
return {
|
|
1890
|
+
supported: data.phase !== "error",
|
|
1891
|
+
local: true,
|
|
1892
|
+
location: "host",
|
|
1893
|
+
pause: true,
|
|
1894
|
+
resume: true,
|
|
1895
|
+
reason: data.phase === "error" ? "Chatterbox could not load the local model." : void 0
|
|
1896
|
+
};
|
|
1897
|
+
}
|
|
1898
|
+
async synthesize(text, { voice, lang, signal } = {}) {
|
|
1899
|
+
if (typeof text !== "string" || !text.trim() || text.length > 12e3 || text.includes("\0"))
|
|
1900
|
+
throw new Error("Speech text must contain 1\u201312000 characters without NUL.");
|
|
1901
|
+
if (voice !== void 0 && voice !== "" && (typeof voice !== "string" || !/^[a-f0-9]{24}$/.test(voice)))
|
|
1902
|
+
throw new Error("Select a Chatterbox voice from the current catalog.");
|
|
1903
|
+
if (lang !== void 0 && (typeof lang !== "string" || !/^pt(?:-BR)?$/i.test(lang)))
|
|
1904
|
+
throw new Error("This Chatterbox fine-tune supports Portuguese; choose pt-BR.");
|
|
1905
|
+
if (this.closed) throw new Error("Chatterbox bridge is closed.");
|
|
1906
|
+
const controller = new AbortController();
|
|
1907
|
+
this.controllers.add(controller);
|
|
1908
|
+
const combined = AbortSignal.any([
|
|
1909
|
+
controller.signal,
|
|
1910
|
+
signal || new AbortController().signal,
|
|
1911
|
+
AbortSignal.timeout(3e5)
|
|
1912
|
+
]);
|
|
1913
|
+
let id;
|
|
1914
|
+
try {
|
|
1915
|
+
combined.throwIfAborted();
|
|
1916
|
+
const job = await this.json("/v1/jobs", {
|
|
1917
|
+
method: "POST",
|
|
1918
|
+
headers: { "content-type": "application/json", "x-chatterbox-client": "dsh" },
|
|
1919
|
+
body: JSON.stringify({ text, voice: voice || void 0 }),
|
|
1920
|
+
signal: AbortSignal.timeout(1e4)
|
|
1921
|
+
});
|
|
1922
|
+
if (!/^[a-f0-9]{32}$/.test(job.id)) throw new Error("Invalid Chatterbox job id.");
|
|
1923
|
+
id = job.id;
|
|
1924
|
+
for (; ; ) {
|
|
1925
|
+
combined.throwIfAborted();
|
|
1926
|
+
const result = await this.json("/v1/jobs/" + id, { signal: combined });
|
|
1927
|
+
if (result.phase === "done") break;
|
|
1928
|
+
if (["error", "cancelled"].includes(result.phase))
|
|
1929
|
+
throw new Error(result.message || "Chatterbox synthesis failed.");
|
|
1930
|
+
await new Promise((resolve2, reject) => {
|
|
1931
|
+
const abort = () => {
|
|
1932
|
+
clearTimeout(timer);
|
|
1933
|
+
reject(combined.reason);
|
|
1934
|
+
};
|
|
1935
|
+
const timer = setTimeout(() => {
|
|
1936
|
+
combined.removeEventListener("abort", abort);
|
|
1937
|
+
resolve2();
|
|
1938
|
+
}, 150);
|
|
1939
|
+
combined.addEventListener("abort", abort, { once: true });
|
|
1940
|
+
if (combined.aborted) abort();
|
|
1941
|
+
});
|
|
1942
|
+
}
|
|
1943
|
+
const response = await this.fetchImpl(this.base + "/v1/audio/" + id, {
|
|
1944
|
+
signal: combined,
|
|
1945
|
+
redirect: "error"
|
|
1946
|
+
});
|
|
1947
|
+
if (!response.ok || response.headers.get("content-type") !== "audio/wav")
|
|
1948
|
+
throw new Error("Invalid Chatterbox audio response.");
|
|
1949
|
+
const data = new Uint8Array(await response.arrayBuffer());
|
|
1950
|
+
if (data.byteLength > 64 * 1024 * 1024 || new TextDecoder().decode(data.subarray(0, 4)) !== "RIFF")
|
|
1951
|
+
throw new Error("Invalid Chatterbox WAV.");
|
|
1952
|
+
return data;
|
|
1953
|
+
} finally {
|
|
1954
|
+
this.controllers.delete(controller);
|
|
1955
|
+
if (id) {
|
|
1956
|
+
try {
|
|
1957
|
+
await this.fetchImpl(this.base + "/v1/jobs/" + id, {
|
|
1958
|
+
method: "DELETE",
|
|
1959
|
+
headers: { "x-chatterbox-client": "dsh" },
|
|
1960
|
+
signal: AbortSignal.timeout(3e3),
|
|
1961
|
+
redirect: "error"
|
|
1962
|
+
});
|
|
1963
|
+
} catch {
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
dispose() {
|
|
1969
|
+
this.closed = true;
|
|
1970
|
+
for (const controller of this.controllers) controller.abort();
|
|
1971
|
+
}
|
|
1972
|
+
};
|
|
1973
|
+
|
|
1974
|
+
// src/app/server/chatterboxRoutes.ts
|
|
1975
|
+
function registerChatterboxRoutes(ctx, { host = new ChatterboxHttpHost(), encode = wavToM4aAac } = {}) {
|
|
1976
|
+
const disposers = ["voices", "capabilities", "speech"].map(
|
|
1977
|
+
(endpoint) => ctx.connection.fetch.register({
|
|
1978
|
+
path: "/api/dsh-live-voice/chatterbox/" + endpoint,
|
|
1979
|
+
methods: [endpoint === "speech" ? "POST" : "GET"],
|
|
1980
|
+
requestBody: "buffered",
|
|
1981
|
+
fetch: async (request) => {
|
|
1982
|
+
const headers = { "cache-control": "no-store" };
|
|
1983
|
+
try {
|
|
1984
|
+
if (endpoint !== "speech")
|
|
1985
|
+
return Response.json(
|
|
1986
|
+
{
|
|
1987
|
+
ok: true,
|
|
1988
|
+
value: await (endpoint === "voices" ? host.voices(request.signal) : host.capability(request.signal))
|
|
1989
|
+
},
|
|
1990
|
+
{ headers }
|
|
1991
|
+
);
|
|
1992
|
+
const text = await request.text();
|
|
1993
|
+
if (new TextEncoder().encode(text).byteLength > 64e3)
|
|
1994
|
+
return Response.json(
|
|
1995
|
+
{ ok: false, error: { code: "invalid-request" } },
|
|
1996
|
+
{ status: 413, headers }
|
|
1997
|
+
);
|
|
1998
|
+
let body;
|
|
1999
|
+
try {
|
|
2000
|
+
body = JSON.parse(text);
|
|
2001
|
+
} catch {
|
|
2002
|
+
return Response.json(
|
|
2003
|
+
{ ok: false, error: { code: "invalid-request" } },
|
|
2004
|
+
{ status: 400, headers }
|
|
2005
|
+
);
|
|
2006
|
+
}
|
|
2007
|
+
const wav = await host.synthesize(body?.text, {
|
|
2008
|
+
voice: body?.voice,
|
|
2009
|
+
lang: body?.lang,
|
|
2010
|
+
signal: request.signal
|
|
2011
|
+
});
|
|
2012
|
+
const audio = await encode(wav, { signal: request.signal });
|
|
2013
|
+
return new Response(audio, { headers: { ...headers, "content-type": "audio/mp4" } });
|
|
2014
|
+
} catch (error) {
|
|
2015
|
+
return Response.json(
|
|
2016
|
+
{
|
|
2017
|
+
ok: false,
|
|
2018
|
+
error: {
|
|
2019
|
+
code: request.signal.aborted ? "cancelled" : "chatterbox-unavailable",
|
|
2020
|
+
message: request.signal.aborted ? "Speech was cancelled." : error?.message || "Chatterbox service unavailable."
|
|
2021
|
+
}
|
|
2022
|
+
},
|
|
2023
|
+
{ status: request.signal.aborted ? 499 : 502, headers }
|
|
2024
|
+
);
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
})
|
|
2028
|
+
);
|
|
2029
|
+
ctx.effect(
|
|
2030
|
+
() => () => {
|
|
2031
|
+
host.dispose();
|
|
2032
|
+
for (const dispose of disposers) dispose();
|
|
2033
|
+
},
|
|
2034
|
+
"dsh-live-voice: remove Chatterbox bridge"
|
|
2035
|
+
);
|
|
2036
|
+
}
|
|
2037
|
+
|
|
1004
2038
|
// src/app/server/vadAssets.ts
|
|
1005
2039
|
import { lstat, readFile as readFile4 } from "node:fs/promises";
|
|
1006
|
-
import { join as
|
|
2040
|
+
import { join as join7 } from "node:path";
|
|
1007
2041
|
import { fileURLToPath } from "node:url";
|
|
1008
2042
|
var VAD_ASSET_TYPES = {
|
|
1009
2043
|
"vad.worker.js": "text/javascript; charset=utf-8",
|
|
@@ -1018,7 +2052,7 @@ var VAD_ASSET_TYPES = {
|
|
|
1018
2052
|
};
|
|
1019
2053
|
var MAX_VAD_ASSET_BYTES = 32 * 1024 * 1024;
|
|
1020
2054
|
function registerVadAssetRoutes(ctx, options = {}) {
|
|
1021
|
-
const version = options.version ?? (true ? "0.
|
|
2055
|
+
const version = options.version ?? (true ? "0.7.0" : "test");
|
|
1022
2056
|
if (!/^[a-zA-Z0-9][a-zA-Z0-9.+-]{0,127}$/.test(version))
|
|
1023
2057
|
throw new Error("Invalid VAD asset version");
|
|
1024
2058
|
const directory = options.directory ?? fileURLToPath(new URL("./vad/", import.meta.url));
|
|
@@ -1043,7 +2077,7 @@ function registerVadAssetRoutes(ctx, options = {}) {
|
|
|
1043
2077
|
if (request.method !== "GET") return new Response(null, { status: 405 });
|
|
1044
2078
|
let available = false;
|
|
1045
2079
|
try {
|
|
1046
|
-
const stats = await Promise.all(required.map((name2) => lstat(
|
|
2080
|
+
const stats = await Promise.all(required.map((name2) => lstat(join7(directory, name2))));
|
|
1047
2081
|
available = stats.every(
|
|
1048
2082
|
(stat) => stat.isFile() && stat.size > 0 && stat.size <= MAX_VAD_ASSET_BYTES
|
|
1049
2083
|
);
|
|
@@ -1070,7 +2104,7 @@ function registerVadAssetRoutes(ctx, options = {}) {
|
|
|
1070
2104
|
headers: { allow: "GET" }
|
|
1071
2105
|
});
|
|
1072
2106
|
try {
|
|
1073
|
-
const target =
|
|
2107
|
+
const target = join7(directory, filename);
|
|
1074
2108
|
const stat = await lstat(target);
|
|
1075
2109
|
if (!stat.isFile() || stat.size > MAX_VAD_ASSET_BYTES)
|
|
1076
2110
|
throw new Error("Invalid asset");
|
|
@@ -1110,9 +2144,9 @@ function registerVadAssetRoutes(ctx, options = {}) {
|
|
|
1110
2144
|
// src/app/server/browserAsrAssets.ts
|
|
1111
2145
|
import { lstat as lstat2, readFile as readFile5 } from "node:fs/promises";
|
|
1112
2146
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
1113
|
-
import { join as
|
|
2147
|
+
import { join as join8 } from "node:path";
|
|
1114
2148
|
function registerBrowserAsrAssets(ctx, options = {}) {
|
|
1115
|
-
const version = options.version ?? (true ? "0.
|
|
2149
|
+
const version = options.version ?? (true ? "0.7.0" : "test");
|
|
1116
2150
|
if (!/^[a-zA-Z0-9][a-zA-Z0-9.+-]{0,127}$/.test(version)) throw new Error("invalid-version");
|
|
1117
2151
|
const base = "/api/dsh-live-voice/asr/" + encodeURIComponent(version) + "/";
|
|
1118
2152
|
const directory = options.directory ?? fileURLToPath2(new URL("./asr/", import.meta.url));
|
|
@@ -1137,7 +2171,7 @@ function registerBrowserAsrAssets(ctx, options = {}) {
|
|
|
1137
2171
|
if (new URL(request.url).pathname !== base + name2) return new Response("", { status: 404 });
|
|
1138
2172
|
if (request.method !== "GET")
|
|
1139
2173
|
return new Response("", { status: 405, headers: { allow: "GET" } });
|
|
1140
|
-
const path =
|
|
2174
|
+
const path = join8(directory, name2);
|
|
1141
2175
|
try {
|
|
1142
2176
|
const meta = await lstat2(path);
|
|
1143
2177
|
if (!meta.isFile() || meta.isSymbolicLink() || meta.size > 100 * 1024 * 1024)
|
|
@@ -1272,6 +2306,9 @@ function apply(ctx, {
|
|
|
1272
2306
|
encodeHostSpeech = wavToM4aAac
|
|
1273
2307
|
} = {}) {
|
|
1274
2308
|
registerSettingsRoute(ctx, settingsStore);
|
|
2309
|
+
registerChatterboxRoutes(ctx, { encode: encodeHostSpeech });
|
|
2310
|
+
if (typeof ctx.inject === "function")
|
|
2311
|
+
ctx.inject(["webServer"], (webCtx) => registerAttendeeRoutes(webCtx));
|
|
1275
2312
|
registerVadAssetRoutes(ctx);
|
|
1276
2313
|
registerBrowserAsrAssets(ctx);
|
|
1277
2314
|
ctx.systemPrompt.variable(
|