orra 0.0.2

Context-aware agent session management for any application
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use async_trait::async_trait;
use reqwest::Client;
use serde::Deserialize;

use crate::tool::{Tool, ToolDefinition, ToolError, ToolRegistry};

// ---------------------------------------------------------------------------
// Shared Discord client config
// ---------------------------------------------------------------------------

/// Configuration for Discord bot API access. Shared across all Discord tools.
#[derive(Clone)]
pub struct DiscordConfig {
    client: Client,
    token: String,
    api_base: String,
}

impl DiscordConfig {
    /// Create a new Discord config with a bot token.
    ///
    /// The token should be the raw bot token (no "Bot " prefix — that's added
    /// automatically in request headers).
    pub fn new(token: impl Into<String>) -> Self {
        Self {
            client: Client::new(),
            token: token.into(),
            api_base: "https://discord.com/api/v10".into(),
        }
    }

    pub fn with_client(mut self, client: Client) -> Self {
        self.client = client;
        self
    }

    pub fn with_api_base(mut self, base: impl Into<String>) -> Self {
        self.api_base = base.into();
        self
    }

    pub fn token(&self) -> &str {
        &self.token
    }

    pub fn api_base(&self) -> &str {
        &self.api_base
    }

    pub fn url(&self, path: &str) -> String {
        format!("{}/{}", self.api_base, path)
    }

    pub fn request(&self, method: reqwest::Method, path: &str) -> reqwest::RequestBuilder {
        self.client
            .request(method, self.url(path))
            .header("Authorization", format!("Bot {}", self.token))
            .header("Content-Type", "application/json")
            .header("User-Agent", "orra")
    }
}

// ---------------------------------------------------------------------------
// Discord API response types
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct Channel {
    id: String,
    name: Option<String>,
    #[serde(rename = "type")]
    channel_type: u8,
    topic: Option<String>,
    parent_id: Option<String>,
    position: Option<i32>,
}

impl Channel {
    fn type_name(&self) -> &str {
        match self.channel_type {
            0 => "text",
            2 => "voice",
            4 => "category",
            5 => "announcement",
            13 => "stage",
            15 => "forum",
            _ => "other",
        }
    }
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct DiscordMessage {
    id: String,
    content: String,
    author: DiscordUser,
    timestamp: String,
    edited_timestamp: Option<String>,
    pinned: bool,
    #[serde(rename = "type")]
    message_type: u8,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct DiscordUser {
    id: String,
    username: String,
    discriminator: Option<String>,
    global_name: Option<String>,
}

impl DiscordUser {
    fn display_name(&self) -> &str {
        self.global_name
            .as_deref()
            .unwrap_or(&self.username)
    }
}

#[derive(Debug, Deserialize)]
struct Guild {
    id: String,
    name: String,
    owner_id: String,
    approximate_member_count: Option<u64>,
}

// ---------------------------------------------------------------------------
// Tools
// ---------------------------------------------------------------------------

pub struct ListChannelsTool {
    dc: DiscordConfig,
}

impl ListChannelsTool {
    pub fn new(dc: DiscordConfig) -> Self {
        Self { dc }
    }
}

#[async_trait]
impl Tool for ListChannelsTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "list_channels".into(),
            description: "List all channels in a Discord server (guild). Returns channel names, types, and IDs.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "guild_id": {
                        "type": "string",
                        "description": "The Discord server (guild) ID"
                    }
                },
                "required": ["guild_id"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let guild_id = input
            .get("guild_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'guild_id'".into()))?;

        let resp = self
            .dc
            .request(reqwest::Method::GET, &format!("guilds/{}/channels", guild_id))
            .send()
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(ToolError::ExecutionFailed(format!(
                "Discord API {}: {}",
                status, body
            )));
        }

        let channels: Vec<Channel> = resp
            .json()
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("parse error: {}", e)))?;

        if channels.is_empty() {
            return Ok("No channels found in this server.".into());
        }

        // Group by category
        let categories: Vec<&Channel> = channels
            .iter()
            .filter(|c| c.channel_type == 4)
            .collect();

        let mut lines = Vec::new();

        // Channels under categories
        for cat in &categories {
            lines.push(format!(
                "\n📁 {} (id: {})",
                cat.name.as_deref().unwrap_or("unnamed"),
                cat.id
            ));

            let children: Vec<&Channel> = channels
                .iter()
                .filter(|c| c.parent_id.as_deref() == Some(&cat.id) && c.channel_type != 4)
                .collect();

            for ch in children {
                let prefix = match ch.channel_type {
                    0 => "#",
                    2 => "🔊",
                    5 => "📢",
                    13 => "🎤",
                    15 => "💬",
                    _ => "·",
                };
                let topic_str = ch
                    .topic
                    .as_deref()
                    .filter(|t| !t.is_empty())
                    .map(|t| format!("{}", t))
                    .unwrap_or_default();
                lines.push(format!(
                    "  {} {} [{}] (id: {}){}",
                    prefix,
                    ch.name.as_deref().unwrap_or("unnamed"),
                    ch.type_name(),
                    ch.id,
                    topic_str
                ));
            }
        }

        // Uncategorized channels
        let uncategorized: Vec<&Channel> = channels
            .iter()
            .filter(|c| c.parent_id.is_none() && c.channel_type != 4)
            .collect();

        if !uncategorized.is_empty() {
            lines.push("\n(uncategorized)".into());
            for ch in uncategorized {
                lines.push(format!(
                    "  #{} [{}] (id: {})",
                    ch.name.as_deref().unwrap_or("unnamed"),
                    ch.type_name(),
                    ch.id,
                ));
            }
        }

        Ok(lines.join("\n").trim_start().to_string())
    }
}

pub struct GetChannelInfoTool {
    dc: DiscordConfig,
}

impl GetChannelInfoTool {
    pub fn new(dc: DiscordConfig) -> Self {
        Self { dc }
    }
}

#[async_trait]
impl Tool for GetChannelInfoTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "get_channel_info".into(),
            description: "Get detailed information about a specific Discord channel.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "channel_id": {
                        "type": "string",
                        "description": "The channel ID"
                    }
                },
                "required": ["channel_id"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let channel_id = input
            .get("channel_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'channel_id'".into()))?;

        let resp = self
            .dc
            .request(reqwest::Method::GET, &format!("channels/{}", channel_id))
            .send()
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(ToolError::ExecutionFailed(format!(
                "Discord API {}: {}",
                status, body
            )));
        }

        let ch: Channel = resp
            .json()
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("parse error: {}", e)))?;

        let topic = ch
            .topic
            .as_deref()
            .filter(|t| !t.is_empty())
            .unwrap_or("(none)");

        Ok(format!(
            "#{} (id: {})\ntype: {}\ntopic: {}",
            ch.name.as_deref().unwrap_or("unnamed"),
            ch.id,
            ch.type_name(),
            topic,
        ))
    }
}

pub struct GetMessagesTool {
    dc: DiscordConfig,
}

impl GetMessagesTool {
    pub fn new(dc: DiscordConfig) -> Self {
        Self { dc }
    }
}

