Class GridAIController
- All Implemented Interfaces:
AIController
Grid<AIDataRow> with
database data via LLM tool calls. Attach it to an AIOrchestrator via
AIOrchestrator.Builder.withController(AIController) to expose its
tools to the LLM. Workflow instructions are delivered through the description
of the get_grid_instructions tool, which the LLM reads as part of the
tool manifest.
var grid = new Grid<AIDataRow>();
var controller = new GridAIController(grid, databaseProvider);
AIOrchestrator orchestrator = AIOrchestrator
.builder(llmProvider, systemPrompt).withController(controller)
.withMessageList(messageList).build();
The grid uses AIDataRow as its item type. Row instances are created
internally when query results are rendered and are not intended to be
constructed or inspected by application code.
The grid automatically creates columns from query results with:
- Lazy loading with SQL
LIMIT/OFFSET - Built-in renderers for dates and numbers
- Right-alignment for numeric columns
- Resizable, sortable columns with auto-width
- Column grouping from dot-separated aliases
State changes requested by the LLM are deferred and applied in
onResponse(Throwable) on the success path, avoiding partial state
and multiple redraws during a multi-tool LLM turn. The grid state is stored
directly on the Grid component, so it survives serialization.
If the LLM turn fails, onResponse(Throwable) fires with the cause —
pending changes are discarded and the grid keeps its last
successfully-rendered state.
Serialization: This controller is not serialized with the
orchestrator. After deserialization, create a new controller and restore
transient dependencies via reconnect(provider) .withController(controller).apply(). The grid
data can be captured via getState() and re-applied via
restoreState(GridState):
var controller = new GridAIController(grid, databaseProvider);
orchestrator.reconnect(llmProvider).withController(controller).apply();
if (savedState != null) {
controller.restoreState(savedState);
}
Register a listener via addStateChangeListener(SerializableConsumer)
to be notified when the grid state changes, for example to persist
getState() after each successful AI request.
- Author:
- Vaadin Ltd
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionGridAIController(Grid<AIDataRow> grid, DatabaseProvider databaseProvider) Creates a new grid AI controller. -
Method Summary
Modifier and TypeMethodDescriptionaddStateChangeListener(SerializableConsumer<GridState> listener) Adds a listener that is notified when the grid state changes after an AI request completes successfully.getState()Returns the current grid state for persistence, ornullif no data has been loaded yet.getTools()Returns the tools this controller exposes to the LLM.voidonResponse(Throwable error) Called on the UI thread under the session lock when the LLM stream has completed — either successfully or with an error.voidrestoreState(GridState state) Restores a previously saved grid state.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.ai.orchestrator.AIController
onRequest
-
Constructor Details
-
GridAIController
Creates a new grid AI controller.- Parameters:
grid- the grid to populate, notnulldatabaseProvider- the database provider for schema and query execution, notnull
-
-
Method Details
-
getTools
Description copied from interface:AIControllerReturns the tools this controller exposes to the LLM.- Specified by:
getToolsin interfaceAIController- Returns:
- list of tools, or empty list if controller provides no tools
-
onResponse
Description copied from interface:AIControllerCalled on the UI thread under the session lock when the LLM stream has completed — either successfully or with an error. Every turn fires this exactly once.On success
errorisnull; use the call to commit staged state or run deferred UI updates. On failureerrorcarries the cause (stream error, timeout, or any throw betweenAIController.onRequest()and the start of the stream); release per-turn state captured inonRequest(locks, pending writes, snapshots) and discard the staged work.The default does nothing. Exceptions thrown from the hook are caught and logged; Errors propagate.
- Specified by:
onResponsein interfaceAIController- Parameters:
error- the cause of failure, ornullon success
-
getState
Returns the current grid state for persistence, ornullif no data has been loaded yet.- Returns:
- the current state, or
null
-
restoreState
Restores a previously saved grid state. Re-executes the stored query and renders the grid.Does not fire state change listeners.
- Parameters:
state- the state to restore, notnull
-
addStateChangeListener
Adds a listener that is notified when the grid state changes after an AI request completes successfully. This is typically used to persist the grid state — for example by callinggetState()and saving the result so that it can be reapplied withrestoreState(GridState)after deserialization.The listener is not fired by
restoreState(GridState).- Parameters:
listener- the listener, notnull- Returns:
- a registration for removing the listener
-