-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
sepia-stt-socket-client.js
278 lines (247 loc) · 7.84 KB
/
sepia-stt-socket-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
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
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
//SEPIA STT Server Client:
class SepiaSttSocketClient {
constructor(serverUrl, clientId, accessToken, engineOptions, serverOptions){
this.serverUrl = (serverUrl || "http://localhost:20741").replace("\/$", "");
this.socketHost = this.serverUrl.replace(/^(http)/, "ws") + "/socket"; //note: '/socket' is a path alias for proxies
this.clientId = clientId || "any";
this.accessToken = accessToken || "test1234";
if (!engineOptions) engineOptions = {};
if (!serverOptions) serverOptions = {};
this.serverVersion = "";
this.asrEngine = "";
this.availableLanguages = [];
this.availableModels = [];
this.availableFeatures = [];
this.activeOptions = Object.assign({
samplerate: 16000,
continuous: false,
language: "",
task: "",
model: ""
}, engineOptions);
this.activeLanguageCode = this.activeOptions.language || "";
this.activeAsrTask = this.activeOptions.task || "";
this.activeAsrModel = this.activeOptions.model || "";
//NOTE: currently not assigned
this.phrases = this.activeOptions.phrases || [];
this.hotWords = this.activeOptions.hotWords || [];
this._msgId = 0;
this.websocket = undefined;
this._connectionId = 0;
this.autoCloseOnLastFinal = true; //applies to non-continuous setup only
this.connectionIsOpen = false;
this.isReadyForStream = false;
this._onOpen = serverOptions.onOpen || function(){};
this._onReady = serverOptions.onReady || function(activeOptions){};
this._onClose = serverOptions.onClose || function(ev){};
this._onResult = serverOptions.onResult || function(res){};
this._onError = serverOptions.onError || function(err){
console.error("SepiaSttSocketClient ERROR", err);
};
this._skipAutoWelcome = serverOptions.skipAutoWelcome;
this._doDebug = serverOptions.doDebug;
}
log(msg){
if (this._doDebug){
console.log("SepiaSttSocketClient", msg);
}
}
//--- HTTP Interface ---
pingServer(successCallback, errorCallback){
const controller = new AbortController();
setTimeout(function(){controller.abort();}, 8000);
fetch(this.serverUrl + "/ping", {
signal: controller.signal
}).then(function(res){ return res.json(); })
.then(function(json){
if (successCallback) successCallback(json);
})
.catch(function(err){
if (errorCallback) errorCallback(err);
});
}
loadServerInfo(successCallback, errorCallback){
const controller = new AbortController();
setTimeout(function(){controller.abort();}, 8000);
fetch(this.serverUrl + "/settings", {
method: "GET",
signal: controller.signal
}).then(function(res){
if (res.ok){
return res.json();
}else{
if (errorCallback) errorCallback({
"name": "FetchError", "message": res.statusText, "code": res.status
});
}
}).then(function(json){
if (json && json.settings){
this._handleServerSettings(json.settings);
if (successCallback) successCallback(json.settings);
}else{
if (successCallback) successCallback({});
}
}).catch(function(err){
if (errorCallback) errorCallback(err);
});
}
_handleServerSettings(settings){
this.serverVersion = settings.version;
this.asrEngine = settings.engine || "";
this.availableLanguages = settings.languages || [];
this.availableModels = settings.models || [];
this.availableFeatures = settings.features || [];
}
//--- WebSocket interface ---
newMessageId(){
var msgId = ++this._msgId;
if (msgId > 999999){
msgId = 1;
}
return msgId;
}
openConnection(){
if (this.websocket && this.websocket.readyState == this.websocket.OPEN){
this._onError({name: "SocketConnectionError", message: "Connection was already OPEN"});
return false;
}
var self = this;
var thisConnecitonId;
//CREATE
this.websocket = new WebSocket(this.socketHost);
//ONOPEN
this.websocket.onopen = function(){
self.log("Connection OPEN");
self.connectionIsOpen = true;
self._connectionId++;
thisConnecitonId = self._connectionId;
self._onOpen();
//send welcome
if (!self._skipAutoWelcome){
self.sendWelcome();
}
}
//ONCLOSE
this.websocket.onclose = function(ev){
self.log("Connection CLOSED");
self.connectionIsOpen = false;
self.isReadyForStream = false;
//understand error: ev.code or ev.reason might give more insights
self._onClose(ev);
}
//ONMESSAGE
this.websocket.onmessage = function(event){
if (event && event.data && typeof event.data == "string"){
self.log("Connection MESSAGE: " + event.data); //DEBUG
try {
self.handleSocketMessage(JSON.parse(event.data));
}catch(err){
console.error("SepiaSttSocketClient - MessageParserError", err);
self.handleSocketError({name: "SocketMessageParserError", message: "Message handler saw invlaid JSON?!"});
}
}
}
//ONERROR
this.websocket.onerror = function(error){
if (!thisConnecitonId){
//never opened
self._onError({name: "SocketConnectionError", message: "Failed to connect"});
}else{
self.handleSocketError(error);
}
}
return true;
}
closeConnection(){
if (this.websocket && this.websocket.readyState == this.websocket.OPEN){
this.websocket.close();
return true;
}else{
//fail silently?
return false;
}
}
handleSocketMessage(msgJson){
if (msgJson.type == "error"){
this.handleSocketError(msgJson);
}else{
//console.log("handleSocketMessage", JSON.stringify(msgJson)); //DEBUG
if (msgJson.type == "ping"){
//TODO: send only after welcome
this.sendJson({type: "pong", msg_id: msgJson.msg_id});
}else if (msgJson.type == "welcome"){
this.log("Connection WELCOME");
//read active session/engine options
this.activeOptions = msgJson.info? msgJson.info.options : {};
this.activeLanguageCode = this.activeOptions.language || "";
this.activeAsrTask = this.activeOptions.task || "";
this.activeAsrModel = this.activeOptions.model || "";
this.isReadyForStream = true;
this._onReady(this.activeOptions);
}else if (msgJson.type == "result"){
this._onResult(msgJson);
if (msgJson.isFinal && !this.activeOptions.continuous && this.autoCloseOnLastFinal){
//after final result, close connection
this.closeConnection();
}
}else if (msgJson.type == "response"){
//anything?
}
}
}
handleSocketError(err){
var error = {};
if (!err) error = {name: "SocketMessageError", message: "unknown"};
else if (typeof err == "string") error = {name: "SocketMessageError", message: err};
else error = {name: (err.name || "SocketMessageError"), message: (err.message || "unknown"), details: err};
//send
this._onError(error);
//Errors are not acceptable :-p - close in any case
this.closeConnection(); //this has probably no effect at all
}
sendJson(json){
if (this.websocket && this.websocket.readyState == this.websocket.OPEN){
this.websocket.send(JSON.stringify(json));
return true;
}else{
this._onError({name: "SocketConnectionError", message: "Connection is closed. Cannot send message."});
//throw error?
return false;
}
}
sendBytes(bufferChunk){
if (!this.websocket || this.websocket.readyState != this.websocket.OPEN){
this._onError({name: "SocketConnectionError", message: "Connection is closed. Cannot send message."});
return false;
}
this.websocket.send(bufferChunk); //this can be a typed array (recommended), view or blob (I guess)
return true;
}
sendMessage(text){
return this.sendJson({
"type": "chat",
"data": {"text": text},
"msg_id": this.newMessageId()
});
}
sendWelcome(options){
var optionsToSend = options || this.activeOptions;
return this.sendJson({
"type": "welcome",
"data": optionsToSend,
"client_id": this.clientId,
"access_token": this.accessToken,
"msg_id": this.newMessageId()
});
}
sendAudioEnd(byteLength, bufferOrTimeLimit){
return this.sendJson({
"type": "audioend",
"data": {
"byteLength": byteLength,
"bufferOrTimeLimit": bufferOrTimeLimit
},
"msg_id": this.newMessageId()
});
}
}