#[async_trait]
impl Tool for GetMessagesTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "get_messages".into(),
            description: "Get recent messages from a Discord channel. Returns message content, author, and timestamps.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "channel_id": {
                        "type": "string",
                        "description": "The channel ID to read messages from"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Number of messages to fetch (1-100). Defaults to 20."
                    },
                    "before": {
                        "type": "string",
                        "description": "Get messages before this message ID (for pagination)"
                    }
                },
                "required": ["channel_id"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let channel_id = input
            .get("channel_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'channel_id'".into()))?;

        let limit = input
            .get("limit")
            .and_then(|v| v.as_u64())
            .unwrap_or(20)
            .min(100);

        let mut req = self
            .dc
            .request(reqwest::Method::GET, &format!("channels/{}/messages", channel_id))
            .query(&[("limit", &limit.to_string())]);

        if let Some(before) = input.get("before").and_then(|v| v.as_str()) {
            req = req.query(&[("before", before)]);
        }

        let resp = req
            .send()
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(ToolError::ExecutionFailed(format!(
                "Discord API {}: {}",
                status, body
            )));
        }

        let messages: Vec<DiscordMessage> = resp
            .json()
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("parse error: {}", e)))?;

        if messages.is_empty() {
            return Ok("No messages found in this channel.".into());
        }

        // Discord returns newest first; reverse for chronological reading
        let mut lines = Vec::new();
        for msg in messages.iter().rev() {
            let edited = if msg.edited_timestamp.is_some() {
                " (edited)"
            } else {
                ""
            };
            let pinned = if msg.pinned { " 📌" } else { "" };
            lines.push(format!(
                "[{}] @{}{}{}: {}",
                msg.timestamp,
                msg.author.display_name(),
                pinned,
                edited,
                msg.content,
            ));
        }

        Ok(lines.join("\n"))
    }
}

pub struct SendMessageTool {
    dc: DiscordConfig,
}

impl SendMessageTool {
    pub fn new(dc: DiscordConfig) -> Self {
        Self { dc }
    }
}

#[async_trait]
impl Tool for SendMessageTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "send_message".into(),
            description: "Send a message to a Discord channel.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "channel_id": {
                        "type": "string",
                        "description": "The channel ID to send the message to"
                    },
                    "content": {
                        "type": "string",
                        "description": "The message text (up to 2000 characters)"
                    }
                },
                "required": ["channel_id", "content"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let channel_id = input
            .get("channel_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'channel_id'".into()))?;

        let content = input
            .get("content")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'content'".into()))?;

        if content.len() > 2000 {
            return Err(ToolError::InvalidInput(
                "message content exceeds 2000 character limit".into(),
            ));
        }

        let resp = self
            .dc
            .request(reqwest::Method::POST, &format!("channels/{}/messages", channel_id))
            .json(&serde_json::json!({ "content": content }))
            .send()
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(ToolError::ExecutionFailed(format!(
                "Discord API {}: {}",
                status, body
            )));
        }

        let msg: DiscordMessage = resp
            .json()
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("parse error: {}", e)))?;

        Ok(format!("Sent message {} in channel {}.", msg.id, channel_id))
    }
}

pub struct ReplyToMessageTool {
    dc: DiscordConfig,
}

impl ReplyToMessageTool {
    pub fn new(dc: DiscordConfig) -> Self {
        Self { dc }
    }
}

#[async_trait]
impl Tool for ReplyToMessageTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "reply_to_message".into(),
            description: "Reply to a specific message in a Discord channel. The reply will show as a threaded response.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "channel_id": {
                        "type": "string",
                        "description": "The channel ID containing the message"
                    },
                    "message_id": {
                        "type": "string",
                        "description": "The ID of the message to reply to"
                    },
                    "content": {
                        "type": "string",
                        "description": "The reply text (up to 2000 characters)"
                    }
                },
                "required": ["channel_id", "message_id", "content"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let channel_id = input
            .get("channel_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'channel_id'".into()))?;

        let message_id = input
            .get("message_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'message_id'".into()))?;

        let content = input
            .get("content")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'content'".into()))?;

        if content.len() > 2000 {
            return Err(ToolError::InvalidInput(
                "message content exceeds 2000 character limit".into(),
            ));
        }

        let resp = self
            .dc
            .request(reqwest::Method::POST, &format!("channels/{}/messages", channel_id))
            .json(&serde_json::json!({
                "content": content,
                "message_reference": {
                    "message_id": message_id
                }
            }))
            .send()
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(ToolError::ExecutionFailed(format!(
                "Discord API {}: {}",
                status, body
            )));
        }

        let msg: DiscordMessage = resp
            .json()
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("parse error: {}", e)))?;

        Ok(format!(
            "Replied to message {} with message {} in channel {}.",
            message_id, msg.id, channel_id
        ))
    }
}

