-
Notifications
You must be signed in to change notification settings - Fork 8
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
5 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/frontend/apps/impress/src/features/docs/doc-editor/api/useAI.tsx
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
|
||
import { APIError, errorCauses, fetchAPI } from '@/api'; | ||
|
||
export type AIActions = | ||
| 'prompt' | ||
| 'rephrase' | ||
| 'summarize' | ||
| 'translate' | ||
| 'correct' | ||
| 'translate_fr' | ||
| 'translate_en' | ||
| 'translate_de'; | ||
|
||
export type AIParams = { | ||
text: string; | ||
action: AIActions; | ||
}; | ||
|
||
export type AIResponse = { | ||
answer: string; | ||
}; | ||
|
||
export const AI = async ({ ...params }: AIParams): Promise<AIResponse> => { | ||
const response = await fetchAPI(`ai/`, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
...params, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new APIError('Failed to request ai', await errorCauses(response)); | ||
} | ||
|
||
return response.json() as Promise<AIResponse>; | ||
}; | ||
|
||
export function useAI() { | ||
return useMutation<AIResponse, APIError, AIParams>({ | ||
mutationFn: AI, | ||
}); | ||
} |
159 changes: 159 additions & 0 deletions
159
src/frontend/apps/impress/src/features/docs/doc-editor/components/AIButton.tsx
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 |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { | ||
useBlockNoteEditor, | ||
useComponentsContext, | ||
useSelectedBlocks, | ||
} from '@blocknote/react'; | ||
import { ReactNode, useCallback, useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Box, Text } from '@/components'; | ||
|
||
import { AIActions, useAI } from '../api/useAI'; | ||
|
||
export function AIGroupButton() { | ||
const editor = useBlockNoteEditor(); | ||
const Components = useComponentsContext(); | ||
const selectedBlocks = useSelectedBlocks(editor); | ||
const { t } = useTranslation(); | ||
|
||
const show = useMemo(() => { | ||
return !!selectedBlocks.find((block) => block.content !== undefined); | ||
}, [selectedBlocks]); | ||
|
||
if (!show || !editor.isEditable || !Components) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Components.Generic.Menu.Root> | ||
<Components.Generic.Menu.Trigger> | ||
<Components.FormattingToolbar.Button | ||
className="bn-button bn-menu-item" | ||
data-test="ai-actions" | ||
label="AI" | ||
mainTooltip={t('AI Actions')} | ||
> | ||
AI | ||
</Components.FormattingToolbar.Button> | ||
</Components.Generic.Menu.Trigger> | ||
<Components.Generic.Menu.Dropdown | ||
className="bn-menu-dropdown bn-drag-handle-menu" | ||
sub={true} | ||
> | ||
<AIMenuItem | ||
action="prompt" | ||
icon={ | ||
<Text $isMaterialIcon $size="s"> | ||
text_fields | ||
</Text> | ||
} | ||
> | ||
{t('Use as prompt')} | ||
</AIMenuItem> | ||
<AIMenuItem | ||
action="rephrase" | ||
icon={ | ||
<Text $isMaterialIcon $size="s"> | ||
refresh | ||
</Text> | ||
} | ||
> | ||
{t('Rephrase')} | ||
</AIMenuItem> | ||
<AIMenuItem | ||
action="summarize" | ||
icon={ | ||
<Text $isMaterialIcon $size="s"> | ||
summarize | ||
</Text> | ||
} | ||
> | ||
{t('Summarize')} | ||
</AIMenuItem> | ||
<AIMenuItem | ||
action="correct" | ||
icon={ | ||
<Text $isMaterialIcon $size="s"> | ||
check | ||
</Text> | ||
} | ||
> | ||
{t('Correct')} | ||
</AIMenuItem> | ||
<Components.Generic.Menu.Root position="right" sub={true}> | ||
<Components.Generic.Menu.Trigger sub={false}> | ||
<Components.Generic.Menu.Item | ||
className="bn-menu-item" | ||
subTrigger={true} | ||
> | ||
<Box $direction="row" $gap="0.6rem"> | ||
<Text $isMaterialIcon $size="s"> | ||
translate | ||
</Text> | ||
Language | ||
</Box> | ||
</Components.Generic.Menu.Item> | ||
</Components.Generic.Menu.Trigger> | ||
<Components.Generic.Menu.Dropdown | ||
sub={false} | ||
className="bn-menu-dropdown bn-drag-handle-menu" | ||
> | ||
<AIMenuItem action="translate_en">{t('English')}</AIMenuItem> | ||
<AIMenuItem action="translate_fr">{t('French')}</AIMenuItem> | ||
<AIMenuItem action="translate_de">{t('German')}</AIMenuItem> | ||
</Components.Generic.Menu.Dropdown> | ||
</Components.Generic.Menu.Root> | ||
</Components.Generic.Menu.Dropdown> | ||
</Components.Generic.Menu.Root> | ||
); | ||
} | ||
|
||
interface AIMenuItemProps { | ||
action: AIActions; | ||
children: ReactNode; | ||
icon?: ReactNode; | ||
} | ||
|
||
const AIMenuItem = ({ action, children, icon }: AIMenuItemProps) => { | ||
const editor = useBlockNoteEditor(); | ||
const Components = useComponentsContext(); | ||
const { mutateAsync: requestAI, isPending } = useAI(); | ||
|
||
const handleAIAction = useCallback(async () => { | ||
const selectedBlocks = editor.getSelection()?.blocks; | ||
|
||
if (!selectedBlocks || selectedBlocks.length === 0) { | ||
return; | ||
} | ||
|
||
const markdown = await editor.blocksToMarkdownLossy(selectedBlocks); | ||
const responseAI = await requestAI({ | ||
text: markdown, | ||
action, | ||
}); | ||
|
||
if (!responseAI.answer) { | ||
return; | ||
} | ||
|
||
const blockMarkdown = await editor.tryParseMarkdownToBlocks( | ||
responseAI.answer, | ||
); | ||
editor.replaceBlocks(selectedBlocks, blockMarkdown); | ||
}, [editor, requestAI, action]); | ||
|
||
if (!Components) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Components.Generic.Menu.Item | ||
closeMenuOnClick={false} | ||
icon={icon} | ||
onClick={() => void handleAIAction()} | ||
rightSection={isPending ? <Box className="loader" /> : undefined} | ||
> | ||
{children} | ||
</Components.Generic.Menu.Item> | ||
); | ||
}; |
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
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