/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import * as CLM from '@conversationlearner/models' ; import { PartialTrainDialog } from '../types/models' ; import { REPROMPT_SELF } from '../types/const' ; import Axios , { AxiosRequestConfig , AxiosResponse } from 'axios' ; import * as querystring from 'query-string' ; export interface ClientHeaders { botChecksum : string ; memoryKey : string ; } interface TypedAxiosResponse < T > extends AxiosResponse { data : T ; } interface IActionCreationResponse { actionId : string ; packageId : string ; trainingStatus : string ; } export type TrainDialogUpdateQueryParams = { ignoreLabelConflicts : boolean ; } ; export default class ClClient { getBaseUrl : ( ) => string ; defaultConfig : AxiosRequestConfig = { method : 'get' , headers : { 'Content-Type' : 'application/json' } } ; forceError : boolean = false ; getClientHeaders : ( ) => ClientHeaders ; constructor ( getBaseUrl , getClientHeaders , defaultHeaders ? , forceError = false ) { this . getBaseUrl = getBaseUrl ; this . getClientHeaders = getClientHeaders ; this . defaultConfig . headers = { ... this . defaultConfig . headers , ... defaultHeaders } ; this . forceError = forceError ; } send < T = any > ( config ) { if ( this . forceError ) { return Promise . reject ( new Error ( "Injected Error" ) ) ; } const clientHeaders = this . getClientHeaders ( ) ; const finalConfig = { ... this . defaultConfig , ... config } ; finalConfig . url = `${ this . getBaseUrl ( ) }${ config . url }` ; finalConfig . headers [ CLM . MEMORY_KEY_HEADER_NAME ] = clientHeaders . memoryKey ; finalConfig . headers [ CLM . BOT_CHECKSUM_HEADER_NAME ] = clientHeaders . botChecksum ; return Axios ( finalConfig ) as Promise < TypedAxiosResponse < T > > ; } async setApp ( app ) { await this . send ( { method : 'put' , url : `/state/app` , data : app } ) ; } async setConversationId ( userName , userId , conversationId ) { await this . send ( { method : 'put' , url : `/state/conversationId?userName=${ userName }&conversationId=${ conversationId }` , } ) ; } async getBotInfo ( browserId , appId ? ) { const response = await this . send < CLM . BotInfo > ( { url : `/bot?browserId=${ browserId }${ appId ? `&appId=${ appId }` : '' }` } ) ; return response . data ; } async apps ( userId ) { const response = await this . send < CLM . UIAppList > ( { url : `/apps?userId=${ userId }` } ) ; return response . data ; } async appGet ( appId ) { const response = await this . send < CLM . AppBase > ( { url : `/app/${ appId }` } ) ; return response . data ; } async appGetTrainingStatus ( appId ) { const response = await this . send < CLM . TrainingStatus > ( { url : `/app/${ appId }/trainingstatus` } ) ; return response . data ; } async appsCreate ( userId , appInput ) { const response = await this . send < CLM . AppBase > ( { method : 'post' , url : `/app?userId=${ userId }` , data : appInput } ) ; return response . data ; } async appCopy ( srcUserId , destUserId , appId ) { await this . send < string > ( { method : 'post' , url : `/apps/copy?srcUserId=${ srcUserId }&destUserId=${ destUserId }&appId=${ appId }` } ) ; } async appsDelete ( appId ) { await this . send ( { method : 'delete' , url : `/app/${ appId }` } ) ; } async appsUpdate ( appId , app ) { await this . send ( { method : 'put' , url : `/app/${ appId }` , data : app } ) ; return app ; } async appCreateTag ( appId , tagName , makeLive ) { const response = await this . send < CLM . AppBase > ( { method : 'put' , url : `/app/${ appId }/publish?version=${ tagName }&makeLive=${ makeLive }` } ) ; return response . data ; } async appSetLiveTag ( appId , tagName ) { const response = await this . send < CLM . AppBase > ( { method : 'post' , url : `/app/${ appId }/publish/${ tagName }` } ) ; return response . data ; } async appSetEditingTag ( appId , tagName ) { const response = await this . send < { [ appId ] : string ; } > ( { method : 'post' , url : `/app/${ appId }/edit/${ tagName }` } ) ; return response . data ; } async appExtract ( appId , userUtterances ) { const response = await this . send < CLM . ExtractResponse [ ] > ( { method : 'post' , url : `/app/${ appId }/extract` , data : userUtterances } ) ; return response . data ; } async entitiesGetById ( appId , entityId ) { const response = await this . send < CLM . EntityBase > ( { url : `/app/${ appId }/entity/${ entityId }` } ) ; return response . data ; } async entities ( appId ) { const response = await this . send < CLM . EntityList > ( { url : `/app/${ appId }/entities` } ) ; return response . data . entities ; } async entitiesCreate ( appId , entity ) { const response = await this . send < CLM . ChangeEntityResponse > ( { method : 'post' , url : `/app/${ appId }/entity` , data : entity } ) ; const changeEntityResponse = response . data ; entity . entityId = changeEntityResponse . entityId ; entity . negativeId = changeEntityResponse . negativeEntityId ; const newEntity = await this . entitiesGetById ( appId , entity . entityId ) ; Object . assign ( entity , newEntity ) ; return entity ; } async entitiesDelete ( appId , entityId ) { const response = await this . send ( { method : 'delete' , url : `/app/${ appId }/entity/${ entityId }` } ) ; return response . data ; } async entitiesDeleteValidation ( appId , packageId , entityId ) { const response = await this . send ( { method : 'get' , url : `/app/${ appId }/entity/${ entityId }/deleteValidation?packageId=${ packageId }` } ) ; return response . data ; } async entitiesUpdate ( appId , entity ) { const { version , packageCreationId , packageDeletionId , ... entityToSend } = entity ; const response = await this . send < CLM . ChangeEntityResponse > ( { method : 'put' , url : `/app/${ appId }/entity/${ entity . entityId }` , data : entityToSend } ) ; const changeEntityResponse = response . data ; entity . entityId = changeEntityResponse . entityId ; entity . negativeId = changeEntityResponse . negativeEntityId ; const newEntity = await this . entitiesGetById ( appId , entity . entityId ) ; Object . assign ( entity , newEntity ) ; return changeEntityResponse ; } async entitiesUpdateValidation ( appId , packageId , entity ) { const { version , packageCreationId , packageDeletionId , ... entityToSend } = entity ; const response = await this . send ( { method : 'post' , url : `/app/${ appId }/entity/${ entity . entityId }/editValidation?packageId=${ packageId }` , data : entityToSend } ) ; return response . data ; } async source ( appId , packageId ) { const response = await this . send < CLM . AppDefinitionChange > ( { url : `/app/${ appId }/source?packageId=${ packageId }` } ) ; return response . data ; } async sourcepost ( appId , source ) { const response = await this . send ( { method : 'post' , url : `/app/${ appId }/source` , data : source } ) ; return response . data ; } async actions ( appId ) { const response = await this . send < CLM . ActionList > ( { url : `/app/${ appId }/actions` } ) ; return response . data . actions ; } async actionsCreate ( appId , action ) { const response = await this . send < IActionCreationResponse > ( { method : 'post' , url : `/app/${ appId }/action` , data : action } ) ; action . actionId = response . data . actionId ; if ( action . repromptActionId === REPROMPT_SELF ) { action . repromptActionId = action . actionId ; } return action ; } async actionsDelete ( appId , actionId , removeFromDialogs ? ) { let url = `/app/${ appId }/action/${ actionId }` ; if ( removeFromDialogs ) { url += `?removeFromDialogs=true` ; } const response = await this . send ( { method : 'delete' , url } ) ; return response . data ; } async actionsDeleteValidation ( appId , packageId , actionId ) { const response = await this . send ( { method : 'get' , url : `/app/${ appId }/action/${ actionId }/deleteValidation?packageId=${ packageId }` } ) ; return response . data ; } async actionsUpdate ( appId , action ) { const { version , packageCreationId , packageDeletionId , ... actionToSend } = action ; const response = await this . send ( { method : 'put' , url : `/app/${ appId }/action/${ action . actionId }` , data : actionToSend } ) ; return response . data ; } async actionsUpdateValidation ( appId , packageId , action ) { const { version , packageCreationId , packageDeletionId , ... actionToSend } = action ; const response = await this . send ( { method : 'post' , url : `/app/${ appId }/action/${ action . actionId }/editValidation?packageId=${ packageId }` , data : actionToSend } ) ; return response . data ; } async trainDialogEdit ( appId , trainDialog , options ? ) { const queryString = querystring . stringify ( options ?? { } ) ; const response = await this . send < CLM . TrainResponse > ( { method : 'put' , url : `/app/${ appId }/traindialog/${ trainDialog . trainDialogId }?${ queryString }` , data : trainDialog } ) ; return response . data ; } async trainDialog ( appId , trainDialogId ) { const response = await this . send < CLM . TrainDialog > ( { url : `/app/${ appId }/traindialog/${ trainDialogId }` } ) ; return response . data ; } async trainDialogs ( appId ) { const response = await this . send < CLM . TrainDialogList > ( { url : `/app/${ appId }/traindialogs?includeDefinitions=false` } ) ; return response . data . trainDialogs ; } async trainDialogsCreate ( appId , trainDialog ) { const response = await this . send ( { method : 'post' , url : `/app/${ appId }/traindialog` , data : trainDialog } ) ; trainDialog . trainDialogId = response . data . trainDialogId ; return trainDialog ; } async trainDialogsDelete ( appId , trainDialogId ) { await this . send ( { method : 'delete' , url : `/app/${ appId }/traindialog/${ trainDialogId }` } ) ; } async trainDialogScoreFromTrainDialog ( appId , trainDialog ) { const response = await this . send < CLM . UIScoreResponse > ( { method : 'post' , url : `/app/${ appId }/scorefromtraindialog` , data : trainDialog } ) ; return response . data ; } async trainDialogExtractFromTrainDialog ( appId , trainDialog , userInput ) { const response = await this . send < CLM . ExtractResponse > ( { method : 'post' , url : `/app/${ appId }/extractfromtraindialog` , data : { trainDialog , userInput } } ) ; return response . data ; } async trainDialogReplay ( appId , trainDialog ) { const response = await this . send < CLM . TrainDialog > ( { method : 'post' , url : `/app/${ appId }/traindialogreplay` , data : trainDialog } ) ; return response . data ; } async trainDialogsUpdateExtractStep ( appId , trainDialogId , turnIndex , userInput ) { const response = await this . send ( { method : 'put' , url : `/app/${ appId }/traindialog/${ trainDialogId }/extractor/${ turnIndex }?includeDefinitions=true` , data : userInput } ) ; return response . data ; } async fetchTextVariationConflict ( appId , trainDialogId , textVariation , excludeDialogId ) { let url = `/app/${ appId }/traindialog/${ trainDialogId }/extractor/textvariation` ; if ( excludeDialogId ) { url = `${ url }?filteredDialog=${ excludeDialogId }` ; } const response = await this . send < CLM . ExtractResponse > ( { method : 'post' , url , data : textVariation } ) ; return response . data ; } async tutorials ( userId ) { const response = await this . send < CLM . UIAppList > ( { url : `/apps?userId=${ userId }` } ) ; return response . data . appList . apps ; } async history ( appId , trainDialog , userName , userId , useMarkdown ) { const response = await this . send < CLM . TeachWithActivities > ( { method : 'post' , url : `/app/${ appId }/history?username=${ userName }&userid=${ userId }&useMarkdown=${ useMarkdown }` , data : trainDialog } ) ; return response . data ; } async logDialog ( appId , logDialogId ) { const response = await this . send < CLM . LogDialog > ( { url : `/app/${ appId }/logdialog/${ logDialogId }` } ) ; return response . data ; } async logDialogs ( appId , packageIds , maxPageSize , continuationToken ? ) { const packages = packageIds . map ( p => `package=${ p }` ) . join ( "&" ) ; let url = `/app/${ appId }/logdialogs?includeDefinitions=false&excludeConverted=true&${ packages }` ; if ( maxPageSize ) { url += `&maxPageSize=${ maxPageSize }` ; } if ( continuationToken ) { url += `&continuationToken=${ encodeURIComponent ( continuationToken ) }` ; } const response = await this . send < CLM . LogQueryResult > ( { url } ) ; return response . data ; } async logDialogsCreate ( appId , logDialog ) { const response = await this . send < string > ( { method : 'post' , url : `/app/${ appId }/logdialog` , data : logDialog } ) ; logDialog . logDialogId = response . data ; return logDialog ; } async logDialogsDelete ( appId , logDialogId ) { await this . send ( { method : 'delete' , url : `/app/${ appId }/logdialog/${ logDialogId }` } ) ; } async logDialogsDeleteMany ( appId , logDialogIds ) { const logDialogIdQueryString = logDialogIds . map ( id => `id=${ id }` ) . join ( "&" ) ; await this . send ( { method : 'delete' , url : `/app/${ appId }/logdialog?${ logDialogIdQueryString }` } ) ; } async logDialogsUpdateExtractStep ( appId , logDialogId , turnIndex , userInput ) { const response = await this . send ( { method : 'put' , url : `/app/${ appId }/logdialog/${ logDialogId }/extractor/${ turnIndex }` , data : userInput } ) ; return response . data ; } async chatSessions ( appId ) { const response = await this . send < CLM . SessionList > ( { url : `/app/${ appId }/sessions` } ) ; return response . data . sessions ; } async chatSessionsCreate ( appId , sessionCreateParams ) { const response = await this . send < CLM . Session > ( { method : 'post' , url : `/app/${ appId }/session` , data : sessionCreateParams } ) ; return response . data ; } async chatSessionsExpire ( appId ) { await this . send < void > ( { method : 'put' , url : `/app/${ appId }/session` } ) ; } async chatSessionsDelete ( appId ) { await this . send ( { method : 'delete' , url : `/app/${ appId }/session` } ) ; } async teachSessions ( appId ) { const response = await this . send < CLM . TeachList > ( { url : `/app/${ appId }/teaches` } ) ; return response . data . teaches ; } async teachSessionCreate ( appId , initialFilledEntities = [ ] ) { const response = await this . send < CLM . TeachResponse > ( { method : 'post' , url : `/app/${ appId }/teach` , data : initialFilledEntities } ) ; return response . data ; } async teachSessionDelete ( appId , teachSession , save ) { const response = await this . send ( { method : 'delete' , url : `/app/${ appId }/teach/${ teachSession . teachId }?save=${ save }` } ) ; return response . data . trainDialogId ; } async teachSessionAddExtractStep ( appId , sessionId , userInput , filteredDialog ) { let url = `/app/${ appId }/teach/${ sessionId }/extractor` ; if ( filteredDialog ) { url = `${ url }?filteredDialog=${ filteredDialog }` ; } const response = await this . send ( { method : 'put' , url , data : userInput } ) ; return response . data ; } async teachSessionRescore ( appId , teachId , scoreInput ) { const response = await this . send < CLM . UIScoreResponse > ( { method : 'put' , url : `/app/${ appId }/teach/${ teachId }/rescore` , data : scoreInput } ) ; return response . data ; } async teachSessionAddScorerStep ( appId , teachId , uiTrainScorerStep ) { const response = await this . send < CLM . UIPostScoreResponse > ( { method : 'post' , url : `/app/${ appId }/teach/${ teachId }/scorer` , data : uiTrainScorerStep } ) ; return response . data ; } async teachSessionUpdateScorerStep ( appId , teachId , uiScoreInput ) { const response = await this . send < CLM . UIScoreResponse > ( { method : 'put' , url : `/app/${ appId }/teach/${ teachId }/scorer` , data : uiScoreInput } ) ; return response . data ; } async teachSessionFromBranch ( appId , trainDialogId , userName , userId , turnIndex ) { const response = await this . send < CLM . TeachWithActivities > ( { method : 'post' , url : `/app/${ appId }/traindialog/${ trainDialogId }/branch/${ turnIndex }?username=${ userName }&userid=${ userId }` } ) ; return response . data ; } async teachSessionFromTrainDialog ( appId , trainDialog , userInput , userName , userId , filteredDialog ) { let url = `/app/${ appId }/teachwithactivities?username=${ userName }&userid=${ userId }` ; if ( filteredDialog ) { url = `${ url }&filteredDialog=${ filteredDialog }` ; } const response = await this . send < CLM . TeachWithActivities > ( { method : 'post' , url , data : { trainDialog , userInput } } ) ; return response . data ; } async validateTranscript ( appId , packageId , testId , transcriptValidationTurns ) { const response = await this . send < string | null > ( { method : 'post' , url : `/app/${ appId }/validatetranscript?testId=${ testId }&packageId=${ packageId }` , data : transcriptValidationTurns } ) ; return response . data ; } async memoryDelete ( appId ) { await this . send ( { method : 'delete' , url : `/app/${ appId }/botmemory` } ) ; } }