Usability pass: tool registry, TUI polish, saved keys and settings
ci / linux (clang) (push) Canceled after 0s
ci / linux (gcc) (push) Canceled after 0s
ci / sanitizers (push) Canceled after 0s
ci / musl (push) Canceled after 0s
ci / macos (push) Canceled after 0s

Tools
- Table-driven tool registry with permission classes and default/minimal
  profiles; new tools: edit_file, list_dir, glob, grep, delete_file,
  move_file, todo_write, web_fetch, ask_user (plus read_file paging).
- Built-in Myers diff; write tools return unified diffs.
- Persistent permission rules (.agent/permissions.json), per-turn undo
  snapshots (.agent/undo), AGENTS.md project notes, auto-compact at 80%
  of the model context, cancellable run_shell with process-group kill.

TUI
- Table-driven slash commands: /provider /key /effort /status /tools
  /permissions /yolo /undo /retry /diff /export /copy /rename /delete
  /init /max-steps /todo, with / and @ completion.
- Permission modal with diff and prefix rules, ask_user modal, todo
  panel, header with model/effort/context, bracketed paste, line editing,
  wide-char cursor, NO_COLOR and ASCII fallback; Esc never quits.

Startup and configuration
- API keys are entered in the TUI (hidden) and saved per provider in
  <config>/credentials (0600); env variables are only a fallback.
- Last provider, per-provider model and reasoning effort are remembered
  in <config>/settings.json; --effort/-e and /effort send
  reasoning_effort (low|medium|high|max) to models that accept it.
- DeepSeek preset uses deepseek-flash / deepseek-v4-pro.
- Session flags --new/--continue/--resume/--list-sessions, --yolo,
  --confirm.

Compatibility and tests
- ncurses detection, macOS/Homebrew support, make install, XDG config
  dir, POSIX cleanups; Gitea CI (gcc/clang/asan/musl/macOS).
- Unit tests for diff, fs tools, registry, undo/perms and config;
  e2e fixtures multi_file_refactor and ask_or_todo; vendored cJSON.
