Usability pass: tool registry, TUI polish, saved keys and settings
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:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user