Skip to content

armgit / janas

Public Imported from GitHub
0

37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.

93.7 KB
Raw Blame
/* SPDX-License-Identifier: GPL-3.0-or-later
   Copyright (C) 2026 Maurizio Cammalleri */
/*
 * janas-chat - a terminal chat with a model, on the public API of
 * libjanas_llm (include/janas/llm.h).
 *
 * On a terminal it takes the window: a banner, the conversation scrolling
 * under it, and a footer that keeps the model, the context used, the last
 * speed and the state of the expert cache under the eye. Lines are edited
 * and recalled with the arrows (src/chat/edit.h). Piped in or out it stays
 * the plain thing it was, for scripts and measurements.
 *
 * Lines starting with / are commands (/help lists them). A message may be
 * more than a line: a backslash and Enter add one, and the backslash is not
 * part of what is sent. Ctrl-C stops a reply, Ctrl-D quits.
 */
#define _XOPEN_SOURCE 700 /* wcwidth, for the width of what is printed */
#include <errno.h>
#include <locale.h>
#include <pthread.h>
#include <stdatomic.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdarg.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "common/devices.h"
#include "common/env_names.h"
#include "common/model_pick.h"
#include "common/sysinfo.h"
#include "common/words.h"
#include "edit.h"
#include "layout.h"
#include "janas/llm.h"
#include "markdown.h"
#include "mcp_chat.h"
#include "options.h"
#include "tune.h"
#include "scroll.h"
#include "term.h"

#ifndef JANAS_VERSION
#define JANAS_VERSION "unknown"
#endif

static volatile sig_atomic_t interrupted;
static volatile sig_atomic_t resized;
static struct janas_term term;
static struct janas_scroll conv; /* what the conversation printed */
static struct janas_hist history;
/* what the whole conversation has cost, so the bar can say the average and
   not only how the last reply went */
static double sess_tokens, sess_seconds;

/* The commands, for Tab and for the help. */
static const char *const commands[] = {
    "/help",    "/about", "/stats", "/reset",   "/system",   "/temp",
    "/spec",    "/think", "/mode",  "/experts", "/markdown", "/mcp",
    "/context", "/image", "/quit",  "/exit",    NULL};

static void on_sigwinch(int sig)
{
    (void)sig;
    resized = 1;
}

/* Windows has no SIGWINCH: the console tells (tty.c) */
static void on_resize(void)
{
    on_sigwinch(0);
}

/*
 * The footer is made of parts, and a part that does not fit is left out
 * rather than cut off at the edge. A big font is a narrow window, and the
 * line used to lose whatever came last in it - which was the speed, the
 * one number a reader watches. Each part says how long it is to be kept:
 * the speed goes last of all, the state of the cache first.
 */
#define FOOT_PARTS 8

struct foot {
    char text[112];
    int keep;
};

static void foot_add(struct foot *p, int *np, int keep, const char *fmt, ...)
{
    va_list ap;
    if (*np >= FOOT_PARTS)
        return;
    p[*np].keep = keep;
    va_start(ap, fmt);
    vsnprintf(p[*np].text, sizeof(p[*np].text), fmt, ap);
    va_end(ap);
    (*np)++;
}

/*
 * The system message and the tools, read ahead (janas_llm_chat_prepare)
 * while the user writes the first message: with Janas's services they are
 * some four thousand tokens, over a minute of a large model the first time
 * on it, and the first reply used to sit on "reading" all that while. On a
 * thread of its own, said on the terminal when it starts, and in the
 * footer, in the part kept longest, until it ends; a message sent before
 * then waits for it in plain sight.
 */
static struct {
    pthread_t th;
    int on; /* started, not joined */
    atomic_int busy, stop, done, total;
} prep;

/*
 * The NPU made ready in sight. The first blocks the NPU reads in a session
 * wait while its weights are copied and its graphs compiled - seconds to a
 * minute, inside janas_llm_chat_next, where the chat draws nothing - so a
 * watcher draws the footer meanwhile, which says how far it is
 * (janas_llm_npu_preparing). Only while the chat is in the engine
 * (in_engine), and under lock, which the chat takes to come out: it never
 * draws beside the chat's own writing, and the footer asks the engine then
 * only what may be asked from aside, as while the system message is read
 * ahead.
 */
static struct {
    pthread_t th;
    int on; /* started, not joined */
    pthread_mutex_t lock;
    atomic_int in_engine, stop;
    const janas_llm *llm;
} watch = {.lock = PTHREAD_MUTEX_INITIALIZER};

/* the chat goes into the engine for a pass that may make the NPU ready,
   and comes out of it: the watcher may draw in between */
static void engine_in(void)
{
    atomic_store(&watch.in_engine, 1);
}

static void engine_out(void)
{
    pthread_mutex_lock(&watch.lock);
    atomic_store(&watch.in_engine, 0);
    pthread_mutex_unlock(&watch.lock);
}

static int prep_pct(void)
{
    int t = atomic_load(&prep.total), d = atomic_load(&prep.done);
    return t > 0 ? (int)(100LL * d / t) : 0;
}

/* The line at the bottom: what is worth having under the eye while the
   conversation scrolls above it. */
/* what main asked for, read by the footer: the address of its own, so a
   /think is seen without a second copy that could fall out of step */
static const struct janas_llm_chat_params *asked;

static void footer(const janas_llm *llm, const janas_llm_chat *c,
                   const char *state)
{
    if (!term.tty)
        return;
    char name[96] = "";
    int32_t len;
    janas_llm_name(llm, name, sizeof(name), &len);
    /* while the system message is read ahead, the chat and the engine's
       settings are the reading thread's: only the cache's filling, which
       is made to be watched from aside, is asked */
    int reading = atomic_load(&prep.busy) || atomic_load(&watch.in_engine);
    struct janas_llm_chat_stats s = {.size = sizeof(s)};
    int have = c && !reading && janas_llm_chat_stats(c, &s) == JANAS_LLM_OK;
    int32_t used = 0, most = 0;
    if (!reading)
        janas_llm_experts(llm, &used, &most);
    int64_t done = 0, total = 0;
    janas_llm_preload(llm, &done, &total);

    struct foot part[FOOT_PARTS];
    int np = 0;
    if (name[0])
        foot_add(part, &np, 3, "%s", name);
    if (!reading) {
        foot_add(part, &np, 2, "%d bit", janas_llm_expert_bits(llm));
        foot_add(part, &np, 2, "%d/%d experts", used, most);
    } else if (!state) /* kept longest: it is what holds the reply back */
        foot_add(part, &np, 9, "reading the tools ahead %d%%", prep_pct());
    int32_t nd = 0, nt = 0;
    double ns = 0;
    if (janas_llm_npu_preparing(llm, &nd, &nt, &ns) == 1) { /* held back too */
        if (nt > 0)
            foot_add(part, &np, 9, "preparing the NPU %d/%d, %.0f s", (int)nd,
                     (int)nt, ns);
        else
            foot_add(part, &np, 9, "preparing the NPU, %.0f s", ns);
    }
    if (have && s.context_size)
        foot_add(part, &np, 4, "context %u/%u", s.context_used, s.context_size);
    /* only where it is on, and only where it means something: it is the one
       switch that changes how long a reply takes before a word of it shows */
    if (asked && asked->thinking != 0 && janas_llm_reasons(llm))
        foot_add(part, &np, 4, "reasoning");
    if (have && s.output_seconds > 0)
        foot_add(part, &np, 7, "%.1f tok/s",
                 s.output_tokens / s.output_seconds);
    if (sess_seconds > 0)
        foot_add(part, &np, 6, "avg %.1f tok/s", sess_tokens / sess_seconds);
    if (total > done)
        foot_add(part, &np, 1, "cache %d%%",
                 (int)(100 * done / (total ? total : 1)));
    if (state && *state)
        foot_add(part, &np, reading ? 9 : 5, "%s", state);

    /* drop the least worth keeping until what is left fits, and never the
       last one standing: one part cut off says more than an empty line */
    int in[FOOT_PARTS];
    for (int i = 0; i < np; i++)
        in[i] = 1;
    for (;;) {
        int width = 1, worst = -1, kept = 0;
        for (int i = 0; i < np; i++) {
            if (!in[i])
                continue;
            width +=
                (kept ? 2 : 0) + term_cols(part[i].text, strlen(part[i].text));
            kept++;
            if (worst < 0 || part[i].keep < part[worst].keep)
                worst = i;
        }
        if (width <= term.cols || kept < 2)
            break;
        in[worst] = 0;
    }

    char line[512];
    size_t n = 1;
    line[0] = ' ';
    for (int i = 0; i < np && n < sizeof(line); i++) {
        if (!in[i])
            continue;
        n += (size_t)snprintf(line + n, sizeof(line) - n, "%s%s",
                              n > 1 ? "  " : "", part[i].text);
    }
    if (n >= sizeof(line))
        n = sizeof(line) - 1;
    line[n] = 0;
    term_footer(&term, line);
}

/*
 * The footer while the editor waits for a key: the filling of the cache
 * goes on meanwhile, and a footer drawn only at the start and after each
 * reply showed whatever it was at that moment - 60% at one start, 0% at
 * the next - and kept it. Drawn again when the share changes, and once
 * more when the filling is over, so that it goes.
 */
static const janas_llm *idle_llm;
static const janas_llm_chat *idle_chat;

static int footer_idle(void)
{
    static int shown = -2, shown_npu = -2;
    int64_t done = 0, total = 0;
    janas_llm_preload(idle_llm, &done, &total);
    int pct = total > done ? (int)(100 * done / total) : -1;
    /* and the reading ahead's share, while it goes */
    pct = pct * 1000 + (atomic_load(&prep.busy) ? prep_pct() : 999);
    /* and the NPU's preparation, a second at a time */
    int32_t nd = 0;
    double ns = 0;
    int npu = janas_llm_npu_preparing(idle_llm, &nd, NULL, &ns) == 1
                  ? (int)nd * 10000 + (int)ns
                  : -1;
    if (pct == shown && npu == shown_npu)
        return 0;
    shown = pct;
    shown_npu = npu;
    footer(idle_llm, idle_chat, NULL);
    return 1;
}

static void on_sigint(int sig)
{
    (void)sig;
#ifdef _WIN32
    signal(SIGINT, on_sigint); /* its C runtime takes the handler back */
#endif
    interrupted = 1;
    mcpc_cancel(); /* a tool running stops too */
}

