Skip to content

Commit d60aceb

Browse files
SaxonFjordienrivasilovjoshenlim
authored
Prompt and tool refactoring (supabase#37500)
* try a really long context window to maximize caching * update examples * attempt to update packages and useChat * update endpoints * update zod * zod * update to v5 * message update * Revert "zod" This reverts commit ec39bac. * revert zod * zod i * fix complete endpoints * remove async * change to content * type cleanup * Revert the package bumps to rebuild them. * Bump zod to 2.25.76 in all packages. * Bump openai in all packages. * Bump ai and ai-related packages. * Remove unneeded files. * Fix the rest of the migration stuff. * Prettier fixes. * add policy list tool * refactor * ai sdk 5 fixes * refactor complete endpoint * edge function prompt * remove example * slight prompt change * Minor clean up * More clean up --------- Co-authored-by: Jordi Enric <[email protected]> Co-authored-by: Ivan Vasilov <[email protected]> Co-authored-by: Joshen Lim <[email protected]>
1 parent a023a59 commit d60aceb

File tree

22 files changed

+1384
-809
lines changed

22 files changed

+1384
-809
lines changed

apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { useCompletion } from '@ai-sdk/react'
12
import type { Monaco } from '@monaco-editor/react'
23
import { useQueryClient } from '@tanstack/react-query'
3-
import { useCompletion } from '@ai-sdk/react'
4-
import { AnimatePresence, motion } from 'framer-motion'
5-
import { ChevronUp, Command, Loader2 } from 'lucide-react'
4+
import { ChevronUp, Loader2 } from 'lucide-react'
65
import dynamic from 'next/dynamic'
76
import { useRouter } from 'next/router'
87
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
@@ -459,11 +458,12 @@ export const SQLEditor = () => {
459458
completion,
460459
isLoading: isCompletionLoading,
461460
} = useCompletion({
462-
api: `${BASE_PATH}/api/ai/sql/complete-v2`,
461+
api: `${BASE_PATH}/api/ai/code/complete`,
463462
body: {
464463
projectRef: project?.ref,
465464
connectionString: project?.connectionString,
466-
includeSchemaMetadata,
465+
language: 'sql',
466+
orgSlug: org?.slug,
467467
},
468468
onError: (error) => {
469469
toast.error(`Failed to generate SQL: ${error.message}`)

apps/studio/components/ui/AIAssistantPanel/AIAssistantChatSelector.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Check, ChevronDown, Edit, MessageSquare, Plus, Trash, X } from 'lucide-react'
1+
import { Check, ChevronDown, Edit, Plus, Trash, X } from 'lucide-react'
22
import { useState } from 'react'
33

44
import { useAiAssistantStateSnapshot } from 'state/ai-assistant-state'
@@ -18,17 +18,12 @@ import {
1818
PopoverTrigger_Shadcn_,
1919
ScrollArea,
2020
} from 'ui'
21-
import { ButtonTooltip } from '../ButtonTooltip'
2221

2322
interface AIAssistantChatSelectorProps {
24-
className?: string
2523
disabled?: boolean
2624
}
2725

28-
export const AIAssistantChatSelector = ({
29-
className,
30-
disabled = false,
31-
}: AIAssistantChatSelectorProps) => {
26+
export const AIAssistantChatSelector = ({ disabled = false }: AIAssistantChatSelectorProps) => {
3227
const snap = useAiAssistantStateSnapshot()
3328
const currentChat = snap.activeChat?.name
3429

@@ -211,7 +206,7 @@ export const AIAssistantChatSelector = ({
211206
disabled={disabled}
212207
>
213208
<Plus size={14} strokeWidth={1.5} />
214-
<span>New chat</span>
209+
<span>Start a new chat</span>
215210
</CommandItem_Shadcn_>
216211
</CommandGroup_Shadcn_>
217212
</CommandList_Shadcn_>

apps/studio/components/ui/AIEditor/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface AIEditorProps {
1919
aiMetadata?: {
2020
projectRef?: string
2121
connectionString?: string | null
22-
includeSchemaMetadata?: boolean
22+
orgSlug?: string
2323
}
2424
initialPrompt?: string
2525
readOnly?: boolean

apps/studio/components/ui/EditorPanel/EditorPanel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Results from 'components/interfaces/SQLEditor/UtilityPanel/Results'
1212
import { SqlRunButton } from 'components/interfaces/SQLEditor/UtilityPanel/RunButton'
1313
import { useSqlTitleGenerateMutation } from 'data/ai/sql-title-mutation'
1414
import { QueryResponseError, useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
15-
import { useOrgAiOptInLevel } from 'hooks/misc/useOrgOptedIntoAi'
15+
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
1616
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
1717
import { BASE_PATH } from 'lib/constants'
1818
import { uuidv4 } from 'lib/helpers'
@@ -93,7 +93,7 @@ export const EditorPanel = ({
9393
const { profile } = useProfile()
9494
const snapV2 = useSqlEditorV2StateSnapshot()
9595
const { mutateAsync: generateSqlTitle } = useSqlTitleGenerateMutation()
96-
const { includeSchemaMetadata } = useOrgAiOptInLevel()
96+
const { data: org } = useSelectedOrganizationQuery()
9797

9898
const [isSaving, setIsSaving] = useState(false)
9999
const [error, setError] = useState<QueryResponseError>()
@@ -328,11 +328,11 @@ export const EditorPanel = ({
328328
language="pgsql"
329329
value={currentValue}
330330
onChange={handleChange}
331-
aiEndpoint={`${BASE_PATH}/api/ai/sql/complete-v2`}
331+
aiEndpoint={`${BASE_PATH}/api/ai/code/complete`}
332332
aiMetadata={{
333333
projectRef: project?.ref,
334334
connectionString: project?.connectionString,
335-
includeSchemaMetadata,
335+
orgSlug: org?.slug,
336336
}}
337337
initialPrompt={initialPrompt}
338338
options={{

apps/studio/components/ui/FileExplorerAndEditor/FileExplorerAndEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface FileExplorerAndEditorProps {
2828
aiMetadata?: {
2929
projectRef?: string
3030
connectionString?: string | null
31-
includeSchemaMetadata?: boolean
31+
orgSlug?: string
3232
}
3333
}
3434

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { getOrganizations } from 'data/organizations/organizations-query'
2+
import { getProjects } from 'data/projects/projects-query'
3+
import { getAiOptInLevel } from 'hooks/misc/useOrgOptedIntoAi'
4+
5+
export const getOrgAIDetails = async ({
6+
orgSlug,
7+
authorization,
8+
projectRef,
9+
}: {
10+
orgSlug: string
11+
authorization: string
12+
projectRef: string
13+
}) => {
14+
const [organizations, projects] = await Promise.all([
15+
getOrganizations({
16+
headers: {
17+
'Content-Type': 'application/json',
18+
...(authorization && { Authorization: authorization }),
19+
},
20+
}),
21+
getProjects({
22+
headers: {
23+
'Content-Type': 'application/json',
24+
...(authorization && { Authorization: authorization }),
25+
},
26+
}),
27+
])
28+
29+
const selectedOrg = organizations.find((org) => org.slug === orgSlug)
30+
const selectedProject = projects.find(
31+
(project) => project.ref === projectRef || project.preview_branch_refs.includes(projectRef)
32+
)
33+
34+
// If the project is not in the organization specific by the org slug, return an error
35+
if (selectedProject?.organization_slug !== selectedOrg?.slug) {
36+
throw new Error('Project and organization do not match')
37+
}
38+
39+
const aiOptInLevel = getAiOptInLevel(selectedOrg?.opt_in_tags)
40+
const isLimited = selectedOrg?.plan.id === 'free'
41+
42+
return {
43+
aiOptInLevel,
44+
isLimited,
45+
}
46+
}

0 commit comments

Comments
 (0)