Skip to content

Commit

Permalink
add: set asr properties handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamakuprina committed May 30, 2024
1 parent c379103 commit 5e84b20
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = "."
}
Expand Down Expand Up @@ -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<String, String>) {
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)
)
}

/**
Expand Down Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String>, 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
)
}
}

0 comments on commit 5e84b20

Please sign in to comment.