opencrabs 0.3.54

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
Documentation
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Config Management Tool
//!
//! Lets the agent read/write config.toml and commands.toml at runtime.

use super::error::Result;
use super::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use async_trait::async_trait;
use serde_json::Value;

pub struct ConfigTool;

#[async_trait]
impl Tool for ConfigTool {
    fn name(&self) -> &str {
        "config_manager"
    }

    fn description(&self) -> &str {
        "Read or write OpenCrabs configuration (config.toml) and user commands (commands.toml). \
         Use this to change settings like approval policy, view current config, \
         add/remove user slash commands, change working directory, or trigger a config reload."
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "enum": [
                        "read_config",
                        "write_config",
                        "read_commands",
                        "add_command",
                        "remove_command",
                        "reload",
                        "set_working_directory"
                    ],
                    "description": "The operation to perform"
                },
                "section": {
                    "type": "string",
                    "description": "Config section for read_config/write_config (e.g. 'agent', 'voice', 'logging'). Omit to read the full config."
                },
                "key": {
                    "type": "string",
                    "description": "Config key within the section for write_config (e.g. 'approval_policy')"
                },
                "value": {
                    "type": "string",
                    "description": "Value to write for write_config (e.g. 'auto-always')"
                },
                "command_name": {
                    "type": "string",
                    "description": "Slash command name for add_command/remove_command (e.g. '/deploy')"
                },
                "command_description": {
                    "type": "string",
                    "description": "Description for add_command"
                },
                "command_prompt": {
                    "type": "string",
                    "description": "Prompt text for add_command"
                },
                "command_action": {
                    "type": "string",
                    "description": "Action type for add_command: 'prompt' or 'system' (default: 'prompt')"
                },
                "path": {
                    "type": "string",
                    "description": "Absolute directory path for set_working_directory (e.g. '/home/user/project')"
                }
            },
            "required": ["operation"]
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::WriteFiles]
    }

    fn requires_approval(&self) -> bool {
        // Writes need approval, reads are safe — but we mark true since
        // the tool *can* write. The agent description guides appropriate use.
        true
    }

    async fn execute(&self, input: Value, context: &ToolExecutionContext) -> Result<ToolResult> {
        let operation = input
            .get("operation")
            .and_then(|v| v.as_str())
            .unwrap_or("");

        match operation {
            "read_config" => self.read_config(&input),
            "write_config" => self.write_config(&input),
            "read_commands" => self.read_commands(),
            "add_command" => self.add_command(&input),
            "remove_command" => self.remove_command(&input),
            "reload" => self.reload_config(),
            "set_working_directory" => self.set_working_directory(&input, context),
            _ => Ok(ToolResult::error(format!(
                "Unknown operation: '{}'. Valid: read_config, write_config, \
                 read_commands, add_command, remove_command, reload, set_working_directory",
                operation
            ))),
        }
    }
}

impl ConfigTool {
    fn read_config(&self, input: &Value) -> Result<ToolResult> {
        let config = match crate::config::Config::load() {
            Ok(c) => c,
            Err(e) => return Ok(ToolResult::error(format!("Failed to load config: {}", e))),
        };

        let section = input.get("section").and_then(|v| v.as_str());

        let output = match section {
            Some("agent") => format_toml(&config.agent),
            Some("voice") => {
                let vc = config.voice_config();
                format!(
                    "stt_enabled = {}\nstt_mode = {:?}\ntts_enabled = {}\ntts_mode = {:?}\ntts_voice = {:?}\nlocal_tts_voice = {:?}",
                    vc.stt_enabled,
                    vc.stt_mode,
                    vc.tts_enabled,
                    vc.tts_mode,
                    vc.tts_voice,
                    vc.local_tts_voice
                )
            }
            Some("logging") => format_toml(&config.logging),
            Some("debug") => format_toml(&config.debug),
            Some("channels") => format_toml(&config.channels),
            Some("crabrace") => format_toml(&config.crabrace),
            Some("database") => format_toml(&config.database),
            Some("providers") => format_toml(&config.providers),
            Some(other) => {
                return Ok(ToolResult::error(format!(
                    "Unknown config section: '{}'. Valid: agent, voice, logging, debug, \
                     channels, crabrace, database, providers",
                    other
                )));
            }
            None => {
                // Full config — skip api_keys for safety
                format_toml(&config)
            }
        };

        Ok(ToolResult::success(output))
    }

