|
| 1 | +--- |
| 2 | +title: Custom AI Commands |
| 3 | +description: Customize the AI menu items (commands) in your BlockNote rich text editor |
| 4 | +imageTitle: BlockNote AI |
| 5 | +path: /docs/ai/custom-commands |
| 6 | +--- |
| 7 | + |
| 8 | +import { Example } from "@/components/example"; |
| 9 | +import { ThemedImage } from "@/components/ThemedImage"; |
| 10 | +import { Callout } from "nextra/components"; |
| 11 | + |
| 12 | +# Custom AI Menu Items (commands) |
| 13 | + |
| 14 | +A central part when users are interacting with the AI agent is the _AI Suggestion Menu_ where users can enter a custom prompt or select a pre-defined command: |
| 15 | + |
| 16 | +<ThemedImage |
| 17 | + src="/img/screenshots/ai-menu.png" |
| 18 | + darkImage="/img/screenshots/ai-menu-dark.png" |
| 19 | + alt="image" |
| 20 | + width={518} |
| 21 | + height={177} |
| 22 | +/> |
| 23 | + |
| 24 | +This menu is easy to customize so you can expose commands fine-tuned to your application. |
| 25 | + |
| 26 | +## Defining your own commands |
| 27 | + |
| 28 | +First, we define a new AI command. Let's create one that makes selected text more informal. |
| 29 | + |
| 30 | +```tsx |
| 31 | +import { AIMenuSuggestionItem, getAIExtension } from "@blocknote/xl-ai"; |
| 32 | + |
| 33 | +// Custom item to make the text more informal. |
| 34 | +export const makeInformal = ( |
| 35 | + editor: BlockNoteEditor, |
| 36 | +): AIMenuSuggestionItem => ({ |
| 37 | + key: "make_informal", |
| 38 | + title: "Make Informal", |
| 39 | + aliases: ["informal", "make informal", "casual"], |
| 40 | + icon: <RiEmotionHappyFill size={18} />, |
| 41 | + onItemClick: async () => { |
| 42 | + await getAIExtension(editor).callLLM({ |
| 43 | + // The prompt to send to the LLM: |
| 44 | + userPrompt: "Give the selected text a more informal (casual) tone", |
| 45 | + // Tell the LLM to specifically use the selected content as context (instead of the whole document) |
| 46 | + useSelection: true, |
| 47 | + // We only want the LLM to update selected text, not to add / delete blocks |
| 48 | + defaultStreamTools: { |
| 49 | + add: false, |
| 50 | + delete: false, |
| 51 | + update: true, |
| 52 | + }, |
| 53 | + }); |
| 54 | + }, |
| 55 | + size: "small", |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +Now, we create a customized AI Menu to show this command when the user has selected some text and opened the AI menu: |
| 60 | + |
| 61 | +```tsx |
| 62 | +import { AIMenu, getDefaultAIMenuItems } from "@blocknote/xl-ai"; |
| 63 | + |
| 64 | +function CustomAIMenu() { |
| 65 | + return ( |
| 66 | + <AIMenu |
| 67 | + items={( |
| 68 | + editor: BlockNoteEditor<any, any, any>, |
| 69 | + aiResponseStatus: |
| 70 | + | "user-input" |
| 71 | + | "thinking" |
| 72 | + | "ai-writing" |
| 73 | + | "error" |
| 74 | + | "user-reviewing" |
| 75 | + | "closed", |
| 76 | + ) => { |
| 77 | + if (aiResponseStatus === "user-input") { |
| 78 | + if (editor.getSelection()) { |
| 79 | + // When a selection is active (so when the AI Menu is opened via the Formatting Toolbar), |
| 80 | + // we add our `makeInformal` command to the default items. |
| 81 | + return [ |
| 82 | + ...getDefaultAIMenuItems(editor, aiResponseStatus), |
| 83 | + makeInformal(editor), |
| 84 | + ]; |
| 85 | + } else { |
| 86 | + return getDefaultAIMenuItems(editor, aiResponseStatus); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // for other states, return the default items |
| 91 | + return getDefaultAIMenuItems(editor, aiResponseStatus); |
| 92 | + }} |
| 93 | + /> |
| 94 | + ); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +Now, let's use this custom AI Menu in our app: |
| 99 | + |
| 100 | +```tsx |
| 101 | +<BlockNoteView |
| 102 | + editor={editor} |
| 103 | + formattingToolbar={false} |
| 104 | + slashMenu={false} |
| 105 | + > |
| 106 | + {/* Creates a new AIMenu with the default items, as well as our custom |
| 107 | + ones. */} |
| 108 | + <AIMenuController aiMenu={CustomAIMenu} /> |
| 109 | + |
| 110 | + {/* ...other UI Elements... */} |
| 111 | + <FormattingToolbarWithAI /> |
| 112 | + <SuggestionMenuWithAI editor={editor} /> |
| 113 | +</BlockNoteView> |
| 114 | +``` |
| 115 | + |
| 116 | +# Full example |
| 117 | + |
| 118 | +Have a look at the full example below, where we also add an AI menu item when no selection is open |
| 119 | +(e.g., when the editor is opened by typing `/ai` in the editor). |
| 120 | + |
| 121 | +<Example name="ai/custom-ai-menu-items" /> |
| 122 | + |
| 123 | +# Reference |
| 124 | + |
| 125 | +So far, we've added basic commands to the editor, but it's possible to completely customize low level prompts sent to the LLM. |
| 126 | +To learn in detail about the `callLLM` method used in this guide, continue to the [AI reference docs](/docs/ai/reference). |
0 commit comments