66 lines
2.2 KiB
C#
66 lines
2.2 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($" ● rename_dir: {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("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($" ● create_dir: {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);
|
|
}
|