From 7a6e9785d613f6464b2ab442b73dc6cd4b3ad09e Mon Sep 17 00:00:00 2001 From: Tomi Eckert Date: Fri, 6 Mar 2026 00:52:24 +0100 Subject: [PATCH] feat: Introduce `EditTools` with file manipulation functions, replacing `RenameFile` and `CopyFile` with `MoveFile` in `ToolRegistry`. --- ToolRegistry.cs | 3 +-- Tools/EditTools.cs | 49 ++++++++++++---------------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/ToolRegistry.cs b/ToolRegistry.cs index f2d0766..87513ec 100644 --- a/ToolRegistry.cs +++ b/ToolRegistry.cs @@ -19,8 +19,7 @@ internal static class ToolRegistry AIFunctionFactory.Create(EditTools.DeleteRange, serializerOptions: jsonOptions), AIFunctionFactory.Create(EditTools.CreateFile, serializerOptions: jsonOptions), AIFunctionFactory.Create(EditTools.DeleteFile, serializerOptions: jsonOptions), - AIFunctionFactory.Create(EditTools.RenameFile, serializerOptions: jsonOptions), - AIFunctionFactory.Create(EditTools.CopyFile, serializerOptions: jsonOptions), + AIFunctionFactory.Create(EditTools.MoveFile, serializerOptions: jsonOptions), AIFunctionFactory.Create(DirTools.CreateDir, serializerOptions: jsonOptions), AIFunctionFactory.Create(DirTools.RenameDir, serializerOptions: jsonOptions), AIFunctionFactory.Create(DirTools.DeleteDir, serializerOptions: jsonOptions), diff --git a/Tools/EditTools.cs b/Tools/EditTools.cs index 5da510c..1dfea3e 100644 --- a/Tools/EditTools.cs +++ b/Tools/EditTools.cs @@ -215,14 +215,16 @@ internal static partial class EditTools } } - [Description("Rename or move a file. Auto-creates target dirs.")] - public static string RenameFile( + [Description("Move or copy a file to a new location.")] + public static string MoveFile( [Description("Current path to the file.")] string sourcePath, - [Description("New path for the file.")] string destinationPath) + [Description("New path for the file.")] string destinationPath, + [Description("If true, copy the file instead of moving it. Defaults to false.")] bool copy = false) { sourcePath = FileTools.ResolvePath(sourcePath); destinationPath = FileTools.ResolvePath(destinationPath); - Log($"Renaming file: {sourcePath} -> {destinationPath}"); + string action = copy ? "Copying" : "Moving"; + Log($"{action} file: {sourcePath} -> {destinationPath}"); if (!File.Exists(sourcePath)) return $"ERROR: Source file not found: {sourcePath}"; @@ -235,41 +237,16 @@ internal static partial class EditTools if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir); - File.Move(sourcePath, destinationPath); - return $"OK (moved to {destinationPath})"; + if (copy) + File.Copy(sourcePath, destinationPath); + else + File.Move(sourcePath, destinationPath); + + return copy ? $"OK (copied to {destinationPath})" : $"OK (moved to {destinationPath})"; } catch (Exception ex) { - return $"ERROR moving file: {ex.Message}"; - } - } - - [Description("Copy a file to a new location.")] - public static string CopyFile( - [Description("Path to the existing file.")] string sourcePath, - [Description("Path for the copy.")] string destinationPath) - { - sourcePath = FileTools.ResolvePath(sourcePath); - destinationPath = FileTools.ResolvePath(destinationPath); - Log($"Copying file: {sourcePath} -> {destinationPath}"); - - if (!File.Exists(sourcePath)) - return $"ERROR: Source file not found: {sourcePath}"; - if (File.Exists(destinationPath)) - return $"ERROR: Destination file already exists: {destinationPath}"; - - try - { - string? dir = Path.GetDirectoryName(destinationPath); - if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir)) - Directory.CreateDirectory(dir); - - File.Copy(sourcePath, destinationPath); - return $"OK (copied to {destinationPath})"; - } - catch (Exception ex) - { - return $"ERROR copying file: {ex.Message}"; + return $"ERROR {action.ToLower()} file: {ex.Message}"; } }