I have a simple MainView with MessageInput and MessageList. I wanted to show the user messages immediately on the view and then the assistant message once I get the response from AI service.
I have @Push on the Application class level.
But I am not able to make the User message appear immediately. What I am doing wrong?
@Route("")
public class MainView extends VerticalLayout {
private final BedrockService bedrockService;
private List<MessageListItem> messageListItemList = new ArrayList<>();
public MainView(BedrockService bedrockService) {
this.bedrockService = bedrockService;
MessageList list = new MessageList();
Instant instant = LocalDateTime.now()
.toInstant(ZoneOffset.UTC);
MessageInput input = new MessageInput();
input.addSubmitListener(submitEvent -> {
Thread.ofVirtual()
.start(UI.getCurrent()
.accessLater(() -> {
MessageListItem userMsg = new MessageListItem(submitEvent.getValue(),
instant, "You");
messageListItemList.add(userMsg);
list.setItems(messageListItemList);
}, null));
Thread.ofVirtual()
.start(UI.getCurrent()
.accessLater(() -> {
MessageListItem assistantMsg = new MessageListItem(bedrockService.callBedrockKB(submitEvent.getValue()),
instant, "Assistant");
messageListItemList.add(assistantMsg);
list.setItems(messageListItemList);
}, null));
});
add(list, input);
}
}