Skip to content

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

Chore/update opt in message (#28846)

Terry Suttonf8228f1f48f7
diff --git a/apps/studio/components/interfaces/Organization/GeneralSettings/GeneralSettings.tsx b/apps/studio/components/interfaces/Organization/GeneralSettings/GeneralSettings.tsx
index 37fd61c2476..ebecfa900f2 100644
--- a/apps/studio/components/interfaces/Organization/GeneralSettings/GeneralSettings.tsx
+++ b/apps/studio/components/interfaces/Organization/GeneralSettings/GeneralSettings.tsx
@@ -1,7 +1,6 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useQueryClient } from '@tanstack/react-query'
-import Link from 'next/link'
-import { useEffect, useState } from 'react'
+import { useEffect } from 'react'
import { toast } from 'sonner'
import { useParams } from 'common'
@@ -17,13 +16,13 @@ import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useOrgOptedIntoAi } from 'hooks/misc/useOrgOptedIntoAi'
import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'
import { OPT_IN_TAGS } from 'lib/constants'
-import { Collapsible, Form, IconChevronRight, Input, Toggle, cn } from 'ui'
+import { Form, Input, Toggle } from 'ui'
+import OptInToOpenAIToggle from './OptInToOpenAIToggle'
import OrganizationDeletePanel from './OrganizationDeletePanel'
const GeneralSettings = () => {
const { slug } = useParams()
const queryClient = useQueryClient()
- const [open, setOpen] = useState(false)
const selectedOrganization = useSelectedOrganization()
const { name } = selectedOrganization ?? {}
@@ -121,69 +120,10 @@ const GeneralSettings = () => {
disabled={!canUpdateOrganization}
size="small"
label="Opt-in to sending anonymous data to OpenAI"
- descriptionText="By opting into sending anonymous data, Supabase AI can improve the answers it shows you"
+ descriptionText="By opting into sending anonymous data, Supabase AI can improve the answers it shows you. This is an organization-wide setting."
/>
- <Collapsible open={open} onOpenChange={setOpen}>
- <Collapsible.Trigger asChild>
- <div className="flex items-center space-x-2 ml-16 cursor-pointer">
- <IconChevronRight
- strokeWidth={2}
- size={16}
- className={cn('transition-all', open ? 'rotate-90' : '')}
- />
- <p className="text-sm text-foreground-light underline">
- Important information regarding opting in
- </p>
- </div>
- </Collapsible.Trigger>
- <Collapsible.Content>
- <div className="space-y-2 py-4 ml-16 text-sm text-foreground-light">
- <p>
- Supabase AI is a chatbot support tool powered by OpenAI. Supabase will
- share the query you submit and information about the databases you
- manage through Supabase with OpenAI, L.L.C. and its affiliates in order
- to provide the Supabase AI tool.
- </p>
- <p>
- OpenAI will only access information about the structure of your
- databases, such as table names, column and row headings. OpenAI will not
- access the contents of the database itself.
- </p>
- <p>
- OpenAI uses this information to generate responses to your query, and
- does not retain or use the information to train its algorithms or
- otherwise improve its products and services.
- </p>
- <p>
- If you have your own individual account on Supabase, we will use any
- personal information collected through [Supabase AI] to provide you with
- the [Supabase AI] tool. If you are in the UK, EEA or Switzerland, the
- processing of this personal information is necessary for the performance
- of a contract between you and us.
- </p>
- <p>
- Supabase collects information about the queries you submit through
- Supabase AI and the responses you receive to assess the performance of
- the Supabase AI tool and improve our services. If you are in the UK, EEA
- or Switzerland, the processing is necessary for our legitimate
- interests, namely informing our product development and improvement.
- </p>
- <p>
- For more information about how we use personal information, please see
- our{' '}
- <Link
- href="https://supabase.com/privacy"
- target="_blank"
- rel="noreferrer"
- className="text-brand border-b border-brand"
- >
- privacy policy
- </Link>
- .
- </p>
- </div>
- </Collapsible.Content>
- </Collapsible>
+
+ <OptInToOpenAIToggle className="ml-16" />
</div>
</FormSectionContent>
</FormSection>
diff --git a/apps/studio/components/interfaces/Organization/GeneralSettings/OptInToOpenAIToggle.tsx b/apps/studio/components/interfaces/Organization/GeneralSettings/OptInToOpenAIToggle.tsx
new file mode 100644
index 00000000000..e360e000d20
--- /dev/null
+++ b/apps/studio/components/interfaces/Organization/GeneralSettings/OptInToOpenAIToggle.tsx
@@ -0,0 +1,71 @@
+import Link from 'next/link'
+import { cn, Collapsible_Shadcn_, CollapsibleTrigger_Shadcn_, CollapsibleContent_Shadcn_ } from 'ui'
+import { useState } from 'react'
+import { ChevronRight } from 'lucide-react'
+
+interface OptInToOpenAIToggleProps {
+ className?: string
+}
+export default function OptInToOpenAIToggle({ className }: OptInToOpenAIToggleProps) {
+ const [open, setOpen] = useState(false)
+
+ return (
+ <Collapsible_Shadcn_ open={open} onOpenChange={setOpen} className={cn('mt-4', className)}>
+ <CollapsibleTrigger_Shadcn_ asChild>
+ <div className="flex items-center space-x-2 cursor-pointer">
+ <ChevronRight
+ strokeWidth={2}
+ size={16}
+ className={cn('transition-all', open ? 'rotate-90' : '')}
+ />
+ <p className="text-sm text-foreground-light underline">
+ Important information regarding opting in
+ </p>
+ </div>
+ </CollapsibleTrigger_Shadcn_>
+ <CollapsibleContent_Shadcn_>
+ <div className="space-y-2 py-4 text-sm text-foreground-light">
+ <p>
+ Supabase AI is a chatbot support tool powered by OpenAI. Supabase will share the query
+ you submit and information about the databases you manage through Supabase with OpenAI,
+ L.L.C. and its affiliates in order to provide the Supabase AI tool.
+ </p>
+ <p>
+ OpenAI will only access information about the structure of your databases, such as table
+ names, column and row headings. OpenAI will not access the contents of the database
+ itself.
+ </p>
+ <p>
+ OpenAI uses this information to generate responses to your query, and does not retain or
+ use the information to train its algorithms or otherwise improve its products and
+ services.
+ </p>
+ <p>
+ If you have your own individual account on Supabase, we will use any personal
+ information collected through [Supabase AI] to provide you with the [Supabase AI] tool.
+ If you are in the UK, EEA or Switzerland, the processing of this personal information is
+ necessary for the performance of a contract between you and us.
+ </p>
+ <p>
+ Supabase collects information about the queries you submit through Supabase AI and the
+ responses you receive to assess the performance of the Supabase AI tool and improve our
+ services. If you are in the UK, EEA or Switzerland, the processing is necessary for our
+ legitimate interests, namely informing our product development and improvement.
+ </p>
+ <p>
+ For more information about how we use personal information, please see our{' '}
+ <Link
+ href="https://supabase.com/privacy"
+ target="_blank"
+ rel="noreferrer"
+ className="text-brand border-b border-brand"
+ >
+ privacy policy
+ </Link>
+ .
+ </p>
+ </div>
+ </CollapsibleContent_Shadcn_>
+ </Collapsible_Shadcn_>
+ )
+}
diff --git a/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx b/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx
index 4ab53522b1b..5d8b06cdaaf 100644
--- a/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx
@@ -1,33 +1,41 @@
+import { PermissionAction } from '@supabase/shared-types/out/constants'
import { Message as MessageType } from 'ai'
+import { useChat } from 'ai/react'
import Telemetry from 'lib/telemetry'
import { compact, last } from 'lodash'
-import { ChevronsUpDown } from 'lucide-react'
-import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect, useMemo, useRef, useState } from 'react'
-
-import { useChat } from 'ai/react'
-import { useTelemetryProps } from 'common'
+import { toast } from 'sonner'
+import { useParams, useTelemetryProps } from 'common'
import { SchemaComboBox } from 'components/ui/SchemaComboBox'
import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
+import { useOrganizationUpdateMutation } from 'data/organizations/organization-update-mutation'
+import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
+import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
import { useSchemasForAi } from 'hooks/misc/useSchemasForAi'
import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
-import { BASE_PATH } from 'lib/constants'
+import { BASE_PATH, LOCAL_STORAGE_KEYS, OPT_IN_TAGS } from 'lib/constants'
import { useProfile } from 'lib/profile'
import uuidv4 from 'lib/uuid'
+import { Info } from 'lucide-react'
import {
AiIconAnimation,
+ Alert_Shadcn_,
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
Button,
cn,
Tooltip_Shadcn_,
TooltipContent_Shadcn_,
TooltipTrigger_Shadcn_,
+ WarningIcon,
} from 'ui'
-import { AssistantChatForm } from 'ui-patterns'
+import { AssistantChatForm } from 'ui-patterns/AssistantChat/AssistantChatForm'
+import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
+import OptInToOpenAIToggle from '../../Organization/GeneralSettings/OptInToOpenAIToggle'
import { DiffType } from '../SQLEditor.types'
import Message from './Message'
-import { ButtonTooltip } from 'components/ui/ButtonTooltip'
export type MessageWithDebug = MessageType & { isDebug: boolean }
@@ -47,7 +55,9 @@ export const AiAssistantPanel = ({
includeSchemaMetadata,
}: AiAssistantPanelProps) => {
const project = useSelectedProject()
- const selectedOrganization = useSelectedOrganization()
+ const router = useRouter()
+ const { ref } = useParams()
+ const { profile } = useProfile()
const [selectedSchemas, setSelectedSchemas] = useSchemasForAi(project?.ref!)
@@ -76,7 +86,6 @@ export const AiAssistantPanel = ({
entityDefinitions: entityDefinitions,
},
})
-
const messages = useMemo(() => {
const merged = [...chatMessages.map((m) => ({ ...m, isDebug: false }))]
@@ -87,8 +96,6 @@ export const AiAssistantPanel = ({
)
}, [chatMessages])
- const router = useRouter()
- const { profile } = useProfile()
const bottomRef = useRef<HTMLDivElement>(null)
const telemetryProps = useTelemetryProps()
@@ -115,6 +122,44 @@ export const AiAssistantPanel = ({
)
}, [isLoading])
+ const selectedOrganization = useSelectedOrganization()
+
+ const [isConfirmOptInModalOpen, setIsConfirmOptInModalOpen] = useState(false)
+
+ const canUpdateOrganization = useCheckPermissions(PermissionAction.UPDATE, 'organizations')
+
+ const { mutate: updateOrganization, isLoading: isUpdating } = useOrganizationUpdateMutation()
+
+ const confirmOptInToShareSchemaData = async () => {
+ if (!canUpdateOrganization) {
+ return toast.error('You do not have the required permissions to update this organization')
+ }
+
+ if (!selectedOrganization?.slug) return console.error('Organization slug is required')
+
+ const existingOptInTags = selectedOrganization?.opt_in_tags ?? []
+
+ const updatedOptInTags = existingOptInTags.includes(OPT_IN_TAGS.AI_SQL)
+ ? existingOptInTags
+ : [...existingOptInTags, OPT_IN_TAGS.AI_SQL]
+
+ updateOrganization(
+ { slug: selectedOrganization?.slug, opt_in_tags: updatedOptInTags },
+ {
+ onSuccess: () => {
+ toast.success('Successfully opted-in')
+ setIsConfirmOptInModalOpen(false)
+ },
+ }
+ )
+ }
+
+ const [showAiNotOptimizedWarningSetting, setShowAiNotOptimizedWarningSetting] =
+ useLocalStorageQuery(LOCAL_STORAGE_KEYS.SHOW_AI_NOT_OPTIMIZED_WARNING(ref as string), true)
+
+ const shouldShowNotOptimizedAlert =
+ selectedOrganization && !includeSchemaMetadata && showAiNotOptimizedWarningSetting
+
return (
<div className="flex flex-col h-full border-l border-control">
<div
@@ -132,75 +177,121 @@ export const AiAssistantPanel = ({
Make sure to verify any generated code or suggestions, and share feedback so that we can
learn and improve.`}
action={
- <Button type="default" onClick={onClose}>
- Close Assistant
- </Button>
+ <div className="flex gap-2">
+ {messages.length > 0 && (
+ <Button type="warning" onClick={() => setChatId(uuidv4())}>
+ Clear history
+ </Button>
+ )}
+ <Button type="default" onClick={onClose}>
+ Close Assistant
+ </Button>
+ </div>
}
>
- <div className="flex flex-row justify-between space-x-2">
- {includeSchemaMetadata ? (
- <SchemaComboBox
- disabled={!includeSchemaMetadata}
- selectedSchemas={selectedSchemas}
- onSelectSchemas={setSelectedSchemas}
- label={
- includeSchemaMetadata && selectedSchemas.length > 0
- ? `${selectedSchemas.length} schema${
- selectedSchemas.length > 1 ? 's' : ''
- } selected`
- : 'No schemas selected'
- }
- />
- ) : (
- <ButtonTooltip
- disabled
- size="tiny"
- type="default"
- className="w-min"
- iconRight={<ChevronsUpDown size={14} />}
- tooltip={{
- content: {
- side: 'bottom',
- className: 'w-72',
- text: (
- <>
- Opt in to sending anonymous data to OpenAI in your{' '}
- <Link
- className="underline"
- href={`/org/${selectedOrganization?.slug}/general`}
- >
- organization settings
- </Link>{' '}
- to share schemas with the Assistant for more accurate responses.
- </>
- ),
- },
- }}
- >
- No schemas selected
- </ButtonTooltip>
- )}
- <Button type="warning" onClick={() => setChatId(uuidv4())}>
- Clear history
- </Button>
- </div>
- </Message>
+ {includeSchemaMetadata ? (
+ <div className="mt-2">
+ <span className="inline-block text-lighter">
+ Include these schemas in your prompts:
+ </span>
+ <div className="flex flex-row justify-between space-x-2 mt-2">
+ <div className="flex items-center gap-2">
+ <SchemaComboBox
+ disabled={!includeSchemaMetadata}
+ selectedSchemas={selectedSchemas}
+ onSelectSchemas={setSelectedSchemas}
+ label={
+ includeSchemaMetadata && selectedSchemas.length > 0
+ ? `${selectedSchemas.length} schema${
+ selectedSchemas.length > 1 ? 's' : ''
+ } selected`
+ : 'No schemas selected'
+ }
+ />
+ <Tooltip_Shadcn_>
+ <TooltipTrigger_Shadcn_ className="flex items-center gap-2 text-lighter">
+ <Info className="text-lighter w-4 h-4" />
+ </TooltipTrigger_Shadcn_>
+ <TooltipContent_Shadcn_ className="w-72" side="right">
+ Select specific schemas to send with your queries. Supabase AI can use this
+ data to improve the responses it shows you.
+ </TooltipContent_Shadcn_>
+ </Tooltip_Shadcn_>
+ </div>
+ </div>
+ </div>
+ ) : (
+ <>
+ {shouldShowNotOptimizedAlert ? (
+ <Alert_Shadcn_ className="[&>svg]:left-5 border-l-0 border-r-0 rounded-none -mx-5 px-5 w-[calc(100%+40px)]">
+ <WarningIcon />
+ <AlertTitle_Shadcn_>AI Assistant is not optimized</AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ You need to agree to share anonymous schema data with OpenAI for the best
+ experience.
+ </AlertDescription_Shadcn_>
+ <div className="flex items-center gap-4 mt-6">
+ <Button type="default" onClick={() => setIsConfirmOptInModalOpen(true)}>
+ Update AI settings
+ </Button>
- {messages.map((m) => (
- <Message
- key={`message-${m.id}`}
- name={m.name}
- role={m.role}
- content={m.content}
- createdAt={new Date(m.createdAt || new Date()).getTime()}
- isDebug={m.isDebug}
- isSelected={selectedMessage === m.id}
- onDiff={(diffType, sql) => onDiff({ id: m.id, diffType, sql })}
- />
- ))}
+ <Button type="text" onClick={() => setShowAiNotOptimizedWarningSetting(false)}>
+ Dismiss
+ </Button>
+ </div>
+ </Alert_Shadcn_>
+ ) : (
+ <></>
+ )}
+ </>
+ )}
+ </Message>
+ {messages.map((m, index) => {
+ const isFirstUserMessage =
+ m.role === 'user' && messages.slice(0, index).every((msg) => msg.role !== 'user')
+ return (
+ <Message
+ key={`message-${m.id}`}
+ name={m.name}
+ role={m.role}
+ content={m.content}
+ createdAt={new Date(m.createdAt || new Date()).getTime()}
+ isDebug={m.isDebug}
+ isSelected={selectedMessage === m.id}
+ onDiff={(diffType, sql) => onDiff({ id: m.id, diffType, sql })}
+ >
+ {isFirstUserMessage && !includeSchemaMetadata && !shouldShowNotOptimizedAlert && (
+ <Alert_Shadcn_>
+ <AlertDescription_Shadcn_ className="flex flex-col gap-4">
+ <span>
+ Quick reminder that you're not sending project metadata with your queries. By
+ opting into sending anonymous data, Supabase AI can improve the answers it
+ shows you.
+ </span>
+ <Button
+ type="default"
+ className="w-fit"
+ onClick={() => setIsConfirmOptInModalOpen(true)}
+ >
+ Update AI settings
+ </Button>
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ )}
+ {isFirstUserMessage && includeSchemaMetadata && selectedSchemas.length === 0 && (
+ <Alert_Shadcn_>
+ <AlertDescription_Shadcn_ className="flex flex-col gap-4">
+ <span>
+ We recommend including the schemas for better answers by Supabase AI.
+ </span>
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ )}
+ </Message>
+ )
+ })}
{pendingReply && <Message key="thinking" role="assistant" content="Thinking..." />}
-
<div ref={bottomRef} className="h-1" />
</div>
@@ -237,6 +328,22 @@ export const AiAssistantPanel = ({
}}
/>
</div>
+ <ConfirmationModal
+ visible={isConfirmOptInModalOpen}
+ size="large"
+ title="Confirm sending anonymous data to OpenAI"
+ confirmLabel="Confirm"
+ onCancel={() => setIsConfirmOptInModalOpen(false)}
+ onConfirm={confirmOptInToShareSchemaData}
+ loading={isUpdating}
+ >
+ <p className="text-sm text-foreground-light">
+ By opting into sending anonymous data, Supabase AI can improve the answers it shows you.
+ This is an organization-wide setting, and affects all projects in your organization.
+ </p>
+
+ <OptInToOpenAIToggle />
+ </ConfirmationModal>
</div>
)
}
diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.ts
index bcfa5112170..5a1e921359c 100644
--- a/apps/studio/lib/constants/index.ts
+++ b/apps/studio/lib/constants/index.ts
@@ -69,6 +69,9 @@ export const LOCAL_STORAGE_KEYS = {
// Key to track the last selected schema. The project ref is intentionally put at the end for easier search in the browser console.
LAST_SELECTED_SCHEMA: (ref: string) => `last-selected-schema-${ref}`,
+
+ // Key to show a warning on the SQL Editor AI Assistant that the org hasn't opted-in to sending anon data
+ SHOW_AI_NOT_OPTIMIZED_WARNING: (ref: string) => `supabase-show-ai-not-optimized-${ref}`,
}
export const OPT_IN_TAGS = {