// CONTENTS Part 03 · Core engine

// PART 03

Core engine

Chapter 13 / 56 3 min read English Part 03 of 14

The core engine is the chat pipeline plus the script runtime: everything between “a message arrives” and “a reply is delivered”. This page maps the machinery; the BASIC language itself is in the BASIC runtime page.

Where the engine lives

Concern Path
WebSocket handshake, identity resolution, frames botserver/src/core/bot/ws/ (handler.rs, message.rs, session/, stream/)
Turn pipeline (stages) botserver/src/core/bot/pipeline/ (turn_context.rs, kb.rs, llm/, exec/, tool_exec/, native_tools/, output_publish/, turn_hooks/, history.rs, sink.rs)
Retrieval / KB injection botserver/src/core/bot/kb_context/
Run observability botserver/src/core/bot/runs/ (spans, events, bus)
API command catalog botserver/src/core/bot/api_catalog/
BASIC host botserver/src/basic/ + botserver/crates/botbasic_*

Message types

The WebSocket protocol is typed. The table is the contract between the suite front end and the pipeline:

ID Name Purpose LLM used?
0 EXTERNAL External message No
1 USER User message Yes
2 BOT_RESPONSE Bot response No
3 CONTINUE Continue processing No
4 SUGGESTION Suggestion button Yes
5 CONTEXT_CHANGE Context change No
6 TOOL_EXEC Direct tool execution No — bypasses the LLM

TOOL_EXEC (type 6) executes the tool’s compiled .ast directly; no KB injection, no model call. The result appears immediately in chat. The suggestion-button flow (type 4) is a normal turn with the model.

A turn, stage by stage

  1. Handshake and identity — ws/handler.rs creates the user session and logs which identity source resolved (token, user_id, or guest).
  2. start.bas, once per session — on first message the bot’s start.bas runs (suggestions, initial context); a Redis flag prevents re-runs.
  3. Context assembly — conversation history, KB injection for bots with USE KB, drive context listings, system prompt from PROMPT-{CHANNEL}.md.
  4. Decision — the model either answers or invokes a tool. Native tools and __api_call__ commands are surfaced through the API command catalog.
  5. Execution — tool .ast runs, MCP calls, or the agent VM; results feed back into the turn.
  6. Delivery — streamed chunks over WS (stream/); output files are published to Drive by output_publish/ with bounded sizes and idempotent digests.

What the engine deliberately does not do

  • It does not call raw REST from chat: chat reaches endpoints only through the command catalog or BASIC keywords.
  • It does not run one model per turn: the provider is built at boot from Vault (see Tools, MCP, and model providers).
  • It does not persist volatile per-turn maps as durable state; the durable record is message_history, run tables and Drive.

Extending the engine

Extension point Mechanism
A new BASIC keyword register in a botbasic_* keyword crate or botserver/src/basic/keywords/
A new chat-reachable endpoint add to the API command catalog with admin_only where appropriate
A new channel adapter + inbound webhook registration (Part 7)
A new model provider provider factory in botllm (Part 11)

Reading list

Question File
How is identity resolved on WS? botserver/src/core/bot/ws/handler.rs
Which pipeline stages exist? ls botserver/src/core/bot/pipeline/
How do suggestions reach the UI? botserver/src/core/bot/ws/suggestions.rs
How are tool files located? pipeline/tool_paths.rs
What publishes turn output to Drive? pipeline/output_publish/

Found a mistake? Open an issue · All books

// SEARCH ESC
Pagefind · 236 pages · 4 books Enter to open ↑ ↓ to move