    fn write_config(&self, input: &Value) -> Result<ToolResult> {
        let section = match input.get("section").and_then(|v| v.as_str()) {
            Some(s) => s,
            None => {
                return Ok(ToolResult::error(
                    "'section' is required for write_config".into(),
                ));
            }
        };
        let key = match input.get("key").and_then(|v| v.as_str()) {
            Some(k) => k,
            None => {
                return Ok(ToolResult::error(
                    "'key' is required for write_config".into(),
                ));
            }
        };
        let value = match input.get("value").and_then(|v| v.as_str()) {
            Some(v) => v,
            None => {
                return Ok(ToolResult::error(
                    "'value' is required for write_config".into(),
                ));
            }
        };

        match crate::config::Config::write_key(section, key, value) {
            Ok(()) => Ok(ToolResult::success(format!(
                "Set [{section}].{key} = \"{value}\" in config.toml"
            ))),
            Err(e) => Ok(ToolResult::error(format!("Failed to write config: {}", e))),
        }
    }

    fn read_commands(&self) -> Result<ToolResult> {
        let brain_path = crate::brain::BrainLoader::resolve_path();
        let loader = crate::brain::CommandLoader::from_brain_path(&brain_path);
        let commands = loader.load();

        if commands.is_empty() {
            return Ok(ToolResult::success(
                "No user-defined commands. Use add_command to create one.".into(),
            ));
        }

        let mut output = format!("{} user command(s):\n\n", commands.len());
        for cmd in &commands {
            output.push_str(&format!(
                "  {}{} (action: {})\n    prompt: {}\n\n",
                cmd.name, cmd.description, cmd.action, cmd.prompt
            ));
        }
        Ok(ToolResult::success(output))
    }

    fn add_command(&self, input: &Value) -> Result<ToolResult> {
        let name = match input.get("command_name").and_then(|v| v.as_str()) {
            Some(n) => n,
            None => {
                return Ok(ToolResult::error(
                    "'command_name' is required for add_command".into(),
                ));
            }
        };
        let description = input
            .get("command_description")
            .and_then(|v| v.as_str())
            .unwrap_or("User command");
        let prompt = match input.get("command_prompt").and_then(|v| v.as_str()) {
            Some(p) => p,
            None => {
                return Ok(ToolResult::error(
                    "'command_prompt' is required for add_command".into(),
                ));
            }
        };
        let action = input
            .get("command_action")
            .and_then(|v| v.as_str())
            .unwrap_or("prompt");

        // Ensure name starts with /
        let name = if name.starts_with('/') {
            name.to_string()
        } else {
            format!("/{}", name)
        };

        let brain_path = crate::brain::BrainLoader::resolve_path();
        let loader = crate::brain::CommandLoader::from_brain_path(&brain_path);

        let cmd = crate::brain::commands::UserCommand {
            name: name.clone(),
            description: description.to_string(),
            action: action.to_string(),
            prompt: prompt.to_string(),
        };

        match loader.add_command(cmd) {
            Ok(()) => Ok(ToolResult::success(format!(
                "Added command {name} to commands.toml"
            ))),
            Err(e) => Ok(ToolResult::error(format!("Failed to add command: {}", e))),
        }
    }

    fn remove_command(&self, input: &Value) -> Result<ToolResult> {
        let name = match input.get("command_name").and_then(|v| v.as_str()) {
            Some(n) => n,
            None => {
                return Ok(ToolResult::error(
                    "'command_name' is required for remove_command".into(),
                ));
            }
        };

        let brain_path = crate::brain::BrainLoader::resolve_path();
        let loader = crate::brain::CommandLoader::from_brain_path(&brain_path);

        match loader.remove_command(name) {
            Ok(true) => Ok(ToolResult::success(format!(
                "Removed command {name} from commands.toml"
            ))),
            Ok(false) => Ok(ToolResult::success(format!(
                "Command {name} not found in commands.toml"
            ))),
            Err(e) => Ok(ToolResult::error(format!(
                "Failed to remove command: {}",
                e
            ))),
        }
    }

