Class GridAIController

java.lang.Object
com.vaadin.flow.component.ai.grid.GridAIController
All Implemented Interfaces:
AIController

public class GridAIController extends Object implements AIController
AI controller for populating a 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 onResponseComplete(), 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.

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 Details

    • GridAIController

      public GridAIController(Grid<AIDataRow> grid, DatabaseProvider databaseProvider)
      Creates a new grid AI controller.
      Parameters:
      grid - the grid to populate, not null
      databaseProvider - the database provider for schema and query execution, not null
  • Method Details

    • getTools

      public List<LLMProvider.ToolSpec> getTools()
      Description copied from interface: AIController
      Returns the tools this controller provides to the LLM.

      Tools are functions that the AI can call to perform actions. Each tool should have a clear name, description, and parameter schema.

      Specified by:
      getTools in interface AIController
      Returns:
      list of tools, or empty list if controller provides no tools
    • onResponseComplete

      public void onResponseComplete()
      Description copied from interface: AIController
      Called by the orchestrator when an LLM request cycle has completed.

      This method is invoked after all tool executions for a given user request have finished and the LLM has generated its final response. Controllers can use this callback to perform deferred operations, such as rendering UI updates or committing state changes.

      The orchestrator invokes this method on the UI thread under the session lock, so implementations may update UI components directly. Any exception thrown is caught by the orchestrator and reported to the user as a generic error message.

      Specified by:
      onResponseComplete in interface AIController
    • getState

      public GridState getState()
      Returns the current grid state for persistence, or null if no data has been loaded yet.
      Returns:
      the current state, or null
    • restoreState

      public void restoreState(GridState state)
      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, not null
    • addStateChangeListener

      public Registration addStateChangeListener(SerializableConsumer<GridState> listener)
      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 calling getState() and saving the result so that it can be reapplied with restoreState(GridState) after deserialization.

      The listener is not fired by restoreState(GridState).

      Parameters:
      listener - the listener, not null
      Returns:
      a registration for removing the listener