File size: 1,988 Bytes
d82bbe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# dataflow_agent/workflow/__init__.py

import importlib
from pathlib import Path

from .registry import RuntimeRegistry
from dataflow_agent.logger import get_logger

log = get_logger(__name__)

# ---- 1. 自动发现并导入所有工作流定义模块 ---------------------------------
# 遍历当前包目录下所有以 wf_*.py 命名的 Python 文件,并动态导入。
# 通过 importlib 以全限定名加载模块,从而确保每个工作流文件中的 @register 装饰器
# 能够在导入时将相应工作流注册到 RuntimeRegistry。
_pkg_path = Path(__file__).resolve().parent
for py in _pkg_path.glob("wf_*.py"):
    # importlib 需要模块的点分路径(dotted-path),例如 dataflow_agent.workflow.wf_xxx
    mod_name = f"{__name__}.{py.stem}"
    try:
        importlib.import_module(mod_name)
    except Exception as e:  # noqa: BLE001
        # Allow partial environments (e.g. HF Space) to import only the workflows
        # they need, without requiring every optional dependency.
        log.warning(f"[workflow] skip import {mod_name}: {e}")
# 模块导入后,各 wf_*.py 文件内的 @register 装饰器会自动注册工作流到 RuntimeRegistry

# ---- 2. 工作流的统一接口 ---------------------------------------------
def get_workflow(name: str):
    """
    根据工作流名称获取 create_pipeline_graph 工厂方法。

    Args:
        name (str): 工作流名称(注册名)

    Returns:
        Callable: 用于构建该工作流图的工厂函数
    """
    return RuntimeRegistry.get(name)

async def run_workflow(name: str, state):
    factory = get_workflow(name)
    graph_builder = factory()

    # graph = graph_builder.compile()
    graph = graph_builder.build()       

    return await graph.ainvoke(state)

# ---- 3. 工作流注册信息公开接口 -------------------------------------------
# 提供所有已注册工作流的列表,便于外部查询与 introspection
list_workflows = RuntimeRegistry.all