    fn reload_config(&self) -> Result<ToolResult> {
        match crate::config::Config::reload() {
            Ok(_) => Ok(ToolResult::success(
                "Configuration reloaded from disk.".into(),
            )),
            Err(e) => Ok(ToolResult::error(format!("Failed to reload config: {}", e))),
        }
    }

    fn set_working_directory(
        &self,
        input: &Value,
        context: &ToolExecutionContext,
    ) -> Result<ToolResult> {
        let path_str = match input.get("path").and_then(|v| v.as_str()) {
            Some(p) => p,
            None => {
                return Ok(ToolResult::error(
                    "'path' is required for set_working_directory".into(),
                ));
            }
        };

        let path = std::path::PathBuf::from(path_str);

        // Validate the path exists and is a directory
        if !path.exists() {
            return Ok(ToolResult::error(format!(
                "Path does not exist: {}",
                path_str
            )));
        }
        if !path.is_dir() {
            return Ok(ToolResult::error(format!(
                "Path is not a directory: {}",
                path_str
            )));
        }

        // Canonicalize to resolve symlinks and relative components
        let canonical = match path.canonicalize() {
            Ok(p) => p,
            Err(e) => return Ok(ToolResult::error(format!("Failed to resolve path: {}", e))),
        };

        // Update runtime working directory via shared handle
        if let Some(ref shared_wd) = context.shared_working_directory {
            *shared_wd.write().expect("working_directory lock poisoned") = canonical.clone();
        }

        // Persist to session DB — that's the source of truth for per-session WD.
        if let Some(ref svc_ctx) = context.service_context {
            let session_svc = crate::services::SessionService::new(svc_ctx.clone());
            let sid = context.session_id;
            let dir_str = canonical.to_string_lossy().to_string();
            tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async {
                    let _ = session_svc
                        .update_session_working_directory(sid, Some(dir_str))
                        .await;
                });
            });
        }

        Ok(ToolResult::success(format!(
            "Working directory changed to: {}",
            canonical.display()
        )))
    }
}

/// Serialise any serde type to pretty TOML, falling back to Debug.
fn format_toml<T: serde::Serialize>(value: &T) -> String {
    toml::to_string_pretty(value).unwrap_or_else(|_| format!("{:?}", "serialization error"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tool_metadata() {
        let tool = ConfigTool;
        assert_eq!(tool.name(), "config_manager");
        assert!(tool.requires_approval());
    }

    #[tokio::test]
    async fn test_unknown_operation() {
        let tool = ConfigTool;
        let ctx = ToolExecutionContext::new(uuid::Uuid::new_v4());
        let result = tool
            .execute(serde_json::json!({"operation": "nope"}), &ctx)
            .await
            .unwrap();
        assert!(!result.success);
        assert!(result.error.unwrap().contains("Unknown operation"));
    }

    #[tokio::test]
    async fn test_read_config() {
        let tool = ConfigTool;
        let ctx = ToolExecutionContext::new(uuid::Uuid::new_v4());
        let result = tool
            .execute(
                serde_json::json!({"operation": "read_config", "section": "agent"}),
                &ctx,
            )
            .await
            .unwrap();
        // Should succeed even with default config
        assert!(result.success);
        assert!(result.output.contains("approval_policy"));
    }

    #[tokio::test]
    async fn test_read_commands_empty() {
        let tool = ConfigTool;
        let ctx = ToolExecutionContext::new(uuid::Uuid::new_v4());
        let result = tool
            .execute(serde_json::json!({"operation": "read_commands"}), &ctx)
            .await
            .unwrap();
        assert!(result.success);
    }

    #[tokio::test]
    async fn test_write_config_missing_fields() {
        let tool = ConfigTool;
        let ctx = ToolExecutionContext::new(uuid::Uuid::new_v4());
        let result = tool
            .execute(serde_json::json!({"operation": "write_config"}), &ctx)
            .await
            .unwrap();
        assert!(!result.success);
    }
}