import { useState, useEffect, useRef } from "react"; import { Button } from "../components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "../components/ui/popover"; import { Textarea } from "../components/ui/textarea"; import { AiIcon } from "../icons/AiIcon"; import { HotkeyIndication } from "../components/ui/hotkey-indication"; import { cn } from "../lib/utils"; import { useAiCompletion } from "./context"; interface AiGenerateProps { prompt: string; placeholder?: string; onComplete: (generatedText: string) => void; className?: string; jsonMode?: boolean; currentValue?: any; nodeId: string; insId?: string; } export function AiGenerate({ prompt, placeholder, onComplete, className, jsonMode = false, currentValue, nodeId, insId }: AiGenerateProps) { const { createCompletion, enabled } = useAiCompletion(); const [isOpen, setIsOpen] = useState(false); const [isGenerating, setIsGenerating] = useState(false); const [input, setInput] = useState(""); const textareaRef = useRef(null); useEffect(() => { if (isOpen) { // Give the popover time to render requestAnimationFrame(() => { textareaRef.current?.focus(); }); } }, [isOpen]); if (!enabled) { return null; } const handleGenerate = async () => { try { setIsGenerating(true); // Replace template variables in the prompt let processedPrompt = prompt.replace(/{{prompt}}/g, input); // Handle the currentValue replacement if (currentValue !== undefined) { const valueString = typeof currentValue === "object" ? JSON.stringify(currentValue, null, 2) : String(currentValue); processedPrompt = processedPrompt.replace(/{{value}}/g, valueString); } const result = await createCompletion({ prompt: processedPrompt, jsonMode, nodeId, insId: insId || "" }); onComplete(result); setInput(""); setIsOpen(false); } finally { setIsGenerating(false); } }; return (