Skip to main content

aidaemon_tools/
system.rs

1use std::time::Duration;
2
3use async_trait::async_trait;
4use serde_json::{Value, json};
5
6use crate::OutcomeTool as Tool;
7use crate::backend::{ExecutionRequest, SharedExecutionBackend};
8use crate::semantics::{ToolCapabilities, ToolRole};
9
10pub struct SystemInfoTool {
11    backend: crate::backend::SharedExecutionBackend,
12}
13
14impl SystemInfoTool {
15    pub fn new(backend: crate::backend::SharedExecutionBackend) -> Self {
16        Self { backend }
17    }
18}
19
20#[async_trait]
21impl Tool for SystemInfoTool {
22    fn name(&self) -> &str {
23        "system_info"
24    }
25
26    fn description(&self) -> &str {
27        "Get system information for the configured execution environment, including hostname, OS, uptime, and memory"
28    }
29
30    fn schema(&self) -> Value {
31        json!({
32            "name": "system_info",
33            "description": self.description(),
34            "parameters": {
35                "type": "object",
36                "properties": {},
37                "required": [],
38                "additionalProperties": false
39            }
40        })
41    }
42
43    async fn execute(
44        &self,
45        _arguments: &str,
46        _facts: &crate::semantics::ToolCallContext,
47    ) -> anyhow::Result<crate::semantics::ToolCallOutcome> {
48        let output: anyhow::Result<String> = async move {
49            let backend = self.backend.clone();
50            let mut info = format!(
51                "Execution target: {} ({})\nWorkspace: {}\n",
52                backend.kind().as_str(),
53                backend.id(),
54                backend.workspace_root()
55            );
56
57            // Current date and time
58            if let Some(date) = command_output(backend.clone(), "date", &[]).await {
59                info.push_str(&format!("Date: {}\n", date));
60            }
61
62            // Hostname
63            if let Some(hostname) = command_output(backend.clone(), "hostname", &[]).await {
64                info.push_str(&format!("Hostname: {}\n", hostname));
65            }
66
67            // OS info
68            if let Some(uname) = command_output(backend.clone(), "uname", &["-a"]).await {
69                info.push_str(&format!("OS: {}\n", uname));
70            }
71
72            // Uptime
73            if let Some(uptime) = command_output(backend.clone(), "uptime", &[]).await {
74                info.push_str(&format!("Uptime: {}\n", uptime));
75            }
76
77            // Probe the execution target rather than compiling this decision for
78            // the daemon host's operating system.
79            let memory = if backend.executable_exists("free").await.unwrap_or(false) {
80                command_output(backend.clone(), "free", &["-h"]).await
81            } else if backend.executable_exists("vm_stat").await.unwrap_or(false) {
82                command_output(backend, "vm_stat", &[]).await
83            } else {
84                None
85            };
86            if let Some(memory) = memory {
87                info.push_str(&format!("Memory:\n{}\n", memory));
88            }
89
90            Ok(info)
91        }
92        .await;
93        output.map(crate::semantics::ToolCallOutcome::from_output)
94    }
95}
96
97impl crate::semantics::ToolSemantics for SystemInfoTool {
98    fn dispatch_facts(&self) -> crate::semantics::ToolDispatchFacts {
99        crate::semantics::ToolDispatchFacts {
100            channel_reach: crate::semantics::ChannelReach::Public,
101            evidence: crate::semantics::EvidenceSource::HostInfo,
102            semantic_scope: Some(crate::semantics::ToolSemanticScope::HostLocal),
103            ..Default::default()
104        }
105    }
106
107    fn tool_role(&self) -> ToolRole {
108        ToolRole::Universal
109    }
110
111    fn capabilities(&self) -> ToolCapabilities {
112        ToolCapabilities::READ_ONLY
113    }
114}
115
116async fn command_output(
117    backend: SharedExecutionBackend,
118    program: &str,
119    args: &[&str],
120) -> Option<String> {
121    let output = backend
122        .execute(
123            ExecutionRequest::argv(
124                program,
125                args.iter()
126                    .map(|argument| (*argument).to_string())
127                    .collect(),
128            ),
129            Duration::from_secs(5),
130        )
131        .await
132        .ok()?;
133    (output.exit_code == 0)
134        .then(|| output.stdout_lossy().trim().to_string())
135        .filter(|text| !text.is_empty())
136}