|
| 1 | +/// <reference types="./vite-env.d.ts" /> |
| 2 | + |
| 3 | +import { createGroq } from "@ai-sdk/groq"; |
| 4 | +import { BlockNoteEditor, filterSuggestionItems } from "@blocknote/core"; |
| 5 | +import "@blocknote/core/fonts/inter.css"; |
| 6 | +import { en } from "@blocknote/core/locales"; |
| 7 | +import { BlockNoteView } from "@blocknote/mantine"; |
| 8 | +import "@blocknote/mantine/style.css"; |
| 9 | +import { |
| 10 | + FormattingToolbar, |
| 11 | + FormattingToolbarController, |
| 12 | + SuggestionMenuController, |
| 13 | + getDefaultReactSlashMenuItems, |
| 14 | + getFormattingToolbarItems, |
| 15 | + useCreateBlockNote, |
| 16 | +} from "@blocknote/react"; |
| 17 | +import { |
| 18 | + AIToolbarButton, |
| 19 | + BlockNoteAIUI, |
| 20 | + locales as aiLocales, |
| 21 | + createAIExtension, |
| 22 | + createBlockNoteAIClient, |
| 23 | + getAISlashMenuItems, |
| 24 | + AIMenu, |
| 25 | + getDefaultAIMenuItemsWithSelection, |
| 26 | + getDefaultAIMenuItemsWithoutSelection, |
| 27 | + getDefaultAIActionMenuItems, |
| 28 | + AIMenuController, |
| 29 | +} from "@blocknote/xl-ai"; |
| 30 | +import "@blocknote/xl-ai/style.css"; |
| 31 | + |
| 32 | +import { findRelatedTopics, makeCasual } from "./customAIMenuItems.js"; |
| 33 | + |
| 34 | +// Optional: proxy requests through the `@blocknote/xl-ai-server` proxy server |
| 35 | +// so that we don't have to expose our API keys to the client |
| 36 | +const client = createBlockNoteAIClient({ |
| 37 | + apiKey: import.meta.env.VITE_BLOCKNOTE_AI_SERVER_API_KEY || "PLACEHOLDER", |
| 38 | + baseURL: |
| 39 | + import.meta.env.VITE_BLOCKNOTE_AI_SERVER_BASE_URL || |
| 40 | + "https://localhost:3000/ai", |
| 41 | +}); |
| 42 | + |
| 43 | +// Use an "open" model such as llama, in this case via groq.com |
| 44 | +const model = createGroq({ |
| 45 | + // call via our proxy client |
| 46 | + ...client.getProviderSettings("groq"), |
| 47 | +})("llama-3.3-70b-versatile"); |
| 48 | + |
| 49 | +/* |
| 50 | +ALTERNATIVES: |
| 51 | +
|
| 52 | +Call a model directly (without the proxy): |
| 53 | +
|
| 54 | +const model = createGroq({ |
| 55 | + apiKey: "<YOUR_GROQ_API_KEY>", |
| 56 | +})("llama-3.3-70b-versatile"); |
| 57 | +
|
| 58 | +Or, use a different provider like OpenAI: |
| 59 | +
|
| 60 | +const model = createOpenAI({ |
| 61 | + ...client.getProviderSettings("openai"), |
| 62 | +})("gpt-4", {}); |
| 63 | +*/ |
| 64 | + |
| 65 | +export default function App() { |
| 66 | + // Creates a new editor instance. |
| 67 | + const editor = useCreateBlockNote({ |
| 68 | + dictionary: { |
| 69 | + ...en, |
| 70 | + ai: aiLocales.en, // add default translations for the AI extension |
| 71 | + }, |
| 72 | + // Register the AI extension |
| 73 | + extensions: [ |
| 74 | + createAIExtension({ |
| 75 | + model, |
| 76 | + }), |
| 77 | + ], |
| 78 | + // We set some initial content for demo purposes |
| 79 | + initialContent: [ |
| 80 | + { |
| 81 | + type: "heading", |
| 82 | + props: { |
| 83 | + level: 1, |
| 84 | + }, |
| 85 | + content: "I love cats", |
| 86 | + }, |
| 87 | + { |
| 88 | + type: "paragraph", |
| 89 | + content: |
| 90 | + "Cats are one of the most beloved and fascinating animals in the world. Known for their agility, independence, and charm, cats have been companions to humans for thousands of years. Domesticated cats, scientifically named Felis catus, come in various breeds, colors, and personalities, making them a popular choice for pet owners everywhere. Their mysterious behavior, sharp reflexes, and quiet affection have earned them a special place in countless households.", |
| 91 | + }, |
| 92 | + { |
| 93 | + type: "paragraph", |
| 94 | + content: |
| 95 | + "Beyond their role as pets, cats have a rich history and cultural significance. In ancient Egypt, they were revered and even worshipped as symbols of protection and grace. Throughout history, they’ve appeared in folklore, art, and literature, often associated with curiosity, luck, and mystery. Despite superstitions surrounding black cats in some cultures, many societies around the world admire and cherish these sleek and graceful animals.", |
| 96 | + }, |
| 97 | + { |
| 98 | + type: "paragraph", |
| 99 | + content: |
| 100 | + "Cats also offer emotional and physical benefits to their owners. Studies have shown that interacting with cats can reduce stress, lower blood pressure, and improve mental well-being. Their gentle purring, playful antics, and warm companionship provide comfort to people of all ages. Whether lounging in the sun, chasing a toy, or curling up on a lap, cats bring joy, peace, and a bit of magic to the lives of those who welcome them into their homes.", |
| 101 | + }, |
| 102 | + ], |
| 103 | + }); |
| 104 | + |
| 105 | + // Renders the editor instance using a React component. |
| 106 | + return ( |
| 107 | + <div> |
| 108 | + <BlockNoteView |
| 109 | + editor={editor} |
| 110 | + formattingToolbar={false} |
| 111 | + slashMenu={false} |
| 112 | + > |
| 113 | + {/* This has AI specific components like the AI Command menu */} |
| 114 | + {/* We pass `aiMenu=false` as we want to render an AIMenu with our own |
| 115 | + items (defined below). */} |
| 116 | + <BlockNoteAIUI aiMenu={false}></BlockNoteAIUI> |
| 117 | + {/* Creates a new AIMenu with the default items, as well as our custom |
| 118 | + ones. */} |
| 119 | + <AIMenuController |
| 120 | + aiMenu={() => ( |
| 121 | + <AIMenu |
| 122 | + items={(editor, aiResponseStatus) => { |
| 123 | + if (aiResponseStatus === "user-input") { |
| 124 | + // Returns different items based on whether the AI Menu was |
| 125 | + // opened via the Formatting Toolbar or the Slash Menu. |
| 126 | + return editor.getSelection() |
| 127 | + ? [ |
| 128 | + // Gets the default AI Menu items for when it's opened via |
| 129 | + // the Formatting Toolbar. |
| 130 | + ...getDefaultAIMenuItemsWithSelection(editor), |
| 131 | + // Adds our custom item to make the text more casual. |
| 132 | + // Only appears when the AI Menu is opened via the |
| 133 | + // Formatting Toolbar. |
| 134 | + makeCasual(editor), |
| 135 | + ] |
| 136 | + : [ |
| 137 | + // Gets the default AI Menu items for when it's opened |
| 138 | + // via the Slash Menu. |
| 139 | + ...getDefaultAIMenuItemsWithoutSelection(editor), |
| 140 | + // Adds our custom item to find related topics. Only |
| 141 | + // appears when the AI Menu is opened via the Slash |
| 142 | + // Menu. |
| 143 | + findRelatedTopics(editor), |
| 144 | + ]; |
| 145 | + } |
| 146 | + |
| 147 | + if (aiResponseStatus === "user-reviewing") { |
| 148 | + // Returns different items once the AI has finished writing, |
| 149 | + // so the user can choose to accept or reject the changes. |
| 150 | + return getDefaultAIActionMenuItems(editor); |
| 151 | + } |
| 152 | + |
| 153 | + // Return no items in other states, e.g. when the AI is writing |
| 154 | + // or when an error occurred. |
| 155 | + return []; |
| 156 | + }} |
| 157 | + /> |
| 158 | + )} |
| 159 | + /> |
| 160 | + |
| 161 | + {/* We disabled the default formatting toolbar with `formattingToolbar=false` |
| 162 | + and replace it for one with an "AI button" (defined below). |
| 163 | + (See "Formatting Toolbar" in docs) |
| 164 | + */} |
| 165 | + <FormattingToolbarWithAI /> |
| 166 | + |
| 167 | + {/* We disabled the default SlashMenu with `slashMenu=false` |
| 168 | + and replace it for one with an AI option (defined below). |
| 169 | + (See "Suggestion Menus" in docs) |
| 170 | + */} |
| 171 | + <SuggestionMenuWithAI editor={editor} /> |
| 172 | + </BlockNoteView> |
| 173 | + </div> |
| 174 | + ); |
| 175 | +} |
| 176 | + |
| 177 | +// Formatting toolbar with the `AIToolbarButton` added |
| 178 | +function FormattingToolbarWithAI() { |
| 179 | + return ( |
| 180 | + <FormattingToolbarController |
| 181 | + formattingToolbar={() => ( |
| 182 | + <FormattingToolbar> |
| 183 | + {...getFormattingToolbarItems()} |
| 184 | + <AIToolbarButton /> |
| 185 | + </FormattingToolbar> |
| 186 | + )} |
| 187 | + /> |
| 188 | + ); |
| 189 | +} |
| 190 | + |
| 191 | +// Slash menu with the AI option added |
| 192 | +function SuggestionMenuWithAI(props: { |
| 193 | + editor: BlockNoteEditor<any, any, any>; |
| 194 | +}) { |
| 195 | + return ( |
| 196 | + <SuggestionMenuController |
| 197 | + triggerCharacter="/" |
| 198 | + getItems={async (query) => |
| 199 | + filterSuggestionItems( |
| 200 | + [ |
| 201 | + ...getDefaultReactSlashMenuItems(props.editor), |
| 202 | + ...getAISlashMenuItems(props.editor), |
| 203 | + ], |
| 204 | + query, |
| 205 | + ) |
| 206 | + } |
| 207 | + /> |
| 208 | + ); |
| 209 | +} |
0 commit comments