From 19d82a228289be88bb323891641d1ce542f48d45 Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Wed, 15 May 2024 12:35:39 +0300 Subject: [PATCH 1/8] feature: add set asr properties telephony reaction --- .../channel/jaicp/dto/config/AsrConfig.kt | 5 ++++- .../jaicp/reactions/TelephonyReactions.kt | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt index da49afaa..e997906b 100644 --- a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt +++ b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt @@ -2,6 +2,7 @@ package com.justai.jaicf.channel.jaicp.dto.config import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject /** * Configuration parameters for the automatic speech recognition (ASR) provider used in a telephone channel. @@ -18,6 +19,7 @@ import kotlinx.serialization.Serializable * @param azure configuration options for the Azure ASR provider, if used. * @param asm configuration options for the ASM ASR provider, if used. * @param sber configuration options for the Sber ASR provider, if used. + * @param asrProperties special properties used with selected ASR, if used. **/ @Serializable data class AsrConfig( @@ -30,7 +32,8 @@ data class AsrConfig( val mts: AsrMtsConfig? = null, val azure: AsrAzureConfig? = null, val asm: AsrAsmConfig? = null, - val sber: AsrSberConfig? = null + val sber: AsrSberConfig? = null, + val asrProperties: JsonObject? = null ) { @Serializable enum class AsrProviderType { 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 276bda16..3b24e7d7 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 @@ -10,6 +10,8 @@ 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 @@ -201,6 +203,25 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J ) } + /** + * This method overrides the ASR properties of the ASR used for the current call. + * example usage: + * ``` + * state("asr") { + * action { + * reactions.telephony?.setAsrProperties( + * mapOf("enable_profanity_filter" to "true") + * ) + * } + * } + * ``` + * @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 = asrConfig?.copy(asrProperties = propertiesJson) + } + /** * Schedules a redial in outbound call campaign. * From e1043b8dcd7463995ac8884593638783f4e58f2b Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Thu, 16 May 2024 14:15:59 +0300 Subject: [PATCH 2/8] feature: add asr properties in asr provider config --- .../channel/jaicp/dto/config/AsrConfig.kt | 26 ++++--- .../jaicp/reactions/TelephonyReactions.kt | 69 ++++++++++++++++++- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt index e997906b..c64390dc 100644 --- a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt +++ b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt @@ -74,18 +74,22 @@ data class AsrConfig( * Subclasses contain provider-specific settings that are used to configure the ASR provider for the current session. **/ @Serializable -sealed class AsrProviderConfig +sealed class AsrProviderConfig{ + abstract val asrProperties: JsonObject? +} @Serializable data class AsrGoogleConfig( val model: String? = null, - val lang: String? = null + val lang: String? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable data class AsrZitechConfig( val model: String? = null, - val lang: String? = null + val lang: String? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable @@ -93,21 +97,24 @@ data class AsrYandexConfig( val model: String? = null, val lang: String? = null, val numbersAsWords: Boolean? = null, - val sensitivityReduction: Boolean? = null + val sensitivityReduction: Boolean? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable data class AsrMtsConfig( val model: String? = null, val lang: String? = null, - val transferType: String? = null + val transferType: String? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable data class AsrAimyvoiceConfig( val codec: String? = null, val mode: String? = null, - val grammarFileNames: String? = null + val grammarFileNames: String? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable @@ -115,17 +122,20 @@ data class AsrAzureConfig( val language: String? = null, val outputFormat: String? = null, val profanityOption: String? = null, - val enableDictation: Boolean? = null + val enableDictation: Boolean? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable data class AsrAsmConfig( val sampleRate: Long? = null, val model: String? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() @Serializable data class AsrSberConfig( val language: String? = null, - val model: String? = null + val model: String? = null, + override val asrProperties: JsonObject? = null ) : AsrProviderConfig() \ No newline at end of file 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 3b24e7d7..3f125e7f 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 @@ -219,7 +219,74 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J * */ fun setAsrProperties(properties: Map) { val propertiesJson = JsonObject(properties.toMutableMap().mapValues { entry -> JsonPrimitive(entry.value) }) - asrConfig = asrConfig?.copy(asrProperties = propertiesJson) + 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 -> {} + } } /** From 2d80c1cb75ba5018fe3e135048458941caa1d0ac Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Fri, 17 May 2024 12:32:20 +0300 Subject: [PATCH 3/8] feature: support asr properties for tinkoff asr --- .../jaicf/channel/jaicp/reactions/TelephonyReactions.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 3f125e7f..99f474a5 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 @@ -285,7 +285,11 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J ) } - AsrConfig.AsrProviderType.KALDI, AsrConfig.AsrProviderType.TINKOFF -> {} + AsrConfig.AsrProviderType.KALDI, AsrConfig.AsrProviderType.TINKOFF -> { + asrConfig = asrConfig?.copy( + asrProperties = propertiesJson + ) + } } } From 0e2aa382f733026d96c4a251790c663694f7d18d Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Fri, 17 May 2024 13:05:45 +0300 Subject: [PATCH 4/8] fix: added token data to asr config --- .../com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt index c64390dc..ab773bc3 100644 --- a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt +++ b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt @@ -33,7 +33,8 @@ data class AsrConfig( val azure: AsrAzureConfig? = null, val asm: AsrAsmConfig? = null, val sber: AsrSberConfig? = null, - val asrProperties: JsonObject? = null + val asrProperties: JsonObject? = null, + val tokenData: JsonObject? = null ) { @Serializable enum class AsrProviderType { From f672603fa98352609e07a943c585cbecd289c09a Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Fri, 17 May 2024 16:05:46 +0300 Subject: [PATCH 5/8] bump version --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 06352597..a4aa9b17 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { allprojects { group = "com.just-ai.jaicf" - version = "1.3.6" + version = "1.3.7" repositories { mavenCentral() From c379103f001d1f5fd1000b89f94b5efaa12a4e3c Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Fri, 17 May 2024 17:51:55 +0300 Subject: [PATCH 6/8] docs: update asr config doc params --- .../com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt index ab773bc3..270e3a37 100644 --- a/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt +++ b/channels/jaicp/src/main/kotlin/com/justai/jaicf/channel/jaicp/dto/config/AsrConfig.kt @@ -20,6 +20,7 @@ import kotlinx.serialization.json.JsonObject * @param asm configuration options for the ASM ASR provider, if used. * @param sber configuration options for the Sber ASR provider, if used. * @param asrProperties special properties used with selected ASR, if used. + * @param tokenData data used for ASR auth, if used. **/ @Serializable data class AsrConfig( From 5e84b205127ff83cf48593e23a458b6b67973a88 Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Thu, 30 May 2024 14:49:58 +0300 Subject: [PATCH 7/8] 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 From e7864b9e001790db4b16f3f3b77e0d8ed190678e Mon Sep 17 00:00:00 2001 From: Tagir Fayzullin Date: Fri, 31 May 2024 17:31:15 +0300 Subject: [PATCH 8/8] refactor: SetAsrPropertiesHandler config handling --- .../jaicp/reactions/TelephonyReactions.kt | 17 ++- .../handlers/SetAsrPropertiesHandler.kt | 113 ++++++++---------- 2 files changed, 63 insertions(+), 67 deletions(-) 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 ef753962..e6365de5 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,7 +4,7 @@ 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.channel.jaicp.reactions.handlers.* import com.justai.jaicf.helpers.http.toUrl import com.justai.jaicf.logging.AudioReaction import com.justai.jaicf.logging.SayReaction @@ -29,7 +29,20 @@ class TelephonyReactions(private val bargeInDefaultProps: BargeInProperties) : J internal var asrConfig: AsrConfig? = null - private val setAsrPropertiesHandler = SetAsrPropertiesHandler() + private val setAsrPropertiesHandler = SetAsrPropertiesHandler( + listOf( + SetAsrPropertiesHandlerSber(), + SetAsrPropertiesHandlerYandex(), + SetAsrPropertiesHandlerGoogle(), + SetAsrPropertiesHandlerMts(), + SetAsrPropertiesHandlerZitech(), + SetAsrPropertiesHandlerAimyvoice(), + SetAsrPropertiesHandlerAzure(), + SetAsrPropertiesHandlerAsm(), + SetAsrPropertiesHandlerKaldi(), + SetAsrPropertiesHandlerTinkoff(), + ) + ) companion object { private const val CURRENT_CONTEXT_PATH = "." 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 index 47cd3fef..b5f73ecf 100644 --- 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 @@ -4,70 +4,26 @@ 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() +class SetAsrPropertiesHandler( + private val listOfActualHandlers: List +) { 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) - } - } + return listOfActualHandlers.first { it.canHandle(checkNotNull(asrConfig.type)) } + .handle(asrConfig, propertiesJson) } } -abstract class SetAsrPropertiesHandlerAbstract { - abstract fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig +interface SetAsrPropertiesHandlerAbstract { + fun canHandle(type: AsrConfig.AsrProviderType): Boolean + fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig } -class SetAsrPropertiesHandlerSber() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerSber() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.SBER + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrSberConfig = checkNotNull(asrConfig.sber) return asrConfig.copy( @@ -77,7 +33,10 @@ class SetAsrPropertiesHandlerSber() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerYandex() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerYandex() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.YANDEX + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrYandexConfig = checkNotNull(asrConfig.yandex) return asrConfig.copy( @@ -87,7 +46,10 @@ class SetAsrPropertiesHandlerYandex() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerGoogle() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerGoogle() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.GOOGLE + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrGoogleConfig = checkNotNull(asrConfig.google) return asrConfig.copy( @@ -97,7 +59,10 @@ class SetAsrPropertiesHandlerGoogle() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerMts() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerMts() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.MTS + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrMtsConfig = checkNotNull(asrConfig.mts) return asrConfig.copy( @@ -107,7 +72,10 @@ class SetAsrPropertiesHandlerMts() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerZitech() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerZitech() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.ZITECH + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrZitechConfig = checkNotNull(asrConfig.zitech) return asrConfig.copy( @@ -117,7 +85,10 @@ class SetAsrPropertiesHandlerZitech() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerAimyvoice() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerAimyvoice() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.AIMYVOICE + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrAimyvoiceConfig = checkNotNull(asrConfig.aimyvoice) return asrConfig.copy( @@ -127,7 +98,10 @@ class SetAsrPropertiesHandlerAimyvoice() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerAzure() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerAzure() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.AZURE + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrAzureConfig = checkNotNull(asrConfig.azure) return asrConfig.copy( @@ -137,7 +111,10 @@ class SetAsrPropertiesHandlerAzure() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerAsm() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerAsm() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.ASM + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { val asrProviderConfig: AsrAsmConfig = checkNotNull(asrConfig.asm) return asrConfig.copy( @@ -147,7 +124,10 @@ class SetAsrPropertiesHandlerAsm() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerKaldi() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerKaldi() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.KALDI + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { return asrConfig.copy( asrProperties = propertiesJson @@ -155,7 +135,10 @@ class SetAsrPropertiesHandlerKaldi() : SetAsrPropertiesHandlerAbstract() { } } -class SetAsrPropertiesHandlerTinkoff() : SetAsrPropertiesHandlerAbstract() { +class SetAsrPropertiesHandlerTinkoff() : SetAsrPropertiesHandlerAbstract { + override fun canHandle(type: AsrConfig.AsrProviderType): Boolean = + type == AsrConfig.AsrProviderType.TINKOFF + override fun handle(asrConfig: AsrConfig, propertiesJson: JsonObject): AsrConfig { return asrConfig.copy( asrProperties = propertiesJson