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

@ -1264,11 +1264,12 @@ impl Channel for TelegramChannel {
message_id: &str,
text: &str,
) -> anyhow::Result<()> {
let (chat_id, _) = Self::parse_reply_target(recipient);
// Rate-limit edits per chat
{
let (chat_id_for_limit, _) = Self::parse_reply_target(recipient);
let last_edits = self.last_draft_edit.lock();
if let Some(last_time) = last_edits.get(&chat_id_for_limit) {
if let Some(last_time) = last_edits.get(&chat_id) {
let elapsed = u64::try_from(last_time.elapsed().as_millis()).unwrap_or(u64::MAX);
if elapsed < self.draft_update_interval_ms {
return Ok(());
@ -1276,8 +1277,6 @@ impl Channel for TelegramChannel {
}
}
let (chat_id, _) = Self::parse_reply_target(recipient);
// Truncate to Telegram limit for mid-stream edits (UTF-8 safe)
let display_text = if text.len() > TELEGRAM_MAX_MESSAGE_LENGTH {
let mut end = 0;
@ -1333,8 +1332,12 @@ impl Channel for TelegramChannel {
message_id: &str,
text: &str,
) -> anyhow::Result<()> {
let text = &strip_tool_call_tags(text);
let (chat_id, thread_id) = Self::parse_reply_target(recipient);
// Clean up rate-limit tracking for this chat
self.last_draft_edit.lock().remove(&chat_id);
// If text exceeds limit, delete draft and send as chunked messages
if text.len() > TELEGRAM_MAX_MESSAGE_LENGTH {
let msg_id = match message_id.parse::<i64>() {
@ -1375,7 +1378,7 @@ impl Channel for TelegramChannel {
};
// Try editing with Markdown formatting
let mut body = serde_json::json!({
let body = serde_json::json!({
"chat_id": chat_id,
"message_id": msg_id,
"text": text,
@ -1394,12 +1397,16 @@ impl Channel for TelegramChannel {
}
// Markdown failed — retry without parse_mode
body.as_object_mut().unwrap().remove("parse_mode");
let plain_body = serde_json::json!({
"chat_id": chat_id,
"message_id": msg_id,
"text": text,
});
let resp = self
.client
.post(self.api_url("editMessageText"))
.json(&body)
.json(&plain_body)
.send()
.await?;