Skip to main content

elizaos_plugin_code/actions/
list_files.rs

1use async_trait::async_trait;
2use serde_json::Value;
3
4use crate::service::CoderService;
5use crate::{Action, ActionExample, ActionResult};
6
7pub struct ListFilesAction;
8
9#[async_trait]
10impl Action for ListFilesAction {
11    fn name(&self) -> &str {
12        "LIST_FILES"
13    }
14
15    fn similes(&self) -> Vec<&str> {
16        vec!["LS", "LIST_DIR", "LIST_DIRECTORY", "DIR"]
17    }
18
19    fn description(&self) -> &str {
20        "List files in a directory."
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 dirpath = state.get("path").and_then(|v| v.as_str()).unwrap_or(".");
43        let conv = message
44            .get("room_id")
45            .and_then(|v| v.as_str())
46            .or_else(|| message.get("agent_id").and_then(|v| v.as_str()))
47            .unwrap_or("default");
48
49        match svc.list_files(conv, dirpath).await {
50            Ok(items) => ActionResult {
51                success: true,
52                text: if items.is_empty() {
53                    "(empty)".to_string()
54                } else {
55                    items.join("\n")
56                },
57                data: None,
58                error: None,
59            },
60            Err(err) => ActionResult {
61                success: false,
62                text: err,
63                data: None,
64                error: Some("list_failed".to_string()),
65            },
66        }
67    }
68
69    fn examples(&self) -> Vec<ActionExample> {
70        vec![ActionExample {
71            user_message: "list files".to_string(),
72            agent_response: "Listing…".to_string(),
73        }]
74    }
75}