English
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| Call | Notes |
|---|---|
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:
| URL | Contents | Auth |
|---|---|---|
/chat/ | The Vue chat UI; it connects to the session given by ?session= | token |
/session/<mount>/ | One display backend: WebSocket event stream and its APIs | token |
/session/<mount>/srv/<key>/ | Temporary static hosting of a workspace directory, started from the file browser (needs expose_files=True); expires after one hour | self-authenticating key |
/docs/ | This documentation, served from docs_dir when the packaged site is installed (GET/HEAD only) | none |
/login | Token form; sets the xun_web_token cookie | — |
/api/sessions… | List, create, remove, rename | token |
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, includingAgent.inherit(parent).initialize().- Every session keeps its own event history and file policy. The history is the display's
EventStore, replayed to clients throughGET /session/<mount>/api/eventsand 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
cancelmessage for that agent, which is chained to its sub-agents; the browser tab title carries a running / waiting marker while the tab is hidden.