1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use anyhow::{Context, Result};
use sapphire_workspace::SyncConfig;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
/// Matrix channel configuration. Required if `discord` is not set.
#[serde(default)]
pub matrix: Option<MatrixConfig>,
/// Discord channel configuration. Required if `matrix` is not set.
#[serde(default)]
pub discord: Option<DiscordConfig>,
pub anthropic: AnthropicConfig,
/// Context compression configuration.
#[serde(default)]
pub compression: CompressionConfig,
/// Tool configuration (search APIs, etc.).
#[serde(default)]
pub tools: ToolsConfig,
/// HTTP API server configuration.
#[serde(default)]
pub serve: Option<ServeConfig>,
/// Directory containing AGENT.md and MEMORY.md.
/// Defaults to the config file's parent directory.
pub workspace_dir: Option<String>,
/// Directory for persisted JSONL sessions.
/// Defaults to `<workspace_dir>/sessions`.
pub sessions_dir: Option<String>,
/// Hour (0–23, local time) at which a new "day" begins.
/// Used for session resets and daily log generation. Default: 0 (midnight).
#[serde(default)]
pub day_boundary_hour: u8,
/// Whether to generate a daily log at the day boundary. Default: true.
#[serde(default = "default_true")]
pub daily_log_enabled: bool,
/// Whether to compact MEMORY.md at the day boundary. Default: true.
#[serde(default = "default_true")]
pub memory_compaction_enabled: bool,
/// Whether to enable heartbeat (day-boundary + cron) tasks. Default: true.
/// Set to false in test environments to avoid duplicate heartbeat tasks
/// when both test and production instances share the same config.
#[serde(default = "default_true")]
pub heartbeat_enabled: bool,
/// Cold-standby mode: only perform git sync, skip channel listening and
/// heartbeat tasks. Useful for maintaining a backup node that stays in
/// sync without actively processing messages. Default: false.
#[serde(default)]
pub standby_mode: bool,
/// Workspace sync configuration.
///
/// The workspace-level config (`{workspace_dir}/.sapphire-agent/config.toml`)
/// provides shared defaults. This per-user `[sync]` section, when present,
/// takes precedence — allowing each user to override the workspace defaults.
#[serde(default)]
pub sync: Option<SyncConfig>,
}
fn default_true() -> bool {
true
}
/// Configuration for the HTTP API server (serve command).
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct ServeConfig {
#[serde(default = "default_serve_host")]
pub host: String,
#[serde(default = "default_serve_port")]
pub port: u16,
}
fn default_serve_host() -> String {
"127.0.0.1".to_string()
}
fn default_serve_port() -> u16 {
9000
}
/// Configuration for built-in tools.
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct ToolsConfig {
/// Tavily API key for `web_search`. If absent the tool is not registered.
pub tavily_api_key: Option<String>,
/// External MCP servers to connect to. Each server's tools are registered
/// with the naming convention `mcp__<name>__<tool_name>`.
#[serde(default)]
pub mcp_servers: Vec<McpServerConfig>,
}
/// Configuration for a single external MCP server.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct McpServerConfig {
/// Human-readable name (used in tool prefix: `mcp__<name>__<tool>`).
pub name: String,
/// Transport configuration.
#[serde(flatten)]
pub transport: McpTransportConfig,
}
/// Transport configuration for connecting to an MCP server.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum McpTransportConfig {
/// Streamable HTTP transport.
#[serde(rename = "http")]
Http {
/// Server URL (e.g. `http://localhost:3000/mcp`).
url: String,
/// Optional API key / bearer token.
#[serde(default)]
api_key: Option<String>,
},
/// stdio transport — spawn a child process and communicate via stdin/stdout.
#[serde(rename = "stdio")]
Stdio {
/// Command to execute (e.g. `"npx"`, `"uvx"`, `"/path/to/server"`).
command: String,
/// Command arguments.
#[serde(default)]
args: Vec<String>,
/// Additional environment variables passed to the child process.
#[serde(default)]
env: std::collections::HashMap<String, String>,
},
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct MatrixConfig {
pub homeserver: String,
pub access_token: String,
pub user_id: String,
pub device_id: String,
pub room_id: String,
#[serde(default)]
pub allowed_users: Vec<String>,
/// E2EE recovery key (optional)
pub recovery_key: Option<String>,
/// Directory for matrix-sdk state/crypto store. Defaults to
/// `~/.local/share/sapphire-agent/matrix`.
pub state_dir: Option<String>,
}
impl MatrixConfig {
pub fn resolved_state_dir(&self) -> PathBuf {
if let Some(dir) = &self.state_dir {
PathBuf::from(shellexpand::tilde(dir).as_ref())
} else if let Some(dirs) = directories::ProjectDirs::from("", "", "sapphire-agent") {
dirs.data_local_dir().join("matrix")
} else {
PathBuf::from(".sapphire-agent/matrix")
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DiscordConfig {
pub bot_token: String,
/// Text channel IDs the bot listens to. Empty = all channels the bot can see.
#[serde(default)]
pub channel_ids: Vec<String>,
/// Discord user IDs allowed to interact. Empty = all users.
#[serde(default)]
pub allowed_users: Vec<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AnthropicConfig {
pub api_key: String,
#[serde(default = "default_model")]
pub model: String,
/// Cheaper model for casual (non-coding) conversations.
/// If set, the agent uses this model by default and switches to `model`
/// when the message appears to be coding-related.
pub light_model: Option<String>,
#[serde(default = "default_max_tokens")]
pub max_tokens: u32,
pub system_prompt: Option<String>,
}
/// Context compression configuration (provider-agnostic).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CompressionConfig {
/// Whether context compression is enabled. Default: true.
#[serde(default = "default_true")]
pub enabled: bool,
/// Context window size in tokens. Defaults to 200,000.
#[serde(default = "default_context_window")]
pub context_window: usize,
/// Fraction of context window at which compression triggers (0.0–1.0).
/// Defaults to 0.80.
#[serde(default = "default_compression_threshold")]
pub threshold: f64,
/// Number of recent messages to preserve verbatim during compression.
/// Defaults to 20.
#[serde(default = "default_preserve_recent")]
pub preserve_recent: usize,
}
impl Default for CompressionConfig {
fn default() -> Self {
Self {
enabled: true,
context_window: default_context_window(),
threshold: default_compression_threshold(),
preserve_recent: default_preserve_recent(),
}
}
}
fn default_model() -> String {
"claude-opus-4-6".to_string()
}
fn default_max_tokens() -> u32 {
8192
}
fn default_context_window() -> usize {
200_000
}
fn default_compression_threshold() -> f64 {
0.80
}
fn default_preserve_recent() -> usize {
20
}
impl Config {
pub fn load(path: &Path) -> Result<Self> {
let content = std::fs::read_to_string(path)
.with_context(|| format!("Failed to read config file: {}", path.display()))?;
let config: Config =
toml::from_str(&content).with_context(|| "Failed to parse config file")?;
Ok(config)
}
/// Resolve the workspace directory: explicit config > config file's parent directory.
pub fn resolved_workspace_dir(&self, config_path: &Path) -> PathBuf {
if let Some(dir) = &self.workspace_dir {
PathBuf::from(shellexpand::tilde(dir).as_ref())
} else {
config_path
.parent()
.unwrap_or_else(|| Path::new("."))
.to_path_buf()
}
}
/// Resolve the sessions directory for JSONL persistence.
///
/// Explicit config value > `<workspace_dir>/sessions` (default).
pub fn resolved_sessions_dir(&self, workspace_dir: &Path) -> PathBuf {
if let Some(dir) = &self.sessions_dir {
PathBuf::from(shellexpand::tilde(dir).as_ref())
} else {
workspace_dir.join("sessions")
}
}
/// Resolve the default config path: `~/.config/sapphire-agent/config.toml`
pub fn default_path() -> PathBuf {
if let Some(dirs) = directories::ProjectDirs::from("", "", "sapphire-agent") {
dirs.config_dir().join("config.toml")
} else {
PathBuf::from("config.toml")
}
}
}