Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

34.2 KB
Raw Blame
#include "jan_llama_shim.h"

#include "server-context.h"
#include "server-common.h"
#include "server-http.h"

#include "arg.h"
#include "preset.h"
#include "common.h"
#include "log.h"
#include "build-info.h"
#include "llama.h"
#include "ggml-backend.h"

#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <unordered_map>

#ifdef _WIN32
#  ifndef WIN32_LEAN_AND_MEAN
#    define WIN32_LEAN_AND_MEAN
#  endif
#  ifndef NOMINMAX
#    define NOMINMAX
#  endif
#  include <windows.h>
#endif

struct jan_llama_engine;

namespace {

#ifdef _WIN32
bool is_ascii(const std::wstring & s) {
    for (wchar_t c : s) {
        if (c >= 0x80) {
            return false;
        }
    }
    return true;
}

/// Restores the process working directory when it goes out of scope, so an
/// exception from the loader cannot leave the engine in the wrong directory.
struct cwd_guard {
    std::wstring previous;
    ~cwd_guard() {
        SetCurrentDirectoryW(previous.c_str());
    }
};
#endif

/// `common_preset_context::load_from_ini` takes a narrow `std::string` and
/// opens it with `std::filesystem::exists` and `std::ifstream`. On Windows
/// both decode a narrow string with the ANSI code page, not UTF-8, so an ini
/// whose path holds a non-ASCII character (a Cyrillic user name is enough)
/// never resolves and the loader throws "preset file does not exist" for a
/// file that is there. For such a path the process switches into the ini's
/// directory for the duration of the load and hands upstream the bare
/// file name, which every code page reads the same way. The directory is
/// changed through the wide API, which does not depend on the code page; a
/// `\\?\` prefix is dropped first because the working directory cannot be a
/// verbatim path. The lock keeps two engine starts from interleaving their
/// directory changes.
///
/// This only covers the ini. jan-llama-worker embeds a UTF-8 active-code-page
/// manifest (build.rs), which makes every narrow path upstream opens -- model,
/// `slot-save-path`, templates -- resolve too, so the working-directory switch
/// is skipped when it is in effect and remains for Windows before 1903, which
/// ignores the manifest.
common_presets load_ini_presets(const common_preset_context & pctx, const char * ini_path, common_preset & global) {
#ifdef _WIN32
    static std::mutex cwd_mutex;

    int wide_len = GetACP() == CP_UTF8
                       ? 0
                       : MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, ini_path, -1, nullptr, 0);
    if (wide_len > 1) {
        std::wstring wide(static_cast<size_t>(wide_len), L'\0');
        MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, ini_path, -1, wide.data(), wide_len);
        wide.resize(static_cast<size_t>(wide_len) - 1);

        if (!is_ascii(wide)) {
            if (wide.rfind(L"\\\\?\\UNC\\", 0) == 0) {
                wide = L"\\\\" + wide.substr(8);
            } else if (wide.rfind(L"\\\\?\\", 0) == 0) {
                wide = wide.substr(4);
            }

            size_t sep = wide.find_last_of(L"\\/");
            if (sep != std::wstring::npos && sep > 0) {
                std::wstring dir  = wide.substr(0, sep);
                std::wstring file = wide.substr(sep + 1);
                if (dir.size() == 2 && dir[1] == L':') {
                    dir += L'\\';  // "C:" alone means "the current directory on C:"
                }
                if (!file.empty() && is_ascii(file)) {
                    std::lock_guard<std::mutex> lock(cwd_mutex);
                    DWORD need = GetCurrentDirectoryW(0, nullptr);
                    if (need > 0) {
                        cwd_guard guard;
                        guard.previous.resize(need);
                        DWORD got = GetCurrentDirectoryW(need, guard.previous.data());
                        guard.previous.resize(got);
                        if (got > 0 && SetCurrentDirectoryW(dir.c_str())) {
                            return pctx.load_from_ini(std::string(file.begin(), file.end()), global);
                        }
                    }
                }
            }
        }
    }
#endif
    return pctx.load_from_ini(ini_path, global);
}