static void usage(void)
{
    fprintf(
        stderr,
        "usage: janas-chat [model] [options]\n"
        "  model            a .jns file, or a name of the catalog (none: the\n"
        "                   list, to choose one; a model not here yet is\n"
        "                   downloaded, after asking)\n"
        "  --quality <high|medium|low>\n"
        "                   the lowest quality the engine may choose to\n"
        "                   be faster where memory is short: low (the\n"
        "                   default) the experts at four bits, the light\n"
        "                   ones not in memory left out; medium: four\n"
        "                   bits, nothing left out; high: nothing\n"
        "                   lowered. With memory for every expert all\n"
        "                   three read alike. Asked for, low also takes\n"
        "                   what was measured faster on this model at a\n"
        "                   small cost (the head at four bits). The\n"
        "                   levers below (--bits, --head...) win over it\n"
        "  --models <dir>   where the models are (default: JANAS_MODELS,\n"
        "                   or ~/.local/share/janas/models)\n"
        "  --tune           measure again how this model runs fastest on\n"
        "                   this machine (asked once, on its first start)\n"
        "  --no-tune        neither ask nor use that measure\n"
        "  --mtp <file>     multi-token prediction file: an MTP block or\n"
        "                   Gemma 4's assistant (faster replies); without\n"
        "                   it, the one beside the model that fits it\n"
        "  --no-mtp         none, not even one found beside the model\n"
        "  --draft <file>   a small model of the same family to guess the\n"
        "                   next tokens; worth it on a dense model, where\n"
        "                   several can be checked in one pass for little\n"
        "                   more than one (Qwen3-0.6B for Qwen3-4B: 1.14x)\n"
        "  --ctx <tokens>   context length (default 16384)\n"
        "  --cache <GiB>    RAM for streamed experts (default: automatic)\n"
        "  --reserve <GiB>  memory left to other programs when the cache is\n"
        "                   automatic (default: a fifth of the machine's)\n"
        "  --bits <2|4|6>   bits per weight of the experts' down matrix\n"
        "                   (files that hold it in planes; default: the\n"
        "                   memory decides)\n"
        "  --head <4|6>     bits per weight of the output head: 4\n"
        "                   requantizes a six-bit one at start, 2-5%%\n"
        "                   faster and a little less exact (default: 6,\n"
        "                   as in the file)\n"
        "  --attention <exact|fast>\n"
        "                   the attention scores: the query as it is, or\n"
        "                   to sixteen bits with an exact integer sum,\n"
        "                   twice as fast for an error of 3e-5\n"
        "                   (default: fast above 16384 tokens of context)\n"
        "  --no-preload     do not fill the expert cache at start with\n"
        "                   the experts used most on this machine\n"
        "  --no-recap       when the context fills, forget the oldest\n"
        "                   exchanges without summing them up first\n"
        "  --system <text>  system message (default: the assistant is "
        "called Janas,\n"
        "                   and which model it thinks with; \"\" for "
        "none)\n"
        "  --temp <t>       temperature (default 0.7; 0: greedy)\n"
        "  --top-k <k>      (default 20)   --top-p <p> (default 0.8)\n"
        "  --min-p <p>      (default 0)    --seed <n> (default: random)\n"
        "  --max <tokens>   longest reply (default: no limit)\n"
        "  --no-spec        no speculative decoding\n"
        "  --mode <m>       auto (default), eco or max\n"
        "  --think <on|off> reasoning before replying, for models that do\n"
        "                   it (default off: it is slow, and a small model\n"
        "                   reasons at length about very little); a number\n"
        "                   N: on, at most N tokens of it, then the answer\n"
        "  --stats          speed report after every reply\n"
        "  --no-markdown    print the model's marks instead of "
        "reading them\n"
        "  --no-gpu         never give the GPU work, whatever the mode "
        "says\n"
        "  --prompt-on <devices>\n"
        "                   what reads the prompts: auto (default: what\n"
        "                   measures fastest here), cpu, gpu, npu, npu+gpu\n"
        "                   (both in each pass), or a list apart by commas\n"
        "                   for the engine to choose from, such as npu,gpu;\n"
        "                   the CPU takes part in every pass, the NPU in\n"
        "                   blocks of 64 tokens and more; a choice wins\n"
        "                   over the mode\n"
        "  --drafts-on <devices>\n"
        "                   the same for the passes that check guessed\n"
        "                   tokens (2 to 16)\n"
        "  --reply-on <devices>\n"
        "                   the same for a reply's tokens, one at a time\n"
        "  --npu-int8       the NPU reads prompts with eight-bit\n"
        "                   activations: faster, a little less exact\n"
        "                   (dense Qwen3 models)\n"
        "  --mcp-config <file>\n"
        "                   the MCP servers whose tools the model may call\n"
        "                   (default ~/.config/janas/mcp.json, in the\n"
        "                   format of the other clients: mcpServers)\n"
        "  --no-mcp         start no MCP server, Janas's services included\n"
        "  --no-services    start none of Janas's own services (janas-flights\n"
        "                   and the others beside janas-chat, started by\n"
        "                   default: their tools run without asking)\n"
        "  --all-tools      write all the services' tools into the system\n"
        "                   message, instead of a catalog the model opens a\n"
        "                   service from when a question needs it\n"
        "  --progress <s>   a line on stderr every s seconds while a long\n"
        "                   prompt is read, and one after each reply\n"
        "  --mcp-instructions\n"
        "                   add what the other MCP servers say of their tools to\n"
        "                   the system message, marked as theirs (off by\n"
        "                   default: it is text their authors wrote, which\n"
        "                   the model would read as instructions)\n"
        "  --mcp-auto       run the tools the model calls without asking\n"
        "                   (needed on a pipe, where nobody can answer)\n");
}

/*
 * The help, as a command and what it does under it rather than as two
 * columns: a column of descriptions is a column of wrapped fragments in a
 * narrow window, and the command is easier to find when it stands alone in
 * the colour of the program. The page lays the text out, so nothing here
 * decides where a line breaks.
 */
static const struct {
    const char *cmd; /* NULL: a paragraph of its own, at the end */
    const char *what;
} help_lines[] = {
    {"/help", "this list"},
    {"/about", "what this is, who wrote it, and what it is running on"},
    {"/stats [on|off]", "speed report after every reply (toggles)"},
    {"/context", "how full the context is, and with what"},
    {"/reset", "new conversation"},
    {"/system", "show the system message"},
    {"/system <text>", "new conversation with this system message"},
    {"/system off", "new conversation with no system message"},
    {"/temp <t>", "temperature (0: greedy)"},
    {"/image <file>",
     "a picture (PNG or JPEG) for your next message, for models that see "
     "(their vision file beside them)"},
    {"/spec [on|off]", "speculative decoding (toggles)"},
    {"/mode [auto|eco|max]", "power mode, and what the engine chose"},
    {"/experts [n]",
     "experts per token (fewer: faster, a little less accurate)"},
    {"/think [on|off|N]",
     "reasoning before replying, for models that do it (off by default: the "
     "bar says reasoning while it is on); N: on, at most N tokens of it, "
     "then the answer"},
    {"/markdown [on|off]", "read the model's marks, or print them"},
    {"/gpu [on|off]", "whether the GPU may be given work"},
    {"/mcp", "the MCP servers running and the tools they give the model"},
    {"/mcp auto [on|off]",
     "run the tools the model calls without asking first (off by default: "
     "every call is shown and waits for y, n or a - always that tool)"},
    {"/save-config [global]",
     "keep these settings for this model's next start (asks to overwrite); "
     "global: for every model"},
    {"/load-config [global]",
     "read them back, losing the current ones (asks first)"},
    {"/del-config [global]",
     "forget them: the next start uses the catalog's and the chat's own"},
    {"/quit", "exit (also Ctrl-D)"},
    {NULL, "A backslash and Enter add a line to the message (the backslash "
           "is not sent); Enter alone sends it. Ctrl-C stops a reply."},
    {NULL, "The arrows move in the message - up and down between its lines "
           "when it has more than one - and bring back earlier prompts from "
           "its first and last line (kept between sessions); Home, End, "
           "Ctrl-A/E/K/U/W and Ctrl-L work as usual."},
    {NULL, "Page Up and Page Down look back over the conversation, Ctrl-Home "
           "and Ctrl-End go to its beginning and its end; the line being "
           "written stays where it is, and sending brings the end back."}};

static void help(const janas_llm *llm)
{
    (void)llm;
    for (size_t i = 0; i < sizeof(help_lines) / sizeof(*help_lines); i++)
        if (help_lines[i].cmd)
            term_field(&term, help_lines[i].cmd, help_lines[i].what);
        else
            term_printf(&term, "%s\n\n", help_lines[i].what);
}

static int on_off(const char *arg, int current)
{
    if (strcmp(arg, "on") == 0)
        return 1;
    if (strcmp(arg, "off") == 0)
        return 0;
    return !current;
}

static void report(const janas_llm_chat *c)
{
    struct janas_llm_chat_stats s = {.size = sizeof(s)};
    if (janas_llm_chat_stats(c, &s) != JANAS_LLM_OK)
        return;
    double in = s.input_seconds > 0 ? s.input_tokens / s.input_seconds : 0;
    double out = s.output_seconds > 0 ? s.output_tokens / s.output_seconds : 0;
    term_printf(&term,
                "%s[input %u tokens, %.2f s, %.1f tok/s | output %u tokens, "
                "%.2f s, %.1f tok/s | total %.2f s | context %u/%u",
                T_USER(&term), s.input_tokens, s.input_seconds, in,
                s.output_tokens, s.output_seconds, out, s.total_seconds,
                s.context_used, s.context_size);
    if (s.drafted > 0)
        term_printf(&term, " | drafts %u/%u accepted", s.accepted, s.drafted);
    if (s.auto_off_passes > 0)
        term_printf(&term, " | drafts off %u passes (the engine judged)",
                    s.auto_off_passes);
    if (s.experts_used > 0)
        term_printf(&term,
                    " | experts %.1f%% cached, %.0f MB read, %.2f s waiting",
                    100.0 * (double)(s.experts_used - s.experts_read) /
                        (double)s.experts_used,
                    (double)s.bytes_read / 1e6, s.io_wait_seconds);
    term_printf(&term,
                " | first token %.2f s, prompt built in %.3f s, read with %d "
                "threads (%.1f s of processor), written with %d | context "
                "memory %.0f MB, peak %.0f MB",
                s.first_token_seconds, s.prepare_seconds, s.input_threads,
                s.input_cpu_seconds, s.output_threads,
                (double)s.context_bytes / 1e6, (double)s.peak_rss_bytes / 1e6);
    term_printf(&term, "]%s\n", T_RESET(&term));
}

/* The context, and what fills it: the last reply's prompt by its parts. */
static void context_page(const janas_llm_chat *c)
{
    struct janas_llm_chat_stats s = {.size = sizeof(s)};
    if (janas_llm_chat_stats(c, &s) != JANAS_LLM_OK || !s.context_size) {
        term_printf(&term, "nothing sent yet\n");
        return;
    }
    uint32_t free_tokens =
        s.context_size > s.context_used ? s.context_size - s.context_used : 0;
    term_printf(&term, "context %u of %u tokens (%.1f%%), %u free\n",
                s.context_used, s.context_size,
                100.0 * s.context_used / s.context_size, free_tokens);
    term_printf(&term,
                "last prompt %u tokens: system %u (tools %u), conversation "
                "%u, last message %u; %u read, %u already computed\n",
                s.prompt_tokens, s.prompt_system, s.prompt_tools,
                s.prompt_history, s.prompt_last, s.input_tokens,
                s.cached_tokens);
    term_printf(&term,
                "its keys and values take %.0f MB; when the context fills, "
                "the oldest exchanges go (summed up first, unless "
                "--no-recap)\n",
                (double)s.context_bytes / 1e6);
}

/*
 * What this is and what it is running on, in one page: the thing a reader
 * wants when they meet the program, and the thing a report of a problem
 * needs - the version, the model, the machine - which until now had to be
 * gathered by hand from three places.
 */
/*
 * What about() writes, caught instead of printed: the page has to be laid
 * out and scrolled, not thrown at the screen, and the terminal already
 * knows how to hand its output to somebody else (the conversation store
 * does the same).
 */
struct about_cap {
    char *buf;
    size_t n, cap;
};

static void about_sink(void *arg, const char *s, size_t n)
{
    struct about_cap *c = arg;
    if (c->n + n + 1 > c->cap) {
        size_t want = (c->n + n + 1) * 2;
        char *bigger = realloc(c->buf, want);
        if (!bigger)
            return;
        c->buf = bigger;
        c->cap = want;
    }
    memcpy(c->buf + c->n, s, n);
    c->n += n;
    c->buf[c->n] = 0;
}

