1919from . import _engine
2020from . import index
2121from . import op
22+ from . import setting
2223from .convert import dump_engine_object
2324from .typing import encode_enriched_type
2425from .runtime import execution_context
@@ -310,7 +311,7 @@ class _FlowBuilderState:
310311
311312 def __init__ (self , / , name : str | None = None ):
312313 flow_name = _flow_name_builder .build_name (name , prefix = "_flow_" )
313- self .engine_flow_builder = _engine .FlowBuilder (flow_name )
314+ self .engine_flow_builder = _engine .FlowBuilder (get_full_flow_name ( flow_name ) )
314315 self .field_name_builder = _NameBuilder ()
315316
316317 def get_data_slice (self , v : Any ) -> _engine .DataSlice :
@@ -481,7 +482,7 @@ def _render_spec(self, verbose: bool = False) -> Tree:
481482 Render the flow spec as a styled rich Tree with hierarchical structure.
482483 """
483484 spec = self ._get_spec (verbose = verbose )
484- tree = Tree (f"Flow: { self .name } " , style = "cyan" )
485+ tree = Tree (f"Flow: { self .full_name } " , style = "cyan" )
485486
486487 def build_tree (label : str , lines : list ):
487488 node = Tree (label , style = "bold magenta" if lines else "cyan" )
@@ -508,9 +509,9 @@ def __repr__(self):
508509 return repr (self ._lazy_engine_flow ())
509510
510511 @property
511- def name (self ) -> str :
512+ def full_name (self ) -> str :
512513 """
513- Get the name of the flow.
514+ Get the full name of the flow.
514515 """
515516 return self ._lazy_engine_flow ().name ()
516517
@@ -566,8 +567,16 @@ def _create_engine_flow() -> _engine.Flow:
566567_flows_lock = Lock ()
567568_flows : dict [str , Flow ] = {}
568569
570+ def get_full_flow_name (name : str ) -> str :
571+ """
572+ Get the full name of a flow.
573+ """
574+ return f"{ setting .get_app_namespace (trailing_delimiter = '.' )} { name } "
575+
569576def add_flow_def (name : str , fl_def : Callable [[FlowBuilder , DataScope ], None ]) -> Flow :
570577 """Add a flow definition to the cocoindex library."""
578+ if not all (c .isalnum () or c == '_' for c in name ):
579+ raise ValueError (f"Flow name '{ name } ' contains invalid characters. Only alphanumeric characters and underscores are allowed." )
571580 with _flows_lock :
572581 if name in _flows :
573582 raise KeyError (f"Flow with name { name } already exists" )
@@ -587,12 +596,12 @@ def flow_names() -> list[str]:
587596 with _flows_lock :
588597 return list (_flows .keys ())
589598
590- def flows () -> list [ Flow ]:
599+ def flows () -> dict [ str , Flow ]:
591600 """
592601 Get all flows.
593602 """
594603 with _flows_lock :
595- return list (_flows . values () )
604+ return dict (_flows )
596605
597606def flow_by_name (name : str ) -> Flow :
598607 """
@@ -605,14 +614,13 @@ def ensure_all_flows_built() -> None:
605614 """
606615 Ensure all flows are built.
607616 """
608- for fl in flows ():
609- fl .internal_flow ()
617+ execution_context .run (ensure_all_flows_built_async ())
610618
611619async def ensure_all_flows_built_async () -> None :
612620 """
613621 Ensure all flows are built.
614622 """
615- for fl in flows ():
623+ for fl in flows (). values () :
616624 await fl .internal_flow_async ()
617625
618626def update_all_flows (options : FlowLiveUpdaterOptions ) -> dict [str , _engine .IndexUpdateInfo ]:
@@ -626,13 +634,13 @@ async def update_all_flows_async(options: FlowLiveUpdaterOptions) -> dict[str, _
626634 Update all flows.
627635 """
628636 await ensure_all_flows_built_async ()
629- async def _update_flow (fl : Flow ) -> _engine .IndexUpdateInfo :
637+ async def _update_flow (name : str , fl : Flow ) -> tuple [ str , _engine .IndexUpdateInfo ] :
630638 async with FlowLiveUpdater (fl , options ) as updater :
631639 await updater .wait_async ()
632- return updater .update_stats ()
640+ return ( name , updater .update_stats () )
633641 fls = flows ()
634- all_stats = await asyncio .gather (* (_update_flow (fl ) for fl in fls ))
635- return { fl . name : stats for fl , stats in zip ( fls , all_stats )}
642+ all_stats = await asyncio .gather (* (_update_flow (name , fl ) for ( name , fl ) in fls . items () ))
643+ return dict ( all_stats )
636644
637645_transient_flow_name_builder = _NameBuilder ()
638646class TransientFlow :
0 commit comments