add write_file tool for reliable file creation
This commit is contained in:
parent
c2be8e6930
commit
f7bcdf9b4b
34
src/tools.rs
34
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!({
|
serde_json::json!({
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
@ -507,6 +522,25 @@ pub async fn execute_tool(
|
|||||||
Err(_) => format!("timeout after {timeout_secs}s"),
|
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" => {
|
"call_gitea_api" => {
|
||||||
let method = args["method"].as_str().unwrap_or("GET").to_uppercase();
|
let method = args["method"].as_str().unwrap_or("GET").to_uppercase();
|
||||||
let path = args["path"].as_str().unwrap_or("").trim_start_matches('/');
|
let path = args["path"].as_str().unwrap_or("").trim_start_matches('/');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user