2024-05-26 12:18:09 -07:00
|
|
|
"use client"
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-10-21 13:57:45 -06:00
|
|
|
import { User } from "@/lib/types"
|
|
|
|
import { Editor } from "@monaco-editor/react"
|
2024-05-26 12:18:09 -07:00
|
|
|
import { Check, Loader2, RotateCw, Sparkles, X } from "lucide-react"
|
2024-10-21 13:57:45 -06:00
|
|
|
import { usePathname, useRouter } from "next/navigation"
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react"
|
2024-05-26 12:18:09 -07:00
|
|
|
import { toast } from "sonner"
|
2024-10-21 13:57:45 -06:00
|
|
|
import { Button } from "../ui/button"
|
2024-05-03 00:52:01 -07:00
|
|
|
// import monaco from "monaco-editor"
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
export default function GenerateInput({
|
2024-05-05 16:51:30 -07:00
|
|
|
user,
|
2024-05-02 17:38:37 -07:00
|
|
|
width,
|
2024-05-03 00:52:01 -07:00
|
|
|
data,
|
|
|
|
editor,
|
2024-05-02 17:38:37 -07:00
|
|
|
onExpand,
|
2024-05-02 18:05:18 -07:00
|
|
|
onAccept,
|
2024-05-14 01:13:57 -07:00
|
|
|
onClose,
|
2024-05-02 17:38:37 -07:00
|
|
|
}: {
|
2024-05-26 12:18:09 -07:00
|
|
|
user: User
|
|
|
|
width: number
|
2024-05-03 00:52:01 -07:00
|
|
|
data: {
|
2024-05-26 12:18:09 -07:00
|
|
|
fileName: string
|
|
|
|
code: string
|
|
|
|
line: number
|
|
|
|
}
|
2024-05-03 00:52:01 -07:00
|
|
|
editor: {
|
2024-05-26 12:18:09 -07:00
|
|
|
language: string
|
|
|
|
}
|
|
|
|
onExpand: () => void
|
|
|
|
onAccept: (code: string) => void
|
|
|
|
onClose: () => void
|
2024-05-02 17:38:37 -07:00
|
|
|
}) {
|
2024-05-26 12:18:09 -07:00
|
|
|
const pathname = usePathname()
|
|
|
|
const router = useRouter()
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
const [code, setCode] = useState("")
|
|
|
|
const [expanded, setExpanded] = useState(false)
|
2024-05-03 00:52:01 -07:00
|
|
|
const [loading, setLoading] = useState({
|
|
|
|
generate: false,
|
|
|
|
regenerate: false,
|
2024-05-26 12:18:09 -07:00
|
|
|
})
|
|
|
|
const [input, setInput] = useState("")
|
|
|
|
const [currentPrompt, setCurrentPrompt] = useState("")
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-02 18:05:18 -07:00
|
|
|
setTimeout(() => {
|
2024-05-26 12:18:09 -07:00
|
|
|
inputRef.current?.focus()
|
|
|
|
}, 100)
|
|
|
|
}, [inputRef.current])
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-03 00:52:01 -07:00
|
|
|
const handleGenerate = async ({
|
|
|
|
regenerate = false,
|
|
|
|
}: {
|
2024-05-26 12:18:09 -07:00
|
|
|
regenerate?: boolean
|
2024-05-03 00:52:01 -07:00
|
|
|
}) => {
|
2024-11-23 20:31:24 -05:00
|
|
|
try {
|
|
|
|
setLoading({ generate: !regenerate, regenerate })
|
|
|
|
setCurrentPrompt(input)
|
|
|
|
|
|
|
|
const response = await fetch("/api/ai", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2024-11-29 13:05:35 -05:00
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
role: "user",
|
|
|
|
content: regenerate ? currentPrompt : input,
|
|
|
|
},
|
|
|
|
],
|
2024-11-23 20:31:24 -05:00
|
|
|
context: null,
|
|
|
|
activeFileContent: data.code,
|
|
|
|
isEditMode: true,
|
|
|
|
fileName: data.fileName,
|
2024-11-29 13:05:35 -05:00
|
|
|
line: data.line,
|
2024-11-23 20:31:24 -05:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
const error = await response.text()
|
|
|
|
toast.error(error)
|
|
|
|
return
|
|
|
|
}
|
2024-05-05 16:51:30 -07:00
|
|
|
|
2024-11-23 20:31:24 -05:00
|
|
|
const reader = response.body?.getReader()
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
let result = ""
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-11-23 20:31:24 -05:00
|
|
|
if (reader) {
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read()
|
|
|
|
if (done) break
|
|
|
|
result += decoder.decode(value, { stream: true })
|
|
|
|
}
|
2024-05-03 00:52:01 -07:00
|
|
|
}
|
2024-11-23 20:31:24 -05:00
|
|
|
|
|
|
|
setCode(result.trim())
|
|
|
|
router.refresh()
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Generation error:", error)
|
|
|
|
toast.error("Failed to generate code")
|
|
|
|
} finally {
|
|
|
|
setLoading({ generate: false, regenerate: false })
|
|
|
|
}
|
2024-05-26 12:18:09 -07:00
|
|
|
}
|
2024-09-06 20:07:15 +01:00
|
|
|
const handleGenerateForm = useCallback(
|
|
|
|
(e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
e.preventDefault()
|
|
|
|
handleGenerate({ regenerate: false })
|
|
|
|
},
|
2024-09-06 15:25:50 -07:00
|
|
|
[input, currentPrompt]
|
2024-09-06 20:07:15 +01:00
|
|
|
)
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-03 00:52:01 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (code) {
|
2024-05-26 12:18:09 -07:00
|
|
|
setExpanded(true)
|
|
|
|
onExpand()
|
|
|
|
setLoading({ generate: false, regenerate: false })
|
2024-05-03 00:52:01 -07:00
|
|
|
}
|
2024-05-26 12:18:09 -07:00
|
|
|
}, [code])
|
2024-05-03 00:52:01 -07:00
|
|
|
|
2024-09-05 13:30:41 +01:00
|
|
|
useEffect(() => {
|
|
|
|
//listen to when Esc key is pressed and close the modal
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
onClose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener("keydown", handleKeyDown)
|
|
|
|
return () => window.removeEventListener("keydown", handleKeyDown)
|
|
|
|
}, [])
|
|
|
|
|
2024-05-02 17:38:37 -07:00
|
|
|
return (
|
|
|
|
<div className="w-full pr-4 space-y-2">
|
2024-09-06 20:07:15 +01:00
|
|
|
<form
|
|
|
|
onSubmit={handleGenerateForm}
|
|
|
|
className="flex items-center font-sans space-x-2"
|
|
|
|
>
|
2024-05-02 17:38:37 -07:00
|
|
|
<input
|
|
|
|
ref={inputRef}
|
|
|
|
style={{
|
|
|
|
width: width + "px",
|
|
|
|
}}
|
2024-05-02 18:05:18 -07:00
|
|
|
value={input}
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
2024-05-26 12:18:09 -07:00
|
|
|
placeholder="Generate code with a prompt"
|
2024-05-02 17:38:37 -07:00
|
|
|
className="h-8 w-full rounded-md border border-muted-foreground bg-transparent px-3 py-1 text-sm shadow-sm transition-all file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
|
|
|
|
/>
|
|
|
|
|
2024-05-02 18:05:18 -07:00
|
|
|
<Button
|
|
|
|
size="sm"
|
2024-09-06 20:07:15 +01:00
|
|
|
type="submit"
|
2024-05-03 00:52:01 -07:00
|
|
|
disabled={loading.generate || loading.regenerate || input === ""}
|
2024-05-02 18:05:18 -07:00
|
|
|
>
|
2024-05-03 00:52:01 -07:00
|
|
|
{loading.generate ? (
|
2024-05-02 17:38:37 -07:00
|
|
|
<>
|
|
|
|
<Loader2 className="animate-spin h-3 w-3 mr-2" />
|
|
|
|
Generating...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Sparkles className="h-3 w-3 mr-2" />
|
|
|
|
Generate Code
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Button>
|
2024-05-14 01:13:57 -07:00
|
|
|
<Button
|
|
|
|
onClick={onClose}
|
2024-09-06 20:07:15 +01:00
|
|
|
type="button"
|
2024-05-14 01:13:57 -07:00
|
|
|
variant="outline"
|
|
|
|
size="smIcon"
|
|
|
|
className="bg-transparent shrink-0 border-muted-foreground"
|
|
|
|
>
|
|
|
|
<X className="h-3 w-3" />
|
|
|
|
</Button>
|
2024-09-06 20:07:15 +01:00
|
|
|
</form>
|
2024-05-02 17:38:37 -07:00
|
|
|
{expanded ? (
|
|
|
|
<>
|
|
|
|
<div className="rounded-md border border-muted-foreground w-full h-28 overflow-y-scroll p-2">
|
2024-05-03 00:52:01 -07:00
|
|
|
<Editor
|
|
|
|
height="100%"
|
|
|
|
defaultLanguage={editor.language}
|
|
|
|
value={code}
|
|
|
|
options={{
|
|
|
|
minimap: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
scrollBeyondLastLine: false,
|
|
|
|
fontFamily: "var(--font-geist-mono)",
|
|
|
|
domReadOnly: true,
|
|
|
|
readOnly: true,
|
|
|
|
lineNumbers: "off",
|
|
|
|
glyphMargin: false,
|
|
|
|
folding: false,
|
|
|
|
// Undocumented see https://github.com/Microsoft/vscode/issues/30795#issuecomment-410998882
|
|
|
|
lineDecorationsWidth: 0,
|
|
|
|
lineNumbersMinChars: 0,
|
|
|
|
}}
|
|
|
|
theme="vs-dark"
|
|
|
|
/>
|
2024-05-02 17:38:37 -07:00
|
|
|
</div>
|
2024-05-03 00:52:01 -07:00
|
|
|
<div className="flex space-x-2 font-sans">
|
|
|
|
<Button
|
|
|
|
disabled={loading.generate || loading.regenerate}
|
|
|
|
onClick={() => onAccept(code)}
|
|
|
|
size="sm"
|
|
|
|
>
|
2024-05-02 17:38:37 -07:00
|
|
|
<Check className="h-3 w-3 mr-2" />
|
|
|
|
Accept
|
|
|
|
</Button>
|
|
|
|
<Button
|
2024-05-03 00:52:01 -07:00
|
|
|
onClick={() => handleGenerate({ regenerate: true })}
|
|
|
|
disabled={loading.generate || loading.regenerate}
|
2024-05-02 17:38:37 -07:00
|
|
|
variant="outline"
|
|
|
|
size="sm"
|
|
|
|
className="bg-transparent border-muted-foreground"
|
|
|
|
>
|
2024-05-03 00:52:01 -07:00
|
|
|
{loading.regenerate ? (
|
|
|
|
<>
|
|
|
|
<Loader2 className="animate-spin h-3 w-3 mr-2" />
|
|
|
|
Generating...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<RotateCw className="h-3 w-3 mr-2" />
|
|
|
|
Re-Generate
|
|
|
|
</>
|
|
|
|
)}
|
2024-05-02 17:38:37 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2024-05-26 12:18:09 -07:00
|
|
|
)
|
2024-05-02 17:38:37 -07:00
|
|
|
}
|