# Backend and front ends `splinter` is split in two so that the screen is not the harness. ``` +-----------------------+ a keyboard ---> | front end (curses) | ---\ +-----------------------+ \ \ +----------------+ a socket ---> | front end (protocol) | --------> | the engine | +-----------------------+ / +----------------+ / agent, tools, -p / --daemon --> | direct library use | ---/ transports, +-----------------------+ session, config, plugins ``` ## The backend `libsplinter.so` is the whole harness minus the screen: | file | what it owns | | --- | --- | | `src/backend/engine.c` | `struct sp_engine`: session, conversation, registry, plugins, transport, agent, turn thread, event queue, slash commands | | `src/backend/agent.c` | the loop: request, tool calls, observations, answer | | `src/backend/transport*.c`, `openai.c`, `anthropic.c`, `sse.c` | a conversation in, a reply out | | `src/backend/builtin.c`, `tool.c` | the tools and the registry | | `src/backend/session.c`, `conv.c` | append-only JSONL, owned messages | | `src/backend/config.c`, `providers.c`, `usage.c` | configuration and providers | | `src/backend/plugin.c` | `dlopen`, widgets, commands, gates, compactors | | `src/backend/telegram.c`, `a2aserver.c`, `mcpserver.c` | outbound protocol adapters | The engine never calls into a front end. A turn runs on a thread of the engine's making and appends `struct sp_event` records to a queue; the front end drains it whenever it draws. That is the whole threading rule, and it is why a slow terminal cannot stall a model, and why a GUI cannot corrupt a turn. `include/splinter_engine.h` is the C boundary. Its event kinds map one to one onto the JSON `ev` names in `docs/PROTOCOL.md`. ## The front ends | file | kind | | --- | --- | | `src/frontend/curses.c` | the screen: transcript, sidebar, prompt, keys | | `src/frontend/ui_backend.h` | what the screen needs from a backend | | `src/frontend/ui_local.c` | that backend as the engine in this process | | `src/frontend/ui_remote.c` | that backend as a protocol client (`--attach`) | | `src/frontend/protocol.c` | `splinter --serve`: JSON lines, sessions, open | | `src/frontend/client.c` | `splinterctl`, the reference protocol client | | `src/main.c` | the launcher: parse, config, engine, pick a front end | `curses.c` owns the screen, the themes, the markdown-to-ASCII pass, the input history and the key bindings. It does not know how a provider is switched, how a session is created or how an `ask` question is waited for — those are `/model`, `/new` and the engine's condition variable. It talks to one of two backends through `ui_backend.h`: the engine linked into the process (`--chat`), or a served engine over the socket (`--attach`). The screen cannot tell them apart, which is the test the protocol has to pass. ``` splinter --serve --socket /tmp/s.sock # one process: engine + protocol splinter --attach --socket /tmp/s.sock # another: the screen as a client ``` The server owns the engine: `sessions` lists the session directory and `open` switches to another id (or `new`/`last`), sending the state and history of that session. The curses screen exposes that as `/sessions` and `/open ID`, in both modes: `--serve` switches its engine, and the in-process `--chat` closes its engine and opens the next one from the same options (plugins keep process globals, so the old one goes first). `/sessions` without an argument is a two-step picker over the same ops (`sources`, then `source_sessions`): the foreign stores appear next to this folder's sessions, and picking one of their rows runs the same `/open :` that imports and continues it. One session is active at a time; two clients still take turns. `--attach` keeps running when the backend disappears: it says so, retries every second, and on the next server asks for the session it was on by id. The history it already had is shown again, and turns continue; if that session is gone, the screen says which one and stays on the session the server has. A GUI gets the same with `sessions`, `open` and its own retry loop. ## Writing a GUI client Two ways, and the second one is the interesting one. 1. **Link the library.** Include `splinter_engine.h`, call `sp_engine_open()`, then `sp_engine_input()` / `sp_engine_submit()` / `sp_engine_poll()` as `src/frontend/curses.c` does. This only works from C or a C-binding language. 2. **Speak the socket.** Run `splinter --serve` and connect to the Unix socket from anything that can open one and write JSON: Python/Tk, Qt, GTK, a web front end through a small bridge. `docs/PROTOCOL.md` is the contract; `docs/proto-example.py` is a complete 60-line client, and `splinter --attach` is the full curses front end running this way. Use `sessions`/`open` for tabs and retry the connect for a server restart. 3. **Let another harness use splinter as a model.** `splinter --openai-listen` serves `/v1/chat/completions`: the caller's tools stay with the caller, splinter's own run locally under `gw_*`. See `docs/GATEWAY.md`. The recommended shape for a GUI: * one window per connection; the `state` event fills the sidebar (session, model, agent, tokens); * on connect, send `history` once and draw the returned items - a GUI that joins a session should not look like it invented it; * append `stream` deltas to the open bubble; close it on `step` or `turn`; * draw `tool` as a call row and `tool_result` under it (or on the same row when `join` is true); * when `ask` arrives, show `text` and `options` and send `answer` with the text the operator gave; the turn is parked until it arrives, so a client that ignores the event leaves the model waiting (the engine's timeout ends it with "nobody answered", but slowly); * `busy` drives the spinner; `cancel` and `steer` are the two buttons that must remain live during a turn; * nothing in the client has to know a provider, a session file or a tool's authorization class — the engine already decided. There is one conversation and one client at a time; a second connection waits until the first is gone. An attaching client asks for `history` (the last 100 messages, up to 256 KiB) and shows the session it joins; live events continue from that point. If a client disconnects, the server cancels a running turn, because an `ask` question that nobody can answer would park the engine forever.