/// Mirrors upstream's `ex_wrapper` (tools/server/server.cpp), which every route
/// is registered behind. `build_table` captures the *unwrapped* handler members,
/// so the two things that wrapper does have to be done here instead: map
/// `std::invalid_argument` and `common_json_error` to 400 rather than 500, and build the body with the
/// JSON library. Upstream's own messages quote the offending field
/// (`response_format type must be one of "text" or "json_object"`), and a
/// nlohmann parse error echoes the caller's bytes, so concatenating `e.what()`
/// into a string literal produced unparseable JSON and let a client inject
/// quotes into the response.
server_http_res_ptr error_response(const std::string & message, error_type type) {
    auto res = std::make_unique<server_http_res>();
    res->status = 500;
    try {
        json error_data = format_error_response(message, type);
        res->status = json_value(error_data, "code", 500);
        res->data   = safe_json_to_str({{ "error", error_data }});
    } catch (const std::exception & e) {
        LOG_ERR("failed to format an error response: %s | for: %s\n", e.what(), message.c_str());
        res->data = R"({"error":{"message":"internal server error","type":"server_error","code":500}})";
    }
    return res;
}

void set_err(char * err, size_t err_len, const std::string & msg) {
    if (err == nullptr || err_len == 0) {
        return;
    }
    const size_t n = std::min(msg.size(), err_len - 1);
    std::memcpy(err, msg.data(), n);
    err[n] = '\0';
}

jan_llama_engine * finish_start(std::unique_ptr<jan_llama_engine> engine,
                                char * err, size_t err_len);

// `server_context::load_model` returns a bool and puts the reason -- a CUDA
// OOM, an unsupported arch, a missing backend -- only in the log, so a caller
// that reports the bool alone cannot tell those apart. These collect
// ERROR-level lines while a load runs so `set_err` can carry the cause.
//
// The first lines are kept rather than the last: the cause comes first
// (`cudaMalloc failed: out of memory`) and everything after it is fallout, and
// keeping the head also stops an unrelated error from a model already serving
// in this process from evicting it.
constexpr size_t LOG_CAPTURE_MAX_LINES = 8;

std::mutex               g_capture_mu;
std::vector<std::string> g_captured;
bool                     g_capturing = false;

// Metal's device-init probe logs an ERROR when the Metal 4 tensor API is
// unavailable, then disables it and continues -- benign, so keep it out of the
// captured cause instead of surfacing it as a load failure.
bool is_benign_probe_error(const std::string & line) {
    return line.find("ggml_metal_library_init_from_source") != std::string::npos;
}

void capture_log_callback(ggml_log_level level, const char * text, void * user_data) {
    if (level == GGML_LOG_LEVEL_ERROR && text != nullptr) {
        std::lock_guard<std::mutex> lock(g_capture_mu);
        if (g_capturing && g_captured.size() < LOG_CAPTURE_MAX_LINES) {
            std::string line(text);
            while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) {
                line.pop_back();
            }
            if (!line.empty() && !is_benign_probe_error(line)) {
                g_captured.push_back(std::move(line));
            }
        }
    }
    common_log_default_callback(level, text, user_data);
}

// Scoped so every exit from the load -- including a throw -- stops capturing.
struct log_capture {
    log_capture() {
        std::lock_guard<std::mutex> lock(g_capture_mu);
        g_captured.clear();
        g_capturing = true;
    }

    ~log_capture() { finish(""); }

    std::string message(const std::string & summary) { return finish(summary); }

private:
    std::string finish(const std::string & summary) {
        std::lock_guard<std::mutex> lock(g_capture_mu);
        g_capturing = false;
        std::string out = summary;
        for (const auto & line : g_captured) {
            out += "; ";
            out += line;
        }
        g_captured.clear();
        return out;
    }
};

// Device descriptions are vendor strings and have carried backslashes and
// quotes; emitting them raw would produce JSON the Rust side cannot parse.
std::string json_quote(const char * raw) {
    std::string out = "\"";
    for (const char * p = raw == nullptr ? "" : raw; *p != '\0'; ++p) {
        const unsigned char c = static_cast<unsigned char>(*p);
        switch (c) {
            case '"':  out += "\\\""; break;
            case '\\': out += "\\\\"; break;
            case '\n': out += "\\n";  break;
            case '\r': out += "\\r";  break;
            case '\t': out += "\\t";  break;
            default:
                if (c < 0x20) {
                    char esc[7];
                    std::snprintf(esc, sizeof(esc), "\\u%04x", c);
                    out += esc;
                } else {
                    out += static_cast<char>(c);
                }
        }
    }
    out += '"';
    return out;
}

