fix: robust internal signal interception and HTML detection
Some checks failed
BotUI CI/CD / build (push) Has been cancelled

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-14 17:53:24 -03:00
parent a91b9c604a
commit 928e259745

View file

@ -302,13 +302,12 @@ function addMessage(sender, content, msgId) {
"</div>";
} else {
// Check if content has HTML (any tag, including comments)
var hasHtmlTags = new RegExp("<\\\\/?[a-zA-Z][^>]*>|<!--|-->", "i").test(content);
var hasHtmlTags = (content.indexOf("<") !== -1 && /<[a-zA-Z]/.test(content)) || content.indexOf("<!--") !== -1;
console.log("Bot message - hasHtmlTags:", hasHtmlTags, "content length:", content.length, "msgId:", msgId);
var parsed;
if (hasHtmlTags && msgId) {
// Streaming HTML content - show as text initially to avoid broken tags
// Will be rendered as HTML at finalizeStreaming
parsed = escapeHtml(content);
} else if (hasHtmlTags) {
// Complete HTML content - render directly
@ -761,7 +760,7 @@ function addMessage(sender, content, msgId) {
lastRenderTime = now;
// Check if content has HTML
var hasHtmlTags = new RegExp("<\\\\/?[a-zA-Z][^>]*>|<!--|-->", "i").test(currentStreamingContent);
var hasHtmlTags = (currentStreamingContent.indexOf("<") !== -1 && /<[a-zA-Z]/.test(currentStreamingContent)) || currentStreamingContent.indexOf("<!--") !== -1;
if (hasHtmlTags) {
// HTML Streaming: Add to temporary buffer and only render valid chunks
@ -815,19 +814,36 @@ function finalizeStreaming() {
}
function processMessage(data) {
// If content is a JSON string, try to parse it (internal signals)
if (data.content && typeof data.content === "string" && data.content.startsWith("{")) {
try {
const inner = JSON.parse(data.content);
if (inner.type === "thinking" || inner.type === "thinking_clear") {
data = inner; // Process as the inner signal
// If content is a JSON string or contains it, try to intercept internal signals
if (data.content && typeof data.content === "string") {
var jsonStart = data.content.indexOf('{"type":"thinking');
if (jsonStart !== -1) {
try {
// Extract only the JSON part
var remaining = data.content.substring(jsonStart);
var jsonEnd = remaining.indexOf("}") + 1;
if (jsonEnd > 0) {
var jsonStr = remaining.substring(0, jsonEnd);
var inner = JSON.parse(jsonStr);
if (inner.type === "thinking") {
showThinkingIndicator(inner.content);
// If there is text before or after, keep processing that as content
data.content = data.content.replace(jsonStr, "").trim();
if (data.content === "") return;
} else if (inner.type === "thinking_clear") {
hideThinkingIndicator();
data.content = data.content.replace(jsonStr, "").trim();
if (data.content === "") return;
}
}
} catch (e) {
console.error("Failed to parse embedded signal:", e);
}
} catch (e) {
// Not valid internal JSON signal, continue as normal text
}
}
// Handle thinking indicator from backend
// Fallback for direct type matches
if (data.type === "thinking") {
showThinkingIndicator(data.content);
return;