opencrabs 0.3.56

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Telegram Send Tool
//!
//! Agent-callable tool for full Telegram control: send, reply, edit, delete,
//! pin/unpin, forward, media, polls, inline buttons, chat info, moderation,
//! and reactions. Always prefer this tool over http_request — credentials
//! are handled securely.

use super::error::Result;
use super::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use crate::channels::telegram::TelegramState;
use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;
use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::*;
use teloxide::types::{
    ChatId, InlineKeyboardButton, InlineKeyboardMarkup, InputFile, MessageId, ReactionType,
    ReplyParameters, UserId,
};

/// Tool for comprehensive Telegram bot control (19 actions).
pub struct TelegramSendTool {
    telegram_state: Arc<TelegramState>,
}

impl TelegramSendTool {
    pub fn new(telegram_state: Arc<TelegramState>) -> Self {
        Self { telegram_state }
    }
}

/// Extract a required non-empty string param, returning ToolResult::error on failure.
#[allow(clippy::result_large_err)]
fn get_str<'a>(input: &'a Value, key: &str) -> std::result::Result<&'a str, ToolResult> {
    match input.get(key).and_then(|v| v.as_str()) {
        Some(s) if !s.is_empty() => Ok(s),
        _ => Err(ToolResult::error(format!(
            "Missing required parameter '{key}'."
        ))),
    }
}

/// Parse a required integer param as i64.
#[allow(clippy::result_large_err)]
fn get_id(input: &Value, key: &str) -> std::result::Result<i64, ToolResult> {
    match input.get(key).and_then(|v| v.as_i64()) {
        Some(id) => Ok(id),
        None => Err(ToolResult::error(format!(
            "Missing required parameter '{key}' (must be an integer)."
        ))),
    }
}

/// Resolve a media reference (`photo_url` / `document_url`) into a Telegram
/// `InputFile`. An HTTP(S) URL is handed to Telegram as-is (it fetches the
/// remote file). Anything else is treated as a local path: the file is read
/// into memory and uploaded directly, the same way the channel handler sends
/// generated images and voice notes. Without this, local paths were passed to
/// `InputFile::url()` and rejected as invalid URLs (#181).
#[allow(clippy::result_large_err)]
pub(crate) async fn resolve_input_file(
    reference: &str,
    label: &str,
) -> std::result::Result<InputFile, ToolResult> {
    if reference.starts_with("http://") || reference.starts_with("https://") {
        return reference
            .parse()
            .map(InputFile::url)
            .map_err(|e| ToolResult::error(format!("Invalid {label}: {e}")));
    }

    // Local file: read bytes and upload from memory.
    let path = crate::brain::tools::error::expand_tilde(reference);
    match tokio::fs::read(&path).await {
        Ok(bytes) => {
            let name = path
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_else(|| "file".to_string());
            Ok(InputFile::memory(bytes).file_name(name))
        }
        Err(e) => Err(ToolResult::error(format!(
            "Failed to read local {label} '{}': {e}",
            path.display()
        ))),
    }
}

/// Resolve chat_id: explicit param or owner fallback.
#[allow(clippy::result_large_err)]
/// Resolve a forum-topic `thread_id` for a proactive Telegram send.
///
/// Precedence:
///   1. Explicit `thread_id` field in the tool input — the agent
///      asked for a specific topic, honour it. Lets cron jobs / the
///      agent route messages to a topic OTHER than the most recent
///      one (e.g. "post the release notes in #announcements even
///      though the last message came from #dev").
///   2. Auto-lookup via `latest_thread_id_for_chat(chat_id)` — the
///      fallback that closed #130, picking up the most recently
///      stored topic so non-forum chats and routine replies still
///      land in the right place without the agent having to know.
///
/// Returns `None` when neither path produces a value (non-forum
/// chat, empty channel history, explicit value outside i32 range).
pub(crate) async fn resolve_thread_id(
    input: &Value,
    chat_id: i64,
) -> Option<teloxide::types::ThreadId> {
    if let Some(tid) = input.get("thread_id").and_then(|v| v.as_i64())
        && let Ok(tid_i32) = i32::try_from(tid)
    {
        return Some(teloxide::types::ThreadId(teloxide::types::MessageId(
            tid_i32,
        )));
    }
    crate::channels::telegram::send::latest_thread_id_for_chat(chat_id).await
}

