@@ -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 异常,并输出到用户侧
0 commit comments