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
//! Context Window Management
//!
//! Sliding window implementation with message pinning and adaptive sizing.

use super::config::ContextConfig;
use super::types::*;
use crate::Message;
use anyhow::{anyhow, Result};
use std::collections::{HashMap, VecDeque};
use std::time::SystemTime;
use tracing::{debug, info, warn};

impl ContextWindow {
    pub fn new(config: &ContextConfig) -> Self {
        Self {
            config: config.clone(),
            messages: VecDeque::new(),
            pinned_messages: HashMap::new(),
            summary: None,
            total_token_count: 0,
        }
    }

    pub async fn add_message(
        &mut self,
        message: Message,
        importance_score: f32,
    ) -> Result<WindowUpdate> {
        let context_message = ContextMessage {
            message,
            importance_score,
            added_at: SystemTime::now(),
            last_accessed: SystemTime::now(),
            access_count: 1,
        };

        // Estimate token count for this message
        let message_tokens = context_message.message.content.len() / 4; // Rough estimate
        self.total_token_count += message_tokens;

        self.messages.push_back(context_message);

        // Check if we need to trim the window
        let mut evicted_messages = Vec::new();
        while self.should_trim_window() {
            if let Some(evicted) = self.evict_least_important().await? {
                evicted_messages.push(evicted);
            } else {
                break;
            }
        }

        Ok(WindowUpdate {
            message_added: true,
            evicted_messages,
            current_size: self.messages.len(),
            token_count: self.total_token_count,
        })
    }

    pub async fn get_effective_messages(&self) -> Result<Vec<Message>> {
        let mut effective_messages = Vec::new();

        // Add pinned messages first
        for pinned in self.pinned_messages.values() {
            if let Some(context_msg) = self
                .messages
                .iter()
                .find(|m| m.message.id == pinned.message_id)
            {
                effective_messages.push(context_msg.message.clone());
            }
        }

        // Add recent messages up to window size
        let recent_count = self
            .config
            .sliding_window_size
            .saturating_sub(effective_messages.len());
        for context_msg in self.messages.iter().rev().take(recent_count) {
            if !effective_messages
                .iter()
                .any(|m| m.id == context_msg.message.id)
            {
                effective_messages.push(context_msg.message.clone());
            }
        }

        // Sort by timestamp to maintain conversation order
        effective_messages.sort_by_key(|m| m.timestamp);

        Ok(effective_messages)
    }

    pub async fn pin_message(&mut self, message_id: &str, reason: PinReason) -> Result<()> {
        if let Some(context_msg) = self.messages.iter().find(|m| m.message.id == message_id) {
            let pinned = PinnedMessage {
                message_id: message_id.to_string(),
                reason,
                pinned_at: SystemTime::now(),
                importance_score: context_msg.importance_score,
            };
            self.pinned_messages.insert(message_id.to_string(), pinned);
            debug!("Pinned message: {}", message_id);
        } else {
            return Err(anyhow!(
                "Message not found in context window: {}",
                message_id
            ));
        }
        Ok(())
    }

    pub async fn unpin_message(&mut self, message_id: &str) -> Result<()> {
        if self.pinned_messages.remove(message_id).is_some() {
            debug!("Unpinned message: {}", message_id);
        } else {
            warn!("Attempted to unpin non-pinned message: {}", message_id);
        }
        Ok(())
    }

    fn should_trim_window(&self) -> bool {
        self.messages.len() > self.config.sliding_window_size
            || self.total_token_count > self.config.max_context_length
    }

    async fn evict_least_important(&mut self) -> Result<Option<Message>> {
        // Find least important non-pinned message
        let mut least_important_idx = None;
        let mut min_score = f32::MAX;

        for (idx, context_msg) in self.messages.iter().enumerate() {
            if !self.pinned_messages.contains_key(&context_msg.message.id)
                && context_msg.importance_score < min_score
            {
                min_score = context_msg.importance_score;
                least_important_idx = Some(idx);
            }
        }

        if let Some(idx) = least_important_idx {
            if let Some(evicted) = self.messages.remove(idx) {
                let evicted_tokens = evicted.message.content.len() / 4;
                self.total_token_count = self.total_token_count.saturating_sub(evicted_tokens);
                return Ok(Some(evicted.message));
            }
        }

        Ok(None)
    }

    pub async fn should_summarize(&self) -> bool {
        self.config.enable_summarization
            && self.messages.len() >= self.config.summarization_threshold
    }

    pub async fn get_messages_for_summarization(&self) -> Result<Vec<Message>> {
        // Get older messages that aren't pinned
        let cutoff_idx = self
            .messages
            .len()
            .saturating_sub(self.config.sliding_window_size);

        Ok(self
            .messages
            .iter()
            .take(cutoff_idx)
            .filter(|m| !self.pinned_messages.contains_key(&m.message.id))
            .map(|m| m.message.clone())
            .collect::<Vec<_>>())
    }

