Skip to content

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

disable generate buttons if no DefaultAIProvider (#2344)

Stanimird4c9d9ddcf32
diff --git a/src/renderer/components/AiPropertiesTab.tsx b/src/renderer/components/AiPropertiesTab.tsx
index db7dbfaae..2197c9f71 100644
--- a/src/renderer/components/AiPropertiesTab.tsx
+++ b/src/renderer/components/AiPropertiesTab.tsx
@@ -16,14 +16,14 @@
*
*/
-import React from 'react';
-import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
-import { Box } from '@mui/material';
+import AppConfig from '-/AppConfig';
import AiGenDescButton from '-/components/chat/AiGenDescButton';
import AiGenTagsButton from '-/components/chat/AiGenTagsButton';
-import { useSelector } from 'react-redux';
-import { isDevMode } from '-/reducers/settings';
import ChatView from '-/components/chat/ChatView';
+import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
+import { isDevMode } from '-/reducers/settings';
+import { Box } from '@mui/material';
+import { useSelector } from 'react-redux';
interface Props {}
@@ -39,7 +39,10 @@ function AiPropertiesTab(props: Props) {
<Box position="relative" display="inline-flex">
<AiGenDescButton />
{openedEntry.meta?.description && (
- <AiGenTagsButton fromDescription={true} />
+ <AiGenTagsButton
+ fromDescription={true}
+ style={{ marginLeft: AppConfig.defaultSpaceBetweenButtons }}
+ />
)}
</Box>
);
diff --git a/src/renderer/components/EditDescriptionButtons.tsx b/src/renderer/components/EditDescriptionButtons.tsx
index a2e11dddf..a635ccbf5 100644
--- a/src/renderer/components/EditDescriptionButtons.tsx
+++ b/src/renderer/components/EditDescriptionButtons.tsx
@@ -6,16 +6,16 @@ import TsIconButton from '-/components/TsIconButton';
import AiGenDescButton from '-/components/chat/AiGenDescButton';
import AiGenTagsButton from '-/components/chat/AiGenTagsButton';
import DescriptionMenu from '-/components/md/DescriptionMenu';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { useFilePropertiesContext } from '-/hooks/useFilePropertiesContext';
import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
import { Pro } from '-/pro';
import { saveAsTextFile } from '-/services/utils-io';
-import { Box, ButtonGroup, Tooltip, useTheme } from '@mui/material';
+import { ButtonGroup, Tooltip, useTheme } from '@mui/material';
import { formatDateTime4Tag } from '@tagspaces/tagspaces-common/misc';
import { extractTitle } from '@tagspaces/tagspaces-common/paths';
import React from 'react';
import { useTranslation } from 'react-i18next';
-import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
interface ButtonsProps {
getHtml: () => string;
@@ -204,18 +204,15 @@ const EditDescriptionButtons: React.FC<ButtonsProps> = ({
<ProTooltip
tooltip={'Add AI generated description based on the file content'}
>
- <AiGenDescButton disabled={!Pro} variant="outlined" />
+ <AiGenDescButton variant="outlined" />
</ProTooltip>
{openedEntry.meta?.description && (
<>
- <Box
- style={{ marginLeft: AppConfig.defaultSpaceBetweenButtons }}
- ></Box>
<ProTooltip
tooltip={'Add AI generated tags based on the description'}
>
<AiGenTagsButton
- disabled={!Pro}
+ style={{ marginLeft: AppConfig.defaultSpaceBetweenButtons }}
fromDescription={true}
variant="outlined"
/>
diff --git a/src/renderer/components/chat/AiGenDescButton.tsx b/src/renderer/components/chat/AiGenDescButton.tsx
index 343c1844e..18abadc91 100644
--- a/src/renderer/components/chat/AiGenDescButton.tsx
+++ b/src/renderer/components/chat/AiGenDescButton.tsx
@@ -25,22 +25,26 @@ import { useIOActionsContext } from '-/hooks/useIOActionsContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
import { AppDispatch } from '-/reducers/app';
-import { actions as SettingsActions } from '-/reducers/settings';
+import {
+ actions as SettingsActions,
+ getDefaultAIProvider,
+} from '-/reducers/settings';
import { TS } from '-/tagspaces.namespace';
import { ButtonGroup } from '@mui/material';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { useDispatch } from 'react-redux';
+import { useDispatch, useSelector } from 'react-redux';
import { AIIcon } from '../CommonIcons';
import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
+import { Pro } from '-/pro';
+import { AIProvider } from '-/components/chat/ChatTypes';
type Props = TSButtonProps & {};
function AiGenDescButton(props: Props) {
const { t } = useTranslation();
- const { disabled } = props;
+ // const { disabled } = props;
const dispatch: AppDispatch = useDispatch();
- //const defaultAiProvider: AIProvider = useSelector(getDefaultAIProvider);
const { findLocation } = useCurrentLocationContext();
const { openedEntry } = useOpenedEntryContext();
const { setDescriptionChange } = useIOActionsContext();
@@ -51,13 +55,13 @@ function AiGenDescButton(props: Props) {
const [isLoading, setIsLoading] = useState<boolean>(false);
- //const openedEntryModel = getEntryModel(openedEntry?.name, defaultAiProvider);
+ const defaultAiProvider: AIProvider = useSelector(getDefaultAIProvider);
+ const disabled = isLoading || !Pro || !defaultAiProvider; //!getEntryModel(openedEntry?.name, defaultAiProvider);
if (
!openedEntry ||
!openedEntry.isFile ||
findLocation(openedEntry.locationID)?.isReadOnly
) {
- //|| !openedEntryModel) {
return null;
}
@@ -70,8 +74,8 @@ function AiGenDescButton(props: Props) {
};
function handleGenerationResult(entry: TS.FileSystemEntry) {
//console.log('newOllamaMessage response:' + response);
+ setIsLoading(false);
if (entry) {
- setIsLoading(false);
if (entry.uuid === openedEntry.uuid) {
dispatch(SettingsActions.setEntryContainerTab(TabNames.descriptionTab));
setDescription(entry.meta?.description);
@@ -88,7 +92,7 @@ function AiGenDescButton(props: Props) {
<ButtonGroup>
<TsButton
loading={isLoading}
- disabled={isLoading || disabled}
+ disabled={disabled}
// tooltip="Uses currently configured AI model to generate description for this file"
data-tid="generateDescriptionAITID"
style={{
@@ -105,6 +109,7 @@ function AiGenDescButton(props: Props) {
{t('core:generateDescription')}
</TsButton>
<TsButton
+ disabled={disabled}
// tooltip={t('core:openGenSettings')}
aria-label={t('core:openGenSettings')}
data-tid="fileContainerPrevFile"
diff --git a/src/renderer/components/chat/AiGenTagsButton.tsx b/src/renderer/components/chat/AiGenTagsButton.tsx
index dee910675..06a8270e8 100644
--- a/src/renderer/components/chat/AiGenTagsButton.tsx
+++ b/src/renderer/components/chat/AiGenTagsButton.tsx
@@ -21,8 +21,10 @@ import TsButton, { TSButtonProps } from '-/components/TsButton';
import { AIProvider } from '-/components/chat/ChatTypes';
import { useAiGenerationDialogContext } from '-/components/dialogs/hooks/useAiGenerationDialogContext';
import { useChatContext } from '-/hooks/useChatContext';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
+import { Pro } from '-/pro';
import { getDefaultAIProvider } from '-/reducers/settings';
import { TS } from '-/tagspaces.namespace';
import { ButtonGroup } from '@mui/material';
@@ -32,7 +34,6 @@ import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { AIIcon } from '../CommonIcons';
-import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
type Props = TSButtonProps & {
variant?: OverridableStringUnion<
@@ -42,11 +43,11 @@ type Props = TSButtonProps & {
entries?: TS.FileSystemEntry[];
fromDescription?: boolean;
generationCompleted?: () => void;
- disabled?: boolean;
+ style?: any;
};
function AiGenTagsButton(props: Props) {
- const { fromDescription, variant, disabled, entries, generationCompleted } =
+ const { fromDescription, variant, entries, generationCompleted, style } =
props;
const { t } = useTranslation();
const { findLocation } = useCurrentLocationContext();
@@ -58,6 +59,8 @@ function AiGenTagsButton(props: Props) {
const [isLoading, setIsLoading] = useState<boolean>(false);
+ const disabled = isLoading || !Pro;
+
let generateEntries: TS.FileSystemEntry[] = entries;
if (!generateEntries && openedEntry) {
generateEntries = [openedEntry];
@@ -87,30 +90,34 @@ function AiGenTagsButton(props: Props) {
if (success) {
const promises = generateEntries.map((entry) => {
tagsGenerate(entry, fromDescription).then(() => {
- setIsLoading(false);
if (generationCompleted) {
generationCompleted();
}
});
});
- Promise.all(promises).then((results) => {
- if (results && results.every((result) => result)) {
- showNotification('Tags generated by an AI.');
- }
- });
+ Promise.all(promises)
+ .then((results) => {
+ setIsLoading(false);
+ if (results && results.every((result) => result)) {
+ showNotification('Tags generated by an AI.');
+ }
+ })
+ .catch((e) => {
+ setIsLoading(false);
+ console.log(e);
+ });
} else {
- showNotification(
- 'Ollama Models not loaded. Check if Ollama service is alive.',
- );
+ setIsLoading(false);
+ showNotification(t('core:noModelsLoaded'));
}
});
};
return (
- <ButtonGroup>
+ <ButtonGroup style={{ ...style }}>
<TsButton
loading={isLoading}
- disabled={isLoading || disabled}
+ disabled={disabled}
// tooltip="Uses currently configured AI model to generate tags for this file"
data-tid="generateTagsAITID"
onClick={handleGeneration}
@@ -126,6 +133,7 @@ function AiGenTagsButton(props: Props) {
)}
</TsButton>
<TsButton
+ disabled={disabled}
// tooltip={t('core:openGenSettings')}
aria-label={t('core:openGenSettings')}
data-tid="fileContainerPrevFile"
diff --git a/src/renderer/components/dialogs/AddRemoveTagsDialog.tsx b/src/renderer/components/dialogs/AddRemoveTagsDialog.tsx
index 5e477a4ba..35969e528 100644
--- a/src/renderer/components/dialogs/AddRemoveTagsDialog.tsx
+++ b/src/renderer/components/dialogs/AddRemoveTagsDialog.tsx
@@ -197,12 +197,6 @@ function AddRemoveTagsDialog(props: Props) {
>
{t('core:cancel')}
</TsButton>
- {/* <AiGenTagsButton
- disabled={!Pro}
- entries={selected}
- variant="outlined"
- generationCompleted={() => onCloseDialog()}
- /> */}
<TsButton
data-tid="cleanTagsMultipleEntries"
disabled={selected.length < 1}
diff --git a/src/renderer/components/dialogs/components/SettingsAI.tsx b/src/renderer/components/dialogs/components/SettingsAI.tsx
index 679f08ab0..1436240b8 100644
--- a/src/renderer/components/dialogs/components/SettingsAI.tsx
+++ b/src/renderer/components/dialogs/components/SettingsAI.tsx
@@ -411,6 +411,7 @@ function SettingsAI(props: Props) {
/>
</FormControl>
<SelectChatModel
+ disabled={!providersAlive.current[provider.id]}
label={t('core:defaultAImodelText') + ' *'}
handleChangeModel={(modelName: string) => {
handleChangeProvider(
@@ -424,6 +425,7 @@ function SettingsAI(props: Props) {
chosenModel={provider.defaultTextModel}
/>
<SelectChatModel
+ disabled={!providersAlive.current[provider.id]}
label={t('core:defaultAImodelImages')}
handleChangeModel={(modelName: string) => {
handleChangeProvider(
diff --git a/src/renderer/hooks/ChatProvider.tsx b/src/renderer/hooks/ChatProvider.tsx
index e21de6525..4e9d3e742 100644
--- a/src/renderer/hooks/ChatProvider.tsx
+++ b/src/renderer/hooks/ChatProvider.tsx
@@ -1248,7 +1248,7 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {
}
return Promise.resolve(entry);
} else {
- showNotification('Error No Models loaded');
+ showNotification(t('core:noModelsLoaded'));
return Promise.resolve(undefined);
}
});
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 4de2c1e9d..8be5bf992 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -974,5 +974,6 @@
"confirmReindex": "Index not available",
"confirmReindexContent": "Do you want to create it now?",
"importingMacTags": "Importing MacOS tags...",
+ "noModelsLoaded": "Error No Models loaded. Check if AI service is alive.",
"-": "-"
}