refactor(telegram): address code review findings

- Add strip_tool_call_tags() to finalize_draft to prevent Markdown
  parse failures from tool-call tags reaching Telegram API
- Deduplicate parse_reply_target() call in update_draft (was called
  twice, discarding thread_id both times)
- Replace body.as_object_mut().unwrap() mutation with separate
  plain_body JSON literal (eliminates unwrap in runtime path)
- Clean up per-chat rate-limit HashMap entry in finalize_draft to
  prevent unbounded growth over long uptimes
- Extract magic number 80 to STREAM_CHUNK_MIN_CHARS constant in
  agent loop
This commit is contained in:
Xiangjun Ma 2026-02-18 00:32:35 -08:00 committed by Chummy
parent e326e12039
commit f1db63219c
2 changed files with 20 additions and 9 deletions

View file

@ -15,6 +15,9 @@ use std::sync::{Arc, LazyLock};
use std::time::Instant;
use uuid::Uuid;
/// Minimum characters per chunk when relaying LLM text to a streaming draft.
const STREAM_CHUNK_MIN_CHARS: usize = 80;
/// Default maximum agentic tool-use iterations per user message to prevent runaway loops.
/// Used as a safe fallback when `max_tool_iterations` is unset or configured as zero.
const DEFAULT_MAX_TOOL_ITERATIONS: usize = 10;
@ -944,11 +947,12 @@ pub(crate) async fn run_tool_call_loop(
// 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 {
// Split on whitespace boundaries, accumulating ~80-char chunks.
// Split on whitespace boundaries, accumulating chunks of at least
// STREAM_CHUNK_MIN_CHARS characters for progressive draft updates.
let mut chunk = String::new();
for word in display_text.split_inclusive(char::is_whitespace) {
chunk.push_str(word);
if chunk.len() >= 80 {
if chunk.len() >= STREAM_CHUNK_MIN_CHARS {
if tx.send(std::mem::take(&mut chunk)).await.is_err() {
break; // receiver dropped
}