- "content": "'use client';\n\nimport { withAIBatch } from '@platejs/ai';\nimport {\n AIChatPlugin,\n AIPlugin,\n applyAISuggestions,\n streamInsertChunk,\n useChatChunk,\n} from '@platejs/ai/react';\nimport { getPluginType, KEYS, PathApi } from 'platejs';\nimport { usePluginOption } from 'platejs/react';\n\nimport { AILoadingBar, AIMenu } from '@/registry/ui/ai-menu';\nimport { AIAnchorElement, AILeaf } from '@/registry/ui/ai-node';\n\nimport { useChat } from '../use-chat';\nimport { CursorOverlayKit } from './cursor-overlay-kit';\nimport { MarkdownKit } from './markdown-kit';\n\nexport const aiChatPlugin = AIChatPlugin.extend({\n options: {\n chatOptions: {\n api: '/api/ai/command',\n body: {},\n },\n },\n render: {\n afterContainer: AILoadingBar,\n afterEditable: AIMenu,\n node: AIAnchorElement,\n },\n shortcuts: { show: { keys: 'mod+j' } },\n useHooks: ({ editor, getOption }) => {\n useChat();\n\n const mode = usePluginOption(AIChatPlugin, 'mode');\n const toolName = usePluginOption(AIChatPlugin, 'toolName');\n\n useChatChunk({\n onChunk: ({ chunk, isFirst, nodes, text: content }) => {\n if (isFirst && mode == 'insert') {\n editor.tf.withoutSaving(() => {\n editor.tf.insertNodes(\n {\n children: [{ text: '' }],\n type: getPluginType(editor, KEYS.aiChat),\n },\n {\n at: PathApi.next(editor.selection!.focus.path.slice(0, 1)),\n }\n );\n });\n editor.setOption(AIChatPlugin, 'streaming', true);\n }\n\n if (mode === 'insert' && nodes.length > 0) {\n withAIBatch(\n editor,\n () => {\n if (!getOption('streaming')) return;\n editor.tf.withScrolling(() => {\n streamInsertChunk(editor, chunk, {\n textProps: {\n [getPluginType(editor, KEYS.ai)]: true,\n },\n });\n });\n },\n { split: isFirst }\n );\n }\n\n if (toolName === 'edit' && mode === 'chat') {\n withAIBatch(\n editor,\n () => {\n applyAISuggestions(editor, content);\n },\n {\n split: isFirst,\n }\n );\n }\n },\n onFinish: () => {\n editor.setOption(AIChatPlugin, 'streaming', false);\n editor.setOption(AIChatPlugin, '_blockChunks', '');\n editor.setOption(AIChatPlugin, '_blockPath', null);\n editor.setOption(AIChatPlugin, '_mdxName', null);\n },\n });\n },\n});\n\nexport const AIKit = [\n ...CursorOverlayKit,\n ...MarkdownKit,\n AIPlugin.withComponent(AILeaf),\n aiChatPlugin,\n];\n",
0 commit comments