Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -89,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'
Expand Down Expand Up @@ -393,23 +390,37 @@ export const Panel = memo(function Panel() {
[setCopilotChatId]
)

const { mutate: deleteCopilotChat, isPending: isDeletingCopilotChat } = useDeleteCopilotChat()
const handleCopilotDeleteChat = useCallback(
(chatId: string) => {
requestJson(deleteCopilotChatContract, { body: { chatId } })
.then(() => {
if (copilotChatId === chatId) {
setCopilotChatId(undefined)
}
// 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) => {
logger.error('Failed to delete copilot chat', { error: toError(err).message, 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 (copilotChatId === chatId) {
if (activeWorkflowId) autoSelectAttemptedFor.add(activeWorkflowId)
setCopilotChatId(undefined)
}
deleteCopilotChat(
{
chatId,
workflowId: activeWorkflowId ?? undefined,
draftKey: copilotDraftKey(workspaceId, copilotDraftWorkflowId, chatId),
},
{
onError: (err) => {
logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId })
},
}
)
},
[copilotChatId, loadCopilotChats, setCopilotChatId, workspaceId, copilotDraftWorkflowId]
[
activeWorkflowId,
autoSelectAttemptedFor,
copilotChatId,
copilotDraftWorkflowId,
deleteCopilotChat,
setCopilotChatId,
workspaceId,
]
)

const handleCopilotToolResult = useCallback(
Expand Down Expand Up @@ -1095,6 +1106,7 @@ export const Panel = memo(function Panel() {
action={
<DropdownMenuItemAction
aria-label='Delete chat'
disabled={isDeletingCopilotChat}
onClick={() => handleCopilotDeleteChat(chat.id)}
>
<Trash />
Expand Down
31 changes: 29 additions & 2 deletions apps/sim/hooks/queries/copilot-chats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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'
import { useMothershipDraftsStore } from '@/stores/mothership-drafts/store'

export type { CopilotChatListItem }

Expand Down Expand Up @@ -38,3 +43,25 @@ export function useCopilotChats(workflowId?: string) {
staleTime: COPILOT_CHAT_LIST_STALE_TIME,
})
}

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 }: 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) })
},
})
}
Loading