@@ -1,6 +1,6 @@
1-// The read, edit, and bash capabilities, exposed as codemode globals.
1+// The read, edit, and bash tools.
22
3-#include "host.h"
3+#include "tool.h"
44
55 #include <errno.h>
66 #include <fcntl.h>
@@ -63,34 +63,21 @@ static void buf_addz(buf *b, const char *s) {
6363 buf_add(b, s, strlen(s));
6464 }
6565
66-static cage_host_result ok_json(char *json) {
67- return (cage_host_result){.json = json};
68-}
69-
70-static cage_host_result err(const char *fmt, ...) {
71- char detail[512];
66+static void set_error(cage_tool_result *res, const char *fmt, ...) {
67+ char detail[1024];
7268 va_list ap;
7369 va_start(ap, fmt);
7470 vsnprintf(detail, sizeof detail, fmt, ap);
7571 va_end(ap);
76- return (cage_host_result){.error = xstrdup(detail)};
77-}
78-
79-// A JSON string value; `s` must be valid UTF-8 and NUL-terminated.
80-static char *json_string(const char *s) {
81- yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
82- yyjson_mut_doc_set_root(doc, yyjson_mut_strcpy(doc, s));
83- char *out = yyjson_mut_write(doc, 0, NULL);
84- yyjson_mut_doc_free(doc);
85- return out;
72+ res->text = xstrdup(detail);
73+ res->is_error = true;
8674 }
8775
88-// nth argument of the JSON array the runtime passes to a host function.
89-static yyjson_val *arg_at(yyjson_doc *doc, size_t i) {
76+// The named member of `args` when it is a string, else NULL.
77+static const char *arg_str(yyjson_doc *doc, const char *key) {
9078 if (!doc) return NULL;
91- yyjson_val *root = yyjson_doc_get_root(doc);
92- if (!yyjson_is_arr(root)) return NULL;
93- return yyjson_arr_get(root, i);
79+ yyjson_val *v = yyjson_obj_get(yyjson_doc_get_root(doc), key);
80+ return yyjson_is_str(v) ? yyjson_get_str(v) : NULL;
9481 }
9582
9683 // ---- UTF-8 ----------------------------------------------------------------
@@ -253,56 +240,56 @@ static const char *image_mime(const char *path) {
253240
254241 // ---- read -----------------------------------------------------------------
255242
256-static cage_host_result host_read(const char *args_json, void *user) {
257- cage_runtime *rt = user;
258- yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
259- yyjson_val *a = arg_at(doc, 0);
260- if (!yyjson_is_str(a)) {
243+static void tool_read(const char *args, bool supports_images, cage_tool_result *res) {
244+ yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
245+ const char *path = arg_str(doc, "path");
246+ if (!path) {
247+ set_error(res, "read failed: path must be a string");
261248 yyjson_doc_free(doc);
262- return err("read: path must be a string");
249+ return;
263250 }
264- const char *path = yyjson_get_str(a);
265251
266252 buf data = {0};
267253 if (slurp(path, &data) != 0) {
268- cage_host_result r = err("read failed: %s: %s", path, strerror(errno));
254+ set_error(res, "read failed: %s: %s", path, strerror(errno));
269255 free(data.data);
270256 yyjson_doc_free(doc);
271- return r;
257+ return;
272258 }
273259
274260 const char *mime = image_mime(path);
275261 if (mime) {
276- if (!cage_runtime_supports_images(rt)) {
277- cage_host_result r = err("read failed: %s is an image and the current model does not accept image input", path);
262+ if (!supports_images) {
263+ set_error(res, "read failed: %s is an image and the current model does not accept image input", path);
278264 free(data.data);
279265 yyjson_doc_free(doc);
280- return r;
266+ return;
281267 }
282268 char *b64 = base64_encode((const unsigned char *)data.data, data.len);
283269 if (strlen(b64) > MAX_IMAGE_B64) {
284- cage_host_result r = err("read failed: %s encodes to %zu bytes, over the %u byte image limit",
285- path, strlen(b64), MAX_IMAGE_B64);
270+ set_error(res, "read failed: %s encodes to %zu bytes, over the %u byte image limit",
271+ path, strlen(b64), MAX_IMAGE_B64);
286272 free(b64);
287273 free(data.data);
288274 yyjson_doc_free(doc);
289- return r;
275+ return;
290276 }
291- char msg[1024];
292- snprintf(msg, sizeof msg, "read %s (%s, %zu bytes)", path, mime, data.len);
293- cage_runtime_add_image(rt, mime, b64);
294- free(b64);
277+ res->text = xmalloc(strlen(path) + strlen(mime) + 64);
278+ sprintf(res->text, "read %s (%s, %zu bytes)", path, mime, data.len);
279+ res->images = xmalloc(sizeof *res->images);
280+ res->images[0] = (cage_image){.media_type = xstrdup(mime), .data = b64};
281+ res->images_len = 1;
295282 free(data.data);
296283 yyjson_doc_free(doc);
297- return ok_json(json_string(msg));
284+ return;
298285 }
299286
300287 size_t sniff = data.len < 8000 ? data.len : 8000;
301288 if (sniff > 0 && memchr(data.data, 0, sniff)) {
302- cage_host_result r = err("read failed: %s is binary; use bash (file, xxd)", path);
289+ set_error(res, "read failed: %s is binary; use bash (file, xxd)", path);
303290 free(data.data);
304291 yyjson_doc_free(doc);
305- return r;
292+ return;
306293 }
307294
308295 size_t take = data.len > MAX_TEXT ? MAX_TEXT : data.len;
@@ -318,18 +305,21 @@ static cage_host_result host_read(const char *args_json, void *user) {
318305 }
319306 free(data.data);
320307 yyjson_doc_free(doc);
321- return ok_json(json_string(clean));
308+ res->text = clean;
322309 }
323310
324311 // ---- edit -----------------------------------------------------------------
325312
326-static cage_host_result host_edit(const char *args_json, void *user) {
327- (void)user;
328- yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
329- yyjson_val *pa = arg_at(doc, 0), *oa = arg_at(doc, 1), *na = arg_at(doc, 2);
313+static void tool_edit(const char *args, cage_tool_result *res) {
314+ yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
315+ yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
316+ yyjson_val *pa = root ? yyjson_obj_get(root, "path") : NULL;
317+ yyjson_val *oa = root ? yyjson_obj_get(root, "oldText") : NULL;
318+ yyjson_val *na = root ? yyjson_obj_get(root, "newText") : NULL;
330319 if (!yyjson_is_str(pa) || !yyjson_is_str(oa) || !yyjson_is_str(na)) {
320+ set_error(res, "edit failed: path, oldText and newText must be strings");
331321 yyjson_doc_free(doc);
332- return err("edit: path, oldText and newText must be strings");
322+ return;
333323 }
334324 const char *path = yyjson_get_str(pa);
335325 const char *old = yyjson_get_str(oa);
@@ -339,42 +329,42 @@ static cage_host_result host_edit(const char *args_json, void *user) {
339329
340330 if (old_len == 0) {
341331 if (access(path, F_OK) == 0) {
342- cage_host_result r = err("edit failed: %s already exists", path);
332+ set_error(res, "edit failed: %s already exists", path);
343333 yyjson_doc_free(doc);
344- return r;
334+ return;
345335 }
346336 if (spill(path, nw, new_len) != 0) {
347- cage_host_result r = err("edit failed: %s: %s", path, strerror(errno));
337+ set_error(res, "edit failed: %s: %s", path, strerror(errno));
348338 yyjson_doc_free(doc);
349- return r;
339+ return;
350340 }
351- char msg[1024];
352- snprintf(msg, sizeof msg, "created %s", path);
341+ res->text = xmalloc(strlen(path) + 16);
342+ sprintf(res->text, "created %s", path);
353343 yyjson_doc_free(doc);
354- return ok_json(json_string(msg));
344+ return;
355345 }
356346
357347 buf data = {0};
358348 if (slurp(path, &data) != 0) {
359- cage_host_result r = err("edit failed: %s: %s", path, strerror(errno));
349+ set_error(res, "edit failed: %s: %s", path, strerror(errno));
360350 free(data.data);
361351 yyjson_doc_free(doc);
362- return r;
352+ return;
363353 }
364354
365355 size_t at;
366356 if (!find_sub(data.data, data.len, old, old_len, &at)) {
367- cage_host_result r = err("edit failed: oldText not found in %s", path);
357+ set_error(res, "edit failed: oldText not found in %s", path);
368358 free(data.data);
369359 yyjson_doc_free(doc);
370- return r;
360+ return;
371361 }
372362 size_t second;
373363 if (find_sub(data.data + at + 1, data.len - at - 1, old, old_len, &second)) {
374- cage_host_result r = err("edit failed: oldText matches more than once in %s", path);
364+ set_error(res, "edit failed: oldText matches more than once in %s", path);
375365 free(data.data);
376366 yyjson_doc_free(doc);
377- return r;
367+ return;
378368 }
379369
380370 buf out = {0};
@@ -382,19 +372,18 @@ static cage_host_result host_edit(const char *args_json, void *user) {
382372 buf_add(&out, nw, new_len);
383373 buf_add(&out, data.data + at + old_len, data.len - at - old_len);
384374 if (spill(path, out.data, out.len) != 0) {
385- cage_host_result r = err("edit failed: %s: %s", path, strerror(errno));
375+ set_error(res, "edit failed: %s: %s", path, strerror(errno));
386376 free(out.data);
387377 free(data.data);
388378 yyjson_doc_free(doc);
389- return r;
379+ return;
390380 }
391381
392- char msg[1024];
393- snprintf(msg, sizeof msg, "edited %s", path);
382+ res->text = xmalloc(strlen(path) + 16);
383+ sprintf(res->text, "edited %s", path);
394384 free(out.data);
395385 free(data.data);
396386 yyjson_doc_free(doc);
397- return ok_json(json_string(msg));
398387 }
399388
400389 // ---- bash -----------------------------------------------------------------
@@ -408,29 +397,29 @@ static void keep_tail(buf *t, bool *truncated) {
408397 *truncated = true;
409398 }
410399
411-static cage_host_result host_bash(const char *args_json, void *user) {
412- cage_runtime *rt = user;
413- yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
414- yyjson_val *a = arg_at(doc, 0);
415- if (!yyjson_is_str(a)) {
400+static void tool_bash(const char *args, cage_tool_output_fn out, void *user,
401+ const volatile bool *cancel, cage_tool_result *res) {
402+ yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
403+ const char *command = arg_str(doc, "command");
404+ if (!command) {
405+ set_error(res, "bash: command must be a string");
416406 yyjson_doc_free(doc);
417- return err("bash: command must be a string");
407+ return;
418408 }
419- const char *command = yyjson_get_str(a);
420409
421410 int pipefd[2];
422411 if (pipe(pipefd) != 0) {
423- cage_host_result r = err("bash: %s", strerror(errno));
412+ set_error(res, "bash: %s", strerror(errno));
424413 yyjson_doc_free(doc);
425- return r;
414+ return;
426415 }
427416 pid_t pid = fork();
428417 if (pid < 0) {
429418 close(pipefd[0]);
430419 close(pipefd[1]);
431- cage_host_result r = err("bash: fork: %s", strerror(errno));
420+ set_error(res, "bash: fork: %s", strerror(errno));
432421 yyjson_doc_free(doc);
433- return r;
422+ return;
434423 }
435424 if (pid == 0) {
436425 setpgid(0, 0);
@@ -454,7 +443,7 @@ static cage_host_result host_bash(const char *args_json, void *user) {
454443
455444 char chunk[8192];
456445 for (;;) {
457- if (cage_runtime_cancelled(rt)) {
446+ if (cancel && *cancel) {
458447 cancelled = true;
459448 break;
460449 }
@@ -472,7 +461,7 @@ static cage_host_result host_bash(const char *args_json, void *user) {
472461 }
473462 if (n == 0) break;
474463 chunk[n] = '\0';
475- cage_runtime_output(rt, chunk);
464+ if (out) out(chunk, user);
476465 if (head.len < MAX_HEAD) {
477466 size_t take = MAX_HEAD - head.len;
478467 if ((size_t)n < take) take = (size_t)n;
@@ -519,23 +508,72 @@ static cage_host_result host_bash(const char *args_json, void *user) {
519508 buf_add(&body, tail.data ? tail.data : "", tail.len);
520509
521510 char *clean = utf8_clean(body.data ? body.data : "", body.len);
522- yyjson_mut_doc *jd = yyjson_mut_doc_new(NULL);
523- yyjson_mut_val *o = yyjson_mut_obj(jd);
524- yyjson_mut_doc_set_root(jd, o);
525- yyjson_mut_obj_add_strcpy(jd, o, "output", clean);
526- yyjson_mut_obj_add_int(jd, o, "exitCode", code);
527- char *json = yyjson_mut_write(jd, 0, NULL);
528- yyjson_mut_doc_free(jd);
511+ buf text = {0};
512+ buf_addz(&text, clean);
513+ if (text.len > 0 && text.data[text.len - 1] != '\n') buf_addz(&text, "\n");
514+ char code_line[32];
515+ snprintf(code_line, sizeof code_line, "exit code: %d", code);
516+ buf_addz(&text, code_line);
517+ res->text = text.data;
518+ res->is_error = code != 0;
529519 free(clean);
530520 free(body.data);
531521 free(head.data);
532522 free(tail.data);
533523 yyjson_doc_free(doc);
534- return ok_json(json);
535524 }
536525
537-void cage_hosts_register(cage_runtime *rt) {
538- cage_runtime_register(rt, "read", host_read, rt);
539- cage_runtime_register(rt, "edit", host_edit, rt);
540- cage_runtime_register(rt, "bash", host_bash, rt);
526+// ---- the toolset ----------------------------------------------------------
527+
528+#define READ_DESCRIPTION \
529+ "Read a file. Returns its text. Prefer bash for search, ranges, or binary files."
530+#define READ_IMAGE_DESCRIPTION \
531+ "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. " \
532+ "Prefer bash for search, ranges, or binary files."
533+#define EDIT_DESCRIPTION \
534+ "Edit a file by exact text replacement. oldText must occur exactly once. " \
535+ "With empty oldText, create a new file (fails if it exists)."
536+#define BASH_DESCRIPTION "Run a bash command in the current working directory."
537+
538+#define READ_PARAMS \
539+ "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"File path\"}}," \
540+ "\"required\":[\"path\"],\"additionalProperties\":false}"
541+#define EDIT_PARAMS \
542+ "{\"type\":\"object\",\"properties\":{" \
543+ "\"path\":{\"type\":\"string\",\"description\":\"File path\"}," \
544+ "\"oldText\":{\"type\":\"string\",\"description\":\"Exact text to replace; empty to create a file\"}," \
545+ "\"newText\":{\"type\":\"string\",\"description\":\"Replacement text\"}}," \
546+ "\"required\":[\"path\",\"oldText\",\"newText\"],\"additionalProperties\":false}"
547+#define BASH_PARAMS \
548+ "{\"type\":\"object\",\"properties\":{\"command\":{\"type\":\"string\",\"description\":\"Command to run\"}}," \
549+ "\"required\":[\"command\"],\"additionalProperties\":false}"
550+
551+const cage_tool *cage_tools(bool supports_images, size_t *len) {
552+ static const cage_tool plain[] = {
553+ {"read", READ_DESCRIPTION, READ_PARAMS},
554+ {"edit", EDIT_DESCRIPTION, EDIT_PARAMS},
555+ {"bash", BASH_DESCRIPTION, BASH_PARAMS},
556+ };
557+ static const cage_tool images[] = {
558+ {"read", READ_IMAGE_DESCRIPTION, READ_PARAMS},
559+ {"edit", EDIT_DESCRIPTION, EDIT_PARAMS},
560+ {"bash", BASH_DESCRIPTION, BASH_PARAMS},
561+ };
562+ *len = 3;
563+ return supports_images ? images : plain;
564+}
565+
566+cage_tool_result cage_tool_run(const char *name, const char *args, bool supports_images,
567+ cage_tool_output_fn out, void *user, const volatile bool *cancel) {
568+ cage_tool_result res = {0};
569+ if (name && strcmp(name, "read") == 0) {
570+ tool_read(args, supports_images, &res);
571+ } else if (name && strcmp(name, "edit") == 0) {
572+ tool_edit(args, &res);
573+ } else if (name && strcmp(name, "bash") == 0) {
574+ tool_bash(args, out, user, cancel, &res);
575+ } else {
576+ set_error(&res, "unknown tool: %s", name ? name : "");
577+ }
578+ return res;
541579 }