static void about(const janas_llm *llm)
{
    char head[80];
    snprintf(head, sizeof(head), "Janas-Chat %s", JANAS_VERSION);
    const char *text[4] = {head, "Maurizio \"camauri\" Cammalleri, 2026",
                           "GPL-3.0-or-later", "github.com/prabanta-dev/janas"};
    const char *face[4] = {T_BOLD(&term), "", T_DIM(&term), T_DIM(&term)};
    term_beside(&term, text, face, 4, term.rows - 1, 1);
    term_printf(&term, "\n");

    char desc[1024];
    int32_t dl;
    if (janas_llm_describe(llm, desc, sizeof(desc), &dl) == JANAS_LLM_OK)
        term_field(&term, "model", desc);
    char cpu[128] = "";
    janas_cpu_name(cpu, sizeof(cpu));
    uint64_t total = janas_mem_total(), free_now = janas_mem_available();
    int mains = janas_on_mains();
    char machine[320];
    snprintf(machine, sizeof(machine),
             "%s, %ld threads, %.1f GiB of memory (%.1f free)%s",
             cpu[0] ? cpu : "processor unknown", sysconf(_SC_NPROCESSORS_ONLN),
             (double)total / (1 << 30), (double)free_now / (1 << 30),
             mains < 0 ? ""
             : mains   ? ", on mains"
                       : ", on battery");
    term_field(&term, "machine", machine);
    term_field(&term, "engine",
               "Janas-LLM " JANAS_VERSION ", C11, no dependency beyond the C "
               "library; the experts stream from the disk, so a model larger "
               "than the memory still runs");
}

/* A page as text, written by whoever draws it into a sink of our own. */
static char *catch_page(void (*draw)(const janas_llm *), const janas_llm *llm)
{
    struct about_cap c = {NULL, 0, 0};
    void (*sink)(void *, const char *, size_t) = term.sink;
    void *arg = term.sink_arg;
    term.sink = about_sink;
    term.sink_arg = &c;
    draw(llm);
    term.sink = sink;
    term.sink_arg = arg;
    return c.buf;
}

/* The default system message: the assistant is called Janas, the model is
   what it thinks with, and both are said, so that it guesses neither. Left
   to itself a model answers out of its training, and they very often claim
   to be somebody else's assistant. */
static char *default_system(const janas_llm *llm)
{
    int32_t n = 0;
    janas_llm_default_system(llm, NULL, 0, &n); /* the length */
    char *s = malloc((size_t)n + 1);
    if (s && janas_llm_default_system(llm, s, n + 1, &n) != JANAS_LLM_OK) {
        free(s);
        s = NULL;
    }
    return s;
}

/* A new conversation with this system message (NULL: none). */
/* What the MCP servers say of their tools, when --mcp-instructions asks
   for it: added after the system message, marked as theirs. */
static char *mcp_extra;

/* The system message, with the servers' instructions after it. */
static int32_t set_system(janas_llm_chat *c, const char *system)
{
    if (!mcp_extra)
        return system ? janas_llm_chat_system(c, system, -1) : JANAS_LLM_OK;
    size_t a = system ? strlen(system) : 0, b = strlen(mcp_extra);
    char *s = malloc(a + b + 3);
    if (!s)
        return JANAS_LLM_ENOMEM;
    snprintf(s, a + b + 3, "%s%s%s", system ? system : "", a ? "\n\n" : "",
             mcp_extra);
    int32_t rc = janas_llm_chat_system(c, s, -1);
    free(s);
    return rc;
}

/* What the reading ahead says, where the user sees it: in the
   conversation, or on stderr when the output is a pipe. */
static void prep_say(const char *colour, const char *fmt, ...)
    __attribute__((format(gnu_printf, 2, 3)));
static void prep_say(const char *colour, const char *fmt, ...)
{
    char text[512];
    va_list ap;
    va_start(ap, fmt);
    vsnprintf(text, sizeof(text), fmt, ap);
    va_end(ap);
    if (term.tty) /* a blank line after it: the prompt is not glued to it */
        term_printf(&term, "%s%s%s\n\n", colour, text, T_RESET(&term));
    else
        fprintf(stderr, "%s\n", text);
}

static double now_ms(void)
{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (double)ts.tv_sec * 1e3 + (double)ts.tv_nsec / 1e6;
}

static void *prep_main(void *arg)
{
    janas_llm_chat *c = arg;
    int32_t d = 0, t = 0;
    while (!atomic_load(&prep.stop) && janas_llm_chat_prepare(c, &d, &t) == 1) {
        atomic_store(&prep.done, d);
        atomic_store(&prep.total, t);
    }
    atomic_store(&prep.busy, 0);
    return NULL;
}

/* The reading ahead of the system message, begun: its first block here,
   which says how long it is, the rest on the thread. */
static void prep_start(janas_llm_chat *c)
{
    if (prep.on)
        return;
    int32_t d = 0, t = 0;
    double t0 = now_ms();
    engine_in(); /* the first block: the NPU may be made ready in it */
    int r = janas_llm_chat_prepare(c, &d, &t);
    engine_out();
    if (r < 0) {
        prep_say(T_WARN(&term),
                 "the system message could not be read "
                 "ahead: %s",
                 janas_llm_last_error());
        return;
    }
    if (r == 0) {
        if (t >= 1024 && now_ms() - t0 < 2000)
            prep_say(T_OK(&term),
                     "System message and tools ready: %d tokens, as read "
                     "in an earlier chat.",
                     (int)t);
        return;
    }
    atomic_store(&prep.done, d);
    atomic_store(&prep.total, t);
    atomic_store(&prep.stop, 0);
    atomic_store(&prep.busy, 1);
    if (pthread_create(&prep.th, NULL, prep_main, c) != 0) {
        atomic_store(&prep.busy, 0); /* the first reply reads it */
        return;
    }
    prep.on = 1;
    prep_say(T_USER(&term),
             "Reading the system message and the tools ahead: %d tokens, "
             "the first time on this model (later chats find them "
             "read). Write meanwhile: the status line shows how far it "
             "is, and the first reply starts when it is done; Ctrl-C "
             "while it waits leaves the rest to the reply.",
             (int)t);
}

/* The reading ahead, waited for in plain sight (the chat is the thread's
   until then); stop: the rest is left to the first reply. */
static void prep_wait(const janas_llm *llm, int stop)
{
    if (!prep.on)
        return;
    if (stop)
        atomic_store(&prep.stop, 1);
    while (atomic_load(&prep.busy)) {
        if (interrupted) {
            atomic_store(&prep.stop, 1);
            interrupted = 0;
        }
        char st[64];
        snprintf(st, sizeof(st), "%s the tools %d%%",
                 atomic_load(&prep.stop) ? "stopping the reading of"
                                         : "waiting for",
                 prep_pct());
        footer(llm, NULL, st);
        struct timespec ts = {0, 200 * 1000 * 1000};
        nanosleep(&ts, NULL);
    }
    pthread_join(prep.th, NULL);
    prep.on = 0;
}

static void *watch_main(void *arg)
{
    (void)arg;
    while (!atomic_load(&watch.stop)) {
        struct timespec ts = {0, 250 * 1000 * 1000};
        nanosleep(&ts, NULL);
        if (!atomic_load(&watch.in_engine) ||
            janas_llm_npu_preparing(watch.llm, NULL, NULL, NULL) != 1)
            continue;
        pthread_mutex_lock(&watch.lock);
        if (atomic_load(&watch.in_engine))
            footer(watch.llm, NULL, "");
        pthread_mutex_unlock(&watch.lock);
    }
    return NULL;
}

static void watch_start(const janas_llm *llm)
{
    watch.llm = llm;
    atomic_store(&watch.stop, 0);
    watch.on = pthread_create(&watch.th, NULL, watch_main, NULL) == 0;
}

static void watch_stop(void)
{
    if (!watch.on)
        return;
    atomic_store(&watch.stop, 1);
    pthread_join(watch.th, NULL);
    watch.on = 0;
}

/* janas_llm_chat_next, the watcher free to draw while it runs */
static int32_t engine_next(janas_llm_chat *c, char *buf, int32_t cap,
                           int32_t *len)
{
    engine_in();
    int32_t rc = janas_llm_chat_next(c, buf, cap, len);
    engine_out();
    return rc;
}

/*
 * A service's layout in the user's language, asked of the model on the
 * side (the conversation saved and put back): once a layout and language,
 * then kept. Said on the terminal, as it takes a while.
 */
static janas_llm_chat *aside_chat;

static int translate(const char *name, const char *system, const char *text,
                     char **out)
{
    if (!aside_chat)
        return -1;
    term_gap(&term);
    prep_say(T_USER(&term),
             "Writing the layout of %s's answers in your language (%s), "
             "once: it is kept in ~/.config/janas/layouts.",
             name, layout_lang());
    footer(idle_llm, aside_chat, "writing a layout");
    size_t cap = 32768;
    char *buf = malloc(cap);
    int32_t len = 0;
    if (!buf || janas_llm_chat_aside(aside_chat, system, -1, text, -1, 4096,
                                     buf, (int32_t)cap, &len) != JANAS_LLM_OK) {
        prep_say(T_WARN(&term), "the layout could not be written: %s",
                 buf ? janas_llm_last_error() : "out of memory");
        free(buf);
        return -1;
    }
    *out = buf;
    return 0;
}

/* The user's language, from the computer's (LANG=it_IT.UTF-8: it). */
static void user_lang(void)
{
    static const char *const vars[] = {"LC_ALL", "LC_MESSAGES", "LANG"};
    for (size_t i = 0; i < sizeof vars / sizeof *vars; i++) {
        const char *v = getenv(vars[i]);
        if (!v || !*v)
            continue;
        char l[16];
        size_t n = strcspn(v, "_.@");
        snprintf(l, sizeof l, "%.*s", (int)(n < 15 ? n : 15), v);
        int ok = n >= 2 && n <= 3;
        for (size_t k = 0; ok && k < n; k++)
            ok = l[k] >= 'a' && l[k] <= 'z';
        layout_set_lang(ok ? l : "en");
        return;
    }
    layout_set_lang("en");
}

static void restart(janas_llm_chat *c, const char *system)
{
    janas_llm_chat_reset(c);
    mcpc_close_services(); /* a new conversation opens them anew */
    janas_llm_chat_tools_add(c, NULL, 0);
    if (set_system(c, system) != JANAS_LLM_OK)
        term_printf(&term, "%s\n", janas_llm_last_error());
}

/* Reads one message (lines joined while they end with a backslash).
   Returns its length, or -1 at the end of the input. */
static long read_message(char **buf, size_t *cap, int tty)
{
    size_t n = 0;
    char *line = NULL;
    size_t lcap = 0;
    for (;;) {
        ssize_t l;
        if (tty) { /* our own editor, with the previous prompts */
            static char edited[8192];
            int r = line_read(&term, n == 0 ? "\u203a " : "  ", edited,
                              sizeof(edited), &history, commands);
            if (r == 0) {
                free(line);
                return n > 0 ? (long)n : -1;
            }
            if (r < 0) { /* the line was given up */
                n = 0;
                continue;
            }
            l = (ssize_t)strlen(edited);
            if ((size_t)l + 2 > lcap) {
                char *t = realloc(line, (size_t)l + 2);
                if (!t) {
                    free(line);
                    return -1;
                }
                line = t;
                lcap = (size_t)l + 2;
            }
            memcpy(line, edited, (size_t)l + 1);
            goto have_line;
        }
        errno = 0;
        l = getline(&line, &lcap, stdin);
        if (l < 0) {
            if (errno == EINTR && interrupted) {
                interrupted = 0;
                clearerr(stdin);
                term_write(&term, "\n", 1);
                n = 0;
                continue;
            }
            free(line);
            return n > 0 ? (long)n : -1;
        }
    have_line:
        while (l > 0 && (line[l - 1] == '\n' || line[l - 1] == '\r'))
            l--;
        int more = l > 0 && line[l - 1] == '\\';
        if (more)
            line[--l] = '\n';
        else
            line[l] = 0;
        if (n + (size_t)l + 1 > *cap) {
            *cap = 2 * (n + (size_t)l) + 256;
            char *t = realloc(*buf, *cap);
            if (!t) {
                free(line);
                return -1;
            }
            *buf = t;
        }
        memcpy(*buf + n, line, (size_t)l + (more ? 1 : 0));
        n += (size_t)l + (more ? 1 : 0);
        (*buf)[n] = 0;
        if (!more)
            break;
    }
    free(line);
    return (long)n;
}

