-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcreateAdapters.js
263 lines (220 loc) · 9.76 KB
/
createAdapters.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
/* eslint complexity: ["error", 33] */
import { AudioConfig } from 'microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/Audio/AudioConfig';
import { BotFrameworkConfig, DialogServiceConnector, PropertyId } from 'microsoft-cognitiveservices-speech-sdk';
import createWebSpeechPonyfillFactory from './createWebSpeechPonyfillFactory';
import DirectLineSpeech from './DirectLineSpeech';
import patchDialogServiceConnectorInline from './patchDialogServiceConnectorInline';
import refreshDirectLineToken from './utils/refreshDirectLineToken';
import resolveFunctionOrReturnValue from './resolveFunctionOrReturnValue';
const DIRECT_LINE_TOKEN_RENEWAL_INTERVAL = 900000; // 15 minutes
const TOKEN_RENEWAL_INTERVAL = 120000;
// eslint-disable-next-line complexity
export default async function create({
audioConfig,
audioContext,
audioInputDeviceId,
enableInternalHTTPSupport,
enableTelemetry,
fetchCredentials,
speechRecognitionEndpointId,
speechRecognitionLanguage = (typeof window !== 'undefined' &&
typeof window.navigator !== 'undefined' &&
window.navigator.language) ||
'en-US',
speechSynthesisDeploymentId,
speechSynthesisOutputFormat,
textNormalization,
userID,
username
}) {
if (!fetchCredentials) {
throw new Error('"fetchCredentials" must be specified.');
}
const {
authorizationToken,
directLineToken,
directLineSpeechHostname,
region,
subscriptionKey
} = await resolveFunctionOrReturnValue(fetchCredentials);
if (
(!authorizationToken && !subscriptionKey) ||
(authorizationToken && subscriptionKey) ||
(authorizationToken && typeof authorizationToken !== 'string') ||
(subscriptionKey && typeof subscriptionKey !== 'string') ||
(enableInternalHTTPSupport && !directLineToken)
) {
throw new Error(
'"fetchCredentials" must return either "authorizationToken" or "subscriptionKey" as a non-empty string only. If enableInternalHTTPSupport is set to true, then it should also return a non-empty "directLineToken"'
);
}
if (typeof enableTelemetry !== 'undefined') {
console.warn(
'botframework-directlinespeech: Telemetry options are not yet supported. Please refer to Cognitive Services documentation for details.'
);
}
if ((directLineSpeechHostname && region) || (!directLineSpeechHostname && !region)) {
throw new Error(
'"fetchCredentials" must return either "directLineSpeechHostname" or "region" and it must not be an empty string.'
);
}
if (directLineSpeechHostname) {
if (typeof directLineSpeechHostname !== 'string') {
throw new Error('"fetchCredentials" must return "directLineSpeechHostname" as a string.');
}
if (enableInternalHTTPSupport) {
throw new Error(
'"fetchCredentials" must not return "directLineSpeechHostname" if "enableInternalHTTPSupport" is set.'
);
}
} else {
if (typeof region !== 'string') {
throw new Error('"fetchCredentials" must return "region" as a string.');
}
}
if (audioConfig && audioInputDeviceId) {
console.warn(
'botframework-directlinespeech-sdk: Only "audioConfig" or "audioInputDeviceId" can be specified, but not both; ignoring "audioInputDeviceId".'
);
} else if (!audioConfig) {
if (audioInputDeviceId) {
audioConfig = AudioConfig.fromMicrophoneInput(audioInputDeviceId);
} else {
audioConfig = AudioConfig.fromDefaultMicrophoneInput();
}
}
if (speechRecognitionEndpointId) {
console.warn(
'botframework-directlinespeech: Custom Speech is currently not supported; ignoring "speechRecognitionEndpointId".'
);
}
if (speechSynthesisDeploymentId) {
console.warn(
'botframework-directlinespeech: Custom Voice is currently not supported; ignoring "speechSynthesisDeploymentId".'
);
}
if (speechSynthesisOutputFormat) {
console.warn(
'botframework-directlinespeech: Synthesis output format is currently not supported; ignoring "speechSynthesisOutputFormat".'
);
}
if (textNormalization) {
console.warn(
'botframework-directlinespeech: Text normalization is currently not supported; ignoring "textNormalization".'
);
}
if (userID || username) {
console.warn(
'botframework-directlinespeech: Custom "userId" and "username" are currently not supported and are ignored.'
);
}
let config;
if (directLineSpeechHostname) {
if (authorizationToken) {
config = BotFrameworkConfig.fromHost(new URL(`wss://${directLineSpeechHostname}`));
config.setProperty(PropertyId.SpeechServiceAuthorization_Token, authorizationToken);
} else {
config = BotFrameworkConfig.fromHost(new URL(`wss://${directLineSpeechHostname}`), subscriptionKey);
}
// TODO: [P1] #3693 In Speech SDK 1.15.0, there is a bug that wrongly construct the endpoint.
// https://github.com/microsoft/cognitive-services-speech-sdk-js/issues/315
// Remove the following line after the bug is resolved.
config.setProperty(PropertyId.SpeechServiceConnection_Host, `wss://${directLineSpeechHostname}`);
} else {
if (authorizationToken) {
config = BotFrameworkConfig.fromAuthorizationToken(authorizationToken, region);
} else {
config = BotFrameworkConfig.fromSubscription(subscriptionKey, region);
}
// If internal HTTP support is enabled, switch the endpoint to Direct Line on Direct Line Speech service.
if (enableInternalHTTPSupport) {
config.setProperty(
PropertyId.SpeechServiceConnection_Endpoint,
`wss://${encodeURI(region)}.convai.speech.microsoft.com/directline/api/v1`
);
config.setProperty(PropertyId.Conversation_Agent_Connection_Id, directLineToken);
}
}
// Supported options can be found in DialogConnectorFactory.js.
// Set the language used for recognition.
config.setProperty(PropertyId.SpeechServiceConnection_RecoLanguage, speechRecognitionLanguage);
// The following code sets the output format.
// As advised by the Speech team, this API may be subject to future changes.
// We are not enabling output format option because it does not send detailed output format to the bot, rendering this option useless.
// config.setProperty(PropertyId.SpeechServiceResponse_OutputFormatOption, OutputFormat[OutputFormat.Detailed]);
// Set the user ID for starting the conversation.
userID && config.setProperty(PropertyId.Conversation_From_Id, userID);
// Set Custom Speech and Custom Voice.
// The following code is copied from C#, and it is not working yet.
// https://github.com/Azure-Samples/Cognitive-Services-Direct-Line-Speech-Client/blob/master/DLSpeechClient/MainWindow.xaml.cs
// speechRecognitionEndpointId && config.setServiceProperty('cid', speechRecognitionEndpointId, ServicePropertyChannel.UriQueryParameter);
// speechSynthesisDeploymentId && config.setProperty(PropertyId.conversation_Custom_Voice_Deployment_Ids, speechSynthesisDeploymentId);
const dialogServiceConnector = patchDialogServiceConnectorInline(new DialogServiceConnector(config, audioConfig));
// Renew token per interval.
if (authorizationToken) {
const interval = setInterval(async () => {
// #2660 If the connector has been disposed, we should stop renewing the token.
// TODO: We should use a public implementation if Speech SDK has one related to "privIsDisposed".
if (dialogServiceConnector.privIsDisposed) {
clearInterval(interval);
}
const {
authorizationToken,
directLineSpeechHostname: nextDirectLineSpeechHostname,
region: nextRegion
} = await resolveFunctionOrReturnValue(fetchCredentials);
if (!authorizationToken) {
return console.warn(
'botframework-directlinespeech-sdk: Renew token failed because "fetchCredentials" call returned no authorization token.'
);
}
if (directLineSpeechHostname) {
if (directLineSpeechHostname !== nextDirectLineSpeechHostname) {
return console.warn(
'botframework-directlinespeech-sdk: "directLineSpeechHostname" change is not supported for renewed token. Authorization token is not renewed.'
);
}
} else {
if (region !== nextRegion) {
return console.warn(
'botframework-directlinespeech-sdk: Region change is not supported for renewed token. Authorization token is not renewed.'
);
}
}
dialogServiceConnector.authorizationToken = authorizationToken; // eslint-disable-line require-atomic-updates
}, TOKEN_RENEWAL_INTERVAL);
}
// Renew token per interval.
if (enableInternalHTTPSupport) {
const interval = setInterval(async () => {
// #2660 If the connector has been disposed, we should stop renewing the token.
// TODO: We should use a public implementation if Speech SDK has one related to "privIsDisposed".
if (dialogServiceConnector.privIsDisposed) {
clearInterval(interval);
}
const refreshedDirectLineToken = await refreshDirectLineToken(directLineToken);
if (!refreshedDirectLineToken) {
return console.warn(
'botframework-directlinespeech-sdk: Renew token failed because call to refresh token Direct Line API did not return a new token.'
);
}
config.setProperty(PropertyId.Conversation_Agent_Connection_Id, refreshedDirectLineToken);
dialogServiceConnector.properties.setProperty(
PropertyId.Conversation_Agent_Connection_Id,
refreshedDirectLineToken
);
dialogServiceConnector.connect();
}, DIRECT_LINE_TOKEN_RENEWAL_INTERVAL);
}
const directLine = new DirectLineSpeech({ dialogServiceConnector });
const webSpeechPonyfillFactory = createWebSpeechPonyfillFactory({
audioContext,
enableTelemetry,
recognizer: dialogServiceConnector,
textNormalization
});
return {
directLine,
webSpeechPonyfillFactory
};
}