using System.Security.Cryptography; using System.Text; namespace XFEWorkflow.Plugins.AgentBrowser; public sealed record BrowserSkill(string Directory, string Instructions, string Hash) { public static string PackageDirectory => Path.GetDirectoryName(typeof(BrowserSkill).Assembly.Location)!; public static string DefaultDirectory => Path.Combine(PackageDirectory, "Skill"); public static async Task LoadAsync(string directory, CancellationToken ct = default) { directory = Path.GetFullPath(directory); var file = Path.Combine(directory, "SKILL.md"); if (!File.Exists(file)) throw new FileNotFoundException("插件包缺少内置浏览器技能,请重新安装插件", file); if (!File.Exists(Path.Combine(directory, "scripts", "browser-session.mjs"))) throw new InvalidDataException("技能缺少 scripts/browser-session.mjs"); if (new FileInfo(file).Length > 256 * 1024) throw new InvalidDataException("SKILL.md 超过 256 KiB"); var text = await File.ReadAllTextAsync(file, ct); if (string.IsNullOrWhiteSpace(text)) throw new InvalidDataException("browser-debugger 技能内容为空"); return new(directory, text, Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(text)))); } public async Task ReadReferenceAsync(string relativePath, CancellationToken ct) { var parts = relativePath.Replace('\\', '/').Split('/'); if (parts.Length != 2 || parts[0] != "references" || parts[1] is "" or "." or ".." || !parts[1].EndsWith(".md", StringComparison.OrdinalIgnoreCase)) throw new ArgumentException("只允许读取技能 references 目录的 Markdown 文件"); var file = Path.Combine(Directory, parts[0], parts[1]); if ((File.GetAttributes(file) & FileAttributes.ReparsePoint) != 0 || (File.GetAttributes(Path.GetDirectoryName(file)!) & FileAttributes.ReparsePoint) != 0) throw new IOException("技能引用不允许符号链接"); if (new FileInfo(file).Length > 256 * 1024) throw new IOException("技能引用超过 256 KiB"); return await File.ReadAllTextAsync(file, ct); } }