style(service): workflow

This commit is contained in:
mengyonghao
2025-12-22 14:59:21 +08:00
parent 00f440f471
commit c15a987701
2 changed files with 110 additions and 109 deletions

View File

@@ -1,6 +1,7 @@
import uuid
import datetime import datetime
from typing import Optional, Any, List, Dict, TYPE_CHECKING import uuid
from typing import Optional, Any, List, Dict
from pydantic import BaseModel, Field, ConfigDict, field_serializer, field_validator from pydantic import BaseModel, Field, ConfigDict, field_serializer, field_validator
@@ -33,7 +34,6 @@ class KnowledgeRetrievalConfig(BaseModel):
reranker_top_k: int = Field(default=10, ge=0, le=1024, description="多知识库结果融合的模型参数") reranker_top_k: int = Field(default=10, ge=0, le=1024, description="多知识库结果融合的模型参数")
class ToolConfig(BaseModel): class ToolConfig(BaseModel):
"""工具配置""" """工具配置"""
enabled: bool = Field(default=False, description="是否启用该工具") enabled: bool = Field(default=False, description="是否启用该工具")
@@ -338,6 +338,7 @@ class DraftRunRequest(BaseModel):
"""试运行请求""" """试运行请求"""
message: str = Field(..., description="用户消息") message: str = Field(..., description="用户消息")
conversation_id: Optional[str] = Field(default=None, description="会话ID用于多轮对话") conversation_id: Optional[str] = Field(default=None, description="会话ID用于多轮对话")
conversation_vars: Optional[dict[str, Any]] = Field(default=None, description="会话变量")
user_id: Optional[str] = Field(default=None, description="用户ID用于会话管理") user_id: Optional[str] = Field(default=None, description="用户ID用于会话管理")
variables: Optional[Dict[str, Any]] = Field(default=None, description="自定义变量参数值") variables: Optional[Dict[str, Any]] = Field(default=None, description="自定义变量参数值")
stream: bool = Field(default=False, description="是否流式返回") stream: bool = Field(default=False, description="是否流式返回")

View File

