home..

Trivai 003

I’m happy to announce the general availability of trivai 0.0.3, which improves the performance of the UI and makes things a bit prettier. Nothing on the AI or generation side has changed yet.

The really interesting work from this sprint however is the server-side-originated events that facilitate the sharing of information and state between clients. For example, as category completions are posted to the database, all clients will get a refresh event, triggering a reload of the available categories.

async def delayed_event(
    delay: float,
    state: State,
    name: str,
    payload: t.Optional[t.Dict[str, t.Any]] = None,
):
    """Submit a "frontend" event from the server for a particular client state."""
    event_dict = dict(
        token=state.get_token(),
        name=name,
        router_data=state.router_data,
    )
    if payload:
        event_dict["payload"] = payload
    sid = state.get_sid()
    await asyncio.sleep(delay)
    event_ns = _APP.sio.namespace_handlers["/event"]
    try:
        await event_ns.on_event(sid=sid, data=json.dumps(event_dict))
    except Exception as e:
        print(f"Error processing delayed event: {e}")

(Note: _APP is set to the pynecone.app.App instance)

As far as I can tell, the event dispatching aspect of pynecone isn’t documented yet, but tapping into the pynecone.app.EventNamespace seems to the key for processing server originated events and communicating between clients, which will be needed down the road for “buzzer”-style play where each player uses their own device (and thus pynecone server-side state).

This mechanism is also used to implement periodic server-driver refresh of the pending genqueue items as long as there are pending items.

asyncio.get_event_loop().create_task(
    delayed_event(
	delay=GQ_REFRESH_DELAY,
	state=self,
	name="state.genqueue_state.delayed_refresh_cb",
    ),
)

Ultimately, this timer logic should be driven on the client side, with server side events only sent when there is actually data to update, but that remains future work. For now, the server decides when the client should request an update, but theoretically the server could send client side events directly. Probably will hold off on sending direct client side events until the websocket is stable.

Upcoming features:

✌️

Thanks for your interest in trivai development.

If you have feedback, please email trivai@0x26.net.

© 2024 Masen Furer   •  Theme  Moonwalk