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
//! Conversation Journey System
//!
//! This module provides multi-step conversation flows with conditional transitions.
//!
//! # Example
//!
//! ```rust,no_run
//! use talk::{Journey, JourneyStep, Transition, TransitionCondition, StepId, JourneyId};
//! use chrono::Utc;
//!
//! let step1_id = StepId::new();
//! let step2_id = StepId::new();
//!
//! let journey = Journey {
//!     id: JourneyId::new(),
//!     name: "Onboarding".to_string(),
//!     description: "New user onboarding".to_string(),
//!     steps: vec![
//!         JourneyStep {
//!             id: step1_id,
//!             name: "Welcome".to_string(),
//!             prompt: "Welcome! What's your name?".to_string(),
//!             expected_response: Some(".*".to_string()),
//!             transitions: vec![Transition {
//!                 condition: TransitionCondition::Always,
//!                 next_step: step2_id,
//!             }],
//!             actions: vec![],
//!         },
//!         JourneyStep {
//!             id: step2_id,
//!             name: "Complete".to_string(),
//!             prompt: "Thank you!".to_string(),
//!             expected_response: None,
//!             transitions: vec![],
//!             actions: vec!["complete".to_string()],
//!         },
//!     ],
//!     initial_step: step1_id,
//!     current_step: None,
//!     created_at: Utc::now(),
//! };
//! ```

use crate::context::Context;
use crate::error::AgentError;
use crate::types::{JourneyId, SessionId, StepId};
use crate::Result;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use tracing::{debug, info};

/// Multi-step conversation journey
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Journey {
    /// Unique journey identifier
    pub id: JourneyId,

    /// Human-readable name
    pub name: String,

    /// Journey description
    pub description: String,

    /// All steps in this journey
    pub steps: Vec<JourneyStep>,

    /// ID of the first step
    pub initial_step: StepId,

    /// Current step (None if not started)
    pub current_step: Option<StepId>,

    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Individual step within a journey
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JourneyStep {
    /// Unique step identifier
    pub id: StepId,

    /// Human-readable step name
    pub name: String,

    /// Prompt to display to user
    pub prompt: String,

    /// Expected response pattern (regex)
    pub expected_response: Option<String>,

    /// Possible transitions to next steps
    pub transitions: Vec<Transition>,

    /// Actions to execute when reaching this step
    pub actions: Vec<String>,
}

/// Transition from one step to another
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transition {
    /// Condition that must be met for transition
    pub condition: TransitionCondition,

    /// Next step to transition to
    pub next_step: StepId,
}

/// Condition for transitioning between steps
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransitionCondition {
    /// Always transition
    Always,

    /// Transition if user message matches regex
    Match(String),

    /// Transition if context variable matches value
    ContextVariable { key: String, value: String },
}

/// Runtime state of a journey for a session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JourneyState {
    /// Journey being executed
    pub journey_id: JourneyId,

    /// Current step in the journey
    pub current_step: StepId,

    /// Steps already completed
    pub completed_steps: Vec<StepId>,

    /// Whether journey is complete
    pub is_complete: bool,

    /// Additional state data
    pub metadata: HashMap<String, serde_json::Value>,

    /// When journey was started
    pub started_at: DateTime<Utc>,

    /// When journey was completed (if is_complete)
    pub completed_at: Option<DateTime<Utc>>,
}

impl JourneyState {
    /// Create a new journey state
    pub fn new(journey_id: JourneyId, initial_step: StepId) -> Self {
        Self {
            journey_id,
            current_step: initial_step,
            completed_steps: Vec::new(),
            is_complete: false,
            metadata: HashMap::new(),
            started_at: Utc::now(),
            completed_at: None,
        }
    }

    /// Mark a step as completed
    pub fn complete_step(&mut self, step_id: StepId) {
        if !self.completed_steps.contains(&step_id) {
            self.completed_steps.push(step_id);
        }
    }

    /// Mark journey as complete
    pub fn mark_complete(&mut self) {
        self.is_complete = true;
        self.completed_at = Some(Utc::now());
    }
}

/// Trait for managing conversation journeys
#[async_trait]
pub trait JourneyManager: Send + Sync {
    /// Start a journey - returns initial state
    async fn start_journey(
        &self,
        _session_id: &SessionId,
        journey_id: &JourneyId,
    ) -> Result<JourneyState>;

    /// Progress to the next step based on user message
    /// Takes journey_id and current_step_id instead of looking up session state
    async fn process_step(
        &self,
        journey_id: &JourneyId,
        current_step_id: StepId,
        message: &str,
    ) -> Result<JourneyStep>;

    /// Register a new journey
    async fn add_journey(&mut self, journey: Journey) -> Result<JourneyId>;

    /// Get a journey by ID
    fn get_journey(&self, journey_id: &JourneyId) -> Option<&Journey>;
}

/// Default implementation of JourneyManager
pub struct DefaultJourneyManager {
    /// Registered journeys
    journeys: HashMap<JourneyId, Journey>,
}

