fix: chat payload to [{action,chatid,chatInput}] JSON array, UUID chatid, unwrap n8n array response
This commit is contained in:
+33
-14
@@ -1942,10 +1942,24 @@
|
|||||||
|
|
||||||
var CHAT_URL =
|
var CHAT_URL =
|
||||||
"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/702862fd-dd17-4a34-8efb-e9056d2c50df/chat";
|
"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/702862fd-dd17-4a34-8efb-e9056d2c50df/chat";
|
||||||
var SESSION =
|
/* Generate a UUID v4 for chatid */
|
||||||
"mbs_" +
|
var CHAT_ID = (function () {
|
||||||
Math.random().toString(36).slice(2) +
|
if (
|
||||||
Date.now().toString(36);
|
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 panel = document.getElementById("chat-panel");
|
||||||
var chatLog = document.getElementById("chat-history");
|
var chatLog = document.getElementById("chat-history");
|
||||||
@@ -2022,13 +2036,16 @@
|
|||||||
var thinkEl = appendMsg("agent", "\u2026");
|
var thinkEl = appendMsg("agent", "\u2026");
|
||||||
thinkEl.classList.add("chat-thinking");
|
thinkEl.classList.add("chat-thinking");
|
||||||
|
|
||||||
var fd = new FormData();
|
|
||||||
fd.append("message", text);
|
|
||||||
fd.append("session_id", SESSION);
|
|
||||||
|
|
||||||
fetch(CHAT_URL, {
|
fetch(CHAT_URL, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: fd,
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify([
|
||||||
|
{
|
||||||
|
action: "sendMessage",
|
||||||
|
chatid: CHAT_ID,
|
||||||
|
chatInput: text,
|
||||||
|
},
|
||||||
|
]),
|
||||||
})
|
})
|
||||||
.then(function (r) {
|
.then(function (r) {
|
||||||
var ct = r.headers.get("content-type") || "";
|
var ct = r.headers.get("content-type") || "";
|
||||||
@@ -2040,12 +2057,14 @@
|
|||||||
.then(function (d) {
|
.then(function (d) {
|
||||||
if (thinkEl.parentNode)
|
if (thinkEl.parentNode)
|
||||||
chatLog.removeChild(thinkEl);
|
chatLog.removeChild(thinkEl);
|
||||||
|
/* n8n returns an array or object — unwrap either */
|
||||||
|
var item = Array.isArray(d) ? d[0] : d;
|
||||||
var reply =
|
var reply =
|
||||||
d.reply ||
|
(item &&
|
||||||
d.message ||
|
(item.output ||
|
||||||
d.response ||
|
item.text ||
|
||||||
d.output ||
|
item.message ||
|
||||||
d.text ||
|
item.reply)) ||
|
||||||
(typeof d === "string" ? d : JSON.stringify(d));
|
(typeof d === "string" ? d : JSON.stringify(d));
|
||||||
appendMsg("agent", reply);
|
appendMsg("agent", reply);
|
||||||
busy = false;
|
busy = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user