zhaoqingang
2025-04-11 afc68af3a67b2f936429feb3a68c1f09e03ce930
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import json
 
from fastapi import WebSocket, WebSocketDisconnect, APIRouter, Depends, HTTPException, Query
import asyncio
import websockets
from sqlalchemy.orm import Session
 
from Log import logger
from app.api import get_current_user_websocket, ResponseList, get_current_user, format_file_url, process_files
from app.config.config import settings
from app.models import MenuCapacityModel
from app.models.agent_model import AgentModel, AgentType
from app.models.base_model import get_db
from app.models.user_model import UserModel
from app.service.bisheng import BishengService
from app.service.service_token import get_bisheng_token
 
router = APIRouter()
 
 
@router.websocket("/ws/{agent_id}/{chat_id}")
async def report_chat(websocket: WebSocket,
                      agent_id: str,
                      chat_id: str,
                      current_user: UserModel = Depends(get_current_user_websocket),
                      db: Session = Depends(get_db)):
    agent = db.query(MenuCapacityModel).filter(MenuCapacityModel.chat_id == agent_id).first()
    if not agent:
        agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
        agent_type = agent.agent_type
        chat_type = agent.type
    else:
        agent_type = agent.capacity_type
        chat_type = agent.chat_type
    if not agent:
        ret = {"message": "Agent not found", "type": "close"}
        return websocket.send_json(ret)
    # agent_type = agent.agent_type
    if chat_id == "" or chat_id == "0":
        ret = {"message": "Chat ID not found", "type": "close"}
        return websocket.send_json(ret)
 
    if agent_type != AgentType.BISHENG:
        ret = {"message": "Agent error", "type": "close"}
        return websocket.send_json(ret)
 
    token = await get_bisheng_token(db, current_user.id)
    service_uri = f"{settings.sgb_websocket_url}/api/v1/chat/{agent_id}?type=L1&t=&chat_id={chat_id}"
    headers = {'cookie': f"access_token_cookie={token};"}
 
    await websocket.accept()
    print(f"Client {agent_id} connected")
 
    async with websockets.connect(service_uri, extra_headers=headers) as service_websocket:
 
        try:
            # 处理客户端发来的消息
            async def forward_to_service():
                while True:
                    message = await websocket.receive_json()
                    print(f"Received from client, {chat_id}: {message}")
                    # 添加 'agent_id' 和 'chat_id' 字段
                    message['flow_id'] = agent_id
                    message['chat_id'] = chat_id
                    await service_websocket.send(json.dumps(message))
                    print(f"Forwarded to bisheng: {message}")
 
            # 监听毕昇发来的消息并转发给客户端
            async def forward_to_client():
                is_answer = False
                while True:
                    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)
 
                        elif category == "system" and steps:
                            result = {"message": steps, "type": "stream", "files": files}
                            await websocket.send_json(result)
                        else:
                            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"
                    #     else:
                    #         t = "stream"
                    #     process_files(files, agent_id)
                    #     result = {"message": msg, "type": t, "files": files}
                    #     await websocket.send_json(result)
                    # elif steps and last_message == "step":
                    #     result = {"step_message": steps, "type": "stream", "files": files}
                    #     await websocket.send_json(result)
 
                    # last_message = "message" if msg else "step"
 
            # 启动两个任务,分别处理客户端和服务端的消息
            tasks = [
                asyncio.create_task(forward_to_service()),
                asyncio.create_task(forward_to_client())
            ]
            done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
 
            # 取消未完成的任务
            for task in pending:
                task.cancel()
                try:
                    await task
                except asyncio.CancelledError as e:
                    print(f"asyncio CancelledError: {e}")
                    pass
 
        except WebSocketDisconnect as e:
            print(f"WebSocket connection closed with code {e.code}: {e.reason}")
            await websocket.close()
            await service_websocket.close()
        except Exception as e:
            print(f"Exception occurred: {e}")
        finally:
            print("Cleaning up resources of bisheng report")
            # 取消所有任务
            for task in tasks:
                if not task.done():
                    task.cancel()
                    try:
                        await task
                    except asyncio.CancelledError:
                        pass
 
 
@router.get("/variables/list", response_model=ResponseList)
async def get_variables(agent_id: str = Query(..., description="The ID of the agent"), db: Session = Depends(get_db), current_user: UserModel = Depends(get_current_user)):
    agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
    if not agent:
        return ResponseList(code=404, msg="Agent not found")
    bisheng_service = BishengService(base_url=settings.sgb_base_url)
    try:
        token = await get_bisheng_token(db, current_user.id)
        result = await bisheng_service.variable_list(token, agent_id)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    return ResponseList(code=200, msg="", data=result)