This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
VirtualDevice.ts
196 lines (163 loc) · 5.69 KB
/
VirtualDevice.ts
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
import { IncomingMessage } from "http";
import * as https from "https";
import * as URL from "url";
export class VirtualDevice {
public baseURL: string;
public homophones: {[id: string]: string[]} = {};
public constructor(public token: string, public locale?: string, public voiceID?: string) {
this.baseURL = process.env.VIRTUAL_DEVICE_BASE_URL
? process.env.VIRTUAL_DEVICE_BASE_URL
: "https://virtual-device.bespoken.io";
}
public addHomophones(word: string, homophones: string[]) {
homophones = homophones.map((s) => s.trim());
this.homophones[word] = homophones;
}
public message(message: string, debug?: boolean, phrases?: string[]): Promise<IVirtualDeviceResult> {
let url = this.baseURL + "/process"
+ "?message=" + message
+ "&user_id=" + this.token;
if (phrases) {
for (const phrase of phrases) {
url += "&phrases=" + phrase;
}
}
if (debug) {
url += "&debug=true";
}
if (this.locale) {
url += "&language_code=" + this.locale;
}
if (this.voiceID) {
url += "&voice_id=" + this.voiceID;
}
url = encodeURI(url);
return new Promise<IVirtualDeviceResult>((resolve, reject) => {
const callback = (response: IncomingMessage) => {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
response.on("end", () => {
if (response.statusCode === 200) {
const result: IVirtualDeviceResult = JSON.parse(data);
result.message = message;
this.applyHomophones(result);
resolve(result);
} else {
reject(data);
}
});
};
const request = https.get(url as any, callback);
request.on("error", function(error: string) {
reject(error);
});
request.end();
});
}
public batchMessage(messages: IMessage[], debug?: boolean): Promise<IVirtualDeviceResult[]> {
let path = "/batch_process?user_id=" + this.token;
if (debug) {
path += "&debug=true";
}
if (this.locale) {
path += "&language_code=" + this.locale;
}
if (this.voiceID) {
path += "&voice_id=" + this.voiceID;
}
const url = URL.parse(this.baseURL);
return new Promise<IVirtualDeviceResult[]>((resolve, reject) => {
const callback = (response: IncomingMessage) => {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
response.on("end", () => {
if (response.statusCode === 200) {
resolve(this.handleBatchResponse(data as string));
} else {
reject(data);
}
});
};
const input = {
messages,
};
const inputString = JSON.stringify(input);
const requestOptions = {
headers: {
"Content-Length": new Buffer(inputString).length,
"Content-Type": "application/json",
},
host: url.hostname,
method: "POST",
path,
port: 443,
};
const request = https.request(requestOptions, callback);
request.on("error", function(error: string) {
reject(error);
});
request.write(inputString);
request.end();
});
}
public async waitForSessionToEnd() {
const ms: number = process.env.SESSION_IDLE_MS
? parseInt(process.env.SESSION_IDLE_MS, 10)
: 8000;
return new Promise((resolve) => setTimeout(resolve, ms));
}
private handleBatchResponse(data: string): IVirtualDeviceResult[] {
const json = JSON.parse(data);
for (const result of json.results) {
this.applyHomophones(result);
}
return json.results;
}
private applyHomophones(result: IVirtualDeviceResult) {
if (!result.debug) {
result.debug = {};
}
if (!result.transcript) {
return;
}
const keys = Object.keys(this.homophones);
result.debug.rawTranscript = result.transcript;
for (const key of keys) {
// Replace underscore with space - because we use environment variables to set these at times,
// underscores are needed
const word = key.split("_").join(" ");
const homophones = this.homophones[key];
for (const homophone of homophones) {
// Replace each of the homophones
result.transcript = result.transcript.split(new RegExp("\\b" + homophone + "\\b")).join(word);
}
}
}
}
export interface IVirtualDeviceResult {
card: ICard | null;
debug: {
rawTranscript?: string;
rawJSON?: any;
};
sessionTimeout: number;
streamURL: string | null;
transcript: string | null;
// message is the message used for this result.
message: string;
}
export interface ICard {
imageURL: string | null;
mainTitle: string | null;
subTitle: string | null;
textField: string | null;
type: string;
}
export interface IMessage {
text: string;
phrases?: string[];
}