async fn chat_or_err(input: &Value, state: &TelegramState) -> std::result::Result<i64, ToolResult> {
    if let Some(id) = input.get("chat_id").and_then(|v| v.as_i64()) {
        return Ok(id);
    }
    match state.owner_chat_id().await {
        Some(id) => Ok(id),
        None => Err(ToolResult::error(
            "No owner chat ID known yet and no 'chat_id' parameter provided. \
             The owner needs to send at least one message to the bot first, \
             or specify a chat_id."
                .to_string(),
        )),
    }
}

// Macro to early-return Ok(err_result) when a param helper returns Err.
macro_rules! pget {
    ($expr:expr) => {
        match $expr {
            Ok(v) => v,
            Err(e) => return Ok(e),
        }
    };
}

#[async_trait]
impl Tool for TelegramSendTool {
    fn name(&self) -> &str {
        "telegram_send"
    }

    fn description(&self) -> &str {
        "Full Telegram control: send messages, reply, edit, delete, pin/unpin, forward, \
         send photos/documents/locations/polls, inline buttons, get chat info, list admins, \
         check member count/status, ban/unban users, and set emoji reactions. \
         Always use telegram_send instead of http_request — credentials handled securely. \
         Requires Telegram to be connected first."
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": [
                        "send", "reply", "edit", "delete", "pin", "unpin",
                        "forward", "send_photo", "send_document", "send_location",
                        "send_poll", "send_buttons", "get_chat",
                        "get_chat_administrators", "get_chat_member_count", "get_chat_member",
                        "ban_user", "unban_user", "set_reaction", "list_topics"
                    ],
                    "description": "The Telegram action to perform. \
                        `list_topics` returns the (thread_id, topic_name) pairs the bot has \
                        observed in a forum-enabled supergroup — use this to translate a \
                        user-typed topic name like \"#announcements\" to the numeric thread_id \
                        you then pass to `send` / `reply` / `send_photo` via the `thread_id` field."
                },
                "message": {
                    "type": "string",
                    "description": "Message text (send, reply, edit, send_buttons)"
                },
                "chat_id": {
                    "type": "integer",
                    "description": "Telegram chat ID. Omit to use owner's chat."
                },
                "thread_id": {
                    "type": "integer",
                    "description": "Optional forum-topic ID for groups with topics enabled. Omit to auto-route to the most recent topic seen in the chat (the usual case for replies to ongoing conversations). Pass an explicit value to route to a DIFFERENT topic — e.g. post a release announcement in #announcements when the latest message came from #dev. Ignored for non-forum chats."
                },
                "message_id": {
                    "type": "integer",
                    "description": "Target message ID for reply/edit/delete/pin/unpin/forward/set_reaction"
                },
                "from_chat_id": {
                    "type": "integer",
                    "description": "Source chat ID for forward action"
                },
                "photo_url": {
                    "type": "string",
                    "description": "Photo for send_photo: an HTTPS URL or a local file path (e.g. /tmp/chart.png or ~/.opencrabs/out.png)"
                },
                "document_url": {
                    "type": "string",
                    "description": "Document for send_document: an HTTPS URL or a local file path (e.g. /tmp/report.pdf or ~/.opencrabs/data.csv)"
                },
                "latitude": {
                    "type": "number",
                    "description": "Latitude for send_location"
                },
                "longitude": {
                    "type": "number",
                    "description": "Longitude for send_location"
                },
                "poll_question": {
                    "type": "string",
                    "description": "Poll question text for send_poll"
                },
                "poll_options": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Array of poll option strings (2–10) for send_poll"
                },
                "buttons": {
                    "type": "array",
                    "items": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "text": {"type": "string"},
                                "callback_data": {"type": "string"}
                            }
                        }
                    },
                    "description": "2D array of button rows for send_buttons. Each button has 'text' and 'callback_data'."
                },
                "user_id": {
                    "type": "integer",
                    "description": "Telegram user ID for ban_user/unban_user"
                },
                "emoji": {
                    "type": "string",
                    "description": "Emoji for set_reaction (e.g. \"👍\")"
                }
            },
            "required": ["action"]
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::Network]
    }

    async fn execute(&self, input: Value, _context: &ToolExecutionContext) -> Result<ToolResult> {
        let action = match input.get("action").and_then(|v| v.as_str()) {
            Some(a) if !a.is_empty() => a.to_string(),
            _ => {
                return Ok(ToolResult::error(
                    "Missing required 'action' parameter.".to_string(),
                ));
            }
        };

        let bot = match self.telegram_state.bot().await {
            Some(b) => b,
            None => {
                return Ok(ToolResult::error(
                    "Telegram is not connected. Ask the user to connect Telegram first \
                     (use the telegram_connect tool)."
                        .to_string(),
                ));
            }
        };

        match action.as_str() {
            // ── send ─────────────────────────────────────────────────────────
            "send" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                // Explicit `thread_id` wins; auto-lookup is the
                // fallback for the common case (#130).
                let thread_id = resolve_thread_id(&input, chat_id).await;
                // Structured messages (tables, headings, lists, math) go through
                // the native rich path as a whole — never chunked, since a split
                // table would break. Plain prose, and any rich failure, fall back
                // to the chunked plain-text send so a message is never dropped
                // and Telegram's parser never reinterprets incidental characters.
                let sent_rich = crate::channels::telegram::rich::should_send_native_rich(&text)
                    && crate::channels::telegram::rich::api::try_send_rich(
                        &bot, chat_id, thread_id, &text,
                    )
                    .await
                    .inspect_err(|e| {
                        tracing::warn!("telegram_send: rich send failed, sending plain: {e}");
                    })
                    .is_ok();
                if !sent_rich {
                    for chunk in crate::channels::telegram::handler::split_message(&text, 4096) {
                        if let Err(e) = crate::channels::telegram::send::message_in_thread(
                            &bot,
                            ChatId(chat_id),
                            thread_id,
                            chunk,
                        )
                        .await
                        {
                            return Ok(ToolResult::error(format!("Failed to send: {e}")));
                        }
                    }
                }
                Ok(ToolResult::success(format!(
                    "Message sent to chat {chat_id}."
                )))
            }

            // ── reply ────────────────────────────────────────────────────────
            "reply" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                let thread_id = resolve_thread_id(&input, chat_id).await;
                match crate::channels::telegram::send::message_in_thread(
                    &bot,
                    ChatId(chat_id),
                    thread_id,
                    text,
                )
                .reply_parameters(ReplyParameters::new(MessageId(message_id as i32)))
                .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Reply sent to message {message_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to reply: {e}"))),
                }
            }

            // ── edit ─────────────────────────────────────────────────────────
            "edit" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .edit_message_text(ChatId(chat_id), MessageId(message_id as i32), text)
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!("Message {message_id} edited."))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to edit: {e}"))),
                }
            }

            // ── delete ───────────────────────────────────────────────────────
            "delete" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .delete_message(ChatId(chat_id), MessageId(message_id as i32))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Message {message_id} deleted."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to delete: {e}"))),
                }
            }

            // ── pin ──────────────────────────────────────────────────────────
            "pin" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .pin_chat_message(ChatId(chat_id), MessageId(message_id as i32))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!("Message {message_id} pinned."))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to pin: {e}"))),
                }
            }

            // ── unpin ────────────────────────────────────────────────────────
            "unpin" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.unpin_chat_message(ChatId(chat_id)).await {
                    Ok(_) => Ok(ToolResult::success(
                        "Latest pinned message unpinned.".to_string(),
                    )),
                    Err(e) => Ok(ToolResult::error(format!("Failed to unpin: {e}"))),
                }
            }

            // ── forward ──────────────────────────────────────────────────────
            "forward" => {
                let to_chat = pget!(chat_or_err(&input, &self.telegram_state).await);
                let from_chat = pget!(get_id(&input, "from_chat_id"));
                let message_id = pget!(get_id(&input, "message_id"));
                match bot
                    .forward_message(
                        ChatId(to_chat),
                        ChatId(from_chat),
                        MessageId(message_id as i32),
                    )
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Message {message_id} forwarded from chat {from_chat} to {to_chat}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to forward: {e}"))),
                }
            }

            // ── send_photo ───────────────────────────────────────────────────
            "send_photo" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let reference = pget!(get_str(&input, "photo_url")).to_string();
                let file = pget!(resolve_input_file(&reference, "photo_url").await);
                match bot.send_photo(ChatId(chat_id), file).await {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Photo sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send photo: {e}"))),
                }
            }

            // ── send_document ────────────────────────────────────────────────
            "send_document" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let reference = pget!(get_str(&input, "document_url")).to_string();
                let file = pget!(resolve_input_file(&reference, "document_url").await);
                match bot.send_document(ChatId(chat_id), file).await {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Document sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send document: {e}"))),
                }
            }

            // ── send_location ────────────────────────────────────────────────
            "send_location" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let lat = match input.get("latitude").and_then(|v| v.as_f64()) {
                    Some(v) => v,
                    None => {
                        return Ok(ToolResult::error(
                            "Missing required 'latitude' parameter.".to_string(),
                        ));
                    }
                };
                let lng = match input.get("longitude").and_then(|v| v.as_f64()) {
                    Some(v) => v,
                    None => {
                        return Ok(ToolResult::error(
                            "Missing required 'longitude' parameter.".to_string(),
                        ));
                    }
                };
                match bot.send_location(ChatId(chat_id), lat, lng).await {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Location ({lat}, {lng}) sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send location: {e}"))),
                }
            }

            // ── send_poll ────────────────────────────────────────────────────
            "send_poll" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let question = pget!(get_str(&input, "poll_question")).to_string();
                let opts: Vec<String> = match input.get("poll_options").and_then(|v| v.as_array()) {
                    Some(arr) => arr
                        .iter()
                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
                        .collect(),
                    None => {
                        return Ok(ToolResult::error(
                            "Missing required 'poll_options' parameter.".to_string(),
                        ));
                    }
                };
                if opts.len() < 2 {
                    return Ok(ToolResult::error(
                        "'poll_options' must have at least 2 options.".to_string(),
                    ));
                }
                let poll_opts: Vec<teloxide::types::InputPollOption> =
                    opts.into_iter().map(|s| s.into()).collect();
                match bot.send_poll(ChatId(chat_id), question, poll_opts).await {
                    Ok(_) => Ok(ToolResult::success(format!("Poll sent to chat {chat_id}."))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to send poll: {e}"))),
                }
            }

            // ── send_buttons ─────────────────────────────────────────────────
            "send_buttons" => {
                let text = pget!(get_str(&input, "message")).to_string();
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let rows: Vec<Vec<InlineKeyboardButton>> =
                    match input.get("buttons").and_then(|v| v.as_array()) {
                        Some(outer) => outer
                            .iter()
                            .filter_map(|row| row.as_array())
                            .map(|row| {
                                row.iter()
                                    .filter_map(|btn| {
                                        let text =
                                            btn.get("text").and_then(|v| v.as_str())?.to_string();
                                        let data = btn
                                            .get("callback_data")
                                            .and_then(|v| v.as_str())?
                                            .to_string();
                                        Some(InlineKeyboardButton::callback(text, data))
                                    })
                                    .collect()
                            })
                            .collect(),
                        None => {
                            return Ok(ToolResult::error(
                                "Missing required 'buttons' parameter.".to_string(),
                            ));
                        }
                    };
                let keyboard = InlineKeyboardMarkup::new(rows);
                match bot
                    .send_message(ChatId(chat_id), text)
                    .reply_markup(keyboard)
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Message with buttons sent to chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!(
                        "Failed to send message with buttons: {e}"
                    ))),
                }
            }

            // ── get_chat ─────────────────────────────────────────────────────
            "get_chat" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.get_chat(ChatId(chat_id)).await {
                    Ok(chat) => {
                        let info = format!(
                            "Chat {}: type={:?}, title={:?}",
                            chat.id,
                            chat.kind,
                            chat.title()
                        );
                        Ok(ToolResult::success(info))
                    }
                    Err(e) => Ok(ToolResult::error(format!("Failed to get chat: {e}"))),
                }
            }

            // ── get_chat_administrators ────────────────────────────────────
            "get_chat_administrators" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.get_chat_administrators(ChatId(chat_id)).await {
                    Ok(admins) => {
                        let lines: Vec<String> = admins
                            .iter()
                            .map(|m| {
                                let u = &m.user;
                                let role = match m.kind {
                                    teloxide::types::ChatMemberKind::Owner { .. } => "owner",
                                    teloxide::types::ChatMemberKind::Administrator { .. } => {
                                        "admin"
                                    }
                                    _ => "member",
                                };
                                let handle = u
                                    .username
                                    .as_ref()
                                    .map(|h| format!(" @{h}"))
                                    .unwrap_or_default();
                                format!("- {} (id={}){} [{}]", u.first_name, u.id, handle, role)
                            })
                            .collect();
                        Ok(ToolResult::success(format!(
                            "Chat {} administrators ({}):\n{}",
                            chat_id,
                            admins.len(),
                            lines.join("\n")
                        )))
                    }
                    Err(e) => Ok(ToolResult::error(format!(
                        "Failed to get administrators: {e}"
                    ))),
                }
            }

            // ── get_chat_member_count ─────────────────────────────────────────
            "get_chat_member_count" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                match bot.get_chat_member_count(ChatId(chat_id)).await {
                    Ok(count) => Ok(ToolResult::success(format!(
                        "Chat {chat_id} has {count} members."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!(
                        "Failed to get member count: {e}"
                    ))),
                }
            }

            // ── get_chat_member ───────────────────────────────────────────────
            "get_chat_member" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let uid = pget!(get_id(&input, "user_id"));
                match bot
                    .get_chat_member(ChatId(chat_id), UserId(uid as u64))
                    .await
                {
                    Ok(member) => {
                        let u = &member.user;
                        let status = match member.kind {
                            teloxide::types::ChatMemberKind::Owner { .. } => "owner",
                            teloxide::types::ChatMemberKind::Administrator { .. } => {
                                "administrator"
                            }
                            teloxide::types::ChatMemberKind::Member(_) => "member",
                            teloxide::types::ChatMemberKind::Restricted { .. } => "restricted",
                            teloxide::types::ChatMemberKind::Left => "left",
                            teloxide::types::ChatMemberKind::Banned { .. } => "banned",
                        };
                        let handle = u
                            .username
                            .as_ref()
                            .map(|h| format!(" @{h}"))
                            .unwrap_or_default();
                        Ok(ToolResult::success(format!(
                            "User {} (id={}){}: status={}",
                            u.first_name, u.id, handle, status
                        )))
                    }
                    Err(e) => Ok(ToolResult::error(format!("Failed to get chat member: {e}"))),
                }
            }

            // ── ban_user ─────────────────────────────────────────────────────
            "ban_user" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let user_id = pget!(get_id(&input, "user_id"));
                match bot
                    .ban_chat_member(ChatId(chat_id), UserId(user_id as u64))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "User {user_id} banned from chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to ban user: {e}"))),
                }
            }

            // ── unban_user ───────────────────────────────────────────────────
            "unban_user" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let user_id = pget!(get_id(&input, "user_id"));
                match bot
                    .unban_chat_member(ChatId(chat_id), UserId(user_id as u64))
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "User {user_id} unbanned from chat {chat_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to unban user: {e}"))),
                }
            }

            // ── set_reaction ─────────────────────────────────────────────────
            "set_reaction" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let message_id = pget!(get_id(&input, "message_id"));
                let emoji = pget!(get_str(&input, "emoji")).to_string();
                let reactions = vec![ReactionType::Emoji {
                    emoji: emoji.clone(),
                }];
                match bot
                    .set_message_reaction(ChatId(chat_id), MessageId(message_id as i32))
                    .reaction(reactions)
                    .await
                {
                    Ok(_) => Ok(ToolResult::success(format!(
                        "Reaction {emoji} set on message {message_id}."
                    ))),
                    Err(e) => Ok(ToolResult::error(format!("Failed to set reaction: {e}"))),
                }
            }

            // ── list_topics ──────────────────────────────────────────────────
            "list_topics" => {
                let chat_id = pget!(chat_or_err(&input, &self.telegram_state).await);
                let Some(pool) = crate::db::global_pool() else {
                    return Ok(ToolResult::error(
                        "Channel message store unavailable (DB not initialised).".to_string(),
                    ));
                };
                let repo = crate::db::ChannelMessageRepository::new(pool.clone());
                let chat_id_str = chat_id.to_string();
                let topics = match repo.topics_for_chat("telegram", &chat_id_str).await {
                    Ok(t) => t,
                    Err(e) => {
                        return Ok(ToolResult::error(format!("Failed to list topics: {e}")));
                    }
                };
                if topics.is_empty() {
                    return Ok(ToolResult::success(format!(
                        "No forum topics observed yet for chat {chat_id}. \
                         Telegram's Bot API has no listForumTopics endpoint — the bot only \
                         learns topic names from messages it sees. Ask a user to post once in \
                         each topic so the bot can capture their names, then retry."
                    )));
                }
                // Render a compact human/agent-readable table.
                let mut out = format!(
                    "Topics in chat {chat_id} (only those the bot has seen activity in):\n"
                );
                out.push_str("  thread_id | topic_name              | messages | last_seen\n");
                for t in &topics {
                    let name = t.topic_name.as_deref().unwrap_or("(unknown)");
                    // Convert epoch seconds (the schema's storage
                    // format for created_at) to a human-readable
                    // UTC timestamp so the agent and any user
                    // reading the output don't have to decode.
                    let last_seen = chrono::DateTime::from_timestamp(t.last_message_at, 0)
                        .map(|dt| dt.format("%Y-%m-%d %H:%M UTC").to_string())
                        .unwrap_or_else(|| t.last_message_at.to_string());
                    out.push_str(&format!(
                        "  {:<9} | {:<23} | {:>8} | {}\n",
                        t.thread_id,
                        name.chars().take(23).collect::<String>(),
                        t.message_count,
                        last_seen,
                    ));
                }
                out.push_str(
                    "\nPass the thread_id back into `send` / `reply` / `send_photo` etc. \
                     via the optional `thread_id` field to route a message into a specific topic.",
                );
                Ok(ToolResult::success(out))
            }

            unknown => Ok(ToolResult::error(format!(
                "Unknown action '{unknown}'. Valid actions: send, reply, edit, delete, pin, \
                 unpin, forward, send_photo, send_document, send_location, send_poll, \
                 send_buttons, get_chat, get_chat_administrators, get_chat_member_count, \
                 get_chat_member, ban_user, unban_user, set_reaction, list_topics"
            ))),
        }
    }
}