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:
WHC
2026-10-05 01:47:01 +08:00
parent 9877ff48b6
commit 2fde898da0
105 changed files with 19348 additions and 3786 deletions
+121
View File
@@ -0,0 +1,121 @@
#include "cst_agent.h"
#include "os_compat.h"
#include "cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int settings_path(char *out, size_t n) {
char dir[OS_PATH_MAX];
if (os_config_dir(dir, sizeof(dir)) != 0)
return -1;
return os_path_join(out, n, dir, "settings.json");
}
static void copy_str(char *dst, size_t n, const cJSON *v) {
if (cJSON_IsString(v) && v->valuestring)
snprintf(dst, n, "%s", v->valuestring);
}
int cst_settings_load(cst_settings_t *s) {
memset(s, 0, sizeof(*s));
char path[OS_PATH_MAX];
if (settings_path(path, sizeof(path)) != 0)
return -1;
FILE *fp = fopen(path, "rb");
if (!fp)
return -1;
char buf[16384];
size_t n = fread(buf, 1, sizeof(buf) - 1, fp);
fclose(fp);
buf[n] = 0;
cJSON *root = cJSON_Parse(buf);
if (!cJSON_IsObject(root)) {
cJSON_Delete(root);
return -1;
}
copy_str(s->provider, sizeof(s->provider),
cJSON_GetObjectItemCaseSensitive(root, "provider"));
copy_str(s->effort, sizeof(s->effort),
cJSON_GetObjectItemCaseSensitive(root, "effort"));
const cJSON *models = cJSON_GetObjectItemCaseSensitive(root, "models");
const cJSON *m;
cJSON_ArrayForEach(m, models) {
if (s->nmodels >= CST_SETTINGS_MAX_MODELS)
break;
if (!m->string || !cJSON_IsString(m))
continue;
snprintf(s->models[s->nmodels].provider,
sizeof(s->models[s->nmodels].provider), "%s", m->string);
copy_str(s->models[s->nmodels].model, sizeof(s->models[s->nmodels].model),
m);
s->nmodels++;
}
cJSON_Delete(root);
return 0;
}
int cst_settings_save(const cst_settings_t *s) {
char dir[OS_PATH_MAX], path[OS_PATH_MAX], tmp[OS_PATH_MAX + 8];
if (os_config_dir(dir, sizeof(dir)) != 0 || os_mkdir_p(dir, 0700) != 0 ||
settings_path(path, sizeof(path)) != 0)
return -1;
if (snprintf(tmp, sizeof(tmp), "%s.tmp", path) >= (int)sizeof(tmp))
return -1;
cJSON *root = cJSON_CreateObject();
if (s->provider[0])
cJSON_AddStringToObject(root, "provider", s->provider);
if (s->effort[0])
cJSON_AddStringToObject(root, "effort", s->effort);
cJSON *models = cJSON_AddObjectToObject(root, "models");
for (int i = 0; i < s->nmodels; i++)
cJSON_AddStringToObject(models, s->models[i].provider, s->models[i].model);
char *text = cJSON_Print(root);
cJSON_Delete(root);
if (!text)
return -1;
FILE *fp = fopen(tmp, "wb");
int rc = -1;
if (fp) {
int ok = fputs(text, fp) >= 0 && fputc('\n', fp) != EOF;
if (fclose(fp) == 0 && ok) {
#ifdef _WIN32
remove(path);
#endif
rc = rename(tmp, path) == 0 ? 0 : -1;
}
if (rc != 0)
remove(tmp);
}
cJSON_free(text);
return rc;
}
const char *cst_settings_model(const cst_settings_t *s, const char *provider) {
for (int i = 0; provider && i < s->nmodels; i++) {
if (strcmp(s->models[i].provider, provider) == 0 && s->models[i].model[0])
return s->models[i].model;
}
return NULL;
}
int cst_settings_remember(const cst_agent_config_t *cfg) {
cst_settings_t s;
cst_settings_load(&s);
snprintf(s.provider, sizeof(s.provider), "%s", cfg->provider);
snprintf(s.effort, sizeof(s.effort), "%s", cfg->effort);
int i = 0;
while (i < s.nmodels && strcmp(s.models[i].provider, cfg->provider) != 0)
i++;
if (i == s.nmodels) {
if (s.nmodels >= CST_SETTINGS_MAX_MODELS)
return -1;
s.nmodels++;
}
snprintf(s.models[i].provider, sizeof(s.models[i].provider), "%s",
cfg->provider);
snprintf(s.models[i].model, sizeof(s.models[i].model), "%s", cfg->model);
return cst_settings_save(&s);
}