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.
Sessions
The service mounts sessions, not displays. A session is anything that satisfies the WebSession protocol (it is structural — no base class required):
| Member | Role |
|---|---|
mount_path | Where it appears under {base_path}/session; "" is the root session. |
display | The WebDisplay served there — the service builds its sub-application. |
name | Label in the UI sidebar; may be changed at runtime (rename). |
close() | On shutdown the service calls this for factory-created (managed) sessions; for one you mount() yourself, closing is the caller's job. |
destroy() | Called when the user removes the session. |
save_meta() | Optional: flush in-memory changes (a rename) to storage. |
SimpleSession(mount_path, display, name="", release=None) is the ready-made implementation: close and destroy both call release() once — pass release=agent.finalize to tie an agent to its session. xun.session.DurableSession (a session persisted under $XUN_HOME/sessions/) satisfies the protocol as well, which is how xuns keeps sessions across restarts.
Mount and serve
python
from xun import WebDisplay, WebDisplayService, setup_agent
from xun.displays import SimpleSession
d1 = WebDisplay(expose_files=True)
a1 = setup_agent(display=d1, default_tools=True, workdir="workdir-a")
setup_agent(display=d1, default_tools=True, workdir="workdir-b") # one display, many agents
service = (
WebDisplayService(port=8877, token="secret")
.mount(SimpleSession("/display1", d1, release=a1.finalize)) # removing the session finalizes a1
.mount(SimpleSession("/display2", WebDisplay()))
)
service.start(blocking=False) # prints the access URLs, returns the server thread| Call | Notes |
|---|---|
WebDisplay(expose_files=False, event_buffer_size=5000, event_store=None) | expose_files enables the file browser and workspace hosting; event_store plugs in another EventStore backend (a database, for instance). |
WebDisplayService(host="localhost", port=18960, token="", base_path="", assets_dir=…, docs_dir=…, session_factory=None) | An empty token generates a random one. assets_dir / docs_dir point at the built UI and docs. |
mount(session, *, managed=False) | Chainable. The URL becomes {base_path}/session{session.mount_path} and the sidebar label is session.name (SimpleSession derives it from the last path segment). managed=True marks factory-created sessions, which the service also closes on shutdown. |
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. unmount calls the session's destroy(). |
access_url(path="") | Ready-to-open chat URL with ?session=…&token=…. |
A mount path may not contain a srv segment, mounts may not overlap (the root session / is exempt and acts as the catch-all under /session), and one 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, /logout | Token form (sets the xun_web_token cookie) and the POST that clears it | — |
/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 lets the browser create sessions at runtime. That is the session_factory argument: a callable returning a WebSession. The service calls it on POST /api/sessions/create (mounting the result as managed) and calls destroy() on removal, so the agent bound to the session is finalized with it. What else that cleanup covers is the factory's own business — only DurableSession erases its directory:
python
import secrets, tempfile
from xun import WebDisplay, WebDisplayService, setup_agent
from xun.displays import SimpleSession
def new_session():
workdir = tempfile.mkdtemp(suffix="-workspace")
display = WebDisplay(expose_files=True)
agent = setup_agent(display=display, default_tools=True, workdir=workdir)
return SimpleSession(f"/{secrets.token_hex(6)}", display, release=agent.finalize)
WebDisplayService(session_factory=new_session)As a convenience the factory may also return a context manager yielding (mount_path, display); the service wraps it in a SimpleSession that closes it on removal. Passing no session_factory keeps the mount set fixed and answers create / remove / rename with 405; the UI always keeps at least one session (removing the last one is a 409). 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. - While an agent runs, the UI can queue further instructions (steering); they are injected before its next execution step. 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.