Skip to content

Agent Zero AI framework

Beautify browser multi JSON arrays in WebUI display

linkliti05b818b9deec
diff --git a/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js b/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js
index d2bc07131..6e91d5311 100644
--- a/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js
+++ b/plugins/_browser/extensions/webui/get_tool_message_handler/browser-tool-handler.js
@@ -61,9 +61,10 @@ function parseBrowserResult(content) {
}
function beautifyJsonForDisplay(text) {
- if (!text || !text.startsWith("{") || !text.endsWith("}")) return text;
+ const t = text && text.trim();
+ if (!t || !((t.startsWith("{") && t.endsWith("}")) || (t.startsWith("[") && t.endsWith("]")))) return text;
try {
- return JSON.stringify(JSON.parse(text), null, 2);
+ return JSON.stringify(JSON.parse(t), null, 2);
} catch {
return text;
}
diff --git a/tests/test_browser_tool_output.py b/tests/test_browser_tool_output.py
index 495c80e4b..63fddd8db 100644
--- a/tests/test_browser_tool_output.py
+++ b/tests/test_browser_tool_output.py
@@ -1,4 +1,6 @@
import json
+import subprocess
+from pathlib import Path
import pytest
@@ -6,6 +8,9 @@ from plugins._browser.helpers.runtime import _BrowserRuntimeCore
from plugins._browser.tools.browser import Browser
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+
+
def test_format_result_minifies_tool_json() -> None:
result = {"browsers": [{"id": "b1", "title": "Example"}]}
@@ -36,3 +41,34 @@ async def test_evaluate_uses_script_arg(monkeypatch) -> None:
assert result == 7
assert seen == ["1+1"]
+
+
+def test_webui_beautifier_formats_objects_and_arrays() -> None:
+ source = (
+ PROJECT_ROOT
+ / "plugins"
+ / "_browser"
+ / "extensions"
+ / "webui"
+ / "get_tool_message_handler"
+ / "browser-tool-handler.js"
+ ).read_text(encoding="utf-8")
+ start = source.index("function beautifyJsonForDisplay")
+ end = source.index("\n}", start) + 2
+ object_literal = json.dumps({"browsers": [{"id": 1}]})
+ array_literal = json.dumps([{"id": 1}, {"id": 2}])
+ script = (
+ source[start:end]
+ + "\nconst pretty = beautifyJsonForDisplay(" + json.dumps(object_literal) + ");\n"
+ + "if (!pretty.includes('\\n')) throw new Error('object JSON was not beautified');\n"
+ + "if (pretty !== " + json.dumps(json.dumps({"browsers": [{"id": 1}]}, indent=2)) + ") throw new Error('object JSON content changed');\n"
+ + "const arrayPretty = beautifyJsonForDisplay(" + json.dumps(array_literal) + ");\n"
+ + "if (!arrayPretty.includes('\\n')) throw new Error('array JSON was not beautified');\n"
+ + "if (JSON.parse(arrayPretty).length !== 2) throw new Error('array content changed');\n"
+ + "const padded = beautifyJsonForDisplay(' ' + " + json.dumps(json.dumps({"a": 1})) + " );\n"
+ + "if (padded !== " + json.dumps('{\n "a": 1\n}') + ") throw new Error('padded JSON was not trimmed');\n"
+ + "if (beautifyJsonForDisplay('plain document') !== 'plain document') throw new Error('plain text changed');\n"
+ + "if (beautifyJsonForDisplay('{broken') !== '{broken') throw new Error('invalid JSON changed');\n"
+ )
+
+ subprocess.run(["node", "--input-type=module", "-e", script], check=True, text=True)