talk 0.1.1

A Rust library for creating controlled LLM agents with behavioral guidelines, tool integration, and multi-step conversation journeys
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
//! Context management for agent conversations
//!
//! This module provides data structures for managing conversation context,
//! messages, and context variables extracted from user inputs.

use crate::types::MessageId;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Role of a message in the conversation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// Message from the system
    System,
    /// Message from the user
    User,
    /// Message from the AI assistant
    Assistant,
    /// Tool execution result
    Tool,
}

/// A single message in the conversation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Message {
    /// Unique identifier for the message
    pub id: MessageId,
    /// Role of the message sender
    pub role: MessageRole,
    /// Content of the message
    pub content: String,
    /// Optional metadata about the message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, serde_json::Value>>,
    /// Timestamp when the message was created
    pub created_at: DateTime<Utc>,
}

impl Message {
    /// Create a new system message
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            id: MessageId::new(),
            role: MessageRole::System,
            content: content.into(),
            metadata: None,
            created_at: Utc::now(),
        }
    }

    /// Create a new user message
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            id: MessageId::new(),
            role: MessageRole::User,
            content: content.into(),
            metadata: None,
            created_at: Utc::now(),
        }
    }

    /// Create a new assistant message
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            id: MessageId::new(),
            role: MessageRole::Assistant,
            content: content.into(),
            metadata: None,
            created_at: Utc::now(),
        }
    }

    /// Create a new tool message
    pub fn tool(content: impl Into<String>) -> Self {
        Self {
            id: MessageId::new(),
            role: MessageRole::Tool,
            content: content.into(),
            metadata: None,
            created_at: Utc::now(),
        }
    }

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

/// Validator for context variables
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Validator {
    /// String type with optional pattern
    String {
        #[serde(skip_serializing_if = "Option::is_none")]
        pattern: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        min_length: Option<usize>,
        #[serde(skip_serializing_if = "Option::is_none")]
        max_length: Option<usize>,
    },
    /// Integer type with optional range
    Integer {
        #[serde(skip_serializing_if = "Option::is_none")]
        min: Option<i64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        max: Option<i64>,
    },
    /// Float type with optional range
    Float {
        #[serde(skip_serializing_if = "Option::is_none")]
        min: Option<f64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        max: Option<f64>,
    },
    /// Boolean type
    Boolean,
    /// Email address
    Email,
    /// URL
    Url,
    /// Date in ISO 8601 format
    Date,
    /// DateTime in ISO 8601 format
    DateTime,
    /// Enum with allowed values
    Enum { allowed_values: Vec<String> },
}

/// A context variable extracted from user input
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContextVariable {
    /// Name of the variable
    pub name: String,
    /// Value of the variable
    pub value: serde_json::Value,
    /// Optional validator for the variable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub validator: Option<Validator>,
    /// Source message ID where this variable was extracted
    pub source_message_id: MessageId,
    /// Timestamp when the variable was extracted
    pub extracted_at: DateTime<Utc>,
}

impl ContextVariable {
    /// Create a new context variable
    pub fn new(
        name: impl Into<String>,
        value: serde_json::Value,
        source_message_id: MessageId,
    ) -> Self {
        Self {
            name: name.into(),
            value,
            validator: None,
            source_message_id,
            extracted_at: Utc::now(),
        }
    }

    /// Add a validator to the context variable
    pub fn with_validator(mut self, validator: Validator) -> Self {
        self.validator = Some(validator);
        self
    }

