Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Fixes sequence ID to work in all cases
elijahbenizzy4f5c674e3035
diff --git a/burr/core/application.py b/burr/core/application.pyindex d90028f0..57a8c490 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -244,9 +244,11 @@ class Application::return: Tuple[Function, dict, State] -- the function that was just ran, the result of running it, and the new state"""- out = self._step(inputs=inputs, _run_hooks=True)- self._increment_sequence_id()- return out+ try:+ out = self._step(inputs=inputs, _run_hooks=True)+ return out+ finally:+ self._increment_sequence_id()def _step(self, inputs: Optional[Dict[str, Any]] = None, _run_hooks: bool = True@@ -364,6 +366,7 @@ class Application:result = await _arun_function(next_action, self._state, inputs=inputs)new_state = _run_reducer(next_action, self._state, result, next_action.name)new_state = self.update_internal_state_value(new_state, next_action)+ self._set_state(new_state)except Exception as e:exc = elogger.exception(_format_error_message(next_action, self._state, inputs))@@ -372,8 +375,9 @@ class Application:await self._adapter_set.call_all_lifecycle_hooks_sync_and_async("post_run_step", action=next_action, state=new_state, result=result, exception=exc)- self._set_state(new_state)- self._increment_sequence_id()+ # we want to increment regardless of failure+ self._increment_sequence_id()+return next_action, result, new_statedef _clean_iterate_params(diff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex d5acad22..acf8d4c0 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -349,7 +349,9 @@ def test_app_step():state=State({}),initial_step="counter",)+ assert app.sequence_id == 0action, result, state = app.step()+ assert app.sequence_id == 1assert action.name == "counter"assert result == {"counter": 1}assert state[PRIOR_STEP] == "counter" # internal contract, not part of the public API@@ -417,7 +419,9 @@ async def test_app_astep():state=State({}),initial_step="counter_async",)+ assert app.sequence_id == 0action, result, state = await app.astep()+ assert app.sequence_id == 1assert action.name == "counter_async"assert result == {"counter": 1}assert state[PRIOR_STEP] == "counter_async" # internal contract, not part of the public API@@ -539,6 +543,7 @@ def test_iterate():action, result, state = generator_resultassert state["counter"] == 10assert result["counter"] == 10+ assert app.sequence_id == 11def test_iterate_with_inputs():@@ -577,16 +582,20 @@ async def test_aiterate():state=State({}),initial_step="counter",)+ assert app.sequence_id == 0gen = app.aiterate(halt_after=["result"])+ assert app.sequence_id == 0counter = 0# Note that we use an async-for loop cause the API is different, this doesn't# return anything (async generators are not allowed to).async for action, result, state in gen:+ print("si", app.sequence_id, action.name, state)if action.name == "counter":assert state["counter"] == result["counter"] == counter + 1counter = result["counter"]else:assert state["counter"] == result["counter"] == 10+ assert app.sequence_id == 11async def test_aiterate_halt_before():