gateway: render the model's markdown as Telegram HTML
A chat that shows **fett**, |---| and <b> lost the answer's shape. tgmd.c turns bold, italic, strike, inline code, fenced blocks, links, headings, bullets and quotes into the tags parse_mode=HTML accepts and escapes everything else, so a '<' stays text and snake_case_name stays a name. Tables keep the shape the terminal chat gives them: aligned columns in <pre>. Telegram's 4096 cap is met by splitting the rendered HTML at tag and entity boundaries, closing open tags at the end of a piece and reopening them at the start of the next. A piece the API refuses to parse is resent stripped, because losing the shape is better than losing the answer. SPLINTER_TG_MARKDOWN=0 keeps the old plain-text path. test_tgmd pins the renderer and the chunker; test_gateway asserts the bot sends parse_mode=HTML with <b>/<code> and a table in <pre>.
This commit is contained in:
1 parent
a00ffef699
commit
e7a5afed3c
13 files changed
+1267
-64
No files matched your search
@@ -23,7 +23,7 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \
|
||||
LIB = libsplinter.so
|
||||
LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \
|
||||
src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \
|
||||
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o src/curl.o src/swarm.o src/provstate.o src/md.o src/b64.o src/a2aserver.o
|
||||
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o src/curl.o src/swarm.o src/provstate.o src/md.o src/tgmd.o src/b64.o src/a2aserver.o
|
||||
BIN = splinter
|
||||
BINOBJ = src/main.o src/ui.o
|
||||
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
@@ -37,7 +37,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/tmux/tmux.so
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
|
||||
tests/test_transport tests/test_agent tests/test_openai \
|
||||
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_planner tests/test_jobs
|
||||
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_planner tests/test_jobs
|
||||
|
||||
RPATH = -Wl,-rpath,'$$ORIGIN'
|
||||
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
|
||||
@@ -183,6 +183,8 @@ tests/test_ascii: tests/test_ascii.c $(LIB) $(BINOBJ)
|
||||
$(TEST_RPATH) $(CURSES_A) $(LIBS)
|
||||
tests/test_md: tests/test_md.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_md.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_tgmd: tests/test_tgmd.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_tgmd.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_b64: tests/test_b64.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_b64.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_pause: tests/test_pause.c $(LIB)
|
||||
|
||||
@@ -304,6 +304,13 @@ 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.
|
||||
|
||||
The model's markdown is rendered as Telegram HTML: bold, italic, strike,
|
||||
inline code, fenced blocks, links and quotes become tags, everything else is
|
||||
escaped. Tables are laid out as columns inside a `<pre>` block. A long answer
|
||||
is split at tag boundaries, with the open tags closed and reopened per
|
||||
message, and a piece Telegram refuses to parse is sent as plain text instead.
|
||||
`SPLINTER_TG_MARKDOWN=0` turns the rendering off.
|
||||
|
||||
`/interview` asks the same six questions as `splinter --interview`, one
|
||||
message per answer; `/skip` skips one and `/cancel` stops. A program in
|
||||
`commands/` answers in the chat: its output is the reply. In the
|
||||
|
||||
@@ -29,6 +29,10 @@ URL-Teile werden nur von erlaubten Hosts geholt.
|
||||
den Turn mit lcm bei 8 KiB und 32 KiB Budget und zählt die Kompressionen
|
||||
- **Tabellen im Chat**: `src/md.c` erkennt Kopf + Trenner, rechnet Spaltenbreiten
|
||||
und zeichnet ausgerichteten Text (`test_md`, `test_ui.sh`)
|
||||
- **Markdown als Telegram-HTML**: `src/tgmd.c` rendert fett/kursiv/Code/Links/
|
||||
Zitate/Tabellen in die Tags von `parse_mode=HTML`, escapt alles andere und
|
||||
teilt lange Antworten an Tag-Grenzen, mit offenen Tags pro Stueck
|
||||
(`test_tgmd`, `test_gateway.sh`)
|
||||
- **Website zählt**: Zeilen, Werkzeuge, Tests, Plugins und Paletten kommen beim
|
||||
Generieren aus dem Baum (`tools/gen_www.py`)
|
||||
- **Letzter Delegate-Fehler**: die letzten 400 Bytes des Kindes stehen im Fehler,
|
||||
|
||||
+194
-46
@@ -39,6 +39,7 @@
|
||||
#include "log.h"
|
||||
#include "cmdscript.h"
|
||||
#include "interview.h"
|
||||
#include "tgmd.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
/*
|
||||
@@ -55,6 +56,8 @@ api_base(void)
|
||||
|
||||
|
||||
#define TG_MAX_CHUNK 3500
|
||||
/* a piece of rendered HTML, tag overhead included; Telegram's cap is 4096 */
|
||||
#define TG_MAX_HTML 3200
|
||||
#define TG_POLL_S 25
|
||||
|
||||
static volatile int tg_stop;
|
||||
@@ -187,12 +190,15 @@ tg_reply_ok(const char *reply)
|
||||
return ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* One sendMessage call. 'parse_mode' is NULL for plain text and "HTML" for a
|
||||
* rendered markdown answer; 'markup' is a ready JSON keyboard, or NULL.
|
||||
*/
|
||||
static int
|
||||
send_message_markup(const char *token, long chat_id, const char *text,
|
||||
const char *markup, struct sp_buf *err)
|
||||
post_message(const char *token, long chat_id, const char *text,
|
||||
const char *markup, const char *parse_mode, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf url, body, reply, sink;
|
||||
size_t off = 0, len = strlen(text);
|
||||
int rc = 0;
|
||||
|
||||
if (sp_buf_init(&url) != 0 || sp_buf_init(&body) != 0 ||
|
||||
@@ -206,8 +212,53 @@ send_message_markup(const char *token, long chat_id, const char *text,
|
||||
if (err == NULL)
|
||||
err = &sink;
|
||||
sp_buf_printf(&url, "%s/bot%s/sendMessage", api_base(), token);
|
||||
if (sp_buf_printf(&body, "{\"chat_id\":%ld,\"text\":\"", chat_id) != 0 ||
|
||||
sp_json_escape(&body, text) != 0 ||
|
||||
sp_buf_puts(&body, "\"") != 0)
|
||||
rc = -1;
|
||||
if (rc == 0 && parse_mode != NULL && *parse_mode != '\0' &&
|
||||
sp_buf_printf(&body, ",\"parse_mode\":\"%s\"", parse_mode) != 0)
|
||||
rc = -1;
|
||||
if (rc == 0 && markup != NULL && *markup != '\0' &&
|
||||
sp_buf_printf(&body, ",\"reply_markup\":%s", markup) != 0)
|
||||
rc = -1;
|
||||
if (rc == 0 && sp_buf_puts(&body, "}") != 0)
|
||||
rc = -1;
|
||||
if (rc == 0 && tg_curl(url.p, body.p, &reply, err) != 0)
|
||||
rc = -1;
|
||||
if (rc == 0 && !tg_reply_ok(reply.p)) {
|
||||
sp_buf_printf(err, "telegram: sendMessage refused: %.200s",
|
||||
reply.p != NULL ? reply.p : "");
|
||||
rc = -1;
|
||||
}
|
||||
sp_buf_free(&url);
|
||||
sp_buf_free(&body);
|
||||
sp_buf_free(&sink);
|
||||
sp_buf_free(&reply);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Markdown on by default; SPLINTER_TG_MARKDOWN=0 sends the text as it came. */
|
||||
static int
|
||||
tg_markdown_enabled(void)
|
||||
{
|
||||
const char *env = getenv("SPLINTER_TG_MARKDOWN");
|
||||
|
||||
if (env == NULL || *env == '\0')
|
||||
return 1;
|
||||
return strcmp(env, "0") != 0 && strcmp(env, "false") != 0 &&
|
||||
strcmp(env, "off") != 0 && strcmp(env, "no") != 0;
|
||||
}
|
||||
|
||||
static int
|
||||
send_message_markup(const char *token, long chat_id, const char *text,
|
||||
const char *markup, struct sp_buf *err)
|
||||
{
|
||||
size_t off = 0, len = strlen(text);
|
||||
|
||||
do {
|
||||
size_t take = len - off;
|
||||
char *piece;
|
||||
|
||||
if (take > TG_MAX_CHUNK) {
|
||||
take = TG_MAX_CHUNK;
|
||||
@@ -222,47 +273,142 @@ send_message_markup(const char *token, long chat_id, const char *text,
|
||||
if (take == 0)
|
||||
take = TG_MAX_CHUNK; /* not UTF-8 at all */
|
||||
}
|
||||
sp_buf_clear(&body);
|
||||
sp_buf_printf(&body, "{\"chat_id\":%ld,\"text\":\"", chat_id);
|
||||
{
|
||||
char *piece = malloc(take + 1);
|
||||
|
||||
if (piece == NULL) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
memcpy(piece, text + off, take);
|
||||
piece[take] = '\0';
|
||||
(void)sp_json_escape(&body, piece);
|
||||
piece = malloc(take + 1);
|
||||
if (piece == NULL)
|
||||
return -1;
|
||||
memcpy(piece, text + off, take);
|
||||
piece[take] = '\0';
|
||||
if (post_message(token, chat_id, piece,
|
||||
off + take >= len ? markup : NULL, NULL, err) != 0) {
|
||||
free(piece);
|
||||
return -1;
|
||||
}
|
||||
sp_buf_puts(&body, "\"");
|
||||
if (markup != NULL && *markup != '\0' &&
|
||||
off + take >= len) {
|
||||
if (sp_buf_printf(&body, ",\"reply_markup\":%s",
|
||||
markup) != 0) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
sp_buf_puts(&body, "}");
|
||||
sp_buf_clear(&reply);
|
||||
if (tg_curl(url.p, body.p, &reply, err) != 0) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
if (!tg_reply_ok(reply.p)) {
|
||||
sp_buf_printf(err, "telegram: sendMessage refused: %.200s",
|
||||
reply.p != NULL ? reply.p : "");
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
free(piece);
|
||||
off += take;
|
||||
} while (off < len);
|
||||
sp_buf_free(&url);
|
||||
sp_buf_free(&body);
|
||||
sp_buf_free(&sink);
|
||||
sp_buf_free(&reply);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The HTML Telegram refused (a broken entity, a tag it does not know): send
|
||||
* the same piece as text. Losing the shape of an answer is bad; losing the
|
||||
* answer is worse.
|
||||
*/
|
||||
static int
|
||||
send_html_stripped(const char *token, long chat_id, const char *html,
|
||||
const char *markup, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf plain, text;
|
||||
size_t i, start = 0;
|
||||
int rc;
|
||||
|
||||
if (sp_buf_init(&plain) != 0 || sp_buf_init(&text) != 0) {
|
||||
sp_buf_free(&plain);
|
||||
sp_buf_free(&text);
|
||||
return -1;
|
||||
}
|
||||
for (i = 0; html[i] != '\0'; i++) {
|
||||
if (html[i] == '<') {
|
||||
const char *gt = strchr(html + i, '>');
|
||||
|
||||
if (gt != NULL) {
|
||||
if (i > start &&
|
||||
sp_buf_add(&plain, html + start, i - start) != 0)
|
||||
goto fail;
|
||||
i = (size_t)(gt - html);
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sp_buf_add(&plain, html + start, i - start) != 0)
|
||||
goto fail;
|
||||
/* unescape what the renderer escaped */
|
||||
for (i = 0; plain.p != NULL && plain.p[i] != '\0'; i++) {
|
||||
const char *rep = NULL;
|
||||
size_t skip = 1;
|
||||
|
||||
if (strncmp(plain.p + i, "&", 5) == 0) {
|
||||
rep = "&";
|
||||
skip = 5;
|
||||
} else if (strncmp(plain.p + i, "<", 4) == 0) {
|
||||
rep = "<";
|
||||
skip = 4;
|
||||
} else if (strncmp(plain.p + i, ">", 4) == 0) {
|
||||
rep = ">";
|
||||
skip = 4;
|
||||
} else if (strncmp(plain.p + i, """, 6) == 0) {
|
||||
rep = "\"";
|
||||
skip = 6;
|
||||
}
|
||||
if (rep != NULL) {
|
||||
if (sp_buf_puts(&text, rep) != 0)
|
||||
goto fail;
|
||||
i += skip - 1;
|
||||
continue;
|
||||
}
|
||||
if (sp_buf_add(&text, plain.p + i, 1) != 0)
|
||||
goto fail;
|
||||
}
|
||||
rc = post_message(token, chat_id,
|
||||
text.p != NULL ? text.p : "", markup, NULL, err);
|
||||
sp_buf_free(&plain);
|
||||
sp_buf_free(&text);
|
||||
return rc;
|
||||
fail:
|
||||
sp_buf_free(&plain);
|
||||
sp_buf_free(&text);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* The model writes markdown; Telegram draws HTML. Render it, then hand it over
|
||||
* in pieces that carry their own open and closed tags, so a long answer is not
|
||||
* one refused message and a chunk boundary is never inside "<code>".
|
||||
*/
|
||||
static int
|
||||
send_markdown(const char *token, long chat_id, const char *text,
|
||||
const char *markup, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf html, piece;
|
||||
struct sp_tgmd_cursor cur;
|
||||
int rc = 0, n;
|
||||
|
||||
if (text == NULL || *text == '\0' || !tg_markdown_enabled())
|
||||
return send_message_markup(token, chat_id, text, markup, err);
|
||||
if (sp_buf_init(&html) != 0 || sp_buf_init(&piece) != 0) {
|
||||
sp_buf_free(&html);
|
||||
sp_buf_free(&piece);
|
||||
return send_message_markup(token, chat_id, text, markup, err);
|
||||
}
|
||||
if (sp_tgmd_render(text, &html) != 0 || html.len == 0) {
|
||||
sp_buf_free(&html);
|
||||
sp_buf_free(&piece);
|
||||
return send_message_markup(token, chat_id, text, markup, err);
|
||||
}
|
||||
sp_tgmd_cursor_init(&cur, html.p);
|
||||
while ((n = sp_tgmd_next(&cur, TG_MAX_HTML, &piece)) > 0) {
|
||||
/*
|
||||
* Only the last piece carries the keyboard: two "reply" buttons
|
||||
* would be two chances to answer the same request.
|
||||
*/
|
||||
const char *mk = cur.pos >= cur.len && cur.nopen == 0 ?
|
||||
markup : NULL;
|
||||
|
||||
if (post_message(token, chat_id, piece.p, mk, "HTML", err) != 0) {
|
||||
sp_buf_clear(err);
|
||||
if (send_html_stripped(token, chat_id, piece.p, mk,
|
||||
err) != 0) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
sp_buf_clear(err);
|
||||
}
|
||||
}
|
||||
if (n < 0)
|
||||
rc = -1;
|
||||
sp_tgmd_cursor_free(&cur);
|
||||
sp_buf_free(&html);
|
||||
sp_buf_free(&piece);
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -972,9 +1118,9 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
sp_host_command(host, name, rest,
|
||||
&answer) : -1;
|
||||
if (crc == 0) {
|
||||
(void)send_message(token, chat_id,
|
||||
(void)send_markdown(token, chat_id,
|
||||
answer.p != NULL ? answer.p :
|
||||
sp_t("reply.empty"), err);
|
||||
sp_t("reply.empty"), NULL, err);
|
||||
sp_buf_clear(err);
|
||||
continue;
|
||||
}
|
||||
@@ -995,10 +1141,11 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
sp_cmdscript_exists(cdir, name)) {
|
||||
if (sp_slash_script(cdir, name,
|
||||
rest, &answer, &cerr) == 0) {
|
||||
(void)send_message(token,
|
||||
(void)send_markdown(token,
|
||||
chat_id,
|
||||
answer.p != NULL ?
|
||||
answer.p : sp_t("reply.empty"), err);
|
||||
answer.p : sp_t("reply.empty"),
|
||||
NULL, err);
|
||||
} else {
|
||||
(void)send_message(token,
|
||||
chat_id,
|
||||
@@ -1062,8 +1209,9 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
tstop = 1;
|
||||
(void)pthread_join(tty, NULL);
|
||||
}
|
||||
(void)send_message(token, chat_id,
|
||||
answer.p != NULL ? answer.p : sp_t("reply.empty"), err);
|
||||
(void)send_markdown(token, chat_id,
|
||||
answer.p != NULL ? answer.p : sp_t("reply.empty"),
|
||||
NULL, err);
|
||||
sp_buf_clear(err);
|
||||
/* the agent already recorded both sides */
|
||||
}
|
||||
|
||||
+768
@@ -0,0 +1,768 @@
|
||||
/*
|
||||
* splinter -- the model's markdown as the HTML Telegram parses.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* A chat that shows "**fett**" and "|---|---|" is a chat that lost the answer's
|
||||
* shape: the model writes markdown, and Telegram speaks a small HTML instead.
|
||||
* So this is a renderer, not a parser: bold, italic, strike, inline code,
|
||||
* fenced blocks, links, headings, bullets and quotes become the handful of
|
||||
* tags parse_mode=HTML accepts, and everything else is escaped. Anything that
|
||||
* is not clearly a marker stays text -- a lone '_' in snake_case_name, a '<' in
|
||||
* "a < b", a '|' in prose.
|
||||
*
|
||||
* Tables are not HTML (Telegram has no table tag), so they keep the shape the
|
||||
* chat gives them: columns in <pre>.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "md.h"
|
||||
#include "tgmd.h"
|
||||
|
||||
#define TGMD_MAX_DEPTH 4
|
||||
#define TGMD_MAX_OPEN 16
|
||||
|
||||
static const char *
|
||||
eol(const char *s)
|
||||
{
|
||||
const char *p = strchr(s, '\n');
|
||||
|
||||
return p != NULL ? p : s + strlen(s);
|
||||
}
|
||||
|
||||
/* Append 's' as HTML text: only & < > need it, and only those. */
|
||||
static int
|
||||
esc(struct sp_buf *out, const char *s, size_t n)
|
||||
{
|
||||
size_t i, start = 0;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
const char *rep = NULL;
|
||||
|
||||
if (s[i] == '&')
|
||||
rep = "&";
|
||||
else if (s[i] == '<')
|
||||
rep = "<";
|
||||
else if (s[i] == '>')
|
||||
rep = ">";
|
||||
if (rep == NULL)
|
||||
continue;
|
||||
if (i > start && sp_buf_add(out, s + start, i - start) != 0)
|
||||
return -1;
|
||||
if (sp_buf_puts(out, rep) != 0)
|
||||
return -1;
|
||||
start = i + 1;
|
||||
}
|
||||
if (n > start && sp_buf_add(out, s + start, n - start) != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* First occurrence of 'c' in [from, n), skipping backslash escapes. */
|
||||
static size_t
|
||||
find_char(const char *s, size_t from, size_t n, char c)
|
||||
{
|
||||
size_t i = from;
|
||||
|
||||
while (i < n) {
|
||||
if (s[i] == '\\' && i + 1 < n) {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (s[i] == c)
|
||||
return i;
|
||||
i++;
|
||||
}
|
||||
return (size_t)-1;
|
||||
}
|
||||
|
||||
/*
|
||||
* The closing marker for an opening one at 'i'.
|
||||
*
|
||||
* A run of stars is the interesting case: in "**fett mit *kursiv***" the run at
|
||||
* the end closes both, so a two-star closer takes the last two of a run of
|
||||
* three. A single-star closer never takes a run of two, or every "**bold**"
|
||||
* would end the italic of an outer span at its first star.
|
||||
*/
|
||||
static int
|
||||
find_close(const char *s, size_t from, size_t n, char c, size_t mlen,
|
||||
size_t *at)
|
||||
{
|
||||
size_t i = from;
|
||||
|
||||
while (i < n) {
|
||||
size_t run = 0;
|
||||
|
||||
if (s[i] == '\\' && i + 1 < n) {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (s[i] != c) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
while (i + run < n && s[i + run] == c)
|
||||
run++;
|
||||
if (mlen == 2) {
|
||||
if (run >= 3) {
|
||||
*at = i + run - 2;
|
||||
return 1;
|
||||
}
|
||||
if (run == 2) {
|
||||
*at = i;
|
||||
return 1;
|
||||
}
|
||||
} else if (run == 1) {
|
||||
*at = i;
|
||||
return 1;
|
||||
}
|
||||
i += run;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Is the emphasis marker at 'i' a real one?
|
||||
*
|
||||
* '*' and '~~' only need a non-space after them; '_' additionally must not be
|
||||
* intraword, or every snake_case_name would turn half its letters italic.
|
||||
*/
|
||||
static int
|
||||
emphasis_at(const char *s, size_t i, size_t n, const char *mark, size_t mlen,
|
||||
int underscore, size_t *close)
|
||||
{
|
||||
size_t j;
|
||||
|
||||
if (i + mlen >= n)
|
||||
return 0;
|
||||
if (s[i + mlen] == ' ' || s[i + mlen] == '\t')
|
||||
return 0;
|
||||
if (underscore && i > 0 && isalnum((unsigned char)s[i - 1]) &&
|
||||
isalnum((unsigned char)s[i + mlen]))
|
||||
return 0;
|
||||
if (!find_close(s, i + mlen, n, mark[0], mlen, &j))
|
||||
return 0;
|
||||
if (j == i + mlen || j + mlen > n)
|
||||
return 0;
|
||||
if (s[j - 1] == ' ' || s[j - 1] == '\t')
|
||||
return 0;
|
||||
if (underscore && j + mlen < n &&
|
||||
isalnum((unsigned char)s[j - 1]) &&
|
||||
isalnum((unsigned char)s[j + mlen]))
|
||||
return 0;
|
||||
*close = j;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
url_ok(const char *u, size_t n)
|
||||
{
|
||||
return (n > 7 && strncasecmp(u, "http://", 7) == 0) ||
|
||||
(n > 8 && strncasecmp(u, "https://", 8) == 0) ||
|
||||
(n > 7 && strncasecmp(u, "mailto:", 7) == 0) ||
|
||||
(n > 5 && strncasecmp(u, "tg://", 5) == 0);
|
||||
}
|
||||
|
||||
static int
|
||||
inline_md(const char *s, size_t n, struct sp_buf *out, int depth);
|
||||
|
||||
/* <open>inner</close>, if the markers at 'i' are a pair. */
|
||||
static int
|
||||
emit_emphasis(const char *s, size_t i, size_t *pos, size_t *start, size_t n,
|
||||
const char *mark, size_t mlen, int underscore, const char *open,
|
||||
const char *close, struct sp_buf *out, int depth)
|
||||
{
|
||||
size_t end;
|
||||
|
||||
if (!emphasis_at(s, i, n, mark, mlen, underscore, &end))
|
||||
return 0;
|
||||
if (esc(out, s + *start, i - *start) != 0 ||
|
||||
sp_buf_puts(out, open) != 0 ||
|
||||
inline_md(s + i + mlen, end - i - mlen, out, depth + 1) != 0 ||
|
||||
sp_buf_puts(out, close) != 0)
|
||||
return -1;
|
||||
*pos = end + mlen;
|
||||
*start = *pos;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
inline_md(const char *s, size_t n, struct sp_buf *out, int depth)
|
||||
{
|
||||
size_t i = 0, start = 0;
|
||||
|
||||
if (depth > TGMD_MAX_DEPTH)
|
||||
return esc(out, s, n);
|
||||
while (i < n) {
|
||||
int r;
|
||||
|
||||
/* \* is a literal star, not a marker */
|
||||
if (s[i] == '\\' && i + 1 < n &&
|
||||
ispunct((unsigned char)s[i + 1])) {
|
||||
if (esc(out, s + start, i - start) != 0 ||
|
||||
esc(out, s + i + 1, 1) != 0)
|
||||
return -1;
|
||||
i += 2;
|
||||
start = i;
|
||||
continue;
|
||||
}
|
||||
if (s[i] == '`') {
|
||||
size_t k = find_char(s, i + 1, n, '`');
|
||||
|
||||
if (k != (size_t)-1) {
|
||||
if (esc(out, s + start, i - start) != 0 ||
|
||||
sp_buf_puts(out, "<code>") != 0 ||
|
||||
esc(out, s + i + 1, k - i - 1) != 0 ||
|
||||
sp_buf_puts(out, "</code>") != 0)
|
||||
return -1;
|
||||
i = k + 1;
|
||||
start = i;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (s[i] == '[') {
|
||||
size_t k = find_char(s, i + 1, n, ']');
|
||||
|
||||
if (k != (size_t)-1 && k + 1 < n && s[k + 1] == '(') {
|
||||
size_t u = find_char(s, k + 2, n, ')');
|
||||
|
||||
if (u != (size_t)-1 &&
|
||||
url_ok(s + k + 2, u - k - 2)) {
|
||||
if (esc(out, s + start, i - start) != 0 ||
|
||||
sp_buf_puts(out, "<a href=\"") != 0 ||
|
||||
esc(out, s + k + 2, u - k - 2) != 0 ||
|
||||
sp_buf_puts(out, "\">") != 0 ||
|
||||
inline_md(s + i + 1, k - i - 1, out,
|
||||
depth + 1) != 0 ||
|
||||
sp_buf_puts(out, "</a>") != 0)
|
||||
return -1;
|
||||
i = u + 1;
|
||||
start = i;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s[i] == '*' || s[i] == '_' || s[i] == '~') {
|
||||
size_t mlen = (i + 1 < n && s[i + 1] == s[i]) ? 2 : 1;
|
||||
|
||||
if (mlen == 2 && s[i] == '~') {
|
||||
char mark[3] = "~~";
|
||||
|
||||
r = emit_emphasis(s, i, &i, &start, n, mark, 2, 0,
|
||||
"<s>", "</s>", out, depth);
|
||||
} else if (mlen == 2) {
|
||||
char mark[3];
|
||||
|
||||
mark[0] = s[i];
|
||||
mark[1] = s[i];
|
||||
mark[2] = '\0';
|
||||
r = emit_emphasis(s, i, &i, &start, n, mark, 2, 0,
|
||||
"<b>", "</b>", out, depth);
|
||||
} else {
|
||||
char mark[2];
|
||||
|
||||
mark[0] = s[i];
|
||||
mark[1] = '\0';
|
||||
r = emit_emphasis(s, i, &i, &start, n, mark, 1,
|
||||
s[i] == '_' ? 1 : 0, "<i>", "</i>", out,
|
||||
depth);
|
||||
}
|
||||
if (r < 0)
|
||||
return -1;
|
||||
if (r > 0)
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return esc(out, s + start, n - start);
|
||||
}
|
||||
|
||||
/* "<pre><code class=...>" when the fence names a language, plain <code> else */
|
||||
static int
|
||||
lang_ok(const char *s, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (n == 0 || n > 32)
|
||||
return 0;
|
||||
for (i = 0; i < n; i++)
|
||||
if (!isalnum((unsigned char)s[i]) && s[i] != '+' &&
|
||||
s[i] != '-' && s[i] != '_')
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* A line that opens or closes a fenced block: ``` or more, optional language. */
|
||||
static int
|
||||
fence_open(const char *s, size_t n, size_t *ticks, const char **lang,
|
||||
size_t *langlen)
|
||||
{
|
||||
size_t i = 0, t = 0;
|
||||
|
||||
while (i < n && (s[i] == ' ' || s[i] == '\t') && i < 4)
|
||||
i++;
|
||||
while (i + t < n && s[i + t] == '`')
|
||||
t++;
|
||||
if (t < 3)
|
||||
return 0;
|
||||
*lang = s + i + t;
|
||||
*langlen = n - i - t;
|
||||
while (*langlen > 0 && ((*lang)[0] == ' ' || (*lang)[0] == '\t')) {
|
||||
(*lang)++;
|
||||
(*langlen)--;
|
||||
}
|
||||
while (*langlen > 0 && ((*lang)[*langlen - 1] == ' ' ||
|
||||
(*lang)[*langlen - 1] == '\t'))
|
||||
(*langlen)--;
|
||||
*ticks = t;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
heading(const char *s, size_t n, size_t *skip)
|
||||
{
|
||||
size_t i = 0, h = 0;
|
||||
|
||||
while (i < n && s[i] == ' ' && i < 3)
|
||||
i++;
|
||||
while (i + h < n && s[i + h] == '#')
|
||||
h++;
|
||||
if (h == 0 || h > 6 || i + h >= n || s[i + h] != ' ')
|
||||
return 0;
|
||||
*skip = i + h;
|
||||
while (*skip < n && s[*skip] == ' ')
|
||||
(*skip)++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
is_hr(const char *s, size_t n)
|
||||
{
|
||||
size_t i = 0, count = 0;
|
||||
char c;
|
||||
|
||||
while (i < n && (s[i] == ' ' || s[i] == '\t'))
|
||||
i++;
|
||||
if (i >= n || (s[i] != '-' && s[i] != '*' && s[i] != '_'))
|
||||
return 0;
|
||||
c = s[i];
|
||||
for (; i < n; i++) {
|
||||
if (s[i] == c)
|
||||
count++;
|
||||
else if (s[i] != ' ' && s[i] != '\t')
|
||||
return 0;
|
||||
}
|
||||
return count >= 3;
|
||||
}
|
||||
|
||||
static int
|
||||
bullet(const char *s, size_t n, size_t *indent, size_t *content)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
while (i < n && s[i] == ' ' && i < 3)
|
||||
i++;
|
||||
if (i + 1 < n && (s[i] == '-' || s[i] == '*' || s[i] == '+') &&
|
||||
s[i + 1] == ' ') {
|
||||
*indent = i;
|
||||
*content = i + 2;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
quote(const char *s, size_t n, size_t *skip)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
while (i < n && s[i] == ' ' && i < 3)
|
||||
i++;
|
||||
if (i < n && s[i] == '>') {
|
||||
i++;
|
||||
if (i < n && s[i] == ' ')
|
||||
i++;
|
||||
*skip = i;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
render_line(const char *p, size_t len, struct sp_buf *out)
|
||||
{
|
||||
size_t skip, indent, content;
|
||||
|
||||
if (heading(p, len, &skip)) {
|
||||
if (sp_buf_puts(out, "<b>") != 0 ||
|
||||
inline_md(p + skip, len - skip, out, 1) != 0 ||
|
||||
sp_buf_puts(out, "</b>") != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
if (is_hr(p, len))
|
||||
return sp_buf_puts(out, "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80");
|
||||
if (bullet(p, len, &indent, &content)) {
|
||||
if (esc(out, p, indent) != 0 ||
|
||||
sp_buf_puts(out, "\xe2\x80\xa2 ") != 0)
|
||||
return -1;
|
||||
return inline_md(p + content, len - content, out, 1);
|
||||
}
|
||||
return inline_md(p, len, out, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* The first line after the table that starts at 'p'. Same shape the table
|
||||
* formatter uses: a header, a separator, then rows that still have a pipe.
|
||||
*/
|
||||
static const char *
|
||||
table_end(const char *p)
|
||||
{
|
||||
const char *e = eol(p);
|
||||
const char *cur;
|
||||
|
||||
if (*e != '\n')
|
||||
return e;
|
||||
cur = e + 1;
|
||||
e = eol(cur);
|
||||
if (*e != '\n')
|
||||
return e;
|
||||
cur = e + 1;
|
||||
while (*cur != '\0') {
|
||||
const char *ce = eol(cur);
|
||||
size_t clen = (size_t)(ce - cur);
|
||||
|
||||
if (clen == 0 || memchr(cur, '|', clen) == NULL)
|
||||
break;
|
||||
if (*ce == '\0') {
|
||||
cur = ce;
|
||||
break;
|
||||
}
|
||||
cur = ce + 1;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
int
|
||||
sp_tgmd_render(const char *md, struct sp_buf *out)
|
||||
{
|
||||
const char *p = md != NULL ? md : "";
|
||||
int in_fence = 0, in_quote = 0;
|
||||
size_t fence_ticks = 0, fence_base = 0;
|
||||
|
||||
if (out == NULL)
|
||||
return -1;
|
||||
while (*p != '\0') {
|
||||
const char *e = eol(p);
|
||||
size_t len = (size_t)(e - p);
|
||||
const char *next = *e == '\n' ? e + 1 : e;
|
||||
int had_nl = *e == '\n';
|
||||
size_t ticks, langlen, skip;
|
||||
const char *lang;
|
||||
|
||||
if (in_fence) {
|
||||
if (fence_open(p, len, &ticks, &lang, &langlen) &&
|
||||
langlen == 0 && ticks >= fence_ticks) {
|
||||
in_fence = 0;
|
||||
if (out->len > fence_base &&
|
||||
out->p[out->len - 1] == '\n')
|
||||
out->p[--out->len] = '\0';
|
||||
if (sp_buf_puts(out, "</code></pre>") != 0)
|
||||
return -1;
|
||||
} else if (esc(out, p, len) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (had_nl && sp_buf_puts(out, "\n") != 0)
|
||||
return -1;
|
||||
p = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* a table: columns in monospace, not a row of pipes */
|
||||
if (sp_md_table_starts(p)) {
|
||||
const char *end = table_end(p);
|
||||
struct sp_buf src, tbl;
|
||||
int ok, rc = 0;
|
||||
|
||||
if (sp_buf_init(&src) != 0)
|
||||
return -1;
|
||||
if (sp_buf_init(&tbl) != 0) {
|
||||
sp_buf_free(&src);
|
||||
return -1;
|
||||
}
|
||||
ok = sp_buf_add(&src, p, (size_t)(end - p)) == 0 &&
|
||||
sp_md_table_format(src.p, &tbl) == 1;
|
||||
sp_buf_free(&src);
|
||||
if (ok) {
|
||||
if (in_quote) {
|
||||
in_quote = 0;
|
||||
if (sp_buf_puts(out, "</blockquote>\n")
|
||||
!= 0)
|
||||
rc = -1;
|
||||
}
|
||||
if (rc == 0 &&
|
||||
(sp_buf_puts(out, "<pre>") != 0 ||
|
||||
esc(out, tbl.p, tbl.len) != 0 ||
|
||||
sp_buf_puts(out, "</pre>\n") != 0))
|
||||
rc = -1;
|
||||
sp_buf_free(&tbl);
|
||||
if (rc != 0)
|
||||
return -1;
|
||||
p = end;
|
||||
continue;
|
||||
}
|
||||
sp_buf_free(&tbl);
|
||||
}
|
||||
|
||||
if (fence_open(p, len, &ticks, &lang, &langlen)) {
|
||||
if (in_quote) {
|
||||
in_quote = 0;
|
||||
if (sp_buf_puts(out, "</blockquote>\n") != 0)
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_puts(out, "<pre><code") != 0)
|
||||
return -1;
|
||||
if (lang_ok(lang, langlen)) {
|
||||
if (sp_buf_puts(out, " class=\"language-") != 0 ||
|
||||
sp_buf_add(out, lang, langlen) != 0 ||
|
||||
sp_buf_puts(out, "\"") != 0)
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_puts(out, ">") != 0)
|
||||
return -1;
|
||||
in_fence = 1;
|
||||
fence_ticks = ticks;
|
||||
fence_base = out->len;
|
||||
/* the fence line is markup, not the first line of code */
|
||||
p = next;
|
||||
continue;
|
||||
} else if (quote(p, len, &skip)) {
|
||||
if (!in_quote) {
|
||||
in_quote = 1;
|
||||
if (sp_buf_puts(out, "<blockquote>") != 0)
|
||||
return -1;
|
||||
}
|
||||
if (inline_md(p + skip, len - skip, out, 1) != 0)
|
||||
return -1;
|
||||
} else {
|
||||
if (in_quote) {
|
||||
in_quote = 0;
|
||||
if (sp_buf_puts(out, "</blockquote>\n") != 0)
|
||||
return -1;
|
||||
}
|
||||
if (render_line(p, len, out) != 0)
|
||||
return -1;
|
||||
}
|
||||
if (had_nl && sp_buf_puts(out, "\n") != 0)
|
||||
return -1;
|
||||
p = next;
|
||||
}
|
||||
if (in_fence) {
|
||||
if (out->len > fence_base && out->p[out->len - 1] == '\n')
|
||||
out->p[--out->len] = '\0';
|
||||
if (sp_buf_puts(out, "</code></pre>\n") != 0)
|
||||
return -1;
|
||||
}
|
||||
if (in_quote && sp_buf_puts(out, "</blockquote>\n") != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- chunking */
|
||||
|
||||
int
|
||||
sp_tgmd_cursor_init(struct sp_tgmd_cursor *c, const char *html)
|
||||
{
|
||||
if (c == NULL)
|
||||
return -1;
|
||||
memset(c, 0, sizeof(*c));
|
||||
c->html = html != NULL ? html : "";
|
||||
c->len = strlen(c->html);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
sp_tgmd_cursor_free(struct sp_tgmd_cursor *c)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (c == NULL)
|
||||
return;
|
||||
for (i = 0; i < c->nopen; i++)
|
||||
free(c->open[i].lit);
|
||||
c->nopen = 0;
|
||||
}
|
||||
|
||||
/* Is there a tag at 'pos'? Returns its name and the index just past '>'. */
|
||||
static int
|
||||
tag_at(const char *s, size_t len, size_t pos, size_t *end, char *name,
|
||||
size_t cap, int *closing)
|
||||
{
|
||||
size_t i = pos + 1, n = 0;
|
||||
|
||||
if (pos >= len || s[pos] != '<')
|
||||
return 0;
|
||||
*closing = 0;
|
||||
if (i < len && s[i] == '/') {
|
||||
*closing = 1;
|
||||
i++;
|
||||
}
|
||||
if (i >= len || !isalpha((unsigned char)s[i]))
|
||||
return 0;
|
||||
while (i < len && (isalnum((unsigned char)s[i]) || s[i] == '-')) {
|
||||
if (n + 1 < cap)
|
||||
name[n++] = (char)tolower((unsigned char)s[i]);
|
||||
i++;
|
||||
}
|
||||
name[n] = '\0';
|
||||
while (i < len && s[i] != '>')
|
||||
i++;
|
||||
if (i >= len)
|
||||
return 0;
|
||||
*end = i + 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
push_open(struct sp_tgmd_cursor *c, const char *lit, size_t litlen,
|
||||
const char *name)
|
||||
{
|
||||
struct sp_tgmd_open *o;
|
||||
char *copy;
|
||||
|
||||
if (c->nopen >= TGMD_MAX_OPEN)
|
||||
return 0;
|
||||
copy = malloc(litlen + 1);
|
||||
if (copy == NULL)
|
||||
return -1;
|
||||
memcpy(copy, lit, litlen);
|
||||
copy[litlen] = '\0';
|
||||
o = &c->open[c->nopen++];
|
||||
snprintf(o->name, sizeof(o->name), "%s", name);
|
||||
o->lit = copy;
|
||||
o->litlen = litlen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
pop_open(struct sp_tgmd_cursor *c, const char *name)
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
if (c->nopen == 0)
|
||||
return;
|
||||
if (strcmp(c->open[c->nopen - 1].name, name) == 0) {
|
||||
free(c->open[--c->nopen].lit);
|
||||
return;
|
||||
}
|
||||
/* the renderer nests, so this is only a guard against odd input */
|
||||
for (i = c->nopen; i-- > 0; ) {
|
||||
if (strcmp(c->open[i].name, name) != 0)
|
||||
continue;
|
||||
for (j = i; j < c->nopen; j++)
|
||||
free(c->open[j].lit);
|
||||
c->nopen = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Never cut a UTF-8 character or an entity in half: both become mojibake. */
|
||||
static size_t
|
||||
safe_cut(const char *s, size_t start, size_t cut)
|
||||
{
|
||||
size_t j;
|
||||
|
||||
while (cut > start && ((unsigned char)s[cut] & 0xc0) == 0x80)
|
||||
cut--;
|
||||
for (j = cut; j > start && cut - j < 10; j--) {
|
||||
if (s[j - 1] == '&') {
|
||||
cut = j - 1;
|
||||
break;
|
||||
}
|
||||
if (s[j - 1] == ';' || s[j - 1] == ' ' || s[j - 1] == '\n')
|
||||
break;
|
||||
}
|
||||
return cut;
|
||||
}
|
||||
|
||||
int
|
||||
sp_tgmd_next(struct sp_tgmd_cursor *c, size_t limit, struct sp_buf *out)
|
||||
{
|
||||
size_t i, prefix;
|
||||
|
||||
if (c == NULL || out == NULL || limit < 256)
|
||||
return -1;
|
||||
if (c->pos >= c->len && c->nopen == 0)
|
||||
return 0;
|
||||
sp_buf_clear(out);
|
||||
for (i = 0; i < c->nopen; i++)
|
||||
if (sp_buf_add(out, c->open[i].lit, c->open[i].litlen) != 0)
|
||||
return -1;
|
||||
prefix = out->len;
|
||||
while (c->pos < c->len) {
|
||||
size_t end, run;
|
||||
char name[16];
|
||||
int closing;
|
||||
|
||||
if (tag_at(c->html, c->len, c->pos, &end, name, sizeof(name),
|
||||
&closing)) {
|
||||
size_t len = end - c->pos;
|
||||
|
||||
if (out->len + len > limit && out->len > prefix)
|
||||
break;
|
||||
if (closing)
|
||||
pop_open(c, name);
|
||||
else if (push_open(c, c->html + c->pos, len, name) != 0)
|
||||
return -1;
|
||||
if (sp_buf_add(out, c->html + c->pos, len) != 0)
|
||||
return -1;
|
||||
c->pos = end;
|
||||
continue;
|
||||
}
|
||||
run = 0;
|
||||
while (c->pos + run < c->len && c->html[c->pos + run] != '<')
|
||||
run++;
|
||||
if (run == 0) {
|
||||
/* a '<' the renderer escaped never looks like this */
|
||||
if (sp_buf_add(out, c->html + c->pos, 1) != 0)
|
||||
return -1;
|
||||
c->pos++;
|
||||
continue;
|
||||
}
|
||||
if (out->len >= limit)
|
||||
break;
|
||||
{
|
||||
size_t avail = limit - out->len;
|
||||
size_t take = run < avail ? run : avail;
|
||||
size_t stop = safe_cut(c->html, c->pos, c->pos + take);
|
||||
|
||||
take = stop - c->pos;
|
||||
if (take > 0 && sp_buf_add(out, c->html + c->pos,
|
||||
take) != 0)
|
||||
return -1;
|
||||
c->pos += take;
|
||||
if (take < run)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (c->pos >= c->len) {
|
||||
for (i = c->nopen; i-- > 0; )
|
||||
if (sp_buf_printf(out, "</%s>", c->open[i].name) != 0)
|
||||
return -1;
|
||||
for (i = 0; i < c->nopen; i++)
|
||||
free(c->open[i].lit);
|
||||
c->nopen = 0;
|
||||
return 1;
|
||||
}
|
||||
for (i = c->nopen; i-- > 0; )
|
||||
if (sp_buf_printf(out, "</%s>", c->open[i].name) != 0)
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/* splinter -- the model's markdown as the HTML Telegram parses. */
|
||||
#ifndef SPLINTER_TGMD_H
|
||||
#define SPLINTER_TGMD_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct sp_buf;
|
||||
|
||||
/*
|
||||
* Render markdown into the small subset of HTML that parse_mode=HTML accepts:
|
||||
* <b>, <i>, <s>, <code>, <pre>, <a href> and <blockquote>. Everything else is
|
||||
* escaped, so a '<' in an answer stays a '<' and never becomes a tag. Tables
|
||||
* are laid out as columns inside <pre>, the same way the chat draws them.
|
||||
*
|
||||
* Returns 0, or -1 when memory runs out.
|
||||
*/
|
||||
int sp_tgmd_render(const char *md, struct sp_buf *out);
|
||||
|
||||
/*
|
||||
* Telegram refuses a message over 4096 characters, and a chunk cut in the
|
||||
* middle of "<code>" or "&" is either invalid or shows as text. So the
|
||||
* rendered HTML is handed out in pieces: a piece never ends inside a tag or an
|
||||
* entity, and tags still open at the cut are closed at the end of the piece
|
||||
* and reopened at the start of the next one. Every piece parses on its own.
|
||||
*/
|
||||
struct sp_tgmd_cursor {
|
||||
const char *html;
|
||||
size_t pos;
|
||||
size_t len;
|
||||
struct sp_tgmd_open {
|
||||
char name[12];
|
||||
char *lit; /* the opening tag, as written */
|
||||
size_t litlen;
|
||||
} open[16];
|
||||
size_t nopen;
|
||||
};
|
||||
|
||||
int sp_tgmd_cursor_init(struct sp_tgmd_cursor *, const char *html);
|
||||
void sp_tgmd_cursor_free(struct sp_tgmd_cursor *);
|
||||
/*
|
||||
* Append the next piece to 'out' and return 1, or 0 when the text is done.
|
||||
* 'limit' is the byte budget for the piece, tag overhead included.
|
||||
*/
|
||||
int sp_tgmd_next(struct sp_tgmd_cursor *, size_t limit, struct sp_buf *out);
|
||||
|
||||
#endif /* SPLINTER_TGMD_H */
|
||||
@@ -53,8 +53,8 @@ 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>25639</b> lines of C99/POSIX ·
|
||||
<b>25</b> tools · <b>33</b> test programs ·
|
||||
<p class="meta"><b>26601</b> lines of C99/POSIX ·
|
||||
<b>25</b> tools · <b>34</b> test programs ·
|
||||
<b>16</b> plugins · <b>42</b> palettes · BSD-3-Clause</p>
|
||||
|
||||
<h2>What it is</h2>
|
||||
@@ -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 "typing" every four seconds.</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. <code>/interview</code> asks the questions in the chat. Programs in <code>commands/</code> answer in the chat; in the TUI their output goes to the model.</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. <code>/interview</code> asks the questions in the chat. Answers are rendered as Telegram HTML: bold, italic, code and links as tags, tables as columns in a monospace block; a piece Telegram cannot parse goes out as text instead of being lost. Programs in <code>commands/</code> answer in the chat; in the TUI their output goes to the model.</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>
|
||||
@@ -152,7 +152,7 @@ plugin = plugins/reminder/reminder.so</pre>
|
||||
<h2>Building</h2>
|
||||
<pre><span class="u">$</span> git clone ssh://host/home/git/splinter.git
|
||||
<span class="u">$</span> cd splinter && make <span class="d">small, nothing but libc and curses</span>
|
||||
<span class="u">$</span> make check <span class="d">33 test programs, plus the i18n checker</span>
|
||||
<span class="u">$</span> make check <span class="d">34 test programs, plus the i18n checker</span>
|
||||
<span class="u">$</span> ./splinter <span class="d">chat, the config is read</span>
|
||||
<span class="u">$</span> ./splinter --daemon <span class="d">Telegram gateway</span></pre>
|
||||
<p>POSIX make, C99, <code>-Wall -Wextra -Werror</code>. NetBSD curses is vendored, so the binary does not depend on a distribution's mood.</p>
|
||||
|
||||
+4
-4
@@ -53,8 +53,8 @@ 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>25639</b> Zeilen C99/POSIX ·
|
||||
<b>25</b> Werkzeuge · <b>33</b> Test-Programme ·
|
||||
<p class="meta"><b>26601</b> Zeilen C99/POSIX ·
|
||||
<b>25</b> Werkzeuge · <b>34</b> Test-Programme ·
|
||||
<b>16</b> Plugins · <b>42</b> Farbschemata · BSD-3-Clause</p>
|
||||
|
||||
<h2>Was es ist</h2>
|
||||
@@ -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 „typing“.</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. <code>/interview</code> stellt die Fragen im Chat. Programme in <code>commands/</code> antworten dort direkt im Chat; im TUI gehen sie ans Modell.</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. <code>/interview</code> stellt die Fragen im Chat. Antworten werden als Telegram-HTML gesetzt: fett, kursiv, Code und Links als Tags, Tabellen als Spalten im Monospace-Block; ein Stueck, das Telegram nicht parst, geht als Text raus statt verloren. Programme in <code>commands/</code> antworten dort direkt im Chat; im TUI gehen sie ans Modell.</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>
|
||||
@@ -152,7 +152,7 @@ plugin = plugins/reminder/reminder.so</pre>
|
||||
<h2>Bauen</h2>
|
||||
<pre><span class="u">$</span> git clone ssh://host/home/git/splinter.git
|
||||
<span class="u">$</span> cd splinter && make <span class="d">klein, nichts ausser libc und curses</span>
|
||||
<span class="u">$</span> make check <span class="d">33 Test-Programme, plus i18n-Pruefer</span>
|
||||
<span class="u">$</span> make check <span class="d">34 Test-Programme, plus i18n-Pruefer</span>
|
||||
<span class="u">$</span> ./splinter <span class="d">Chat, Konfiguration wird gelesen</span>
|
||||
<span class="u">$</span> ./splinter --daemon <span class="d">Telegram-Gateway</span></pre>
|
||||
<p>POSIX make, C99, <code>-Wall -Wextra -Werror</code>. Die NetBSD-curses liegt im Baum, damit das Binary nicht von der Laune einer Distribution abhaengt.</p>
|
||||
|
||||
@@ -53,8 +53,8 @@ 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>25639</b> linhas C99/POSIX ·
|
||||
<b>25</b> ferramentas · <b>33</b> programas de teste ·
|
||||
<p class="meta"><b>26601</b> linhas C99/POSIX ·
|
||||
<b>25</b> ferramentas · <b>34</b> programas de teste ·
|
||||
<b>16</b> plugins · <b>42</b> paletas · BSD-3-Clause</p>
|
||||
|
||||
<h2>O que e</h2>
|
||||
@@ -109,7 +109,8 @@ em ordem de nome. Chave desconhecida e erro com numero de linha.</p>
|
||||
<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 os comandos dos plugins carregados
|
||||
e os programas em <code>~/.splinter/commands/</code>; uma aprovacao chega com
|
||||
botoes Sim/Nao.</p>
|
||||
botoes Sim/Nao; as respostas viram HTML do Telegram (negrito, codigo, links,
|
||||
tabelas em bloco monoespaceado).</p>
|
||||
|
||||
<h2>O que fica registrado</h2>
|
||||
<p>Cada sessao escreve <code>session.log</code> (eventos legiveis) e
|
||||
@@ -117,7 +118,7 @@ botoes Sim/Nao.</p>
|
||||
|
||||
<h2>Compilar</h2>
|
||||
<pre>make
|
||||
make check <span class="d"># 33 binarios de teste, i18n, site</span></pre>
|
||||
make check <span class="d"># 34 binarios de teste, i18n, site</span></pre>
|
||||
<p>Sem dependencias alem de libc e pthread; ncurses vem do vendor.</p>
|
||||
|
||||
<h2>O que nao e</h2>
|
||||
|
||||
@@ -81,6 +81,9 @@ class H(http.server.BaseHTTPRequestHandler):
|
||||
markup = req.get("reply_markup")
|
||||
with open(sys.argv[3], "a") as f:
|
||||
f.write(text.replace("\n", " | ") + "\n")
|
||||
if req.get("parse_mode"):
|
||||
# the gateway renders markdown as HTML for Telegram
|
||||
f.write("mode: %s\n" % req["parse_mode"])
|
||||
if markup is not None:
|
||||
f.write("markup: " + json.dumps(
|
||||
markup, separators=(",", ":")) + "\n")
|
||||
@@ -101,7 +104,11 @@ class H(http.server.BaseHTTPRequestHandler):
|
||||
if m.get("role") == "user":
|
||||
last_user = m.get("content") or ""
|
||||
break
|
||||
if "tabelle" in last_user:
|
||||
if "markdown" in last_user:
|
||||
answer = "**fett** und `code`\n"
|
||||
chunks = [{"choices": [{"delta": {"content": answer}}]},
|
||||
{"choices": [{"delta": {}, "finish_reason": "stop"}]}]
|
||||
elif "tabelle" in last_user:
|
||||
# a markdown table: the chat must draw columns, not pipes
|
||||
answer = "| a | b |\n|---|---|\n| 1 | 22 |\n"
|
||||
chunks = [{"choices": [{"delta": {"content": answer}}]},
|
||||
|
||||
@@ -116,6 +116,19 @@ check "an unknown command is an error" \
|
||||
grep -q '^unbekanntes Kommando: /nosuch' "$D/sent.log"
|
||||
check "an unknown command does not start a model turn" test ! -e "$D/out.txt"
|
||||
|
||||
# the model's markdown is rendered: Telegram draws HTML, not asterisks
|
||||
run_text "zeig markdown" '<b>fett</b>' 40
|
||||
check "an answer is sent with parse_mode=HTML" grep -q '^mode: HTML' "$D/sent.log"
|
||||
check "bold markdown becomes a tag" grep -q '<b>fett</b>' "$D/sent.log"
|
||||
check "inline code becomes a tag" grep -q '<code>code</code>' "$D/sent.log"
|
||||
check "the raw markers do not reach the chat" \
|
||||
sh -c "! grep -q '\*\*fett\*\*' '$D/sent.log'"
|
||||
|
||||
# a table is for the eye: columns in a monospace block, not a row of pipes
|
||||
run_text "zeig eine tabelle" '<pre>' 50
|
||||
check "a table is drawn in a monospace block" grep -q '<pre>' "$D/sent.log"
|
||||
check "a table keeps its columns" grep -q '| a | b |' "$D/sent.log"
|
||||
|
||||
# /interview asks the six questions and writes the profile
|
||||
run_text "/interview" '^Profil geschrieben' 30
|
||||
prof=$(ls "$D/home/.splinter/profile/"*.md 2>/dev/null | head -1)
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* splinter -- model markdown as Telegram HTML.
|
||||
*
|
||||
* The chat used to show "**fett**", "|---|" and "<b>" as characters. These
|
||||
* cases pin what has to become a tag, what has to stay text (snake_case, a bare
|
||||
* '<'), and how a long answer is split so that every piece parses on its own.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "tgmd.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static void
|
||||
check_render(const char *md, const char *want)
|
||||
{
|
||||
struct sp_buf b;
|
||||
|
||||
if (sp_buf_init(&b) != 0) {
|
||||
fprintf(stderr, "FAIL init\n");
|
||||
failures++;
|
||||
return;
|
||||
}
|
||||
if (sp_tgmd_render(md, &b) != 0) {
|
||||
fprintf(stderr, "FAIL render of:\n%s\n", md);
|
||||
failures++;
|
||||
} else if (b.p == NULL || strcmp(b.p, want) != 0) {
|
||||
fprintf(stderr, "FAIL %s:%d:\n--- in ---\n%s--- want ---\n%s"
|
||||
"--- got ---\n%s\n", __FILE__, __LINE__, md, want,
|
||||
b.p != NULL ? b.p : "(null)");
|
||||
failures++;
|
||||
}
|
||||
sp_buf_free(&b);
|
||||
}
|
||||
|
||||
/* tag names opened and closed in 's', in order: 1 when every tag balances */
|
||||
static int
|
||||
balanced(const char *s)
|
||||
{
|
||||
char stack[32][16];
|
||||
size_t depth = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; s[i] != '\0'; i++) {
|
||||
char name[16];
|
||||
size_t n = 0, j;
|
||||
|
||||
if (s[i] != '<')
|
||||
continue;
|
||||
j = i + 1;
|
||||
if (s[j] == '/') {
|
||||
j++;
|
||||
while (s[j] != '\0' && s[j] != '>')
|
||||
name[n++] = s[j++];
|
||||
name[n] = '\0';
|
||||
if (depth == 0 || strcmp(stack[depth - 1], name) != 0)
|
||||
return 0;
|
||||
depth--;
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
while (s[j] != '\0' && s[j] != '>' && s[j] != ' ')
|
||||
name[n++] = s[j++];
|
||||
name[n] = '\0';
|
||||
if (n == 0)
|
||||
continue;
|
||||
if (depth == sizeof(stack) / sizeof(stack[0]))
|
||||
return 0;
|
||||
snprintf(stack[depth++], sizeof(stack[0]), "%s", name);
|
||||
i = j;
|
||||
}
|
||||
return depth == 0;
|
||||
}
|
||||
|
||||
/* the text between tags, with the entities the renderer writes undone */
|
||||
static int
|
||||
plain_into(struct sp_buf *out, const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; s[i] != '\0'; i++) {
|
||||
if (s[i] == '<') {
|
||||
const char *gt = strchr(s + i, '>');
|
||||
|
||||
if (gt == NULL)
|
||||
return -1;
|
||||
i = (size_t)(gt - s);
|
||||
continue;
|
||||
}
|
||||
if (strncmp(s + i, "&", 5) == 0) {
|
||||
if (sp_buf_puts(out, "&") != 0)
|
||||
return -1;
|
||||
i += 4;
|
||||
} else if (strncmp(s + i, "<", 4) == 0) {
|
||||
if (sp_buf_puts(out, "<") != 0)
|
||||
return -1;
|
||||
i += 3;
|
||||
} else if (strncmp(s + i, ">", 4) == 0) {
|
||||
if (sp_buf_puts(out, ">") != 0)
|
||||
return -1;
|
||||
i += 3;
|
||||
} else if (sp_buf_add(out, s + i, 1) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
check_chunks(void)
|
||||
{
|
||||
struct sp_buf md, html, piece, all, back;
|
||||
struct sp_tgmd_cursor cur;
|
||||
size_t i;
|
||||
int n, pieces = 0;
|
||||
|
||||
CHECK(sp_buf_init(&md) == 0);
|
||||
CHECK(sp_buf_init(&html) == 0);
|
||||
CHECK(sp_buf_init(&piece) == 0);
|
||||
CHECK(sp_buf_init(&all) == 0);
|
||||
CHECK(sp_buf_init(&back) == 0);
|
||||
for (i = 0; i < 120; i++)
|
||||
CHECK(sp_buf_printf(&md,
|
||||
"**Zeile %zu** mit `code` und einem <tag> & mehr Text, "
|
||||
"damit die Antwort lang genug fuer mehrere Nachrichten ist.\n",
|
||||
i) == 0);
|
||||
CHECK(sp_tgmd_render(md.p, &html) == 0);
|
||||
CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0);
|
||||
while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0) {
|
||||
pieces++;
|
||||
CHECK(piece.len > 0);
|
||||
/* a piece is what Telegram gets: it has to fit and to parse */
|
||||
CHECK(piece.len < 4096);
|
||||
CHECK(balanced(piece.p));
|
||||
CHECK(plain_into(&all, piece.p) == 0);
|
||||
}
|
||||
CHECK(n == 0);
|
||||
CHECK(pieces > 1);
|
||||
CHECK(plain_into(&back, html.p) == 0);
|
||||
CHECK(all.p != NULL && back.p != NULL && strcmp(all.p, back.p) == 0);
|
||||
sp_tgmd_cursor_free(&cur);
|
||||
sp_buf_free(&md);
|
||||
sp_buf_free(&html);
|
||||
sp_buf_free(&piece);
|
||||
sp_buf_free(&all);
|
||||
sp_buf_free(&back);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
/* inline: the shapes a model actually writes */
|
||||
check_render("**fett**", "<b>fett</b>");
|
||||
check_render("__fett__", "<b>fett</b>");
|
||||
check_render("*kursiv*", "<i>kursiv</i>");
|
||||
check_render("_kursiv_", "<i>kursiv</i>");
|
||||
check_render("~~weg~~", "<s>weg</s>");
|
||||
check_render("ein `code` hier", "ein <code>code</code> hier");
|
||||
check_render("**fett mit *kursiv***",
|
||||
"<b>fett mit <i>kursiv</i></b>");
|
||||
check_render("[Titel](https://example.org/a?x=1&y=2)",
|
||||
"<a href=\"https://example.org/a?x=1&y=2\">Titel</a>");
|
||||
|
||||
/* what must stay text */
|
||||
check_render("snake_case_name", "snake_case_name");
|
||||
check_render("2 * 3 * 4 = 24", "2 * 3 * 4 = 24");
|
||||
check_render("a < b & c > d", "a < b & c > d");
|
||||
check_render("<script>x</script>", "<script>x</script>");
|
||||
check_render("[kein link](javascript:alert(1))",
|
||||
"[kein link](javascript:alert(1))");
|
||||
check_render("* nicht fett", "\xe2\x80\xa2 nicht fett");
|
||||
|
||||
/* blocks */
|
||||
check_render("## Titel\nText\n", "<b>Titel</b>\nText\n");
|
||||
check_render("> zitat\n> mehr\n",
|
||||
"<blockquote>zitat\nmehr\n</blockquote>\n");
|
||||
check_render("---\n", "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\n");
|
||||
check_render("- eins\n- zwei\n",
|
||||
"\xe2\x80\xa2 eins\n\xe2\x80\xa2 zwei\n");
|
||||
check_render("```c\nif (a < b)\n\treturn **x**;\n```\n",
|
||||
"<pre><code class=\"language-c\">if (a < b)\n\treturn **x**;"
|
||||
"</code></pre>\n");
|
||||
check_render("```\nplain & simple\n```\n",
|
||||
"<pre><code>plain & simple</code></pre>\n");
|
||||
|
||||
/* a table: columns in <pre>, not a row of pipes in a proportional font */
|
||||
check_render("| a | b |\n|---|---|\n| 1 | 22 |\n",
|
||||
"<pre>| a | b |\n|---|----|\n| 1 | 22 |\n</pre>\n");
|
||||
|
||||
check_chunks();
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_tgmd: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
+4
-3
@@ -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 „typing“.</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. <code>/interview</code> stellt die Fragen im Chat. Programme in <code>commands/</code> antworten dort direkt im Chat; im TUI gehen sie ans Modell.</p>\n<p>Slash-Kommandos funktionieren "
|
||||
"Chat alle vier Sekunden „typing“.</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. <code>/interview</code> stellt die Fragen im Chat. Antworten werden als Telegram-HTML gesetzt: fett, kursiv, Code und Links als Tags, Tabellen als Spalten im Monospace-Block; ein Stueck, das Telegram nicht parst, geht als Text raus statt verloren. Programme in <code>commands/</code> antworten dort direkt im Chat; im TUI gehen sie ans Modell.</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 "
|
||||
""typing" 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. <code>/interview</code> asks the questions in the chat. Programs in <code>commands/</code> answer in the chat; in the TUI their output goes to the model.</p>\n<p>Slash commands work there too. "
|
||||
""typing" 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. <code>/interview</code> asks the questions in the chat. Answers are rendered as Telegram HTML: bold, italic, code and links as tags, tables as columns in a monospace block; a piece Telegram cannot parse goes out as text instead of being lost. Programs in <code>commands/</code> answer in the chat; in the TUI their output goes to the model.</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 "
|
||||
@@ -566,7 +566,8 @@ em ordem de nome. Chave desconhecida e erro com numero de linha.</p>""",
|
||||
<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 os comandos dos plugins carregados
|
||||
e os programas em <code>~/.splinter/commands/</code>; uma aprovacao chega com
|
||||
botoes Sim/Nao.</p>""",
|
||||
botoes Sim/Nao; as respostas viram HTML do Telegram (negrito, codigo, links,
|
||||
tabelas em bloco monoespaceado).</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>""",
|
||||
|
||||
Reference in new issue
Block a user