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