fix: chat payload to [{action,chatid,chatInput}] JSON array, UUID chatid, unwrap n8n array response

This commit is contained in:
oliver
2026-04-24 12:45:30 -03:00
parent a158148057
commit 2bca1f4e90
+33 -14
View File
@@ -1942,10 +1942,24 @@
var CHAT_URL =
"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/702862fd-dd17-4a34-8efb-e9056d2c50df/chat";
var SESSION =
"mbs_" +
Math.random().toString(36).slice(2) +
Date.now().toString(36);
/* Generate a UUID v4 for chatid */
var CHAT_ID = (function () {
if (
typeof crypto !== "undefined" &&
typeof crypto.randomUUID === "function"
) {
return crypto.randomUUID();
}
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
var r = (Math.random() * 16) | 0;
return (c === "x" ? r : (r & 0x3) | 0x8).toString(
16,
);
},
);
})();
var panel = document.getElementById("chat-panel");
var chatLog = document.getElementById("chat-history");
@@ -2022,13 +2036,16 @@
var thinkEl = appendMsg("agent", "\u2026");
thinkEl.classList.add("chat-thinking");
var fd = new FormData();
fd.append("message", text);
fd.append("session_id", SESSION);
fetch(CHAT_URL, {
method: "POST",
body: fd,
headers: { "Content-Type": "application/json" },
body: JSON.stringify([
{
action: "sendMessage",
chatid: CHAT_ID,
chatInput: text,
},
]),
})
.then(function (r) {
var ct = r.headers.get("content-type") || "";
@@ -2040,12 +2057,14 @@
.then(function (d) {
if (thinkEl.parentNode)
chatLog.removeChild(thinkEl);
/* n8n returns an array or object — unwrap either */
var item = Array.isArray(d) ? d[0] : d;
var reply =
d.reply ||
d.message ||
d.response ||
d.output ||
d.text ||
(item &&
(item.output ||
item.text ||
item.message ||
item.reply)) ||
(typeof d === "string" ? d : JSON.stringify(d));
appendMsg("agent", reply);
busy = false;