/model writes its choice to <config>.d/90-model.conf, so the next start uses
it; picking main deletes the file and the configuration speaks again. With a
plugin transport nothing is written, because that program picks the model.
/model probe asks every endpoint /v1/models and offers what it really serves
as buttons; the callback carries an index, because a GGUF path does not fit
in Telegram's 64 bytes. The TUI gets the same command: the list, or
/model <id|name>, with the same persistence.
/connect runs one tiny turn against the provider it just saved (30 s) and
says whether endpoint, model and key answer. The message with the buttons
goes out before the wait starts, so the save is visible immediately.
A2A no longer refuses binary parts: file{bytes} and raw with a non-text media
type are written to the session's inbox (0600, 8 MiB cap) and the prompt says
[file <name> at <path> (mime, n bytes)], so a text model can hand the file to
a tool. The client saves what a remote agent sends the same way
(SPLINTER_A2A_INBOX). SubscribeToTask stays refused: the task store has no
live events, and a one-shot replay would pretend to be a subscription.
Markdown gains <https://...> autolinks and images (as links, since Telegram
has no inline image without sendPhoto).
225 lines
7.4 KiB
C
225 lines
7.4 KiB
C
/*
|
|
* splinter -- the provider list and /connect's config line.
|
|
*
|
|
* /model shows what the config already says and /connect adds a line to
|
|
* <config>.d/20-connect.conf. The line has to survive a restart, so the test
|
|
* writes it, loads the config again and looks at the delegate it produced.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "config.h"
|
|
#include "providers.h"
|
|
#include "splinter.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
|
|
base(struct sp_config *cfg)
|
|
{
|
|
struct sp_delegate *d;
|
|
|
|
memset(cfg, 0, sizeof(*cfg));
|
|
sp_config_init(cfg);
|
|
snprintf(cfg->endpoint, sizeof(cfg->endpoint),
|
|
"http://127.0.0.1:8080/v1/chat/completions");
|
|
snprintf(cfg->model, sizeof(cfg->model), "local");
|
|
snprintf(cfg->key_env, sizeof(cfg->key_env), "LOCAL_KEY");
|
|
d = &cfg->delegate[cfg->ndelegates++];
|
|
snprintf(d->id, sizeof(d->id), "far");
|
|
snprintf(d->kind, sizeof(d->kind), "openai");
|
|
snprintf(d->endpoint, sizeof(d->endpoint),
|
|
"https://api.example/v1/chat/completions");
|
|
snprintf(d->model, sizeof(d->model), "big");
|
|
snprintf(d->key_env, sizeof(d->key_env), "FAR_KEY");
|
|
/* a cli delegate has no endpoint: it is not a model to switch to */
|
|
d = &cfg->delegate[cfg->ndelegates++];
|
|
snprintf(d->id, sizeof(d->id), "claude");
|
|
snprintf(d->kind, sizeof(d->kind), "cli");
|
|
snprintf(d->cmd, sizeof(d->cmd), "claude -p");
|
|
/* a delegate without a model is not one either */
|
|
d = &cfg->delegate[cfg->ndelegates++];
|
|
snprintf(d->id, sizeof(d->id), "empty");
|
|
snprintf(d->endpoint, sizeof(d->endpoint), "https://x/v1/chat/completions");
|
|
snprintf(cfg->exit[cfg->nexits].id, sizeof(cfg->exit[0].id), "groq");
|
|
snprintf(cfg->exit[cfg->nexits].endpoint,
|
|
sizeof(cfg->exit[0].endpoint), "https://api.groq.com/openai/v1/chat/completions");
|
|
snprintf(cfg->exit[cfg->nexits].model, sizeof(cfg->exit[0].model),
|
|
"llama");
|
|
cfg->nexits++;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_config cfg, back;
|
|
struct sp_provider p;
|
|
struct sp_buf err;
|
|
char dir[512], drop[600], mainconf[700], path[800];
|
|
char line[600];
|
|
const char *tmp = getenv("TMPDIR");
|
|
FILE *f;
|
|
size_t idx;
|
|
int nlines = 0;
|
|
char buf[1024];
|
|
|
|
if (tmp == NULL || *tmp == '\0')
|
|
tmp = "/var/tmp";
|
|
snprintf(dir, sizeof(dir), "%s/splinter-prov-XXXXXX", tmp);
|
|
CHECK(mkdtemp(dir) != NULL);
|
|
/* the loader reads <config>.d, so /connect writes there too */
|
|
snprintf(drop, sizeof(drop), "%s/splinter.conf.d", dir);
|
|
|
|
base(&cfg);
|
|
CHECK(sp_provider_count(&cfg) == 3);
|
|
CHECK(sp_provider_at(&cfg, 0, &p) == 0);
|
|
CHECK(strcmp(p.id, "main") == 0 && p.main == 1);
|
|
CHECK(strcmp(p.model, "local") == 0);
|
|
CHECK(sp_provider_at(&cfg, 1, &p) == 0);
|
|
CHECK(strcmp(p.id, "far") == 0 && strcmp(p.model, "big") == 0);
|
|
CHECK(sp_provider_at(&cfg, 2, &p) == 0);
|
|
CHECK(strcmp(p.id, "groq") == 0);
|
|
CHECK(sp_provider_at(&cfg, 3, &p) != 0);
|
|
|
|
CHECK(sp_provider_find(&cfg, "far", &idx) == 1 && idx == 1);
|
|
CHECK(sp_provider_find(&cfg, "nope", &idx) == 0);
|
|
|
|
/* what /connect must refuse before it becomes a line nobody can read */
|
|
CHECK(sp_provider_id_ok("ocgo") == 1);
|
|
CHECK(sp_provider_id_ok("my-prov_2") == 1);
|
|
CHECK(sp_provider_id_ok("Main") == 0);
|
|
CHECK(sp_provider_id_ok("main") == 0);
|
|
CHECK(sp_provider_id_ok("a b") == 0);
|
|
CHECK(sp_provider_id_ok("") == 0);
|
|
CHECK(sp_provider_endpoint_ok("http://127.0.0.1:11434/v1/chat/completions") == 1);
|
|
CHECK(sp_provider_endpoint_ok("https://x/v1") == 1);
|
|
CHECK(sp_provider_endpoint_ok("x/v1") == 0);
|
|
CHECK(sp_provider_endpoint_ok("http://x/a b") == 0);
|
|
CHECK(sp_provider_endpoint_ok("http://x/a|b") == 0);
|
|
CHECK(sp_provider_model_ok("deepseek-v4.1-flash") == 1);
|
|
CHECK(sp_provider_model_ok("") == 0);
|
|
CHECK(sp_provider_model_ok("a|b") == 0);
|
|
CHECK(sp_provider_key_ok("OPENCODE_GO_API_KEY") == 1);
|
|
CHECK(sp_provider_key_ok("") == 1);
|
|
CHECK(sp_provider_key_ok("1BAD") == 0);
|
|
CHECK(sp_provider_key_ok("has-dash") == 0);
|
|
|
|
/* add: one line, and the config remembers it */
|
|
CHECK(sp_buf_init(&err) == 0);
|
|
CHECK(sp_provider_add(&cfg, drop, "ocgo",
|
|
"https://opencode.ai/zen/go/v1/chat/completions",
|
|
"deepseek-v4.1-flash", "OPENCODE_GO_API_KEY", &err) == 0);
|
|
CHECK(sp_provider_count(&cfg) == 4);
|
|
CHECK(sp_provider_find(&cfg, "ocgo", &idx) == 1);
|
|
snprintf(path, sizeof(path), "%s/20-connect.conf", drop);
|
|
f = fopen(path, "r");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
while (fgets(buf, sizeof(buf), f) != NULL)
|
|
if (strstr(buf, "delegate =") != NULL)
|
|
nlines++;
|
|
fclose(f);
|
|
}
|
|
CHECK(nlines == 1);
|
|
|
|
/* a second add replaces the line instead of growing the file */
|
|
CHECK(sp_provider_add(&cfg, drop, "ocgo",
|
|
"https://opencode.ai/zen/go/v1/chat/completions",
|
|
"other-model", "", &err) == 0);
|
|
nlines = 0;
|
|
f = fopen(path, "r");
|
|
if (f != NULL) {
|
|
while (fgets(buf, sizeof(buf), f) != NULL)
|
|
if (strstr(buf, "delegate =") != NULL)
|
|
nlines++;
|
|
fclose(f);
|
|
}
|
|
CHECK(nlines == 1);
|
|
CHECK(sp_provider_find(&cfg, "ocgo", &idx) == 1);
|
|
CHECK(sp_provider_at(&cfg, idx, &p) == 0);
|
|
CHECK(strcmp(p.model, "other-model") == 0 && p.key_env[0] == '\0');
|
|
|
|
/* the written line is a line the loader accepts */
|
|
snprintf(mainconf, sizeof(mainconf), "%s/splinter.conf", dir);
|
|
f = fopen(mainconf, "w");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("endpoint = http://127.0.0.1:8080/v1/chat/completions\n"
|
|
"model = local\n", f);
|
|
fclose(f);
|
|
}
|
|
memset(&back, 0, sizeof(back));
|
|
sp_config_init(&back);
|
|
CHECK(sp_config_load(&back, mainconf, &err) == 0);
|
|
{
|
|
const struct sp_delegate *d = NULL;
|
|
size_t i;
|
|
|
|
for (i = 0; i < back.ndelegates; i++)
|
|
if (strcmp(back.delegate[i].id, "ocgo") == 0)
|
|
d = &back.delegate[i];
|
|
CHECK(d != NULL);
|
|
if (d != NULL) {
|
|
CHECK(strcmp(d->model, "other-model") == 0);
|
|
CHECK(strcmp(d->endpoint,
|
|
"https://opencode.ai/zen/go/v1/chat/completions") == 0);
|
|
/* no kind= in the line means openai */
|
|
CHECK(d->kind[0] == '\0' || strcmp(d->kind, "openai") == 0);
|
|
}
|
|
}
|
|
|
|
/* /model writes 90-model.conf; picking main removes it again */
|
|
{
|
|
struct sp_provider pick;
|
|
char mpath[800], mbuf[1024];
|
|
struct sp_buf merr;
|
|
FILE *mf;
|
|
|
|
memset(&pick, 0, sizeof(pick));
|
|
snprintf(pick.id, sizeof(pick.id), "ocgo");
|
|
snprintf(pick.endpoint, sizeof(pick.endpoint),
|
|
"https://opencode.ai/zen/go/v1/chat/completions");
|
|
snprintf(pick.model, sizeof(pick.model), "other-model");
|
|
snprintf(pick.key_env, sizeof(pick.key_env), "OPENCODE_GO_API_KEY");
|
|
sp_buf_init(&merr);
|
|
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
|
|
snprintf(mpath, sizeof(mpath), "%s.d/90-model.conf", mainconf);
|
|
mf = fopen(mpath, "r");
|
|
CHECK(mf != NULL);
|
|
if (mf != NULL) {
|
|
mbuf[0] = '\0';
|
|
(void)fread(mbuf, 1, sizeof(mbuf) - 1, mf);
|
|
mbuf[sizeof(mbuf) - 1] = '\0';
|
|
fclose(mf);
|
|
CHECK(strstr(mbuf, "model = other-model") != NULL);
|
|
CHECK(strstr(mbuf, "endpoint = https://") != NULL);
|
|
CHECK(strstr(mbuf, "api_key_env = OPENCODE_GO_API_KEY") !=
|
|
NULL);
|
|
}
|
|
memset(&pick, 0, sizeof(pick));
|
|
pick.main = 1;
|
|
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
|
|
CHECK(access(mpath, F_OK) != 0);
|
|
sp_buf_free(&merr);
|
|
}
|
|
|
|
CHECK(sp_provider_line(line, sizeof(line), "ocgo", "https://x/v1",
|
|
"m", "K") == 0);
|
|
CHECK(strstr(line, "delegate = ocgo |") != NULL);
|
|
CHECK(strstr(line, "key_env=K") != NULL);
|
|
|
|
sp_buf_free(&err);
|
|
printf("%s", failures == 0 ? "test_providers: ok\n" : "");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|