79113
Ma
mini-coding-agent / files
mini-coding-agent / src / prompt.zig
4.3 KB
1const std = @import("std"); 2const platform = @import("platform.zig"); 3const filesystem = @import("filesystem.zig"); 4const config = @import("config.zig"); 5const text = @import("text.zig"); 6 7const Skill = struct { name: []const u8, description: []const u8, path: []const u8 }; 8 9const Frontmatter = struct { name: ?[]const u8 = null, description: ?[]const u8 = null }; 10 11fn frontmatter(raw: []const u8) Frontmatter { 12 var out = Frontmatter{}; 13 if (!std.mem.startsWith(u8, raw, "---")) return out; 14 const rest = raw[3..]; 15 const end = std.mem.indexOf(u8, rest, "\n---") orelse return out; 16 var lines = std.mem.splitScalar(u8, rest[0..end], '\n'); 17 while (lines.next()) |line| { 18 const sep = std.mem.indexOfScalar(u8, line, ':') orelse continue; 19 const key = std.mem.trim(u8, line[0..sep], " \t\r"); 20 var value = std.mem.trim(u8, line[sep + 1 ..], " \t\r"); 21 value = std.mem.trim(u8, value, "\"'"); 22 if (std.mem.eql(u8, key, "name")) out.name = value; 23 if (std.mem.eql(u8, key, "description")) out.description = value; 24 } 25 return out; 26} 27 28fn discoverSkills(a: std.mem.Allocator, dirs: []const []const u8) ![]Skill { 29 var skills: std.ArrayList(Skill) = .empty; 30 for (dirs) |root| { 31 for (filesystem.listDir(a, root) catch continue) |entry| { 32 const path = filesystem.join(a, &.{ root, entry, "SKILL.md" }) catch continue; 33 if (!filesystem.fileExists(path)) continue; 34 const content = filesystem.readFileAlloc(a, path, 1 << 20) catch continue; 35 const meta = frontmatter(content); 36 const name = meta.name orelse continue; 37 try skills.append(a, .{ 38 .name = name, 39 .description = meta.description orelse "", 40 .path = path, 41 }); 42 } 43 } 44 return skills.items; 45} 46 47fn agentFiles(a: std.mem.Allocator) ![]const []const u8 { 48 var roots: std.ArrayList([]const u8) = .empty; 49 if (platform.home()) |h| { 50 const agents = filesystem.join(a, &.{ h, ".agents" }) catch ""; 51 if (agents.len > 0) try roots.append(a, agents); 52 } 53 const cwd = try std.process.currentPathAlloc(platform.io, a); 54 try roots.append(a, cwd); 55 var found: std.ArrayList([]const u8) = .empty; 56 var seen: std.ArrayList([]const u8) = .empty; 57 for (roots.items) |root| { 58 for ([_][]const u8{ "AGENTS.md", "CLAUDE.md" }) |name| { 59 const path = try filesystem.join(a, &.{ root, name }); 60 if (!filesystem.fileExists(path)) continue; 61 const real = filesystem.realPath(a, path) orelse path; 62 if (contains(seen.items, real)) continue; 63 try seen.append(a, real); 64 try found.append(a, path); 65 } 66 } 67 return found.items; 68} 69 70fn contains(haystack: []const []const u8, needle: []const u8) bool { 71 for (haystack) |item| if (std.mem.eql(u8, item, needle)) return true; 72 return false; 73} 74 75pub const Loaded = struct { agent_files: usize = 0, skills: usize = 0 }; 76 77pub const Built = struct { prompt: []const u8, loaded: Loaded }; 78 79pub fn buildSystemPrompt(a: std.mem.Allocator, cfg: *const config.Config) !Built { 80 var sections: std.ArrayList([]const u8) = .empty; 81 var loaded: Loaded = .{}; 82 83 const configured = std.mem.trim(u8, cfg.system_prompt, " \t\r\n"); 84 if (configured.len > 0) try sections.append(a, configured); 85 86 if (cfg.skills_dirs.len > 0) { 87 const skills = try discoverSkills(a, cfg.skills_dirs); 88 loaded.skills = skills.len; 89 if (skills.len > 0) { 90 var out: std.ArrayList(u8) = .empty; 91 try out.appendSlice(a, "## Skills\n"); 92 for (skills) |s| { 93 try out.print(a, "\n- {s}: {s} ({s})", .{ s.name, s.description, s.path }); 94 } 95 try sections.append(a, out.items); 96 } 97 } 98 99 if (cfg.discover_agent_files) { 100 const files = try agentFiles(a); 101 loaded.agent_files = files.len; 102 for (files) |path| { 103 const content = try filesystem.readFileAlloc(a, path, 1 << 24); 104 try sections.append(a, try std.fmt.allocPrint(a, "## {s}\n\n{s}", .{ 105 path, 106 std.mem.trim(u8, content, " \t\r\n"), 107 })); 108 } 109 } 110 111 return .{ .prompt = try text.join(a, sections.items, "\n\n"), .loaded = loaded }; 112}