From a01c838f32839349c243f586fb003333899cab81 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期一, 06 一月 2025 17:54:22 +0800
Subject: [PATCH] yohua

---
 app/api/agent.py |  131 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 103 insertions(+), 28 deletions(-)

diff --git a/app/api/agent.py b/app/api/agent.py
index 698ca0d..f3ea907 100644
--- a/app/api/agent.py
+++ b/app/api/agent.py
@@ -6,7 +6,8 @@
 from pydantic import BaseModel
 from sqlalchemy.orm import Session
 
-from app.api import Response, get_current_user, ResponseList
+from app.api import Response, get_current_user, ResponseList, process_files
+from app.api.user import reset_user_pwd
 from app.config.config import settings
 from app.models.agent_model import AgentType, AgentModel
 from app.models.base_model import get_db
@@ -112,37 +113,67 @@
         except Exception as e:
             raise HTTPException(status_code=500, detail=str(e))
     elif agent.agent_type == AgentType.BISHENG:
+        is_join = False
+        if agent.name == "鎶ュ憡鐢熸垚":
+            is_join = True
         bisheng_service = BishengService(base_url=settings.sgb_base_url)
         try:
             token = get_bisheng_token(db, current_user.id)
             result = await bisheng_service.get_session_log(token, agent_id, conversation_id)
             combined_logs = []
             last_question = None
-
+            answer_str = ""
+            files = []
             for session in result:
-                message = session['message']
+                # print(session)
+                # 妫�鏌� session 鏄惁涓� None
+                if session is None:
+                    continue
 
-                # 鍒ゆ柇message鏄瓧绗︿覆杩樻槸json 瀵硅薄,濡傛灉鏄痡son鍙栧叾涓殑question瀛楁锛屾垨鑰卹eport_name瀛楁璧嬪�肩粰message
-                try:
-                    message_json = json.loads(message)
-                    if 'question' in message_json:
-                        message = message_json['question']
-                    elif 'query' in message_json:
-                        message = message_json['query']
-                    elif 'report_name' in message_json:
-                        message = message_json['report_name']
-                except json.JSONDecodeError:
-                    pass
+                message = session.get('message')
 
-                if session['role'] == 'question':
-                    last_question = message
-                elif session['role'] == 'answer' and last_question:
-                    combined_logs.append({
-                        'question': last_question,
-                        'answer': message
-                    })
-                    last_question = None
-            return JSONResponse(status_code=200, content={"code": 200, "data": combined_logs})
+                # 鍒ゆ柇 message 鏄惁鏄瓧绗︿覆锛岀劧鍚庡皾璇曡В鏋愪负 JSON 瀵硅薄
+                if isinstance(message, str):
+                    try:
+                        message_json = json.loads(message)
+                        if 'question' in message_json:
+                            message = message_json['question']
+                        elif 'query' in message_json:
+                            message = message_json['query']
+                        elif 'report_name' in message_json:
+                            message = message_json['report_name']
+
+                    except Exception as e:
+                        pass  # 闈� JSON 瀛楃涓诧紝缁х画浣跨敤鍘熷 message
+                if session.get('files') and isinstance(session.get('files'), str):
+                    try:
+                        files = json.loads(session.get('files'))
+                        process_files(files, agent_id)
+
+                    except Exception as e:
+                        pass  # 闈� JSON 瀛楃涓诧紝缁х画浣跨敤鍘熷 message
+
+                # 妫�鏌� message 鏄惁涓� None
+                if message is None:
+                    continue
+                if is_join:
+                    ...
+                    if session.get('role') == 'question':
+                        last_question = message
+                    elif session.get('role') == 'answer':
+                        answer_str += message
+
+                else:
+                    if session.get('role') == 'question':
+                        last_question = message
+                    elif session.get('role') == 'answer' and last_question:
+                        combined_logs.append({
+                            'question': last_question,
+                            'answer': message
+                        })
+                        last_question = None
+            return JSONResponse(status_code=200, content={"code": 200, "data": combined_logs if combined_logs else [{'question': last_question,
+                            'answer': answer_str, 'files': files}]})
         except Exception as e:
             raise HTTPException(status_code=500, detail=str(e))
     elif agent.agent_type == AgentType.BASIC:
@@ -172,6 +203,11 @@
                         tmp_data["code"] = i.get("code")
                     if "e" in i:
                         tmp_data["e"] = i.get("e")
+
+                    if "image_name" in i:
+                        tmp_data["image_name"] = i.get("image_name")
+                    if "excel_name" in i:
+                        tmp_data["excel_name"] = i.get("excel_name")
                     data.append(tmp_data)
                     tmp_data = {}
 
@@ -187,14 +223,51 @@
             for i in session.log_to_json().get("message", []):
                 if i.get("role") == "user":
                     tmp_data["question"] = i.get("content")
+                    if "type" in i.get("content"):
+                        tmp_data["type"] =  i.get("content")["type"]
                 elif i.get("role") == "assistant":
                     if isinstance(i.get("content"), dict):
-                        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"),
-                                                  "file_url": i.get("content", {}).get("file_url")}]
+                        content = i.get("content", {})
+                        tmp_data["answer"] = content.get("answer")
+                        if "file_name" in content:
+                            tmp_data["files"] = [{"file_name": content.get("file_name"),
+                                                  "file_url": content.get("file_url")}]
                         if "images" in i.get("content", {}):
-                            tmp_data["images"] = i.get("content", {}).get("images")
+                            tmp_data["images"] = content.get("images")
+
+                        if "download_url" in content:
+                            tmp_data["download_url"] = 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)
@@ -216,3 +289,5 @@
         return Response(code=404, msg="Agent not found")
 
     return Response(code=200, msg="", data={"chat_id": uuid.uuid4().hex})
+
+

--
Gitblit v1.8.0