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
//! Session management for agent conversations
//!
//! This module provides data structures for managing conversation sessions,
//! including session metadata, status tracking, and journey state.

use crate::context::Context;
use crate::types::{AgentId, JourneyId, SessionId, StepId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Status of a conversation session
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SessionStatus {
    /// Session is active and can process messages
    Active,
    /// Session is paused (temporarily inactive)
    Paused,
    /// Session has been completed
    Completed,
    /// Session has been terminated/cancelled
    Terminated,
}

/// Journey state within a session
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JourneyState {
    /// ID of the journey being executed
    pub journey_id: JourneyId,
    /// Current step in the journey
    pub current_step: StepId,
    /// When the journey was started
    pub started_at: DateTime<Utc>,
    /// When the journey was completed (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<DateTime<Utc>>,
    /// Step history (ordered list of visited steps)
    pub step_history: Vec<StepId>,
}

impl JourneyState {
    /// Create a new journey state
    pub fn new(journey_id: JourneyId, initial_step: StepId) -> Self {
        Self {
            journey_id,
            current_step: initial_step,
            started_at: Utc::now(),
            completed_at: None,
            step_history: vec![initial_step],
        }
    }

    /// Move to the next step
    pub fn move_to_step(&mut self, step_id: StepId) {
        self.current_step = step_id;
        self.step_history.push(step_id);
    }

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

    /// Check if the journey is completed
    pub fn is_completed(&self) -> bool {
        self.completed_at.is_some()
    }
}

/// A conversation session
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Session {
    /// Unique identifier for the session
    pub id: SessionId,
    /// ID of the agent managing this session
    pub agent_id: AgentId,
    /// Current status of the session
    pub status: SessionStatus,
    /// Conversation context (messages and variables)
    pub context: Context,
    /// Active journey state (if any)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub journey_state: Option<JourneyState>,
    /// Session metadata
    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
    pub metadata: HashMap<String, serde_json::Value>,
    /// When the session was created
    pub created_at: DateTime<Utc>,
    /// When the session was last updated
    pub updated_at: DateTime<Utc>,
    /// When the session expires (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<DateTime<Utc>>,
}

impl Session {
    /// Create a new session
    pub fn new(agent_id: AgentId) -> Self {
        let now = Utc::now();
        Self {
            id: SessionId::new(),
            agent_id,
            status: SessionStatus::Active,
            context: Context::new(),
            journey_state: None,
            metadata: HashMap::new(),
            created_at: now,
            updated_at: now,
            expires_at: None,
        }
    }

    /// Create a new session with custom context
    pub fn with_context(agent_id: AgentId, context: Context) -> Self {
        let now = Utc::now();
        Self {
            id: SessionId::new(),
            agent_id,
            status: SessionStatus::Active,
            context,
            journey_state: None,
            metadata: HashMap::new(),
            created_at: now,
            updated_at: now,
            expires_at: None,
        }
    }

    /// Set session expiration time
    pub fn with_expiration(mut self, expires_at: DateTime<Utc>) -> Self {
        self.expires_at = Some(expires_at);
        self
    }

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

    /// Update the session's updated_at timestamp
    pub fn touch(&mut self) {
        self.updated_at = Utc::now();
    }

    /// Check if the session is active
    pub fn is_active(&self) -> bool {
        self.status == SessionStatus::Active
    }

    /// Check if the session has expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            Utc::now() > expires_at
        } else {
            false
        }
    }

    /// Pause the session
    pub fn pause(&mut self) {
        self.status = SessionStatus::Paused;
        self.touch();
    }

    /// Resume the session
    pub fn resume(&mut self) {
        if self.status == SessionStatus::Paused {
            self.status = SessionStatus::Active;
            self.touch();
        }
    }

    /// Complete the session
    pub fn complete(&mut self) {
        self.status = SessionStatus::Completed;
        self.touch();
    }

    /// Terminate the session
    pub fn terminate(&mut self) {
        self.status = SessionStatus::Terminated;
        self.touch();
    }

    /// Start a journey in this session
    pub fn start_journey(&mut self, journey_id: JourneyId, initial_step: StepId) {
        self.journey_state = Some(JourneyState::new(journey_id, initial_step));
        self.touch();
    }

    /// Get the current journey state
    pub fn get_journey_state(&self) -> Option<&JourneyState> {
        self.journey_state.as_ref()
    }

    /// Get mutable journey state
    pub fn get_journey_state_mut(&mut self) -> Option<&mut JourneyState> {
        self.journey_state.as_mut()
    }

    /// Complete the active journey
    pub fn complete_journey(&mut self) {
        if let Some(ref mut journey) = self.journey_state {
            journey.complete();
            self.touch();
        }
    }

    /// Clear the journey state
    pub fn clear_journey(&mut self) {
        self.journey_state = None;
        self.touch();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::Message;
    use chrono::Duration;

    #[test]
    fn test_session_status_serialization() {
        let status = SessionStatus::Active;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"active\"");

        let deserialized: SessionStatus = serde_json::from_str(&json).unwrap();
        assert_eq!(status, deserialized);
    }

    #[test]
    fn test_journey_state_creation() {
        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.step_history.len(), 1);
        assert!(!state.is_completed());
    }

    #[test]
    fn test_journey_state_move_to_step() {
        let journey_id = JourneyId::new();
        let step1 = StepId::new();
        let step2 = StepId::new();
        let step3 = StepId::new();

        let mut state = JourneyState::new(journey_id, step1);
        state.move_to_step(step2);
        state.move_to_step(step3);

        assert_eq!(state.current_step, step3);
        assert_eq!(state.step_history.len(), 3);
        assert_eq!(state.step_history[0], step1);
        assert_eq!(state.step_history[1], step2);
        assert_eq!(state.step_history[2], step3);
    }

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

        assert!(!state.is_completed());
        state.complete();
        assert!(state.is_completed());
        assert!(state.completed_at.is_some());
    }

    #[test]
    fn test_session_creation() {
        let agent_id = AgentId::new();
        let session = Session::new(agent_id);

        assert_eq!(session.agent_id, agent_id);
        assert_eq!(session.status, SessionStatus::Active);
        assert!(session.context.messages.is_empty());
        assert!(session.journey_state.is_none());
        assert!(session.metadata.is_empty());
    }

    #[test]
    fn test_session_with_context() {
        let agent_id = AgentId::new();
        let mut context = Context::new();
        context.add_message(Message::user("Hello"));

        let session = Session::with_context(agent_id, context.clone());

        assert_eq!(session.context.messages.len(), 1);
    }

    #[test]
    fn test_session_with_expiration() {
        let agent_id = AgentId::new();
        let expires_at = Utc::now() + Duration::hours(24);
        let session = Session::new(agent_id).with_expiration(expires_at);

        assert!(session.expires_at.is_some());
        assert!(!session.is_expired());
    }

    #[test]
    fn test_session_expiration() {
        let agent_id = AgentId::new();
        let expires_at = Utc::now() - Duration::hours(1); // Expired 1 hour ago
        let session = Session::new(agent_id).with_expiration(expires_at);

        assert!(session.is_expired());
    }

    #[test]
    fn test_session_with_metadata() {
        let agent_id = AgentId::new();
        let session = Session::new(agent_id)
            .with_metadata("user_id", serde_json::json!("user123"))
            .with_metadata("language", serde_json::json!("en"));

        assert_eq!(session.metadata.len(), 2);
        assert_eq!(
            session.metadata.get("user_id"),
            Some(&serde_json::json!("user123"))
        );
    }

    #[test]
    fn test_session_lifecycle() {
        let agent_id = AgentId::new();
        let mut session = Session::new(agent_id);

        assert!(session.is_active());

        session.pause();
        assert_eq!(session.status, SessionStatus::Paused);
        assert!(!session.is_active());

        session.resume();
        assert_eq!(session.status, SessionStatus::Active);
        assert!(session.is_active());

        session.complete();
        assert_eq!(session.status, SessionStatus::Completed);
        assert!(!session.is_active());
    }

    #[test]
    fn test_session_terminate() {
        let agent_id = AgentId::new();
        let mut session = Session::new(agent_id);

        session.terminate();
        assert_eq!(session.status, SessionStatus::Terminated);
        assert!(!session.is_active());
    }

    #[test]
    fn test_session_touch() {
        let agent_id = AgentId::new();
        let mut session = Session::new(agent_id);

        let initial_updated_at = session.updated_at;
        std::thread::sleep(std::time::Duration::from_millis(10));

        session.touch();
        assert!(session.updated_at > initial_updated_at);
    }

    #[test]
    fn test_session_journey_lifecycle() {
        let agent_id = AgentId::new();
        let mut session = Session::new(agent_id);
        let journey_id = JourneyId::new();
        let step_id = StepId::new();

        assert!(session.get_journey_state().is_none());

        session.start_journey(journey_id, step_id);
        assert!(session.get_journey_state().is_some());
        assert_eq!(session.get_journey_state().unwrap().journey_id, journey_id);

        session.complete_journey();
        assert!(session.get_journey_state().unwrap().is_completed());

        session.clear_journey();
        assert!(session.get_journey_state().is_none());
    }

    #[test]
    fn test_session_journey_state_mut() {
        let agent_id = AgentId::new();
        let mut session = Session::new(agent_id);
        let journey_id = JourneyId::new();
        let step1 = StepId::new();
        let step2 = StepId::new();

        session.start_journey(journey_id, step1);

        if let Some(journey) = session.get_journey_state_mut() {
            journey.move_to_step(step2);
        }

        assert_eq!(session.get_journey_state().unwrap().current_step, step2);
    }

    #[test]
    fn test_session_serialization() {
        let agent_id = AgentId::new();
        let session = Session::new(agent_id).with_metadata("test", serde_json::json!("value"));

        let json = serde_json::to_string(&session).unwrap();
        let deserialized: Session = serde_json::from_str(&json).unwrap();

        assert_eq!(session.id, deserialized.id);
        assert_eq!(session.agent_id, deserialized.agent_id);
        assert_eq!(session.status, deserialized.status);
        assert_eq!(session.metadata, deserialized.metadata);
    }

    #[test]
    fn test_session_with_journey_serialization() {
        let agent_id = AgentId::new();
        let mut session = Session::new(agent_id);
        let journey_id = JourneyId::new();
        let step_id = StepId::new();

        session.start_journey(journey_id, step_id);

        let json = serde_json::to_string(&session).unwrap();
        let deserialized: Session = serde_json::from_str(&json).unwrap();

        assert!(deserialized.journey_state.is_some());
        assert_eq!(
            deserialized.journey_state.as_ref().unwrap().journey_id,
            journey_id
        );
    }
}