Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: tsfy #5

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 71 additions & 37 deletions src/components/editor/action/command-functions.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,78 @@
import { Commands, Extension } from '@tiptap/react'
import { Commands, Extension } from "@tiptap/react";
import { Editor } from "@tiptap/core";
import { Transaction } from "prosemirror-state";
import { ChangeForm, FacetType, PromptAction } from "@/types/custom-action.type";
import {
ChangeForm,
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";

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

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

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

runAiAction: {
runAiAction: (action: PromptAction) => 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

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

if (empty) return null

return editor.state.doc.textBetween(from, to, ' ')
},
callLlm: (action: PromptAction) => ({ tr, commands }: { tr: Transaction, commands: Commands }) => {
// TODO: @phodal convert action to request
// TODO: @genffy add LLM request type
// do execute action
},
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)
}
}
},
})
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;

if (empty) return null;

return editor.state.doc.textBetween(from, to, " ");
},
callLlm:
(action: PromptAction) =>
({ tr, commands }: { tr: Transaction; commands: Commands }) => {
// TODO: @phodal convert action to request
// TODO: @genffy add LLM request type
// do execute action
},
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);
},
};
},
});
Loading