Skip to main content

ai_chain/tools/tools/
exit.rs

1use crate::tools::description::{Describe, Format, ToolDescription};
2use crate::tools::tool::{Tool, ToolError};
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use thiserror::Error;
6
7/// A tool that exits the program with the given status code.
8pub struct ExitTool {}
9
10impl ExitTool {
11    /// Creates a new `ExitTool`.
12    pub fn new() -> Self {
13        ExitTool {}
14    }
15}
16
17impl Default for ExitTool {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23/// Represents the input for `ExitTool`.
24#[derive(Serialize, Deserialize)]
25pub struct ExitToolInput {
26    status_code: i32,
27}
28
29/// Represents the output for `ExitTool`.
30#[derive(Serialize, Deserialize)]
31pub struct ExitToolOutput {}
32
33impl Describe for ExitToolInput {
34    fn describe() -> Format {
35        vec![("status_code", "<integer> UNIX status to exit with").into()].into()
36    }
37}
38
39impl Describe for ExitToolOutput {
40    fn describe() -> Format {
41        vec![].into()
42    }
43}
44
45#[derive(Debug, Error)]
46pub enum ExitToolError {
47    #[error(transparent)]
48    YamlError(#[from] serde_yaml::Error),
49    #[error(transparent)]
50    IOError(#[from] std::io::Error),
51}
52
53impl ToolError for ExitToolError {}
54
55#[async_trait]
56impl Tool for ExitTool {
57    type Input = ExitToolInput;
58    type Output = ExitToolOutput;
59    type Error = ExitToolError;
60    /// Invokes the `ExitTool` with the provided input.
61    async fn invoke_typed(&self, input: &ExitToolInput) -> Result<ExitToolOutput, ExitToolError> {
62        std::process::exit(input.status_code);
63    }
64    /// Returns a `ToolDescription` for `ExitTool`.
65    fn description(&self) -> ToolDescription {
66        ToolDescription::new(
67            "ExitTool",
68            "Exits the program with the given status code",
69            "Use this when your task is complete and you want to exit the program.",
70            ExitToolInput::describe(),
71            ExitToolOutput::describe(),
72        )
73    }
74}