Files
supabase/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx
Charis a4d925c230 fix(studio): write debug prompt into current chat when assistant is already open (#49911)
## Summary

* Query blocks embedded inside an active assistant conversation
(`AssistantQueryCell`) reused the same "Debug with Assistant" handler as
standalone query blocks (Explorer Query tab, notebook cells), which
always opens a brand-new chat and navigates away.
* Clicking Debug on a block that's already part of the open conversation
silently abandoned it for an unrelated new chat, which read as the
button doing nothing.
* Added an optional `onDebug` override threaded through `QueryEditor` →
`QueryResultRenderer` → `QueryResultError`; `AssistantQueryCell` now
uses it to write the debug prompt into the currently active chat's
composer (`ai-assistant-state`'s new `setInitialInput`) instead of
creating a new chat. Standalone query blocks keep the existing "open a
new chat" behavior since no `onDebug` override is passed there.
* `ExplorerChatTab` now wires `composerContext` into `AssistantChat` (it
wasn't before), so the pre-filled prompt actually reaches the visible
textarea on the Explorer chat route.

Fixes
[FE-4319](https://linear.app/supabase/issue/FE-4319/debug-with-ai-assistant-does-seemingly-nothing-if-query-is-already).

## Test plan

- [X] `pnpm vitest run` on `QueryResultError.test.tsx` /
`QueryResultError.selfhosted.test.tsx` / `ExplorerChatTab.test.tsx` /
`AssistantQueryCell.utils.test.ts` — all pass, including new test
asserting `onDebug` is called instead of `createChat`.
- [X] `pnpm exec eslint` on touched files — clean (only pre-existing
unrelated warnings).
- [X] Manual check: run a query inside an assistant chat that errors,
click "Debug with Assistant" on that block, confirm the debug prompt
appears in the current chat's composer rather than opening a new chat.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Added a “Debug with Assistant” workflow that sends SQL error details
to the AI Assistant as its initial input.
* Preserved the existing behavior of opening a new debug chat when the
Assistant panel is unavailable.

* **Tests**
* Added coverage confirming that debugging invokes the Assistant
callback without creating an additional chat.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-04 09:26:18 -04:00

96 lines
3.4 KiB
TypeScript

import { useParams } from 'common'
import { Loader2, MessageSquare } from 'lucide-react'
import { useRouter } from 'next/router'
import { useEffect, useEffectEvent } from 'react'
import { Button } from 'ui'
import { ExplorerChatToolbar } from './ExplorerChatToolbar'
import { useCreateChat } from './hooks'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { AssistantChat } from '@/components/ui/AIAssistantPanel/AssistantChat'
import { useAiAssistantState, useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
export const ExplorerChatTab = () => {
const { id, ref } = useParams()
const router = useRouter()
const tabs = useTabsStateSnapshot()
const aiAssistant = useAiAssistantStateSnapshot()
const aiAssistantState = useAiAssistantState()
const { createChat, openChat } = useCreateChat()
const { activeSidebar } = useSidebarManagerSnapshot()
const chat = id ? aiAssistant.chats[id] : undefined
const chatInstance = id ? aiAssistant.chatInstances[id] : undefined
const tabId = id ? createTabId('chat', { id }) : undefined
const shortcutsEnabled = activeSidebar?.id !== SIDEBAR_KEYS.AI_ASSISTANT
const ensureChatInstance = useEffectEvent(() => {
if (!id || !chat) return
aiAssistantState.ensureChatInstance(id)
})
const removeDeletedChatTab = useEffectEvent(() => {
if (!tabId || !tabs.openTabs.includes(tabId)) return
tabs.handleTabClose({
id: tabId,
router,
editor: 'explorer',
onClearDashboardHistory: () => {},
})
})
useEffect(() => ensureChatInstance(), [id, chat])
useEffect(() => {
if (aiAssistant.isInitialized && id && !chat) removeDeletedChatTab()
}, [aiAssistant.isInitialized, id, chat])
if (!aiAssistant.isInitialized || (chat && !chatInstance)) {
return (
<div className="h-full bg-surface-100 flex items-center justify-center">
<Loader2 className="animate-spin text-foreground-muted" />
</div>
)
}
if (!id || !chat) {
return (
<div className="h-full bg-surface-100 flex flex-col items-center justify-center gap-3 px-6 text-center">
<MessageSquare className="text-foreground-muted" />
<div>
<h2 className="text-sm text-foreground">Chat not found</h2>
<p className="text-sm text-foreground-lighter">
This chat may have been deleted or is no longer available.
</p>
</div>
<Button variant="default" onClick={() => router.push(`/project/${ref}/explorer`)}>
Back to Explorer
</Button>
</div>
)
}
const handleBranchChat = (messageId: string) => {
const branchId = aiAssistantState.createBranch(id, messageId)
if (branchId) openChat(branchId)
}
return (
<AssistantChat
chatId={id}
shortcutsEnabled={shortcutsEnabled}
className="bg-surface-100"
onNewChat={() => createChat()}
onSelectChat={openChat}
onBranchChat={handleBranchChat}
composerContext={{ initialInput: aiAssistant.initialInput }}
onInputChange={() => tabs.makeTabPermanent(createTabId('chat', { id }))}
renderHeader={(headerProps) => (
<ExplorerChatToolbar {...headerProps} chatId={id} shortcutsEnabled={shortcutsEnabled} />
)}
/>
)
}