Skip to main content

elizaos_plugin_shell/actions/
clear_history.rs

1use crate::{Action, ActionExample, ActionResult, ShellService};
2use async_trait::async_trait;
3use serde_json::Value;
4
5pub struct ClearHistoryAction;
6
7impl ClearHistoryAction {
8    const CLEAR_KEYWORDS: &'static [&'static str] =
9        &["clear", "reset", "delete", "remove", "clean"];
10    const HISTORY_KEYWORDS: &'static [&'static str] = &["history", "terminal", "shell", "command"];
11
12    fn has_clear_keyword(text: &str) -> bool {
13        let lower = text.to_lowercase();
14        Self::CLEAR_KEYWORDS.iter().any(|kw| lower.contains(kw))
15    }
16
17    fn has_history_keyword(text: &str) -> bool {
18        let lower = text.to_lowercase();
19        Self::HISTORY_KEYWORDS.iter().any(|kw| lower.contains(kw))
20    }
21}
22
23#[async_trait]
24impl Action for ClearHistoryAction {
25    fn name(&self) -> &str {
26        "CLEAR_SHELL_HISTORY"
27    }
28
29    fn similes(&self) -> Vec<&str> {
30        vec![
31            "RESET_SHELL",
32            "CLEAR_TERMINAL",
33            "CLEAR_HISTORY",
34            "RESET_HISTORY",
35        ]
36    }
37
38    fn description(&self) -> &str {
39        "Clears the recorded history of shell commands for the current conversation"
40    }
41
42    async fn validate(&self, message: &Value, _state: &Value) -> bool {
43        let text = message
44            .get("content")
45            .and_then(|c| c.get("text"))
46            .and_then(|t| t.as_str())
47            .unwrap_or("");
48
49        Self::has_clear_keyword(text) && Self::has_history_keyword(text)
50    }
51
52    async fn handler(
53        &self,
54        message: &Value,
55        _state: &Value,
56        service: Option<&mut ShellService>,
57    ) -> ActionResult {
58        let service = match service {
59            Some(s) => s,
60            None => {
61                return ActionResult {
62                    success: false,
63                    text: "Shell service is not available.".to_string(),
64                    data: None,
65                    error: Some("Shell service is not available".to_string()),
66                }
67            }
68        };
69
70        let conversation_id = message
71            .get("room_id")
72            .and_then(|r| r.as_str())
73            .or_else(|| message.get("agent_id").and_then(|a| a.as_str()))
74            .unwrap_or("default");
75
76        service.clear_command_history(conversation_id);
77
78        ActionResult {
79            success: true,
80            text: "Shell command history has been cleared.".to_string(),
81            data: None,
82            error: None,
83        }
84    }
85
86    fn examples(&self) -> Vec<ActionExample> {
87        vec![
88            ActionExample {
89                user_message: "clear my shell history".to_string(),
90                agent_response: "Shell command history has been cleared.".to_string(),
91            },
92            ActionExample {
93                user_message: "reset the terminal history".to_string(),
94                agent_response: "Shell command history has been cleared.".to_string(),
95            },
96            ActionExample {
97                user_message: "delete command history".to_string(),
98                agent_response: "Shell command history has been cleared.".to_string(),
99            },
100        ]
101    }
102}