diff --git a/editor/src/components/editor/action/AiActionExecutor.ts b/editor/src/components/editor/action/AiActionExecutor.ts index 138b122..32a0517 100644 --- a/editor/src/components/editor/action/AiActionExecutor.ts +++ b/editor/src/components/editor/action/AiActionExecutor.ts @@ -1,9 +1,10 @@ import { Editor } from "@tiptap/core"; import { ChangeForm, OutputForm, PromptAction } from "@/components/editor/defs/custom-action.type"; -import { PromptCompiler, actionPosition } from "@/components/editor/action/PromptCompiler"; +import { actionPosition, PromptCompiler } from "@/components/editor/action/PromptCompiler"; // @ts-ignore import { MarkdownParser } from "node_modules/tiptap-markdown/src/parse/MarkdownParser"; +import { BuiltinFunctionExecutor } from "@/components/editor/action/BuiltinFunctionExecutor"; export class AiActionExecutor { private readonly editor: Editor; @@ -93,7 +94,11 @@ export class AiActionExecutor { } public async execute(action: PromptAction) { - // builtinFunction + console.log("execute action", action) + if (action.builtinFunction) { + return new BuiltinFunctionExecutor(this.editor).execute(action); + } + const actionExecutor = new PromptCompiler(action, this.editor); actionExecutor.compile(); diff --git a/editor/src/components/editor/action/BuiltinFunctionExecutor.ts b/editor/src/components/editor/action/BuiltinFunctionExecutor.ts new file mode 100644 index 0000000..85aa626 --- /dev/null +++ b/editor/src/components/editor/action/BuiltinFunctionExecutor.ts @@ -0,0 +1,42 @@ +import { Editor } from "@tiptap/core"; +import { BuiltInFunc, PromptAction } from "@/components/editor/defs/custom-action.type"; + +export class BuiltinFunctionExecutor { + private editor: Editor; + + constructor(editor: Editor) { + this.editor = editor; + } + + execute(action: PromptAction): Promise | undefined { + switch (action.builtinFunction) { + case BuiltInFunc.GRAMMAR_CHECK: + break; + case BuiltInFunc.SIMILAR_CHUNKS: + return this.searchSimilarChunks(action); + case BuiltInFunc.SPELLING_CHECK: + break; + case BuiltInFunc.WIKI_SUMMARY: + break; + case BuiltInFunc.RELATED_CHUNKS: + break; + } + } + + private searchSimilarChunks(action: PromptAction) { + return new Promise((resolve, reject) => { + const selection = this.editor.state.selection; + const query = this.editor.state.doc.textBetween(selection.from, selection.to); + + const response = fetch(`http://127.0.0.1:8080/api/embedding-document/search?q=${query}`, { + method: "GET", + }); + + response.then((res) => { + res.json().then((data) => { + resolve(data); + }); + }); + }); + } +} \ No newline at end of file diff --git a/editor/src/components/editor/defs/custom-action.type.ts b/editor/src/components/editor/defs/custom-action.type.ts index 86afec3..d129080 100644 --- a/editor/src/components/editor/defs/custom-action.type.ts +++ b/editor/src/components/editor/defs/custom-action.type.ts @@ -51,11 +51,11 @@ export enum ChangeForm { } export enum BuiltInFunc { - SIMILAR_CHUNKS = 0, - RELATED_CHUNKS = 1, - GRAMMAR_CHECK = 2, - SPELLING_CHECK = 3, - WIKI_SUMMARY = 4, + SIMILAR_CHUNKS = "SIMILAR_CHUNKS", + RELATED_CHUNKS = "RELATED_CHUNKS", + GRAMMAR_CHECK = "GRAMMAR_CHECK", + SPELLING_CHECK = "SPELLING_CHECK", + WIKI_SUMMARY = "WIKI_SUMMARY", } export enum DefinedVariable { diff --git a/local_server/src/document_handler.rs b/local_server/src/document_handler.rs index f311d21..44929e0 100644 --- a/local_server/src/document_handler.rs +++ b/local_server/src/document_handler.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use crate::app_state::AppState; -#[post("/embedding-document")] +#[post("/api/embedding-document")] async fn create_embedding_document( form: web::Form, _data: web::Data, @@ -17,7 +17,7 @@ async fn create_embedding_document( .body(response) } -#[get("/embedding-document/search")] +#[get("/api/embedding-document/search")] async fn search_embedding_document( query: web::Query, data: web::Data, diff --git a/local_server/src/main.rs b/local_server/src/main.rs index 797d968..4dc5d54 100644 --- a/local_server/src/main.rs +++ b/local_server/src/main.rs @@ -31,6 +31,7 @@ async fn main() -> std::io::Result<()> { App::new() .wrap(Cors::default() .allowed_origin("https://editor.unitmesh.cc") + .allowed_origin("http://localhost:3000") .allowed_methods(vec!["GET", "POST"]) .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT]) .allowed_header(http::header::CONTENT_TYPE)