The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Hook up analyze button in notebook (#49463)
Joshen Lim3490a63a7cac
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsxindex 086575bdfb7..b62220e86b7 100644--- a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx+++ b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx@@ -52,13 +52,14 @@ import {ExplorerToolbarIcon,ExplorerToolbarTitle,} from './ExplorerToolbar'-import { useLoadNotebook } from './hooks'+import { useCreateChat, useLoadNotebook } from './hooks'import { MarkdownCell } from './MarkdownCell'import { QueryCell } from './QueryCell'import { type QueryEditorHandle } from './QueryEditor'import { createMarkdownCellSkeleton, createQueryCellSkeleton } from './utils'import { ButtonTooltip } from '@/components/ui/ButtonTooltip'import { useContentDeleteMutation } from '@/data/content/content-delete-mutation'+import { hasDiscardableChanges } from '@/data/content/notebooks/notebook-cache'import {isQueryCell,WritableCell,@@ -79,6 +80,7 @@ export const ExplorerNotebookTab = () => {const { id, ref } = useParams()const tabs = useTabsStateSnapshot()const snap = useNotebooksStateSnapshot()+ const { createChat, isCreating } = useCreateChat()const [isIntellisenseEnabled, setIsIntellisenseEnabled] = useLocalStorageQuery(LOCAL_STORAGE_KEYS.SQL_EDITOR_INTELLISENSE,@@ -93,6 +95,7 @@ export const ExplorerNotebookTab = () => {const [isRunningNotebook, setIsRunningNotebook] = useState(false)const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false)+ const [isSaveBeforeAnalyzeOpen, setIsSaveBeforeAnalyzeOpen] = useState(false)const [pendingMutationCells, setPendingMutationCells] = useState<{ id: string; title: string }[] | null>(null)@@ -105,6 +108,10 @@ export const ExplorerNotebookTab = () => {if (id && content === savedContentRef.current) {snap.markSaved({ id })toast.success('Successfully saved notebook!')+ if (isSaveBeforeAnalyzeOpen) {+ setIsSaveBeforeAnalyzeOpen(false)+ handleAnalyze()+ }}},})@@ -238,6 +245,21 @@ export const ExplorerNotebookTab = () => {})}+ const handleAnalyze = () => {+ createChat({+ name: `Analyze ${name} notebook`,+ initialMessage: `Run the notebook "${name}" (id: ${id}) and analyze the results. Summarize the key findings per cell, calling out anomalies or trends, and use any markdown cells for context. Skip or flag any cell that would mutate data rather than running it.`,+ })+ }++ const handleClickAnalyze = () => {+ if (hasDiscardableChanges(currentNotebook)) {+ setIsSaveBeforeAnalyzeOpen(true)+ } else {+ handleAnalyze()+ }+ }+const handleConfirmDeleteNotebook = () => {if (!ref || !id) returndeleteNotebook({ projectRef: ref, ids: [id] })@@ -293,7 +315,11 @@ export const ExplorerNotebookTab = () => {</ExplorerToolbarIcon><ExplorerToolbarTitle onSaveTitle={handleSaveTitle}>{name ?? ''}</ExplorerToolbarTitle><ExplorerToolbarActions>- <ExplorerToolbarAction icon={<AiIconAnimation size={16} />}>+ <ExplorerToolbarAction+ icon={<AiIconAnimation size={16} />}+ loading={isCreating}+ onClick={handleClickAnalyze}+ >Analyze</ExplorerToolbarAction><ExplorerToolbarAction@@ -421,6 +447,22 @@ export const ExplorerNotebookTab = () => {</p></ConfirmationModal>+ <ConfirmationModal+ size="small"+ visible={isSaveBeforeAnalyzeOpen}+ title="Save notebook before analyzing?"+ confirmLabel="Save and analyze"+ confirmLabelLoading="Saving notebook"+ loading={isUpdating}+ onCancel={() => setIsSaveBeforeAnalyzeOpen(false)}+ onConfirm={handleSaveNotebook}+ >+ <p className="text-sm">+ This notebook has unsaved changes. Save it first so the assistant analyzes the latest+ content.+ </p>+ </ConfirmationModal>+<ConfirmationModalsize="small"visible={pendingMutationCells !== null}diff --git a/apps/studio/components/interfaces/Explorer/hooks.ts b/apps/studio/components/interfaces/Explorer/hooks.tsindex b5922c03ce7..39c2e75719d 100644--- a/apps/studio/components/interfaces/Explorer/hooks.ts+++ b/apps/studio/components/interfaces/Explorer/hooks.ts@@ -1,5 +1,5 @@import { useRouter } from 'next/router'-import { useEffect, useEffectEvent } from 'react'+import { useEffect, useEffectEvent, useState } from 'react'import { useNotebookQuery } from '@/data/content/notebooks/notebook-query'import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'@@ -92,6 +92,8 @@ export const useCreateChat = () => {const { data: organization } = useSelectedOrganizationQuery()const aiAssistantState = useAiAssistantState()+ const [isCreating, setIsCreating] = useState(false)+const openChat = (id: string) => {if (!project) {console.error('Project is required')@@ -115,23 +117,30 @@ export const useCreateChat = () => {return undefined}- // Hydration replaces the chat map and the selected model wholesale, so wait it out before- // creating anything — otherwise the new chat is dropped as soon as the persisted state lands.- await whenAiAssistantInitialized(aiAssistantState)+ setIsCreating(true)- aiAssistantState.setContext({- projectRef: project.ref,- orgSlug: organization?.slug,- connectionString: project.connectionString ?? '',- })- if (model) aiAssistantState.setModel(model)+ try {+ // Hydration replaces the chat map and the selected model wholesale, so wait it out before+ // creating anything — otherwise the new chat is dropped as soon as the persisted state lands.+ await whenAiAssistantInitialized(aiAssistantState)- const id = aiAssistantState.createChat({ name, initialMessage })- router.push(`/project/${project.ref}/explorer/chat/${id}`)- return id+ aiAssistantState.setContext({+ projectRef: project.ref,+ orgSlug: organization?.slug,+ connectionString: project.connectionString ?? '',+ })+ if (model) aiAssistantState.setModel(model)++ const id = aiAssistantState.createChat({ name, initialMessage })++ router.push(`/project/${project.ref}/explorer/chat/${id}`)+ return id+ } finally {+ setIsCreating(false)+ }}- return { createChat, openChat }+ return { createChat, openChat, isCreating }}export const useCreateQuery = () => {