graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
refactor(images): move the /images handler out of commands_model (#103)
justrach1f4b5e139bf1
diff --git a/src/commands_model.zig b/src/commands_model.zigindex 3f7226db..bd16f944 100644--- a/src/commands_model.zig+++ b/src/commands_model.zig@@ -44,7 +44,7 @@ const fallback_config = @import("fallback_config.zig");const serde = @import("serde.zig");const oauth = @import("oauth.zig");-const oauth_helpers = @import("oauth_helpers.zig"); // openBrowser moved here; the #103 branch predates that+const images_command = @import("images_command.zig");const images = @import("images.zig");const reasoning_levels = [_]PickItem{@@ -61,33 +61,6 @@ const stageImagePath = vision.stageImagePath;const visionCapable = vision.visionCapable;const grabClipboardImage = vision.grabClipboardImage;-/// Serialized JSON of the most recent turn (from the user's last typed prompt to-/// the end) — what /images scans for image URLs. Provider-agnostic: URLs in-/// assistant text and tool outputs survive JSON serialization intact.-fn recentTurnText(root: *Agent, arena: Allocator) []const u8 {- const msgs = root.messages.items;- var start: usize = 0;- var k = msgs.len;- while (k > 0) : (k -= 1) {- // Stop at the genuinely most-recent user turn — plain text OR an image- // attachment (array content) — but not an anthropic tool_result-only user- // message; cleanUserTurn is exactly that discriminator.- if (Agent.cleanUserTurn(msgs[k - 1])) {- start = k - 1;- break;- }- }- var aw: Io.Writer.Allocating = .init(arena);- for (msgs[start..]) |m| {- // A fresh Stringify per message: one instance can only serialize a single- // top-level value. Newline-delimited is fine — we only scan for URLs.- var s: std.json.Stringify = .{ .writer = &aw.writer };- s.write(m) catch {};- aw.writer.writeByte('\n') catch {};- }- return aw.writer.buffered();-}-fn providerKnown(id: []const u8) bool {return provider_mod.specFor(id) != null;}@@ -550,40 +523,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,return true;}if (std.mem.eql(u8, line, "/images")) {- // Transient: serialize + scan the last turn in a scratch arena freed on- // return, so /images never accumulates in the long-lived session arena (#124).- var scratch = std.heap.ArenaAllocator.init(root.gpa);- defer scratch.deinit();- const sa = scratch.allocator();- const urls = images.extractImageUrls(sa, recentTurnText(root, sa)) catch &[_][]const u8{};- if (urls.len == 0) {- try out.writeAll("no image URLs in the last response — /images opens images from the most recent turn's output (e.g. a `gh issue view` with attachments)\n");- try out.flush();- return true;- }- // GRAFF_NO_BROWSER (headless/SSH) suppresses the spawn; the command then- // just lists the URLs, which also lets a test assert them without opening- // real browser tabs.- const suppress = main_mod.g_no_browser;- // Cap the spawn so a reply full of image URLs can't flood the browser with- // tabs; the full list is always printed so the rest are one copy away.- const max_open = 8;- if (!suppress) {- const n = @min(urls.len, max_open);- for (urls[0..n]) |u| oauth_helpers.openBrowser(root.io, u);- }- if (urls.len == 1) {- try out.print("{s} {s}\n", .{ if (suppress) "image:" else "opening", urls[0] });- } else {- try out.print("{s} {d} images:\n", .{ if (suppress) "found" else "opening", urls.len });- for (urls, 1..) |u, n| try out.print(" {d}. {s}\n", .{ n, u });- if (!suppress and urls.len > max_open)- try out.print("(opened the first {d}; open the rest from the list above)\n", .{max_open});- }- try out.flush();- return true;- }- if (std.mem.startsWith(u8, line, "/image")) {+ if (std.mem.eql(u8, line, "/images")) return images_command.run(root, out);const path = std.mem.trim(u8, line["/image".len..], " \t");if (path.len == 0) {if (root.pending_image) |pi| {diff --git a/src/images_command.zig b/src/images_command.zignew file mode 100644index 00000000..bd795677--- /dev/null+++ b/src/images_command.zig@@ -0,0 +1,78 @@+//! The Agent-facing half of `/images` (#103): find the most recent turn, scan it+//! with the pure extractor in images.zig, and open what it finds in a browser.+//! Kept out of images.zig on purpose - that module's header states it stays pure+//! (no Agent/IO) so the load-bearing URL logic is unit-testable without a+//! browser. Kept out of commands_model.zig because that file is at the 600-line+//! cap.++const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;++const main_mod = @import("main.zig");+const Agent = @import("agent.zig").Agent;+const images = @import("images.zig");+const oauth_helpers = @import("oauth_helpers.zig");++/// Serialized JSON of the most recent turn (from the user's last typed prompt to+/// the end) — what /images scans for image URLs. Provider-agnostic: URLs in+/// assistant text and tool outputs survive JSON serialization intact.+fn recentTurnText(root: *Agent, arena: Allocator) []const u8 {+ const msgs = root.messages.items;+ var start: usize = 0;+ var k = msgs.len;+ while (k > 0) : (k -= 1) {+ // Stop at the genuinely most-recent user turn — plain text OR an image+ // attachment (array content) — but not an anthropic tool_result-only user+ // message; cleanUserTurn is exactly that discriminator.+ if (Agent.cleanUserTurn(msgs[k - 1])) {+ start = k - 1;+ break;+ }+ }+ var aw: Io.Writer.Allocating = .init(arena);+ for (msgs[start..]) |m| {+ // A fresh Stringify per message: one instance can only serialize a single+ // top-level value. Newline-delimited is fine — we only scan for URLs.+ var s: std.json.Stringify = .{ .writer = &aw.writer };+ s.write(m) catch {};+ aw.writer.writeByte('\n') catch {};+ }+ return aw.writer.buffered();+}++/// `/images` — returns true (handled) always; the caller dispatches on the line.+pub fn run(root: *Agent, out: *Io.Writer) !bool {+ // Transient: serialize + scan the last turn in a scratch arena freed on+ // return, so /images never accumulates in the long-lived session arena (#124).+ var scratch = std.heap.ArenaAllocator.init(root.gpa);+ defer scratch.deinit();+ const sa = scratch.allocator();+ const urls = images.extractImageUrls(sa, recentTurnText(root, sa)) catch &[_][]const u8{};+ if (urls.len == 0) {+ try out.writeAll("no image URLs in the last response — /images opens images from the most recent turn's output (e.g. a `gh issue view` with attachments)\n");+ try out.flush();+ return true;+ }+ // GRAFF_NO_BROWSER (headless/SSH) suppresses the spawn; the command then+ // just lists the URLs, which also lets a test assert them without opening+ // real browser tabs.+ const suppress = main_mod.g_no_browser;+ // Cap the spawn so a reply full of image URLs can't flood the browser with+ // tabs; the full list is always printed so the rest are one copy away.+ const max_open = 8;+ if (!suppress) {+ const n = @min(urls.len, max_open);+ for (urls[0..n]) |u| oauth_helpers.openBrowser(root.io, u);+ }+ if (urls.len == 1) {+ try out.print("{s} {s}\n", .{ if (suppress) "image:" else "opening", urls[0] });+ } else {+ try out.print("{s} {d} images:\n", .{ if (suppress) "found" else "opening", urls.len });+ for (urls, 1..) |u, n| try out.print(" {d}. {s}\n", .{ n, u });+ if (!suppress and urls.len > max_open)+ try out.print("(opened the first {d}; open the rest from the list above)\n", .{max_open});+ }+ try out.flush();+ return true;+}