Skip to content

Commit

Permalink
Route ChatHistoryService correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 8, 2024
1 parent 590fe41 commit 8d659f8
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 58 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.frame.JabRefFrame;
import org.jabref.gui.help.VersionWorker;
import org.jabref.gui.icon.IconTheme;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class JabRefGUI extends Application {

// AI Service handles chat messages etc. Therefore, it is tightly coupled to the GUI.
private static AiService aiService;
private static ChatHistoryService chatHistoryService;

private static StateManager stateManager;
private static ThemeManager themeManager;
Expand Down Expand Up @@ -95,6 +97,7 @@ public void start(Stage stage) {
fileUpdateMonitor,
preferencesService,
aiService,
chatHistoryService,
stateManager,
countingUndoManager,
Injector.instantiateModelOrService(BibEntryTypesManager.class),
Expand Down Expand Up @@ -164,6 +167,11 @@ public void initialize() {
dialogService,
taskExecutor);
Injector.setModelOrService(AiService.class, aiService);

JabRefGUI.chatHistoryService = new ChatHistoryService(
preferencesService.getCitationKeyPatternPreferences(),
dialogService);
Injector.setModelOrService(ChatHistoryService.class, chatHistoryService);
}

private void setupProxy() {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/jabref/gui/entryeditor/AiChatTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.scene.control.Tooltip;

import org.jabref.gui.DialogService;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.ai.components.aichat.AiChatGuardedComponent;
import org.jabref.gui.ai.components.privacynotice.PrivacyNoticeComponent;
import org.jabref.gui.ai.components.util.errorstate.ErrorStateComponent;
Expand All @@ -30,6 +31,7 @@
public class AiChatTab extends EntryEditorTab {
private final BibDatabaseContext bibDatabaseContext;
private final AiService aiService;
private final ChatHistoryService chatHistoryService;
private final DialogService dialogService;
private final AiPreferences aiPreferences;
private final FilePreferences filePreferences;
Expand All @@ -41,13 +43,15 @@ public class AiChatTab extends EntryEditorTab {

public AiChatTab(BibDatabaseContext bibDatabaseContext,
AiService aiService,
ChatHistoryService chatHistoryService,
DialogService dialogService,
PreferencesService preferencesService,
TaskExecutor taskExecutor
) {
this.bibDatabaseContext = bibDatabaseContext;

this.aiService = aiService;
this.chatHistoryService = chatHistoryService;
this.dialogService = dialogService;

this.aiPreferences = preferencesService.getAiPreferences();
Expand All @@ -72,9 +76,7 @@ public boolean shouldShow(BibEntry entry) {
*/
@Override
protected void bindToEntry(BibEntry entry) {
previousBibEntry.ifPresent(previousBibEntry ->
aiService.getChatHistoryService().closeChatHistoryForEntry(previousBibEntry));

previousBibEntry.ifPresent(previousBibEntry -> chatHistoryService.closeChatHistoryForEntry(previousBibEntry));
previousBibEntry = Optional.of(entry);

if (!aiPreferences.getEnableAi()) {
Expand Down Expand Up @@ -133,7 +135,7 @@ private void bindToCorrectEntry(BibEntry entry) {

setContent(new AiChatGuardedComponent(
chatName,
aiService.getChatHistoryService().getChatHistoryForEntry(entry),
chatHistoryService.getChatHistoryForEntry(entry),
bibDatabaseContext,
FXCollections.observableArrayList(new ArrayList<>(List.of(entry))),
aiService,
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.citationkeypattern.GenerateCitationKeySingleAction;
import org.jabref.gui.cleanup.CleanupSingleAction;
import org.jabref.gui.entryeditor.citationrelationtab.CitationRelationsTab;
Expand Down Expand Up @@ -118,6 +119,7 @@ public class EntryEditor extends BorderPane {
@Inject private KeyBindingRepository keyBindingRepository;
@Inject private JournalAbbreviationRepository journalAbbreviationRepository;
@Inject private AiService aiService;
@Inject private ChatHistoryService chatHistoryService;

private final List<EntryEditorTab> allPossibleTabs;
private final Collection<OffersPreview> previewTabs;
Expand Down Expand Up @@ -313,7 +315,7 @@ private List<EntryEditorTab> createTabs() {
tabs.add(new LatexCitationsTab(databaseContext, preferencesService, dialogService, directoryMonitorManager));
tabs.add(new FulltextSearchResultsTab(stateManager, preferencesService, dialogService, databaseContext, taskExecutor, libraryTab.searchQueryProperty()));
tabs.add(new AiSummaryTab(libraryTab.getBibDatabaseContext(), aiService, dialogService, preferencesService));
tabs.add(new AiChatTab(libraryTab.getBibDatabaseContext(), aiService, dialogService, preferencesService, taskExecutor));
tabs.add(new AiChatTab(libraryTab.getBibDatabaseContext(), aiService, chatHistoryService, dialogService, preferencesService, taskExecutor));

return tabs;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/gui/frame/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.importer.NewEntryAction;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
Expand Down Expand Up @@ -76,6 +77,7 @@ public class JabRefFrame extends BorderPane implements LibraryTabContainer, UiMe
private final SplitPane splitPane = new SplitPane();
private final PreferencesService prefs;
private final AiService aiService;
private final ChatHistoryService chatHistoryService;
private final GlobalSearchBar globalSearchBar;

private final FileHistoryMenu fileHistory;
Expand Down Expand Up @@ -104,6 +106,7 @@ public JabRefFrame(Stage mainStage,
FileUpdateMonitor fileUpdateMonitor,
PreferencesService preferencesService,
AiService aiService,
ChatHistoryService chatHistoryService,
StateManager stateManager,
CountingUndoManager undoManager,
BibEntryTypesManager entryTypesManager,
Expand All @@ -114,6 +117,7 @@ public JabRefFrame(Stage mainStage,
this.fileUpdateMonitor = fileUpdateMonitor;
this.prefs = preferencesService;
this.aiService = aiService;
this.chatHistoryService = chatHistoryService;
this.stateManager = stateManager;
this.undoManager = undoManager;
this.entryTypesManager = entryTypesManager;
Expand Down Expand Up @@ -153,7 +157,7 @@ public JabRefFrame(Stage mainStage,
this.sidePane = new SidePane(
this,
prefs,
aiService,
chatHistoryService,
Injector.instantiateModelOrService(JournalAbbreviationRepository.class),
taskExecutor,
dialogService,
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/gui/groups/GroupTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.search.SearchTextField;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.gui.util.RecursiveTreeItem;
import org.jabref.gui.util.ViewModelTreeTableCellFactory;
import org.jabref.gui.util.ViewModelTreeTableRowFactory;
import org.jabref.logic.ai.AiService;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.entry.BibEntry;
Expand All @@ -82,7 +82,7 @@ public class GroupTreeView extends BorderPane {

private final StateManager stateManager;
private final DialogService dialogService;
private final AiService aiService;
private final ChatHistoryService chatHistoryService;
private final TaskExecutor taskExecutor;
private final PreferencesService preferencesService;

Expand All @@ -108,13 +108,13 @@ public GroupTreeView(TaskExecutor taskExecutor,
StateManager stateManager,
PreferencesService preferencesService,
DialogService dialogService,
AiService aiService
ChatHistoryService chatHistoryService
) {
this.taskExecutor = taskExecutor;
this.stateManager = stateManager;
this.preferencesService = preferencesService;
this.dialogService = dialogService;
this.aiService = aiService;
this.chatHistoryService = chatHistoryService;

createNodes();
this.getStylesheets().add(Objects.requireNonNull(GroupTreeView.class.getResource("GroupTree.css")).toExternalForm());
Expand Down Expand Up @@ -164,7 +164,7 @@ private void createNodes() {

private void initialize() {
this.localDragboard = stateManager.getLocalDragboard();
viewModel = new GroupTreeViewModel(stateManager, dialogService, aiService, preferencesService, taskExecutor, localDragboard);
viewModel = new GroupTreeViewModel(stateManager, dialogService, chatHistoryService, preferencesService, taskExecutor, localDragboard);

// Set-up groups tree
groupTree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jabref.gui.AbstractViewModel;
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.ai.components.aichat.AiChatWindow;
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.logic.ai.AiService;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class GroupTreeViewModel extends AbstractViewModel {
private final ListProperty<GroupNodeViewModel> selectedGroups = new SimpleListProperty<>(FXCollections.observableArrayList());
private final StateManager stateManager;
private final DialogService dialogService;
private final AiService aiService;
private final ChatHistoryService chatHistoryService;
private final PreferencesService preferences;
private final TaskExecutor taskExecutor;
private final CustomLocalDragboard localDragboard;
Expand All @@ -77,10 +78,10 @@ public class GroupTreeViewModel extends AbstractViewModel {
};
private Optional<BibDatabaseContext> currentDatabase = Optional.empty();

public GroupTreeViewModel(StateManager stateManager, DialogService dialogService, AiService aiService, PreferencesService preferencesService, TaskExecutor taskExecutor, CustomLocalDragboard localDragboard) {
public GroupTreeViewModel(StateManager stateManager, DialogService dialogService, ChatHistoryService chatHistoryService, PreferencesService preferencesService, TaskExecutor taskExecutor, CustomLocalDragboard localDragboard) {
this.stateManager = Objects.requireNonNull(stateManager);
this.dialogService = Objects.requireNonNull(dialogService);
this.aiService = Objects.requireNonNull(aiService);
this.chatHistoryService = Objects.requireNonNull(chatHistoryService);
this.preferences = Objects.requireNonNull(preferencesService);
this.taskExecutor = Objects.requireNonNull(taskExecutor);
this.localDragboard = Objects.requireNonNull(localDragboard);
Expand Down Expand Up @@ -398,7 +399,7 @@ public void chatWithGroup(GroupNodeViewModel group) {
StringProperty nameProperty = new SimpleStringProperty(Localization.lang("Group %0", groupNameProperty.get()));
groupNameProperty.addListener((obs, oldValue, newValue) -> nameProperty.setValue(Localization.lang("Group %0", groupNameProperty.get())));

ObservableList<ChatMessage> chatHistory = aiService.getChatHistoryService().getChatHistoryForGroup(group.getGroupNode());
ObservableList<ChatMessage> chatHistory = chatHistoryService.getChatHistoryForGroup(group.getGroupNode());
ObservableList<BibEntry> bibEntries = FXCollections.observableArrayList(group.getGroupNode().findMatches(currentDatabase.get().getDatabase()));

openAiChat(nameProperty, chatHistory, currentDatabase.get(), bibEntries);
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.help.HelpAction;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableKeyChange;
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.ai.AiService;
import org.jabref.logic.citationkeypattern.CitationKeyGenerator;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.citationstyle.CitationStyle;
Expand Down Expand Up @@ -95,7 +93,6 @@ public class OpenOfficePanel {
private final VBox vbox = new VBox();

private final PreferencesService preferencesService;
private final AiService aiService;
private final StateManager stateManager;
private final ClipBoardManager clipBoardManager;
private final UndoManager undoManager;
Expand All @@ -109,8 +106,6 @@ public class OpenOfficePanel {

public OpenOfficePanel(LibraryTabContainer tabContainer,
PreferencesService preferencesService,
AiService aiService,
KeyBindingRepository keyBindingRepository,
JournalAbbreviationRepository abbreviationRepository,
UiTaskExecutor taskExecutor,
DialogService dialogService,
Expand All @@ -123,7 +118,6 @@ public OpenOfficePanel(LibraryTabContainer tabContainer,
this.fileUpdateMonitor = fileUpdateMonitor;
this.entryTypesManager = entryTypesManager;
this.preferencesService = preferencesService;
this.aiService = aiService;
this.taskExecutor = taskExecutor;
this.dialogService = dialogService;
this.stateManager = stateManager;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/sidepane/SidePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jabref.gui.LibraryTabContainer;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.logic.ai.AiService;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.entry.BibEntryTypesManager;
Expand All @@ -33,7 +33,7 @@ public class SidePane extends VBox {

public SidePane(LibraryTabContainer tabContainer,
PreferencesService preferencesService,
AiService aiService,
ChatHistoryService chatHistoryService,
JournalAbbreviationRepository abbreviationRepository,
TaskExecutor taskExecutor,
DialogService dialogService,
Expand All @@ -47,7 +47,7 @@ public SidePane(LibraryTabContainer tabContainer,
this.viewModel = new SidePaneViewModel(
tabContainer,
preferencesService,
aiService,
chatHistoryService,
abbreviationRepository,
stateManager,
taskExecutor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTabContainer;
import org.jabref.gui.StateManager;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.gui.groups.GroupTreeView;
import org.jabref.gui.importer.fetcher.WebSearchPaneView;
import org.jabref.gui.openoffice.OpenOfficePanel;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.ai.AiService;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.entry.BibEntryTypesManager;
Expand All @@ -22,7 +22,7 @@
public class SidePaneContentFactory {
private final LibraryTabContainer tabContainer;
private final PreferencesService preferences;
private final AiService aiService;
private final ChatHistoryService chatHistoryService;
private final JournalAbbreviationRepository abbreviationRepository;
private final TaskExecutor taskExecutor;
private final DialogService dialogService;
Expand All @@ -34,7 +34,7 @@ public class SidePaneContentFactory {

public SidePaneContentFactory(LibraryTabContainer tabContainer,
PreferencesService preferences,
AiService aiService,
ChatHistoryService chatHistoryService,
JournalAbbreviationRepository abbreviationRepository,
TaskExecutor taskExecutor,
DialogService dialogService,
Expand All @@ -45,7 +45,7 @@ public SidePaneContentFactory(LibraryTabContainer tabContainer,
UndoManager undoManager) {
this.tabContainer = tabContainer;
this.preferences = preferences;
this.aiService = aiService;
this.chatHistoryService = chatHistoryService;
this.abbreviationRepository = abbreviationRepository;
this.taskExecutor = taskExecutor;
this.dialogService = dialogService;
Expand All @@ -63,12 +63,10 @@ public Node create(SidePaneType sidePaneType) {
stateManager,
preferences,
dialogService,
aiService);
chatHistoryService);
case OPEN_OFFICE -> new OpenOfficePanel(
tabContainer,
preferences,
aiService,
preferences.getKeyBindingRepository(),
abbreviationRepository,
(UiTaskExecutor) taskExecutor,
dialogService,
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/sidepane/SidePaneViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.jabref.gui.LibraryTabContainer;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.logic.ai.AiService;
import org.jabref.gui.ai.chatting.chathistory.ChatHistoryService;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.entry.BibEntryTypesManager;
Expand All @@ -42,7 +42,7 @@ public class SidePaneViewModel extends AbstractViewModel {

public SidePaneViewModel(LibraryTabContainer tabContainer,
PreferencesService preferencesService,
AiService aiService,
ChatHistoryService chatHistoryService,
JournalAbbreviationRepository abbreviationRepository,
StateManager stateManager,
TaskExecutor taskExecutor,
Expand All @@ -57,7 +57,7 @@ public SidePaneViewModel(LibraryTabContainer tabContainer,
this.sidePaneContentFactory = new SidePaneContentFactory(
tabContainer,
preferencesService,
aiService,
chatHistoryService,
abbreviationRepository,
taskExecutor,
dialogService,
Expand Down
Loading

0 comments on commit 8d659f8

Please sign in to comment.