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 (readable and assignable) 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 attempt starts (model_call_id, remaining_iterations); every retry emits a fresh id
ModelMessageEventa model answer completes (same model_call_id, plus content, reasoning, total_tokens, conclusive)
ToolCallEvent / ToolResultEventa tool call is issued / returns; join the pair on tool_call_id
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

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. 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. ChoiceOutcome has no truth value: if agent.get_confirm(…) raises TypeError — test .choice.

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 writes

The 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 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.3 (source 5f35bd8, 2026-10-18) documentation, generated from the source tree.