79113
Ma
mini-coding-agent / files
mini-coding-agent / src / config.zig
9.1 KB
1const std = @import("std"); 2const platform = @import("platform.zig"); 3const filesystem = @import("filesystem.zig"); 4 5pub const ToolName = enum { edit, read, bash }; 6 7pub const Notifications = enum { off, osc9, bell, both }; 8 9pub const Api = enum { 10 @"openai-completions", 11 @"openai-responses", 12 @"anthropic-messages", 13 @"google-generative-ai", 14}; 15 16pub const CustomModel = struct { 17 id: []const u8, 18 name: ?[]const u8 = null, 19 api: Api, 20 baseUrl: []const u8, 21 context: u64, 22 maxOutput: u64, 23 images: bool, 24 reasoning: bool, 25 effort: []const []const u8 = &.{}, 26 cost: [3]f64, 27}; 28 29pub const CustomProvider = struct { 30 id: []const u8, 31 name: ?[]const u8 = null, 32 models: []const CustomModel, 33 envKeys: []const []const u8 = &.{}, 34 headers: ?std.json.ArrayHashMap([]const u8) = null, 35}; 36 37pub const Colorscheme = struct { 38 comment: ?[]const u8 = null, 39 prompt: ?[]const u8 = null, 40 accent: ?[]const u8 = null, 41 err: ?[]const u8 = null, 42 warn: ?[]const u8 = null, 43 code: ?[]const u8 = null, 44 output: ?[]const u8 = null, 45 diff_add: ?[]const u8 = null, 46 diff_delete: ?[]const u8 = null, 47 inline_code: ?[]const u8 = null, 48 link: ?[]const u8 = null, 49 uri: ?[]const u8 = null, 50 heading1: ?[]const u8 = null, 51 heading2: ?[]const u8 = null, 52 heading3: ?[]const u8 = null, 53 heading4: ?[]const u8 = null, 54 heading5: ?[]const u8 = null, 55 heading6: ?[]const u8 = null, 56}; 57 58pub fn parseHex(text: []const u8) ?u24 { 59 const body = if (text.len > 0 and text[0] == '#') text[1..] else text; 60 if (body.len != 6) return null; 61 return std.fmt.parseInt(u24, body, 16) catch null; 62} 63 64pub const Config = struct { 65 path: []const u8, 66 sessions_dir: []const u8, 67 system_prompt: []const u8, 68 discover_agent_files: bool, 69 skills_dirs: []const []const u8, 70 tools: []const ToolName, 71 provider: ?[]const u8, 72 model: ?[]const u8, 73 thinking_effort: ?[]const u8, 74 program_status: bool, 75 herdr: bool, 76 notifications: Notifications, 77 retries: usize, 78 retry_base_ms: u64, 79 stream_idle_timeout_ms: u64, 80 show_images: bool, 81 custom_providers: []const CustomProvider, 82 snapshot_ignore_dirs: []const []const u8, 83 snapshot_uses_gitignore: bool, 84 snapshot_diffs_in_context: bool, 85 history_size: usize, 86 max_diff_lines: usize, 87 colorscheme: Colorscheme, 88}; 89 90const File = struct { 91 historySize: ?usize = null, 92 maxDiffLines: ?usize = null, 93 sessionsDir: ?[]const u8 = null, 94 systemPrompt: ?[]const u8 = null, 95 discoverAgentFiles: ?bool = null, 96 skillsDirs: ?[]const []const u8 = null, 97 tools: ?[]const ToolName = null, 98 provider: ?[]const u8 = null, 99 model: ?[]const u8 = null, 100 thinkingEffort: ?[]const u8 = null, 101 customProviders: ?[]const CustomProvider = null, 102 programStatus: ?bool = null, 103 herdr: ?bool = null, 104 notifications: ?Notifications = null, 105 retries: ?usize = null, 106 retryBaseMs: ?u64 = null, 107 streamIdleTimeoutMs: ?u64 = null, 108 showImages: ?bool = null, 109 snapshotIgnoreDirs: ?[]const []const u8 = null, 110 snapshotUsesGitignore: ?bool = null, 111 snapshotDiffsInContext: ?bool = null, 112 colorscheme: ?Colorscheme = null, 113}; 114 115fn xdg(key: []const u8) ?[]const u8 { 116 const value = platform.getEnv(key) orelse return null; 117 return if (value.len == 0) null else value; 118} 119 120pub fn configDir(a: std.mem.Allocator) []const u8 { 121 const base = xdg("XDG_CONFIG_HOME") orelse 122 filesystem.join(a, &.{ platform.home() orelse "", ".config" }) catch ""; 123 return filesystem.join(a, &.{ base, "mini-coding-agent" }) catch "mini-coding-agent"; 124} 125 126pub fn configPath(a: std.mem.Allocator) []const u8 { 127 return filesystem.join(a, &.{ configDir(a), "config.json" }) catch "config.json"; 128} 129 130fn stateDir(a: std.mem.Allocator) []const u8 { 131 return xdg("XDG_STATE_HOME") orelse blk: { 132 const home = platform.home() orelse return ""; 133 break :blk filesystem.join(a, &.{ home, ".local/state" }) catch ""; 134 }; 135} 136 137fn stateFile(a: std.mem.Allocator, name: []const u8) []const u8 { 138 const state = stateDir(a); 139 if (state.len == 0) return name; 140 return filesystem.join(a, &.{ state, "mini-coding-agent", name }) catch name; 141} 142 143pub fn historyPath(a: std.mem.Allocator) []const u8 { 144 return stateFile(a, "history"); 145} 146 147fn sessionsDir(a: std.mem.Allocator) []const u8 { 148 return stateFile(a, "sessions"); 149} 150pub fn load(a: std.mem.Allocator, override: ?[]const u8, no_config: bool) !Config { 151 var cfg = try defaults(a); 152 if (no_config) return cfg; 153 154 try readInto(a, configPath(a), false, &cfg); 155 if (override) |path| { 156 cfg.path = path; 157 try readInto(a, path, true, &cfg); 158 } 159 return cfg; 160} 161 162fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config) !void { 163 const text = filesystem.readFileAlloc(a, path, 1 << 20) catch |e| switch (e) { 164 error.FileNotFound => { 165 if (!required) return; 166 platform.printErr("config {s}: no such file\n", .{path}); 167 return error.InvalidConfig; 168 }, 169 else => { 170 platform.printErr("config {s}: {s}\n", .{ path, @errorName(e) }); 171 return error.InvalidConfig; 172 }, 173 }; 174 const file = std.json.parseFromSliceLeaky(File, a, text, .{}) catch |e| { 175 platform.printErr("config {s}: {s}\n", .{ path, @errorName(e) }); 176 return error.InvalidConfig; 177 }; 178 179 if (file.sessionsDir) |v| cfg.sessions_dir = v; 180 if (file.systemPrompt) |v| cfg.system_prompt = v; 181 if (file.provider) |v| cfg.provider = v; 182 if (file.model) |v| cfg.model = v; 183 if (file.discoverAgentFiles) |v| cfg.discover_agent_files = v; 184 if (file.skillsDirs) |v| cfg.skills_dirs = v; 185 if (file.tools) |v| cfg.tools = v; 186 if (file.customProviders) |v| cfg.custom_providers = v; 187 if (file.thinkingEffort) |v| cfg.thinking_effort = v; 188 if (file.programStatus) |v| cfg.program_status = v; 189 if (file.herdr) |v| cfg.herdr = v; 190 if (file.notifications) |v| cfg.notifications = v; 191 if (file.retries) |v| cfg.retries = v; 192 if (file.retryBaseMs) |v| cfg.retry_base_ms = v; 193 if (file.streamIdleTimeoutMs) |v| cfg.stream_idle_timeout_ms = v; 194 if (file.showImages) |v| cfg.show_images = v; 195 if (file.snapshotIgnoreDirs) |v| cfg.snapshot_ignore_dirs = v; 196 if (file.snapshotUsesGitignore) |v| cfg.snapshot_uses_gitignore = v; 197 if (file.snapshotDiffsInContext) |v| cfg.snapshot_diffs_in_context = v; 198 if (file.historySize) |v| cfg.history_size = v; 199 if (file.maxDiffLines) |v| cfg.max_diff_lines = v; 200 if (file.colorscheme) |scheme| { 201 inline for (@typeInfo(Colorscheme).@"struct".field_names) |name| { 202 if (@field(scheme, name)) |hex| { 203 if (parseHex(hex) == null) { 204 platform.printErr("config {s}: colorscheme.{s}: expected \"#rrggbb\"\n", .{ path, name }); 205 return error.InvalidConfig; 206 } 207 @field(cfg.colorscheme, name) = hex; 208 } 209 } 210 } 211} 212 213pub const Update = struct { 214 provider: ?[]const u8 = null, 215 model: ?[]const u8 = null, 216 thinking_effort: ?[]const u8 = null, 217}; 218 219pub fn save(a: std.mem.Allocator, cfg: *const Config, update: Update) !void { 220 var arena = std.heap.ArenaAllocator.init(a); 221 defer arena.deinit(); 222 const tmp = arena.allocator(); 223 224 const path = cfg.path; 225 var root: std.json.Value = .{ .object = std.json.ObjectMap.empty }; 226 if (filesystem.readFileAlloc(tmp, path, 1 << 20)) |text| { 227 const parsed = std.json.parseFromSliceLeaky(std.json.Value, tmp, text, .{}) catch 228 return error.InvalidConfig; 229 if (parsed != .object) return error.InvalidConfig; 230 root = parsed; 231 } else |_| {} 232 233 if (update.provider) |v| root.object.put(tmp, "provider", .{ .string = v }) catch return error.OutOfMemory; 234 if (update.model) |v| root.object.put(tmp, "model", .{ .string = v }) catch return error.OutOfMemory; 235 if (update.thinking_effort) |v| root.object.put(tmp, "thinkingEffort", .{ .string = v }) catch return error.OutOfMemory; 236 237 const json_text = std.json.Stringify.valueAlloc(tmp, root, .{ .whitespace = .indent_2 }) catch 238 return error.OutOfMemory; 239 const out = try std.fmt.allocPrint(tmp, "{s}\n", .{json_text}); 240 const temp = try std.fmt.allocPrint(tmp, "{s}.tmp", .{path}); 241 try filesystem.writeFile(temp, out); 242 try std.Io.Dir.cwd().rename(temp, std.Io.Dir.cwd(), path, platform.io); 243} 244 245fn defaults(a: std.mem.Allocator) !Config { 246 return .{ 247 .path = configPath(a), 248 .sessions_dir = sessionsDir(a), 249 .system_prompt = "", 250 .discover_agent_files = true, 251 .skills_dirs = &.{}, 252 .tools = &.{ .edit, .read, .bash }, 253 .provider = null, 254 .model = null, 255 .thinking_effort = null, 256 .program_status = false, 257 .herdr = false, 258 .notifications = .off, 259 .retries = 3, 260 .retry_base_ms = 1000, 261 .stream_idle_timeout_ms = 120_000, 262 .show_images = false, 263 .custom_providers = &.{}, 264 .snapshot_ignore_dirs = &.{}, 265 .snapshot_uses_gitignore = true, 266 .snapshot_diffs_in_context = false, 267 .history_size = 100, 268 .max_diff_lines = 2000, 269 .colorscheme = .{}, 270 }; 271}