From 37d038f7165d5ac46294902f22515602a9d6eece Mon Sep 17 00:00:00 2001 From: lotherk Date: Thu, 19 Nov 2026 15:20:20 +0000 Subject: [PATCH] telegram: /status and an inline menu with /model The command menu now carries /status and /menu; /model was already there. The inline keyboard under /menu, /status, /help and /start has Status, Model, New and Help. Status shows the TUI's facts (session, model, endpoint, agent, tokens, steps, limits, compaction, meter); Model opens the provider picker, which now has a Status button back. The bot menu button is forced to the command list, so the commands show even if an older bot had another button. The live config lists the local models (main/checked, plain, bench35, bench4b, bench9b) so /model has more than one button; the last two need their containers running. tests/test_gateway.sh: /status, the /menu callback, the buttons and the command list. tests/fake_telegram.py: the status callback and setChatMenuButton. make check: all tests passed. check-asan: 41/41 clean. --- share/locale/de.msg | 12 +++ share/locale/en.msg | 12 +++ share/locale/pt_BR.msg | 12 +++ src/backend/telegram.c | 234 +++++++++++++++++++++++++++++++++++++++-- tests/fake_telegram.py | 5 + tests/test_gateway.sh | 16 +++ 6 files changed, 280 insertions(+), 11 deletions(-) diff --git a/share/locale/de.msg b/share/locale/de.msg index dee0969..e76b9ba 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -122,6 +122,8 @@ tg.cmd.help = Hilfe und Befehle tg.cmd.goal = Ziel setzen oder zeigen, dann zusammenfassen tg.cmd.run = fuehrt /%s aus tg.cmd.tasks = Plandateien zeigen +tg.cmd.status = Sitzung, Modell, Tokens, Limits +tg.cmd.menu = Menue: Status, Modell, Neu, Hilfe tg.help = Ich bin splinter. Schreib eine Nachricht fuer einen Turn. /goal setzt das Ziel und startet einen Recap-Turn, /tasks zeigt den Plan. Aenderungen fragen hier mit Ja/Nein oder ok / no . ack.yes = Ja ack.no = Nein @@ -156,6 +158,12 @@ tg.cmd.new = neue Konversation tg.cmd.model = ein Modell waehlen tg.cmd.connect = einen Provider hinzufuegen +tg.menu.text = MenĂ¼ +tg.menu.status = Status +tg.menu.model = Modell +tg.menu.new = Neu +tg.menu.help = Hilfe +tg.menu.toast = ok model.choose = Waehle ein Modell. Die Liste kommt aus der Konfiguration; die Wahl gilt fuer diese Sitzung. model.none = Kein Modell konfiguriert. Mit /connect kommt eins dazu. model.unknown = Kein solches Modell: %s @@ -231,6 +239,10 @@ status.last = zuletzt status.last_value = %ld ms, %zu Schritte, %zu Toolaufrufe status.tokens = Tokens status.tokens_value = ein %zu, aus %zu +status.steps = Schritte +status.steps_value = %zu Schritte, %zu Toolaufrufe +status.cost = Kosten +status.cost_value = %.4f USD status.compact = Kompaktierung status.compact_value = %zu angewandt, %zu fehlgeschlagen, %zu Runden (Agent %zu) status.limits = Limits diff --git a/share/locale/en.msg b/share/locale/en.msg index 676d616..22d6f96 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -121,6 +121,8 @@ tg.cmd.help = Help and commands tg.cmd.goal = Set or show the goal, then recap it tg.cmd.run = run /%s tg.cmd.tasks = Show the plan +tg.cmd.status = session, model, tokens, limits +tg.cmd.menu = menu: status, model, new, help tg.help = I am splinter. Send a message for a turn. /goal sets the goal and starts a recap turn, /tasks shows the plan. Changes ask here with Yes/No or ok / no . ack.yes = Yes ack.no = No @@ -155,6 +157,12 @@ tg.cmd.new = new conversation tg.cmd.model = choose a model tg.cmd.connect = add a provider +tg.menu.text = Menu +tg.menu.status = Status +tg.menu.model = Model +tg.menu.new = New +tg.menu.help = Help +tg.menu.toast = ok model.choose = Choose a model. The list is the configuration; the choice lasts for this session. model.none = No model is configured. Add one with /connect. model.unknown = No such model: %s @@ -230,6 +238,10 @@ status.last = last status.last_value = %ld ms, %zu steps, %zu tool calls status.tokens = tokens status.tokens_value = in %zu, out %zu +status.steps = steps +status.steps_value = %zu steps, %zu tool calls +status.cost = cost +status.cost_value = %.4f USD status.compact = compact status.compact_value = %zu applied, %zu failed, %zu turns (agent %zu) status.limits = limits diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 739e7a1..0700f84 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -125,6 +125,8 @@ tg.cmd.help = Ajuda e comandos tg.cmd.goal = Definir ou mostrar a meta, depois resumir tg.cmd.run = executa /%s tg.cmd.tasks = Mostrar o plano +tg.cmd.status = sessao, modelo, tokens, limites +tg.cmd.menu = menu: status, modelo, novo, ajuda tg.help = Eu sou o splinter. Envie uma mensagem para um turno. /goal define a meta e inicia um turno de recap, /tasks mostra o plano. Mudancas perguntam aqui com Sim/Nao ou ok / no . ack.yes = Sim ack.no = Nao @@ -159,6 +161,12 @@ tg.cmd.new = nova conversa tg.cmd.model = escolher um modelo tg.cmd.connect = adicionar um provedor +tg.menu.text = Menu +tg.menu.status = Status +tg.menu.model = Modelo +tg.menu.new = Novo +tg.menu.help = Ajuda +tg.menu.toast = ok model.choose = Escolha um modelo. A lista e a configuracao; a escolha vale para esta sessao. model.none = Nenhum modelo configurado. Adicione um com /connect. model.unknown = Modelo inexistente: %s @@ -234,6 +242,10 @@ status.last = ultimo status.last_value = %ld ms, %zu passos, %zu chamadas de ferramenta status.tokens = tokens status.tokens_value = entrada %zu, saida %zu +status.steps = passos +status.steps_value = %zu passos, %zu chamadas de ferramenta +status.cost = custo +status.cost_value = %.4f USD status.compact = compactacao status.compact_value = %zu aplicadas, %zu falharam, %zu turnos (agente %zu) status.limits = limites diff --git a/src/backend/telegram.c b/src/backend/telegram.c index f9d361e..5566039 100644 --- a/src/backend/telegram.c +++ b/src/backend/telegram.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "ack.h" @@ -845,6 +846,10 @@ tg_known_desc(const char *name) return sp_t("tg.cmd.goal"); if (strcmp(name, "tasks") == 0) return sp_t("tg.cmd.tasks"); + if (strcmp(name, "status") == 0) + return sp_t("tg.cmd.status"); + if (strcmp(name, "menu") == 0) + return sp_t("tg.cmd.menu"); return NULL; } @@ -888,6 +893,8 @@ tg_collect(struct sp_host *host, const char *sess_dir, struct tg_cmd *cmds, tg_add_cmd(cmds, n, "start", tg_known_desc("start")); tg_add_cmd(cmds, n, "help", tg_known_desc("help")); + tg_add_cmd(cmds, n, "status", tg_known_desc("status")); + tg_add_cmd(cmds, n, "menu", tg_known_desc("menu")); tg_add_cmd(cmds, n, "interview", sp_t("tg.cmd.interview")); tg_add_cmd(cmds, n, "new", sp_t("tg.cmd.new")); tg_add_cmd(cmds, n, "model", sp_t("tg.cmd.model")); @@ -943,6 +950,8 @@ tg_set_commands(const char *token, struct sp_host *host, const char *sess_dir) } sp_buf_puts(&body, "]}"); tg_api_ok(token, "setMyCommands", body.p); + tg_api_ok(token, "setChatMenuButton", + "{\"menu_button\":{\"type\":\"commands\"}}"); sp_buf_free(&body); } @@ -954,7 +963,7 @@ tg_help_text(struct sp_host *host, const char *sess_dir, struct sp_buf *out) char dir[700]; sp_buf_puts(out, sp_t("tg.help")); - sp_buf_puts(out, "\n\n/start /help /interview /new /model /connect "); + sp_buf_puts(out, "\n\n/start /help /status /menu /interview /new /model /connect "); if (host != NULL) (void)sp_host_command_help(host, out); if (sp_cmdscript_dir(sess_dir, dir, sizeof(dir)) == 0 && @@ -1132,7 +1141,10 @@ tg_model_menu(const char *token, long chat_id, struct sp_config *conf, } sp_buf_puts(&mark, ",[{\"text\":\""); (void)sp_json_escape(&mark, sp_t("model.probe")); - sp_buf_puts(&mark, "\",\"callback_data\":\"mprobe\"}]]}"); + sp_buf_puts(&mark, "\",\"callback_data\":\"mprobe\"}]"); + sp_buf_puts(&mark, ",[{\"text\":\""); + (void)sp_json_escape(&mark, sp_t("tg.menu.status")); + sp_buf_puts(&mark, "\",\"callback_data\":\"status\"}]]}"); (void)send_message_markup(token, chat_id, msg.p, mark.p, err); sp_buf_free(&msg); sp_buf_free(&mark); @@ -1226,6 +1238,99 @@ tg_model_named(const char *token, long chat_id, struct sp_agent *ag, sp_buf_free(&perr); } +/* + * /status: the same facts the TUI shows, in a message a chat can read. The + * gateway has the agent (session, totals) and the config (model, endpoint); + * the host adds the meter and compaction counters. + */ +static void +tg_status_text(struct sp_agent *ag, struct sp_config *conf, struct sp_host *host, + struct sp_buf *out) +{ + char tbuf[64], model[256] = "", endpoint[512] = ""; + time_t now = time(NULL); + struct tm tm; + struct sp_provider p; + size_t idx = 0, comp = 0, failed = 0; + unsigned turns = 0; + struct sp_meter m; + + (void)localtime_r(&now, &tm); + strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm); + if (conf != NULL && tg_model_id[0] != '\0' && + sp_provider_find(conf, tg_model_id, &idx) && + sp_provider_at(conf, idx, &p) == 0) { + snprintf(model, sizeof(model), "%s", p.model); + snprintf(endpoint, sizeof(endpoint), "%s", p.endpoint); + } else if (conf != NULL) { + snprintf(model, sizeof(model), "%s", conf->model); + snprintf(endpoint, sizeof(endpoint), "%s", conf->endpoint); + } + sp_buf_printf(out, sp_t("status.title"), tbuf); + sp_buf_puts(out, "\n"); + sp_buf_printf(out, " %-10s %s\n", sp_t("status.session"), + ag != NULL && ag->sess != NULL ? ag->sess->id : "-"); + sp_buf_printf(out, " %-10s %s\n", sp_t("status.dir"), + ag != NULL && ag->sess != NULL ? ag->sess->dir : "-"); + sp_buf_printf(out, " %-10s %s\n", sp_t("status.model"), + model[0] != '\0' ? model : "-"); + sp_buf_printf(out, " %-10s %s\n", sp_t("status.endpoint"), + endpoint[0] != '\0' ? endpoint : "-"); + sp_buf_printf(out, " %-10s %s\n", sp_t("status.agent"), + ag != NULL && ag->policy != NULL ? ag->policy->name : "default"); + if (ag != NULL) { + sp_buf_printf(out, " %-10s ", sp_t("status.tokens")); + sp_buf_printf(out, sp_t("status.tokens_value"), + ag->stats.tokens_in, ag->stats.tokens_out); + sp_buf_printf(out, ", cache %zu\n", ag->stats.tokens_cached); + sp_buf_printf(out, " %-10s ", sp_t("status.steps")); + sp_buf_printf(out, sp_t("status.steps_value"), + ag->stats.steps, ag->stats.tool_calls); + sp_buf_puts(out, "\n"); + if (ag->stats.cost > 0) { + sp_buf_printf(out, " %-10s ", sp_t("status.cost")); + sp_buf_printf(out, sp_t("status.cost_value"), + ag->stats.cost); + sp_buf_puts(out, "\n"); + } + if (ag->cfg != NULL) { + sp_buf_printf(out, " %-10s ", sp_t("status.limits")); + sp_buf_printf(out, sp_t("status.limits_value"), + ag->cfg->max_bytes, ag->cfg->max_steps, + ag->cfg->autonomous ? sp_t("status.autonomous") : ""); + sp_buf_puts(out, "\n"); + } + } + if (host != NULL) { + (void)sp_host_compaction_stats(host, &comp, &failed, &turns); + sp_buf_printf(out, " %-10s ", sp_t("status.compact")); + sp_buf_printf(out, sp_t("status.compact_value"), comp, failed, + turns, ag != NULL ? ag->stats.compactions : 0); + sp_buf_puts(out, "\n"); + memset(&m, 0, sizeof(m)); + sp_host_meter_get(host, &m); + sp_buf_printf(out, " %-10s ", sp_t("status.meter")); + sp_buf_printf(out, sp_t("status.meter_value"), m.ctx_used, + m.ctx_max, m.cached, m.tok_in, m.tok_out); + sp_buf_puts(out, "\n"); + } +} + +/* the little keyboard /menu, /status and /help put under the answer */ +static void +tg_menu_keyboard(struct sp_buf *mark) +{ + sp_buf_puts(mark, "{\"inline_keyboard\":[[{\"text\":\""); + (void)sp_json_escape(mark, sp_t("tg.menu.status")); + sp_buf_puts(mark, "\",\"callback_data\":\"status\"},{\"text\":\""); + (void)sp_json_escape(mark, sp_t("tg.menu.model")); + sp_buf_puts(mark, "\",\"callback_data\":\"modelmenu\"}],[{\"text\":\""); + (void)sp_json_escape(mark, sp_t("tg.menu.new")); + sp_buf_puts(mark, "\",\"callback_data\":\"new\"},{\"text\":\""); + (void)sp_json_escape(mark, sp_t("tg.menu.help")); + sp_buf_puts(mark, "\",\"callback_data\":\"help\"}]]}"); +} + /* * /model probe: ask every endpoint what it serves (/v1/models) and offer the * answer as buttons. The buttons carry an index into this table, not a model @@ -1673,7 +1778,8 @@ tg_new_conversation(const char *token, long chat_id, struct sp_agent *ag, */ static int tg_callback(const char *token, const char *allow, const mcp_json *cq, - struct sp_agent *ag, struct sp_config *conf) + struct sp_agent *ag, struct sp_config *conf, struct sp_host *host, + const char *sess_dir) { const mcp_json *from, *msg; const char *data, *cqid, *sep; @@ -1694,6 +1800,71 @@ tg_callback(const char *token, const char *allow, const mcp_json *cq, } if (data == NULL || cqid == NULL || !allowed(allow, from_id)) return 0; + if (strcmp(data, "status") == 0) { + tg_answer_callback(token, cqid, sp_t("tg.menu.toast")); + if (ag != NULL && conf != NULL && chat != 0) { + struct sp_buf text = { 0 }, mark = { 0 }, sink = { 0 }; + + if (sp_buf_init(&text) == 0 && sp_buf_init(&mark) == 0 && + sp_buf_init(&sink) == 0) { + tg_status_text(ag, conf, host, &text); + tg_menu_keyboard(&mark); + (void)send_message_markup(token, chat, text.p, + mark.p, &sink); + } + sp_buf_free(&text); + sp_buf_free(&mark); + sp_buf_free(&sink); + } + return 1; + } + if (strcmp(data, "modelmenu") == 0) { + tg_answer_callback(token, cqid, sp_t("tg.menu.toast")); + if (mid != 0) + tg_clear_buttons(token, chat, mid); + if (conf != NULL && chat != 0) { + struct sp_buf sink; + + if (sp_buf_init(&sink) == 0) { + tg_model_menu(token, chat, conf, &sink); + sp_buf_free(&sink); + } + } + return 1; + } + if (strcmp(data, "new") == 0) { + tg_answer_callback(token, cqid, sp_t("tg.menu.toast")); + if (mid != 0) + tg_clear_buttons(token, chat, mid); + if (ag != NULL && chat != 0) { + struct sp_buf sink; + + if (sp_buf_init(&sink) == 0) { + tg_new_conversation(token, chat, ag, &sink); + sp_buf_free(&sink); + } + } + return 1; + } + if (strcmp(data, "help") == 0) { + tg_answer_callback(token, cqid, sp_t("tg.menu.toast")); + if (chat != 0) { + struct sp_buf text = { 0 }, mark = { 0 }, sink = { 0 }; + const char *dir = sess_dir != NULL ? sess_dir : ""; + + if (sp_buf_init(&text) == 0 && sp_buf_init(&mark) == 0 && + sp_buf_init(&sink) == 0) { + tg_help_text(host, dir, &text); + tg_menu_keyboard(&mark); + (void)send_message_markup(token, chat, text.p, + mark.p, &sink); + } + sp_buf_free(&text); + sp_buf_free(&mark); + sp_buf_free(&sink); + } + return 1; + } if (strncmp(data, "model:", 6) == 0) { tg_answer_callback(token, cqid, sp_t("model.toast")); if (mid != 0) @@ -1872,7 +2043,8 @@ tg_approve(void *ctx, const char *name, int auth, const char *args) tg_offset = uid + 1; cq = mcp_arg(update, "callback_query"); if (cq != NULL) { - (void)tg_callback(tg_cur.token, tg_cur.allow, cq, NULL, NULL); + (void)tg_callback(tg_cur.token, tg_cur.allow, cq, + NULL, NULL, NULL, NULL); continue; } m = mcp_arg(update, "message"); @@ -1994,7 +2166,9 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, tg_offset = update_id + 1; cq = mcp_arg(update, "callback_query"); if (cq != NULL) { - (void)tg_callback(token, allow, cq, ag, conf); + (void)tg_callback(token, allow, cq, ag, conf, + host, ag != NULL && ag->sess != NULL ? + ag->sess->dir : NULL); continue; } msg = mcp_arg(update, "message"); @@ -2074,18 +2248,56 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, sp_buf_clear(err); continue; } + + if (strcmp(text, "/status") == 0) { + struct sp_buf st = { 0 }, mark = { 0 }, + se = { 0 }; + + if (sp_buf_init(&st) == 0 && + sp_buf_init(&mark) == 0 && + sp_buf_init(&se) == 0) { + tg_status_text(ag, conf, host, &st); + tg_menu_keyboard(&mark); + (void)send_message_markup(token, + chat_id, st.p, mark.p, &se); + } + sp_buf_free(&st); + sp_buf_free(&mark); + sp_buf_free(&se); + sp_buf_clear(err); + continue; + } + if (strcmp(text, "/menu") == 0) { + struct sp_buf mark = { 0 }, se = { 0 }; + + if (sp_buf_init(&mark) == 0 && + sp_buf_init(&se) == 0) { + tg_menu_keyboard(&mark); + (void)send_message_markup(token, + chat_id, sp_t("tg.menu.text"), + mark.p, &se); + } + sp_buf_free(&mark); + sp_buf_free(&se); + sp_buf_clear(err); + continue; + } if (strcmp(text, "/start") == 0 || strcmp(text, "/help") == 0) { - struct sp_buf help; + struct sp_buf help = { 0 }, mark = { 0 }; - if (sp_buf_init(&help) == 0) { + if (sp_buf_init(&help) == 0 && + sp_buf_init(&mark) == 0) { tg_help_text(host, ag->sess != NULL ? ag->sess->dir : NULL, &help); - (void)send_message(token, chat_id, - help.p != NULL ? help.p : - sp_t("tg.help"), err); - sp_buf_free(&help); + tg_menu_keyboard(&mark); + (void)send_message_markup(token, + chat_id, help.p != NULL ? + help.p : sp_t("tg.help"), + mark.p, err); } + sp_buf_free(&help); + sp_buf_free(&mark); sp_buf_clear(err); continue; } diff --git a/tests/fake_telegram.py b/tests/fake_telegram.py index 037f52f..24359c6 100644 --- a/tests/fake_telegram.py +++ b/tests/fake_telegram.py @@ -89,6 +89,7 @@ class H(http.server.BaseHTTPRequestHandler): return self.reply({"ok": True, "result": True}) if ("/answerCallbackQuery" in self.path or "/editMessageReplyMarkup" in self.path or + "/setChatMenuButton" in self.path or "/sendChatAction" in self.path): return self.reply({"ok": True, "result": True}) if "/sendPhoto" in self.path: @@ -109,6 +110,10 @@ class H(http.server.BaseHTTPRequestHandler): if markup is not None: f.write("markup: " + json.dumps( markup, separators=(",", ":")) + "\n") + # /menu: the inline keyboard arrives; press Status. The status + # text itself starts with "Status (", so this does not loop. + if text.startswith("MenĂ¼"): + add_callback("status") # /model: the menu arrives, press the second provider (the tests # run with SPLINTER_LOCALE=de) if "Waehle ein Modell" in text: diff --git a/tests/test_gateway.sh b/tests/test_gateway.sh index 11630c9..5ac0007 100755 --- a/tests/test_gateway.sh +++ b/tests/test_gateway.sh @@ -61,6 +61,8 @@ run ok 0 check "the command menu is registered" grep -q '^cmd: start' "$D/sent.log" check "the menu carries plugin commands" grep -q '^cmd: goal' "$D/sent.log" check "the menu carries command scripts" grep -q '^cmd: hello' "$D/sent.log" +check "the menu carries /status" grep -q '^cmd: status' "$D/sent.log" +check "the menu carries /menu" grep -q '^cmd: menu' "$D/sent.log" check "approval is asked in the chat, with the tool" \ grep -q '^Zustimmung [0-9]*: write ' "$D/sent.log" check "approval carries inline buttons" \ @@ -184,6 +186,20 @@ check "the button switches the model" \ check "the next turn goes to the new endpoint" \ grep -q '^openai:' "$D/sent.log" +# /status: the facts plus the same inline menu +run_text "/status" 'Status (' 120 +check "status names the session" grep -q 'Sitzung' "$D/sent.log" +check "status names the model" grep -q 'Modell' "$D/sent.log" +check "status names the endpoint" grep -q 'Endpunkt' "$D/sent.log" +check "status carries the inline menu" grep -q '"callback_data":"status"' "$D/sent.log" +check "status carries the model button" grep -q '"callback_data":"modelmenu"' "$D/sent.log" + +# /menu: the inline keyboard, and pressing Status goes through the callback +run_text "/menu" 'Status (' 130 'Sitzung' +check "the menu carries the status button" grep -q '"callback_data":"status"' "$D/sent.log" +check "the menu carries the model button" grep -q '"callback_data":"modelmenu"' "$D/sent.log" +check "the status callback answers" grep -q 'Sitzung' "$D/sent.log" + # /model probe asks the endpoint what it serves and switches to one of them run_text "/model probe" 'Provider local' 55 check "the probe lists served models" grep -q '"callback_data":"mm:0"' "$D/sent.log"