Skip to content
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

Added markdown in AI chat #12392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import org.jabref.gui.icon.JabRefIconView?>
<?import com.dlsc.gemsfx.ExpandingTextArea?>
<fx:root nodeOrientation="LEFT_TO_RIGHT" type="javafx.scene.layout.Pane"
xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.jabref.gui.ai.components.aichat.chatmessage.ChatMessageComponent">
Expand All @@ -23,7 +23,7 @@
<Font name="System Bold" size="12.0" />
</font>
</Label>
<ExpandingTextArea fx:id="contentTextArea" editable="false" nodeOrientation="LEFT_TO_RIGHT" text="Content" styleClass="chat-message-text-area" />
<Text fx:id="contentText" nodeOrientation="LEFT_TO_RIGHT" text="Content" styleClass="chat-message-text-area" />
</VBox>

<VBox fx:id="buttonsVBox" alignment="BASELINE_RIGHT">
Expand All @@ -35,6 +35,14 @@
<Tooltip text="%Delete message from chat history" />
</tooltip>
</Button>
<Button onAction="#copyToClipboard" styleClass="icon-button,narrow" textAlignment="CENTER">
<graphic>
<JabRefIconView glyph="COPY"/>
</graphic>
<tooltip>
<Tooltip text="%Copy to clipboard" />
</tooltip>
</Button>
</VBox>
</HBox>
</fx:root>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jabref.gui.ai.components.aichat.chatmessage;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.function.Consumer;

import javafx.beans.property.ObjectProperty;
Expand All @@ -9,12 +12,13 @@
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

import org.jabref.logic.ai.util.ErrorMessage;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.format.MarkdownFormatter;

import com.airhacks.afterburner.views.ViewLoader;
import com.dlsc.gemsfx.ExpandingTextArea;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
Expand All @@ -24,13 +28,15 @@
public class ChatMessageComponent extends HBox {
private static final Logger LOGGER = LoggerFactory.getLogger(ChatMessageComponent.class);

MarkdownFormatter markdownFormatter = new MarkdownFormatter();

private final ObjectProperty<ChatMessage> chatMessage = new SimpleObjectProperty<>();
private final ObjectProperty<Consumer<ChatMessageComponent>> onDelete = new SimpleObjectProperty<>();

@FXML private HBox wrapperHBox;
@FXML private VBox vBox;
@FXML private Label sourceLabel;
@FXML private ExpandingTextArea contentTextArea;
@FXML private Text contentText;
@FXML private VBox buttonsVBox;

public ChatMessageComponent() {
Expand Down Expand Up @@ -67,23 +73,26 @@ private void loadChatMessage() {
switch (chatMessage.get()) {
case UserMessage userMessage -> {
setColor("-jr-ai-message-user", "-jr-ai-message-user-border");
setTextWrapping(400.0);
setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
sourceLabel.setText(Localization.lang("User"));
contentTextArea.setText(userMessage.singleText());
contentText.setText(userMessage.singleText());
}

case AiMessage aiMessage -> {
setColor("-jr-ai-message-ai", "-jr-ai-message-ai-border");
setTextWrapping(400.0);
setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
sourceLabel.setText(Localization.lang("AI"));
contentTextArea.setText(aiMessage.text());
contentText.setText(aiMessage.text());
}

case ErrorMessage errorMessage -> {
setColor("-jr-ai-message-error", "-jr-ai-message-error-border");
setTextWrapping(400.0);
setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
sourceLabel.setText(Localization.lang("Error"));
contentTextArea.setText(errorMessage.getText());
contentText.setText(errorMessage.getText());
}

default ->
Expand All @@ -103,7 +112,17 @@ private void onDeleteClick() {
}
}

@FXML
private void copyToClipboard() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(contentText.getText()), null);
}

private void setColor(String fillColor, String borderColor) {
vBox.setStyle("-fx-background-color: " + fillColor + "; -fx-border-radius: 10; -fx-background-radius: 10; -fx-border-color: " + borderColor + "; -fx-border-width: 3;");
}

private void setTextWrapping(Double wrappingWidth) {
contentText.setWrappingWidth(wrappingWidth);
}
}
Loading