From 1b707c8e510460c65f9a6fefa498364aad666be6 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 03:31:07 -0800 Subject: [PATCH 1/3] fix(copilot): block duplicate chat deletes and leave the open chat first --- .../w/[workflowId]/components/panel/panel.tsx | 29 ++++++++++--------- apps/sim/hooks/queries/copilot-chats.ts | 17 +++++++++-- 2 files changed, 30 insertions(+), 16 deletions(-) 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 87261b2504d..2a2dfa9d329 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 @@ -30,10 +30,7 @@ import { useShallow } from 'zustand/react/shallow' import { VariableIcon } from '@/components/icons' import { ThinkingLoader } from '@/components/ui' import { requestJson } from '@/lib/api/client/request' -import { - createWorkflowCopilotChatContract, - deleteCopilotChatContract, -} from '@/lib/api/contracts/copilot' +import { createWorkflowCopilotChatContract } from '@/lib/api/contracts/copilot' import { getWorkflowNormalizedStateContract } from '@/lib/api/contracts/workflows' import { useSession } from '@/lib/auth/client' import { getWorkspaceUsageLimitAction } from '@/lib/billing/workspace-permissions' @@ -79,6 +76,7 @@ import { type CopilotChatListItem, copilotChatsKeys, useCopilotChats, + useDeleteCopilotChat, } from '@/hooks/queries/copilot-chats' import { useFolderMap } from '@/hooks/queries/folders' import { isWorkflowEffectivelyLocked } from '@/hooks/queries/utils/folder-tree' @@ -393,23 +391,25 @@ export const Panel = memo(function Panel() { [setCopilotChatId] ) + const { mutate: deleteCopilotChat, isPending: isDeletingCopilotChat } = useDeleteCopilotChat( + activeWorkflowId ?? undefined + ) const handleCopilotDeleteChat = useCallback( (chatId: string) => { - requestJson(deleteCopilotChatContract, { body: { chatId } }) - .then(() => { - if (copilotChatId === chatId) { - setCopilotChatId(undefined) - } + // Leave the chat first: its deleted event drops the open transcript, which would refetch. + if (copilotChatId === chatId) setCopilotChatId(undefined) + deleteCopilotChat(chatId, { + onSuccess: () => { // The draft store is persisted, so an unpruned key survives forever. const draftKey = copilotDraftKey(workspaceId, copilotDraftWorkflowId, chatId) if (draftKey) useMothershipDraftsStore.getState().clearDraft(draftKey) - loadCopilotChats() - }) - .catch((err) => { + }, + onError: (err) => { logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) - }) + }, + }) }, - [copilotChatId, loadCopilotChats, setCopilotChatId, workspaceId, copilotDraftWorkflowId] + [copilotChatId, deleteCopilotChat, setCopilotChatId, workspaceId, copilotDraftWorkflowId] ) const handleCopilotToolResult = useCallback( @@ -1095,6 +1095,7 @@ export const Panel = memo(function Panel() { action={ handleCopilotDeleteChat(chat.id)} > diff --git a/apps/sim/hooks/queries/copilot-chats.ts b/apps/sim/hooks/queries/copilot-chats.ts index e9df553dbff..c5087ee67c3 100644 --- a/apps/sim/hooks/queries/copilot-chats.ts +++ b/apps/sim/hooks/queries/copilot-chats.ts @@ -1,7 +1,11 @@ -import { skipToken, useQuery } from '@tanstack/react-query' +import { skipToken, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { ApiClientError } from '@/lib/api/client/errors' import { requestJson } from '@/lib/api/client/request' -import { type CopilotChatListItem, listCopilotChatsContract } from '@/lib/api/contracts/copilot' +import { + type CopilotChatListItem, + deleteCopilotChatContract, + listCopilotChatsContract, +} from '@/lib/api/contracts/copilot' export type { CopilotChatListItem } @@ -38,3 +42,12 @@ export function useCopilotChats(workflowId?: string) { staleTime: COPILOT_CHAT_LIST_STALE_TIME, }) } + +/** Deletes a workflow copilot chat and refreshes that workflow's chat list. */ +export function useDeleteCopilotChat(workflowId?: string) { + const queryClient = useQueryClient() + return useMutation({ + mutationFn: (chatId: string) => requestJson(deleteCopilotChatContract, { body: { chatId } }), + onSuccess: () => queryClient.invalidateQueries({ queryKey: copilotChatsKeys.list(workflowId) }), + }) +} From 63fccde998ccaeecbab6f5a20a22e5fd7346b93b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 03:41:19 -0800 Subject: [PATCH 2/3] fix(copilot): keep a deleting chat from auto-reopening and restore it on failure --- .../w/[workflowId]/components/panel/panel.tsx | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) 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 2a2dfa9d329..90ab79c7d59 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 @@ -71,7 +71,10 @@ import { useDeleteWorkflow } from '@/app/workspace/[workspaceId]/w/hooks' import { RequestAccessModal } from '@/ee/access-requests/components/request-access-action' import { getMyAccessRequestHref } from '@/ee/access-requests/lib/navigation' import { useDiscoverAccessRequests } from '@/hooks/queries/access-requests' -import { useCopilotChatSelection } from '@/hooks/queries/copilot-chat-selection' +import { + copilotChatSelectionKeys, + useCopilotChatSelection, +} from '@/hooks/queries/copilot-chat-selection' import { type CopilotChatListItem, copilotChatsKeys, @@ -396,8 +399,13 @@ export const Panel = memo(function Panel() { ) const handleCopilotDeleteChat = useCallback( (chatId: string) => { + const wasOpen = copilotChatId === chatId // Leave the chat first: its deleted event drops the open transcript, which would refetch. - if (copilotChatId === chatId) setCopilotChatId(undefined) + // Auto-select must not reopen it from the list while the delete is in flight. + if (wasOpen) { + if (activeWorkflowId) autoSelectAttemptedFor.add(activeWorkflowId) + setCopilotChatId(undefined) + } deleteCopilotChat(chatId, { onSuccess: () => { // The draft store is persisted, so an unpruned key survives forever. @@ -406,10 +414,23 @@ export const Panel = memo(function Panel() { }, onError: (err) => { logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) + const selection = queryClient.getQueryData( + copilotChatSelectionKeys.workflow(activeWorkflowId ?? undefined) + ) + if (wasOpen && !selection) setCopilotChatId(chatId) }, }) }, - [copilotChatId, deleteCopilotChat, setCopilotChatId, workspaceId, copilotDraftWorkflowId] + [ + activeWorkflowId, + autoSelectAttemptedFor, + copilotChatId, + copilotDraftWorkflowId, + deleteCopilotChat, + queryClient, + setCopilotChatId, + workspaceId, + ] ) const handleCopilotToolResult = useCallback( From c65c1d08cc9bd036e0ea489ba58a17daa13c7a3a Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 03:53:28 -0800 Subject: [PATCH 3/3] fix(copilot): prune drafts and invalidate from mutation variables, drop failure restore --- .../w/[workflowId]/components/panel/panel.tsx | 38 +++++++------------ apps/sim/hooks/queries/copilot-chats.ts | 22 +++++++++-- 2 files changed, 32 insertions(+), 28 deletions(-) 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 90ab79c7d59..2ec3ba00662 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 @@ -71,10 +71,7 @@ import { useDeleteWorkflow } from '@/app/workspace/[workspaceId]/w/hooks' import { RequestAccessModal } from '@/ee/access-requests/components/request-access-action' import { getMyAccessRequestHref } from '@/ee/access-requests/lib/navigation' import { useDiscoverAccessRequests } from '@/hooks/queries/access-requests' -import { - copilotChatSelectionKeys, - useCopilotChatSelection, -} from '@/hooks/queries/copilot-chat-selection' +import { useCopilotChatSelection } from '@/hooks/queries/copilot-chat-selection' import { type CopilotChatListItem, copilotChatsKeys, @@ -90,7 +87,6 @@ import { usePermissionConfig } from '@/hooks/use-permission-config' import { useSettingsNavigation } from '@/hooks/use-settings-navigation' import { useChatStore } from '@/stores/chat/store' import { useVariablesModalStore } from '@/stores/modals/variables/store' -import { useMothershipDraftsStore } from '@/stores/mothership-drafts/store' import type { ChatContext, PanelTab } from '@/stores/panel' import { usePanelStore } from '@/stores/panel' import { useVariablesStore } from '@/stores/variables/store' @@ -394,32 +390,27 @@ export const Panel = memo(function Panel() { [setCopilotChatId] ) - const { mutate: deleteCopilotChat, isPending: isDeletingCopilotChat } = useDeleteCopilotChat( - activeWorkflowId ?? undefined - ) + const { mutate: deleteCopilotChat, isPending: isDeletingCopilotChat } = useDeleteCopilotChat() const handleCopilotDeleteChat = useCallback( (chatId: string) => { - const wasOpen = copilotChatId === chatId // 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 (wasOpen) { + if (copilotChatId === chatId) { if (activeWorkflowId) autoSelectAttemptedFor.add(activeWorkflowId) setCopilotChatId(undefined) } - deleteCopilotChat(chatId, { - onSuccess: () => { - // The draft store is persisted, so an unpruned key survives forever. - const draftKey = copilotDraftKey(workspaceId, copilotDraftWorkflowId, chatId) - if (draftKey) useMothershipDraftsStore.getState().clearDraft(draftKey) - }, - onError: (err) => { - logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) - const selection = queryClient.getQueryData( - copilotChatSelectionKeys.workflow(activeWorkflowId ?? undefined) - ) - if (wasOpen && !selection) setCopilotChatId(chatId) + deleteCopilotChat( + { + chatId, + workflowId: activeWorkflowId ?? undefined, + draftKey: copilotDraftKey(workspaceId, copilotDraftWorkflowId, chatId), }, - }) + { + onError: (err) => { + logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) + }, + } + ) }, [ activeWorkflowId, @@ -427,7 +418,6 @@ export const Panel = memo(function Panel() { copilotChatId, copilotDraftWorkflowId, deleteCopilotChat, - queryClient, setCopilotChatId, workspaceId, ] diff --git a/apps/sim/hooks/queries/copilot-chats.ts b/apps/sim/hooks/queries/copilot-chats.ts index c5087ee67c3..4cefd12496c 100644 --- a/apps/sim/hooks/queries/copilot-chats.ts +++ b/apps/sim/hooks/queries/copilot-chats.ts @@ -6,6 +6,7 @@ import { deleteCopilotChatContract, listCopilotChatsContract, } from '@/lib/api/contracts/copilot' +import { useMothershipDraftsStore } from '@/stores/mothership-drafts/store' export type { CopilotChatListItem } @@ -43,11 +44,24 @@ export function useCopilotChats(workflowId?: string) { }) } -/** Deletes a workflow copilot chat and refreshes that workflow's chat list. */ -export function useDeleteCopilotChat(workflowId?: string) { +interface DeleteCopilotChatVariables { + chatId: string + /** Workflow whose chat list drops the chat, captured when the delete starts. */ + workflowId?: string + /** Persisted composer draft to prune once the chat is gone. */ + draftKey?: string +} + +/** Deletes a workflow copilot chat, prunes its draft, and refreshes its workflow's chat list. */ +export function useDeleteCopilotChat() { const queryClient = useQueryClient() return useMutation({ - mutationFn: (chatId: string) => requestJson(deleteCopilotChatContract, { body: { chatId } }), - onSuccess: () => queryClient.invalidateQueries({ queryKey: copilotChatsKeys.list(workflowId) }), + mutationFn: ({ chatId }: DeleteCopilotChatVariables) => + requestJson(deleteCopilotChatContract, { body: { chatId } }), + onSuccess: (_data, { workflowId, draftKey }) => { + // The draft store is persisted, so an unpruned key survives forever. + if (draftKey) useMothershipDraftsStore.getState().clearDraft(draftKey) + return queryClient.invalidateQueries({ queryKey: copilotChatsKeys.list(workflowId) }) + }, }) }