microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 6.69 kB
Source Map (JSON)
{"version":3,"sources":["src/common.speech/TranslationConnectionFactory.ts"],"names":[],"mappings":"AAOA,OAAO,EACH,WAAW,EACX,iBAAiB,EACpB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACH,qBAAqB,EACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACH,QAAQ,EACR,gBAAgB,EAEnB,MAAM,cAAc,CAAC;AAKtB,qBAAa,4BAA6B,SAAQ,qBAAqB;IAEnE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAsC;IACnE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAsD;IAE1E,MAAM,CACf,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,QAAQ,EAClB,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAmCzC,cAAc,CAAC,MAAM,EAAE,gBAAgB,EAAE,uBAAuB,CAAC,EAAE,OAAO,GAAG,MAAM;IAmCnF,cAAc,CAAC,WAAW,EAAE,iBAAiB,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI;CAyBrH","file":"TranslationConnectionFactory.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 IConnection,\r\n IStringDictionary,\r\n} from \"../common/Exports.js\";\r\nimport { StringUtils } from \"../common/StringUtils.js\";\r\nimport {\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 { QueryParameterNames } from \"./QueryParameterNames.js\";\r\nimport { RecognitionMode } from \"./ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\r\n\r\nexport class TranslationConnectionFactory extends ConnectionFactoryBase {\r\n\r\n private readonly universalUri: string = \"/stt/speech/universal/v2\";\r\n private readonly translationV1Uri: string = \"/speech/translation/cognitiveservices/v1\";\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 = this.getEndpointUrl(config);\r\n\r\n const queryParams: IStringDictionary<string> = {};\r\n\r\n // Determine if we're using V1 or V2 endpoint\r\n this.setQueryParams(queryParams, config, 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;\r\n\r\n endpoint = await ConnectionFactoryBase.getRedirectUrlFromEndpoint(endpointUrl.toString());\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\r\n config.parameters.setProperty(PropertyId.SpeechServiceConnection_Url, endpoint);\r\n\r\n const enableCompression: boolean = config.parameters.getProperty(\"SPEECH-EnableWebsocketCompression\", \"false\") === \"true\";\r\n const webSocketConnection = new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), enableCompression, connectionId);\r\n\r\n return webSocketConnection;\r\n }\r\n\r\n public getEndpointUrl(config: RecognizerConfig, returnRegionPlaceholder?: boolean): string {\r\n const region: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region);\r\n const hostSuffix: string = ConnectionFactoryBase.getHostSuffix(region);\r\n\r\n // First check for an explicitly specified endpoint\r\n let endpointUrl: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, undefined);\r\n\r\n // If an explicit endpoint is provided, use it\r\n if (endpointUrl) {\r\n if (returnRegionPlaceholder === true) {\r\n return endpointUrl;\r\n }\r\n return StringUtils.formatString(endpointUrl, { region });\r\n }\r\n\r\n // Check if V1 endpoint is explicitly requested\r\n const forceV1Endpoint: boolean = config.parameters.getProperty(\"SPEECH-ForceV1Endpoint\", \"false\") === \"true\";\r\n\r\n if (forceV1Endpoint) {\r\n // Use V1 endpoint with s2s.speech host\r\n const host: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, \"wss://{region}.s2s.speech\" + hostSuffix);\r\n endpointUrl = host + this.translationV1Uri;\r\n } else {\r\n // Default to V2 endpoint with stt.speech host\r\n const host: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, \"wss://{region}.stt.speech\" + hostSuffix);\r\n endpointUrl = host + this.universalUri;\r\n }\r\n\r\n if (returnRegionPlaceholder === true) {\r\n return endpointUrl;\r\n }\r\n\r\n return StringUtils.formatString(endpointUrl, { region });\r\n }\r\n\r\n public setQueryParams(queryParams: IStringDictionary<string>, config: RecognizerConfig, endpointUrl: string): void {\r\n // Common parameters for both V1 and V2 endpoints\r\n queryParams.from = config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);\r\n queryParams.to = config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages);\r\n queryParams.scenario = config.recognitionMode === RecognitionMode.Interactive ? \"interactive\" :\r\n config.recognitionMode === RecognitionMode.Conversation ? \"conversation\" : \"\";\r\n\r\n // Set common parameters\r\n this.setCommonUrlParams(config, queryParams, endpointUrl);\r\n this.setUrlParameter(\r\n PropertyId.SpeechServiceResponse_TranslationRequestStablePartialResult,\r\n QueryParameterNames.StableTranslation,\r\n config,\r\n queryParams,\r\n endpointUrl\r\n );\r\n\r\n // Handle translation voice if specified\r\n const translationVoice: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined);\r\n if (translationVoice !== undefined) {\r\n queryParams.voice = translationVoice;\r\n // Updated to match C++ implementation\r\n queryParams.features = \"requireVoice\";\r\n }\r\n }\r\n}\r\n"]}