Inside an @aiui.reply (or @aiui.on_message) handler you can ask who is calling, keep state for
@aiui.reply
@aiui.on_message
this chat, read the chat's history in the shape an LLM wants, pop a notice,
and call the UI from blocking worker code. Nothing to configure.
Each API resolves to the chat being answered (two people chatting at once never
see each other's data), also in tasks the handler starts and aiui.make_async threads.
aiui.make_async
| Attribute | Value |
|---|---|
| id | The chat (session) id. |
id
| user | The signed-in user as a dict (id, username, extra fields; never the password hash), or None. aiui.current_user() returns the same dict. |
user
username
None
aiui.current_user()
| client_type | "webapp" (a browser), "api" (a script or HTTP client), "copilot", or the bot platform ("slack", "discord", "teams", …). |
client_type
"webapp"
"api"
"copilot"
"slack"
"discord"
"teams"
| chat_profile | The profile chosen for this chat, or the server-wide selection. |
chat_profile
| chat_settings | Settings sent with the chat (dict). |
chat_settings
| env | Per-chat values sent by the client (dict of strings). |
env
| language | The best tag of the caller's Accept-Language ("fr-CA"), or None. |
language
Accept-Language
"fr-CA"
| headers | Request headers, lower-cased, with credentials removed (Authorization, Cookie, and any header whose name mentions a token, key, secret, password, auth or session). |
headers
Authorization
Cookie
It is None outside a handler. A client names itself with ?client=<name> on the chat WebSocket or an
?client=<name>
X-Aiui-Client: <name> header (letters, digits, - and _, up to 32
X-Aiui-Client: <name>
-
_
characters). A chat turn ({type: "chat", ...} on /api/chat/ws, or the body
{type: "chat", ...}
/api/chat/ws
of POST /api/chat/send) may carry chat_profile, chat_settings and env;
POST /api/chat/send
they stay with the chat for its later turns.
A key/value store private to the current chat, kept across its turns.
Preset keys: id, user, chat_profile, chat_settings, env. Values live
in memory (the last 5,000 chats), so keep what must survive a restart in your
own database. Outside a handler get returns the default and set raises.
get
set
The chat's messages, oldest first, including the one being answered; each
finished reply is added. After a restart the saved history is reloaded from
the datastore. At most 200 messages are kept.
| Method | Does |
| get() | A copy of the messages: [{"role", "content"}, ...]. |
get()
[{"role", "content"}, ...]
| add(content, role="assistant") | Append a message (a string with a role, or a {"role", "content"} dict). Roles: system, user, assistant, tool. |
add(content, role="assistant")
{"role", "content"}
system
assistant
tool
| remove(message) | Remove the latest message equal to message; returns True if one was removed. |
remove(message)
message
True
| clear() | Forget the history (for example on a "new topic" command). |
clear()
| to_openai(system=None, max_messages=None) | OpenAI chat messages, with an optional system prompt first and only the last max_messages. Works with any OpenAI-compatible client. |
to_openai(system=None, max_messages=None)
max_messages
Run blocking work in a thread so the chat stays responsive, and call the UI
from inside it:
make_async(fn, executor=None) runs fn on a thread pool (yours, if given)
make_async(fn, executor=None)
fn
with the handler's context. From that thread, run_sync(coro, timeout=None)
run_sync(coro, timeout=None)
runs the coroutine on the server's loop and waits; in a plain script it starts
its own loop; in async code it raises RuntimeError (use await).
RuntimeError
await
@aiui.on_chat_start runs once per chat before its first reply, as that chat,
@aiui.on_chat_start
so it is the place to fill aiui.user_session; @aiui.on_chat_end runs as the
aiui.user_session
@aiui.on_chat_end
chat when its last connection closes. See
Chat lifecycle.
Load a model, an index or a client once and share it across every chat
(@aiui.cache(ttl=3600, maxsize=8) takes options; async def works too):
@aiui.cache(ttl=3600, maxsize=8)
async def
| Option | Does |
| ttl=None | Seconds an entry is kept; None keeps it until cleared. |
ttl=None
| maxsize=None | Keep only the most recently used entries; None for no limit. |
maxsize=None
| fn.cache_clear() | Forget every entry. |
fn.cache_clear()
Entries are keyed by the arguments (unhashable ones, like a list, by their
repr) and live in the server's memory. It is thread-safe; concurrent first
repr
calls share one computation (a caller cancelled by Stop does not cancel it).
Exceptions are never cached, so the next call tries again.
type is info, success, warning or error; duration is in
type
info
success
warning
error
duration
milliseconds. The notice is never saved with the reply or added to
aiui.chat_context. Clients receive `{type: "toast", session_id, run_id,
aiui.chat_context
message, variant, duration?}` on the chat WebSocket; both built-in chats show
it as a toast. Returns False outside a handler. (aiui.toast(...)
False
aiui.toast(...)
builds a toast component for a page.)