Skip to content

About

nanocode agents in C: the Rust SDK as one library, polled from a game thread

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

libnanocode

How It Works · Docs · SDK

nanocode agents in C: the Rust SDK compiled into one library with a header of five functions. It runs inside the host's process, a game engine's included. The game thread polls for messages, the library keeps to its own threads, and its memory comes from the allocator the game gives it.

The game thread sends commands and polls for messages, and libnanocode's own threads run the agent, call the model, run Code Mode cells and write SQLite

Interface

bool nanocode_set_allocator(const NanocodeAllocator *allocator);
NanocodeHandle *nanocode_start(const uint8_t *config, size_t length, NanocodeCallback callback, void *context);
void nanocode_send(NanocodeHandle *handle, const uint8_t *data, size_t length);
size_t nanocode_poll(NanocodeHandle *handle, size_t limit);
void nanocode_stop(NanocodeHandle *handle);

Everything that crosses is JSON, in the messages nanocode-swift uses.

Message Direction Meaning
{"id":1,"command":{"open":{…}}} in Open a session: provider, model, endpoint, credentials, instructions, host tools
{"id":2,"command":{"submit":{…}}} in Queue a turn. steer, cancel, page, settings, compact and close have the same shape
{"reply":2,"ok":…} out The answer to the command with that id, or "error"
{"event":{"session":…,"logged":[…]}} out The session's events, and "turn" when a turn ends
{"call":{"id":7,"kind":"tool","body":{…}}} out The agent calls a host tool. Answer with {"command":{"reply":{"id":7,"ok":…}}}

Sessions are journaled in nanocode.sqlite under data_dir. A turn the process died in resumes when its session is opened again.

In a Game

nanocode_start takes these beside data_dir. Each is optional.

Field Effect
poll Messages wait in a lock-free queue, and the callback runs only inside nanocode_poll, on the thread that calls it, for at most limit messages
threads, blocking_threads, thread_stack_bytes The runtime's worker threads, its cap on blocking threads and their stack size
sqlite_readers, sqlite_cache_bytes, sqlite_mmap_bytes SQLite's reader threads, page cache and memory map

nanocode_set_allocator, called before anything else, routes Rust's, QuickJS's and SQLite's allocations through the host's functions. The runtime never runs on the caller's thread, and only nanocode_stop waits, for the sessions to close.

Measured on an Apple M-series laptop with 10 cores (DECISIONS.md has the commands):

nanocode_poll in a 60 Hz loop during a turn 9 to 13 µs a frame, 125 µs at most
Threads 16 with threads: 1 and sqlite_readers: 0, 35 by default. 10 are QuickJS workers, one a core
Memory after a turn 1.8 MB through the allocator, 44 KB outside it
Stripped program with the library linked in 21.3 MB on macOS arm64, 9.9 MB gzipped

Build

scripts/sdk.sh      # checks the SDK out beside this repository
scripts/build.sh    # build/<target>/: libnanocode.a, the shared library, nanocode.h, link.txt

TARGET selects another Rust target. macOS arm64, Linux arm64 and Windows x86_64 (MinGW) were built and ran a turn. The Windows build ran only under Wine on an emulator, where some runs died, so it is not verified. The MSVC and console targets were not built.

Examples

  • examples/game is a 60 Hz loop that polls, counts its allocations and lets the agent call spawn_enemy on the game thread.
  • examples/turn takes messages on the library's threads.
  • examples/unreal/Nanocode is an Unreal Engine plugin: a game instance subsystem that polls on tick, with FMemory as the allocator. It was not compiled against the engine.
  • examples/mock.mjs is a model that answers both programs without a key: node examples/mock.mjs 8787, then game http://127.0.0.1:8787 <data_dir>.

License

MIT or Apache-2.0.

About

nanocode agents in C: the Rust SDK as one library, polled from a game thread

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages