Skip to content

Commit 6c47aa2

Browse files
author
mobiusy
committed
feat(http_request_stream): 新增从流式响应中提取 conversation_id 的功能
在流式响应中检测形如 "conversation_id/xxxxx" 的片段并提取为单独变量输出,同时优化逻辑确保 conversation_id 仅输出一次且不重复出现在 stream_text 中
1 parent 422d266 commit 6c47aa2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

http_request_stream/tools/http_request_stream.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import httpx
55
from json import loads
6+
import re
67

78
from dify_plugin import Tool
89
from dify_plugin.entities.tool import ToolInvokeMessage
@@ -70,7 +71,10 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
7071
raise ValueError("body must be a valid JSON string.")
7172

7273
try:
73-
text = ""
74+
# 标记是否已经提取并输出过 conversation_id,避免重复输出
75+
conversation_id_emitted = False
76+
# 预编译匹配 conversation_id 的正则表达式:匹配形如 conversation_id/xxxxx 的片段
77+
conversation_id_pattern = re.compile(r"\bconversation_id\/([^\s]+)")
7478
with httpx.stream(**stream_kwargs) as response:
7579
# 对非 2xx 状态码进行友好提示并终止流
7680
if response.status_code < 200 or response.status_code >= 300:
@@ -83,7 +87,18 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
8387
if line.startswith("data:"):
8488
line = line[5:].strip()
8589
if line:
86-
text += line
90+
# 先检测是否包含 conversation_id 片段
91+
match = conversation_id_pattern.search(line)
92+
if match:
93+
# 命中 conversation_id 片段的 chunk,不输出到 stream_text
94+
if not conversation_id_emitted:
95+
conversation_id_value = match.group(1)
96+
# 输出一次 conversation_id 变量,供后续节点引用
97+
yield self.create_variable_message("conversation_id", conversation_id_value)
98+
conversation_id_emitted = True
99+
# 跳过本次 chunk 的 stream_text 输出
100+
continue
101+
# 非 conversation_id 的 chunk,正常输出到 stream_text
87102
yield self.create_stream_variable_message("stream_text", line)
88103

89104
except httpx.HTTPError as e:

http_request_stream/tools/http_request_stream.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,7 @@ output_schema:
9696
stream_text:
9797
type: string
9898
description: The stream text generated by the tool
99+
conversation_id:
100+
type: string
101+
description: Extracted conversation id when a chunk matches the pattern "conversation_id/xxxxx"
99102

http_request_stream/working/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
## 已完成工作
1111
- [2025-11-07 16:53:37 +08:00] 完成 README 文档的全面撰写与结构化说明
1212
- [2025-11-07 17:51:36 +08:00] 新增可选 headers 参数(JSON 对象字符串),实现工具代码读取并与默认 SSE 请求头合并,支持自定义请求头覆盖默认值
13+
- [2025-11-07 18:12:22 +08:00] 新增输出参数 conversation_id:在流式 chunk 中检测形如 "conversation_id/xxxxx" 的片段,提取并以变量输出,更新 YAML 的 output_schema 与工具实现
14+
- [2025-11-07 18:23:33 +08:00] 优化 conversation_id 提取逻辑:当 chunk 匹配到 conversation_id 时不输出到 stream_text,同时保持 conversation_id 仅输出一次
1315

1416
## 待办事项
1517
- [x] 支持自定义请求头(headers 参数,JSON 字符串)
18+
- [x] 从流式 chunk 中提取并输出 conversation_id 参数
1619
- [ ] 提供认证信息示例(如 Bearer Token)
1720
- [ ] 增加更多使用示例(不同服务的 SSE 与分块响应)
1821
- [ ] 完善错误信息的指引与用户友好提示

0 commit comments

Comments
 (0)