This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathSpeechConnectionFactory.ts
More file actions
82 lines (68 loc) · 2.69 KB
/
SpeechConnectionFactory.ts
File metadata and controls
82 lines (68 loc) · 2.69 KB
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
import { WebsocketConnection } from "../../common.browser/Exports";
import {
IConnection,
IStringDictionary,
Promise,
Storage,
} from "../../common/Exports";
import {
AuthInfo,
IAuthentication,
IConnectionFactory,
RecognitionMode,
RecognizerConfig,
SpeechResultFormat,
WebsocketMessageFormatter,
} from "../speech/Exports";
const TestHooksParamName: string = "testhooks";
const EndpointIdParamName: string = "cid";
const ConnectionIdHeader: string = "X-ConnectionId";
export class SpeechConnectionFactory implements IConnectionFactory {
public Create = (
config: RecognizerConfig,
authInfo: AuthInfo,
connectionId?: string): IConnection => {
let endpoint = "";
switch (config.RecognitionMode) {
case RecognitionMode.Conversation:
endpoint = this.Host(config.Host) + this.ConversationRelativeUri;
break;
case RecognitionMode.Dictation:
endpoint = this.Host(config.Host) + this.DictationRelativeUri;
break;
default:
endpoint = this.Host(config.Host) + this.InteractiveRelativeUri; // default is interactive
break;
}
const queryParams: IStringDictionary<string> = {
format: SpeechResultFormat[config.Format].toString().toLowerCase(),
language: config.Language,
};
if (this.IsDebugModeEnabled) {
queryParams[TestHooksParamName] = "1";
}
if (config.EndpointId) {
queryParams[EndpointIdParamName] = config.EndpointId;
}
const headers: IStringDictionary<string> = {};
headers[authInfo.HeaderName] = authInfo.Token;
headers[ConnectionIdHeader] = connectionId;
return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), connectionId);
}
private Host(host: string): string {
return Storage.Local.GetOrAdd("Host", host);
}
private get InteractiveRelativeUri(): string {
return Storage.Local.GetOrAdd("InteractiveRelativeUri", "/speech/recognition/interactive/cognitiveservices/v1");
}
private get ConversationRelativeUri(): string {
return Storage.Local.GetOrAdd("ConversationRelativeUri", "/speech/recognition/conversation/cognitiveservices/v1");
}
private get DictationRelativeUri(): string {
return Storage.Local.GetOrAdd("DictationRelativeUri", "/speech/recognition/dictation/cognitiveservices/v1");
}
private get IsDebugModeEnabled(): boolean {
const value = Storage.Local.GetOrAdd("IsDebugModeEnabled", "false");
return value.toLowerCase() === "true";
}
}