Skip to content

Commit b7b9a5f

Browse files
committed
fix: enable streaming response and handle chunked data in tools.py
1 parent fc0d5e7 commit b7b9a5f

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

apps/chat/mcp/tools.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import re
23

34
import uuid_utils.compat as uuid
45
from django.db.models import QuerySet
@@ -69,7 +70,7 @@ def call_tool(self, params):
6970

7071
payload = {
7172
'message': args.get('message'),
72-
'stream': False,
73+
'stream': True,
7374
're_chat': False
7475
}
7576
resp = ChatSerializers(data={
@@ -81,6 +82,24 @@ def call_tool(self, params):
8182
'source': {"type": ChatSourceChoices.ONLINE.value},
8283
'debug': False,
8384
}).chat(payload)
84-
data = json.loads(str(resp.text))
85+
chunks = []
86+
for raw_line in resp:
87+
line = raw_line.decode("utf-8", errors="replace").rstrip("\r\n")
88+
if not line.startswith("data:"):
89+
continue
90+
payload = line[5:].strip()
91+
if not payload or payload == "[DONE]":
92+
continue
93+
try:
94+
event = json.loads(payload)
95+
except json.JSONDecodeError:
96+
continue
97+
if event.get("operate") is True:
98+
chunks.append(event.get("content", ""))
99+
if event.get("is_end"):
100+
break
85101

86-
return {"content": [{"type": "text", "text": data.get('data', {}).get('content')}]}
102+
data = ''.join(chunks)
103+
# 排除<tool_calls_render></tool_calls_render>标签
104+
data = re.sub(r'<tool_calls_render>.*?</tool_calls_render>', '', data, flags=re.DOTALL)
105+
return {"content": [{"type": "text", "text": data}]}

0 commit comments

Comments
 (0)