impl DefaultJourneyManager {
    /// Create a new journey manager
    pub fn new() -> Self {
        Self {
            journeys: HashMap::new(),
        }
    }

    /// Validate a journey's structure
    fn validate_journey(&self, journey: &Journey) -> Result<()> {
        // Check initial_step exists in steps
        let initial_step_exists = journey.steps.iter().any(|s| s.id == journey.initial_step);
        if !initial_step_exists {
            return Err(AgentError::Journey(format!(
                "Initial step {:?} not found in journey steps",
                journey.initial_step
            )));
        }

        // Check all transition targets exist
        let step_ids: HashSet<StepId> = journey.steps.iter().map(|s| s.id).collect();
        for step in &journey.steps {
            for transition in &step.transitions {
                if !step_ids.contains(&transition.next_step) {
                    return Err(AgentError::Journey(format!(
                        "Transition target {:?} not found in journey steps",
                        transition.next_step
                    )));
                }
            }
        }

        // Check for circular dependencies
        self.check_circular_dependencies(journey)?;

        Ok(())
    }

    /// Check for circular dependencies in journey
    fn check_circular_dependencies(&self, _journey: &Journey) -> Result<()> {
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();

        fn has_cycle(
            step_id: StepId,
            journey: &Journey,
            visited: &mut HashSet<StepId>,
            rec_stack: &mut HashSet<StepId>,
        ) -> bool {
            if rec_stack.contains(&step_id) {
                return true; // Cycle detected
            }

            if visited.contains(&step_id) {
                return false;
            }

            visited.insert(step_id);
            rec_stack.insert(step_id);

            // Find step and check its transitions
            if let Some(step) = journey.steps.iter().find(|s| s.id == step_id) {
                for transition in &step.transitions {
                    if has_cycle(transition.next_step, journey, visited, rec_stack) {
                        return true;
                    }
                }
            }

            rec_stack.remove(&step_id);
            false
        }

        if has_cycle(_journey.initial_step, _journey, &mut visited, &mut rec_stack) {
            return Err(AgentError::Journey(
                "Circular dependency detected in journey".to_string(),
            ));
        }

        Ok(())
    }

    /// Find a step by ID in a journey
    fn find_step<'a>(&self, journey: &'a Journey, step_id: StepId) -> Option<&'a JourneyStep> {
        journey.steps.iter().find(|s| s.id == step_id)
    }

    /// Evaluate a transition condition
    fn evaluate_condition(
        &self,
        condition: &TransitionCondition,
        message: &str,
        _context: &Context,
    ) -> Result<bool> {
        match condition {
            TransitionCondition::Always => Ok(true),

            TransitionCondition::Match(pattern) => {
                let regex = Regex::new(pattern)
                    .map_err(|e| AgentError::Journey(format!("Invalid regex pattern: {}", e)))?;
                Ok(regex.is_match(message))
            }

            TransitionCondition::ContextVariable { key, value } => {
                // Get value from context
                let context_value = _context
                    .variables
                    .get(key)
                    .and_then(|v| v.value.as_str())
                    .unwrap_or("");
                Ok(context_value == value)
            }
        }
    }

    /// Find the next step based on transitions
    async fn find_next_step(
        &self,
        _journey: &Journey,
        current_step: &JourneyStep,
        message: &str,
        context: &Context,
    ) -> Result<Option<StepId>> {
        for transition in &current_step.transitions {
            if self.evaluate_condition(&transition.condition, message, context)? {
                debug!(
                    step_id = ?current_step.id,
                    next_step = ?transition.next_step,
                    "Transition condition matched"
                );
                return Ok(Some(transition.next_step));
            }
        }

        // No transition matched
        Ok(None)
    }
}

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

#[async_trait]
impl JourneyManager for DefaultJourneyManager {
    async fn start_journey(
        &self,
        session_id: &SessionId,
        journey_id: &JourneyId,
    ) -> Result<JourneyState> {
        let journey = self
            .journeys
            .get(journey_id)
            .ok_or_else(|| AgentError::Journey(format!("Journey {:?} not found", journey_id)))?;

        info!(
            session_id = ?session_id,
            journey_id = ?journey_id,
            journey_name = %journey.name,
            "Starting journey"
        );

        let state = JourneyState::new(*journey_id, journey.initial_step);
        Ok(state)
    }

