Skip to Content
Motoko

Listing and naming sessions

listSessions() answers what a list of sessions shows, each session as createSession answers it, oldest first: { id, createdAt, updatedAt, title?, metadata? }. It is one read of the sessions record (see Deleting a session), however many sessions there are.

  • updatedAt is its creation, or the last submit or continue it admitted, written in the commit that admits it; a retry of either changes nothing, and an ask (replyTo) leaves it as it was.
  • title is, until the session is named, the start of the first message submitted to it, on one line: wire's titleOf, each run of whitespace one space, trimmed and cut to 100 characters (code points). It is absent before the first message. submit answers the title as its commit left it, so a client lists it without listing again, whatever order its submits answer in.
  • session.rename(title) names it, after the first message or before it, and resolves the session as the list shows it now; the name stays whatever is submitted later. rename(null) puts back the first message's. A title that is blank or longer than 100 characters, once on one line, rejects with InvalidRequestError and changes nothing. A child session's rejects with SessionOwnedError.
  • metadata is the app's, a JSON object: createSession({ metadata }) sets it, configure({ metadata }) replaces it whole and null takes it away, and settings() reads it. Something else rejects with a TypeError and changes nothing. Every listing reads all of it, so keep it small: the project a session is filed under, say, not its files. No call of a client sets it; over a transport, sessionOptions gives each new session its own.

The wire's renameSession call is rename, so a user names a session from the client. A server that names it itself, say with a small model of its own, calls rename with the model's answer:

import { titleOf } from "@chia1104/motoko-wire";

const session = instance.session(id);
const text = "Which of my notes mention the Q3 launch, and what did we decide on pricing?";
await session.submit({ text, requestId: crypto.randomUUID() });
// The list shows the start of `text` until then. `summarize` is the app's; titleOf fits its answer on one line.
const name = titleOf(await summarize(text));
const listed = (await instance.listSessions()).find((info) => info.id === id);
// A name the user gave in the meantime stands.
if (name !== undefined && listed?.title === titleOf(text)) await session.rename(name);