Skip to content
Kavachकवच

ask<T>: Typed answers

● Intermediate3 min

The answer's type can hold anything a type can: true or false, text, numbers, lists and unions.

type Category = Billing | Bug | Question

type Triage {
    /// what the customer needs
    category: Category
    /// true if the customer can't use the product at all
    urgent: Bool
    /// a one-line summary for the support team
    summary: String
}

agent Inbox {
    can Ai, Console

    on message(text: String) ! Error {
        const t = try ask<Triage>("Sort this support message: {text}")
        match t.category {
            Category.Billing => print("to billing, urgent: {t.urgent}"),
            Category.Bug => print("to engineering, urgent: {t.urgent}"),
            Category.Question => print("to the help desk, urgent: {t.urgent}"),
        }
    }
}

fn main() {
    const bot = Inbox.start()
    bot.message("I was charged twice this month") catch e => print("failed: {e}")
}

The program ran and printed:

kavach run ask-triage.kav

to the help desk, urgent: true[dry run] ask("Sort this support message: I was charged twice this month") -> Triage(category: Question, urgent: true, summary: "fry ey")
ask-triage.kav: Kavach code, then what running it prints

Category is a union, so the model must pick one of its three cases, and match handles each. The model can't answer with a fourth team, because that answer wouldn't be a Triage.

The answers in a dry run are made up from the type, not real judgements: that is why this one sends a billing question to the help desk.