feat: Introduce EditTools with file manipulation functions, replacing RenameFile and CopyFile with MoveFile in ToolRegistry.
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user