using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; namespace AxCopilot.Services.Agent; public class FileInfoTool : IAgentTool { public string Name => "file_info"; public string Description => "Get file or directory metadata without reading contents. Returns: size, created/modified dates, line count (files), item count (directories), encoding hint."; public ToolParameterSchema Parameters { get { ToolParameterSchema obj = new ToolParameterSchema { Properties = new Dictionary { ["path"] = new ToolProperty { Type = "string", Description = "File or directory path" } } }; int num = 1; List list = new List(num); CollectionsMarshal.SetCount(list, num); CollectionsMarshal.AsSpan(list)[0] = "path"; obj.Required = list; return obj; } } public Task ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct = default(CancellationToken)) { string text = args.GetProperty("path").GetString() ?? ""; string text2 = (Path.IsPathRooted(text) ? text : Path.Combine(context.WorkFolder, text)); if (!context.IsPathAllowed(text2)) { return Task.FromResult(ToolResult.Fail("경로 접근 차단: " + text2)); } try { if (File.Exists(text2)) { FileInfo fileInfo = new FileInfo(text2); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine("Type: File"); StringBuilder stringBuilder2 = stringBuilder; StringBuilder stringBuilder3 = stringBuilder2; StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(6, 1, stringBuilder2); handler.AppendLiteral("Path: "); handler.AppendFormatted(fileInfo.FullName); stringBuilder3.AppendLine(ref handler); stringBuilder2 = stringBuilder; StringBuilder stringBuilder4 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(15, 2, stringBuilder2); handler.AppendLiteral("Size: "); handler.AppendFormatted(FormatSize(fileInfo.Length)); handler.AppendLiteral(" ("); handler.AppendFormatted(fileInfo.Length, "N0"); handler.AppendLiteral(" bytes)"); stringBuilder4.AppendLine(ref handler); stringBuilder2 = stringBuilder; StringBuilder stringBuilder5 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(11, 1, stringBuilder2); handler.AppendLiteral("Extension: "); handler.AppendFormatted(fileInfo.Extension); stringBuilder5.AppendLine(ref handler); stringBuilder2 = stringBuilder; StringBuilder stringBuilder6 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(9, 1, stringBuilder2); handler.AppendLiteral("Created: "); handler.AppendFormatted(fileInfo.CreationTime, "yyyy-MM-dd HH:mm:ss"); stringBuilder6.AppendLine(ref handler); stringBuilder2 = stringBuilder; StringBuilder stringBuilder7 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(10, 1, stringBuilder2); handler.AppendLiteral("Modified: "); handler.AppendFormatted(fileInfo.LastWriteTime, "yyyy-MM-dd HH:mm:ss"); stringBuilder7.AppendLine(ref handler); stringBuilder2 = stringBuilder; StringBuilder stringBuilder8 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(10, 1, stringBuilder2); handler.AppendLiteral("ReadOnly: "); handler.AppendFormatted(fileInfo.IsReadOnly); stringBuilder8.AppendLine(ref handler); HashSet hashSet = new HashSet(StringComparer.OrdinalIgnoreCase) { ".cs", ".py", ".js", ".ts", ".java", ".cpp", ".c", ".h", ".xml", ".json", ".yaml", ".yml", ".md", ".txt", ".csv", ".html", ".htm", ".css", ".sql", ".sh", ".bat", ".ps1", ".config", ".ini", ".log", ".xaml" }; if (hashSet.Contains(fileInfo.Extension) && fileInfo.Length < 52428800) { int num = File.ReadLines(text2).Take(1000000).Count(); stringBuilder2 = stringBuilder; StringBuilder stringBuilder9 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(7, 2, stringBuilder2); handler.AppendLiteral("Lines: "); handler.AppendFormatted(num, "N0"); handler.AppendFormatted((num >= 1000000) ? "+" : ""); stringBuilder9.AppendLine(ref handler); } return Task.FromResult(ToolResult.Ok(stringBuilder.ToString())); } if (Directory.Exists(text2)) { DirectoryInfo directoryInfo = new DirectoryInfo(text2); FileInfo[] files = directoryInfo.GetFiles("*", SearchOption.TopDirectoryOnly); DirectoryInfo[] directories = directoryInfo.GetDirectories(); long bytes = files.Sum((FileInfo f) => f.Length); StringBuilder stringBuilder10 = new StringBuilder(); stringBuilder10.AppendLine("Type: Directory"); StringBuilder stringBuilder2 = stringBuilder10; StringBuilder stringBuilder11 = stringBuilder2; StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(6, 1, stringBuilder2); handler.AppendLiteral("Path: "); handler.AppendFormatted(directoryInfo.FullName); stringBuilder11.AppendLine(ref handler); stringBuilder2 = stringBuilder10; StringBuilder stringBuilder12 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(7, 1, stringBuilder2); handler.AppendLiteral("Files: "); handler.AppendFormatted(files.Length); stringBuilder12.AppendLine(ref handler); stringBuilder2 = stringBuilder10; StringBuilder stringBuilder13 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(16, 1, stringBuilder2); handler.AppendLiteral("Subdirectories: "); handler.AppendFormatted(directories.Length); stringBuilder13.AppendLine(ref handler); stringBuilder2 = stringBuilder10; StringBuilder stringBuilder14 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(30, 1, stringBuilder2); handler.AppendLiteral("Total Size (top-level files): "); handler.AppendFormatted(FormatSize(bytes)); stringBuilder14.AppendLine(ref handler); stringBuilder2 = stringBuilder10; StringBuilder stringBuilder15 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(9, 1, stringBuilder2); handler.AppendLiteral("Created: "); handler.AppendFormatted(directoryInfo.CreationTime, "yyyy-MM-dd HH:mm:ss"); stringBuilder15.AppendLine(ref handler); stringBuilder2 = stringBuilder10; StringBuilder stringBuilder16 = stringBuilder2; handler = new StringBuilder.AppendInterpolatedStringHandler(10, 1, stringBuilder2); handler.AppendLiteral("Modified: "); handler.AppendFormatted(directoryInfo.LastWriteTime, "yyyy-MM-dd HH:mm:ss"); stringBuilder16.AppendLine(ref handler); return Task.FromResult(ToolResult.Ok(stringBuilder10.ToString())); } return Task.FromResult(ToolResult.Fail("경로를 찾을 수 없습니다: " + text2)); } catch (Exception ex) { return Task.FromResult(ToolResult.Fail("정보 조회 오류: " + ex.Message)); } } private static string FormatSize(long bytes) { if (1 == 0) { } string result = ((bytes < 1048576) ? ((bytes >= 1024) ? $"{(double)bytes / 1024.0:F1}KB" : $"{bytes}B") : ((bytes >= 1073741824) ? $"{(double)bytes / 1073741824.0:F2}GB" : $"{(double)bytes / 1048576.0:F1}MB")); if (1 == 0) { } return result; } }