telegram: own session tree and a status channel
The daemon writes its sessions under <state>/telegram, so a CLI -c in
the same directory never picks a gateway conversation and /new in the
chat never creates a file a CLI would resume. Command scripts and
USER.txt stay in the base state dir; only the session files move.
Add --notify TEXT ("-" reads stdin) and telegram { status_chat = ID }
to send status and cron messages to the status chat. The daemon
announces its start there, and the cron bridge can use the same path.
This commit is contained in:
1 parent
5fd8f76870
commit
6b51f48335
8 files changed
+176
-26
No files matched your search
@@ -469,6 +469,8 @@ int sp_delegate_register(struct sp_registry *, const struct sp_config *,
|
||||
struct sp_host;
|
||||
int sp_telegram_run(struct sp_agent *, struct sp_host *host,
|
||||
const char *token, const char *allow, struct sp_config *conf,
|
||||
const char *base_dir, struct sp_buf *err);
|
||||
int sp_telegram_notify(const struct sp_config *conf, const char *text,
|
||||
struct sp_buf *err);
|
||||
int sp_telegram_token(char *buf, size_t len, const char *path);
|
||||
const char *sp_user_name(void);
|
||||
|
||||
@@ -903,6 +903,13 @@ sp_confblock_translate(const char *text, const char *path, struct sp_buf *out,
|
||||
|
||||
joined(&e[i].v, list, sizeof(list));
|
||||
emit(&c, e[i].line, "telegram_allow = %s", list);
|
||||
} else if (!strcmp(e[i].key, "status_chat")) {
|
||||
const char *s = one(&c, &e[i]);
|
||||
|
||||
if (s == NULL || !clean(&c, &e[i], s))
|
||||
break;
|
||||
emit(&c, e[i].line,
|
||||
"telegram_status_chat = %s", s);
|
||||
} else
|
||||
fail(&c, e[i].line,
|
||||
"unknown telegram field '%s'", e[i].key);
|
||||
|
||||
@@ -676,6 +676,8 @@ load_stream(struct sp_config *c, FILE *f, const char *path,
|
||||
set_str(c->transport, sizeof(c->transport), val);
|
||||
else if (!strcmp(key, "telegram_allow"))
|
||||
set_str(c->telegram_allow, sizeof(c->telegram_allow), val);
|
||||
else if (!strcmp(key, "telegram_status_chat"))
|
||||
c->telegram_status_chat = strtol(val, NULL, 10);
|
||||
else if (!strcmp(key, "locale"))
|
||||
set_str(c->locale, sizeof(c->locale), val);
|
||||
else if (!strcmp(key, "theme"))
|
||||
|
||||
@@ -146,6 +146,7 @@ struct sp_config {
|
||||
size_t ncli_env;
|
||||
char cli_fallback[256]; /* cli delegate ids, comma separated */
|
||||
char telegram_allow[256]; /* comma separated user ids */
|
||||
long telegram_status_chat; /* 0: the first allow id */
|
||||
char locale[32]; /* language code, exported as SPLINTER_LOCALE */
|
||||
char plugins[SP_CONFIG_MAX_PLUGINS][512];
|
||||
size_t nplugins;
|
||||
|
||||
@@ -1633,7 +1633,9 @@ capture_interview(struct sp_engine *e, struct sp_buf *answer)
|
||||
|
||||
if (answer == NULL || answer->p == NULL)
|
||||
return 0;
|
||||
if (!sp_interview_capture(e->sess.dir, answer->p, clean,
|
||||
if (!sp_interview_capture(
|
||||
e->conf != NULL && e->conf->session_dir[0] != '\0' ?
|
||||
e->conf->session_dir : e->sess.dir, answer->p, clean,
|
||||
sizeof(clean)))
|
||||
return 0;
|
||||
sp_buf_clear(answer);
|
||||
@@ -3437,7 +3439,9 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o,
|
||||
* What the interview learned about the user rides in the system
|
||||
* prompt of every session, not in a tool the model has to call.
|
||||
*/
|
||||
char *ub = sp_interview_user_block(e->sess.dir);
|
||||
char *ub = sp_interview_user_block(
|
||||
e->conf != NULL && e->conf->session_dir[0] != '\0' ?
|
||||
e->conf->session_dir : e->sess.dir);
|
||||
|
||||
if (ub != NULL) {
|
||||
size_t bl = e->base_prompt != NULL ?
|
||||
|
||||
+61
-14
@@ -1037,7 +1037,7 @@ static int tg_iv_active;
|
||||
|
||||
static void
|
||||
tg_interview_begin(const char *token, long chat_id, struct sp_agent *ag,
|
||||
struct sp_buf *err)
|
||||
const char *base_dir, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf msg, answer;
|
||||
|
||||
@@ -1068,7 +1068,8 @@ tg_interview_begin(const char *token, long chat_id, struct sp_agent *ag,
|
||||
char clean[16384], note[700];
|
||||
|
||||
if (answer.p != NULL && sp_interview_capture(
|
||||
ag->sess != NULL ? ag->sess->dir : NULL, answer.p, clean,
|
||||
base_dir != NULL && *base_dir != '\0' ? base_dir :
|
||||
(ag->sess != NULL ? ag->sess->dir : NULL), answer.p, clean,
|
||||
sizeof(clean))) {
|
||||
snprintf(note, sizeof(note), "%s", sp_td("interview.saved",
|
||||
"interview saved to USER.txt"));
|
||||
@@ -2075,13 +2076,56 @@ tg_approve(void *ctx, const char *name, int auth, const char *args)
|
||||
return yes;
|
||||
}
|
||||
|
||||
int
|
||||
sp_telegram_notify(const struct sp_config *conf, const char *text,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
char token[256], path[700];
|
||||
const char *base, *home, *p;
|
||||
long chat = 0;
|
||||
|
||||
if (conf == NULL || text == NULL)
|
||||
return -1;
|
||||
chat = conf->telegram_status_chat;
|
||||
if (chat == 0) {
|
||||
p = conf->telegram_allow;
|
||||
while (p != NULL && (*p == ' ' || *p == ','))
|
||||
p++;
|
||||
if (p != NULL && *p != '\0')
|
||||
chat = strtol(p, NULL, 10);
|
||||
}
|
||||
if (chat == 0) {
|
||||
if (err != NULL)
|
||||
sp_buf_puts(err, "telegram: no status_chat and no allow id");
|
||||
return -1;
|
||||
}
|
||||
base = conf->session_dir[0] != '\0' ? conf->session_dir : NULL;
|
||||
home = getenv("HOME");
|
||||
if (base != NULL)
|
||||
snprintf(path, sizeof(path), "%s/telegram.token", base);
|
||||
else
|
||||
snprintf(path, sizeof(path), "%s/.splinter/telegram.token",
|
||||
home != NULL ? home : ".");
|
||||
if (sp_telegram_token(token, sizeof(token), path) != 0) {
|
||||
if (err != NULL)
|
||||
sp_buf_printf(err, "telegram: no token in %s", path);
|
||||
return -1;
|
||||
}
|
||||
/* status text is not a chat answer: send it exactly as written */
|
||||
(void)setenv("SPLINTER_TG_MARKDOWN", "0", 0);
|
||||
return send_message(token, chat, text, err);
|
||||
}
|
||||
|
||||
int
|
||||
sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
const char *allow, struct sp_config *conf, struct sp_buf *err)
|
||||
const char *allow, struct sp_config *conf, const char *base_dir,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf url, reply, answer, aerr;
|
||||
struct sp_agent_config gcfg;
|
||||
const struct sp_agent_config *orig = ag->cfg;
|
||||
const char *cmddir = base_dir != NULL && *base_dir != '\0' ? base_dir :
|
||||
(ag->sess != NULL ? ag->sess->dir : NULL);
|
||||
|
||||
if (sp_buf_init(&url) != 0 || sp_buf_init(&reply) != 0 ||
|
||||
sp_buf_init(&answer) != 0 || sp_buf_init(&aerr) != 0)
|
||||
@@ -2114,10 +2158,16 @@ 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, host,
|
||||
ag->sess != NULL ? ag->sess->dir : NULL);
|
||||
tg_set_commands(token, host, cmddir);
|
||||
fprintf(stderr, "splinter: gateway listening (allow: %s)\n",
|
||||
allow != NULL && *allow != '\0' ? allow : "nobody");
|
||||
if (conf != NULL && conf->telegram_status_chat != 0) {
|
||||
char note[256];
|
||||
|
||||
snprintf(note, sizeof(note), "splinter gateway started (session %s)",
|
||||
ag->sess != NULL ? ag->sess->id : "?");
|
||||
(void)sp_telegram_notify(conf, note, NULL);
|
||||
}
|
||||
|
||||
while (!tg_stop) {
|
||||
mcp_json *v;
|
||||
@@ -2167,8 +2217,7 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
cq = mcp_arg(update, "callback_query");
|
||||
if (cq != NULL) {
|
||||
(void)tg_callback(token, allow, cq, ag, conf,
|
||||
host, ag != NULL && ag->sess != NULL ?
|
||||
ag->sess->dir : NULL);
|
||||
host, cmddir);
|
||||
continue;
|
||||
}
|
||||
msg = mcp_arg(update, "message");
|
||||
@@ -2205,7 +2254,8 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
if (tg_ack_reply(token, chat_id, text))
|
||||
continue;
|
||||
if (strcmp(text, "/interview") == 0) {
|
||||
tg_interview_begin(token, chat_id, ag, err);
|
||||
tg_interview_begin(token, chat_id, ag,
|
||||
cmddir, err);
|
||||
sp_buf_clear(err);
|
||||
continue;
|
||||
}
|
||||
@@ -2288,8 +2338,7 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
|
||||
if (sp_buf_init(&help) == 0 &&
|
||||
sp_buf_init(&mark) == 0) {
|
||||
tg_help_text(host, ag->sess != NULL ?
|
||||
ag->sess->dir : NULL, &help);
|
||||
tg_help_text(host, cmddir, &help);
|
||||
tg_menu_keyboard(&mark);
|
||||
(void)send_message_markup(token,
|
||||
chat_id, help.p != NULL ?
|
||||
@@ -2332,8 +2381,7 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
struct sp_buf cerr;
|
||||
|
||||
sp_buf_init(&cerr);
|
||||
if (sp_cmdscript_dir(ag->sess != NULL ?
|
||||
ag->sess->dir : NULL, cdir,
|
||||
if (sp_cmdscript_dir(cmddir, cdir,
|
||||
sizeof(cdir)) == 0 &&
|
||||
sp_cmdscript_exists(cdir, name)) {
|
||||
if (sp_slash_script(cdir, name,
|
||||
@@ -2428,8 +2476,7 @@ tg_turn_ok:
|
||||
if (answer.p != NULL && tg_iv_active) {
|
||||
char clean[16384], note[700];
|
||||
|
||||
if (sp_interview_capture(
|
||||
ag->sess != NULL ? ag->sess->dir : NULL,
|
||||
if (sp_interview_capture(cmddir,
|
||||
answer.p, clean, sizeof(clean))) {
|
||||
snprintf(note, sizeof(note), "%s",
|
||||
sp_td("interview.saved",
|
||||
|
||||
+96
-9
@@ -85,6 +85,8 @@ usage(FILE *f)
|
||||
" --delegate ID run that delegate with -p TEXT, print its "
|
||||
"observation, exit\n"
|
||||
" --daemon answer on Telegram until SIGINT\n"
|
||||
" --notify TEXT send TEXT to the Telegram status chat and exit\n"
|
||||
" (\"-\" reads stdin)\n"
|
||||
" --no-stdin ignore data on standard input\n"
|
||||
" -c, --continue continue the last session started in this "
|
||||
"directory\n"
|
||||
@@ -206,6 +208,7 @@ static long g_max_wait;
|
||||
struct opts {
|
||||
const char *prompt, *endpoint, *script, *model, *key_env;
|
||||
const char *sessdir, *sessid, *theme, *transport, *config_path;
|
||||
const char *notify;
|
||||
const char *kind; /* "anthropic" when -m chose such a provider */
|
||||
const char *choose, *mcp_ip;
|
||||
const char *delegate;
|
||||
@@ -335,7 +338,14 @@ parse_args(int argc, char **argv, struct opts *o, struct sp_agent_config *cfg)
|
||||
o->delegate = argv[++i];
|
||||
} else if (!strcmp(a, "--daemon"))
|
||||
o->daemon = 1;
|
||||
else if (!strcmp(a, "--ack") && i + 2 < argc) {
|
||||
else if (!strcmp(a, "--notify")) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "splinter: --notify needs text "
|
||||
"(or - for stdin)\n");
|
||||
return 2;
|
||||
}
|
||||
o->notify = argv[++i];
|
||||
} else if (!strcmp(a, "--ack") && i + 2 < argc) {
|
||||
o->ack_id = atoi(argv[++i]);
|
||||
o->ack_yes = ack_word(argv[++i]);
|
||||
if (o->ack_yes < 0) {
|
||||
@@ -1075,16 +1085,17 @@ mcp_bg_thread(void *arg)
|
||||
|
||||
/* --daemon: answer on Telegram until SIGINT */
|
||||
static int
|
||||
run_daemon(struct sp_engine *eng, struct sp_buf *err)
|
||||
run_daemon(struct sp_engine *eng, const char *base_dir, struct sp_buf *err)
|
||||
{
|
||||
char token[256], tpath[700];
|
||||
struct sp_config *conf = sp_engine_config(eng);
|
||||
struct sp_agent *ag = sp_engine_agent(eng);
|
||||
struct sp_host *host = sp_engine_host(eng);
|
||||
const char *state_dir = base_dir != NULL && *base_dir != '\0' ?
|
||||
base_dir : sp_engine_state_dir(eng);
|
||||
int rc, lockfd;
|
||||
|
||||
snprintf(tpath, sizeof(tpath), "%s/telegram.token",
|
||||
sp_engine_state_dir(eng));
|
||||
snprintf(tpath, sizeof(tpath), "%s/telegram.token", state_dir);
|
||||
if (sp_telegram_token(token, sizeof(token), tpath) != 0) {
|
||||
fprintf(stderr, "splinter: no Telegram token: put it in %s (0600) "
|
||||
"or set SPLINTER_TELEGRAM_TOKEN\n", tpath);
|
||||
@@ -1095,7 +1106,7 @@ run_daemon(struct sp_engine *eng, struct sp_buf *err)
|
||||
"be answered\n");
|
||||
return 2;
|
||||
}
|
||||
lockfd = daemon_lock(sp_engine_state_dir(eng), err);
|
||||
lockfd = daemon_lock(state_dir, err);
|
||||
if (lockfd < 0) {
|
||||
fprintf(stderr, "splinter: %s\n",
|
||||
err->len > 0 ? err->p : "gateway already running");
|
||||
@@ -1103,7 +1114,7 @@ run_daemon(struct sp_engine *eng, struct sp_buf *err)
|
||||
}
|
||||
sp_engine_set_stream(eng, daemon_stream, NULL);
|
||||
rc = sp_telegram_run(ag, host, token, conf->telegram_allow, conf,
|
||||
err);
|
||||
state_dir, err);
|
||||
close(lockfd); /* the flock goes with it */
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "splinter: %s\n",
|
||||
@@ -1227,6 +1238,8 @@ main(int argc, char **argv)
|
||||
struct sp_engine_opts eo;
|
||||
struct sp_engine *eng = NULL;
|
||||
struct sp_buf err;
|
||||
const char *daemon_base = NULL;
|
||||
char tgdir[1024], basebuf[1024];
|
||||
int rc;
|
||||
|
||||
memset(&o, 0, sizeof(o));
|
||||
@@ -1252,7 +1265,8 @@ main(int argc, char **argv)
|
||||
printf("%s\n", names[k]);
|
||||
return 0;
|
||||
}
|
||||
take_stdin(&o);
|
||||
if (o.notify == NULL)
|
||||
take_stdin(&o);
|
||||
if ((rc = load_config(&o, &conf, &cfg, &err)) >= 0)
|
||||
return rc;
|
||||
/* providers from the environment: OLLAMA_KEY(_N), ... */
|
||||
@@ -1266,6 +1280,49 @@ main(int argc, char **argv)
|
||||
hide_secrets(&o, &conf);
|
||||
/* the catalogue is chosen now: a config file may set the locale */
|
||||
(void)sp_i18n_init(NULL);
|
||||
if (o.notify != NULL) {
|
||||
struct sp_buf text = { 0 };
|
||||
const char *msg = o.notify;
|
||||
|
||||
if (strcmp(o.notify, "-") == 0) {
|
||||
int binary;
|
||||
size_t raw;
|
||||
|
||||
if (sp_buf_init(&text) != 0) {
|
||||
sp_buf_free(&err);
|
||||
return 3;
|
||||
}
|
||||
if (read_stdin_block(&text, &binary, &raw) != 0) {
|
||||
fprintf(stderr, "splinter: --notify: cannot read "
|
||||
"stdin\n");
|
||||
sp_buf_free(&text);
|
||||
sp_buf_free(&err);
|
||||
return 2;
|
||||
}
|
||||
if (binary) {
|
||||
fprintf(stderr, "splinter: --notify reads text, "
|
||||
"not binary input\n");
|
||||
sp_buf_free(&text);
|
||||
sp_buf_free(&err);
|
||||
return 2;
|
||||
}
|
||||
if (text.p == NULL || text.len == 0) {
|
||||
fprintf(stderr, "splinter: --notify: empty "
|
||||
"stdin\n");
|
||||
sp_buf_free(&text);
|
||||
sp_buf_free(&err);
|
||||
return 2;
|
||||
}
|
||||
msg = text.p;
|
||||
}
|
||||
rc = sp_telegram_notify(&conf, msg, &err);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "splinter: %s\n",
|
||||
err.len > 0 ? err.p : "notify failed");
|
||||
sp_buf_free(&text);
|
||||
sp_buf_free(&err);
|
||||
return rc == 0 ? 0 : 1;
|
||||
}
|
||||
if (o.model_list)
|
||||
return run_model_list(&conf, &o);
|
||||
if (o.doctor)
|
||||
@@ -1360,6 +1417,36 @@ main(int argc, char **argv)
|
||||
eo.session_id = o.sessid;
|
||||
eo.resume = o.resume;
|
||||
eo.continue_last = o.continue_here;
|
||||
/*
|
||||
* Telegram owns its sessions: the daemon writes below <state>/telegram,
|
||||
* so a CLI -c in the same directory never picks a gateway conversation
|
||||
* (and /new in the chat never creates a file a CLI would resume).
|
||||
*/
|
||||
if (o.daemon) {
|
||||
const char *base = o.sessdir;
|
||||
|
||||
if (base == NULL || *base == '\0') {
|
||||
const char *home = getenv("HOME");
|
||||
|
||||
snprintf(basebuf, sizeof(basebuf), "%s/.splinter",
|
||||
home != NULL ? home : ".");
|
||||
base = basebuf;
|
||||
}
|
||||
size_t blen = strlen(base);
|
||||
|
||||
if (blen + sizeof("/telegram") > sizeof(tgdir)) {
|
||||
fprintf(stderr, "splinter: session path too long\n");
|
||||
sp_buf_free(&err);
|
||||
return 2;
|
||||
}
|
||||
memcpy(tgdir, base, blen);
|
||||
memcpy(tgdir + blen, "/telegram", sizeof("/telegram"));
|
||||
daemon_base = base;
|
||||
eo.session_dir = tgdir;
|
||||
eo.session_id = NULL;
|
||||
eo.resume = 1;
|
||||
eo.continue_last = 0;
|
||||
}
|
||||
/*
|
||||
* The MCP server is tools-only unless a model is given (or the daemon
|
||||
* owns the engine): the session tools need a transport to run a turn.
|
||||
@@ -1445,7 +1532,7 @@ main(int argc, char **argv)
|
||||
rc = 3;
|
||||
goto done;
|
||||
}
|
||||
rc = run_daemon(eng, &err);
|
||||
rc = run_daemon(eng, daemon_base, &err);
|
||||
/*
|
||||
* The MCP thread works on this engine: it must be gone
|
||||
* before sp_engine_close() frees it.
|
||||
@@ -1489,7 +1576,7 @@ main(int argc, char **argv)
|
||||
err.len > 0 ? err.p : "gateway failed");
|
||||
rc = rc == 0 ? 0 : 1;
|
||||
} else if (o.daemon)
|
||||
rc = run_daemon(eng, &err);
|
||||
rc = run_daemon(eng, daemon_base, &err);
|
||||
else if (o.chat) {
|
||||
if (!sp_profile_exists(sp_engine_state_dir(eng)))
|
||||
fprintf(stderr, sp_td("main.no_profile",
|
||||
|
||||
@@ -173,7 +173,7 @@ check "the reference does not stay in the chat" \
|
||||
# /new starts a fresh conversation and a fresh session file
|
||||
run_text "/new" 'Neue Konversation: sess-' 45
|
||||
check "the bot says the new session" grep -q '^Neue Konversation: sess-' "$D/sent.log"
|
||||
check "a session file was opened" sh -c "ls '$D'/home/.splinter/sess-*.jsonl | tail -1 | grep -q sess-"
|
||||
check "a session file was opened" sh -c "ls '$D'/home/.splinter/telegram/sess-*.jsonl | tail -1 | grep -q sess-"
|
||||
|
||||
# /model offers the configured models and switches on the button
|
||||
run_text "/model" 'Provider far' 60 '^openai:'
|
||||
|
||||
Reference in new issue
Block a user