Skip to content

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

wip: opt-in for database metadata in ai requests

Greg Richardson2dceb4098506
diff --git a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
index 4ca52f5d7aa..b4fcb03252a 100644
--- a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
@@ -10,14 +10,25 @@ import {
import { useSqlGenerateMutation } from 'data/ai/sql-generate-mutation'
import { SqlSnippet } from 'data/content/sql-snippets-query'
import { m } from 'framer-motion'
-import { useCheckPermissions, useStore } from 'hooks'
+import { useCheckPermissions, useLocalStorage, useSelectedOrganization, useStore } from 'hooks'
import { uuidv4 } from 'lib/helpers'
import { useProfile } from 'lib/profile'
import Telemetry from 'lib/telemetry'
+import Link from 'next/link'
import { useRouter } from 'next/router'
+import { useState } from 'react'
import { format } from 'sql-formatter'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
-import { AiIcon, IconCornerDownLeft, Input } from 'ui'
+import {
+ AiIcon,
+ Alert,
+ IconCornerDownLeft,
+ IconExternalLink,
+ IconSettings,
+ Input,
+ Modal,
+ Toggle,
+} from 'ui'
import { createSqlSnippetSkeleton } from '../SQLEditor.utils'
import SQLCard from './SQLCard'
@@ -28,6 +39,16 @@ const SQLTemplates = observer(() => {
const { profile } = useProfile()
const [sql, quickStart] = partition(SQL_TEMPLATES, { type: 'template' })
const { mutateAsync: generateSql, isLoading: isSqlGenerateLoading } = useSqlGenerateMutation()
+ const selectedOrganization = useSelectedOrganization()
+ const isOptedInToAI =
+ selectedOrganization?.opt_in_tags?.includes('AI_SQL_GENERATOR_OPT_IN') ?? false
+ const [isOptedInToAISchema, setIsOptedInToAISchema] = useLocalStorage(
+ 'supabase_sql-editor-ai-schema',
+ false
+ )
+ const [isAISettingsOpen, setIsAISettingsOpen] = useState(false)
+
+ const includeSchemaMetadata = isOptedInToAI && isOptedInToAISchema
const telemetryProps = useTelemetryProps()
const snap = useSqlEditorStateSnapshot()
@@ -61,221 +82,264 @@ const SQLTemplates = observer(() => {
}
return (
- <div className="block h-full space-y-8 overflow-y-auto p-6">
- <div className="mt-32 mb-32 flex flex-col items-center">
- <m.h1
- className="text-scale-1200 mb-8 text-3xl"
- variants={{
- visible: {
- y: 0,
- opacity: 1,
- },
- hidden: {
- y: 10,
- opacity: 0,
- },
- }}
- initial="hidden"
- animate="visible"
- >
- What do you want to build?
- </m.h1>
- <div className="w-full flex justify-center">
- {!isSqlGenerateLoading ? (
- <m.div
- key="ask-ai-input-container"
- layoutId="ask-ai-input-container"
- className="w-full max-w-2xl border border-brand-900"
- variants={{
- visible: {
- y: 0,
- opacity: 1,
- borderRadius: 6,
- },
- hidden: {
- y: -50,
- opacity: 0,
- borderRadius: 6,
- },
- }}
- initial="hidden"
- animate="visible"
+ <>
+ <Modal
+ visible={isAISettingsOpen}
+ hideFooter
+ closable
+ header="SQL Editor AI Settings"
+ onCancel={() => setIsAISettingsOpen(false)}
+ >
+ <div className="flex flex-col items-start justify-between gap-4 px-6 py-3">
+ <div className="flex items-center justify-between">
+ <div>
+ <p className="text-sm">Include database metadata in AI requests</p>
+ <p className="text-sm text-scale-1000">
+ Includes table names, column names and their corresponding data types in the
+ request. This will generate snippets that are more relevant to your project.
+ </p>
+ </div>
+ <Toggle
+ disabled={!isOptedInToAI}
+ checked={includeSchemaMetadata}
+ onChange={() => setIsOptedInToAISchema((prev) => !prev)}
+ />
+ </div>
+ {!isOptedInToAI && selectedOrganization && (
+ <Alert
+ variant="warning"
+ title="This option requires the OpenAI data opt-in on your organization"
>
- <Input
- size="xlarge"
- inputRef={(inputElement: HTMLInputElement) => {
- setTimeout(() => {
- inputElement?.focus()
- }, 200)
+ <Link href={`/org/${selectedOrganization.slug}/general`} passHref>
+ <a className="flex flex-row gap-1 items-center" target="_blank" rel="noopener">
+ Go to your organization's settings to opt-in.
+ <IconExternalLink className="inline-block w-3 h-3" />
+ </a>
+ </Link>
+ </Alert>
+ )}
+ </div>
+ </Modal>
+ <div className="block h-full space-y-8 overflow-y-auto p-6">
+ <div className="mt-32 mb-32 flex flex-col items-center">
+ <m.h1
+ className="text-scale-1200 mb-8 text-3xl"
+ variants={{
+ visible: {
+ y: 0,
+ opacity: 1,
+ },
+ hidden: {
+ y: 10,
+ opacity: 0,
+ },
+ }}
+ initial="hidden"
+ animate="visible"
+ >
+ What do you want to build?
+ </m.h1>
+ <div className="w-full flex justify-center">
+ {!isSqlGenerateLoading ? (
+ <m.div
+ key="ask-ai-input-container"
+ layoutId="ask-ai-input-container"
+ className="w-full max-w-2xl border border-brand-900"
+ variants={{
+ visible: {
+ y: 0,
+ opacity: 1,
+ borderRadius: 6,
+ },
+ hidden: {
+ y: -50,
+ opacity: 0,
+ borderRadius: 6,
+ },
}}
- icon={
- <m.div
- key="ask-ai-input-icon"
- layoutId="ask-ai-input-icon"
- className="ml-1"
- variants={{
- visible: {
- rotate: 0,
- },
- }}
- initial="visible"
- animate="visible"
- >
- <AiIcon className="w-4 h-4" />
- </m.div>
- }
- inputClassName="w-full border-none py-4 focus:!ring-0"
- iconContainerClassName="transition text-scale-800 text-brand-900"
- placeholder="Ask Supabase AI to build a query"
- className="w-full"
- onKeyPress={async (e) => {
- if (e.key === 'Enter') {
- try {
- const value = e.currentTarget.value
+ initial="hidden"
+ animate="visible"
+ >
+ <Input
+ size="xlarge"
+ inputRef={(inputElement: HTMLInputElement) => {
+ setTimeout(() => {
+ inputElement?.focus()
+ }, 200)
+ }}
+ icon={
+ <m.div
+ key="ask-ai-input-icon"
+ layoutId="ask-ai-input-icon"
+ className="ml-1"
+ variants={{
+ visible: {
+ rotate: 0,
+ },
+ }}
+ initial="visible"
+ animate="visible"
+ >
+ <AiIcon className="w-4 h-4" />
+ </m.div>
+ }
+ inputClassName="w-full border-none py-4 focus:!ring-0 pr-20"
+ iconContainerClassName="transition text-scale-800 text-brand-900"
+ placeholder="Ask Supabase AI to build a query"
+ className="w-full"
+ onKeyPress={async (e) => {
+ if (e.key === 'Enter') {
+ try {
+ const value = e.currentTarget.value
- if (!value) {
- return
- }
+ if (!value) {
+ return
+ }
- const { title, sql } = await generateSql({ prompt: e.currentTarget.value })
+ const { title, sql } = await generateSql({ prompt: e.currentTarget.value })
- const formattedSql =
- sqlAiDisclaimerComment +
- '\n\n' +
- format(sql, {
- language: 'postgresql',
- keywordCase: 'lower',
- })
+ const formattedSql =
+ sqlAiDisclaimerComment +
+ '\n\n' +
+ format(sql, {
+ language: 'postgresql',
+ keywordCase: 'lower',
+ })
- await handleNewQuery(formattedSql, title)
- } catch (error: unknown) {
- if (
- error &&
- typeof error === 'object' &&
- 'message' in error &&
- typeof error.message === 'string'
- ) {
- ui.setNotification({
- category: 'error',
- message: error.message,
- })
+ await handleNewQuery(formattedSql, title)
+ } catch (error: unknown) {
+ if (
+ error &&
+ typeof error === 'object' &&
+ 'message' in error &&
+ typeof error.message === 'string'
+ ) {
+ ui.setNotification({
+ category: 'error',
+ message: error.message,
+ })
+ }
}
}
+ }}
+ actions={
+ <div className="flex items-center space-x-1 mr-4 gap-2">
+ <IconCornerDownLeft size={16} strokeWidth={1.5} />
+ <IconSettings
+ className="cursor-pointer"
+ onClick={() => setIsAISettingsOpen(true)}
+ />
+ </div>
}
- }}
- actions={
- <div className="flex items-center space-x-1 mr-6">
- <IconCornerDownLeft size={16} strokeWidth={1.5} />
- </div>
- }
- />
- </m.div>
- ) : (
- <m.div
- key="ask-ai-loading"
- layoutId="ask-ai-input-container"
- className="p-5 border border-brand-900 text-brand-900"
- variants={{
- visible: {
- borderRadius: 50,
- },
- }}
- transition={{
- type: 'spring',
- mass: 0.1,
- stiffness: 200,
- damping: 30,
- }}
- initial="visible"
- animate="visible"
- >
+ />
+ </m.div>
+ ) : (
<m.div
- key="ask-ai-loading-icon"
- layoutId="ask-ai-input-icon"
- animate={{
- rotate: 360,
- transition: {
- delay: 0.2,
- ease: 'linear',
- duration: 2,
- repeat: Infinity,
+ key="ask-ai-loading"
+ layoutId="ask-ai-input-container"
+ className="p-5 border border-brand-900 text-brand-900"
+ variants={{
+ visible: {
+ borderRadius: 50,
},
}}
+ transition={{
+ type: 'spring',
+ mass: 0.1,
+ stiffness: 200,
+ damping: 30,
+ }}
+ initial="visible"
+ animate="visible"
>
- <AiIcon className="w-4 h-4" />
+ <m.div
+ key="ask-ai-loading-icon"
+ layoutId="ask-ai-input-icon"
+ animate={{
+ rotate: 360,
+ transition: {
+ delay: 0.2,
+ ease: 'linear',
+ duration: 2,
+ repeat: Infinity,
+ },
+ }}
+ >
+ <AiIcon className="w-4 h-4" />
+ </m.div>
</m.div>
- </m.div>
- )}
- </div>
- </div>
- <div>
- <div className="mb-4">
- <h1 className="text-scale-1200 mb-3 text-xl">Scripts</h1>
- <p className="text-scale-1100 text-sm">Quick scripts to run on your database.</p>
- <p className="text-scale-1100 text-sm">
- Click on any script to fill the query box, modify the script, then click
- <span className="text-code">Run</span>.
- </p>
- </div>
- <div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
- {sql.map((x) => (
- <SQLCard
- key={x.title}
- title={x.title}
- description={x.description}
- sql={x.sql}
- onClick={(sql, title) => {
- handleNewQuery(sql, title)
- Telemetry.sendEvent(
- {
- category: 'scripts',
- action: 'script_clicked',
- label: x.title,
- },
- telemetryProps,
- router
- )
- }}
- />
- ))}
+ )}
+ </div>
</div>
- </div>
- <div className="mb-8">
- <div className="mb-4">
- <h1 className="text-scale-1200 mb-3 text-xl">Quick start</h1>
- <p className="text-scale-1100 text-sm">
- While we're in beta, we want to offer a quick way to explore Supabase. While we build
- importers, check out these simple starters.
- </p>
- <p className="text-scale-1100 text-sm">
- Click on any script to fill the query box, modify the script, then click
- <span className="text-code">Run</span>.
- </p>
+ <div>
+ <div className="mb-4">
+ <h1 className="text-scale-1200 mb-3 text-xl">Scripts</h1>
+ <p className="text-scale-1100 text-sm">Quick scripts to run on your database.</p>
+ <p className="text-scale-1100 text-sm">
+ Click on any script to fill the query box, modify the script, then click
+ <span className="text-code">Run</span>.
+ </p>
+ </div>
+ <div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
+ {sql.map((x) => (
+ <SQLCard
+ key={x.title}
+ title={x.title}
+ description={x.description}
+ sql={x.sql}
+ onClick={(sql, title) => {
+ handleNewQuery(sql, title)
+ Telemetry.sendEvent(
+ {
+ category: 'scripts',
+ action: 'script_clicked',
+ label: x.title,
+ },
+ telemetryProps,
+ router
+ )
+ }}
+ />
+ ))}
+ </div>
</div>
- <div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
- {quickStart.map((x) => (
- <SQLCard
- key={x.title}
- title={x.title}
- description={x.description}
- sql={x.sql}
- onClick={(sql, title) => {
- handleNewQuery(sql, title)
- Telemetry.sendEvent(
- {
- category: 'quickstart',
- action: 'quickstart_clicked',
- label: x.title,
- },
- telemetryProps,
- router
- )
- }}
- />
- ))}
+ <div className="mb-8">
+ <div className="mb-4">
+ <h1 className="text-scale-1200 mb-3 text-xl">Quick start</h1>
+ <p className="text-scale-1100 text-sm">
+ While we're in beta, we want to offer a quick way to explore Supabase. While we build
+ importers, check out these simple starters.
+ </p>
+ <p className="text-scale-1100 text-sm">
+ Click on any script to fill the query box, modify the script, then click
+ <span className="text-code">Run</span>.
+ </p>
+ </div>
+ <div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
+ {quickStart.map((x) => (
+ <SQLCard
+ key={x.title}
+ title={x.title}
+ description={x.description}
+ sql={x.sql}
+ onClick={(sql, title) => {
+ handleNewQuery(sql, title)
+ Telemetry.sendEvent(
+ {
+ category: 'quickstart',
+ action: 'quickstart_clicked',
+ label: x.title,
+ },
+ telemetryProps,
+ router
+ )
+ }}
+ />
+ ))}
+ </div>
</div>
</div>
- </div>
+ </>
)
})