Skip to content

Display Extension ​

The agent never prints. It emits typed events to a DisplayAbstract, and the display decides what to render, store or forward. Swapping a terminal UI for a web UI is therefore a constructor argument, not a refactor.

The interface ​

Two methods are abstract; everything else (binding, buffering, HTML export) comes from DisplayAbstract / DisplayEventStoreMixin.

python
from xun import DisplayAbstract

class RawDisplay(DisplayAbstract):
    def on_event(self, event):
        print(type(event.payload).__name__, event.payload)

    def get_choice(self, request: DisplayAbstract.ChoiceRequest) -> str:
        return request.default or request.choices[0]

A display may be bound to several agents at once; display.agents maps identifier → agent, and every event carries the emitting agent's AgentInfo (name, identifier, workdir).

Built-in displayBehaviour
NullDisplaySwallows everything. The default of a bare Agent() — programmatic agents stay silent.
DisplayConsole UI (rich + prompt_toolkit): event rendering, /-completion, interruptible prompts. Used by setup_agent() and xun.
WebDisplayWebSocket-backed chat UI. See Web Display Mounting.

Events an agent emits ​

on_event receives a DisplayEvent envelope (name, agent, payload). Payload classes are re-exported from xun.display_abstract; the common ones:

PayloadEmitted when
ModelWorkingEventa model call starts (remaining_iterations)
ModelMessageEventa model answer completes (content, reasoning, total_tokens)
ToolCallEvent / ToolResultEventa tool call is issued / returns
UserMessageEvent, UserCommandEventinput reaches the agent
InfoEvent, WarningEvent, ErrorEvent, HTMLInfoEventmessages for the user
ConfirmEventa confirmation was answered (source: user or auto)
AgentBindEvent / AgentUnbindEventdisplay bind / unbind
AgentRunningStartEvent / AgentRunningEndEventa run starts / ends
ShowHistoryEvent, ShowToolsEvent, ShowExtensionsEvent, ShowHelpEventthe matching slash command ran

Treat the payload set as additive: a display should ignore event names it does not know. Display.get_choice is the only pull direction; ask the user through the agent instead of the display:

python
outcome = agent.get_choice("Which model?", ["a", "b"], default="a")   # ChoiceOutcome[str]
ok = agent.get_confirm("Overwrite config.json?")                      # ChoiceOutcome[bool]

ChoiceOutcome carries (choice, source) where source is "user" or "auto" — with auto_confirm enabled, the default is returned without prompting (pass skip_auto_confirm=True to force a prompt). agent.info() / warning() / error() are the message helpers; each is interceptable by a before_display_*hooks.

Buffering and export ​

Every event is also recorded in a ring buffer, so late subscribers can catch up and a session can be re-rendered:

python
display.with_buffer_size(1000)     # chainable; 0 (the default) keeps nothing
display.events()                   # buffered events, oldest first
display.events(agent_id=...)       # only one agent's
display.clear_events()

Display uses 1000 events and WebDisplay 5000 internally; the base default is 0 because payloads can be large (tool results, base64 images). The same buffer feeds /render, which writes a standalone HTML transcript.

Interaction with a custom input loop ​

A console-style display also supplies input. Display.input() renders the agent-labelled prompt and file/command completion; set_command_provider() feeds the completion list:

python
from xun import Agent, Display, interactive_session

display = Display()
agent = Agent(display=display).initialize()
interactive_session(agent)     # wires slash-command completion and reads input

interactive_session(agent, task="…") is the reusable loop: it parses input (/command, [image:…], plain text), executes it, and on Ctrl-C drops the trailing user message so the turn can be retried.

Xun 1.4.1 (unreleased; package 1.4.0, source 9e8246e, 2026-10-14) documentation, generated from the source tree.