Skip to main content

scv_server/config/
schema.rs

1//! The configuration schema: every table of `config.toml`, with defaults.
2
3use std::{
4    collections::{BTreeMap, HashMap},
5    path::PathBuf,
6};
7
8use scv_channels::state::AccountSettings;
9use scv_client::Secret;
10use scv_core::ContextConfig;
11use scv_provider_openai::ProviderLimits;
12use scv_tools::BusyBehavior;
13use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16#[serde(default, deny_unknown_fields)]
17pub struct Config {
18    pub provider: ProviderConfig,
19    /// Named provider profiles. When non-empty, `provider.active` selects one.
20    pub(crate) providers: HashMap<String, ProviderConfig>,
21    pub(crate) provider_active: Option<String>,
22    pub(crate) agent: AgentConfig,
23    pub(crate) session: SessionConfig,
24    pub(crate) context: ContextConfigFile,
25    pub(crate) tools: ToolConfig,
26    pub(crate) protocol: ProtocolConfig,
27    pub(crate) tui: TuiConfig,
28    pub update: UpdateConfig,
29    pub(crate) notify: NotifyConfig,
30    pub(crate) history: HistoryConfig,
31    pub(crate) provider_limits: ProviderLimitsFile,
32    pub(crate) skills: SkillsConfig,
33    pub(crate) agents: AgentsConfig,
34    pub(crate) web: WebConfig,
35    /// `[channels.<channel>.<account>]`: each chat account's settings. SCV's
36    /// channel store reads and edits them in the instance's `config.toml`;
37    /// here they are only validated.
38    pub(crate) channels: BTreeMap<String, BTreeMap<String, AccountSettings>>,
39    /// The process-owned root: see [`Layout`] for what it holds.
40    #[serde(skip)]
41    pub(crate) instance_home: PathBuf,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(default, deny_unknown_fields)]
46pub struct ProviderConfig {
47    pub(crate) active: Option<String>,
48    pub kind: String,
49    pub wire_api: String,
50    pub model: String,
51    pub base_url: String,
52    pub api_key: Option<Secret>,
53    pub api_key_env: Option<String>,
54    pub timeout_seconds: u64,
55    /// Extra request headers; their values may carry credentials.
56    pub headers: HashMap<String, Secret>,
57    /// Show images users attach to the model as image input. Turn it off
58    /// for a model without vision; SCV also stops for the session after the
59    /// provider rejects an image.
60    pub(crate) image_input: bool,
61    /// The reasoning effort SCV's own requests ask this provider's model for
62    /// (Responses `reasoning.effort`), such as `high`. Unset, requests name
63    /// none and the model uses its default.
64    pub reasoning_effort: Option<String>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize, Default)]
68#[serde(default, deny_unknown_fields)]
69pub struct UpdateConfig {
70    /// Optional Cargo registry index URL used by `scv update`.
71    pub index_url: Option<String>,
72}
73
74/// Where SCV sends notices nobody asked for: an update started from a
75/// terminal, a rollback, a restart after a crash, or a disconnected account.
76#[derive(Debug, Clone, Serialize, Deserialize, Default)]
77#[serde(default, deny_unknown_fields)]
78pub(crate) struct NotifyConfig {
79    /// Accounts as `<channel>:<account>`, such as `feishu:default`. A notice
80    /// goes to the owner of the first one that is connected, on that one
81    /// account only. Empty: the chat the owner last wrote from.
82    pub(crate) owner: Vec<String>,
83}
84
85/// The chat log of the owner's direct chats and the files kept from them.
86#[derive(Debug, Clone, Serialize, Deserialize)]
87#[serde(default, deny_unknown_fields)]
88pub(crate) struct HistoryConfig {
89    /// Minutes without a message after which the next one starts a new
90    /// episode, which a new session no longer reloads.
91    pub(crate) episode_gap_minutes: u64,
92    /// The share of each disk holding the chat log, chat media, or kept
93    /// files that must stay free; below it the owner is told and SCV stops
94    /// saving new files from chat. 0 turns the check off.
95    pub(crate) min_free_percent: u8,
96    /// Where files the owner asks to keep go, under
97    /// `<channel>/<account>/<conversation>/files/`; the history directory
98    /// when unset.
99    pub(crate) archive_dir: Option<PathBuf>,
100}
101
102impl Default for HistoryConfig {
103    fn default() -> Self {
104        Self {
105            episode_gap_minutes: 120,
106            min_free_percent: 20,
107            archive_dir: None,
108        }
109    }
110}
111
112impl Default for ProviderConfig {
113    fn default() -> Self {
114        Self {
115            active: None,
116            kind: "openai-compatible".into(),
117            wire_api: "responses".into(),
118            model: "gpt-4.1-mini".into(),
119            base_url: "https://api.openai.com/v1".into(),
120            api_key: None,
121            api_key_env: Some("OPENAI_API_KEY".into()),
122            timeout_seconds: 600,
123            headers: HashMap::new(),
124            image_input: true,
125            reasoning_effort: None,
126        }
127    }
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131#[serde(default, deny_unknown_fields)]
132pub(crate) struct AgentConfig {
133    pub(crate) max_steps: usize,
134    pub(crate) system_prompt: String,
135    /// The `agent` tool is offered only while this SCV's own delegation depth
136    /// is below this, so delegation chains stay bounded. 0 disables it.
137    pub(crate) max_delegation_depth: u32,
138    /// Delegated conversations a session remembers; starting another forgets
139    /// the least recently used idle one.
140    pub(crate) max_conversations: usize,
141    /// A delegated conversation unused this long is forgotten.
142    pub(crate) conversation_idle_seconds: u64,
143    /// Background agent jobs (`background: true`) a session may run at once;
144    /// 0 turns background delegation off.
145    pub(crate) max_background: usize,
146    pub(crate) on_busy: BusyBehavior,
147    pub(crate) steer_fallback: BusyBehavior,
148    pub(crate) max_queued_turns: usize,
149    /// Agents the user prefers, in order (such as `["codex", "claude"]`);
150    /// the system prompt names the offered ones, and the first of them runs
151    /// an `agent` call that names none. Empty states no preference.
152    pub(crate) prefer: Vec<String>,
153}
154
155impl Default for AgentConfig {
156    fn default() -> Self {
157        Self {
158            max_steps: 128,
159            max_delegation_depth: 2,
160            max_conversations: 8,
161            conversation_idle_seconds: 86400,
162            // The main agent hands most work to background jobs and stays
163            // available, so a few may run at once.
164            max_background: 4,
165            on_busy: BusyBehavior::Queue,
166            steer_fallback: BusyBehavior::Queue,
167            max_queued_turns: 4,
168            prefer: Vec::new(),
169            system_prompt: "You are SCV, a concise and careful agent. Use tools to inspect, change, and verify.".into(),
170        }
171    }
172}
173
174#[derive(Debug, Clone, Serialize, Deserialize)]
175#[serde(default, deny_unknown_fields)]
176pub(crate) struct SessionConfig {
177    pub(crate) max_history_bytes: usize,
178    pub(crate) max_messages: usize,
179}
180
181impl Default for SessionConfig {
182    fn default() -> Self {
183        Self {
184            max_history_bytes: 16 * 1024 * 1024,
185            max_messages: 10_000,
186        }
187    }
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
191#[serde(default, deny_unknown_fields)]
192pub(crate) struct ContextConfigFile {
193    pub(crate) max_tokens: usize,
194    pub(crate) reserve_output_tokens: usize,
195    pub(crate) safety_margin_tokens: usize,
196    pub(crate) bytes_per_token: usize,
197    pub(crate) summary_max_chars: usize,
198}
199
200impl Default for ContextConfigFile {
201    fn default() -> Self {
202        let value = ContextConfig::default();
203        Self {
204            max_tokens: value.max_tokens,
205            reserve_output_tokens: value.reserve_output_tokens,
206            safety_margin_tokens: value.safety_margin_tokens,
207            bytes_per_token: value.bytes_per_token,
208            summary_max_chars: value.summary_max_chars,
209        }
210    }
211}
212
213impl From<&ContextConfigFile> for ContextConfig {
214    fn from(value: &ContextConfigFile) -> Self {
215        Self {
216            max_tokens: value.max_tokens,
217            reserve_output_tokens: value.reserve_output_tokens,
218            safety_margin_tokens: value.safety_margin_tokens,
219            bytes_per_token: value.bytes_per_token,
220            summary_max_chars: value.summary_max_chars,
221        }
222    }
223}
224
225#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
226#[serde(rename_all = "kebab-case")]
227pub enum ApprovalPolicy {
228    OnRisk,
229    Always,
230    Never,
231}
232
233#[derive(Debug, Clone, Serialize, Deserialize)]
234#[serde(default, deny_unknown_fields)]
235pub(crate) struct ToolConfig {
236    pub(crate) approval_policy: ApprovalPolicy,
237    /// Let background `agent` calls set `no_timeout` and run with no main
238    /// deadline. Off unless the owner turns it on.
239    pub(crate) allow_no_timeout: bool,
240    /// `bash` timeout when a call does not choose one.
241    pub(crate) command_timeout_seconds: u64,
242    /// Native-agent timeout when a call does not choose one.
243    pub(crate) agent_timeout_seconds: u64,
244    /// The longest timeout a single tool call may request.
245    pub(crate) max_timeout_seconds: u64,
246    pub(crate) output_limit_bytes: usize,
247    pub(crate) max_read_bytes: usize,
248    pub(crate) max_write_bytes: usize,
249}
250
251impl Default for ToolConfig {
252    fn default() -> Self {
253        Self {
254            approval_policy: ApprovalPolicy::OnRisk,
255            allow_no_timeout: false,
256            command_timeout_seconds: 600,
257            agent_timeout_seconds: 3600,
258            max_timeout_seconds: 14400,
259            output_limit_bytes: 64 * 1024,
260            max_read_bytes: 256 * 1024,
261            max_write_bytes: 1024 * 1024,
262        }
263    }
264}
265
266#[derive(Debug, Clone, Serialize, Deserialize)]
267#[serde(default, deny_unknown_fields)]
268pub(crate) struct ProtocolConfig {
269    pub(crate) max_client_frame_bytes: usize,
270    pub(crate) max_server_frame_bytes: usize,
271}
272
273impl Default for ProtocolConfig {
274    fn default() -> Self {
275        Self {
276            max_client_frame_bytes: 1024 * 1024,
277            max_server_frame_bytes: 8 * 1024 * 1024,
278        }
279    }
280}
281
282#[derive(Debug, Clone, Serialize, Deserialize)]
283#[serde(default, deny_unknown_fields)]
284pub(crate) struct TuiConfig {
285    pub(crate) max_transcript_bytes: usize,
286    pub(crate) max_transcript_items: usize,
287    pub(crate) max_prompt_history_bytes: usize,
288    pub(crate) max_prompt_history_items: usize,
289}
290
291impl Default for TuiConfig {
292    fn default() -> Self {
293        Self {
294            max_transcript_bytes: 8 * 1024 * 1024,
295            max_transcript_items: 10_000,
296            max_prompt_history_bytes: 1024 * 1024,
297            max_prompt_history_items: 200,
298        }
299    }
300}
301
302#[derive(Debug, Clone, Serialize, Deserialize)]
303#[serde(default, deny_unknown_fields)]
304pub(crate) struct ProviderLimitsFile {
305    pub(crate) max_sse_event_bytes: usize,
306    pub(crate) max_response_bytes: usize,
307    pub(crate) max_assistant_bytes: usize,
308    pub(crate) max_tool_calls: usize,
309    pub(crate) max_tool_arguments_bytes: usize,
310    pub(crate) max_retries: usize,
311}
312
313impl Default for ProviderLimitsFile {
314    fn default() -> Self {
315        let value = ProviderLimits::default();
316        Self {
317            max_sse_event_bytes: value.max_sse_event_bytes,
318            max_response_bytes: value.max_response_bytes,
319            max_assistant_bytes: value.max_assistant_bytes,
320            max_tool_calls: value.max_tool_calls,
321            max_tool_arguments_bytes: value.max_tool_arguments_bytes,
322            max_retries: value.max_retries,
323        }
324    }
325}
326
327#[derive(Debug, Clone, Serialize, Deserialize)]
328#[serde(default, deny_unknown_fields)]
329pub(crate) struct SkillsConfig {
330    pub(crate) user_dir: PathBuf,
331    pub(crate) project_dir: PathBuf,
332    /// List the agent skills (`.agents/skills`, `.claude/skills`) of the
333    /// workspace and its immediate child projects in tool-enabled sessions.
334    pub(crate) scan_projects: bool,
335    pub(crate) max_skills: usize,
336    pub(crate) max_skill_bytes: usize,
337}
338
339impl Default for SkillsConfig {
340    fn default() -> Self {
341        Self {
342            user_dir: PathBuf::from("~/.scv/skills"),
343            project_dir: PathBuf::from(".scv/skills"),
344            scan_projects: true,
345            max_skills: 128,
346            max_skill_bytes: 256 * 1024,
347        }
348    }
349}
350
351/// Where `web_search` results come from.
352#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
353#[serde(rename_all = "lowercase")]
354pub(crate) enum WebSearchMode {
355    Off,
356    /// The provider endpoint's hosted Responses `web_search` tool.
357    Provider,
358    Searxng,
359    Brave,
360}
361
362#[derive(Debug, Clone, Serialize, Deserialize)]
363#[serde(default, deny_unknown_fields)]
364pub(crate) struct WebConfig {
365    /// Offer `web_fetch` (and search, when configured) to tool-enabled sessions.
366    pub(crate) enabled: bool,
367    pub(crate) fetch_max_bytes: usize,
368    pub(crate) fetch_timeout_seconds: u64,
369    pub(crate) max_redirects: usize,
370    /// HTTPS hosts `web_fetch` may read without approval.
371    pub(crate) auto_approve_domains: Vec<String>,
372    /// Let `web_fetch` reach loopback, private, and link-local addresses.
373    pub(crate) allow_private_addresses: bool,
374    pub(crate) search: WebSearchMode,
375    pub(crate) searxng_url: Option<String>,
376    pub(crate) brave_url: String,
377    pub(crate) brave_api_key: Option<Secret>,
378    pub(crate) brave_api_key_env: Option<String>,
379    pub(crate) max_search_results: usize,
380}
381
382impl Default for WebConfig {
383    fn default() -> Self {
384        Self {
385            enabled: true,
386            fetch_max_bytes: 2 * 1024 * 1024,
387            fetch_timeout_seconds: 30,
388            max_redirects: 5,
389            auto_approve_domains: [
390                "docs.rs",
391                "crates.io",
392                "doc.rust-lang.org",
393                "docs.python.org",
394                "pypi.org",
395                "developer.mozilla.org",
396            ]
397            .map(String::from)
398            .to_vec(),
399            allow_private_addresses: false,
400            search: WebSearchMode::Off,
401            searxng_url: None,
402            brave_url: "https://api.search.brave.com/res/v1/web/search".into(),
403            brave_api_key: None,
404            brave_api_key_env: Some("BRAVE_SEARCH_API_KEY".into()),
405            max_search_results: 8,
406        }
407    }
408}
409
410#[derive(Debug, Clone, Serialize, Deserialize, Default)]
411#[serde(default, deny_unknown_fields)]
412pub(crate) struct AdapterConfig {
413    pub(crate) command: String,
414    pub(crate) args: Vec<String>,
415    /// `full` adds the CLI's own switches for unprompted, unsandboxed work.
416    pub(crate) permissions: AgentPermissions,
417    /// Placed immediately before the prompt (`grok -p <prompt>`).
418    pub(crate) prompt_args: Vec<String>,
419    /// Appended when a call selects a model; `{model}` is substituted.
420    pub(crate) model_args: Vec<String>,
421    /// Appended when a call selects an effort; `{effort}` is substituted.
422    pub(crate) effort_args: Vec<String>,
423    /// How SCV talks to the agent: its ACP server or one process per turn.
424    pub(crate) transport: AgentTransport,
425    /// When to choose this agent, in the user's words; added to its tool
426    /// description so the model can pick between agents.
427    pub(crate) use_for: Option<String>,
428    /// The model an `agent` call to this agent runs on when it names none.
429    pub(crate) model: Option<String>,
430    /// The effort an `agent` call runs at when it names none.
431    pub(crate) effort: Option<String>,
432    /// The effort the main agent is told to pass for a hard task.
433    pub(crate) hard_task_effort: Option<String>,
434    pub(crate) on_busy: Option<BusyBehavior>,
435    pub(crate) steer_fallback: Option<BusyBehavior>,
436    pub(crate) max_queued_turns: Option<usize>,
437}
438
439impl AdapterConfig {
440    /// The user's model and efforts for this agent, as the tools take them.
441    pub(crate) fn defaults(&self) -> scv_tools::AgentDefaults {
442        scv_tools::AgentDefaults {
443            model: self.model.clone(),
444            effort: self.effort.clone(),
445            hard_task_effort: self.hard_task_effort.clone(),
446        }
447    }
448
449    /// The ACP server this agent runs on, when `descriptor` has one and the
450    /// transport allows it: `acp`, or `auto` with the built-in command. A
451    /// custom `command` points SCV at a specific CLI, which the ACP server
452    /// would not run.
453    pub(crate) fn acp_launch(
454        &self,
455        descriptor: &scv_tools::adapters::AdapterDescriptor,
456    ) -> Option<scv_tools::adapters::AcpLaunch> {
457        descriptor.acp.filter(|_| match self.transport {
458            AgentTransport::Acp => true,
459            AgentTransport::Resume => false,
460            AgentTransport::Auto => self.command == descriptor.command,
461        })
462    }
463}
464
465/// How SCV talks to a delegated agent that has an ACP server.
466#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
467#[serde(rename_all = "lowercase")]
468pub(crate) enum AgentTransport {
469    /// The agent's ACP server when it is installed, else one process per turn.
470    #[default]
471    Auto,
472    /// Only its ACP server; the agent is not offered while it is missing.
473    Acp,
474    /// One CLI process per turn, continued through the CLI's own resume.
475    Resume,
476}
477
478/// How much a delegated CLI may do without its own prompts.
479#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
480#[serde(rename_all = "lowercase")]
481pub(crate) enum AgentPermissions {
482    /// Add nothing: the CLI's own configuration decides.
483    #[default]
484    Default,
485    /// Add the CLI's full-autonomy switches: no approval prompts, no sandbox,
486    /// and web search where the CLI gates it. An explicit user opt-in.
487    Full,
488}
489
490/// `[agents.<name>]` for every adapter in [`scv_tools::adapters::ADAPTERS`].
491#[derive(Debug, Clone, Serialize, Deserialize)]
492#[serde(transparent)]
493pub(crate) struct AgentsConfig(pub(crate) BTreeMap<String, AdapterConfig>);
494
495impl Default for AgentsConfig {
496    fn default() -> Self {
497        let strings = |values: &[&str]| values.iter().map(|value| (*value).to_owned()).collect();
498        Self(
499            scv_tools::adapters::ADAPTERS
500                .iter()
501                .map(|adapter| {
502                    (
503                        adapter.name.to_owned(),
504                        AdapterConfig {
505                            command: adapter.command.into(),
506                            args: strings(adapter.args),
507                            permissions: AgentPermissions::Default,
508                            prompt_args: strings(adapter.prompt_args),
509                            model_args: strings(adapter.model_args),
510                            effort_args: strings(adapter.effort_args),
511                            transport: AgentTransport::Auto,
512                            use_for: None,
513                            model: None,
514                            effort: None,
515                            hard_task_effort: None,
516                            on_busy: None,
517                            steer_fallback: None,
518                            max_queued_turns: None,
519                        },
520                    )
521                })
522                .collect(),
523        )
524    }
525}
526
527/// What the command line, or a client's `session.start`, changes on top of
528/// the configuration files.
529#[derive(Debug, Clone, Default)]
530pub struct ConfigOverrides {
531    pub provider: Option<String>,
532    pub model: Option<String>,
533    pub base_url: Option<String>,
534    pub approval_policy: Option<ApprovalPolicy>,
535    pub no_tools: bool,
536    /// An explicit configuration layer (`--config`, or `SCV_CONFIG` as the
537    /// process received it), applied after the user and project files.
538    pub config_file: Option<PathBuf>,
539}