@event becomes a command the client can send.
Custom event handlers
Use@event to define actions the client can trigger. The method’s signature becomes the event schema.
{"type": "get_current_prompt", "data": {}} and the runtime calls your method. This works, but printing to stdout isn’t useful for the client. We’ll see how to send data back in Outbound messages.
Events can also take parameters:
Deduplicating expensive handlers
Passdedupe=True to collapse duplicate events that queue up while the model is busy. Only the latest payload is processed; earlier ones are discarded.
Lifecycle hooks
@connected
Runs once when a client connects, before run() resumes from await self.connected.wait().
async def or plain def. Only one @connected handler is allowed per model class.
@disconnected
Runs once when a client disconnects.
async def or plain def. Only one @disconnected handler is allowed per model class.
Outbound messages
Earlier we printed the prompt to stdout. What if we want to send it to the client instead? Define aModelMessage subclass:
{"type": "current_prompt", "data": {"prompt": "..."}}. The type on the wire is the snake_case version of the class name.
Now replace the print with self.send():
run():
File uploads
When your model needs binary files from the client (reference images, style assets, etc.), type an@event parameter as UploadedFile. The runtime handles the upload and delivers the file bytes to your handler automatically.
UploadedFile has four fields: name, mime_type, size, and data (the raw bytes). You can use them to filter by type, decode images, or pass the content to your pipeline:
@file_uploaded decorator instead. It fires once per upload regardless of which command triggered it:
uploaded_file. Only one @file_uploaded handler is allowed per model class.
Putting it together
A model that combines everything from this page: explicit events, lifecycle hooks, and outbound messages.- Send
set_promptto change the prompt. - Send
get_current_promptand receive acurrent_promptmessage back. - Receive
progressmessages each frame.
Next
Video Input
Read webcam frames with manual buffer management.
Model Anatomy
Back to the ReactorModel overview.