fix(agent): relay final response as progressive chunks via on_delta

Previously on_delta sent the entire completed response as a single
message, defeating the purpose of the streaming draft updates. Now
the text is split into ~80-char chunks on whitespace boundaries
(UTF-8 safe via split_inclusive) and sent progressively through the
channel, so Telegram draft edits show text arriving incrementally.

The consumer in process_channel_message already accumulates chunks
and calls update_draft with the full text so far, and Telegram's
rate-limiting (draft_update_interval_ms) throttles editMessageText
calls to avoid API spam.
This commit is contained in:
Xiangjun Ma 2026-02-18 00:01:19 -08:00 committed by Chummy
parent 118cd53922
commit 93538a70e3

View file

@ -941,9 +941,22 @@ pub(crate) async fn run_tool_call_loop(
if tool_calls.is_empty() {
// No tool calls — this is the final response.
// If a streaming sender is provided, send the final text through it.
// If a streaming sender is provided, relay the text in small chunks
// so the channel can progressively update the draft message.
if let Some(ref tx) = on_delta {
let _ = tx.send(display_text.clone()).await;
// Split on whitespace boundaries, accumulating ~80-char chunks.
let mut chunk = String::new();
for word in display_text.split_inclusive(char::is_whitespace) {
chunk.push_str(word);
if chunk.len() >= 80 {
if tx.send(std::mem::take(&mut chunk)).await.is_err() {
break; // receiver dropped
}
}
}
if !chunk.is_empty() {
let _ = tx.send(chunk).await;
}
}
history.push(ChatMessage::assistant(response_text.clone()));
return Ok(display_text);