use std::fmt;
use std::path::Path;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum AgentMode {
#[default]
Casual,
Planning,
Agent,
}
impl AgentMode {
pub fn prompts_filename(&self) -> &'static str {
match self {
AgentMode::Casual => "casual.md",
AgentMode::Planning => "planning.md",
AgentMode::Agent => "agent.md",
}
}
pub fn uses_header(&self) -> bool {
!matches!(self, AgentMode::Casual)
}
fn load_header(prompts_dir: &Path) -> String {
let file_path = prompts_dir.join("header.md");
match std::fs::read_to_string(&file_path) {
Ok(content) => {
let trimmed = content.trim();
if trimmed.is_empty() {
include_str!("prompts/header.md").trim().to_string()
} else {
trimmed.to_string()
}
}
Err(_) => include_str!("prompts/header.md").trim().to_string(),
}
}
pub fn load_system_prompt(&self, prompts_dir: &Path) -> String {
let mode_path = prompts_dir.join(self.prompts_filename());
let mode_content = match std::fs::read_to_string(&mode_path) {
Ok(content) => {
let trimmed = content.trim();
if trimmed.is_empty() {
self.default_system_prompt().to_string()
} else {
trimmed.to_string()
}
}
Err(_) => self.default_system_prompt().to_string(),
};
if self.uses_header() {
let header = Self::load_header(prompts_dir);
format!("{}\n\n{}", header, mode_content)
} else {
mode_content
}
}
pub fn default_system_prompt(&self) -> &'static str {
match self {
AgentMode::Casual => include_str!("prompts/casual.md"),
AgentMode::Planning => include_str!("prompts/planning.md"),
AgentMode::Agent => include_str!("prompts/agent.md"),
}
}
}
impl fmt::Display for AgentMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AgentMode::Casual => f.write_str("casual"),
AgentMode::Planning => f.write_str("planning"),
AgentMode::Agent => f.write_str("agent"),
}
}
}
impl FromStr for AgentMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_lowercase().as_str() {
"casual" => Ok(AgentMode::Casual),
"planning" | "plan" | "research" | "researching" => Ok(AgentMode::Planning),
"agent" | "dev" => Ok(AgentMode::Agent),
_ => Err(format!(
"Unknown mode '{}'. Valid modes: casual, planning, agent",
s
)),
}
}
}