    pub async fn apply_summarization(&mut self, summary: ContextSummary) -> Result<()> {
        // Remove summarized messages
        let cutoff_idx = self
            .messages
            .len()
            .saturating_sub(self.config.sliding_window_size);
        for _ in 0..cutoff_idx {
            if let Some(removed) = self.messages.pop_front() {
                if !self.pinned_messages.contains_key(&removed.message.id) {
                    let removed_tokens = removed.message.content.len() / 4;
                    self.total_token_count = self.total_token_count.saturating_sub(removed_tokens);
                }
            }
        }

        self.summary = Some(summary);
        Ok(())
    }

    pub async fn get_summary(&self) -> Option<ContextSummary> {
        self.summary.clone()
    }

    pub async fn total_messages(&self) -> usize {
        self.messages.len()
    }

    pub async fn active_messages(&self) -> usize {
        std::cmp::min(self.messages.len(), self.config.sliding_window_size)
    }

    pub async fn pinned_count(&self) -> usize {
        self.pinned_messages.len()
    }

    pub async fn get_state_snapshot(&self) -> ContextState {
        ContextState {
            message_count: self.messages.len(),
            pinned_count: self.pinned_messages.len(),
            token_count: self.total_token_count,
            has_summary: self.summary.is_some(),
            current_topic: None,
        }
    }

    pub async fn get_state_snapshot_with_topic(
        &self,
        current_topic: Option<String>,
    ) -> ContextState {
        ContextState {
            message_count: self.messages.len(),
            pinned_count: self.pinned_messages.len(),
            token_count: self.total_token_count,
            has_summary: self.summary.is_some(),
            current_topic,
        }
    }

    pub async fn adjust_for_topic(
        &mut self,
        transition: &TopicTransition,
    ) -> Result<WindowAdjustment> {
        let mut messages_reordered = false;
        let mut importance_rescored = false;
        let mut window_size_adjusted = false;

        // 1. Adjust window size based on topic complexity and confidence
        let optimal_window_size = self
            .calculate_optimal_window_size_for_topic(transition)
            .await;
        if optimal_window_size != self.config.sliding_window_size {
            self.config.sliding_window_size = optimal_window_size;
            window_size_adjusted = true;
            debug!(
                "Adjusted window size to {} for topic '{}'",
                optimal_window_size, transition.to_topic
            );
        }

        // 2. Rescore message importance based on topic relevance
        if transition.confidence > 0.6 {
            importance_rescored = self.rescore_messages_for_topic(transition).await?;
        }

        // 3. Reorder messages based on new importance scores
        if importance_rescored {
            messages_reordered = self.reorder_messages_by_importance().await;
        }

        // 4. Apply topic-specific filtering if needed
        if transition.confidence > 0.8 {
            self.apply_topic_specific_filtering(transition).await?;
        }

        // 5. Adjust message priorities for pinned messages
        self.adjust_pinned_message_priorities_for_topic(transition)
            .await?;

        info!(
            "Topic adjustments completed for '{}': reordered={}, rescored={}, window_adjusted={}",
            transition.to_topic, messages_reordered, importance_rescored, window_size_adjusted
        );

        Ok(WindowAdjustment {
            messages_reordered,
            importance_rescored,
            window_size_adjusted,
        })
    }

    /// Calculate optimal window size for the given topic
    async fn calculate_optimal_window_size_for_topic(&self, transition: &TopicTransition) -> usize {
        let base_size = self.config.sliding_window_size;

        // Adjust based on topic complexity (estimated from topic name length and confidence)
        let topic_complexity_factor = if transition.to_topic.len() > 20 {
            1.2 // Complex topics need more context
        } else if transition.to_topic.len() < 10 {
            0.8 // Simple topics need less context
        } else {
            1.0
        };

        // Adjust based on transition confidence
        let confidence_factor = if transition.confidence > 0.9 {
            1.1 // High confidence topics can use more context
        } else if transition.confidence < 0.5 {
            0.9 // Low confidence topics should use less context
        } else {
            1.0
        };

        let adjusted_size =
            (base_size as f32 * topic_complexity_factor * confidence_factor) as usize;

        // Clamp to reasonable bounds
        adjusted_size.clamp(10, 100)
    }

    /// Rescore messages based on their relevance to the new topic
    async fn rescore_messages_for_topic(&mut self, transition: &TopicTransition) -> Result<bool> {
        let mut rescored = false;
        let topic_keywords = self.extract_topic_keywords(&transition.to_topic);

        // Create a vector of adjustments to avoid borrow checker issues
        let mut adjustments = Vec::new();

        for (index, context_message) in self.messages.iter().enumerate() {
            let topic_relevance = Self::calculate_message_topic_relevance_static(
                &context_message.message,
                &topic_keywords,
            );

            let original_score = context_message.importance_score;
            let topic_adjustment = match topic_relevance {
                relevance if relevance > 0.8 => 1.3, // High relevance boost
                relevance if relevance > 0.5 => 1.1, // Moderate relevance boost
                relevance if relevance > 0.2 => 1.0, // No change
                _ => 0.8,                            // Low relevance penalty
            };

            let new_score = (original_score * topic_adjustment).min(1.0);
            adjustments.push((index, new_score, original_score));
        }

        // Apply adjustments
        for (index, new_score, original_score) in adjustments {
            if let Some(context_message) = self.messages.get_mut(index) {
                context_message.importance_score = new_score;
                if (new_score - original_score).abs() > 0.05 {
                    rescored = true;
                }
            }
        }

        if rescored {
            debug!(
                "Rescored {} messages for topic relevance",
                self.messages.len()
            );
        }

        Ok(rescored)
    }

