class Thread(PageState):
said: list = field(default_factory=list)
class Draft(ClientState):
text: str = field(default="")
def reply(files=None) -> None:
"""The message as it left: the text from the draft, the files posted
with it — answered here by an echo, by an AI elsewhere."""
text = (Draft().text or "").strip()
names = [f.filename for f in (files if isinstance(files, list) else [files])
if hasattr(f, "filename")]
thread = Thread()
thread.said = [*thread.said, {"role": "user", "text": text, "files": names},
{"role": "assistant",
"text": f"You wrote {len(text.split())} word(s)"
+ (f" and joined {len(names)} file(s)." if names else ".")}]
@refreshable
def conversation() -> None:
with ui.message_list(classes="px-1"):
for one in Thread().said:
ui.message(one["text"], side="end" if one["role"] == "user" else "start",
files=one.get("files"))
if not Thread().said:
ui.text("Nothing said yet.", color="muted", size="sm")
def write_and_send() -> None:
with ui.vstack(gap="lg", classes="w-full max-w-xl h-96"):
conversation()
with ui.message_input(value=Draft().text, on_submit=reply, voice=True,
files=(".pdf", "image/*"),
placeholder="Ask, dictate, drop a file…"):
ui.badge("Q3 report", variant="soft", icon_left="at-sign", dismissible=True)
ui.message_input_tool("Insert a template", icon="file-text",
on_click="alert('A dialog of templates would open here.')")