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.
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
- Open the suite at
http://localhost:3000. - Call
/api/authto obtain the page identity. - Open the WebSocket only after that answer; the shell gates the socket on the auth response.
- On connect,
start.basruns 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
- With a token, the handshake resolves the signed-in user (cookie
gb-access-token, thenaccess_tokenortoken). - Without a token, it uses the
user_idquery value supplied by/api/auth. - With neither, it creates a guest identity (
guest_<id>@anonymous.local).
The handshake logs which of the three paths it used.
Session Lifecycle
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
- Read the
WS handshake: identity=...line in the server log. - Compare the user id with the
user_sessionsrow and themessage_historyrows for the turn. - 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.