|
| 1 | +use serde::Serialize; |
| 2 | +use std::env; |
| 3 | +use std::fs; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | +use tokio::task; |
| 6 | + |
| 7 | +#[derive(Serialize, Clone)] |
| 8 | +pub(crate) struct CustomPromptEntry { |
| 9 | + pub(crate) name: String, |
| 10 | + pub(crate) path: String, |
| 11 | + pub(crate) description: Option<String>, |
| 12 | + #[serde(rename = "argumentHint")] |
| 13 | + pub(crate) argument_hint: Option<String>, |
| 14 | + pub(crate) content: String, |
| 15 | +} |
| 16 | + |
| 17 | +fn resolve_home_dir() -> Option<PathBuf> { |
| 18 | + if let Ok(value) = env::var("HOME") { |
| 19 | + if !value.trim().is_empty() { |
| 20 | + return Some(PathBuf::from(value)); |
| 21 | + } |
| 22 | + } |
| 23 | + if let Ok(value) = env::var("USERPROFILE") { |
| 24 | + if !value.trim().is_empty() { |
| 25 | + return Some(PathBuf::from(value)); |
| 26 | + } |
| 27 | + } |
| 28 | + None |
| 29 | +} |
| 30 | + |
| 31 | +fn resolve_codex_home() -> Option<PathBuf> { |
| 32 | + if let Ok(value) = env::var("CODEX_HOME") { |
| 33 | + if !value.trim().is_empty() { |
| 34 | + let path = PathBuf::from(value.trim()); |
| 35 | + if path.exists() { |
| 36 | + return path.canonicalize().ok().or(Some(path)); |
| 37 | + } |
| 38 | + return None; |
| 39 | + } |
| 40 | + } |
| 41 | + resolve_home_dir().map(|home| home.join(".codex")) |
| 42 | +} |
| 43 | + |
| 44 | +fn default_prompts_dir() -> Option<PathBuf> { |
| 45 | + resolve_codex_home().map(|home| home.join("prompts")) |
| 46 | +} |
| 47 | + |
| 48 | +fn parse_frontmatter(content: &str) -> (Option<String>, Option<String>, String) { |
| 49 | + let mut segments = content.split_inclusive('\n'); |
| 50 | + let Some(first_segment) = segments.next() else { |
| 51 | + return (None, None, String::new()); |
| 52 | + }; |
| 53 | + let first_line = first_segment.trim_end_matches(['\r', '\n']); |
| 54 | + if first_line.trim() != "---" { |
| 55 | + return (None, None, content.to_string()); |
| 56 | + } |
| 57 | + |
| 58 | + let mut description: Option<String> = None; |
| 59 | + let mut argument_hint: Option<String> = None; |
| 60 | + let mut frontmatter_closed = false; |
| 61 | + let mut consumed = first_segment.len(); |
| 62 | + |
| 63 | + for segment in segments { |
| 64 | + let line = segment.trim_end_matches(['\r', '\n']); |
| 65 | + let trimmed = line.trim(); |
| 66 | + |
| 67 | + if trimmed == "---" { |
| 68 | + frontmatter_closed = true; |
| 69 | + consumed += segment.len(); |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + if trimmed.is_empty() || trimmed.starts_with('#') { |
| 74 | + consumed += segment.len(); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if let Some((key, value)) = trimmed.split_once(':') { |
| 79 | + let mut val = value.trim().to_string(); |
| 80 | + if val.len() >= 2 { |
| 81 | + let bytes = val.as_bytes(); |
| 82 | + let first = bytes[0]; |
| 83 | + let last = bytes[bytes.len() - 1]; |
| 84 | + if (first == b'"' && last == b'"') || (first == b'\'' && last == b'\'') { |
| 85 | + val = val[1..val.len().saturating_sub(1)].to_string(); |
| 86 | + } |
| 87 | + } |
| 88 | + match key.trim().to_ascii_lowercase().as_str() { |
| 89 | + "description" => description = Some(val), |
| 90 | + "argument-hint" | "argument_hint" => argument_hint = Some(val), |
| 91 | + _ => {} |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + consumed += segment.len(); |
| 96 | + } |
| 97 | + |
| 98 | + if !frontmatter_closed { |
| 99 | + return (None, None, content.to_string()); |
| 100 | + } |
| 101 | + |
| 102 | + let body = if consumed >= content.len() { |
| 103 | + String::new() |
| 104 | + } else { |
| 105 | + content[consumed..].to_string() |
| 106 | + }; |
| 107 | + (description, argument_hint, body) |
| 108 | +} |
| 109 | + |
| 110 | +fn discover_prompts_in(dir: &Path) -> Vec<CustomPromptEntry> { |
| 111 | + let mut out: Vec<CustomPromptEntry> = Vec::new(); |
| 112 | + let entries = match fs::read_dir(dir) { |
| 113 | + Ok(entries) => entries, |
| 114 | + Err(_) => return out, |
| 115 | + }; |
| 116 | + |
| 117 | + for entry in entries.flatten() { |
| 118 | + let path = entry.path(); |
| 119 | + let is_file = fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false); |
| 120 | + if !is_file { |
| 121 | + continue; |
| 122 | + } |
| 123 | + let is_md = path |
| 124 | + .extension() |
| 125 | + .and_then(|s| s.to_str()) |
| 126 | + .map(|ext| ext.eq_ignore_ascii_case("md")) |
| 127 | + .unwrap_or(false); |
| 128 | + if !is_md { |
| 129 | + continue; |
| 130 | + } |
| 131 | + let Some(name) = path |
| 132 | + .file_stem() |
| 133 | + .and_then(|s| s.to_str()) |
| 134 | + .map(str::to_string) |
| 135 | + else { |
| 136 | + continue; |
| 137 | + }; |
| 138 | + let content = match fs::read_to_string(&path) { |
| 139 | + Ok(content) => content, |
| 140 | + Err(_) => continue, |
| 141 | + }; |
| 142 | + let (description, argument_hint, body) = parse_frontmatter(&content); |
| 143 | + out.push(CustomPromptEntry { |
| 144 | + name, |
| 145 | + path: path.to_string_lossy().to_string(), |
| 146 | + description, |
| 147 | + argument_hint, |
| 148 | + content: body, |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + out.sort_by(|a, b| a.name.cmp(&b.name)); |
| 153 | + out |
| 154 | +} |
| 155 | + |
| 156 | +#[tauri::command] |
| 157 | +pub(crate) async fn prompts_list(_workspace_id: String) -> Result<Vec<CustomPromptEntry>, String> { |
| 158 | + let Some(dir) = default_prompts_dir() else { |
| 159 | + return Ok(Vec::new()); |
| 160 | + }; |
| 161 | + task::spawn_blocking(move || discover_prompts_in(&dir)) |
| 162 | + .await |
| 163 | + .map_err(|_| "prompt discovery failed".to_string()) |
| 164 | +} |
0 commit comments