android应用

This commit is contained in:
rjb
2026-01-20 11:03:55 +08:00
parent f6568f252a
commit d59f015362
16 changed files with 1754 additions and 17 deletions

View File

@@ -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)
):
"""导出AgentJSON格式"""
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)
):
"""导入AgentJSON格式"""
# 提取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