Skip to main content

remem/retrieval/search/memory/
explain.rs

1use serde::Serialize;
2
3#[derive(Debug, Clone, Serialize)]
4pub struct SearchExplain {
5    pub query: String,
6    pub project: Option<String>,
7    pub memory_type: Option<String>,
8    pub branch: Option<String>,
9    pub include_stale: bool,
10    pub limit: i64,
11    pub offset: i64,
12    pub fetch_limit: i64,
13    pub expanded_terms: Vec<String>,
14    pub core_terms: Vec<String>,
15    pub claim_terms: Vec<String>,
16    pub fts_query: Option<String>,
17    pub temporal_range: Option<(i64, i64)>,
18    pub temporal_field: Option<String>,
19    pub rrf_k: f64,
20    pub min_evidence_confidence: f64,
21    pub filtered_result_count: usize,
22    pub timings: Vec<crate::perf::PhaseTiming>,
23    pub channels: Vec<SearchExplainChannel>,
24    pub results: Vec<SearchExplainResult>,
25    pub has_more: bool,
26    pub raw_fallback_count: usize,
27}
28
29impl SearchExplain {
30    pub fn retain_result_ids(&mut self, result_ids: &[i64], has_more: bool, visible_limit: i64) {
31        self.has_more = has_more;
32        self.limit = visible_limit;
33        self.results
34            .retain(|result| result_ids.contains(&result.memory_id));
35        for (index, result) in self.results.iter_mut().enumerate() {
36            result.final_rank = index + 1;
37        }
38    }
39
40    pub fn set_raw_fallback_count(&mut self, count: usize) {
41        self.raw_fallback_count = count;
42    }
43}
44
45#[derive(Debug, Clone, Serialize)]
46pub struct SearchExplainChannel {
47    pub name: String,
48    pub enabled: bool,
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub disabled_reason: Option<String>,
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub candidates_scanned: Option<usize>,
53    pub hits: Vec<ChannelHit>,
54}
55
56#[derive(Debug, Clone, Serialize)]
57pub struct ChannelHit {
58    pub memory_id: i64,
59    pub rank: usize,
60}
61
62#[derive(Debug, Clone, Serialize)]
63pub struct SearchExplainResult {
64    pub memory_id: i64,
65    pub final_rank: usize,
66    pub final_score: f64,
67    pub evidence_confidence: f64,
68    pub project: String,
69    pub scope: String,
70    pub visibility: String,
71    pub staleness: crate::memory::MemoryStalenessLabel,
72    pub contributions: Vec<ChannelContribution>,
73}
74
75#[derive(Debug, Clone, Serialize)]
76pub struct ChannelContribution {
77    pub channel: String,
78    pub rank: usize,
79    pub score: f64,
80}