Skip to content

Commit

Permalink
Merge pull request #26 from InAnYan/fix-for-issue-19
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed May 29, 2024
2 parents 8b4d3c6 + df60c5f commit baad7b3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/java/org/jabref/gui/entryeditor/aichattab/AiChatTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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 <https://openai.com/policies/privacy-policy/>.")));
} 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)) {
Expand All @@ -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)
Expand Down

0 comments on commit baad7b3

Please sign in to comment.