Skip to content

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

chore(cmdk): clean up ai command

Greg Richardsonee5d2e1708ec
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx
index 9477b70a4e8..7d34e38f364 100644
--- a/packages/ui/src/components/Command/AiCommand.tsx
+++ b/packages/ui/src/components/Command/AiCommand.tsx
@@ -5,16 +5,10 @@ import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import { SSE } from 'sse.js'
-import { useSupabaseClient } from '@supabase/auth-helpers-react'
-import { useTheme } from 'common/Providers'
-import { IconAlertCircle, IconAlertTriangle, IconLoader, IconUser, Input } from 'ui'
-// import components from '~/components'
-// import { IS_PLATFORM } from '~/lib/constants'
-import { IconCopy } from '../Icon/icons/IconCopy'
+import { Button, IconAlertCircle, IconAlertTriangle, IconLoader, IconUser, Input } from 'ui'
import { CommandGroup, CommandItem } from './Command.utils'
-
-import { COMMAND_ROUTES } from './CommandMenu'
import { AiIcon, AiIconChat } from './Command.icons'
+import { COMMAND_ROUTES } from './CommandMenu'
import { useCommandMenu } from './CommandMenuProvider'
const questions = [
@@ -26,15 +20,6 @@ const questions = [
'How do I set up authentication?',
]
-const RLSquestions = [
- 'How do I get started with Supabase?',
- 'How do I run Supabase locally?',
- 'How do I connect to my database?',
- 'How do I run migrations? ',
- 'How do I listen to changes in a table?',
- 'How do I set up authentication?',
-]
-
function getEdgeFunctionUrl() {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL?.replace(/\/$/, '')
@@ -60,22 +45,14 @@ function promptDataReducer(
type?: 'remove-last-item' | string
}
) {
- // console.log('running reducer')
- // console.log('what is currently in', ...state)
- // console.log('what is the action payload', action)
// set a standard state to use later
let current = [...state]
- console.log(action)
-
if (action.type) {
switch (action.type) {
case 'remove-last-item':
- console.log('removing last item')
current.pop()
return [...current]
- break
-
default:
break
}
@@ -88,9 +65,8 @@ function promptDataReducer(
current[action.index] = { query: '', answer: '', status: '' }
}
- // if (action.answer !== undefined) {
current[action.index].answer = action.answer
- // }
+
if (action.query) {
current[action.index].query = action.query
}
@@ -98,22 +74,13 @@ function promptDataReducer(
current[action.index].status = action.status
}
- // console.log('what i will update with', current)
- console.log(current)
return [...current]
-
- throw Error('Unknown action.')
}
const AiCommand = () => {
- const { isDarkMode } = useTheme()
-
const [answer, setAnswer] = useState<string | undefined>('')
- const [results, setResults] = useState<any[]>()
const [isResponding, setIsResponding] = useState(false)
const [hasClippyError, setHasClippyError] = useState(false)
- const [hasSearchError, setHasSearchError] = useState(false)
- const [selectedTab, setSelectedTab] = useState('clippy-panel')
const eventSourceRef = useRef<SSE>()
const { isLoading, setIsLoading, currentPage, search, setSearch } = useCommandMenu()
@@ -129,96 +96,28 @@ const AiCommand = () => {
? 'Clippy has failed you'
: undefined
- // const handleSearchConfirm = useCallback(
- // async (query: string) => {
- // console.log('query', query)
- // // console.log('query', query)
-
- // setResults(undefined)
- // setAnswer(undefined)
- // setIsResponding(false)
- // setHasClippyError(false)
- // setHasSearchError(false)
- // setIsLoading(true)
-
- // const { error, data: pageSections } = await supabaseClient.functions.invoke('search', {
- // body: { query: query },
- // })
-
- // setIsLoading(false)
-
- // if (error) {
- // setIsLoading(false)
- // setIsResponding(false)
- // setHasSearchError(true)
- // console.error(error)
- // return
- // }
-
- // if (!Array.isArray(pageSections)) {
- // setIsLoading(false)
- // setIsResponding(false)
- // setHasSearchError(true)
- // console.error('Malformed response')
- // return
- // }
-
- // setResults(pageSections)
- // },
- // [supabaseClient]
- // )
-
- // console.log('current index', promptIndex)
-
- const handleClippyConfirm = useCallback(
+ const handleConfirm = useCallback(
async (query: string) => {
- // setResults(undefined)
- // const promptIndex = promptIndex
- console.log('handleClippyConfirm ran')
- console.log('promptIndex', promptIndex)
setAnswer(undefined)
setSearch('')
- dispatchPromptData({ index: promptIndex, answer: undefined, query: query })
+ dispatchPromptData({ index: promptIndex, answer: undefined, query })
setIsResponding(false)
setHasClippyError(false)
- setHasSearchError(false)
setIsLoading(true)
- let queryToSend = query
-
- switch (currentPage) {
- case COMMAND_ROUTES.AI_ASK_ANYTHING:
- queryToSend = query
- break
-
- case COMMAND_ROUTES.AI_RLS_POLICY:
- queryToSend = `Given this table schema:
-
- Schema STRIPE has tables:
- CHARGE with columns [ID, AMOUNT, CREATED, CURRENCY, CUSTOMER_ID]
- CUSTOMER with columns [ID, NAME, CREATED, SHIPPING_ADDRESS_STATE]
-
- \n\nAnswer with only an RLS policy in SQL, no other text: ${query}`
- break
- default:
- break
- }
-
- console.log('query to send:', queryToSend)
-
const eventSource = new SSE(`${edgeFunctionUrl}/clippy-search`, {
headers: {
apikey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ?? '',
Authorization: `Bearer ${process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY}`,
'Content-Type': 'application/json',
},
- payload: JSON.stringify({ query: queryToSend }),
+ payload: JSON.stringify({ query }),
})
function handleError<T>(err: T) {
setIsLoading(false)
setIsResponding(false)
- // setHasClippyError(true)
+ setHasClippyError(true)
console.error(err)
}
@@ -228,7 +127,6 @@ const AiCommand = () => {
setIsLoading(false)
if (e.data === '[DONE]') {
- // console.log('I HAVE FINSHED')
setIsResponding(false)
setAnswer(undefined)
setPromptIndex((x) => {
@@ -242,8 +140,6 @@ const AiCommand = () => {
const completionResponse: CreateCompletionResponse = JSON.parse(e.data)
const [{ text }] = completionResponse.choices
- console.log('Im going to use index:', promptIndex)
-
setAnswer((answer) => {
const currentAnswer = answer ?? ''
@@ -268,37 +164,18 @@ const AiCommand = () => {
[promptIndex, promptData]
)
- const handleConfirm = useCallback(
- (selectedTab: string, query: string) => {
- switch (selectedTab) {
- case 'search-panel':
- // return handleSearchConfirm(query)
- case 'clippy-panel':
- return handleClippyConfirm(query)
- }
- },
- [
- // handleSearchConfirm,
- handleClippyConfirm,
- ]
- )
-
- // function handleResetPrompt() {
- // eventSourceRef.current?.close()
- // eventSourceRef.current = undefined
- // setQuery('')
- // setResults(undefined)
- // setAnswer(undefined)
- // setIsResponding(false)
- // setHasClippyError(false)
- // setHasSearchError(false)
- // }
-
- // console.log(promptData)
+ function handleResetPrompt() {
+ eventSourceRef.current?.close()
+ eventSourceRef.current = undefined
+ setSearch('')
+ setAnswer(undefined)
+ setIsResponding(false)
+ setHasClippyError(false)
+ }
useEffect(() => {
if (search) {
- handleClippyConfirm(search)
+ handleConfirm(search)
}
}, [])
@@ -312,90 +189,6 @@ const AiCommand = () => {
return (
<div onClick={(e) => e.stopPropagation()}>
<div className="relative mb-[70px] py-4 overflow-y-auto overflow-hidden max-h-[720px]">
- {/* <Tabs
- activeId={selectedTab}
- onChange={(tabId) => {
- setSelectedTab(tabId)
- if (!query) {
- handleResetPrompt()
- return
- }
- handleConfirm(tabId, query)
- }}
- > */}
- {/* <Tabs.Panel id="search-panel" label="Guides & Reference">
- <div className="mb-6">
- {!isLoading && !hasSearchError && !results && (
- <div className="p-10 grid">
- <h2 className="text-lg text-center text-scale-1100">
- Search Supabase guides & reference docs
- </h2>
- </div>
- )}
- {results && results.length > 0 && (
- <div className="flex flex-col gap-3 max-h-[70vh] lg:max-h-[50vh] overflow-y-auto px-4 py-4 rounded-lg bg-scale-200">
- {results.map((page) => {
- const pageSections = page.sections.filter((section) => !!section.heading)
- return (
- <div key={page.id} className="flex flex-col gap-3">
- <SearchResult
- href={page.path}
- type={SearchResultType.Document}
- title={page.meta.title}
- />
- {pageSections.length > 0 && (
- <div className="flex flex-row">
- <div className="border bg-scale-300 rounded-xl self-stretch p-[1px] ml-4 mr-4"></div>
- <div className="flex flex-col gap-3 items-stretch grow">
- {pageSections.map((section) => (
- <SearchResult
- key={section.id}
- href={`${page.path}${page.type === 'reference' ? '/' : '#'}${
- section.slug
- }`}
- type={SearchResultType.Section}
- title={section.heading}
- chip={page.meta.title}
- />
- ))}
- </div>
- </div>
- )}
- </div>
- )
- })}
- </div>
- )}
- {isLoading && (
- <div className="p-6 grid gap-6 mt-4">
- <Loading active>{}</Loading>
- <p className="text-lg text-center">Searching for results</p>
- </div>
- )}
- {results && results.length === 0 && (
- <div className="p-6 flex flex-col items-center gap-6 mt-4">
- <IconAlertTriangle strokeWidth={1.5} size={40} />
- <p className="text-lg text-center">No results found.</p>
- <Button size="tiny" type="secondary" onClick={handleResetPrompt}>
- Try again?
- </Button>
- </div>
- )}
- {hasSearchError && (
- <div className="p-6 flex flex-col items-center gap-6 mt-4">
- <IconAlertTriangle strokeWidth={1.5} size={40} />
- <p className="text-lg text-center">
- Sorry, looks like we&apos;re having some issues with search!
- </p>
- <p className="text-sm text-center">Please try again in a bit.</p>
- <Button size="tiny" type="secondary" onClick={handleResetPrompt}>
- Try again?
- </Button>
- </div>
- )}
- </div>
- </Tabs.Panel> */}
- {/* <Tabs.Panel id="clippy-panel" label="Ask Clippy"> */}
{/* {!isLoading && !answer && !hasClippyError && (
<div className="">
<div className="mt-2">
@@ -439,112 +232,46 @@ const AiCommand = () => {
)}
<div className="px-4">
- {
- // cantHelp
- false ? (
- <p className="flex flex-col gap-4 items-center p-4">
- <div className="grid md:flex items-center gap-2 mt-4 text-center justify-items-center">
- <IconAlertCircle />
- <p>Sorry, I don&apos;t know how to help with that.</p>
- </div>
- {/* <Button size="tiny" type="secondary" onClick={handleResetPrompt}>
+ {cantHelp ? (
+ <p className="flex flex-col gap-4 items-center p-4">
+ <div className="grid md:flex items-center gap-2 mt-4 text-center justify-items-center">
+ <IconAlertCircle />
+ <p>Sorry, I don&apos;t know how to help with that.</p>
+ </div>
+ <Button size="tiny" type="secondary" onClick={handleResetPrompt}>
Try again?
- </Button> */}
- </p>
- ) : (
- <div className="flex gap-6">
- <AiIconChat />
- {/* <div
- className="w-7 h-7
-
- bg-gradient-to-r from-purple-900 to-pink-900
-
- rounded-lg border border-pink-400 flex items-center justify-center
- shadow-sm
- "
- >
- <svg
- xmlns="http://www.w3.org/2000/svg"
- fill="none"
- viewBox="0 0 24 24"
- stroke-width="1.5"
- stroke="currentColor"
- className="w-4 h-4 text-white"
- >
- <path
- stroke-linecap="round"
- stroke-linejoin="round"
- d="M9.813 15.904L9 18.75l-.813-2.846a4.5 4.5 0 00-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 003.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 003.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 00-3.09 3.09zM18.259 8.715L18 9.75l-.259-1.035a3.375 3.375 0 00-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 002.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 002.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 00-2.456 2.456zM16.894 20.567L16.5 21.75l-.394-1.183a2.25 2.25 0 00-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 001.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 001.423 1.423l1.183.394-1.183.394a2.25 2.25 0 00-1.423 1.423z"
- />
- </svg>
- </div> */}
- <div className="w-full">
- {isLoading && promptIndex === i ? (
- <div className="bg-scale-700 h-[21px] w-[13px] mt-1 animate-pulse animate-bounce"></div>
- ) : (
- <ReactMarkdown
- linkTarget="_blank"
- className="prose dark:prose-dark"
- remarkPlugins={[remarkGfm]}
- transformLinkUri={(href) => {
- const supabaseUrl = new URL('https://supabase.com')
- const linkUrl = new URL(href, 'https://supabase.com')
-
- if (linkUrl.origin === supabaseUrl.origin) {
- return linkUrl.toString()
- }
-
- return href
- }}
- // components={components}
- >
- {prompt.answer}
- </ReactMarkdown>
- )}
- </div>
+ </Button>
+ </p>
+ ) : (
+ <div className="flex gap-6">
+ <AiIconChat />
+ <div className="w-full">
+ {isLoading && promptIndex === i ? (
+ <div className="bg-scale-700 h-[21px] w-[13px] mt-1 animate-pulse animate-bounce"></div>
+ ) : (
+ // TODO: pull in markdown components from docs for better styling (code blocks, etc)
+ <ReactMarkdown
+ linkTarget="_blank"
+ className="prose dark:prose-dark"
+ remarkPlugins={[remarkGfm]}
+ transformLinkUri={(href) => {
+ const supabaseUrl = new URL('https://supabase.com')
+ const linkUrl = new URL(href, 'https://supabase.com')
+
+ if (linkUrl.origin === supabaseUrl.origin) {
+ return linkUrl.toString()
+ }
+
+ return href
+ }}
+ >
+ {prompt.answer}
+ </ReactMarkdown>
+ )}
</div>
- )
- }
+ </div>
+ )}
</div>
-
- {showActions && promptIndex === i + 1 && (
- <CommandGroup heading="" forceMount className="!pt-0 !pl-[60px]">
- <CommandItem
- type="command"
- onSelect={() => {
- if (!search) {
- alert("You've selected me")
- }
- }}
- forceMount
- >
- <IconCopy className="mr-2" />
- <span>Copy SQL to clipboard</span>
- </CommandItem>
- {promptData.length > 1 && (
- <CommandItem
- type="command"
- forceMount
- onSelect={() => {
- if (!search) {
- dispatchPromptData({ type: 'remove-last-item' })
- setPromptIndex((x) => {
- return x - 1
- })
- // alert('back to previous version')
- }
- }}
- >
- <IconCopy className="mr-2" />
- <span>Back to previous version</span>
- </CommandItem>
- )}
- <CommandItem type="command" forceMount>
- <IconCopy className="mr-2" />
- <span>Run in SQL editor</span>
- </CommandItem>
- </CommandGroup>
- )}
</>
)
})}
@@ -559,7 +286,7 @@ const AiCommand = () => {
type="command"
onSelect={() => {
if (!search) {
- handleClippyConfirm(question)
+ handleConfirm(question)
}
}}
forceMount
@@ -572,35 +299,6 @@ const AiCommand = () => {
})}
</CommandGroup>
)}
-
- {/* {promptData.length <= 0 && page === COMMAND_ROUTES.AI_RLS_POLICY && (
- <CommandGroup heading="Examples" forceMount>
- {RLSquestions.map((question) => {
- const key = question.replace(/\s+/g, '_')
- return (
- <CommandItem
- onSelect={() => {
- if (!query) {
- handleClippyConfirm(question)
- }
- }}
- forceMount
- key={key}
- >
- <AiIcon />
- {question}
- </CommandItem>
- )
- })}
- </CommandGroup>
- )} */}
-
- {/* {isLoading && (
- <div className="p-6 grid gap-6 mt-4">
- <Loading active>{}</Loading>
- <p className="text-lg text-center">Searching for results</p>
- </div>
- )} */}
{hasClippyError && (
<div className="p-6 flex flex-col items-center gap-6 mt-4">
<IconAlertTriangle className="text-amber-900" strokeWidth={1.5} size={21} />
@@ -608,41 +306,27 @@ const AiCommand = () => {
Sorry, looks like Clippy is having a hard time!
</p>
<p className="text-sm text-scale-900 text-center">Please try again in a bit.</p>
- {/* <Button size="tiny" type="secondary" onClick={handleResetPrompt}>
+ <Button size="tiny" type="secondary" onClick={handleResetPrompt}>
Try again?
- </Button> */}
- </div>
- )}
- <div className="absolute right-0 top-0 mt-3 mr-4 hidden md:block">
- {/* <Button type="default" size="tiny" onClick={close}>
- esc
- </Button> */}
- </div>
- {/* {!isLoading && answer && (
- <div className="absolute right-0 top-0 mt-3 mr-16 hidden md:block">
- <Button type="text" size="tiny" onClick={handleResetPrompt}>
- Try again
</Button>
</div>
- )} */}
+ )}
</div>
<div className="absolute bottom-0 w-full bg-scale-200">
- {/* <Input */}
<Input
className="bg-scale-100 rounded mx-3"
autoFocus
type="textarea"
- placeholder={selectedTab === 'search-panel' ? 'Search documentation' : 'Ask a question'}
+ placeholder="Ask a question"
value={search}
onChange={(e) => setSearch(e.target.value)}
- // icon={<IconSearch size="small" />}
onKeyDown={(e) => {
switch (e.key) {
case 'Enter':
if (!search) {
return
}
- handleConfirm(selectedTab, search)
+ handleConfirm(search)
return
default:
return
@@ -651,17 +335,15 @@ const AiCommand = () => {
/>
<div className="text-scale-1100 px-3">
<div className="flex justify-between items-center text-xs">
- <div className="flex items-centerp gap-1 py-2 text-scale-800">
+ <div className="flex items-center gap-1 py-2 text-scale-800">
<span>Powered by OpenAI.</span>
</div>
<div className="flex items-center gap-6 py-1">
- {status ? (
+ {status && (
<span className="bg-scale-400 rounded-lg py-1 px-2 items-center gap-2 hidden md:flex">
{(isLoading || isResponding) && <IconLoader size={14} className="animate-spin" />}
{status}
</span>
- ) : (
- <></>
)}
</div>
</div>