    /// Validate the variable value against its validator
    pub fn validate(&self) -> Result<(), String> {
        match &self.validator {
            None => Ok(()),
            Some(Validator::String {
                pattern,
                min_length,
                max_length,
            }) => {
                let s = self
                    .value
                    .as_str()
                    .ok_or_else(|| "Value is not a string".to_string())?;

                if let Some(min) = min_length {
                    if s.len() < *min {
                        return Err(format!(
                            "String length {} is less than minimum {}",
                            s.len(),
                            min
                        ));
                    }
                }

                if let Some(max) = max_length {
                    if s.len() > *max {
                        return Err(format!("String length {} exceeds maximum {}", s.len(), max));
                    }
                }

                if let Some(pat) = pattern {
                    let re = regex::Regex::new(pat)
                        .map_err(|e| format!("Invalid regex pattern: {}", e))?;
                    if !re.is_match(s) {
                        return Err(format!("String does not match pattern: {}", pat));
                    }
                }

                Ok(())
            }
            Some(Validator::Integer { min, max }) => {
                let i = self
                    .value
                    .as_i64()
                    .ok_or_else(|| "Value is not an integer".to_string())?;

                if let Some(min_val) = min {
                    if i < *min_val {
                        return Err(format!("Integer {} is less than minimum {}", i, min_val));
                    }
                }

                if let Some(max_val) = max {
                    if i > *max_val {
                        return Err(format!("Integer {} exceeds maximum {}", i, max_val));
                    }
                }

                Ok(())
            }
            Some(Validator::Float { min, max }) => {
                let f = self
                    .value
                    .as_f64()
                    .ok_or_else(|| "Value is not a float".to_string())?;

                if let Some(min_val) = min {
                    if f < *min_val {
                        return Err(format!("Float {} is less than minimum {}", f, min_val));
                    }
                }

                if let Some(max_val) = max {
                    if f > *max_val {
                        return Err(format!("Float {} exceeds maximum {}", f, max_val));
                    }
                }

                Ok(())
            }
            Some(Validator::Boolean) => {
                self.value
                    .as_bool()
                    .ok_or_else(|| "Value is not a boolean".to_string())?;
                Ok(())
            }
            Some(Validator::Email) => {
                let s = self
                    .value
                    .as_str()
                    .ok_or_else(|| "Value is not a string".to_string())?;

                // Simple email validation
                if !s.contains('@') || !s.contains('.') {
                    return Err("Invalid email format".to_string());
                }

                Ok(())
            }
            Some(Validator::Url) => {
                let s = self
                    .value
                    .as_str()
                    .ok_or_else(|| "Value is not a string".to_string())?;

                // Simple URL validation
                if !s.starts_with("http://") && !s.starts_with("https://") {
                    return Err(
                        "Invalid URL format (must start with http:// or https://)".to_string()
                    );
                }

                Ok(())
            }
            Some(Validator::Date) | Some(Validator::DateTime) => {
                let s = self
                    .value
                    .as_str()
                    .ok_or_else(|| "Value is not a string".to_string())?;

                // Validate ISO 8601 format
                chrono::DateTime::parse_from_rfc3339(s)
                    .map_err(|e| format!("Invalid date/datetime format: {}", e))?;

                Ok(())
            }
            Some(Validator::Enum { allowed_values }) => {
                let s = self
                    .value
                    .as_str()
                    .ok_or_else(|| "Value is not a string".to_string())?;

                if !allowed_values.contains(&s.to_string()) {
                    return Err(format!(
                        "Value '{}' not in allowed values: {:?}",
                        s, allowed_values
                    ));
                }

                Ok(())
            }
        }
    }
}

/// Context for a conversation session
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Context {
    /// Message history
    pub messages: Vec<Message>,
    /// Extracted context variables
    pub variables: HashMap<String, ContextVariable>,
    /// Maximum number of messages to keep in context
    #[serde(default = "default_max_messages")]
    pub max_messages: usize,
}

fn default_max_messages() -> usize {
    100
}

impl Context {
    /// Create a new empty context
    pub fn new() -> Self {
        Self {
            messages: Vec::new(),
            variables: HashMap::new(),
            max_messages: default_max_messages(),
        }
    }

    /// Create a new context with a custom max message limit
    pub fn with_max_messages(max_messages: usize) -> Self {
        Self {
            messages: Vec::new(),
            variables: HashMap::new(),
            max_messages,
        }
    }

    /// Add a message to the context
    pub fn add_message(&mut self, message: Message) {
        self.messages.push(message);

        // Trim old messages if we exceed the limit
        if self.messages.len() > self.max_messages {
            let excess = self.messages.len() - self.max_messages;
            self.messages.drain(0..excess);
        }
    }

    /// Add a context variable
    pub fn add_variable(&mut self, variable: ContextVariable) {
        self.variables.insert(variable.name.clone(), variable);
    }

    /// Get a context variable by name
    pub fn get_variable(&self, name: &str) -> Option<&ContextVariable> {
        self.variables.get(name)
    }

    /// Get all messages with a specific role
    pub fn messages_by_role(&self, role: MessageRole) -> Vec<&Message> {
        self.messages.iter().filter(|m| m.role == role).collect()
    }

    /// Get the most recent message
    pub fn last_message(&self) -> Option<&Message> {
        self.messages.last()
    }

    /// Clear all messages and variables
    pub fn clear(&mut self) {
        self.messages.clear();
        self.variables.clear();
    }
}

impl Default for Context {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_message_creation() {
        let msg = Message::user("Hello");
        assert_eq!(msg.role, MessageRole::User);
        assert_eq!(msg.content, "Hello");
        assert!(msg.metadata.is_none());
    }

    #[test]
    fn test_message_with_metadata() {
        let msg =
            Message::assistant("Response").with_metadata("confidence", serde_json::json!(0.95));
        assert!(msg.metadata.is_some());
        assert_eq!(
            msg.metadata.unwrap().get("confidence"),
            Some(&serde_json::json!(0.95))
        );
    }

    #[test]
    fn test_message_serialization() {
        let msg = Message::system("System prompt");
        let json = serde_json::to_string(&msg).unwrap();
        let deserialized: Message = serde_json::from_str(&json).unwrap();
        assert_eq!(msg, deserialized);
    }

    #[test]
    fn test_context_variable_creation() {
        let msg_id = MessageId::new();
        let var = ContextVariable::new("name", serde_json::json!("Alice"), msg_id);
        assert_eq!(var.name, "name");
        assert_eq!(var.value, serde_json::json!("Alice"));
        assert_eq!(var.source_message_id, msg_id);
    }

