Skip to content

Commit

Permalink
refactor: fix requirements type
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 2, 2023
1 parent 519dfde commit 3db583e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 25 deletions.
20 changes: 8 additions & 12 deletions editor/src/components/editor/action/command-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ declare module "@tiptap/core" {
}
}

let articleType = ARTICLE_TYPE_OPTIONS[0];

export const CommandFunctions = Extension.create({
name: "commandFunctions",
addStorage: () => ({
backgroundContext: "",
articleType: ARTICLE_TYPE_OPTIONS[0],
}),

// @ts-ignore
addCommands: () => {
return {
Expand All @@ -59,16 +58,13 @@ export const CommandFunctions = Extension.create({
},
getArticleType:
() =>
({ editor }: { editor: Editor }) => {
return editor.state.tr.getMeta("articleType") as ArticleTypeOption
({ tr }: { tr: Transaction }) => {
return articleType
},
setArticleType:
(articleType: ArticleTypeOption) =>
({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: Dispatch }) => {
console.info("set article type to: ", articleType);
tr.setMeta("articleType", articleType);
// @ts-ignore
dispatch(tr);
(type: ArticleTypeOption) =>
({ editor, tr, dispatch }: { editor: Editor, tr: Transaction, dispatch: any }) => {
articleType = type;
},

callLlm:
Expand Down
2 changes: 1 addition & 1 deletion editor/src/components/editor/data/ArticleTypeOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export interface ArticleTypeOption {

export const ARTICLE_TYPE_OPTIONS: ArticleTypeOption[] = [
{ value: 'article', label: '文章' },
{ value: 'user-story', label: '需求文档' },
{ value: 'requirements', label: '需求文档' },
];
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { CookieIcon } from "@radix-ui/react-icons";
import React from "react";
import { FacetType } from "@/types/custom-action.type";
import React, { useCallback, useEffect } from "react";
import { Editor } from "@tiptap/core";
import { Button, DropdownMenu } from "@radix-ui/themes";

export const MenuAiDropdown = ({ editor }: {
import { FacetType } from "@/types/custom-action.type";

export const ToolbarAiDropdown = ({ editor }: {
editor: Editor
}) => {
const [menus, setMenus] = React.useState<any[]>([]);

React.useEffect(() => {
useEffect(() => {
setMenus(editor?.commands?.getAiActions(FacetType.TOOLBAR_MENU));
}, []);
}, [editor]);

return (
<DropdownMenu.Root>
<DropdownMenu.Root onOpenChange={() => {
let aiActions = editor?.commands?.getAiActions(FacetType.TOOLBAR_MENU);
setMenus(aiActions)
}}>
<DropdownMenu.Trigger>
<Button className={'bg-pink-500 text-white'}>
AI
Expand Down
4 changes: 2 additions & 2 deletions editor/src/components/editor/menu-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React from 'react'
import * as ToggleGroup from '@radix-ui/react-toggle-group'
import { Editor } from "@tiptap/core";

import { MenuAiDropdown } from './intelli/menu/menu-ai-dropdown'
import { ToolbarAiDropdown } from './intelli/menu/toolbar-ai-dropdown'

export const MenuBar = ({ editor }: { editor: Editor }) => {
return (
Expand Down Expand Up @@ -126,7 +126,7 @@ export const MenuBar = ({ editor }: { editor: Editor }) => {
<DividerHorizontalIcon/>
</ToggleGroup.Item>
<div className="empty-separator" />
<MenuAiDropdown editor={editor} />
<ToolbarAiDropdown editor={editor} />
</ToggleGroup.Root>
)
}
21 changes: 17 additions & 4 deletions editor/src/components/editor/prompts/prompts-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import i18next from "i18next";
import { DefinedVariable, FacetType, PromptAction } from "@/types/custom-action.type";
import ArticlePrompts from "@/components/editor/prompts/article-prompts";
import { ArticleTypeOption } from "@/components/editor/data/ArticleTypeOption";
import RequirementsPrompts from "@/components/editor/prompts/requirements-prompts";

export class PromptsManager {
private backgroundContext: string = "";

private constructor() {
}

Expand All @@ -20,16 +22,27 @@ export class PromptsManager {
}

getPrompt(type: FacetType, articleType: ArticleTypeOption): PromptAction[] {
let actions = ArticlePrompts.filter(prompt => prompt.facetType === type);
const filterActions = actions.map(prompt => {
let typedPrompts: PromptAction[] = []
console.log(articleType?.value)

switch (articleType?.value) {
case "requirements":
typedPrompts = RequirementsPrompts;
break;
default:
typedPrompts = ArticlePrompts;
}

console.log(typedPrompts)

let actions = typedPrompts.filter(prompt => prompt.facetType === type);
return actions.map(prompt => {
if (prompt.i18Name) {
prompt.name = i18next.t(prompt.name)
}

return prompt
})

return filterActions
}

save(prompt: PromptAction[]) {
Expand Down
91 changes: 91 additions & 0 deletions editor/src/components/editor/prompts/requirements-prompts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
BuiltInFunc,
ChangeForm,
DefinedVariable,
FacetType,
OutputForm,
PromptAction, SourceType
} from "@/types/custom-action.type";


const ToolbarMenu: PromptAction[] = [
{
name: 'Generate Requirements',
i18Name: true,
template: `You are an software assistant helping a user to generate an requirements outline. Output in markdown format. ###{{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.TOOLBAR_MENU,
outputForm: OutputForm.STREAMING,
},
{
name: 'Continue writing',
i18Name: true,
template: `You are an assistant helping a user write requirements. Output how the document continues, no more than 3 sentences. ###{{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.TOOLBAR_MENU,
outputForm: OutputForm.STREAMING,
},
{
name: 'Help Me Write',
i18Name: true,
template: ` You are an assistant helping a user write more content in a document based on a prompt. Output in markdown format. ###{{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.TOOLBAR_MENU,
outputForm: OutputForm.STREAMING,
},
{
name: 'Spelling and Grammar',
i18Name: true,
template: `You are an assistant helping a user to check spelling and grammar. Output in markdown format. ###{{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.TOOLBAR_MENU,
outputForm: OutputForm.STREAMING,
},
];

const BubbleMenu: PromptAction[] = [
{
name: 'Polish',
i18Name: true,
template: `You are an assistant helping to polish sentence. Output in markdown format. \n ###{{${DefinedVariable.SELECTION}}}###`,
facetType: FacetType.BUBBLE_MENU,
outputForm: OutputForm.STREAMING,
},
{
name: 'Simplify Content',
i18Name: true,
template: `You are an assistant helping to simplify content. Output in markdown format. \n ###{{${DefinedVariable.SELECTION}}}###`,
facetType: FacetType.BUBBLE_MENU,
outputForm: OutputForm.STREAMING,
changeForm: ChangeForm.DIFF,
},
{
name: 'Similar Chunk',
i18Name: true,
template: `{{${DefinedVariable.SELECTION}}}`,
builtinFunction: BuiltInFunc.SIMILAR_CHUNKS,
facetType: FacetType.BUBBLE_MENU,
outputForm: OutputForm.STREAMING,
},
{
name: 'Translate',
i18Name: true,
template: `You are an assistant helping to translate a sentence. Output in markdown format. \n ###{{${DefinedVariable.SELECTION}}}###`,
facetType: FacetType.BUBBLE_MENU,
outputForm: OutputForm.STREAMING,
}
];

const SlashCommands: PromptAction[] = [
{
name: 'Summarize',
i18Name: true,
template: `You are an assistant helping to summarize a article. Output in markdown format. \n ###{{${DefinedVariable.BEFORE_CURSOR}}}###`,
facetType: FacetType.SLASH_COMMAND,
outputForm: OutputForm.STREAMING
}
]

const RequirementsPrompts: PromptAction[] = [
ToolbarMenu,
BubbleMenu,
SlashCommands
].flat();

export default RequirementsPrompts;
2 changes: 2 additions & 0 deletions editor/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const resources = {
'Similar Chunk': 'Similar Content Chunk',
'Simplify Content': 'Simplify Content',
'Translate': 'Translate',
'Generate Outline': 'Generate Outline',
}
},
zh: {
Expand All @@ -102,6 +103,7 @@ const resources = {
'Similar Chunk': '相似内容块',
'Simplify Content': '精简内容',
'Translate': '翻译',
'Generate Outline': '生成大纲',
}
}
}
Expand Down

0 comments on commit 3db583e

Please sign in to comment.