/*
 * The options, read from words: the command line gives them, and so does the
 * file /save-config writes, which holds the same words. One reader for both,
 * so a setting can never mean one thing typed and another saved.
 */
struct chat_opts { /* where the words land: main keeps them, this names them */
    struct janas_llm_params *mp;
    struct janas_llm_chat_params *cp;
    const char **system_arg;
    int *stats, *markdown, *gpu;
    const char **mcp_config;
    int *mcp, *mcp_auto, *mcp_instructions, *services;
    int *no_mtp;
    const char *mtp_found; /* the MTP file found, not asked for: unsaved */
    int *all_tools;
    int *profile; /* 1: the catalog's fast profile */
    int *tune;    /* 1: measure again (--tune), -1: never (--no-tune) */
    /* --prompt-on, --drafts-on, --reply-on as masks (common/devices.h; 0:
       as the engine chooses) and --npu-int8, put in the engine's variables
       when read, for the next model to open */
    int *devices;
    int *npu_int8;
};

/* every option, for the nearest to one mistyped: the first N_ALONE stand
   alone, the others take a value */
#define N_ALONE 15
static const char *const OPTIONS[] = {
    "--no-spec",     "--stats",      "--no-markdown", "--no-gpu",
    "--no-preload",  "--no-recap",   "--no-mtp",      "--no-mcp",
    "--no-services", "--all-tools",  "--mcp-auto",    "--mcp-instructions",
    "--tune",        "--no-tune",    "--npu-int8",    "--mtp",
    "--draft",       "--think",      "--mode",        "--ctx",
    "--cache",       "--reserve",    "--bits",        "--attention",
    "--system",      "--mcp-config", "--progress",    "--temp",
    "--top-k",       "--top-p",      "--min-p",       "--seed",
    "--max",         "--head",       "--profile",     "--models",
    "--quality",     "--prompt-on",  "--drafts-on",   "--reply-on"};
#define N_OPTIONS (sizeof OPTIONS / sizeof *OPTIONS)

/* v of option a as a whole number or a number within limits; on a
   mistake, the reason into why and -1 */
static int opt_int(const char *a, const char *v, long long lo, long long hi,
                   long long *x, char *why, size_t why_len)
{
    char r[160];
    if (janas_word_int(v, lo, hi, x, r, sizeof r) == 0)
        return 0;
    snprintf(why, why_len, "%s: %s", a, r);
    return -1;
}

static int opt_real(const char *a, const char *v, double lo, double hi,
                    double *x, char *why, size_t why_len)
{
    char r[160];
    if (janas_word_real(v, lo, hi, x, r, sizeof r) == 0)
        return 0;
    snprintf(why, why_len, "%s: %s", a, r);
    return -1;
}

static int opt_choice(const char *a, const char *v, const char *const *names,
                      size_t n, char *why, size_t why_len)
{
    char r[300];
    int k = janas_word_choice(v, names, n, r, sizeof r);
    if (k < 0)
        snprintf(why, why_len, "%s: %s", a, r);
    return k;
}

/* The words of a command line or of the settings file. 0, or -1 with what
   was wrong in why: an option not known (with the nearest), a value that
   is not one. */
static int take_options(int n, char **w, struct chat_opts *o, char *why,
                        size_t why_len)
{
    static const char *const ON_OFF[] = {"off", "on"};
    static const char *const MODES[] = {"auto", "eco", "max"};
    static const char *const ATTN[] = {"auto", "exact", "fast"};
    static const char *const BITS[] = {"2", "4", "6"};
    static const char *const HEAD[] = {"4", "6"};
    static const char *const PROFILES[] = {"quality", "fast"};
    static const char *const QUALITY[] = {"high", "medium", "low"};
    for (int i = 0; i < n; i++) {
        const char *a = w[i], *v = i + 1 < n ? w[i + 1] : NULL;
        long long x;
        double r;
        int k;
        if (strcmp(a, "--no-spec") == 0) {
            o->cp->speculate = 0;
            continue;
        }
        if (strcmp(a, "--stats") == 0) {
            *o->stats = 1;
            continue;
        }
        if (strcmp(a, "--no-markdown") == 0) {
            *o->markdown = 0;
            continue;
        }
        if (strcmp(a, "--no-gpu") == 0) {
            *o->gpu = 0;
            continue;
        }
        if (strcmp(a, "--no-preload") == 0) {
            o->mp->no_preload = 1;
            continue;
        }
        if (strcmp(a, "--no-recap") == 0) {
            o->cp->recap = 0;
            continue;
        }
        if (strcmp(a, "--no-mtp") == 0) {
            *o->no_mtp = 1;
            continue;
        }
        if (strcmp(a, "--no-mcp") == 0) {
            *o->mcp = 0;
            continue;
        }
        if (strcmp(a, "--no-services") == 0) {
            *o->services = 0;
            continue;
        }
        if (strcmp(a, "--all-tools") == 0) {
            *o->all_tools = 1;
            continue;
        }
        if (strcmp(a, "--mcp-auto") == 0) {
            *o->mcp_auto = 1;
            continue;
        }
        if (strcmp(a, "--mcp-instructions") == 0) {
            *o->mcp_instructions = 1;
            continue;
        }
        if (strcmp(a, "--tune") == 0 || strcmp(a, "--no-tune") == 0) {
            *o->tune = a[2] == 't' ? 1 : -1;
            continue;
        }
        if (strcmp(a, "--npu-int8") == 0) {
            *o->npu_int8 = 1;
            setenv("JANAS_NPU_A8", "1", 1); /* read when the model opens */
            continue;
        }
        int valued = 0;
        for (size_t j = N_ALONE; j < N_OPTIONS && !valued; j++)
            valued = strcmp(a, OPTIONS[j]) == 0;
        if (!valued) {
            janas_unknown_word(why, why_len, a, "an option", OPTIONS,
                               N_OPTIONS);
            return -1;
        }
        if (!v) {
            snprintf(why, why_len, "%s wants a value", a);
            return -1;
        }
        i++;
        if ((k = janas_devices_option(a, v, why, why_len)) != 0) {
            if (k < 0)
                return -1;
            o->devices[k - 1] = janas_devices_mask(v, why, why_len);
        } else if (strcmp(a, "--mtp") == 0)
            o->mp->mtp_path = v;
        else if (strcmp(a, "--draft") == 0)
            o->mp->draft_path = v;
        else if (strcmp(a, "--think") == 0) {
            char *end;
            long b = strtol(v, &end, 10);
            if (*v && !*end && b > 0 && b <= INT32_MAX) {
                o->cp->thinking = 1; /* on, at most b tokens of it */
                o->cp->max_reasoning = (int32_t)b;
            } else {
                if ((k = opt_choice(a, v, ON_OFF, 2, why, why_len)) < 0)
                    return -1;
                o->cp->thinking = k;
                o->cp->max_reasoning = 0;
            }
        } else if (strcmp(a, "--mode") == 0) {
            if ((k = opt_choice(a, v, MODES, 3, why, why_len)) < 0)
                return -1;
            o->mp->mode = k == 1   ? JANAS_LLM_MODE_ECO
                          : k == 2 ? JANAS_LLM_MODE_MAX
                                   : JANAS_LLM_MODE_AUTO;
        } else if (strcmp(a, "--ctx") == 0) {
            if (opt_int(a, v, 0, UINT32_MAX, &x, why, why_len) != 0)
                return -1;
            o->mp->n_ctx = (uint32_t)x;
        } else if (strcmp(a, "--cache") == 0) {
            if (opt_real(a, v, 0, 1 << 20, &r, why, why_len) != 0)
                return -1;
            o->mp->cache_bytes = (uint64_t)(r * (1 << 30));
        } else if (strcmp(a, "--reserve") == 0) {
            if (opt_real(a, v, 0, 1 << 20, &r, why, why_len) != 0)
                return -1;
            o->mp->reserve_bytes = (uint64_t)(r * (1 << 30));
        } else if (strcmp(a, "--bits") == 0) {
            if ((k = opt_choice(a, v, BITS, 3, why, why_len)) < 0)
                return -1;
            o->mp->expert_bits = 2 + 2 * k;
        } else if (strcmp(a, "--quality") == 0) {
            if ((k = opt_choice(a, v, QUALITY, 3, why, why_len)) < 0)
                return -1;
            o->mp->min_quality = 2 - k; /* JANAS_LLM_QUALITY_* */
            *o->profile = k == 2;       /* the catalog's choices: low */
        } else if (strcmp(a, "--profile") == 0) {
            /* the older name: quality is high, fast is low */
            if ((k = opt_choice(a, v, PROFILES, 2, why, why_len)) < 0)
                return -1;
            o->mp->min_quality =
                k ? JANAS_LLM_QUALITY_LOW : JANAS_LLM_QUALITY_HIGH;
            *o->profile = k;
        } else if (strcmp(a, "--models") == 0) {
            setenv("JANAS_MODELS", v, 1); /* read when the model is found */
        } else if (strcmp(a, "--head") == 0) {
            if ((k = opt_choice(a, v, HEAD, 2, why, why_len)) < 0)
                return -1;
            o->mp->head_bits = k == 0 ? 4 : 0;
        } else if (strcmp(a, "--attention") == 0) {
            if ((k = opt_choice(a, v, ATTN, 3, why, why_len)) < 0)
                return -1;
            o->mp->attn_scores = k == 1   ? JANAS_LLM_ATTN_EXACT
                                 : k == 2 ? JANAS_LLM_ATTN_FAST
                                          : JANAS_LLM_ATTN_AUTO;
        } else if (strcmp(a, "--system") == 0)
            *o->system_arg = v;
        else if (strcmp(a, "--mcp-config") == 0)
            *o->mcp_config = v;
        else if (strcmp(a, "--progress") == 0) {
            if (opt_real(a, v, 0, 86400, &r, why, why_len) != 0)
                return -1;
            setenv("JANAS_PROGRESS", v, 1);
        } else if (strcmp(a, "--temp") == 0) {
            if (opt_real(a, v, 0, 100, &r, why, why_len) != 0)
                return -1;
            o->cp->temperature = (float)r;
        } else if (strcmp(a, "--top-k") == 0) {
            if (opt_int(a, v, 0, INT32_MAX, &x, why, why_len) != 0)
                return -1;
            o->cp->top_k = (int32_t)x;
        } else if (strcmp(a, "--top-p") == 0) {
            if (opt_real(a, v, 0, 1, &r, why, why_len) != 0)
                return -1;
            o->cp->top_p = (float)r;
        } else if (strcmp(a, "--min-p") == 0) {
            if (opt_real(a, v, 0, 1, &r, why, why_len) != 0)
                return -1;
            o->cp->min_p = (float)r;
        } else if (strcmp(a, "--seed") == 0) {
            if (opt_int(a, v, 0, LLONG_MAX, &x, why, why_len) != 0)
                return -1;
            o->cp->seed = (uint64_t)x;
        } else if (strcmp(a, "--max") == 0) {
            if (opt_int(a, v, 0, INT32_MAX, &x, why, why_len) != 0)
                return -1;
            o->cp->max_reply = (int32_t)x;
        }
    }
    return 0;
}

/*
 * The settings kept between sessions. The file holds the words of the
 * command line, one option to a line, the rest of the line being its value:
 * the same reader takes both, so a setting cannot mean one thing typed and
 * another saved, and the file is one anybody can open and edit.
 *
 * It is read before the command line, which therefore wins, and nothing is
 * written to it unless /save-config is asked for: what a command changes is
 * for this conversation only, which is how a knob turned to try something
 * does not become the way things are.
 */
/* A question answered with one key. Piped in, nobody is there to answer:
   the caller asked for it in writing and that is the answer. */
/* A key for a question: a window resized meanwhile (SIGWINCH, no
   SA_RESTART) is not an answer, Ctrl+C is (no) */