    #[test]
    fn test_context_variable_string_validator() {
        let msg_id = MessageId::new();
        let var = ContextVariable::new("name", serde_json::json!("Alice"), msg_id).with_validator(
            Validator::String {
                pattern: None,
                min_length: Some(3),
                max_length: Some(10),
            },
        );

        assert!(var.validate().is_ok());

        let short_var = ContextVariable::new("name", serde_json::json!("Al"), msg_id)
            .with_validator(Validator::String {
                pattern: None,
                min_length: Some(3),
                max_length: Some(10),
            });

        assert!(short_var.validate().is_err());
    }

    #[test]
    fn test_context_variable_integer_validator() {
        let msg_id = MessageId::new();
        let var = ContextVariable::new("age", serde_json::json!(25), msg_id).with_validator(
            Validator::Integer {
                min: Some(0),
                max: Some(120),
            },
        );

        assert!(var.validate().is_ok());

        let invalid_var = ContextVariable::new("age", serde_json::json!(150), msg_id)
            .with_validator(Validator::Integer {
                min: Some(0),
                max: Some(120),
            });

        assert!(invalid_var.validate().is_err());
    }

    #[test]
    fn test_context_variable_enum_validator() {
        let msg_id = MessageId::new();
        let var = ContextVariable::new("color", serde_json::json!("red"), msg_id).with_validator(
            Validator::Enum {
                allowed_values: vec!["red".to_string(), "green".to_string(), "blue".to_string()],
            },
        );

        assert!(var.validate().is_ok());

        let invalid_var = ContextVariable::new("color", serde_json::json!("yellow"), msg_id)
            .with_validator(Validator::Enum {
                allowed_values: vec!["red".to_string(), "green".to_string(), "blue".to_string()],
            });

        assert!(invalid_var.validate().is_err());
    }

    #[test]
    fn test_context_creation() {
        let ctx = Context::new();
        assert_eq!(ctx.messages.len(), 0);
        assert_eq!(ctx.variables.len(), 0);
        assert_eq!(ctx.max_messages, 100);
    }

    #[test]
    fn test_context_add_message() {
        let mut ctx = Context::new();
        ctx.add_message(Message::user("Hello"));
        ctx.add_message(Message::assistant("Hi there"));

        assert_eq!(ctx.messages.len(), 2);
        assert_eq!(ctx.messages[0].content, "Hello");
        assert_eq!(ctx.messages[1].content, "Hi there");
    }

    #[test]
    fn test_context_max_messages() {
        let mut ctx = Context::with_max_messages(3);

        for i in 0..5 {
            ctx.add_message(Message::user(format!("Message {}", i)));
        }

        assert_eq!(ctx.messages.len(), 3);
        assert_eq!(ctx.messages[0].content, "Message 2");
        assert_eq!(ctx.messages[2].content, "Message 4");
    }

    #[test]
    fn test_context_add_variable() {
        let mut ctx = Context::new();
        let msg_id = MessageId::new();
        let var = ContextVariable::new("name", serde_json::json!("Alice"), msg_id);

        ctx.add_variable(var.clone());

        assert_eq!(ctx.variables.len(), 1);
        assert_eq!(ctx.get_variable("name"), Some(&var));
    }

    #[test]
    fn test_context_messages_by_role() {
        let mut ctx = Context::new();
        ctx.add_message(Message::user("User message 1"));
        ctx.add_message(Message::assistant("Assistant message 1"));
        ctx.add_message(Message::user("User message 2"));

        let user_messages = ctx.messages_by_role(MessageRole::User);
        assert_eq!(user_messages.len(), 2);

        let assistant_messages = ctx.messages_by_role(MessageRole::Assistant);
        assert_eq!(assistant_messages.len(), 1);
    }

    #[test]
    fn test_context_last_message() {
        let mut ctx = Context::new();
        assert!(ctx.last_message().is_none());

        ctx.add_message(Message::user("First"));
        ctx.add_message(Message::user("Second"));

        assert_eq!(ctx.last_message().unwrap().content, "Second");
    }

    #[test]
    fn test_context_clear() {
        let mut ctx = Context::new();
        ctx.add_message(Message::user("Hello"));
        ctx.add_variable(ContextVariable::new(
            "name",
            serde_json::json!("Alice"),
            MessageId::new(),
        ));

        ctx.clear();

        assert_eq!(ctx.messages.len(), 0);
        assert_eq!(ctx.variables.len(), 0);
    }

    #[test]
    fn test_context_serialization() {
        let mut ctx = Context::new();
        ctx.add_message(Message::user("Hello"));
        ctx.add_variable(ContextVariable::new(
            "name",
            serde_json::json!("Alice"),
            MessageId::new(),
        ));

        let json = serde_json::to_string(&ctx).unwrap();
        let deserialized: Context = serde_json::from_str(&json).unwrap();

        assert_eq!(ctx.messages.len(), deserialized.messages.len());
        assert_eq!(ctx.variables.len(), deserialized.variables.len());
    }
}