oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
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 state machine for multi-turn chat interactions.
//!
//! Tracks the lifecycle of a conversation through well-defined phases,
//! enforcing valid transitions and recording the full transition history.

use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Phase and transition types
// ---------------------------------------------------------------------------

/// The current phase of a conversation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConversationPhase {
    /// Initial greeting / session start.
    Greeting,
    /// Gathering additional information from the user.
    Clarification,
    /// Performing computation or retrieval behind the scenes.
    Processing,
    /// Delivering a response to the user.
    Responding,
    /// Waiting for the user to confirm or reject a proposal.
    WaitingForConfirmation,
    /// The conversation has finished successfully.
    Completed,
    /// The conversation ended due to an error.
    Error,
}

/// A recorded phase transition.
#[derive(Debug, Clone)]
pub struct StateTransition {
    /// The phase before the transition.
    pub from: ConversationPhase,
    /// The phase after the transition.
    pub to: ConversationPhase,
    /// A human-readable description of what caused the transition.
    pub trigger: String,
    /// Millisecond timestamp of the transition.
    pub timestamp: u64,
}

// ---------------------------------------------------------------------------
// ConversationState
// ---------------------------------------------------------------------------

/// State machine for a single conversation session.
pub struct ConversationState {
    id: String,
    phase: ConversationPhase,
    history: Vec<StateTransition>,
    context: HashMap<String, String>,
    created_at: u64,
    turn_count: usize,
}

impl ConversationState {
    /// Create a new conversation in the [`ConversationPhase::Greeting`] phase.
    pub fn new(id: impl Into<String>, now_ms: u64) -> Self {
        Self {
            id: id.into(),
            phase: ConversationPhase::Greeting,
            history: Vec::new(),
            context: HashMap::new(),
            created_at: now_ms,
            turn_count: 0,
        }
    }

    /// Attempt a transition to `to`, recording the transition with `trigger`.
    ///
    /// Returns `true` when the transition is allowed; `false` when `to` is not
    /// a valid successor to the current phase.
    pub fn transition(
        &mut self,
        to: ConversationPhase,
        trigger: impl Into<String>,
        now_ms: u64,
    ) -> bool {
        let allowed = Self::allowed_transitions(&self.phase);
        if !allowed.contains(&to) {
            return false;
        }
        let t = StateTransition {
            from: self.phase.clone(),
            to: to.clone(),
            trigger: trigger.into(),
            timestamp: now_ms,
        };
        self.history.push(t);
        self.phase = to;
        true
    }

    /// The current phase.
    pub fn current_phase(&self) -> &ConversationPhase {
        &self.phase
    }

    /// Store a context key-value pair.
    pub fn set_context(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.context.insert(key.into(), value.into());
    }

    /// Retrieve a context value by key.
    pub fn get_context(&self, key: &str) -> Option<&str> {
        self.context.get(key).map(String::as_str)
    }

    /// Increment the conversation turn counter.
    pub fn increment_turn(&mut self) {
        self.turn_count += 1;
    }

    /// The number of turns recorded so far.
    pub fn turn_count(&self) -> usize {
        self.turn_count
    }

    /// The full history of phase transitions in chronological order.
    pub fn history(&self) -> &[StateTransition] {
        &self.history
    }

    /// Whether the conversation is in a terminal phase (Completed or Error).
    pub fn is_terminal(&self) -> bool {
        matches!(
            self.phase,
            ConversationPhase::Completed | ConversationPhase::Error
        )
    }

    /// The set of phases that can be reached from `phase`.
    pub fn allowed_transitions(phase: &ConversationPhase) -> Vec<ConversationPhase> {
        match phase {
            ConversationPhase::Greeting => vec![
                ConversationPhase::Clarification,
                ConversationPhase::Processing,
                ConversationPhase::Error,
            ],
            ConversationPhase::Clarification => vec![
                ConversationPhase::Processing,
                ConversationPhase::Clarification,
                ConversationPhase::Error,
            ],
            ConversationPhase::Processing => vec![
                ConversationPhase::Responding,
                ConversationPhase::WaitingForConfirmation,
                ConversationPhase::Error,
            ],
            ConversationPhase::Responding => vec![
                ConversationPhase::Greeting,
                ConversationPhase::Clarification,
                ConversationPhase::Processing,
                ConversationPhase::Completed,
                ConversationPhase::Error,
            ],
            ConversationPhase::WaitingForConfirmation => vec![
                ConversationPhase::Processing,
                ConversationPhase::Clarification,
                ConversationPhase::Completed,
                ConversationPhase::Error,
            ],
            ConversationPhase::Completed => vec![],
            ConversationPhase::Error => vec![],
        }
    }