// llama.cpp reads slot actions from the query string, so the shim has to hand
// it a param map. Percent-decoding is included because a value is not
// guaranteed to be url-safe -- ours are ints and fixed keywords, but a caller
// with a filename would silently get the escaped form otherwise.
int hex_value(unsigned char c) {
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    return -1;
}

std::string url_decode(const std::string & in) {
    std::string out;
    out.reserve(in.size());
    for (size_t i = 0; i < in.size(); ++i) {
        if (in[i] == '+') {
            out += ' ';
            continue;
        }
        if (in[i] == '%' && i + 2 < in.size()) {
            const int hi = hex_value(static_cast<unsigned char>(in[i + 1]));
            const int lo = hex_value(static_cast<unsigned char>(in[i + 2]));
            if (hi >= 0 && lo >= 0) {
                out += static_cast<char>(hi * 16 + lo);
                i += 2;
                continue;
            }
        }
        out += in[i];
    }
    return out;
}

std::map<std::string, std::string> parse_query(const char * query) {
    std::map<std::string, std::string> out;
    if (query == nullptr) {
        return out;
    }
    std::string q(query);
    size_t pos = 0;
    while (pos < q.size()) {
        const size_t amp = q.find('&', pos);
        const std::string pair = q.substr(pos, amp == std::string::npos ? std::string::npos : amp - pos);
        pos = amp == std::string::npos ? q.size() : amp + 1;
        if (pair.empty()) {
            continue;
        }
        const size_t eq = pair.find('=');
        if (eq == std::string::npos) {
            out[url_decode(pair)] = "";
        } else {
            out[url_decode(pair.substr(0, eq))] = url_decode(pair.substr(eq + 1));
        }
    }
    return out;
}

} // namespace

struct jan_llama_engine {
    common_params params;
    server_context ctx;
    jan_llama_state_cb on_state = nullptr;
    void *             state_ud = nullptr;
    std::unique_ptr<server_routes> routes;
    std::thread loop;
    std::atomic<bool> running{false};
    // Set once start_loop() has returned, i.e. the loop is provably finished.
    // stop() polls this because server_queue::start_loop sets `running = true`
    // at its top (server-queue.cpp:279): a terminate() that lands before the
    // loop thread reaches that line is erased, and the loop then blocks
    // forever. Re-issuing terminate() until the thread exits is the only
    // ordering-independent fix.
    std::atomic<bool> loop_done{false};

    std::unordered_map<std::string, server_http_context::handler_t *> table;

    void build_table() {
        auto & r = *routes;
        table = {
            {"get_health",                 &r.get_health},
            {"get_metrics",                &r.get_metrics},
            {"get_slots",                  &r.get_slots},
            {"post_slots",                 &r.post_slots},
            {"get_props",                  &r.get_props},
            {"post_props",                 &r.post_props},
            {"post_infill",                &r.post_infill},
            {"post_completions",           &r.post_completions},
            {"post_completions_oai",       &r.post_completions_oai},
            {"post_chat_completions",      &r.post_chat_completions},
            {"post_chat_completions_tok",  &r.post_chat_completions_tok},
            {"post_control",               &r.post_control},
            {"post_responses_oai",         &r.post_responses_oai},
            {"post_apply_template",        &r.post_apply_template},
            {"get_models",                 &r.get_models},
            {"post_tokenize",              &r.post_tokenize},
            {"post_detokenize",            &r.post_detokenize},
            {"post_embeddings",            &r.post_embeddings},
            {"post_embeddings_oai",        &r.post_embeddings_oai},
            {"post_rerank",                &r.post_rerank},
            {"get_lora_adapters",          &r.get_lora_adapters},
            {"post_lora_adapters",         &r.post_lora_adapters},
        };
    }
};

// server_routes::handle_completions_impl (server-context.cpp) does
// `res->set_req(&req)`, and server_res_spipe keeps
// that pointer for the life of the stream -- so the request, and the
// std::function its `should_stop` member references, must outlive the response.
// Declaration order here is the destruction order that guarantees it.
struct jan_llama_response {
    std::atomic<bool> cancel{false};
    std::function<bool()> should_stop;
    std::unique_ptr<server_http_req> req;
    server_http_res_ptr res;

    std::string chunk;         // owns the buffer handed back across the ABI
    bool drained = false;      // next() reported the last chunk
    bool completed = false;    // on_complete() has run

    void finish() {
        if (res && !completed) {
            res->on_complete();
            completed = true;
        }
    }

