Skip to main content

elizaos_plugin_code/actions/
change_directory.rs

1use async_trait::async_trait;
2use serde_json::Value;
3
4use crate::service::CoderService;
5use crate::{Action, ActionExample, ActionResult};
6
7pub struct ChangeDirectoryAction;
8
9#[async_trait]
10impl Action for ChangeDirectoryAction {
11    fn name(&self) -> &str {
12        "CHANGE_DIRECTORY"
13    }
14
15    fn similes(&self) -> Vec<&str> {
16        vec!["CD", "CWD"]
17    }
18
19    fn description(&self) -> &str {
20        "Change the working directory (restricted)."
21    }
22
23    async fn validate(&self, _message: &Value, _state: &Value) -> bool {
24        true
25    }
26
27    async fn handler(
28        &self,
29        message: &Value,
30        state: &Value,
31        service: Option<&mut CoderService>,
32    ) -> ActionResult {
33        let Some(svc) = service else {
34            return ActionResult {
35                success: false,
36                text: "Coder service is not available.".to_string(),
37                data: None,
38                error: Some("missing_service".to_string()),
39            };
40        };
41
42        let target = state.get("path").and_then(|v| v.as_str()).unwrap_or("");
43        if target.trim().is_empty() {
44            return ActionResult {
45                success: false,
46                text: "Missing path.".to_string(),
47                data: None,
48                error: Some("missing_path".to_string()),
49            };
50        }
51
52        let conv = message
53            .get("room_id")
54            .and_then(|v| v.as_str())
55            .or_else(|| message.get("agent_id").and_then(|v| v.as_str()))
56            .unwrap_or("default");
57
58        let result = svc.change_directory(conv, target).await;
59        ActionResult {
60            success: result.success,
61            text: if result.success {
62                result.stdout
63            } else {
64                result.stderr
65            },
66            data: None,
67            error: None,
68        }
69    }
70
71    fn examples(&self) -> Vec<ActionExample> {
72        vec![ActionExample {
73            user_message: "cd src".to_string(),
74            agent_response: "Changed directory…".to_string(),
75        }]
76    }
77}