feat(conversations): 支持删除对话并优化侧栏

This commit is contained in:
2026-06-08 21:39:38 +08:00
parent 2244b69d62
commit ef0a9ee13e
7 changed files with 918 additions and 17 deletions

View File

@@ -26,6 +26,13 @@
return;
}
function getCsrfToken() {
if (!composer) {
return "";
}
return new FormData(composer).get("csrfmiddlewaretoken") || "";
}
function isMobile() {
return window.matchMedia("(max-width: 980px)").matches;
}
@@ -415,19 +422,60 @@
empty.remove();
}
var item = document.createElement("a");
var item = document.createElement("div");
item.className = "history-item active";
item.setAttribute("data-conversation-id", conversationId);
item.href = "/?conversation=" + conversationId;
item.setAttribute("data-delete-url", "/api/review-agent/conversations/" + conversationId + "/");
item.innerHTML =
'<span class="history-title">' +
'<a class="history-link" href="/?conversation=' +
encodeURIComponent(conversationId) +
'"><span class="history-title">' +
escapeHtml(encodedTitle) +
'</span><span class="history-meta">' +
meta +
"</span>";
'</span></a><button class="history-delete" type="button" data-conversation-delete aria-label="删除对话 ' +
escapeHtml(encodedTitle) +
'" title="删除对话">×</button>';
list.prepend(item);
}
async function deleteConversation(item) {
if (!item) {
return;
}
var url = item.getAttribute("data-delete-url");
var conversationId = item.getAttribute("data-conversation-id");
if (!url || !conversationId) {
return;
}
var titleNode = item.querySelector(".history-title");
var title = titleNode ? titleNode.textContent.trim() : "这个对话";
if (!window.confirm('确定删除对话“' + title + '”?')) {
return;
}
var response = await fetch(url, {
method: "DELETE",
headers: {
"X-CSRFToken": getCsrfToken(),
},
});
if (!response.ok) {
throw new Error("删除对话失败");
}
var isCurrent = currentConversationId() === conversationId;
item.remove();
var list = document.querySelector(".history-list");
if (list && !list.querySelector(".history-item")) {
var empty = document.createElement("div");
empty.className = "history-empty";
empty.innerHTML = "<p>暂无会话记录</p><span>点击上方“新对话”开始审核。</span>";
list.appendChild(empty);
}
if (isCurrent) {
window.location.href = "/";
}
}
function setConversationTitle(title) {
if (!title) {
return;
@@ -1167,6 +1215,25 @@
});
}
function bindConversationDeleteButtons() {
var list = document.querySelector(".history-list");
if (!list) {
return;
}
list.addEventListener("click", function (event) {
var button = event.target.closest("[data-conversation-delete]");
if (!button) {
return;
}
event.preventDefault();
event.stopPropagation();
var item = button.closest(".history-item");
deleteConversation(item).catch(function () {
window.alert("删除对话失败,请稍后重试。");
});
});
}
syncNodeRailVisibility();
syncLatestMessageIdFromDom();
bindNodeAnchorClicks();
@@ -1176,6 +1243,7 @@
bindConditionConfirmForms();
bindRectificationActionButtons();
bindPromptTemplateButtons();
bindConversationDeleteButtons();
refreshRunningWorkflowCards();
if (chatScroll) {