feat(trace): improve session workbench review

This commit is contained in:
zhuyongxin
2026-07-07 19:06:18 +08:00
parent 3b62a8940c
commit 0ee27eb523
9 changed files with 895 additions and 104 deletions
+50 -2
View File
@@ -8,6 +8,7 @@ class SuperBizAgentApp {
this.currentChatHistory = []; // 当前对话的消息历史
this.chatHistories = this.loadChatHistories(); // 所有历史对话
this.isCurrentChatFromHistory = false; // 标记当前对话是否是从历史记录加载的
this.lastTraceSessionId = this.loadLastTraceSessionId();
this.initializeElements();
this.bindEvents();
@@ -15,6 +16,7 @@ class SuperBizAgentApp {
this.initMarkdown();
this.checkAndSetCentered();
this.renderChatHistory();
this.updateTraceWorkbenchLink();
}
// 初始化Markdown配置
@@ -116,6 +118,7 @@ class SuperBizAgentApp {
this.chatContainer = document.querySelector('.chat-container');
this.welcomeGreeting = document.getElementById('welcomeGreeting');
this.chatHistoryList = document.getElementById('chatHistoryList');
this.traceWorkbenchLink = document.getElementById('traceWorkbenchLink');
// 初始化时检查是否需要居中
this.checkAndSetCentered();
@@ -265,6 +268,7 @@ class SuperBizAgentApp {
// 生成新的会话ID
this.sessionId = this.generateSessionId();
this.updateTraceWorkbenchLink();
// 重置模式为快速
this.currentMode = 'quick';
@@ -312,6 +316,7 @@ class SuperBizAgentApp {
// 添加到历史记录列表的开头
this.chatHistories.unshift(chatHistory);
this.rememberTraceSessionId(this.sessionId);
// 限制历史记录数量(最多保存50条)
if (this.chatHistories.length > 50) {
@@ -439,6 +444,7 @@ class SuperBizAgentApp {
// 加载历史对话
this.sessionId = history.id;
this.rememberTraceSessionId(this.sessionId);
this.currentChatHistory = [...history.messages];
this.isCurrentChatFromHistory = true; // 标记为从历史记录加载
@@ -468,10 +474,45 @@ class SuperBizAgentApp {
this.chatMessages.innerHTML = '';
}
this.sessionId = this.generateSessionId();
this.updateTraceWorkbenchLink();
this.checkAndSetCentered();
}
}
loadLastTraceSessionId() {
try {
return localStorage.getItem('lastTraceSessionId') || '';
} catch (e) {
return '';
}
}
rememberTraceSessionId(sessionId) {
if (!sessionId) {
return;
}
this.lastTraceSessionId = sessionId;
try {
localStorage.setItem('lastTraceSessionId', sessionId);
} catch (e) {
// localStorage may be unavailable in private or restricted contexts.
}
this.updateTraceWorkbenchLink();
}
updateTraceWorkbenchLink() {
if (!this.traceWorkbenchLink) {
return;
}
const recentHistory = this.chatHistories.find(history => history && history.id);
const sessionId = this.lastTraceSessionId
|| (this.currentChatHistory.length > 0 ? this.sessionId : '')
|| (recentHistory ? recentHistory.id : '');
this.traceWorkbenchLink.href = sessionId
? `trace.html?sessionId=${encodeURIComponent(sessionId)}`
: 'trace.html';
}
// 切换模式下拉菜单
toggleModeDropdown() {
if (this.modeSelectorBtn && this.modeDropdown) {
@@ -637,6 +678,7 @@ class SuperBizAgentApp {
// 保存后端返回的 sessionId,用于 feedback 提交
if (chatResponse.sessionId) {
this.lastSessionId = chatResponse.sessionId;
this.rememberTraceSessionId(chatResponse.sessionId);
}
// 成功:添加实际响应消息(即使 answer 为空也显示)
const answer = chatResponse.answer || '(无回复内容)';
@@ -1231,7 +1273,10 @@ class SuperBizAgentApp {
for (const jsonStr of matches) {
try {
const sseMessage = JSON.parse(jsonStr);
if (sseMessage.type === 'content') {
if (sseMessage.type === 'session') {
this.lastSessionId = sseMessage.data;
this.rememberTraceSessionId(sseMessage.data);
} else if (sseMessage.type === 'content') {
fullResponse += sseMessage.data || '';
} else if (sseMessage.type === 'done') {
console.log('AI Ops 流完成,最终内容长度:', fullResponse.length);
@@ -1261,7 +1306,10 @@ class SuperBizAgentApp {
try {
const sseMessage = JSON.parse(rawData);
if (sseMessage && sseMessage.type) {
if (sseMessage.type === 'content') {
if (sseMessage.type === 'session') {
this.lastSessionId = sseMessage.data;
this.rememberTraceSessionId(sseMessage.data);
} else if (sseMessage.type === 'content') {
fullResponse += sseMessage.data || '';
if (loadingMessageElement) {
this.updateAIOpsStreamContent(loadingMessageElement, fullResponse);