Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
build!:update markdowneditor to 2.0.1 fix:fix update message implementation and feat(demo):add generative ai simulation demo#50
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
bc3bd03c759f0f97e5a61c2ba082d009174928ea7aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,7 +20,6 @@ | ||
| package com.flowingcode.vaadin.addons.chatassistant; | ||
| import com.flowingcode.vaadin.addons.chatassistant.model.Message; | ||
| import com.flowingcode.vaadin.addons.markdown.BaseMarkdownComponent.DataColorMode; | ||
| import com.flowingcode.vaadin.addons.markdown.MarkdownViewer; | ||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.component.HasComponents; | ||
| @@ -46,6 +45,7 @@ public class ChatMessage<T extends Message> extends Component implements HasComp | ||
| private T message; | ||
| private boolean markdownEnabled; | ||
| private Div loader; | ||
| private MarkdownViewer markdownViewer; | ||
| /** | ||
| * Creates a new ChatMessage based on the supplied message without markdown support. | ||
| @@ -64,6 +64,14 @@ public ChatMessage(T message) { | ||
| */ | ||
| public ChatMessage(T message, boolean markdownEnabled) { | ||
| this.markdownEnabled = markdownEnabled; | ||
| loader = new Div(new Div(),new Div(), new Div(), new Div()); | ||
| loader.setClassName("lds-ellipsis"); | ||
| loader.setVisible(false); | ||
| this.add(loader); | ||
| if (markdownEnabled) { | ||
| markdownViewer = new MarkdownViewer(message.getContent()); | ||
| this.add(markdownViewer); | ||
| } | ||
| setMessage(message); | ||
| } | ||
| @@ -74,7 +82,7 @@ public ChatMessage(T message, boolean markdownEnabled) { | ||
| */ | ||
| public void setMessage(T message) { | ||
| this.message = message; | ||
| updateLoadingState(message); | ||
| updateMessage(message); | ||
| if (message.getName()!=null) { | ||
| this.setUserName(message.getName()); | ||
| if (message.getAvatar()!=null) { | ||
| @@ -87,21 +95,17 @@ public void setMessage(T message) { | ||
| } | ||
| } | ||
| private void updateLoadingState(T message) { | ||
| if (message.isLoading()) { | ||
| loader = new Div(new Div(),new Div(), new Div(), new Div()); | ||
| loader.setClassName("lds-ellipsis"); | ||
| this.add(loader); | ||
| } else { | ||
| if (loader!=null) { | ||
| this.remove(loader); | ||
| loader = null; | ||
| } | ||
| /** | ||
| * Updates the displayed message content and loading state. | ||
| * @param message | ||
| */ | ||
| private void updateMessage(T message) { | ||
| loader.setVisible(message.isLoading()); | ||
| if (!message.isLoading()) { | ||
| if (markdownEnabled) { | ||
| MarkdownViewer mdv = new MarkdownViewer(message.getContent()); | ||
| mdv.setDataColorMode(DataColorMode.LIGHT); | ||
javier-godoy marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| this.add(mdv); | ||
| markdownViewer.setContent(message.getContent()); | ||
| } else { | ||
| this.getElement().executeJs("[...this.childNodes].forEach(node => node.nodeType === 3 && this.removeChild(node));"); | ||
| this.getElement().executeJs("this.appendChild(document.createTextNode($0));", message.getContent()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,143 @@ | ||||||||||
| /*- | ||||||||||
| * #%L | ||||||||||
| * Chat Assistant Add-on | ||||||||||
| * %% | ||||||||||
| * Copyright (C) 2023 - 2025 Flowing Code | ||||||||||
| * %% | ||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||
| * You may obtain a copy of the License at | ||||||||||
| * | ||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| * | ||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| * See the License for the specific language governing permissions and | ||||||||||
| * limitations under the License. | ||||||||||
| * #L% | ||||||||||
| */ | ||||||||||
| package com.flowingcode.vaadin.addons.chatassistant; | ||||||||||
| import com.flowingcode.vaadin.addons.demo.DemoSource; | ||||||||||
| import com.flowingcode.vaadin.addons.demo.SourcePosition; | ||||||||||
| import com.google.common.base.Strings; | ||||||||||
| import com.vaadin.flow.component.UI; | ||||||||||
| import com.vaadin.flow.component.avatar.Avatar; | ||||||||||
| import com.vaadin.flow.component.button.Button; | ||||||||||
| import com.vaadin.flow.component.dependency.CssImport; | ||||||||||
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||||||||||
| import com.vaadin.flow.component.textfield.TextArea; | ||||||||||
| import com.vaadin.flow.data.renderer.ComponentRenderer; | ||||||||||
| import com.vaadin.flow.router.PageTitle; | ||||||||||
| import com.vaadin.flow.router.Route; | ||||||||||
| import java.time.LocalDateTime; | ||||||||||
| import java.util.Arrays; | ||||||||||
| import java.util.concurrent.CompletableFuture; | ||||||||||
| import java.util.concurrent.ThreadLocalRandom; | ||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||
| import java.util.stream.Stream; | ||||||||||
| @DemoSource(sourcePosition = SourcePosition.PRIMARY) | ||||||||||
| @PageTitle("Generative Answer Demo") | ||||||||||
| @SuppressWarnings("serial") | ||||||||||
| @Route(value = "chat-assistant/generative-demo", layout = ChatAssistantDemoView.class) | ||||||||||
| @CssImport("./styles/chat-assistant-styles-demo.css") | ||||||||||
| public class ChatAssistantGenerativeDemo extends VerticalLayout { | ||||||||||
| public ChatAssistantGenerativeDemo() { | ||||||||||
| String sampleText = "Hi, I'm an advanced language model. I'm here to help you demonstrate" | ||||||||||
| + " how a text-streaming chat component works in Vaadin. As you can see, each word appears" | ||||||||||
| + " with a slight pause, simulating the time it would take me to \"think\" and generate" | ||||||||||
| + " the next word. I hope this is useful for your demonstration!"; | ||||||||||
| ChatAssistant<CustomMessage> chatAssistant = new ChatAssistant<>(); | ||||||||||
| chatAssistant.setAvatarProvider(()->new Avatar("Chat Assistant","chatbot.png")); | ||||||||||
| TextArea message = new TextArea(); | ||||||||||
| message.setLabel("Enter a message from the assistant"); | ||||||||||
| message.setSizeFull(); | ||||||||||
| message.setValue(sampleText); | ||||||||||
| message.addKeyPressListener(ev->{ | ||||||||||
| if (Strings.isNullOrEmpty(chatAssistant.getWhoIsTyping())) { | ||||||||||
| chatAssistant.setWhoIsTyping("Assistant is generating an answer ..."); | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| message.addBlurListener(ev->chatAssistant.clearWhoIsTyping()); | ||||||||||
| chatAssistant.setMessagesRenderer(new ComponentRenderer<CustomChatMessage,CustomMessage>(m -> { | ||||||||||
| return new CustomChatMessage(m); | ||||||||||
| }, | ||||||||||
| (component, m) -> { | ||||||||||
| ((CustomChatMessage) component).setMessage(m); | ||||||||||
| return component; | ||||||||||
| })); | ||||||||||
| chatAssistant.setSubmitListener(se -> { | ||||||||||
| chatAssistant.sendMessage(CustomMessage.builder().messageTime(LocalDateTime.now()) | ||||||||||
| .name("User").content(se.getValue()).tagline("Generated by user").build()); | ||||||||||
| }); | ||||||||||
| Button chat = new Button("Chat"); | ||||||||||
| chat.addClickListener(ev -> { | ||||||||||
| CustomMessage m = CustomMessage.builder().content(message.getValue()).messageTime(LocalDateTime.now()) | ||||||||||
| .name("Assistant").avatar("chatbot.png").tagline("Generated by assistant").build(); | ||||||||||
| chatAssistant.sendMessage(m); | ||||||||||
| message.clear(); | ||||||||||
| }); | ||||||||||
| Button chatWithThinking = new Button("Chat With Generative Thinking"); | ||||||||||
| chatWithThinking.addClickListener(ev -> { | ||||||||||
| String messageToSend = message.getValue(); | ||||||||||
| CustomMessage delayedMessage = CustomMessage.builder().loading(true).content("") | ||||||||||
| .messageTime(LocalDateTime.now()) | ||||||||||
| .name("Assistant").avatar("chatbot.png").tagline("Generated by assistant").build(); | ||||||||||
| UI currentUI = UI.getCurrent(); | ||||||||||
| chatAssistant.sendMessage(delayedMessage); | ||||||||||
| CompletableFuture.runAsync(() -> { | ||||||||||
| try { | ||||||||||
| TimeUnit.MILLISECONDS.sleep(500); | ||||||||||
| currentUI.access(() -> { | ||||||||||
| delayedMessage.setLoading(false); | ||||||||||
| }); | ||||||||||
| streamWords(messageToSend) | ||||||||||
| .forEach(item -> { | ||||||||||
| currentUI.access(() -> { | ||||||||||
| delayedMessage.setContent(delayedMessage.getContent() + item); | ||||||||||
| chatAssistant.updateMessage(delayedMessage); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
Comment on lines
+102
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix double-spacing and inefficient string concatenation. Line 106 adds a space before each word, but Apply this diff to fix both issues: streamWords(messageToSend)
.forEach(item -> {
currentUI.access(() -> {
- delayedMessage.setContent(delayedMessage.getContent() + " " + item);+ delayedMessage.setContent(delayedMessage.getContent() + item);
chatAssistant.updateMessage(delayedMessage); });
});🤖 Prompt for AI Agents | ||||||||||
| } catch (InterruptedException e) { | ||||||||||
| Thread.currentThread().interrupt(); | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| message.clear(); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove redundant clear() call. Line 115 calls Apply this diff: });
- message.clear();
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents | ||||||||||
| }); | ||||||||||
| chatAssistant.sendMessage(CustomMessage.builder().content("Hello, I am here to assist you") | ||||||||||
| .messageTime(LocalDateTime.now()) | ||||||||||
| .name("Assistant").avatar("chatbot.png").tagline("Generated by assistant").build()); | ||||||||||
| add(message, chat, chatWithThinking, chatAssistant); | ||||||||||
| } | ||||||||||
| public Stream<String> streamWords(String fullText) { | ||||||||||
| if (fullText == null || fullText.isEmpty()) { | ||||||||||
| return Stream.empty(); | ||||||||||
| } | ||||||||||
| String[] words = fullText.split("\\s+"); | ||||||||||
| return Arrays.stream(words) | ||||||||||
| .map(word -> { | ||||||||||
| try { | ||||||||||
| long delay = ThreadLocalRandom.current().nextLong(50, 250); | ||||||||||
| Thread.sleep(delay); | ||||||||||
| } catch (InterruptedException e) { | ||||||||||
| Thread.currentThread().interrupt(); | ||||||||||
| } | ||||||||||
| return word + " "; | ||||||||||
| }); | ||||||||||
| } | ||||||||||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||||||||||
| } | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.