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
+133 -81
View File
@@ -1,4 +1,6 @@
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include "cst_agent.h"
#include "os_compat.h"
@@ -11,10 +13,13 @@
#ifdef _WIN32
#include <io.h>
#else
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#endif
/* Config dir via os_compat: ~/.config/cst-agent or %APPDATA%\cst-agent */
/* Config dir via os_compat: $XDG_CONFIG_HOME/cst-agent (~/.config/cst-agent)
* or %APPDATA%\cst-agent */
static int credentials_path(char *out, size_t n) {
char dir[OS_PATH_MAX];
@@ -23,8 +28,24 @@ static int credentials_path(char *out, size_t n) {
return os_path_join(out, n, dir, "credentials");
}
int cst_creds_load_api_key(char *out, size_t out_sz) {
if (!out || out_sz < 2)
static void chomp(char *s) {
size_t len = strlen(s);
while (len > 0 && (s[len - 1] == '\n' || s[len - 1] == '\r' ||
s[len - 1] == ' ' || s[len - 1] == '\t'))
s[--len] = 0;
}
static int same_base(const char *a, const char *b) {
size_t la = strlen(a), lb = strlen(b);
while (la > 0 && a[la - 1] == '/')
la--;
while (lb > 0 && b[lb - 1] == '/')
lb--;
return la == lb && strncmp(a, b, la) == 0;
}
int cst_creds_load(const char *provider, char *out, size_t out_sz) {
if (!provider || !provider[0] || !out || out_sz < 2)
return -1;
out[0] = 0;
char path[OS_PATH_MAX];
@@ -33,108 +54,139 @@ int cst_creds_load_api_key(char *out, size_t out_sz) {
FILE *fp = fopen(path, "r");
if (!fp)
return -1;
char want[64];
snprintf(want, sizeof(want), "%s.api_key=", provider);
size_t wl = strlen(want);
/* Old single-key files ("api_key=" + "api_base=") belong to the provider
* whose base URL matches; without a base they were MiniMax keys. */
char legacy_key[1024] = "", legacy_base[1024] = "";
char line[1024];
int found = 0;
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "api_key=", 8) != 0)
continue;
char *v = line + 8;
size_t len = strlen(v);
while (len > 0 && (v[len - 1] == '\n' || v[len - 1] == '\r'))
v[--len] = 0;
if (len == 0 || len + 1 > out_sz) {
fclose(fp);
return -1;
chomp(line);
if (strncmp(line, want, wl) == 0 && line[wl]) {
snprintf(out, out_sz, "%s", line + wl);
found = 1;
break;
}
memcpy(out, v, len + 1);
fclose(fp);
return 0;
if (strncmp(line, "api_key=", 8) == 0)
snprintf(legacy_key, sizeof(legacy_key), "%s", line + 8);
else if (strncmp(line, "api_base=", 9) == 0)
snprintf(legacy_base, sizeof(legacy_base), "%s", line + 9);
}
fclose(fp);
if (found)
return 0;
if (legacy_key[0]) {
const cst_provider_t *p = cst_provider_find(provider);
int match = p && (legacy_base[0] ? same_base(legacy_base, p->api_base)
: strcmp(provider, "minimax") == 0);
if (match && strlen(legacy_key) < out_sz) {
snprintf(out, out_sz, "%s", legacy_key);
return 0;
}
}
return -1;
}
int cst_creds_save_api_key(const char *key) {
if (!key || !key[0])
int cst_creds_save(const char *provider, const char *key) {
if (!provider || !provider[0] || !key || !key[0] || strchr(key, '\n'))
return -1;
char dir[OS_PATH_MAX], path[OS_PATH_MAX];
if (os_config_dir(dir, sizeof(dir)) != 0)
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 ||
credentials_path(path, sizeof(path)) != 0)
return -1;
if (os_mkdir(dir, 0700) != 0)
if (snprintf(tmp, sizeof(tmp), "%s.tmp", path) >= (int)sizeof(tmp))
return -1;
if (credentials_path(path, sizeof(path)) != 0)
char want[64];
snprintf(want, sizeof(want), "%s.api_key=", provider);
size_t wl = strlen(want);
#ifdef _WIN32
FILE *out = fopen(tmp, "w");
#else
/* never exists world-readable, even briefly */
int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
FILE *out = fd >= 0 ? fdopen(fd, "w") : NULL;
if (!out && fd >= 0)
close(fd);
#endif
if (!out)
return -1;
FILE *fp = fopen(path, "w");
if (!fp)
FILE *in = fopen(path, "r");
if (in) {
char line[1024];
while (fgets(line, sizeof(line), in)) {
if (strncmp(line, want, wl) != 0)
fputs(line, out);
}
fclose(in);
}
fprintf(out, "%s%s\n", want, key);
if (fclose(out) != 0) {
remove(tmp);
return -1;
fprintf(fp, "api_key=%s\n", key);
fprintf(fp, "api_base=https://api.minimax.cn/v1\n");
fprintf(fp, "model=MiniMax-M2.7\n");
fclose(fp);
}
#ifdef _WIN32
remove(path);
#endif
if (rename(tmp, path) != 0) {
remove(tmp);
return -1;
}
(void)os_chmod_private(path);
return 0;
}
/* Zenity on Unix; stdin prompt elsewhere / when zenity missing. */
int cst_creds_prompt_api_key(char *out, size_t out_sz) {
int cst_creds_resolve(const char *provider, char *out, size_t out_sz,
const char **from) {
if (from)
*from = NULL;
if (cst_creds_load(provider, out, out_sz) == 0 && out[0]) {
if (from)
*from = "saved";
return 0;
}
const cst_provider_t *p = cst_provider_find(provider);
const char *env = p ? getenv(p->key_env) : NULL;
if (env && env[0]) {
snprintf(out, out_sz, "%s", env);
if (from)
*from = p->key_env;
return 0;
}
out[0] = 0;
return -1;
}
int cst_creds_prompt_console(const char *provider, char *out, size_t out_sz) {
if (!out || out_sz < 2)
return -1;
out[0] = 0;
fprintf(stderr, "API key for %s (input hidden, saved to the config dir): ",
provider ? provider : "?");
fflush(stderr);
#ifndef _WIN32
FILE *fp = popen(
"zenity --password --title='CST-Agent / MiniMax' "
"--text='请输入 MiniMax API Key(将保存到配置目录 credentials)' "
"2>/dev/null",
"r");
if (fp) {
if (fgets(out, (int)out_sz, fp)) {
int st = pclose(fp);
size_t len = strlen(out);
while (len > 0 && (out[len - 1] == '\n' || out[len - 1] == '\r'))
out[--len] = 0;
if (st == 0 && len > 0)
return 0;
} else {
pclose(fp);
}
out[0] = 0;
struct termios old, quiet;
int tty = isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &old) == 0;
if (tty) {
quiet = old;
quiet.c_lflag &= ~(tcflag_t)ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &quiet);
}
#endif
/* Fallback: console prompt (headless / Windows / no zenity). */
fprintf(stderr, "CST-Agent: enter MiniMax API Key (saved to config dir): ");
fflush(stderr);
if (!fgets(out, (int)out_sz, stdin)) {
char *got = fgets(out, (int)out_sz, stdin);
#ifndef _WIN32
if (tty) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
fputc('\n', stderr);
}
#endif
if (!got) {
out[0] = 0;
return -1;
}
size_t len = strlen(out);
while (len > 0 && (out[len - 1] == '\n' || out[len - 1] == '\r'))
out[--len] = 0;
if (len == 0) {
out[0] = 0;
return -1;
}
return 0;
}
int cst_creds_ensure_api_key(char *out, size_t out_sz) {
const char *env = getenv("CST_API_KEY");
if (!env || !env[0])
env = getenv("MINIMAX_API_KEY");
if (!env || !env[0])
env = getenv("OPENAI_API_KEY");
if (env && env[0]) {
snprintf(out, out_sz, "%s", env);
return 0;
}
if (cst_creds_load_api_key(out, out_sz) == 0 && out[0])
return 0;
if (cst_creds_prompt_api_key(out, out_sz) != 0)
return -1;
if (cst_creds_save_api_key(out) != 0)
fprintf(stderr, "warning: failed to save credentials file\n");
else
fprintf(stderr, "saved API key to config directory\n");
return 0;
chomp(out);
return out[0] ? 0 : -1;
}