{"version":3,"sources":["src/common.speech/SpeechConnectionFactory.ts"],"names":[],"mappings":"AAWA,OAAO,EACH,WAAW,EAEd,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACH,qBAAqB,EACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACH,QAAQ,EACR,gBAAgB,EAEnB,MAAM,cAAc,CAAC;AAOtB,qBAAa,uBAAwB,SAAQ,qBAAqB;IAE9D,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAkE;IACzG,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAmE;IAC3G,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAgE;IACrG,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAqC;IAErD,MAAM,CACf,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,QAAQ,EAClB,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAwFnD","file":"SpeechConnectionFactory.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n    ProxyInfo,\r\n    WebsocketConnection,\r\n} from \"../common.browser/Exports.js\";\r\nimport {\r\n    ForceDictationPropertyName,\r\n    OutputFormatPropertyName,\r\n} from \"../common.speech/Exports.js\";\r\nimport {\r\n    IConnection,\r\n    IStringDictionary\r\n} from \"../common/Exports.js\";\r\nimport {\r\n    OutputFormat,\r\n    PropertyId\r\n} from \"../sdk/Exports.js\";\r\nimport {\r\n    ConnectionFactoryBase\r\n} from \"./ConnectionFactoryBase.js\";\r\nimport {\r\n    AuthInfo,\r\n    RecognizerConfig,\r\n    WebsocketMessageFormatter\r\n} from \"./Exports.js\";\r\nimport { HeaderNames } from \"./HeaderNames.js\";\r\nimport {\r\n    QueryParameterNames\r\n} from \"./QueryParameterNames.js\";\r\nimport { RecognitionMode } from \"./ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\r\n\r\nexport class SpeechConnectionFactory extends ConnectionFactoryBase {\r\n\r\n    private readonly interactiveRelativeUri: string = \"/speech/recognition/interactive/cognitiveservices/v1\";\r\n    private readonly conversationRelativeUri: string = \"/speech/recognition/conversation/cognitiveservices/v1\";\r\n    private readonly dictationRelativeUri: string = \"/speech/recognition/dictation/cognitiveservices/v1\";\r\n    private readonly universalUri: string = \"/stt/speech/universal/v\";\r\n\r\n    public async create(\r\n        config: RecognizerConfig,\r\n        authInfo: AuthInfo,\r\n        connectionId?: string): Promise<IConnection> {\r\n\r\n        let endpoint: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, undefined);\r\n        const region: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, undefined);\r\n        const hostSuffix: string = ConnectionFactoryBase.getHostSuffix(region);\r\n        const host: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, \"wss://\" + region + \".stt.speech\" + hostSuffix);\r\n        const queryParams: IStringDictionary<string> = {};\r\n        const endpointId: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_EndpointId, undefined);\r\n        const language: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage, undefined);\r\n\r\n        if (endpointId) {\r\n            if (!endpoint || endpoint.search(QueryParameterNames.CustomSpeechDeploymentId) === -1) {\r\n                queryParams[QueryParameterNames.CustomSpeechDeploymentId] = endpointId;\r\n            }\r\n        } else if (language) {\r\n            if (!endpoint || endpoint.search(QueryParameterNames.Language) === -1) {\r\n                queryParams[QueryParameterNames.Language] = language;\r\n            }\r\n        }\r\n\r\n        if (!endpoint || endpoint.search(QueryParameterNames.Format) === -1) {\r\n            queryParams[QueryParameterNames.Format] = config.parameters.getProperty(OutputFormatPropertyName, OutputFormat[OutputFormat.Simple]).toLowerCase();\r\n        }\r\n\r\n        if (config.autoDetectSourceLanguages !== undefined) {\r\n            queryParams[QueryParameterNames.EnableLanguageId] = \"true\";\r\n        }\r\n\r\n        this.setCommonUrlParams(config, queryParams, endpoint);\r\n\r\n        if (!!endpoint) {\r\n            const endpointUrl = new URL(endpoint);\r\n            const pathName = endpointUrl.pathname;\r\n\r\n            if (pathName === \"\" || pathName === \"/\") {\r\n                // We need to generate the path, and we need to check for a redirect.\r\n                endpointUrl.pathname = this.universalUri + config.recognitionEndpointVersion;\r\n\r\n                endpoint = await ConnectionFactoryBase.getRedirectUrlFromEndpoint(endpointUrl.toString());\r\n            }\r\n        }\r\n\r\n        if (!endpoint) {\r\n            switch (config.recognitionMode) {\r\n                case RecognitionMode.Conversation:\r\n                    if (config.parameters.getProperty(ForceDictationPropertyName, \"false\") === \"true\") {\r\n                        endpoint = host + this.dictationRelativeUri;\r\n                    } else {\r\n                        if (config.recognitionEndpointVersion !== undefined && parseInt(config.recognitionEndpointVersion, 10) > 1) {\r\n                            endpoint = `${host}${this.universalUri}${config.recognitionEndpointVersion}`;\r\n                        } else {\r\n                            endpoint = host + this.conversationRelativeUri;\r\n                        }\r\n                    }\r\n                    break;\r\n                case RecognitionMode.Dictation:\r\n                    endpoint = host + this.dictationRelativeUri;\r\n                    break;\r\n                default:\r\n                    if (config.recognitionEndpointVersion !== undefined && parseInt(config.recognitionEndpointVersion, 10) > 1) {\r\n                        endpoint = `${host}${this.universalUri}${config.recognitionEndpointVersion}`;\r\n                    } else {\r\n                        endpoint = host + this.interactiveRelativeUri; // default is interactive\r\n                    }\r\n                    break;\r\n            }\r\n        }\r\n\r\n        const headers: IStringDictionary<string> = {};\r\n        if (authInfo.token !== undefined && authInfo.token !== \"\") {\r\n            headers[authInfo.headerName] = authInfo.token;\r\n        }\r\n        headers[HeaderNames.ConnectionId] = connectionId;\r\n        headers.connectionId = connectionId;\r\n\r\n        const enableCompression: boolean = config.parameters.getProperty(\"SPEECH-EnableWebsocketCompression\", \"false\") === \"true\";\r\n\r\n        const webSocketConnection = new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), enableCompression, connectionId);\r\n\r\n        // Set the value of SpeechServiceConnection_Url to webSocketConnection.uri (and not to `endpoint`), since this value is the final\r\n        // URI that was used to make the connection (including query parameters).\r\n        const uri: string = webSocketConnection.uri;\r\n        config.parameters.setProperty(PropertyId.SpeechServiceConnection_Url, uri);\r\n\r\n        return webSocketConnection;\r\n    }\r\n\r\n\r\n}\r\n\r\n"]}