@@ -56,7 +56,7 @@ def get_conversation_status(base_url: str, conversation_id: str) -> dict:
5656
5757def format_event (event_type : str , data : dict ) -> None :
5858 if event_type == "complete" :
59- print (f"[complete] { data .get ('message' , '' )} " )
59+ print (f"[complete] { data .get ('message' , '' )} " , flush = True )
6060 return
6161
6262 if event_type == "usage_metrics" :
@@ -71,46 +71,52 @@ def format_event(event_type: str, data: dict) -> None:
7171 + token_usage .get ("completion_tokens" , 0 )
7272 + token_usage .get ("reasoning_tokens" , 0 )
7373 )
74- print (f"[usage] model={ model_name } cost_rmb={ cost :.6f} tokens={ total_tokens } " )
74+ print (
75+ f"[usage] model={ model_name } cost_rmb={ cost :.6f} tokens={ total_tokens } " ,
76+ flush = True ,
77+ )
7578 return
7679
7780 if event_type != "agent_event" :
78- print (f"[{ event_type } ] { json .dumps (data , ensure_ascii = True )} " )
81+ print (f"[{ event_type } ] { json .dumps (data , ensure_ascii = True )} " , flush = True )
7982 return
8083
8184 data_type = data .get ("type" , "unknown" )
8285 if data_type == "MessageEvent" :
8386 role = data .get ("role" , "unknown" )
8487 text = data .get ("text" , "" )
85- print (f"[message:{ role } ] { text [:400 ]} " )
88+ print (f"[message:{ role } ] { text [:400 ]} " , flush = True )
8689 return
8790
8891 if data_type == "ThoughtEvent" :
8992 thought = data .get ("thought" , data .get ("content" , "" ))
90- print (f"[thought] { thought [:200 ]} " )
93+ print (f"[thought] { thought [:200 ]} " , flush = True )
9194 return
9295
9396 if data_type == "ActionEvent" :
9497 action = data .get ("action" , {})
9598 if isinstance (action , dict ):
9699 action_name = action .get ("action" , "unknown" )
97- print (f"[action] { action_name } " )
100+ print (f"[action] { action_name } " , flush = True )
98101 else :
99- print (f"[action] { action } " )
102+ print (f"[action] { action } " , flush = True )
100103 return
101104
102105 if data_type == "ObservationEvent" :
103106 success = data .get ("success" , False )
104107 message = data .get ("message" , "" )
105108 state = "ok" if success else "error"
106- print (f"[observation:{ state } ] { message [:200 ]} " )
109+ print (f"[observation:{ state } ] { message [:200 ]} " , flush = True )
107110 return
108111
109112 if data_type == "ErrorEvent" :
110- print (f"[error] { data .get ('error' , 'unknown error' )} " )
113+ print (f"[error] { data .get ('error' , 'unknown error' )} " , flush = True )
111114 return
112115
113- print (f"[agent_event:{ data_type } ] { json .dumps (data , ensure_ascii = True )} " )
116+ print (
117+ f"[agent_event:{ data_type } ] { json .dumps (data , ensure_ascii = True )} " ,
118+ flush = True ,
119+ )
114120
115121
116122def stream_task (
@@ -132,8 +138,8 @@ def stream_task(
132138 method = "POST" ,
133139 )
134140
135- print (f"[conversation] { conversation_id } " )
136- print (f"[task] { task } " )
141+ print (f"[conversation] { conversation_id } " , flush = True )
142+ print (f"[task] { task } " , flush = True )
137143
138144 with urlopen (request , timeout = None ) as response :
139145 sse_event = None
@@ -145,7 +151,7 @@ def stream_task(
145151 try :
146152 format_event (sse_event , json .loads (sse_data ))
147153 except json .JSONDecodeError :
148- print (f"[{ sse_event } ] { sse_data } " )
154+ print (f"[{ sse_event } ] { sse_data } " , flush = True )
149155 sse_event = None
150156 sse_data = None
151157 continue
0 commit comments