Skip to content

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

Add assistant CTA for query errors in RLS tester (#45628)

Joshen Lim94851d1f8ff1
diff --git a/apps/studio/components/interfaces/Auth/RLSTester/RLSTesterSheet.tsx b/apps/studio/components/interfaces/Auth/RLSTester/RLSTesterSheet.tsx
index 41e139abdaf..e57f1221b14 100644
--- a/apps/studio/components/interfaces/Auth/RLSTester/RLSTesterSheet.tsx
+++ b/apps/studio/components/interfaces/Auth/RLSTester/RLSTesterSheet.tsx
@@ -39,14 +39,20 @@ import { UserSelector } from './UserSelector'
import { UserSqlEditor } from './UserSqlEditor'
import { useTestQueryRLS } from './useTestQueryRLS'
import type { Policy } from '@/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRow.utils'
+import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
+import { AiAssistantDropdown } from '@/components/ui/AiAssistantDropdown'
import { FeaturePreviewBadge } from '@/components/ui/FeaturePreviewBadge'
+import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
import { useRoleImpersonationStateSnapshot } from '@/state/role-impersonation-state'
+import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
interface RLSTesterSheetProps {
handleSelectEditPolicy: (policy: Policy) => void
}
export const RLSTesterSheet = ({ handleSelectEditPolicy }: RLSTesterSheetProps) => {
+ const aiSnap = useAiAssistantStateSnapshot()
+ const { openSidebar } = useSidebarManagerSnapshot()
const { setRole } = useRoleImpersonationStateSnapshot()
const [open, setOpen] = useState(false)
@@ -102,6 +108,25 @@ export const RLSTesterSheet = ({ handleSelectEditPolicy }: RLSTesterSheetProps)
}
}
+ const assistantSql = format === 'lib' && inferredSQL ? acceptUntrustedSql(inferredSQL) : value
+
+ const getDebugPrompt = ({ includeSql = false }: { includeSql?: boolean } = {}) => {
+ const prompt = `Help me fix my RLS policy based on the attached SQL snippet that gave the following error: \n\n${executeSqlError?.message}\n\nEvaluate if the problem might be query first, before checking my RLS policies.`
+
+ return includeSql ? `${prompt}\n\nSQL Query:\n\`\`\`sql\n${assistantSql}\n\`\`\`` : prompt
+ }
+
+ const onDebugWithAssistant = () => {
+ const prompt = getDebugPrompt()
+ openSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
+ aiSnap.newChat({
+ name: 'Debug RLS policies',
+ sqlSnippets: [assistantSql],
+ initialInput: prompt,
+ })
+ setOpen(false)
+ }
+
useEffect(() => {
setRole({ type: 'postgrest', role: 'anon' })
// Flip back to service role
@@ -197,7 +222,7 @@ export const RLSTesterSheet = ({ handleSelectEditPolicy }: RLSTesterSheetProps)
<DialogSectionSeparator />
- {parseQueryError && (
+ {parseQueryError ? (
<div className="p-4">
<Admonition
type="warning"
@@ -205,9 +230,7 @@ export const RLSTesterSheet = ({ handleSelectEditPolicy }: RLSTesterSheetProps)
description={parseQueryError.message}
/>
</div>
- )}
-
- {parseClientCodeError && (
+ ) : parseClientCodeError ? (
<div className="p-4">
<Admonition
type="warning"
@@ -215,20 +238,29 @@ export const RLSTesterSheet = ({ handleSelectEditPolicy }: RLSTesterSheetProps)
description={parseClientCodeError.message}
/>
</div>
- )}
-
- {executeSqlError && (
- <div className="p-4">
- <Admonition
- type="warning"
- title="Error running SQL query"
- description={executeSqlError.message}
- />
- </div>
+ ) : (
+ executeSqlError && (
+ <div className="p-4">
+ <Admonition
+ type="warning"
+ title="Error running SQL query"
+ description={executeSqlError.message}
+ actions={[
+ <AiAssistantDropdown
+ key="ai-assistant"
+ label="Ask Assistant"
+ telemetrySource="rls_tester"
+ buildPrompt={() => getDebugPrompt({ includeSql: true })}
+ onOpenAssistant={onDebugWithAssistant}
+ />,
+ ]}
+ />
+ </div>
+ )
)}
{results === null ? (
- <RLSTesterEmptyState />
+ !parseQueryError && !parseClientCodeError && !executeSqlError && <RLSTesterEmptyState />
) : !!parseQueryResults ? (
<RLSTesterResults
results={results}
diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.ts
index 7f2fe2d91b3..3154ff64a05 100644
--- a/packages/common/telemetry-constants.ts
+++ b/packages/common/telemetry-constants.ts
@@ -2740,6 +2740,7 @@ export type AiAssistantSource =
| 'log_explorer'
| 'error_code'
| 'advisor_signal_detail'
+ | 'rls_tester'
/**
* User copied an AI prompt to clipboard instead of using the built-in assistant.