1
0

feat: Introduce EditTools with file manipulation functions, replacing RenameFile and CopyFile with MoveFile in ToolRegistry.

This commit is contained in:
2026-03-06 00:52:24 +01:00
parent 1af1665839
commit 7a6e9785d6
2 changed files with 14 additions and 38 deletions

View File

@@ -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),

View File

@@ -215,43 +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}");
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.Move(sourcePath, destinationPath);
return $"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}");
string action = copy ? "Copying" : "Moving";
Log($"{action} file: {sourcePath} -> {destinationPath}");
if (!File.Exists(sourcePath))
return $"ERROR: Source file not found: {sourcePath}";
@@ -264,12 +237,16 @@ internal static partial class EditTools
if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir))
Directory.CreateDirectory(dir);
if (copy)
File.Copy(sourcePath, destinationPath);
return $"OK (copied to {destinationPath})";
else
File.Move(sourcePath, destinationPath);
return copy ? $"OK (copied to {destinationPath})" : $"OK (moved to {destinationPath})";
}
catch (Exception ex)
{
return $"ERROR copying file: {ex.Message}";
return $"ERROR {action.ToLower()} file: {ex.Message}";
}
}