Skip to main content

agent_engine/tools/shell/
end.rs

1//! `shell_end` tool — close an interactive shell session.
2
3use crate::tools::{Tool, ToolContext};
4use crate::{Result, RuntimeError};
5use serde_json::{json, Value};
6
7pub struct ShellEndTool;
8
9#[async_trait::async_trait]
10impl Tool for ShellEndTool {
11    fn origin(&self) -> crate::tools::ToolOrigin {
12        crate::tools::ToolOrigin::Builtin
13    }
14
15    fn name(&self) -> &str {
16        "shell_end"
17    }
18
19    fn description(&self) -> &str {
20        "Close an interactive shell session and clean up resources. Returns the final output if any."
21    }
22
23    fn parameters(&self) -> Value {
24        json!({
25            "type": "object",
26            "properties": {
27                "session_id": {
28                    "type": "string",
29                    "description": "Session ID to close"
30                }
31            },
32            "required": ["session_id"]
33        })
34    }
35
36    async fn execute(&self, params: Value, ctx: ToolContext) -> Result<String> {
37        let mgr = ctx
38            .capabilities
39            .session_manager
40            .as_ref()
41            .ok_or_else(|| RuntimeError::Tool("Shell sessions not available".into()))?;
42
43        let session_id = params["session_id"]
44            .as_str()
45            .ok_or_else(|| RuntimeError::Tool("Missing session_id parameter".into()))?;
46
47        let output = mgr.close_session(session_id).await?;
48
49        if output.is_empty() {
50            Ok(format!("[Session {} closed]", session_id))
51        } else {
52            Ok(format!("{}\n[Session {} closed]", output, session_id))
53        }
54    }
55}