"use client"; import { Command, CommandInput } from "../../ui/command"; import { useCompletion } from "@ai-sdk/react"; import { ArrowUp } from "lucide-react"; import { useEditor } from "novel"; import { addAIHighlight } from "novel"; import { useState } from "react"; import Markdown from "react-markdown"; import { toast } from "sonner"; import { Button } from "../../ui/button"; import Magic from "../../ui/icons/magic"; import { ScrollArea } from "../../ui/scroll-area"; import AICompletionCommands from "./ai-completion-command"; import AISelectorCommands from "./ai-selector-commands"; import { LoadingOutlined } from "@ant-design/icons"; //TODO: I think it makes more sense to create a custom Tiptap extension for this functionality https://tiptap.dev/docs/editor/ai/introduction interface AISelectorProps { open: boolean; onOpenChange: (open: boolean) => void; } export function AISelector({ onOpenChange }: AISelectorProps) { const { editor } = useEditor(); const [inputValue, setInputValue] = useState(""); const { completion, complete, isLoading } = useCompletion({ // id: "novel", api: "/api/generate", onResponse: (response) => { if (response.status === 429) { toast.error("You have reached your request limit for the day."); return; } }, onError: (e) => { toast.error(e.message); }, }); if (!editor) return null; const hasCompletion = completion.length > 0; return ( {hasCompletion && (
{completion}
)} {isLoading && (
AI is thinking
)} {!isLoading && ( <>
addAIHighlight(editor)} />
{hasCompletion ? ( { editor.chain().unsetHighlight().focus().run(); onOpenChange(false); }} completion={completion} /> ) : ( complete(value, { body: { option } }) } /> )} )}
); }