Add a dashed-border banner between User Input and Response cards that
copies both response content and reasoning content in a combined format:
---
Response:
{response}
---
Reasoning:
{reasoning}
- template/turn_detail.html: conditional banner (only when reasoning exists)
- static/hermes-trace.css: .detail-card-banner styles (dashed border, hover
accent)
- static/hermes-trace.js: click handler + _copyResponsePlusReasoning() that
scans DOM for Response/Reasoning cards and combines their <pre> content
211 lines
8.1 KiB
JavaScript
211 lines
8.1 KiB
JavaScript
// hermes-trace.js — Shared JS for hermes-trace (no inline scripts, CSP-compatible)
|
|
(function () {
|
|
"use strict";
|
|
|
|
// ── Alpine component: turn detail panel state ──
|
|
// This file loads without `defer` while Alpine loads with `defer`,
|
|
// so alpine:init always fires after this script runs.
|
|
document.addEventListener("alpine:init", function () {
|
|
Alpine.data("turnDetail", function (cfg) {
|
|
return {
|
|
tab: "preview",
|
|
jsonLoaded: false,
|
|
focusTc: cfg.focusTc,
|
|
turnId: cfg.turnId,
|
|
init: function () {
|
|
var self = this;
|
|
if (self.focusTc) {
|
|
self.$nextTick(function () {
|
|
var el = document.getElementById("tc-" + self.focusTc);
|
|
if (el) {
|
|
el.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
el.classList.add("tc-focused");
|
|
}
|
|
});
|
|
}
|
|
},
|
|
loadJson: function () {
|
|
if (this.jsonLoaded) return;
|
|
this.jsonLoaded = true;
|
|
var id = this.turnId;
|
|
setTimeout(function () {
|
|
htmx.ajax("GET", "/turn/" + id + "/payload?type=request", { target: "#json-request", swap: "innerHTML" });
|
|
htmx.ajax("GET", "/turn/" + id + "/payload?type=response", { target: "#json-response", swap: "innerHTML" });
|
|
}, 50);
|
|
},
|
|
};
|
|
});
|
|
});
|
|
|
|
// ── Payload race guard: drop swaps that belong to a different turn ──
|
|
document.addEventListener("htmx:beforeSwap", function (evt) {
|
|
var target = evt.detail.target;
|
|
if (!target || (target.id !== "json-request" && target.id !== "json-response")) return;
|
|
var path = (evt.detail.requestConfig && evt.detail.requestConfig.path) || "";
|
|
var m = path.match(/\/turn\/(\d+)\/payload/);
|
|
if (!m) return;
|
|
var owner = target.closest("[data-turn-id]");
|
|
if (owner && owner.dataset.turnId !== m[1]) {
|
|
evt.detail.shouldSwap = false;
|
|
}
|
|
});
|
|
|
|
// ── HTMX error handling: surface failures instead of failing silently ──
|
|
document.addEventListener("htmx:responseError", function (evt) {
|
|
var el = evt.detail.target;
|
|
if (el && (el.id === "detail-body" || el.id === "json-request" || el.id === "json-response")) {
|
|
el.innerHTML = '<div class="error">Failed to load. Please try again.</div>';
|
|
}
|
|
});
|
|
|
|
// ── Copy button handler (delegated) ──
|
|
document.addEventListener("click", function (e) {
|
|
var btn = e.target.closest(".copy-btn");
|
|
if (btn) {
|
|
e.preventDefault();
|
|
_copyContent(btn);
|
|
return;
|
|
}
|
|
var banner = e.target.closest(".detail-card-banner");
|
|
if (banner) {
|
|
e.preventDefault();
|
|
_copyResponsePlusReasoning(banner);
|
|
}
|
|
});
|
|
|
|
// ── Resize handler for detail overlay ──
|
|
var _resizeInitialized = false;
|
|
function _initResize() {
|
|
if (_resizeInitialized) return;
|
|
var handle = document.getElementById("resize-handle");
|
|
if (!handle) return;
|
|
_resizeInitialized = true;
|
|
var panel = handle.parentElement;
|
|
|
|
handle.addEventListener("mousedown", function (e) {
|
|
e.preventDefault();
|
|
var startX = e.clientX;
|
|
var startWidth = panel.offsetWidth;
|
|
panel.classList.add("no-transition");
|
|
|
|
function onMove(e) {
|
|
var delta = startX - e.clientX;
|
|
var w = Math.max(
|
|
350,
|
|
Math.min(window.innerWidth * 0.92, startWidth + delta),
|
|
);
|
|
panel.style.width = w + "px";
|
|
}
|
|
|
|
function onUp() {
|
|
panel.classList.remove("no-transition");
|
|
document.removeEventListener("mousemove", onMove);
|
|
document.removeEventListener("mouseup", onUp);
|
|
}
|
|
|
|
document.addEventListener("mousemove", onMove);
|
|
document.addEventListener("mouseup", onUp);
|
|
});
|
|
}
|
|
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", _initResize);
|
|
} else {
|
|
_initResize();
|
|
}
|
|
|
|
// Also re-run resize init after HTMX swaps (new panels loaded dynamically)
|
|
document.addEventListener("htmx:afterSwap", function () {
|
|
_initResize();
|
|
});
|
|
|
|
// ── Copy helpers ──
|
|
|
|
/** Find the content to copy based on the button's context. */
|
|
function _copyContent(btn) {
|
|
// Priority 1: explicit copy source (tool JSON or full payload)
|
|
var raw =
|
|
btn.closest(".td-json-section")?.querySelector(".copy-raw") ||
|
|
btn.closest(".td-payload")?.querySelector(".copy-source");
|
|
if (raw) {
|
|
var text = raw.tagName === "TEXTAREA" ? raw.value : raw.textContent;
|
|
return _copyText(text.trim(), btn);
|
|
}
|
|
// Priority 2: parent .td-section's first pre (old layout)
|
|
var section = btn.closest(".td-section");
|
|
if (section) {
|
|
var pre = section.querySelector("pre");
|
|
if (pre) return _copyText(pre.textContent.trim(), btn);
|
|
}
|
|
// Priority 3: detail-card (Preview tab / tool call sections)
|
|
var card = btn.closest(".detail-card");
|
|
if (card) {
|
|
var raw2 = card.querySelector(".detail-card-body .copy-raw");
|
|
if (raw2) return _copyText(raw2.textContent.trim(), btn);
|
|
var pre2 = card.querySelector(".detail-card-body pre");
|
|
if (pre2) return _copyText(pre2.textContent.trim(), btn);
|
|
}
|
|
// Priority 4: json-section (JSON tab)
|
|
var js = btn.closest(".json-section");
|
|
if (js) {
|
|
var textarea = js.querySelector("textarea");
|
|
if (textarea) return _copyText(textarea.value.trim(), btn);
|
|
}
|
|
}
|
|
|
|
function _copyText(text, btn) {
|
|
if (!text) return;
|
|
navigator.clipboard
|
|
.writeText(text)
|
|
.then(function () {
|
|
_copied(btn);
|
|
})
|
|
.catch(function () {
|
|
var ta = document.createElement("textarea");
|
|
ta.value = text;
|
|
ta.style.cssText = "position:fixed;opacity:0;";
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
document.execCommand("copy");
|
|
document.body.removeChild(ta);
|
|
_copied(btn);
|
|
});
|
|
}
|
|
|
|
function _copied(btn) {
|
|
var orig = btn.textContent;
|
|
btn.textContent = "✓";
|
|
btn.style.color = "var(--green)";
|
|
setTimeout(function () {
|
|
btn.textContent = orig;
|
|
btn.style.color = "";
|
|
}, 1200);
|
|
}
|
|
|
|
// ── Banner copy: Response + Reasoning combined ──
|
|
function _copyResponsePlusReasoning(btn) {
|
|
var panel = btn.closest(".tab-pane");
|
|
if (!panel) return;
|
|
var cards = panel.querySelectorAll(".detail-card");
|
|
var responseText = "";
|
|
var reasoningText = "";
|
|
cards.forEach(function (card) {
|
|
var headerSpan = card.querySelector(".detail-card-header > span");
|
|
if (!headerSpan) return;
|
|
var label = headerSpan.textContent.trim();
|
|
var pre = card.querySelector(".detail-card-body pre");
|
|
if (!pre) return;
|
|
if (label.indexOf("Response") === 0) {
|
|
responseText = pre.textContent.trim();
|
|
} else if (label.indexOf("Reasoning") === 0) {
|
|
reasoningText = pre.textContent.trim();
|
|
}
|
|
});
|
|
if (!responseText && !reasoningText) return;
|
|
var text = "---\nResponse:\n" + responseText + "\n---\nReasoning:\n" + reasoningText;
|
|
_copyText(text, btn);
|
|
}
|
|
|
|
// Expose globally for any inline Alpine handlers that may reference it
|
|
window.copyContent = _copyContent;
|
|
})();
|