Skip to main content

fetchium_core/intelligence/pie/
qpm.rs

1//! Query Prediction Model (QPM) — predicts follow-up queries from historical patterns.
2//!
3//! Schema:
4//! - `query_history(id, query_hash, query_text, topic, timestamp)`
5//! - `follow_up_patterns(topic, follow_up_query, count)`
6//! - `topic_frequency(topic, count, last_queried)`
7
8use rusqlite::Connection;
9use std::sync::Mutex;
10
11use crate::error::FetchiumError;
12use crate::intelligence::{enable_wal, sha256_hex};
13
14pub struct QueryPredictionModel {
15    conn: Mutex<Connection>,
16}
17
18impl QueryPredictionModel {
19    pub fn new(db_path: &std::path::Path) -> Result<Self, FetchiumError> {
20        let conn = Connection::open(db_path)?;
21        enable_wal(&conn)?;
22        conn.execute_batch(
23            "CREATE TABLE IF NOT EXISTS query_history (
24                id          INTEGER PRIMARY KEY AUTOINCREMENT,
25                query_hash  TEXT    NOT NULL,
26                query_text  TEXT    NOT NULL,
27                topic       TEXT    NOT NULL,
28                timestamp   TEXT    NOT NULL DEFAULT (datetime('now'))
29            );
30            CREATE TABLE IF NOT EXISTS follow_up_patterns (
31                topic           TEXT NOT NULL,
32                follow_up_query TEXT NOT NULL,
33                count           INTEGER NOT NULL DEFAULT 1,
34                PRIMARY KEY (topic, follow_up_query)
35            );
36            CREATE TABLE IF NOT EXISTS topic_frequency (
37                topic        TEXT PRIMARY KEY,
38                count        INTEGER NOT NULL DEFAULT 0,
39                last_queried TEXT    NOT NULL DEFAULT (datetime('now'))
40            );",
41        )?;
42        Ok(Self {
43            conn: Mutex::new(conn),
44        })
45    }
46
47    /// Record a user query and its extracted topic.
48    ///
49    /// Optionally also record a follow-up pattern (query that immediately followed
50    /// a previous query on the same topic).
51    pub fn record_query(
52        &self,
53        query: &str,
54        topic: &str,
55        follow_up_of: Option<&str>,
56    ) -> Result<(), FetchiumError> {
57        let conn = self.conn.lock().unwrap();
58        let hash = sha256_hex(query);
59
60        conn.execute(
61            "INSERT INTO query_history (query_hash, query_text, topic) VALUES (?1, ?2, ?3)",
62            rusqlite::params![hash, query, topic],
63        )?;
64
65        if let Some(prev_topic) = follow_up_of {
66            conn.execute(
67                "INSERT INTO follow_up_patterns (topic, follow_up_query, count)
68                 VALUES (?1, ?2, 1)
69                 ON CONFLICT(topic, follow_up_query) DO UPDATE SET count = count + 1",
70                rusqlite::params![prev_topic, query],
71            )?;
72        }
73
74        conn.execute(
75            "INSERT INTO topic_frequency (topic, count, last_queried)
76             VALUES (?1, 1, datetime('now'))
77             ON CONFLICT(topic) DO UPDATE SET
78                count        = count + 1,
79                last_queried = datetime('now')",
80            [topic],
81        )?;
82
83        Ok(())
84    }
85
86    /// Predict likely follow-up queries given a current topic, sorted by frequency.
87    pub fn predict_follow_ups(
88        &self,
89        current_topic: &str,
90        limit: usize,
91    ) -> Result<Vec<String>, FetchiumError> {
92        let conn = self.conn.lock().unwrap();
93        let mut stmt = conn.prepare(
94            "SELECT follow_up_query FROM follow_up_patterns
95             WHERE topic = ?1
96             ORDER BY count DESC
97             LIMIT ?2",
98        )?;
99        let results = stmt
100            .query_map(rusqlite::params![current_topic, limit as i64], |row| {
101                row.get::<_, String>(0)
102            })?
103            .filter_map(|r| r.ok())
104            .collect();
105        Ok(results)
106    }
107
108    /// Most frequently queried topics.
109    pub fn top_topics(&self, limit: usize) -> Result<Vec<(String, u64)>, FetchiumError> {
110        let conn = self.conn.lock().unwrap();
111        let mut stmt = conn.prepare(
112            "SELECT topic, count FROM topic_frequency
113             ORDER BY count DESC
114             LIMIT ?1",
115        )?;
116        let results = stmt
117            .query_map([limit as i64], |row| {
118                Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as u64))
119            })?
120            .filter_map(|r| r.ok())
121            .collect();
122        Ok(results)
123    }
124
125    /// Total queries recorded.
126    pub fn query_count(&self) -> Result<u64, FetchiumError> {
127        let conn = self.conn.lock().unwrap();
128        let n: i64 = conn.query_row("SELECT COUNT(*) FROM query_history", [], |row| row.get(0))?;
129        Ok(n as u64)
130    }
131
132    /// Reset all query prediction data.
133    pub fn reset(&self) -> Result<(), FetchiumError> {
134        let conn = self.conn.lock().unwrap();
135        conn.execute_batch(
136            "DELETE FROM query_history;
137             DELETE FROM follow_up_patterns;
138             DELETE FROM topic_frequency;
139             VACUUM;",
140        )?;
141        Ok(())
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148    use tempfile::NamedTempFile;
149
150    #[test]
151    fn predict_returns_most_frequent_follow_up() {
152        let tmp = NamedTempFile::new().unwrap();
153        let qpm = QueryPredictionModel::new(tmp.path()).unwrap();
154
155        // Record "Rust memory model" 3× after "Rust ownership"
156        for _ in 0..3 {
157            qpm.record_query("Rust memory model", "Rust", Some("Rust"))
158                .unwrap();
159        }
160        qpm.record_query("Rust async", "Rust", Some("Rust"))
161            .unwrap();
162
163        let suggestions = qpm.predict_follow_ups("Rust", 5).unwrap();
164        assert!(
165            !suggestions.is_empty(),
166            "should predict at least one follow-up"
167        );
168        assert_eq!(suggestions[0], "Rust memory model");
169    }
170
171    #[test]
172    fn query_count_increments() {
173        let tmp = NamedTempFile::new().unwrap();
174        let qpm = QueryPredictionModel::new(tmp.path()).unwrap();
175        for i in 0..5u32 {
176            qpm.record_query(&format!("query {i}"), "topic", None)
177                .unwrap();
178        }
179        assert_eq!(qpm.query_count().unwrap(), 5);
180    }
181}