Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): write debug prompt into current chat when assistant is already open (#49911)

Charisa4d925c23062
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx b/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx
index 21552016d2c..7ed5a1b5c80 100644
--- a/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx
+++ b/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx
@@ -85,6 +85,7 @@ export const ExplorerChatTab = () => {
onNewChat={() => createChat()}
onSelectChat={openChat}
onBranchChat={handleBranchChat}
+ composerContext={{ initialInput: aiAssistant.initialInput }}
onInputChange={() => tabs.makeTabPermanent(createTabId('chat', { id }))}
renderHeader={(headerProps) => (
<ExplorerChatToolbar {...headerProps} chatId={id} shortcutsEnabled={shortcutsEnabled} />
diff --git a/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.test.tsx b/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.test.tsx
index 574860d4a9f..f7d6b75c223 100644
--- a/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.test.tsx
+++ b/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.test.tsx
@@ -105,6 +105,24 @@ describe('QueryResultError', () => {
)
})
+ it('calls onDebug with the prompt instead of opening a new chat when provided', () => {
+ const onDebug = vi.fn()
+
+ customRender(
+ <QueryResultError
+ error={{ message: 'relation "foo" does not exist' }}
+ sql="select * from foo;"
+ source="database"
+ onDebug={onDebug}
+ />
+ )
+
+ fireEvent.click(screen.getByRole('button', { name: 'Debug with Assistant' }))
+
+ expect(onDebug).toHaveBeenCalledWith(expect.stringContaining('select * from foo;'))
+ expect(mocks.createChat).not.toHaveBeenCalled()
+ })
+
it('copies the same debug prompt text via the dropdown', async () => {
const user = userEvent.setup()
customRender(
diff --git a/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.tsx b/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.tsx
index d83b46ed85f..c3a0863e695 100644
--- a/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.tsx
+++ b/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultError.tsx
@@ -23,11 +23,16 @@ export const QueryResultError = ({
autoLimit,
sql,
source,
+ onDebug,
}: {
error: NonNullable<QueryResult['error']>
autoLimit?: QueryResult['autoLimit']
sql?: string
source?: SqlSnippetSource
+ /** Overrides the default "open a new debug chat" behavior — used when this query block
+ * is already rendered inside an open assistant conversation, so debugging should write
+ * into that conversation's composer instead of abandoning it for a new chat. */
+ onDebug?: (prompt: string) => void
}) => {
const { ref } = useParams()
@@ -61,7 +66,9 @@ export const QueryResultError = ({
)
const handleDebug = () =>
- createChat({ name: 'Debug SQL snippet', initialMessage: buildDebugPrompt() })
+ onDebug
+ ? onDebug(buildDebugPrompt())
+ : createChat({ name: 'Debug SQL snippet', initialMessage: buildDebugPrompt() })
const isTimeout =
error.message?.includes('canceling statement due to statement timeout') ||
diff --git a/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultRenderer.tsx b/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultRenderer.tsx
index 52442928cfb..a389395cd07 100644
--- a/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultRenderer.tsx
+++ b/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultRenderer.tsx
@@ -12,6 +12,7 @@ interface QueryResultRendererProps {
/** The query that produced `result`, used to build the "Debug with Assistant" prompt on error. */
sql?: string
source?: SqlSnippetSource
+ onDebug?: (prompt: string) => void
}
export const QueryResultRenderer = ({
@@ -20,6 +21,7 @@ export const QueryResultRenderer = ({
chart,
sql,
source,
+ onDebug,
}: QueryResultRendererProps) => {
const { rows, error, autoLimit } = result ?? {}
@@ -28,7 +30,15 @@ export const QueryResultRenderer = ({
}
if (error) {
- return <QueryResultError error={error} autoLimit={autoLimit} sql={sql} source={source} />
+ return (
+ <QueryResultError
+ error={error}
+ autoLimit={autoLimit}
+ sql={sql}
+ source={source}
+ onDebug={onDebug}
+ />
+ )
}
if ((rows ?? []).length === 0) {
diff --git a/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx b/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx
index f8fc7176096..e8b3287bd09 100644
--- a/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx
+++ b/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx
@@ -141,6 +141,7 @@ type QueryEditorProps = {
onRowLimitChange?: (val: number) => void
onDisplayChange?: (display: QueryDisplay) => void
onRun?: () => void
+ onDebug?: (prompt: string) => void
}
/**
@@ -171,6 +172,7 @@ export const QueryEditor = forwardRef<QueryEditorHandle, QueryEditorProps>(funct
onRowLimitChange,
onDisplayChange,
onRun,
+ onDebug,
}: QueryEditorProps,
ref
) {
@@ -603,6 +605,7 @@ export const QueryEditor = forwardRef<QueryEditorHandle, QueryEditorProps>(funct
chart={display?.chart}
sql={result?.sql}
source={result?.source}
+ onDebug={onDebug}
/>
</ExplorerQueryResults>
diff --git a/apps/studio/components/ui/AIAssistantPanel/AssistantQueryCell.tsx b/apps/studio/components/ui/AIAssistantPanel/AssistantQueryCell.tsx
index 8612987d991..b8d3f4d30c0 100644
--- a/apps/studio/components/ui/AIAssistantPanel/AssistantQueryCell.tsx
+++ b/apps/studio/components/ui/AIAssistantPanel/AssistantQueryCell.tsx
@@ -19,6 +19,7 @@ import {
type QuerySourceTag,
} from '@/data/query-sources/query-source-registry'
import { useTrack } from '@/lib/telemetry/track'
+import { useAiAssistantState } from '@/state/ai-assistant-state'
import { useLocalRoleImpersonationState } from '@/state/role-impersonation-state'
interface AssistantQueryCellProps {
@@ -72,6 +73,7 @@ export const AssistantQueryCell = ({
}: AssistantQueryCellProps) => {
const track = useTrack()
const roleImpersonationState = useLocalRoleImpersonationState()
+ const aiAssistantState = useAiAssistantState()
const fallbackTitle =
initialTitle?.trim() ||
@@ -180,6 +182,7 @@ export const AssistantQueryCell = ({
}
onDisplayChange={handleDisplayChange}
onRun={handleRun}
+ onDebug={aiAssistantState.setInitialInput}
/>
</Confirm>
)
diff --git a/apps/studio/state/ai-assistant-state.tsx b/apps/studio/state/ai-assistant-state.tsx
index b86c82e3a11..2e418b93ad2 100644
--- a/apps/studio/state/ai-assistant-state.tsx
+++ b/apps/studio/state/ai-assistant-state.tsx
@@ -426,6 +426,10 @@ export const createAiAssistantState = (): AiAssistantState => {
state.model = model
},
+ setInitialInput: (text: string) => {
+ state.initialInput = text
+ },
+
// Chat management
get activeChat(): ChatSession | undefined {
return state.activeChatId ? state.chats[state.activeChatId] : undefined
@@ -716,6 +720,7 @@ export type AiAssistantState = AiAssistantData & {
isInitialized: boolean
setContext: (context: Partial<AiAssistantContext>) => void
setModel: (model: AssistantModel) => void
+ setInitialInput: (text: string) => void
createChat: (options?: CreateChatOptions) => string
newChat: (options?: NewChatOptions) => string
createBranch: (sourceChatId: string, messageId: string) => string | undefined