pub struct GetGuildInfoTool {
    dc: DiscordConfig,
}

impl GetGuildInfoTool {
    pub fn new(dc: DiscordConfig) -> Self {
        Self { dc }
    }
}

#[async_trait]
impl Tool for GetGuildInfoTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "get_guild_info".into(),
            description: "Get information about a Discord server (guild), including name, owner, and member count.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "guild_id": {
                        "type": "string",
                        "description": "The Discord server (guild) ID"
                    }
                },
                "required": ["guild_id"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let guild_id = input
            .get("guild_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'guild_id'".into()))?;

        let resp = self
            .dc
            .request(
                reqwest::Method::GET,
                &format!("guilds/{}?with_counts=true", guild_id),
            )
            .send()
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(ToolError::ExecutionFailed(format!(
                "Discord API {}: {}",
                status, body
            )));
        }

        let guild: Guild = resp
            .json()
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("parse error: {}", e)))?;

        let members = guild
            .approximate_member_count
            .map(|c| format!("\nmembers: ~{}", c))
            .unwrap_or_default();

        Ok(format!(
            "{} (id: {})\nowner: {}{}",
            guild.name, guild.id, guild.owner_id, members,
        ))
    }
}

// ---------------------------------------------------------------------------
// Convenience registration
// ---------------------------------------------------------------------------

