|  |  |  | 
|---|
|  |  |  | from app.service.dialog import get_session_history | 
|---|
|  |  |  | from app.service.ragflow import RagflowService | 
|---|
|  |  |  | from app.service.service_token import get_ragflow_token, get_bisheng_token | 
|---|
|  |  |  | from app.task.fetch_agent import initialize_agents | 
|---|
|  |  |  | # from app.task.fetch_agent import initialize_agents | 
|---|
|  |  |  |  | 
|---|
|  |  |  | router = APIRouter() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | if agent_type == AgentType.RAGFLOW: | 
|---|
|  |  |  | ragflow_service = RagflowService(base_url=settings.fwr_base_url) | 
|---|
|  |  |  | try: | 
|---|
|  |  |  | token = await get_ragflow_token(db, current_user.id) | 
|---|
|  |  |  | result = await ragflow_service.get_chat_sessions(token, agent_id) | 
|---|
|  |  |  | result = await get_session_history(db, current_user.id, agent_id, page, limit) | 
|---|
|  |  |  | if not result: | 
|---|
|  |  |  | result = await get_session_history(db, current_user.id, agent_id, page, limit) | 
|---|
|  |  |  | token = await get_ragflow_token(db, current_user.id) | 
|---|
|  |  |  | result = await ragflow_service.get_chat_sessions(token, agent_id) | 
|---|
|  |  |  | except Exception as e: | 
|---|
|  |  |  | print(e) | 
|---|
|  |  |  | raise HTTPException(status_code=500, detail=str(e)) | 
|---|
|  |  |  | 
|---|
|  |  |  | for i in session.log_to_json().get("message", []): | 
|---|
|  |  |  | if i.get("role") == "user": | 
|---|
|  |  |  | tmp_data["question"]=i.get("content") | 
|---|
|  |  |  | if i.get("download_url") is not None: | 
|---|
|  |  |  | tmp_data["download_url"] = i.get("download_url") | 
|---|
|  |  |  | if "upload_filenames" in i: | 
|---|
|  |  |  | tmp_data["upload_filenames"] = i.get("upload_filenames") | 
|---|
|  |  |  | elif i.get("role") == "assistant": | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if isinstance(i.get("content"), dict): | 
|---|
|  |  |  | 
|---|
|  |  |  | for i in session.log_to_json().get("message", []): | 
|---|
|  |  |  | if i.get("role") == "user": | 
|---|
|  |  |  | tmp_data["question"] = i.get("content") | 
|---|
|  |  |  | if "upload_filenames" in i: | 
|---|
|  |  |  | tmp_data["upload_filenames"] = i.get("upload_filenames") | 
|---|
|  |  |  | elif i.get("role") == "assistant": | 
|---|
|  |  |  | if isinstance(i.get("content"), dict): | 
|---|
|  |  |  | content = i.get("content", {}) | 
|---|
|  |  |  | tmp_data["answer"] = i.get("content", {}).get("answer") | 
|---|
|  |  |  | if "file_name" in i.get("content", {}): | 
|---|
|  |  |  | tmp_data["files"] = [{"file_name": i.get("content", {}).get("file_name"), | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if "download_url" in i.get("content", {}): | 
|---|
|  |  |  | tmp_data["download_url"] = i.get("content", {}).get("download_url") | 
|---|
|  |  |  | if "node_list" in content: | 
|---|
|  |  |  | node_dict = { | 
|---|
|  |  |  | "node_data": [], | 
|---|
|  |  |  | # {"title": "去除冗余",  # 节点名称 "status": "succeeded",  # 节点状态"created_at": 1735817337,  # 开始时间"finished_at": 1735817337,  # 结束时间"error": ""  # 错误日志} | 
|---|
|  |  |  | "total_tokens": 0,  # 花费token数 | 
|---|
|  |  |  | "created_at": 0,  # 开始时间 | 
|---|
|  |  |  | "finished_at": 0,  # 结束时间 | 
|---|
|  |  |  | "elapsed_time": 0,  # 结束时间 | 
|---|
|  |  |  | "status": "succeeded",  # 工作流状态 | 
|---|
|  |  |  | "error": "",  # 错误日志 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for node in content["node_list"]: | 
|---|
|  |  |  | if node.get("event") == "node_finished": | 
|---|
|  |  |  | node_dict["node_data"].append({ | 
|---|
|  |  |  | "title": node.get("data", {}).get("title", ""), | 
|---|
|  |  |  | "status": node.get("data", {}).get("status", ""), | 
|---|
|  |  |  | "created_at": node.get("data", {}).get("created_at", 0), | 
|---|
|  |  |  | "finished_at": node.get("data", {}).get("finished_at", 0), | 
|---|
|  |  |  | "node_type": node.get("data", {}).get("node_type", 0), | 
|---|
|  |  |  | "elapsed_time": node.get("data", {}).get("elapsed_time", 0), | 
|---|
|  |  |  | "error": node.get("data", {}).get("error", ""), | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | elif node.get("event") == "workflow_finished": | 
|---|
|  |  |  | node_dict["total_tokens"] = node.get("data", {}).get("total_tokens", 0) | 
|---|
|  |  |  | node_dict["created_at"] = node.get("data", {}).get("created_at", 0) | 
|---|
|  |  |  | node_dict["finished_at"] = node.get("data", {}).get("finished_at", 0) | 
|---|
|  |  |  | node_dict["status"] = node.get("data", {}).get("status", "") | 
|---|
|  |  |  | node_dict["error"] = node.get("data", {}).get("error", "") | 
|---|
|  |  |  | node_dict["elapsed_time"] = node.get("data", {}).get("elapsed_time", 0) | 
|---|
|  |  |  | tmp_data["workflow"] = node_dict | 
|---|
|  |  |  | else: | 
|---|
|  |  |  | tmp_data["answer"] = i.get("content") | 
|---|
|  |  |  | data.append(tmp_data) | 
|---|