Add `condition` to `halt_after` / `halt_before`
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 195
- Avg merge
- 6d 10h
- Merged PRs (30d)
- 17
Description
**Is your feature request related to a problem? Please describe.**
Currently, it is not possible to halt the app after a given action iff a `condition` is met. Here is a motivating example of when it might be needed:
Say you have a chatgpt-like app with various "chats", where each chat is a distinct burr app. When a user starts a new chat, you'd like to have a quick routing step that will detect, based on the first prompt, if the new chat is actually the continuation of a previous chat. If so, we want to halt the current app and restart the relevant app. If not, the app can continue its normal lifecycle.
**Describe the solution you'd like**
Instead of providing a list of actions to `halt_after` (or `halt_before`), provide a list of tuples `(action, condition)`. The application will halt after a given `action` if and only if `condition` is met. If no condition is provided, the app will always halt after `action` is processed (current behavior).
**Describe alternatives you've considered**
Right now, the alternative is to have a dummy action to halt on and a conditional transition to it.
```py
with_transitions(
("routing_action", "continue_action", when(continue=True)),
("routing_action, "halt_action", when(continue=False))
)
```
And then when calling `app.run`:
```py
final_action, result, final_state = app.run(
halt_after=["halt_action"],
inputs={"prompt" : user_input}
)
```
This is less clean than the suggested API.
Another possible way to address this would be to enable halting the application from within an action, although it may be harder to implement.
Contributor guide
Assessment
This issue has not been assessed yet.