    ~jan_llama_response() {
        finish();
    }
};

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) {
    try {
        auto engine = std::make_unique<jan_llama_engine>();
        engine->on_state = on_state;
        engine->state_ud = user_data;

        // common_params_parse wants char**; it does not retain the pointers.
        std::vector<std::string> owned;
        owned.reserve(argc);
        for (int i = 0; i < argc; i++) {
            owned.emplace_back(argv[i] ? argv[i] : "");
        }
        std::vector<char *> raw;
        raw.reserve(argc);
        for (auto & s : owned) {
            raw.push_back(const_cast<char *>(s.c_str()));
        }

        if (!common_params_parse(argc, raw.data(), engine->params, LLAMA_EXAMPLE_SERVER)) {
            set_err(err, err_len, "failed to parse server arguments");
            return nullptr;
        }

        return finish_start(std::move(engine), err, err_len);
    } catch (const std::exception & e) {
        set_err(err, err_len, e.what());
        return nullptr;
    } catch (...) {
        set_err(err, err_len, "unknown exception while starting the engine");
        return nullptr;
    }
}

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) {
    try {
        if (ini_path == nullptr || preset_name == nullptr) {
            set_err(err, err_len, "ini_path and preset_name are required");
            return nullptr;
        }
        auto engine = std::make_unique<jan_llama_engine>();
        engine->on_state = on_state;
        engine->state_ud = user_data;

        common_preset_context pctx(LLAMA_EXAMPLE_SERVER);
        // The ini is shared with the router, which owns keys like
        // load-on-startup that are not common_params options.
        pctx.ignore_unknown_keys = true;

        // apply_to_params only writes the keys the ini names, so the base has
        // to be the example's own defaults rather than a default-constructed
        // common_params -- otherwise the per-example values
        // common_params_parser_init sets (for SERVER, n_parallel = -1 = auto)
        // are silently dropped and an unset key means something different here
        // than it does on the command line. pctx already built them.
        engine->params = pctx.default_params;

        common_preset global;
        common_presets presets = load_ini_presets(pctx, ini_path, global);

        auto it = presets.find(preset_name);
        if (it == presets.end()) {
            set_err(err, err_len, std::string("no such preset: ") + preset_name);
            return nullptr;
        }

        // [*] first, then the named section overrides it.
        global.apply_to_params(engine->params);
        it->second.apply_to_params(engine->params);

        // The router starts every child with `--alias <section name>`
        // (server-models.cpp, update_args), and server_context answers with the
        // first alias as the model name. Nothing here is a router, so without
        // this it falls back to the model *file path* -- which is what API
        // clients read back as `model` (`\\?\C:\...\model.gguf` on Windows).
        // Replaced rather than added to: the router does the same, and the
        // first member of a set of aliases is the one that gets used.
        engine->params.model_alias.clear();
        engine->params.model_alias.insert(preset_name);

        // common_params_parse does this in common_params_parse_ex (arg.cpp);
        // apply_to_params does not. Without it n_threads stays 0 and ggml_threadpool_new_impl
        // memsets a zero-length array -> SIGSEGV. The router never hits this
        // because it forwards keys to a child that re-parses them.
        postprocess_cpu_params(engine->params.cpuparams, nullptr);
        postprocess_cpu_params(engine->params.cpuparams_batch, &engine->params.cpuparams);
        postprocess_cpu_params(engine->params.speculative.draft.cpuparams,
                               &engine->params.cpuparams);
        postprocess_cpu_params(engine->params.speculative.draft.cpuparams_batch,
                               &engine->params.cpuparams_batch);

        // common_params_parse_ex, added in 0.4.1: an unset `-mmdev` follows
        // `--device` rather than falling back to auto-placement. Without it a preset that
        // pins the model to one GPU would still let the projector land wherever
        // ggml chose, which is the split the setting exists to prevent.
        if (engine->params.mmproj_use_gpu && engine->params.mmproj_device == nullptr &&
            !engine->params.devices.empty()) {
            engine->params.mmproj_device  = engine->params.devices.front();
            engine->params.mmproj_use_gpu = engine->params.mmproj_device != nullptr;
        }

        // Same omission, common_params_parse_ex: common_params_fit needs spare
        // slots to write its overrides into and throws "did not provide buffer to
        // set tensor_buft_overrides" without them, so auto-fit silently gave
        // up and the load then asked for more VRAM than the device had.
        const size_t ntbo = llama_max_tensor_buft_overrides();
        while (engine->params.tensor_buft_overrides.size() < ntbo) {
            engine->params.tensor_buft_overrides.push_back({nullptr, nullptr});
        }
        if (!engine->params.speculative.draft.tensor_buft_overrides.empty()) {
            engine->params.speculative.draft.tensor_buft_overrides.push_back({nullptr, nullptr});
        }

        // common_params_parse_ex. llama_model_params::kv_overrides is a C array
        // walked until an entry with an empty key, so a hand-edited `override-kv` in
        // the ini would otherwise be read past its end.
        if (!engine->params.kv_overrides.empty()) {
            engine->params.kv_overrides.emplace_back();
            engine->params.kv_overrides.back().key[0] = 0;
        }

        // The end of common_params_parse_ex (arg.cpp). Since 0.4.0 the
        // kwarg defaults to on when the user did not name it, and a template
        // with `supports_preserve_reasoning` reads it -- so omitting this here
        // would have the preset path preserve less reasoning than the argv path
        // and than llama-server, for the same ini.
        if (!engine->params.default_template_kwargs.count("preserve_reasoning")) {
            engine->params.default_template_kwargs["preserve_reasoning"] = "true";
        }

        return finish_start(std::move(engine), err, err_len);
    } catch (const std::exception & e) {
        set_err(err, err_len, e.what());
        return nullptr;
    } catch (...) {
        set_err(err, err_len, "unknown exception while starting from preset");
        return nullptr;
    }
}

