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