Skip to content

Commit 44e9702

Browse files
author
mobiusy
committed
fix(http_request_stream): 修复SSE错误处理和conversation_id逻辑
改进HTTP错误处理,添加错误消息响应文本显示 增加对SSE事件类型"error"的处理逻辑 优化conversation_id提取逻辑,避免重复输出
1 parent ec918ce commit 44e9702

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## 基本信息
66
- 作者:mobiusy
7-
- 版本:0.0.1
7+
- 版本:0.0.2
88
- 类型:tool
99

1010
## 功能特性

http_request_stream/manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 0.0.1
1+
version: 0.0.2
22
type: plugin
33
author: mobiusy
44
name: http_request_stream

http_request_stream/tools/http_request_stream.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,37 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
9797
# 对非 2xx 状态码进行友好提示并终止流
9898
if response.status_code < 200 or response.status_code >= 300:
9999
err_msg = f"HTTP Error: {response.status_code}"
100+
response.read()
101+
err_msg = f"{err_msg}. Response Text: {response.text if response.text else '(empty)'}"
100102
logger.error(err_msg)
101-
# 使用 httpx 内置的 raise_for_status 构造并抛出包含 request/response 的异常
102-
response.raise_for_status()
103+
raise httpx.HTTPStatusError(err_msg, request=response.request, response=response)
104+
current_event: str | None = None
103105
for raw_line in response.iter_lines():
104106
if raw_line is None:
105107
continue
106108
line = raw_line.decode("utf-8") if isinstance(raw_line, bytes) else raw_line
107109
line = line.strip()
110+
if not line:
111+
continue
112+
if line.startswith("event:"):
113+
current_event = line[6:].strip()
114+
continue
108115
if line.startswith("data:"):
109116
line = line[5:].strip()
110-
if line:
111-
# 先检测是否包含 conversation_id 片段
112-
match = conversation_id_pattern.search(line)
113-
if match:
114-
# 命中 conversation_id 片段的 chunk,不输出到 stream_text
115-
if not conversation_id_emitted:
116-
conversation_id_value = match.group(1)
117-
# 输出一次 conversation_id 变量,供后续节点引用
118-
yield self.create_variable_message("conversation_id", conversation_id_value)
119-
conversation_id_emitted = True
120-
# 跳过本次 chunk 的 stream_text 输出
121-
continue
122-
# 非 conversation_id 的 chunk,正常输出到 stream_text
123-
yield self.create_stream_variable_message("stream_text", line)
117+
if current_event == "error" and line:
118+
err_msg = line
119+
yield self.create_variable_message("error", err_msg)
120+
logger.error(err_msg)
121+
raise RuntimeError(err_msg)
122+
# 先检测是否包含 conversation_id 片段
123+
match = conversation_id_pattern.search(line)
124+
if match:
125+
if not conversation_id_emitted:
126+
conversation_id_value = match.group(1)
127+
yield self.create_variable_message("conversation_id", conversation_id_value)
128+
conversation_id_emitted = True
129+
continue
130+
yield self.create_stream_variable_message("stream_text", line)
124131

125132
except httpx.HTTPError as e:
126133
# 捕获 httpx 的 HTTP 异常,并输出到用户侧

http_request_stream/tools/http_request_stream.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ output_schema:
9999
conversation_id:
100100
type: string
101101
description: Extracted conversation id when a chunk matches the pattern "conversation_id/xxxxx"
102-
102+
error:
103+
type: string
104+
description: Error message extracted when receiving SSE event "error"

0 commit comments

Comments
 (0)