Implementation:AIScientists Dev WorldSeed server routes shared
| Knowledge Sources | |
|---|---|
| Domains | Server, Gateway |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete shared helpers for WorldSeed server route modules — token management, gateway payload assembly, gateway process control, and engine access utilities provided by the WorldSeed library.
Description
The _shared module centralizes cross-route logic. `init_token_state`/`clear_tokens` manage per-app token maps. `build_gateway_payload(engine, run_id)` is the single source of truth for the gateway world payload — it assembles agents, per-agent action catalogs, stripped configs (YAML-dumped), and wake-summary config. `_eng`/`_require_agent`/`_resolve_agent` provide engine access and agent validation. `_spawn_gateway`/`_kill_gateway`/`_gateway_status` manage the OpenClaw gateway subprocess lifecycle, including writing openclaw config for the current port and killing external gateways. `maybe_auto_start_ticks` starts the tick loop when all preset agents have registered. `build_intro_data` assembles intro page data from a live engine.
Usage
Import these helpers from route modules to avoid duplication — the gateway payload is used by both `auth_ok` (websocket) and `run_switched` (world route), ensuring identical payload structure. Gateway spawn/kill is called during world start/stop/switch.
Code Reference
Source Location
- Repository: WorldSeed
- File: src/worldseed/server/routes/_shared.py
Signature
def init_token_state(app: FastAPI) -> None: ...
def clear_tokens(app: FastAPI) -> None: ...
def build_gateway_payload(engine: WorldEngine, run_id: str) -> dict[str, Any]: ...
def _eng(app: FastAPI) -> WorldEngine: ...
def _require_agent(engine: WorldEngine, agent_id: str) -> None: ...
def _resolve_agent(token: str | None, agent_id: str | None, tkns: dict[str, str]) -> str: ...
def _spawn_gateway(app: FastAPI) -> None: ...
def _kill_gateway(app: FastAPI) -> None: ...
def _gateway_status(app: FastAPI, ws_manager: ConnectionManager) -> dict[str, Any]: ...
async def maybe_auto_start_ticks(engine: WorldEngine, tick_runner: Any, agents_ready: set[str]) -> bool: ...
def build_intro_data(engine: WorldEngine) -> dict[str, Any]: ...
Import
from worldseed.server.routes._shared import (
init_token_state, clear_tokens, build_gateway_payload,
_eng, _require_agent, _resolve_agent,
_spawn_gateway, _kill_gateway, _gateway_status,
maybe_auto_start_ticks, build_intro_data,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| app | FastAPI | Yes | FastAPI app instance with state.engine, state.tokens |
| engine | WorldEngine | Yes (payload/intro) | Active world engine |
| run_id | str | Yes (payload) | Current run identifier |
| tick_runner | Any | Yes (auto_start) | Background tick loop runner |
| agents_ready | set[str] | Yes (auto_start) | Set of registered agent IDs |
Outputs
| Name | Type | Description |
|---|---|---|
| build_gateway_payload returns | dict[str, Any] | Gateway payload with agents, catalogs, configs |
| maybe_auto_start_ticks returns | bool | True if ticks were started |
| build_intro_data returns | dict[str, Any] | Intro page data with scene, entities, agents |
| _gateway_status returns | dict[str, Any] | Gateway process + connection status |
Usage Examples
from worldseed.server.routes._shared import build_gateway_payload, maybe_auto_start_ticks
# Assemble gateway payload for websocket auth
payload = build_gateway_payload(engine, run_id)
# payload = {"scene": "teahouse", "agents": [...], "action_catalog": {...}, ...}
# Auto-start ticks when all agents register
started = await maybe_auto_start_ticks(engine, tick_runner, agents_ready)
if started:
print("All agents ready — ticks auto-started")
Related Pages
Implements Principle
- Principle:AIScientists_Dev_WorldSeed_Director_Signal_API
- Principle:AIScientists_Dev_WorldSeed_Server_Launch