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 (readable and assignable) 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 (timestamp, name, agent, span, payload). Payload classes live in xun.core.display_event and are re-exported by xun.core.display_abstract; the common ones:
| Payload | Emitted when |
|---|---|
ModelWorkingEvent | a model-call attempt starts (model_call_id, remaining_iterations); every retry emits a fresh id |
ModelMessageEvent | a model answer completes (same model_call_id, plus content, reasoning, total_tokens, conclusive) |
ToolCallEvent / ToolResultEvent | a tool call is issued / returns; join the pair on tool_call_id |
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 |
CustomEvent | an extension called display_event(CustomEvent(value=…)) |
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; a call cancelled mid-flight simply has no paired result.
Treat the payload set as additive: a display should ignore event names it does not know (CustomEvent relies on exactly that: the console display drops it, WebDisplay forwards it to the browser, where nothing renders it yet). 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 still work as aliases, but are scheduled for removal. ChoiceOutcome has no truth value: if agent.get_confirm(…) raises TypeError — test .choice.
Spans
Every event carries the SpanSnapshot it was emitted in: execution (one execute(), opened by execute() itself), step (one loop iteration) and tool_call (one call) — those two are opened by the execution loop — plus parent, the snapshot of the agent that spawned this one. Agent.inherit chains the child's span to its parent's, so an event stream can be rebuilt as a call tree without any extra bookkeeping. That is exactly what the web UI's trace graph and flame chart render; a custom display can group or colour events the same way. Reach the current context through agent.span.snapshot() when you emit events of your own.
Buffering and export
Every event is also recorded in a store, so late subscribers can catch up and a session can be re-rendered:
python
display.with_buffer_size(1000) # chainable; base default 0 keeps nothing
display.events(agent_id=…, offset=…, limit=…, from_latest=True) # oldest first, reversed by from_latest
display.event_count(agent_id=...)
display.clear_events()
display.render_history_as_html("Session") # the HTML file /render writesThe store is pluggable: display.event_store is an EventStore (thread-safe add / select / count / clear), and assigning the property replaces the backend — persistence or custom retention need no subclass of the display itself. The default is MemoryEventStore, an in-process ring buffer: 1000 events in Display, 5000 in WebDisplay, and 0 in the base class because payloads can be large (tool results, base64 images).
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.