Skip to content
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 @@ -15,11 +15,13 @@
*/
package com.vaadin.flow.component.ai.tests;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vaadin.flow.component.ai.common.AIAttachment;
import com.vaadin.flow.component.ai.common.ChatMessage;
import com.vaadin.flow.component.ai.orchestrator.AIOrchestrator;
import com.vaadin.flow.component.ai.provider.LLMProvider;
import com.vaadin.flow.component.html.Div;
Expand All @@ -34,6 +36,7 @@
import com.vaadin.flow.component.upload.UploadFileListVariant;
import com.vaadin.flow.component.upload.UploadManager;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.VaadinSession;

import reactor.core.publisher.Flux;

Expand All @@ -45,14 +48,17 @@
@Route("vaadin-ai/orchestrator")
public class AIOrchestratorPage extends UploadDropZone {

private final AIOrchestrator orchestrator;
private static final String HISTORY_SESSION_KEY = "ai-orchestrator-history";

private AIOrchestrator orchestrator;

// Attachment storage keyed by message ID
private final Map<String, List<AIAttachment>> attachmentStorage = new HashMap<>();

// Displays info about the last clicked attachment
private final Span clickedAttachmentInfo = new Span();

@SuppressWarnings("unchecked")
public AIOrchestratorPage() {
setHeightFull();

Expand All @@ -74,7 +80,7 @@ public AIOrchestratorPage() {

clickedAttachmentInfo.setId("clicked-attachment-info");

orchestrator = AIOrchestrator.builder(new EchoLLMProvider(), null)
var builder = AIOrchestrator.builder(new EchoLLMProvider(), null)
.withMessageList(messageList).withInput(messageInput)
.withFileReceiver(uploadManager)
.withAttachmentSubmitListener(event -> {
Expand All @@ -89,7 +95,18 @@ public AIOrchestratorPage() {
clickedAttachmentInfo.setText(attachment.name() + " | "
+ attachment.mimeType());
}
}).build();
})
.withResponseCompleteListener(event -> VaadinSession
.getCurrent().setAttribute(HISTORY_SESSION_KEY,
orchestrator.getHistory()));

var savedHistory = (List<ChatMessage>) VaadinSession.getCurrent()
.getAttribute(HISTORY_SESSION_KEY);
if (savedHistory != null) {
builder.withHistory(savedHistory, Collections.emptyMap());
Copy link
Member

Choose a reason for hiding this comment

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

Instead of Collections.emptyMap() (= not restoring attachments with the history), also store the attachmentStorage to VaadinSession so they get restored on refresh

}

orchestrator = builder.build();

var promptButton = new NativeButton("Send Hello",
e -> orchestrator.prompt("Hello from button"));
Expand Down Expand Up @@ -117,5 +134,11 @@ public Flux<String> stream(LLMRequest request) {
var response = "Echo: " + request.userMessage();
return Flux.fromArray(response.split(" ")).map(word -> word + " ");
}

@Override
public void setHistory(List<ChatMessage> history,
Map<String, List<AIAttachment>> attachmentsByMessageId) {
// No-op for testing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ public void uploadFile_submitMessage_attachmentRenderedInMessage()
Assert.assertNotNull(userMessage.getAttachmentByName("test-file.txt"));
}

@Test
public void submitMessage_refreshPage_historyRestored() {
messageInput.submit("Hello");
waitUntil(driver -> getMessageCount() >= 2, 5);
Assert.assertEquals(2, getMessageCount());

// Refresh the page - history should be auto-restored from session
open();
messageList = $(MessageListElement.class).single();

waitUntil(driver -> getMessageCount() >= 2, 5);
var messages = messageList.getMessageElements();
Assert.assertEquals(2, messages.size());
Assert.assertTrue(messages.get(0).getText().contains("Hello"));
Assert.assertTrue(messages.get(1).getText().contains("Echo: Hello"));
}

@Test
public void uploadFile_submitMessage_clickAttachment_infoDisplayed()
throws Exception {
Expand Down