Live queries
A block can show data that changes after the agent wrote it, such as the state of a build. The agent declares a query of a source the host registered. The host reads it with the access of the chat that wrote the block, and the page shows the newest reading while the block is in view.
$branch = "main"
$runs = @Query("ci.runs", {branch: $branch, limit: 5})
<Segmented value={$branch}>
<Option value="main">main</Option>
<Option value="release">release</Option>
</Segmented>
<Table rows={$runs}>
<Column key="name" title="Run"/>
<Column key="ms" title="Time" as="duration"/>
</Table>Declaring a query
$name = @Query("source", {arguments}) is a declaration at the top of the block:
- The source is a string literal the host registered. An unknown source is
unknown_query. - The arguments are a record. They may read declared local state, so a control can change what is read, but never a query result.
- The source's schema must accept the arguments evaluated with the defaults, or the block gets
invalid_query. - A block declares at most
UI_HOST_LIMITS.queries(8) queries. - A query is read only.
$name holds the result. Until the first reading arrives, parts that read it draw their fallback.
Registering sources
UiCompileOptions.querySchemas maps each source name to its zod argument schema. With @adecore/agents, pass a ChatUiHost as ChatCoreOptions.intelligentUi and the sessions take the schemas from it:
import { z } from 'zod';
import type { ChatUiHost } from '@adecore/agents/chat';
const intelligentUi: ChatUiHost = {
// Called once per reply that may read a source or name a link; the result stays on the backend.
capture: async (info) => ({ projects: await projectsOf(info) }),
sources: {
'ci.runs': {
args: z.strictObject({ branch: z.string(), limit: z.number().int().positive().optional() }),
result: z.array(z.strictObject({ name: z.string(), ms: z.number() })),
authorize: async (info, access, args) => assertCanRead(info, access, args),
read: (info, args, signal) => fetchRuns(args, signal)
}
}
};projectsOf, assertCanRead and fetchRuns stand for your own code. capture records what the writing chat may read, once per reply. authorize runs before every read with that captured access and the chat's current info, and throws to refuse. read returns a result that must pass result.
To refuse with a code a client can word itself, throw a ChatUiRefusal(code, reason) from @adecore/agents/chat: throw new ChatUiRefusal('project-closed', 'The project is closed.'). From authorize the reading is refused, from read it is failed, and from ChatUiHost.link the link stays plain. Keep codes stable across versions, and reuse a host code below when one fits.
Reading
A client sends ui.query with the block's identity, the query name and its input values. The backend then:
- Finds the stored, completed block at that
revision, or answersblock-stale. - Checks the query is declared and its source registered.
- Validates the inputs with
uiValidatedStateand evaluates the arguments withuiQueryArguments(block, name, input, limits?, queries?)against the stored block.queriesholds the newest fresh reading of each query, so a value picked from options a query read passes. - Parses the arguments with the source's schema and authorizes them with the captured access.
- Answers from the cache within ten seconds of the same read, or reads.
uiValidatedState(block, input, queries?, limits?) accepts only values a person could have set on screen; see choices. uiQueryArguments throws a UiFailure (invalid_query, refused_binding, invalid_value) for anything else.
A reading is a ChatUiQueryReading: state (fresh, failed or refused), the value and a readId when fresh, readAt, and for a failure a reason with a stable code.
Limits
UI_HOST_LIMITS bounds the host reads per block. The agents host adds its own.
| Limit | Value | Where |
|---|---|---|
| Queries per block | 8 | UI_HOST_LIMITS.queries |
| Link targets per block | 64 | UI_HOST_LIMITS.links |
| One read per query and input | 10 s | UI_HOST_LIMITS.refreshMilliseconds |
| A changed input reads after | 250 ms | The agents host |
| Reads at once | 2 per chat, 16 in all | The agents host |
| A read times out after | 8 s | The agents host; the source is aborted |
| Result size | 64 KB | The agents host |
| Captured access | 32 KB | The agents host |
The backend reads only when a visible block asks.
Freshness
When a reply is final, the backend reads every query once and freezes those first readings, the link resolutions and a text fallback in the item's uiQueries. A page draws them at once, and a client that cannot read live still has them. uiQueryFallback(block, values) builds the fallback text from those first values, so the agent's history keeps what the person saw.
The page reads again while a block is visible and the app is in front: at most every ten seconds, and once more after an input rests for 300 ms. A failed or refused read keeps the last value until the next read succeeds: every node that reads the query dims, and the reason shows under the block. A read the host turned away (busy, refresh-limit) changes nothing while a value stands. A value that changed lights up once. uiNodeQueries(block) names the queries each written node reads, by node id, the queries of an enclosing Each included, so a client that draws natively can dim the same nodes. Choices stay closed while a block reads, and send the read ids of the values on screen; see choices.
Reason codes
A failed or refused reading, a plain link and a refused choice carry a stable code and a reason in words. A client words the codes it knows and shows the reason for others. uiReasonText in agents-react does this.
| Code | Meaning |
|---|---|
block-stale | The block changed since the client drew it |
query-undeclared | The block does not declare that query |
source-unregistered | The host has no such source |
access-unavailable | The writing chat's captured access is missing |
refresh-limit | The same query and input were read less than ten seconds ago |
busy | Too many reads are running |
timed-out | The source took longer than eight seconds |
result-too-large | The result is over 64 KB |
snapshot-too-large | The first readings were too large to freeze |
stale-read | A choice named a read the backend no longer holds for its inputs |
links-unsupported | The host resolves no links |
link-unsupported | The node is not a visible link of the block |
link-unchecked | The link has to be checked again |
unreadable | The page could not reach the backend |
Any other error thrown by authorize, read or link gets a fixed reason (This query could not be read.), because the reason is frozen into the record. Only a ChatUiRefusal chooses its words.
uiReasonText also words these host codes:
| Code | Meaning |
|---|---|
outside-project | The source or target lies outside the writer's project |
target-unavailable | The file, commit, diff or node a link names is gone |
no-unique-match | A name the source looks up matches nothing, or more than one |