Skip to content

aaa.session

SessionManager

SessionManager(
    engine: Engine,
    algo: Algorithm,
    *,
    system: str = "",
    context: ContextPolicy | None = None,
    turn_budget: Budget | None = None,
    session_budget: Budget | None = None,
)

Creates, finds and lists sessions for one inner algorithm. Build it in EVERY process that may handle a turn (workers register the algorithms; clients just call it).

create async

create(
    *,
    tenant: str | None = None,
    user: str | None = None,
    title: str | None = None,
    meta: dict[str, str | int | float | bool] | None = None,
    session_id: str | None = None,
) -> Session

A new session. user is who it is for (memory is scoped by it; it is never taken from the model).

get async

get(
    session_id: str, *, tenant: str | None = None
) -> Session

Reattach to an existing session. KeyError if it does not exist or belongs to another tenant.

Session

Session(manager: SessionManager, session_id: str)

wait async

wait(
    turn: int,
    *,
    timeout_s: float | None = None,
    _from_seq: int = 0,
) -> TurnResult

The reply to turn, from any process, even if the one that sent it died. Raises SessionClosed if the session ends first.

send async

send(
    text: str, *, timeout_s: float | None = None
) -> TurnResult

Say something and wait for the answer. One turn at a time (SessionBusy).

stream async

stream(text: str) -> TurnStream

Like send, but iterate the reply as it is written (needs an inner algorithm built with stream=True).

close async

close(reason: str = 'closed') -> None

End the session. A running turn is cancelled. A closed session is an ordinary finished run, so it can be erased and falls under retention like any other.

TenantSessions

TenantSessions(manager: SessionManager, tenant: str)

Sessions of one tenant: ids are namespaced and a lookup of another tenant's session is a plain KeyError.

TurnResult dataclass

TurnResult(
    turn: int,
    text: str,
    kind: str,
    reason: str | None = None,
    run_id: str | None = None,
)

TurnStream

TurnStream(session: Session, turn: int, seq: int)

async for piece in chat.stream(...) yields the reply as it is written; await s.result() is the final turn.

SessionInfo dataclass

SessionInfo(
    id: str,
    tenant: str | None,
    user: str | None,
    open: bool,
)

Turn

Bases: BaseModel

SessionAlgo

SessionAlgo(
    inner: Algorithm,
    *,
    system: str = "",
    context: ContextPolicy | None = None,
    turn_budget: Budget | None = None,
)

Reacts to aaa.session.message by starting the inner algorithm on the conversation, and to the child's completion by recording aaa.session.reply. Stateless, like every algorithm: all state is the stream.

SessionBusy

Bases: AAAError

A turn is still running; one turn at a time per session.

SessionClosed

Bases: AAAError

The session has ended (closed, or its budget ran out).