Skip to main content

steer_tools/tools/
edit.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5use crate::ToolSpec;
6use crate::error::{ToolExecutionError, WorkspaceOpError};
7use crate::result::{EditResult, MultiEditResult};
8
9pub const EDIT_TOOL_NAME: &str = "edit_file";
10
11pub struct EditToolSpec;
12
13impl ToolSpec for EditToolSpec {
14    type Params = EditParams;
15    type Result = EditResult;
16    type Error = EditError;
17
18    const NAME: &'static str = EDIT_TOOL_NAME;
19    const DISPLAY_NAME: &'static str = "Edit File";
20
21    fn execution_error(error: Self::Error) -> ToolExecutionError {
22        ToolExecutionError::Edit(error)
23    }
24}
25
26#[derive(Deserialize, Serialize, Debug, JsonSchema, Clone, Error)]
27#[serde(tag = "code", content = "details", rename_all = "snake_case")]
28pub enum EditError {
29    #[error("{0}")]
30    Workspace(WorkspaceOpError),
31}
32
33#[derive(Deserialize, Serialize, Debug, JsonSchema, Clone)]
34pub struct SingleEditOperation {
35    /// The exact string to find and replace.
36    pub old_string: String,
37    /// The string to replace `old_string` with.
38    pub new_string: String,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
42pub struct EditParams {
43    /// The absolute path to the file to edit
44    pub file_path: String,
45    /// The exact string to find and replace. If empty, the file will be created.
46    pub old_string: String,
47    /// The string to replace `old_string` with.
48    pub new_string: String,
49}
50
51pub mod multi_edit {
52    use super::{
53        Deserialize, Error, JsonSchema, MultiEditResult, Serialize, SingleEditOperation,
54        ToolExecutionError, ToolSpec, WorkspaceOpError,
55    };
56
57    pub const MULTI_EDIT_TOOL_NAME: &str = "multi_edit";
58
59    pub struct MultiEditToolSpec;
60
61    impl ToolSpec for MultiEditToolSpec {
62        type Params = MultiEditParams;
63        type Result = MultiEditResult;
64        type Error = MultiEditError;
65
66        const NAME: &'static str = MULTI_EDIT_TOOL_NAME;
67        const DISPLAY_NAME: &'static str = "Multi Edit";
68
69        fn execution_error(error: Self::Error) -> ToolExecutionError {
70            ToolExecutionError::MultiEdit(error)
71        }
72    }
73
74    #[derive(Deserialize, Serialize, Debug, JsonSchema, Clone, Error)]
75    #[serde(tag = "code", content = "details", rename_all = "snake_case")]
76    pub enum MultiEditError {
77        #[error("{0}")]
78        Workspace(WorkspaceOpError),
79    }
80
81    #[derive(Deserialize, Serialize, Debug, JsonSchema)]
82    pub struct MultiEditParams {
83        /// The absolute path to the file to edit.
84        pub file_path: String,
85        /// A list of edit operations to apply sequentially.
86        pub edits: Vec<SingleEditOperation>,
87    }
88}