messages show up again
This commit is contained in:
parent
a530eaf521
commit
55c17a5c9e
3 changed files with 38 additions and 12 deletions
|
|
@ -91,7 +91,8 @@ const MessageList: React.FC<MessageListProps> = ({ workflowState }) => {
|
||||||
try {
|
try {
|
||||||
const transformed = await Promise.all(all.map(msg => transformWorkflowMessage(msg, request)));
|
const transformed = await Promise.all(all.map(msg => transformWorkflowMessage(msg, request)));
|
||||||
setMessages(transformed);
|
setMessages(transformed);
|
||||||
} catch {
|
} catch (error) {
|
||||||
|
console.error('❌ Message transformation failed:', error);
|
||||||
setMessages([]);
|
setMessages([]);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|
|
||||||
|
|
@ -118,8 +118,8 @@ export const transformWorkflowMessage = async (msg: any, request: any): Promise<
|
||||||
docs = msg.documents.map((d: any) => ({
|
docs = msg.documents.map((d: any) => ({
|
||||||
id: d.id || d.fileId,
|
id: d.id || d.fileId,
|
||||||
fileId: typeof d.fileId === 'string' ? parseInt(d.fileId) : d.fileId,
|
fileId: typeof d.fileId === 'string' ? parseInt(d.fileId) : d.fileId,
|
||||||
name: d.filename,
|
name: d.filename || `File_${d.id || d.fileId || 'unknown'}`,
|
||||||
ext: d.filename.split('.').pop() || 'unknown',
|
ext: (d.filename && d.filename.includes('.')) ? d.filename.split('.').pop() : 'unknown',
|
||||||
type: d.mimeType,
|
type: d.mimeType,
|
||||||
size: d.fileSize,
|
size: d.fileSize,
|
||||||
downloadUrl: `/api/workflows/files/${d.fileId}/download`
|
downloadUrl: `/api/workflows/files/${d.fileId}/download`
|
||||||
|
|
@ -149,7 +149,9 @@ export const transformWorkflowMessage = async (msg: any, request: any): Promise<
|
||||||
role: msg.role,
|
role: msg.role,
|
||||||
agentName: msg.role === 'user' ? 'You' : 'Assistant',
|
agentName: msg.role === 'user' ? 'You' : 'Assistant',
|
||||||
content: msg.message || msg.content || msg.text || msg.body || '',
|
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
|
documents: docs
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -42,19 +42,28 @@ export function useWorkflowManager(initialWorkflowId?: string | null): [Workflow
|
||||||
const isLoading = workflowLoading || statusLoading || messagesLoading || logsLoading;
|
const isLoading = workflowLoading || statusLoading || messagesLoading || logsLoading;
|
||||||
const error = workflowError || statusError || messagesError || logsError;
|
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(() => {
|
const filteredMessages = useMemo(() => {
|
||||||
if (!messages) return [];
|
if (!messages) return [];
|
||||||
|
|
||||||
// If we've tracked ANY sent messages, always filter out user messages from backend
|
// For completed/stopped workflows, always show all messages (including user messages from backend)
|
||||||
// This prevents flickering during new workflow creation
|
const isWorkflowComplete = currentWorkflow && ['completed', 'stopped', 'failed', 'error'].includes(currentWorkflow.status);
|
||||||
if (sentUserMessages.size > 0) {
|
|
||||||
|
// 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');
|
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;
|
return messages;
|
||||||
}, [messages, sentUserMessages]);
|
}, [messages, sentUserMessages, pendingMessages, currentWorkflow]);
|
||||||
|
|
||||||
// Auto-polling for active workflows and message updates
|
// Auto-polling for active workflows and message updates
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -81,8 +90,14 @@ export function useWorkflowManager(initialWorkflowId?: string | null): [Workflow
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
const loadWorkflow = useCallback(async (workflowId: string) => {
|
const loadWorkflow = useCallback(async (workflowId: string) => {
|
||||||
|
// Immediately clear pending state when loading a different workflow
|
||||||
|
if (currentWorkflowId !== workflowId) {
|
||||||
|
setPendingMessages([]);
|
||||||
|
setSentUserMessages(new Set());
|
||||||
|
}
|
||||||
|
|
||||||
setCurrentWorkflowId(workflowId);
|
setCurrentWorkflowId(workflowId);
|
||||||
}, []);
|
}, [currentWorkflowId]);
|
||||||
|
|
||||||
const startNewWorkflow = useCallback(async (prompt: string, fileIds: string[] = []): Promise<string | null> => {
|
const startNewWorkflow = useCallback(async (prompt: string, fileIds: string[] = []): Promise<string | null> => {
|
||||||
// Add optimistic message immediately
|
// Add optimistic message immediately
|
||||||
|
|
@ -226,10 +241,18 @@ export function useWorkflowManager(initialWorkflowId?: string | null): [Workflow
|
||||||
if (currentWorkflowId && currentWorkflow) {
|
if (currentWorkflowId && currentWorkflow) {
|
||||||
const isActive = ['running', 'processing', 'started'].includes(currentWorkflow.status);
|
const isActive = ['running', 'processing', 'started'].includes(currentWorkflow.status);
|
||||||
setIsPolling(isActive);
|
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 {
|
} else {
|
||||||
setIsPolling(false);
|
setIsPolling(false);
|
||||||
}
|
}
|
||||||
}, [currentWorkflowId, currentWorkflow?.status]);
|
}, [currentWorkflowId, currentWorkflow?.status, pendingMessages.length, sentUserMessages.size]);
|
||||||
|
|
||||||
const state: WorkflowState = {
|
const state: WorkflowState = {
|
||||||
currentWorkflowId,
|
currentWorkflowId,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue