generalbots/botui/ui/suite/chat/chat-suggestions.js
Rodrigo Rodriguez 73e0121d0b Fix: start.bas não executa e HTML truncado
- Remove streaming de chunks LLM, acumula resposta completa antes de enviar
- Corrige variável 'action' para 'actionData' no click handler de suggestions
- Adiciona fallback window.sendMessage() se WebSocket não estiver aberto
- Adiciona guard DOMContentLoaded no chat-init.js
- Adiciona cache-busting (?v=4) no chat.html

Impacto:
- start.bas executa corretamente ao conectar WebSocket
- HTML não é mais truncado (tags fecham corretamente)
- Sugestões executam tool invocations via WebSocket
2026-05-01 02:02:57 -03:00

71 lines
2.2 KiB
JavaScript

function renderSuggestions(suggestions) {
var suggestionsEl = document.getElementById("suggestions");
if (!suggestionsEl) return;
suggestionsEl.innerHTML = "";
suggestionsEl.classList.add("has-bot-suggestions");
suggestions.forEach(function (suggestion) {
var chip = document.createElement("button");
chip.className = "suggestion-chip";
chip.textContent = suggestion.text || "Suggestion";
chip.onclick = (function (sugg) {
return function () {
console.log("[SUGGESTION] Clicked:", sugg.text, "Action:", sugg.action);
if (sugg.action) {
try {
var actionData = typeof sugg.action === "string"
? JSON.parse(sugg.action)
: sugg.action;
console.log("[SUGGESTION] Parsed action:", actionData);
if (actionData.type === "invoke_tool") {
console.log("[SUGGESTION] Invoking tool:", actionData.tool);
// Check if WebSocket is available
if (ChatState.ws && ChatState.ws.readyState === WebSocket.OPEN) {
var msg = {
bot_id: ChatState.currentBotId,
user_id: ChatState.currentUserId,
session_id: ChatState.currentSessionId,
channel: "web",
content: actionData.tool,
message_type: 6,
active_switchers: Array.from(ChatState.activeSwitchers),
timestamp: new Date().toISOString(),
};
console.log("[SUGGESTION] Sending via WS:", msg);
ChatState.ws.send(JSON.stringify(msg));
console.log("[SUGGESTION] Sent successfully");
} else {
console.log("[SUGGESTION] WS not available, fallback to sendMessage");
// Fallback: send as regular message if WS not available
window.sendMessage(sugg.text);
}
return;
} else if (actionData.type === "switch_context" && actionData.switcher) {
if (!ChatState.activeSwitchers.has(actionData.switcher)) {
ChatState.activeSwitchers.add(actionData.switcher);
renderSwitcherChips();
}
window.sendMessage(sugg.text);
} else if (actionData.type === "send_message") {
window.sendMessage(actionData.message || sugg.text);
} else if (actionData.type === "select_context") {
window.sendMessage(actionData.context);
} else {
window.sendMessage(sugg.text);
}
} catch (e) {
window.sendMessage(sugg.text);
}
} else {
window.sendMessage(sugg.text);
}
};
})(suggestion);
suggestionsEl.appendChild(chip);
});
}