diff options
| author | Dhravya <[email protected]> | 2024-06-19 20:04:15 -0500 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-06-19 20:04:15 -0500 |
| commit | e4c6c1aab001be6cbb9473f276002c36f643c9b6 (patch) | |
| tree | eecf0d61c2e1000339fe708700779ce2f3725e8e | |
| parent | Another Failed Attempt at streaming (diff) | |
| download | supermemory-e4c6c1aab001be6cbb9473f276002c36f643c9b6.tar.xz supermemory-e4c6c1aab001be6cbb9473f276002c36f643c9b6.zip | |
works
| -rw-r--r-- | apps/cf-ai-backend/package.json | 2 | ||||
| -rw-r--r-- | apps/web/app/(editor)/components/aigenerate.tsx | 78 | ||||
| -rw-r--r-- | apps/web/app/(editor)/editor.tsx | 5 |
3 files changed, 33 insertions, 52 deletions
diff --git a/apps/cf-ai-backend/package.json b/apps/cf-ai-backend/package.json index 480f9601..78353e08 100644 --- a/apps/cf-ai-backend/package.json +++ b/apps/cf-ai-backend/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "jest --verbose", "deploy": "wrangler deploy", - "dev": "wrangler dev", + "dev": "wrangler dev --remote --port 8686", "start": "wrangler dev", "unsafe-reset-vector-db": "wrangler vectorize delete supermem-vector && wrangler vectorize create --dimensions=1536 supermem-vector-1 --metric=cosine" }, diff --git a/apps/web/app/(editor)/components/aigenerate.tsx b/apps/web/app/(editor)/components/aigenerate.tsx index c9ba657b..de9b2a3f 100644 --- a/apps/web/app/(editor)/components/aigenerate.tsx +++ b/apps/web/app/(editor)/components/aigenerate.tsx @@ -8,8 +8,7 @@ import Autocompletesvg from "./ui/autocompletesvg"; import { motion, AnimatePresence } from "framer-motion"; import type { Editor } from "@tiptap/core"; import { useEditor } from "novel"; -import { NodeSelection } from 'prosemirror-state' - +import { NodeSelection } from "prosemirror-state"; function Aigenerate() { const [visible, setVisible] = useState(false); @@ -56,6 +55,7 @@ function Aigenerate() { }} className="absolute z-50 top-0" > + {/* TODO: handle Editor not initalised, maybe with a loading state. */} <ToolBar setGeneratingfn={setGeneratingfn} editor={editor} /> <div className="h-8 w-18rem bg-blue-600 blur-[16rem]" /> </motion.div> @@ -66,10 +66,22 @@ function Aigenerate() { export default Aigenerate; const options = [ - <><Translatesvg />Translate</>, - <><Rewritesvg />Change Tone</>, - <><Asksvg />Ask Gemini</>, - <><Autocompletesvg />Auto Complete</> + <> + <Translatesvg /> + Translate + </>, + <> + <Rewritesvg /> + Change Tone + </>, + <> + <Asksvg /> + Ask Gemini + </>, + <> + <Autocompletesvg /> + Auto Complete + </>, ]; function ToolBar({ @@ -134,7 +146,7 @@ async function AigenerateContent({ setGeneratingfn(true); const { from, to } = editor.view.state.selection; - + const slice = editor.state.selection.content(); const text = editor.storage.markdown.serializer.serialize(slice.content); @@ -143,9 +155,8 @@ async function AigenerateContent({ "change tone, improve the way be more formal", "ask, answer the question", "continue this, minimum 80 characters, do not repeat just continue don't use ... to denote start", - ] + ]; - // ERROR #3 - This is where we call the ai generate api const resp = await fetch("/api/editorai", { method: "POST", body: JSON.stringify({ @@ -154,53 +165,22 @@ async function AigenerateContent({ }), }); - // this is the exact replica of your chatwindow code, I have - // 2 more versions of these code commented below, but they also dont work const reader = resp.body?.getReader(); let done = false; + let position = to; while (!done && reader) { const { value, done: d } = await reader.read(); done = d; - console.log(new TextDecoder().decode(value)) + const decoded = new TextDecoder().decode(value); + console.log(decoded); + editor + .chain() + .focus() + .insertContentAt(position + 1, decoded) + .run(); + position += decoded.length; } - // 2nd Method - // if (!resp.body) { - // console.error("No response body"); - // return; - // } - // const reader = resp.body.getReader(); - // const decoder = new TextDecoder(); - // let done = false; - // let position = to; - - // while (!done) { - // const { value, done: readerDone } = await reader.read(); - // done = readerDone; - // if (value) { - // const chunk = decoder.decode(value, { stream: true }); - // console.log(chunk); - // editor.chain().focus().insertContentAt(position + 1, chunk).run(); - // position += chunk.length; - // } - // } - // console.log("Stream complete"); - - - // 3rd method - // const reader = resp.body?.getReader(); - // let done = false; - // let position = from; - // while (!done && reader) { - // const { value, done: d } = await reader.read(); - // done = d; - - // const cont = new TextDecoder().decode(value) - // console.log(cont); - // } - // const {completion}: {completion: string} = await res.json(); - // console.log(completion) - setGeneratingfn(false); } diff --git a/apps/web/app/(editor)/editor.tsx b/apps/web/app/(editor)/editor.tsx index 5b4a60ce..f7f9a098 100644 --- a/apps/web/app/(editor)/editor.tsx +++ b/apps/web/app/(editor)/editor.tsx @@ -15,19 +15,20 @@ import Topbar from "./components/topbar"; const Editor = () => { const [initialContent, setInitialContent] = useState<null | JSONContent>( - null + null, ); const [saveStatus, setSaveStatus] = useState("Saved"); const [charsCount, setCharsCount] = useState(); const [visible, setVisible] = useState(true); useEffect(() => { + if (typeof window === "undefined") return; const content = window.localStorage.getItem("novel-content"); if (content) setInitialContent(JSON.parse(content)); else setInitialContent(defaultEditorContent); }, []); - if (!initialContent) return null; + if (!initialContent) return <>Loading...</>; return ( <div className="relative w-full max-w-screen-xl"> |