# The OpenAI gateway `splinter --openai-listen IP [--openai-listen-port N]` is a small OpenAI-compatible endpoint. Another harness points its OpenAI client at it and gets an answer from splinter's configured provider, with **tools on both sides**: splinter executes its own tools where splinter runs, the caller executes its tools where the caller runs. ```sh # the side with the files and the shell splinter -m myprovider --openai-listen 127.0.0.1 --openai-listen-port 8097 # the other harness export OPENAI_BASE_URL=http://127.0.0.1:8097/v1 export OPENAI_API_KEY=unused # unless SPLINTER_GATEWAY_TOKEN is set ``` `GET /v1/models` answers with the run's model, so a client that probes first is happy. `POST /v1/chat/completions` accepts the usual `messages`, `tools` and `stream` fields. ## Two tool namespaces The model sees two kinds of tools in one request: | name | who runs it | where | | --- | --- | --- | | `gw_read`, `gw_write`, `gw_edit`, `gw_run`, … | splinter | on the host where splinter runs | | everything in the caller's `tools` array | the calling harness | where the harness runs | `gw_` is reserved: a caller tool with that prefix is refused with HTTP 400, because that is how a local `read` and a caller's `read` stay apart. The loop, inside one HTTP request: 1. splinter asks its provider with the caller's conversation, the caller's tools **and** its own tools renamed `gw_*`. 2. If the model calls `gw_*`, splinter runs it here (read tools always; mutating and destructive ones as the registry offers them), appends the observation and asks the provider again. The caller never sees these calls. 3. If the model calls a caller tool, splinter returns a normal `chat.completion` with `tool_calls` and `finish_reason: "tool_calls"`. The harness runs them, sends the results as `tool` messages in the next request, and the gateway continues. 4. If a reply mixes both, the local calls run here and the caller's calls are returned in the same `tool_calls` answer. Since the caller cannot answer a `gw_` call, the local observations travel in the message `content` (under "local gateway tools ran on the splinter host:"); the caller sends them back with the assistant message as usual, and the model sees both sides. Every request is stateless, as the OpenAI protocol says: the conversation travels with it. The loop is capped at 16 local tool rounds and each observation at 32 KiB. ## Request shaping The caller's `temperature`, `top_p` and `max_tokens` (or `max_completion_tokens`) are forwarded to the provider; absent values stay the provider's default. The response echoes the request's `model` name, and every message, including the mixed-reply case, stays within the OpenAI chat shape. Bodies may arrive with `Content-Length` or `Transfer-Encoding: chunked`; only one request per connection is read, then the connection closes. ## Streaming With `"stream": true` the gateway uses the provider's streaming transport and forwards each text delta as a `chat.completion.chunk` event while it arrives - the caller sees the answer grow instead of waiting for the last token. The caller's tool calls are sent as one `tool_calls` delta at the end of the step, because a name is only complete when the provider has finished it; local `gw_*` calls stay invisible. `"stream_options":{"include_usage":true}` adds a final usage chunk before `[DONE]`, with the token counts summed over every upstream round of the request. If the provider fails after the stream has started, the stream carries an `{"error":...}` object and `[DONE]`; a request that was invalid before the first byte is still an ordinary HTTP 400. The gateway ignores SIGPIPE, so a caller that closes its stream early cannot kill the server. ## Examples A local tool, executed by splinter, from the caller's point of view an ordinary completion: ```sh curl -s http://127.0.0.1:8097/v1/chat/completions \ -H 'Content-Type: application/json' \ -d '{"model":"x","messages":[{"role":"user","content":"read Cargo.toml"}]}' ``` A caller tool comes back instead: ```json {"choices":[{"index":0,"message":{"role":"assistant","content":"", "tool_calls":[{"id":"c1","type":"function", "function":{"name":"browser_open","arguments":"{\"url\":\"...\"}"}}]}, "finish_reason":"tool_calls"}]} ``` ## Security - Beyond loopback the server refuses to start without `SPLINTER_GATEWAY_TOKEN`; with a token set, every request needs `Authorization: Bearer `. - the endpoint has no approval step: whatever `gw_*` tool the registry offers is callable (`gw_write`, `gw_run`, ...). `--autonomous` is accepted and changes nothing. Who may reach the endpoint is decided by the token and by what the operator registers, not by a question the gateway could not put to anyone. - There is no TLS. Expose it through ssh, WireGuard or a TLS proxy when the network is not yours. - One request is served at a time; the endpoint is a gateway, not a load balanced service. That makes a silent client everyone's problem: an unanswered read ends after 60 seconds (`SPLINTER_GATEWAY_READ_TIMEOUT`), and the next connection is then served. ## Where the tools come from `gw_*` is the engine's registry: the builtins (`read`, `write`, `edit`, `search`, `list`, `run`, `websearch`, `webfetch`), `folder_history`, `delegate`, and the skills tools. Plugins loaded with `--plugin` add theirs. The caller's tools are passed through to the provider untouched, except for the `gw_` reservation.