(function () { "use strict"; var sharedQid = null; function esc(value) { return String(value === null || value === undefined ? "" : value) .replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); } function text(value) { if (value === null || value === undefined) return ""; if (typeof value === "string") return value; return JSON.stringify(value, null, 2); } function inputText(event) { var value = event && event.input; if (value === null || value === undefined) return ""; if (typeof value !== "object" || Array.isArray(value)) return text(value); var keys = Object.keys(value); if (keys.length === 1 && typeof value[keys[0]] === "string") return value[keys[0]]; return JSON.stringify(value, null, 2); } function humanBytes(length) { if (length < 1024) return length + " B"; if (length < 1024 * 1024) return (length / 1024).toFixed(1) + " KB"; return (length / (1024 * 1024)).toFixed(2) + " MB"; } function toolStep(index, event, resultEvent) { var name = event.name || "tool"; var input = inputText(event); var output = resultEvent ? text(resultEvent.content) : ""; var collapsed = output.length > 2000; return '
' + '
' + index + 'tool' + esc(name) + "
" + (input ? '
Input
' +
        esc(input) + "
" : "") + (resultEvent ? '
Output ' + humanBytes(output.length) + '
' +
        esc(output) + "
" + (collapsed ? '' : "") + "
" : "") + "
"; } function messageStep(index, event) { var content = text(event.content).trim(); if (!content) return ""; return '
' + '' + index + 'model
' + '
' + esc(content) + "
"; } function otherStep(index, event) { var content = text(event.content || event.input || event); return '
' + '' + index + '' + esc(event.type || "event") + "
" + '
' + esc(content) + "
"; } function render(events, label) { events = events || []; if (!events.length) return ""; var parts = []; var step = 0; for (var i = 0; i < events.length; i++) { var event = events[i] || {}; if (event.type === "tool_call") { step++; var next = events[i + 1]; parts.push(toolStep(step, event, next && next.type === "tool_result" ? next : null)); if (next && next.type === "tool_result") i++; } else if (event.type === "tool_result") { step++; parts.push(toolStep(step, { name: "tool" }, event)); } else if (["text", "llm_response", "assistant", "model"].indexOf(event.type) >= 0) { step++; parts.push(messageStep(step, event)); } else if (event.type !== "done") { step++; parts.push(otherStep(step, event)); } } return '
' + esc(label || "Agent trajectory") + " · " + events.length + ' events
' + parts.join("") + "
"; } function bind(container) { if (!container) return; container.querySelectorAll(".pretty-output-toggle").forEach(function (button) { button.addEventListener("click", function () { var wrap = button.closest(".pretty-output-wrap"); var collapsed = wrap.classList.toggle("collapsed"); button.textContent = collapsed ? "Expand output ▾" : "Collapse output ▴"; }); }); } function goldItems(value) { var values = Array.isArray(value) ? value : [value]; return values.filter(function (item) { return item !== null && item !== undefined && String(item).trim() !== ""; }).map(function (item) { if (typeof item === "object") { return item.answer_text || item.value || item.name || JSON.stringify(item); } return String(item); }); } function gold(value, label) { var values = goldItems(value); return '
' + esc(label || "Gold") + (values.length > 1 ? " · " + values.length : "") + '
' + (values.length ? values.map(function (item) { return '' + esc(item) + ""; }).join("") : 'none') + "
"; } function supporting(label, html, count) { return '
' + esc(label) + (count !== undefined ? " · " + count : "") + '
' + html + "
"; } window.TrajectoryUI = { render: render, bind: bind, gold: gold, supporting: supporting, setQid: function (qid) { if (qid !== null && qid !== undefined) sharedQid = String(qid); }, getQid: function () { return sharedQid; }, }; })();