using System.IO; using System.Text; using System.Text.Json; namespace AxCopilot.Services.Agent; /// /// 배치파일(.bat) / PowerShell 스크립트(.ps1)를 생성하는 내장 스킬. /// 파일 생성만 수행하며 자동 실행하지 않습니다. /// 시스템 수준 명령(레지스트리, 서비스, 드라이버 등)은 차단합니다. /// public class BatchSkill : IAgentTool { public string Name => "script_create"; public string Description => "Create a batch (.bat) or PowerShell (.ps1) script file. The script is ONLY created, NOT executed. System-level commands are blocked."; public ToolParameterSchema Parameters => new() { Properties = new() { ["path"] = new() { Type = "string", Description = "Output file path (.bat or .ps1). Relative to work folder." }, ["content"] = new() { Type = "string", Description = "Script content. Each line should have Korean comments explaining the command." }, ["description"] = new() { Type = "string", Description = "Brief description of what this script does." }, }, Required = ["path", "content"] }; // 시스템 수준 명령 차단 목록 private static readonly string[] BlockedCommands = [ "reg ", "reg.exe", "regedit", "sc ", "sc.exe", "net stop", "net start", "net user", "bcdedit", "diskpart", "format ", "shutdown", "schtasks", "wmic", "powercfg", "Set-Service", "Stop-Service", "Start-Service", "New-Service", "Remove-Service", "Set-ItemProperty.*HKLM", "Set-ItemProperty.*HKCU", "Remove-Item.*-Recurse.*-Force", ]; public async Task ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct) { var path = args.GetProperty("path").GetString() ?? ""; var content = args.GetProperty("content").GetString() ?? ""; var desc = args.TryGetProperty("description", out var d) ? d.GetString() ?? "" : ""; var fullPath = FileReadTool.ResolvePath(path, context.WorkFolder); if (context.ActiveTab == "Cowork") fullPath = AgentContext.EnsureTimestampedPath(fullPath); // 확장자 검증 var ext = Path.GetExtension(fullPath).ToLowerInvariant(); if (ext != ".bat" && ext != ".ps1" && ext != ".cmd") return ToolResult.Fail("지원하는 스크립트 형식: .bat, .cmd, .ps1"); if (!context.IsPathAllowed(fullPath)) return ToolResult.Fail($"경로 접근 차단: {fullPath}"); // 시스템 명령 차단 검사 var contentLower = content.ToLowerInvariant(); foreach (var blocked in BlockedCommands) { if (contentLower.Contains(blocked.ToLowerInvariant())) return ToolResult.Fail($"시스템 수준 명령이 포함되어 차단됨: {blocked.Trim()}"); } if (!await context.CheckWritePermissionAsync(Name, fullPath)) return ToolResult.Fail($"쓰기 권한 거부: {fullPath}"); try { var dir = Path.GetDirectoryName(fullPath); if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir); // 파일 상단에 설명 주석 추가 var sb = new StringBuilder(); if (!string.IsNullOrEmpty(desc)) { var commentPrefix = ext == ".ps1" ? "#" : "REM"; sb.AppendLine($"{commentPrefix} === {desc} ==="); sb.AppendLine($"{commentPrefix} 이 스크립트는 AX Copilot에 의해 생성되었습니다."); sb.AppendLine($"{commentPrefix} 실행 전 내용을 반드시 확인하세요."); sb.AppendLine(); } sb.Append(content); await File.WriteAllTextAsync(fullPath, sb.ToString(), new UTF8Encoding(false), ct); return ToolResult.Ok( $"스크립트 파일 생성 완료: {fullPath}\n형식: {ext}, 설명: {(string.IsNullOrEmpty(desc) ? "(없음)" : desc)}\n⚠ 자동 실행되지 않습니다. 내용을 확인한 후 직접 실행하세요.", fullPath); } catch (Exception ex) { return ToolResult.Fail($"스크립트 생성 실패: {ex.Message}"); } } }