namespace {

jan_llama_engine * finish_start(std::unique_ptr<jan_llama_engine> engine,
                                char * err, size_t err_len) {
    try {
        // server_prepare_params (server.cpp), in llama-server's startup --
        // which neither entry point here goes through. n_parallel = -1 is the SERVER default, and
        // server_context loops `for (i = 0; i < n_parallel; i++)` to build its
        // slots, so leaving the sentinel unresolved yields an engine with no
        // slots at all and every request waiting forever.
        if (engine->params.n_parallel < 0) {
            engine->params.n_parallel = 4;
            engine->params.kv_unified = true;
        }

        // server_prepare_params, immediately after the block above and
        // dependent on it: the pool is n_parallel wide, so it can only be sized once the
        // sentinel is resolved. Only `-c 0` (size to the model's trained
        // context) leaves n_ctx at 0 for this to act on; any explicit ctx-size
        // pins the pool and the cap then applies per slot inside it.
        if (engine->params.kv_unified_per_slot > 0 && engine->params.n_ctx == 0 &&
            (uint32_t) engine->params.fit_params_min_ctx != UINT32_MAX) {
            engine->params.n_ctx = engine->params.n_parallel * engine->params.kv_unified_per_slot;
        }

        common_init();
        // After common_init, which installs its own callback.
        llama_log_set(capture_log_callback, nullptr);
        llama_backend_init();
        llama_numa_init(engine->params.numa);

        // Must be constructed before load_model: its ctor registers the
        // on_sleeping_state callback the sleep path depends on.
        engine->routes = std::make_unique<server_routes>(engine->params, engine->ctx);

        // Real load progress: server_context reports it here (and nowhere a
        // caller could poll for it), throttled to one sample per 200ms with
        // the first and last always emitted.
        if (engine->on_state != nullptr) {
            auto * cb = engine->on_state;
            void * ud = engine->state_ud;
            engine->ctx.set_state_callback([cb, ud](server_state st, json payload) {
                const std::string state = server_state_to_str(st);
                const std::string body  = payload.dump();
                cb(state.c_str(), body.c_str(), ud);
            });
        }

        log_capture capture;
        if (!engine->ctx.load_model(engine->params)) {
            set_err(err, err_len, capture.message("failed to load model"));
            return nullptr;
        }

        engine->routes->update_meta(engine->ctx);
        engine->build_table();

        // start_loop blocks, so it gets its own thread and requests arrive from
        // the caller's threads via server_queue.
        engine->running.store(true);
        auto * raw_engine = engine.get();
        engine->loop = std::thread([raw_engine]() {
            try {
                raw_engine->ctx.start_loop();
            } catch (const std::exception & e) {
                LOG_ERR("server loop threw: %s\n", e.what());
            } catch (...) {
                LOG_ERR("%s", "server loop threw an unknown exception\n");
            }
            raw_engine->running.store(false);
            raw_engine->loop_done.store(true);
        });

        return engine.release();
    } catch (const std::exception & e) {
        set_err(err, err_len, e.what());
        return nullptr;
    } catch (...) {
        set_err(err, err_len, "unknown exception while finishing startup");
        return nullptr;
    }
}

} // namespace

