feat: Enhance chat message handling by appending assistant messages and triggering project refresh on session updates

This commit is contained in:
simos
2025-08-12 14:48:47 +03:00
parent cdce59edb4
commit 50f6cdfac9

View File

@@ -1910,11 +1910,23 @@ function ChatInterface({ selectedProject, selectedSession, ws, sendMessage, mess
// Handle Cursor streaming format (content_block_delta / content_block_stop) // Handle Cursor streaming format (content_block_delta / content_block_stop)
if (messageData && typeof messageData === 'object' && messageData.type) { if (messageData && typeof messageData === 'object' && messageData.type) {
if (messageData.type === 'content_block_delta' && messageData.delta?.text) { if (messageData.type === 'content_block_delta' && messageData.delta?.text) {
setChatMessages(prev => [...prev, { setChatMessages(prev => {
type: 'assistant', // Check if the last message is an assistant message we can append to
content: messageData.delta.text, if (prev.length > 0 && prev[prev.length - 1].type === 'assistant' && !prev[prev.length - 1].isToolUse) {
timestamp: new Date() // Append to the last assistant message
}]); const updatedMessages = [...prev];
const lastMessage = updatedMessages[updatedMessages.length - 1];
lastMessage.content = (lastMessage.content || '') + messageData.delta.text;
return updatedMessages;
} else {
// Create a new assistant message for the first delta
return [...prev, {
type: 'assistant',
content: messageData.delta.text,
timestamp: new Date()
}];
}
});
return; return;
} }
if (messageData.type === 'content_block_stop') { if (messageData.type === 'content_block_stop') {
@@ -2168,10 +2180,15 @@ function ChatInterface({ selectedProject, selectedSession, ws, sendMessage, mess
onSessionInactive(cursorSessionId); onSessionInactive(cursorSessionId);
} }
// Store session ID for future use // Store session ID for future use and trigger refresh
if (cursorSessionId && !currentSessionId) { if (cursorSessionId && !currentSessionId) {
setCurrentSessionId(cursorSessionId); setCurrentSessionId(cursorSessionId);
sessionStorage.removeItem('pendingSessionId'); sessionStorage.removeItem('pendingSessionId');
// Trigger a project refresh to update the sidebar with the new session
if (window.refreshProjects) {
setTimeout(() => window.refreshProjects(), 500);
}
} }
break; break;
@@ -2223,6 +2240,11 @@ function ChatInterface({ selectedProject, selectedSession, ws, sendMessage, mess
if (pendingSessionId && !currentSessionId && latestMessage.exitCode === 0) { if (pendingSessionId && !currentSessionId && latestMessage.exitCode === 0) {
setCurrentSessionId(pendingSessionId); setCurrentSessionId(pendingSessionId);
sessionStorage.removeItem('pendingSessionId'); sessionStorage.removeItem('pendingSessionId');
// Trigger a project refresh to update the sidebar with the new session
if (window.refreshProjects) {
setTimeout(() => window.refreshProjects(), 500);
}
} }
// Clear persisted chat messages after successful completion // Clear persisted chat messages after successful completion
@@ -2877,7 +2899,7 @@ function ChatInterface({ selectedProject, selectedSession, ws, sendMessage, mess
</div> </div>
) : chatMessages.length === 0 ? ( ) : chatMessages.length === 0 ? (
<div className="flex items-center justify-center h-full"> <div className="flex items-center justify-center h-full">
{!selectedSession && ( {!selectedSession && !currentSessionId && (
<div className="text-center px-6 sm:px-4 py-8"> <div className="text-center px-6 sm:px-4 py-8">
<h2 className="text-2xl font-bold text-gray-900 dark:text-white mb-3">Choose Your AI Assistant</h2> <h2 className="text-2xl font-bold text-gray-900 dark:text-white mb-3">Choose Your AI Assistant</h2>
<p className="text-gray-600 dark:text-gray-400 mb-8"> <p className="text-gray-600 dark:text-gray-400 mb-8">