-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters