From 319e3e93bbb4807da58db2ac72bb8f3029738dd7 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 03 十二月 2024 13:48:40 +0800
Subject: [PATCH] dify报告合并

---
 app/api/report.py |   88 +++++++++++++++++++++++--------------------
 1 files changed, 47 insertions(+), 41 deletions(-)

diff --git a/app/api/report.py b/app/api/report.py
index 213fdc7..0d33194 100644
--- a/app/api/report.py
+++ b/app/api/report.py
@@ -61,49 +61,55 @@
             async def forward_to_client():
                 is_answer = False
                 while True:
-                    message = await service_websocket.recv()
-                    print(f"Received from bisheng: {message}")
-                    data = json.loads(message)
-                    files = data.get("files", [])
-                    steps = data.get("intermediate_steps", "")
-                    msg = data.get("message", "")
-                    category = data.get("category", "")
+                    try:
+                        message = await service_websocket.recv()
+                        # print(f"Received from bisheng: {message}")
+                        data = json.loads(message)
+                        files = data.get("files", [])
+                        steps = data.get("intermediate_steps", "")
+                        msg = data.get("message", "")
+                        category = data.get("category", "")
+                        process_files(files, agent_id)
+                        if category == "question" and steps:
+                            is_answer = False
+                            if not steps:
+                                steps = "\n"
+                            else:
+                                steps = steps + "\n"
+                            result = {"message": steps, "type": "stream", "files": files}
+                            await websocket.send_json(result)
+                        if category == "answer" and not is_answer:
+                            if not steps.endswith("\n"):
+                                steps += "\n\n"
+                            result = {"message": steps, "type": "stream", "files": files}
+                            await websocket.send_json(result)
+                        if category == "answer" and is_answer:
+                            # process_files(files, agent_id)
+                            result = {"message": "\n", "type": "stream", "files": files}
+                            await websocket.send_json(result)
+                        elif data["type"] == "close":
+                            # process_files(files, agent_id)
+                            result = {"message": "", "type": "close", "files": files}
+                            await websocket.send_json(result)
+                        elif category == "processing":
+                            # process_files(files, agent_id)
+                            is_answer = True
+                            result = {"message": msg, "type": "stream", "files": files}
+                            await websocket.send_json(result)
+                        elif files:
+                            # process_files(files, agent_id)
+                            result = {"message": "", "type": "stream", "files": files}
+                            await websocket.send_json(result)
 
-                    if category == "question" and steps:
-                        is_answer = False
-                        if not steps:
-                            steps = "\n"
+                        elif category == "system" and steps:
+                            result = {"message": steps, "type": "stream", "files": files}
+                            await websocket.send_json(result)
                         else:
-                            steps =steps +  "\n"
-
-                        result = {"message": steps, "type": "stream", "files": files}
-                        await websocket.send_json(result)
-                    if category == "answer" and not is_answer:
-                        process_files(files, agent_id)
-                        if not steps.endswith("\n"):
-                            steps+= "\n\n"
-                        result = {"message": steps, "type": "stream", "files": files}
-                        await websocket.send_json(result)
-                    if category == "answer" and is_answer:
-                        process_files(files, agent_id)
-                        result = {"message": "\n", "type": "stream", "files": files}
-                        await websocket.send_json(result)
-                    elif category == "processing":
-                        process_files(files, agent_id)
-                        is_answer = True
-                        result = {"message": msg, "type": "stream", "files": files}
-                        await websocket.send_json(result)
-                    elif files:
-                        process_files(files, agent_id)
-                        result = {"message": "", "type": "stream", "files": files}
-                        await websocket.send_json(result)
-                    elif data["type"] == "close":
-                        process_files(files, agent_id)
-                        result = {"message": "", "type": "close", "files": files}
-                        await websocket.send_json(result)
-                    else:
-                        logger.error("-------------------11111111111111--------------------------")
-                        logger.error(data)
+                            logger.error("-------------------11111111111111--------------------------")
+                            logger.error(data)
+                    except Exception as e:
+                        logger.error(e)
+                        await websocket.send_json({"message": "杩炴帴寮傚父锛�", "type": "close", "files": []})
                     # if len(files) != 0 or (msg and category != "answer") or data["type"] == "close":
                     #     if data["type"] == "close":
                     #         t = "close"

--
Gitblit v1.8.0