static int answer_byte(unsigned char *k)
{
    for (;;) {
        int r = tty_byte(k);
        if (r < 0 && errno == EINTR && !interrupted)
            continue;
        return r;
    }
}

static int ask_yes(const char *question)
{
    if (!term.tty)
        return 1;
    term_printf(&term, "%s [y/N] ", question);
    term_raw(&term, 1);
    unsigned char k = 0;
    if (answer_byte(&k) < 0)
        k = 0;
    term_raw(&term, 0);
    int yes = k == 'y' || k == 'Y';
    term_printf(&term, "%c\n", yes ? 'y' : 'n');
    return yes;
}

/* The settings file: the chat's own (key NULL), or a model's - key its
   catalog name or its file's - in models/ beside it. */
static int conf_path(char *buf, size_t n, int create, const char *key)
{
    const char *xdg = getenv("XDG_CONFIG_HOME"), *home = getenv("HOME");
    char dir[768];
    if (xdg && *xdg)
        snprintf(dir, sizeof(dir), "%s/janas", xdg);
    else if (home)
        snprintf(dir, sizeof(dir), "%s/.config/janas", home);
    else
        return -1;
    if (create) {
        char up[832];
        snprintf(up, sizeof(up), "%s", dir);
        char *slash = strrchr(up, '/');
        if (slash) {
            *slash = 0;
            mkdir(up, 0700);
        }
        mkdir(dir, 0700);
        if (key) {
            char md[800];
            snprintf(md, sizeof(md), "%s/models", dir);
            mkdir(md, 0700);
        }
    }
    if (key)
        snprintf(buf, n, "%s/models/%s.conf", dir, key);
    else
        snprintf(buf, n, "%s/chat.conf", dir);
    return 0;
}

/* The file as words, kept alive in *store (freed by the caller, or never). */
static char **conf_read(const char *path, int *n, char **store)
{
    *n = 0;
    *store = NULL;
    FILE *f = fopen(path, "r");
    if (!f)
        return NULL;
    static char line[1024];
    char **w = NULL;
    size_t cap = 0, used = 0, hold = 0;
    char *text = NULL;
    while (fgets(line, sizeof(line), f)) {
        char *a = line;
        while (*a == ' ' || *a == '\t')
            a++;
        a[strcspn(a, "\n")] = 0;
        if (!*a || *a == '#')
            continue;
        char *v = a;
        while (*v && *v != ' ' && *v != '\t')
            v++;
        if (*v) {
            *v++ = 0;
            while (*v == ' ' || *v == '\t')
                v++;
        }
        for (int k = 0; k < (*v ? 2 : 1); k++) {
            const char *piece = k ? v : a;
            size_t len = strlen(piece) + 1;
            char *grown = realloc(text, hold + len);
            if (!grown)
                break;
            text = grown;
            memcpy(text + hold, piece, len);
            hold += len;
            if (used == cap) {
                size_t c2 = cap ? cap * 2 : 16;
                char **g2 = realloc(w, c2 * sizeof(*w));
                if (!g2)
                    break;
                w = g2;
                cap = c2;
            }
            w[used++] = (char *)(uintptr_t)(hold - len); /* an offset for now */
        }
    }
    fclose(f);
    /* the offsets become pointers once the text has stopped moving */
    for (size_t k = 0; k < used; k++)
        w[k] = text + (uintptr_t)w[k];
    *n = (int)used;
    *store = text;
    return w;
}

static void conf_write(const char *path, const struct chat_opts *o)
{
    FILE *f = fopen(path, "w");
    if (!f) {
        term_printf(&term, "cannot write %s\n", path);
        return;
    }
    const struct janas_llm_params *m = o->mp;
    const struct janas_llm_chat_params *c = o->cp;
    fprintf(f, "# janas-chat: written by /save-config. The words are those of\n"
               "# the command line, one to a line; the command line wins over\n"
               "# this file, and /load-config reads it again.\n");
    if (m->mtp_path && m->mtp_path != o->mtp_found)
        fprintf(f, "--mtp %s\n", m->mtp_path);
    fprintf(f, "--ctx %u\n", m->n_ctx);
    if (m->cache_bytes)
        fprintf(f, "--cache %.2f\n", (double)m->cache_bytes / (1 << 30));
    if (m->reserve_bytes)
        fprintf(f, "--reserve %.2f\n", (double)m->reserve_bytes / (1 << 30));
    if (m->expert_bits)
        fprintf(f, "--bits %d\n", m->expert_bits);
    if (m->head_bits == 4)
        fprintf(f, "--head 4\n");
    if (m->attn_scores == JANAS_LLM_ATTN_EXACT)
        fprintf(f, "--attention exact\n");
    else if (m->attn_scores == JANAS_LLM_ATTN_FAST)
        fprintf(f, "--attention fast\n");
    fprintf(f, "--mode %s\n",
            m->mode == JANAS_LLM_MODE_ECO   ? "eco"
            : m->mode == JANAS_LLM_MODE_MAX ? "max"
                                            : "auto");
    fprintf(f, "--temp %g\n--top-k %d\n--top-p %g\n--min-p %g\n",
            (double)c->temperature, c->top_k, (double)c->top_p,
            (double)c->min_p);
    if (c->max_reply)
        fprintf(f, "--max %d\n", c->max_reply);
    if (c->thinking && c->max_reasoning > 0)
        fprintf(f, "--think %d\n", c->max_reasoning);
    else
        fprintf(f, "--think %s\n", c->thinking ? "on" : "off");
    if (!c->speculate)
        fprintf(f, "--no-spec\n");
    if (*o->stats)
        fprintf(f, "--stats\n");
    if (!*o->markdown)
        fprintf(f, "--no-markdown\n");
    if (!*o->gpu)
        fprintf(f, "--no-gpu\n");
    for (int k = 0; k < JANAS_DEVICES_OPTIONS; k++)
        if (o->devices[k]) {
            char nm[64];
            janas_devices_name(o->devices[k], nm, sizeof nm);
            fprintf(f, "%s %s\n", janas_devices_options[k], nm);
        }
    if (*o->npu_int8)
        fprintf(f, "--npu-int8\n");
    if (*o->mcp_config)
        fprintf(f, "--mcp-config %s\n", *o->mcp_config);
    if (!*o->mcp)
        fprintf(f, "--no-mcp\n");
    if (!*o->services)
        fprintf(f, "--no-services\n");
    if (*o->all_tools)
        fprintf(f, "--all-tools\n");
    if (*o->mcp_auto)
        fprintf(f, "--mcp-auto\n");
    if (*o->mcp_instructions)
        fprintf(f, "--mcp-instructions\n");
    if (m->min_quality == JANAS_LLM_QUALITY_HIGH)
        fprintf(f, "--quality high\n");
    else if (m->min_quality == JANAS_LLM_QUALITY_MEDIUM)
        fprintf(f, "--quality medium\n");
    else if (*o->profile) /* asked for: with the catalog's choices */
        fprintf(f, "--quality low\n");
    fclose(f);
    term_printf(&term, "written to %s (the system message is not kept)\n",
                path);
}

/* The MCP servers and their tools, for /mcp. */
static void mcp_list(void)
{
    if (!mcpc_count()) {
        term_printf(&term, "no MCP server is running: Janas's services start "
                           "when they are beside janas-chat, the others are "
                           "configured in ~/.config/janas/mcp.json, or the "
                           "file --mcp-config names\n");
        return;
    }
    for (size_t i = 0; i < mcpc_count(); i++) {
        char info[2048], names[4096];
        int32_t len;
        janas_mcp_info(mcpc_server(i), info, sizeof(info), &len);
        janas_mcp_tool_names(mcpc_server(i), names, sizeof(names), &len);
        term_printf(&term, "%s%s%s%s\n%s%s", T_BOLD(&term), mcpc_name(i),
                    T_RESET(&term),
                    mcpc_is_own(i) ? " - a service of Janas: its tools run "
                                     "without asking"
                                   : "",
                    info, T_DIM(&term));
        for (char *t = strtok(names, "\n"); t; t = strtok(NULL, "\n"))
            term_printf(&term, "  %s__%s\n", mcpc_name(i), t);
        term_printf(&term, "%s\n", T_RESET(&term));
    }
}

/* A question answered with one key, lowercase (0 when none came). */
static int ask_key(const char *question)
{
    term_printf(&term, "%s ", question);
    term_raw(&term, 1);
    unsigned char k = 0;
    if (answer_byte(&k) < 0)
        k = 0;
    term_raw(&term, 0);
    if (k >= 'A' && k <= 'Z')
        k = (char)(k - 'A' + 'a');
    term_printf(&term, "%c\n", k >= ' ' && k < 0x7f ? k : ' ');
    return k;
}

/* Text of the library, of any length, into a buffer of the caller's. */
static char *call_part(const janas_llm_chat *c, int32_t i, int args)
{
    int32_t nl = 0, al = 0;
    janas_llm_chat_call(c, i, NULL, 0, &nl, NULL, 0, &al);
    char *name = malloc((size_t)nl + 1), *arg = malloc((size_t)al + 1);
    if (!name || !arg ||
        janas_llm_chat_call(c, i, name, nl + 1, &nl, arg, al + 1, &al) !=
            JANAS_LLM_OK) {
        free(name);
        free(arg);
        return NULL;
    }
    free(args ? name : arg);
    return args ? arg : name;
}

/*
 * The tool calls of the reply that just ended: each shown, confirmed (or
 * not, in auto mode or for a tool always allowed), run, and what the tools
 * answered sent back to the model. 1 when a new reply follows.
 */
/* The calls that failed in this exchange, as "name args": one asked for
   again is not run (it would fail the same way), and at the second time
   the exchange stops. A model at temperature 0 can otherwise ask for it
   until the rounds run out, without ever answering. */
#define FAILED_MAX 16
static struct {
    char *call[FAILED_MAX];
    int n, again;
} failed;

static void failed_clear(void)
{
    for (int i = 0; i < failed.n; i++)
        free(failed.call[i]);
    failed.n = failed.again = 0;
}

static char *call_key(const char *name, const char *args)
{
    size_t a = strlen(name), b = strlen(args);
    char *k = malloc(a + b + 2);
    if (k) {
        memcpy(k, name, a);
        k[a] = ' ';
        memcpy(k + a + 1, args, b + 1);
    }
    return k;
}

static int failed_before(const char *key)
{
    for (int i = 0; key && i < failed.n; i++)
        if (strcmp(failed.call[i], key) == 0)
            return 1;
    return 0;
}

/* 1 when the results went back to the model for its next reply, 0 when
   there is nothing more to do, -1 when the exchange stops here. */
