Guides

Server & inference

Serve HTTP and SSE from the runtime, load GGUF models in-process, and understand the experimental inference boundary.

Updated Nov 13, 20264 min readv0.3.0-beta
On this page7 sections

Sofuu binds HTTP sockets in the same process that runs your script, so a server can call sofuu.ai and sofuu.memory without an IPC hop. Two surfaces exist: a JavaScript server built on sofuu.http.createServer, and the OpenAI-compatible server started with sofuu serve.

Create a server

sofuu.http.createServer(cb) binds a non-blocking server over OS sockets and calls cb with req and res for every request. The example below streams model output as Server-Sent Events.

JavaScript
// app.js — a server that streams model tokens over Server-Sent Events
const server = sofuu.http.createServer(async (req, res) => {
  if (req.url === "/chat") {
    const stream = sofuu.ai.stream("Explain quantum mechanics simply.", {
      provider: "openai", model: "gpt-4o",
    });
    res.writeHead(200, { "Content-Type": "text/event-stream" });
    for await (const chunk of stream) res.write(chunk.text);
    res.end();
  } else {
    res.writeHead(200);
    res.end("AI Edge Runtime Active");
  }
});

server.listen(8080, "0.0.0.0");
console.log("Server bound on port 8080.");

The handler sees req.url and req.method; the body is buffered before the handler runs. A response offers writeHead(status, headers), write(chunk), and end(data).

server.listen(port, host) refs the handle and keeps the process alive while listening, matching Node semantics: a script that ends with a bound server keeps serving. server.unref() releases the loop while leaving the socket bound, and server.ref() reverses it. An embedded host (sofuu_rt_new) uses the opposite default and calls ref() per server it wants held open.

Request and response surface

CallBehavior
sofuu.http.createServer(cb)Creates the server and registers the request handler.
server.listen(port, host)Binds and refs the listening handle.
server.unref() / server.ref()Stops or resumes holding the event loop open. Returns the server.
res.writeHead(status, headers)Sends the status line and headers.
res.write(chunk)Writes body bytes without ending the response.
res.end(data)Flushes the body and finishes the response.
sofuu.fetch(url, opts?)HTTP(S) client over a statically linked curl, resolving to a response with .text() and .json(). Also reachable as sofuu.http.fetch.

In an embedded host, the http.serve funnel takes {handler, port, host}, where handler names a global function.

The OpenAI-compatible server

sofuu serve runs the shipped handler and exposes an OpenAI-shaped API over a local model, a brain, or both. It needs no external daemon, but it does need a model file or brain.

Shell
sofuu serve --model ~/models/qwen.gguf --model-id qwen --port 7707
sofuu serve --brain ./brain.qtsq
RouteNotes
GET /healthz, GET /livezNo auth, no data. Liveness only.
GET /healthObeys the token when one is configured.
GET /v1/modelsConfigured model ids from --model-id, defaulting to the file's basename.
POST /v1/chat/completionsChat with stream or JSON, usage, finish_reason, stop, and tools.
POST /v1/completions, POST /inferRaw-text completion; the prompt is not chat-templated.
POST /v1/embeddingsLocal embedder; batch cap 64 inputs, 8192 tokens each.
POST /v1/agent/runsRuns sofuu.agent.run with SSE step events, or JSON with stream: false.
GET /v1/tools, POST /v1/tools/:name/callTool inventory and execution, with policy verdicts.
POST /remember, GET /recall, POST /forgetBrain routes; 503 brain_unavailable when no brain is configured.

Every route except /healthz and /livez requires Authorization: Bearer <token>. The token comes from --token or SOFUU_SERVE_TOKEN, otherwise one is generated into ~/.sofuu/serve_token with mode 0600. A bind to a host other than 127.0.0.1 or localhost refuses to start with a generated token, so pass one explicitly. policy.http.auth = "none" is the only way to disable auth, and it prints a warning at start. Routes, auth rules, and the compatibility matrix are in docs/HTTP-SERVER.md.

Compiled micro-server

The compiled form packages an MCP server against the micro runtime base: sofuu mcp build server.ts -o dist/my-mcp-server, or the equivalent sofuu compile server.ts --profile micro -o dist/my-mcp-server. The result is a single file; OS libraries and any services the server calls are still required. See MCP for the server API and transport options.

Local inference (preview)

The engine loads models in-process with zero-copy mmap and executes on Apple Metal, ARM NEON, or x86_64 AVX2, with no Python and no separate model server.

Shell
sofuu infer ./model.gguf "Hello"
sofuu serve --infer -m ./model.gguf

A successful load says nothing about output quality, context length, or throughput. Measure the workload you intend to ship, and keep a fallback provider.

Edge swarms (preview)

Shell
sofuu swarm worker --model ./model.gguf --listen 0.0.0.0:9001 --layers 16..32
sofuu swarm leader --model ./model.gguf --workers 192.168.1.50:9001 "Hello"

A worker holds a layer range and listens on a socket; the leader addresses one or more workers. Nothing here changes the auth story of sofuu serve, so do not expose a worker to an untrusted network.

Next

The API reference lists the sofuu.http, sofuu.ai, and sofuu.fetch shapes. Troubleshooting covers provider configuration, model-file failures, and MCP transport errors.