deepseek_rust_cli/tools/
system_tools.rs1use std::{collections::HashMap, path::Path};
2
3use anyhow::Result;
4use async_trait::async_trait;
5use serde_json::Value;
6
7use crate::{agent::types::UndoAction, tools, tools::base::Tool};
8
9pub struct SystemInfoTool;
10#[async_trait]
11impl Tool for SystemInfoTool {
12 fn name(&self) -> &str {
13 "get_system_info"
14 }
15 async fn execute(
16 &self,
17 _args: &HashMap<String, Value>,
18 _undo: &mut Vec<UndoAction>,
19 _cwd: Option<&Path>,
20 ) -> Result<String> {
21 tools::system::get_system_info()
22 }
23}
24
25pub struct ShellTool;
26#[async_trait]
27impl Tool for ShellTool {
28 fn name(&self) -> &str {
29 "execute_shell_command"
30 }
31 async fn execute(
32 &self,
33 args: &HashMap<String, Value>,
34 _undo: &mut Vec<UndoAction>,
35 agent_cwd: Option<&Path>,
36 ) -> Result<String> {
37 let cmd = args
38 .get("command")
39 .and_then(|v| v.as_str())
40 .ok_or_else(|| anyhow::anyhow!("Missing 'command'"))?;
41 let bg = args
42 .get("is_background")
43 .and_then(|v| v.as_bool())
44 .unwrap_or(false);
45 let cwd = args
46 .get("cwd")
47 .and_then(|v| v.as_str())
48 .or_else(|| agent_cwd.and_then(|p| p.to_str()));
49 let env_vars = args.get("env").and_then(|v| v.as_object()).map(|obj| {
50 obj.iter()
51 .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("").to_string()))
52 .collect()
53 });
54 tools::system::execute_shell_command(cmd, bg, cwd, env_vars).await
55 }
56}
57
58pub struct StartBackgroundProcessTool;
59#[async_trait]
60impl Tool for StartBackgroundProcessTool {
61 fn name(&self) -> &str {
62 "start_background_process"
63 }
64 async fn execute(
65 &self,
66 args: &HashMap<String, Value>,
67 _undo: &mut Vec<UndoAction>,
68 agent_cwd: Option<&Path>,
69 ) -> Result<String> {
70 let cmd = args
71 .get("command")
72 .and_then(|v| v.as_str())
73 .ok_or_else(|| anyhow::anyhow!("Missing 'command'"))?;
74 let cwd = args
75 .get("cwd")
76 .and_then(|v| v.as_str())
77 .or_else(|| agent_cwd.and_then(|p| p.to_str()));
78 let env_vars = args.get("env").and_then(|v| v.as_object()).map(|obj| {
79 obj.iter()
80 .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("").to_string()))
81 .collect()
82 });
83 tools::system::start_background_process(cmd, cwd, env_vars).await
84 }
85}
86
87pub struct ReadBackgroundProcessLogsTool;
88#[async_trait]
89impl Tool for ReadBackgroundProcessLogsTool {
90 fn name(&self) -> &str {
91 "read_background_process_logs"
92 }
93 async fn execute(
94 &self,
95 args: &HashMap<String, Value>,
96 _undo: &mut Vec<UndoAction>,
97 _cwd: Option<&Path>,
98 ) -> Result<String> {
99 let pid = args
100 .get("pid")
101 .and_then(|v| v.as_u64())
102 .ok_or_else(|| anyhow::anyhow!("Missing 'pid'"))? as u32;
103 tools::system::read_background_process_logs(pid).await
104 }
105}
106
107pub struct KillBackgroundProcessTool;
108#[async_trait]
109impl Tool for KillBackgroundProcessTool {
110 fn name(&self) -> &str {
111 "kill_background_process"
112 }
113 async fn execute(
114 &self,
115 args: &HashMap<String, Value>,
116 _undo: &mut Vec<UndoAction>,
117 _cwd: Option<&Path>,
118 ) -> Result<String> {
119 let pid = args
120 .get("pid")
121 .and_then(|v| v.as_u64())
122 .ok_or_else(|| anyhow::anyhow!("Missing 'pid'"))? as u32;
123 tools::system::kill_background_process(pid).await
124 }
125}
126
127pub struct ListBackgroundProcessesTool;
128#[async_trait]
129impl Tool for ListBackgroundProcessesTool {
130 fn name(&self) -> &str {
131 "list_background_processes"
132 }
133 async fn execute(
134 &self,
135 _args: &HashMap<String, Value>,
136 _undo: &mut Vec<UndoAction>,
137 _cwd: Option<&Path>,
138 ) -> Result<String> {
139 tools::system::list_background_processes().await
140 }
141}
142
143pub struct CheckPortStatusTool;
144#[async_trait]
145impl Tool for CheckPortStatusTool {
146 fn name(&self) -> &str {
147 "check_port_status"
148 }
149 async fn execute(
150 &self,
151 args: &HashMap<String, Value>,
152 _undo: &mut Vec<UndoAction>,
153 _cwd: Option<&Path>,
154 ) -> Result<String> {
155 let port = args
156 .get("port")
157 .and_then(|v| v.as_u64())
158 .ok_or_else(|| anyhow::anyhow!("Missing 'port'"))? as u16;
159 let host = args.get("host").and_then(|v| v.as_str());
160 tools::system::check_port_status(port, host).await
161 }
162}