int jan_llama_engine_stop(jan_llama_engine * engine) {
    if (engine == nullptr) {
        return 0;
    }
    // Re-issue terminate until the loop actually exits: a single call can be
    // lost to the `running = true` at the top of server_queue::start_loop.
    constexpr int  STOP_POLLS    = 200;         // ~10s ceiling
    constexpr auto STOP_INTERVAL = std::chrono::milliseconds(50);
    for (int i = 0; i < STOP_POLLS && !engine->loop_done.load(); ++i) {
        try {
            engine->ctx.terminate();
        } catch (...) {
            // best-effort; the join below is what actually decides
        }
        if (engine->loop_done.load()) {
            break;
        }
        std::this_thread::sleep_for(STOP_INTERVAL);
    }
    if (!engine->loop_done.load()) {
        LOG_ERR("%s", "server loop did not stop; leaking it rather than "
                      "blocking teardown forever\n");
        engine->loop.detach();
        // The engine is deliberately not deleted: the detached loop still
        // references it. Leaking one object on a shutdown path we already
        // failed is better than a use-after-free.
        //
        // Reported to the caller so it applies the same policy to the state
        // callback it owns: the lambda installed in `ctx` captured that raw
        // pointer by value, and the detached loop still reaches it through
        // `handle_sleeping_state`, so freeing it here would be the
        // use-after-free this branch exists to avoid.
        return 1;
    }
    if (engine->loop.joinable()) {
        engine->loop.join();
    }
    delete engine;
    llama_backend_free();
    return 0;
}

jan_llama_response * jan_llama_engine_request(jan_llama_engine * engine,
                                              const char *       route,
                                              const char *       query,
                                              const char *       body,
                                              size_t             body_len) {
    auto out = new jan_llama_response();
    try {
        if (engine == nullptr || route == nullptr) {
            out->res = error_response("engine or route is null", ERROR_TYPE_SERVER);
            return out;
        }

        auto it = engine->table.find(route);
        if (it == engine->table.end()) {
            out->res = error_response(std::string("unknown route: ") + route,
                                      ERROR_TYPE_NOT_FOUND);
            return out;
        }

        out->should_stop = [cancel = &out->cancel]() { return cancel->load(); };
        out->req.reset(new server_http_req{
            /* params       */ parse_query(query),
            /* headers      */ {},
            /* path         */ route,
            /* query_string */ query ? query : "",
            /* body         */ std::string(body ? body : "", body_len),
            /* files        */ {},
            /* should_stop  */ out->should_stop,
        });

        out->res = (*it->second)(*out->req);
        if (!out->res) {
            out->res = error_response("handler returned no response", ERROR_TYPE_SERVER);
        }
        return out;
    } catch (const std::invalid_argument & e) {
        // Upstream's own mapping: a malformed request is the client's to fix, so
        // it must not look retryable. server-common.cpp throws this from 23
        // sites (missing `messages`, bad `content` type, `top_logprobs` without
        // `logprobs`, ...), all of which reached Jan's API as 500 before.
        out->res = error_response(e.what(), ERROR_TYPE_INVALID_REQUEST);
        return out;
    } catch (const common_json_error & e) {
        // Since 0.7.0 upstream maps JSON parse and type errors to 400 too
        // (server.cpp ex_wrapper); it derives from std::runtime_error, so it
        // must be caught before the std::exception arm below.
        out->res = error_response(e.what(), ERROR_TYPE_INVALID_REQUEST);
        return out;
    } catch (const std::exception & e) {
        out->res = error_response(e.what(), ERROR_TYPE_SERVER);
        return out;
    } catch (...) {
        out->res = error_response("unknown error", ERROR_TYPE_SERVER);
        return out;
    }
}

int jan_llama_response_status(const jan_llama_response * res) {
    return (res && res->res) ? res->res->status : 500;
}

const char * jan_llama_response_content_type(const jan_llama_response * res) {
    return (res && res->res) ? res->res->content_type.c_str() : "";
}

