Skip to content

Commit

Permalink
refactor: extract for ai executor
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 23, 2024
1 parent 976e8e7 commit 5bcc030
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
20 changes: 13 additions & 7 deletions web/core/lib/editor/action/AiActionExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { Editor } from '@tiptap/core';
import { ChangeForm, OutputForm, PromptAction } from '@/editor/defs/custom-action.type';
import { OutputForm, PromptAction } from '@/editor/defs/custom-action.type';
import { actionPosition, PromptCompiler } from '@/editor/action/PromptCompiler';

// @ts-ignore
import { MarkdownParser } from '@/../node_modules/tiptap-markdown/src/parse/MarkdownParser';
import { BuiltinFunctionExecutor } from '@/editor/action/BuiltinFunctionExecutor';

export class AiActionExecutor {
private readonly editor: Editor;
private editor: Editor;
private endpointUrl: string = '/api/completion/mock';

constructor() {
}

constructor(editor: Editor) {
setEditor(editor: Editor) {
this.editor = editor;
}

setEndpointUrl(url: string) {
this.endpointUrl = url;
}


/**
* TODO: will according the {@link PromptAction.useModel} to return the endpoint in future
* @param action
*/
endpoint(action: PromptAction) {
const endpoint = '/api/completion/mock';
return endpoint;
return this.endpointUrl;
}

private async handleStreaming(action: PromptAction, prompt: string) {
Expand Down
7 changes: 5 additions & 2 deletions web/core/lib/editor/action/custom-editor-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ declare module '@tiptap/core' {

let articleType = ARTICLE_TYPE_OPTIONS[0];

export const CustomEditorCommands = (promptsManager: PromptsManager = PromptsManager.getInstance()) => {
export const CustomEditorCommands = (
actionHandler: AiActionExecutor,
promptsManager: PromptsManager = PromptsManager.getInstance()
) => {
return Extension.create({
name: 'commandFunctions',

Expand All @@ -56,7 +59,7 @@ export const CustomEditorCommands = (promptsManager: PromptsManager = PromptsMan
callLlm:
(action: PromptAction) =>
async ({ tr, commands, editor }: { tr: Transaction; commands: Commands, editor: Editor }) => {
const actionHandler = new AiActionExecutor(editor);
actionHandler.setEditor(editor);
return await actionHandler.execute(action);
},
getAiActions:
Expand Down
5 changes: 3 additions & 2 deletions web/core/lib/editor/live-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ 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';
import { AiActionExecutor } from '@/editor/action/AiActionExecutor.ts';

const md = new MarkdownIt();

export const setupExtensions = (promptsManager: PromptsManager = PromptsManager.getInstance()) => {
export const setupExtensions = (promptsManager: PromptsManager) => {
return [
// we define all commands here
CustomEditorCommands(promptsManager),
CustomEditorCommands(new AiActionExecutor(), promptsManager,),
AdviceExtension.configure({
HTMLAttributes: {
class: 'my-advice'
Expand Down
2 changes: 1 addition & 1 deletion web/core/lib/editor/menu/menu-bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const MenuBubble = ({ editor }: {
setIsOpen(false);
setLoading(true);

const text = await editor.commands?.callLlm(menu);
const text = editor.commands?.callLlm(menu);
setLoading(false);

const newComment = newAdvice(text || '');
Expand Down
1 change: 1 addition & 0 deletions web/core/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export { Sidebar } from '@/editor/components/sidebar.tsx';
export { AdviceManager } from '@/editor/extensions/advice/advice-manager';
export { AdviceView } from '@/editor/extensions/advice/advice-view';
export { Settings } from '@/editor/components/settings';
export { AiActionExecutor } from '@/editor/action/AiActionExecutor.ts';
2 changes: 1 addition & 1 deletion web/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studio-b3/web-core",
"version": "0.0.4",
"version": "0.0.5",
"type": "module",
"main": "dist/main.js",
"types": "dist-types/main.d.ts",
Expand Down

0 comments on commit 5bcc030

Please sign in to comment.