use serde::{Deserialize, Serialize};
use tea_coding_tools::{
DEFAULT_FETCH_CACHE_ENTRIES, DEFAULT_FETCH_CACHE_ENTRY_BYTES, DEFAULT_FETCH_CACHE_TOTAL_BYTES,
DEFAULT_FETCH_CACHE_TTL, DEFAULT_TAVILY_SEARCH_ENDPOINT,
};
use tea_mcp::McpServerConfig;
use tea_model::{WebSearchLocation, WebSearchOptions};
use crate::mcp_config::McpServerSettings;
pub const CODING_SETTINGS_SCHEMA_VERSION: u32 = 1;
pub const DEFAULT_WEB_SEARCH_API_KEY_ENVIRONMENT: &str = "TAVILY_API_KEY";
pub const DEFAULT_WEB_SEARCH_TIMEOUT_MILLIS: u64 = 30_000;
pub const MAX_WEB_SEARCH_API_KEY_ENVIRONMENT_BYTES: usize = 128;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProjectTrustDefault {
#[default]
Ask,
Ignore,
Reject,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CodingSettings {
pub schema_version: u32,
pub provider: String,
pub model: String,
pub thinking: String,
pub active_tools: Vec<String>,
pub session_database: Option<String>,
pub max_retries: u32,
pub retry_base_delay_ms: u64,
pub retry_max_delay_ms: u64,
pub compaction_enabled: bool,
pub tui: TuiSettings,
pub resources: ResourceSettings,
#[serde(default)]
pub web_search: WebSearchSettings,
#[serde(default)]
pub web_fetch: WebFetchSettings,
pub project_trust: ProjectTrustDefault,
#[serde(skip, default)]
pub mcp_servers: Vec<McpServerConfig>,
}
impl Default for CodingSettings {
fn default() -> Self {
Self {
schema_version: CODING_SETTINGS_SCHEMA_VERSION,
provider: "openai".to_owned(),
model: "gpt-5.4".to_owned(),
thinking: "medium".to_owned(),
active_tools: vec!["read", "write", "edit", "bash"]
.into_iter()
.map(str::to_owned)
.collect(),
session_database: None,
max_retries: 3,
retry_base_delay_ms: 2_000,
retry_max_delay_ms: 60_000,
compaction_enabled: false,
tui: TuiSettings::default(),
resources: ResourceSettings::default(),
web_search: WebSearchSettings::default(),
web_fetch: WebFetchSettings::default(),
project_trust: ProjectTrustDefault::Ask,
mcp_servers: Vec::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum WebSearchRoutePreference {
#[default]
PreferHosted,
ForceClient,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum WebSearchClientBackend {
#[default]
Tavily,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct WebSearchSettings {
pub route_preference: WebSearchRoutePreference,
pub allowed_domains: Vec<String>,
pub blocked_domains: Vec<String>,
pub location: Option<WebSearchLocationSettings>,
pub client: ClientWebSearchSettings,
}
impl Default for WebSearchSettings {
fn default() -> Self {
Self {
route_preference: WebSearchRoutePreference::PreferHosted,
allowed_domains: Vec::new(),
blocked_domains: Vec::new(),
location: None,
client: ClientWebSearchSettings::default(),
}
}
}
impl WebSearchSettings {
pub(crate) fn runtime_options(&self) -> Result<WebSearchOptions, tea_model::ModelRequestError> {
let mut options = WebSearchOptions::new();
if !self.allowed_domains.is_empty() {
options = options.with_allowed_domains(self.allowed_domains.clone())?;
}
if !self.blocked_domains.is_empty() {
options = options.with_blocked_domains(self.blocked_domains.clone())?;
}
if let Some(configured) = &self.location {
let mut location = WebSearchLocation::new();
if let Some(country) = &configured.country {
location = location.with_country(country.clone())?;
}
if let Some(city) = &configured.city {
location = location.with_city(city.clone())?;
}
if let Some(region) = &configured.region {
location = location.with_region(region.clone())?;
}
if let Some(timezone) = &configured.timezone {
location = location.with_timezone(timezone.clone())?;
}
options = options.with_location(location);
}
Ok(options)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct WebSearchLocationSettings {
pub country: Option<String>,
pub city: Option<String>,
pub region: Option<String>,
pub timezone: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ClientWebSearchSettings {
pub enabled: bool,
pub backend: WebSearchClientBackend,
pub endpoint: String,
pub api_key_environment: String,
pub timeout_millis: u64,
}
impl Default for ClientWebSearchSettings {
fn default() -> Self {
Self {
enabled: false,
backend: WebSearchClientBackend::Tavily,
endpoint: DEFAULT_TAVILY_SEARCH_ENDPOINT.to_owned(),
api_key_environment: DEFAULT_WEB_SEARCH_API_KEY_ENVIRONMENT.to_owned(),
timeout_millis: DEFAULT_WEB_SEARCH_TIMEOUT_MILLIS,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum WebFetchBackend {
#[default]
Http,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct WebFetchSettings {
pub enabled: bool,
pub backend: WebFetchBackend,
pub cache: WebFetchCacheSettings,
}
impl Default for WebFetchSettings {
fn default() -> Self {
Self {
enabled: false,
backend: WebFetchBackend::Http,
cache: WebFetchCacheSettings::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct WebFetchCacheSettings {
pub ttl_seconds: u64,
pub max_entries: usize,
pub max_total_bytes: usize,
pub max_entry_bytes: usize,
}
impl Default for WebFetchCacheSettings {
fn default() -> Self {
Self {
ttl_seconds: DEFAULT_FETCH_CACHE_TTL.as_secs(),
max_entries: DEFAULT_FETCH_CACHE_ENTRIES,
max_total_bytes: DEFAULT_FETCH_CACHE_TOTAL_BYTES,
max_entry_bytes: DEFAULT_FETCH_CACHE_ENTRY_BYTES,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct TuiSettings {
pub viewport: String,
pub collapse_thinking: bool,
pub reduced_motion: bool,
pub submit_key: String,
pub newline_key: String,
pub abort_key: String,
pub clear_key: String,
pub exit_key: String,
pub model_key: String,
pub toggle_thinking_key: String,
pub toggle_tools_key: String,
pub copy_key: String,
pub steering_key: String,
pub follow_up_key: String,
pub retrieve_queued_key: String,
}
impl Default for TuiSettings {
fn default() -> Self {
Self {
viewport: "inline".to_owned(),
collapse_thinking: false,
reduced_motion: false,
submit_key: "enter".to_owned(),
newline_key: "shift+enter".to_owned(),
abort_key: "escape".to_owned(),
clear_key: "ctrl+l".to_owned(),
exit_key: "ctrl+d".to_owned(),
model_key: "ctrl+o".to_owned(),
toggle_thinking_key: "ctrl+t".to_owned(),
toggle_tools_key: "ctrl+g".to_owned(),
copy_key: "ctrl+y".to_owned(),
steering_key: "enter".to_owned(),
follow_up_key: "alt+enter".to_owned(),
retrieve_queued_key: "ctrl+r".to_owned(),
}
}
}
#[cfg(test)]
mod tests {
use super::TuiSettings;
#[test]
fn tui_defaults_to_codex_style_inline_viewport() {
assert_eq!(TuiSettings::default().viewport, "inline");
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ResourceSettings {
pub context_files: bool,
pub skill_paths: Vec<String>,
pub prompt_templates: bool,
}
impl Default for ResourceSettings {
fn default() -> Self {
Self {
context_files: true,
skill_paths: Vec::new(),
prompt_templates: true,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SettingsLayer {
pub schema_version: Option<u32>,
pub provider: Option<String>,
pub model: Option<String>,
pub thinking: Option<String>,
pub active_tools: Option<Vec<String>>,
pub session_database: Option<String>,
pub max_retries: Option<u32>,
pub retry_base_delay_ms: Option<u64>,
pub retry_max_delay_ms: Option<u64>,
pub compaction_enabled: Option<bool>,
pub tui: Option<TuiSettingsLayer>,
pub resources: Option<ResourceSettingsLayer>,
pub web_search: Option<WebSearchSettingsLayer>,
pub web_fetch: Option<WebFetchSettingsLayer>,
pub project_trust: Option<ProjectTrustDefault>,
pub mcp_servers: Option<Vec<McpServerSettings>>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct WebSearchSettingsLayer {
pub route_preference: Option<WebSearchRoutePreference>,
pub allowed_domains: Option<Vec<String>>,
pub blocked_domains: Option<Vec<String>>,
pub location: Option<WebSearchLocationSettingsLayer>,
pub client: Option<ClientWebSearchSettingsLayer>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct WebSearchLocationSettingsLayer {
pub country: Option<String>,
pub city: Option<String>,
pub region: Option<String>,
pub timezone: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ClientWebSearchSettingsLayer {
pub enabled: Option<bool>,
pub backend: Option<WebSearchClientBackend>,
pub endpoint: Option<String>,
pub api_key_environment: Option<String>,
pub timeout_millis: Option<u64>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct WebFetchSettingsLayer {
pub enabled: Option<bool>,
pub backend: Option<WebFetchBackend>,
pub cache: Option<WebFetchCacheSettingsLayer>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct WebFetchCacheSettingsLayer {
pub ttl_seconds: Option<u64>,
pub max_entries: Option<usize>,
pub max_total_bytes: Option<usize>,
pub max_entry_bytes: Option<usize>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TuiSettingsLayer {
pub viewport: Option<String>,
pub collapse_thinking: Option<bool>,
pub reduced_motion: Option<bool>,
pub submit_key: Option<String>,
pub newline_key: Option<String>,
pub abort_key: Option<String>,
pub clear_key: Option<String>,
pub exit_key: Option<String>,
pub model_key: Option<String>,
pub toggle_thinking_key: Option<String>,
pub toggle_tools_key: Option<String>,
pub copy_key: Option<String>,
pub steering_key: Option<String>,
pub follow_up_key: Option<String>,
pub retrieve_queued_key: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ResourceSettingsLayer {
pub context_files: Option<bool>,
pub skill_paths: Option<Vec<String>>,
pub prompt_templates: Option<bool>,
}