summaryrefslogtreecommitdiff
path: root/src/Agent.App
diff options
context:
space:
mode:
Diffstat (limited to 'src/Agent.App')
-rw-r--r--src/Agent.App/Configuration/SkillLoader.cs105
-rw-r--r--src/Agent.App/Program.cs4
2 files changed, 97 insertions, 12 deletions
diff --git a/src/Agent.App/Configuration/SkillLoader.cs b/src/Agent.App/Configuration/SkillLoader.cs
index 6923c12..bac1559 100644
--- a/src/Agent.App/Configuration/SkillLoader.cs
+++ b/src/Agent.App/Configuration/SkillLoader.cs
@@ -1,24 +1,109 @@
+using System.Text.RegularExpressions;
+using Agent.Core.Skills;
+using YamlDotNet.Serialization;
+using YamlDotNet.Serialization.NamingConventions;
+
namespace Agent.App.Configuration;
public static class SkillLoader
{
- public static string[] Load(string directory)
+ private static readonly Regex NamePattern = new("^[a-z0-9]([a-z0-9-]*[a-z0-9])?$", RegexOptions.Compiled);
+
+ public static IReadOnlyList<AgentSkill> Load(string directory)
{
- var paths = new List<string>();
- paths.AddRange(Directory.EnumerateFiles(directory, "*.md", SearchOption.TopDirectoryOnly));
+ if (!Directory.Exists(directory)) return [];
+
+ var skills = new List<AgentSkill>();
+ var names = new HashSet<string>(StringComparer.Ordinal);
foreach (string child in Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly))
{
var info = new DirectoryInfo(child);
if ((info.Attributes & FileAttributes.ReparsePoint) != 0) continue;
- string standardSkill = Path.Combine(info.FullName, "SKILL.md");
- if (File.Exists(standardSkill)) paths.Add(standardSkill);
+
+ string skillMd = Path.Combine(info.FullName, "SKILL.md");
+ if (!File.Exists(skillMd)) continue;
+
+ string dirName = info.Name;
+ string content = File.ReadAllText(skillMd);
+ var (frontmatter, body) = SplitFrontmatter(content);
+
+ if (frontmatter is null)
+ throw new SkillLoadException($"Skill '{dirName}': SKILL.md is missing YAML frontmatter.");
+
+ SkillFrontmatter fm = ParseFrontmatter(frontmatter);
+
+ ValidateSkill(dirName, fm, names);
+
+ skills.Add(new AgentSkill(
+ RootPath: info.FullName,
+ Name: fm.Name,
+ Description: fm.Description,
+ Instructions: body.Trim(),
+ License: fm.License,
+ Compatibility: fm.Compatibility,
+ Metadata: fm.Metadata,
+ AllowedTools: fm.AllowedTools));
+ }
+
+ return skills;
+ }
+
+ private static void ValidateSkill(string dirName, SkillFrontmatter fm, HashSet<string> names)
+ {
+ if (string.IsNullOrWhiteSpace(fm.Name))
+ throw new SkillLoadException($"Skill '{dirName}': name is required.");
+ if (fm.Name.Length < 1 || fm.Name.Length > 64)
+ throw new SkillLoadException($"Skill '{dirName}': name must be 1-64 characters.");
+ if (!NamePattern.IsMatch(fm.Name))
+ throw new SkillLoadException($"Skill '{dirName}': name must be lowercase alphanumeric with hyphens.");
+ if (!fm.Name.Equals(dirName, StringComparison.Ordinal))
+ throw new SkillLoadException($"Skill '{dirName}': name '{fm.Name}' does not match directory name.");
+ if (string.IsNullOrWhiteSpace(fm.Description))
+ throw new SkillLoadException($"Skill '{dirName}': description is required.");
+ if (fm.Description.Length > 1024)
+ throw new SkillLoadException($"Skill '{dirName}': description must be 1-1024 characters.");
+ if (!names.Add(fm.Name))
+ throw new SkillLoadException($"Duplicate skill name '{fm.Name}'.");
+ }
+
+ private static (string? Frontmatter, string Body) SplitFrontmatter(string content)
+ {
+ if (!content.StartsWith("---", StringComparison.Ordinal)) return (null, content);
+ int end = content.IndexOf("\n---", 3, StringComparison.Ordinal);
+ if (end < 0) return (null, content);
+ string frontmatter = content[3..end].Trim();
+ int bodyStart = end + 4;
+ if (bodyStart < content.Length && content[bodyStart] == '\r') bodyStart++;
+ if (bodyStart < content.Length && content[bodyStart] == '\n') bodyStart++;
+ string body = bodyStart < content.Length ? content[bodyStart..] : string.Empty;
+ return (frontmatter, body);
+ }
+
+ private static SkillFrontmatter ParseFrontmatter(string yaml)
+ {
+ try
+ {
+ var deserializer = new DeserializerBuilder()
+ .WithNamingConvention(UnderscoredNamingConvention.Instance)
+ .Build();
+ return deserializer.Deserialize<SkillFrontmatter>(yaml);
+ }
+ catch (Exception ex)
+ {
+ throw new SkillLoadException($"Malformed YAML frontmatter: {ex.Message}");
}
+ }
- return paths
- .Distinct(StringComparer.OrdinalIgnoreCase)
- .Order(StringComparer.OrdinalIgnoreCase)
- .Select(File.ReadAllText)
- .ToArray();
+ private sealed class SkillFrontmatter
+ {
+ public string Name { get; set; } = "";
+ public string Description { get; set; } = "";
+ public string? License { get; set; }
+ public string? Compatibility { get; set; }
+ public Dictionary<string, string>? Metadata { get; set; }
+ public string? AllowedTools { get; set; }
}
}
+
+public sealed class SkillLoadException(string message) : Exception(message);
diff --git a/src/Agent.App/Program.cs b/src/Agent.App/Program.cs
index 82a3f36..7839e82 100644
--- a/src/Agent.App/Program.cs
+++ b/src/Agent.App/Program.cs
@@ -85,7 +85,7 @@ internal static class Program
{
string userPrompt = File.ReadAllText(ConfigurationLoader.Resolve(baseDirectory, configuration.Agent.UserSystemPrompt));
string skillDirectory = ConfigurationLoader.Resolve(baseDirectory, configuration.Skills.Directory);
- string[] skills = SkillLoader.Load(skillDirectory);
+ var skills = SkillLoader.Load(skillDirectory);
var runtimeSettings = new AgentRuntimeSettings(userPrompt, language, skills, "Windows AppContainer, ACL, Job Object, child-process restriction, and WFP sandbox capabilities.", configuration.Agent.MaxIterations, configuration.Agent.SameFailureLimit, locale.Get("ollama.vision_unsupported"));
var visionLimits = new VisionLimits(configuration.Mcp.Images.MaxCount, configuration.Mcp.Images.MaxBytesEach, configuration.Mcp.Images.MaxTotalBytes);
await McpHost.RunStdioAsync(locale, OllamaSettingsFactory.Create(configuration.Ollama), runtimeSettings, visionLimits, CancellationToken.None,
@@ -96,7 +96,7 @@ internal static class Program
{
string userPrompt = File.ReadAllText(ConfigurationLoader.Resolve(baseDirectory, configuration.Agent.UserSystemPrompt));
string skillDirectory = ConfigurationLoader.Resolve(baseDirectory, configuration.Skills.Directory);
- string[] skills = SkillLoader.Load(skillDirectory);
+ var skills = SkillLoader.Load(skillDirectory);
var runtimeSettings = new AgentRuntimeSettings(userPrompt, language, skills, "Windows AppContainer, ACL, Job Object, child-process restriction, and WFP sandbox capabilities.", configuration.Agent.MaxIterations, configuration.Agent.SameFailureLimit, locale.Get("ollama.vision_unsupported"));
var visionLimits = new VisionLimits(configuration.Mcp.Images.MaxCount, configuration.Mcp.Images.MaxBytesEach, configuration.Mcp.Images.MaxTotalBytes);
await McpHost.RunHttpAsync(locale, OllamaSettingsFactory.Create(configuration.Ollama), runtimeSettings, visionLimits,