Skip to content

Web Display Mounting ​

WebDisplay is a display whose event stream goes to a browser over WebSocket. On its own it is only a FastAPI sub-application; WebDisplayService is the server that hosts one or more of them behind a single token.

Mount and serve ​

python
from xun import WebDisplay, WebDisplayService, setup_agent

d1 = WebDisplay(expose_files=True)
setup_agent(display=d1, default_tools=True, workdir="workdir-a")
setup_agent(display=d1, default_tools=True, workdir="workdir-b")   # one display, many agents

d2 = WebDisplay()
setup_agent(display=d2, default_tools=True, workdir="workdir-c")

service = (
    WebDisplayService(port=8877, token="secret")
    .mount("/display1", d1)
    .mount("/display2", d2)
)
service.start(blocking=False)     # prints the access URLs, returns the server thread
CallNotes
WebDisplay(expose_files=False, event_buffer_size=5000)expose_files enables the file browser and workspace hosting for this display.
WebDisplayService(host="localhost", port=18960, token="", base_path="", assets_dir=…, docs_dir=…, session_manager=None)An empty token generates a random one. assets_dir / docs_dir point at the built UI and docs.
mount(path, display, *, name=None)Chainable. The URL becomes {base_path}/session{path}; the session name defaults to the last path segment.
start(*, blocking=False)Keyword-only. Binds the socket (so port=0 works), starts uvicorn in a daemon thread and prints access_url() for every session. Requires at least one mount.
stop(), unmount(path), rename(path, name), list_sessions()Lifecycle and bookkeeping.
access_url(path="")Ready-to-open chat URL with ?session=…&token=….

Mount paths must not overlap (the root session / is exempt and acts as the catch-all under /session), must not contain a srv segment, and a WebDisplay can be mounted only once. mount() before start(); mounts added afterwards are attached to the running loop.

URL layout ​

With base_path empty:

URLContentsAuth
/chat/The Vue chat UI; it connects to the session given by ?session=token
/session/<mount>/One display backend: WebSocket event stream and its APIstoken
/session/<mount>/srv/<key>/Temporary static hosting of a workspace directory, started from the file browser (needs expose_files=True); expires after one hourself-authenticating key
/docs/This documentation, served from docs_dir when the packaged site is installed (GET/HEAD only)none
/loginToken form; sets the xun_web_token cookie—
/api/sessions…List, create, remove, renametoken

Token presentation: Authorization: Bearer <token>, the cookie, or ?token= on the chat URL. Requests to / redirect to /chat/.

Dynamic sessions ​

xuns allows the browser to create sessions at runtime. That is the session_manager argument: a callable returning a context manager that yields (mount_path, display). The service calls it on POST /api/sessions/create and closes it on removal, so the agent is finalized and its temporary workspace deleted with the session:

python
@contextmanager
def new_session():
    mount_path = f"/{uuid.uuid4()}"          # unique; the service keys sessions by it
    with tempfile.TemporaryDirectory(suffix="-workspace") as workdir:
        display = WebDisplay(expose_files=True)
        agent = setup_agent(display=display, default_tools=True, workdir=workdir)
        try:
            yield mount_path, display        # the service owns the mount until removal
        finally:
            agent.finalize()

WebDisplayService(session_manager=new_session)

Passing no session_manager (the default when you use WebDisplayService directly) keeps the mount set fixed and answers create/remove/rename with 405. The CLI form is xuns [workdir] with --host/--port/--token/--base-path/--manage-sessions/--initial-agent — see Session Entrypoints.

Notes ​

  • setup_agent(display=web_display, …) is the shortest way to attach an agent; anything that binds a display works, including Agent.inherit(parent).initialize().
  • Every session keeps its own event history and file policy. The history is the display's EventStore, replayed to clients through GET /session/<mount>/api/events and dropped by /clear. Agents bound to one display share that stream and the top-bar agent selector; the sidebar lists sessions.
  • The stop button sends a cancel message for that agent, which is chained to its sub-agents; the browser tab title carries a running / waiting marker while the tab is hidden.

Xun 1.4.3 (source 5f35bd8, 2026-10-18) documentation, generated from the source tree.