44"""
55
66import re
7+ import uuid
8+ from hashlib import md5
79from typing import Any , Dict , List , Tuple
810import inspect
911
1315 raise ImportError ("burr package is not installed. Please install it with 'pip install scrapegraphai[burr]'" )
1416
1517from burr import tracking
16- from burr .core import Application , ApplicationBuilder , State , Action , default
18+ from burr .core import Application , ApplicationBuilder , State , Action , default , ApplicationContext
1719from burr .lifecycle import PostRunStepHook , PreRunStepHook
1820
1921
@@ -55,7 +57,7 @@ def writes(self) -> list[str]:
5557
5658 def update (self , result : dict , state : State ) -> State :
5759 return state .update (** result )
58-
60+
5961 def get_source (self ) -> str :
6062 return inspect .getsource (self .node .__class__ )
6163
@@ -100,13 +102,12 @@ class BurrBridge:
100102 def __init__ (self , base_graph , burr_config ):
101103 self .base_graph = base_graph
102104 self .burr_config = burr_config
103- self .project_name = burr_config .get ("project_name" , "default-project" )
104- self .tracker = tracking .LocalTrackingClient (project = self .project_name )
105+ self .project_name = burr_config .get ("project_name" , "scrapegraph: {}" )
105106 self .app_instance_id = burr_config .get ("app_instance_id" , "default-instance" )
106107 self .burr_inputs = burr_config .get ("inputs" , {})
107108 self .burr_app = None
108109
109- def _initialize_burr_app (self , initial_state : Dict [str , Any ] = {} ) -> Application :
110+ def _initialize_burr_app (self , initial_state : Dict [str , Any ] = None ) -> Application :
110111 """
111112 Initialize a Burr application from the base graph.
112113
@@ -116,24 +117,41 @@ def _initialize_burr_app(self, initial_state: Dict[str, Any] = {}) -> Applicatio
116117 Returns:
117118 Application: The Burr application instance.
118119 """
120+ if initial_state is None :
121+ initial_state = {}
119122
120123 actions = self ._create_actions ()
121124 transitions = self ._create_transitions ()
122125 hooks = [PrintLnHook ()]
123126 burr_state = State (initial_state )
124-
125- app = (
127+ application_context = ApplicationContext . get ()
128+ builder = (
126129 ApplicationBuilder ()
127130 .with_actions (** actions )
128131 .with_transitions (* transitions )
129132 .with_entrypoint (self .base_graph .entry_point )
130133 .with_state (** burr_state )
131- .with_identifiers (app_id = self .app_instance_id )
132- .with_tracker (self .tracker )
134+ .with_identifiers (app_id = str (uuid .uuid4 ())) # TODO -- grab this from state
133135 .with_hooks (* hooks )
134- .build ()
135136 )
136- return app
137+ if application_context is not None :
138+ builder = (
139+ builder
140+ # if we're using a tracker, we want to copy it/pass in
141+ .with_tracker (
142+ application_context .tracker .copy () if application_context .tracker is not None else None
143+ ) # remember to do `copy()` here!
144+ .with_spawning_parent (
145+ application_context .app_id ,
146+ application_context .sequence_id ,
147+ application_context .partition_key ,
148+ )
149+ )
150+ else :
151+ # This is the case in which nothing is spawning it
152+ # in this case, we want to create a new tracker from scratch
153+ builder = builder .with_tracker (tracking .LocalTrackingClient (project = self .project_name ))
154+ return builder .build ()
137155
138156 def _create_actions (self ) -> Dict [str , Any ]:
139157 """
0 commit comments