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
//! Temporal Consciousness
//!
//! Temporal pattern recognition and future projection.

use anyhow::Result;
use std::collections::{HashMap, VecDeque};

use super::super::*;
use super::state_machine::ConsciousnessState;

#[derive(Debug, Clone)]
pub struct TemporalPatternRecognition {
    patterns: Vec<String>,
    confidence: f64,
}

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

impl TemporalPatternRecognition {
    pub fn new() -> Self {
        Self {
            patterns: Vec::new(),
            confidence: 0.0,
        }
    }

    /// Find patterns relevant to the given query
    pub fn find_relevant_patterns(&self, query: &str) -> Result<Vec<String>> {
        // Simple pattern matching based on keyword overlap
        let query_words: Vec<&str> = query.split_whitespace().collect();
        let relevant_patterns = self
            .patterns
            .iter()
            .filter(|pattern| {
                let pattern_words: Vec<&str> = pattern.split_whitespace().collect();
                query_words.iter().any(|word| pattern_words.contains(word))
            })
            .cloned()
            .collect();
        Ok(relevant_patterns)
    }

    /// Update patterns with new information
    pub fn update_patterns(&mut self, new_patterns: Vec<String>) {
        for pattern in new_patterns {
            if !self.patterns.contains(&pattern) {
                self.patterns.push(pattern);
            }
        }
        // Update confidence based on pattern count
        self.confidence = (self.patterns.len() as f64).min(100.0) / 100.0;
    }
}

/// Future projection engine for predictions
#[derive(Debug, Clone)]
pub struct FutureProjectionEngine {
    predictions: Vec<String>,
    horizon: Duration,
}

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

impl FutureProjectionEngine {
    pub fn new() -> Self {
        Self {
            predictions: Vec::new(),
            horizon: Duration::from_secs(3600),
        }
    }

    /// Project future implications based on current events
    pub fn project_implications(
        &self,
        query: &str,
        events: &[TemporalEvent],
    ) -> Result<Vec<String>> {
        let mut implications = Vec::new();

        // Analyze events for potential future implications
        for event in events {
            if event.content.contains(&query.to_lowercase())
                || query.to_lowercase().contains(&event.content.to_lowercase())
            {
                let implication = format!(
                    "Based on recent event '{}', potential future development could involve {}",
                    event.content, query
                );
                implications.push(implication);
            }
        }

        // Add general implications based on existing predictions
        if implications.is_empty() {
            implications.push(format!(
                "Future implications for '{query}' will depend on emerging patterns"
            ));
        }

        Ok(implications)
    }
}

/// Temporal processing metrics
#[derive(Debug, Clone)]
pub struct TemporalMetrics {
    pub pattern_detection_rate: f64,
    pub prediction_accuracy: f64,
    pub temporal_coherence: f64,
}

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

impl TemporalMetrics {
    pub fn new() -> Self {
        Self {
            pattern_detection_rate: 0.0,
            prediction_accuracy: 0.0,
            temporal_coherence: 0.0,
        }
    }
}

/// Temporal pattern structure
#[derive(Debug, Clone)]
pub struct TemporalPattern {
    pub pattern_type: String,
    pub frequency: Duration,
    pub confidence: f64,
    pub occurrences: Vec<std::time::Instant>,
}

/// Long-term temporal trend
#[derive(Debug, Clone)]
pub struct TemporalTrend {
    pub trend_name: String,
    pub direction: f64, // positive for increasing, negative for decreasing
    pub strength: f64,
    pub timespan: Duration,
}

/// Cyclic event in temporal patterns
#[derive(Debug, Clone)]
pub struct CyclicEvent {
    pub event_type: String,
    pub cycle_duration: Duration,
    pub last_occurrence: std::time::Instant,
    pub intensity: f64,
}

/// Maintains awareness of historical context and temporal patterns
#[derive(Debug, Clone)]
pub struct TemporalConsciousness {
    temporal_memory: TemporalMemoryBank,
    pattern_recognition: TemporalPatternRecognition,
    future_projection: FutureProjectionEngine,
    temporal_metrics: TemporalMetrics,
}

#[derive(Debug, Clone)]
pub struct TemporalMemoryBank {
    short_term: VecDeque<TemporalEvent>,
    medium_term: Vec<TemporalPattern>,
    long_term: Vec<TemporalTrend>,
    cyclic_patterns: HashMap<Duration, Vec<CyclicEvent>>,
}

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

impl TemporalMemoryBank {
    pub fn new() -> Self {
        Self {
            short_term: VecDeque::new(),
            medium_term: Vec::new(),
            long_term: Vec::new(),
            cyclic_patterns: HashMap::new(),
        }
    }

    /// Get recent events within the specified duration
    pub fn get_recent_events(&self, duration: Duration) -> Vec<TemporalEvent> {
        let now = std::time::Instant::now();
        self.short_term
            .iter()
            .filter(|event| now.duration_since(event.timestamp) <= duration)
            .cloned()
            .collect()
    }
}

#[derive(Debug, Clone)]
pub struct TemporalEvent {
    content: String,
    timestamp: std::time::Instant,
    significance: f64,
    context_tags: Vec<String>,
    emotional_valence: f64,
}

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

