Skip to content

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

let the assistant decide when to run (#30789)

Saxon Fletcherccb26246f4e3
diff --git a/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx b/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx
index 14c6dfc9606..ff9f68ca873 100644
--- a/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx
+++ b/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx
@@ -62,6 +62,7 @@ const SqlSnippetWrapper = ({
isChart={props.isChart === 'true'}
xAxis={props.xAxis}
yAxis={props.yAxis}
+ runQuery={props.runQuery === 'true'}
title={title}
readOnly={readOnly}
isLoading={isLoading}
@@ -76,6 +77,7 @@ interface ParsedSqlProps {
isLoading?: boolean
readOnly?: boolean
isChart: boolean
+ runQuery?: boolean
xAxis: string
yAxis: string
}
@@ -83,6 +85,7 @@ interface ParsedSqlProps {
export const SqlCard = ({
sql,
isChart,
+ runQuery = false,
xAxis,
yAxis,
title,
@@ -100,7 +103,7 @@ export const SqlCard = ({
const isInSQLEditor = router.pathname.includes('/sql')
const isInNewSnippet = router.pathname.endsWith('/sql')
- const [showCode, setShowCode] = useState(readOnly || !isReadOnlySelect(sql))
+ const [showCode, setShowCode] = useState(readOnly || !runQuery || !isReadOnlySelect(sql))
const [showResults, setShowResults] = useState(false)
const [results, setResults] = useState<any[]>()
const [error, setError] = useState<QueryResponseError>()
@@ -163,11 +166,11 @@ export const SqlCard = ({
(error?.formattedError?.split('\n') ?? [])?.filter((x: string) => x.length > 0) ?? []
useEffect(() => {
- if (isReadOnlySelect(sql) && !results && !readOnly && !isLoading) {
+ if (runQuery && isReadOnlySelect(sql) && !results && !readOnly && !isLoading) {
handleExecute()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [sql, readOnly, isLoading])
+ }, [sql, readOnly, isLoading, runQuery])
return (
<div className="overflow-hidden rounded border w-auto bg-surface-100">
diff --git a/apps/studio/pages/api/ai/sql/generate-v3.ts b/apps/studio/pages/api/ai/sql/generate-v3.ts
index 50aee1ef7a6..e7325b0829c 100644
--- a/apps/studio/pages/api/ai/sql/generate-v3.ts
+++ b/apps/studio/pages/api/ai/sql/generate-v3.ts
@@ -76,9 +76,10 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
- Always use semicolons
- Output as markdown
- Always include code snippets if available
- - If a code snippet is SQL, the first line of the snippet should always be -- props: {"title": "Query title", "isChart": "true", "xAxis": "columnOrAlias", "yAxis": "columnOrAlias"}
+ - If a code snippet is SQL, the first line of the snippet should always be -- props: {"title": "Query title", "runQuery": "false", "isChart": "true", "xAxis": "columnOrAlias", "yAxis": "columnOrAlias"}
- Only include one line of comment props per markdown snippet, even if the snippet has multiple queries
- Only set chart to true if the query makes sense as a chart. xAxis and yAxis need to be columns or aliases returned by the query.
+ - Only set runQuery to true if the query has no risk of writing data and is not a debugging request. Set it to false if there are any values that need to be replaced with real data.
- Explain what the snippet does in a sentence or two before showing it
- Use vector(384) data type for any embedding/vector related query
- When debugging, retrieve sql schema details to ensure sql is correct