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, 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 (starts an agent-driven preview server); expires after one hourself-authenticating key
/docs/This documentation, straight from the packaged sitenone
/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
def new_session():
    stack = ExitStack()
    workdir = stack.enter_context(tempfile.TemporaryDirectory(suffix="-workspace"))
    display = stack.enter_context(_session_display(Path(workdir)))
    return mount_path, display            # the service owns the return value

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.

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; agents bound to the same display share one sidebar listing them all.
  • 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.2dev (unreleased; source 51d23f3, 2026-10-16) documentation, generated from the source tree.