match / case
match / case is good for mapping values to handlers. The default is hit_first, and you can switch to hit_all for multi-tag routing.
Scenario: fixed value routing
python
from agently import TriggerFlow, TriggerFlowEventData
flow = TriggerFlow()
@flow.chunk
def input_level(_: TriggerFlowEventData):
return "medium"
@flow.chunk
def low(_: TriggerFlowEventData):
return "priority: low"
@flow.chunk
def medium(_: TriggerFlowEventData):
return "priority: medium"
@flow.chunk
def high(_: TriggerFlowEventData):
return "priority: high"
@flow.chunk
def unknown(_: TriggerFlowEventData):
return "priority: unknown"
@flow.chunk
def print_result(data: TriggerFlowEventData):
print(data.value)
(
flow.to(input_level)
.match()
.case("low")
.to(low)
.case("medium")
.to(medium)
.case("high")
.to(high)
.case_else()
.to(unknown)
.end_match()
.to(print_result)
)
flow.start(wait_for_result=False)Output:
text
priority: mediumScenario: case with a condition function
python
from agently import TriggerFlow, TriggerFlowEventData
flow = TriggerFlow()
@flow.chunk
def input_value(_: TriggerFlowEventData):
return 3
@flow.chunk
def neg(_: TriggerFlowEventData):
return "neg"
@flow.chunk
def small(_: TriggerFlowEventData):
return "small"
@flow.chunk
def big(_: TriggerFlowEventData):
return "big"
@flow.chunk
def print_result(data: TriggerFlowEventData):
print(data.value)
(
flow.to(input_value)
.match()
.case(lambda d: d.value < 0)
.to(neg)
.case(lambda d: d.value < 5)
.to(small)
.case_else()
.to(big)
.end_match()
.to(print_result)
)
flow.start(wait_for_result=False)Output:
text
smallScenario: mode="hit_all" multi-tag routing
python
from agently import TriggerFlow, TriggerFlowEventData
flow = TriggerFlow()
@flow.chunk
def input_value(_: TriggerFlowEventData):
return 3
@flow.chunk
def small(_: TriggerFlowEventData):
return "small"
@flow.chunk
def odd(_: TriggerFlowEventData):
return "odd"
@flow.chunk
def print_result(data: TriggerFlowEventData):
print(data.value)
(
flow.to(input_value)
.match(mode="hit_all")
.case(lambda d: d.value < 5)
.to(small)
.case(lambda d: d.value % 2 == 1)
.to(odd)
.end_match()
.to(print_result)
)
flow.start(wait_for_result=False)Output:
text
['small', 'odd']