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