just-ai/jaicf-kotlin

Extend generic types to hooks

Denire opened this issue · 1 comments

Version 0.10.0 brought type-specific actions, e.g.:

action(telegram) { // safe-casts request and reactions type to TelegramBotRequest, TelegramReactions
    reactions.document("some-url") // use smartcasted method
}

Version 0.13.0 brought improved DSL, so we can set generic type for whole scenario.

val bot = Scenario(telegram) {
    state("state") {
        action {
            reactions.say("You said: ${request.input}", "button1", "button2")
        }
    }
}

This works great, but the only thing missing is type-casts in hooks. This issue suggests extend generic types and smart-casts to hooks. Expected DSL, generic types in handle:

handle<BeforeActionHook>(telegram) { // declare token same as in action
    reactions.document("...") // use smart-casted reactions/request/activator same as in action
}

, and generic types propagation from Scenario:

val bot = Scenario(telegram) {
    handle<BeforeActionHook> { // handle uses propagated request/reactions type
        reactions.document("...")
    }
}

As it turns out, it's impossible to create the DSL we expected

handle<BeforeActionHook>(telegram){ ... }

because ActionHook class becomes generic and there is no way to pass generic type from type token to type declaration in hadle method.

The second solution can be that we create methods like

handleActionError { ... }
handleAnyError { ... }
handleBeforeAction { .. }
// etc

This will bring handler types closer to dsl and allow us to use type tokens inside them.