fix(file-summary): 同步压缩包工作流状态与结果刷新
This commit is contained in:
106
static/js/app.js
106
static/js/app.js
@@ -20,6 +20,7 @@
|
||||
var nodeAnchors = [];
|
||||
var workflowPollingTimers = {};
|
||||
var WORKFLOW_POLL_INTERVAL_MS = 1500;
|
||||
var latestMessageId = 0;
|
||||
|
||||
if (!workspace) {
|
||||
return;
|
||||
@@ -52,6 +53,15 @@
|
||||
nodeAnchors = Array.prototype.slice.call(document.querySelectorAll(".node-anchor"));
|
||||
}
|
||||
|
||||
function syncLatestMessageIdFromDom() {
|
||||
document.querySelectorAll(".message[data-message-id]").forEach(function (message) {
|
||||
var id = parseInt(message.getAttribute("data-message-id"), 10);
|
||||
if (!Number.isNaN(id)) {
|
||||
latestMessageId = Math.max(latestMessageId, id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (sidebarToggle) {
|
||||
sidebarToggle.addEventListener("click", toggleSidebar);
|
||||
}
|
||||
@@ -271,6 +281,9 @@
|
||||
var article = document.createElement("article");
|
||||
article.className = "message " + role;
|
||||
article.id = messageId;
|
||||
if (typeof messageId === "number") {
|
||||
article.setAttribute("data-message-id", messageId);
|
||||
}
|
||||
if (label) {
|
||||
article.setAttribute("data-node-label", label);
|
||||
}
|
||||
@@ -295,6 +308,48 @@
|
||||
return { article: article, bubble: bubble, text: text };
|
||||
}
|
||||
|
||||
function appendConversationMessage(message) {
|
||||
if (!message || document.querySelector('.message[data-message-id="' + message.id + '"]')) {
|
||||
return;
|
||||
}
|
||||
var label = message.role === "assistant" ? "AI " : "用户 ";
|
||||
label += document.querySelectorAll(".message").length + 1;
|
||||
var created = createMessage(message.role, message.content || "", "message-" + message.id, label);
|
||||
created.article.setAttribute("data-message-id", message.id);
|
||||
latestMessageId = Math.max(latestMessageId, message.id);
|
||||
if (message.role === "user") {
|
||||
appendNode(created.article.id, label, true);
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshConversationMessages() {
|
||||
var conversationId = currentConversationId();
|
||||
if (!conversationId || !summaryPanel) {
|
||||
return;
|
||||
}
|
||||
var url = templateUrl("data-message-url-template", "__conversation_id__", conversationId);
|
||||
if (!url) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var response = await fetch(url + "?after=" + latestMessageId, { cache: "no-store" });
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
var payload = await response.json();
|
||||
(payload.messages || []).forEach(appendConversationMessage);
|
||||
if (payload.latest_message_id) {
|
||||
latestMessageId = Math.max(latestMessageId, payload.latest_message_id);
|
||||
}
|
||||
syncNodeRailVisibility();
|
||||
bindNodeAnchorClicks();
|
||||
setActiveNode();
|
||||
scrollChatToBottom();
|
||||
} catch (error) {
|
||||
console.error("Conversation message refresh failed", error);
|
||||
}
|
||||
}
|
||||
|
||||
function appendNode(targetId, title, isLatest) {
|
||||
if (!nodeRail) {
|
||||
return;
|
||||
@@ -530,13 +585,31 @@
|
||||
var status = card.querySelector(".workflow-status");
|
||||
status.textContent = payload.batch.status;
|
||||
status.className = "workflow-status status-" + payload.batch.status;
|
||||
var batchError = card.querySelector(".workflow-error");
|
||||
if (payload.batch.error_message) {
|
||||
if (!batchError) {
|
||||
batchError = document.createElement("p");
|
||||
batchError.className = "workflow-error";
|
||||
card.insertBefore(batchError, card.querySelector("ol"));
|
||||
}
|
||||
batchError.textContent = payload.batch.error_message;
|
||||
} else if (batchError) {
|
||||
batchError.remove();
|
||||
}
|
||||
var list = card.querySelector("ol");
|
||||
list.innerHTML = "";
|
||||
(payload.nodes || []).forEach(function (node) {
|
||||
var item = document.createElement("li");
|
||||
item.className = "node-status status-" + node.status;
|
||||
item.setAttribute("data-node-code", node.node_code);
|
||||
item.innerHTML = "<span>" + escapeHtml(node.node_name) + "</span><em>" + node.progress + "%</em>";
|
||||
item.innerHTML =
|
||||
'<div><span>' +
|
||||
escapeHtml(node.node_name) +
|
||||
"</span>" +
|
||||
(node.message ? "<small>" + escapeHtml(node.message) + "</small>" : "") +
|
||||
"</div><em>" +
|
||||
node.progress +
|
||||
"%</em>";
|
||||
list.appendChild(item);
|
||||
});
|
||||
return payload.batch.status || "";
|
||||
@@ -561,11 +634,13 @@
|
||||
workflowPollingTimers[batchId] = window.setInterval(async function () {
|
||||
var status = await refreshWorkflowCard(batchId);
|
||||
if (isWorkflowTerminalStatus(status)) {
|
||||
refreshConversationMessages();
|
||||
stopWorkflowPolling(batchId);
|
||||
}
|
||||
}, WORKFLOW_POLL_INTERVAL_MS);
|
||||
refreshWorkflowCard(batchId).then(function (status) {
|
||||
if (isWorkflowTerminalStatus(status)) {
|
||||
refreshConversationMessages();
|
||||
stopWorkflowPolling(batchId);
|
||||
}
|
||||
});
|
||||
@@ -666,6 +741,11 @@
|
||||
return;
|
||||
}
|
||||
if (eventName === "meta") {
|
||||
if (payload.user_message_id) {
|
||||
userMessage.article.id = "message-" + payload.user_message_id;
|
||||
userMessage.article.setAttribute("data-message-id", payload.user_message_id);
|
||||
latestMessageId = Math.max(latestMessageId, payload.user_message_id);
|
||||
}
|
||||
if (payload.conversation_id) {
|
||||
conversationIdInput.value = payload.conversation_id;
|
||||
window.history.replaceState({}, "", "/?conversation=" + payload.conversation_id);
|
||||
@@ -678,6 +758,10 @@
|
||||
assistantText += payload.delta || "";
|
||||
assistantMessage.text.innerHTML = renderAssistantContent(assistantText);
|
||||
scrollChatToBottom();
|
||||
} else if (eventName === "replace") {
|
||||
assistantText = payload.content || "";
|
||||
assistantMessage.text.innerHTML = renderAssistantContent(assistantText);
|
||||
scrollChatToBottom();
|
||||
} else if (eventName === "error") {
|
||||
assistantText = payload.message || "模型调用失败。";
|
||||
assistantMessage.text.innerHTML = renderAssistantContent(assistantText);
|
||||
@@ -687,6 +771,8 @@
|
||||
} else if (eventName === "done") {
|
||||
if (payload.assistant_message_id) {
|
||||
assistantMessage.article.id = "message-" + payload.assistant_message_id;
|
||||
assistantMessage.article.setAttribute("data-message-id", payload.assistant_message_id);
|
||||
latestMessageId = Math.max(latestMessageId, payload.assistant_message_id);
|
||||
}
|
||||
if (payload.title) {
|
||||
setConversationTitle(payload.title);
|
||||
@@ -711,7 +797,24 @@
|
||||
}
|
||||
}
|
||||
|
||||
function bindPromptKeyboardShortcuts() {
|
||||
if (!promptInput || !composer) {
|
||||
return;
|
||||
}
|
||||
promptInput.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Enter" && !event.ctrlKey) {
|
||||
event.preventDefault();
|
||||
if (typeof composer.requestSubmit === "function") {
|
||||
composer.requestSubmit();
|
||||
} else {
|
||||
composer.dispatchEvent(new Event("submit", { cancelable: true }));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
syncNodeRailVisibility();
|
||||
syncLatestMessageIdFromDom();
|
||||
bindNodeAnchorClicks();
|
||||
renderExistingAssistantMessages();
|
||||
refreshRunningWorkflowCards();
|
||||
@@ -724,6 +827,7 @@
|
||||
if (composer) {
|
||||
composer.addEventListener("submit", streamChat);
|
||||
}
|
||||
bindPromptKeyboardShortcuts();
|
||||
|
||||
if (uploadDropzone && attachmentInput) {
|
||||
uploadDropzone.addEventListener("click", function () {
|
||||
|
||||
Reference in New Issue
Block a user