splinter: websearch and webfetch, through curl

Two read tools, because a harness that can read files but not the web invents
answers about anything current:

  websearch  query -> results as plain text
  webfetch   url   -> page as plain text

curl is what every machine already has; a second HTTP stack inside the harness
would only be a second set of bugs. Tags are stripped in the pipeline, output is
capped at 20 kB, and an empty answer says so instead of pretending.

Backend: a SearXNG instance or anything that answers JSON, configured once with
SPLINTER_SEARCH_URL=http://search.hiddenbox.org. Without it, DuckDuckGo lite - no
key, no account.

Measured, both through the model, not by hand:

  websearch 'NetBSD 11 release date'
    -> www.netbsd.org/releases/formal-11/ ... NetBSD 11.0 ... 2026-07-30
    -> NetBSD-11.1.html ... 2026-11-02

  webfetch https://www.netbsd.org/
    -> 'The NetBSD Project'

16 tools now.
This commit is contained in:
Konrad committed 2026-11-08 17:53:31 +00:00
1 parent 3f65dfdc4c
commit f07cb01051
1 file changed
+143
+143
View File
@@ -709,6 +709,126 @@ tool_list(const char *args, struct sp_buf *out, void *ctx)
return 0;
}
/*
* Web access through curl, because curl is what every machine already has and a
* second HTTP stack in the harness would only be a second set of bugs.
*
* Search backend: a SearXNG instance or anything that answers JSON:
*
* SPLINTER_SEARCH_URL=http://search.hiddenbox.org
*
* Without it DuckDuckGo's lite page is used - no key, no account, and its HTML is
* simple enough to strip honestly. Whatever comes back loses its tags and is
* capped; an answer that silently drops half a page is worse than a short one.
*/
static void
url_encode(const char *in, char *out, size_t len)
{
static const char hex[] = "0123456789ABCDEF";
size_t o = 0;
for (; *in != '\0' && o + 4 < len; in++) {
unsigned char c = (unsigned char)*in;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' ||
c == '~') {
out[o++] = (char)c;
} else if (c == ' ') {
out[o++] = '+';
} else {
out[o++] = '%';
out[o++] = hex[c >> 4];
out[o++] = hex[c & 0x0f];
}
}
out[o] = '\0';
}
static int
curl_text(const char *url, struct sp_buf *out)
{
struct sp_buf cmd = { 0 };
FILE *p;
char chunk[4096];
size_t got, total = 0;
if (sp_buf_init(&cmd) != 0)
return -1;
if (sp_buf_printf(&cmd,
"curl -sSL --max-time 25 -A 'splinter/0.1' '%s' "
"| sed -e 's/<[^>]*>/ /g' -e 's/&nbsp;/ /g' "
"| tr -s ' \t' ' ' | grep -v '^ *$' | head -c 20000", url) != 0) {
sp_buf_free(&cmd);
return -1;
}
p = popen(cmd.p, "r");
sp_buf_free(&cmd);
if (p == NULL)
return sp_buf_printf(out, "error: curl: %s", strerror(errno));
while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) {
total += got;
(void)sp_buf_add(out, chunk, got);
}
(void)pclose(p);
if (total == 0)
sp_buf_puts(out, "(no answer - check the connection or "
"SPLINTER_SEARCH_URL)");
return 0;
}
static int
tool_websearch(const char *args, struct sp_buf *out, void *ctx)
{
struct sp_buf keep = { 0 }, url = { 0 };
const char *q, *base;
char enc[1024];
int rc;
(void)ctx;
if (sp_buf_init(&keep) != 0 || sp_buf_init(&url) != 0)
return -1;
q = arg_text(args, "query", &keep);
if (q == NULL || *q == '\0') {
sp_buf_free(&keep);
sp_buf_free(&url);
return sp_buf_puts(out, "error: empty query");
}
url_encode(q, enc, sizeof(enc));
base = getenv("SPLINTER_SEARCH_URL");
if (base != NULL && *base != '\0')
(void)sp_buf_printf(&url, "%s/search?q=%s&format=json", base, enc);
else
(void)sp_buf_printf(&url,
"https://lite.duckduckgo.com/lite/?q=%s", enc);
rc = curl_text(url.p, out);
sp_buf_free(&keep);
sp_buf_free(&url);
return rc;
}
static int
tool_webfetch(const char *args, struct sp_buf *out, void *ctx)
{
struct sp_buf keep = { 0 };
const char *u;
int rc;
(void)ctx;
if (sp_buf_init(&keep) != 0)
return -1;
u = arg_text(args, "url", &keep);
if (u == NULL || *u == '\0') {
sp_buf_free(&keep);
return sp_buf_puts(out, "error: empty url");
}
rc = curl_text(u, out);
sp_buf_free(&keep);
return rc;
}
int
sp_builtin_register(struct sp_registry *r)
{
@@ -725,6 +845,29 @@ sp_builtin_register(struct sp_registry *r)
if (sp_registry_add(r, &t) != 0)
return -1;
memset(&t, 0, sizeof(t));
t.name = "websearch";
t.description = "Search the web and return the results as plain text. "
"Use it before inventing an answer about anything current.";
t.params = "{\"type\":\"object\",\"properties\":{\"query\":"
"{\"type\":\"string\",\"description\":\"what to search for\"}},"
"\"required\":[\"query\"]}";
t.auth = SP_AUTH_READ;
t.run = tool_websearch;
if (sp_registry_add(r, &t) != 0)
return -1;
memset(&t, 0, sizeof(t));
t.name = "webfetch";
t.description = "Fetch one URL and return it as plain text.";
t.params = "{\"type\":\"object\",\"properties\":{\"url\":"
"{\"type\":\"string\",\"description\":\"http or https URL\"}},"
"\"required\":[\"url\"]}";
t.auth = SP_AUTH_READ;
t.run = tool_webfetch;
if (sp_registry_add(r, &t) != 0)
return -1;
memset(&t, 0, sizeof(t));
t.name = "run";
t.description = "Run a shell command and return its combined output.";