|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Chat Assistant Add-on |
| 4 | + * %% |
| 5 | + * Copyright (C) 2023 - 2024 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.chatassistant; |
| 21 | + |
| 22 | +import com.flowingcode.vaadin.addons.chatassistant.model.Message; |
| 23 | +import com.flowingcode.vaadin.addons.demo.DemoSource; |
| 24 | +import com.flowingcode.vaadin.addons.demo.SourcePosition; |
| 25 | +import com.google.common.base.Strings; |
| 26 | +import com.vaadin.flow.component.UI; |
| 27 | +import com.vaadin.flow.component.button.Button; |
| 28 | +import com.vaadin.flow.component.dependency.CssImport; |
| 29 | +import com.vaadin.flow.component.html.Span; |
| 30 | +import com.vaadin.flow.component.icon.Icon; |
| 31 | +import com.vaadin.flow.component.icon.VaadinIcon; |
| 32 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
| 33 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 34 | +import com.vaadin.flow.component.textfield.TextArea; |
| 35 | +import com.vaadin.flow.router.PageTitle; |
| 36 | +import com.vaadin.flow.router.Route; |
| 37 | +import java.time.LocalDateTime; |
| 38 | +import java.util.Timer; |
| 39 | + |
| 40 | +@DemoSource(sourcePosition = SourcePosition.PRIMARY) |
| 41 | +@PageTitle("Markdown Demo") |
| 42 | +@SuppressWarnings("serial") |
| 43 | +@Route(value = "chat-assistant/markdown-demo", layout = ChatAssistantDemoView.class) |
| 44 | +@CssImport("./styles/chat-assistant-styles-demo.css") |
| 45 | +public class ChatAssistantMarkdownDemo extends VerticalLayout { |
| 46 | + |
| 47 | + public ChatAssistantMarkdownDemo() { |
| 48 | + ChatAssistant chatAssistant = new ChatAssistant(true); |
| 49 | + TextArea message = new TextArea(); |
| 50 | + message.setLabel("Enter a message from the assistant (try using Markdown)"); |
| 51 | + message.setSizeFull(); |
| 52 | + message.addKeyPressListener(ev->{ |
| 53 | + if (Strings.isNullOrEmpty(chatAssistant.getWhoIsTyping())) { |
| 54 | + chatAssistant.setWhoIsTyping("Assistant is generating an answer ..."); |
| 55 | + } |
| 56 | + }); |
| 57 | + message.addBlurListener(ev->chatAssistant.clearWhoIsTyping()); |
| 58 | + |
| 59 | + Button chat = new Button("Chat"); |
| 60 | + chat.addClickListener(ev -> { |
| 61 | + Message m = Message.builder().content(message.getValue()).messageTime(LocalDateTime.now()) |
| 62 | + .name("Assistant").avatar("chatbot.png").build(); |
| 63 | + |
| 64 | + chatAssistant.sendMessage(m); |
| 65 | + message.clear(); |
| 66 | + }); |
| 67 | + chatAssistant.sendMessage(Message.builder().content("**Hello, I am here to assist you**") |
| 68 | + .messageTime(LocalDateTime.now()) |
| 69 | + .name("Assistant").avatar("chatbot.png").build()); |
| 70 | + |
| 71 | + add(message, chat, chatAssistant); |
| 72 | + } |
| 73 | +} |
0 commit comments