xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / tui / status.zig

5.6 KB
1const std = @import("std");
2const builtin = @import("builtin");
3const platform = @import("../platform.zig");
4const time = @import("../time.zig");
5const config = @import("../config.zig");
6
7const app = "mini-coding-agent";
8const max_msg = 256;
9
10const herdr_source = "mini";
11const herdr_agent = "mini";
12const herdr_report_timeout_ms = 2000;
13const herdr_release_timeout_ms = 1000;
14
15var program_status = false;
16var notify_mode: config.Notifications = .off;
17var herdr_on = false;
18var herdr_bin: []const u8 = "";
19var herdr_pane: []const u8 = "";
20
21var lock: std.Io.Mutex = .init;
22var pending: ?Job = null;
23var pending_msg: [max_msg]u8 = undefined;
24var seq: u64 = 0;
25
26const Job = struct {
27    state: []const u8,
28    msg_len: usize,
29    seq: u64,
30};
31
32pub fn init(cfg: *const config.Config) void {
33    program_status = cfg.program_status;
34    notify_mode = cfg.notifications;
35    if (!cfg.herdr) return;
36    if (!std.mem.eql(u8, platform.getEnv("HERDR_ENV") orelse "", "1")) return;
37    herdr_bin = platform.getEnv("HERDR_BIN_PATH") orelse return;
38    herdr_pane = platform.getEnv("HERDR_PANE_ID") orelse return;
39    if (herdr_bin.len == 0 or herdr_pane.len == 0) return;
40
41    seq = @intCast(@max(0, time.nowMs()));
42    herdr_on = true;
43
44    const worker = std.Thread.spawn(.{}, loop, .{}) catch {
45        herdr_on = false;
46        return;
47    };
48    worker.detach();
49}
50
51pub fn report(state: []const u8, kind: ?[]const u8, msg: ?[]const u8) void {
52    if (program_status) osc7501(state, kind, msg);
53    if (!herdr_on) return;
54
55    const mapped = herdrState(state);
56    const text: []const u8 = if (std.mem.eql(u8, mapped, "blocked") and msg != null)
57        msg.?[0..@min(msg.?.len, max_msg)]
58    else
59        "";
60
61    lock.lockUncancelable(platform.io);
62    defer lock.unlock(platform.io);
63    seq += 1;
64    if (text.len > 0) @memcpy(pending_msg[0..text.len], text);
65    pending = .{ .state = mapped, .msg_len = text.len, .seq = seq };
66}
67
68pub fn notify(message: []const u8) void {
69    if (notify_mode == .osc9 or notify_mode == .both) {
70        var buf: [max_msg + 8]u8 = undefined;
71        const out = std.fmt.bufPrint(&buf, "\x1b]9;{s}\x07", .{message[0..@min(message.len, max_msg)]}) catch return;
72        platform.writeOut(out);
73    }
74    if (notify_mode == .bell or notify_mode == .both) platform.writeOut("\x07");
75}
76
77pub fn release() void {
78    if (!herdr_on) return;
79    herdr_on = false;
80
81    var seq_buf: [24]u8 = undefined;
82    const n = std.fmt.bufPrint(&seq_buf, "{d}", .{nextSeq()}) catch return;
83    runQuiet(&.{ herdr_bin, "pane", "release-agent", herdr_pane, "--source", herdr_source, "--agent", herdr_agent, "--seq", n }, herdr_release_timeout_ms);
84}
85
86fn herdrState(state: []const u8) []const u8 {
87    if (std.mem.eql(u8, state, "working")) return "working";
88    if (std.mem.eql(u8, state, "blocked")) return "blocked";
89    return "idle";
90}
91
92fn nextSeq() u64 {
93    lock.lockUncancelable(platform.io);
94    defer lock.unlock(platform.io);
95    seq += 1;
96    return seq;
97}
98
99fn loop() void {
100    while (true) {
101        var msg: [max_msg]u8 = undefined;
102        var job: ?Job = null;
103        lock.lockUncancelable(platform.io);
104        if (pending) |p| {
105            @memcpy(msg[0..p.msg_len], pending_msg[0..p.msg_len]);
106            job = p;
107            pending = null;
108        }
109        lock.unlock(platform.io);
110
111        if (job) |j| send(j, msg[0..j.msg_len]);
112        std.Io.sleep(platform.io, .{ .nanoseconds = 20 * std.time.ns_per_ms }, .boot) catch {};
113    }
114}
115
116fn send(job: Job, msg: []const u8) void {
117    var seq_buf: [24]u8 = undefined;
118    const n = std.fmt.bufPrint(&seq_buf, "{d}", .{job.seq}) catch return;
119
120    const base = [_][]const u8{
121        herdr_bin, "pane",      "report-agent", herdr_pane, "--source", herdr_source,
122        "--agent", herdr_agent, "--state",      job.state,  "--seq",    n,
123    };
124    var argv: [base.len + 2][]const u8 = undefined;
125    @memcpy(argv[0..base.len], &base);
126    var len = base.len;
127    if (msg.len > 0) {
128        argv[len] = "--message";
129        argv[len + 1] = msg;
130        len += 2;
131    }
132    runQuiet(argv[0..len], herdr_report_timeout_ms);
133}
134
135fn runQuiet(argv: []const []const u8, timeout_ms: i64) void {
136    var child = std.process.spawn(platform.io, .{
137        .argv = argv,
138        .stdin = .ignore,
139        .stdout = .ignore,
140        .stderr = .ignore,
141    }) catch return;
142
143    const pid = child.id orelse return;
144    const deadline = time.nowMs() + timeout_ms;
145    while (!reaped(pid)) {
146        if (time.nowMs() >= deadline) {
147            child.kill(platform.io);
148            return;
149        }
150        std.Io.sleep(platform.io, .{ .nanoseconds = 5 * std.time.ns_per_ms }, .boot) catch {};
151    }
152}
153
154fn reaped(pid: std.posix.pid_t) bool {
155    var status: if (builtin.link_libc) c_int else u32 = undefined;
156    const rc = std.posix.system.waitpid(pid, &status, std.posix.W.NOHANG);
157    return std.posix.errno(rc) == .SUCCESS and rc != 0;
158}
159
160fn osc7501(state: []const u8, kind: ?[]const u8, msg: ?[]const u8) void {
161    var buf: [1024]u8 = undefined;
162    const head = "\x1b]7501;";
163    @memcpy(buf[0..head.len], head);
164    var len: usize = head.len;
165
166    len += (std.fmt.bufPrint(buf[len..], "state={s}:app={s}", .{ state, app }) catch return).len;
167    if (kind) |k| len += (std.fmt.bufPrint(buf[len..], ":kind={s}", .{k}) catch return).len;
168    if (msg) |m| {
169        const clipped = m[0..@min(m.len, max_msg)];
170        var encoded: [std.base64.standard.Encoder.calcSize(max_msg)]u8 = undefined;
171        const b64 = std.base64.standard.Encoder.encode(&encoded, clipped);
172        len += (std.fmt.bufPrint(buf[len..], ":msg={s}", .{b64}) catch return).len;
173    }
174    buf[len] = 0x1b;
175    buf[len + 1] = '\\';
176    len += 2;
177
178    platform.writeOut(buf[0..len]);
179}