chatpack 0.5.1

Prepare chat data for RAG / LLM ingestion. Supports Telegram, WhatsApp, Instagram, Discord.
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
//! Universal message type for all chat platforms.
//!
//! This module provides [`Message`], the normalized representation of chat messages.
//! All platform parsers convert their native formats into this structure, enabling
//! uniform processing regardless of source.
//!
//! # Overview
//!
//! A message consists of:
//! - **Required**: `sender` and `content`
//! - **Optional**: `timestamp`, `id`, `reply_to`, `edited`
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```
//! use chatpack::Message;
//!
//! let msg = Message::new("Alice", "Hello, world!");
//! assert_eq!(msg.sender(), "Alice");
//! assert_eq!(msg.content(), "Hello, world!");
//! ```
//!
//! ## Builder Pattern
//!
//! ```
//! use chatpack::Message;
//! use chrono::Utc;
//!
//! let msg = Message::new("Bob", "Check this out!")
//!     .with_id(12345)
//!     .with_timestamp(Utc::now())
//!     .with_reply_to(12344);
//!
//! assert!(msg.has_metadata());
//! ```
//!
//! ## Serialization
//!
//! ```
//! use chatpack::Message;
//!
//! let msg = Message::new("Alice", "Hello!");
//! let json = serde_json::to_string(&msg)?;
//! let parsed: Message = serde_json::from_str(&json)?;
//!
//! assert_eq!(msg, parsed);
//! # Ok::<(), serde_json::Error>(())
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// A normalized chat message from any supported platform.
///
/// This struct is the core data type in chatpack. All platform-specific parsers
/// convert their native message formats into this universal representation,
/// enabling uniform processing, filtering, and export.
///
/// # Fields
///
/// | Field | Type | Description |
/// |-------|------|-------------|
/// | `sender` | `String` | Display name or username of the message author |
/// | `content` | `String` | Text content of the message |
/// | `timestamp` | `Option<DateTime<Utc>>` | When the message was sent |
/// | `id` | `Option<u64>` | Platform-specific message identifier |
/// | `reply_to` | `Option<u64>` | ID of the parent message (for replies) |
/// | `edited` | `Option<DateTime<Utc>>` | When the message was last edited |
///
/// # Construction
///
/// Use [`Message::new`] for simple messages or the builder pattern for metadata:
///
/// ```
/// use chatpack::Message;
/// use chrono::Utc;
///
/// // Simple message
/// let msg = Message::new("Alice", "Hello!");
///
/// // With metadata
/// let msg = Message::new("Alice", "Hello!")
///     .with_timestamp(Utc::now())
///     .with_id(12345);
/// ```
///
/// # Serialization
///
/// Implements `Serialize` and `Deserialize` with these behaviors:
/// - Optional fields are omitted from JSON when `None`
/// - Timestamps use RFC 3339 format
/// - Suitable for storage, IPC, and RAG pipelines
///
/// ```
/// use chatpack::Message;
///
/// let msg = Message::new("Alice", "Hello!").with_id(123);
/// let json = serde_json::to_string(&msg)?;
///
/// // timestamp is omitted (None)
/// assert!(!json.contains("timestamp"));
/// assert!(json.contains("123"));
/// # Ok::<(), serde_json::Error>(())
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Message {
    /// Display name or username of the message author.
    pub sender: String,

    /// Text content of the message.
    ///
    /// May contain newlines for multiline messages. Platform-specific
    /// attachments (images, files) are typically represented as text
    /// placeholders like `[Attachment: image.png]`.
    pub content: String,

    /// When the message was originally sent.
    ///
    /// Available from most platforms except some WhatsApp export formats.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub timestamp: Option<DateTime<Utc>>,

    /// Platform-specific message identifier.
    ///
    /// - Telegram: message ID from the chat
    /// - Discord: snowflake ID
    /// - WhatsApp/Instagram: typically not available in exports
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub id: Option<u64>,

    /// ID of the message this is replying to.
    ///
    /// Enables reconstruction of reply chains and conversation threads.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub reply_to: Option<u64>,

    /// When the message was last edited.
    ///
    /// Present when the platform tracks edit history (Telegram, Discord).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub edited: Option<DateTime<Utc>>,
}

