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 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 pub fn uses_header(&self) -> bool {
28 !matches!(self, AgentMode::Casual)
29 }
30
31 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 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 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}