Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/add replace code to ai assist #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTelemetryProps } from 'common'
import { InsertCode, ReplaceCode } from 'icons'
import { Check, Copy } from 'lucide-react'
import { ArrowLeftRight, Check, Copy } from 'lucide-react'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { format } from 'sql-formatter'
Expand Down Expand Up @@ -113,6 +113,31 @@ export const AiMessagePre = ({ onDiff, children, className }: AiMessagePreProps)
Replace code
</TooltipContent_Shadcn_>
</Tooltip_Shadcn_>
<Tooltip_Shadcn_>
<TooltipTrigger_Shadcn_ asChild>
<Button
type="text"
size="tiny"
onClick={() => {
onDiff(DiffType.Overwrite, formatted)
Telemetry.sendEvent(
{
category: 'sql_editor_ai_assistant',
action: 'ai_suggestion_overwritten',
label: 'sql-editor-ai-assistant',
},
telemetryProps,
router
)
}}
>
<ArrowLeftRight className="h-3.5 w-3.5 text-foreground-light" strokeWidth={1.5} />
</Button>
</TooltipTrigger_Shadcn_>
<TooltipContent_Shadcn_ side="bottom" className="font-sans">
Overwrite code
</TooltipContent_Shadcn_>
</Tooltip_Shadcn_>

<Tooltip_Shadcn_>
<TooltipTrigger_Shadcn_ asChild>
Expand Down
37 changes: 30 additions & 7 deletions apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,34 @@ const SQLEditor = () => {
setAiQueryCount((count) => count + 1)

const existingValue = editorRef.current?.getValue() ?? ''
if (existingValue.length === 0) {
// if the editor is empty, just copy over the code

const applyDirectly = (content: string) => {
editorRef.current?.executeEdits('apply-ai-message', [
{
text: `${sqlAiDisclaimerComment}\n\n${sql}`,
text: content,
range: editorModel.getFullModelRange(),
},
])
} else {
}

const setupDiffView = (original: string, modified: string) => {
setSelectedMessage(id)
const currentSql = editorRef.current?.getValue()
const diff = { original: currentSql || '', modified: sql }
setSourceSqlDiff(diff)
setSourceSqlDiff({ original, modified })
setSelectedDiffType(diffType)
}

const sqlWithDisclaimer = `${sqlAiDisclaimerComment}\n\n${sql}`

switch (true) {
case existingValue.length === 0:
applyDirectly(sqlWithDisclaimer)
break
case diffType === DiffType.Overwrite:
applyDirectly(sqlWithDisclaimer)
break
default:
setupDiffView(existingValue, sql)
}
},
[setAiQueryCount]
)
Expand Down Expand Up @@ -554,6 +567,12 @@ const SQLEditor = () => {
return
}

case DiffType.Overwrite: {
const transformedDiff = compareAsNewSnippet(sourceSqlDiff)
applyDiff(transformedDiff)
return
}

default:
throw new Error(`Unknown diff type '${selectedDiffType}'`)
}
Expand Down Expand Up @@ -592,6 +611,10 @@ const SQLEditor = () => {
return compareAsNewSnippet(sourceSqlDiff)
}

case DiffType.Overwrite: {
return compareAsNewSnippet(sourceSqlDiff)
}

default:
return { original: '', modified: '' }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export enum DiffType {
Modification = 'modification',
Addition = 'addition',
NewSnippet = 'new-snippet',
Overwrite = 'overwrite',
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export function getDiffTypeButtonLabel(diffType: DiffType) {
return 'Accept addition'
case DiffType.NewSnippet:
return 'Create new snippet'
case DiffType.Overwrite:
return 'Overwrite with snippet'
default:
throw new Error(`Unknown diff type '${diffType}'`)
}
Expand All @@ -89,6 +91,8 @@ export function getDiffTypeDropdownLabel(diffType: DiffType) {
return 'Compare as addition'
case DiffType.NewSnippet:
return 'Compare as new snippet'
case DiffType.Overwrite:
return 'Overwrite as new snippet'
default:
throw new Error(`Unknown diff type '${diffType}'`)
}
Expand Down