1#![warn(missing_docs)]
16#![warn(clippy::all)]
17
18use std::collections::HashMap;
19use std::path::{Path, PathBuf};
20
21use infernum_core::Result;
22use serde::{Deserialize, Serialize};
23
24pub const GRIMOIRE_PATH_ENV: &str = "INFERNUM_GRIMOIRE_PATH";
26
27#[must_use]
33pub fn default_grimoire_path() -> PathBuf {
34 if let Ok(path) = std::env::var(GRIMOIRE_PATH_ENV) {
36 return PathBuf::from(path);
37 }
38
39 dirs::data_local_dir()
41 .unwrap_or_else(|| PathBuf::from("."))
42 .join("infernum")
43 .join("personas")
44}
45
46#[deprecated(since = "0.2.0", note = "Use default_grimoire_path() instead")]
48pub const DEFAULT_GRIMOIRE_PATH: &str = "~/.local/share/infernum/personas/";
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct GrimoirePersona {
53 pub id: String,
55 pub name: String,
57 pub system_prompt: String,
59 pub variants: HashMap<String, String>,
61 pub metadata: HashMap<String, String>,
63}
64
65pub struct GrimoireLoader {
67 base_path: PathBuf,
68 cache: dashmap::DashMap<String, GrimoirePersona>,
69}
70
71impl GrimoireLoader {
72 #[must_use]
77 pub fn new() -> Self {
78 Self::with_path(default_grimoire_path())
79 }
80
81 #[must_use]
83 pub fn with_path(path: impl Into<PathBuf>) -> Self {
84 Self {
85 base_path: path.into(),
86 cache: dashmap::DashMap::new(),
87 }
88 }
89
90 pub async fn load(&self, persona_id: &str) -> Result<GrimoirePersona> {
96 if let Some(cached) = self.cache.get(persona_id) {
98 return Ok(cached.clone());
99 }
100
101 let path = self.base_path.join(persona_id);
103 let prompt_path = if path.is_dir() {
104 path.join("prompt.md")
105 } else {
106 path.with_extension("md")
107 };
108
109 let content = tokio::fs::read_to_string(&prompt_path)
111 .await
112 .map_err(|e| infernum_core::Error::Io(e))?;
113
114 let persona = self.parse_persona(persona_id, &content)?;
116
117 self.cache.insert(persona_id.to_string(), persona.clone());
119
120 Ok(persona)
121 }
122
123 fn parse_persona(&self, id: &str, content: &str) -> Result<GrimoirePersona> {
125 Ok(GrimoirePersona {
127 id: id.to_string(),
128 name: id.to_string(),
129 system_prompt: content.to_string(),
130 variants: HashMap::new(),
131 metadata: HashMap::new(),
132 })
133 }
134
135 pub async fn list(&self) -> Result<Vec<String>> {
141 let mut personas = Vec::new();
142
143 if !self.base_path.exists() {
144 return Ok(personas);
145 }
146
147 let mut entries = tokio::fs::read_dir(&self.base_path).await?;
148 while let Some(entry) = entries.next_entry().await? {
149 let path = entry.path();
150 if path.is_dir() || path.extension().map_or(false, |e| e == "md") {
151 if let Some(name) = path.file_stem().and_then(|s| s.to_str()) {
152 personas.push(name.to_string());
153 }
154 }
155 }
156
157 Ok(personas)
158 }
159
160 pub fn clear_cache(&self) {
162 self.cache.clear();
163 }
164
165 #[must_use]
167 pub fn base_path(&self) -> &Path {
168 &self.base_path
169 }
170}
171
172impl Default for GrimoireLoader {
173 fn default() -> Self {
174 Self::new()
175 }
176}