XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Security.Cryptography;
using System.Text;

namespace XFEWorkflow.Plugins.AgentBrowser;

public sealed record BrowserSkill(string Directory, string Instructions, string Hash)
{
    public const string DefaultDirectory = @"C:\Users\XFEstudio\Desktop\work\AI\skill\browser-debugger";
    public static async Task<BrowserSkill> LoadAsync(string directory, CancellationToken ct = default)
    {
        directory = Path.GetFullPath(directory);
        var file = Path.Combine(directory, "SKILL.md");
        if (!File.Exists(file)) throw new FileNotFoundException("找不到 browser-debugger 技能,请在 Agent 设置中检查技能目录", 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<string> 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);
    }
}