xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / tool.c

18.3 KB
1// The read, edit, and bash tools.
2
3#include "tool.h"
4
5#include <errno.h>
6#include <fcntl.h>
7#include <poll.h>
8#include <signal.h>
9#include <stdarg.h>
10#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <strings.h>
15#include <sys/stat.h>
16#include <sys/wait.h>
17#include <unistd.h>
18
19#include <yyjson.h>
20
21#include "diff.h"
22#include "util.h"
23
24#define MAX_TEXT 100000
25#define MAX_IMAGE_B64 (5u * 1024 * 1024)
26#define MAX_READ (16u * 1024 * 1024)
27#define MAX_HEAD 10000
28#define MAX_TAIL 6000
29#define TRUNCATED "\n\n... output truncated ...\n\n"
30
31// ---- small helpers --------------------------------------------------------
32
33static void set_error(tiny_tool_result *res, const char *fmt, ...) {
34    char detail[1024];
35    va_list ap;
36    va_start(ap, fmt);
37    vsnprintf(detail, sizeof detail, fmt, ap);
38    va_end(ap);
39    res->text = xstrdup(detail);
40    res->is_error = true;
41}
42
43// The named member of `args` when it is a string, else NULL.
44static const char *arg_str(yyjson_doc *doc, const char *key) {
45    if (!doc) return NULL;
46    yyjson_val *v = yyjson_obj_get(yyjson_doc_get_root(doc), key);
47    return yyjson_is_str(v) ? yyjson_get_str(v) : NULL;
48}
49
50// ---- UTF-8 ----------------------------------------------------------------
51
52// Validates one sequence at `p` (n bytes remain), returning its length.
53static bool utf8_seq(const unsigned char *p, size_t n, size_t *len) {
54    unsigned char c = p[0];
55    if (c >= 0xC2 && c <= 0xDF) {
56        if (n >= 2 && (p[1] & 0xC0) == 0x80) return (*len = 2, true);
57    } else if (c == 0xE0) {
58        if (n >= 3 && p[1] >= 0xA0 && p[1] <= 0xBF && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
59    } else if (c >= 0xE1 && c <= 0xEC) {
60        if (n >= 3 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
61    } else if (c == 0xED) {
62        if (n >= 3 && p[1] >= 0x80 && p[1] <= 0x9F && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
63    } else if (c >= 0xEE && c <= 0xEF) {
64        if (n >= 3 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
65    } else if (c == 0xF0) {
66        if (n >= 4 && p[1] >= 0x90 && p[1] <= 0xBF && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80)
67            return (*len = 4, true);
68    } else if (c >= 0xF1 && c <= 0xF3) {
69        if (n >= 4 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80)
70            return (*len = 4, true);
71    } else if (c == 0xF4) {
72        if (n >= 4 && p[1] >= 0x80 && p[1] <= 0x8F && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80)
73            return (*len = 4, true);
74    }
75    return false;
76}
77
78// Replaces invalid byte sequences with U+FFFD, so the text survives the JSON
79// and session round trips. Mirrors a UTF-8 decode with replacement.
80static char *utf8_clean(const char *in, size_t n) {
81    tiny_buf b = {0};
82    const unsigned char *p = (const unsigned char *)in;
83    size_t i = 0;
84    while (i < n) {
85        size_t len;
86        if (p[i] < 0x80) {
87            buf_add(&b, (const char *)&p[i], 1);
88            i += 1;
89        } else if (utf8_seq(p + i, n - i, &len)) {
90            buf_add(&b, (const char *)(p + i), len);
91            i += len;
92        } else {
93            buf_add(&b, "\xef\xbf\xbd", 3);
94            i += 1;
95        }
96    }
97    if (!b.data) buf_add(&b, "", 1);
98    return b.data;
99}
100
101// ---- file helpers ---------------------------------------------------------
102
103// Reads up to MAX_READ bytes. Returns 0, or -1 with errno set.
104static int slurp(const char *path, tiny_buf *out) {
105    int fd = open(path, O_RDONLY);
106    if (fd < 0) return -1;
107    struct stat st;
108    if (fstat(fd, &st) != 0) {
109        int e = errno;
110        close(fd);
111        errno = e;
112        return -1;
113    }
114    if (S_ISDIR(st.st_mode)) {
115        close(fd);
116        errno = EISDIR;
117        return -1;
118    }
119    for (;;) {
120        if (out->len >= MAX_READ) break;
121        char tmp[65536];
122        size_t want = sizeof tmp;
123        if (out->len + want > MAX_READ) want = MAX_READ - out->len;
124        ssize_t n = read(fd, tmp, want);
125        if (n < 0) {
126            if (errno == EINTR) continue;
127            int e = errno;
128            close(fd);
129            errno = e;
130            return -1;
131        }
132        if (n == 0) break;
133        buf_add(out, tmp, (size_t)n);
134    }
135    close(fd);
136    return 0;
137}
138
139static int spill(const char *path, const char *data, size_t len) {
140    int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
141    if (fd < 0) return -1;
142    size_t off = 0;
143    while (off < len) {
144        ssize_t n = write(fd, data + off, len - off);
145        if (n < 0) {
146            if (errno == EINTR) continue;
147            int e = errno;
148            close(fd);
149            errno = e;
150            return -1;
151        }
152        off += (size_t)n;
153    }
154    if (close(fd) != 0) return -1;
155    return 0;
156}
157
158static bool find_sub(const char *hay, size_t hlen, const char *needle, size_t nlen, size_t *at) {
159    if (nlen == 0 || nlen > hlen) return false;
160    for (size_t i = 0; i + nlen <= hlen; i++) {
161        if (hay[i] == needle[0] && memcmp(hay + i, needle, nlen) == 0) {
162            *at = i;
163            return true;
164        }
165    }
166    return false;
167}
168
169static char *base64_encode(const unsigned char *data, size_t len) {
170    static const char table[] =
171        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
172    char *out = xmalloc(4 * ((len + 2) / 3) + 1);
173    size_t i = 0, o = 0;
174    for (; i + 3 <= len; i += 3) {
175        uint32_t v = (uint32_t)data[i] << 16 | (uint32_t)data[i + 1] << 8 | data[i + 2];
176        out[o++] = table[(v >> 18) & 63];
177        out[o++] = table[(v >> 12) & 63];
178        out[o++] = table[(v >> 6) & 63];
179        out[o++] = table[v & 63];
180    }
181    if (len - i == 1) {
182        uint32_t v = (uint32_t)data[i] << 16;
183        out[o++] = table[(v >> 18) & 63];
184        out[o++] = table[(v >> 12) & 63];
185        out[o++] = '=';
186        out[o++] = '=';
187    } else if (len - i == 2) {
188        uint32_t v = (uint32_t)data[i] << 16 | (uint32_t)data[i + 1] << 8;
189        out[o++] = table[(v >> 18) & 63];
190        out[o++] = table[(v >> 12) & 63];
191        out[o++] = table[(v >> 6) & 63];
192        out[o++] = '=';
193    }
194    out[o] = '\0';
195    return out;
196}
197
198static const char *image_mime(const char *path) {
199    const char *dot = strrchr(path, '.');
200    if (!dot) return NULL;
201    const char *e = dot + 1;
202    if (strcasecmp(e, "png") == 0) return "image/png";
203    if (strcasecmp(e, "jpg") == 0 || strcasecmp(e, "jpeg") == 0) return "image/jpeg";
204    if (strcasecmp(e, "webp") == 0) return "image/webp";
205    return NULL;
206}
207
208// ---- read -----------------------------------------------------------------
209
210static void tool_read(const char *args, bool supports_images, tiny_tool_result *res) {
211    yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
212    const char *path = arg_str(doc, "path");
213    if (!path) {
214        set_error(res, "read failed: path must be a string");
215        yyjson_doc_free(doc);
216        return;
217    }
218
219    tiny_buf data = {0};
220    if (slurp(path, &data) != 0) {
221        set_error(res, "read failed: %s: %s", path, strerror(errno));
222        free(data.data);
223        yyjson_doc_free(doc);
224        return;
225    }
226
227    const char *mime = image_mime(path);
228    if (mime) {
229        if (!supports_images) {
230            set_error(res, "read failed: %s is an image and the current model does not accept image input", path);
231            free(data.data);
232            yyjson_doc_free(doc);
233            return;
234        }
235        char *b64 = base64_encode((const unsigned char *)data.data, data.len);
236        if (strlen(b64) > MAX_IMAGE_B64) {
237            set_error(res, "read failed: %s encodes to %zu bytes, over the %u byte image limit",
238                      path, strlen(b64), MAX_IMAGE_B64);
239            free(b64);
240            free(data.data);
241            yyjson_doc_free(doc);
242            return;
243        }
244        res->text = xmalloc(strlen(path) + strlen(mime) + 64);
245        sprintf(res->text, "read %s (%s, %zu bytes)", path, mime, data.len);
246        res->images = xmalloc(sizeof *res->images);
247        res->images[0] = (tiny_image){.media_type = xstrdup(mime), .data = b64};
248        res->images_len = 1;
249        free(data.data);
250        yyjson_doc_free(doc);
251        return;
252    }
253
254    size_t sniff = data.len < 8000 ? data.len : 8000;
255    if (sniff > 0 && memchr(data.data, 0, sniff)) {
256        set_error(res, "read failed: %s is binary; use bash (file, xxd)", path);
257        free(data.data);
258        yyjson_doc_free(doc);
259        return;
260    }
261
262    size_t take = data.len > MAX_TEXT ? MAX_TEXT : data.len;
263    char *clean = utf8_clean(data.data ? data.data : "", take);
264    if (data.len > MAX_TEXT) {
265        tiny_buf out = {0};
266        buf_adds(&out, clean);
267        char marker[256];
268        snprintf(marker, sizeof marker, "\n\n... truncated ... %s is %zu characters\n", path, data.len);
269        buf_adds(&out, marker);
270        free(clean);
271        clean = out.data;
272    }
273    free(data.data);
274    yyjson_doc_free(doc);
275    res->text = clean;
276}
277
278// ---- edit -----------------------------------------------------------------
279
280static void tool_edit(const char *args, tiny_tool_result *res) {
281    yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
282    yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
283    yyjson_val *pa = root ? yyjson_obj_get(root, "path") : NULL;
284    yyjson_val *oa = root ? yyjson_obj_get(root, "oldText") : NULL;
285    yyjson_val *na = root ? yyjson_obj_get(root, "newText") : NULL;
286    if (!yyjson_is_str(pa) || !yyjson_is_str(oa) || !yyjson_is_str(na)) {
287        set_error(res, "edit failed: path, oldText and newText must be strings");
288        yyjson_doc_free(doc);
289        return;
290    }
291    const char *path = yyjson_get_str(pa);
292    const char *old = yyjson_get_str(oa);
293    size_t old_len = yyjson_get_len(oa);
294    const char *nw = yyjson_get_str(na);
295    size_t new_len = yyjson_get_len(na);
296
297    if (old_len == 0) {
298        if (access(path, F_OK) == 0) {
299            set_error(res, "edit failed: %s already exists", path);
300            yyjson_doc_free(doc);
301            return;
302        }
303        if (spill(path, nw, new_len) != 0) {
304            set_error(res, "edit failed: %s: %s", path, strerror(errno));
305            yyjson_doc_free(doc);
306            return;
307        }
308        char *patch = tiny_unified_diff(path, "", 0, nw, new_len);
309        tui_buf b;
310        buf_init(&b);
311        buf_addf(&b, "created %s\n", path);
312        buf_adds(&b, patch);
313        res->text = buf_steal(&b);
314        buf_free(&b);
315        free(patch);
316        yyjson_doc_free(doc);
317        return;
318    }
319
320    tiny_buf data = {0};
321    if (slurp(path, &data) != 0) {
322        set_error(res, "edit failed: %s: %s", path, strerror(errno));
323        free(data.data);
324        yyjson_doc_free(doc);
325        return;
326    }
327
328    size_t at;
329    if (!find_sub(data.data, data.len, old, old_len, &at)) {
330        set_error(res, "edit failed: oldText not found in %s", path);
331        free(data.data);
332        yyjson_doc_free(doc);
333        return;
334    }
335    size_t second;
336    if (find_sub(data.data + at + 1, data.len - at - 1, old, old_len, &second)) {
337        set_error(res, "edit failed: oldText matches more than once in %s", path);
338        free(data.data);
339        yyjson_doc_free(doc);
340        return;
341    }
342
343    tiny_buf out = {0};
344    buf_add(&out, data.data, at);
345    buf_add(&out, nw, new_len);
346    buf_add(&out, data.data + at + old_len, data.len - at - old_len);
347    if (spill(path, out.data, out.len) != 0) {
348        set_error(res, "edit failed: %s: %s", path, strerror(errno));
349        free(out.data);
350        free(data.data);
351        yyjson_doc_free(doc);
352        return;
353    }
354
355    char *patch = tiny_unified_diff(path, data.data, data.len, out.data, out.len);
356    tui_buf b;
357    buf_init(&b);
358    buf_addf(&b, "edited %s\n", path);
359    buf_adds(&b, patch);
360    res->text = buf_steal(&b);
361    buf_free(&b);
362    free(patch);
363    free(out.data);
364    free(data.data);
365    yyjson_doc_free(doc);
366}
367
368// ---- bash -----------------------------------------------------------------
369
370// Keeps only the last MAX_TAIL bytes, flagging that a gap was dropped.
371static void keep_tail(tiny_buf *t, bool *truncated) {
372    if (t->len <= MAX_TAIL) return;
373    memmove(t->data, t->data + t->len - MAX_TAIL, MAX_TAIL);
374    t->len = MAX_TAIL;
375    t->data[t->len] = '\0';
376    *truncated = true;
377}
378
379static void tool_bash(const char *args, tiny_tool_output_fn out, void *user,
380                      const volatile bool *cancel, tiny_tool_result *res) {
381    yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
382    const char *command = arg_str(doc, "command");
383    if (!command) {
384        set_error(res, "bash: command must be a string");
385        yyjson_doc_free(doc);
386        return;
387    }
388
389    int pipefd[2];
390    if (pipe(pipefd) != 0) {
391        set_error(res, "bash: %s", strerror(errno));
392        yyjson_doc_free(doc);
393        return;
394    }
395    pid_t pid = fork();
396    if (pid < 0) {
397        close(pipefd[0]);
398        close(pipefd[1]);
399        set_error(res, "bash: fork: %s", strerror(errno));
400        yyjson_doc_free(doc);
401        return;
402    }
403    if (pid == 0) {
404        setpgid(0, 0);
405        if (dup2(pipefd[1], STDOUT_FILENO) < 0 || dup2(pipefd[1], STDERR_FILENO) < 0) _exit(127);
406        close(pipefd[0]);
407        close(pipefd[1]);
408        int devnull = open("/dev/null", O_RDONLY);
409        if (devnull >= 0) {
410            dup2(devnull, STDIN_FILENO);
411            close(devnull);
412        }
413        execlp("bash", "bash", "-c", command, (char *)NULL);
414        _exit(127);
415    }
416    close(pipefd[1]);
417    setpgid(pid, pid);
418
419    tiny_buf head = {0}, tail = {0};
420    bool truncated = false;
421    bool cancelled = false;
422
423    char chunk[8192];
424    for (;;) {
425        if (cancel && *cancel) {
426            cancelled = true;
427            break;
428        }
429        struct pollfd pfd = {.fd = pipefd[0], .events = POLLIN};
430        int pr = poll(&pfd, 1, 100);
431        if (pr < 0) {
432            if (errno == EINTR) continue;
433            break;
434        }
435        if (pr == 0) continue;
436        ssize_t n = read(pipefd[0], chunk, sizeof chunk - 1);
437        if (n < 0) {
438            if (errno == EINTR) continue;
439            break;
440        }
441        if (n == 0) break;
442        chunk[n] = '\0';
443        if (out) out(chunk, user);
444        if (head.len < MAX_HEAD) {
445            size_t take = MAX_HEAD - head.len;
446            if ((size_t)n < take) take = (size_t)n;
447            buf_add(&head, chunk, take);
448            if (take < (size_t)n) {
449                buf_add(&tail, chunk + take, (size_t)n - take);
450                keep_tail(&tail, &truncated);
451            }
452        } else {
453            buf_add(&tail, chunk, (size_t)n);
454            keep_tail(&tail, &truncated);
455        }
456    }
457    close(pipefd[0]);
458
459    int status = 0;
460    if (cancelled) {
461        kill(-pid, SIGTERM);
462        bool reaped = false;
463        for (int i = 0; i < 30; i++) {
464            if (waitpid(pid, &status, WNOHANG) == pid) {
465                reaped = true;
466                break;
467            }
468            usleep(100000);
469        }
470        if (!reaped) {
471            kill(-pid, SIGKILL);
472            while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
473        }
474    } else {
475        while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
476    }
477
478    int code;
479    if (cancelled) code = 143;
480    else if (WIFEXITED(status)) code = WEXITSTATUS(status);
481    else if (WIFSIGNALED(status)) code = 128 + WTERMSIG(status);
482    else code = 0;
483
484    tiny_buf body = {0};
485    buf_add(&body, head.data ? head.data : "", head.len);
486    if (truncated) buf_adds(&body, TRUNCATED);
487    buf_add(&body, tail.data ? tail.data : "", tail.len);
488
489    char *clean = utf8_clean(body.data ? body.data : "", body.len);
490    tiny_buf text = {0};
491    buf_adds(&text, clean);
492    if (text.len > 0 && text.data[text.len - 1] != '\n') buf_adds(&text, "\n");
493    char code_line[32];
494    snprintf(code_line, sizeof code_line, "exit code: %d", code);
495    buf_adds(&text, code_line);
496    res->text = text.data;
497    res->is_error = code != 0;
498    free(clean);
499    free(body.data);
500    free(head.data);
501    free(tail.data);
502    yyjson_doc_free(doc);
503}
504
505// ---- the toolset ----------------------------------------------------------
506
507#define READ_DESCRIPTION \
508    "Read a file. Returns its text. Prefer bash for search, ranges, or binary files."
509#define READ_IMAGE_DESCRIPTION \
510    "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. " \
511    "Prefer bash for search, ranges, or binary files."
512#define EDIT_DESCRIPTION \
513    "Edit a file by exact text replacement. oldText must occur exactly once. " \
514    "With empty oldText, create a new file (fails if it exists)."
515#define BASH_DESCRIPTION "Run a bash command in the current working directory."
516
517#define READ_PARAMS \
518    "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"File path\"}}," \
519    "\"required\":[\"path\"],\"additionalProperties\":false}"
520#define EDIT_PARAMS \
521    "{\"type\":\"object\",\"properties\":{" \
522    "\"path\":{\"type\":\"string\",\"description\":\"File path\"}," \
523    "\"oldText\":{\"type\":\"string\",\"description\":\"Exact text to replace; empty to create a file\"}," \
524    "\"newText\":{\"type\":\"string\",\"description\":\"Replacement text\"}}," \
525    "\"required\":[\"path\",\"oldText\",\"newText\"],\"additionalProperties\":false}"
526#define BASH_PARAMS \
527    "{\"type\":\"object\",\"properties\":{\"command\":{\"type\":\"string\",\"description\":\"Command to run\"}}," \
528    "\"required\":[\"command\"],\"additionalProperties\":false}"
529
530const tiny_tool *tiny_tools(bool supports_images, size_t *len) {
531    static const tiny_tool plain[] = {
532        {"read", READ_DESCRIPTION, READ_PARAMS},
533        {"edit", EDIT_DESCRIPTION, EDIT_PARAMS},
534        {"bash", BASH_DESCRIPTION, BASH_PARAMS},
535    };
536    static const tiny_tool images[] = {
537        {"read", READ_IMAGE_DESCRIPTION, READ_PARAMS},
538        {"edit", EDIT_DESCRIPTION, EDIT_PARAMS},
539        {"bash", BASH_DESCRIPTION, BASH_PARAMS},
540    };
541    *len = 3;
542    return supports_images ? images : plain;
543}
544
545tiny_tool_result tiny_tool_run(const char *name, const char *args, bool supports_images,
546                               tiny_tool_output_fn out, void *user, const volatile bool *cancel) {
547    tiny_tool_result res = {0};
548    if (name && strcmp(name, "read") == 0) {
549        tool_read(args, supports_images, &res);
550    } else if (name && strcmp(name, "edit") == 0) {
551        tool_edit(args, &res);
552    } else if (name && strcmp(name, "bash") == 0) {
553        tool_bash(args, out, user, cancel, &res);
554    } else {
555        set_error(&res, "unknown tool: %s", name ? name : "");
556    }
557    return res;
558}