use std::path::PathBuf;
use std::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use crate::mode::AgentMode;
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ProviderKind {
Ollama,
LlamaCpp,
Vllm,
}
impl fmt::Display for ProviderKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProviderKind::Ollama => f.write_str("ollama"),
ProviderKind::LlamaCpp => f.write_str("llama.cpp"),
ProviderKind::Vllm => f.write_str("vllm"),
}
}
}
impl FromStr for ProviderKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_lowercase().as_str() {
"ollama" => Ok(ProviderKind::Ollama),
"llama.cpp" | "llamacpp" | "llama_cpp" => Ok(ProviderKind::LlamaCpp),
"vllm" => Ok(ProviderKind::Vllm),
_ => Err(format!("Unknown provider '{}'", s)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum OllamaThinkType {
Off,
Low,
Medium,
High,
}
impl fmt::Display for OllamaThinkType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OllamaThinkType::Off => f.write_str("off"),
OllamaThinkType::Low => f.write_str("low"),
OllamaThinkType::Medium => f.write_str("medium"),
OllamaThinkType::High => f.write_str("high"),
}
}
}
impl FromStr for OllamaThinkType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_lowercase().as_str() {
"off" | "false" | "0" => Ok(OllamaThinkType::Off),
"low" => Ok(OllamaThinkType::Low),
"medium" | "mid" => Ok(OllamaThinkType::Medium),
"high" => Ok(OllamaThinkType::High),
_ => Err(format!(
"Unknown think type '{}'. Valid values: off, low, medium, high",
s
)),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub last_provider: ProviderKind,
pub last_model: Option<String>,
pub preferred_mode: AgentMode,
pub ollama_api_key: Option<String>,
pub ollama_timeout_secs: u64,
pub ollama_max_retries: u32,
pub ollama_think_type: OllamaThinkType,
pub show_thinking: bool,
pub context_limit: Option<u32>,
pub auto_accept_safe_commands: bool,
pub safe_command_prefixes: Option<Vec<String>>,
pub denied_command_prefixes: Option<Vec<String>>,
}
impl Default for Settings {
fn default() -> Self {
Settings {
last_provider: ProviderKind::Ollama,
last_model: None,
preferred_mode: AgentMode::Casual,
ollama_api_key: None,
ollama_timeout_secs: 5,
ollama_max_retries: 3,
ollama_think_type: OllamaThinkType::Medium,
show_thinking: false,
context_limit: None,
auto_accept_safe_commands: true,
safe_command_prefixes: None,
denied_command_prefixes: None,
}
}
}
pub fn get_default_safe_commands() -> Vec<String> {
vec![
"cd".to_string(),
"ls".to_string(),
"grep".to_string(),
"glob".to_string(),
"find".to_string(),
"cat".to_string(),
"head".to_string(),
"tail".to_string(),
"wc".to_string(),
"pwd".to_string(),
"echo".to_string(),
"printf".to_string(),
"tree".to_string(),
"file".to_string(),
"stat".to_string(),
"du".to_string(),
"df".to_string(),
"free".to_string(),
"uptime".to_string(),
"whoami".to_string(),
"hostname".to_string(),
"uname".to_string(),
"date".to_string(),
"cal".to_string(),
"ps".to_string(),
"env".to_string(),
"printenv".to_string(),
"which".to_string(),
"whereis".to_string(),
"type".to_string(),
"git status".to_string(),
"git diff".to_string(),
"git log".to_string(),
"git show".to_string(),
"git branch".to_string(),
"git remote".to_string(),
"git tag".to_string(),
"git describe".to_string(),
"git rev-parse".to_string(),
"cargo tree".to_string(),
"cargo metadata".to_string(),
"cargo doc".to_string(),
"rustc --version".to_string(),
"cargo --version".to_string(),
]
}
impl Settings {
pub fn get_safe_commands(&self) -> Vec<String> {
self.safe_command_prefixes
.clone()
.unwrap_or_else(get_default_safe_commands)
}
pub fn get_denied_commands(&self) -> Vec<String> {
self.denied_command_prefixes.clone().unwrap_or_default()
}
}
#[derive(Debug)]
pub enum SettingsError {
Io(std::io::Error),
Parse(serde_json::Error),
}
impl std::fmt::Display for SettingsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SettingsError::Io(e) => write!(f, "I/O error: {}", e),
SettingsError::Parse(e) => write!(f, "parse error: {}", e),
}
}
}
impl std::error::Error for SettingsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
SettingsError::Io(e) => Some(e),
SettingsError::Parse(e) => Some(e),
}
}
}
impl From<std::io::Error> for SettingsError {
fn from(e: std::io::Error) -> Self {
SettingsError::Io(e)
}
}
impl From<serde_json::Error> for SettingsError {
fn from(e: serde_json::Error) -> Self {
SettingsError::Parse(e)
}
}
pub struct SettingsStore {
path: std::path::PathBuf,
}
impl SettingsStore {
pub fn default_path() -> Self {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
let path = std::path::PathBuf::from(home).join(".config/tinyharness/settings.json");
SettingsStore { path }
}
pub fn new(path: std::path::PathBuf) -> Self {
SettingsStore { path }
}
pub fn load(&self) -> Result<Settings, SettingsError> {
if !self.path.exists() {
return Ok(Settings::default());
}
let content = std::fs::read_to_string(&self.path)?;
let settings = serde_json::from_str(&content)?;
Ok(settings)
}
pub fn load_or_default(&self) -> Settings {
self.load().unwrap_or_else(|e| {
eprintln!("Warning: Failed to load settings: {}. Using defaults.", e);
Settings::default()
})
}
pub fn save(&self, settings: &Settings) -> Result<(), SettingsError> {
let dir = self.path.parent().ok_or_else(|| {
SettingsError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"settings path has no parent directory",
))
})?;
std::fs::create_dir_all(dir)?;
let json = serde_json::to_string_pretty(settings)?;
let tmp_path = dir.join("settings.json.tmp");
{
let mut file = std::fs::File::create(&tmp_path)?;
std::io::Write::write_all(&mut file, json.as_bytes())?;
std::io::Write::flush(&mut file)?;
}
std::fs::rename(&tmp_path, &self.path)?;
Ok(())
}
pub fn path(&self) -> &std::path::Path {
&self.path
}
}
pub fn load_settings() -> Settings {
SettingsStore::default_path().load_or_default()
}
pub fn save_settings(settings: &Settings) {
let store = SettingsStore::default_path();
if let Err(e) = store.save(settings) {
eprintln!("Warning: Failed to save settings: {}", e);
}
}
pub fn prompts_dir() -> PathBuf {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
PathBuf::from(home)
.join(".config")
.join("tinyharness")
.join("prompts")
}
pub fn ensure_prompts_initialized() -> PathBuf {
let dir = prompts_dir();
if !dir.exists() {
std::fs::create_dir_all(&dir).ok();
}
let modes = [
AgentMode::Casual,
AgentMode::Planning,
AgentMode::Agent,
AgentMode::Research,
];
for mode in &modes {
let file_path = dir.join(mode.prompts_filename());
if !file_path.exists() {
let default_text = mode.default_system_prompt();
if let Err(e) = std::fs::write(&file_path, default_text.trim()) {
eprintln!(
"Warning: Failed to write default prompt to {}: {}",
file_path.display(),
e
);
}
}
}
dir
}