137 lines
5.2 KiB
JavaScript
137 lines
5.2 KiB
JavaScript
import { api } from "../lib/api.js";
|
|
import { on } from "../lib/bus.js";
|
|
|
|
class RunTimeline extends HTMLElement {
|
|
constructor() {
|
|
super();
|
|
this.attachShadow({ mode: "open" });
|
|
this.chatId = null;
|
|
this.items = [];
|
|
this.selectedDebug = null;
|
|
|
|
this.shadowRoot.innerHTML = `
|
|
<style>
|
|
:host { display:block; padding:12px; }
|
|
.box { background:#121823; border:1px solid #1e2a3a; border-radius:10px; padding:10px; margin-bottom:10px; }
|
|
.row { display:flex; gap:8px; align-items:center; }
|
|
.muted { color:#8aa0b5; font-size:12px; }
|
|
.title { font-weight:800; }
|
|
.chatlog { display:flex; flex-direction:column; gap:8px; max-height:520px; overflow:auto; padding-right:6px; margin-top:8px; }
|
|
/* WhatsApp-ish dark theme bubbles */
|
|
.bubble { max-width:90%; padding:8px 10px; border-radius:14px; border:1px solid #253245; font-size:13px; line-height:1.35; white-space:pre-wrap; word-break:break-word; box-shadow: 0 1px 0 rgba(0,0,0,.35); }
|
|
.bubble.user { align-self:flex-end; background:#0f2a1a; border-color:#1f6f43; color:#e7eef7; }
|
|
.bubble.bot { align-self:flex-start; background:#111b2a; border-color:#2a3a55; color:#e7eef7; }
|
|
.bubble.err { align-self:flex-start; background:#241214; border-color:#e74c3c; color:#ffe9ea; cursor:pointer; }
|
|
.bubble .meta { display:block; margin-top:6px; font-size:11px; color:#8aa0b5; }
|
|
.bubble.user .meta { color:#b9d9c6; opacity:.85; }
|
|
.bubble.bot .meta { color:#a9bed6; opacity:.85; }
|
|
.bubble.err .meta { color:#ffd0d4; opacity:.85; }
|
|
.toolbar { display:flex; gap:8px; margin-top:8px; align-items:center; }
|
|
button { cursor:pointer; background:#0f1520; color:#e7eef7; border:1px solid #253245; border-radius:8px; padding:8px; font-size:13px; }
|
|
pre { white-space:pre-wrap; word-break:break-word; background:#0f1520; border:1px solid #253245; border-radius:10px; padding:10px; margin:0; font-size:12px; color:#d7e2ef; }
|
|
</style>
|
|
|
|
<div class="box">
|
|
<div class="muted">Conversación</div>
|
|
<div class="title" id="chat">—</div>
|
|
<div class="muted" id="meta">Seleccioná una conversación.</div>
|
|
<div class="toolbar">
|
|
<button id="refresh">Refresh</button>
|
|
<span class="muted" id="count"></span>
|
|
</div>
|
|
<div class="chatlog" id="log"></div>
|
|
</div>
|
|
|
|
<div class="box">
|
|
<div class="muted">Debug (click en burbuja roja para ver detalles)</div>
|
|
<pre id="debug">—</pre>
|
|
</div>
|
|
|
|
`;
|
|
}
|
|
|
|
connectedCallback() {
|
|
this.shadowRoot.getElementById("refresh").onclick = () => this.loadMessages();
|
|
|
|
this._unsubSel = on("ui:selectedChat", async ({ chat_id }) => {
|
|
this.chatId = chat_id;
|
|
await this.loadMessages();
|
|
});
|
|
|
|
this._unsubRun = on("run:created", (run) => {
|
|
if (this.chatId && run.chat_id === this.chatId) {
|
|
// nuevo run => refrescar mensajes para ver los bubbles actualizados
|
|
this.loadMessages();
|
|
}
|
|
});
|
|
}
|
|
|
|
disconnectedCallback() {
|
|
this._unsubSel?.();
|
|
this._unsubRun?.();
|
|
}
|
|
|
|
async loadMessages() {
|
|
this.shadowRoot.getElementById("chat").textContent = this.chatId || "—";
|
|
this.shadowRoot.getElementById("meta").textContent = "Cargando…";
|
|
this.shadowRoot.getElementById("count").textContent = "";
|
|
|
|
if (!this.chatId) {
|
|
this.shadowRoot.getElementById("meta").textContent = "Seleccioná una conversación.";
|
|
this.shadowRoot.getElementById("log").innerHTML = "";
|
|
this.shadowRoot.getElementById("debug").textContent = "—";
|
|
return;
|
|
}
|
|
|
|
const data = await api.messages({ chat_id: this.chatId, limit: 200 });
|
|
this.items = data.items || [];
|
|
this.render();
|
|
}
|
|
|
|
isErrorMsg(m) {
|
|
const txt = String(m?.text || "");
|
|
return Boolean(m?.payload?.error) || txt.startsWith("[ERROR]") || txt.toLowerCase().includes("internal_error");
|
|
}
|
|
|
|
render() {
|
|
const meta = this.shadowRoot.getElementById("meta");
|
|
const count = this.shadowRoot.getElementById("count");
|
|
const log = this.shadowRoot.getElementById("log");
|
|
const dbg = this.shadowRoot.getElementById("debug");
|
|
|
|
meta.textContent = `Mostrando historial (últimos ${this.items.length}).`;
|
|
count.textContent = this.items.length ? `${this.items.length} msgs` : "";
|
|
|
|
log.innerHTML = "";
|
|
|
|
for (const m of this.items) {
|
|
const who = m.direction === "in" ? "user" : "bot";
|
|
const isErr = this.isErrorMsg(m);
|
|
const bubble = document.createElement("div");
|
|
bubble.className = `bubble ${isErr ? "err" : who}`;
|
|
bubble.textContent = m.text || (isErr ? "Error" : "—");
|
|
|
|
const metaEl = document.createElement("span");
|
|
metaEl.className = "meta";
|
|
metaEl.textContent = `${new Date(m.ts).toLocaleString()} • ${m.provider} • ${m.message_id}`;
|
|
bubble.appendChild(metaEl);
|
|
|
|
if (isErr) {
|
|
bubble.title = "Click para ver detalles (JSON)";
|
|
bubble.onclick = () => {
|
|
dbg.textContent = JSON.stringify(m, null, 2);
|
|
};
|
|
}
|
|
|
|
log.appendChild(bubble);
|
|
}
|
|
|
|
// auto-scroll
|
|
log.scrollTop = log.scrollHeight;
|
|
|
|
if (!this.items.length) dbg.textContent = "—";
|
|
}
|
|
}
|
|
|
|
customElements.define("run-timeline", RunTimeline);
|