android应用
This commit is contained in:
@@ -397,3 +397,112 @@ async def duplicate_agent(
|
||||
|
||||
logger.info(f"用户 {current_user.username} 复制了Agent: {original_agent.name} ({agent_id}) -> {new_agent.name} ({new_agent.id})")
|
||||
return new_agent
|
||||
|
||||
|
||||
@router.get("/{agent_id}/export", status_code=status.HTTP_200_OK)
|
||||
async def export_agent(
|
||||
agent_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""导出Agent(JSON格式)"""
|
||||
try:
|
||||
agent = db.query(Agent).filter(Agent.id == agent_id).first()
|
||||
|
||||
if not agent:
|
||||
raise NotFoundError("Agent", agent_id)
|
||||
|
||||
# 检查权限:read权限
|
||||
if not check_agent_permission(db, current_user, agent, "read"):
|
||||
raise HTTPException(status_code=403, detail="无权导出此Agent")
|
||||
|
||||
# 验证工作流配置
|
||||
workflow_config = agent.workflow_config
|
||||
if not workflow_config:
|
||||
raise ValidationError("Agent工作流配置为空,无法导出")
|
||||
|
||||
export_data = {
|
||||
"id": str(agent.id),
|
||||
"name": agent.name,
|
||||
"description": agent.description,
|
||||
"workflow_config": workflow_config,
|
||||
"version": agent.version,
|
||||
"status": agent.status,
|
||||
"exported_at": datetime.utcnow().isoformat()
|
||||
}
|
||||
|
||||
logger.info(f"用户 {current_user.username} 导出Agent: {agent.name} ({agent_id})")
|
||||
return export_data
|
||||
except Exception as e:
|
||||
logger.error(f"导出Agent失败: {str(e)}", exc_info=True)
|
||||
raise
|
||||
|
||||
|
||||
@router.post("/import", response_model=AgentResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def import_agent(
|
||||
agent_data: Dict[str, Any],
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""导入Agent(JSON格式)"""
|
||||
# 提取Agent数据
|
||||
name = agent_data.get("name", "导入的Agent")
|
||||
description = agent_data.get("description")
|
||||
workflow_config = agent_data.get("workflow_config", {})
|
||||
|
||||
# 验证工作流配置
|
||||
if not workflow_config:
|
||||
raise ValidationError("Agent工作流配置不能为空")
|
||||
|
||||
nodes = workflow_config.get("nodes", [])
|
||||
edges = workflow_config.get("edges", [])
|
||||
|
||||
if not nodes or not edges:
|
||||
raise ValidationError("Agent工作流配置无效:缺少节点或边")
|
||||
|
||||
# 验证工作流
|
||||
validation_result = validate_workflow(nodes, edges)
|
||||
if not validation_result["valid"]:
|
||||
raise ValidationError(f"导入的Agent工作流验证失败: {', '.join(validation_result['errors'])}")
|
||||
|
||||
# 重新生成节点ID(避免ID冲突)
|
||||
node_id_mapping = {}
|
||||
for node in nodes:
|
||||
old_id = node["id"]
|
||||
new_id = f"node_{len(node_id_mapping)}_{old_id}"
|
||||
node_id_mapping[old_id] = new_id
|
||||
node["id"] = new_id
|
||||
|
||||
# 更新边的源节点和目标节点ID
|
||||
for edge in edges:
|
||||
if edge.get("source") in node_id_mapping:
|
||||
edge["source"] = node_id_mapping[edge["source"]]
|
||||
if edge.get("target") in node_id_mapping:
|
||||
edge["target"] = node_id_mapping[edge["target"]]
|
||||
|
||||
# 检查名称是否已存在
|
||||
base_name = name
|
||||
counter = 1
|
||||
while db.query(Agent).filter(
|
||||
Agent.name == name,
|
||||
Agent.user_id == current_user.id
|
||||
).first():
|
||||
name = f"{base_name} (导入 {counter})"
|
||||
counter += 1
|
||||
|
||||
# 创建Agent
|
||||
agent = Agent(
|
||||
name=name,
|
||||
description=description,
|
||||
workflow_config={
|
||||
"nodes": nodes,
|
||||
"edges": edges
|
||||
},
|
||||
user_id=current_user.id,
|
||||
status="draft", # 导入的Agent默认为草稿状态
|
||||
version=1
|
||||
)
|
||||
db.add(agent)
|
||||
db.commit()
|
||||
db.refresh(agent)
|
||||
return agent
|
||||
|
||||
@@ -1936,11 +1936,29 @@ class WorkflowEngine:
|
||||
# 如果是条件节点,根据分支结果过滤边
|
||||
if node.get('type') == 'condition':
|
||||
branch = result.get('branch', 'false')
|
||||
logger.info(f"[rjb] 条件节点分支过滤: node_id={next_node_id}, branch={branch}")
|
||||
# 移除不符合条件的边
|
||||
active_edges = [
|
||||
edge for edge in active_edges
|
||||
if not (edge['source'] == next_node_id and edge.get('sourceHandle') != branch)
|
||||
]
|
||||
# 只保留:1) 不是从条件节点出发的边,或 2) 从条件节点出发且sourceHandle匹配分支的边
|
||||
edges_to_remove = []
|
||||
edges_to_keep = []
|
||||
for edge in active_edges:
|
||||
if edge['source'] == next_node_id:
|
||||
# 这是从条件节点出发的边
|
||||
edge_handle = edge.get('sourceHandle')
|
||||
if edge_handle == branch:
|
||||
# sourceHandle匹配分支,保留
|
||||
edges_to_keep.append(edge)
|
||||
logger.info(f"[rjb] 保留边: {edge.get('id')} (sourceHandle={edge_handle} == branch={branch})")
|
||||
else:
|
||||
# sourceHandle不匹配或为None,移除
|
||||
edges_to_remove.append(edge)
|
||||
logger.info(f"[rjb] 移除边: {edge.get('id')} (sourceHandle={edge_handle} != branch={branch})")
|
||||
else:
|
||||
# 不是从条件节点出发的边,保留
|
||||
edges_to_keep.append(edge)
|
||||
|
||||
active_edges = edges_to_keep
|
||||
logger.info(f"[rjb] 条件节点过滤后: 保留{len(active_edges)}条边,移除{len(edges_to_remove)}条边")
|
||||
|
||||
# 如果是循环节点,跳过循环体的节点(循环体已在节点内部执行)
|
||||
if node.get('type') in ['loop', 'foreach']:
|
||||
|
||||
Reference in New Issue
Block a user