praisonai 0.2.0

Core library for PraisonAI - Agent, Tools, Workflows
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
//! Bots Module for PraisonAI Rust SDK
//!
//! Defines protocols and types for messaging bot implementations.
//! Enables agents to communicate through messaging platforms like
//! Telegram, Discord, Slack, etc.
//!
//! # Example
//!
//! ```rust,ignore
//! use praisonai::bots::{BotMessage, BotUser, BotChannel, MessageType};
//!
//! let user = BotUser::new("user-123")
//!     .username("john_doe")
//!     .display_name("John Doe");
//!
//! let message = BotMessage::text("Hello!", user.clone(), "channel-1");
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

use crate::agent::Agent;
use crate::error::Result;

/// Types of bot messages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageType {
    Text,
    Image,
    Audio,
    Video,
    File,
    Location,
    Sticker,
    Command,
    Callback,
    Reaction,
    Reply,
    Edit,
    Delete,
}

impl Default for MessageType {
    fn default() -> Self {
        Self::Text
    }
}

impl std::fmt::Display for MessageType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Text => "text",
            Self::Image => "image",
            Self::Audio => "audio",
            Self::Video => "video",
            Self::File => "file",
            Self::Location => "location",
            Self::Sticker => "sticker",
            Self::Command => "command",
            Self::Callback => "callback",
            Self::Reaction => "reaction",
            Self::Reply => "reply",
            Self::Edit => "edit",
            Self::Delete => "delete",
        };
        write!(f, "{}", s)
    }
}

/// Get current timestamp in seconds since UNIX epoch.
fn current_timestamp() -> f64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs_f64())
        .unwrap_or(0.0)
}

/// Represents a user in a messaging platform.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BotUser {
    /// Platform-specific user identifier
    pub user_id: String,
    /// User's username (if available)
    pub username: Option<String>,
    /// User's display name
    pub display_name: Option<String>,
    /// Whether this user is a bot
    pub is_bot: bool,
    /// Additional platform-specific metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl BotUser {
    /// Create a new bot user.
    pub fn new(user_id: impl Into<String>) -> Self {
        Self {
            user_id: user_id.into(),
            username: None,
            display_name: None,
            is_bot: false,
            metadata: HashMap::new(),
        }
    }

    /// Set username.
    pub fn username(mut self, username: impl Into<String>) -> Self {
        self.username = Some(username.into());
        self
    }

    /// Set display name.
    pub fn display_name(mut self, name: impl Into<String>) -> Self {
        self.display_name = Some(name.into());
        self
    }

    /// Set is_bot flag.
    pub fn is_bot(mut self, is_bot: bool) -> Self {
        self.is_bot = is_bot;
        self
    }

    /// Add metadata.
    pub fn metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Convert to dictionary.
    pub fn to_dict(&self) -> HashMap<String, serde_json::Value> {
        let mut map = HashMap::new();
        map.insert("user_id".to_string(), serde_json::json!(self.user_id));
        map.insert("username".to_string(), serde_json::json!(self.username));
        map.insert("display_name".to_string(), serde_json::json!(self.display_name));
        map.insert("is_bot".to_string(), serde_json::json!(self.is_bot));
        map.insert("metadata".to_string(), serde_json::json!(self.metadata));
        map
    }
}

/// Represents a channel/chat in a messaging platform.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BotChannel {
    /// Platform-specific channel identifier
    pub channel_id: String,
    /// Channel name (if available)
    pub name: Option<String>,
    /// Type of channel (dm, group, channel, thread)
    pub channel_type: String,
    /// Additional platform-specific metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl BotChannel {
    /// Create a new bot channel.
    pub fn new(channel_id: impl Into<String>) -> Self {
        Self {
            channel_id: channel_id.into(),
            name: None,
            channel_type: "dm".to_string(),
            metadata: HashMap::new(),
        }
    }

    /// Set channel name.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set channel type.
    pub fn channel_type(mut self, channel_type: impl Into<String>) -> Self {
        self.channel_type = channel_type.into();
        self
    }

    /// Add metadata.
    pub fn metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Convert to dictionary.
    pub fn to_dict(&self) -> HashMap<String, serde_json::Value> {
        let mut map = HashMap::new();
        map.insert("channel_id".to_string(), serde_json::json!(self.channel_id));
        map.insert("name".to_string(), serde_json::json!(self.name));
        map.insert("channel_type".to_string(), serde_json::json!(self.channel_type));
        map.insert("metadata".to_string(), serde_json::json!(self.metadata));
        map
    }
}

/// Represents a message in a messaging platform.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BotMessage {
    /// Platform-specific message identifier
    pub message_id: String,
    /// Message content (text or structured data)
    pub content: serde_json::Value,
    /// Type of message
    pub message_type: MessageType,
    /// User who sent the message
    pub sender: Option<BotUser>,
    /// Channel where the message was sent
    pub channel: Option<BotChannel>,
    /// Message timestamp
    pub timestamp: f64,
    /// ID of message being replied to
    pub reply_to: Option<String>,
    /// Thread identifier (for threaded conversations)
    pub thread_id: Option<String>,
    /// List of attachment URLs or data
    pub attachments: Vec<serde_json::Value>,
    /// Additional platform-specific metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl BotMessage {
    /// Create a new bot message.
    pub fn new(content: serde_json::Value) -> Self {
        Self {
            message_id: Uuid::new_v4().to_string(),
            content,
            message_type: MessageType::Text,
            sender: None,
            channel: None,
            timestamp: current_timestamp(),
            reply_to: None,
            thread_id: None,
            attachments: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Create a text message.
    pub fn text(
        text: impl Into<String>,
        sender: BotUser,
        channel_id: impl Into<String>,
    ) -> Self {
        Self {
            message_id: Uuid::new_v4().to_string(),
            content: serde_json::json!(text.into()),
            message_type: MessageType::Text,
            sender: Some(sender),
            channel: Some(BotChannel::new(channel_id)),
            timestamp: current_timestamp(),
            reply_to: None,
            thread_id: None,
            attachments: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Set message type.
    pub fn message_type(mut self, msg_type: MessageType) -> Self {
        self.message_type = msg_type;
        self
    }

    /// Set sender.
    pub fn sender(mut self, sender: BotUser) -> Self {
        self.sender = Some(sender);
        self
    }

    /// Set channel.
    pub fn channel(mut self, channel: BotChannel) -> Self {
        self.channel = Some(channel);
        self
    }

    /// Set reply_to.
    pub fn reply_to(mut self, message_id: impl Into<String>) -> Self {
        self.reply_to = Some(message_id.into());
        self
    }

    /// Set thread_id.
    pub fn thread_id(mut self, thread_id: impl Into<String>) -> Self {
        self.thread_id = Some(thread_id.into());
        self
    }

    /// Add attachment.
    pub fn attachment(mut self, attachment: serde_json::Value) -> Self {
        self.attachments.push(attachment);
        self
    }

    /// Add metadata.
    pub fn metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Get text content if available.
    pub fn text_content(&self) -> Option<&str> {
        self.content.as_str()
    }

    /// Check if message is a command.
    pub fn is_command(&self) -> bool {
        self.message_type == MessageType::Command
            || self
                .content
                .as_str()
                .map(|s| s.starts_with('/'))
                .unwrap_or(false)
    }

    /// Extract command name if this is a command message.
    pub fn command(&self) -> Option<String> {
        if !self.is_command() {
            return None;
        }
        self.content.as_str().and_then(|text| {
            if text.starts_with('/') {
                text.split_whitespace()
                    .next()
                    .map(|s| s[1..].to_string())
            } else {
                None
            }
        })
    }

    /// Extract command arguments if this is a command message.
    pub fn command_args(&self) -> Vec<String> {
        if !self.is_command() {
            return Vec::new();
        }
        self.content
            .as_str()
            .map(|text| {
                text.split_whitespace()
                    .skip(1)
                    .map(String::from)
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Convert to dictionary.
    pub fn to_dict(&self) -> HashMap<String, serde_json::Value> {
        let mut map = HashMap::new();
        map.insert("message_id".to_string(), serde_json::json!(self.message_id));
        map.insert("content".to_string(), self.content.clone());
        map.insert("message_type".to_string(), serde_json::json!(self.message_type.to_string()));
        map.insert("sender".to_string(), serde_json::json!(self.sender.as_ref().map(|s| s.to_dict())));
        map.insert("channel".to_string(), serde_json::json!(self.channel.as_ref().map(|c| c.to_dict())));
        map.insert("timestamp".to_string(), serde_json::json!(self.timestamp));
        map.insert("reply_to".to_string(), serde_json::json!(self.reply_to));
        map.insert("thread_id".to_string(), serde_json::json!(self.thread_id));
        map.insert("attachments".to_string(), serde_json::json!(self.attachments));
        map.insert("metadata".to_string(), serde_json::json!(self.metadata));
        map
    }
}

impl Default for BotMessage {
    fn default() -> Self {
        Self::new(serde_json::json!(""))
    }
}

/// Configuration for a bot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BotConfig {
    /// Bot token for authentication
    pub token: String,
    /// Platform name (telegram, discord, slack, etc.)
    pub platform: String,
    /// Whether to use webhooks (vs polling)
    pub use_webhooks: bool,
    /// Webhook URL (if use_webhooks is true)
    pub webhook_url: Option<String>,
    /// Polling interval in seconds
    pub polling_interval: u64,
    /// Command prefix (default: "/")
    pub command_prefix: String,
    /// Additional platform-specific configuration
    pub extra: HashMap<String, serde_json::Value>,
}

impl Default for BotConfig {
    fn default() -> Self {
        Self {
            token: String::new(),
            platform: "telegram".to_string(),
            use_webhooks: false,
            webhook_url: None,
            polling_interval: 1,
            command_prefix: "/".to_string(),
            extra: HashMap::new(),
        }
    }
}

impl BotConfig {
    /// Create a new bot config.
    pub fn new(token: impl Into<String>, platform: impl Into<String>) -> Self {
        Self {
            token: token.into(),
            platform: platform.into(),
            ..Default::default()
        }
    }

    /// Enable webhooks.
    pub fn webhooks(mut self, url: impl Into<String>) -> Self {
        self.use_webhooks = true;
        self.webhook_url = Some(url.into());
        self
    }

    /// Set polling interval.
    pub fn polling_interval(mut self, seconds: u64) -> Self {
        self.polling_interval = seconds;
        self
    }

    /// Set command prefix.
    pub fn command_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.command_prefix = prefix.into();
        self
    }

    /// Add extra configuration.
    pub fn extra(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.extra.insert(key.into(), value);
        self
    }
}

/// Protocol for messaging bot implementations.
///
/// Bots connect agents to messaging platforms, handling:
/// - Message receiving and sending
/// - Command handling
/// - Webhook/polling management
/// - User and channel management
#[async_trait]
pub trait BotProtocol: Send + Sync {
    /// Whether the bot is currently running.
    fn is_running(&self) -> bool;

    /// Platform name (telegram, discord, slack, etc.).
    fn platform(&self) -> &str;

    /// The bot's user information.
    fn bot_user(&self) -> Option<&BotUser>;

    /// Start the bot (begin receiving messages).
    async fn start(&mut self) -> Result<()>;

    /// Stop the bot.
    async fn stop(&mut self) -> Result<()>;

    /// Set the agent that handles messages.
    fn set_agent(&mut self, agent: Arc<Agent>);

    /// Get the current agent.
    fn get_agent(&self) -> Option<Arc<Agent>>;

    /// Send a message to a channel.
    async fn send_message(
        &self,
        channel_id: &str,
        content: serde_json::Value,
        reply_to: Option<String>,
        thread_id: Option<String>,
    ) -> Result<BotMessage>;

    /// Edit an existing message.
    async fn edit_message(
        &self,
        channel_id: &str,
        message_id: &str,
        content: serde_json::Value,
    ) -> Result<BotMessage>;

    /// Delete a message.
    async fn delete_message(&self, channel_id: &str, message_id: &str) -> Result<bool>;

    /// Send typing indicator to a channel.
    async fn send_typing(&self, channel_id: &str) -> Result<()>;

    /// Get user information.
    async fn get_user(&self, user_id: &str) -> Result<Option<BotUser>>;

    /// Get channel information.
    async fn get_channel(&self, channel_id: &str) -> Result<Option<BotChannel>>;
}

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

    #[test]
    fn test_message_type() {
        assert_eq!(MessageType::default(), MessageType::Text);
        assert_eq!(MessageType::Command.to_string(), "command");
        assert_eq!(MessageType::Image.to_string(), "image");
    }

    #[test]
    fn test_bot_user_new() {
        let user = BotUser::new("user-123");
        assert_eq!(user.user_id, "user-123");
        assert!(user.username.is_none());
        assert!(!user.is_bot);
    }

    #[test]
    fn test_bot_user_builder() {
        let user = BotUser::new("user-123")
            .username("john_doe")
            .display_name("John Doe")
            .is_bot(false)
            .metadata("role", serde_json::json!("admin"));

        assert_eq!(user.username, Some("john_doe".to_string()));
        assert_eq!(user.display_name, Some("John Doe".to_string()));
        assert_eq!(user.metadata.get("role").unwrap(), "admin");
    }

    #[test]
    fn test_bot_channel_new() {
        let channel = BotChannel::new("channel-1");
        assert_eq!(channel.channel_id, "channel-1");
        assert_eq!(channel.channel_type, "dm");
    }

    #[test]
    fn test_bot_channel_builder() {
        let channel = BotChannel::new("channel-1")
            .name("General")
            .channel_type("group");

        assert_eq!(channel.name, Some("General".to_string()));
        assert_eq!(channel.channel_type, "group");
    }

    #[test]
    fn test_bot_message_text() {
        let user = BotUser::new("user-1");
        let msg = BotMessage::text("Hello world", user, "channel-1");

        assert_eq!(msg.text_content(), Some("Hello world"));
        assert_eq!(msg.message_type, MessageType::Text);
        assert!(msg.sender.is_some());
        assert!(msg.channel.is_some());
    }

    #[test]
    fn test_bot_message_command() {
        let user = BotUser::new("user-1");
        let msg = BotMessage::text("/help arg1 arg2", user, "channel-1");

        assert!(msg.is_command());
        assert_eq!(msg.command(), Some("help".to_string()));
        assert_eq!(msg.command_args(), vec!["arg1", "arg2"]);
    }

    #[test]
    fn test_bot_message_not_command() {
        let user = BotUser::new("user-1");
        let msg = BotMessage::text("Hello world", user, "channel-1");

        assert!(!msg.is_command());
        assert!(msg.command().is_none());
        assert!(msg.command_args().is_empty());
    }

    #[test]
    fn test_bot_message_builder() {
        let msg = BotMessage::new(serde_json::json!("Hello"))
            .message_type(MessageType::Text)
            .reply_to("msg-123")
            .thread_id("thread-1")
            .attachment(serde_json::json!({"url": "https://example.com/image.png"}));

        assert_eq!(msg.reply_to, Some("msg-123".to_string()));
        assert_eq!(msg.thread_id, Some("thread-1".to_string()));
        assert_eq!(msg.attachments.len(), 1);
    }

    #[test]
    fn test_bot_config_default() {
        let config = BotConfig::default();
        assert_eq!(config.platform, "telegram");
        assert!(!config.use_webhooks);
        assert_eq!(config.command_prefix, "/");
    }

    #[test]
    fn test_bot_config_builder() {
        let config = BotConfig::new("token-123", "discord")
            .webhooks("https://example.com/webhook")
            .command_prefix("!")
            .polling_interval(5);

        assert_eq!(config.token, "token-123");
        assert_eq!(config.platform, "discord");
        assert!(config.use_webhooks);
        assert_eq!(config.webhook_url, Some("https://example.com/webhook".to_string()));
        assert_eq!(config.command_prefix, "!");
        assert_eq!(config.polling_interval, 5);
    }

    #[test]
    fn test_bot_user_to_dict() {
        let user = BotUser::new("user-1").username("test");
        let dict = user.to_dict();

        assert_eq!(dict.get("user_id").unwrap(), "user-1");
        assert_eq!(dict.get("username").unwrap(), "test");
    }

    #[test]
    fn test_bot_message_to_dict() {
        let user = BotUser::new("user-1");
        let msg = BotMessage::text("Hello", user, "channel-1");
        let dict = msg.to_dict();

        assert!(dict.contains_key("message_id"));
        assert_eq!(dict.get("content").unwrap(), "Hello");
        assert_eq!(dict.get("message_type").unwrap(), "text");
    }
}