semantic_query/interceptors/
file.rs

1use super::Interceptor;
2use async_trait::async_trait;
3use std::path::PathBuf;
4use tokio::fs;
5use tokio::io::AsyncWriteExt;
6use chrono::Utc;
7
8#[derive(Debug)]
9pub struct FileInterceptor {
10    base_path: PathBuf,
11}
12
13impl FileInterceptor {
14    pub fn new(base_path: PathBuf) -> Self {
15        Self { base_path }
16    }
17}
18
19#[async_trait]
20impl Interceptor for FileInterceptor {
21    async fn save(&self, prompt: &str, response: &str) -> Result<(), Box<dyn std::error::Error>> {
22        let timestamp = Utc::now();
23        let filename = format!("query_{}.md", timestamp.format("%Y%m%d_%H%M%S_%3f"));
24        let file_path = self.base_path.join(filename);
25        
26        // Ensure the directory exists
27        if let Some(parent) = file_path.parent() {
28            fs::create_dir_all(parent).await?;
29        }
30        
31        let content = format!(
32            "# Prompt\n\n{}\n\n# Response\n\n{}\n",
33            prompt,
34            response
35        );
36        
37        let mut file = fs::File::create(&file_path).await?;
38        file.write_all(content.as_bytes()).await?;
39        file.flush().await?;
40        
41        Ok(())
42    }
43}