From 6db1354a9bd6996bd4f05ea4ef07bde52b93ba11 Mon Sep 17 00:00:00 2001 From: Saxon Fletcher Date: Fri, 8 Nov 2024 16:19:32 +1000 Subject: [PATCH] added panels --- .../ui/AIAssistantPanel/AIAssistant.tsx | 461 +++++++++--------- .../ui/AIAssistantPanel/SqlSnippet.tsx | 2 +- 2 files changed, 220 insertions(+), 243 deletions(-) diff --git a/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx b/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx index 67403dd0728..fa16d9476cb 100644 --- a/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx +++ b/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx @@ -1,7 +1,15 @@ import { PermissionAction } from '@supabase/shared-types/out/constants' import { AnimatePresence, motion } from 'framer-motion' import { last, partition } from 'lodash' -import { Box, Code, FileText, MessageCircleMore, Plus, WandSparkles } from 'lucide-react' +import { + Box, + Code, + FileText, + MessageCircleMore, + Plus, + WandSparkles, + ChevronLeft, +} from 'lucide-react' import Link from 'next/link' import { useEffect, useMemo, useRef, useState } from 'react' import { toast } from 'sonner' @@ -68,6 +76,13 @@ import { SchemasDropdownMenu } from './SchemasDropdownMenu' import { SQL_TEMPLATES } from 'components/interfaces/SQLEditor/SQLEditor.queries' const ANIMATION_DURATION = 0.3 +type PanelType = 'assistant' | 'templates' | 'quickStart' | 'functions' + +interface Panel { + title: string + component: React.ReactNode +} + interface AIAssistantProps { id: string className?: string @@ -76,8 +91,6 @@ interface AIAssistantProps { onResetConversation: () => void } -// [Joshen] For some reason I'm having issues working with dropdown menu and scroll area - export const AIAssistant = ({ id, className, @@ -92,6 +105,7 @@ export const AIAssistant = ({ const includeSchemaMetadata = isOptedInToAI || !IS_PLATFORM const [templates, quickStart] = partition(SQL_TEMPLATES, { type: 'template' }) + const [activePanel, setActivePanel] = useState('assistant') const disablePrompts = useFlag('disableAssistantPrompts') const { aiAssistantPanel } = useAppStateSnapshot() @@ -109,7 +123,6 @@ export const AIAssistant = ({ const [contextHistory, setContextHistory] = useState<{ [key: string]: { entity: string; schemas: string[]; tables: string[] } }>({}) - // [Joshen] Mainly for error handling on useChat - cause last sent messages will be voided const [assistantError, setAssistantError] = useState() const [lastSentMessage, setLastSentMessage] = useState() const [isConfirmOptInModalOpen, setIsConfirmOptInModalOpen] = useState(false) @@ -173,8 +186,6 @@ export const AIAssistant = ({ onError: (error) => setAssistantError(JSON.parse(error.message).error), }) - console.log('messages:', chatMessages) - const canUpdateOrganization = useCheckPermissions(PermissionAction.UPDATE, 'organizations') const { mutate: updateOrganization, isLoading: isUpdating } = useOrganizationUpdateMutation() @@ -226,14 +237,12 @@ export const AIAssistant = ({ )} - {isFirstUserMessage && - includeSchemaMetadata && - selectedSchemas.length === 0 && ( - - )} + {isFirstUserMessage && includeSchemaMetadata && selectedSchemas.length === 0 && ( + + )} ) }) @@ -279,10 +288,10 @@ export const AIAssistant = ({ } } - const onClickQuickPrompt = (type: SupportedAssistantQuickPromptTypes) => { + const onClickQuickPrompt = (type: SupportedAssistantQuickPromptTypes, entityId?: string) => { const prompt = generatePrompt({ type, - context: selectedDatabaseEntity as any, + context: entityId || (selectedDatabaseEntity as any), schemas: selectedSchemas, tables: selectedTables, }) @@ -317,6 +326,171 @@ export const AIAssistant = ({ ) } + const panels: Record = { + assistant: { + title: 'Assistant', + component: ( +
+

+ How can I help you + {!!entityContext ? ( + <> + {' '} + with{' '} + + {entityContext.id === 'rls-policies' + ? entityContext.label + : `Database ${entityContext.label}`} + + + ) : ( + ' today' + )} + ? +

+

+ I can help you get setup and even generate your entire database schema. Choose a + starting point or describe what you want to build. +

+ {activePanel === 'assistant' && !selectedDatabaseEntity && !hasMessages && ( +
+ + +
+ )} + {ASSISTANT_SUPPORT_ENTITIES.filter( + (e) => !selectedDatabaseEntity || e.id === selectedDatabaseEntity + ).map((entity) => ( +
+

+ {entity.label} +

+
+ + + + + + {/* */} +
+
+ ))} +
+ ), + }, + templates: { + title: 'Templates', + component: ( +
+
+ {templates.map((template) => ( + + ))} +
+
+ ), + }, + quickStart: { + title: 'Quick Start', + component: ( +
+
+ {quickStart.map((template) => ( + + ))} +
+
+ ), + }, + } + useEffect(() => { if (editor) { const mode = ASSISTANT_SUPPORT_ENTITIES.find((x) => x.id === editor) @@ -363,22 +537,25 @@ export const AIAssistant = ({ return ( <>
-
-
Assistant
- +
+ {activePanel !== 'assistant' && ( + - + )} - +
@@ -389,220 +566,20 @@ export const AIAssistant = ({ )} {!hasMessages && ( -
- - {!hasMessages && ( - -

- How can I help you - {!!entityContext ? ( - <> - {' '} - with{' '} - - {entityContext.id === 'rls-policies' - ? entityContext.label - : `Database ${entityContext.label}`} - - - ) : ( - ' today' - )} - ? -

-
-

- Quick start -

-
- {quickStart.map((template) => ( - - ))} -
-
-
-

- Templates -

-
- {templates.map((template) => ( - - ))} -
-
-
-

- Functions -

-
- - - -
-
-
- )} -
-
- {disablePrompts && ( - - )} - {!isApiKeySet && ( - - } - /> - )} - - - {!hasMessages && ( - -
- - - - - - Suggest some{' '} - {entityContext?.id === 'rls-policies' - ? entityContext.label - : `database ${entityContext?.label.toLowerCase()}`} - - - - - - - - Provide some examples of{' '} - {entityContext?.id === 'rls-policies' - ? entityContext.label - : `database ${entityContext?.label.toLowerCase()}`} - - - - - - - - What are{' '} - {entityContext?.id === 'rls-policies' - ? entityContext.label - : `database ${entityContext?.label.toLowerCase()}`} - ? - - - {docsUrl !== undefined && } -
-
- )} -
-
-
+ + + {panels[activePanel].component} + + )}
+
diff --git a/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx b/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx index 88e45931d86..6f69122971a 100644 --- a/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx +++ b/apps/studio/components/ui/AIAssistantPanel/SqlSnippet.tsx @@ -102,7 +102,7 @@ export const SqlSnippet = ({ sql, isLoading }: SqlSnippetProps) => { console.log('results:', results) return ( -
+

{title}