use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
fn default_true() -> bool {
true
}
#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq)]
pub enum PermissionMode {
#[default]
Developer,
ReadOnly,
SystemAdmin,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct HematiteConfig {
#[serde(default)]
pub mode: PermissionMode,
pub permissions: Option<PermissionRules>,
#[serde(default)]
pub trust: WorkspaceTrustConfig,
pub model: Option<String>,
pub fast_model: Option<String>,
pub think_model: Option<String>,
#[serde(default = "default_true")]
pub gemma_native_auto: bool,
#[serde(default)]
pub gemma_native_formatting: bool,
pub api_url: Option<String>,
pub voice: Option<String>,
pub voice_speed: Option<f32>,
pub voice_volume: Option<f32>,
pub context_hint: Option<String>,
pub deno_path: Option<String>,
#[serde(default)]
pub verify: VerifyProfilesConfig,
#[serde(default)]
pub hooks: crate::agent::hooks::RuntimeHookConfig,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct WorkspaceTrustConfig {
#[serde(default = "default_trusted_workspace_roots")]
pub allow: Vec<String>,
#[serde(default)]
pub deny: Vec<String>,
}
impl Default for WorkspaceTrustConfig {
fn default() -> Self {
Self {
allow: default_trusted_workspace_roots(),
deny: Vec::new(),
}
}
}
fn default_trusted_workspace_roots() -> Vec<String> {
vec![".".to_string()]
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct VerifyProfilesConfig {
pub default_profile: Option<String>,
#[serde(default)]
pub profiles: BTreeMap<String, VerifyProfile>,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct VerifyProfile {
pub build: Option<String>,
pub test: Option<String>,
pub lint: Option<String>,
pub fix: Option<String>,
pub timeout_secs: Option<u64>,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct PermissionRules {
#[serde(default)]
pub allow: Vec<String>,
#[serde(default)]
pub ask: Vec<String>,
#[serde(default)]
pub deny: Vec<String>,
}
pub fn settings_path() -> std::path::PathBuf {
crate::tools::file_ops::workspace_root()
.join(".hematite")
.join("settings.json")
}
fn load_global_config() -> Option<HematiteConfig> {
let home = std::env::var_os("USERPROFILE").or_else(|| std::env::var_os("HOME"))?;
let path = std::path::PathBuf::from(home)
.join(".hematite")
.join("settings.json");
let data = std::fs::read_to_string(&path).ok()?;
serde_json::from_str(&data).ok()
}
pub fn load_config() -> HematiteConfig {
let path = settings_path();
let workspace: Option<HematiteConfig> = if path.exists() {
std::fs::read_to_string(&path)
.ok()
.and_then(|d| serde_json::from_str(&d).ok())
} else {
write_default_config(&path);
None
};
let global = load_global_config();
match (workspace, global) {
(Some(ws), Some(gb)) => {
HematiteConfig {
model: ws.model.or(gb.model),
fast_model: ws.fast_model.or(gb.fast_model),
think_model: ws.think_model.or(gb.think_model),
api_url: ws.api_url.or(gb.api_url),
voice: if ws.voice != HematiteConfig::default().voice {
ws.voice
} else {
gb.voice
},
voice_speed: ws.voice_speed.or(gb.voice_speed),
voice_volume: ws.voice_volume.or(gb.voice_volume),
context_hint: ws.context_hint.or(gb.context_hint),
gemma_native_auto: ws.gemma_native_auto,
gemma_native_formatting: ws.gemma_native_formatting,
..ws
}
}
(Some(ws), None) => ws,
(None, Some(gb)) => gb,
(None, None) => HematiteConfig::default(),
}
}
pub fn save_config(config: &HematiteConfig) -> Result<(), String> {
let path = settings_path();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
let json = serde_json::to_string_pretty(config).map_err(|e| e.to_string())?;
std::fs::write(&path, json).map_err(|e| e.to_string())
}
pub fn set_gemma_native_formatting(enabled: bool) -> Result<(), String> {
set_gemma_native_mode(if enabled { "on" } else { "off" })
}
pub fn set_gemma_native_mode(mode: &str) -> Result<(), String> {
let mut config = load_config();
match mode {
"on" => {
config.gemma_native_auto = false;
config.gemma_native_formatting = true;
}
"off" => {
config.gemma_native_auto = false;
config.gemma_native_formatting = false;
}
"auto" => {
config.gemma_native_auto = true;
config.gemma_native_formatting = false;
}
_ => return Err(format!("Unknown gemma native mode: {}", mode)),
}
save_config(&config)
}
pub fn set_voice(voice_id: &str) -> Result<(), String> {
let mut config = load_config();
config.voice = Some(voice_id.to_string());
save_config(&config)
}
pub fn effective_voice(config: &HematiteConfig) -> String {
config.voice.clone().unwrap_or_else(|| "af_sky".to_string())
}
pub fn effective_voice_speed(config: &HematiteConfig) -> f32 {
config.voice_speed.unwrap_or(1.0).clamp(0.5, 2.0)
}
pub fn effective_voice_volume(config: &HematiteConfig) -> f32 {
config.voice_volume.unwrap_or(1.0).clamp(0.0, 3.0)
}
pub fn effective_gemma_native_formatting(config: &HematiteConfig, model_name: &str) -> bool {
crate::agent::inference::is_gemma4_model_name(model_name)
&& (config.gemma_native_formatting || config.gemma_native_auto)
}
pub fn gemma_native_mode_label(config: &HematiteConfig, model_name: &str) -> &'static str {
if !crate::agent::inference::is_gemma4_model_name(model_name) {
"inactive"
} else if config.gemma_native_formatting {
"on"
} else if config.gemma_native_auto {
"auto"
} else {
"off"
}
}
fn write_default_config(path: &std::path::Path) {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let default = r#"{
"_comment": "Hematite settings — edit and save, changes apply immediately without restart.",
"permissions": {
"allow": [
"cargo *",
"git status",
"git log *",
"git diff *",
"git branch *"
],
"ask": [],
"deny": []
},
"trust": {
"allow": ["."],
"deny": []
},
"auto_approve_moderate": false,
"api_url": null,
"voice": null,
"voice_speed": null,
"voice_volume": null,
"context_hint": null,
"model": null,
"fast_model": null,
"think_model": null,
"gemma_native_auto": true,
"gemma_native_formatting": false,
"verify": {
"default_profile": null,
"profiles": {
"rust": {
"build": "cargo build --color never",
"test": "cargo test --color never",
"lint": "cargo clippy --all-targets --all-features -- -D warnings",
"fix": "cargo fmt",
"timeout_secs": 120
}
}
},
"hooks": {
"pre_tool_use": [],
"post_tool_use": []
}
}
"#;
let _ = std::fs::write(path, default);
}
pub fn permission_for_shell(cmd: &str, config: &HematiteConfig) -> PermissionDecision {
if let Some(rules) = &config.permissions {
for pattern in &rules.deny {
if glob_matches(pattern, cmd) {
return PermissionDecision::Deny;
}
}
for pattern in &rules.allow {
if glob_matches(pattern, cmd) {
return PermissionDecision::Allow;
}
}
for pattern in &rules.ask {
if glob_matches(pattern, cmd) {
return PermissionDecision::Ask;
}
}
}
PermissionDecision::UseRiskClassifier
}
#[derive(Debug, PartialEq)]
pub enum PermissionDecision {
Allow,
Deny,
Ask,
UseRiskClassifier,
}
pub fn glob_matches(pattern: &str, text: &str) -> bool {
let p = pattern.to_lowercase();
let t = text.to_lowercase();
if p == "*" {
return true;
}
if let Some(star) = p.find('*') {
let prefix = &p[..star];
let suffix = &p[star + 1..];
t.starts_with(prefix) && (suffix.is_empty() || t.ends_with(suffix))
} else {
t.contains(&p)
}
}