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, Eq, Default, Serialize, Deserialize)]
8pub enum AgentMode {
9    #[default]
10    Casual,
11    Planning,
12    Agent,
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        }
23    }
24
25    /// Returns whether this mode uses the shared developer header.
26    /// Casual mode is self-contained; all other modes use header + mode section.
27    pub fn uses_header(&self) -> bool {
28        !matches!(self, AgentMode::Casual)
29    }
30
31    /// Load the shared header prompt from a `.md` file in `prompts_dir`.
32    /// Falls back to the hardcoded default if the file cannot be read.
33    fn load_header(prompts_dir: &Path) -> String {
34        let file_path = prompts_dir.join("header.md");
35        match std::fs::read_to_string(&file_path) {
36            Ok(content) => {
37                let trimmed = content.trim();
38                if trimmed.is_empty() {
39                    include_str!("prompts/header.md").trim().to_string()
40                } else {
41                    trimmed.to_string()
42                }
43            }
44            Err(_) => include_str!("prompts/header.md").trim().to_string(),
45        }
46    }
47
48    /// Load the system prompt for this mode from `.md` files in `prompts_dir`.
49    ///
50    /// For Agent and Planning modes, the prompt is assembled as:
51    ///   `header.md` + blank line + `<mode>.md`
52    ///
53    /// Casual mode uses only its own file (self-contained).
54    ///
55    /// Falls back to hardcoded defaults if files cannot be read.
56    pub fn load_system_prompt(&self, prompts_dir: &Path) -> String {
57        let mode_path = prompts_dir.join(self.prompts_filename());
58        let mode_content = match std::fs::read_to_string(&mode_path) {
59            Ok(content) => {
60                let trimmed = content.trim();
61                if trimmed.is_empty() {
62                    self.default_system_prompt().to_string()
63                } else {
64                    trimmed.to_string()
65                }
66            }
67            Err(_) => self.default_system_prompt().to_string(),
68        };
69
70        if self.uses_header() {
71            let header = Self::load_header(prompts_dir);
72            format!("{}\n\n{}", header, mode_content)
73        } else {
74            mode_content
75        }
76    }
77
78    /// Returns the hardcoded default system prompt (embedded at compile time
79    /// via `include_str!`). Used as fallback when no custom prompt file exists
80    /// and as the seed for first-launch initialization.
81    ///
82    /// For Agent and Planning modes, this returns only the mode-specific
83    /// section (the header is handled separately).
84    pub fn default_system_prompt(&self) -> &'static str {
85        match self {
86            AgentMode::Casual => include_str!("prompts/casual.md"),
87            AgentMode::Planning => include_str!("prompts/planning.md"),
88            AgentMode::Agent => include_str!("prompts/agent.md"),
89        }
90    }
91}
92
93impl fmt::Display for AgentMode {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        match self {
96            AgentMode::Casual => f.write_str("casual"),
97            AgentMode::Planning => f.write_str("planning"),
98            AgentMode::Agent => f.write_str("agent"),
99        }
100    }
101}
102
103impl FromStr for AgentMode {
104    type Err = String;
105
106    fn from_str(s: &str) -> Result<Self, Self::Err> {
107        match s.trim().to_lowercase().as_str() {
108            "casual" => Ok(AgentMode::Casual),
109            "planning" | "plan" | "research" | "researching" => Ok(AgentMode::Planning),
110            "agent" | "dev" => Ok(AgentMode::Agent),
111            _ => Err(format!(
112                "Unknown mode '{}'. Valid modes: casual, planning, agent",
113                s
114            )),
115        }
116    }
117}