    /// Elapsed time since the conversation was created.
    pub fn duration_ms(&self, now_ms: u64) -> u64 {
        now_ms.saturating_sub(self.created_at)
    }

    /// The conversation's unique identifier.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Millisecond timestamp at which the conversation was created.
    pub fn created_at(&self) -> u64 {
        self.created_at
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn new_state(id: &str) -> ConversationState {
        ConversationState::new(id, 0)
    }

    // --- creation ---

    #[test]
    fn test_new_starts_in_greeting() {
        let s = new_state("sess1");
        assert_eq!(s.current_phase(), &ConversationPhase::Greeting);
    }

    #[test]
    fn test_new_id_preserved() {
        let s = new_state("my-conversation-id");
        assert_eq!(s.id(), "my-conversation-id");
    }

    #[test]
    fn test_new_created_at_stored() {
        let s = ConversationState::new("s", 42_000);
        assert_eq!(s.created_at(), 42_000);
    }

    #[test]
    fn test_new_empty_history() {
        let s = new_state("s");
        assert!(s.history().is_empty());
    }

    #[test]
    fn test_new_turn_count_zero() {
        let s = new_state("s");
        assert_eq!(s.turn_count(), 0);
    }

    // --- valid transitions ---

    #[test]
    fn test_greeting_to_processing_valid() {
        let mut s = new_state("s");
        assert!(s.transition(ConversationPhase::Processing, "user sent query", 100));
        assert_eq!(s.current_phase(), &ConversationPhase::Processing);
    }

    #[test]
    fn test_greeting_to_clarification_valid() {
        let mut s = new_state("s");
        assert!(s.transition(ConversationPhase::Clarification, "needs more info", 100));
        assert_eq!(s.current_phase(), &ConversationPhase::Clarification);
    }

    #[test]
    fn test_processing_to_responding_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 100);
        assert!(s.transition(ConversationPhase::Responding, "done", 200));
        assert_eq!(s.current_phase(), &ConversationPhase::Responding);
    }

    #[test]
    fn test_responding_to_completed_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 100);
        s.transition(ConversationPhase::Responding, "t", 200);
        assert!(s.transition(ConversationPhase::Completed, "done", 300));
        assert_eq!(s.current_phase(), &ConversationPhase::Completed);
    }

    #[test]
    fn test_clarification_to_processing_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Clarification, "t", 100);
        assert!(s.transition(ConversationPhase::Processing, "got info", 200));
    }

    #[test]
    fn test_clarification_to_clarification_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Clarification, "t", 100);
        assert!(s.transition(ConversationPhase::Clarification, "still need info", 200));
    }

    #[test]
    fn test_any_phase_to_error_valid() {
        let phases = [
            ConversationPhase::Greeting,
            ConversationPhase::Clarification,
            ConversationPhase::Processing,
            ConversationPhase::Responding,
            ConversationPhase::WaitingForConfirmation,
        ];
        for phase in &phases {
            let allowed = ConversationState::allowed_transitions(phase);
            assert!(
                allowed.contains(&ConversationPhase::Error),
                "Error should be reachable from {phase:?}"
            );
        }
    }

    // --- invalid transitions ---

    #[test]
    fn test_greeting_to_completed_invalid() {
        let mut s = new_state("s");
        assert!(!s.transition(ConversationPhase::Completed, "skip", 100));
        assert_eq!(s.current_phase(), &ConversationPhase::Greeting);
    }

    #[test]
    fn test_completed_to_any_invalid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 10);
        s.transition(ConversationPhase::Responding, "t", 20);
        s.transition(ConversationPhase::Completed, "t", 30);
        assert!(!s.transition(ConversationPhase::Greeting, "restart", 40));
        assert_eq!(s.current_phase(), &ConversationPhase::Completed);
    }

    #[test]
    fn test_error_to_any_invalid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Error, "crash", 100);
        assert!(!s.transition(ConversationPhase::Greeting, "retry", 200));
    }

    #[test]
    fn test_invalid_transition_does_not_record_history() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Completed, "skip", 100);
        assert!(s.history().is_empty());
    }

    // --- history ---

    #[test]
    fn test_history_records_transition() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "user asked", 500);
        assert_eq!(s.history().len(), 1);
        let t = &s.history()[0];
        assert_eq!(t.from, ConversationPhase::Greeting);
        assert_eq!(t.to, ConversationPhase::Processing);
        assert_eq!(t.trigger, "user asked");
        assert_eq!(t.timestamp, 500);
    }

    #[test]
    fn test_history_grows_with_transitions() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t1", 100);
        s.transition(ConversationPhase::Responding, "t2", 200);
        s.transition(ConversationPhase::Completed, "t3", 300);
        assert_eq!(s.history().len(), 3);
    }

    #[test]
    fn test_history_chronological() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "a", 100);
        s.transition(ConversationPhase::Responding, "b", 200);
        assert!(s.history()[0].timestamp <= s.history()[1].timestamp);
    }

    // --- context ---

    #[test]
    fn test_set_and_get_context() {
        let mut s = new_state("s");
        s.set_context("user_name", "Alice");
        assert_eq!(s.get_context("user_name"), Some("Alice"));
    }

    #[test]
    fn test_get_context_missing_key_returns_none() {
        let s = new_state("s");
        assert!(s.get_context("nonexistent").is_none());
    }

    #[test]
    fn test_set_context_overwrite() {
        let mut s = new_state("s");
        s.set_context("k", "v1");
        s.set_context("k", "v2");
        assert_eq!(s.get_context("k"), Some("v2"));
    }

    #[test]
    fn test_context_multiple_keys() {
        let mut s = new_state("s");
        s.set_context("a", "1");
        s.set_context("b", "2");
        s.set_context("c", "3");
        assert_eq!(s.get_context("a"), Some("1"));
        assert_eq!(s.get_context("b"), Some("2"));
        assert_eq!(s.get_context("c"), Some("3"));
    }

    // --- turn_count ---

    #[test]
    fn test_increment_turn() {
        let mut s = new_state("s");
        s.increment_turn();
        assert_eq!(s.turn_count(), 1);
        s.increment_turn();
        assert_eq!(s.turn_count(), 2);
    }

    // --- is_terminal ---

    #[test]
    fn test_is_terminal_completed() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 10);
        s.transition(ConversationPhase::Responding, "t", 20);
        s.transition(ConversationPhase::Completed, "t", 30);
        assert!(s.is_terminal());
    }

    #[test]
    fn test_is_terminal_error() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Error, "crash", 10);
        assert!(s.is_terminal());
    }

    #[test]
    fn test_is_not_terminal_processing() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 10);
        assert!(!s.is_terminal());
    }

    #[test]
    fn test_is_not_terminal_greeting() {
        let s = new_state("s");
        assert!(!s.is_terminal());
    }

    // --- allowed_transitions ---

    #[test]
    fn test_allowed_from_completed_is_empty() {
        let allowed = ConversationState::allowed_transitions(&ConversationPhase::Completed);
        assert!(allowed.is_empty());
    }

    #[test]
    fn test_allowed_from_error_is_empty() {
        let allowed = ConversationState::allowed_transitions(&ConversationPhase::Error);
        assert!(allowed.is_empty());
    }

    #[test]
    fn test_allowed_from_greeting_non_empty() {
        let allowed = ConversationState::allowed_transitions(&ConversationPhase::Greeting);
        assert!(!allowed.is_empty());
    }

    #[test]
    fn test_allowed_from_waiting_for_confirmation() {
        let allowed =
            ConversationState::allowed_transitions(&ConversationPhase::WaitingForConfirmation);
        assert!(allowed.contains(&ConversationPhase::Processing));
        assert!(allowed.contains(&ConversationPhase::Completed));
        assert!(allowed.contains(&ConversationPhase::Error));
    }

    // --- duration_ms ---

    #[test]
    fn test_duration_ms_zero_when_same_time() {
        let s = ConversationState::new("s", 5000);
        assert_eq!(s.duration_ms(5000), 0);
    }

    #[test]
    fn test_duration_ms_elapsed() {
        let s = ConversationState::new("s", 1000);
        assert_eq!(s.duration_ms(4000), 3000);
    }

    #[test]
    fn test_duration_ms_saturating_subtraction() {
        let s = ConversationState::new("s", 5000);
        // now_ms before created_at — should not panic
        assert_eq!(s.duration_ms(1000), 0);
    }

    // --- combined scenarios ---

    #[test]
    fn test_full_happy_path() {
        let mut s = ConversationState::new("conv1", 0);
        assert!(s.transition(ConversationPhase::Processing, "query received", 100));
        s.increment_turn();
        s.set_context("query", "What is Jena?");
        assert!(s.transition(ConversationPhase::Responding, "answer ready", 200));
        s.increment_turn();
        assert!(s.transition(ConversationPhase::Completed, "user satisfied", 300));

        assert!(s.is_terminal());
        assert_eq!(s.turn_count(), 2);
        assert_eq!(s.get_context("query"), Some("What is Jena?"));
        assert_eq!(s.history().len(), 3);
    }

    #[test]
    fn test_error_path_from_processing() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "start", 100);
        s.transition(ConversationPhase::Error, "timeout", 500);
        assert!(s.is_terminal());
        assert!(!s.transition(ConversationPhase::Greeting, "retry", 600));
    }

    #[test]
    fn test_greeting_to_error_valid() {
        let mut s = new_state("s");
        assert!(s.transition(ConversationPhase::Error, "auth failed", 50));
        assert!(s.is_terminal());
    }

    #[test]
    fn test_responding_back_to_greeting() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 1);
        s.transition(ConversationPhase::Responding, "t", 2);
        assert!(s.transition(ConversationPhase::Greeting, "new topic", 3));
        assert_eq!(s.current_phase(), &ConversationPhase::Greeting);
    }

    #[test]
    fn test_processing_to_waiting_for_confirmation() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 1);
        assert!(s.transition(
            ConversationPhase::WaitingForConfirmation,
            "confirm action?",
            2
        ));
        assert_eq!(
            s.current_phase(),
            &ConversationPhase::WaitingForConfirmation
        );
    }

    #[test]
    fn test_phase_equality() {
        assert_eq!(ConversationPhase::Greeting, ConversationPhase::Greeting);
        assert_ne!(ConversationPhase::Greeting, ConversationPhase::Error);
    }

    // --- additional coverage ---

    #[test]
    fn test_waiting_for_confirmation_to_completed_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 1);
        s.transition(ConversationPhase::WaitingForConfirmation, "confirm?", 2);
        assert!(s.transition(ConversationPhase::Completed, "confirmed", 3));
        assert!(s.is_terminal());
    }

    #[test]
    fn test_waiting_for_confirmation_to_clarification_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 1);
        s.transition(ConversationPhase::WaitingForConfirmation, "confirm?", 2);
        assert!(s.transition(ConversationPhase::Clarification, "unclear", 3));
        assert_eq!(s.current_phase(), &ConversationPhase::Clarification);
    }

    #[test]
    fn test_transition_trigger_string_stored() {
        let mut s = new_state("s");
        let trigger = "user sent hello";
        s.transition(ConversationPhase::Processing, trigger, 100);
        assert_eq!(s.history()[0].trigger, trigger);
    }

    #[test]
    fn test_processing_to_clarification_invalid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 1);
        // Processing → Clarification is NOT in allowed list
        assert!(!s.transition(ConversationPhase::Clarification, "ask more", 2));
    }

    #[test]
    fn test_responding_to_processing_valid() {
        let mut s = new_state("s");
        s.transition(ConversationPhase::Processing, "t", 1);
        s.transition(ConversationPhase::Responding, "t", 2);
        assert!(s.transition(ConversationPhase::Processing, "follow-up", 3));
        assert_eq!(s.current_phase(), &ConversationPhase::Processing);
    }
}