Extensions: skills, plugins, MCP, sub-agents; configurable keys
- Skills (SKILL.md discovery, skill tool, /skills /skill) - plugin.json exec tools with permission prompts, timeouts and workspace trust (/plugins) - MCP stdio client: JSON-RPC initialize/tools/list/tools/call, mcp__server__tool, /mcp and sidebar status - task tool with general/explore/plan and .agent/agents/*.md roles, parallel read-only runs, cancel propagation, /agents - Keymap with Ctrl+X leader, keybinds.json overrides and /keys panel - Collapsed thinking, throttled streaming Markdown, leaner tool output, parallel read-only tools - Spawn: close-on-exec pipes and a process-wide fork lock - Tests for keymap, skills, plugins, MCP, sub-agents, shell compaction; docs/plugins.md
This commit is contained in:
+333
-89
@@ -50,6 +50,93 @@ static int is_regular(const char *full) {
|
||||
|
||||
/* --- read_file ----------------------------------------------------------- */
|
||||
|
||||
#define READ_LINE_MAX 2000
|
||||
|
||||
/* Default-profile read: "path · lines a-b of N" header, `N|` numbers,
|
||||
* long lines cut, and the next offset when more remains. */
|
||||
static int read_compact(FILE *fp, const char *path, long offset, long limit,
|
||||
int numbers, cst_tool_result_t *r) {
|
||||
if (offset <= 0)
|
||||
offset = 1;
|
||||
if (limit <= 0)
|
||||
limit = RANGE_DEFAULT_LINES;
|
||||
cst_buf_t b = {0}, line = {0};
|
||||
long lineno = 0, shown = 0, cut_lines = 0;
|
||||
int full = 0, binary = 0, ch;
|
||||
for (;;) {
|
||||
line.len = 0;
|
||||
int got = 0, cr = 0;
|
||||
size_t len = 0;
|
||||
while ((ch = fgetc(fp)) != EOF) {
|
||||
got = 1;
|
||||
if (ch == 0)
|
||||
binary = 1;
|
||||
if (ch == '\n')
|
||||
break;
|
||||
len++;
|
||||
cr = ch == '\r';
|
||||
if (line.len < READ_LINE_MAX + 8) {
|
||||
char cc = (char)ch;
|
||||
cst_buf_append(&line, &cc, 1);
|
||||
}
|
||||
}
|
||||
if (!got || binary)
|
||||
break;
|
||||
lineno++;
|
||||
if (lineno < offset || shown >= limit || full)
|
||||
continue;
|
||||
len -= (size_t)cr;
|
||||
if (b.len + (len < READ_LINE_MAX ? len : READ_LINE_MAX) > RANGE_OUT_MAX) {
|
||||
full = 1;
|
||||
continue;
|
||||
}
|
||||
if (numbers)
|
||||
cst_buf_printf(&b, "%ld|", lineno);
|
||||
if (len > READ_LINE_MAX) {
|
||||
size_t keep = cst_utf8_floor(line.buf, READ_LINE_MAX);
|
||||
cst_buf_append(&b, line.buf, keep);
|
||||
cst_buf_printf(&b, " … [line cut, +%zu bytes]\n", len - keep);
|
||||
cut_lines++;
|
||||
} else {
|
||||
if (len)
|
||||
cst_buf_append(&b, line.buf, len);
|
||||
cst_buf_puts(&b, "\n");
|
||||
}
|
||||
shown++;
|
||||
}
|
||||
cst_buf_free(&line);
|
||||
if (binary) {
|
||||
cst_buf_free(&b);
|
||||
return tool_set_resultf(r, 1,
|
||||
"error: %.200s is a binary file; inspect it with "
|
||||
"run_shell (file, xxd | head)",
|
||||
path);
|
||||
}
|
||||
long last = offset + shown - 1;
|
||||
cst_buf_t out = {0};
|
||||
if (lineno == 0)
|
||||
cst_buf_printf(&out, "%s · empty file\n", path);
|
||||
else if (shown == 0)
|
||||
cst_buf_printf(&out, "%s · %ld line%s; offset %ld is past the end\n", path,
|
||||
lineno, lineno == 1 ? "" : "s", offset);
|
||||
else if (offset == 1 && last == lineno)
|
||||
cst_buf_printf(&out, "%s · %ld line%s\n", path, lineno,
|
||||
lineno == 1 ? "" : "s");
|
||||
else
|
||||
cst_buf_printf(&out, "%s · lines %ld-%ld of %ld\n", path, offset, last,
|
||||
lineno);
|
||||
if (b.buf)
|
||||
cst_buf_append(&out, b.buf, b.len);
|
||||
cst_buf_free(&b);
|
||||
if (shown && last < lineno)
|
||||
cst_buf_printf(&out, "… %ld more line%s; continue with offset=%ld\n",
|
||||
lineno - last, lineno - last == 1 ? "" : "s", last + 1);
|
||||
if (cut_lines)
|
||||
cst_buf_printf(&out, "(%ld long line%s cut at %d bytes)\n", cut_lines,
|
||||
cut_lines == 1 ? "" : "s", READ_LINE_MAX);
|
||||
return tool_take_buf(r, &out, 0);
|
||||
}
|
||||
|
||||
int cst_tool_read_file2(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r) {
|
||||
const char *path = tool_arg(c, "path");
|
||||
@@ -70,8 +157,13 @@ int cst_tool_read_file2(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
|
||||
long offset = tool_arg_long(c, "offset", 0);
|
||||
long limit = tool_arg_long(c, "limit", 0);
|
||||
int numbers = tool_arg_bool(c, "line_numbers", 0);
|
||||
int numbers = tool_arg_bool(c, "line_numbers", ctx->compact);
|
||||
|
||||
if (ctx->compact) {
|
||||
int rc = read_compact(fp, path, offset, limit, numbers, r);
|
||||
fclose(fp);
|
||||
return rc;
|
||||
}
|
||||
if (offset <= 0 && limit <= 0 && !numbers) {
|
||||
fclose(fp);
|
||||
size_t n = 0;
|
||||
@@ -168,6 +260,87 @@ static size_t count_occ(const char *hay, size_t hn, const char *needle,
|
||||
return cnt;
|
||||
}
|
||||
|
||||
/* "12, 40, 77" — line numbers of the first few occurrences. */
|
||||
static void occ_lines(const char *hay, size_t hn, const char *nd, size_t nn,
|
||||
char *out, size_t out_sz) {
|
||||
out[0] = 0;
|
||||
size_t used = 0;
|
||||
long line = 1;
|
||||
size_t last = 0;
|
||||
int k = 0;
|
||||
for (size_t i = 0; nn && i + nn <= hn && k < 6;) {
|
||||
const char *p = memchr(hay + i, nd[0], hn - i - nn + 1);
|
||||
if (!p)
|
||||
break;
|
||||
i = (size_t)(p - hay);
|
||||
if (memcmp(p, nd, nn) != 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
for (size_t j = last; j < i; j++)
|
||||
line += hay[j] == '\n';
|
||||
last = i;
|
||||
int w = snprintf(out + used, out_sz - used, "%s%ld", k ? ", " : "", line);
|
||||
if (w < 0 || (size_t)w >= out_sz - used)
|
||||
break;
|
||||
used += (size_t)w;
|
||||
k++;
|
||||
i += nn;
|
||||
}
|
||||
}
|
||||
|
||||
static int trimmed_eq(const char *a, size_t an, const char *b, size_t bn) {
|
||||
while (an && isspace((unsigned char)*a))
|
||||
a++, an--;
|
||||
while (an && isspace((unsigned char)a[an - 1]))
|
||||
an--;
|
||||
while (bn && isspace((unsigned char)*b))
|
||||
b++, bn--;
|
||||
while (bn && isspace((unsigned char)b[bn - 1]))
|
||||
bn--;
|
||||
return an && an == bn && memcmp(a, b, an) == 0;
|
||||
}
|
||||
|
||||
/* old_string was not found: lines whose trimmed text equals old_string's
|
||||
* first non-blank line, as "42, 97" (empty when none). */
|
||||
static void near_lines(const char *hay, size_t hn, const char *old, size_t on,
|
||||
char *out, size_t out_sz) {
|
||||
out[0] = 0;
|
||||
const char *f = old;
|
||||
size_t fl = 0;
|
||||
for (size_t i = 0; i < on;) {
|
||||
const char *e = memchr(old + i, '\n', on - i);
|
||||
size_t end = e ? (size_t)(e - old) : on;
|
||||
int blank = 1;
|
||||
for (size_t j = i; j < end; j++)
|
||||
if (!isspace((unsigned char)old[j]))
|
||||
blank = 0;
|
||||
if (!blank) {
|
||||
f = old + i;
|
||||
fl = end - i;
|
||||
break;
|
||||
}
|
||||
i = end + 1;
|
||||
}
|
||||
if (!fl)
|
||||
return;
|
||||
size_t used = 0;
|
||||
long line = 1;
|
||||
int k = 0;
|
||||
for (size_t i = 0; i < hn && k < 5; line++) {
|
||||
const char *e = memchr(hay + i, '\n', hn - i);
|
||||
size_t end = e ? (size_t)(e - hay) : hn;
|
||||
if (trimmed_eq(hay + i, end - i, f, fl)) {
|
||||
int w = snprintf(out + used, out_sz - used, "%s%ld", k ? ", " : "", line);
|
||||
if (w < 0 || (size_t)w >= out_sz - used)
|
||||
break;
|
||||
used += (size_t)w;
|
||||
k++;
|
||||
}
|
||||
i = end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static char *replace_all(const char *hay, size_t hn, const char *old,
|
||||
size_t on, const char *nw, size_t nwn, int all,
|
||||
size_t *out_len) {
|
||||
@@ -272,17 +445,29 @@ static int plan_edit(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
}
|
||||
int rc = 0;
|
||||
if (cnt == 0) {
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string not found in %.200s; it must match the file "
|
||||
"exactly (whitespace and indentation included) — re-read the "
|
||||
"file and copy the text",
|
||||
path);
|
||||
char near[64];
|
||||
near_lines(pl->oldc, pl->olen, old, on, near, sizeof(near));
|
||||
if (near[0])
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string not found in %.200s, but its first line "
|
||||
"appears (whitespace aside) at line %s — read_file around "
|
||||
"there and copy the exact text, indentation included",
|
||||
path, near);
|
||||
else
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string not found in %.200s; it must match the file "
|
||||
"exactly (whitespace and indentation included) — re-read the "
|
||||
"file (or grep for a distinctive word) and copy the text",
|
||||
path);
|
||||
rc = -1;
|
||||
} else if (cnt > 1 && !all) {
|
||||
char where[64];
|
||||
occ_lines(pl->oldc, pl->olen, old, on, where, sizeof(where));
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string matches %zu places in %.200s; include more "
|
||||
"surrounding lines to make it unique, or set replace_all=true",
|
||||
cnt, path);
|
||||
"error: old_string matches %zu places in %.200s (lines %s%s); "
|
||||
"include more surrounding lines to make it unique, or set "
|
||||
"replace_all=true",
|
||||
cnt, path, where, cnt > 6 ? ", …" : "");
|
||||
rc = -1;
|
||||
} else {
|
||||
pl->newc = replace_all(pl->oldc, pl->olen, old, on, nw, nwn, all,
|
||||
@@ -665,29 +850,54 @@ static int start_rel(const cst_tool_ctx_t *ctx, const char *path,
|
||||
}
|
||||
|
||||
/* list_dir */
|
||||
#define LIST_COUNT_MAX 100000
|
||||
|
||||
typedef struct {
|
||||
cst_buf_t b;
|
||||
int count;
|
||||
int base_depth;
|
||||
int truncated;
|
||||
int more; /* entries past LIST_MAX (counted, not shown) */
|
||||
int truncated; /* stopped counting */
|
||||
} list_ud_t;
|
||||
|
||||
/* Entries directly inside a directory (for the depth boundary). */
|
||||
static int dir_entries(const char *full) {
|
||||
DIR *d = opendir(full);
|
||||
if (!d)
|
||||
return -1;
|
||||
int n = 0;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
|
||||
n++;
|
||||
}
|
||||
closedir(d);
|
||||
return n;
|
||||
}
|
||||
|
||||
static int list_cb(walker_t *w, const char *rel, const char *full,
|
||||
const struct stat *st, int depth) {
|
||||
(void)full;
|
||||
list_ud_t *u = w->ud;
|
||||
if (u->count >= LIST_MAX) {
|
||||
u->truncated = 1;
|
||||
return 1;
|
||||
if (++u->more >= LIST_COUNT_MAX) {
|
||||
u->truncated = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
const char *name = strrchr(rel, '/');
|
||||
name = name ? name + 1 : rel;
|
||||
for (int i = 0; i < depth; i++)
|
||||
cst_buf_puts(&u->b, " ");
|
||||
if (S_ISDIR(st->st_mode))
|
||||
cst_buf_printf(&u->b, "%s/%s\n", name,
|
||||
skip_dir_name(name) ? " (skipped)" : "");
|
||||
else if (S_ISLNK(st->st_mode))
|
||||
if (S_ISDIR(st->st_mode)) {
|
||||
int k = -1;
|
||||
if (skip_dir_name(name))
|
||||
cst_buf_printf(&u->b, "%s/ (skipped)\n", name);
|
||||
else if (depth + 1 >= w->max_depth && (k = dir_entries(full)) > 0)
|
||||
cst_buf_printf(&u->b, "%s/ (%d entr%s)\n", name, k,
|
||||
k == 1 ? "y" : "ies");
|
||||
else
|
||||
cst_buf_printf(&u->b, "%s/%s\n", name, k == 0 ? " (empty)" : "");
|
||||
} else if (S_ISLNK(st->st_mode))
|
||||
cst_buf_printf(&u->b, "%s@\n", name);
|
||||
else
|
||||
cst_buf_printf(&u->b, "%s (%lld B)\n", name, (long long)st->st_size);
|
||||
@@ -726,9 +936,10 @@ int cst_tool_list_dir(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
free(w.ig.v);
|
||||
if (u.count == 0)
|
||||
cst_buf_puts(&u.b, "(empty)\n");
|
||||
if (u.truncated)
|
||||
cst_buf_printf(&u.b, "... (stopped after %d entries; list a subdirectory)\n",
|
||||
LIST_MAX);
|
||||
if (u.more)
|
||||
cst_buf_printf(&u.b, "… %s%d more entries not shown (list a subdirectory "
|
||||
"or use a smaller depth)\n",
|
||||
u.truncated ? "over " : "", u.more);
|
||||
return tool_take_buf(r, &u.b, 0);
|
||||
}
|
||||
|
||||
@@ -824,15 +1035,16 @@ static int glob_cb(walker_t *w, const char *rel, const char *full,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static glob_ud_t *g_sort_ud;
|
||||
static int idx_by_mtime(const void *a, const void *b) {
|
||||
int ia = *(const int *)a, ib = *(const int *)b;
|
||||
double d = g_sort_ud->mtimes[ib] - g_sort_ud->mtimes[ia];
|
||||
if (d > 0)
|
||||
return 1;
|
||||
if (d < 0)
|
||||
return -1;
|
||||
return strcmp(g_sort_ud->hits[ia], g_sort_ud->hits[ib]);
|
||||
typedef struct {
|
||||
const char *path;
|
||||
double mtime;
|
||||
} glob_hit_t;
|
||||
|
||||
static int hit_by_mtime(const void *a, const void *b) {
|
||||
const glob_hit_t *x = a, *y = b;
|
||||
if (x->mtime != y->mtime)
|
||||
return x->mtime < y->mtime ? 1 : -1;
|
||||
return strcmp(x->path, y->path);
|
||||
}
|
||||
|
||||
int cst_tool_glob(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
@@ -865,22 +1077,24 @@ int cst_tool_glob(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
free(w.ig.v);
|
||||
|
||||
cst_buf_t b = {0};
|
||||
int *idx = malloc((size_t)(u.n ? u.n : 1) * sizeof(int));
|
||||
if (idx) {
|
||||
for (int i = 0; i < u.n; i++)
|
||||
idx[i] = i;
|
||||
g_sort_ud = &u;
|
||||
qsort(idx, (size_t)u.n, sizeof(int), idx_by_mtime);
|
||||
g_sort_ud = NULL;
|
||||
glob_hit_t *hs = malloc((size_t)(u.n ? u.n : 1) * sizeof(*hs));
|
||||
if (hs) {
|
||||
for (int i = 0; i < u.n; i++) {
|
||||
hs[i].path = u.hits[i];
|
||||
hs[i].mtime = u.mtimes[i];
|
||||
}
|
||||
qsort(hs, (size_t)u.n, sizeof(*hs), hit_by_mtime);
|
||||
for (int i = 0; i < u.n && i < GLOB_MAX; i++)
|
||||
cst_buf_printf(&b, "%s\n", u.hits[idx[i]]);
|
||||
cst_buf_printf(&b, "%s\n", hs[i].path);
|
||||
}
|
||||
free(idx);
|
||||
free(hs);
|
||||
if (u.n == 0)
|
||||
cst_buf_printf(&b, "(no files match %s)\n", pattern);
|
||||
else if (u.n > GLOB_MAX || u.truncated)
|
||||
cst_buf_printf(&b, "... (%s%d matches; showing the %d newest)\n",
|
||||
u.truncated ? "over " : "", u.n, GLOB_MAX);
|
||||
cst_buf_printf(&b, "… %s%d more files (showing the %d newest of %s%d; "
|
||||
"narrow the pattern or path)\n",
|
||||
u.truncated ? "over " : "", u.n - GLOB_MAX, GLOB_MAX,
|
||||
u.truncated ? "over " : "", u.n);
|
||||
for (int i = 0; i < u.n; i++)
|
||||
free(u.hits[i]);
|
||||
free(u.hits);
|
||||
@@ -930,19 +1144,24 @@ int cst_workspace_files(const char *ws, char ***out, int max) {
|
||||
return u.n;
|
||||
}
|
||||
|
||||
/* grep */
|
||||
/* grep: matches grouped by file, "N matches in F files" summary. */
|
||||
#define GREP_PER_FILE 20
|
||||
#define GREP_COUNT_MAX 5000
|
||||
|
||||
typedef struct {
|
||||
regex_t re;
|
||||
const char *glob;
|
||||
int context;
|
||||
cst_buf_t b;
|
||||
int matches;
|
||||
int files;
|
||||
int truncated;
|
||||
int matches; /* counted (up to GREP_COUNT_MAX) */
|
||||
int shown;
|
||||
int files; /* text files searched */
|
||||
int hit_files; /* files with a match */
|
||||
int truncated; /* stopped counting */
|
||||
} grep_ud_t;
|
||||
|
||||
static void put_line(cst_buf_t *b, const char *rel, long ln, char sep,
|
||||
const char *s, size_t n) {
|
||||
static void put_line(cst_buf_t *b, long ln, char sep, const char *s,
|
||||
size_t n) {
|
||||
if (n && s[n - 1] == '\n')
|
||||
n--;
|
||||
if (n && s[n - 1] == '\r')
|
||||
@@ -952,11 +1171,23 @@ static void put_line(cst_buf_t *b, const char *rel, long ln, char sep,
|
||||
n = cst_utf8_floor(s, GREP_LINE_MAX);
|
||||
cut = 1;
|
||||
}
|
||||
cst_buf_printf(b, "%s%c%ld%c", rel, sep, ln, sep);
|
||||
cst_buf_printf(b, "%ld%c", ln, sep);
|
||||
cst_buf_append(b, s, n);
|
||||
cst_buf_puts(b, cut ? " …\n" : "\n");
|
||||
}
|
||||
|
||||
static int line_hit(grep_ud_t *u, const char *s, size_t n) {
|
||||
char tmp[GREP_LINE_MAX * 8];
|
||||
size_t m = n < sizeof(tmp) - 1 ? n : sizeof(tmp) - 1;
|
||||
memcpy(tmp, s, m);
|
||||
tmp[m] = 0;
|
||||
if (m && tmp[m - 1] == '\n')
|
||||
tmp[--m] = 0;
|
||||
if (m && tmp[m - 1] == '\r')
|
||||
tmp[--m] = 0;
|
||||
return regexec(&u->re, tmp, 0, NULL, 0) == 0;
|
||||
}
|
||||
|
||||
static void grep_file(grep_ud_t *u, const char *rel, const char *full) {
|
||||
size_t n = 0;
|
||||
char *buf = tool_slurp(full, &n, READ_FILE_MAX);
|
||||
@@ -996,45 +1227,48 @@ static void grep_file(grep_ud_t *u, const char *rel, const char *full) {
|
||||
nl++;
|
||||
i = end;
|
||||
}
|
||||
int last_printed = -1;
|
||||
for (int i = 0; i < nl && !u->truncated; i++) {
|
||||
char tmp[GREP_LINE_MAX * 8];
|
||||
size_t m = ln[i] < sizeof(tmp) - 1 ? ln[i] : sizeof(tmp) - 1;
|
||||
memcpy(tmp, ls[i], m);
|
||||
tmp[m] = 0;
|
||||
if (m && tmp[m - 1] == '\n')
|
||||
tmp[--m] = 0;
|
||||
if (regexec(&u->re, tmp, 0, NULL, 0) != 0)
|
||||
cst_buf_t fb = {0};
|
||||
int fm = 0, fshown = 0, last_printed = -1;
|
||||
for (int i = 0; i < nl; i++) {
|
||||
if (!line_hit(u, ls[i], ln[i]))
|
||||
continue;
|
||||
if (u->matches >= GREP_MAX_MATCHES) {
|
||||
fm++;
|
||||
if (++u->matches >= GREP_COUNT_MAX)
|
||||
u->truncated = 1;
|
||||
if (fshown < GREP_PER_FILE && u->shown < GREP_MAX_MATCHES) {
|
||||
int from = i - u->context < 0 ? 0 : i - u->context;
|
||||
if (from <= last_printed)
|
||||
from = last_printed + 1;
|
||||
else if (u->context && last_printed >= 0)
|
||||
cst_buf_puts(&fb, "--\n");
|
||||
for (int k = from; k < i; k++)
|
||||
put_line(&fb, k + 1, '-', ls[k], ln[k]);
|
||||
put_line(&fb, i + 1, ':', ls[i], ln[i]);
|
||||
last_printed = i;
|
||||
for (int k = i + 1; k <= i + u->context && k < nl; k++) {
|
||||
if (line_hit(u, ls[k], ln[k]))
|
||||
break; /* printed by the main loop */
|
||||
put_line(&fb, k + 1, '-', ls[k], ln[k]);
|
||||
last_printed = k;
|
||||
}
|
||||
fshown++;
|
||||
u->shown++;
|
||||
}
|
||||
if (u->truncated)
|
||||
break;
|
||||
}
|
||||
int from = i - u->context < 0 ? 0 : i - u->context;
|
||||
if (from <= last_printed)
|
||||
from = last_printed + 1;
|
||||
else if (u->context && last_printed >= 0)
|
||||
cst_buf_puts(&u->b, "--\n");
|
||||
for (int k = from; k < i; k++)
|
||||
put_line(&u->b, rel, k + 1, '-', ls[k], ln[k]);
|
||||
put_line(&u->b, rel, i + 1, ':', ls[i], ln[i]);
|
||||
last_printed = i;
|
||||
u->matches++;
|
||||
int to = i + u->context;
|
||||
for (int k = i + 1; k <= to && k < nl; k++) {
|
||||
/* a later matching line is printed by the main loop */
|
||||
char t2[GREP_LINE_MAX * 8];
|
||||
size_t m2 = ln[k] < sizeof(t2) - 1 ? ln[k] : sizeof(t2) - 1;
|
||||
memcpy(t2, ls[k], m2);
|
||||
t2[m2] = 0;
|
||||
if (m2 && t2[m2 - 1] == '\n')
|
||||
t2[--m2] = 0;
|
||||
if (regexec(&u->re, t2, 0, NULL, 0) == 0)
|
||||
break;
|
||||
put_line(&u->b, rel, k + 1, '-', ls[k], ln[k]);
|
||||
last_printed = k;
|
||||
}
|
||||
}
|
||||
if (fm) {
|
||||
u->hit_files++;
|
||||
if (fm == 1)
|
||||
cst_buf_printf(&u->b, "%s\n", rel);
|
||||
else
|
||||
cst_buf_printf(&u->b, "%s (%d matches)\n", rel, fm);
|
||||
if (fb.buf)
|
||||
cst_buf_append(&u->b, fb.buf, fb.len);
|
||||
if (fm > fshown)
|
||||
cst_buf_printf(&u->b, "… %d more in this file\n", fm - fshown);
|
||||
}
|
||||
cst_buf_free(&fb);
|
||||
free(ls);
|
||||
free(ln);
|
||||
free(buf);
|
||||
@@ -1137,14 +1371,24 @@ int cst_tool_grep(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
free(w.ig.v);
|
||||
}
|
||||
regfree(&u.re);
|
||||
if (u.matches == 0)
|
||||
if (u.matches == 0) {
|
||||
cst_buf_free(&u.b);
|
||||
cst_buf_printf(&u.b, "(no matches in %d file%s)\n", u.files,
|
||||
u.files == 1 ? "" : "s");
|
||||
else if (u.truncated)
|
||||
cst_buf_printf(&u.b, "... (stopped at %d matches; narrow the pattern or "
|
||||
"path)\n",
|
||||
GREP_MAX_MATCHES);
|
||||
return tool_take_buf(r, &u.b, 0);
|
||||
return tool_take_buf(r, &u.b, 0);
|
||||
}
|
||||
cst_buf_t out = {0};
|
||||
cst_buf_printf(&out, "%s%d match%s in %d file%s", u.truncated ? "over " : "",
|
||||
u.matches, u.matches == 1 ? "" : "es", u.hit_files,
|
||||
u.hit_files == 1 ? "" : "s");
|
||||
if (u.shown < u.matches)
|
||||
cst_buf_printf(&out, " (showing %d; narrow with path/glob or a tighter "
|
||||
"pattern)",
|
||||
u.shown);
|
||||
cst_buf_puts(&out, "\n");
|
||||
cst_buf_append(&out, u.b.buf, u.b.len);
|
||||
cst_buf_free(&u.b);
|
||||
return tool_take_buf(r, &out, 0);
|
||||
}
|
||||
|
||||
#else /* _WIN32: directory tools are not implemented natively */
|
||||
|
||||
+328
-59
@@ -2,9 +2,13 @@
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifndef _WIN32
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "cst_tools.h"
|
||||
@@ -25,116 +29,135 @@ static int run_apply_patch(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
}
|
||||
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_ex(ctx->workspace_root, c, r, ctx->sandbox);
|
||||
int rc = cst_tool_run_shell_ex(ctx->workspace_root, c, r, ctx->sandbox);
|
||||
if (rc == 0 && ctx->compact && r->out) {
|
||||
size_t n = 0;
|
||||
char *s = cst_shell_compact(r->out, r->out_len, &n);
|
||||
if (s) {
|
||||
free(r->out);
|
||||
r->out = s;
|
||||
r->out_len = n;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
#define S "string"
|
||||
#define I "integer"
|
||||
#define B "boolean"
|
||||
#define NO_DYN NULL, NULL, NULL, NULL, NULL
|
||||
|
||||
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 text file. Output starts with `path · lines a-b of N` and "
|
||||
"prefixes lines with `N|` (not part of the file — never copy it into "
|
||||
"edits). Returns up to 2000 lines; continue with the offset it prints.",
|
||||
"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},
|
||||
{"line_numbers", B, 0, "`N|` line prefixes (default true)"}},
|
||||
CST_TP_READ, 1, run_read_file, NULL, NO_DYN},
|
||||
{"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 non-interactive shell command in the workspace root for builds, "
|
||||
"tests, git and other CLIs (stdin is empty; long runs are killed). "
|
||||
"Returns stdout+stderr with colors stripped and long output cut to head "
|
||||
"+ tail. Prefer read_file/grep/glob/list_dir over cat/grep/find.",
|
||||
"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},
|
||||
CST_TP_SHELL, 1, run_shell, NULL, NO_DYN},
|
||||
{"write_file",
|
||||
"Create or overwrite a whole file (parent directories are created).",
|
||||
"Create a file or replace a whole file (parent directories are "
|
||||
"created). For changes to existing files prefer edit_file.",
|
||||
"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},
|
||||
CST_TP_WRITE, 1, run_write_file, cst_preview_write_file, NO_DYN},
|
||||
{"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, 3 "
|
||||
"context lines) across one or more files; all hunks apply or nothing "
|
||||
"changes. Use for multi-hunk or multi-file edits.",
|
||||
"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},
|
||||
CST_TP_WRITE, 1, run_apply_patch, cst_preview_apply_patch, NO_DYN},
|
||||
{"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.",
|
||||
"Replace an exact snippet in a file — the default for small edits. "
|
||||
"old_string must match byte-for-byte (indentation included, without "
|
||||
"read_file's `N|` prefixes) and be unique unless replace_all=true.",
|
||||
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},
|
||||
CST_TP_WRITE, 0, cst_tool_edit_file, cst_preview_edit_file, NO_DYN},
|
||||
{"list_dir",
|
||||
"List a directory as a tree (directories end with /). Skips .git, "
|
||||
"node_modules, .agent and build output.",
|
||||
"Show a directory tree (dirs end with /, files show sizes, dirs at the "
|
||||
"depth limit show entry counts). Skips .git, node_modules and "
|
||||
".gitignore'd paths.",
|
||||
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},
|
||||
CST_TP_READ, 0, cst_tool_list_dir, NULL, NO_DYN},
|
||||
{"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.",
|
||||
"Find files by name, newest first (max 200). `*`/`?` 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},
|
||||
CST_TP_READ, 0, cst_tool_glob, NULL, NO_DYN},
|
||||
{"grep",
|
||||
"Search file contents with a POSIX extended regex. Returns "
|
||||
"file:line:text matches.",
|
||||
"Search file contents with an extended regex (\\d \\w \\s work). "
|
||||
"Results are grouped by file as `line:text` under each path, with a "
|
||||
"total count first; narrow with path/glob when it is cut.",
|
||||
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},
|
||||
CST_TP_READ, 0, cst_tool_grep, NULL, NO_DYN},
|
||||
{"delete_file",
|
||||
"Delete a file (not directories).",
|
||||
"Delete one file (not directories). Undoable with /undo.",
|
||||
NULL,
|
||||
{{"path", S, 1, "file path"}},
|
||||
CST_TP_WRITE, 0, cst_tool_delete_file, cst_preview_delete_file},
|
||||
CST_TP_WRITE, 0, cst_tool_delete_file, cst_preview_delete_file, NO_DYN},
|
||||
{"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},
|
||||
CST_TP_WRITE, 0, cst_tool_move_file, cst_preview_move_file, NO_DYN},
|
||||
{"todo_write",
|
||||
"Replace your task list for multi-step work. One item per line: "
|
||||
"`[ ] todo`, `[~] in progress`, `[x] done`.",
|
||||
"Replace your task list for work with 3+ steps; keep exactly one item "
|
||||
"in progress. 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},
|
||||
CST_TP_META, 0, cst_tool_todo_write, NULL, NO_DYN},
|
||||
{"web_fetch",
|
||||
"Fetch an http(s) URL and return its text (HTML is reduced to text).",
|
||||
"Fetch an http(s) URL (docs, issues, APIs) and return its text; HTML "
|
||||
"is reduced to readable 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},
|
||||
CST_TP_NET, 0, cst_tool_web_fetch, NULL, NO_DYN},
|
||||
{"ask_user",
|
||||
"Ask the user a question and wait for the answer. Use only for "
|
||||
"decisions that are truly the user's.",
|
||||
"Ask the user and wait for the answer. Only for decisions that are "
|
||||
"truly theirs (preferences, credentials, destructive choices).",
|
||||
NULL,
|
||||
{{"question", S, 1, "the question"},
|
||||
{"options", S, 0, "suggested answers, one per line"}},
|
||||
CST_TP_ASK, 0, cst_tool_ask_user, NULL},
|
||||
CST_TP_ASK, 0, cst_tool_ask_user, NULL, NO_DYN},
|
||||
};
|
||||
#define NTOOLS ((int)(sizeof(TOOLS) / sizeof(TOOLS[0])))
|
||||
|
||||
#undef S
|
||||
#undef I
|
||||
#undef B
|
||||
#undef NO_DYN
|
||||
|
||||
int cst_tool_profile_parse(const char *s, int *out) {
|
||||
if (!s || !out)
|
||||
@@ -154,17 +177,38 @@ const char *cst_tool_profile_name(int p) {
|
||||
return p == CST_TOOLS_MINIMAL ? "minimal" : "default";
|
||||
}
|
||||
|
||||
int cst_tool_count(void) { return NTOOLS; }
|
||||
/* Dynamic tools: published append-only into a fixed table so readers on
|
||||
* other threads never see a half-built entry. */
|
||||
#define DYN_MAX 256
|
||||
static cst_tool_def_t *g_dyn[DYN_MAX];
|
||||
static atomic_int g_ndyn;
|
||||
#ifndef _WIN32
|
||||
static pthread_mutex_t g_dyn_mu = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define DYN_LOCK() pthread_mutex_lock(&g_dyn_mu)
|
||||
#define DYN_UNLOCK() pthread_mutex_unlock(&g_dyn_mu)
|
||||
#else
|
||||
#define DYN_LOCK() ((void)0)
|
||||
#define DYN_UNLOCK() ((void)0)
|
||||
#endif
|
||||
|
||||
int cst_tool_count(void) { return NTOOLS + atomic_load(&g_ndyn); }
|
||||
|
||||
const cst_tool_def_t *cst_tool_at(int i) {
|
||||
return i >= 0 && i < NTOOLS ? &TOOLS[i] : NULL;
|
||||
if (i < 0)
|
||||
return NULL;
|
||||
if (i < NTOOLS)
|
||||
return &TOOLS[i];
|
||||
i -= NTOOLS;
|
||||
return i < atomic_load(&g_ndyn) ? g_dyn[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)
|
||||
int n = cst_tool_count();
|
||||
for (int i = 0; i < n; i++) {
|
||||
const cst_tool_def_t *t = cst_tool_at(i);
|
||||
if (t && strcmp(t->name, name) == 0)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
@@ -176,6 +220,109 @@ const cst_tool_def_t *cst_tool_find(const char *name) {
|
||||
|
||||
int cst_tool_known(const char *name) { return cst_tool_find(name) != NULL; }
|
||||
|
||||
void cst_tool_sanitize_name(const char *in, char *out, size_t out_sz) {
|
||||
size_t o = 0;
|
||||
for (size_t i = 0; in && in[i] && o + 1 < out_sz && o < 63; i++) {
|
||||
char c = in[i];
|
||||
int ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '_' || c == '-';
|
||||
out[o++] = ok ? c : '_';
|
||||
}
|
||||
if (out_sz)
|
||||
out[o] = 0;
|
||||
}
|
||||
|
||||
static char *dup_or_null(const char *s) { return s ? strdup(s) : NULL; }
|
||||
|
||||
static void dyn_free(cst_tool_def_t *t) {
|
||||
if (!t)
|
||||
return;
|
||||
free((char *)t->name);
|
||||
free((char *)t->desc);
|
||||
free((char *)t->schema_json);
|
||||
free((char *)t->owner);
|
||||
free(t);
|
||||
}
|
||||
|
||||
int cst_tool_register(const cst_tool_def_t *def) {
|
||||
if (!def || !def->name || !def->name[0] || (!def->run && !def->run_ex))
|
||||
return -1;
|
||||
DYN_LOCK();
|
||||
int rc = -1;
|
||||
int n = atomic_load(&g_ndyn);
|
||||
if (n < DYN_MAX && cst_tool_index(def->name) < 0) {
|
||||
cst_tool_def_t *t = calloc(1, sizeof(*t));
|
||||
if (t) {
|
||||
*t = *def;
|
||||
t->min_desc = NULL;
|
||||
t->minimal = 0;
|
||||
t->name = dup_or_null(def->name);
|
||||
t->desc = dup_or_null(def->desc ? def->desc : "");
|
||||
t->schema_json = dup_or_null(def->schema_json);
|
||||
t->owner = dup_or_null(def->owner);
|
||||
if (t->name && t->desc) {
|
||||
g_dyn[n] = t;
|
||||
atomic_store(&g_ndyn, n + 1);
|
||||
rc = 0;
|
||||
} else {
|
||||
dyn_free(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
DYN_UNLOCK();
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_tool_unregister_owner(const char *owner) {
|
||||
if (!owner)
|
||||
return 0;
|
||||
DYN_LOCK();
|
||||
int n = atomic_load(&g_ndyn), k = 0, removed = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (g_dyn[i]->owner && strcmp(g_dyn[i]->owner, owner) == 0) {
|
||||
dyn_free(g_dyn[i]);
|
||||
removed++;
|
||||
} else {
|
||||
g_dyn[k++] = g_dyn[i];
|
||||
}
|
||||
}
|
||||
for (int i = k; i < n; i++)
|
||||
g_dyn[i] = NULL;
|
||||
atomic_store(&g_ndyn, k);
|
||||
DYN_UNLOCK();
|
||||
return removed;
|
||||
}
|
||||
|
||||
int cst_tool_disabled(const cst_agent_config_t *cfg, const char *name) {
|
||||
if (!cfg || !name || !cfg->tools_off[0])
|
||||
return 0;
|
||||
char key[CTL_MAX_NAME + 2];
|
||||
snprintf(key, sizeof(key), ",%s,", name);
|
||||
return strstr(cfg->tools_off, key) != NULL;
|
||||
}
|
||||
|
||||
void cst_tool_set_disabled(cst_agent_config_t *cfg, const char *name,
|
||||
int off) {
|
||||
if (!cfg || !name || !name[0])
|
||||
return;
|
||||
char key[CTL_MAX_NAME + 2];
|
||||
snprintf(key, sizeof(key), ",%s,", name);
|
||||
char *hit = strstr(cfg->tools_off, key);
|
||||
if (off && !hit) {
|
||||
size_t n = strlen(cfg->tools_off);
|
||||
if (n == 0)
|
||||
snprintf(cfg->tools_off, sizeof(cfg->tools_off), "%s", key);
|
||||
else if (n + strlen(key) < sizeof(cfg->tools_off))
|
||||
snprintf(cfg->tools_off + n, sizeof(cfg->tools_off) - n, "%s", key + 1);
|
||||
} else if (!off && hit) {
|
||||
/* drop ",name" keeping the trailing comma */
|
||||
size_t kl = strlen(key) - 1;
|
||||
memmove(hit, hit + kl, strlen(hit + kl) + 1);
|
||||
if (strcmp(cfg->tools_off, ",") == 0)
|
||||
cfg->tools_off[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -193,10 +340,80 @@ int cst_tool_active(const cst_agent_config_t *cfg, const cst_tool_def_t *t) {
|
||||
return 1;
|
||||
if (cfg->tool_profile == CST_TOOLS_MINIMAL && !t->minimal)
|
||||
return 0;
|
||||
if (cfg->plan_mode && (t->perm == CST_TP_WRITE || t->perm == CST_TP_SHELL))
|
||||
if ((cfg->plan_mode || cfg->read_only) &&
|
||||
(t->perm == CST_TP_WRITE || t->perm == CST_TP_SHELL))
|
||||
return 0;
|
||||
int i = (int)(t - TOOLS);
|
||||
return !(cfg->tools_off & (1ULL << i));
|
||||
if (cfg->depth > 0 && strcmp(t->name, "task") == 0)
|
||||
return 0;
|
||||
if (cfg->tools_only[0]) {
|
||||
char key[CTL_MAX_NAME + 2];
|
||||
snprintf(key, sizeof(key), ",%s,", t->name);
|
||||
if (!strstr(cfg->tools_only, key))
|
||||
return 0;
|
||||
}
|
||||
return !cst_tool_disabled(cfg, t->name);
|
||||
}
|
||||
|
||||
/* Parsed schema of a dynamic tool, or NULL. */
|
||||
static cJSON *tool_schema(const cst_tool_def_t *t) {
|
||||
if (!t->schema_json)
|
||||
return NULL;
|
||||
cJSON *s = cJSON_Parse(t->schema_json);
|
||||
if (s && !cJSON_IsObject(s)) {
|
||||
cJSON_Delete(s);
|
||||
return NULL;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static int schema_required(const cJSON *schema, const char *key) {
|
||||
const cJSON *req = cJSON_GetObjectItemCaseSensitive(schema, "required");
|
||||
const cJSON *it = NULL;
|
||||
cJSON_ArrayForEach(it, req) {
|
||||
if (cJSON_IsString(it) && strcmp(it->valuestring, key) == 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *cst_tool_args_json(const cst_tool_def_t *t, const ctl_tool_call_t *c) {
|
||||
cJSON *schema = t ? tool_schema(t) : NULL;
|
||||
const cJSON *props = cJSON_GetObjectItemCaseSensitive(schema, "properties");
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
for (int i = 0; i < c->nargs; i++) {
|
||||
const char *k = c->args[i].key;
|
||||
const char *v = c->args[i].value ? c->args[i].value : "";
|
||||
const char *type = NULL;
|
||||
const cJSON *pp = cJSON_GetObjectItemCaseSensitive(props, k);
|
||||
const cJSON *ty = cJSON_GetObjectItemCaseSensitive(pp, "type");
|
||||
if (cJSON_IsString(ty))
|
||||
type = ty->valuestring;
|
||||
for (int j = 0; !type && t && j < CST_TOOL_MAX_PARAMS && t->params[j].name;
|
||||
j++) {
|
||||
if (strcmp(t->params[j].name, k) == 0)
|
||||
type = t->params[j].type;
|
||||
}
|
||||
cJSON *val = NULL;
|
||||
if (!type || strcmp(type, "string") != 0) {
|
||||
const char *p = v;
|
||||
while (*p == ' ' || *p == '\n')
|
||||
p++;
|
||||
/* untyped: only structured values are taken as JSON */
|
||||
if (type || *p == '{' || *p == '[')
|
||||
val = cJSON_Parse(v);
|
||||
if (val && type && strcmp(type, "integer") == 0 && !cJSON_IsNumber(val)) {
|
||||
cJSON_Delete(val);
|
||||
val = NULL;
|
||||
}
|
||||
}
|
||||
if (!val)
|
||||
val = cJSON_CreateString(v);
|
||||
cJSON_AddItemToObject(o, k, val);
|
||||
}
|
||||
char *s = cJSON_PrintUnformatted(o);
|
||||
cJSON_Delete(o);
|
||||
cJSON_Delete(schema);
|
||||
return s;
|
||||
}
|
||||
|
||||
static int is_minimal(const cst_agent_config_t *cfg) {
|
||||
@@ -208,9 +425,10 @@ void *cst_tools_schema_cfg(const cst_agent_config_t *cfg) {
|
||||
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))
|
||||
int n = cst_tool_count();
|
||||
for (int i = 0; i < n; i++) {
|
||||
const cst_tool_def_t *t = cst_tool_at(i);
|
||||
if (!t || !cst_tool_active(cfg, t))
|
||||
continue;
|
||||
cJSON *tool = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(tool, "type", "function");
|
||||
@@ -218,6 +436,17 @@ void *cst_tools_schema_cfg(const cst_agent_config_t *cfg) {
|
||||
cJSON_AddStringToObject(fn, "name", t->name);
|
||||
cJSON_AddStringToObject(fn, "description",
|
||||
legacy && t->min_desc ? t->min_desc : t->desc);
|
||||
cJSON *schema = tool_schema(t);
|
||||
if (t->schema_json) {
|
||||
if (!schema) {
|
||||
schema = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(schema, "type", "object");
|
||||
cJSON_AddObjectToObject(schema, "properties");
|
||||
}
|
||||
cJSON_AddItemToObject(fn, "parameters", schema);
|
||||
cJSON_AddItemToArray(arr, tool);
|
||||
continue;
|
||||
}
|
||||
cJSON *params = cJSON_AddObjectToObject(fn, "parameters");
|
||||
cJSON_AddStringToObject(params, "type", "object");
|
||||
cJSON *props = cJSON_AddObjectToObject(params, "properties");
|
||||
@@ -243,25 +472,47 @@ 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))
|
||||
int n = cst_tool_count();
|
||||
for (int i = 0; i < n; i++) {
|
||||
const cst_tool_def_t *t = cst_tool_at(i);
|
||||
if (!t || !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++)
|
||||
if (t->schema_json) {
|
||||
cJSON *schema = tool_schema(t);
|
||||
const cJSON *props =
|
||||
cJSON_GetObjectItemCaseSensitive(schema, "properties");
|
||||
const cJSON *it = NULL;
|
||||
int k = 0;
|
||||
cJSON_ArrayForEach(it, props) {
|
||||
cst_buf_printf(&b, "%s%s%s", k++ ? ", " : "", it->string,
|
||||
schema_required(schema, it->string) ? "" : "?");
|
||||
}
|
||||
cJSON_Delete(schema);
|
||||
}
|
||||
for (int k = 0; !t->schema_json && 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);
|
||||
/* first line of a long description is enough in the prompt */
|
||||
const char *d = t->desc ? t->desc : "";
|
||||
size_t dl = strlen(d);
|
||||
if (t->schema_json && dl > 300)
|
||||
dl = cst_utf8_floor(d, 300);
|
||||
cst_buf_printf(&b, "): %.*s%s\n", (int)dl, d,
|
||||
dl < strlen(d) ? "…" : "");
|
||||
}
|
||||
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);
|
||||
int n = 0, cnt = cst_tool_count();
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
const cst_tool_def_t *t = cst_tool_at(i);
|
||||
if (t && cst_tool_active(cfg, t))
|
||||
cst_buf_printf(&b, "%s%s", n++ ? ", " : "", t->name);
|
||||
}
|
||||
if (!b.buf)
|
||||
cst_buf_puts(&b, "");
|
||||
@@ -285,6 +536,22 @@ static int parse_int_value(const char *v, long *out) {
|
||||
|
||||
int cst_tool_validate(const cst_tool_def_t *t, const ctl_tool_call_t *call,
|
||||
char *err, size_t err_sz) {
|
||||
if (t->schema_json) {
|
||||
cJSON *schema = tool_schema(t);
|
||||
const cJSON *req = cJSON_GetObjectItemCaseSensitive(schema, "required");
|
||||
const cJSON *it = NULL;
|
||||
int rc = 0;
|
||||
cJSON_ArrayForEach(it, req) {
|
||||
if (cJSON_IsString(it) && !tool_arg(call, it->valuestring)) {
|
||||
snprintf(err, err_sz, "error: %s needs argument \"%s\"", t->name,
|
||||
it->valuestring);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(schema);
|
||||
return rc;
|
||||
}
|
||||
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);
|
||||
@@ -319,6 +586,8 @@ int cst_tool_run(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call,
|
||||
char err[256];
|
||||
if (cst_tool_validate(t, call, err, sizeof(err)) != 0)
|
||||
return tool_set_result(r, err, 1);
|
||||
if (t->run_ex)
|
||||
return t->run_ex(ctx, call, r, t->ud);
|
||||
return t->run(ctx, call, r);
|
||||
}
|
||||
|
||||
|
||||
@@ -323,6 +323,170 @@ int cst_tool_run_shell_ex(const char *workspace_root,
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define SHC_LINE_MAX 2000
|
||||
#define SHC_HEAD 80
|
||||
#define SHC_TAIL 120
|
||||
#define SHC_BYTES_MAX (40 * 1024)
|
||||
|
||||
/* One output line: escapes removed and only what follows the last lone
|
||||
* `\r` kept (what a terminal would finally show). */
|
||||
static void shc_clean_line(const char *s, size_t n, cst_buf_t *out) {
|
||||
size_t start = out->len;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned char ch = (unsigned char)s[i];
|
||||
if (ch == 0x1b && i + 1 < n) {
|
||||
unsigned char k = (unsigned char)s[i + 1];
|
||||
if (k == '[') {
|
||||
i += 2;
|
||||
while (i < n && ((unsigned char)s[i] < 0x40 || (unsigned char)s[i] > 0x7e))
|
||||
i++;
|
||||
} else if (k == ']') {
|
||||
i += 2;
|
||||
while (i < n && s[i] != 0x07 &&
|
||||
!(s[i] == 0x1b && i + 1 < n && s[i + 1] == '\\'))
|
||||
i++;
|
||||
if (i < n && s[i] == 0x1b)
|
||||
i++;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (ch == '\r') {
|
||||
if (i + 1 < n)
|
||||
out->len = start;
|
||||
continue;
|
||||
}
|
||||
if (ch == '\b') {
|
||||
if (out->len > start)
|
||||
out->len--;
|
||||
continue;
|
||||
}
|
||||
if (ch < 0x20 && ch != '\t')
|
||||
continue;
|
||||
cst_buf_append(out, (const char *)&s[i], 1);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
size_t off, len;
|
||||
} shc_line_t;
|
||||
|
||||
static void shc_put(cst_buf_t *b, const char *s, size_t n) {
|
||||
if (n > SHC_LINE_MAX) {
|
||||
size_t keep = cst_utf8_floor(s, SHC_LINE_MAX);
|
||||
cst_buf_append(b, s, keep);
|
||||
cst_buf_printf(b, " … [+%zu bytes]\n", n - keep);
|
||||
return;
|
||||
}
|
||||
cst_buf_append(b, s, n);
|
||||
cst_buf_puts(b, "\n");
|
||||
}
|
||||
|
||||
char *cst_shell_compact(const char *s, size_t n, size_t *out_len) {
|
||||
cst_buf_t clean = {0};
|
||||
shc_line_t *lines = NULL;
|
||||
size_t nl = 0, cap = 0;
|
||||
for (size_t i = 0; i < n;) {
|
||||
const char *e = memchr(s + i, '\n', n - i);
|
||||
size_t end = e ? (size_t)(e - s) : n;
|
||||
size_t ln = end - i;
|
||||
if (ln && s[i + ln - 1] == '\r')
|
||||
ln--;
|
||||
if (nl == cap) {
|
||||
cap = cap ? cap * 2 : 256;
|
||||
shc_line_t *nv = realloc(lines, cap * sizeof(*nv));
|
||||
if (!nv)
|
||||
break;
|
||||
lines = nv;
|
||||
}
|
||||
size_t off = clean.len;
|
||||
shc_clean_line(s + i, ln, &clean);
|
||||
lines[nl].off = off;
|
||||
lines[nl].len = clean.len - off;
|
||||
nl++;
|
||||
i = e ? end + 1 : n;
|
||||
}
|
||||
/* Collapse runs of identical lines: keep one plus a count. */
|
||||
size_t m = 0;
|
||||
int *rep = calloc(nl ? nl : 1, sizeof(int));
|
||||
for (size_t i = 0; rep && i < nl;) {
|
||||
size_t j = i + 1;
|
||||
while (j < nl && lines[j].len == lines[i].len &&
|
||||
memcmp(clean.buf + lines[j].off, clean.buf + lines[i].off,
|
||||
lines[i].len) == 0)
|
||||
j++;
|
||||
lines[m] = lines[i];
|
||||
rep[m] = (int)(j - i);
|
||||
m++;
|
||||
if (j - i == 2) {
|
||||
lines[m] = lines[i];
|
||||
rep[m - 1] = rep[m] = 1;
|
||||
m++;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
if (!rep)
|
||||
m = nl;
|
||||
size_t total = 0;
|
||||
for (size_t i = 0; i < m; i++)
|
||||
total += (lines[i].len < SHC_LINE_MAX ? lines[i].len : SHC_LINE_MAX) + 1;
|
||||
int cut = m > SHC_HEAD + SHC_TAIL + 20 || total > SHC_BYTES_MAX;
|
||||
size_t head = m, tail_from = m;
|
||||
if (cut) {
|
||||
head = m < SHC_HEAD ? m : SHC_HEAD;
|
||||
tail_from = m > SHC_TAIL ? m - SHC_TAIL : 0;
|
||||
if (tail_from < head)
|
||||
tail_from = head;
|
||||
/* byte budget: shrink the head first, keep the tail (errors live there) */
|
||||
size_t tb = 0;
|
||||
for (size_t i = tail_from; i < m; i++)
|
||||
tb += (lines[i].len < SHC_LINE_MAX ? lines[i].len : SHC_LINE_MAX) + 1;
|
||||
while (tb > SHC_BYTES_MAX * 3 / 4 && tail_from + 1 < m) {
|
||||
tb -= (lines[tail_from].len < SHC_LINE_MAX ? lines[tail_from].len
|
||||
: SHC_LINE_MAX) + 1;
|
||||
tail_from++;
|
||||
}
|
||||
size_t hb = 0, h = 0;
|
||||
while (h < head) {
|
||||
size_t l = (lines[h].len < SHC_LINE_MAX ? lines[h].len : SHC_LINE_MAX) + 1;
|
||||
if (hb + l + tb > SHC_BYTES_MAX)
|
||||
break;
|
||||
hb += l;
|
||||
h++;
|
||||
}
|
||||
head = h;
|
||||
}
|
||||
cst_buf_t b = {0};
|
||||
for (size_t i = 0; i < m; i++) {
|
||||
if (cut && i == head && head < tail_from) {
|
||||
size_t omitted = 0;
|
||||
for (size_t k = head; k < tail_from; k++)
|
||||
omitted += rep ? (size_t)rep[k] : 1;
|
||||
cst_buf_printf(&b, "… %zu lines omitted …\n", omitted);
|
||||
i = tail_from;
|
||||
if (i >= m)
|
||||
break;
|
||||
}
|
||||
shc_put(&b, clean.buf ? clean.buf + lines[i].off : "", lines[i].len);
|
||||
if (rep && rep[i] > 2)
|
||||
cst_buf_printf(&b, "… (previous line repeated %d more times)\n",
|
||||
rep[i] - 1);
|
||||
}
|
||||
/* Keep "no trailing newline" outputs that way. */
|
||||
if (n && s[n - 1] != '\n' && b.len && b.buf[b.len - 1] == '\n')
|
||||
b.len--;
|
||||
if (!b.buf)
|
||||
cst_buf_puts(&b, "");
|
||||
else
|
||||
b.buf[b.len] = 0;
|
||||
free(rep);
|
||||
free(lines);
|
||||
cst_buf_free(&clean);
|
||||
*out_len = b.len;
|
||||
return cst_buf_steal(&b);
|
||||
}
|
||||
|
||||
/* --- apply_patch --------------------------------------------------------- */
|
||||
|
||||
typedef struct {
|
||||
|
||||
Reference in New Issue
Block a user