From ac18b29a4d1de0b8c8cb470ac64ce55a10b37895 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 10 Nov 2026 06:58:12 +0000 Subject: [PATCH] telegram: the command menu follows what the harness has setMyCommands is built at startup from /start, /help, every command a loaded plugin registers and every program in ~/.splinter/commands whose name is a valid Telegram command. /start and /help list the same live set in the chat; a name Telegram cannot take (a dot, upper case, over 32 characters) stays usable in the chat but cannot appear in the menu. The live bot now shows start, help, goal, recap and the local uptime script; loading another plugin adds its command without editing the gateway. test_gateway.sh loads focus and a command script and checks that both reach the menu. --- README.md | 13 ++-- TODO.md | 7 +- docs/PLUGINS.md | 5 +- share/locale/de.msg | 1 + share/locale/en.msg | 1 + share/locale/pt_BR.msg | 1 + src/telegram.c | 151 ++++++++++++++++++++++++++++++++++++----- src/www/index.en.html | 4 +- src/www/index.html | 4 +- src/www/index.pt.html | 8 +-- tests/test_gateway.sh | 8 ++- tools/gen_www.py | 10 +-- 12 files changed, 172 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 4b74119..e0bdfc8 100644 --- a/README.md +++ b/README.md @@ -289,11 +289,14 @@ Signature handling is the session's business, not the gateway's. A bot cannot start a conversation - the user has to write first, otherwise the API answers `chat not found`. That is Telegram, not the harness. -The bot registers its commands with `setMyCommands`, so Telegram's menu button -shows `/start`, `/help`, `/goal`, `/recap` and `/tasks`. `/start` and `/help` -answer with the short command list. An approval arrives as a message with -**Yes/No buttons**; the callback answers the button, removes the buttons and -writes the ack file. The text form `ok ` / `no ` still works. +The bot registers the commands that actually exist with `setMyCommands`: +`/start`, `/help`, every command a loaded plugin registers (`/goal`, `/recap`, +`/tasks`, `/quota`, `/tmux`, ...) and every program in +`~/.splinter/commands/` whose name is a valid Telegram command (lower-case, +digits, underscore, at most 32). `/start` and `/help` answer with the short +text plus that live list. An approval arrives as a message with **Yes/No +buttons**; the callback answers the button, removes the buttons and writes the +ack file. The text form `ok ` / `no ` still works. `docs/splinter-gateway.service` is a systemd user unit if it should survive a logout. diff --git a/TODO.md b/TODO.md index 3b657fc..6b19209 100644 --- a/TODO.md +++ b/TODO.md @@ -47,9 +47,10 @@ Naechster Schritt: entscheiden, ob die npm-Reste weg duerfen. Kein Code. im Chat, das Gateway loeste sie auf, der `write` lief und schrieb `/tmp/opencode/ack-proof.txt`. Das installierte `~/.splinter/bin/tg-ask` war eine alte Kopie ohne Tokenpfad und wurde durch `tools/tg-ask.sh` ersetzt. -- **Telegram-Menue und Knoepfe**: `setMyCommands` registriert `/start`, - `/help`, `/goal`, `/recap`, `/tasks`; `/start` und `/help` antworten mit der - Befehlsliste. Zustimmungen kommen mit Ja/Nein-Knoepfen; `callback_query` +- **Telegram-Menue und Knoepfe**: `setMyCommands` registriert dynamisch + `/start`, `/help`, alle Befehle der geladenen Plugins und alle Programme in + `~/.splinter/commands/` mit gueltigem Telegram-Namen; `/start` und `/help` + listen sie im Chat. Zustimmungen kommen mit Ja/Nein-Knoepfen; `callback_query` prueft denselben Allow-List-Eintrag, antwortet, entfernt die Knoepfe und legt die Antwortdatei ab. Der Textweg `ok ` bleibt. `test_gateway.sh` prueft Menue, Markup, den Fremden-Callback und den echten Callback. diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index 20dbd4b..b1e7dc4 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -217,7 +217,10 @@ Antworten, aus jedem Prozess: Ein Timeout ist ein Nein. Stille ist keine Zustimmung. -Das Telegram-Gateway legt die Anfrage als Nachricht mit **Ja/Nein-Knoepfen** vor. +Das Telegram-Gateway registriert seine Befehle beim Start: die der geladenen +Plugins, die Programme in `~/.splinter/commands/` mit gueltigem Telegram-Namen +und `/start`/`/help`; `/help` listet sie im Chat. Die Anfrage selbst legt es als +Nachricht mit **Ja/Nein-Knoepfen** vor. Der Knopf ist ein `callback_query`, kein Text: das Gateway prueft denselben Allow-List-Eintrag, antwortet dem Knopf, entfernt die Knoepfe und legt die Antwortdatei ab. Die Textform `ok ` / `no ` bleibt als Rueckfall. diff --git a/share/locale/de.msg b/share/locale/de.msg index 5f1b53e..4db00c2 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -96,6 +96,7 @@ tg.cmd.start = Begruessung und Befehle tg.cmd.help = Hilfe und Befehle tg.cmd.goal = Ziel setzen oder zeigen tg.cmd.recap = Ziel in die naechste Runde legen +tg.cmd.run = fuehrt /%s aus tg.cmd.tasks = Plandateien zeigen tg.help = Ich bin splinter. Schreib eine Nachricht fuer einen Turn. /goal setzt das Ziel, /recap legt es in die naechste Runde, /tasks zeigt den Plan. Aenderungen fragen hier mit Ja/Nein oder ok / no . ack.yes = Ja diff --git a/share/locale/en.msg b/share/locale/en.msg index 5167d60..1192dbf 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -95,6 +95,7 @@ tg.cmd.start = Greeting and commands tg.cmd.help = Help and commands tg.cmd.goal = Set or show the goal tg.cmd.recap = Put the goal into the next turn +tg.cmd.run = run /%s tg.cmd.tasks = Show the plan tg.help = I am splinter. Send a message for a turn. /goal sets the goal, /recap puts it into the next turn, /tasks shows the plan. Changes ask here with Yes/No or ok / no . ack.yes = Yes diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 68a38ec..efd99e8 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -99,6 +99,7 @@ tg.cmd.start = Saudacao e comandos tg.cmd.help = Ajuda e comandos tg.cmd.goal = Definir ou mostrar a meta tg.cmd.recap = Levar a meta para o proximo turno +tg.cmd.run = executa /%s tg.cmd.tasks = Mostrar o plano tg.help = Eu sou o splinter. Envie uma mensagem para um turno. /goal define a meta, /recap leva a meta para o proximo turno, /tasks mostra o plano. Mudancas perguntam aqui com Sim/Nao ou ok / no . ack.yes = Sim diff --git a/src/telegram.c b/src/telegram.c index b77ef2d..426f6a2 100644 --- a/src/telegram.c +++ b/src/telegram.c @@ -319,28 +319,116 @@ tg_clear_buttons(const char *token, long chat, long mid) * descriptions come from the catalogue, so a German gateway says German. The * menu is best effort: a bot that cannot set it still answers messages. */ -static void -tg_set_commands(const char *token) -{ - static const char *const names[] = { "start", "help", "goal", "recap", - "tasks" }; - static const char *const keys[] = { "tg.cmd.start", "tg.cmd.help", - "tg.cmd.goal", "tg.cmd.recap", "tg.cmd.tasks" }; - struct sp_buf body; - size_t i; +struct tg_cmd { + char name[40]; + char desc[200]; +}; +#define TG_MAX_CMDS 100 + +static const char * +tg_known_desc(const char *name) +{ + if (strcmp(name, "start") == 0) + return sp_t("tg.cmd.start"); + if (strcmp(name, "help") == 0) + return sp_t("tg.cmd.help"); + if (strcmp(name, "goal") == 0) + return sp_t("tg.cmd.goal"); + if (strcmp(name, "recap") == 0) + return sp_t("tg.cmd.recap"); + if (strcmp(name, "tasks") == 0) + return sp_t("tg.cmd.tasks"); + return NULL; +} + +/* + * Telegram command names are lower-case a-z, digits and underscore, at most 32 + * characters. A program called "deploy.sh" stays usable as /deploy.sh in the + * chat but cannot appear in the menu; the help text still lists it. + */ +static void +tg_add_cmd(struct tg_cmd *cmds, int *n, const char *name, const char *desc) +{ + size_t i; + const char *p; + + if (name == NULL || *name == '\0' || *n >= TG_MAX_CMDS) + return; + for (i = 0; name[i] != '\0'; i++) { + if (i >= 32) + return; + if (!((name[i] >= 'a' && name[i] <= 'z') || + (name[i] >= '0' && name[i] <= '9') || name[i] == '_')) + return; + } + for (i = 0; i < (size_t)*n; i++) + if (strcmp(cmds[i].name, name) == 0) + return; + snprintf(cmds[*n].name, sizeof(cmds[*n].name), "%s", name); + p = desc != NULL && *desc != '\0' ? desc : name; + snprintf(cmds[*n].desc, sizeof(cmds[*n].desc), "%s", p); + (*n)++; +} + +/* host commands from plugins, then the programs in commands/ */ +static void +tg_collect(struct sp_host *host, const char *sess_dir, struct tg_cmd *cmds, + int *n) +{ + struct sp_buf list; + char dir[700], generic[200]; + char *tok, *save; + + tg_add_cmd(cmds, n, "start", tg_known_desc("start")); + tg_add_cmd(cmds, n, "help", tg_known_desc("help")); + if (sp_buf_init(&list) != 0) + return; + if (host != NULL) { + (void)sp_host_command_help(host, &list); + for (tok = list.len > 0 ? strtok_r(list.p, " /", &save) : NULL; + tok != NULL; tok = strtok_r(NULL, " /", &save)) { + const char *d = tg_known_desc(tok); + + if (d == NULL) { + snprintf(generic, sizeof(generic), + sp_t("tg.cmd.run"), tok); + d = generic; + } + tg_add_cmd(cmds, n, tok, d); + } + } + if (sp_cmdscript_dir(sess_dir, dir, sizeof(dir)) == 0) { + sp_buf_clear(&list); + (void)sp_cmdscript_list(dir, &list); + for (tok = list.len > 0 ? strtok_r(list.p, " /", &save) : NULL; + tok != NULL; tok = strtok_r(NULL, " /", &save)) { + snprintf(generic, sizeof(generic), sp_t("tg.cmd.run"), + tok); + tg_add_cmd(cmds, n, tok, generic); + } + } + sp_buf_free(&list); +} + +static void +tg_set_commands(const char *token, struct sp_host *host, const char *sess_dir) +{ + struct tg_cmd cmds[TG_MAX_CMDS]; + struct sp_buf body; + int n = 0, i; + + tg_collect(host, sess_dir, cmds, &n); if (sp_buf_init(&body) != 0) return; sp_buf_puts(&body, "{\"commands\":["); - for (i = 0; i < sizeof(names) / sizeof(names[0]); i++) { - const char *d = sp_t(keys[i]); - + for (i = 0; i < n; i++) { if (i > 0) sp_buf_puts(&body, ","); sp_buf_puts(&body, "{\"command\":\""); - sp_json_escape(&body, names[i]); + sp_json_escape(&body, cmds[i].name); sp_buf_puts(&body, "\",\"description\":\""); - sp_json_escape(&body, d != NULL && *d != '\0' ? d : names[i]); + sp_json_escape(&body, cmds[i].desc); sp_buf_puts(&body, "\"}"); } sp_buf_puts(&body, "]}"); @@ -348,6 +436,26 @@ tg_set_commands(const char *token) sp_buf_free(&body); } +/* /start and /help: the short text plus every command that exists right now */ +static void +tg_help_text(struct sp_host *host, const char *sess_dir, struct sp_buf *out) +{ + struct sp_buf list; + char dir[700]; + + sp_buf_puts(out, sp_t("tg.help")); + sp_buf_puts(out, "\n\n/start /help "); + if (host != NULL) + (void)sp_host_command_help(host, out); + if (sp_cmdscript_dir(sess_dir, dir, sizeof(dir)) == 0 && + sp_buf_init(&list) == 0) { + (void)sp_cmdscript_list(dir, &list); + if (list.len > 0) + sp_buf_puts(out, list.p); + sp_buf_free(&list); + } +} + /* * A Telegram id as a long. mcp_arg_int truncated to int: user ids above 2^31 * (most accounts made in recent years) became garbage and were never @@ -622,7 +730,8 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, (void)signal(SIGINT, on_signal); (void)signal(SIGTERM, on_signal); - tg_set_commands(token); + tg_set_commands(token, host, + ag->sess != NULL ? ag->sess->dir : NULL); fprintf(stderr, "splinter: gateway listening (allow: %s)\n", allow != NULL && *allow != '\0' ? allow : "nobody"); @@ -706,8 +815,16 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, continue; if (strcmp(text, "/start") == 0 || strcmp(text, "/help") == 0) { - (void)send_message(token, chat_id, - sp_t("tg.help"), err); + struct sp_buf help; + + if (sp_buf_init(&help) == 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); + } sp_buf_clear(err); continue; } diff --git a/src/www/index.en.html b/src/www/index.en.html index a60b56a..7ea4dc3 100644 --- a/src/www/index.en.html +++ b/src/www/index.en.html @@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }

splinter_

An agent harness you own: chat in the terminal, tools with authorization classes, plugins, any model that speaks OpenAI — built by us so we can change it, instead of getting used to how somebody else's client behaves.

-

24090 lines of C99/POSIX · +

24207 lines of C99/POSIX · 23 tools · 31 test programs · 16 plugins · 42 palettes · BSD-3-Clause

@@ -141,7 +141,7 @@ plugin = plugins/reminder/reminder.so

Gateway: Telegram

splinter --daemon long-polls getUpdates, one turn per message, answers with sendMessage. The token lives in ~/.splinter/telegram.token with mode 0600 and is never passed as an argument — arguments end up in ps. While a turn runs, the chat sees "typing" every four seconds.

-

The bot registers its commands: the menu shows /start, /goal, /recap and /tasks; an approval arrives with Yes/No buttons.

+

The bot registers its commands: the menu shows the loaded plugins' commands and the programs in ~/.splinter/commands/; an approval arrives with Yes/No buttons.

Slash commands work there too. Programs in ~/.splinter/commands/ become /name: they get the rest of the line as an argument, and their output goes to the model as context — not to you as a reply. That is how the model fetches what it cannot see itself.

What gets written down

diff --git a/src/www/index.html b/src/www/index.html index 29177f9..857c47a 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }

splinter_

Ein Agent-Harness, das dir gehoert: Chat im Terminal, Werkzeuge mit Autorisierungsklassen, Plugins, jedes Modell das OpenAI spricht — von uns gebaut, damit wir es umbauen koennen, statt uns an das Verhalten eines fremden Clients zu gewoehnen.

-

24090 Zeilen C99/POSIX · +

24207 Zeilen C99/POSIX · 23 Werkzeuge · 31 Test-Programme · 16 Plugins · 42 Farbschemata · BSD-3-Clause

@@ -141,7 +141,7 @@ plugin = plugins/reminder/reminder.so

Gateway: Telegram

splinter --daemon pollt getUpdates, ein Turn pro Nachricht, Antwort per sendMessage. Der Token liegt in ~/.splinter/telegram.token mit Modus 0600 und wird nie als Argument uebergeben — Argumente stehen in ps. Waehrend eines Turns sieht der Chat alle vier Sekunden „typing“.

-

Der Bot registriert seine Befehle: das Menue zeigt /start, /goal, /recap und /tasks; eine Zustimmung kommt mit Ja/Nein-Knoepfen.

+

Der Bot registriert seine Befehle: das Menue zeigt die geladenen Plugin-Befehle und die Programme in ~/.splinter/commands/; eine Zustimmung kommt mit Ja/Nein-Knoepfen.

