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
Adds PostApplicationCreate hook
elijahbenizzye41623cfa057
diff --git a/burr/core/application.py b/burr/core/application.pyindex 1dceefa7..e7b18c3b 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -188,6 +188,9 @@ class Application:self._state = stateself._adapter_set = adapter_set if adapter_set is not None else LifecycleAdapterSet()self._graph = self._create_graph()+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "post_application_create", state=self._state, application_graph=self._graph+ )def step(self) -> Optional[Tuple[Action, dict, State]]:"""Performs a single step, advancing the state machine along.diff --git a/burr/lifecycle/base.py b/burr/lifecycle/base.pyindex 390d86ab..43644713 100644--- a/burr/lifecycle/base.py+++ b/burr/lifecycle/base.py@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Any, Optional, Unionif TYPE_CHECKING:# type-checking-only for a circular import- from burr.core import State, Action+ from burr.core import State, Action, ApplicationGraphfrom burr.lifecycle.internal import lifecycle@@ -88,6 +88,25 @@ class PostRunStepHookAsync(abc.ABC):pass[email protected]_hook("post_application_create")+class PostApplicationCreateHook(abc.ABC):+ """Synchronous hook that runs post instantiation of an ``Application``+ object (after ``.build()`` is called on the ``ApplicationBuilder`` object.)"""++ @abc.abstractmethod+ def post_application_create(+ self, *, state: "State", application_graph: "ApplicationGraph", **future_kwargs: Any+ ):+ """Runs after an "application" object is instantiated. This is run by the Application, in its constructor,+ as the last step.++ :param state: Current state of the application+ :param application_graph: Application graph of the application, representing the state machine+ :param future_kwargs: Future keyword arguments for backwards compatibility+ """+ pass++# THESE ARE NOT IN USE# TODO -- implement/decide how to use them@lifecycle.base_hook("pre_run_application")@@ -133,4 +152,5 @@ LifecycleAdapter = Union[PreRunApplicationHookAsync,PostRunApplicationHook,PostRunApplicationHookAsync,+ PostApplicationCreateHook,]diff --git a/docs/reference/lifecycle.rst b/docs/reference/lifecycle.rstindex bf2778d7..7287d3b3 100644--- a/docs/reference/lifecycle.rst+++ b/docs/reference/lifecycle.rst@@ -19,6 +19,9 @@ and add instances to the application builder to customize your state machines's.. autoclass:: burr.lifecycle.base.PostRunStepHookAsync:members:+.. autoclass:: burr.lifecycle.base.PostApplicationCreateHook+ :members:+These hooks are available for you to use:.. autoclass:: burr.lifecycle.default.StateAndResultsFullLoggerdiff --git a/tests/core/test_application.py b/tests/core/test_application.pyindex 8f87c6b3..d1b111a7 100644--- a/tests/core/test_application.py+++ b/tests/core/test_application.py@@ -27,6 +27,7 @@ from burr.lifecycle import (PreRunStepHookAsync,internal,)+from burr.lifecycle.base import PostApplicationCreateHookclass PassedInAction(Action):@@ -545,7 +546,7 @@ def test_application_builder_unset():ApplicationBuilder().build()-def test_application_runs_hooks_sync():+def test_application_run_step_hooks_sync():class ActionTracker(PreRunStepHook, PostRunStepHook):def __init__(self):self.pre_called = []@@ -577,7 +578,7 @@ def test_application_runs_hooks_sync():assert len(tracker.post_called) == 11-async def test_application_runs_hooks_async():+async def test_application_run_step_hooks_async():class ActionTrackerAsync(PreRunStepHookAsync, PostRunStepHookAsync):def __init__(self):self.pre_called = []@@ -611,6 +612,34 @@ async def test_application_runs_hooks_async():assert len(tracker.post_called) == 11+def test_application_post_application_create_hook():+ class PostApplicationCreateTracker(PostApplicationCreateHook):+ def __init__(self):+ self.called_args = None+ self.call_count = 0++ def post_application_create(self, **kwargs):+ self.called_args = kwargs+ self.call_count += 1++ tracker = PostApplicationCreateTracker()+ counter_action = base_counter_action.with_name("counter")+ result_action = Result(fields=["counter"]).with_name("result")+ Application(+ actions=[counter_action, result_action],+ transitions=[+ Transition(counter_action, result_action, Condition.expr("counter >= 10")),+ Transition(counter_action, counter_action, default),+ ],+ state=State({}),+ initial_step="counter",+ adapter_set=internal.LifecycleAdapterSet(tracker),+ )+ assert "state" in tracker.called_args+ assert "application_graph" in tracker.called_args+ assert tracker.call_count == 1++async def test_application_gives_graph():counter_action = base_counter_action.with_name("counter")result_action = Result(fields=["counter"]).with_name("result")