static int run_calls(janas_llm_chat *c, int autorun)
{
    /* a reply held to a call (below) holds only that one */
    janas_llm_chat_tool_choice(c, JANAS_LLM_TOOLS_AUTO, NULL, 0, 1);
    int32_t n = janas_llm_chat_calls(c);
    if (n <= 0 || !mcpc_count())
        return 0;
    char **texts = calloc((size_t)n, sizeof(*texts));
    if (!texts)
        return 0;
    int ok = 1;
    for (int32_t i = 0; i < n && ok; i++) {
        char *name = call_part(c, i, 0), *args = call_part(c, i, 1);
        if (!name || !args) {
            free(name);
            free(args);
            ok = 0;
            break;
        }
        term_gap(&term);
        /* a service of Janas's runs by itself: its call is only noted */
        /* one that changes something (janas-git's commit, push) is asked
           for every time, even with --mcp-auto: never "always"; shown
           whole, since the user says yes to what it holds */
        int writes = mcpc_writes(name);
        int go = !writes && (autorun || mcpc_always(name) || mcpc_own(name));
        /* whole where the user is asked: a yes (or an always) is to all of
           it, not to its first 400 bytes */
        int cut = go ? 400 : INT_MAX;
        char *sname = term_cleaned(name), *sargs = term_cleaned(args);
        term_printf(
            &term, "%s→ %s%s %s%.*s%s%s\n",
            mcpc_own(name) && !writes ? T_DIM(&term) : T_CMD(&term),
            sname ? sname : "?", T_DIM(&term), "", cut, sargs ? sargs : "?",
            sargs && strlen(sargs) > (size_t)cut ? " ..." : "", T_RESET(&term));
        free(sname);
        free(sargs);
        char *key = call_key(name, args);
        if (failed_before(key)) {
            go = 0;
            failed.again++;
            term_printf(&term, "%snot run: the same call failed already%s\n",
                        T_WARN(&term), T_RESET(&term));
            texts[i] = strdup("Not run: this very call, with the same "
                              "arguments, failed already in this exchange "
                              "and would fail again. Do not ask for it "
                              "again: answer the user with what you have, "
                              "or ask them for what is missing.");
        } else if (!go && interrupted) {
            go = 0;
        } else if (!go && !term.tty) {
            term_printf(&term, "not run: on a pipe nobody can say yes%s\n",
                        writes ? " (it changes something: always asked)"
                               : " (--mcp-auto runs them)");
        } else if (!go && writes) {
            int k = ask_key("it changes something: run it? [y]es, [n]o");
            go = k == 'y';
        } else if (!go) {
            int k = ask_key("run it? [y]es, [n]o, [a]lways this tool");
            go = k == 'y' || k == 'a';
            if (k == 'a')
                mcpc_set_always(name);
        }
        if (go) {
            footer(idle_llm, c, "running a tool");
            char *shown = NULL;
            unsigned gen = mcpc_opened();
            int rc = mcpc_call(name, args, &texts[i], &shown);
            if (mcpc_opened() != gen) { /* a service opened: its tools */
                char *more = mcpc_opened_tools();
                if (janas_llm_chat_tools_add(c, more, -1) != JANAS_LLM_OK)
                    term_printf(&term, "%s\n", janas_llm_last_error());
                free(more);
                /* and the next reply is a call: told to call one, a small
                   model (Qwen3.5-0.8B) still made up the weather instead;
                   and to one of the service's own tools: Qwen3.8-Flash-Next
                   (two bits a weight) opened the service again and again,
                   its name being that of its tools' prefix (22 Oct 2026) */
                janas_llm_chat_tool_choice(c, JANAS_LLM_TOOLS_REQUIRED,
                                           mcpc_opened_prefix(), -1, 1);
            }
            if (rc != 0 && key && failed.n < FAILED_MAX) {
                failed.call[failed.n++] = key;
                key = NULL;
            }
            char *t = term_cleaned(texts[i] ? texts[i] : "");
            const char *tt = t ? t : "";
            size_t line = strcspn(tt, "\n");
            term_printf(
                &term, "%s%s%.*s%s%s\n", rc ? T_WARN(&term) : T_DIM(&term),
                rc > 0 ? "tool error: " : "", (int)(line < 200 ? line : 200),
                tt, line < strlen(tt) || line > 200 ? " ..." : "",
                T_RESET(&term));
            free(t);
            if (shown && *shown) {
                /* the user's, as the tool wrote it (a page): the model
                   has only a note of it */
                term_gap(&term);
                shown[term_clean(shown, strlen(shown))] = 0;
                term_printf(&term, "%s\n", shown);
            }
            free(shown);
        } else if (!texts[i]) {
            texts[i] = strdup("The user did not allow this call: it was "
                              "not run.");
        }
        free(key);
        free(name);
        free(args);
        if (!texts[i])
            ok = 0;
    }
    int sent = 0;
    if (ok && !interrupted) {
        if (janas_llm_chat_send_results(c, n, (const char *const *)texts,
                                        NULL) == JANAS_LLM_OK)
            sent = 1;
        else
            term_printf(&term, "%s\n", janas_llm_last_error());
    }
    for (int32_t i = 0; i < n; i++)
        free(texts[i]);
    free(texts);
    interrupted = 0;
    if (sent && failed.again >= 2) {
        term_printf(&term,
                    "%sstopped: the model keeps asking for a call that "
                    "failed%s\n",
                    T_WARN(&term), T_RESET(&term));
        return -1;
    }
    return sent;
}

/* The reply, written as it comes; -1 when it failed or was stopped. */
static int show_reply(const janas_llm *llm, janas_llm_chat *c, int markdown,
                      int stats)
{
    interrupted = 0;
    term_gap(&term);
    footer(llm, c, janas_llm_chat_thinking(c) ? "reasoning" : "generating");
    char piece[256];
    int32_t len = 0, rc; /* written by chat_next when it says OK */
    int grey = 0;
    char tail = '\n'; /* the reply's last byte: a model often ends on a
                          newline of its own, and one is enough */
    unsigned pieces = 0;
    struct janas_md md;
    md_init(&md, &term, markdown);
    term_puts(&term, T_MODEL(&term));
    while ((rc = engine_next(c, piece, sizeof(piece), &len)) == JANAS_LLM_OK) {
        /* the reasoning in grey: the colour changes only where the
           reasoning starts and ends */
        int th = janas_llm_chat_thinking(c);
        if (th != grey) {
            md.base = th ? T_THINK(&term) : "";
            term_puts(&term, th ? T_THINK(&term) : T_RESET(&term));
            grey = th;
        }
        if (resized) { /* the window changed: the footer follows it */
            resized = 0;
            term_size(&term);
            term_region(&term);
            if (conv.back)
                scroll_draw(&term, &conv); /* re-wrapped to the new width */
        }
        if (len > 0)
            tail = piece[len - 1];
        /* what the model writes drives no terminal; its tabs (code) four
           spaces */
        char wide[4 * sizeof(piece)];
        size_t wn = 0;
        for (int32_t i = 0; i < len; i++)
            if (piece[i] == '\t')
                for (int k = 0; k < 4; k++)
                    wide[wn++] = ' ';
            else
                wide[wn++] = piece[i];
        md_write(&md, wide, term_clean(wide, wn));
        fflush(stdout);
        struct janas_llm_chat_stats rs = {.size = sizeof(rs)};
        if (len == 0 && janas_llm_chat_stats(c, &rs) == JANAS_LLM_OK &&
            rs.stage == JANAS_LLM_STAGE_INPUT) {
            /* a long prompt, a block at a time: how far, how fast */
            char state[96];
            double rate =
                rs.input_seconds > 0 ? rs.input_done / rs.input_seconds : 0;
            double left = rs.size >= sizeof(rs) ? rs.input_eta_seconds : -1;
            char eta[32] = "";
            if (left >= 3600)
                snprintf(eta, sizeof(eta), ", ~%dh%02dm left",
                         (int)(left / 3600), (int)(left / 60) % 60);
            else if (left >= 60)
                snprintf(eta, sizeof(eta), ", ~%dm left", (int)(left / 60));
            else if (left >= 0)
                snprintf(eta, sizeof(eta), ", ~%ds left", (int)left);
            snprintf(state, sizeof(state), "reading %u%% %.0f tok/s%s",
                     rs.input_tokens ? 100 * rs.input_done / rs.input_tokens
                                     : 100,
                     rate, eta);
            footer(llm, c, state);
        } else if (++pieces % 16 == 0) { /* the footer follows the reply */
            struct janas_llm_chat_stats st = {.size = sizeof(st)};
            const char *what =
                janas_llm_chat_thinking(c) ? "reasoning" : "generating";
            char state[64];
            snprintf(state, sizeof(state), "%s", what);
            if (janas_llm_chat_stats(c, &st) == JANAS_LLM_OK &&
                st.output_tokens)
                snprintf(state, sizeof(state), "%s %u", what, st.output_tokens);
            footer(llm, c, state);
        }
        if (interrupted)
            break;
    }
    md_end(&md);
    term_puts(&term, T_RESET(&term));
    grey = 0;
    if (tail != '\n')
        term_write(&term, "\n", 1);
    if (rc < 0) {
        term_gap(&term);
        term_printf(&term, "[%s]\n", janas_llm_last_error());
    } else if (interrupted) {
        term_gap(&term);
        term_printf(&term, "[stopped]\n");
    }
    int stopped = interrupted || rc < 0;
    interrupted = 0;
    {
        struct janas_llm_chat_stats st = {.size = sizeof(st)};
        if (janas_llm_chat_stats(c, &st) == JANAS_LLM_OK &&
            st.output_seconds > 0) {
            sess_tokens += st.output_tokens;
            sess_seconds += st.output_seconds;
        }
    }
    /* the NPU made ready meanwhile: said once, so a slow first reply is
       understood */
    static double npu_said;
    double ns = 0;
    if (term.tty && janas_llm_npu_preparing(llm, NULL, NULL, &ns) == 0 &&
        ns > npu_said + 0.5) {
        term_gap(&term);
        term_printf(&term,
                    "%s[the NPU made ready for this model in %.0f s, "
                    "once a session]%s\n",
                    T_DIM(&term), ns - npu_said, T_RESET(&term));
        npu_said = ns;
    }
    if (stats) {
        term_gap(&term);
        report(c);
    }
    return stopped ? -1 : 0;
}

