Skip to main content

vtcode_core/prompts/
output_styles.rs

1use std::path::Path;
2use std::sync::Arc;
3use tokio::sync::RwLock;
4use vtcode_config::{OutputStyleManager, VTCodeConfig};
5
6pub struct OutputStyleApplier {
7    manager: Arc<RwLock<OutputStyleManager>>,
8}
9
10impl OutputStyleApplier {
11    pub fn new() -> Self {
12        Self {
13            manager: Arc::new(RwLock::new(OutputStyleManager::new())),
14        }
15    }
16
17    pub async fn load_styles_from_config(&self, _config: &VTCodeConfig, workspace: &Path) -> anyhow::Result<()> {
18        let output_styles_dir = workspace.join(".vtcode").join("output-styles");
19        let manager = OutputStyleManager::load_from_directory(&output_styles_dir)?;
20
21        {
22            let mut guard = self.manager.write().await;
23            *guard = manager;
24        }
25
26        Ok(())
27    }
28
29    pub async fn apply_style(&self, style_name: &str, base_prompt: &str, config: &VTCodeConfig) -> String {
30        let guard = self.manager.read().await;
31
32        if let Some(style) = guard.get_style(style_name) {
33            if style.config.keep_coding_instructions {
34                // Combine base prompt with style content
35                format!("{}\n\n{}", base_prompt, style.content)
36            } else {
37                // Replace base prompt with style content
38                style.content.clone()
39            }
40        } else {
41            // If the requested style doesn't exist, use the active style from config
42            if let Some(active_style) = guard.get_style(&config.output_style.active_style) {
43                if active_style.config.keep_coding_instructions {
44                    format!("{}\n\n{}", base_prompt, active_style.content)
45                } else {
46                    active_style.content.clone()
47                }
48            } else {
49                // Fallback to base prompt if no style is found
50                base_prompt.to_string()
51            }
52        }
53    }
54
55    pub async fn get_available_styles(&self) -> Vec<String> {
56        let guard = self.manager.read().await;
57        guard.list_styles().into_iter().map(|(name, _)| name.to_string()).collect()
58    }
59}
60
61impl Default for OutputStyleApplier {
62    fn default() -> Self {
63        Self::new()
64    }
65}