This commit is contained in:
WHC
2026-10-05 01:47:01 +08:00
parent 9877ff48b6
commit 2fde898da0
105 changed files with 19348 additions and 3786 deletions
+108
View File
@@ -0,0 +1,108 @@
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cst_tools.h"
#include "cst_util.h"
#include "tools_internal.h"
/* Accepts `[ ] x`, `- [x] x`, `[~] x`, `* [-] x`; bare lines count as todo. */
static int todo_line(const char *s, size_t n, char *mark, const char **text,
size_t *tn) {
while (n && (*s == ' ' || *s == '\t')) {
s++;
n--;
}
while (n && (s[n - 1] == ' ' || s[n - 1] == '\r' || s[n - 1] == '\t'))
n--;
if (n && (*s == '-' || *s == '*') && n > 1 && s[1] == ' ') {
s += 2;
n -= 2;
}
if (!n)
return 0;
*mark = ' ';
if (n >= 3 && s[0] == '[' && s[2] == ']') {
char m = s[1];
if (m == 'x' || m == 'X')
*mark = 'x';
else if (m == '~' || m == '-' || m == '>' || m == '/')
*mark = '~';
s += 3;
n -= 3;
while (n && *s == ' ') {
s++;
n--;
}
}
if (!n)
return 0;
*text = s;
*tn = n;
return 1;
}
int cst_tool_todo_write(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
size_t len = 0;
const char *todos = tool_arg_n(c, "todos", &len);
if (!todos)
return tool_set_result(r, "error: missing todos", 1);
cst_buf_t list = {0};
int total = 0, done = 0, doing = 0;
for (size_t i = 0; i < len;) {
const char *e = memchr(todos + i, '\n', len - i);
size_t end = e ? (size_t)(e - todos) : len;
char mark;
const char *text;
size_t tn;
if (todo_line(todos + i, end - i, &mark, &text, &tn)) {
cst_buf_printf(&list, "[%c] ", mark);
cst_buf_append(&list, text, tn);
cst_buf_puts(&list, "\n");
total++;
done += mark == 'x';
doing += mark == '~';
}
i = end + 1;
}
if (!total) {
cst_buf_free(&list);
return tool_set_result(r, "error: no todo items (one per line: `[ ] task`)",
1);
}
if (ctx->set_todo)
ctx->set_todo(list.buf, ctx->todo_ud);
cst_buf_t b = {0};
cst_buf_printf(&b, "todo list updated: %d/%d done, %d in progress\n", done,
total, doing);
cst_buf_append(&b, list.buf, list.len);
cst_buf_free(&list);
return tool_take_buf(r, &b, 0);
}
int cst_tool_ask_user(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
const char *q = tool_arg(c, "question");
const char *opts = tool_arg(c, "options");
if (!q || !q[0])
return tool_set_result(r, "error: missing question", 1);
if (!ctx->ask)
return tool_set_result(r,
"error: no interactive user is available (headless "
"run). Decide yourself and state the assumption "
"in your answer.",
1);
char ans[2048];
ans[0] = 0;
if (ctx->ask(q, opts, ans, sizeof(ans), ctx->ask_ud) != 0)
return tool_set_result(r,
"the user dismissed the question without "
"answering; continue with your best judgement",
1);
return tool_set_resultf(r, 0, "user answered: %s", ans);
}
+280
View File
@@ -0,0 +1,280 @@
/* Line diff (Myers O(ND)) rendered like GNU `diff -u`. */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "cst_tools.h"
#include "cst_util.h"
#define DIFF_CONTEXT 3
/* Edit distance beyond this falls back to "replace the changed middle" so
* the trace stays O(DMAX^2) ints. */
#define DIFF_DMAX 2000
typedef struct {
const char *p;
size_t n;
int nl; /* line ended with '\n' */
uint64_t h;
} dline_t;
typedef struct {
char op; /* ' ', '-', '+' */
const dline_t *l;
} dop_t;
static dline_t *split_lines(const char *s, size_t len, int *count) {
int cap = 64, n = 0;
dline_t *v = malloc((size_t)cap * sizeof(*v));
if (!v)
return NULL;
size_t i = 0;
while (i < len) {
const char *nl = memchr(s + i, '\n', len - i);
size_t e = nl ? (size_t)(nl - s) : len;
if (n == cap) {
cap *= 2;
dline_t *nv = realloc(v, (size_t)cap * sizeof(*v));
if (!nv) {
free(v);
return NULL;
}
v = nv;
}
v[n].p = s + i;
v[n].n = e - i;
v[n].nl = nl != NULL;
v[n].h = cst_fnv1a64(CST_FNV_INIT, s + i, e - i) ^ (uint64_t)v[n].nl;
n++;
i = nl ? e + 1 : len;
}
*count = n;
return v;
}
static int line_eq(const dline_t *a, const dline_t *b) {
return a->h == b->h && a->n == b->n && a->nl == b->nl &&
memcmp(a->p, b->p, a->n) == 0;
}
/* Middle section edit script into ops (appended). Returns 0 or -1 (oom). */
static int myers(const dline_t *a, int N, const dline_t *b, int M, dop_t *ops,
int *nops) {
int dmax = N + M;
if (dmax > DIFF_DMAX)
dmax = DIFF_DMAX;
int off = dmax + 1;
int *V = calloc((size_t)(2 * dmax + 3), sizeof(int));
int **trace = calloc((size_t)dmax + 1, sizeof(int *));
if (!V || !trace) {
free(V);
free(trace);
return -1;
}
int found = -1;
for (int d = 0; d <= dmax && found < 0; d++) {
for (int k = -d; k <= d; k += 2) {
int x;
if (k == -d || (k != d && V[off + k - 1] < V[off + k + 1]))
x = V[off + k + 1];
else
x = V[off + k - 1] + 1;
int y = x - k;
while (x < N && y < M && line_eq(&a[x], &b[y])) {
x++;
y++;
}
V[off + k] = x;
if (x >= N && y >= M) {
found = d;
break;
}
}
trace[d] = malloc((size_t)(2 * d + 1) * sizeof(int));
if (!trace[d]) {
found = -2;
break;
}
memcpy(trace[d], V + off - d, (size_t)(2 * d + 1) * sizeof(int));
}
int rc = 0;
if (found < 0) {
/* too different (or oom): delete the old middle, insert the new one */
for (int i = 0; i < N; i++)
ops[(*nops)++] = (dop_t){'-', &a[i]};
for (int j = 0; j < M; j++)
ops[(*nops)++] = (dop_t){'+', &b[j]};
} else {
/* backtrack into a reversed list */
dop_t *rev = malloc((size_t)(N + M + 1) * sizeof(dop_t));
if (!rev) {
rc = -1;
} else {
int nr = 0, x = N, y = M;
for (int d = found; d > 0; d--) {
const int *P = trace[d - 1]; /* index k + (d-1) */
int k = x - y;
int pk;
if (k == -d ||
(k != d && P[k - 1 + (d - 1)] < P[k + 1 + (d - 1)]))
pk = k + 1;
else
pk = k - 1;
int px = P[pk + (d - 1)];
int py = px - pk;
while (x > px && y > py) {
rev[nr++] = (dop_t){' ', &a[x - 1]};
x--;
y--;
}
if (x == px)
rev[nr++] = (dop_t){'+', &b[y - 1]};
else
rev[nr++] = (dop_t){'-', &a[x - 1]};
x = px;
y = py;
}
while (x > 0 && y > 0) {
rev[nr++] = (dop_t){' ', &a[x - 1]};
x--;
y--;
}
for (int i = nr - 1; i >= 0; i--)
ops[(*nops)++] = rev[i];
free(rev);
}
}
for (int d = 0; d <= dmax; d++)
free(trace[d]);
free(trace);
free(V);
return rc;
}
/* Within each run of changes put deletions before insertions (GNU order). */
static void normalize_runs(dop_t *ops, int n, dop_t *tmp) {
int i = 0;
while (i < n) {
if (ops[i].op == ' ') {
i++;
continue;
}
int j = i;
while (j < n && ops[j].op != ' ')
j++;
int t = 0;
for (int k = i; k < j; k++)
if (ops[k].op == '-')
tmp[t++] = ops[k];
for (int k = i; k < j; k++)
if (ops[k].op == '+')
tmp[t++] = ops[k];
memcpy(ops + i, tmp, (size_t)(j - i) * sizeof(dop_t));
i = j;
}
}
static void range(cst_buf_t *b, char sign, int before, int count) {
int start = count == 0 ? before : before + 1;
if (count == 1)
cst_buf_printf(b, "%c%d", sign, start);
else
cst_buf_printf(b, "%c%d,%d", sign, start, count);
}
static void emit_line(cst_buf_t *b, const dop_t *o) {
cst_buf_append(b, &o->op, 1);
cst_buf_append(b, o->l->p, o->l->n);
cst_buf_puts(b, "\n");
if (!o->l->nl)
cst_buf_puts(b, "\\ No newline at end of file\n");
}
char *cst_diff_unified(const char *label, const char *a, size_t alen,
const char *b, size_t blen) {
int na = 0, nb = 0;
dline_t *A = split_lines(a ? a : "", alen, &na);
dline_t *B = split_lines(b ? b : "", blen, &nb);
dop_t *ops = malloc((size_t)(na + nb + 1) * sizeof(dop_t));
dop_t *tmp = malloc((size_t)(na + nb + 1) * sizeof(dop_t));
char *res = NULL;
if (!A || !B || !ops || !tmp)
goto out;
int p = 0;
while (p < na && p < nb && line_eq(&A[p], &B[p]))
p++;
int s = 0;
while (s < na - p && s < nb - p && line_eq(&A[na - 1 - s], &B[nb - 1 - s]))
s++;
int nops = 0;
for (int i = 0; i < p; i++)
ops[nops++] = (dop_t){' ', &A[i]};
if (myers(A + p, na - p - s, B + p, nb - p - s, ops, &nops) != 0)
goto out;
for (int i = na - s; i < na; i++)
ops[nops++] = (dop_t){' ', &A[i]};
normalize_runs(ops, nops, tmp);
cst_buf_t o = {0};
int any = 0;
int old_before = 0, new_before = 0; /* lines consumed before op i */
int i = 0;
while (i < nops) {
if (ops[i].op == ' ') {
old_before++;
new_before++;
i++;
continue;
}
/* change at i: hunk starts DIFF_CONTEXT lines earlier */
int start = i - DIFF_CONTEXT < 0 ? 0 : i - DIFF_CONTEXT;
int back = i - start;
int ob = old_before - back, nbf = new_before - back;
int end = i;
for (;;) {
while (end < nops && ops[end].op != ' ')
end++;
int nx = end;
while (nx < nops && ops[nx].op == ' ')
nx++;
if (nx < nops && nx - end <= 2 * DIFF_CONTEXT) {
end = nx;
continue;
}
end = end + DIFF_CONTEXT > nops ? nops : end + DIFF_CONTEXT;
break;
}
int oc = 0, nc = 0;
for (int k = start; k < end; k++) {
if (ops[k].op != '+')
oc++;
if (ops[k].op != '-')
nc++;
}
if (!any) {
cst_buf_printf(&o, "--- a/%s\n+++ b/%s\n", label, label);
any = 1;
}
cst_buf_puts(&o, "@@ ");
range(&o, '-', ob, oc);
cst_buf_puts(&o, " ");
range(&o, '+', nbf, nc);
cst_buf_puts(&o, " @@\n");
for (int k = start; k < end; k++)
emit_line(&o, &ops[k]);
old_before = ob + oc;
new_before = nbf + nc;
i = end;
}
if (!o.buf)
cst_buf_puts(&o, "");
res = cst_buf_steal(&o);
out:
free(A);
free(B);
free(ops);
free(tmp);
return res;
}
+1178
View File
File diff suppressed because it is too large Load Diff
+317
View File
@@ -0,0 +1,317 @@
#include "cst_agent.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
const char *p;
size_t n;
} line_t;
typedef struct {
line_t *v;
size_t n;
size_t cap;
} lines_t;
static int lines_push(lines_t *L, const char *p, size_t n) {
if (L->n == L->cap) {
size_t nc = L->cap ? L->cap * 2 : 64;
line_t *nv = realloc(L->v, nc * sizeof(*nv));
if (!nv)
return -1;
L->v = nv;
L->cap = nc;
}
L->v[L->n].p = p;
L->v[L->n].n = n;
L->n++;
return 0;
}
/* Split into lines without '\n'. A trailing newline does not add an empty
* final line. */
static int split_lines(const char *s, size_t n, lines_t *out) {
size_t start = 0;
for (size_t i = 0; i < n; i++) {
if (s[i] == '\n') {
if (lines_push(out, s + start, i - start) != 0)
return -1;
start = i + 1;
}
}
if (start < n && lines_push(out, s + start, n - start) != 0)
return -1;
return 0;
}
static size_t rtrim_len(const line_t *l) {
size_t n = l->n;
while (n > 0 && (l->p[n - 1] == ' ' || l->p[n - 1] == '\t' ||
l->p[n - 1] == '\r'))
n--;
return n;
}
static int line_eq(const line_t *a, const line_t *b, int loose) {
if (!loose)
return a->n == b->n && memcmp(a->p, b->p, a->n) == 0;
size_t na = rtrim_len(a), nb = rtrim_len(b);
return na == nb && memcmp(a->p, b->p, na) == 0;
}
static int seq_at(const lines_t *file, size_t pos, const lines_t *seq,
int loose) {
if (pos + seq->n > file->n)
return 0;
for (size_t i = 0; i < seq->n; i++) {
if (!line_eq(&file->v[pos + i], &seq->v[i], loose))
return 0;
}
return 1;
}
/* Find seq in file at or after min_pos. hint<0 means unknown. Returns
* position, -1 when absent, -2 when ambiguous without a hint. */
static long locate(const lines_t *file, size_t min_pos, const lines_t *seq,
long hint, int loose) {
if (seq->n > file->n)
return -1;
size_t last = file->n - seq->n;
if (hint >= 0) {
long best = -1;
long best_d = 0;
for (size_t p = min_pos; p <= last; p++) {
if (!seq_at(file, p, seq, loose))
continue;
long d = (long)p - hint;
if (d < 0)
d = -d;
if (best < 0 || d < best_d) {
best = (long)p;
best_d = d;
}
}
return best;
}
long found = -1;
for (size_t p = min_pos; p <= last; p++) {
if (seq_at(file, p, seq, loose)) {
if (found >= 0)
return -2;
found = (long)p;
}
}
return found;
}
static int parse_hunk_header(const char *p, size_t n, long *a, long *b,
long *c, long *d) {
/* "@@ -a[,b] +c[,d] @@..." */
char tmp[128];
if (n >= sizeof(tmp))
n = sizeof(tmp) - 1;
memcpy(tmp, p, n);
tmp[n] = 0;
*b = *d = 1;
if (sscanf(tmp, "@@ -%ld,%ld +%ld,%ld", a, b, c, d) == 4)
return 0;
*b = *d = 1;
if (sscanf(tmp, "@@ -%ld +%ld,%ld", a, c, d) == 3)
return 0;
*b = *d = 1;
if (sscanf(tmp, "@@ -%ld,%ld +%ld", a, b, c) == 3)
return 0;
*b = *d = 1;
if (sscanf(tmp, "@@ -%ld +%ld", a, c) == 2)
return 0;
return -1;
}
int cst_patch_apply_buffer(const char *orig, size_t olen, const char *hunks,
size_t hlen, char **out, size_t *out_len,
int *nhunks, char *err, size_t err_sz) {
lines_t file = {0}, hl = {0};
lines_t old_seq = {0}, new_seq = {0};
int rc = -1;
int count = 0;
long delta = 0;
size_t min_pos = 0;
int orig_final_nl = olen == 0 || orig[olen - 1] == '\n';
int final_nl = orig_final_nl;
if (err && err_sz)
err[0] = 0;
if (out)
*out = NULL;
if (split_lines(orig ? orig : "", olen, &file) != 0 ||
split_lines(hunks ? hunks : "", hlen, &hl) != 0) {
snprintf(err, err_sz, "oom");
goto done;
}
size_t i = 0;
while (i < hl.n) {
const line_t *h = &hl.v[i];
if (!(h->n >= 2 && h->p[0] == '@' && h->p[1] == '@')) {
i++;
continue;
}
long a = -1, b = -1, c = -1, d = -1;
int have_hint = parse_hunk_header(h->p, h->n, &a, &b, &c, &d) == 0;
i++;
old_seq.n = new_seq.n = 0;
int last_kind = 0; /* ' ', '-', '+' of previous body line */
int new_no_nl = 0, old_no_nl = 0;
for (; i < hl.n; i++) {
const line_t *l = &hl.v[i];
if (l->n >= 2 && l->p[0] == '@' && l->p[1] == '@')
break;
if (l->n == 0) {
/* models often drop the leading space of blank context lines; a
* blank line at the very end of the patch is just a terminator */
if (i + 1 == hl.n)
break;
lines_push(&old_seq, l->p, 0);
lines_push(&new_seq, l->p, 0);
last_kind = ' ';
continue;
}
char tag = l->p[0];
if (tag == ' ') {
lines_push(&old_seq, l->p + 1, l->n - 1);
lines_push(&new_seq, l->p + 1, l->n - 1);
} else if (tag == '-') {
if (l->n >= 4 && memcmp(l->p, "--- ", 4) == 0 && i + 1 < hl.n &&
hl.v[i + 1].n >= 4 && memcmp(hl.v[i + 1].p, "+++ ", 4) == 0)
break; /* next file header */
lines_push(&old_seq, l->p + 1, l->n - 1);
} else if (tag == '+') {
lines_push(&new_seq, l->p + 1, l->n - 1);
} else if (tag == '\\') {
if (last_kind == '+')
new_no_nl = 1;
else if (last_kind == '-')
old_no_nl = 1;
else if (last_kind == ' ')
new_no_nl = old_no_nl = 1;
continue;
} else {
break; /* diff --git, index ..., prose */
}
last_kind = tag;
}
long hint = -1;
if (have_hint) {
/* "-a,0" inserts after line a; otherwise a is 1-based start */
hint = (b == 0 ? a : a - 1) + delta;
if (hint < 0)
hint = 0;
}
long pos;
if (old_seq.n == 0) {
if (hint >= 0)
pos = hint > (long)file.n ? (long)file.n : hint;
else if (file.n == 0)
pos = 0;
else {
snprintf(err, err_sz,
"hunk %d: pure insertion needs @@ -a,0 +c,d @@ line numbers",
count + 1);
goto done;
}
} else {
pos = locate(&file, min_pos, &old_seq, hint, 0);
if (pos == -1)
pos = locate(&file, min_pos, &old_seq, hint, 1);
if (pos == -1 && min_pos > 0) {
pos = locate(&file, 0, &old_seq, hint, 0);
if (pos == -1)
pos = locate(&file, 0, &old_seq, hint, 1);
}
if (pos == -2) {
snprintf(err, err_sz,
"hunk %d: ambiguous, context matches several places; add "
"line numbers or more context",
count + 1);
goto done;
}
if (pos < 0) {
snprintf(err, err_sz,
"hunk %d: context/removed lines not found in file (first: "
"\"%.*s\")",
count + 1, (int)(old_seq.v[0].n > 80 ? 80 : old_seq.v[0].n),
old_seq.v[0].p);
goto done;
}
}
/* splice: file[pos .. pos+old) -> new_seq */
size_t old_n = old_seq.n, new_n = new_seq.n;
size_t need = file.n - old_n + new_n;
if (need > file.cap) {
line_t *nv = realloc(file.v, (need + 16) * sizeof(line_t));
if (!nv) {
snprintf(err, err_sz, "oom");
goto done;
}
file.v = nv;
file.cap = need + 16;
}
memmove(&file.v[pos + new_n], &file.v[pos + old_n],
(file.n - (size_t)pos - old_n) * sizeof(line_t));
for (size_t k = 0; k < new_n; k++)
file.v[pos + k] = new_seq.v[k];
int touches_end = (size_t)pos + new_n == need;
file.n = need;
delta += (long)new_n - (long)old_n;
min_pos = (size_t)pos + new_n;
if (touches_end) {
if (new_no_nl)
final_nl = 0;
else if (old_no_nl)
final_nl = 1;
}
count++;
}
if (count == 0) {
snprintf(err, err_sz, "no @@ hunk found");
goto done;
}
{
size_t total = 1;
for (size_t k = 0; k < file.n; k++)
total += file.v[k].n + 1;
char *buf = malloc(total);
if (!buf) {
snprintf(err, err_sz, "oom");
goto done;
}
size_t o = 0;
for (size_t k = 0; k < file.n; k++) {
memcpy(buf + o, file.v[k].p, file.v[k].n);
o += file.v[k].n;
if (k + 1 < file.n || final_nl)
buf[o++] = '\n';
}
buf[o] = 0;
*out = buf;
if (out_len)
*out_len = o;
}
if (nhunks)
*nhunks = count;
rc = 0;
done:
free(file.v);
free(hl.v);
free(old_seq.v);
free(new_seq.v);
return rc;
}
+342
View File
@@ -0,0 +1,342 @@
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "cst_tools.h"
#include "cst_util.h"
#include "tools_internal.h"
static int run_read_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
return cst_tool_read_file2(ctx, c, r);
}
static int run_write_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
return cst_tool_write_file(ctx->workspace_root, c, r);
}
static int run_apply_patch(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
return cst_tool_apply_patch(ctx->workspace_root, c, r);
}
static int run_shell(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
return cst_tool_run_shell(ctx->workspace_root, c, r);
}
#define S "string"
#define I "integer"
#define B "boolean"
static const cst_tool_def_t TOOLS[] = {
{"read_file",
"Read a UTF-8 text file. For big files pass offset (first line, "
"1-based) and limit (number of lines); line_numbers=true prefixes each "
"line with its number.",
"Read a UTF-8 text file under the workspace.",
{{"path", S, 1, "file path relative to the workspace"},
{"offset", I, 0, "first line to return (1-based)"},
{"limit", I, 0, "maximum number of lines"},
{"line_numbers", B, 0, "prefix lines with their numbers"}},
CST_TP_READ, 1, run_read_file, NULL},
{"run_shell",
"Run a non-interactive shell command in the workspace root (stdin is "
"empty; long-running commands are killed). Returns stdout+stderr.",
"Run a shell command in the workspace root (non-interactive, stdin is "
"empty, killed after a timeout). Returns combined stdout+stderr.",
{{"cmd", S, 1, "shell command"}},
CST_TP_SHELL, 1, run_shell, NULL},
{"write_file",
"Create or overwrite a whole file (parent directories are created).",
"Create or overwrite a whole file under the workspace (parent "
"directories are created).",
{{"path", S, 1, "file path"}, {"content", S, 1, "complete new content"}},
CST_TP_WRITE, 1, run_write_file, cst_preview_write_file},
{"apply_patch",
"Apply a unified diff (---/+++ headers, @@ -a,b +c,d @@ hunks with 3 "
"context lines) to one or more files; all hunks apply or nothing "
"changes.",
"Apply a unified diff (---/+++ headers, @@ -a,b +c,d @@ hunks with "
"context lines) to one or more files. All hunks must apply or nothing "
"changes.",
{{"patch", S, 1, "unified diff text"}},
CST_TP_WRITE, 1, run_apply_patch, cst_preview_apply_patch},
{"edit_file",
"Replace an exact snippet in a file. old_string must match the file "
"byte-for-byte (including indentation) and be unique unless "
"replace_all=true. Best for small edits.",
NULL,
{{"path", S, 1, "file path"},
{"old_string", S, 1, "exact text to replace"},
{"new_string", S, 1, "replacement text"},
{"replace_all", B, 0, "replace every occurrence"}},
CST_TP_WRITE, 0, cst_tool_edit_file, cst_preview_edit_file},
{"list_dir",
"List a directory as a tree (directories end with /). Skips .git, "
"node_modules, .agent and build output.",
NULL,
{{"path", S, 0, "directory (default: workspace root)"},
{"depth", I, 0, "levels to descend (default 2, max 6)"}},
CST_TP_READ, 0, cst_tool_list_dir, NULL},
{"glob",
"Find files by name pattern, newest first. `*` and `?` stay within one "
"path segment, `**` spans directories (`src/**/*.c`); a pattern "
"without / matches file names at any depth.",
NULL,
{{"pattern", S, 1, "glob relative to path"},
{"path", S, 0, "directory to search (default: workspace root)"}},
CST_TP_READ, 0, cst_tool_glob, NULL},
{"grep",
"Search file contents with a POSIX extended regex. Returns "
"file:line:text matches.",
NULL,
{{"pattern", S, 1, "extended regular expression"},
{"path", S, 0, "file or directory (default: workspace root)"},
{"glob", S, 0, "only files whose name matches, e.g. *.c"},
{"ignore_case", B, 0, "case-insensitive"},
{"context", I, 0, "lines of context around each match (max 5)"}},
CST_TP_READ, 0, cst_tool_grep, NULL},
{"delete_file",
"Delete a file (not directories).",
NULL,
{{"path", S, 1, "file path"}},
CST_TP_WRITE, 0, cst_tool_delete_file, cst_preview_delete_file},
{"move_file",
"Move or rename a file; fails if the destination exists.",
NULL,
{{"from", S, 1, "existing file"}, {"to", S, 1, "new path"}},
CST_TP_WRITE, 0, cst_tool_move_file, cst_preview_move_file},
{"todo_write",
"Replace your task list for multi-step work. One item per line: "
"`[ ] todo`, `[~] in progress`, `[x] done`.",
NULL,
{{"todos", S, 1, "the whole list, one item per line"}},
CST_TP_META, 0, cst_tool_todo_write, NULL},
{"web_fetch",
"Fetch an http(s) URL and return its text (HTML is reduced to text).",
NULL,
{{"url", S, 1, "http:// or https:// URL"},
{"max_bytes", I, 0, "maximum bytes to return (default 65536)"}},
CST_TP_NET, 0, cst_tool_web_fetch, NULL},
{"ask_user",
"Ask the user a question and wait for the answer. Use only for "
"decisions that are truly the user's.",
NULL,
{{"question", S, 1, "the question"},
{"options", S, 0, "suggested answers, one per line"}},
CST_TP_ASK, 0, cst_tool_ask_user, NULL},
};
#define NTOOLS ((int)(sizeof(TOOLS) / sizeof(TOOLS[0])))
#undef S
#undef I
#undef B
int cst_tool_profile_parse(const char *s, int *out) {
if (!s || !out)
return -1;
if (strcmp(s, "default") == 0 || strcmp(s, "full") == 0) {
*out = CST_TOOLS_DEFAULT;
return 0;
}
if (strcmp(s, "minimal") == 0) {
*out = CST_TOOLS_MINIMAL;
return 0;
}
return -1;
}
const char *cst_tool_profile_name(int p) {
return p == CST_TOOLS_MINIMAL ? "minimal" : "default";
}
int cst_tool_count(void) { return NTOOLS; }
const cst_tool_def_t *cst_tool_at(int i) {
return i >= 0 && i < NTOOLS ? &TOOLS[i] : NULL;
}
int cst_tool_index(const char *name) {
if (!name)
return -1;
for (int i = 0; i < NTOOLS; i++) {
if (strcmp(TOOLS[i].name, name) == 0)
return i;
}
return -1;
}
const cst_tool_def_t *cst_tool_find(const char *name) {
return cst_tool_at(cst_tool_index(name));
}
int cst_tool_known(const char *name) { return cst_tool_find(name) != NULL; }
int cst_tool_is_shell(const char *name) {
const cst_tool_def_t *t = cst_tool_find(name);
return t && t->perm == CST_TP_SHELL;
}
int cst_tool_is_write(const char *name) {
const cst_tool_def_t *t = cst_tool_find(name);
return t && t->perm == CST_TP_WRITE;
}
int cst_tool_active(const cst_agent_config_t *cfg, const cst_tool_def_t *t) {
if (!t)
return 0;
if (!cfg)
return 1;
if (cfg->tool_profile == CST_TOOLS_MINIMAL && !t->minimal)
return 0;
int i = (int)(t - TOOLS);
return !(cfg->tools_off & (1ULL << i));
}
static int is_minimal(const cst_agent_config_t *cfg) {
return cfg && cfg->tool_profile == CST_TOOLS_MINIMAL;
}
void *cst_tools_schema_cfg(const cst_agent_config_t *cfg) {
cJSON *arr = cJSON_CreateArray();
if (!arr)
return NULL;
int legacy = is_minimal(cfg);
for (int i = 0; i < NTOOLS; i++) {
const cst_tool_def_t *t = &TOOLS[i];
if (!cst_tool_active(cfg, t))
continue;
cJSON *tool = cJSON_CreateObject();
cJSON_AddStringToObject(tool, "type", "function");
cJSON *fn = cJSON_AddObjectToObject(tool, "function");
cJSON_AddStringToObject(fn, "name", t->name);
cJSON_AddStringToObject(fn, "description",
legacy && t->min_desc ? t->min_desc : t->desc);
cJSON *params = cJSON_AddObjectToObject(fn, "parameters");
cJSON_AddStringToObject(params, "type", "object");
cJSON *props = cJSON_AddObjectToObject(params, "properties");
cJSON *req = cJSON_AddArrayToObject(params, "required");
for (int k = 0; k < CST_TOOL_MAX_PARAMS && t->params[k].name; k++) {
const cst_tool_param_t *pp = &t->params[k];
if (legacy && !pp->required)
continue;
cJSON *p = cJSON_AddObjectToObject(props, pp->name);
cJSON_AddStringToObject(p, "type", pp->type);
if (!legacy && pp->desc)
cJSON_AddStringToObject(p, "description", pp->desc);
if (pp->required)
cJSON_AddItemToArray(req, cJSON_CreateString(pp->name));
}
cJSON_AddItemToArray(arr, tool);
}
return arr;
}
void *cst_tools_schema(void) { return cst_tools_schema_cfg(NULL); }
char *cst_tools_prompt(const cst_agent_config_t *cfg) {
cst_buf_t b = {0};
cst_buf_puts(&b, "Tools:\n");
for (int i = 0; i < NTOOLS; i++) {
const cst_tool_def_t *t = &TOOLS[i];
if (!cst_tool_active(cfg, t))
continue;
cst_buf_printf(&b, "- %s(", t->name);
for (int k = 0; k < CST_TOOL_MAX_PARAMS && t->params[k].name; k++)
cst_buf_printf(&b, "%s%s%s", k ? ", " : "", t->params[k].name,
t->params[k].required ? "" : "?");
cst_buf_printf(&b, "): %s\n", t->desc);
}
return cst_buf_steal(&b);
}
char *cst_tools_names(const cst_agent_config_t *cfg) {
cst_buf_t b = {0};
int n = 0;
for (int i = 0; i < NTOOLS; i++) {
if (cst_tool_active(cfg, &TOOLS[i]))
cst_buf_printf(&b, "%s%s", n++ ? ", " : "", TOOLS[i].name);
}
if (!b.buf)
cst_buf_puts(&b, "");
return cst_buf_steal(&b);
}
static int parse_int_value(const char *v, long *out) {
while (*v == ' ' || *v == '"' || *v == '\'')
v++;
char *end = NULL;
long x = strtol(v, &end, 10);
if (end == v)
return -1;
while (*end == ' ' || *end == '"' || *end == '\'' || *end == '\n')
end++;
if (*end)
return -1;
*out = x;
return 0;
}
int cst_tool_validate(const cst_tool_def_t *t, const ctl_tool_call_t *call,
char *err, size_t err_sz) {
for (int k = 0; k < CST_TOOL_MAX_PARAMS && t->params[k].name; k++) {
const cst_tool_param_t *p = &t->params[k];
const char *v = tool_arg(call, p->name);
if (!v && strcmp(t->name, "run_shell") == 0)
v = tool_arg(call, "command");
if (!v) {
if (p->required) {
snprintf(err, err_sz, "error: %s needs argument \"%s\"", t->name,
p->name);
return -1;
}
continue;
}
long x;
if (strcmp(p->type, "integer") == 0 && v[0] && parse_int_value(v, &x)) {
snprintf(err, err_sz, "error: %s: argument \"%s\" must be an integer",
t->name, p->name);
return -1;
}
}
return 0;
}
int cst_tool_run(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call,
cst_tool_result_t *r) {
if (!ctx || !call || !r)
return -1;
memset(r, 0, sizeof(*r));
const cst_tool_def_t *t = cst_tool_find(call->name);
if (!t)
return tool_set_resultf(r, 1, "error: unknown tool: %.64s", call->name);
char err[256];
if (cst_tool_validate(t, call, err, sizeof(err)) != 0)
return tool_set_result(r, err, 1);
return t->run(ctx, call, r);
}
int cst_tool_preview(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call,
char **out) {
*out = NULL;
const cst_tool_def_t *t = cst_tool_find(call->name);
if (!t || !t->preview)
return -1;
char err[256];
if (cst_tool_validate(t, call, err, sizeof(err)) != 0) {
*out = strdup(err);
return -1;
}
return t->preview(ctx, call, out);
}
int cst_tool_dispatch(const char *workspace_root, const ctl_tool_call_t *call,
cst_tool_result_t *r) {
cst_tool_ctx_t ctx = {0};
ctx.workspace_root = workspace_root;
return cst_tool_run(&ctx, call, r);
}
+544 -635
View File
File diff suppressed because it is too large Load Diff
+63
View File
@@ -0,0 +1,63 @@
#ifndef CST_TOOLS_INTERNAL_H
#define CST_TOOLS_INTERNAL_H
#include "cst_tools.h"
#include "cst_util.h"
#define READ_FILE_MAX (2 * 1024 * 1024)
/* Value of key, or NULL when absent. */
const char *tool_arg(const ctl_tool_call_t *call, const char *key);
const char *tool_arg_n(const ctl_tool_call_t *call, const char *key,
size_t *len);
/* Integer arg, def when absent or empty. */
long tool_arg_long(const ctl_tool_call_t *call, const char *key, long def);
int tool_arg_bool(const ctl_tool_call_t *call, const char *key, int def);
int tool_set_result(cst_tool_result_t *r, const char *s, int code);
int tool_set_resultf(cst_tool_result_t *r, int code, const char *fmt, ...);
int tool_take_buf(cst_tool_result_t *r, cst_buf_t *b, int code);
/* Whole file (NULL when missing, unreadable or larger than max>0). */
char *tool_slurp(const char *full, size_t *out_len, size_t max);
int tool_write_whole(const char *full, const char *data, size_t n);
void tool_ensure_parent_dirs(const char *workspace_root, const char *rel);
void tool_append_diff(cst_buf_t *b, const char *relpath, const char *oldc,
size_t olen, const char *newc, size_t nlen);
int tool_is_binary(const char *s, size_t n);
/* Workspace-relative display form of an absolute resolved path. */
const char *tool_rel(const char *workspace_real, const char *full);
int cst_tool_read_file2(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_list_dir(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_glob(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_grep(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_edit_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_delete_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_move_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_todo_write(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_ask_user(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_tool_web_fetch(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r);
int cst_preview_write_file(const cst_tool_ctx_t *ctx,
const ctl_tool_call_t *c, char **out);
int cst_preview_apply_patch(const cst_tool_ctx_t *ctx,
const ctl_tool_call_t *c, char **out);
int cst_preview_edit_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
char **out);
int cst_preview_delete_file(const cst_tool_ctx_t *ctx,
const ctl_tool_call_t *c, char **out);
int cst_preview_move_file(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
char **out);
#endif
+285
View File
@@ -0,0 +1,285 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <curl/curl.h>
#include "cst_tools.h"
#include "cst_util.h"
#include "tools_internal.h"
#define FETCH_DOWNLOAD_MAX (5 * 1024 * 1024)
#define FETCH_DEFAULT_OUT (64 * 1024)
#define FETCH_MAX_OUT (1024 * 1024)
typedef struct {
cst_buf_t body;
int overflow;
} fetch_t;
static size_t on_body(char *p, size_t sz, size_t n, void *ud) {
fetch_t *f = ud;
size_t len = sz * n;
if (f->body.len + len > FETCH_DOWNLOAD_MAX) {
f->overflow = 1;
return 0; /* abort */
}
cst_buf_append(&f->body, p, len);
return len;
}
static int tag_is(const char *p, const char *name) {
size_t n = strlen(name);
if (strncasecmp(p, name, n) != 0)
return 0;
char c = p[n];
return c == '>' || c == ' ' || c == '/' || c == '\t' || c == '\n' ||
c == '\r';
}
static const char *const BLOCK_TAGS[] = {
"p", "br", "div", "li", "tr", "h1", "h2", "h3", "h4", "h5", "h6",
"section", "article", "header", "footer", "pre", "table", "ul", "ol",
"blockquote", "hr"};
static void put_entity(cst_buf_t *b, const char **pp, const char *end) {
static const struct {
const char *name;
const char *rep;
} ents[] = {{"&amp;", "&"}, {"&lt;", "<"}, {"&gt;", ">"},
{"&quot;", "\""}, {"&#39;", "'"}, {"&apos;", "'"},
{"&nbsp;", " "}};
const char *p = *pp;
for (size_t i = 0; i < sizeof(ents) / sizeof(ents[0]); i++) {
size_t n = strlen(ents[i].name);
if ((size_t)(end - p) >= n && strncmp(p, ents[i].name, n) == 0) {
cst_buf_puts(b, ents[i].rep);
*pp = p + n;
return;
}
}
if (end - p > 3 && p[1] == '#') {
char *e = NULL;
long cp = p[2] == 'x' || p[2] == 'X' ? strtol(p + 3, &e, 16)
: strtol(p + 2, &e, 10);
if (e && *e == ';' && cp > 0 && cp < 0x110000) {
char u[4];
int k = 0;
if (cp < 0x80) {
u[k++] = (char)cp;
} else if (cp < 0x800) {
u[k++] = (char)(0xC0 | (cp >> 6));
u[k++] = (char)(0x80 | (cp & 0x3F));
} else if (cp < 0x10000) {
u[k++] = (char)(0xE0 | (cp >> 12));
u[k++] = (char)(0x80 | ((cp >> 6) & 0x3F));
u[k++] = (char)(0x80 | (cp & 0x3F));
} else {
u[k++] = (char)(0xF0 | (cp >> 18));
u[k++] = (char)(0x80 | ((cp >> 12) & 0x3F));
u[k++] = (char)(0x80 | ((cp >> 6) & 0x3F));
u[k++] = (char)(0x80 | (cp & 0x3F));
}
cst_buf_append(b, u, (size_t)k);
*pp = e + 1;
return;
}
}
cst_buf_append(b, p, 1);
*pp = p + 1;
}
/* Crude HTML → text: drop script/style/head, tags become spaces or
* newlines, entities decoded, blank runs collapsed. */
static char *html_to_text(const char *s, size_t n) {
cst_buf_t b = {0};
const char *p = s, *end = s + n;
for (const char *t = s; t + 7 < end; t++) {
if (*t == '<' && strncasecmp(t + 1, "title", 5) == 0) {
const char *gt = memchr(t, '>', (size_t)(end - t));
const char *e = gt ? gt + 1 : end;
while (e + 7 < end && strncasecmp(e, "</title", 7) != 0)
e++;
if (gt && e + 7 < end && e > gt + 1) {
cst_buf_puts(&b, "# ");
cst_buf_append(&b, gt + 1, (size_t)(e - gt - 1));
cst_buf_puts(&b, "\n\n");
}
break;
}
}
while (p < end) {
if (*p == '<') {
const char *q = p + 1;
int closing = *q == '/';
if (closing)
q++;
if (!closing && (tag_is(q, "script") || tag_is(q, "style") ||
tag_is(q, "head") || tag_is(q, "noscript"))) {
const char *name = tag_is(q, "script") ? "</script"
: tag_is(q, "style") ? "</style"
: tag_is(q, "head") ? "</head"
: "</noscript";
const char *e = p;
size_t nl = strlen(name);
while (e < end && !(end - e >= (long)nl && strncasecmp(e, name, nl) == 0))
e++;
const char *gt = e < end ? memchr(e, '>', (size_t)(end - e)) : NULL;
p = gt ? gt + 1 : end;
continue;
}
if (end - p >= 4 && strncmp(p, "<!--", 4) == 0) {
const char *e = p + 4;
while (e + 2 < end && strncmp(e, "-->", 3) != 0)
e++;
p = e + 3 <= end ? e + 3 : end;
continue;
}
int block = 0;
for (size_t i = 0; i < sizeof(BLOCK_TAGS) / sizeof(BLOCK_TAGS[0]); i++)
if (tag_is(q, BLOCK_TAGS[i]))
block = 1;
const char *gt = memchr(p, '>', (size_t)(end - p));
p = gt ? gt + 1 : end;
cst_buf_puts(&b, block ? "\n" : " ");
continue;
}
if (*p == '&') {
put_entity(&b, &p, end);
continue;
}
cst_buf_append(&b, p, 1);
p++;
}
/* collapse whitespace: spaces within lines, at most one blank line */
cst_buf_t o = {0};
int nls = 0, sp = 0;
for (size_t i = 0; i < b.len; i++) {
char ch = b.buf[i];
if (ch == '\r')
continue;
if (ch == '\n') {
if (nls < 2 && o.len)
cst_buf_puts(&o, "\n");
nls++;
sp = 0;
continue;
}
if (ch == ' ' || ch == '\t') {
sp = 1;
continue;
}
if (sp && nls == 0 && o.len)
cst_buf_puts(&o, " ");
sp = 0;
nls = 0;
cst_buf_append(&o, &ch, 1);
}
cst_buf_free(&b);
if (!o.buf)
cst_buf_puts(&o, "");
return cst_buf_steal(&o);
}
static int textual(const char *ctype) {
if (!ctype || !ctype[0])
return 1;
return strncasecmp(ctype, "text/", 5) == 0 || strstr(ctype, "json") ||
strstr(ctype, "xml") || strstr(ctype, "javascript") ||
strstr(ctype, "yaml") || strstr(ctype, "toml");
}
int cst_tool_web_fetch(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
(void)ctx;
const char *url = tool_arg(c, "url");
if (!url || !url[0])
return tool_set_result(r, "error: missing url", 1);
while (*url == ' ')
url++;
if (strncasecmp(url, "http://", 7) != 0 &&
strncasecmp(url, "https://", 8) != 0)
return tool_set_result(r, "error: only http:// and https:// URLs", 1);
long max_out = tool_arg_long(c, "max_bytes", FETCH_DEFAULT_OUT);
if (max_out <= 0)
max_out = FETCH_DEFAULT_OUT;
if (max_out > FETCH_MAX_OUT)
max_out = FETCH_MAX_OUT;
CURL *curl = curl_easy_init();
if (!curl)
return tool_set_result(r, "error: curl init failed", 1);
fetch_t f;
memset(&f, 0, sizeof(f));
char errbuf[CURL_ERROR_SIZE];
errbuf[0] = 0;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);
#if LIBCURL_VERSION_NUM >= 0x075500
curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, "http,https");
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "http,https");
#else
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, (long)(CURLPROTO_HTTP | CURLPROTO_HTTPS));
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS,
(long)(CURLPROTO_HTTP | CURLPROTO_HTTPS));
#endif
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "cst-agent/1.0 (+web_fetch)");
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, on_body);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &f);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
CURLcode rc = curl_easy_perform(curl);
long http = 0;
char *ctype = NULL;
char *final_url = NULL;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ctype);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &final_url);
cst_buf_t b = {0};
int code = 0;
if (rc != CURLE_OK && !f.overflow) {
cst_buf_printf(&b, "error: fetch failed: %s",
errbuf[0] ? errbuf : curl_easy_strerror(rc));
code = 1;
} else {
cst_buf_printf(&b, "HTTP %ld %s\ncontent-type: %s\n\n", http,
final_url ? final_url : url, ctype ? ctype : "?");
if (http >= 400)
code = 1;
if (!textual(ctype)) {
cst_buf_printf(&b, "(non-text content, %zu bytes not shown)\n",
f.body.len);
} else {
char *text = NULL;
size_t tlen = 0;
int html = ctype && strstr(ctype, "html");
if (html) {
text = html_to_text(f.body.buf ? f.body.buf : "", f.body.len);
tlen = text ? strlen(text) : 0;
}
const char *src = html ? text : (f.body.buf ? f.body.buf : "");
size_t slen = html ? tlen : f.body.len;
size_t keep = slen > (size_t)max_out
? cst_utf8_floor(src, (size_t)max_out)
: slen;
size_t clean_len = 0;
char *clean = cst_utf8_sanitize(src, keep, &clean_len);
if (clean)
cst_buf_append(&b, clean, clean_len);
free(clean);
if (keep < slen || f.overflow)
cst_buf_printf(&b, "\n...(truncated; %zu%s bytes in total)\n", slen,
f.overflow ? "+" : "");
free(text);
}
}
curl_easy_cleanup(curl);
cst_buf_free(&f.body);
return tool_take_buf(r, &b, code);
}