int main(int argc, char **argv)
{
    janas_env_check("janas-chat");
    /* wcwidth() answers for the locale's character set, and in the C locale
       it answers -1 to everything past ASCII: ask for the user's own. Not on
       Windows, where wcwidth() is not used (term.h) and the user's locale
       did harm: msvcrt then translated what was written to the console from
       that locale's code page, so the UTF-8 came out as CP1251 under a
       Ukrainian one, "в–„" for "▄" (issue #44) */
#ifndef _WIN32
    setlocale(LC_CTYPE, "");
#endif
    for (int i = 1; i < argc; i++)
        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
            usage();
            return 0;
        }
    /* the model first, if given: a path or a name of the catalog */
    const char *marg = argc >= 2 && argv[1][0] != '-' ? argv[1] : NULL;
    int oi = marg ? 2 : 1;
    if (janas_llm_abi_version() != JANAS_LLM_ABI_VERSION) {
        fprintf(stderr, "janas-chat: library ABI %d, expected %d\n",
                janas_llm_abi_version(), JANAS_LLM_ABI_VERSION);
        return 1;
    }
    struct janas_llm_params mp;
    struct janas_llm_chat_params cp;
    janas_llm_params_default(&mp);
    janas_llm_chat_params_default(&cp);
    /* the chat asks for no reasoning unless it is asked for. The library's
       own default is the model's, which for Qwen3 is to reason: on a small
       model that is two hundred tokens of rough work before the first word
       of an answer to "what is seventeen times three". --think on, /think
       on, or --think on in the configuration file bring it back. */
    cp.thinking = 0;
    asked = &cp;
    const char *system_arg = NULL;
    int stats = 0;
    int markdown = 1; /* the marks read, not printed; never on a pipe */
    int gpu = 1;      /* the GPU may be given work, if there is one */
    const char *mcp_config = NULL; /* NULL: the default file */
    int mcp = 1, mcp_auto = 0, mcp_instructions = 0, services = 1, no_mtp = 0;
    /* profile: the catalog's faster choices, at --quality low asked */
    int all_tools = 0, profile = 0, tune = 0, npu_int8 = 0;
    int devices[JANAS_DEVICES_OPTIONS] = {0, 0, 0};
    static char mtp_found[4096];
    struct chat_opts o = {
        &mp,       &cp,         &system_arg, &stats,     &markdown,
        &gpu,      &mcp_config, &mcp,        &mcp_auto,  &mcp_instructions,
        &services, &no_mtp,     mtp_found,   &all_tools, &profile,
        &tune,     devices,     &npu_int8};
    /*
     * The settings in layers, each over the one before: the catalog's
     * profile for the model, the chat's own file, the model's file, the
     * command line. Only /save-config writes, and only the model's file
     * (or, asked, the chat's): the catalog is never changed.
     */
    char cpath[1024];
    char *conf_text = NULL, *mconf_text = NULL;
    char **old_texts = NULL; /* those /load-config replaced, freed at the end */
    int n_old_texts = 0;
    int cn = 0, mn = 0;
    char **cw = conf_path(cpath, sizeof(cpath), 0, NULL) == 0
                    ? conf_read(cpath, &cn, &conf_text)
                    : NULL;
    const char *where = chat_words_last(argc - oi, argv + oi, "--models");
    if (!where)
        where = chat_words_last(cn, cw, "--models");
    if (where)
        setenv("JANAS_MODELS", where, 1);
    struct janas_model_pick cm;
    if (janas_model_pick("janas-chat", marg, &cm) != 0)
        return 1;
    if (cm.entry && cm.entry->test_only)
        fprintf(stderr,
                "note: %s is a test model of the engine, not for chatting: "
                "its answers are often wrong (janas-chat with no model lists "
                "the others)\n",
                cm.entry->name);
    char mpath[1024];
    char **mw = conf_path(mpath, sizeof(mpath), 0, cm.key) == 0
                    ? conf_read(mpath, &mn, &mconf_text)
                    : NULL;
    /* the quality asked (--quality, or --profile its older name): typed,
       else the model's file, else the chat's; low when none says */
    const char *qs[3][2] = {
        {chat_words_last(argc - oi, argv + oi, "--quality"),
         chat_words_last(argc - oi, argv + oi, "--profile")},
        {chat_words_last(mn, mw, "--quality"),
         chat_words_last(mn, mw, "--profile")},
        {chat_words_last(cn, cw, "--quality"),
         chat_words_last(cn, cw, "--profile")}};
    const char *qv = NULL;
    int asked = 0;
    for (int i = 0; i < 3 && !qv; i++)
        if ((qv = qs[i][0] ? qs[i][0] : qs[i][1]) != NULL)
            asked = 1;
    /* low by default lets the engine choose by the machine (memory short:
       faster, plenty: the file's quality); asked for, it adds the
       catalog's choices measured faster on this model whatever the
       machine (the head at four bits, say) */
    int low = asked && (!strcmp(qv, "low") || !strcmp(qv, "fast"));
    char why[400];
    if (low) {
        const char *fw = cm.entry ? cm.entry->fast : NULL;
        if (!fw) {
            fprintf(stderr, "quality low: nothing measured faster on this "
                            "model beyond the engine's own choices\n");
        } else {
            /* the catalog's words, split on spaces */
            static char fbuf[256];
            char *pw[16];
            int pn = 0;
            snprintf(fbuf, sizeof(fbuf), "%s", fw);
            for (char *t = strtok(fbuf, " "); t && pn < 16;
                 t = strtok(NULL, " "))
                pw[pn++] = t;
            if (take_options(pn, pw, &o, why, sizeof why) != 0) {
                fprintf(stderr, "janas-chat: catalog: %s\n", why);
                return 2;
            }
            fprintf(stderr,
                    "quality low: %s (measured faster on this "
                    "model; --quality high keeps the file's)\n",
                    fw);
        }
    }
    /* the levers the user set, in any file or typed: the measure leaves
       them alone */
    static const char *const SPEC_OPTS[] = {"--mtp", "--draft", "--no-mtp",
                                            "--no-spec", NULL};
    static const char *const HEAD_OPTS[] = {"--head", NULL};
    int spec_user = chat_words_any(cn, cw, SPEC_OPTS) ||
                    chat_words_any(mn, mw, SPEC_OPTS) ||
                    chat_words_any(argc - oi, argv + oi, SPEC_OPTS);
    int head_user = chat_words_any(cn, cw, HEAD_OPTS) ||
                    chat_words_any(mn, mw, HEAD_OPTS) ||
                    chat_words_any(argc - oi, argv + oi, HEAD_OPTS);
    const char *bad_in = NULL;
    if (cw && take_options(cn, cw, &o, why, sizeof why) != 0)
        bad_in = cpath;
    else if (mw && take_options(mn, mw, &o, why, sizeof why) != 0)
        bad_in = mpath;
    free(cw);
    free(mw);
    if (bad_in) {
        fprintf(stderr, "janas-chat: %s: %s\n", bad_in, why);
        return 2;
    }
    if (take_options(argc - oi, argv + oi, &o, why, sizeof why) != 0) {
        fprintf(stderr, "janas-chat: %s\n", why);
        fprintf(stderr, "janas-chat --help lists the options\n");
        free(conf_text);
        return 2;
    }

    tty_setup();
    term_init(&term);
    if (!term.tty)
        markdown = 0; /* a pipe gets the marks: a script may want them */
    scroll_init(&conv);
    scroll_attach(&term, &conv); /* from here the conversation is kept */
    int tty = term.tty;
    /* the model's MTP block or assistant, when not named: the one beside
       it that fits (--no-mtp: none) */
    /* the measure of this model on this machine: a layer of its own over
       the catalog's, for the levers the user left alone */
    struct chat_tune_dims dims = {
        .spec = !spec_user,
        .head = profile && cm.entry && cm.entry->fast &&
                strstr(cm.entry->fast, "--head 4") && !head_user};
    char tpath[2300];
    if (tune >= 0 && (dims.spec || dims.head) &&
        chat_tune_path(tpath, sizeof(tpath), cm.key, profile) == 0) {
        int have = access(tpath, F_OK) == 0;
        int go = tune == 1;
        if (!go && !have && isatty(STDIN_FILENO) && isatty(STDERR_FILENO)) {
            go = janas_ask_yes("Measure how this model runs fastest on this "
                               "machine (a reply or two each way, a minute "
                               "or so on a large model)?");
            if (!go) { /* asked once: the answer is kept */
                FILE *f = fopen(tpath, "w");
                if (f) {
                    fprintf(f, "# janas-chat: not measured (declined); "
                               "janas-chat --tune measures it\n");
                    fclose(f);
                }
            }
        }
        if (go) {
            struct janas_llm_params probe = mp;
            static char probe_mtp[4096], probe_draft[4096];
            if (!probe.mtp_path && !probe.draft_path && !no_mtp &&
                !(probe.draft_path = janas_model_pick_draft(
                      &cm, probe_draft, sizeof(probe_draft))))
                probe.mtp_path =
                    janas_model_pick_mtp(&cm, probe_mtp, sizeof(probe_mtp));
            chat_tune_run(cm.path, &probe, &cp, gpu, dims, tpath);
        }
        int tn = 0;
        char *ttext = NULL; /* kept: the words point into it */
        char **tw = conf_read(tpath, &tn, &ttext);
        char *keep[16];
        int kn = 0;
        for (int i = 0; tw && i < tn && kn < 15; i++) {
            int head = strcmp(tw[i], "--head") == 0;
            if (head ? !dims.head : !dims.spec) {
                i += head; /* skip the value too */
                continue;
            }
            keep[kn++] = tw[i];
            if (head && i + 1 < tn)
                keep[kn++] = tw[++i];
        }
        if (kn) {
            if (take_options(kn, keep, &o, why, sizeof why) != 0)
                fprintf(stderr, "janas-chat: %s: %s\n", tpath, why);
            else {
                fprintf(stderr, "measured on this machine:");
                for (int i = 0; i < kn; i++)
                    fprintf(stderr, " %s", keep[i]);
                fprintf(stderr, " (--no-tune: without)\n");
            }
        }
        free(tw);
    }
    static char draft_found[4096];
    /* no drafting, no prediction file or draft to load */
    if (!cp.speculate)
        no_mtp = 1;
    if (!mp.mtp_path && !mp.draft_path && !no_mtp &&
        (mp.draft_path =
             janas_model_pick_draft(&cm, draft_found, sizeof(draft_found))))
        fprintf(stderr,
                "drafts from %s, as the catalog says (--no-mtp: without)\n",
                cm.entry->draft);
    if (!mp.mtp_path && !mp.draft_path && !no_mtp &&
        janas_model_pick_mtp(&cm, mtp_found, sizeof(mtp_found))) {
        mp.mtp_path = mtp_found;
        const char *b = strrchr(mtp_found, '/');
        fprintf(stderr,
                "multi-token prediction from %s, found beside the model "
                "(--no-mtp: without)\n",
                b ? b + 1 : mtp_found);
    }
    fprintf(stderr, "loading %s ...\n", cm.path);
    janas_llm *llm = NULL;
    janas_llm_chat *c = NULL; /* only written when the open succeeded, which
                                 a compiler looking across the whole program
                                 cannot always see */
    if (janas_llm_open(cm.path, &mp, &llm) != JANAS_LLM_OK ||
        janas_llm_chat_create(llm, &cp, &c) != JANAS_LLM_OK) {
        fprintf(stderr, "janas-chat: %s\n", janas_llm_last_error());
        return 1;
    }
    char *system = system_arg ? (*system_arg ? strdup(system_arg) : NULL)
                              : default_system(llm);
    if (system && janas_llm_chat_system(c, system, -1) != JANAS_LLM_OK) {
        fprintf(stderr, "janas-chat: %s\n", janas_llm_last_error());
        return 1;
    }
    if (!gpu && janas_llm_set_gpu(llm, 0) != JANAS_LLM_OK)
        fprintf(stderr, "janas-chat: %s\n", janas_llm_last_error());
    /* the tools of the MCP servers configured and of Janas's own services
       beside the program, given to the model */
    aside_chat = c;
    user_lang();
    layout_set_translator(translate);
    layout_set_model(cm.key);
    if (mcp) {
        mcpc_defer(!all_tools);
        mcpc_start(mcp_config);
        if (services)
            mcpc_start_services();
    }
    if (mcpc_count() && mcpc_tools() &&
        janas_llm_chat_tools(c, mcpc_tools(), -1) != JANAS_LLM_OK) {
        fprintf(stderr, "janas-chat: MCP tools: %s\n", janas_llm_last_error());
        mcpc_stop();
    }
    /* what Janas's services say of their tools, and, asked for, what the
       other servers say of theirs */
    if (mcpc_count() && (mcp_extra = mcpc_instructions(mcp_instructions)) &&
        set_system(c, system) != JANAS_LLM_OK)
        fprintf(stderr, "janas-chat: %s\n", janas_llm_last_error());
    /* the system message once read is kept, in memory and on the disk
       (~/.cache/janas), so that a /reset or the next chat on this model
       does not read it again */
    if (janas_llm_chat_keep(c, 2, 0) != JANAS_LLM_OK ||
        janas_llm_chat_keep_disk(c, (uint64_t)2 << 30) != JANAS_LLM_OK)
        fprintf(stderr, "janas-chat: %s\n", janas_llm_last_error());
    char desc[512];
    int32_t dl;
    janas_llm_describe(llm, desc, sizeof(desc), &dl);
    char name[96] = "";
    janas_llm_name(llm, name, sizeof(name), &dl);
    if (term.tty) {
        term_banner(&term, "Janas-Chat", name[0] ? name : cm.path,
                    "/help for the commands, /quit to leave");
        term_printf(&term, "%s%s%s\n\n", T_DIM(&term), desc, T_RESET(&term));
        char hp[512];
        const char *xdg = getenv("XDG_CACHE_HOME"), *home = getenv("HOME");
        if (xdg && *xdg)
            snprintf(hp, sizeof(hp), "%s/janas/chat_history", xdg);
        else if (home)
            snprintf(hp, sizeof(hp), "%s/.cache/janas/chat_history", home);
        else
            hp[0] = 0;
        hist_load(&history, hp);
        footer(llm, c, NULL);
        idle_llm = llm;
        idle_chat = c;
        line_idle = footer_idle;
        watch_start(llm);
    } else
        fprintf(stderr, "%s\n/help for the commands\n", desc);
    prep_start(c);

#ifdef _WIN32
    signal(SIGINT, on_sigint);
    tty_on_resize = on_resize;
