From 58caf9faf780bc61d5744d9f3d7bb79d0cf148c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Sat, 30 May 2026 23:12:54 +0200 Subject: [PATCH] agent: pretty-print JSON command results Re-indents JSON output with 2-space indentation for better readability in the terminal, while keeping non-JSON output unchanged. --- src/agent/Agent.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig index f37a45fe..40524f4a 100644 --- a/src/agent/Agent.zig +++ b/src/agent/Agent.zig @@ -901,12 +901,29 @@ fn printCommandResult(self: *Agent, cmd: Command, result: browser_tools.ToolResu else => return, }; if (cmd.producesData() and !result.is_error) { - self.terminal.printAssistant(result.text); + self.printData(result.text); return; } self.terminal.printToolOutcome(tc.name(), result.text, result.is_error); } +// REPL-only: re-indent JSON output for the terminal. MCP keeps renderJson's +// compact form. Non-JSON output (markdown, tree, urls) prints unchanged. +fn printData(self: *Agent, text: []const u8) void { + const trimmed = std.mem.trimLeft(u8, text, &std.ascii.whitespace); + if (trimmed.len > 0 and (trimmed[0] == '{' or trimmed[0] == '[')) { + if (std.json.parseFromSlice(std.json.Value, self.allocator, text, .{})) |parsed| { + defer parsed.deinit(); + if (std.json.Stringify.valueAlloc(self.allocator, parsed.value, .{ .whitespace = .indent_2 })) |pretty| { + defer self.allocator.free(pretty); + self.terminal.printAssistant(pretty); + return; + } else |_| {} + } else |_| {} + } + self.terminal.printAssistant(text); +} + fn runScript(self: *Agent, path: []const u8) bool { self.terminal.printInfo("Running script: {s}", .{path});