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.
This commit is contained in:
lotherk committed 2026-11-10 06:58:12 +00:00
1 parent 50e5eb9111
commit ac18b29a4d
12 files changed
+172 -41

No files matched your search

+8 -5
View File
@@ -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 <id>` / `no <id>` 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 <id>` / `no <id>` still works.
`docs/splinter-gateway.service` is a systemd user unit if it should survive a
logout.
+4 -3
View File
@@ -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 <id>` bleibt. `test_gateway.sh` prueft
Menue, Markup, den Fremden-Callback und den echten Callback.
+4 -1
View File
@@ -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 <id>` / `no <id>` bleibt als Rueckfall.
+1
View File
@@ -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 <id> / no <id>.
ack.yes = Ja
+1
View File
@@ -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 <id> / no <id>.
ack.yes = Yes
+1
View File
@@ -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 <id> / no <id>.
ack.yes = Sim
+134 -17
View File
@@ -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;
}
+2 -2
View File
@@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }
<h1>splinter<span>_</span></h1>
<p class="lead">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.</p>
<p class="meta"><b>24090</b> lines of C99/POSIX &middot;
<p class="meta"><b>24207</b> lines of C99/POSIX &middot;
<b>23</b> tools &middot; <b>31</b> test programs &middot;
<b>16</b> plugins &middot; <b>42</b> palettes &middot; BSD-3-Clause</p>
@@ -141,7 +141,7 @@ plugin = plugins/reminder/reminder.so</pre>
<h2>Gateway: Telegram</h2>
<p><code>splinter --daemon</code> long-polls <code>getUpdates</code>, one turn per message, answers with <code>sendMessage</code>. The token lives in <code>~/.splinter/telegram.token</code> with mode 0600 and is never passed as an argument — arguments end up in <code>ps</code>. While a turn runs, the chat sees &quot;typing&quot; every four seconds.</p>
<p>The bot registers its commands: the menu shows <code>/start</code>, <code>/goal</code>, <code>/recap</code> and <code>/tasks</code>; an approval arrives with Yes/No buttons.</p>
<p>The bot registers its commands: the menu shows the loaded plugins' commands and the programs in <code>~/.splinter/commands/</code>; an approval arrives with Yes/No buttons.</p>
<p>Slash commands work there too. Programs in <code>~/.splinter/commands/</code> become <code>/name</code>: they get the rest of the line as an argument, and their output goes <em>to the model</em> as context — not to you as a reply. That is how the model fetches what it cannot see itself.</p>
<h2>What gets written down</h2>
+2 -2
View File
@@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }
<h1>splinter<span>_</span></h1>
<p class="lead">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.</p>
<p class="meta"><b>24090</b> Zeilen C99/POSIX &middot;
<p class="meta"><b>24207</b> Zeilen C99/POSIX &middot;
<b>23</b> Werkzeuge &middot; <b>31</b> Test-Programme &middot;
<b>16</b> Plugins &middot; <b>42</b> Farbschemata &middot; BSD-3-Clause</p>
@@ -141,7 +141,7 @@ plugin = plugins/reminder/reminder.so</pre>
<h2>Gateway: Telegram</h2>
<p><code>splinter --daemon</code> pollt <code>getUpdates</code>, ein Turn pro Nachricht, Antwort per <code>sendMessage</code>. Der Token liegt in <code>~/.splinter/telegram.token</code> mit Modus 0600 und wird nie als Argument uebergeben — Argumente stehen in <code>ps</code>. Waehrend eines Turns sieht der Chat alle vier Sekunden &bdquo;typing&ldquo;.</p>
<p>Der Bot registriert seine Befehle: das Menue zeigt <code>/start</code>, <code>/goal</code>, <code>/recap</code> und <code>/tasks</code>; eine Zustimmung kommt mit Ja/Nein-Knoepfen.</p>
<p>Der Bot registriert seine Befehle: das Menue zeigt die geladenen Plugin-Befehle und die Programme in <code>~/.splinter/commands/</code>; eine Zustimmung kommt mit Ja/Nein-Knoepfen.</p>
<p>Slash-Kommandos funktionieren dort genauso. Programme in <code>~/.splinter/commands/</code> werden zu <code>/name</code>: sie bekommen den Rest der Zeile als Argument, und ihre Ausgabe geht <em>ans Modell</em> als Kontext — nicht an dich als Antwort. Damit holt sich das Modell, was es selbst nicht sehen kann.</p>
<h2>Was mitgeschrieben wird</h2>
+4 -4
View File
@@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }
<h1>splinter<span>_</span></h1>
<p class="lead">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.</p>
<p class="meta"><b>24090</b> linhas C99/POSIX &middot;
<p class="meta"><b>24207</b> linhas C99/POSIX &middot;
<b>23</b> ferramentas &middot; <b>31</b> programas de teste &middot;
<b>16</b> plugins &middot; <b>42</b> paletas &middot; BSD-3-Clause</p>
@@ -107,9 +107,9 @@ em ordem de nome. Chave desconhecida e erro com numero de linha.</p>
<h2>Gateway: Telegram</h2>
<p><code>splinter --daemon</code> fala Telegram com indicador de digitacao e os
mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra
<code>/start</code>, <code>/goal</code>, <code>/recap</code> e
<code>/tasks</code>; uma aprovacao chega com botoes Sim/Nao.</p>
mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra os comandos dos plugins carregados
e os programas em <code>~/.splinter/commands/</code>; uma aprovacao chega com
botoes Sim/Nao.</p>
<h2>O que fica registrado</h2>
<p>Cada sessao escreve <code>session.log</code> (eventos legiveis) e
+6 -2
View File
@@ -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" \
+5 -5
View File
@@ -305,7 +305,7 @@ SECTIONS = {
"<code>sendMessage</code>. Der Token liegt in "
"<code>~/.splinter/telegram.token</code> mit Modus 0600 und wird nie als Argument "
"uebergeben — Argumente stehen in <code>ps</code>. Waehrend eines Turns sieht der "
"Chat alle vier Sekunden &bdquo;typing&ldquo;.</p>\n<p>Der Bot registriert seine Befehle: das Menue zeigt <code>/start</code>, <code>/goal</code>, <code>/recap</code> und <code>/tasks</code>; eine Zustimmung kommt mit Ja/Nein-Knoepfen.</p>\n<p>Slash-Kommandos funktionieren "
"Chat alle vier Sekunden &bdquo;typing&ldquo;.</p>\n<p>Der Bot registriert seine Befehle: das Menue zeigt die geladenen Plugin-Befehle und die Programme in <code>~/.splinter/commands/</code>; eine Zustimmung kommt mit Ja/Nein-Knoepfen.</p>\n<p>Slash-Kommandos funktionieren "
"dort genauso. Programme in <code>~/.splinter/commands/</code> werden zu "
"<code>/name</code>: sie bekommen den Rest der Zeile als Argument, und ihre Ausgabe "
"geht <em>ans Modell</em> als Kontext — nicht an dich als Antwort. Damit holt sich "
@@ -385,7 +385,7 @@ SECTIONS = {
"<code>sendMessage</code>. The token lives in "
"<code>~/.splinter/telegram.token</code> with mode 0600 and is never passed as an "
"argument — arguments end up in <code>ps</code>. While a turn runs, the chat sees "
"&quot;typing&quot; every four seconds.</p>\n<p>The bot registers its commands: the menu shows <code>/start</code>, <code>/goal</code>, <code>/recap</code> and <code>/tasks</code>; an approval arrives with Yes/No buttons.</p>\n<p>Slash commands work there too. "
"&quot;typing&quot; every four seconds.</p>\n<p>The bot registers its commands: the menu shows the loaded plugins' commands and the programs in <code>~/.splinter/commands/</code>; an approval arrives with Yes/No buttons.</p>\n<p>Slash commands work there too. "
"Programs in <code>~/.splinter/commands/</code> become <code>/name</code>: they get "
"the rest of the line as an argument, and their output goes <em>to the model</em> as "
"context — not to you as a reply. That is how the model fetches what it cannot see "
@@ -551,9 +551,9 @@ autonomous = 1</pre>
em ordem de nome. Chave desconhecida e erro com numero de linha.</p>""",
"""<h2>Gateway: Telegram</h2>
<p><code>splinter --daemon</code> fala Telegram com indicador de digitacao e os
mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra
<code>/start</code>, <code>/goal</code>, <code>/recap</code> e
<code>/tasks</code>; uma aprovacao chega com botoes Sim/Nao.</p>""",
mesmos slash commands do terminal. O bot registra seus comandos: o menu mostra os comandos dos plugins carregados
e os programas em <code>~/.splinter/commands/</code>; uma aprovacao chega com
botoes Sim/Nao.</p>""",
"""<h2>O que fica registrado</h2>
<p>Cada sessao escreve <code>session.log</code> (eventos legiveis) e
<code>wire.jsonl</code> (o que foi de fato enviado ao modelo).</p>""",