From 5e84b205127ff83cf48593e23a458b6b67973a88 Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Thu, 30 May 2024 14:49:58 +0300 Subject: [PATCH] add: set asr properties handler --- .../jaicp/reactions/TelephonyReactions.kt | 98 +++-------- .../handlers/SetAsrPropertiesHandler.kt | 164 ++++++++++++++++++ 2 files changed, 187 insertions(+), 75 deletions(-) create mode 100644 channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/handlers/SetAsrPropertiesHandler.kt diff --git a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/TelephonyReactions.kt b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/TelephonyReactions.kt index 99f474a5..ef753962 100755 --- a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/TelephonyReactions.kt +++ b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/TelephonyReactions.kt @@ -4,14 +4,13 @@ import com.justai.jaicf.channel.jaicp.channels.TelephonyChannel import com.justai.jaicf.channel.jaicp.dto.* import com.justai.jaicf.channel.jaicp.dto.bargein.* import com.justai.jaicf.channel.jaicp.dto.config.* +import com.justai.jaicf.channel.jaicp.reactions.handlers.SetAsrPropertiesHandler import com.justai.jaicf.helpers.http.toUrl import com.justai.jaicf.logging.AudioReaction import com.justai.jaicf.logging.SayReaction import com.justai.jaicf.logging.currentState import com.justai.jaicf.plugin.PathValue import com.justai.jaicf.reactions.Reactions -import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.JsonPrimitive import java.time.DayOfWeek import java.time.Duration import java.time.Instant @@ -30,6 +29,8 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J internal var asrConfig: AsrConfig? = null + private val setAsrPropertiesHandler = SetAsrPropertiesHandler() + companion object { private const val CURRENT_CONTEXT_PATH = "." } @@ -218,79 +219,10 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J * @param properties map of properties names with its assigned values. * */ fun setAsrProperties(properties: Map) { - val propertiesJson = JsonObject(properties.toMutableMap().mapValues { entry -> JsonPrimitive(entry.value) }) - asrConfig = (executionContext.request as TelephonyBotRequest).asrConfig - when (checkNotNull(asrConfig?.type)) { - AsrConfig.AsrProviderType.SBER -> { - val asrProviderConfig: AsrSberConfig = checkNotNull(asrConfig?.sber) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - sber = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.YANDEX -> { - val asrProviderConfig: AsrYandexConfig = checkNotNull(asrConfig?.yandex) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - yandex = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.GOOGLE -> { - val asrProviderConfig: AsrGoogleConfig = checkNotNull(asrConfig?.google) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - google = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.MTS -> { - val asrProviderConfig: AsrMtsConfig = checkNotNull(asrConfig?.mts) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - mts = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.ZITECH -> { - val asrProviderConfig: AsrZitechConfig = checkNotNull(asrConfig?.zitech) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - zitech = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.AIMYVOICE -> { - val asrProviderConfig: AsrAimyvoiceConfig = checkNotNull(asrConfig?.aimyvoice) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - aimyvoice = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.AZURE -> { - val asrProviderConfig: AsrAzureConfig = checkNotNull(asrConfig?.azure) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - azure = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.ASM -> { - val asrProviderConfig: AsrAsmConfig = checkNotNull(asrConfig?.asm) - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson, - asm = asrProviderConfig.copy(asrProperties = propertiesJson) - ) - } - - AsrConfig.AsrProviderType.KALDI, AsrConfig.AsrProviderType.TINKOFF -> { - asrConfig = asrConfig?.copy( - asrProperties = propertiesJson - ) - } - } + asrConfig = setAsrPropertiesHandler.handle( + properties, + mergeAsrConfigs(asrConfig, (executionContext.request as TelephonyBotRequest).asrConfig) + ) } /** @@ -552,4 +484,20 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J private fun ensureBargeInProps() { bargeIn = bargeIn ?: bargeInDefaultProps } + + private fun mergeAsrConfigs(firstAsrConfig: AsrConfig?, secondAsrConfig: AsrConfig?): AsrConfig { + return AsrConfig( + type = firstAsrConfig?.type ?: secondAsrConfig?.type, + yandex = firstAsrConfig?.yandex ?: secondAsrConfig?.yandex, + zitech = firstAsrConfig?.zitech ?: secondAsrConfig?.zitech, + google = firstAsrConfig?.google ?: secondAsrConfig?.google, + aimyvoice = firstAsrConfig?.aimyvoice ?: secondAsrConfig?.aimyvoice, + mts = firstAsrConfig?.mts ?: secondAsrConfig?.mts, + azure = firstAsrConfig?.azure ?: secondAsrConfig?.azure, + asm = firstAsrConfig?.asm ?: secondAsrConfig?.asm, + sber = firstAsrConfig?.sber ?: secondAsrConfig?.sber, + asrProperties = firstAsrConfig?.asrProperties ?: secondAsrConfig?.asrProperties, + tokenData = firstAsrConfig?.tokenData ?: secondAsrConfig?.tokenData + ) + } } diff --git a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/handlers/SetAsrPropertiesHandler.kt b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/handlers/SetAsrPropertiesHandler.kt new file mode 100644 index 00000000..47cd3fef --- /dev/null +++ b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/reactions/handlers/SetAsrPropertiesHandler.kt @@ -0,0 +1,164 @@ +package com.justai.jaicf.channel.jaicp.reactions.handlers + +import com.justai.jaicf.channel.jaicp.dto.config.* +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive + +class SetAsrPropertiesHandler { + + private val setAsrPropertiesHandlerSber = SetAsrPropertiesHandlerSber() + private val setAsrPropertiesHandlerYandex = SetAsrPropertiesHandlerYandex() + private val setAsrPropertiesHandlerGoogle = SetAsrPropertiesHandlerGoogle() + private val setAsrPropertiesHandlerMts = SetAsrPropertiesHandlerMts() + private val setAsrPropertiesHandlerZitech = SetAsrPropertiesHandlerZitech() + private val setAsrPropertiesHandlerAimyvoice = SetAsrPropertiesHandlerAimyvoice() + private val setAsrPropertiesHandlerAzure = SetAsrPropertiesHandlerAzure() + private val setAsrPropertiesHandlerAsm = SetAsrPropertiesHandlerAsm() + private val setAsrPropertiesHandlerKaldi = SetAsrPropertiesHandlerKaldi() + private val setAsrPropertiesHandlerTinkoff = SetAsrPropertiesHandlerTinkoff() + + fun handle(properties: Map, asrConfig: AsrConfig): AsrConfig { + val propertiesJson = JsonObject(properties.toMutableMap().mapValues { entry -> JsonPrimitive(entry.value) }) + return when (checkNotNull(asrConfig.type)) { + AsrConfig.AsrProviderType.SBER -> { + setAsrPropertiesHandlerSber.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.YANDEX -> { + setAsrPropertiesHandlerYandex.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.GOOGLE -> { + setAsrPropertiesHandlerGoogle.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.MTS -> { + setAsrPropertiesHandlerMts.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.ZITECH -> { + setAsrPropertiesHandlerZitech.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.AIMYVOICE -> { + setAsrPropertiesHandlerAimyvoice.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.AZURE -> { + setAsrPropertiesHandlerAzure.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.ASM -> { + setAsrPropertiesHandlerAsm.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.TINKOFF -> { + setAsrPropertiesHandlerTinkoff.handle(asrConfig, propertiesJson) + } + + AsrConfig.AsrProviderType.KALDI -> { + setAsrPropertiesHandlerKaldi.handle(asrConfig, propertiesJson) + } + } + } +} + +abstract class SetAsrPropertiesHandlerAbstract { + abstract fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig +} + +class SetAsrPropertiesHandlerSber() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrSberConfig = checkNotNull(asrConfig.sber) + return asrConfig.copy( + asrProperties = propertiesJson, + sber = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerYandex() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrYandexConfig = checkNotNull(asrConfig.yandex) + return asrConfig.copy( + asrProperties = propertiesJson, + yandex = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerGoogle() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrGoogleConfig = checkNotNull(asrConfig.google) + return asrConfig.copy( + asrProperties = propertiesJson, + google = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerMts() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrMtsConfig = checkNotNull(asrConfig.mts) + return asrConfig.copy( + asrProperties = propertiesJson, + mts = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerZitech() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrZitechConfig = checkNotNull(asrConfig.zitech) + return asrConfig.copy( + asrProperties = propertiesJson, + zitech = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerAimyvoice() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrAimyvoiceConfig = checkNotNull(asrConfig.aimyvoice) + return asrConfig.copy( + asrProperties = propertiesJson, + aimyvoice = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerAzure() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrAzureConfig = checkNotNull(asrConfig.azure) + return asrConfig.copy( + asrProperties = propertiesJson, + azure = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerAsm() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + val asrProviderConfig: AsrAsmConfig = checkNotNull(asrConfig.asm) + return asrConfig.copy( + asrProperties = propertiesJson, + asm = asrProviderConfig.copy(asrProperties = propertiesJson) + ) + } +} + +class SetAsrPropertiesHandlerKaldi() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + return asrConfig.copy( + asrProperties = propertiesJson + ) + } +} + +class SetAsrPropertiesHandlerTinkoff() : SetAsrPropertiesHandlerAbstract() { + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { + return asrConfig.copy( + asrProperties = propertiesJson + ) + } +} \ No newline at end of file