From 4926deb385085d816319c73d5579109cb7cd71d3 Mon Sep 17 00:00:00 2001 From: Veronika Akhapkina Date: Wed, 21 Feb 2024 10:08:18 +0300 Subject: [PATCH] feature: added CailaCatchAllActivator to save cailaAnalyzeResult with catchAll activation --- .../activator/caila/CailaCatchAllActivator.kt | 33 +++++++++++++++++++ .../caila/CailaCatchAllActivatorContext.kt | 12 +++++++ .../activator/caila/CailaIntentActivator.kt | 1 + .../activator/catchall/CatchAllActivator.kt | 2 +- .../catchall/CatchAllActivatorContext.kt | 2 +- 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivator.kt create mode 100644 activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivatorContext.kt diff --git a/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivator.kt b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivator.kt new file mode 100644 index 000000000..32ad4b29c --- /dev/null +++ b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivator.kt @@ -0,0 +1,33 @@ +package com.justai.jaicf.activator.caila + +import com.justai.jaicf.activator.ActivatorFactory +import com.justai.jaicf.activator.caila.dto.CailaAnalyzeResponseData +import com.justai.jaicf.activator.catchall.CatchAllActivationRule +import com.justai.jaicf.activator.catchall.CatchAllActivator +import com.justai.jaicf.activator.catchall.CatchAllActivatorContext +import com.justai.jaicf.api.BotRequest +import com.justai.jaicf.api.hasQuery +import com.justai.jaicf.context.BotContext +import com.justai.jaicf.helpers.context.tempProperty + +import com.justai.jaicf.model.scenario.ScenarioModel + +class CailaCatchAllActivator(model: ScenarioModel) : CatchAllActivator(model) { + + override val name = "cailaCatchAllActivator" + + override fun canHandle(request: BotRequest) = request.hasQuery() + + override fun provideRuleMatcher(botContext: BotContext, request: BotRequest) = + ruleMatcher { + botContext.cailaAnalyzeResult?.let { result -> + CailaCatchAllActivatorContext(result) + } ?: CatchAllActivatorContext() + } + + companion object : ActivatorFactory { + override fun create(model: ScenarioModel) = CailaCatchAllActivator(model) + } +} + +var BotContext.cailaAnalyzeResult by tempProperty(removeOnNull = true) { null } diff --git a/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivatorContext.kt b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivatorContext.kt new file mode 100644 index 000000000..5c4fae677 --- /dev/null +++ b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaCatchAllActivatorContext.kt @@ -0,0 +1,12 @@ +package com.justai.jaicf.activator.caila + +import com.justai.jaicf.activator.caila.dto.CailaAnalyzeResponseData +import com.justai.jaicf.activator.catchall.CatchAllActivatorContext +import com.justai.jaicf.context.ActivatorContext + +class CailaCatchAllActivatorContext( + val result: CailaAnalyzeResponseData, +) : CatchAllActivatorContext() + +val ActivatorContext.cailaCatchAll + get() = this as? CailaCatchAllActivatorContext diff --git a/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaIntentActivator.kt b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaIntentActivator.kt index 760cbd3b7..06176cc50 100644 --- a/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaIntentActivator.kt +++ b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/CailaIntentActivator.kt @@ -37,6 +37,7 @@ open class CailaIntentActivator( override fun provideRuleMatcher(botContext: BotContext, request: BotRequest): ActivationRuleMatcher { val results = client.analyze(request.input) ?: return ActivationRuleMatcher { null } + botContext.cailaAnalyzeResult = results val intents = extractIntents(results).sortedByDescending { it.confidence } val intentMatcher = ruleMatcher { intents.firstOrNull(it.matches) } diff --git a/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivator.kt b/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivator.kt index 38856d579..e76659a58 100755 --- a/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivator.kt +++ b/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivator.kt @@ -28,7 +28,7 @@ import com.justai.jaicf.model.scenario.ScenarioModel * * @see CatchAllActivatorContext */ -class CatchAllActivator(model: ScenarioModel) : BaseActivator(model) { +open class CatchAllActivator(model: ScenarioModel) : BaseActivator(model) { override val name = "catchAllActivator" diff --git a/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivatorContext.kt b/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivatorContext.kt index c54800f59..9ea9ed403 100644 --- a/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivatorContext.kt +++ b/core/src/main/kotlin/com/justai/jaicf/activator/catchall/CatchAllActivatorContext.kt @@ -6,7 +6,7 @@ import com.justai.jaicf.context.StrictActivatorContext /** * Appears in the context of action block if [CatchAllActivator] handled the user's request. */ -class CatchAllActivatorContext: StrictActivatorContext() +open class CatchAllActivatorContext : StrictActivatorContext() val ActivatorContext.catchAll get() = this as? CatchAllActivatorContext \ No newline at end of file