@@ -39,14 +39,14 @@ class WorkflowService:
# ==================== 配置管理 ==================== # ==================== 配置管理 ====================
def create_workflow_config( def create_workflow_config(
self, self,
app_id: uuid.UUID, app_id: uuid.UUID,
nodes: list[dict[str, Any]], nodes: list[dict[str, Any]],
edges: list[dict[str, Any]], edges: list[dict[str, Any]],
variables: list[dict[str, Any]] | None = None, variables: list[dict[str, Any]] | None = None,
execution_config: dict[str, Any] | None = None, execution_config: dict[str, Any] | None = None,
triggers: list[dict[str, Any]] | None = None, triggers: list[dict[str, Any]] | None = None,
validate: bool = True validate: bool = True
) -> WorkflowConfig: ) -> WorkflowConfig:
"""创建工作流配置 """创建工作流配置
@@ -109,14 +109,14 @@ class WorkflowService:
return self.config_repo.get_by_app_id(app_id) return self.config_repo.get_by_app_id(app_id)
def update_workflow_config( def update_workflow_config(
self, self,
app_id: uuid.UUID, app_id: uuid.UUID,
nodes: list[dict[str, Any]] | None = None, nodes: list[dict[str, Any]] | None = None,
edges: list[dict[str, Any]] | None = None, edges: list[dict[str, Any]] | None = None,
variables: list[dict[str, Any]] | None = None, variables: list[dict[str, Any]] | None = None,
execution_config: dict[str, Any] | None = None, execution_config: dict[str, Any] | None = None,
triggers: list[dict[str, Any]] | None = None, triggers: list[dict[str, Any]] | None = None,
validate: bool = True validate: bool = True
) -> WorkflowConfig: ) -> WorkflowConfig:
"""更新工作流配置 """更新工作流配置
@@ -226,8 +226,8 @@ class WorkflowService:
return config return config
def validate_workflow_config_for_publish( def validate_workflow_config_for_publish(
self, self,
app_id: uuid.UUID app_id: uuid.UUID
) -> tuple[bool, list[str]]: ) -> tuple[bool, list[str]]:
"""验证工作流配置是否可以发布 """验证工作流配置是否可以发布
@@ -260,13 +260,13 @@ class WorkflowService:
# ==================== 执行管理 ==================== # ==================== 执行管理 ====================
def create_execution( def create_execution(
self, self,
workflow_config_id: uuid.UUID, workflow_config_id: uuid.UUID,
app_id: uuid.UUID, app_id: uuid.UUID,
trigger_type: str, trigger_type: str,
triggered_by: uuid.UUID | None = None, triggered_by: uuid.UUID | None = None,
conversation_id: uuid.UUID | None = None, conversation_id: uuid.UUID | None = None,
input_data: dict[str, Any] | None = None input_data: dict[str, Any] | None = None
) -> WorkflowExecution: ) -> WorkflowExecution:
"""创建工作流执行记录 """创建工作流执行记录
@@ -314,10 +314,10 @@ class WorkflowService:
return self.execution_repo.get_by_execution_id(execution_id) return self.execution_repo.get_by_execution_id(execution_id)
def get_executions_by_app( def get_executions_by_app(
self, self,
app_id: uuid.UUID, app_id: uuid.UUID,
limit: int = 50, limit: int = 50,
offset: int = 0 offset: int = 0
) -> list[WorkflowExecution]: ) -> list[WorkflowExecution]:
"""获取应用的执行记录列表 """获取应用的执行记录列表
@@ -332,12 +332,12 @@ class WorkflowService:
return self.execution_repo.get_by_app_id(app_id, limit, offset) return self.execution_repo.get_by_app_id(app_id, limit, offset)
def update_execution_status( def update_execution_status(
self, self,
execution_id: str, execution_id: str,
status: str, status: str,
output_data: dict[str, Any] | None = None, output_data: dict[str, Any] | None = None,
error_message: str | None = None, error_message: str | None = None,
error_node_id: str | None = None error_node_id: str | None = None
) -> WorkflowExecution: ) -> WorkflowExecution:
"""更新执行状态 """更新执行状态
@@ -407,10 +407,10 @@ class WorkflowService:
# ==================== 工作流执行 ==================== # ==================== 工作流执行 ====================
async def run( async def run(
self, self,
app_id: uuid.UUID, app_id: uuid.UUID,
payload: DraftRunRequest, payload: DraftRunRequest,
config: WorkflowConfig config: WorkflowConfig
): ):
"""运行工作流 """运行工作流
@@ -527,10 +527,10 @@ class WorkflowService:
) )
async def run_stream( async def run_stream(
self, self,
app_id: uuid.UUID, app_id: uuid.UUID,
payload: DraftRunRequest, payload: DraftRunRequest,
config: WorkflowConfig config: WorkflowConfig
): ):
"""运行工作流(流式) """运行工作流(流式)
@@ -600,11 +600,11 @@ class WorkflowService:
# 调用流式执行executor 会发送 workflow_start 和 workflow_end 事件) # 调用流式执行executor 会发送 workflow_start 和 workflow_end 事件)
async for event in self._run_workflow_stream( async for event in self._run_workflow_stream(
workflow_config=workflow_config_dict, workflow_config=workflow_config_dict,
input_data=input_data, input_data=input_data,
execution_id=execution.execution_id, execution_id=execution.execution_id,
workspace_id="", workspace_id="",
user_id=payload.user_id user_id=payload.user_id
): ):
# 直接转发 executor 的事件(已经是正确的格式) # 直接转发 executor 的事件(已经是正确的格式)
yield event yield event
@@ -626,12 +626,12 @@ class WorkflowService:
} }
async def run_workflow( async def run_workflow(
self, self,
app_id: uuid.UUID, app_id: uuid.UUID,
input_data: dict[str, Any], input_data: dict[str, Any],
triggered_by: uuid.UUID, triggered_by: uuid.UUID,
conversation_id: uuid.UUID | None = None, conversation_id: uuid.UUID | None = None,
stream: bool = False stream: bool = False
) -> AsyncGenerator | dict: ) -> AsyncGenerator | dict:
"""运行工作流 """运行工作流
@@ -778,12 +778,12 @@ class WorkflowService:
return clean_value(event) return clean_value(event)
async def _run_workflow_stream( async def _run_workflow_stream(
self, self,
workflow_config: dict[str, Any], workflow_config: dict[str, Any],
input_data: dict[str, Any], input_data: dict[str, Any],
execution_id: str, execution_id: str,
workspace_id: str, workspace_id: str,
user_id: str): user_id: str):
"""运行工作流(流式,内部方法) """运行工作流(流式,内部方法)
Args: Args:
@@ -800,11 +800,11 @@ class WorkflowService:
try: try:
async for event in execute_workflow_stream( async for event in execute_workflow_stream(
workflow_config=workflow_config, workflow_config=workflow_config,
input_data=input_data, input_data=input_data,
execution_id=execution_id, execution_id=execution_id,
workspace_id=workspace_id, workspace_id=workspace_id,
user_id=user_id user_id=user_id
): ):
# 直接转发事件executor 已经返回正确格式) # 直接转发事件executor 已经返回正确格式)
yield event yield event
@@ -828,7 +828,7 @@ class WorkflowService:
# ==================== 依赖注入函数 ==================== # ==================== 依赖注入函数 ====================
def get_workflow_service( def get_workflow_service(
db: Annotated[Session, Depends(get_db)] db: Annotated[Session, Depends(get_db)]
) -> WorkflowService: ) -> WorkflowService:
"""获取工作流服务(依赖注入)""" """获取工作流服务(依赖注入)"""
return WorkflowService(db) return WorkflowService(db)