From f9defd30d132c99b8932151366c074c70ddaaccb Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 4 Feb 2026 09:26:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=80=BB=E7=BB=93=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=E6=B5=81=E5=BC=8F=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=EF=BC=8C=E5=B9=B6=E4=B8=94=E5=8A=A0=E5=85=A5=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=E6=80=9D=E8=80=83=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/message_summary/main.py | 89 +++++++++++---------------------- 1 file changed, 29 insertions(+), 60 deletions(-) diff --git a/plugins/message_summary/main.py b/plugins/message_summary/main.py index 18d4a59..947976c 100644 --- a/plugins/message_summary/main.py +++ b/plugins/message_summary/main.py @@ -229,8 +229,7 @@ class MessageSummaryPlugin(MessagePluginInterface): # 设置请求头 headers = { "Authorization": f"Bearer {self._api_key}", - "Content-Type": "application/json", - "Accept": "text/event-stream" + "Content-Type": "application/json" } try: @@ -239,65 +238,35 @@ class MessageSummaryPlugin(MessagePluginInterface): async with session.post(self._api_url, headers=headers, json=data) as response: response.raise_for_status() self.LOG.info(f"Dify API响应状态码: {response.status}") - ctype = response.headers.get("Content-Type", "") - answer = "" + answer_parts = [] + current_event = None + data_buffer = [] + async for raw_line in response.content: + line = raw_line.decode("utf-8").rstrip("\n") + if not line: + if data_buffer: + payload_str = "\n".join(data_buffer) + try: + payload = json.loads(payload_str) + if current_event == "message": + chunk = payload.get("answer") or "" + if chunk: + answer_parts.append(chunk) + except Exception as e: + self.LOG.debug(f"SSE数据解析失败: {e}") + current_event = None + data_buffer = [] + continue + if line.startswith(":"): + continue + if line.startswith("event:"): + current_event = line.split(":", 1)[1].strip() + continue + if line.startswith("data:"): + data_buffer.append(line[5:].strip()) + continue + answer = "".join(answer_parts) spath = None - tokens_info = "" - if "text/event-stream" in ctype: - answer_parts = [] - current_event = None - data_buffer = [] - async for raw_line in response.content: - line = raw_line.decode("utf-8", errors="ignore").rstrip("\r\n") - if not line: - if data_buffer: - payload_str = "\n".join(data_buffer) - try: - payload = json.loads(payload_str) - if current_event == "message": - chunk = payload.get("answer") or "" - if chunk: - answer_parts.append(chunk) - elif current_event in ("message_end", "end", "completed"): - meta = payload.get("metadata", {}) - usage = meta.get("usage", {}) - if usage: - pt = usage.get("prompt_tokens", 0) - ct = usage.get("completion_tokens", 0) - tt = usage.get("total_tokens", 0) - tokens_info = f"\n\n【tokens】输入: {pt} 生成: {ct} 总: {tt}" - except Exception as e: - self.LOG.debug(f"SSE数据解析失败: {e}") - current_event = None - data_buffer = [] - continue - if line.startswith(":"): - continue - if line.startswith("event:"): - current_event = line.split(":", 1)[1].strip() - continue - if line.startswith("data:"): - data_buffer.append(line[5:].strip()) - continue - answer = "".join(answer_parts) - if tokens_info: - answer += tokens_info - else: - try: - response_data = await response.json() - self.LOG.debug(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}") - answer = response_data.get("answer", "") or "" - metadata = response_data.get("metadata", {}) or {} - usage = metadata.get("usage", {}) or {} - if usage: - pt = usage.get("prompt_tokens", 0) - ct = usage.get("completion_tokens", 0) - tt = usage.get("total_tokens", 0) - tokens_info = f"\n\n【tokens】输入: {pt} 生成: {ct} 总: {tt}" - answer += tokens_info - except Exception as e: - self.LOG.error(f"解析非SSE响应失败: {e}") - answer = "" if answer: try: timestamp = int(time.time())