Skip to content

Commit

Permalink
fix: fix compile issue for sample
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 30, 2023
1 parent d3cd6ef commit 2b85fd5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 79 deletions.
152 changes: 75 additions & 77 deletions src/components/editor/action/command-functions.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,93 @@
import {Commands, Extension} from "@tiptap/react";
import {Editor} from "@tiptap/core";
import {Transaction} from "prosemirror-state";
import { Commands, Extension } from "@tiptap/react";
import { Editor } from "@tiptap/core";
import { Transaction } from "prosemirror-state";
import {
ChangeForm,
FacetType,
PromptAction,
FacetType,
PromptAction,
} from "@/types/custom-action.type";
import {PromptsManager} from "@/prompts/prompts-manager";
import {Range} from "@tiptap/core";
import {ActionExecutor} from "@/components/editor/action/ActionExecutor";
import { PromptsManager } from "@/prompts/prompts-manager";
import { ActionExecutor } from "@/components/editor/action/ActionExecutor";

declare module "@tiptap/core" {
interface Commands<ReturnType> {
variable: {
variable: () => ReturnType;
};
interface Commands<ReturnType> {
variable: {
variable: () => ReturnType;
};

getSelectedText: {
getSelectedText: () => string;
};
getSelectedText: {
getSelectedText: () => string;
};

callLlm: {
callLlm: (action: PromptAction) => void;
}
callLlm: {
callLlm: (action: PromptAction) => void;
}

getAiActions: {
getAiActions: (facet: FacetType) => PromptAction[];
};
getAiActions: {
getAiActions: (facet: FacetType) => PromptAction[];
};

runAiAction: {
runAiAction: (action: PromptAction) => ReturnType;
};
setBackgroundContext: () => ReturnType,
}
runAiAction: {
runAiAction: (action: PromptAction) => ReturnType;
};
setBackgroundContext: () => ReturnType,
}
}

export const CommandFunctions = Extension.create({
name: "commandFunctions",
// @ts-ignore
addCommands: () => {
return {
// for examples: $selection, $beforeCursor
variable: (variableName: string, variableValue: string) => () => {
console.log("variable", variableName, variableValue);
},
getSelectedText:
() =>
({editor}: { editor: Editor }) => {
if (!editor.state) return null;
name: "commandFunctions",
// @ts-ignore
addCommands: () => {
return {
// for examples: $selection, $beforeCursor
variable: (variableName: string, variableValue: string) => () => {
console.log("variable", variableName, variableValue);
},
getSelectedText:
() =>
({ editor }: { editor: Editor }) => {
if (!editor.state) return null;

const {from, to, empty} = editor.state.selection;
const { from, to, empty } = editor.state.selection;

if (empty) return null;
if (empty) return null;

return editor.state.doc.textBetween(from, to, " ");
},
callLlm:
(action: PromptAction) =>
async ({tr, commands, editor}: { tr: Transaction; commands: Commands, editor: Editor }) => {
// do execute action
const actionExecutor = new ActionExecutor(action, editor);
actionExecutor.compile();
if (action.compiledTemplate == null) {
throw Error("template is not been compiled yet! compile it first");
}
return editor.state.doc.textBetween(from, to, " ");
},
callLlm:
(action: PromptAction) =>
async ({ tr, commands, editor }: { tr: Transaction; commands: Commands, editor: Editor }) => {
// do execute action
const actionExecutor = new ActionExecutor(action, editor);
actionExecutor.compile();
if (action.compiledTemplate == null) {
throw Error("template is not been compiled yet! compile it first");
}
console.info("compiledTemplate: \n\n", action.compiledTemplate);

const msg = await fetch("/api/completion", {
method: "POST",
body: JSON.stringify({prompt: action.compiledTemplate}),
}).then(it => it.text());
const msg = await fetch("/api/completion/openai", {
method: "POST",
body: JSON.stringify({ prompt: action.compiledTemplate }),
}).then(it => it.text());

const posInfo = actionExecutor.position(editor.state.selection);
editor.chain().focus().insertContentAt(posInfo, msg).run();
const posInfo = actionExecutor.position(editor.state.selection);
editor.chain().focus().insertContentAt(posInfo, msg).run();

},
getAiActions:
(facet: FacetType) =>
({editor}: { editor: Editor }) => {
return PromptsManager.getInstance().get(facet);
},
runAiAction:
(action: PromptAction) =>
({editor}: { editor: Editor }) => {
// call LLM
console.log("executeAction", action);
},
setBackgroundContext:
(context: string) =>
({editor}: { editor: Editor }) => {
PromptsManager.getInstance().saveBackgroundContext(context);
},
};
},
},
getAiActions:
(facet: FacetType) =>
({ editor }: { editor: Editor }) => {
return PromptsManager.getInstance().get(facet);
},
runAiAction:
(action: PromptAction) =>
({ editor }: { editor: Editor }) => {
editor.commands.callLlm(action);
},
setBackgroundContext:
(context: string) =>
({ editor }: { editor: Editor }) => {
PromptsManager.getInstance().saveBackgroundContext(context);
},
};
},
});
4 changes: 2 additions & 2 deletions src/prompts/prebuild-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ const BubbleMenu: PromptAction[] = [

const SlashCommands: PromptAction[] = [
{
sourceType: SourceType.SELECTION,
sourceType: SourceType.BEFORE_CURSOR,
name: 'Summarize',
i18Name: true,
template: `You are an assistant helping to summarize a document. Output in markdown format. \n ###{{${DefinedVariable.SELECTION}}}###`,
template: `You are an assistant helping to summarize a document. Output in markdown format. \n ###{{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.SLASH_COMMAND,
outputForm: OutputForm.STREAMING
}
Expand Down
6 changes: 6 additions & 0 deletions src/types/llm-model.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export enum OPENAI_MODEL {
CHATGPT_4 = 'chatgpt-4',
}

// aka Yiyan
export enum ERNIEBOT {
ERNIEBOT = 'ernie-bot',
}

export const LlmModelType = {
OPENAI: OPENAI_MODEL,
ERNIEBOT: ERNIEBOT,
}

0 comments on commit 2b85fd5

Please sign in to comment.