Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mcp_feedback_enhanced/web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def create_session(self, project_directory: str, summary: str) -> str:
if old_session and old_session.websocket:
old_websocket = old_session.websocket
debug_log("保存舊會話的 WebSocket 連接以發送更新通知")

old_session.next_step("反饋處理完成")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Calling old_session.next_step() here might lead to an incorrect state transition if the session is already in a submitted state. Consider checking the session's state before calling next_step() to avoid unintended side effects.

# 創建新會話
session_id = str(uuid.uuid4())
session = WebFeedbackSession(session_id, project_directory, summary)
Expand Down
96 changes: 81 additions & 15 deletions src/mcp_feedback_enhanced/web/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,11 @@
// 更新 AI 摘要區域顯示「已送出反饋」狀態
const submittedMessage = window.i18nManager ? window.i18nManager.t('feedback.submittedWaiting') : '已送出反饋,等待下次 MCP 調用...';
this.updateSummaryStatus(submittedMessage);


// 刷新會話列表以顯示最新狀態
console.log('🔄 反饋提交成功,刷新會話列表');
this.refreshSessionList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a duplicate call to this.refreshSessionList() on line 775. Remove this redundant call to prevent unnecessary reloads of the session list.


// 執行提交回饋後的自動命令
this.executeAutoCommandOnFeedbackSubmit();

Expand All @@ -780,6 +784,7 @@
// 如果有會話管理器,觸發數據刷新
if (this.sessionManager && this.sessionManager.dataManager) {
console.log('🔄 刷新會話列表以顯示最新狀態');
// 然後從服務器加載最新數據
this.sessionManager.dataManager.loadFromServer();
} else {
console.log('⚠️ 會話管理器未初始化,跳過會話列表刷新');
Expand Down Expand Up @@ -851,29 +856,90 @@
window.MCPFeedback.Utils.CONSTANTS.MESSAGE_SUCCESS
);

// 局部更新頁面內容而非開啟新視窗
// 智能選擇更新策略:音效通知啟用時使用局部更新,否則使用 window.open
const self = this;
setTimeout(function() {
console.log('🔄 執行局部更新頁面內容');
// 檢查音效通知是否啟用
const audioNotificationEnabled = self.audioManager &&
self.audioManager.currentAudioSettings &&
self.audioManager.currentAudioSettings.enabled;

if (audioNotificationEnabled) {
console.log('🔊 音效通知已啟用,使用局部更新策略');

// 使用局部更新方案
// 1. 更新會話資訊
if (data.session_info) {
self.currentSessionId = data.session_info.session_id;
console.log('📋 新會話 ID:', self.currentSessionId);
}

// 1. 更新會話資訊
if (data.session_info) {
self.currentSessionId = data.session_info.session_id;
console.log('📋 新會話 ID:', self.currentSessionId);
}
// 2. 刷新頁面內容(AI 摘要、表單等)
self.refreshPageContent();

// 3. 重置表單狀態
self.clearFeedback();

} else {
console.log('🔇 音效通知未啟用,使用 window.open 策略');

try {
// 嘗試打開新標籤頁
const newWindow = window.open(window.location.href, '_blank');

if (newWindow) {
console.log('✅ 新標籤頁打開成功,準備關閉當前標籤頁');

// 2. 刷新頁面內容(AI 摘要、表單等)
self.refreshPageContent();
// 短暫延遲後關閉當前標籤頁
setTimeout(function() {
console.log('🔄 關閉當前標籤頁');
window.close();
}, 800); // 給新標籤頁一些時間加載

} else {
console.warn('❌ window.open 被阻止,回退到局部更新');

// 回退到局部更新方案
// 1. 更新會話資訊
if (data.session_info) {
self.currentSessionId = data.session_info.session_id;
console.log('📋 新會話 ID:', self.currentSessionId);
}

// 2. 刷新頁面內容(AI 摘要、表單等)
self.refreshPageContent();

// 3. 重置表單狀態
self.clearFeedback();
}
} catch (error) {
console.error('❌ window.open 執行失敗:', error);

// 回退到局部更新方案
// 1. 更新會話資訊
if (data.session_info) {
self.currentSessionId = data.session_info.session_id;
console.log('📋 新會話 ID:', self.currentSessionId);
}

// 2. 刷新頁面內容(AI 摘要、表單等)
self.refreshPageContent();

// 3. 重置表單狀態
self.clearFeedback();
}
}
Comment on lines +867 to +931

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The partial page update logic is repeated in the if/else block. Extract this logic into a separate function to improve code maintainability and reduce redundancy. This will make the code easier to understand and modify in the future.


// 3. 重置表單狀態
self.clearFeedback();
// 4. 強制刷新會話列表(新會話創建時)
console.log('🔄 新會話創建,強制刷新會話列表');
self.refreshSessionList(true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The refreshSessionList function doesn't accept any arguments. Remove the true argument from this call.

Suggested change
self.refreshSessionList(true);
self.refreshSessionList();


// 4. 重置回饋狀態為等待中
// 5. 重置回饋狀態為等待中
if (self.uiManager) {
self.uiManager.setFeedbackState(window.MCPFeedback.Utils.CONSTANTS.FEEDBACK_WAITING, self.currentSessionId);
}

// 5. 重新啟動會話超時計時器(如果已啟用)
// 6. 重新啟動會話超時計時器(如果已啟用)
if (self.settingsManager && self.settingsManager.get('sessionTimeoutEnabled')) {
console.log('🔄 新會話創建,重新啟動會話超時計時器');
const timeoutSettings = {
Expand All @@ -883,7 +949,7 @@
self.webSocketManager.updateSessionTimeoutSettings(timeoutSettings);
}

// 6. 檢查並啟動自動提交
// 7. 檢查並啟動自動提交
self.checkAndStartAutoSubmit();

console.log('✅ 局部更新完成,頁面已準備好接收新的回饋');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,11 @@

const self = this;

// 檢查數據是否有變化(簡單比較長度)
if (self.lastRenderedData.historyLength === sessionHistory.length) {
// 長度沒有變化,跳過渲染(可以進一步優化為深度比較)
// 檢查數據是否有變化(比較長度和狀態)
const currentHistoryHash = self.calculateHistoryHash(sessionHistory);
if (self.lastRenderedData.historyLength === sessionHistory.length &&
self.lastRenderedData.historyHash === currentHistoryHash) {
Comment on lines +400 to +401

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a comment explaining why both historyLength and historyHash are checked. Clarifying the purpose of the hash check will improve code understanding.

// 長度和狀態都沒有變化,跳過渲染
return;
}

Expand All @@ -412,6 +414,20 @@
}, self.renderDebounceDelay);
};

/**
* 計算會話歷史的哈希值(用於檢測狀態變化)
*/
SessionUIRenderer.prototype.calculateHistoryHash = function(sessionHistory) {
if (!sessionHistory || sessionHistory.length === 0) {
return 'empty';
}

// 計算基於會話 ID 和狀態的簡單哈希
return sessionHistory.map(session =>
`${session.session_id}:${session.status}:${session.feedback_completed}`
).join('|');
};

/**
* 執行實際的會話歷史渲染
*/
Expand All @@ -420,6 +436,7 @@

// 更新快取
this.lastRenderedData.historyLength = sessionHistory.length;
this.lastRenderedData.historyHash = this.calculateHistoryHash(sessionHistory);

// 清空現有內容
DOMUtils.clearElement(this.historyList);
Expand Down