Files
supabase/apps/studio/components/ui/AIEditor/AskAIWidget.tsx
Joshen Lim dde45ab06c Joshenlim/fe 4195 explorer queryeditor cmd k completion support (#49248)
## Context

Adds the inline AI completion functionality into Explorer QueryEditor,
similar to what we've got for the existing SQL editor
- Shifts the `ResizableAIWidget` and `InlineWidget` components out of
the SQL Editor folder into `components/ui/AiEditor` to be used by both
SQL Editor and Query Editor
- Consolidates the "proposal" logic that was initially set up for the
Clickhouse Migration functionality with this Inline AI stuff
- Also added the prompt into the proposal header (Refer to the
screenshots below)
- SQL Editor didn't have this - but figured this is useful as context
for the user

<img width="935" height="352" alt="image"
src="https://github.com/user-attachments/assets/3f71539a-dda1-4763-be08-a850bdc8aec6"
/>

Source selected: Database
<img width="922" height="357" alt="image"
src="https://github.com/user-attachments/assets/81e772e6-dcc9-440d-83db-49d0d487dd13"
/>

Source selected: Logs
<img width="920" height="345" alt="image"
src="https://github.com/user-attachments/assets/83f1dae3-cf62-4424-be42-b06e70cb366d"
/>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added AI-assisted SQL generation with contextual prompts and
OS-specific guidance.
- Review generated SQL changes in a diff, then accept, reject, or cancel
suggestions.
- Added inline, resizable AI prompt controls with loading and submission
states.
  - Added the Ctrl/Cmd+Shift+K shortcut to run AI SQL generation.

- **Improvements**
  - Added options to disable query execution and run custom actions.
  - Renamed “Recent” to “Recently updated.”
  - Improved editor widget positioning and display behavior.
  - Added clearer error notifications when AI generation fails.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-20 14:05:38 +08:00

112 lines
3.5 KiB
TypeScript

import { ChangeEvent, KeyboardEvent, useCallback, useEffect, useRef } from 'react'
import { Button, ExpandingTextArea } from 'ui'
interface AskAIWidgetProps {
value: string
onChange: (value: string) => void
onSubmit: (prompt: string) => void
onAccept?: () => void
onReject?: () => void
onCancel?: () => void
isDiffVisible: boolean
isLoading?: boolean
}
export const AskAIWidget = ({
value,
onChange,
onSubmit,
onAccept,
onReject,
onCancel,
isDiffVisible,
isLoading = false,
}: AskAIWidgetProps) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null)
useEffect(() => {
setTimeout(() => {
textAreaRef.current?.focus()
textAreaRef.current?.setSelectionRange(value.length, value.length)
}, 100)
}, [])
const handleSubmit = useCallback(() => {
if (value.trim() && !isLoading) {
onSubmit(value)
}
}, [value, isLoading, onSubmit])
const handleChange = useCallback(
(e: ChangeEvent<HTMLTextAreaElement>) => {
onChange(e.target.value)
},
[onChange]
)
const handleKeyDown = useCallback(
(e: KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey && !e.metaKey && !e.ctrlKey) {
e.preventDefault()
handleSubmit()
}
},
[handleSubmit]
)
return (
<div className="overflow-hidden rounded-md p-0 bg-popover border border-foreground/20 focus-within:border-foreground/30 shadow-xl text-sm max-w-xl">
<ExpandingTextArea
ref={textAreaRef}
className="bg-transparent border-0 outline-0 ring-0 ring-offset-0 focus:outline-0 focus:ring-0 focus:ring-offset-0 focus-visible:outline-0 focus-visible:ring-0 focus-visible:ring-offset-0 focus-within:outline-0 focus-within:ring-0 focus-within:ring-offset-0 shadow-none rounded-none gap-4 text-xs md:text-xs py-2 pl-3 leading-[20px]!"
placeholder={isDiffVisible ? 'Make an edit...' : 'Edit via the Assistant...'}
value={value}
onChange={handleChange}
onKeyDown={handleKeyDown}
disabled={isLoading}
/>
{isDiffVisible ? (
<div className="flex justify-start p-0 border-t">
<Button
variant="text"
onClick={onAccept}
className="text-xs h-auto py-1 rounded-none px-3 border-r-border"
disabled={isLoading}
>
Accept <span className="text-xs text-foreground-light"> + Enter</span>
</Button>
<Button
onClick={onReject}
variant="text"
className="text-xs h-auto py-1 rounded-none px-3 border-r-border"
disabled={isLoading}
>
Reject <span className="text-xs text-foreground-light">Esc</span>
</Button>
</div>
) : (
<div className="flex justify-start p-0 border-t">
<Button
variant="text"
onClick={handleSubmit}
loading={isLoading}
className="text-xs h-auto py-1 rounded-none px-3 border-r-border"
disabled={isLoading}
>
{isLoading ? 'Generating...' : 'Generate'}{' '}
{!isLoading && <span className="text-xs text-foreground-light">Enter</span>}
</Button>
<Button
onClick={onCancel}
variant="text"
className="text-xs h-auto py-1 rounded-none px-3 border-r-border"
disabled={isLoading}
>
Cancel <span className="text-xs text-foreground-light">Esc</span>
</Button>
</div>
)}
</div>
)
}