vtcode_core/prompts/
system_prompt_cache.rs1use lru::LruCache;
2use std::num::NonZeroUsize;
3use std::sync::{LazyLock, Mutex};
4
5const MAX_CACHE_SIZE: usize = 128;
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
10pub enum TaskType {
11 System,
12 Lightweight,
13 Specialized,
14}
15
16pub trait PromptProvider {
18 fn cache_key(&self) -> String;
19 fn task_type(&self) -> TaskType;
20}
21
22pub struct SystemPromptCache {
25 entries: Mutex<LruCache<String, String>>,
26}
27
28impl Default for SystemPromptCache {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl SystemPromptCache {
35 pub fn new() -> Self {
36 let cache_size = NonZeroUsize::new(MAX_CACHE_SIZE).unwrap_or(NonZeroUsize::MIN);
37 Self {
38 entries: Mutex::new(LruCache::new(cache_size)),
39 }
40 }
41
42 pub fn get_or_insert_with<F>(&self, key: &str, builder: F) -> String
45 where
46 F: FnOnce() -> String,
47 {
48 {
50 let mut store = self.entries.lock().unwrap_or_else(|p| p.into_inner());
51 if let Some(value) = store.get(key) {
52 return value.clone();
53 }
54 }
55
56 let value = builder();
58
59 {
61 let mut store = self.entries.lock().unwrap_or_else(|p| p.into_inner());
62 store.put(key.to_string(), value.clone());
63 }
65
66 value
67 }
68
69 pub fn get(&self, key: &str) -> Option<String> {
71 let mut store = self.entries.lock().unwrap_or_else(|p| p.into_inner());
72 store.get(key).cloned()
73 }
74
75 pub fn insert(&self, key: String, value: String) {
77 let mut store = self.entries.lock().unwrap_or_else(|p| p.into_inner());
78 store.put(key, value);
79 }
81
82 pub fn clear(&self) {
83 if let Ok(mut store) = self.entries.lock() {
84 store.clear();
85 }
86 }
87}
88
89pub static PROMPT_CACHE: LazyLock<SystemPromptCache> = LazyLock::new(SystemPromptCache::new);