reflex/pulse/
llm_cache.rs1use anyhow::{Context, Result};
7use serde::{Deserialize, Serialize};
8use std::path::{Path, PathBuf};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct CachedResponse {
13 pub context_hash: String,
14 pub response: String,
15 pub timestamp: String,
16}
17
18pub struct LlmCache {
20 cache_dir: PathBuf,
21}
22
23impl LlmCache {
24 pub fn new(reflex_cache_path: &Path) -> Self {
26 Self {
27 cache_dir: reflex_cache_path.join("pulse").join("llm-cache"),
28 }
29 }
30
31 pub fn from_dir(cache_dir: PathBuf) -> Self {
33 Self { cache_dir }
34 }
35
36 pub fn cache_dir(&self) -> &Path {
38 &self.cache_dir
39 }
40
41 pub fn compute_key(snapshot_id: &str, module_path: &str, context: &str) -> String {
45 let input = format!("{}:{}:{}", snapshot_id, module_path, context);
46 blake3::hash(input.as_bytes()).to_hex().to_string()
47 }
48
49 pub fn get(&self, key: &str) -> Result<Option<CachedResponse>> {
51 let path = self.cache_dir.join(format!("{}.json", key));
52 if !path.exists() {
53 return Ok(None);
54 }
55
56 let content = std::fs::read_to_string(&path).context("Failed to read LLM cache entry")?;
57 let cached: CachedResponse =
58 serde_json::from_str(&content).context("Failed to parse LLM cache entry")?;
59 Ok(Some(cached))
60 }
61
62 pub fn put(&self, key: &str, context_hash: &str, response: &str) -> Result<()> {
64 std::fs::create_dir_all(&self.cache_dir).context("Failed to create LLM cache directory")?;
65
66 let entry = CachedResponse {
67 context_hash: context_hash.to_string(),
68 response: response.to_string(),
69 timestamp: chrono::Local::now().to_rfc3339(),
70 };
71
72 let json = serde_json::to_string_pretty(&entry)?;
73 let path = self.cache_dir.join(format!("{}.json", key));
74 std::fs::write(&path, json).context("Failed to write LLM cache entry")?;
75
76 Ok(())
77 }
78
79 pub fn clear(&self) -> Result<()> {
81 if self.cache_dir.exists() {
82 std::fs::remove_dir_all(&self.cache_dir).context("Failed to clear LLM cache")?;
83 }
84 Ok(())
85 }
86
87 pub fn count(&self) -> usize {
89 if !self.cache_dir.exists() {
90 return 0;
91 }
92 std::fs::read_dir(&self.cache_dir)
93 .map(|entries| entries.filter(|e| e.is_ok()).count())
94 .unwrap_or(0)
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn test_cache_key_determinism() {
104 let key1 = LlmCache::compute_key("snap1", "src", "context_abc");
105 let key2 = LlmCache::compute_key("snap1", "src", "context_abc");
106 assert_eq!(key1, key2);
107
108 let key3 = LlmCache::compute_key("snap1", "src", "context_different");
109 assert_ne!(key1, key3);
110 }
111
112 #[test]
113 fn test_cache_roundtrip() {
114 let dir = tempfile::tempdir().unwrap();
115 let cache = LlmCache::new(dir.path());
116
117 let key = "test_key_123";
118 assert!(cache.get(key).unwrap().is_none());
119 assert_eq!(cache.count(), 0);
120
121 cache
122 .put(key, "hash123", "This module handles authentication.")
123 .unwrap();
124 assert_eq!(cache.count(), 1);
125
126 let cached = cache.get(key).unwrap().unwrap();
127 assert_eq!(cached.response, "This module handles authentication.");
128 assert_eq!(cached.context_hash, "hash123");
129 }
130}