// PART 04
Bot pipeline
The bot pipeline is the path a message takes from the WebSocket (or a channel webhook) to a delivered reply, with LLM integration at its center. This page describes the mechanics the code implements.
Where the pipeline lives
- WebSocket surface:
botserver/src/core/bot/ws/—handler.rs(handshake, identity),message.rs(frame dispatch),stream/(chunked delivery),suggestions.rs. - Stages:
botserver/src/core/bot/pipeline/—turn_context.rs,kb.rs,llm/,exec/,tool_exec/,native_tools/,surface_tools/,output_publish/,turn_hooks/,history.rs,sink.rs. - Channel entries:
pipeline/channel_entry.rsnormalizes inbound channel messages into the same turn shape.
The LLM provider
At boot the server builds one LLM provider from Vault
(state_builder.rs → provider_factory::create_llm_provider_from_url). The
URL, model and key come from secret/gbo/{org}/{branch}/{bot} with the global
secret/gbo/llm fallback; the provider type is derived from the URL pattern.
Every turn uses that provider — per-bot model selection happens at boot, not
per turn. If Vault has no entry, LLM_URL / LLM_MODEL / LLM_KEY
(or OPENAI_API_KEY) are the documented environment fallbacks.
What a turn assembles before the model call:
| Input | Source |
|---|---|
| System prompt | PROMPT.md / PROMPT-{CHANNEL}.md from the bot’s .gbot/ |
| History | bounded conversation history (history.rs) |
| KB context | retrieval result for USE KB collections (see the RAG page) |
| Command catalog | compact endpoint inventory so the LLM can call APIs |
| Attachments | inbound media as marker lines ([image] inbox/…) |
Streaming and delivery
Replies stream as WS chunks (stream/). Channels other than the web chat
receive the turn as whole messages through channel_delivery.rs. Files the
turn produced are published to Drive at the end of the turn
(output_publish/) — bounded (a small file count, per-file and per-turn size
caps), idempotent (unchanged files are not re-uploaded) and best effort (a
Drive outage never fails the turn).
Tools on the pipeline
| Mechanism | Trigger | Path |
|---|---|---|
TOOL_EXEC (message type 6) |
frontend button | direct .ast execution, no LLM, no KB |
| Native tools | the model calls a chat-native primitive | pipeline/native_tools/ |
__api_call__ |
the model emits a catalog command | pipeline/api_call/ + consent gate |
CALL "tool" |
BASIC script | in-memory procedure or {name}.bas in .gbdialog/ |
| MCP tools | declared in .mcp.json |
botbasic_ai/src/keywords/mcp_client/ |
Tool definitions themselves come from compiling .bas headers (PARAM,
DESCRIPTION) into the basic_tools table — see the compilation section of
the BASIC API page.
Keyword surfaces the pipeline honors
USE KB collections, USE TOOL activations (via start.bas), ADD SUGGESTION entries (delivered per session through Redis), memory keywords,
and SET CONTEXT strings all feed the same turn context. This is why a bot
without PROMPT-{CHANNEL}.md still answers: the generic fallback prompt is
used — and why tool calls then silently stop, because the fallback prompt does
not advertise them.
Observing a turn
| Signal | Where |
|---|---|
| Turn spans and events | core/bot/runs/ — agent_spans, agent_runs, run_events |
| Per-turn logs | botserver.log (stage lines, tool registration, errors) |
| Suggestion state | Redis suggestions:{bot}:{session} |
| Published artifacts | the user’s Drive, conversations/{title}/ |