Skip to content

docs: Custom AI menu items docs #1690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/09-ai/01-minimal/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export default function App() {
editor={editor}
// We're disabling some default UI elements
formattingToolbar={false}
slashMenu={false}>
slashMenu={false}
>
{/* This has AI specific components like the AI Command menu */}
<BlockNoteAIUI />

Expand Down
5 changes: 4 additions & 1 deletion examples/09-ai/01-minimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ Select some text and click the AI (magic wand) button, or type `/ai` anywhere in

**Relevant Docs:**

- TODO
- [Editor Setup](/docs/editor-basics/setup)
- [Changing the Formatting Toolbar](/docs/ui-components/formatting-toolbar#changing-the-formatting-toolbar)
- [Changing Slash Menu Items](/docs/ui-components/suggestion-menus#changing-slash-menu-items)
- [Getting Stared with BlockNote AI](/docs/ai/setup)
5 changes: 4 additions & 1 deletion examples/09-ai/02-playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ Change the configuration, the highlight some text to access the AI menu, or type

**Relevant Docs:**

- TODO
- [Editor Setup](/docs/editor-basics/setup)
- [Changing the Formatting Toolbar](/docs/ui-components/formatting-toolbar#changing-the-formatting-toolbar)
- [Changing Slash Menu Items](/docs/ui-components/suggestion-menus#changing-slash-menu-items)
- [Getting Stared with BlockNote AI](/docs/ai/setup)
15 changes: 15 additions & 0 deletions examples/09-ai/03-ai-menu-items/.bnexample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"playground": true,
"docs": true,
"author": "matthewlipski",
"tags": ["AI", "llm"],
"dependencies": {
"@blocknote/xl-ai": "latest",
"@mantine/core": "^7.10.1",
"ai": "^4.1.0",
"@ai-sdk/openai": "^1.1.0",
"@ai-sdk/groq": "^1.1.0",
"react-icons": "^5.2.1",
"zustand": "^5.0.3"
}
}
209 changes: 209 additions & 0 deletions examples/09-ai/03-ai-menu-items/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/// <reference types="./vite-env.d.ts" />

import { createGroq } from "@ai-sdk/groq";
import { BlockNoteEditor, filterSuggestionItems } from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import { en } from "@blocknote/core/locales";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import {
FormattingToolbar,
FormattingToolbarController,
SuggestionMenuController,
getDefaultReactSlashMenuItems,
getFormattingToolbarItems,
useCreateBlockNote,
} from "@blocknote/react";
import {
AIToolbarButton,
BlockNoteAIUI,
locales as aiLocales,
createAIExtension,
createBlockNoteAIClient,
getAISlashMenuItems,
AIMenu,
getDefaultAIMenuItemsWithSelection,
getDefaultAIMenuItemsWithoutSelection,
getDefaultAIActionMenuItems,
AIMenuController,
} from "@blocknote/xl-ai";
import "@blocknote/xl-ai/style.css";

import { findRelatedTopics, makeCasual } from "./customAIMenuItems.js";

// Optional: proxy requests through the `@blocknote/xl-ai-server` proxy server
// so that we don't have to expose our API keys to the client
const client = createBlockNoteAIClient({
apiKey: import.meta.env.VITE_BLOCKNOTE_AI_SERVER_API_KEY || "PLACEHOLDER",
baseURL:
import.meta.env.VITE_BLOCKNOTE_AI_SERVER_BASE_URL ||
"https://localhost:3000/ai",
});

// Use an "open" model such as llama, in this case via groq.com
const model = createGroq({
// call via our proxy client
...client.getProviderSettings("groq"),
})("llama-3.3-70b-versatile");

/*
ALTERNATIVES:

Call a model directly (without the proxy):

const model = createGroq({
apiKey: "<YOUR_GROQ_API_KEY>",
})("llama-3.3-70b-versatile");

Or, use a different provider like OpenAI:

const model = createOpenAI({
...client.getProviderSettings("openai"),
})("gpt-4", {});
*/

export default function App() {
// Creates a new editor instance.
const editor = useCreateBlockNote({
dictionary: {
...en,
ai: aiLocales.en, // add default translations for the AI extension
},
// Register the AI extension
extensions: [
createAIExtension({
model,
}),
],
// We set some initial content for demo purposes
initialContent: [
{
type: "heading",
props: {
level: 1,
},
content: "I love cats",
},
{
type: "paragraph",
content:
"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.",
},
{
type: "paragraph",
content:
"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.",
},
{
type: "paragraph",
content:
"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.",
},
],
});

// Renders the editor instance using a React component.
return (
<div>
<BlockNoteView
editor={editor}
formattingToolbar={false}
slashMenu={false}
>
{/* This has AI specific components like the AI Command menu */}
{/* We pass `aiMenu=false` as we want to render an AIMenu with our own
items (defined below). */}
<BlockNoteAIUI aiMenu={false}></BlockNoteAIUI>
{/* Creates a new AIMenu with the default items, as well as our custom
ones. */}
<AIMenuController
aiMenu={() => (
<AIMenu
items={(editor, aiResponseStatus) => {
if (aiResponseStatus === "user-input") {
// Returns different items based on whether the AI Menu was
// opened via the Formatting Toolbar or the Slash Menu.
return editor.getSelection()
? [
// Gets the default AI Menu items for when it's opened via
// the Formatting Toolbar.
...getDefaultAIMenuItemsWithSelection(editor),
// Adds our custom item to make the text more casual.
// Only appears when the AI Menu is opened via the
// Formatting Toolbar.
makeCasual(editor),
]
: [
// Gets the default AI Menu items for when it's opened
// via the Slash Menu.
...getDefaultAIMenuItemsWithoutSelection(editor),
// Adds our custom item to find related topics. Only
// appears when the AI Menu is opened via the Slash
// Menu.
findRelatedTopics(editor),
];
}

if (aiResponseStatus === "user-reviewing") {
// Returns different items once the AI has finished writing,
// so the user can choose to accept or reject the changes.
return getDefaultAIActionMenuItems(editor);
}

// Return no items in other states, e.g. when the AI is writing
// or when an error occurred.
return [];
}}
/>
)}
/>

{/* We disabled the default formatting toolbar with `formattingToolbar=false`
and replace it for one with an "AI button" (defined below).
(See "Formatting Toolbar" in docs)
*/}
<FormattingToolbarWithAI />

{/* We disabled the default SlashMenu with `slashMenu=false`
and replace it for one with an AI option (defined below).
(See "Suggestion Menus" in docs)
*/}
<SuggestionMenuWithAI editor={editor} />
</BlockNoteView>
</div>
);
}

// Formatting toolbar with the `AIToolbarButton` added
function FormattingToolbarWithAI() {
return (
<FormattingToolbarController
formattingToolbar={() => (
<FormattingToolbar>
{...getFormattingToolbarItems()}
<AIToolbarButton />
</FormattingToolbar>
)}
/>
);
}

// Slash menu with the AI option added
function SuggestionMenuWithAI(props: {
editor: BlockNoteEditor<any, any, any>;
}) {
return (
<SuggestionMenuController
triggerCharacter="/"
getItems={async (query) =>
filterSuggestionItems(
[
...getDefaultReactSlashMenuItems(props.editor),
...getAISlashMenuItems(props.editor),
],
query,
)
}
/>
);
}
13 changes: 13 additions & 0 deletions examples/09-ai/03-ai-menu-items/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Adding AI Menu Items

In this example, we add two items to the AI Menu. The first prompts the AI to make the selected text more casual, and can be found by selecting some text and click the AI (magic wand) button. The second prompts the AI to give ideas on related topics to extend the document with, and can be found by clicking the "Ask AI" Slash Menu item.

Select some text and click the AI (magic wand) button, or type `/ai` anywhere in the editor to access AI functionality.

**Relevant Docs:**

- [Editor Setup](/docs/editor-basics/setup)
- [Changing the Formatting Toolbar](/docs/ui-components/formatting-toolbar#changing-the-formatting-toolbar)
- [Changing Slash Menu Items](/docs/ui-components/suggestion-menus#changing-slash-menu-items)
- [Getting Stared with BlockNote AI](/docs/ai/setup)
- [Custom AI Menu Items](/docs/ai/custom-commands)
61 changes: 61 additions & 0 deletions examples/09-ai/03-ai-menu-items/customAIMenuItems.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { BlockNoteEditor } from "@blocknote/core";
import { AIMenuSuggestionItem, getAIExtension } from "@blocknote/xl-ai";
import { RiApps2AddFill, RiEmotionHappyFill } from "react-icons/ri";

// Custom item to make the text more casual.
export const makeCasual = (editor: BlockNoteEditor): AIMenuSuggestionItem => ({
key: "make_casual",
title: "Make Casual",
// Aliases used when filtering AI Menu items from
// text in prompt input.
aliases: ["casual", "informal", "make informal"],
icon: <RiEmotionHappyFill size={18} />,
onItemClick: async () => {
await getAIExtension(editor).callLLM({
userPrompt: "Make casual",
// Set to true to tell the LLM to specifically
// use the selected content as context. Defaults
// to false.
useSelection: true,
// Sets what operations the LLM is allowed to do.
// In this case, we only want to allow updating
// the selected content, so only `update` is set
// to true. Defaults to `true` for all
// operations.
defaultStreamTools: {
add: false,
delete: false,
update: true,
},
});
},
size: "small",
});

// Custom item to find related topics.
export const findRelatedTopics = (
editor: BlockNoteEditor,
): AIMenuSuggestionItem => ({
key: "find_related_topics",
title: "Find Related Topics",
// Aliases used when filtering AI Menu items from
// text in prompt input.
aliases: ["related topics", "find topics"],
icon: <RiApps2AddFill size={18} />,
onItemClick: async () => {
await getAIExtension(editor).callLLM({
userPrompt:
"Find several related topics to the current text and write a sentence about each",
// Sets what operations the LLM is allowed to do.
// In this case, we only want to allow adding new
// content, so only `add` is set to true.
// Defaults to `true` for all operations.
defaultStreamTools: {
add: true,
delete: false,
update: false,
},
});
},
size: "small",
});
14 changes: 14 additions & 0 deletions examples/09-ai/03-ai-menu-items/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html lang="en">
<head>
<script>
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY -->
</script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Adding AI Menu Items</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/09-ai/03-ai-menu-items/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";

const root = createRoot(document.getElementById("root")!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
34 changes: 34 additions & 0 deletions examples/09-ai/03-ai-menu-items/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@blocknote/example-ai-ai-menu-items",
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
"private": true,
"version": "0.12.4",
"scripts": {
"start": "vite",
"dev": "vite",
"build:prod": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@blocknote/core": "latest",
"@blocknote/react": "latest",
"@blocknote/ariakit": "latest",
"@blocknote/mantine": "latest",
"@blocknote/shadcn": "latest",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"@blocknote/xl-ai": "latest",
"@mantine/core": "^7.10.1",
"ai": "^4.1.0",
"@ai-sdk/openai": "^1.1.0",
"@ai-sdk/groq": "^1.1.0",
"react-icons": "^5.2.1",
"zustand": "^5.0.3"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"@vitejs/plugin-react": "^4.3.1",
"vite": "^5.3.4"
}
}
Loading
Loading