From 55c17a5c9ea1c6286caae3f4d6a21bb2a5cc0be7 Mon Sep 17 00:00:00 2001 From: Ida Dittrich Date: Tue, 2 Sep 2025 07:32:08 +0200 Subject: [PATCH] messages show up again --- .../DashboardChatAreaMessageList.tsx | 3 +- .../dashboardChatAreaProgressBar.ts | 8 ++-- .../DashboardChat/useWorkflowManager.ts | 39 +++++++++++++++---- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/components/Dashboard/DashboardChat/DashboardChatAreaMessageList.tsx b/src/components/Dashboard/DashboardChat/DashboardChatAreaMessageList.tsx index 46a72a6..cf1261e 100644 --- a/src/components/Dashboard/DashboardChat/DashboardChatAreaMessageList.tsx +++ b/src/components/Dashboard/DashboardChat/DashboardChatAreaMessageList.tsx @@ -91,7 +91,8 @@ const MessageList: React.FC = ({ workflowState }) => { try { const transformed = await Promise.all(all.map(msg => transformWorkflowMessage(msg, request))); setMessages(transformed); - } catch { + } catch (error) { + console.error('❌ Message transformation failed:', error); setMessages([]); } finally { setIsLoading(false); diff --git a/src/components/Dashboard/DashboardChat/dashboardChatAreaProgressBar.ts b/src/components/Dashboard/DashboardChat/dashboardChatAreaProgressBar.ts index c9154f3..6ab8391 100644 --- a/src/components/Dashboard/DashboardChat/dashboardChatAreaProgressBar.ts +++ b/src/components/Dashboard/DashboardChat/dashboardChatAreaProgressBar.ts @@ -118,8 +118,8 @@ export const transformWorkflowMessage = async (msg: any, request: any): Promise< docs = msg.documents.map((d: any) => ({ id: d.id || d.fileId, fileId: typeof d.fileId === 'string' ? parseInt(d.fileId) : d.fileId, - name: d.filename, - ext: d.filename.split('.').pop() || 'unknown', + name: d.filename || `File_${d.id || d.fileId || 'unknown'}`, + ext: (d.filename && d.filename.includes('.')) ? d.filename.split('.').pop() : 'unknown', type: d.mimeType, size: d.fileSize, downloadUrl: `/api/workflows/files/${d.fileId}/download` @@ -149,7 +149,9 @@ export const transformWorkflowMessage = async (msg: any, request: any): Promise< role: msg.role, agentName: msg.role === 'user' ? 'You' : 'Assistant', content: msg.message || msg.content || msg.text || msg.body || '', - timestamp: msg.publishedAt || msg.timestamp, + timestamp: msg.publishedAt + ? (typeof msg.publishedAt === 'number' ? new Date(msg.publishedAt * 1000).toISOString() : msg.publishedAt) + : msg.timestamp, documents: docs }; }; diff --git a/src/components/Dashboard/DashboardChat/useWorkflowManager.ts b/src/components/Dashboard/DashboardChat/useWorkflowManager.ts index fb9cc45..59f929d 100644 --- a/src/components/Dashboard/DashboardChat/useWorkflowManager.ts +++ b/src/components/Dashboard/DashboardChat/useWorkflowManager.ts @@ -42,19 +42,28 @@ export function useWorkflowManager(initialWorkflowId?: string | null): [Workflow const isLoading = workflowLoading || statusLoading || messagesLoading || logsLoading; const error = workflowError || statusError || messagesError || logsError; - // Filter out ALL user messages from backend - we only show user messages from pendingMessages + // Filter messages based on workflow state and pending messages const filteredMessages = useMemo(() => { if (!messages) return []; - // If we've tracked ANY sent messages, always filter out user messages from backend - // This prevents flickering during new workflow creation - if (sentUserMessages.size > 0) { + // For completed/stopped workflows, always show all messages (including user messages from backend) + const isWorkflowComplete = currentWorkflow && ['completed', 'stopped', 'failed', 'error'].includes(currentWorkflow.status); + + // If workflow is complete OR we have no pending messages and no tracked sent messages, + // show all messages (this covers historical workflows and completed workflows) + if (isWorkflowComplete || (pendingMessages.length === 0 && sentUserMessages.size === 0)) { + return messages; + } + + // For active workflows with pending messages, filter out user messages from backend + // to prevent duplicates with optimistic messages + if (pendingMessages.length > 0 || sentUserMessages.size > 0) { return messages.filter(msg => msg.role !== 'user'); } - // If no tracked sent messages, this is a historical workflow - show all messages + // Default: show all messages return messages; - }, [messages, sentUserMessages]); + }, [messages, sentUserMessages, pendingMessages, currentWorkflow]); // Auto-polling for active workflows and message updates useEffect(() => { @@ -81,8 +90,14 @@ export function useWorkflowManager(initialWorkflowId?: string | null): [Workflow // Actions const loadWorkflow = useCallback(async (workflowId: string) => { + // Immediately clear pending state when loading a different workflow + if (currentWorkflowId !== workflowId) { + setPendingMessages([]); + setSentUserMessages(new Set()); + } + setCurrentWorkflowId(workflowId); - }, []); + }, [currentWorkflowId]); const startNewWorkflow = useCallback(async (prompt: string, fileIds: string[] = []): Promise => { // Add optimistic message immediately @@ -226,10 +241,18 @@ export function useWorkflowManager(initialWorkflowId?: string | null): [Workflow if (currentWorkflowId && currentWorkflow) { const isActive = ['running', 'processing', 'started'].includes(currentWorkflow.status); setIsPolling(isActive); + + // Clear pending messages and sent user tracking when workflow completes + // This allows all backend messages to show for completed workflows + if (!isActive && (pendingMessages.length > 0 || sentUserMessages.size > 0)) { + console.log('🏁 Workflow completed, clearing pending messages and sent user tracking'); + setPendingMessages([]); + setSentUserMessages(new Set()); + } } else { setIsPolling(false); } - }, [currentWorkflowId, currentWorkflow?.status]); + }, [currentWorkflowId, currentWorkflow?.status, pendingMessages.length, sentUserMessages.size]); const state: WorkflowState = { currentWorkflowId,