diff --git a/src/main/java/org/jabref/gui/entryeditor/aichattab/AiChatTab.java b/src/main/java/org/jabref/gui/entryeditor/aichattab/AiChatTab.java index 55a93a1cafc..0b3e36d360e 100644 --- a/src/main/java/org/jabref/gui/entryeditor/aichattab/AiChatTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/aichattab/AiChatTab.java @@ -17,6 +17,8 @@ import org.jabref.logic.ai.AiService; import org.jabref.logic.ai.AiIngestor; import org.jabref.logic.ai.ChatMessage; +import org.jabref.logic.citationkeypattern.CitationKeyGenerator; +import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.database.BibDatabaseContext; @@ -44,6 +46,7 @@ public class AiChatTab extends EntryEditorTab { private final FilePreferences filePreferences; private final AiPreferences aiPreferences; private final EntryEditorPreferences entryEditorPreferences; + private final CitationKeyPatternPreferences citationKeyPatternPreferences; private final BibDatabaseContext bibDatabaseContext; private final TaskExecutor taskExecutor; @@ -61,6 +64,7 @@ public AiChatTab(DialogService dialogService, PreferencesService preferencesServ this.filePreferences = preferencesService.getFilePreferences(); this.aiPreferences = preferencesService.getAiPreferences(); this.entryEditorPreferences = preferencesService.getEntryEditorPreferences(); + this.citationKeyPatternPreferences = preferencesService.getCitationKeyPatternPreferences(); this.aiService = aiService; @@ -80,13 +84,17 @@ public boolean shouldShow(BibEntry entry) { @Override protected void bindToEntry(BibEntry entry) { if (!aiPreferences.getEnableChatWithFiles()) { + setContent(new Label(Localization.lang("JabRef uses OpenAI to enable \"chatting\" with PDF files. OpenAI is an external service. To enable JabRef chatgting with PDF files, the content of the PDF files need to be shared with OpenAI. As soon as you ask a question, the text content of all PDFs attached to the entry are send to OpenAI. The privacy policy of OpenAI applies. You find it at ."))); + } else if (!checkIfCitationKeyIsAppropriate(bibDatabaseContext, entry)) { + CitationKeyGenerator citationKeyGenerator = new CitationKeyGenerator(bibDatabaseContext, citationKeyPatternPreferences); + if (citationKeyGenerator.generateAndSetKey(entry).isEmpty()) { + setContent(new Label(Localization.lang("Citation key could not be generated. Please provide a non-empty and unique citation key for this entry"))); + } else { + bindToEntry(entry); + } setContent(new PrivacyNoticeComponent(dialogService, aiPreferences, filePreferences, () -> { bindToEntry(entry); })); - } else if (entry.getCitationKey().isEmpty()) { - setContent(new ErrorStateComponent(Localization.lang("Error"), Localization.lang("Please provide a citation key for the entry in order to enable chatting with PDF files."))); - } else if (!checkIfCitationKeyIsUnique(bibDatabaseContext, entry.getCitationKey().get())) { - setContent(new ErrorStateComponent(Localization.lang("Error"), Localization.lang("Please provide a unique citation key for the entry in order to enable chatting with PDF files."))); } else if (entry.getFiles().isEmpty()) { setContent(new ErrorStateComponent(Localization.lang("Unable to chat"), Localization.lang("Please attach at least one PDF file to enable chatting with PDF files."))); } else if (!entry.getFiles().stream().map(LinkedFile::getLink).map(Path::of).allMatch(FileUtil::isPDFFile)) { @@ -96,6 +104,15 @@ protected void bindToEntry(BibEntry entry) { } } + private static boolean checkIfCitationKeyIsAppropriate(BibDatabaseContext bibDatabaseContext, BibEntry bibEntry) { + //noinspection OptionalGetWithoutIsPresent + return !checkIfCitationKeyIsEmpty(bibEntry) && checkIfCitationKeyIsUnique(bibDatabaseContext, bibEntry.getCitationKey().get()); + } + + private static boolean checkIfCitationKeyIsEmpty(BibEntry bibEntry) { + return bibEntry.getCitationKey().isEmpty() || bibEntry.getCitationKey().get().isEmpty(); + } + private static boolean checkIfCitationKeyIsUnique(BibDatabaseContext bibDatabaseContext, String citationKey) { return bibDatabaseContext.getDatabase().getEntries().stream() .map(BibEntry::getCitationKey)