Skip to content

TagSpaces is an offline, open source, document manager with tagging support

feat: added custom prompts management with crud operations

Ilian Sapundshiev3419c406783e
diff --git a/src/renderer/components/chat/ChatView.tsx b/src/renderer/components/chat/ChatView.tsx
index 85a524df3..6c9e3e6ff 100644
--- a/src/renderer/components/chat/ChatView.tsx
+++ b/src/renderer/components/chat/ChatView.tsx
@@ -22,6 +22,7 @@ import {
ArrowDropUpIcon,
CancelIcon,
CloseIcon,
+ EditIcon,
MoreMenuIcon,
SendIcon,
} from '-/components/CommonIcons';
@@ -31,7 +32,9 @@ import TsTextField from '-/components/TsTextField';
import ChatDndTargetFile from '-/components/chat/ChatDndTargetFile';
import ChatMenu from '-/components/chat/ChatMenu';
import { AIProvider, ChatMode } from '-/components/chat/ChatTypes';
+import PromptEditDialog from '-/components/chat/PromptEditDialog';
import SelectChatModel from '-/components/chat/SelectChatModel';
+import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
import ChatMdEditor from '-/components/md/ChatMdEditor';
import { CrepeRef } from '-/components/md/useCrepeHandler';
import { useChatContext } from '-/hooks/useChatContext';
@@ -44,7 +47,7 @@ import {
saveAsTextFile,
} from '-/services/utils-io';
import { MilkdownProvider } from '@milkdown/react';
-import { Box, Divider, Grid } from '@mui/material';
+import { Box, Divider, Grid, Stack } from '@mui/material';
import CircularProgress from '@mui/material/CircularProgress';
import FormControl from '@mui/material/FormControl';
import FormHelperText from '@mui/material/FormHelperText';
@@ -54,23 +57,32 @@ import MenuItem from '@mui/material/MenuItem';
import { useTheme } from '@mui/material/styles';
import { formatDateTime4Tag } from '@tagspaces/tagspaces-common/misc';
import { extractFileExtension } from '@tagspaces/tagspaces-common/paths';
-import React, { ChangeEvent, useCallback, useReducer, useRef } from 'react';
+import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
+import React, {
+ ChangeEvent,
+ useCallback,
+ useEffect,
+ useReducer,
+ useRef,
+} from 'react';
import { NativeTypes } from 'react-dnd-html5-backend';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import TsMenuList from '../TsMenuList';
-const aiPrompts = [
- {
- title: 'Translate into German',
- prompt:
- 'You are a professional {sourceLang} to {targeLang} translator. Your goal is to accurately convey the meaning and nuances of the original {sourceLang} text while adhering to {targeLang} grammar, vocabulary, and cultural sensitivities. Produce only the {targeLang} translation, without any additional explanations or commentary. Please translate the following {sourceLang} text into {targeLang}:',
- },
- {
- title: 'Correct German spelling ',
- prompt:
- 'You are a professional text corrector. Improve the text grammatically in German.',
- },
+interface AiPrompt {
+ id?: string;
+ title: string;
+ content: string;
+}
+
+// @todo Retrieve von ext config
+const defaultPrompts: AiPrompt[] = [
+ // {
+ // title: 'Correct German spelling ',
+ // content:
+ // 'You are a professional text corrector. Improve the text grammatically in German.',
+ // },
];
function ChatView() {
@@ -101,6 +113,31 @@ function ChatView() {
React.useState<null | HTMLElement>(null);
const [promptHistory, setPromptHistory] = React.useState<string[]>([]);
const [historyIndex, setHistoryIndex] = React.useState<number>(-1);
+ const [aiPrompts, setAiPrompts] = React.useState<AiPrompt[]>([]);
+ const [dialogOpen, setDialogOpen] = React.useState<boolean>(false);
+ const [editingPromptId, setEditingPromptId] = React.useState<string | null>(
+ null,
+ );
+ const [dialogTitle, setDialogTitle] = React.useState<string>('');
+ const [dialogContent, setDialogContent] = React.useState<string>('');
+ const [deleteConfirmOpen, setDeleteConfirmOpen] =
+ React.useState<boolean>(false);
+
+ // Load prompts from localStorage on mount
+ useEffect(() => {
+ const savedPrompts = localStorage.getItem('tsAiPrompts');
+ if (savedPrompts) {
+ try {
+ const parsed = JSON.parse(savedPrompts);
+ setAiPrompts(parsed);
+ } catch (e) {
+ console.error('Failed to parse saved prompts:', e);
+ setAiPrompts(defaultPrompts);
+ }
+ } else {
+ setAiPrompts(defaultPrompts);
+ }
+ }, []);
// Input change handler
const handleInputChange = useCallback(
@@ -272,18 +309,115 @@ function ChatView() {
}
}, []);
+ // Prompt management handlers
+ const savePromptsToStorage = useCallback((prompts: AiPrompt[]) => {
+ localStorage.setItem('tsAiPrompts', JSON.stringify(prompts));
+ }, []);
+
+ const handleOpenCreatePrompt = useCallback(() => {
+ setEditingPromptId(null);
+ setDialogTitle('');
+ setDialogContent('');
+ setDialogOpen(true);
+ setPromptAnchorEl(null);
+ }, []);
+
+ const handleOpenEditPrompt = useCallback((prompt: AiPrompt) => {
+ setEditingPromptId(prompt.id || null);
+ setDialogTitle(prompt.title);
+ setDialogContent(prompt.content);
+ setDialogOpen(true);
+ setPromptAnchorEl(null);
+ }, []);
+
+ const handleCloseDialog = useCallback(() => {
+ setDialogOpen(false);
+ setEditingPromptId(null);
+ setDialogTitle('');
+ setDialogContent('');
+ }, []);
+
+ const handleSavePrompt = useCallback(() => {
+ if (!dialogTitle.trim() || !dialogContent.trim()) {
+ showNotification(t('core:mandatoryFieldsEmpty'), 'error', false);
+ return;
+ }
+
+ let updatedPrompts: AiPrompt[];
+ if (editingPromptId) {
+ // Edit existing prompt
+ updatedPrompts = aiPrompts.map((p) =>
+ p.id === editingPromptId
+ ? { ...p, title: dialogTitle, content: dialogContent }
+ : p,
+ );
+ } else {
+ // Create new prompt
+ const newPrompt: AiPrompt = {
+ id: getUuid(),
+ title: dialogTitle,
+ content: dialogContent,
+ };
+ updatedPrompts = [...aiPrompts, newPrompt];
+ }
+
+ setAiPrompts(updatedPrompts);
+ savePromptsToStorage(updatedPrompts);
+ handleCloseDialog();
+ showNotification(
+ editingPromptId ? t('core:promptUpdated') : t('core:promptCreated'),
+ 'info',
+ false,
+ );
+ }, [
+ dialogTitle,
+ dialogContent,
+ editingPromptId,
+ aiPrompts,
+ savePromptsToStorage,
+ handleCloseDialog,
+ showNotification,
+ t,
+ ]);
+
+ const handleDeletePrompt = useCallback(() => {
+ if (editingPromptId) {
+ const updatedPrompts = aiPrompts.filter((p) => p.id !== editingPromptId);
+ setAiPrompts(updatedPrompts);
+ savePromptsToStorage(updatedPrompts);
+ handleCloseDialog();
+ showNotification(t('core:promptDeleted'), 'info', false);
+ setPromptAnchorEl(null);
+ }
+ }, [
+ editingPromptId,
+ aiPrompts,
+ savePromptsToStorage,
+ handleCloseDialog,
+ showNotification,
+ t,
+ ]);
+
+ const handleOpenDeleteConfirm = useCallback(() => {
+ setDeleteConfirmOpen(true);
+ }, []);
+
+ const handleCloseDeleteConfirm = useCallback(() => {
+ setDeleteConfirmOpen(false);
+ }, []);
+
const getSelectedIFrameContent = () => {
const iframe = document.getElementsByTagName('iframe')[0];
- const iframeWindow = iframe.contentWindow;
- const selection = iframeWindow.getSelection();
- const selectionText = selection.toString();
+ const iframeWindow = iframe?.contentWindow;
+ const selection = iframeWindow?.getSelection();
+ const selectionText = selection?.toString();
if (selectionText) {
return selectionText;
} else {
const childIframe =
iframeWindow.document.getElementsByTagName('iframe')[0];
- const subselection = childIframe.contentWindow.getSelection();
- return subselection.toString();
+ const subselection = childIframe?.contentWindow.getSelection();
+ return subselection?.toString() || '';
}
};
@@ -492,14 +626,40 @@ function ChatView() {
}}
>
<TsMenuList>
- {aiPrompts.map((aiPrompt, index) => (
+ <MenuItem onClick={handleOpenCreatePrompt}>
+ {t('core:createPrompt')}
+ </MenuItem>
+ {aiPrompts.length > 0 && <Divider />}
+ {aiPrompts.map((aiPrompt) => (
<MenuItem
- key={index}
+ key={aiPrompt.id || aiPrompt.title}
onClick={() => {
- handlePromptSelect(aiPrompt.prompt);
+ handlePromptSelect(aiPrompt.content);
}}
+ sx={{ flexGrow: 1 }}
>
- {aiPrompt.title}
+ <Stack
+ direction="row"
+ spacing={1}
+ sx={{
+ width: '100%',
+ alignItems: 'center',
+ }}
+ >
+ <Box sx={{ flexGrow: 1 }}>
+ {aiPrompt.title}
+ </Box>
+ <TsIconButton
+ size="small"
+ onClick={(e) => {
+ e.stopPropagation();
+ handleOpenEditPrompt(aiPrompt);
+ }}
+ tooltip={t('core:edit')}
+ >
+ <EditIcon fontSize="small" />
+ </TsIconButton>
+ </Stack>
</MenuItem>
))}
{openedEntry?.isFile && (
@@ -557,6 +717,35 @@ function ChatView() {
</Grid>
</Grid>
</Grid>
+
+ {/* Edit/Create Prompt Dialog */}
+ <PromptEditDialog
+ open={dialogOpen}
+ editingPromptId={editingPromptId}
+ dialogTitle={dialogTitle}
+ dialogContent={dialogContent}
+ onTitleChange={setDialogTitle}
+ onContentChange={setDialogContent}
+ onSave={handleSavePrompt}
+ onClose={handleCloseDialog}
+ onDelete={handleOpenDeleteConfirm}
+ />
+
+ {/* Delete Confirmation Dialog */}
+ <ConfirmDialog
+ open={deleteConfirmOpen}
+ onClose={handleCloseDeleteConfirm}
+ title={t('core:deletePromptConfirm')}
+ content={t('core:deletePromptConfirmDescription')}
+ confirmCallback={(result) => {
+ if (result) {
+ handleDeletePrompt();
+ }
+ }}
+ customCancelText={t('core:cancel')}
+ customConfirmText={t('core:delete')}
+ list={[]}
+ />
</Box>
);
}
diff --git a/src/renderer/components/chat/PromptEditDialog.tsx b/src/renderer/components/chat/PromptEditDialog.tsx
new file mode 100644
index 000000000..80f416bcb
--- /dev/null
+++ b/src/renderer/components/chat/PromptEditDialog.tsx
@@ -0,0 +1,115 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2026-present TagSpaces GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+import TsTextField from '-/components/TsTextField';
+import {
+ Dialog,
+ DialogContent,
+ Paper,
+ useMediaQuery,
+ useTheme,
+} from '@mui/material';
+import { useTranslation } from 'react-i18next';
+import TSDialogActions from '../dialogs/components/TsDialogActions';
+import TsDialogTitle from '../dialogs/components/TsDialogTitle';
+import DraggablePaper from '../DraggablePaper';
+import TsButton from '../TsButton';
+
+interface PromptEditDialogProps {
+ open: boolean;
+ editingPromptId: string | null;
+ dialogTitle: string;
+ dialogContent: string;
+ onTitleChange: (value: string) => void;
+ onContentChange: (value: string) => void;
+ onSave: () => void;
+ onClose: () => void;
+ onDelete: () => void;
+}
+
+function PromptEditDialog(props: PromptEditDialogProps) {
+ const {
+ open,
+ editingPromptId,
+ dialogTitle,
+ dialogContent,
+ onTitleChange,
+ onContentChange,
+ onSave,
+ onClose,
+ onDelete,
+ } = props;
+ const { t } = useTranslation();
+ const theme = useTheme();
+ const smallScreen = useMediaQuery(theme.breakpoints.down('md'));
+
+ return (
+ <Dialog
+ fullScreen={smallScreen}
+ PaperComponent={smallScreen ? Paper : DraggablePaper}
+ open={open}
+ scroll="paper"
+ onClose={onClose}
+ maxWidth="sm"
+ fullWidth
+ >
+ <TsDialogTitle
+ dialogTitle={
+ editingPromptId ? t('core:editPrompt') : t('core:createPrompt')
+ }
+ closeButtonTestId={''}
+ onClose={onClose}
+ ></TsDialogTitle>
+ <DialogContent
+ sx={{ display: 'flex', flexDirection: 'column', gap: 2, pt: 2 }}
+ >
+ <TsTextField
+ autoFocus
+ label={t('core:promptTitle')}
+ value={dialogTitle}
+ onChange={(e) => onTitleChange(e.target.value)}
+ fullWidth
+ placeholder={t('core:promptTitlePlaceholder')}
+ />
+ <TsTextField
+ label={t('core:promptContent')}
+ value={dialogContent}
+ onChange={(e) => onContentChange(e.target.value)}
+ fullWidth
+ multiline
+ minRows={6}
+ maxRows={20}
+ placeholder={t('core:promptContentPlaceholder')}
+ />
+ </DialogContent>
+ <TSDialogActions>
+ {editingPromptId && (
+ <TsButton onClick={onDelete} sx={{ mr: 'auto' }}>
+ {t('core:delete')}
+ </TsButton>
+ )}
+ <TsButton onClick={onClose}>{t('core:cancel')}</TsButton>
+ <TsButton onClick={onSave} variant="contained" color="primary">
+ {t('core:save')}
+ </TsButton>
+ </TSDialogActions>
+ </Dialog>
+ );
+}
+
+export default PromptEditDialog;
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 22dafdd0b..10d732cee 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -970,6 +970,17 @@
"remove": "Remove",
"send": "Send",
"appendSelectionToPrompt": "Append selected text to prompt",
+ "promptUpdated": "Prompt updated",
+ "promptCreated": "Prompt created",
+ "promptDeleted": "Prompt deleted",
+ "promptTitle": "Prompt title",
+ "promptTitlePlaceholder": "e.g., Translate to English",
+ "promptContent": "Prompt content",
+ "promptContentPlaceholder": "Enter the prompt content...",
+ "deletePromptConfirm": "Delete Prompt",
+ "deletePromptConfirmDescription": "Are you sure you want to delete this prompt?",
+ "createPrompt": "Create prompt",
+ "editPrompt": "Edit prompt",
"folderSpecificSettings": "folder specific",
"compareAndUpgrade": "Compare & Upgrade",
"start": "Start",