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
2 changes: 1 addition & 1 deletion articles/flow/ai-support/ai-powered-form.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Only the value is hidden. A field's description, type, and option or `enum` labe

== Field Locking During a Fill Turn

While a fill is in progress, every field the AI can write -- visible, enabled, and not read-only -- shows an "AI is working" shimmer and is guarded against user edits, so the user cannot type into a field the AI is about to overwrite. The guard is applied on the client only: the field's server-side read-only state is never changed, so it doesn't affect what the LLM sees or writes, and a field's application-set read-only state is left untouched. Fields the application had already disabled or set read-only stay as they were, and fields excluded with [methodname]`ignoreField()` stay editable throughout. The working state clears automatically when the turn ends, whether it succeeded or failed. A field switched to read-only on the server mid-turn, for example by a [interfacename]`ValueChangeListener` reacting to one of the model's writes, stays read-only when the guard is released.
While a fill is in progress, every field the AI can write -- visible, enabled, and not read-only -- shows an "AI is working" shimmer and is guarded against user edits, so the user cannot type into a field the AI is about to overwrite. A field that one of the model's writes makes writable gets the same shimmer and guard for the rest of the turn. This happens, for example, when a [interfacename]`ValueChangeListener` enables or reveals the field in reaction to the write. The guard is applied on the client only: the field's server-side read-only state is never changed, so it doesn't affect what the LLM sees or writes, and a field's application-set read-only state is left untouched. Fields the application had already disabled or set read-only stay as they were, and fields excluded with [methodname]`ignoreField()` stay editable throughout. The working state clears automatically when the turn ends, whether it succeeded or failed. A field switched to read-only on the server mid-turn, for example by a [interfacename]`ValueChangeListener` reacting to one of the model's writes, stays read-only when the guard is released.


[[marking-ai-changes]]
Expand Down
12 changes: 6 additions & 6 deletions articles/flow/ai-support/conversation-history.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ The orchestrator tracks conversation history internally. Use [methodname]`getHis

== Persisting History

Use [classname]`ResponseListener` to persist conversation state after each exchange:
Store the orchestrator in a field of the view. Use a [classname]`ResponseListener` to capture the history after each successful exchange and queue the snapshot for a background writer.

[source,java]
----
var orchestrator = AiOrchestrator
this.orchestrator = AiOrchestrator
.builder(provider, systemPrompt)
.withMessageList(messageList)
.withInput(messageInput)
.withResponseListener(event -> {
if (event.getError().isPresent()) {
return;
}
var history = orchestrator.getHistory();
saveToDatabase(sessionId, history);
var history = this.orchestrator.getHistory();
historyWriter().execute(() -> saveToDatabase(sessionId, history));
})
.build();
----

[methodname]`saveToDatabase` is a placeholder for application code. Write the [classname]`ChatMessage` list to whichever storage the application uses, so the history can be loaded again later.
In this example, [methodname]`historyWriter()` returns a background executor that the application manages. Make sure that it runs the writes of a conversation in the order they were submitted, as a single-threaded executor does. Otherwise, an older snapshot can overwrite a newer one. Implement [methodname]`saveToDatabase()` to store the snapshot.

The listener fires once per turn for both successful and failed exchanges -- check [methodname]`event.getError()` to tell them apart. It is not called when history is restored via [methodname]`withHistory()`, and empty responses (turns where the model emitted only tool calls or no visible content) are not appended to history. The event also carries the [since:com.vaadin:vaadin@V25.3]#finish reason and token usage# of the turn -- see <<listeners#response-metadata,Response Metadata>>.

.UI Updates from ResponseListener
[IMPORTANT]
With a streaming provider, or when <<llm-providers#background-execution,background execution>> is enabled, the listener runs on a background thread, where blocking I/O (such as database writes) is safe. With a synchronous provider in the default execution mode, the whole turn -- this listener included -- runs in the request that triggered the prompt, so blocking work extends that request. To update Vaadin UI components from this callback, wrap the update in `ui.access()`.
While the UI is attached, the <<listeners#response-listener,response listener>> holds the session lock, so it can update UI components directly. Capture the snapshot in the listener and write it to storage on a background thread. If the UI detaches before the callback runs, the listener can still save the history.


== Restoring History
Expand Down
3 changes: 2 additions & 1 deletion articles/flow/ai-support/listeners.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The event carries:
The listener runs on the UI thread under the session lock: components can be updated directly, and long-running work should be offloaded to a worker thread.


[[response-listener]]
== Response Listener

The response listener is called when the turn ends -- normally when the assistant's response has completed, successfully or with an error, but also when the turn fails before a response ever starts. It fires at most once per prompt, and not at all for a prompt rejected by the interceptor, for a postponed prompt abandoned because its UI was detached, or for history restored via [methodname]`withHistory()`.
Expand All @@ -56,7 +57,7 @@ A typical use is persisting the conversation after each exchange -- see <<conver

.UI Updates from ResponseListener
[IMPORTANT]
The listener is called from whichever thread ends the turn. With a streaming provider, or when <<llm-providers#background-execution,background execution>> is enabled, that's a background thread, where blocking I/O (such as database writes) is safe. With a synchronous provider in the default execution mode, the whole turn -- this listener included -- runs in the request that triggered the prompt. To update Vaadin UI components from this callback, wrap the update in `ui.access()`.
When the UI is attached, the orchestrator calls the listener under the session lock, even with streaming or <<llm-providers#background-execution,background execution>>. If a controller is registered, its [methodname]`onResponse()` runs first. The listener can update UI components directly. Run blocking work such as database writes on a separate thread to keep the session responsive. If the UI detaches before the callback runs, the orchestrator skips the controller hook and calls the listener without a session.


[[response-metadata]]
Expand Down
6 changes: 4 additions & 2 deletions articles/flow/ai-support/llm-providers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ Everything that the orchestrator does before it hands the prompt to the provider
* The request listener
* The session context supplier

The model calls, every tool execution, and the [classname]`ResponseListener` run on a background thread, as do the retrieval and the input guardrails of a LangChain4j customizer. There, [methodname]`UI.getCurrent()` and other Vaadin thread locals return `null` and components must not be touched directly. Thread-bound framework state, such as Spring Security's [classname]`SecurityContext`, is absent there for the same reason.
The model calls and every tool execution run on a background thread, as do the retrieval and the input guardrails of a LangChain4j customizer. There, [methodname]`UI.getCurrent()` and other Vaadin thread locals return `null` and components must not be touched directly. Thread-bound framework state, such as Spring Security's [classname]`SecurityContext`, is absent there for the same reason.

Wrap component access in `ui.access()`, or capture what a tool needs in [methodname]`AiController.onRequest()` while the request thread is still current -- see <<tool-calling#,Tool Calling & Programmatic Prompts>> and <<controllers#,Controllers>>. The built-in controllers already handle this. [methodname]`AiController.onResponse()` is the exception: the orchestrator calls it through `ui.access()`, so it can update components directly.
Wrap component access in `ui.access()`, or capture what a tool needs in [methodname]`AiController.onRequest()` while the request thread is still current -- see <<tool-calling#,Tool Calling & Programmatic Prompts>> and <<controllers#,Controllers>>. The built-in controllers already handle this.

While the UI is attached, the orchestrator calls [methodname]`AiController.onResponse()` and then the <<listeners#response-listener,response listener>> under the session lock. Both can update components directly. Move blocking work to a worker thread to keep the session responsive.

.One Prompt at a Time
[NOTE]
Expand Down
Loading