Slash-Kommandos funktionieren dort genauso. Programme in ~/.splinter/commands/ werden zu /name: sie bekommen den Rest der Zeile als Argument, und ihre Ausgabe geht ans Modell als Kontext — nicht an dich als Antwort. Damit holt sich das Modell, was es selbst nicht sehen kann.

Was mitgeschrieben wird

diff --git a/src/www/index.pt.html b/src/www/index.pt.html index 00f090a..641dbba 100644 --- a/src/www/index.pt.html +++ b/src/www/index.pt.html @@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }

splinter_

Um harness de agente que e seu: chat no terminal, ferramentas com classes de autorizacao, plugins, qualquer modelo que fale OpenAI - escrito por nos para poder mudar-lo, em vez de nos acostumar com o comportamento de um cliente alheio.

-

24090 linhas C99/POSIX · +

24207 linhas C99/POSIX · 23 ferramentas · 31 programas de teste · 16 plugins · 42 paletas · BSD-3-Clause

@@ -107,9 +107,9 @@ em ordem de nome. Chave desconhecida e erro com numero de linha.

Gateway: Telegram

splinter --daemon fala Telegram com indicador de digitacao e os -mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra -/start, /goal, /recap e -/tasks; uma aprovacao chega com botoes Sim/Nao.

+mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra os comandos dos plugins carregados +e os programas em ~/.splinter/commands/; uma aprovacao chega com +botoes Sim/Nao.

O que fica registrado

Cada sessao escreve session.log (eventos legiveis) e diff --git a/tests/test_gateway.sh b/tests/test_gateway.sh index 980ed03..ac53254 100755 --- a/tests/test_gateway.sh +++ b/tests/test_gateway.sh @@ -15,8 +15,10 @@ fails=0 run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt P=$((20000 + ($$ + ${2:-0}) % 30000)) rm -rf "$D/home" "$D/out.txt" "$D/sent.log" - mkdir -p "$D/home" - printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\n' \ + mkdir -p "$D/home/.splinter/commands" + printf '#!/bin/sh\necho hallo\n' > "$D/home/.splinter/commands/hello" + chmod +x "$D/home/.splinter/commands/hello" + printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\nplugin = plugins/focus/focus.so\n' \ "$P" > "$D/home/splinter.conf" python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" "$1" & S=$! @@ -45,6 +47,8 @@ check() { # $1 = description, $2.. = command 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 "approval is asked in the chat, with the tool" \ grep -q '^Zustimmung [0-9]*: write ' "$D/sent.log" check "approval carries inline buttons" \ diff --git a/tools/gen_www.py b/tools/gen_www.py index 27a39b5..eab88e7 100644 --- a/tools/gen_www.py +++ b/tools/gen_www.py @@ -305,7 +305,7 @@ SECTIONS = { "sendMessage. Der Token liegt in " "~/.splinter/telegram.token mit Modus 0600 und wird nie als Argument " "uebergeben — Argumente stehen in ps. Waehrend eines Turns sieht der " - "Chat alle vier Sekunden „typing“.

\n

Der Bot registriert seine Befehle: das Menue zeigt /start, /goal, /recap und /tasks; eine Zustimmung kommt mit Ja/Nein-Knoepfen.

\n

Slash-Kommandos funktionieren " + "Chat alle vier Sekunden „typing“.

\n

Der Bot registriert seine Befehle: das Menue zeigt die geladenen Plugin-Befehle und die Programme in ~/.splinter/commands/; eine Zustimmung kommt mit Ja/Nein-Knoepfen.

\n

Slash-Kommandos funktionieren " "dort genauso. Programme in ~/.splinter/commands/ werden zu " "/name: sie bekommen den Rest der Zeile als Argument, und ihre Ausgabe " "geht ans Modell als Kontext — nicht an dich als Antwort. Damit holt sich " @@ -385,7 +385,7 @@ SECTIONS = { "sendMessage. The token lives in " "~/.splinter/telegram.token with mode 0600 and is never passed as an " "argument — arguments end up in ps. While a turn runs, the chat sees " - ""typing" every four seconds.

\n

The bot registers its commands: the menu shows /start, /goal, /recap and /tasks; an approval arrives with Yes/No buttons.

\n

Slash commands work there too. " + ""typing" every four seconds.

\n

The bot registers its commands: the menu shows the loaded plugins' commands and the programs in ~/.splinter/commands/; an approval arrives with Yes/No buttons.

\n

Slash commands work there too. " "Programs in ~/.splinter/commands/ become /name: they get " "the rest of the line as an argument, and their output goes to the model as " "context — not to you as a reply. That is how the model fetches what it cannot see " @@ -551,9 +551,9 @@ autonomous = 1 em ordem de nome. Chave desconhecida e erro com numero de linha.

""", """

Gateway: Telegram

splinter --daemon fala Telegram com indicador de digitacao e os -mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra -/start, /goal, /recap e -/tasks; uma aprovacao chega com botoes Sim/Nao.

""", +mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra os comandos dos plugins carregados +e os programas em ~/.splinter/commands/; uma aprovacao chega com +botoes Sim/Nao.

""", """

O que fica registrado

Cada sessao escreve session.log (eventos legiveis) e wire.jsonl (o que foi de fato enviado ao modelo).

""",