UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

69 lines (67 loc) 3.36 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. import { ProxyInfo, WebsocketConnection, } from "../common.browser/Exports"; import { OutputFormat, PropertyId } from "../sdk/Exports"; import { ConnectionFactoryBase } from "./ConnectionFactoryBase"; import { WebsocketMessageFormatter } from "./Exports"; import { QueryParameterNames } from "./QueryParameterNames"; const baseUrl = "convai.speech"; const botFramework = { authHeader: "X-DLS-Secret", resourcePath: "", version: "v3" }; const customCommands = { authHeader: "X-CommandsAppId", resourcePath: "commands", version: "v1" }; const pathSuffix = "api"; function getDialogSpecificValues(dialogType) { switch (dialogType) { case "custom_commands": { return customCommands; } 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; queryParams[QueryParameterNames.FormatParamName] = config.parameters.getProperty(PropertyId.SpeechServiceResponse_OutputFormatOption, OutputFormat[OutputFormat.Simple]).toLowerCase(); const { resourcePath, version, authHeader } = getDialogSpecificValues(dialogType); const headers = {}; if (authInfo.token != null && authInfo.token !== "") { headers[authInfo.headerName] = authInfo.token; } headers[QueryParameterNames.ConnectionIdHeader] = connectionId; if (applicationId !== "") { headers[authHeader] = applicationId; } let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, ""); if (endpoint === "") { const hostSuffix = (region && region.toLowerCase().startsWith("china")) ? ".azure.cn" : ".microsoft.com"; // ApplicationId is only required for CustomCommands, so we're using that to determine default endpoint if (applicationId === "") { endpoint = `wss://${region}.${baseUrl}${hostSuffix}/${pathSuffix}/${version}`; } else { endpoint = `wss://${region}.${baseUrl}${hostSuffix}/${resourcePath}/${pathSuffix}/${version}`; } } this.setCommonUrlParams(config, queryParams, endpoint); return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), connectionId); }; } } //# sourceMappingURL=DialogConnectorFactory.js.map