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 @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
},
}
)
Expand All @@ -418,6 +426,7 @@ export const Panel = memo(function Panel() {
copilotChatId,
copilotDraftWorkflowId,
deleteCopilotChat,
peekCopilotChatId,
setCopilotChatId,
workspaceId,
]
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion apps/sim/hooks/queries/copilot-chat-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null>(copilotChatSelectionKeys.workflow(workflowId)) ??
undefined,
[workflowId, queryClient]
)

return { chatId: chatId ?? undefined, setChatId, peekChatId }
}
11 changes: 9 additions & 2 deletions apps/sim/hooks/queries/copilot-chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading