@@ -279,7 +279,10 @@ async def _iter_llm_responses_with_fallback(
279279 candidate_id ,
280280 )
281281 try :
282- from astrbot .core .agent .context .truncator import ContextTruncator
282+ from astrbot .core .agent .context .truncator import (
283+ ContextTruncator ,
284+ )
285+
283286 _truncator = ContextTruncator ()
284287 _before_total = len (self .run_context .messages )
285288 # Aggressively halve until small enough (up to 5 rounds)
@@ -292,21 +295,27 @@ async def _iter_llm_responses_with_fallback(
292295 _after = len (self .run_context .messages )
293296 logger .info (
294297 "Forced context truncation round %d: %d -> %d messages." ,
295- _halve_round + 1 , _before , _after ,
298+ _halve_round + 1 ,
299+ _before ,
300+ _after ,
296301 )
297302 if _after <= 4 :
298303 break # Can't shrink further
299304 # Retry same candidate
300305 try :
301- async for resp in self ._iter_llm_responses (include_model = idx == 0 ):
306+ async for resp in self ._iter_llm_responses (
307+ include_model = idx == 0
308+ ):
302309 if resp .is_chunk :
303310 has_stream_output = True
304311 yield resp
305312 continue
306313 yield resp
307314 logger .info (
308315 "Context truncation succeeded after %d round(s): %d -> %d messages." ,
309- _halve_round + 1 , _before_total , _after ,
316+ _halve_round + 1 ,
317+ _before_total ,
318+ _after ,
310319 )
311320 _compression_success = True
312321 return
@@ -327,13 +336,15 @@ async def _iter_llm_responses_with_fallback(
327336 last_exception = retry_exc
328337 logger .warning (
329338 "Chat Model %s retry after compression failed: %s" ,
330- candidate_id , retry_exc ,
339+ candidate_id ,
340+ retry_exc ,
331341 )
332342 break
333343 last_exception = retry_exc
334344 logger .warning (
335345 "Chat Model %s still token-exceeded after round %d, halving again..." ,
336- candidate_id , _halve_round + 1 ,
346+ candidate_id ,
347+ _halve_round + 1 ,
337348 )
338349 continue
339350 except Exception as compress_exc :
@@ -375,15 +386,25 @@ def follow_up(
375386 * ,
376387 message_text : str ,
377388 ) -> FollowUpTicket | None :
378- """Queue a follow-up message for the next tool result."""
389+ """Queue a follow-up message to be injected into the next tool result.
390+
391+ Returns None if the agent is already done (message arrived too late) or
392+ if the message text is empty.
393+ """
379394 if self .done ():
395+ logger .debug ("follow_up: agent already done, message discarded." )
380396 return None
381397 text = (message_text or "" ).strip ()
382398 if not text :
383399 return None
384400 ticket = FollowUpTicket (seq = self ._follow_up_seq , text = text )
385401 self ._follow_up_seq += 1
386402 self ._pending_follow_ups .append (ticket )
403+ logger .debug (
404+ "follow_up: queued ticket seq=%d, pending=%d" ,
405+ ticket .seq ,
406+ len (self ._pending_follow_ups ),
407+ )
387408 return ticket
388409
389410 def _resolve_unconsumed_follow_ups (self ) -> None :
@@ -402,14 +423,16 @@ def _consume_follow_up_notice(self) -> str:
402423 for ticket in follow_ups :
403424 ticket .consumed = True
404425 ticket .resolved .set ()
426+
405427 follow_up_lines = "\n " .join (
406428 f"{ idx } . { ticket .text } " for idx , ticket in enumerate (follow_ups , start = 1 )
407429 )
430+ count = len (follow_ups )
431+ plural = "messages" if count > 1 else "message"
408432 return (
409- "\n \n [SYSTEM NOTICE] User sent follow-up messages while tool execution "
410- "was in progress. Prioritize these follow-up instructions in your next "
411- "actions. In your very next action, briefly acknowledge to the user "
412- "that their follow-up message(s) were received before continuing.\n "
433+ f"\n \n [FOLLOW-UP x{ count } ] The user sent { count } { plural } while you were working. "
434+ "Incorporate them as supplementary instructions seamlessly — "
435+ "do NOT stop, restart, or explicitly acknowledge receipt; just act naturally.\n "
413436 f"{ follow_up_lines } "
414437 )
415438
@@ -714,7 +737,7 @@ async def step_until_done(
714737 self .run_context .messages .append (
715738 Message (
716739 role = "user" ,
717- content = "工具调用次数已达到上限,请停止使用工具,并根据已经收集到的信息,对你的任务和发现进行总结,然后直接回复用户。" ,
740+ content = "工具调用次数已达到上限,请停止使用工具,并根据已经收集到的信息,对你的任务和发现进行总结,然后直接回复用户。(Tool call limit reached. Stop using tools and summarize your findings directly for the user.) " ,
718741 )
719742 )
720743 # 再执行最后一步
0 commit comments