    /// Extract keywords from topic name for relevance calculation
    fn extract_topic_keywords(&self, topic: &str) -> Vec<String> {
        topic
            .to_lowercase()
            .split(|c: char| !c.is_alphanumeric())
            .filter(|word| word.len() > 2) // Filter out short words
            .map(|word| word.to_string())
            .collect()
    }

    /// Calculate how relevant a message is to the current topic
    async fn calculate_message_topic_relevance(
        &self,
        message: &Message,
        topic_keywords: &[String],
    ) -> f32 {
        Self::calculate_message_topic_relevance_static(message, topic_keywords)
    }

    /// Static version to avoid borrow checker issues
    fn calculate_message_topic_relevance_static(
        message: &Message,
        topic_keywords: &[String],
    ) -> f32 {
        let message_text = message.content.to_lowercase();
        let mut relevance_score = 0.0;
        let mut keyword_matches = 0;

        for keyword in topic_keywords {
            if message_text.contains(keyword) {
                keyword_matches += 1;
                relevance_score += 0.2; // Base score per keyword match

                // Bonus for exact word matches (not just substring matches)
                if message_text.split_whitespace().any(|word| word == keyword) {
                    relevance_score += 0.1;
                }
            }
        }

        // Apply diminishing returns for multiple keyword matches
        if keyword_matches > 0 {
            relevance_score *= 1.0 - (keyword_matches as f32 * 0.05).min(0.3);
        }

        // Check message metadata for additional topic relevance indicators
        if let Some(_metadata) = &message.metadata {
            // Basic boost for messages with metadata (indicating they were processed)
            relevance_score += 0.05;
        }

        relevance_score.min(1.0)
    }

    /// Reorder messages by their importance scores
    async fn reorder_messages_by_importance(&mut self) -> bool {
        let original_order: Vec<_> = self.messages.iter().map(|m| m.message.id.clone()).collect();

        // Sort by importance score (descending) while maintaining relative chronological order for equal scores
        self.messages.make_contiguous().sort_by(|a, b| {
            b.importance_score
                .partial_cmp(&a.importance_score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.added_at.cmp(&b.added_at))
        });

        let new_order: Vec<_> = self.messages.iter().map(|m| m.message.id.clone()).collect();
        let reordered = original_order != new_order;

        if reordered {
            debug!("Reordered {} messages by importance", self.messages.len());
        }

        reordered
    }

    /// Apply topic-specific filtering to remove less relevant messages
    async fn apply_topic_specific_filtering(&mut self, transition: &TopicTransition) -> Result<()> {
        let original_count = self.messages.len();

        // Remove messages with very low importance scores (below threshold)
        let min_importance_threshold = if transition.confidence > 0.9 {
            0.3
        } else {
            0.2
        };

        self.messages
            .retain(|context_message| context_message.importance_score >= min_importance_threshold);

        let filtered_count = original_count - self.messages.len();
        if filtered_count > 0 {
            debug!(
                "Filtered out {} low-importance messages for topic '{}'",
                filtered_count, transition.to_topic
            );
        }

        Ok(())
    }

    /// Adjust priorities for pinned messages based on topic relevance
    async fn adjust_pinned_message_priorities_for_topic(
        &mut self,
        transition: &TopicTransition,
    ) -> Result<()> {
        let topic_keywords = self.extract_topic_keywords(&transition.to_topic);

        // Collect adjustments to avoid borrow checker issues
        let mut adjustments = Vec::new();

        for (message_id, pinned_message) in &self.pinned_messages {
            if let Some(context_message) =
                self.messages.iter().find(|m| m.message.id == *message_id)
            {
                let topic_relevance = Self::calculate_message_topic_relevance_static(
                    &context_message.message,
                    &topic_keywords,
                );

                let original_score = pinned_message.importance_score;
                let new_score = (original_score + topic_relevance * 0.3).min(1.0);

                adjustments.push((message_id.clone(), original_score, new_score));
            }
        }

        // Apply adjustments
        for (message_id, original_score, new_score) in adjustments {
            if let Some(pinned_message) = self.pinned_messages.get_mut(&message_id) {
                pinned_message.importance_score = new_score;

                debug!(
                    "Adjusted pinned message '{}' importance from {:.2} to {:.2} for topic relevance",
                    message_id, original_score, new_score
                );
            }
        }

        Ok(())
    }
}