impl TemporalConsciousness {
    pub fn new() -> Self {
        Self {
            temporal_memory: TemporalMemoryBank::new(),
            pattern_recognition: TemporalPatternRecognition::new(),
            future_projection: FutureProjectionEngine::new(),
            temporal_metrics: TemporalMetrics::new(),
        }
    }

    /// Analyze temporal context for consciousness processing
    pub fn analyze_temporal_context(
        &self,
        query: &str,
        current_time: std::time::Instant,
    ) -> Result<TemporalContext> {
        let recent_events = self
            .temporal_memory
            .get_recent_events(Duration::from_secs(3600));
        let relevant_patterns = self.pattern_recognition.find_relevant_patterns(query)?;
        let future_implications = self
            .future_projection
            .project_implications(query, &recent_events)?;

        Ok(TemporalContext {
            recent_events,
            relevant_patterns,
            future_implications,
            temporal_coherence: self.calculate_temporal_coherence(),
            time_awareness: self.calculate_time_awareness(current_time),
        })
    }

    /// Record new temporal event
    pub fn record_event(
        &mut self,
        content: String,
        significance: f64,
        tags: Vec<String>,
    ) -> Result<()> {
        let event = TemporalEvent {
            content,
            timestamp: std::time::Instant::now(),
            significance,
            context_tags: tags,
            emotional_valence: 0.0, // Could be calculated from content
        };

        self.temporal_memory.short_term.push_back(event);

        // Keep short-term memory bounded
        if self.temporal_memory.short_term.len() > 100 {
            self.temporal_memory.short_term.pop_front();
        }

        // Update patterns
        let event_contents: Vec<String> = self
            .temporal_memory
            .short_term
            .iter()
            .map(|e| e.content.clone())
            .collect();
        self.pattern_recognition.update_patterns(event_contents);

        Ok(())
    }

    /// Calculate temporal coherence based on memory patterns
    fn calculate_temporal_coherence(&self) -> f64 {
        if self.temporal_memory.short_term.is_empty() {
            return 0.5; // Neutral coherence when no events
        }

        // Calculate coherence based on event timestamps and significance
        let total_events = self.temporal_memory.short_term.len() as f64;
        let avg_significance: f64 = self
            .temporal_memory
            .short_term
            .iter()
            .map(|e| e.significance)
            .sum::<f64>()
            / total_events;

        // Normalize to 0-1 range
        avg_significance.clamp(0.0, 1.0)
    }

    /// Calculate time awareness based on current time and recent events
    fn calculate_time_awareness(&self, current_time: std::time::Instant) -> f64 {
        if self.temporal_memory.short_term.is_empty() {
            return 0.5; // Neutral awareness when no events
        }

        // Calculate awareness based on how recent the events are
        let recent_threshold = Duration::from_secs(300); // 5 minutes
        let recent_events = self
            .temporal_memory
            .short_term
            .iter()
            .filter(|e| current_time.duration_since(e.timestamp) <= recent_threshold)
            .count();

        let total_events = self.temporal_memory.short_term.len();
        let recent_ratio = recent_events as f64 / total_events as f64;

        // High ratio of recent events = high time awareness
        recent_ratio.clamp(0.0, 1.0)
    }
}

/// Supporting structures for consciousness enhancements
#[derive(Debug, Clone)]
pub struct TransitionRule {
    target_state: ConsciousnessState,
    condition: TransitionCondition,
    probability: f64,
}

impl TransitionRule {
    pub fn new(
        target_state: ConsciousnessState,
        condition: TransitionCondition,
        probability: f64,
    ) -> Self {
        Self {
            target_state,
            condition,
            probability,
        }
    }
}

#[derive(Debug, Clone)]
pub enum TransitionCondition {
    QueryComplexity(f64),
    EmotionalContent(f64),
    KeywordPresence(Vec<&'static str>),
    LogicalPattern,
    PhilosophicalContent,
    TimeElapsed(Duration),
}

#[derive(Debug, Clone)]
pub struct StateProcessingParameters {
    pub attention_focus: f64,
    pub emotional_sensitivity: f64,
    pub creativity_boost: f64,
    pub analytical_depth: f64,
    pub memory_consolidation: f64,
}

impl Default for StateProcessingParameters {
    fn default() -> Self {
        Self {
            attention_focus: 0.5,
            emotional_sensitivity: 0.5,
            creativity_boost: 0.5,
            analytical_depth: 0.5,
            memory_consolidation: 0.5,
        }
    }
}

#[derive(Debug, Clone)]
pub struct StateMetrics {
    state_transitions: usize,
    average_state_duration: Duration,
    most_frequent_state: ConsciousnessState,
    state_effectiveness: HashMap<ConsciousnessState, f64>,
}

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

impl StateMetrics {
    pub fn new() -> Self {
        Self {
            state_transitions: 0,
            average_state_duration: Duration::from_secs(0),
            most_frequent_state: ConsciousnessState::Baseline,
            state_effectiveness: HashMap::new(),
        }
    }
}

// Additional supporting structures would continue here...

// Use NeuralActivation from consciousness_types instead
// This provides an extensive foundation for advanced consciousness processing