mirror of
https://github.com/Minidoracat/mcp-feedback-enhanced.git
synced 2025-07-27 10:42:25 +08:00
用户信息保存到服务器
This commit is contained in:
parent
33c94b79be
commit
bc87d4b580
@ -131,6 +131,7 @@ class WebFeedbackSession:
|
|||||||
self.feedback_completed = threading.Event()
|
self.feedback_completed = threading.Event()
|
||||||
self.process: subprocess.Popen | None = None
|
self.process: subprocess.Popen | None = None
|
||||||
self.command_logs: list[str] = []
|
self.command_logs: list[str] = []
|
||||||
|
self.user_messages: list[dict] = [] # 用戶消息記錄
|
||||||
self._cleanup_done = False # 防止重複清理
|
self._cleanup_done = False # 防止重複清理
|
||||||
|
|
||||||
# 新增:會話狀態管理
|
# 新增:會話狀態管理
|
||||||
@ -497,6 +498,22 @@ class WebFeedbackSession:
|
|||||||
|
|
||||||
# 重構:不再自動關閉 WebSocket,保持連接以支援頁面持久性
|
# 重構:不再自動關閉 WebSocket,保持連接以支援頁面持久性
|
||||||
|
|
||||||
|
def add_user_message(self, message_data: dict[str, Any]) -> None:
|
||||||
|
"""添加用戶消息記錄"""
|
||||||
|
import time
|
||||||
|
|
||||||
|
# 創建用戶消息記錄
|
||||||
|
user_message = {
|
||||||
|
"timestamp": int(time.time() * 1000), # 毫秒時間戳
|
||||||
|
"content": message_data.get("content", ""),
|
||||||
|
"images": message_data.get("images", []),
|
||||||
|
"submission_method": message_data.get("submission_method", "manual"),
|
||||||
|
"type": "feedback"
|
||||||
|
}
|
||||||
|
|
||||||
|
self.user_messages.append(user_message)
|
||||||
|
debug_log(f"會話 {self.session_id} 添加用戶消息,總數: {len(self.user_messages)}")
|
||||||
|
|
||||||
def _process_images(self, images: list[dict]) -> list[dict]:
|
def _process_images(self, images: list[dict]) -> list[dict]:
|
||||||
"""
|
"""
|
||||||
處理圖片數據,轉換為統一格式
|
處理圖片數據,轉換為統一格式
|
||||||
|
@ -175,7 +175,8 @@ def setup_routes(manager: "WebUIManager"):
|
|||||||
"last_activity": int(session.last_activity * 1000),
|
"last_activity": int(session.last_activity * 1000),
|
||||||
"feedback_completed": session.feedback_completed.is_set(),
|
"feedback_completed": session.feedback_completed.is_set(),
|
||||||
"has_websocket": session.websocket is not None,
|
"has_websocket": session.websocket is not None,
|
||||||
"is_current": session == manager.current_session
|
"is_current": session == manager.current_session,
|
||||||
|
"user_messages": session.user_messages # 包含用戶消息記錄
|
||||||
}
|
}
|
||||||
sessions_data.append(session_info)
|
sessions_data.append(session_info)
|
||||||
|
|
||||||
@ -192,6 +193,32 @@ def setup_routes(manager: "WebUIManager"):
|
|||||||
content={"error": f"獲取會話狀態失敗: {e!s}"}
|
content={"error": f"獲取會話狀態失敗: {e!s}"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@manager.app.post("/api/add-user-message")
|
||||||
|
async def add_user_message(request: Request):
|
||||||
|
"""添加用戶消息到當前會話"""
|
||||||
|
try:
|
||||||
|
data = await request.json()
|
||||||
|
current_session = manager.get_current_session()
|
||||||
|
|
||||||
|
if not current_session:
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=404,
|
||||||
|
content={"error": "沒有活躍會話"}
|
||||||
|
)
|
||||||
|
|
||||||
|
# 添加用戶消息到會話
|
||||||
|
current_session.add_user_message(data)
|
||||||
|
|
||||||
|
debug_log(f"用戶消息已添加到會話 {current_session.session_id}")
|
||||||
|
return JSONResponse(content={"status": "success", "message": "用戶消息已記錄"})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
debug_log(f"添加用戶消息失敗: {e}")
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=500,
|
||||||
|
content={"error": f"添加用戶消息失敗: {e!s}"}
|
||||||
|
)
|
||||||
|
|
||||||
@manager.app.websocket("/ws")
|
@manager.app.websocket("/ws")
|
||||||
async def websocket_endpoint(websocket: WebSocket):
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
"""WebSocket 端點 - 重構後移除 session_id 依賴"""
|
"""WebSocket 端點 - 重構後移除 session_id 依賴"""
|
||||||
|
@ -313,6 +313,9 @@
|
|||||||
// 記錄用戶最後互動時間
|
// 記錄用戶最後互動時間
|
||||||
this.currentSession.last_user_interaction = TimeUtils.getCurrentTimestamp();
|
this.currentSession.last_user_interaction = TimeUtils.getCurrentTimestamp();
|
||||||
|
|
||||||
|
// 發送用戶消息到服務器端
|
||||||
|
this.sendUserMessageToServer(userMessage);
|
||||||
|
|
||||||
// 立即保存當前會話到伺服器
|
// 立即保存當前會話到伺服器
|
||||||
this.saveCurrentSessionToServer();
|
this.saveCurrentSessionToServer();
|
||||||
|
|
||||||
@ -320,6 +323,29 @@
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 發送用戶消息到服務器端
|
||||||
|
*/
|
||||||
|
SessionDataManager.prototype.sendUserMessageToServer = function(userMessage) {
|
||||||
|
fetch('/api/add-user-message', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(userMessage)
|
||||||
|
})
|
||||||
|
.then(function(response) {
|
||||||
|
if (response.ok) {
|
||||||
|
console.log('📊 用戶消息已發送到服務器端');
|
||||||
|
} else {
|
||||||
|
console.warn('📊 發送用戶消息到服務器端失敗:', response.status);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function(error) {
|
||||||
|
console.warn('📊 發送用戶消息到服務器端出錯:', error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 建立用戶訊息記錄
|
* 建立用戶訊息記錄
|
||||||
*/
|
*/
|
||||||
@ -457,16 +483,24 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根據 ID 查找會話
|
* 根據 ID 查找會話(包含完整的用戶消息數據)
|
||||||
*/
|
*/
|
||||||
SessionDataManager.prototype.findSessionById = function(sessionId) {
|
SessionDataManager.prototype.findSessionById = function(sessionId) {
|
||||||
// 先檢查當前會話
|
// 先檢查當前會話
|
||||||
if (this.currentSession && this.currentSession.session_id === sessionId) {
|
if (this.currentSession && this.currentSession.session_id === sessionId) {
|
||||||
|
console.log('📊 從當前會話獲取數據:', sessionId, '用戶消息數量:', this.currentSession.user_messages ? this.currentSession.user_messages.length : 0);
|
||||||
return this.currentSession;
|
return this.currentSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 再檢查歷史記錄
|
// 再檢查歷史記錄
|
||||||
return this.sessionHistory.find(s => s.session_id === sessionId) || null;
|
const historySession = this.sessionHistory.find(s => s.session_id === sessionId);
|
||||||
|
if (historySession) {
|
||||||
|
console.log('📊 從歷史記錄獲取數據:', sessionId, '用戶消息數量:', historySession.user_messages ? historySession.user_messages.length : 0);
|
||||||
|
return historySession;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn('📊 找不到會話:', sessionId);
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user