-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from supabase-community/feat/drop-sql-file
feat: import sql files
- Loading branch information
Showing
7 changed files
with
255 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useMemo } from 'react' | ||
import { formatSql } from '~/lib/sql-util' | ||
import { ToolInvocation } from '~/lib/tools' | ||
import CodeAccordion from '../code-accordion' | ||
|
||
export type SqlImportProps = { | ||
toolInvocation: ToolInvocation<'importSql'> | ||
} | ||
|
||
export default function SqlImport({ toolInvocation }: SqlImportProps) { | ||
const { fileId, sql } = toolInvocation.args | ||
|
||
const formattedSql = useMemo(() => formatSql(sql), [sql]) | ||
|
||
if (!('result' in toolInvocation)) { | ||
return null | ||
} | ||
|
||
const { result } = toolInvocation | ||
|
||
if (!result.success) { | ||
return ( | ||
<CodeAccordion | ||
title="Error executing SQL" | ||
language="sql" | ||
code={formattedSql ?? sql} | ||
error={result.error} | ||
/> | ||
) | ||
} | ||
|
||
return <CodeAccordion title="Executed SQL" language="sql" code={formattedSql ?? sql} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { generateId } from 'ai' | ||
import { useChat } from 'ai/react' | ||
import { m } from 'framer-motion' | ||
import { Paperclip } from 'lucide-react' | ||
import { loadFile, saveFile } from '~/lib/files' | ||
import { ToolInvocation } from '~/lib/tools' | ||
import { downloadFile } from '~/lib/util' | ||
import { useWorkspace } from '../workspace' | ||
|
||
export type SqlRequestProps = { | ||
toolInvocation: ToolInvocation<'requestSql'> | ||
} | ||
|
||
export default function SqlRequest({ toolInvocation }: SqlRequestProps) { | ||
const { databaseId } = useWorkspace() | ||
|
||
const { addToolResult } = useChat({ | ||
id: databaseId, | ||
api: '/api/chat', | ||
}) | ||
|
||
if ('result' in toolInvocation) { | ||
const { result } = toolInvocation | ||
|
||
if (!result.success) { | ||
return ( | ||
<m.div | ||
layout="position" | ||
layoutId={toolInvocation.toolCallId} | ||
className="self-end px-5 py-2.5 text-base rounded-full bg-destructive flex gap-2 items-center text-lighter italic" | ||
> | ||
No SQL file selected | ||
</m.div> | ||
) | ||
} | ||
|
||
return ( | ||
<m.div | ||
layout="position" | ||
layoutId={toolInvocation.toolCallId} | ||
className="self-end px-5 py-2.5 text-base rounded-full bg-border flex gap-2 items-center text-lighter italic" | ||
style={{ | ||
// same value as tailwind, used to keep constant radius during framer animation | ||
// see: https://www.framer.com/motion/layout-animations/##scale-correction | ||
borderRadius: 9999, | ||
}} | ||
> | ||
<Paperclip size={14} /> | ||
<m.span | ||
className="cursor-pointer hover:underline" | ||
layout | ||
onClick={async () => { | ||
const file = await loadFile(result.fileId) | ||
downloadFile(file) | ||
}} | ||
> | ||
{result.file.name} | ||
</m.span> | ||
</m.div> | ||
) | ||
} | ||
|
||
return ( | ||
<m.div layout="position" layoutId={toolInvocation.toolCallId}> | ||
<input | ||
type="file" | ||
onChange={async (e) => { | ||
if (e.target.files) { | ||
try { | ||
const [file] = Array.from(e.target.files) | ||
|
||
if (!file) { | ||
throw new Error('No file found') | ||
} | ||
|
||
if (file.type !== 'text/sql') { | ||
throw new Error('File is not a SQL file') | ||
} | ||
|
||
const fileId = generateId() | ||
|
||
await saveFile(fileId, file) | ||
|
||
const text = await file.text() | ||
|
||
addToolResult({ | ||
toolCallId: toolInvocation.toolCallId, | ||
result: { | ||
success: true, | ||
fileId: fileId, | ||
file: { | ||
name: file.name, | ||
size: file.size, | ||
type: file.type, | ||
lastModified: file.lastModified, | ||
}, | ||
preview: text.split('\n').slice(0, 10).join('\n').trim(), | ||
}, | ||
}) | ||
} catch (error) { | ||
addToolResult({ | ||
toolCallId: toolInvocation.toolCallId, | ||
result: { | ||
success: false, | ||
error: error instanceof Error ? error.message : 'An unknown error occurred', | ||
}, | ||
}) | ||
} | ||
} | ||
}} | ||
/> | ||
</m.div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters