microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 10.3 kB
Source Map (JSON)
{"version":3,"sources":["src/common.speech/Transcription/ConversationManager.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,kBAAkB,EAAc,MAAM,sBAAsB,CAAC;AAItE,qBAAa,mBAAmB;IAE5B,OAAO,CAAC,iBAAiB,CAAc;IACvC,OAAO,CAAC,UAAU,CAAiB;IACnC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,eAAe,CAAqB;;IAY5C;;;;;;OAMG;IACI,YAAY,CAAC,IAAI,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAuG/H;;;;;OAKG;IACI,KAAK,CAAC,IAAI,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CA+C9E","file":"ConversationManager.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n IRequestOptions,\r\n IRestParams,\r\n IRestResponse,\r\n RestMessageAdapter,\r\n RestRequestType\r\n} from \"../../common.browser/Exports.js\";\r\nimport { IErrorMessages, IStringDictionary } from \"../../common/Exports.js\";\r\nimport { Contracts } from \"../../sdk/Contracts.js\";\r\nimport { PropertyCollection, PropertyId } from \"../../sdk/Exports.js\";\r\nimport { ConversationConnectionConfig } from \"./ConversationConnectionConfig.js\";\r\nimport { IConversationResponseError, IInternalConversation } from \"./ConversationTranslatorInterfaces.js\";\r\n\r\nexport class ConversationManager {\r\n\r\n private privRequestParams: IRestParams;\r\n private privErrors: IErrorMessages;\r\n private privHost: string;\r\n private privApiVersion: string;\r\n private privRestPath: string;\r\n private privRestAdapter: RestMessageAdapter;\r\n\r\n public constructor() {\r\n //\r\n this.privRequestParams = ConversationConnectionConfig.configParams;\r\n this.privErrors = ConversationConnectionConfig.restErrors;\r\n this.privHost = ConversationConnectionConfig.host;\r\n this.privApiVersion = ConversationConnectionConfig.apiVersion;\r\n this.privRestPath = ConversationConnectionConfig.restPath;\r\n this.privRestAdapter = new RestMessageAdapter({});\r\n }\r\n\r\n /**\r\n * Make a POST request to the Conversation Manager service endpoint to create or join a conversation.\r\n * @param args\r\n * @param conversationCode\r\n * @param callback\r\n * @param errorCallback\r\n */\r\n public createOrJoin(args: PropertyCollection, conversationCode: string, cb?: (c: any) => void, err?: (e: string) => void): void {\r\n\r\n try {\r\n\r\n Contracts.throwIfNullOrUndefined(args, \"args\");\r\n\r\n const languageCode: string = args.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage, ConversationConnectionConfig.defaultLanguageCode);\r\n const nickname: string = args.getProperty(PropertyId.ConversationTranslator_Name, \"conversation_host\");\r\n const endpointHost: string = args.getProperty(PropertyId.ConversationTranslator_Host, this.privHost);\r\n const correlationId: string = args.getProperty(PropertyId.ConversationTranslator_CorrelationId);\r\n const subscriptionKey: string = args.getProperty(PropertyId.SpeechServiceConnection_Key);\r\n const subscriptionRegion: string = args.getProperty(PropertyId.SpeechServiceConnection_Region);\r\n const authToken: string = args.getProperty(PropertyId.SpeechServiceAuthorization_Token);\r\n\r\n Contracts.throwIfNullOrWhitespace(languageCode, \"languageCode\");\r\n Contracts.throwIfNullOrWhitespace(nickname, \"nickname\");\r\n Contracts.throwIfNullOrWhitespace(endpointHost, \"endpointHost\");\r\n\r\n const queryParams: IStringDictionary<string> = {};\r\n queryParams[this.privRequestParams.apiVersion] = this.privApiVersion;\r\n queryParams[this.privRequestParams.languageCode] = languageCode;\r\n queryParams[this.privRequestParams.nickname] = nickname;\r\n\r\n const headers: IStringDictionary<string> = {};\r\n if (correlationId) {\r\n headers[this.privRequestParams.correlationId] = correlationId;\r\n }\r\n headers[this.privRequestParams.clientAppId] = ConversationConnectionConfig.clientAppId;\r\n\r\n if (conversationCode !== undefined) {\r\n queryParams[this.privRequestParams.roomId] = conversationCode;\r\n } else {\r\n Contracts.throwIfNullOrUndefined(subscriptionRegion, this.privErrors.authInvalidSubscriptionRegion);\r\n headers[this.privRequestParams.subscriptionRegion] = subscriptionRegion;\r\n if (subscriptionKey) {\r\n headers[this.privRequestParams.subscriptionKey] = subscriptionKey;\r\n } else if (authToken) {\r\n headers[this.privRequestParams.authorization] = `Bearer ${authToken}`;\r\n } else {\r\n Contracts.throwIfNullOrUndefined(subscriptionKey, this.privErrors.authInvalidSubscriptionKey);\r\n }\r\n }\r\n\r\n const config: IRequestOptions = {};\r\n config.headers = headers;\r\n this.privRestAdapter.options = config;\r\n\r\n const endpoint: string = `https://${endpointHost}${this.privRestPath}`;\r\n\r\n // TODO: support a proxy and certificate validation\r\n this.privRestAdapter.request(RestRequestType.Post, endpoint, queryParams, null).then((response: IRestResponse): void => {\r\n\r\n const requestId: string = RestMessageAdapter.extractHeaderValue(this.privRequestParams.requestId, response.headers);\r\n\r\n if (!response.ok) {\r\n if (!!err) {\r\n // get the error\r\n let errorMessage: string = this.privErrors.invalidCreateJoinConversationResponse.replace(\"{status}\", response.status.toString());\r\n let errMessageRaw: IConversationResponseError;\r\n try {\r\n errMessageRaw = JSON.parse(response.data) as IConversationResponseError;\r\n errorMessage += ` [${errMessageRaw.error.code}: ${errMessageRaw.error.message}]`;\r\n } catch (e) {\r\n errorMessage += ` [${response.data}]`;\r\n }\r\n if (requestId) {\r\n errorMessage += ` ${requestId}`;\r\n }\r\n\r\n err(errorMessage);\r\n }\r\n return;\r\n }\r\n const conversation: IInternalConversation = JSON.parse(response.data) as IInternalConversation;\r\n if (conversation) {\r\n conversation.requestId = requestId;\r\n }\r\n if (!!cb) {\r\n try {\r\n cb(conversation);\r\n } catch (e) {\r\n if (!!err) {\r\n err(e as string);\r\n }\r\n }\r\n cb = undefined;\r\n }\r\n // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n }).catch( (): void => { });\r\n\r\n } catch (error) {\r\n if (!!err) {\r\n if (error instanceof Error) {\r\n const typedError: Error = error;\r\n err(typedError.name + \": \" + typedError.message);\r\n\r\n } else {\r\n err(error as string);\r\n }\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Make a DELETE request to the Conversation Manager service endpoint to leave the conversation.\r\n * @param args\r\n * @param sessionToken\r\n * @param callback\r\n */\r\n public leave(args: PropertyCollection, sessionToken: string): Promise<void> {\r\n return new Promise<void>((resolve: () => void, reject: (reason: string) => void): void => {\r\n try {\r\n\r\n Contracts.throwIfNullOrUndefined(args, this.privErrors.invalidArgs.replace(\"{arg}\", \"config\"));\r\n Contracts.throwIfNullOrWhitespace(sessionToken, this.privErrors.invalidArgs.replace(\"{arg}\", \"token\"));\r\n\r\n const endpointHost: string = args.getProperty(PropertyId.ConversationTranslator_Host, this.privHost);\r\n const correlationId: string = args.getProperty(PropertyId.ConversationTranslator_CorrelationId);\r\n\r\n const queryParams: IStringDictionary<string> = {};\r\n queryParams[this.privRequestParams.apiVersion] = this.privApiVersion;\r\n queryParams[this.privRequestParams.sessionToken] = sessionToken;\r\n\r\n const headers: IStringDictionary<string> = {};\r\n if (correlationId) {\r\n headers[this.privRequestParams.correlationId] = correlationId;\r\n }\r\n\r\n const config: IRequestOptions = {};\r\n config.headers = headers;\r\n this.privRestAdapter.options = config;\r\n\r\n const endpoint: string = `https://${endpointHost}${this.privRestPath}`;\r\n\r\n // TODO: support a proxy and certificate validation\r\n this.privRestAdapter.request(RestRequestType.Delete, endpoint, queryParams, null).then((response: IRestResponse): void => {\r\n\r\n if (!response.ok) {\r\n // ignore errors on delete\r\n }\r\n\r\n resolve();\r\n // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n }).catch( (): void => {});\r\n\r\n } catch (error) {\r\n if (error instanceof Error) {\r\n const typedError: Error = error;\r\n reject(typedError.name + \": \" + typedError.message);\r\n\r\n } else {\r\n reject(error as string);\r\n }\r\n }\r\n });\r\n }\r\n}\r\n"]}