#else
    struct sigaction sa = {.sa_handler = on_sigint};
    sigemptyset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL); /* no SA_RESTART: getline returns */
    struct sigaction sw = {.sa_handler = on_sigwinch};
    sigemptyset(&sw.sa_mask); /* no SA_RESTART: the editor redraws itself */
    sigaction(SIGWINCH, &sw, NULL);
    (void)on_resize;
#endif

    char *msg = NULL;
    size_t cap = 0;
    long n;
    while ((n = read_message(&msg, &cap, tty)) >= 0) {
        if (n == 0)
            continue;
        /* the chat is the reading thread's until it ends: but for the
           pages, which do not touch it, everything waits for it */
        if (strcmp(msg, "/help") != 0 && strcmp(msg, "/about") != 0)
            prep_wait(llm,
                      strcmp(msg, "/quit") == 0 || strcmp(msg, "/exit") == 0);
        if (msg[0] == '/') {
            term_gap(&term); /* what a command answers is one block */
            char *arg = strchr(msg, ' ');
            if (arg)
                *arg++ = 0;
            else
                arg = msg + strlen(msg);
            if (strcmp(msg, "/quit") == 0 || strcmp(msg, "/exit") == 0)
                break;
            if (strcmp(msg, "/help") == 0 || strcmp(msg, "/about") == 0) {
                char *page =
                    catch_page(strcmp(msg, "/help") == 0 ? help : about, llm);
                if (!page)
                    term_printf(&term, "not enough memory for it\n");
                else if (term.tty) {
                    term_page_begin(&term);
                    term_pager(&term, page);
                    term_page_end(&term);
                } else
                    fputs(page, stdout);
                free(page);
            } else if (strcmp(msg, "/save-config") == 0) {
                const char *key = strcmp(arg, "global") == 0 ? NULL : cm.key;
                if (conf_path(cpath, sizeof(cpath), 1, key) != 0)
                    term_printf(&term, "nowhere to write it\n");
                /* only when there is something to lose: asking about an
                   empty place is noise, and noise is what makes people
                   stop reading the questions that matter */
                else if (access(cpath, F_OK) != 0 ||
                         ask_yes("overwrite the saved settings?"))
                    conf_write(cpath, &o);
            } else if (strcmp(msg, "/del-config") == 0) {
                const char *key = strcmp(arg, "global") == 0 ? NULL : cm.key;
                if (conf_path(cpath, sizeof(cpath), 0, key) != 0)
                    term_printf(&term, "nowhere to look for it\n");
                else if (access(cpath, F_OK) != 0)
                    term_printf(&term, "there is no %s\n", cpath);
                else if (ask_yes("delete the saved settings?")) {
                    if (remove(cpath) == 0)
                        term_printf(&term,
                                    "%s deleted: the next start goes without "
                                    "it\n",
                                    cpath);
                    else
                        term_printf(&term, "cannot delete %s\n", cpath);
                }
            } else if (strcmp(msg, "/load-config") == 0) {
                if (!ask_yes("read the saved settings, losing these?"))
                    continue;
                struct janas_llm_params was = mp;
                int cn = 0;
                char *text = NULL;
                const char *key = strcmp(arg, "global") == 0 ? NULL : cm.key;
                char **cw = conf_path(cpath, sizeof(cpath), 0, key) == 0
                                ? conf_read(cpath, &cn, &text)
                                : NULL;
                if (!cw) {
                    term_printf(&term, "no %s to read\n", cpath);
                    free(text);
                } else {
                    char bad[400];
                    if (take_options(cn, cw, &o, bad, sizeof bad) != 0)
                        term_printf(&term, "%s: %s\n", cpath, bad);
                    free(cw);
                    /* the older words stay: options not in the new file
                       (an MTP path, the MCP configuration) still point
                       into them, and /save-config reads those */
                    char **ot = realloc(old_texts, (size_t)(n_old_texts + 1) *
                                                       sizeof(*ot));
                    if (ot) {
                        old_texts = ot;
                        old_texts[n_old_texts++] = conf_text;
                    }
                    conf_text = text;
                    if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)
                        term_printf(&term, "%s\n", janas_llm_last_error());
                    janas_llm_set_mode(llm, mp.mode);
                    if (janas_llm_set_gpu(llm, gpu) != JANAS_LLM_OK)
                        term_printf(&term, "%s\n", janas_llm_last_error());
                    term_printf(&term, "read %s\n", cpath);
                    if (was.n_ctx != mp.n_ctx ||
                        was.cache_bytes != mp.cache_bytes ||
                        was.expert_bits != mp.expert_bits ||
                        was.head_bits != mp.head_bits ||
                        was.attn_scores != mp.attn_scores ||
                        was.mtp_path != mp.mtp_path)
                        term_printf(&term,
                                    "context, cache, bits, attention and the "
                                    "MTP block are read at the next start\n");
                }
            } else if (strcmp(msg, "/gpu") == 0) {
                int want = on_off(arg, gpu);
                if (janas_llm_set_gpu(llm, want) != JANAS_LLM_OK)
                    term_printf(&term, "%s\n", janas_llm_last_error());
                else {
                    gpu = want;
                    term_printf(&term, "GPU %s\n", gpu ? "on" : "off");
                }
            } else if (strcmp(msg, "/mcp") == 0) {
                if (strncmp(arg, "auto", 4) == 0) {
                    const char *v = arg[4] == ' ' ? arg + 5 : "";
                    mcp_auto = on_off(v, mcp_auto);
                    term_printf(&term, "MCP tools %s\n",
                                mcp_auto ? "run without asking"
                                         : "run after asking");
                } else {
                    mcp_list();
                }
            } else if (strcmp(msg, "/context") == 0) {
                context_page(c);
            } else if (strcmp(msg, "/markdown") == 0) {
                markdown = on_off(arg, markdown);
                term_printf(&term, "markdown %s\n",
                            markdown ? "read" : "printed as it comes");
            } else if (strcmp(msg, "/stats") == 0) {
                stats = on_off(arg, stats);
                term_printf(&term, "stats %s\n", stats ? "on" : "off");
            } else if (strcmp(msg, "/reset") == 0) {
                restart(c, system);
                if (term.tty) { /* a clean window, as at the start */
                    term_banner(&term, "Janas-Chat", name[0] ? name : cm.path,
                                "/help for the commands, /quit to leave");
                    footer(llm, c, NULL);
                } else
                    term_printf(&term, "new conversation\n");
                prep_start(c);
            } else if (strcmp(msg, "/system") == 0) {
                if (!*arg) {
                    term_printf(&term, "%s\n",
                                system ? system : "(no system message)");
                    continue;
                }
                free(system);
                system = strcmp(arg, "off") == 0 ? NULL : strdup(arg);
                restart(c, system);
                term_printf(&term, "new conversation %s\n",
                            system ? "with this system message"
                                   : "with no system message");
                prep_start(c);
            } else if (strcmp(msg, "/experts") == 0) {
                int32_t used = 0, most = 0;
                if (*arg &&
                    janas_llm_set_experts(llm, atoi(arg)) != JANAS_LLM_OK) {
                    term_printf(&term, "%s\n", janas_llm_last_error());
                    continue;
                }
                janas_llm_experts(llm, &used, &most);
                term_printf(&term, "%d experts per token of %d\n", used, most);
            } else if (strcmp(msg, "/image") == 0) {
                /* a picture for the next message, read now */
                FILE *f = *arg ? fopen(arg, "rb") : NULL;
                long fn = -1;
                char *data = NULL;
                if (f && fseek(f, 0, SEEK_END) == 0 && (fn = ftell(f)) > 0 &&
                    fn <= (32L << 20) && fseek(f, 0, SEEK_SET) == 0 &&
                    (data = malloc((size_t)fn)) &&
                    fread(data, 1, (size_t)fn, f) != (size_t)fn) {
                    free(data);
                    data = NULL;
                }
                if (f)
                    fclose(f);
                if (!*arg)
                    term_printf(&term, "/image <file>: a PNG or a JPEG\n");
                else if (!data)
                    term_printf(&term, "cannot read %s\n", arg);
                else if (janas_llm_chat_image(c, data, fn) != JANAS_LLM_OK)
                    term_printf(&term, "%s\n", janas_llm_last_error());
                else
                    term_printf(&term, "the picture goes with your next "
                                       "message\n");
                free(data);
            } else if (strcmp(msg, "/temp") == 0 && *arg) {
                cp.temperature = (float)atof(arg);
                if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)
                    term_printf(&term, "%s\n", janas_llm_last_error());
            } else if (strcmp(msg, "/think") == 0) {
                char *end;
                long b = strtol(arg, &end, 10);
                if (*arg && !*end && b > 0 && b <= INT32_MAX) {
                    cp.thinking = 1; /* on, at most b tokens of it */
                    cp.max_reasoning = (int32_t)b;
                } else {
                    cp.thinking = strcmp(arg, "off") == 0  ? 0
                                  : strcmp(arg, "on") == 0 ? 1
                                                           : !cp.thinking;
                    if (strcmp(arg, "on") == 0)
                        cp.max_reasoning = 0;
                }
                if (janas_llm_chat_set_params(c, &cp) != JANAS_LLM_OK)
                    term_printf(&term, "%s\n", janas_llm_last_error());
                else if (cp.thinking && cp.max_reasoning > 0)
                    term_printf(&term,
                                "reasoning on, at most %d tokens of it (for "
                                "models that reason)\n",
                                cp.max_reasoning);
                else
                    term_printf(&term,
                                "reasoning %s (for models that reason)\n",
                                cp.thinking ? "on" : "off");
            } else if (strcmp(msg, "/mode") == 0) {
                int32_t md = strcmp(arg, "eco") == 0    ? JANAS_LLM_MODE_ECO
                             : strcmp(arg, "max") == 0  ? JANAS_LLM_MODE_MAX
                             : strcmp(arg, "auto") == 0 ? JANAS_LLM_MODE_AUTO
                                                        : -1;
                if (*arg && md < 0)
                    term_printf(&term, "modes: auto, eco, max\n");
                else if (*arg && janas_llm_set_mode(llm, md) != JANAS_LLM_OK)
                    term_printf(&term, "%s\n", janas_llm_last_error());
                char tb[512];
                int32_t tl;
                if (janas_llm_tuning(llm, tb, sizeof(tb), &tl) == JANAS_LLM_OK)
                    term_printf(&term, "%s\n", tb);
            } else if (strcmp(msg, "/spec") == 0) {
                cp.speculate = on_off(arg, cp.speculate);
                if (janas_llm_chat_set_params(c, &cp) == JANAS_LLM_OK)
                    term_printf(&term, "speculative decoding %s\n",
                                cp.speculate ? "on" : "off");
                else
                    term_printf(&term, "%s\n", janas_llm_last_error());
            } else {
                term_printf(&term, "unknown command %s: /help lists them\n",
                            msg);
            }
            continue;
        }
        if (janas_llm_chat_send(c, msg, (int32_t)n) != JANAS_LLM_OK) {
            term_gap(&term);
            term_printf(&term, "%s\n", janas_llm_last_error());
            continue;
        }
        /* the reply, and while it calls tools and they answer, the next */
        failed_clear();
        for (int round = 0; round < 16; round++)
            if (show_reply(llm, c, markdown, stats) != 0 ||
                run_calls(c, mcp_auto) <= 0)
                break;
        footer(llm, c, NULL);
    }
    prep_wait(llm, 1);
    watch_stop();
    failed_clear();
    free(msg);
    free(system);
    free(mcp_extra);
    mcpc_stop();
    janas_llm_chat_destroy(c);
    janas_llm_close(llm);
    scroll_free(&conv);
    free(conf_text);
    for (int i = 0; i < n_old_texts; i++)
        free(old_texts[i]);
    free(old_texts);
    if (term.tty) {
        hist_save(&history);
        hist_free(&history);
        term_raw(&term, 0);
        term_region_off(&term);
    }
    return 0;
}