@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
60 lines (58 loc) • 2.6 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { ProxyInfo, WebsocketConnection, } from "../common.browser/Exports";
import { PropertyId } from "../sdk/Exports";
import { ConnectionFactoryBase } from "./ConnectionFactoryBase";
import { WebsocketMessageFormatter } from "./Exports";
import { QueryParameterNames } from "./QueryParameterNames";
const baseUrl = "convai.speech.microsoft.com";
const botFramework = {
authHeader: "X-DLS-Secret",
resourcePath: "",
version: "v3"
};
const speechCommands = {
authHeader: "X-CommandsAppId",
resourcePath: "commands",
version: "v1"
};
const pathSuffix = "api";
function getDialogSpecificValues(dialogType) {
switch (dialogType) {
case "speech_commands": {
return speechCommands;
}
case "bot_framework": {
return botFramework;
}
}
throw new Error(`Invalid dialog type '${dialogType}'`);
}
export class DialogConnectionFactory extends ConnectionFactoryBase {
constructor() {
super(...arguments);
this.create = (config, authInfo, connectionId) => {
const applicationId = config.parameters.getProperty(PropertyId.Conversation_ApplicationId, "");
const dialogType = config.parameters.getProperty(PropertyId.Conversation_DialogType);
const region = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region);
const language = config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage, "en-US");
const queryParams = {};
queryParams[QueryParameterNames.LanguageParamName] = language;
const { resourcePath, version, authHeader } = getDialogSpecificValues(dialogType);
const headers = {};
headers[authInfo.headerName] = authInfo.token;
headers[QueryParameterNames.ConnectionIdHeader] = connectionId;
let endpoint;
// ApplicationId is only required for CustomCommands
if (applicationId === "") {
endpoint = `wss://${region}.${baseUrl}/${pathSuffix}/${version}`;
}
else {
endpoint = `wss://${region}.${baseUrl}/${resourcePath}/${pathSuffix}/${version}`;
headers[authHeader] = applicationId;
}
return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), connectionId);
};
}
}
//# sourceMappingURL=DialogConnectorFactory.js.map