|
2 | 2 |
|
3 | 3 | import React from 'react'; |
4 | 4 |
|
5 | | -import { AIChatPlugin, AIPlugin } from '@udecode/plate-ai/react'; |
| 5 | +import type { AIChatPluginConfig } from '@udecode/plate-ai/react'; |
| 6 | + |
| 7 | +import { PathApi } from '@udecode/plate'; |
| 8 | +import { streamInsertChunk, withAIBatch } from '@udecode/plate-ai'; |
| 9 | +import { AIChatPlugin, AIPlugin, useChatChunk } from '@udecode/plate-ai/react'; |
| 10 | +import { useEditorPlugin, usePluginOption } from '@udecode/plate/react'; |
6 | 11 |
|
7 | 12 | import { markdownPlugin } from '@/registry/default/components/editor/plugins/markdown-plugin'; |
8 | 13 | import { AILoadingBar } from '@/registry/default/plate-ui/ai-loading-bar'; |
9 | 14 | import { AIMenu } from '@/registry/default/plate-ui/ai-menu'; |
10 | 15 |
|
11 | 16 | import { cursorOverlayPlugin } from './cursor-overlay-plugin'; |
| 17 | + |
12 | 18 | const systemCommon = `\ |
13 | 19 | You are an advanced AI-powered note-taking assistant, designed to enhance productivity and creativity in note management. |
14 | 20 | Respond directly to user prompts with clear, concise, and relevant content. Maintain a neutral, helpful tone. |
@@ -113,5 +119,55 @@ export const aiPlugins = [ |
113 | 119 | afterContainer: () => <AILoadingBar />, |
114 | 120 | afterEditable: () => <AIMenu />, |
115 | 121 | }, |
| 122 | + }).extend({ |
| 123 | + useHooks: () => { |
| 124 | + const { editor, getOption } = useEditorPlugin(AIChatPlugin); |
| 125 | + |
| 126 | + const mode = usePluginOption( |
| 127 | + { key: 'aiChat' } as AIChatPluginConfig, |
| 128 | + 'mode' |
| 129 | + ); |
| 130 | + |
| 131 | + useChatChunk({ |
| 132 | + onChunk: ({ chunk, isFirst, nodes, text }) => { |
| 133 | + if (isFirst && mode == 'insert') { |
| 134 | + editor.tf.withoutSaving(() => { |
| 135 | + editor.tf.insertNodes( |
| 136 | + { |
| 137 | + children: [{ text: '' }], |
| 138 | + type: AIChatPlugin.key, |
| 139 | + }, |
| 140 | + { |
| 141 | + at: PathApi.next(editor.selection!.focus.path.slice(0, 1)), |
| 142 | + } |
| 143 | + ); |
| 144 | + }); |
| 145 | + editor.setOption(AIChatPlugin, 'streaming', true); |
| 146 | + } |
| 147 | + |
| 148 | + if (mode === 'insert' && nodes.length > 0) { |
| 149 | + withAIBatch( |
| 150 | + editor, |
| 151 | + () => { |
| 152 | + if (!getOption('streaming')) return; |
| 153 | + editor.tf.withScrolling(() => { |
| 154 | + streamInsertChunk(editor, chunk, { |
| 155 | + textProps: { |
| 156 | + ai: true, |
| 157 | + }, |
| 158 | + }); |
| 159 | + }); |
| 160 | + }, |
| 161 | + { split: isFirst } |
| 162 | + ); |
| 163 | + } |
| 164 | + }, |
| 165 | + onFinish: ({ content }) => { |
| 166 | + editor.setOption(AIChatPlugin, 'streaming', false); |
| 167 | + editor.setOption(AIChatPlugin, '_blockChunks', ''); |
| 168 | + editor.setOption(AIChatPlugin, '_blockPath', null); |
| 169 | + }, |
| 170 | + }); |
| 171 | + }, |
116 | 172 | }), |
117 | 173 | ] as const; |
0 commit comments