diff --git a/web/core/lib/editor/action/custom-editor-commands.ts b/web/core/lib/editor/action/custom-editor-commands.ts index 4e424dd..0302fa3 100644 --- a/web/core/lib/editor/action/custom-editor-commands.ts +++ b/web/core/lib/editor/action/custom-editor-commands.ts @@ -1,99 +1,101 @@ -import { Commands, Extension } from "@tiptap/react"; -import { Editor } from "@tiptap/core"; -import { Transaction } from "prosemirror-state"; -import { DefinedVariable, FacetType, OutputForm, PromptAction, } from "@/editor/defs/custom-action.type"; -import { PromptsManager } from "@/editor/prompts/prompts-manager"; -import { ARTICLE_TYPE_OPTIONS, TypeOptions } from "@/editor/defs/type-options.type"; -import { AiActionExecutor } from "@/editor/action/AiActionExecutor"; +import { Commands, Extension } from '@tiptap/react'; +import { Editor } from '@tiptap/core'; +import { Transaction } from 'prosemirror-state'; +import { DefinedVariable, FacetType, OutputForm, PromptAction } from '@/editor/defs/custom-action.type'; +import { PromptsManager } from '@/editor/prompts/prompts-manager'; +import { ARTICLE_TYPE_OPTIONS, TypeOptions } from '@/editor/defs/type-options.type'; +import { AiActionExecutor } from '@/editor/action/AiActionExecutor'; -declare module "@tiptap/core" { - interface Commands { - callLlm: { - callLlm: (action: PromptAction) => string | undefined; - }; - getAiActions: { - getAiActions: (facet: FacetType) => PromptAction[]; - }; - callQuickAction: { - callQuickAction: (text: string) => ReturnType; - } - runAiAction: { - runAiAction: (action: PromptAction) => ReturnType; - }; - replaceRange: { - replaceRange: (text: string) => ReturnType; - } - setBackgroundContext: () => ReturnType, - getArticleType: { - getArticleType: () => TypeOptions, - } - setArticleType: { - setArticleType: (articleType: TypeOptions) => ReturnType - }, - } +declare module '@tiptap/core' { + interface Commands { + callLlm: { + callLlm: (action: PromptAction) => string | undefined; + }; + getAiActions: { + getAiActions: (facet: FacetType) => PromptAction[]; + }; + callQuickAction: { + callQuickAction: (text: string) => ReturnType; + } + runAiAction: { + runAiAction: (action: PromptAction) => ReturnType; + }; + replaceRange: { + replaceRange: (text: string) => ReturnType; + } + setBackgroundContext: () => ReturnType, + getArticleType: { + getArticleType: () => TypeOptions, + } + setArticleType: { + setArticleType: (articleType: TypeOptions) => ReturnType + }, + } } let articleType = ARTICLE_TYPE_OPTIONS[0]; -export const CustomEditorCommands = Extension.create({ - name: "commandFunctions", +export const CustomEditorCommands = (promptsManager: PromptsManager = PromptsManager.getInstance()) => { + return Extension.create({ + name: 'commandFunctions', - // @ts-ignore - addCommands: () => { - return { - getArticleType: - () => - ({ tr }: { tr: Transaction }) => { - return articleType - }, - setArticleType: - (type: TypeOptions) => - ({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => { - articleType = type; - }, + // @ts-ignore + addCommands: () => { + return { + getArticleType: + () => + ({ tr }: { tr: Transaction }) => { + return articleType; + }, + setArticleType: + (type: TypeOptions) => + ({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => { + articleType = type; + }, - callLlm: - (action: PromptAction) => - async ({ tr, commands, editor }: { tr: Transaction; commands: Commands, editor: Editor }) => { - let actionHandler = new AiActionExecutor(editor); - return await actionHandler.execute(action) - }, - getAiActions: - (facet: FacetType) => - ({ editor }: { editor: Editor }) => { - let articleType = editor.commands.getArticleType(); - return PromptsManager.getInstance().getActions(facet, articleType); - }, - runAiAction: - (action: PromptAction) => - ({ editor }: { editor: Editor }) => { - editor.commands.callLlm(action); - }, - callQuickAction: - (text: string) => - ({ editor }: { editor: Editor }) => { - editor.setEditable(false); - editor.commands.callLlm({ - name: text, - template: `You are an assistant to help user write article. Here is user command:` + text + `\n Here is some content ###Article title: {{${DefinedVariable.TITLE}}}, Before Content: {{${DefinedVariable.BEFORE_CURSOR}}}###`, - facetType: FacetType.QUICK_INSERT, - outputForm: OutputForm.STREAMING, - }); + callLlm: + (action: PromptAction) => + async ({ tr, commands, editor }: { tr: Transaction; commands: Commands, editor: Editor }) => { + const actionHandler = new AiActionExecutor(editor); + return await actionHandler.execute(action); + }, + getAiActions: + (facet: FacetType) => + ({ editor }: { editor: Editor }) => { + const articleType = editor.commands.getArticleType(); + return promptsManager.getActions(facet, articleType); + }, + runAiAction: + (action: PromptAction) => + ({ editor }: { editor: Editor }) => { + editor.commands.callLlm(action); + }, + callQuickAction: + (text: string) => + ({ editor }: { editor: Editor }) => { + editor.setEditable(false); + editor.commands.callLlm({ + name: text, + template: `You are an assistant to help user write article. Here is user command:` + text + `\n Here is some content ###Article title: {{${DefinedVariable.TITLE}}}, Before Content: {{${DefinedVariable.BEFORE_CURSOR}}}###`, + facetType: FacetType.QUICK_INSERT, + outputForm: OutputForm.STREAMING + }); - editor.setEditable(true); - }, - replaceRange: - (text: string) => - ({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => { - const { from, to } = editor.state.selection; - tr.replaceRangeWith(from, to, editor.state.schema.text(text)); - dispatch(tr); - }, - setBackgroundContext: - (context: string) => - ({ editor }: { editor: Editor }) => { - PromptsManager.getInstance().saveBackgroundContext(context); - }, - }; - }, -}); + editor.setEditable(true); + }, + replaceRange: + (text: string) => + ({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => { + const { from, to } = editor.state.selection; + tr.replaceRangeWith(from, to, editor.state.schema.text(text)); + dispatch(tr); + }, + setBackgroundContext: + (context: string) => + ({ editor }: { editor: Editor }) => { + promptsManager.saveBackgroundContext(context); + } + }; + } + }); +}; diff --git a/web/core/lib/editor/extensions/slash-command/slash-extension.ts b/web/core/lib/editor/extensions/slash-command/slash-extension.ts index 6928f6a..682d62f 100644 --- a/web/core/lib/editor/extensions/slash-command/slash-extension.ts +++ b/web/core/lib/editor/extensions/slash-command/slash-extension.ts @@ -7,7 +7,7 @@ import { PluginKey } from '@tiptap/pm/state'; import { FacetType } from '@/editor/defs/custom-action.type'; import { PromptsManager } from '@/editor/prompts/prompts-manager'; -export const createSlashExtension = (extensionName: string) => { +export const createSlashExtension = (extensionName: string, promptsManager: PromptsManager = PromptsManager.getInstance()) => { return Node.create({ name: "slash-command", addOptions() { @@ -42,7 +42,7 @@ export const createSlashExtension = (extensionName: string) => { }, items: () => { const articleType = this.editor.commands.getArticleType(); - return (PromptsManager.getInstance().getActions(FacetType.SLASH_COMMAND, articleType) || []); + return (promptsManager.getActions(FacetType.SLASH_COMMAND, articleType) || []); }, render: () => { let component: ReactRenderer; diff --git a/web/core/lib/editor/live-editor.tsx b/web/core/lib/editor/live-editor.tsx index 19fed04..f01b740 100644 --- a/web/core/lib/editor/live-editor.tsx +++ b/web/core/lib/editor/live-editor.tsx @@ -30,52 +30,55 @@ import { Advice } from "@/editor/extensions/advice/advice"; import { AdviceManager } from "@/editor/extensions/advice/advice-manager"; import { AdviceView } from "@/editor/extensions/advice/advice-view"; import { Settings } from "@/editor/components/settings"; +import { PromptsManager } from '@/editor/prompts/prompts-manager.ts'; const md = new MarkdownIt() -export const extensions = [ - // we define all commands here - CustomEditorCommands, - AdviceExtension.configure({ - HTMLAttributes: { - class: "my-advice", - }, - setAdviceCommand: (advice: Advice) => { - AdviceManager.getInstance().addAdvice(advice); - }, - onAdviceActivated: (adviceId) => { - if (adviceId) AdviceManager.getInstance().setActiveId(adviceId); - }, - }), - InlineCompletion, - StarterKit.configure({ - bulletList: { - keepMarks: true, - keepAttributes: false, - }, - orderedList: { - keepMarks: true, - keepAttributes: false, - }, - }), - createSlashExtension('ai-slash'), - createQuickBox(), - CharacterCount.configure({}), - Color.configure({ types: [TextStyle.name, ListItem.name] }), - // @ts-ignore - TextStyle.configure({ types: [ListItem.name] }), - Table, - TableRow, - TableCell, - TableHeader -] +export const setupExtensions = (promptsManager: PromptsManager = PromptsManager.getInstance()) => { + return [ + // we define all commands here + CustomEditorCommands(promptsManager), + AdviceExtension.configure({ + HTMLAttributes: { + class: "my-advice", + }, + setAdviceCommand: (advice: Advice) => { + AdviceManager.getInstance().addAdvice(advice); + }, + onAdviceActivated: (adviceId) => { + if (adviceId) AdviceManager.getInstance().setActiveId(adviceId); + }, + }), + InlineCompletion, + StarterKit.configure({ + bulletList: { + keepMarks: true, + keepAttributes: false, + }, + orderedList: { + keepMarks: true, + keepAttributes: false, + }, + }), + createSlashExtension('ai-slash', promptsManager), + createQuickBox(), + CharacterCount.configure({}), + Color.configure({ types: [TextStyle.name, ListItem.name] }), + // @ts-ignore + TextStyle.configure({ types: [ListItem.name] }), + Table, + TableRow, + TableCell, + TableHeader + ] +} const LiveEditor = () => { const { t } = useTranslation(); const editor = useEditor({ - extensions, + extensions: setupExtensions(PromptsManager.getInstance()), content: md.render(t('Editor Placeholder')), editorProps: { attributes: { diff --git a/web/core/lib/main.ts b/web/core/lib/main.ts index af449a7..17505b5 100644 --- a/web/core/lib/main.ts +++ b/web/core/lib/main.ts @@ -1,8 +1,6 @@ -export * from "@/editor/live-editor"; +export * from '@/editor/live-editor'; -export { - extensions, - default as LiveEditor -} from "@/editor/live-editor"; - -export { ToolbarMenu } from "@/editor/menu/toolbar-menu"; +export { default as LiveEditor } from '@/editor/live-editor'; +export { setupExtensions } from '@/editor/live-editor'; +export { ToolbarMenu } from '@/editor/menu/toolbar-menu'; +export { PromptsManager } from '@/editor/prompts/prompts-manager.ts';