Skip to content

Feature ai chat regenerate button #12794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

xiaoMartintin
Copy link

@xiaoMartintin xiaoMartintin commented Mar 21, 2025

Closes #12191

This PR adds a "Regenerate" button to the AI Chat tab in the entry editor.

Changes Made

chatmessage/ChatMessageComponent.fxml
chatmessage/ChatMessageComponent.java
chatmessage/AiChatComponent.java
chathistory/ChatHistoryComponent.java

Sorry for the confusion, but I really don't know how I could add the "Regenerate AI response" to all properties. I only appended the sentence in following files.

resources/l10n/JabRef_en.properties
JabRef_de.properties
JabRef_fr.properties
JabRef_pt_BR.properties

Here's the screenshot.
截屏2025-03-21 15 40 28

Additional notes

Some unrelated tests in main already fail before this PR. This PR does not touch any of those areas:

  • ArgumentProcessorTest.convertBibtexToTableRefsAsBib
  • PushToTeXworksTest.pushEntries() (macOS-specific behavior)
  • ThemeManagerTest.liveReloadCssDataUrl

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • [/ ] Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • [/ ] Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (if change is visible to the user)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

Comment on lines +95 to +96
deleteLastMessage();
deleteLastMessage();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling deleteLastMessage() twice creates code duplication. Consider creating a new method like deleteLastTwoMessages() to encapsulate this logic and improve maintainability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or add a Java comment above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False positive in this case.

});
if (chatMessage instanceof AiMessage) {
component.setOnRegenerate(comp -> {
if (!items.isEmpty() && items.getLast() == chatMessage && items.size() > 1) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct access to the last element without checking if the list has sufficient elements could cause runtime exceptions. Should implement proper validation first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reorder the conditions. I think, size check should come first and empty can be ommitted

@koppor
Copy link
Member

koppor commented Mar 21, 2025

}

@FXML
public void initialize() {
uiChatHistory.setItems(aiChatLogic.getChatHistory());
uiChatHistory.setRegenerateCallback(userPrompt -> {
// Remove the last two messages: first the AI response, then the corresponding user message
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment duplicated in two places and simply restates what the code does without providing additional insight or reasoning about the implementation.

Comment on lines +57 to +58
if (items.size() > 1 && items.getLast() == chatMessage) {
ChatMessage previous = items.get(items.size() - 2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't follow fail-fast principle. The conditions should be checked separately with early returns to avoid deep nesting and improve readability.

})));
for (ChatMessage chatMessage : items) {
ChatMessageComponent component = new ChatMessageComponent(chatMessage, comp -> {
items.remove(chatMessage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you removed this code:

int index = vBox.getChildren().indexOf(chatMessageComponent);
items.remove(index);

});
if (chatMessage instanceof AiMessage) {
component.setOnRegenerate(comp -> {
if (items.size() > 1 && items.getLast() == chatMessage) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move out this code to AiChatComponent? So that ChatHistory component would be responsible only for history?

if (previous instanceof UserMessage message) {
String userText = message.singleText();
// Remove the last two messages: first the AI message, then the corresponding user message
items.removeLast();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public void setOnRegenerate(Consumer<ChatMessageComponent> callback) {
this.onRegenerate.set(callback);
}

private void loadChatMessage() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also make that "Regenerate AI response" button is shown only if the message is an AiMessage or ErrorMessage?

@InAnYan
Copy link
Member

InAnYan commented Mar 24, 2025

Thanks for checking out AI features!

But, I have to say that regeneration doesn't work as intended.

It seems to generate a new answer to previous questions, but whole chat history is cleared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add "regenerate" button to AI chat tab
4 participants