87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
|
|
namespace AnchorCli.Tools;
|
|
|
|
/// <summary>
|
|
/// Directory manipulation tools exposed to the LLM as AIFunctions.
|
|
/// </summary>
|
|
internal static class DirTools
|
|
{
|
|
public static Action<string> Log { get; set; } = Console.WriteLine;
|
|
|
|
[Description("Rename or move a directory. Can move a directory to a new location.")]
|
|
public static string RenameDir(
|
|
[Description("Current path to the directory.")] string sourcePath,
|
|
[Description("New path for the directory.")] string destinationPath)
|
|
{
|
|
sourcePath = ResolvePath(sourcePath);
|
|
destinationPath = ResolvePath(destinationPath);
|
|
Log($"Renaming/moving directory: {sourcePath} -> {destinationPath}");
|
|
|
|
if (!Directory.Exists(sourcePath))
|
|
return $"ERROR: Directory not found: {sourcePath}";
|
|
|
|
try
|
|
{
|
|
var destDir = Path.GetDirectoryName(destinationPath);
|
|
if (!string.IsNullOrEmpty(destDir) && !Directory.Exists(destDir))
|
|
{
|
|
Directory.CreateDirectory(destDir);
|
|
}
|
|
|
|
Directory.Move(sourcePath, destinationPath);
|
|
return $"OK: Directory moved from '{sourcePath}' to '{destinationPath}'";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return $"ERROR moving directory '{sourcePath}': {ex.Message}";
|
|
}
|
|
}
|
|
|
|
[Description("Delete a directory and all its contents permanently.")]
|
|
public static string DeleteDir(
|
|
[Description("Path to the directory to delete.")] string path,
|
|
[Description("If true, delete recursively. Defaults to true.")] bool recursive = true)
|
|
{
|
|
path = ResolvePath(path);
|
|
Log($"Deleting directory: {path}");
|
|
|
|
if (!Directory.Exists(path))
|
|
return $"ERROR: Directory not found: {path}";
|
|
|
|
try
|
|
{
|
|
Directory.Delete(path, recursive);
|
|
return $"OK: Directory deleted: '{path}'";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return $"ERROR deleting directory '{path}': {ex.Message}";
|
|
}
|
|
}
|
|
[Description("Create a new directory. Creates parent directories if they don't exist. Returns OK on success, or an error message if the directory already exists or creation fails.")]
|
|
public static string CreateDir(
|
|
[Description("Path to the directory to create.")] string path)
|
|
{
|
|
path = ResolvePath(path);
|
|
Log($"Creating directory: {path}");
|
|
|
|
if (Directory.Exists(path))
|
|
return $"ERROR: Directory already exists: {path}";
|
|
|
|
try
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
return $"OK (created {path})";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return $"ERROR creating directory '{path}': {ex.Message}";
|
|
}
|
|
}
|
|
|
|
internal static string ResolvePath(string path) =>
|
|
Path.IsPathRooted(path) ? path : Path.GetFullPath(path, Environment.CurrentDirectory);
|
|
}
|