1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(deny_unknown_fields)]
6pub struct WriteParams {
7 pub path: String,
8 pub content: String,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct EditParams {
14 pub path: String,
15 pub old_string: String,
16 pub new_string: String,
17 #[serde(default, skip_serializing_if = "Option::is_none")]
18 pub replace_all: Option<bool>,
19 #[serde(default, skip_serializing_if = "Option::is_none")]
20 pub dry_run: Option<bool>,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(deny_unknown_fields)]
25pub struct EditSpec {
26 pub old_string: String,
27 pub new_string: String,
28 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub replace_all: Option<bool>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33#[serde(deny_unknown_fields)]
34pub struct MultiEditParams {
35 pub path: String,
36 pub edits: Vec<EditSpec>,
37 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub dry_run: Option<bool>,
39}
40
41#[derive(Debug, Clone, thiserror::Error)]
42pub enum WriteParseError {
43 #[error("{0}")]
44 Message(String),
45}
46
47pub fn safe_parse_write_params(input: &Value) -> Result<WriteParams, WriteParseError> {
48 let parsed: WriteParams = serde_json::from_value(input.clone())
49 .map_err(|e| WriteParseError::Message(e.to_string()))?;
50 if parsed.path.is_empty() {
51 return Err(WriteParseError::Message("path must not be empty".to_string()));
52 }
53 Ok(parsed)
54}
55
56pub fn safe_parse_edit_params(input: &Value) -> Result<EditParams, WriteParseError> {
57 let parsed: EditParams = serde_json::from_value(input.clone())
58 .map_err(|e| WriteParseError::Message(e.to_string()))?;
59 if parsed.path.is_empty() {
60 return Err(WriteParseError::Message("path must not be empty".to_string()));
61 }
62 if parsed.old_string.is_empty() {
63 return Err(WriteParseError::Message(
64 "old_string must not be empty".to_string(),
65 ));
66 }
67 Ok(parsed)
68}
69
70pub fn safe_parse_multi_edit_params(input: &Value) -> Result<MultiEditParams, WriteParseError> {
71 let parsed: MultiEditParams = serde_json::from_value(input.clone())
72 .map_err(|e| WriteParseError::Message(e.to_string()))?;
73 if parsed.path.is_empty() {
74 return Err(WriteParseError::Message("path must not be empty".to_string()));
75 }
76 if parsed.edits.is_empty() {
77 return Err(WriteParseError::Message(
78 "edits must contain at least one edit".to_string(),
79 ));
80 }
81 for (i, e) in parsed.edits.iter().enumerate() {
82 if e.old_string.is_empty() {
83 return Err(WriteParseError::Message(format!(
84 "edits[{}].old_string must not be empty",
85 i
86 )));
87 }
88 }
89 Ok(parsed)
90}
91
92pub const WRITE_TOOL_NAME: &str = "write";
93pub const EDIT_TOOL_NAME: &str = "edit";
94pub const MULTIEDIT_TOOL_NAME: &str = "multiedit";
95
96pub const WRITE_TOOL_DESCRIPTION: &str = "Create a new file, or overwrite an existing file.\n\nUsage:\n- New file (path does not exist): call Write directly. No prior Read is required.\n- Existing file: you must Read it first in this session, or Write fails with NOT_READ_THIS_SESSION.\n- Prefer Edit or MultiEdit for targeted changes to existing files.\n- Write is atomic: bytes land via a temporary file + rename.\n- Path must be absolute. If relative, it resolves against the session cwd.";
97
98pub const EDIT_TOOL_DESCRIPTION: &str = "Replace exactly one occurrence of old_string with new_string in a file.\n\nUsage:\n- The file must have been Read first in this session.\n- old_string must match the file content exactly, character for character, including whitespace and indentation.\n- If old_string appears more than once, the call fails with OLD_STRING_NOT_UNIQUE.\n- If old_string does not match, the call fails with OLD_STRING_NOT_FOUND and returns the top fuzzy candidates.\n- Use dry_run: true to preview the unified diff without writing.\n- CRLF is normalized to LF on both sides.";
99
100pub const MULTIEDIT_TOOL_DESCRIPTION: &str = "Apply a sequence of edits to a single file atomically.\n\nUsage:\n- edits is an ordered list of { old_string, new_string, replace_all? } objects.\n- Edits apply sequentially in memory: later edits see the output of earlier edits.\n- If any edit fails, none of the edits are applied and the file is untouched.\n- The file must have been Read first in this session.\n- Use dry_run: true to preview the final unified diff without writing.";