1use serde::{Deserialize, Serialize};
2use tea_coding_tools::{
3 DEFAULT_FETCH_CACHE_ENTRIES, DEFAULT_FETCH_CACHE_ENTRY_BYTES, DEFAULT_FETCH_CACHE_TOTAL_BYTES,
4 DEFAULT_FETCH_CACHE_TTL, DEFAULT_TAVILY_SEARCH_ENDPOINT,
5};
6use tea_mcp::McpServerConfig;
7use tea_model::{WebSearchLocation, WebSearchOptions};
8
9use crate::mcp_config::McpServerSettings;
10
11pub const CODING_SETTINGS_SCHEMA_VERSION: u32 = 1;
13pub const DEFAULT_WEB_SEARCH_API_KEY_ENVIRONMENT: &str = "TAVILY_API_KEY";
15pub const DEFAULT_WEB_SEARCH_TIMEOUT_MILLIS: u64 = 30_000;
17pub const MAX_WEB_SEARCH_API_KEY_ENVIRONMENT_BYTES: usize = 128;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
22#[serde(rename_all = "snake_case")]
23pub enum ProjectTrustDefault {
24 #[default]
26 Ask,
27 Ignore,
29 Reject,
31}
32
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase", deny_unknown_fields)]
36pub struct CodingSettings {
37 pub schema_version: u32,
39 pub provider: String,
41 pub model: String,
43 pub thinking: String,
45 pub active_tools: Vec<String>,
47 pub session_database: Option<String>,
49 pub max_retries: u32,
51 pub retry_base_delay_ms: u64,
53 pub retry_max_delay_ms: u64,
55 pub compaction_enabled: bool,
57 pub tui: TuiSettings,
59 pub resources: ResourceSettings,
61 #[serde(default)]
63 pub web_search: WebSearchSettings,
64 #[serde(default)]
66 pub web_fetch: WebFetchSettings,
67 pub project_trust: ProjectTrustDefault,
69 #[serde(skip, default)]
71 pub mcp_servers: Vec<McpServerConfig>,
72}
73
74impl Default for CodingSettings {
75 fn default() -> Self {
76 Self {
77 schema_version: CODING_SETTINGS_SCHEMA_VERSION,
78 provider: "openai".to_owned(),
79 model: "gpt-5.4".to_owned(),
80 thinking: "medium".to_owned(),
81 active_tools: vec!["read", "write", "edit", "bash"]
82 .into_iter()
83 .map(str::to_owned)
84 .collect(),
85 session_database: None,
86 max_retries: 3,
87 retry_base_delay_ms: 2_000,
88 retry_max_delay_ms: 60_000,
89 compaction_enabled: false,
90 tui: TuiSettings::default(),
91 resources: ResourceSettings::default(),
92 web_search: WebSearchSettings::default(),
93 web_fetch: WebFetchSettings::default(),
94 project_trust: ProjectTrustDefault::Ask,
95 mcp_servers: Vec::new(),
96 }
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
102#[serde(rename_all = "snake_case")]
103pub enum WebSearchRoutePreference {
104 #[default]
106 PreferHosted,
107 ForceClient,
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
113#[serde(rename_all = "snake_case")]
114pub enum WebSearchClientBackend {
115 #[default]
117 Tavily,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
123pub struct WebSearchSettings {
124 pub route_preference: WebSearchRoutePreference,
126 pub allowed_domains: Vec<String>,
128 pub blocked_domains: Vec<String>,
130 pub location: Option<WebSearchLocationSettings>,
132 pub client: ClientWebSearchSettings,
134}
135
136impl Default for WebSearchSettings {
137 fn default() -> Self {
138 Self {
139 route_preference: WebSearchRoutePreference::PreferHosted,
140 allowed_domains: Vec::new(),
141 blocked_domains: Vec::new(),
142 location: None,
143 client: ClientWebSearchSettings::default(),
144 }
145 }
146}
147
148impl WebSearchSettings {
149 pub(crate) fn runtime_options(&self) -> Result<WebSearchOptions, tea_model::ModelRequestError> {
150 let mut options = WebSearchOptions::new();
151 if !self.allowed_domains.is_empty() {
152 options = options.with_allowed_domains(self.allowed_domains.clone())?;
153 }
154 if !self.blocked_domains.is_empty() {
155 options = options.with_blocked_domains(self.blocked_domains.clone())?;
156 }
157 if let Some(configured) = &self.location {
158 let mut location = WebSearchLocation::new();
159 if let Some(country) = &configured.country {
160 location = location.with_country(country.clone())?;
161 }
162 if let Some(city) = &configured.city {
163 location = location.with_city(city.clone())?;
164 }
165 if let Some(region) = &configured.region {
166 location = location.with_region(region.clone())?;
167 }
168 if let Some(timezone) = &configured.timezone {
169 location = location.with_timezone(timezone.clone())?;
170 }
171 options = options.with_location(location);
172 }
173 Ok(options)
174 }
175}
176
177#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
179#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
180pub struct WebSearchLocationSettings {
181 pub country: Option<String>,
183 pub city: Option<String>,
185 pub region: Option<String>,
187 pub timezone: Option<String>,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
193#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
194pub struct ClientWebSearchSettings {
195 pub enabled: bool,
197 pub backend: WebSearchClientBackend,
199 pub endpoint: String,
201 pub api_key_environment: String,
203 pub timeout_millis: u64,
205}
206
207impl Default for ClientWebSearchSettings {
208 fn default() -> Self {
209 Self {
210 enabled: false,
211 backend: WebSearchClientBackend::Tavily,
212 endpoint: DEFAULT_TAVILY_SEARCH_ENDPOINT.to_owned(),
213 api_key_environment: DEFAULT_WEB_SEARCH_API_KEY_ENVIRONMENT.to_owned(),
214 timeout_millis: DEFAULT_WEB_SEARCH_TIMEOUT_MILLIS,
215 }
216 }
217}
218
219#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
221#[serde(rename_all = "snake_case")]
222pub enum WebFetchBackend {
223 #[default]
225 Http,
226}
227
228#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
230#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
231pub struct WebFetchSettings {
232 pub enabled: bool,
234 pub backend: WebFetchBackend,
236 pub cache: WebFetchCacheSettings,
238}
239
240impl Default for WebFetchSettings {
241 fn default() -> Self {
242 Self {
243 enabled: false,
244 backend: WebFetchBackend::Http,
245 cache: WebFetchCacheSettings::default(),
246 }
247 }
248}
249
250#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
252#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
253pub struct WebFetchCacheSettings {
254 pub ttl_seconds: u64,
256 pub max_entries: usize,
258 pub max_total_bytes: usize,
260 pub max_entry_bytes: usize,
262}
263
264impl Default for WebFetchCacheSettings {
265 fn default() -> Self {
266 Self {
267 ttl_seconds: DEFAULT_FETCH_CACHE_TTL.as_secs(),
268 max_entries: DEFAULT_FETCH_CACHE_ENTRIES,
269 max_total_bytes: DEFAULT_FETCH_CACHE_TOTAL_BYTES,
270 max_entry_bytes: DEFAULT_FETCH_CACHE_ENTRY_BYTES,
271 }
272 }
273}
274
275#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
277#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
278pub struct TuiSettings {
279 pub viewport: String,
281 pub collapse_thinking: bool,
283 pub reduced_motion: bool,
285 pub submit_key: String,
287 pub newline_key: String,
289 pub abort_key: String,
291 pub clear_key: String,
293 pub exit_key: String,
295 pub model_key: String,
297 pub toggle_thinking_key: String,
299 pub toggle_tools_key: String,
301 pub copy_key: String,
303 pub steering_key: String,
305 pub follow_up_key: String,
307 pub retrieve_queued_key: String,
309}
310
311impl Default for TuiSettings {
312 fn default() -> Self {
313 Self {
314 viewport: "inline".to_owned(),
315 collapse_thinking: false,
316 reduced_motion: false,
317 submit_key: "enter".to_owned(),
318 newline_key: "shift+enter".to_owned(),
319 abort_key: "escape".to_owned(),
320 clear_key: "ctrl+l".to_owned(),
321 exit_key: "ctrl+d".to_owned(),
322 model_key: "ctrl+o".to_owned(),
323 toggle_thinking_key: "ctrl+t".to_owned(),
324 toggle_tools_key: "ctrl+g".to_owned(),
325 copy_key: "ctrl+y".to_owned(),
326 steering_key: "enter".to_owned(),
327 follow_up_key: "alt+enter".to_owned(),
328 retrieve_queued_key: "ctrl+r".to_owned(),
329 }
330 }
331}
332
333#[cfg(test)]
334mod tests {
335 use super::TuiSettings;
336
337 #[test]
338 fn tui_defaults_to_codex_style_inline_viewport() {
339 assert_eq!(TuiSettings::default().viewport, "inline");
340 }
341}
342
343#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
345#[serde(rename_all = "camelCase", deny_unknown_fields)]
346pub struct ResourceSettings {
347 pub context_files: bool,
349 pub skill_paths: Vec<String>,
351 pub prompt_templates: bool,
353}
354
355impl Default for ResourceSettings {
356 fn default() -> Self {
357 Self {
358 context_files: true,
359 skill_paths: Vec::new(),
360 prompt_templates: true,
361 }
362 }
363}
364
365#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
367#[serde(rename_all = "camelCase", deny_unknown_fields)]
368pub struct SettingsLayer {
369 pub schema_version: Option<u32>,
371 pub provider: Option<String>,
373 pub model: Option<String>,
375 pub thinking: Option<String>,
377 pub active_tools: Option<Vec<String>>,
379 pub session_database: Option<String>,
381 pub max_retries: Option<u32>,
383 pub retry_base_delay_ms: Option<u64>,
385 pub retry_max_delay_ms: Option<u64>,
387 pub compaction_enabled: Option<bool>,
389 pub tui: Option<TuiSettingsLayer>,
391 pub resources: Option<ResourceSettingsLayer>,
393 pub web_search: Option<WebSearchSettingsLayer>,
395 pub web_fetch: Option<WebFetchSettingsLayer>,
397 pub project_trust: Option<ProjectTrustDefault>,
399 pub mcp_servers: Option<Vec<McpServerSettings>>,
401}
402
403#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
405#[serde(rename_all = "camelCase", deny_unknown_fields)]
406pub struct WebSearchSettingsLayer {
407 pub route_preference: Option<WebSearchRoutePreference>,
409 pub allowed_domains: Option<Vec<String>>,
411 pub blocked_domains: Option<Vec<String>>,
413 pub location: Option<WebSearchLocationSettingsLayer>,
415 pub client: Option<ClientWebSearchSettingsLayer>,
417}
418
419#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
421#[serde(rename_all = "camelCase", deny_unknown_fields)]
422pub struct WebSearchLocationSettingsLayer {
423 pub country: Option<String>,
425 pub city: Option<String>,
427 pub region: Option<String>,
429 pub timezone: Option<String>,
431}
432
433#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
435#[serde(rename_all = "camelCase", deny_unknown_fields)]
436pub struct ClientWebSearchSettingsLayer {
437 pub enabled: Option<bool>,
439 pub backend: Option<WebSearchClientBackend>,
441 pub endpoint: Option<String>,
443 pub api_key_environment: Option<String>,
445 pub timeout_millis: Option<u64>,
447}
448
449#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
451#[serde(rename_all = "camelCase", deny_unknown_fields)]
452pub struct WebFetchSettingsLayer {
453 pub enabled: Option<bool>,
455 pub backend: Option<WebFetchBackend>,
457 pub cache: Option<WebFetchCacheSettingsLayer>,
459}
460
461#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
463#[serde(rename_all = "camelCase", deny_unknown_fields)]
464pub struct WebFetchCacheSettingsLayer {
465 pub ttl_seconds: Option<u64>,
467 pub max_entries: Option<usize>,
469 pub max_total_bytes: Option<usize>,
471 pub max_entry_bytes: Option<usize>,
473}
474
475#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
477#[serde(rename_all = "camelCase", deny_unknown_fields)]
478pub struct TuiSettingsLayer {
479 pub viewport: Option<String>,
481 pub collapse_thinking: Option<bool>,
483 pub reduced_motion: Option<bool>,
485 pub submit_key: Option<String>,
487 pub newline_key: Option<String>,
489 pub abort_key: Option<String>,
491 pub clear_key: Option<String>,
493 pub exit_key: Option<String>,
495 pub model_key: Option<String>,
497 pub toggle_thinking_key: Option<String>,
499 pub toggle_tools_key: Option<String>,
501 pub copy_key: Option<String>,
503 pub steering_key: Option<String>,
505 pub follow_up_key: Option<String>,
507 pub retrieve_queued_key: Option<String>,
509}
510
511#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
513#[serde(rename_all = "camelCase", deny_unknown_fields)]
514pub struct ResourceSettingsLayer {
515 pub context_files: Option<bool>,
517 pub skill_paths: Option<Vec<String>>,
519 pub prompt_templates: Option<bool>,
521}