English
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 display | Behaviour |
|---|---|
NullDisplay | Swallows everything. The default of a bare Agent() — programmatic agents stay silent. |
Display | Console UI (rich + prompt_toolkit): event rendering, /-completion, interruptible prompts. Used by setup_agent() and xun. |
WebDisplay | WebSocket-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:
| Payload | Emitted when |
|---|---|
ModelWorkingEvent | a model call starts (remaining_iterations) |
ModelMessageEvent | a model answer completes (content, reasoning, total_tokens, conclusive) |
ToolCallEvent / ToolResultEvent | a tool call is issued / returns |
UserMessageEvent, UserCommandEvent | input reaches the agent |
InfoEvent, WarningEvent, ErrorEvent, HTMLInfoEvent | messages for the user |
ConfirmEvent | a confirmation was answered (source: user or auto) |
AgentBindEvent / AgentUnbindEvent | display bind / unbind |
AgentRunningStartEvent / AgentRunningEndEvent | a run starts / ends |
ShowHistoryEvent, ShowToolsEvent, ShowExtensionsEvent, ShowHelpEvent | the matching slash command ran |
ModelMessageEvent.conclusive is false while the message is only an intermediate step that the model follows with tool calls. Tool events come in pairs: every ToolCallEvent that gets a response — even one whose arguments failed to parse — is followed by a ToolResultEvent whose is_error flag marks a failed call. Render the failure on the tool row instead of waiting for a separate WarningEvent.
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.display_info() / display_warning() / display_error() are the message helpers; each is interceptable by a before_display_*hook. The old info / warning / error names stay as aliases until v1.5.
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 inputinteractive_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.