From f7bcdf9b4b0c9f6ac3740157666e2ca056775bb2 Mon Sep 17 00:00:00 2001 From: Fam Zheng Date: Fri, 10 Apr 2026 23:01:07 +0100 Subject: [PATCH] add write_file tool for reliable file creation --- src/tools.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/tools.rs b/src/tools.rs index 2adf1ec..aa9a5cd 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -233,6 +233,21 @@ pub fn discover_tools() -> serde_json::Value { } } }), + serde_json::json!({ + "type": "function", + "function": { + "name": "write_file", + "description": "将内容写入服务器上的文件。如果文件已存在会被覆盖,目录不存在会自动创建。", + "parameters": { + "type": "object", + "properties": { + "path": {"type": "string", "description": "文件的绝对路径"}, + "content": {"type": "string", "description": "要写入的完整内容"} + }, + "required": ["path", "content"] + } + } + }), serde_json::json!({ "type": "function", "function": { @@ -507,6 +522,25 @@ pub async fn execute_tool( Err(_) => format!("timeout after {timeout_secs}s"), } } + "write_file" => { + let path_str = args["path"].as_str().unwrap_or(""); + let content = args["content"].as_str().unwrap_or(""); + if path_str.is_empty() { + return "Error: path is required".to_string(); + } + let path = Path::new(path_str); + if let Some(parent) = path.parent() { + if !parent.exists() { + if let Err(e) = std::fs::create_dir_all(parent) { + return format!("Failed to create directory: {e}"); + } + } + } + match std::fs::write(path, content) { + Ok(_) => format!("Written {} bytes to {path_str}", content.len()), + Err(e) => format!("Failed to write {path_str}: {e}"), + } + } "call_gitea_api" => { let method = args["method"].as_str().unwrap_or("GET").to_uppercase(); let path = args["path"].as_str().unwrap_or("").trim_start_matches('/');