Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility flow wait until done #913

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions docs/colang-2/examples/test_csl.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,37 +485,58 @@ async def test_wait_indefinitely():


@pytest.mark.asyncio
async def test_wait_until_done():
async def test_it_finished():
colang_code = """
# COLANG_START: test_wait_until_done
# COLANG_START: test_it_finished
import core

flow handle welcome
user said "hi"
flow bot greet
bot say "hello"

flow test0
sklinglernv marked this conversation as resolved.
Show resolved Hide resolved
user said "hi"
await UtteranceBotAction(script="hello") as $ref
await it finished $ref

flow test1
user said "hi"
await bot greet as $ref
await it finished $ref

flow test2
user said "hi"
start bot greet as $ref
send $ref.Stop()
await it finished $ref

flow main
start handle welcome as $ref
await test0
bot say "test0 success"

start test1 as $ref
match $ref.Finished()
bot say "test1 success"

while True
when wait until done $ref
bot say "greetings done"
break
or when user said "stop"
print "stopping flow"
send $ref.Stop()
start test2 as $ref
match $ref.Failed()
bot say "test2 success"

# COLANG_END: test_wait_until_done
# COLANG_END: test_it_finished
"""

test_script = """
# USAGE_START: test_wait_until_done
# USAGE_START: test_it_finished
> hi
hello
test0 success
> hi
hello
test1 success
> hi
hello
greetings done
> stop
greetings done
# USAGE_END: test_wait_until_done
Event: StopUtteranceBotAction
test2 success
# USAGE_END: test_it_finished
"""

await compare_interaction_with_test_script(test_script, colang_code)
Expand Down
31 changes: 25 additions & 6 deletions nemoguardrails/colang/v2_x/library/core.co
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,31 @@ flow await_flow_by_name $flow_name $new_flow_instance_uid = None
match FlowStarted(flow_id=$flow_name, flow_instance_uid=$new_flow_instance_uid) as $event_ref
match $event_ref.flow.Finished()

flow wait until done $ref
"""Wait until an action or flow is done (has finished or was stopped)."""
flow it finished $ref -> $finished_event_ref
"""Wait until a flow or action has finished."""

if type($ref) != "Action" and type($ref) != "FlowState"
log "flow `is done` only supports action and flow references."
$type = type($ref)
if $type != "Action" and $type != "FlowState":
log "flow `it finished` expects a flow or action reference as parameter."
abort

if $ref.status.value != "finished" and $ref.status.value != "stopped":
match $ref.Finished()
if $type == "FlowState" and $ref.status.value == "stopped":
schuellc-nvidia marked this conversation as resolved.
Show resolved Hide resolved
# We abort this flow since it will never finish
abort

if $ref.status.value != "finished":
match $ref.Finished() as $finished_event_ref

flow flow_failed $flow_ref -> $failed_event_ref
sklinglernv marked this conversation as resolved.
Show resolved Hide resolved
"""Wait until a flow has failed."""

if type($flow_ref) != "FlowState":
log "flow `flow_failed` expects a flow reference as parameter."
abort

if $flow_ref.status.value == "finished":
# We abort this flow since it will never fail
abort

if $flow_ref.status.value != "stopped":
match $flow_ref.Failed() as $failed_event_ref