feat: 完成input_context节点
This commit is contained in:
+17
-6
@@ -1,9 +1,12 @@
|
||||
import logging
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TypeVar
|
||||
from typing import TYPE_CHECKING, TypeVar
|
||||
|
||||
from app.config import Settings, settings
|
||||
from pydantic import ValidationError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.config import Settings
|
||||
from app.exceptions import AppError, ImportProcessError
|
||||
|
||||
T = TypeVar("T")
|
||||
@@ -12,18 +15,26 @@ T = TypeVar("T")
|
||||
class BaseNode(ABC):
|
||||
name: str = "base_node"
|
||||
|
||||
def __init__(self, config: Settings | None = None):
|
||||
"""传入配置时使用传入值,否则使用全局配置"""
|
||||
self.config = config if config is not None else settings
|
||||
def __init__(self, config: "Settings | None" = None):
|
||||
"""保存显式配置;全局配置在节点实际使用时才加载。"""
|
||||
self._config = config
|
||||
self.logger = logging.getLogger(f"import.{self.name}")
|
||||
|
||||
@property
|
||||
def config(self) -> "Settings":
|
||||
if self._config is None:
|
||||
from app.config import settings
|
||||
|
||||
self._config = settings
|
||||
return self._config
|
||||
|
||||
def __call__(self, state: T) -> T:
|
||||
"""执行节点,统一记录日志、耗时并保留业务异常类型"""
|
||||
start_time = time.perf_counter()
|
||||
self.logger.info("--- %s 开始 ---", self.name)
|
||||
try:
|
||||
result = self.process(state)
|
||||
except AppError:
|
||||
except (AppError, ValidationError):
|
||||
self.logger.exception("%s 执行失败", self.name)
|
||||
raise
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user