/// Register all Discord tools into a ToolRegistry.
pub fn register_tools(registry: &mut ToolRegistry, config: &DiscordConfig) {
    registry.register(Box::new(ListChannelsTool::new(config.clone())));
    registry.register(Box::new(GetChannelInfoTool::new(config.clone())));
    registry.register(Box::new(GetMessagesTool::new(config.clone())));
    registry.register(Box::new(SendMessageTool::new(config.clone())));
    registry.register(Box::new(ReplyToMessageTool::new(config.clone())));
    registry.register(Box::new(GetGuildInfoTool::new(config.clone())));
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn discord_config_url() {
        let config = DiscordConfig::new("tok123");
        assert_eq!(
            config.url("channels/456/messages"),
            "https://discord.com/api/v10/channels/456/messages"
        );
        assert_eq!(
            config.url("guilds/789/channels"),
            "https://discord.com/api/v10/guilds/789/channels"
        );
    }

    #[test]
    fn discord_config_custom_base() {
        let config = DiscordConfig::new("tok").with_api_base("http://localhost:9999");
        assert_eq!(
            config.url("channels/1/messages"),
            "http://localhost:9999/channels/1/messages"
        );
    }

    #[test]
    fn discord_config_accessors() {
        let config = DiscordConfig::new("tok");
        assert_eq!(config.api_base(), "https://discord.com/api/v10");
    }

    #[test]
    fn channel_type_names() {
        let ch = |t: u8| Channel {
            id: "1".into(),
            name: None,
            channel_type: t,
            topic: None,
            parent_id: None,
            position: None,
        };

        assert_eq!(ch(0).type_name(), "text");
        assert_eq!(ch(2).type_name(), "voice");
        assert_eq!(ch(4).type_name(), "category");
        assert_eq!(ch(5).type_name(), "announcement");
        assert_eq!(ch(13).type_name(), "stage");
        assert_eq!(ch(15).type_name(), "forum");
        assert_eq!(ch(99).type_name(), "other");
    }

    #[test]
    fn user_display_name_prefers_global() {
        let user = DiscordUser {
            id: "1".into(),
            username: "alice_99".into(),
            discriminator: None,
            global_name: Some("Alice".into()),
        };
        assert_eq!(user.display_name(), "Alice");
    }

    #[test]
    fn user_display_name_falls_back_to_username() {
        let user = DiscordUser {
            id: "1".into(),
            username: "alice_99".into(),
            discriminator: None,
            global_name: None,
        };
        assert_eq!(user.display_name(), "alice_99");
    }

    #[test]
    fn register_tools_adds_all_six() {
        let config = DiscordConfig::new("tok");
        let mut registry = ToolRegistry::new();
        register_tools(&mut registry, &config);

        assert_eq!(registry.len(), 6);
        assert!(registry.get("list_channels").is_some());
        assert!(registry.get("get_channel_info").is_some());
        assert!(registry.get("get_messages").is_some());
        assert!(registry.get("send_message").is_some());
        assert!(registry.get("reply_to_message").is_some());
        assert!(registry.get("get_guild_info").is_some());
    }

    #[test]
    fn tool_definitions_have_schemas() {
        let config = DiscordConfig::new("tok");
        let tools: Vec<Box<dyn Tool>> = vec![
            Box::new(ListChannelsTool::new(config.clone())),
            Box::new(GetChannelInfoTool::new(config.clone())),
            Box::new(GetMessagesTool::new(config.clone())),
            Box::new(SendMessageTool::new(config.clone())),
            Box::new(ReplyToMessageTool::new(config.clone())),
            Box::new(GetGuildInfoTool::new(config)),
        ];

        for tool in &tools {
            let def = tool.definition();
            assert!(!def.name.is_empty());
            assert!(!def.description.is_empty());
            assert_eq!(def.input_schema["type"], "object");
        }
    }

    #[tokio::test]
    async fn send_message_validates_length() {
        let config = DiscordConfig::new("tok");
        let tool = SendMessageTool::new(config);

        let long_content = "x".repeat(2001);
        let err = tool
            .execute(serde_json::json!({
                "channel_id": "123",
                "content": long_content
            }))
            .await
            .unwrap_err();

        assert!(matches!(err, ToolError::InvalidInput(_)));
        assert!(err.to_string().contains("2000"));
    }

    #[tokio::test]
    async fn reply_validates_length() {
        let config = DiscordConfig::new("tok");
        let tool = ReplyToMessageTool::new(config);

        let long_content = "x".repeat(2001);
        let err = tool
            .execute(serde_json::json!({
                "channel_id": "123",
                "message_id": "456",
                "content": long_content
            }))
            .await
            .unwrap_err();

        assert!(matches!(err, ToolError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn send_message_missing_fields() {
        let config = DiscordConfig::new("tok");
        let tool = SendMessageTool::new(config);

        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));

        let config2 = DiscordConfig::new("tok");
        let tool2 = SendMessageTool::new(config2);
        let err2 = tool2
            .execute(serde_json::json!({"channel_id": "123"}))
            .await
            .unwrap_err();
        assert!(matches!(err2, ToolError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn list_channels_missing_guild_id() {
        let config = DiscordConfig::new("tok");
        let tool = ListChannelsTool::new(config);
        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn get_messages_missing_channel_id() {
        let config = DiscordConfig::new("tok");
        let tool = GetMessagesTool::new(config);
        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn get_channel_info_missing_channel_id() {
        let config = DiscordConfig::new("tok");
        let tool = GetChannelInfoTool::new(config);
        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn get_guild_info_missing_guild_id() {
        let config = DiscordConfig::new("tok");
        let tool = GetGuildInfoTool::new(config);
        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));
    }

    #[tokio::test]
    async fn reply_missing_fields() {
        let config = DiscordConfig::new("tok");
        let tool = ReplyToMessageTool::new(config);

        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));

        let config2 = DiscordConfig::new("tok");
        let tool2 = ReplyToMessageTool::new(config2);
        let err2 = tool2
            .execute(serde_json::json!({"channel_id": "1", "message_id": "2"}))
            .await
            .unwrap_err();
        assert!(matches!(err2, ToolError::InvalidInput(_)));
    }
}