messages show up again

This commit is contained in:
Ida Dittrich 2025-09-02 07:32:08 +02:00
parent a530eaf521
commit 55c17a5c9e
3 changed files with 38 additions and 12 deletions

View file

@ -91,7 +91,8 @@ const MessageList: React.FC<MessageListProps> = ({ 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);

View file

@ -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
};
};

View file

@ -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<string | null> => {
// 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,