diff --git a/apps/entities/appcenter.py b/apps/entities/appcenter.py index a34be181cf0c2a21f99ff16b3521fa6892144c24..eb02d6fb056cba94d4cb7a7ecb20270424b6b3ee 100644 --- a/apps/entities/appcenter.py +++ b/apps/entities/appcenter.py @@ -49,4 +49,4 @@ class AppData(BaseModel): history_len: int = Field(3, alias="dialogRounds", ge=1, le=10, description="对话轮次(1~10)") permission: AppPermissionData = Field( default_factory=lambda: AppPermissionData(authorizedUsers=None), description="权限配置") - workflows: list[str] = Field(default=[], description="工作流ID列表") + workflows: list[dict] = Field(default=[], description="工作流ID,名称列表") diff --git a/apps/entities/flow.py b/apps/entities/flow.py index ca82a4726bc2bf83e898a50a342c3fbf9e1d25f1..21592ff9dd3f3a6779dcbfcf0c1820ccf2eaa114 100644 --- a/apps/entities/flow.py +++ b/apps/entities/flow.py @@ -24,9 +24,9 @@ class Edge(BaseModel): """Flow中Edge的数据""" id: str = Field(description="边的ID") - edge_from: str = Field(description="边的来源节点ID", alias="from") - edge_to: str = Field(description="边的目标节点ID", alias="to") - edge_type: Optional[EdgeType] = Field(description="边的类型", alias="type") + edge_from: str = Field(description="边的来源节点ID") + edge_to: str = Field(description="边的目标节点ID") + edge_type: Optional[EdgeType] = Field(description="边的类型") class Step(BaseModel): diff --git a/apps/entities/flow_topology.py b/apps/entities/flow_topology.py index bde97fcb248c19ea5577c7e0b6cc828e98321797..6c890fb4b50cee4a8ca4823178c79e96049f4bdc 100644 --- a/apps/entities/flow_topology.py +++ b/apps/entities/flow_topology.py @@ -15,7 +15,7 @@ class NodeMetaDataItem(BaseModel): type: str name: str description: str - parameters_template: Optional[dict[str, Any]] = Field(alias="parametersTemplate") + parameters: Optional[dict[str, Any]] editable: bool = Field(default=True) created_at: Optional[float] = Field(alias="createdAt") @@ -41,12 +41,13 @@ class DependencyItem(BaseModel): class NodeItem(BaseModel): """请求/响应中的节点变量类""" node_id: str = Field(alias="nodeId",default="") - api_id: str = Field(alias="apiId",default="") + service_id: str = Field(alias="serviceId",default="") + node_meta_data_id: str = Field(alias="nodeMetaDataId",default="") name: str=Field(default="") type: str = Field(default=NodeType.NORMAL.value) description: str = Field(default='') enable: bool = Field(default=True) - parameters: Optional[dict[str, Any]]=None + parameters: dict[str, Any] = Field(default={}) depedency: Optional[DependencyItem] = None position: PositionItem=Field(default=PositionItem()) editable: bool = Field(default=True) diff --git a/apps/manager/flow.py b/apps/manager/flow.py index 57e6bf912b3f8e238304b373741e4f9f5273f8a4..935110fdcf52d8bc37ca52f23f78450912b40dc6 100644 --- a/apps/manager/flow.py +++ b/apps/manager/flow.py @@ -10,7 +10,7 @@ from apps.entities.flow import StepPos, Edge, Step, Flow, FlowConfig from apps.entities.pool import AppFlow from apps.entities.flow_topology import NodeServiceItem, NodeMetaDataItem, FlowItem, NodeItem, EdgeItem, PositionItem from apps.models.mongo import MongoDB -from apps.entities.enum_var import PermissionType +from apps.entities.enum_var import NodeType, PermissionType class FlowManager: @@ -61,16 +61,22 @@ class FlowManager: nodes_meta_data_items = [] async for node_pool_record in cursor: + parameters = { + "input_parameters": node_pool_record["params_schema"], + "output_parameters": node_pool_record["output_schema"] + } node_meta_data_item = NodeMetaDataItem( nodeMetaDataId=node_pool_record["_id"], type=node_pool_record["call_id"], - name=node_pool_record['name'], - description=node_pool_record['description'], + name=node_pool_record["name"], + description=node_pool_record["description"], editable=True, - createdAt=node_pool_record['created_at'], + createdAt=node_pool_record["created_at"], + parameters=parameters, # 添加 parametersTemplate 参数 ) nodes_meta_data_items.append(node_meta_data_item) + return nodes_meta_data_items except Exception as e: @@ -115,7 +121,7 @@ class FlowManager: name=record["name"], type="default", nodeMetaDatas=[], - createdAt=record["created_at"] + createdAt=str(record["created_at"]) ) for record in service_records ] @@ -139,9 +145,9 @@ class FlowManager: node_pool_collection = MongoDB.get_collection("node") # 获取节点集合 try: node_pool_record = await node_pool_collection.find_one({"_id": node_meta_data_id}) - parameters_template = { - "input_schema": node_pool_record['params_schema'], - "output_schema": node_pool_record['output_schema'] + parameters = { + "input_parameters": node_pool_record['params_schema'], + "output_parameters": node_pool_record['output_schema'] } node_meta_data=NodeMetaDataItem( nodeMetaDataId=node_pool_record["_id"], @@ -149,7 +155,7 @@ class FlowManager: name=node_pool_record['name'], description=node_pool_record['description'], editable=True, - parametersTemplate=parameters_template, + parameters=parameters, createdAt=node_pool_record['created_at'], ) return node_meta_data @@ -187,61 +193,66 @@ class FlowManager: LOGGER.error( f"Get flow by app_id and flow_id failed due to: {e}") return None - if flow_record: - flow_config_collection = MongoDB.get_collection("flow_config") - flow_config_record = await flow_config_collection.find_one({"app_id": app_id, "flow_id": flow_id}) - if flow_config_record is None or not flow_config_record.get("flow_config"): - return None - flow_config = flow_config_record['flow_config'] - if not flow_config: - LOGGER.error( - "Get flow config by app_id and flow_id failed") - return None - focus_point = flow_record["focus_point"] - flow_item = FlowItem( - flowId=flow_id, - name=flow_config['name'], - description=flow_config['description'], - enable=True, - editable=True, - nodes=[], - edges=[], - createdAt=flow_record["created_at"] - ) - for node_config in flow_config['steps']: - node_item = NodeItem( - nodeId=node_config['id'], - apiId=node_config['node'], - name=node_config['name'], - description=node_config['description'], + try: + if flow_record: + flow_config_collection = MongoDB.get_collection("flow_config") + flow_config_record = await flow_config_collection.find_one({"app_id": app_id, "flow_id": flow_id}) + if flow_config_record is None or not flow_config_record.get("flow_config"): + return None + flow_config = flow_config_record['flow_config'] + if not flow_config: + LOGGER.error( + "Get flow config by app_id and flow_id failed") + return None + focus_point = flow_record["focus_point"] + flow_item = FlowItem( + flowId=flow_id, + name=flow_config['name'], + description=flow_config['description'], enable=True, editable=True, - type=node_config['type'], - parameters=node_config['params'], - position=PositionItem( - x=node_config['pos']['x'], y=node_config['pos']['y']) + nodes=[], + edges=[], + createdAt=flow_record["created_at"] ) - flow_item.nodes.append(node_item) + for node_config in flow_config['steps']: + node_item = NodeItem( + nodeId=node_config['id'], + nodeMetaDataId=node_config['node'], + name=node_config['name'], + description=node_config['description'], + enable=True, + editable=True, + type=node_config['type'], + parameters=node_config['params'], + position=PositionItem( + x=node_config['pos']['x'], y=node_config['pos']['y']) + ) + flow_item.nodes.append(node_item) - for edge_config in flow_config['edges']: - edge_from = edge_config['edge_from'] - branch_id = '' - tmp_list = edge_config['edge_from'].split('.') - if len(tmp_list)==0 or len(tmp_list)>=2: - LOGGER.error("edge from format error") - continue - if len(tmp_list) == 2: - edge_from = tmp_list[0] - branch_id = tmp_list[1] - flow_item.edges.append(EdgeItem( - edgeId=edge_config['id'], - sourceNode=edge_from, - targetNode=edge_config['edge_to'], - type=edge_config['edge_type'], - branchId=branch_id, - )) - return (flow_item, focus_point) - return None + for edge_config in flow_config['edges']: + edge_from = edge_config['edge_from'] + branch_id = "" + tmp_list = edge_config['edge_from'].split('.') + if len(tmp_list)==0 or len(tmp_list)>2: + LOGGER.error("edge from format error") + continue + if len(tmp_list) == 2: + edge_from = tmp_list[0] + branch_id = tmp_list[1] + flow_item.edges.append(EdgeItem( + edgeId=edge_config['id'], + sourceNode=edge_from, + targetNode=edge_config['edge_to'], + type=edge_config['edge_type'], + branchId=branch_id, + )) + return (flow_item, focus_point) + return None + except Exception as e: + LOGGER.error( + f"Get flow by app_id and flow_id failed due to: {e}") + return None @staticmethod async def put_flow_by_app_and_flow_id( @@ -284,18 +295,18 @@ class FlowManager: edge_config = Step( id=node_item.node_id, type=node_item.type, - node=node_item.api_id, + node=node_item.node_meta_data_id, name=node_item.name, description=node_item.description, pos=StepPos(x=node_item.position.x, y=node_item.position.y), - params=node_item.parameters + params=node_item.parameters, ) flow_config.steps.append(edge_config) for edge_item in flow_item.edges: edge_from = edge_item.source_node if edge_item.branch_id: - edge_from = edge_from+'.'+edge_item.branch_id + edge_from = edge_from+"."+edge_item.branch_id edge_config = Edge( id=edge_item.edge_id, edge_from=edge_from, diff --git a/apps/routers/appcenter.py b/apps/routers/appcenter.py index f9c1255fc34bc8018a568783d2ef1b96dfc28452..d32c75597d94cfd0a1abb319c67fa69a7805a046 100644 --- a/apps/routers/appcenter.py +++ b/apps/routers/appcenter.py @@ -150,7 +150,7 @@ async def get_application( # noqa: ANN201 message="找不到应用", result={}, ).model_dump(exclude_none=True, by_alias=True)) - workflows = [flow.id for flow in app_data.flows] + workflows = [{"id":flow.id,"name":flow.name} for flow in app_data.flows] return JSONResponse(status_code=status.HTTP_200_OK, content=GetAppPropertyRsp( code=status.HTTP_200_OK, message="查询成功", diff --git a/apps/utils/flow.py b/apps/utils/flow.py index 761d8c3fc86e7109778c95a36eedb768de155f96..a8613f0ee6326f15bb6c70a4c361dbd7c2b9f289 100644 --- a/apps/utils/flow.py +++ b/apps/utils/flow.py @@ -16,17 +16,18 @@ class FlowService: for node in flow_item.nodes: node_to_branches[node.node_id] = set() if node.type == NodeType.CHOICE.value: + node.parameters = node.parameters["input_parameters"] if 'choices' not in node.parameters.keys(): node.parameters['choices'] = [] for branch in node.parameters['choices']: - if branch['branch'] in node_to_branches[node.node_id]: + if branch['branchId'] in node_to_branches[node.node_id]: LOGGER.error(msg="分支id重复") - raise Exception(f"节点{node.name}的分支{branch['branch']}重复") + raise Exception(f"节点{node.name}的分支{branch['branchId']}重复") for illegal_char in branch_illegal_chars: - if illegal_char in branch['branch']: + if illegal_char in branch['branchId']: LOGGER.error(msg="分支名称中含有非法字符") - raise Exception(f"节点{node.name}的分支{branch['branch']}名称中含有非法字符") - node_to_branches[node.node_id].add(branch['branch']) + raise Exception(f"节点{node.name}的分支{branch['branchId']}名称中含有非法字符") + node_to_branches[node.node_id].add(branch['branchId']) else: node_to_branches[node.node_id].add('') new_edges_items = []