    async fn process_step(
        &self,
        journey_id: &JourneyId,
        current_step_id: StepId,
        message: &str,
    ) -> Result<JourneyStep> {
        let journey = self
            .journeys
            .get(journey_id)
            .ok_or_else(|| AgentError::Journey("Journey not found".to_string()))?;

        let current_step = self
            .find_step(journey, current_step_id)
            .ok_or_else(|| AgentError::Journey("Current step not found".to_string()))?;

        debug!(
            journey_id = ?journey_id,
            current_step = ?current_step.id,
            step_name = %current_step.name,
            message = %message,
            "Processing journey step"
        );

        // Create a temporary context for evaluation
        let context = Context::new();

        // Find next step
        let next_step_id = self
            .find_next_step(journey, current_step, message, &context)
            .await?;

        if let Some(next_id) = next_step_id {
            let next_step = self
                .find_step(journey, next_id)
                .ok_or_else(|| AgentError::Journey("Next step not found".to_string()))?;

            info!(
                journey_id = ?journey_id,
                from_step = ?current_step.id,
                to_step = ?next_step.id,
                "Journey step transition"
            );

            Ok(next_step.clone())
        } else {
            // No transition - this is the final step
            debug!(
                journey_id = ?journey_id,
                current_step = ?current_step.id,
                "No transitions available - journey complete"
            );

            Ok(current_step.clone())
        }
    }

    async fn add_journey(&mut self, journey: Journey) -> Result<JourneyId> {
        // Validate journey structure
        self.validate_journey(&journey)?;

        let journey_id = journey.id;
        info!(
            journey_id = ?journey_id,
            journey_name = %journey.name,
            step_count = journey.steps.len(),
            "Registering journey"
        );

        self.journeys.insert(journey_id, journey);
        Ok(journey_id)
    }

    fn get_journey(&self, journey_id: &JourneyId) -> Option<&Journey> {
        self.journeys.get(journey_id)
    }
}

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

    #[test]
    fn test_journey_state_new() {
        let journey_id = JourneyId::new();
        let step_id = StepId::new();
        let state = JourneyState::new(journey_id, step_id);

        assert_eq!(state.journey_id, journey_id);
        assert_eq!(state.current_step, step_id);
        assert_eq!(state.completed_steps.len(), 0);
        assert!(!state.is_complete);
    }

    #[test]
    fn test_journey_state_complete_step() {
        let journey_id = JourneyId::new();
        let step_id = StepId::new();
        let mut state = JourneyState::new(journey_id, step_id);

        state.complete_step(step_id);
        assert_eq!(state.completed_steps.len(), 1);
        assert!(state.completed_steps.contains(&step_id));

        // Completing same step again should not duplicate
        state.complete_step(step_id);
        assert_eq!(state.completed_steps.len(), 1);
    }

    #[test]
    fn test_journey_state_mark_complete() {
        let journey_id = JourneyId::new();
        let step_id = StepId::new();
        let mut state = JourneyState::new(journey_id, step_id);

        assert!(!state.is_complete);
        assert!(state.completed_at.is_none());

        state.mark_complete();
        assert!(state.is_complete);
        assert!(state.completed_at.is_some());
    }

    #[tokio::test]
    async fn test_validate_journey_invalid_initial_step() {
        let manager = DefaultJourneyManager::new();
        let journey = Journey {
            id: JourneyId::new(),
            name: "Test".to_string(),
            description: "Test".to_string(),
            steps: vec![JourneyStep {
                id: StepId::new(),
                name: "Step 1".to_string(),
                prompt: "Prompt".to_string(),
                expected_response: None,
                transitions: vec![],
                actions: vec![],
            }],
            initial_step: StepId::new(), // Different ID
            current_step: None,
            created_at: Utc::now(),
        };

        let result = manager.validate_journey(&journey);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_validate_journey_circular_dependency() {
        let manager = DefaultJourneyManager::new();
        let step1_id = StepId::new();
        let step2_id = StepId::new();

        let journey = Journey {
            id: JourneyId::new(),
            name: "Circular".to_string(),
            description: "Test".to_string(),
            steps: vec![
                JourneyStep {
                    id: step1_id,
                    name: "Step 1".to_string(),
                    prompt: "Prompt".to_string(),
                    expected_response: None,
                    transitions: vec![Transition {
                        condition: TransitionCondition::Always,
                        next_step: step2_id,
                    }],
                    actions: vec![],
                },
                JourneyStep {
                    id: step2_id,
                    name: "Step 2".to_string(),
                    prompt: "Prompt".to_string(),
                    expected_response: None,
                    transitions: vec![Transition {
                        condition: TransitionCondition::Always,
                        next_step: step1_id, // Circular!
                    }],
                    actions: vec![],
                },
            ],
            initial_step: step1_id,
            current_step: None,
            created_at: Utc::now(),
        };

        let result = manager.validate_journey(&journey);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_evaluate_condition_always() {
        let manager = DefaultJourneyManager::new();
        let context = Context::new();
        let condition = TransitionCondition::Always;

        let result = manager.evaluate_condition(&condition, "any message", &context);
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[tokio::test]
    async fn test_evaluate_condition_match() {
        let manager = DefaultJourneyManager::new();
        let context = Context::new();
        let condition = TransitionCondition::Match(r"(?i)yes".to_string());

        let result_yes = manager.evaluate_condition(&condition, "YES", &context);
        assert!(result_yes.is_ok());
        assert!(result_yes.unwrap());

        let result_no = manager.evaluate_condition(&condition, "no", &context);
        assert!(result_no.is_ok());
        assert!(!result_no.unwrap());
    }
}