Every conversation follows a predictable sequence of events. You only need to write @aiui.reply to start; add the other decorators whenever you want custom greetings, per-chat setup, or clean-up.
@aiui.reply
Every decorator works with both async def and plain def.
async def
def
| Decorator | Alias | When it runs | Typical use |
|-----------|-------|--------------|-------------|
| @aiui.welcome | — | Once when an empty chat loads | Show a friendly greeting above starter cards |
@aiui.welcome
| @aiui.on_chat_start | — | Once per chat, right before its first reply | Initialize per-chat memory, DB client, or agent |
@aiui.on_chat_start
| @aiui.reply | @aiui.on_message | Every time the user sends a message | Call your model, stream tokens, run tools |
@aiui.on_message
| @aiui.cancel | @aiui.on_stop | When the user clicks Stop mid-reply | Log cancellation or clean up an external job |
@aiui.cancel
@aiui.on_stop
| @aiui.goodbye | @aiui.on_chat_end | When the chat's last browser tab closes | Close database connections or temporary files |
@aiui.goodbye
@aiui.on_chat_end
<Tip>
Annotate your @aiui.reply parameter as str (async def on_message(text: str)) to receive plain text. Omit the annotation (or type it as MessageContext) when you also need message.session_id, message.agent_name, or message.attachments.
str
async def on_message(text: str)
MessageContext
message.session_id
message.agent_name
message.attachments
</Tip>
Save this as app.py and run aiui run app.py:
app.py
aiui run app.py
asyncio.CancelledError
await
aiui.user_session
<CardGroup cols={2}>
<Card title="Messages & streaming" icon="message-square" href="messages-and-streaming.md">
Stream tokens, ask interactive follow-up questions and add clickable buttons.
</Card>
<Card title="Sessions & persistence" icon="database" href="../features/sessions.md">
Save conversations to disk or SQL so users can resume later.
</CardGroup>