diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx index 2ec3ba00662..a1550ea4304 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx @@ -331,9 +331,13 @@ export const Panel = memo(function Panel() { const canMutateWorkflow = userPermissions.canEdit && !workflowLocked const { isSnapshotView } = useCurrentWorkflow() - const { chatId: copilotChatId, setChatId: setCopilotChatId } = useCopilotChatSelection( - activeWorkflowId ?? undefined - ) + const { + chatId: copilotChatId, + setChatId: setCopilotChatId, + peekChatId: peekCopilotChatId, + } = useCopilotChatSelection(activeWorkflowId ?? undefined) + /** No transcript and no send in flight, read by callbacks that outlive the render that made them. */ + const copilotPanelIdleRef = useRef(true) const copilotDraftWorkflowId = activeWorkflowId ?? scopedWorkflowId ?? undefined const copilotDraftScopeKey = copilotDraftKey(workspaceId, copilotDraftWorkflowId, copilotChatId) @@ -395,7 +399,8 @@ export const Panel = memo(function Panel() { (chatId: string) => { // Leave the chat first: its deleted event drops the open transcript, which would refetch. // Auto-select must not reopen it from the list while the delete is in flight. - if (copilotChatId === chatId) { + const wasOpen = copilotChatId === chatId + if (wasOpen) { if (activeWorkflowId) autoSelectAttemptedFor.add(activeWorkflowId) setCopilotChatId(undefined) } @@ -408,6 +413,9 @@ export const Panel = memo(function Panel() { { onError: (err) => { logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) + // The chat still exists: reopen it only while the panel shows no other chat or conversation. + if (wasOpen && !peekCopilotChatId() && copilotPanelIdleRef.current) + setCopilotChatId(chatId) }, } ) @@ -418,6 +426,7 @@ export const Panel = memo(function Panel() { copilotChatId, copilotDraftWorkflowId, deleteCopilotChat, + peekCopilotChatId, setCopilotChatId, workspaceId, ] @@ -526,6 +535,10 @@ export const Panel = memo(function Panel() { } }, [copilotResolvedChatId, copilotChatId, loadCopilotChats, setCopilotChatId]) + useEffect(() => { + copilotPanelIdleRef.current = !copilotIsSending && copilotMessages.length === 0 + }, [copilotIsSending, copilotMessages.length]) + const wasCopilotSendingRef = useRef(false) useEffect(() => { if (wasCopilotSendingRef.current && !copilotIsSending) { diff --git a/apps/sim/hooks/queries/copilot-chat-selection.ts b/apps/sim/hooks/queries/copilot-chat-selection.ts index db58cde327c..153d653d289 100644 --- a/apps/sim/hooks/queries/copilot-chat-selection.ts +++ b/apps/sim/hooks/queries/copilot-chat-selection.ts @@ -34,5 +34,13 @@ export function useCopilotChatSelection(workflowId?: string) { [workflowId, queryClient] ) - return { chatId: chatId ?? undefined, setChatId } + /** The workflow's selection at call time, for callbacks that outlive the render that made them. */ + const peekChatId = useCallback( + () => + queryClient.getQueryData(copilotChatSelectionKeys.workflow(workflowId)) ?? + undefined, + [workflowId, queryClient] + ) + + return { chatId: chatId ?? undefined, setChatId, peekChatId } } diff --git a/apps/sim/hooks/queries/copilot-chats.ts b/apps/sim/hooks/queries/copilot-chats.ts index 4cefd12496c..9bc8602f4b9 100644 --- a/apps/sim/hooks/queries/copilot-chats.ts +++ b/apps/sim/hooks/queries/copilot-chats.ts @@ -56,8 +56,15 @@ interface DeleteCopilotChatVariables { export function useDeleteCopilotChat() { const queryClient = useQueryClient() return useMutation({ - mutationFn: ({ chatId }: DeleteCopilotChatVariables) => - requestJson(deleteCopilotChatContract, { body: { chatId } }), + mutationFn: async ({ chatId }: DeleteCopilotChatVariables) => { + try { + await requestJson(deleteCopilotChatContract, { body: { chatId } }) + } catch (error) { + // Deleted elsewhere between the route's lookup and its delete: the chat is gone either way. + if (error instanceof ApiClientError && error.status === 404) return + throw error + } + }, onSuccess: (_data, { workflowId, draftKey }) => { // The draft store is persisted, so an unpruned key survives forever. if (draftKey) useMothershipDraftsStore.getState().clearDraft(draftKey)