Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
// C ABI over llama.cpp's server-context. Everything OpenAI-shaped -- chat
// templates, tool-call parsing, reasoning, sampling, slots, prompt cache,
// multimodal -- stays inside libserver-context.a; this only moves bytes.
#ifndef JAN_LLAMA_SHIM_H
#define JAN_LLAMA_SHIM_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct jan_llama_engine jan_llama_engine;
typedef struct jan_llama_response jan_llama_response;
// Load-lifecycle notifications, called on the thread driving the load (and
// later, for a sleep/resume, on the engine's own loop thread). `state` is
// llama.cpp's server state ("loading", "ready", ...) and `payload` its JSON --
// for "loading" that is {"stages":[..],"current":..,"value":0..1}, which is
// the only source of real load progress. Must not throw and must stay valid
// until the engine is stopped. NULL for no notifications.
typedef void (*jan_llama_state_cb)(const char * state,
const char * payload,
void * user_data);
// argv is llama-server's own flag set (common_params_parse), so callers reuse
// the upstream arg table instead of mirroring common_params across the FFI.
// Returns NULL on failure and writes a message into err.
jan_llama_engine * jan_llama_engine_start(const char * const * argv,
int argc,
jan_llama_state_cb on_state,
void * user_data,
char * err,
size_t err_len);
// Starts from a router.preset.ini section, exactly as Jan already generates it:
// the [*] section is applied first, then the named one overrides it.
jan_llama_engine * jan_llama_engine_start_from_preset(const char * ini_path,
const char * preset_name,
jan_llama_state_cb on_state,
void * user_data,
char * err,
size_t err_len);
// Returns 1 when the server loop did not stop in time and was detached, so the
// engine (and the state callback the detached loop can still reach) is leaked
// rather than freed; 0 on the clean join. The caller owns the `user_data` it
// passed to start, and must leak it too on a 1 -- freeing it would leave the
// detached loop calling into freed memory.
int jan_llama_engine_stop(jan_llama_engine * engine);
// route names match server_routes members, e.g. "post_chat_completions".
// Never returns NULL: transport failures come back as a 5xx response.
//
// `query` is a url query string ("id_slot=0&action=save"), parsed into the
// request's param map. llama.cpp reads slot actions and a few flags from there
// rather than from the body, so a route like post_slots is unreachable without
// it. NULL or "" means no params.
jan_llama_response * jan_llama_engine_request(jan_llama_engine * engine,
const char * route,
const char * query,
const char * body,
size_t body_len);
int jan_llama_response_status(const jan_llama_response * res);
const char * jan_llama_response_content_type(const jan_llama_response * res);
const char * jan_llama_response_body(const jan_llama_response * res, size_t * len);
int jan_llama_response_is_stream(const jan_llama_response * res);
// 1 = chunk written (valid until the next call on this response), 0 = finished,
// -1 = the generator threw. Empty chunks are legal and mean "nothing yet".
int jan_llama_response_next(jan_llama_response * res, const char ** chunk, size_t * len);
void jan_llama_response_free(jan_llama_response * res);
// Asks the in-flight generator to stop; safe to call from another thread.
void jan_llama_response_cancel(jan_llama_response * res);
// Registers the ggml compute backends. `dir` names a directory of backend
// modules; NULL uses ggml's own search (the executable's directory, then the
// cwd), which is what a packaged install wants.
//
// Only call this with a non-NULL `dir` when the executable has no backend
// modules beside it: ggml's loader has no already-loaded guard, so a directory
// pre-load followed by its internal search would register a backend twice.
void jan_llama_load_backends(const char * dir);
// The devices this build can offload to, as a JSON array of
// {id, name, mem, free} with memory in MiB -- the same fields the old
// `llama-server --list-devices` stdout was parsed into, so the Rust and TS
// sides are unchanged. Needs no engine: ggml devices are process-global.
// Never NULL; free the result with jan_llama_string_free.
char * jan_llama_devices_json(void);
// The same devices as {id, name} only, never asking one for its memory. A
// worker that stays up lists its devices with this: for a CUDA device the
// memory query in jan_llama_devices_json creates a context that the process
// then holds for as long as it runs. Names need no such call.
// Never NULL; free the result with jan_llama_string_free.
char * jan_llama_device_identities_json(void);
void jan_llama_string_free(char * s);
// ---- ggml op diagnostics (shim/jan_llama_diag.cpp) ----------------------
//
// llama.cpp's own tests/test-backend-ops.cpp, compiled unmodified, driven one
// device at a time. Like the device listing these need no engine, only the
// ggml backends to be registered (jan_llama_load_backends) beforehand.
// One JSON object per call, with no trailing newline. Every object has an
// "event" key: testing_start, backend_init, result, operation, summary,
// failed_tests, backend_status, overall_summary, log, and last of all `done`.
// Called on the thread that called jan_llama_diag_run, or on one of its
// parallel workers; calls never overlap. `line` is valid for the call only.
// Must not throw.
typedef void (*jan_llama_diag_cb)(const char * line, void * user_data);
#define JAN_LLAMA_DIAG_MODE_TEST 0 // compare against the CPU reference
#define JAN_LLAMA_DIAG_MODE_SUPPORT 1 // only probe supports_op
// Runs the cases of one device. `backend` is a ggml device name as listed by
// jan_llama_diag_backends_json ("CPU", "CUDA0", "Vulkan0"). `op_filter` is
// upstream's -o (comma separated op names, full case strings or regexes) and
// `params_filter` its -p (a regex searched in each case's params); NULL or ""
// means all. `workers` is upstream's -j (test mode only).
//
// Returns 1 when every case passed, 0 when one failed, -1 when nothing could
// run (the reason is in `err`, and in the final `done` event). Never throws.
// A crash inside a driver still kills the process: that is the caller's
// isolation to provide.
int jan_llama_diag_run(int mode,
const char * backend,
const char * op_filter,
const char * params_filter,
int workers,
jan_llama_diag_cb on_event,
void * user_data,
char * err,
size_t err_len);
// [{"op":"ADD","cases":N}, ...]: every testable ggml op and how many cases
// test mode would run for it (0 = upstream has no test). Builds the whole case
// list, so it takes a few seconds. Never NULL; free with jan_llama_string_free.
char * jan_llama_diag_ops_json(void);
// [{"name","description","type","reg"}] for every registered ggml device,
// CPU included, type being cpu/gpu/igpu/accel/meta. Never asks a device for
// its memory. Never NULL; free with jan_llama_string_free.
char * jan_llama_diag_backends_json(void);
// The llama.cpp the shim was compiled against, for the pin assertion in
// build.rs / engine::assert_pinned_version.
const char * jan_llama_version(void); // e.g. "1.0.0"
int jan_llama_build_number(void); // e.g. 12359
const char * jan_llama_commit(void);
#ifdef __cplusplus
}
#endif
#endif