impl Message {
    /// Creates a new message with only sender and content.
    ///
    /// All metadata fields (timestamp, id, `reply_to`, edited) are set to `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use chatpack::Message;
    ///
    /// let msg = Message::new("Alice", "Hello!");
    /// assert_eq!(msg.sender(), "Alice");
    /// assert_eq!(msg.content(), "Hello!");
    /// assert!(msg.timestamp().is_none());
    /// ```
    pub fn new(sender: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            sender: sender.into(),
            content: content.into(),
            timestamp: None,
            id: None,
            reply_to: None,
            edited: None,
        }
    }

    /// Creates a new message with all fields specified.
    ///
    /// Use this when you have all metadata available upfront.
    /// For incremental construction, prefer [`new`](Self::new) with builder methods.
    pub fn with_metadata(
        sender: impl Into<String>,
        content: impl Into<String>,
        timestamp: Option<DateTime<Utc>>,
        id: Option<u64>,
        reply_to: Option<u64>,
        edited: Option<DateTime<Utc>>,
    ) -> Self {
        Self {
            sender: sender.into(),
            content: content.into(),
            timestamp,
            id,
            reply_to,
            edited,
        }
    }

    // =========================================================================
    // Builder methods
    // =========================================================================

    /// Builder method to set the timestamp.
    ///
    /// # Example
    ///
    /// ```rust
    /// use chatpack::Message;
    /// use chrono::Utc;
    ///
    /// let msg = Message::new("Alice", "Hello")
    ///     .with_timestamp(Utc::now());
    /// assert!(msg.timestamp().is_some());
    /// ```
    #[must_use]
    pub fn with_timestamp(mut self, ts: DateTime<Utc>) -> Self {
        self.timestamp = Some(ts);
        self
    }

    /// Builder method to set the message ID.
    ///
    /// # Example
    ///
    /// ```rust
    /// use chatpack::Message;
    ///
    /// let msg = Message::new("Alice", "Hello")
    ///     .with_id(12345);
    /// assert_eq!(msg.id(), Some(12345));
    /// ```
    #[must_use]
    pub fn with_id(mut self, id: u64) -> Self {
        self.id = Some(id);
        self
    }

    /// Builder method to set the reply reference.
    ///
    /// # Example
    ///
    /// ```rust
    /// use chatpack::Message;
    ///
    /// let msg = Message::new("Bob", "I agree!")
    ///     .with_reply_to(12344);
    /// assert_eq!(msg.reply_to(), Some(12344));
    /// ```
    #[must_use]
    pub fn with_reply_to(mut self, reply_id: u64) -> Self {
        self.reply_to = Some(reply_id);
        self
    }

    /// Builder method to set the edited timestamp.
    ///
    /// # Example
    ///
    /// ```rust
    /// use chatpack::Message;
    /// use chrono::Utc;
    ///
    /// let msg = Message::new("Alice", "Updated message")
    ///     .with_edited(Utc::now());
    /// assert!(msg.edited().is_some());
    /// ```
    #[must_use]
    pub fn with_edited(mut self, ts: DateTime<Utc>) -> Self {
        self.edited = Some(ts);
        self
    }

    // =========================================================================
    // Accessor methods
    // =========================================================================

    /// Returns the sender name.
    pub fn sender(&self) -> &str {
        &self.sender
    }

    /// Returns the message content.
    pub fn content(&self) -> &str {
        &self.content
    }

    /// Returns the timestamp, if available.
    pub fn timestamp(&self) -> Option<DateTime<Utc>> {
        self.timestamp
    }

    /// Returns the message ID, if available.
    pub fn id(&self) -> Option<u64> {
        self.id
    }

    /// Returns the reply-to ID, if available.
    pub fn reply_to(&self) -> Option<u64> {
        self.reply_to
    }

    /// Returns the edited timestamp, if available.
    pub fn edited(&self) -> Option<DateTime<Utc>> {
        self.edited
    }

    // =========================================================================
    // Utility methods
    // =========================================================================

    /// Returns `true` if this message has any metadata (timestamp, id, `reply_to`, or edited).
    pub fn has_metadata(&self) -> bool {
        self.timestamp.is_some()
            || self.id.is_some()
            || self.reply_to.is_some()
            || self.edited.is_some()
    }

    /// Returns `true` if this message's content is empty or whitespace-only.
    pub fn is_empty(&self) -> bool {
        self.content.trim().is_empty()
    }
}

impl Default for Message {
    fn default() -> Self {
        Self::new("", "")
    }
}

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

    #[test]
    fn test_message_new() {
        let msg = Message::new("Alice", "Hello");
        assert_eq!(msg.sender(), "Alice");
        assert_eq!(msg.content(), "Hello");
        assert!(msg.timestamp().is_none());
        assert!(msg.id().is_none());
    }

    #[test]
    fn test_message_builder() {
        let ts = Utc.with_ymd_and_hms(2024, 6, 15, 12, 0, 0).unwrap();
        let msg = Message::new("Alice", "Hello")
            .with_timestamp(ts)
            .with_id(123)
            .with_reply_to(122)
            .with_edited(ts);

        assert_eq!(msg.timestamp(), Some(ts));
        assert_eq!(msg.id(), Some(123));
        assert_eq!(msg.reply_to(), Some(122));
        assert_eq!(msg.edited(), Some(ts));
    }

    #[test]
    fn test_message_has_metadata() {
        let msg1 = Message::new("Alice", "Hello");
        assert!(!msg1.has_metadata());

        let msg2 = Message::new("Alice", "Hello").with_id(123);
        assert!(msg2.has_metadata());
    }

    #[test]
    fn test_message_is_empty() {
        assert!(Message::new("Alice", "").is_empty());
        assert!(Message::new("Alice", "   ").is_empty());
        assert!(!Message::new("Alice", "Hello").is_empty());
    }

    #[test]
    fn test_message_serialization() {
        let msg = Message::new("Alice", "Hello").with_id(123);
        let json = serde_json::to_string(&msg).unwrap();
        assert!(json.contains("Alice"));
        assert!(json.contains("123"));
        // timestamp should be skipped (None)
        assert!(!json.contains("timestamp"));
    }

    #[test]
    fn test_message_deserialization() {
        let json = r#"{"sender":"Bob","content":"Hi","id":456}"#;
        let msg: Message = serde_json::from_str(json).unwrap();
        assert_eq!(msg.sender(), "Bob");
        assert_eq!(msg.content(), "Hi");
        assert_eq!(msg.id(), Some(456));
        assert!(msg.timestamp().is_none());
    }

    #[test]
    fn test_message_accessors() {
        let ts = Utc.with_ymd_and_hms(2024, 6, 15, 12, 0, 0).unwrap();
        let msg = Message::new("Alice", "Hello")
            .with_timestamp(ts)
            .with_id(123)
            .with_reply_to(122)
            .with_edited(ts);

        assert_eq!(msg.sender(), "Alice");
        assert_eq!(msg.content(), "Hello");
        assert_eq!(msg.timestamp(), Some(ts));
        assert_eq!(msg.id(), Some(123));
        assert_eq!(msg.reply_to(), Some(122));
        assert_eq!(msg.edited(), Some(ts));
    }
}