@mulingai-npm/redis 1.5.0 → 1.6.1
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.
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { RedisClient } from '../redis-client';
|
|
2
|
+
export type SttStatus = 'INIT' | 'DISCARDED' | 'READY' | 'USED';
|
|
3
|
+
export type TranslationStatus = 'INIT' | 'DISCARDED' | 'READY' | 'USED';
|
|
4
|
+
export type TtsStatus = 'INIT' | 'DISCARDED' | 'READY' | 'USED';
|
|
5
|
+
export type MulingstreamChunkStatus = 'INIT' | 'DISCARDED' | 'USED';
|
|
6
|
+
export type MulingstreamChunkData = {
|
|
7
|
+
mulingstreamChunkId: string;
|
|
8
|
+
roomId: string;
|
|
9
|
+
chunkNumber: number;
|
|
10
|
+
language: string;
|
|
11
|
+
mulingstreamChunkStatus: MulingstreamChunkStatus;
|
|
12
|
+
audioChunk: {
|
|
13
|
+
start: number;
|
|
14
|
+
end: number;
|
|
15
|
+
duration: number;
|
|
16
|
+
isFirst: boolean;
|
|
17
|
+
isLast: boolean;
|
|
18
|
+
theme: string;
|
|
19
|
+
processingStart: number;
|
|
20
|
+
};
|
|
21
|
+
stt: {
|
|
22
|
+
services: string[];
|
|
23
|
+
azureTranscription?: string;
|
|
24
|
+
whisperTranscription?: string;
|
|
25
|
+
googleTranscription?: string;
|
|
26
|
+
finalTranscription: string;
|
|
27
|
+
status: SttStatus;
|
|
28
|
+
};
|
|
29
|
+
postStt: {
|
|
30
|
+
[language: string]: {
|
|
31
|
+
translation: string;
|
|
32
|
+
translationStatus: TranslationStatus;
|
|
33
|
+
ttsAudioPath: string;
|
|
34
|
+
ttsStatus: TtsStatus;
|
|
35
|
+
isEmitted: boolean;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export declare class MulingstreamChunkManager {
|
|
40
|
+
private redisClient;
|
|
41
|
+
constructor(redisClient: RedisClient);
|
|
42
|
+
private parseHashData;
|
|
43
|
+
addMulingstreamChunk(params: {
|
|
44
|
+
roomId: string;
|
|
45
|
+
chunkNumber: number;
|
|
46
|
+
language: string;
|
|
47
|
+
start: number;
|
|
48
|
+
end: number;
|
|
49
|
+
duration: number;
|
|
50
|
+
isFirst: boolean;
|
|
51
|
+
isLast: boolean;
|
|
52
|
+
theme: string;
|
|
53
|
+
services: string[];
|
|
54
|
+
postSttLanguages: string[];
|
|
55
|
+
}): Promise<string>;
|
|
56
|
+
getMulingstreamChunks(): Promise<MulingstreamChunkData[]>;
|
|
57
|
+
getMulingstreamChunk(mulingstreamChunkId: string): Promise<MulingstreamChunkData | null>;
|
|
58
|
+
getMulingstreamChunksByRoom(roomId: string): Promise<MulingstreamChunkData[]>;
|
|
59
|
+
getByChunkNumber(chunkNumber: number, roomId: string): Promise<MulingstreamChunkData | null>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MulingstreamChunkManager = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const EXPIRATION = 4 * 60 * 60; // 4 hours in seconds
|
|
6
|
+
class MulingstreamChunkManager {
|
|
7
|
+
constructor(redisClient) {
|
|
8
|
+
this.redisClient = redisClient;
|
|
9
|
+
}
|
|
10
|
+
parseHashData(data) {
|
|
11
|
+
// Parse nested JSON fields
|
|
12
|
+
const audioChunk = JSON.parse(data.audioChunk);
|
|
13
|
+
const stt = JSON.parse(data.stt);
|
|
14
|
+
const postStt = JSON.parse(data.postStt);
|
|
15
|
+
return {
|
|
16
|
+
mulingstreamChunkId: data.mulingstreamChunkId,
|
|
17
|
+
roomId: data.roomId,
|
|
18
|
+
chunkNumber: parseInt(data.chunkNumber, 10),
|
|
19
|
+
language: data.language,
|
|
20
|
+
mulingstreamChunkStatus: data.mulingstreamChunkStatus,
|
|
21
|
+
audioChunk,
|
|
22
|
+
stt,
|
|
23
|
+
postStt
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
// Adds a new mulingstream chunk.
|
|
27
|
+
// - Creates a unique mulingstreamChunkId.
|
|
28
|
+
// - Stores the chunk data in a Redis hash under `mulingstreamChunk:{mulingstreamChunkId}`.
|
|
29
|
+
// - Applies an expiration (4h).
|
|
30
|
+
async addMulingstreamChunk(params) {
|
|
31
|
+
const { roomId, chunkNumber, language, start, end, duration, isFirst, isLast, theme, services, postSttLanguages } = params;
|
|
32
|
+
const mulingstreamChunkId = `[${roomId}]-[${chunkNumber}]-[${(0, uuid_1.v4)()}]`;
|
|
33
|
+
// Prepare the nested objects
|
|
34
|
+
const audioChunk = {
|
|
35
|
+
start,
|
|
36
|
+
end,
|
|
37
|
+
duration,
|
|
38
|
+
isFirst,
|
|
39
|
+
isLast,
|
|
40
|
+
theme,
|
|
41
|
+
processingStart: Date.now()
|
|
42
|
+
};
|
|
43
|
+
const stt = {
|
|
44
|
+
services,
|
|
45
|
+
azureTranscription: '',
|
|
46
|
+
whisperTranscription: '',
|
|
47
|
+
googleTranscription: '',
|
|
48
|
+
finalTranscription: '',
|
|
49
|
+
status: 'INIT'
|
|
50
|
+
};
|
|
51
|
+
// Build postStt object keyed by each language
|
|
52
|
+
const postStt = {};
|
|
53
|
+
for (const lang of postSttLanguages) {
|
|
54
|
+
postStt[lang] = {
|
|
55
|
+
translation: '',
|
|
56
|
+
translationStatus: 'INIT',
|
|
57
|
+
ttsAudioPath: '',
|
|
58
|
+
ttsStatus: 'INIT',
|
|
59
|
+
isEmitted: false
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// Store the data in a Redis hash
|
|
63
|
+
await this.redisClient.hset(`mulingstreamChunk:${mulingstreamChunkId}`, {
|
|
64
|
+
mulingstreamChunkId,
|
|
65
|
+
roomId,
|
|
66
|
+
chunkNumber: chunkNumber.toString(),
|
|
67
|
+
language,
|
|
68
|
+
mulingstreamChunkStatus: 'INIT',
|
|
69
|
+
audioChunk: JSON.stringify(audioChunk),
|
|
70
|
+
stt: JSON.stringify(stt),
|
|
71
|
+
postStt: JSON.stringify(postStt)
|
|
72
|
+
});
|
|
73
|
+
// set expiration
|
|
74
|
+
await this.redisClient.expire(`mulingstreamChunk:${mulingstreamChunkId}`, EXPIRATION);
|
|
75
|
+
return mulingstreamChunkId;
|
|
76
|
+
}
|
|
77
|
+
// Retrieves all mulingstream chunks by scanning keys `mulingstreamChunk:*`.
|
|
78
|
+
async getMulingstreamChunks() {
|
|
79
|
+
const keys = await this.redisClient.keys('mulingstreamChunk:*');
|
|
80
|
+
if (!keys || keys.length === 0) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const results = [];
|
|
84
|
+
for (const key of keys) {
|
|
85
|
+
const data = await this.redisClient.hgetall(key);
|
|
86
|
+
if (data && data.mulingstreamChunkId) {
|
|
87
|
+
results.push(this.parseHashData(data));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return results;
|
|
91
|
+
}
|
|
92
|
+
// Retrieves a single mulingstream chunk by ID.
|
|
93
|
+
async getMulingstreamChunk(mulingstreamChunkId) {
|
|
94
|
+
const data = await this.redisClient.hgetall(`mulingstreamChunk:${mulingstreamChunkId}`);
|
|
95
|
+
if (!data || !data.mulingstreamChunkId) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return this.parseHashData(data);
|
|
99
|
+
}
|
|
100
|
+
// Retrieves all mulingstream chunks for a specific room by pattern matching keys `mulingstreamChunk:[roomId]-*`.
|
|
101
|
+
async getMulingstreamChunksByRoom(roomId) {
|
|
102
|
+
const pattern = `mulingstreamChunk:[${roomId}]-*`;
|
|
103
|
+
const keys = await this.redisClient.keys(pattern);
|
|
104
|
+
if (!keys || keys.length === 0) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
const results = [];
|
|
108
|
+
for (const key of keys) {
|
|
109
|
+
const data = await this.redisClient.hgetall(key);
|
|
110
|
+
if (!data || !data.mulingstreamChunkId) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
results.push(this.parseHashData(data));
|
|
114
|
+
}
|
|
115
|
+
return results;
|
|
116
|
+
}
|
|
117
|
+
// Retrieves a mulingstream chunk by (roomId, chunkNumber).
|
|
118
|
+
// Since mulingstreamChunkId = `[${roomId}]-[${chunkNumber}]-[uuid]`, we can pattern-match on keys.
|
|
119
|
+
async getByChunkNumber(chunkNumber, roomId) {
|
|
120
|
+
const pattern = `mulingstreamChunk:[${roomId}]-[${chunkNumber}]-*`;
|
|
121
|
+
const keys = await this.redisClient.keys(pattern);
|
|
122
|
+
if (!keys || keys.length === 0) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
// If multiple match, return the first
|
|
126
|
+
const data = await this.redisClient.hgetall(keys[0]);
|
|
127
|
+
if (!data || !data.mulingstreamChunkId) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
return this.parseHashData(data);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.MulingstreamChunkManager = MulingstreamChunkManager;
|