llm_chain_tools/tools/
exit.rs

1use crate::description::{Describe, Format, ToolDescription};
2use crate::tool::{gen_invoke_function, Tool};
3use serde::{Deserialize, Serialize};
4
5/// A tool that exits the program with the given status code.
6pub struct ExitTool {}
7
8impl ExitTool {
9    pub fn new() -> Self {
10        ExitTool {}
11    }
12}
13
14impl Default for ExitTool {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20#[derive(Serialize, Deserialize)]
21pub struct ExitToolInput {
22    status_code: i32,
23}
24
25#[derive(Serialize, Deserialize)]
26pub struct ExitToolOutput {}
27
28impl Describe for ExitToolInput {
29    fn describe() -> Format {
30        vec![("status_code", "<integer> UNIX status to exit with").into()].into()
31    }
32}
33
34impl Describe for ExitToolOutput {
35    fn describe() -> Format {
36        vec![].into()
37    }
38}
39
40impl ExitTool {
41    fn invoke_typed(&self, input: &ExitToolInput) -> Result<ExitToolOutput, String> {
42        std::process::exit(input.status_code);
43    }
44}
45
46impl Tool for ExitTool {
47    gen_invoke_function!();
48    fn description(&self) -> ToolDescription {
49        ToolDescription::new(
50            "ExitTool",
51            "Exits the program with the given status code",
52            "Use this when your task is complete and you want to exit the program.",
53            ExitToolInput::describe(),
54            ExitToolOutput::describe(),
55        )
56    }
57}