@mulingai-npm/redis 3.1.1 → 3.2.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/README.md +7 -7
- package/dist/enums/redis-database.d.ts +5 -5
- package/dist/enums/redis-database.js +9 -9
- package/dist/loggers/mulingstream-chunk-logger.d.ts +19 -19
- package/dist/loggers/mulingstream-chunk-logger.js +81 -81
- package/dist/managers/mulingstream-chunk-manager.d.ts +115 -109
- package/dist/managers/mulingstream-chunk-manager.js +366 -382
- package/dist/managers/mulingstream-listener-manager.d.ts +29 -29
- package/dist/managers/mulingstream-listener-manager.js +169 -169
- package/dist/managers/mulingstream-speaker-manager.d.ts +37 -37
- package/dist/managers/mulingstream-speaker-manager.js +225 -225
- package/dist/redis-client.d.ts +44 -44
- package/dist/redis-client.js +143 -143
- package/dist/utils/finders.d.ts +1 -1
- package/dist/utils/finders.js +18 -18
- package/package.json +34 -34
- package/dist/queue-managers/mulingstream-chunk-queue.d.ts +0 -83
- package/dist/queue-managers/mulingstream-chunk-queue.js +0 -12
|
@@ -1,382 +1,366 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MulingstreamChunkManager = void 0;
|
|
4
|
-
const uuid_1 = require("uuid");
|
|
5
|
-
/* -------------------------------------------------------------------------- */
|
|
6
|
-
/* Constants */
|
|
7
|
-
/* -------------------------------------------------------------------------- */
|
|
8
|
-
const EXPIRATION = 12 * 60 * 60; // 12 h
|
|
9
|
-
const ROOM_ARRAY_LENGTH = 50; // keep last 50 chunks
|
|
10
|
-
/* -------------------------------------------------------------------------- */
|
|
11
|
-
/* Main class */
|
|
12
|
-
/* -------------------------------------------------------------------------- */
|
|
13
|
-
class MulingstreamChunkManager {
|
|
14
|
-
constructor(redisClient) {
|
|
15
|
-
this.redisClient = redisClient;
|
|
16
|
-
}
|
|
17
|
-
/* ----------------------------- key helpers ----------------------------- */
|
|
18
|
-
roomZsetKey(roomId) {
|
|
19
|
-
return `room:${roomId}:chunks`;
|
|
20
|
-
}
|
|
21
|
-
chunkHashKey(chunkId) {
|
|
22
|
-
return `chunk:${chunkId}`;
|
|
23
|
-
}
|
|
24
|
-
generateChunkId(roomId, chunkNumber) {
|
|
25
|
-
return `[${roomId}]-[${chunkNumber}]-[${(0, uuid_1.v4)()}]`;
|
|
26
|
-
}
|
|
27
|
-
/* --------------------------- (de)serialization -------------------------- */
|
|
28
|
-
serialize(value) {
|
|
29
|
-
return JSON.stringify(value);
|
|
30
|
-
}
|
|
31
|
-
deserialize(v) {
|
|
32
|
-
return v ? JSON.parse(v) : undefined;
|
|
33
|
-
}
|
|
34
|
-
hashToChunk(h) {
|
|
35
|
-
return {
|
|
36
|
-
chunkId: h.chunkId,
|
|
37
|
-
roomId: h.roomId,
|
|
38
|
-
chunkNumber: parseInt(h.chunkNumber, 10),
|
|
39
|
-
language: h.language,
|
|
40
|
-
sttProviders: this.deserialize(h.sttProviders),
|
|
41
|
-
targetLanguages: this.deserialize(h.targetLanguages),
|
|
42
|
-
shortCodeTargetLanguages: this.deserialize(h.shortCodeTargetLanguages),
|
|
43
|
-
finalTranscription: h.finalTranscription,
|
|
44
|
-
sttStatus: h.sttStatus,
|
|
45
|
-
createdAt: parseInt(h.createdAt, 10),
|
|
46
|
-
audioChunk: this.deserialize(h.audioChunk),
|
|
47
|
-
stt: this.deserialize(h.stt),
|
|
48
|
-
translation: this.deserialize(h.translation),
|
|
49
|
-
tts: this.deserialize(h.tts)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
/* ------------------------------ timeouts ------------------------------- */
|
|
53
|
-
getTimeout(start, end) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
async
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
p.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
/*
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
c.stt[
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
c.
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
c.tts[
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
if (
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
async
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
break;
|
|
368
|
-
ready.push(window[i]);
|
|
369
|
-
continue;
|
|
370
|
-
}
|
|
371
|
-
break; // got USED/DISCARDED unexpectedly
|
|
372
|
-
}
|
|
373
|
-
/* persist DISCARDED changes (if any) */
|
|
374
|
-
if (!ready.length || blocked) {
|
|
375
|
-
const p = this.redisClient.pipeline();
|
|
376
|
-
window.forEach((c) => p.hset(this.chunkHashKey(c.chunkId), { tts: this.serialize(c.tts) }));
|
|
377
|
-
await p.exec();
|
|
378
|
-
}
|
|
379
|
-
return ready;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
exports.MulingstreamChunkManager = MulingstreamChunkManager;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MulingstreamChunkManager = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
/* -------------------------------------------------------------------------- */
|
|
6
|
+
/* Constants */
|
|
7
|
+
/* -------------------------------------------------------------------------- */
|
|
8
|
+
const EXPIRATION = 12 * 60 * 60; // 12 h
|
|
9
|
+
const ROOM_ARRAY_LENGTH = 50; // keep last 50 chunks
|
|
10
|
+
/* -------------------------------------------------------------------------- */
|
|
11
|
+
/* Main class */
|
|
12
|
+
/* -------------------------------------------------------------------------- */
|
|
13
|
+
class MulingstreamChunkManager {
|
|
14
|
+
constructor(redisClient) {
|
|
15
|
+
this.redisClient = redisClient;
|
|
16
|
+
}
|
|
17
|
+
/* ----------------------------- key helpers ----------------------------- */
|
|
18
|
+
roomZsetKey(roomId) {
|
|
19
|
+
return `room:${roomId}:chunks`;
|
|
20
|
+
}
|
|
21
|
+
chunkHashKey(chunkId) {
|
|
22
|
+
return `chunk:${chunkId}`;
|
|
23
|
+
}
|
|
24
|
+
generateChunkId(roomId, chunkNumber) {
|
|
25
|
+
return `[${roomId}]-[${chunkNumber}]-[${(0, uuid_1.v4)()}]`;
|
|
26
|
+
}
|
|
27
|
+
/* --------------------------- (de)serialization -------------------------- */
|
|
28
|
+
serialize(value) {
|
|
29
|
+
return JSON.stringify(value);
|
|
30
|
+
}
|
|
31
|
+
deserialize(v) {
|
|
32
|
+
return v ? JSON.parse(v) : undefined;
|
|
33
|
+
}
|
|
34
|
+
hashToChunk(h) {
|
|
35
|
+
return {
|
|
36
|
+
chunkId: h.chunkId,
|
|
37
|
+
roomId: h.roomId,
|
|
38
|
+
chunkNumber: parseInt(h.chunkNumber, 10),
|
|
39
|
+
language: h.language,
|
|
40
|
+
sttProviders: this.deserialize(h.sttProviders),
|
|
41
|
+
targetLanguages: this.deserialize(h.targetLanguages),
|
|
42
|
+
shortCodeTargetLanguages: this.deserialize(h.shortCodeTargetLanguages),
|
|
43
|
+
finalTranscription: h.finalTranscription,
|
|
44
|
+
sttStatus: h.sttStatus,
|
|
45
|
+
createdAt: parseInt(h.createdAt, 10),
|
|
46
|
+
audioChunk: this.deserialize(h.audioChunk),
|
|
47
|
+
stt: this.deserialize(h.stt),
|
|
48
|
+
translation: this.deserialize(h.translation),
|
|
49
|
+
tts: this.deserialize(h.tts)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/* ------------------------------ timeouts ------------------------------- */
|
|
53
|
+
getTimeout(start, end) {
|
|
54
|
+
return 30000;
|
|
55
|
+
}
|
|
56
|
+
/* ---------------------------------------------------------------------- */
|
|
57
|
+
/* Public API (same) */
|
|
58
|
+
/* ---------------------------------------------------------------------- */
|
|
59
|
+
/* Room helpers ---------------------------------------------------------- */
|
|
60
|
+
async initRoom(roomId) {
|
|
61
|
+
const exists = await this.redisClient.exists(this.roomZsetKey(roomId));
|
|
62
|
+
if (exists)
|
|
63
|
+
return false;
|
|
64
|
+
await this.redisClient.expire(this.roomZsetKey(roomId), EXPIRATION);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
async getRooms() {
|
|
68
|
+
const keys = await this.redisClient.keys('room:*:chunks');
|
|
69
|
+
return keys.map((k) => k.replace(/^room:(.*):chunks$/, '$1'));
|
|
70
|
+
}
|
|
71
|
+
async getMulingstreamChunksByRoom(roomId) {
|
|
72
|
+
const ids = await this.redisClient.zrange(this.roomZsetKey(roomId), 0, -1);
|
|
73
|
+
if (!ids.length)
|
|
74
|
+
return null;
|
|
75
|
+
const pipe = this.redisClient.pipeline();
|
|
76
|
+
ids.forEach((cid) => pipe.hgetall(this.chunkHashKey(cid)));
|
|
77
|
+
const res = await pipe.exec();
|
|
78
|
+
if (!res)
|
|
79
|
+
return null;
|
|
80
|
+
return res === null || res === void 0 ? void 0 : res.map(([, d]) => this.hashToChunk(d));
|
|
81
|
+
}
|
|
82
|
+
getRoomById(roomId) {
|
|
83
|
+
return this.getMulingstreamChunksByRoom(roomId);
|
|
84
|
+
}
|
|
85
|
+
/* ------------------------- chunk insertion ---------------------------- */
|
|
86
|
+
async addMulingstreamChunk(params) {
|
|
87
|
+
var _a, _b;
|
|
88
|
+
const { roomId, chunkNumber, language, start, end, duration, isFirst, isLast, theme, sttProviders, targetLanguages, shortCodeTargetLanguages } = params;
|
|
89
|
+
if (chunkNumber === 1) {
|
|
90
|
+
const old = await this.redisClient.zrange(this.roomZsetKey(roomId), 0, -1);
|
|
91
|
+
if (old.length) {
|
|
92
|
+
const p = this.redisClient.pipeline();
|
|
93
|
+
old.forEach((cid) => p.unlink(this.chunkHashKey(cid)));
|
|
94
|
+
p.del(this.roomZsetKey(roomId));
|
|
95
|
+
await p.exec();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const dup = await this.redisClient.zrangebyscore(this.roomZsetKey(roomId), chunkNumber, chunkNumber);
|
|
100
|
+
if (dup.length) {
|
|
101
|
+
const p = this.redisClient.pipeline();
|
|
102
|
+
dup.forEach((cid) => p.unlink(this.chunkHashKey(cid)));
|
|
103
|
+
p.zrem(this.roomZsetKey(roomId), ...dup);
|
|
104
|
+
await p.exec();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const chunkId = this.generateChunkId(roomId, chunkNumber);
|
|
108
|
+
const audioChunk = { start, end, duration, isFirst, isLast, theme, processingStart: Date.now() };
|
|
109
|
+
const stt = {};
|
|
110
|
+
sttProviders.forEach((p) => (stt[p.service] = { transcription: '', model: p.model, status: 'INIT' }));
|
|
111
|
+
const translation = {};
|
|
112
|
+
const tts = {};
|
|
113
|
+
shortCodeTargetLanguages.forEach((l) => {
|
|
114
|
+
translation[l] = { translation: '', status: 'INIT' };
|
|
115
|
+
tts[l] = { ttsAudioPath: '', status: 'INIT', isEmitted: false, totalCheck: 0 };
|
|
116
|
+
});
|
|
117
|
+
const hash = {
|
|
118
|
+
chunkId,
|
|
119
|
+
roomId,
|
|
120
|
+
chunkNumber: String(chunkNumber),
|
|
121
|
+
language,
|
|
122
|
+
sttProviders: this.serialize(sttProviders),
|
|
123
|
+
targetLanguages: this.serialize(targetLanguages),
|
|
124
|
+
shortCodeTargetLanguages: this.serialize(shortCodeTargetLanguages),
|
|
125
|
+
finalTranscription: '',
|
|
126
|
+
sttStatus: 'INIT',
|
|
127
|
+
createdAt: String(Date.now()),
|
|
128
|
+
audioChunk: this.serialize(audioChunk),
|
|
129
|
+
stt: this.serialize(stt),
|
|
130
|
+
translation: this.serialize(translation),
|
|
131
|
+
tts: this.serialize(tts)
|
|
132
|
+
};
|
|
133
|
+
const pipe = this.redisClient.pipeline();
|
|
134
|
+
pipe.hset(this.chunkHashKey(chunkId), hash);
|
|
135
|
+
pipe.expire(this.chunkHashKey(chunkId), EXPIRATION);
|
|
136
|
+
pipe.zadd(this.roomZsetKey(roomId), chunkNumber, chunkId);
|
|
137
|
+
pipe.expire(this.roomZsetKey(roomId), EXPIRATION);
|
|
138
|
+
pipe.zrange(this.roomZsetKey(roomId), 0, -ROOM_ARRAY_LENGTH - 1, 'WITHSCORES');
|
|
139
|
+
const execResults = await pipe.exec();
|
|
140
|
+
const oldPairs = ((_b = (_a = execResults === null || execResults === void 0 ? void 0 : execResults[4]) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : []);
|
|
141
|
+
if (Array.isArray(oldPairs) && oldPairs.length) {
|
|
142
|
+
const oldIds = [];
|
|
143
|
+
for (let i = 0; i < oldPairs.length; i += 2)
|
|
144
|
+
oldIds.push(oldPairs[i]);
|
|
145
|
+
const trim = this.redisClient.pipeline();
|
|
146
|
+
trim.zrem(this.roomZsetKey(roomId), ...oldIds);
|
|
147
|
+
oldIds.forEach((cid) => trim.unlink(this.chunkHashKey(cid)));
|
|
148
|
+
await trim.exec();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/* --------------------------- helper lookup ---------------------------- */
|
|
152
|
+
async getChunkId(roomId, n) {
|
|
153
|
+
const ids = await this.redisClient.zrangebyscore(this.roomZsetKey(roomId), n, n);
|
|
154
|
+
return ids.length ? ids[0] : null;
|
|
155
|
+
}
|
|
156
|
+
async getMulingstreamChunkById(roomId, n) {
|
|
157
|
+
const cid = await this.getChunkId(roomId, n);
|
|
158
|
+
if (!cid)
|
|
159
|
+
return null;
|
|
160
|
+
const raw = await this.redisClient.hgetall(this.chunkHashKey(cid));
|
|
161
|
+
return raw.chunkId ? this.hashToChunk(raw) : null;
|
|
162
|
+
}
|
|
163
|
+
/* ------------------------------ Updaters ------------------------------ */
|
|
164
|
+
/* helper to fetch-mutate-save one chunk */
|
|
165
|
+
async withChunk(roomId, n, fn) {
|
|
166
|
+
const cid = await this.getChunkId(roomId, n);
|
|
167
|
+
if (!cid)
|
|
168
|
+
return null;
|
|
169
|
+
const key = this.chunkHashKey(cid);
|
|
170
|
+
const raw = await this.redisClient.hgetall(key);
|
|
171
|
+
if (!raw.chunkId)
|
|
172
|
+
return null;
|
|
173
|
+
const chunk = this.hashToChunk(raw);
|
|
174
|
+
const out = await fn(chunk);
|
|
175
|
+
const p = this.redisClient.pipeline();
|
|
176
|
+
p.hset(key, {
|
|
177
|
+
finalTranscription: chunk.finalTranscription,
|
|
178
|
+
sttStatus: chunk.sttStatus,
|
|
179
|
+
stt: this.serialize(chunk.stt),
|
|
180
|
+
translation: this.serialize(chunk.translation),
|
|
181
|
+
tts: this.serialize(chunk.tts)
|
|
182
|
+
});
|
|
183
|
+
p.expire(key, EXPIRATION);
|
|
184
|
+
await p.exec();
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
async updateStt(roomId, n, service, opt) {
|
|
188
|
+
return ((await this.withChunk(roomId, n, (c) => {
|
|
189
|
+
if (!c.stt[service])
|
|
190
|
+
return false;
|
|
191
|
+
if (opt.transcription !== undefined)
|
|
192
|
+
c.stt[service].transcription = opt.transcription;
|
|
193
|
+
if (opt.sttStatus !== undefined)
|
|
194
|
+
c.stt[service].status = opt.sttStatus;
|
|
195
|
+
return true;
|
|
196
|
+
})) === true);
|
|
197
|
+
}
|
|
198
|
+
async updateSttObject(roomId, n, newStt) {
|
|
199
|
+
return ((await this.withChunk(roomId, n, (c) => {
|
|
200
|
+
c.stt = newStt;
|
|
201
|
+
return true;
|
|
202
|
+
})) === true);
|
|
203
|
+
}
|
|
204
|
+
async discardStt(roomId, n) {
|
|
205
|
+
return ((await this.withChunk(roomId, n, (c) => {
|
|
206
|
+
Object.keys(c.stt).forEach((p) => {
|
|
207
|
+
c.stt[p].transcription = '';
|
|
208
|
+
c.stt[p].status = 'DISCARDED';
|
|
209
|
+
});
|
|
210
|
+
c.finalTranscription = '';
|
|
211
|
+
c.sttStatus = 'DISCARDED';
|
|
212
|
+
return true;
|
|
213
|
+
})) === true);
|
|
214
|
+
}
|
|
215
|
+
async updateFinalTranscription(roomId, n, opt) {
|
|
216
|
+
return this.withChunk(roomId, n, (c) => {
|
|
217
|
+
if (opt.transcription !== undefined)
|
|
218
|
+
c.finalTranscription = opt.transcription;
|
|
219
|
+
if (opt.sttStatus !== undefined)
|
|
220
|
+
c.sttStatus = opt.sttStatus;
|
|
221
|
+
return c;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
async discardPostStt(roomId, n) {
|
|
225
|
+
return ((await this.withChunk(roomId, n, (c) => {
|
|
226
|
+
Object.values(c.translation).forEach((t) => (t.status = 'DISCARDED'));
|
|
227
|
+
Object.values(c.tts).forEach((t) => (t.status = 'DISCARDED'));
|
|
228
|
+
return true;
|
|
229
|
+
})) === true);
|
|
230
|
+
}
|
|
231
|
+
async discardLanguage(roomId, n, lang) {
|
|
232
|
+
return ((await this.withChunk(roomId, n, (c) => {
|
|
233
|
+
if (c.translation[lang])
|
|
234
|
+
c.translation[lang].status = 'DISCARDED';
|
|
235
|
+
if (c.tts[lang])
|
|
236
|
+
c.tts[lang].status = 'DISCARDED';
|
|
237
|
+
return true;
|
|
238
|
+
})) === true);
|
|
239
|
+
}
|
|
240
|
+
async discardLanguages(roomId, n, opt) {
|
|
241
|
+
return this.withChunk(roomId, n, (c) => {
|
|
242
|
+
var _a, _b;
|
|
243
|
+
(_a = opt.translation) === null || _a === void 0 ? void 0 : _a.forEach((l) => {
|
|
244
|
+
const e = c.translation[l];
|
|
245
|
+
if (e && e.status === 'INIT')
|
|
246
|
+
e.status = 'DISCARDED';
|
|
247
|
+
});
|
|
248
|
+
(_b = opt.tts) === null || _b === void 0 ? void 0 : _b.forEach((l) => {
|
|
249
|
+
const e = c.tts[l];
|
|
250
|
+
if (e && e.status === 'INIT')
|
|
251
|
+
e.status = 'DISCARDED';
|
|
252
|
+
});
|
|
253
|
+
return c;
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async updateTranslation(roomId, n, lang, opt) {
|
|
257
|
+
return this.withChunk(roomId, n, (c) => {
|
|
258
|
+
const e = c.translation[lang];
|
|
259
|
+
if (!e)
|
|
260
|
+
return null;
|
|
261
|
+
if (opt.translation !== undefined)
|
|
262
|
+
e.translation = opt.translation;
|
|
263
|
+
if (opt.status !== undefined)
|
|
264
|
+
e.status = opt.status;
|
|
265
|
+
return c;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
async updateTranslationInBulk(roomId, n, dict, status = 'READY') {
|
|
269
|
+
return this.withChunk(roomId, n, (c) => {
|
|
270
|
+
Object.entries(dict).forEach(([l, txt]) => {
|
|
271
|
+
if (!c.translation[l])
|
|
272
|
+
return;
|
|
273
|
+
c.translation[l].translation = txt;
|
|
274
|
+
c.translation[l].status = status;
|
|
275
|
+
});
|
|
276
|
+
return c;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
async updateTts(roomId, n, lang, opt) {
|
|
280
|
+
return this.withChunk(roomId, n, (c) => {
|
|
281
|
+
const e = c.tts[lang];
|
|
282
|
+
if (!e)
|
|
283
|
+
return null;
|
|
284
|
+
if (opt.ttsAudioPath !== undefined)
|
|
285
|
+
e.ttsAudioPath = opt.ttsAudioPath;
|
|
286
|
+
if (opt.status !== undefined)
|
|
287
|
+
e.status = opt.status;
|
|
288
|
+
if (opt.isEmitted !== undefined)
|
|
289
|
+
e.isEmitted = opt.isEmitted;
|
|
290
|
+
if (opt.totalCheck !== undefined)
|
|
291
|
+
e.totalCheck = opt.totalCheck;
|
|
292
|
+
return c;
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
async increaseTotalCheck(roomId, n, lang) {
|
|
296
|
+
return this.withChunk(roomId, n, (c) => {
|
|
297
|
+
if (!c.tts[lang])
|
|
298
|
+
return null;
|
|
299
|
+
c.tts[lang].totalCheck += 1;
|
|
300
|
+
return c;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
async areTranslationsProcessed(roomId, n) {
|
|
304
|
+
const c = await this.getMulingstreamChunkById(roomId, n);
|
|
305
|
+
return !!c && Object.values(c.translation).every((t) => t.status !== 'INIT');
|
|
306
|
+
}
|
|
307
|
+
/* -------------------------- READY-TTS sequence ------------------------- */
|
|
308
|
+
async getAllReadyTts(roomId, lang) {
|
|
309
|
+
var _a;
|
|
310
|
+
const chunks = (_a = (await this.getMulingstreamChunksByRoom(roomId))) !== null && _a !== void 0 ? _a : [];
|
|
311
|
+
if (!chunks.length)
|
|
312
|
+
return [];
|
|
313
|
+
chunks.sort((a, b) => a.chunkNumber - b.chunkNumber);
|
|
314
|
+
let lastIdx = -1;
|
|
315
|
+
for (let i = chunks.length - 1; i >= 0; i--)
|
|
316
|
+
if (chunks[i].tts[lang]) {
|
|
317
|
+
lastIdx = i;
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
if (lastIdx === -1)
|
|
321
|
+
return [];
|
|
322
|
+
let firstIdx = lastIdx;
|
|
323
|
+
for (let i = lastIdx - 1; i >= 0; i--) {
|
|
324
|
+
if (!chunks[i].tts[lang])
|
|
325
|
+
break;
|
|
326
|
+
firstIdx = i;
|
|
327
|
+
}
|
|
328
|
+
const window = chunks.slice(firstIdx, lastIdx + 1);
|
|
329
|
+
let lastDone = -1;
|
|
330
|
+
for (let i = window.length - 1; i >= 0; i--) {
|
|
331
|
+
const s = window[i].tts[lang].status;
|
|
332
|
+
if (s === 'USED' || s === 'DISCARDED') {
|
|
333
|
+
lastDone = i;
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const ready = [];
|
|
338
|
+
let blocked = false;
|
|
339
|
+
const now = Date.now();
|
|
340
|
+
for (let i = lastDone + 1; i < window.length; i++) {
|
|
341
|
+
const entry = window[i].tts[lang];
|
|
342
|
+
const audio = window[i].audioChunk;
|
|
343
|
+
if (entry.status === 'INIT') {
|
|
344
|
+
if (now - window[i].createdAt > this.getTimeout(audio.start, audio.end))
|
|
345
|
+
entry.status = 'DISCARDED';
|
|
346
|
+
else
|
|
347
|
+
blocked = true;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (entry.status === 'READY') {
|
|
351
|
+
if (blocked)
|
|
352
|
+
break;
|
|
353
|
+
ready.push(window[i]);
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
if (!ready.length || blocked) {
|
|
359
|
+
const p = this.redisClient.pipeline();
|
|
360
|
+
window.forEach((c) => p.hset(this.chunkHashKey(c.chunkId), { tts: this.serialize(c.tts) }));
|
|
361
|
+
await p.exec();
|
|
362
|
+
}
|
|
363
|
+
return ready;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
exports.MulingstreamChunkManager = MulingstreamChunkManager;
|