Skip to main content

elizaos_plugin_code/providers/
coder_status.rs

1use async_trait::async_trait;
2use serde_json::{json, Value};
3
4use crate::service::CoderService;
5use crate::{Provider, ProviderResult};
6
7pub struct CoderStatusProvider;
8
9#[async_trait]
10impl Provider for CoderStatusProvider {
11    fn name(&self) -> &str {
12        "CODER_STATUS"
13    }
14
15    fn description(&self) -> &str {
16        "Provides current working directory, allowed directory, and recent command history"
17    }
18
19    fn position(&self) -> i32 {
20        99
21    }
22
23    async fn get(
24        &self,
25        message: &Value,
26        _state: &Value,
27        service: Option<&CoderService>,
28    ) -> ProviderResult {
29        let Some(svc) = service else {
30            return ProviderResult {
31                values: json!({
32                    "coderStatus": "Coder service is not available",
33                    "currentWorkingDirectory": "N/A",
34                    "allowedDirectory": "N/A"
35                }),
36                text: "# Coder Status\n\nCoder service is not available".to_string(),
37                data: json!({ "historyCount": 0, "cwd": "N/A", "allowedDir": "N/A" }),
38            };
39        };
40
41        let conv = message
42            .get("room_id")
43            .and_then(|v| v.as_str())
44            .or_else(|| message.get("agent_id").and_then(|v| v.as_str()))
45            .unwrap_or("default");
46
47        let cwd = svc.current_directory(conv).display().to_string();
48        let allowed = svc.allowed_directory().display().to_string();
49        let history = svc.get_command_history(conv, Some(10));
50
51        let history_text = if history.is_empty() {
52            "No commands in history.".to_string()
53        } else {
54            history
55                .iter()
56                .map(|h| format!("{}> {}", h.working_directory, h.command))
57                .collect::<Vec<String>>()
58                .join("\n")
59        };
60
61        ProviderResult {
62            values: json!({
63                "coderStatus": history_text,
64                "currentWorkingDirectory": cwd,
65                "allowedDirectory": allowed
66            }),
67            text: format!(
68                "Current Directory: {}\nAllowed Directory: {}\n\n{}",
69                svc.current_directory(conv).display(),
70                svc.allowed_directory().display(),
71                history_text
72            ),
73            data: json!({
74                "historyCount": history.len(),
75                "cwd": svc.current_directory(conv).display().to_string(),
76                "allowedDir": svc.allowed_directory().display().to_string()
77            }),
78        }
79    }
80}