Skip to main content

tinyharness_lib/
mode.rs

1use std::fmt;
2use std::path::Path;
3use std::str::FromStr;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
8pub enum AgentMode {
9    Casual,
10    Planning,
11    Agent,
12    Research,
13}
14
15impl AgentMode {
16    /// Returns the filename for this mode's prompt file (e.g. "casual.md").
17    pub fn prompts_filename(&self) -> &'static str {
18        match self {
19            AgentMode::Casual => "casual.md",
20            AgentMode::Planning => "planning.md",
21            AgentMode::Agent => "agent.md",
22            AgentMode::Research => "research.md",
23        }
24    }
25
26    /// Load the system prompt for this mode from a `.md` file in `prompts_dir`.
27    /// Falls back to the hardcoded default if the file cannot be read.
28    pub fn load_system_prompt(&self, prompts_dir: &Path) -> String {
29        let file_path = prompts_dir.join(self.prompts_filename());
30        match std::fs::read_to_string(&file_path) {
31            Ok(content) => {
32                let trimmed = content.trim();
33                if trimmed.is_empty() {
34                    self.default_system_prompt().to_string()
35                } else {
36                    trimmed.to_string()
37                }
38            }
39            Err(_) => self.default_system_prompt().to_string(),
40        }
41    }
42
43    /// Returns the hardcoded default system prompt (embedded at compile time
44    /// via `include_str!`). Used as fallback when no custom prompt file exists
45    /// and as the seed for first-launch initialization.
46    pub fn default_system_prompt(&self) -> &'static str {
47        match self {
48            AgentMode::Casual => include_str!("prompts/casual.md"),
49            AgentMode::Planning => include_str!("prompts/planning.md"),
50            AgentMode::Agent => include_str!("prompts/agent.md"),
51            AgentMode::Research => include_str!("prompts/research.md"),
52        }
53    }
54}
55
56impl fmt::Display for AgentMode {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            AgentMode::Casual => f.write_str("casual"),
60            AgentMode::Planning => f.write_str("planning"),
61            AgentMode::Agent => f.write_str("agent"),
62            AgentMode::Research => f.write_str("research"),
63        }
64    }
65}
66
67impl FromStr for AgentMode {
68    type Err = String;
69
70    fn from_str(s: &str) -> Result<Self, Self::Err> {
71        match s.trim().to_lowercase().as_str() {
72            "casual" => Ok(AgentMode::Casual),
73            "planning" | "plan" => Ok(AgentMode::Planning),
74            "agent" | "dev" => Ok(AgentMode::Agent),
75            "research" | "researching" => Ok(AgentMode::Research),
76            _ => Err(format!(
77                "Unknown mode '{}'. Valid modes: casual, planning, agent, research",
78                s
79            )),
80        }
81    }
82}