Skip to content

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.

ui
$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:

ts
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:

  1. Finds the stored, completed block at that revision, or answers block-stale.
  2. Checks the query is declared and its source registered.
  3. Validates the inputs with uiValidatedState and evaluates the arguments with uiQueryArguments(block, name, input, limits?, queries?) against the stored block. queries holds the newest fresh reading of each query, so a value picked from options a query read passes.
  4. Parses the arguments with the source's schema and authorizes them with the captured access.
  5. 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.

LimitValueWhere
Queries per block8UI_HOST_LIMITS.queries
Link targets per block64UI_HOST_LIMITS.links
One read per query and input10 sUI_HOST_LIMITS.refreshMilliseconds
A changed input reads after250 msThe agents host
Reads at once2 per chat, 16 in allThe agents host
A read times out after8 sThe agents host; the source is aborted
Result size64 KBThe agents host
Captured access32 KBThe 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.

CodeMeaning
block-staleThe block changed since the client drew it
query-undeclaredThe block does not declare that query
source-unregisteredThe host has no such source
access-unavailableThe writing chat's captured access is missing
refresh-limitThe same query and input were read less than ten seconds ago
busyToo many reads are running
timed-outThe source took longer than eight seconds
result-too-largeThe result is over 64 KB
snapshot-too-largeThe first readings were too large to freeze
stale-readA choice named a read the backend no longer holds for its inputs
links-unsupportedThe host resolves no links
link-unsupportedThe node is not a visible link of the block
link-uncheckedThe link has to be checked again
unreadableThe 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:

CodeMeaning
outside-projectThe source or target lies outside the writer's project
target-unavailableThe file, commit, diff or node a link names is gone
no-unique-matchA name the source looks up matches nothing, or more than one

Licenses and third-party notices are listed in each package.