-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAigentConnector.js
293 lines (274 loc) · 9.62 KB
/
AigentConnector.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import WebSocket from "ws";
import fs from "fs";
/**
*
* @tag AigentConnector
* @summary metadata message type
* @description defines a metadata message type of send method
*/
const codeMetadata = 1;
/**
* @tag AigentConnector
* @summary voice message type
* @description defines a voice message type of send method
*/
const codeVoice = 2;
/**
* @tag AigentConnector
* @summary flush message type
* @description defines a message to be sent at the end of the call to flush the buffer and receive the best results
*/
const codeFlush = 3;
/**
* @tag AigentConnector
* @summary a class to connect a browser with Aigent.com
* @description send audio stream to Aigent cluster via websocket.
Make sure your browser is up-to-date and supports both: MediaStream and
Websocket.
* @param {url} Aigent cluster address (e.g. http://example.com:8080)
* @param {metadata} media stream metadata. Map object type.
Example:
{
voice: {
'channel': 'client', // client|agent (mandatory field)
'clientCallId': generateCallId(), // (mandatory field, must be unique per a call) generated uuid
'codec': 'libopus', // (mandatory field)
'samplingRate': 48000, // (mandatory field)
'direction': 'inbound', // inbound|outbound (inbound is default)
},
'agentId': '100900', // (mandatory field)
'category': 'catogory-of-program', // VDN (optional field, this allows the addition of filters to triggers)
'agentWindowsUsername': 'corp//alex' // the osUsername of the client
'ani': 'client-phone-number', // (optional field, but some features will not be available)
'agentAni': 'agent-phone-number', // (optional field)
'programId': 'program-id', // (optional field)
}
* @param {username} username
* @param {password} password
* @param {verbose} optional. If true print information to console.log()
Default is false
*/
export class AigentConnector {
constructor(url, metadata, username, password, verbose = false) {
this.url = url;
this.metadata = metadata;
this.verbose = verbose;
this.username = username;
this.password = password;
this.buffer = [];
this.isMetaDataSent = false;
this.dequeueBufferJob;
}
/**
* @tag AigentConnector
* @summary method
* @description open a new audio stream and send metadata
*/
startStream(keycloak_access_token) {
const uri = `${this.url}?aigent-api-token=${keycloak_access_token}`;
this.socket = new WebSocket(uri);
this.socket.onopen = () => {
if (this.verbose) {
console.log("Connector: opened stream: call id:", this.metadata.voice.clientCallId);
}
this.isMetaDataSent = this.sendCode(codeMetadata, new TextEncoder().encode(JSON.stringify(this.metadata)));
this.dequeueBufferTrigger();
};
this.socket.onmessage = msg => {
// console.log("Connector: msg: ", msg);
console.log("connector msg:", msg.data);
};
this.socket.onclose = event => {
if (event.wasClean) {
if (this.verbose) {
console.log("Connector: connection closed by peer.");
}
this.clean();
return;
}
if (this.verbose) {
console.log("Connector: connection closed.", event);
}
this.clean();
};
this.socket.onerror = err => {
if (this.verbose) {
console.log("Connector: error: ", err);
}
};
}
/**
* @tag AigentConnector
* @summary method
* @description send the given media byte stream
* @param {voice} Uint8Array voice buffer
*/
sendVoice(voice) {
if (!this.isConnected() || (this.buffer && this.buffer.length > 0)) {
/** buffer voice until previous voice data is transmitted via socket */
this.buffer && this.buffer.push({ code: codeVoice, payload: voice });
} else {
this.sendCode(codeVoice, voice);
}
}
/**
* @tag AigentConnector
* @summary method
* @description send the given data
* @param {code} integer. Message type id
* @param {data} Uint8Array data buffer
* * @response true or false
*/
sendCode(code, data) {
if (data && this.isConnected()) {
if (code !== codeMetadata || (code === codeMetadata && !this.isMetaDataSent)) {
this.socket.send(encode(code, data));
return true;
}
} else if (this.verbose) {
// console.log("Connector: unable to send data via socket. Stored in buffer to send it later.", this.buffer);
console.log("Connector: unable to send data via socket. Stored in buffer to send it later.");
}
return false;
}
sendFlush() {
const payload = new Uint8Array(1);
console.log("Connector: send flush");
if (!this.isConnected() || (this.buffer && this.buffer.length > 0)) {
/** buffer voice until previous voice data is transmitted via socket */
this.buffer && this.buffer.push({ code: codeFlush, payload: payload });
} else {
this.sendCode(codeVoice, payload);
}
}
/**
* @tag AigentConnector
* @summary trigger
* @description dequeue buffer to transmit payload via socket
*/
dequeueBuffer() {
while (this.buffer && this.buffer.length > 0) {
let { code, payload } = this.buffer[0];
let isPayloadSent = this.sendCode(code, payload);
if (isPayloadSent) {
this.buffer.shift();
}
}
}
/**
* @tag AigentConnector
* @summary trigger
* @description start buffer dequeue job to run dequeue buffer method at every 100ms interval
*/
dequeueBufferTrigger() {
this.dequeueBufferJob = setInterval(() => {
this.dequeueBuffer();
}, 100);
}
/**
* @tag AigentConnector
* @summary method
* @description close a current connection. Signal end of the stream and clean buffer
*/
clean() {
clearTimeout(this.dequeueBufferJob);
if (this.socket) {
this.socket.close(1000);
delete this.socket;
}
delete this.buffer;
}
/**
* @tag AigentConnector
* @summary method
* @description transmit any leftover data in buffer and close current connection within 3 seconds
*/
close() {
const self = this;
// console.log("buffer is", this.buffer);
clearTimeout(this.dequeueBufferJob);
if (this.buffer && this.buffer.length > 0) {
if (this.socket.readyState === WebSocket.CONNECTING) {
if (this.verbose) {
console.log(
"Connector: connection is connecting. Wait 1 second and try again to send all the data."
);
}
setTimeout(function () {
self.close();
}, 1000);
return;
}
if (this.verbose) {
console.log("Connector: closing connection. Transmitting leftover data in buffer.");
}
this.dequeueBuffer();
// setTimeout(function () {
// self.clean();
// }, 3000);
// self.clean();
} else {
// this.clean();
}
// allow websocket to be closed gracefully from the server side
}
/**
* @tag AigentConnector
* @summary method
* @description true if a socket is connected and in ready state
* @response true or false
*/
isConnected() {
return this.socket && this.socket.readyState === WebSocket.OPEN;
}
}
/**
* @tag AigentConnector
* @summary prepare the given payload for send
* @description Uses internally by send method
* @param {code} integer. Message type id
* @param {payload} Uint8Array. Data being sent
* @response Uint8Array encoded message
*/
const timestampArrayLength = 4;
function generateTimestampArray() {
let timestamp = Math.round(new Date().getTime() / 1000); // get unix timestamp in seconds
let timestampArray = new Uint8Array(timestampArrayLength);
for (var index = 0; index < timestampArray.length; index++) {
var byte = timestamp & 0xff;
timestampArray[index] = byte;
timestamp = (timestamp - byte) / 256;
}
return timestampArray;
}
function fromTimestampArrayToNumber(byteArray) {
var value = 0;
for (var i = byteArray.length - 1; i >= 0; i--) {
value = value * 256 + byteArray[i];
}
return value;
}
function encode(code, payload) {
const binCode = new Uint8Array([code & 0x00ff, (code & 0xff00) >> 8]);
const binMsg = new Uint8Array(binCode.byteLength + timestampArrayLength + payload.byteLength);
const timestampArray = generateTimestampArray();
binMsg.set(binCode, 0);
binMsg.set(timestampArray, binCode.byteLength);
binMsg.set(payload, binCode.byteLength + timestampArrayLength);
return binMsg;
}
/**
* @tag AigentConnector
* @summary generate unique call identifier.
* @description generate uuidv4 using xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx schema
* @param {code} integer. Message type id
* @param {payload} Uint8Array. Data being sent
* @response uuid string value
*/
function generateCallId() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
const v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}