@hahnpro/ms-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 6.43 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.\n// Licensed under the MIT license.\n\nimport {\n ProxyInfo,\n WebsocketConnection,\n} from \"../common.browser/Exports.js\";\nimport {\n IConnection,\n IStringDictionary,\n} from \"../common/Exports.js\";\nimport { StringUtils } from \"../common/StringUtils.js\";\nimport {\n PropertyId\n} from \"../sdk/Exports.js\";\nimport {\n ConnectionFactoryBase\n} from \"./ConnectionFactoryBase.js\";\nimport {\n AuthInfo,\n RecognizerConfig,\n WebsocketMessageFormatter,\n} from \"./Exports.js\";\nimport { HeaderNames } from \"./HeaderNames.js\";\nimport { QueryParameterNames } from \"./QueryParameterNames.js\";\nimport { RecognitionMode } from \"./ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\n\nexport class TranslationConnectionFactory extends ConnectionFactoryBase {\n\n private readonly universalUri: string = \"/stt/speech/universal/v2\";\n private readonly translationV1Uri: string = \"/speech/translation/cognitiveservices/v1\";\n\n public async create(\n config: RecognizerConfig,\n authInfo: AuthInfo,\n connectionId?: string): Promise<IConnection> {\n\n let endpoint: string = this.getEndpointUrl(config);\n\n const queryParams: IStringDictionary<string> = {};\n\n // Determine if we're using V1 or V2 endpoint\n this.setQueryParams(queryParams, config, endpoint);\n\n if (!!endpoint) {\n const endpointUrl = new URL(endpoint);\n const pathName = endpointUrl.pathname;\n\n if (pathName === \"\" || pathName === \"/\") {\n // We need to generate the path, and we need to check for a redirect.\n endpointUrl.pathname = this.universalUri;\n\n endpoint = await ConnectionFactoryBase.getRedirectUrlFromEndpoint(endpointUrl.toString());\n }\n }\n\n const headers: IStringDictionary<string> = {};\n if (authInfo.token !== undefined && authInfo.token !== \"\") {\n headers[authInfo.headerName] = authInfo.token;\n }\n headers[HeaderNames.ConnectionId] = connectionId;\n\n config.parameters.setProperty(PropertyId.SpeechServiceConnection_Url, endpoint);\n\n const enableCompression: boolean = config.parameters.getProperty(\"SPEECH-EnableWebsocketCompression\", \"false\") === \"true\";\n const webSocketConnection = new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), enableCompression, connectionId);\n\n return webSocketConnection;\n }\n\n public getEndpointUrl(config: RecognizerConfig, returnRegionPlaceholder?: boolean): string {\n const region: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region);\n const hostSuffix: string = ConnectionFactoryBase.getHostSuffix(region);\n\n // First check for an explicitly specified endpoint\n let endpointUrl: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, undefined);\n\n // If an explicit endpoint is provided, use it\n if (endpointUrl) {\n if (returnRegionPlaceholder === true) {\n return endpointUrl;\n }\n return StringUtils.formatString(endpointUrl, { region });\n }\n\n // Check if V1 endpoint is explicitly requested\n const forceV1Endpoint: boolean = config.parameters.getProperty(\"SPEECH-ForceV1Endpoint\", \"false\") === \"true\";\n\n if (forceV1Endpoint) {\n // Use V1 endpoint with s2s.speech host\n const host: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, \"wss://{region}.s2s.speech\" + hostSuffix);\n endpointUrl = host + this.translationV1Uri;\n } else {\n // Default to V2 endpoint with stt.speech host\n const host: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, \"wss://{region}.stt.speech\" + hostSuffix);\n endpointUrl = host + this.universalUri;\n }\n\n if (returnRegionPlaceholder === true) {\n return endpointUrl;\n }\n\n return StringUtils.formatString(endpointUrl, { region });\n }\n\n public setQueryParams(queryParams: IStringDictionary<string>, config: RecognizerConfig, endpointUrl: string): void {\n // Common parameters for both V1 and V2 endpoints\n queryParams.from = config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);\n queryParams.to = config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages);\n queryParams.scenario = config.recognitionMode === RecognitionMode.Interactive ? \"interactive\" :\n config.recognitionMode === RecognitionMode.Conversation ? \"conversation\" : \"\";\n\n // Set common parameters\n this.setCommonUrlParams(config, queryParams, endpointUrl);\n this.setUrlParameter(\n PropertyId.SpeechServiceResponse_TranslationRequestStablePartialResult,\n QueryParameterNames.StableTranslation,\n config,\n queryParams,\n endpointUrl\n );\n\n // Handle translation voice if specified\n const translationVoice: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined);\n if (translationVoice !== undefined) {\n queryParams.voice = translationVoice;\n // Updated to match C++ implementation\n queryParams.features = \"requireVoice\";\n }\n }\n}\n"]}