Class AIOrchestrator.Builder
- Enclosing class:
AIOrchestrator
AIOrchestrator instance.
The builder requires an LLMProvider and a system prompt. All
other settings are optional:
withInput(AIInput)orwithInput(MessageInput)– connects a text input component so that user submissions are automatically forwarded to the LLM. If omitted, useAIOrchestrator.prompt(String)to send messages programmatically.withMessageList(AIMessageList)orwithMessageList(MessageList)– connects a message list component to display the conversation. If omitted, responses are still streamed but not rendered.withFileReceiver(AIFileReceiver),withFileReceiver(UploadManager), orwithFileReceiver(Upload)– enables file upload support. Uploaded files are sent to the LLM as attachments with the next prompt.withTools(Object...)– registers objects containing vendor-specific tool-annotated methods (e.g. LangChain4j's@Toolor Spring AI's@Tool) that the LLM can invoke.withController(AIController)– sets the controller that provides framework-agnostic tools and lifecycle hooks.withUserName(String)– sets the display name for user messages (defaults to "You").withAssistantName(String)– sets the display name for assistant messages (defaults to "Assistant").withRequestListener(RequestListener)– registers a callback that fires on every prompt with the user message, the assigned message id, and any attachments.withResponseListener(ResponseListener)– registers a callback that fires after each exchange with the assistant's response text and an optional error (success and failure use the same listener), enabling persistence viaAIOrchestrator.getHistory()or follow-up actions.withHistory(List, Map)– restores a previously saved conversation history with attachments (fromAIOrchestrator.getHistory()).withMetadata(SerializableSupplier)– sets a supplier the orchestrator invokes on every turn to give the LLM free-form session context. Defaults to a current-date-and-time supplier so the LLM can interpret relative date/time references; passnullto disable, or a custom supplier to include tenant, locale, page state, or anything else worth keeping out of the system prompt.
Both Flow components (MessageInput, MessageList,
UploadManager, Upload) and custom implementations of the
AI interfaces (AIInput, AIMessageList,
AIFileReceiver) are accepted.
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the orchestrator.withAssistantName(String assistantName) Sets the display name for AI assistant messages in the message list.Sets a listener that is called when an attachment in the message list is clicked.withController(AIController controller) Sets the controller that provides framework-agnostic tools and lifecycle hooks to the orchestrator.withFileReceiver(AIFileReceiver fileReceiver) Sets the file receiver component for file uploads.withFileReceiver(com.vaadin.flow.component.upload.Upload upload) Sets the file receiver component using a Flow Upload component.withFileReceiver(com.vaadin.flow.component.upload.UploadManager uploadManager) Sets the file receiver component using a Flow UploadManager.withHistory(List<ChatMessage> history, Map<String, List<AIAttachment>> attachmentsByMessageId) Sets the conversation history and associated attachments to restore when the orchestrator is built.Sets the input component.withInput(com.vaadin.flow.component.messages.MessageInput messageInput) Sets the input component using a Flow MessageInput component.withMessageList(AIMessageList messageList) Sets the message list component.withMessageList(com.vaadin.flow.component.messages.MessageList messageList) Sets the message list component using a Flow MessageList component.withMetadata(com.vaadin.flow.function.SerializableSupplier<String> contextSupplier) Sets the supplier of free-form session context the LLM sees on every turn.withRequestListener(RequestListener listener) Sets a listener that is called on every prompt, just before the LLM stream opens.withResponseListener(ResponseListener listener) Sets a listener that is called once per turn when the assistant's stream has completed — successfully or with an error.Sets the objects containing vendor-specific tool-annotated methods that will be available to the LLM.withUserName(String userName) Sets the display name for user messages in the message list.
-
Method Details
-
withMessageList
Sets the message list component.- Parameters:
messageList- the message list- Returns:
- this builder
-
withMessageList
public AIOrchestrator.Builder withMessageList(com.vaadin.flow.component.messages.MessageList messageList) Sets the message list component using a Flow MessageList component.- Parameters:
messageList- the Flow MessageList component- Returns:
- this builder
-
withInput
Sets the input component.- Parameters:
input- the input component- Returns:
- this builder
-
withInput
public AIOrchestrator.Builder withInput(com.vaadin.flow.component.messages.MessageInput messageInput) Sets the input component using a Flow MessageInput component.- Parameters:
messageInput- the Flow MessageInput component- Returns:
- this builder
-
withFileReceiver
Sets the file receiver component for file uploads.- Parameters:
fileReceiver- the file receiver- Returns:
- this builder
-
withFileReceiver
public AIOrchestrator.Builder withFileReceiver(com.vaadin.flow.component.upload.UploadManager uploadManager) Sets the file receiver component using a Flow UploadManager. The provided manager should not have an UploadHandler set beforehand.- Parameters:
uploadManager- the Flow UploadManager- Returns:
- this builder
- Throws:
IllegalArgumentException- if theUploadManageralready has anUploadHandler
-
withFileReceiver
Sets the file receiver component using a Flow Upload component. The provided upload should not have an UploadHandler or a Receiver set beforehand.- Parameters:
upload- the Flow Upload component- Returns:
- this builder
- Throws:
IllegalArgumentException- if theUploadalready has anUploadHandleror aReceiver
-
withTools
Sets the objects containing vendor-specific tool-annotated methods that will be available to the LLM.For LangChain4j, use
@dev.langchain4j.agent.tool.Tool. For Spring AI, use@org.springframework.ai.tool.annotation.Tool.- Parameters:
tools- the objects containing tool methods- Returns:
- this builder
-
withController
Sets the controller that provides framework-agnostic tools and lifecycle hooks to the orchestrator. The controller's tools are collected before each LLM request.- Parameters:
controller- the controller to set, notnull- Returns:
- this builder
- Throws:
NullPointerException- if controller isnullIllegalArgumentException- if any tool name is invalid
-
withUserName
Sets the display name for user messages in the message list.This name is shown as the author of messages sent by the user. If not set, defaults to "You".
- Parameters:
userName- the display name for user messages, notnull- Returns:
- this builder
-
withAssistantName
Sets the display name for AI assistant messages in the message list.This name is shown as the author of messages generated by the AI. If not set, defaults to "Assistant".
- Parameters:
assistantName- the display name for AI messages, notnull- Returns:
- this builder
-
withRequestListener
Sets a listener that is called on every prompt, just before the LLM stream opens. The listener receives the user message, the assignedmessageId, and the attachments included with the message (empty list when none). Same lifecycle moment asAIController.onRequest().Typical use: persist attachment data in your own storage keyed by
messageId, so the same id can be used later to look the attachment up viaAttachmentClickListener.AttachmentClickEvent.getMessageId()or when restoring conversation history viawithHistory(List, Map).- Parameters:
listener- the listener to call on each prompt- Returns:
- this builder
-
withAttachmentClickListener
Sets a listener that is called when an attachment in the message list is clicked. The listener receives the message ID and attachment index, allowing you to retrieve attachment data from your own storage using the same message ID provided inRequestListener.RequestEvent.getMessageId().Note: This listener requires a message list to be configured via
withMessageList(MessageList). If no message list is set, the listener will have no effect.- Parameters:
listener- the listener to call on attachment click- Returns:
- this builder
-
withResponseListener
Sets a listener that is called once per turn when the assistant's stream has completed — successfully or with an error. This is the recommended hook for persisting conversation state (viaAIOrchestrator.getHistory()), triggering follow-up actions, or surfacing errors to the user. Same lifecycle moment asAIController.onResponse(Throwable).On success the response text may be empty if the model emitted only tool calls or otherwise stopped without producing visible content. Such turns are still successful exchanges; check
event.getResponse().isEmpty()if your listener should only react to text-bearing responses. Empty responses are not appended to the conversation history.On failure
event.getError()carries the cause and the response text is empty or a partial stream that was received before the failure.The listener is called from a background thread (Reactor scheduler). It is safe to perform blocking I/O (e.g. database writes) directly. To update Vaadin UI components from this listener, use
ui.access().The listener is not called when history is restored via
withHistory(List, Map).- Parameters:
listener- the listener to call after each exchange- Returns:
- this builder
-
withMetadata
public AIOrchestrator.Builder withMetadata(com.vaadin.flow.function.SerializableSupplier<String> contextSupplier) Sets the supplier of free-form session context the LLM sees on every turn. The supplier is invoked once per turn on the UI thread when the request is being built.The supplier may return any string the application wants the LLM to have on hand — current date and time, the active tenant, the user's locale, the page the user is on, feature flags. Compose multiple pieces of context with plain string concatenation.
If the supplier returns
nullor an empty/blank string, no context is added for that turn — useful for "context only when X" patterns. If the supplier throws, the turn is aborted via the normal error path: the assistant placeholder is updated to a generic error message,AIController.onResponse(Throwable)fires with the thrown exception, and the exception propagates to the caller of the prompt entry point.Passing
nulldisables session context entirely, including the built-in default. By default, the orchestrator installs a supplier that yields the current date and time so the LLM can interpret relative date/time references ("show me sales from the past two months") without having to guess.The supplier runs on the UI thread, so it can read
UI.getCurrent()and session-scoped state.- Parameters:
contextSupplier- supplier of the per-turn context string, ornullto disable session context entirely- Returns:
- this builder
-
withHistory
public AIOrchestrator.Builder withHistory(List<ChatMessage> history, Map<String, List<AIAttachment>> attachmentsByMessageId) Sets the conversation history and associated attachments to restore when the orchestrator is built. This restores the LLM provider's conversation context (including multimodal content), the message list UI with attachment thumbnails, and the internal message ID mappings for attachment click handling.The attachment map is keyed by
ChatMessage.messageId()and contains the list ofAIAttachmentobjects for each message. Messages whose IDs are not in the map are restored as text-only. The attachments are used once during initialization and are not retained by the orchestrator. PassCollections.emptyMap()if there are no attachments to restore.- Parameters:
history- the conversation history to restore, notnullattachmentsByMessageId- a map from message ID to attachment list, notnull- Returns:
- this builder
-
build
Builds the orchestrator.- Returns:
- the configured orchestrator
-