const char * jan_llama_response_body(const jan_llama_response * res, size_t * len) {
    if (!res || !res->res) {
        if (len) *len = 0;
        return "";
    }
    if (len) *len = res->res->data.size();
    return res->res->data.c_str();
}

int jan_llama_response_is_stream(const jan_llama_response * res) {
    return (res && res->res && res->res->is_stream()) ? 1 : 0;
}

int jan_llama_response_next(jan_llama_response * res, const char ** chunk, size_t * len) {
    if (!res || !res->res || !res->res->next) {
        return 0;
    }
    if (res->drained) {
        return 0;
    }
    try {
        res->chunk.clear();
        const bool more = res->res->next(res->chunk);
        if (chunk) *chunk = res->chunk.c_str();
        if (len)   *len   = res->chunk.size();
        if (!more) {
            // Last call: hand back any trailing bytes now and report completion
            // on the next call, so a final chunk is never dropped.
            res->drained = true;
            res->finish();
            return res->chunk.empty() ? 0 : 1;
        }
        return 1;
    } catch (...) {
        res->drained = true;
        return -1;
    }
}

void jan_llama_response_cancel(jan_llama_response * res) {
    if (res) {
        res->cancel.store(true);
    }
}

void jan_llama_response_free(jan_llama_response * res) {
    delete res;
}

void jan_llama_load_backends(const char * dir) {
    try {
        if (dir == nullptr || *dir == '\0') {
            ggml_backend_load_all();
        } else {
            ggml_backend_load_all_from_path(dir);
        }
    } catch (...) {
        // A backend module that throws while loading must not abort startup;
        // the engine degrades to whatever did register.
    }
}

// Mirrors common_print_available_devices (common/arg.cpp): load every
// backend, then report the non-CPU devices with memory in MiB. The CPU device
// is excluded there too -- "offload to CPU" is not a choice the setting offers.
char * jan_llama_devices_json(void) {
    constexpr size_t MiB = 1024 * 1024;
    std::string out = "[";
    try {
        bool first = true;
        for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
            ggml_backend_dev_t dev = ggml_backend_dev_get(i);
            if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
                continue;
            }
            size_t free_mem = 0;
            size_t total    = 0;
            ggml_backend_dev_memory(dev, &free_mem, &total);
            if (!first) {
                out += ',';
            }
            first = false;
            out += "{\"id\":";
            out += json_quote(ggml_backend_dev_name(dev));
            out += ",\"name\":";
            out += json_quote(ggml_backend_dev_description(dev));
            out += ",\"mem\":" + std::to_string(total / MiB);
            out += ",\"free\":" + std::to_string(free_mem / MiB) + "}";
        }
    } catch (...) {
        // A backend module that throws while probing must not abort the
        // process: an empty list reads as "no GPU", which is recoverable.
        out = "[";
    }
    out += ']';

    char * buf = static_cast<char *>(std::malloc(out.size() + 1));
    if (buf == nullptr) {
        return nullptr;
    }
    std::memcpy(buf, out.c_str(), out.size() + 1);
    return buf;
}

// jan_llama_devices_json without the memory query, which is the only part that
// touches the GPU; the id and description are read from ggml's registry.
char * jan_llama_device_identities_json(void) {
    std::string out = "[";
    try {
        bool first = true;
        for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
            ggml_backend_dev_t dev = ggml_backend_dev_get(i);
            if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
                continue;
            }
            if (!first) {
                out += ',';
            }
            first = false;
            out += "{\"id\":";
            out += json_quote(ggml_backend_dev_name(dev));
            out += ",\"name\":";
            out += json_quote(ggml_backend_dev_description(dev));
            out += '}';
        }
    } catch (...) {
        // Same stance as jan_llama_devices_json: an empty list is recoverable.
        out = "[";
    }
    out += ']';

    char * buf = static_cast<char *>(std::malloc(out.size() + 1));
    if (buf == nullptr) {
        return nullptr;
    }
    std::memcpy(buf, out.c_str(), out.size() + 1);
    return buf;
}

void jan_llama_string_free(char * s) {
    std::free(s);
}

// llama_version() is libllama's (include/llama.h); llama_build_number() and
// llama_commit() come from build-info.cpp in llama-common-base.
const char * jan_llama_version(void) {
    return llama_version();
}

int jan_llama_build_number(void) {
    return llama_build_number();
}

const char * jan_llama_commit(void) {
    return llama_commit();
}