Skip to content

Sessions and Channels 🟑 BETA

This page explains where a chat thread is kept and how the channel identifies its user, so you can resume a conversation or debug one.

The four BotBook guides: sessions and daily chat belong to GB Lite.

What Is a Session?

A session is the conversation between a user and a bot. It carries a session id, a user id, the bot, the message history and an activity timestamp in the user_sessions table.

How Sessions Start

UI Interface

  1. Open the suite at http://localhost:3000.
  2. Call /api/auth to obtain the page identity.
  3. Open the WebSocket only after that answer; the shell gates the socket on the auth response.
  4. On connect, start.bas runs once for the session and may send suggestions.

API Access

The REST surface includes POST /api/chat, POST /api/chat/sessions/new and GET /api/chat/history/sessions. Use the token returned by the auth endpoint for requests that require identity. The WebSocket path is /ws.

Anonymous vs Authenticated

  1. With a token, the handshake resolves the signed-in user (cookie gb-access-token, then access_token or token).
  2. Without a token, it uses the user_id query value supplied by /api/auth.
  3. With neither, it creates a guest identity (guest_<id>@anonymous.local).

The handshake logs which of the three paths it used.

Session Lifecycle

The session manager: a session groups a user, a bot and a thread.

Session states from creation through active use to expiry.

start.bas runs once per session; an in-memory guard and the session:{id}:initialized flag prevent a second run. The thread stays available when you return to it. An expired identity must be signed in again.

What Gets Stored

PostgreSQL (Permanent Storage)

user_sessions holds the id, user, bot and activity fields. Chat history is read through the /api/chat/history/sessions routes.

Cache (Fast Access)

The cache holds active session data and the suggestions sent by ADD_SUGGESTION. Its keys are derived from the bot and session ids.

Session Variables

SET and GET operate on the current conversation. Memory with a wider scope uses SET BOT MEMORY, GET BOT MEMORY and the user-memory forms. The generated BASIC reference gives the verified forms; this page states no storage quota.

Context Management

Each thread keeps its own history. history-limit controls how much of it is sent to the model; the code default is 10 turns. Documents are read for a turn from the bot's .gbkb/ content.

Multi-Bot Sessions

A bot has its own identity and prompt. Open /default or another bot route in the suite; each bot's thread and configuration remain separate.

Session Security

Token Generation

Tokens are issued by /api/auth and passed to the shell. This page states no entropy figure.

Token Validation

The WebSocket and the routes resolve identity before a turn runs. Role resolution uses resolve_user_role; admin-only suggestions and tools are gated in start.bas.

Security Features

Guest sessions are isolated per user id. Do not put a token in a log, a chat message or a documentation page.

Debugging Sessions

View Current Session

  1. Read the WS handshake: identity=... line in the server log.
  2. Compare the user id with the user_sessions row and the message_history rows for the turn.
  3. Use the history route to read the messages for a session id.

Database Inspection

Query user_sessions by id or user to see the bot, creation time and last activity. Keep the query scoped to one session.

Cache Inspection

Read the active-session and suggestion keys with the cache client. This page does not publish a key format beyond the bot-and-session scope.

Session Limits

Limits depend on the enabled features and the host. Read the source for a limit before you quote it; this page states no message count, timeout or concurrency figure.

Advanced Features

Session Persistence

History is stored through the database and cache layers. A reconnect uses the same session id when the identity resolves.

Session Context Isolation

A loaded knowledge base or tool affects the current session. Other users' threads are not changed by it.

How It Works Automatically

Session creation, history storage, cleanup of expired rows and identity resolution happen in the server. A script uses TALK, HEAR, SET and GET without managing the transport.

Common Patterns

Welcome Back

Use GET BOT MEMORY with a key derived from the session id, then SET BOT MEMORY with the new timestamp. Check the generated reference for the call forms before you paste a script.

Progressive Disclosure

Send suggestions from start.bas with ADD_SUGGESTION; clear them with CLEAR SUGGESTIONS when the path changes.

Multi-User Support

Each resolved user id gets its own scope. A guest identity is per user, per connection path.

Troubleshooting

Symptom Check
A thread does not resume Confirm the identity used in the handshake log and the session id
No suggestions appear Check start.bas and ADD_SUGGESTION in the session's tool set
Missing prompt behaviour Confirm PROMPT-{CHANNEL}.md exists in the bot's .gbot/ folder
Tools stop being called Check the identity: a guest turn may run without the expected prompt

Write Once, Run Everywhere

The same BASIC script runs on channels whose adapters are compiled in. The adapter translates the platform format; the prompt and tools stay with the bot.

Summary

A session ties one user, one bot and one thread together. Identity is resolved at the WebSocket handshake, history is stored through the database and cache, and the channel supplies only the transport.