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:
+166
-171
@@ -1,10 +1,14 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "cst_agent.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -49,6 +53,7 @@ struct cst_session {
|
||||
int nmsgs;
|
||||
int cap;
|
||||
FILE *fp;
|
||||
pthread_mutex_t mu;
|
||||
};
|
||||
|
||||
static char *xstrdup(const char *s) {
|
||||
@@ -77,6 +82,8 @@ static const char *role_str(cst_role_t r) {
|
||||
}
|
||||
|
||||
static cst_role_t role_from(const char *s) {
|
||||
if (!s)
|
||||
return CST_ROLE_USER;
|
||||
if (strcmp(s, "system") == 0)
|
||||
return CST_ROLE_SYSTEM;
|
||||
if (strcmp(s, "assistant") == 0)
|
||||
@@ -86,60 +93,11 @@ static cst_role_t role_from(const char *s) {
|
||||
return CST_ROLE_USER;
|
||||
}
|
||||
|
||||
static int json_escape_write(FILE *fp, const char *s) {
|
||||
fputc('"', fp);
|
||||
for (; s && *s; s++) {
|
||||
unsigned char c = (unsigned char)*s;
|
||||
if (c == '"' || c == '\\') {
|
||||
fputc('\\', fp);
|
||||
fputc(c, fp);
|
||||
} else if (c == '\n') {
|
||||
fputs("\\n", fp);
|
||||
} else if (c == '\r') {
|
||||
fputs("\\r", fp);
|
||||
} else if (c == '\t') {
|
||||
fputs("\\t", fp);
|
||||
} else if (c < 0x20) {
|
||||
fprintf(fp, "\\u%04x", c);
|
||||
} else {
|
||||
fputc(c, fp);
|
||||
}
|
||||
}
|
||||
fputc('"', fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Minimal extract "key":"value" from a single JSONL object line. */
|
||||
static int extract_str(const char *line, const char *key, char *out,
|
||||
size_t out_sz) {
|
||||
char pat[64];
|
||||
snprintf(pat, sizeof(pat), "\"%s\":\"", key);
|
||||
const char *p = strstr(line, pat);
|
||||
if (!p)
|
||||
return -1;
|
||||
p += strlen(pat);
|
||||
size_t o = 0;
|
||||
while (*p && o + 1 < out_sz) {
|
||||
if (*p == '\\' && p[1]) {
|
||||
p++;
|
||||
if (*p == 'n')
|
||||
out[o++] = '\n';
|
||||
else if (*p == 'r')
|
||||
out[o++] = '\r';
|
||||
else if (*p == 't')
|
||||
out[o++] = '\t';
|
||||
else
|
||||
out[o++] = *p;
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
if (*p == '"') {
|
||||
out[o] = 0;
|
||||
return 0;
|
||||
}
|
||||
out[o++] = *p++;
|
||||
}
|
||||
return -1;
|
||||
static void msg_free(cst_message_t *m) {
|
||||
free(m->content);
|
||||
free(m->tool_calls_json);
|
||||
free(m->meta_json);
|
||||
m->content = m->tool_calls_json = m->meta_json = NULL;
|
||||
}
|
||||
|
||||
static int session_grow(cst_session_t *s) {
|
||||
@@ -154,27 +112,30 @@ static int session_grow(cst_session_t *s) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int session_push_mem(cst_session_t *s, cst_role_t role, const char *content,
|
||||
const char *tool_id, const char *tool_name,
|
||||
const char *tool_calls_json) {
|
||||
static int session_push_mem(cst_session_t *s, const cst_message_t *src) {
|
||||
if (session_grow(s) != 0)
|
||||
return -1;
|
||||
cst_message_t *d = &s->msgs[s->nmsgs];
|
||||
memset(d, 0, sizeof(*d));
|
||||
d->role = role;
|
||||
d->content = xstrdup(content ? content : "");
|
||||
d->role = src->role;
|
||||
d->content = xstrdup(src->content ? src->content : "");
|
||||
if (!d->content)
|
||||
return -1;
|
||||
if (tool_id)
|
||||
snprintf(d->tool_call_id, sizeof(d->tool_call_id), "%s", tool_id);
|
||||
if (tool_name)
|
||||
snprintf(d->tool_name, sizeof(d->tool_name), "%s", tool_name);
|
||||
if (tool_calls_json && tool_calls_json[0])
|
||||
d->tool_calls_json = xstrdup(tool_calls_json);
|
||||
snprintf(d->tool_call_id, sizeof(d->tool_call_id), "%s", src->tool_call_id);
|
||||
snprintf(d->tool_name, sizeof(d->tool_name), "%s", src->tool_name);
|
||||
if (src->tool_calls_json && src->tool_calls_json[0])
|
||||
d->tool_calls_json = xstrdup(src->tool_calls_json);
|
||||
if (src->meta_json && src->meta_json[0])
|
||||
d->meta_json = xstrdup(src->meta_json);
|
||||
s->nmsgs++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *jstr(const cJSON *o, const char *key) {
|
||||
const cJSON *it = cJSON_GetObjectItemCaseSensitive(o, key);
|
||||
return cJSON_IsString(it) ? it->valuestring : NULL;
|
||||
}
|
||||
|
||||
static void load_existing(cst_session_t *s) {
|
||||
FILE *rp = fopen(s->path, "r");
|
||||
if (!rp)
|
||||
@@ -182,116 +143,142 @@ static void load_existing(cst_session_t *s) {
|
||||
char *line = NULL;
|
||||
size_t cap = 0;
|
||||
while (getline(&line, &cap, rp) != -1) {
|
||||
char role[32] = {0};
|
||||
char *content = malloc(cap > 8 ? cap : 4096);
|
||||
char tid[CTL_MAX_ID] = {0};
|
||||
char tname[CTL_MAX_NAME] = {0};
|
||||
if (!content)
|
||||
continue;
|
||||
content[0] = 0;
|
||||
if (extract_str(line, "role", role, sizeof(role)) != 0) {
|
||||
free(content);
|
||||
cJSON *o = cJSON_Parse(line);
|
||||
if (!o || !cJSON_IsObject(o)) {
|
||||
cJSON_Delete(o);
|
||||
continue;
|
||||
}
|
||||
/* content may be large — reuse line extract into growing buffer */
|
||||
free(content);
|
||||
content = malloc(strlen(line) + 1);
|
||||
if (!content)
|
||||
continue;
|
||||
if (extract_str(line, "content", content, strlen(line) + 1) != 0) {
|
||||
free(content);
|
||||
const char *role = jstr(o, "role");
|
||||
if (!role) {
|
||||
cJSON_Delete(o);
|
||||
continue;
|
||||
}
|
||||
extract_str(line, "tool_call_id", tid, sizeof(tid));
|
||||
extract_str(line, "tool_name", tname, sizeof(tname));
|
||||
char *tcj = malloc(strlen(line) + 1);
|
||||
const char *tcjp = NULL;
|
||||
if (tcj) {
|
||||
tcj[0] = 0;
|
||||
if (extract_str(line, "tool_calls_json", tcj, strlen(line) + 1) == 0)
|
||||
tcjp = tcj;
|
||||
}
|
||||
session_push_mem(s, role_from(role), content, tid[0] ? tid : NULL,
|
||||
tname[0] ? tname : NULL, tcjp);
|
||||
cst_message_t m = {0};
|
||||
m.role = role_from(role);
|
||||
m.content = (char *)(jstr(o, "content") ? jstr(o, "content") : "");
|
||||
const char *tid = jstr(o, "tool_call_id");
|
||||
const char *tname = jstr(o, "tool_name");
|
||||
if (tid)
|
||||
snprintf(m.tool_call_id, sizeof(m.tool_call_id), "%s", tid);
|
||||
if (tname)
|
||||
snprintf(m.tool_name, sizeof(m.tool_name), "%s", tname);
|
||||
char *tcj = NULL;
|
||||
const cJSON *tc = cJSON_GetObjectItemCaseSensitive(o, "tool_calls");
|
||||
if (cJSON_IsArray(tc))
|
||||
tcj = cJSON_PrintUnformatted(tc);
|
||||
else if (jstr(o, "tool_calls_json")) /* legacy string field */
|
||||
tcj = xstrdup(jstr(o, "tool_calls_json"));
|
||||
m.tool_calls_json = tcj;
|
||||
char *meta = NULL;
|
||||
const cJSON *mo = cJSON_GetObjectItemCaseSensitive(o, "meta");
|
||||
if (cJSON_IsObject(mo))
|
||||
meta = cJSON_PrintUnformatted(mo);
|
||||
m.meta_json = meta;
|
||||
session_push_mem(s, &m);
|
||||
free(tcj);
|
||||
free(content);
|
||||
free(meta);
|
||||
cJSON_Delete(o);
|
||||
}
|
||||
free(line);
|
||||
fclose(rp);
|
||||
}
|
||||
|
||||
static void free_all(cst_session_t *s) {
|
||||
for (int i = 0; i < s->nmsgs; i++)
|
||||
msg_free(&s->msgs[i]);
|
||||
s->nmsgs = 0;
|
||||
}
|
||||
|
||||
cst_session_t *cst_session_open(const char *path) {
|
||||
cst_session_t *s = calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
return NULL;
|
||||
pthread_mutex_init(&s->mu, NULL);
|
||||
snprintf(s->path, sizeof(s->path), "%s", path ? path : "session.jsonl");
|
||||
load_existing(s);
|
||||
s->fp = fopen(s->path, "a+");
|
||||
if (!s->fp) {
|
||||
for (int i = 0; i < s->nmsgs; i++) {
|
||||
free(s->msgs[i].content);
|
||||
free(s->msgs[i].tool_calls_json);
|
||||
}
|
||||
if (!s->fp || session_grow(s) != 0) {
|
||||
if (s->fp)
|
||||
fclose(s->fp);
|
||||
free_all(s);
|
||||
free(s->msgs);
|
||||
pthread_mutex_destroy(&s->mu);
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
if (!s->msgs) {
|
||||
s->cap = 32;
|
||||
s->msgs = calloc((size_t)s->cap, sizeof(cst_message_t));
|
||||
if (!s->msgs) {
|
||||
fclose(s->fp);
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void cst_session_close(cst_session_t *s) {
|
||||
if (!s)
|
||||
return;
|
||||
for (int i = 0; i < s->nmsgs; i++) {
|
||||
free(s->msgs[i].content);
|
||||
free(s->msgs[i].tool_calls_json);
|
||||
}
|
||||
free_all(s);
|
||||
free(s->msgs);
|
||||
if (s->fp)
|
||||
fclose(s->fp);
|
||||
pthread_mutex_destroy(&s->mu);
|
||||
free(s);
|
||||
}
|
||||
|
||||
void cst_session_lock(cst_session_t *s) {
|
||||
if (s)
|
||||
pthread_mutex_lock(&s->mu);
|
||||
}
|
||||
|
||||
void cst_session_unlock(cst_session_t *s) {
|
||||
if (s)
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
}
|
||||
|
||||
static int write_line(FILE *fp, const cst_message_t *msg) {
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
if (!o)
|
||||
return -1;
|
||||
cJSON_AddStringToObject(o, "role", role_str(msg->role));
|
||||
cJSON_AddStringToObject(o, "content", msg->content ? msg->content : "");
|
||||
if (msg->tool_call_id[0])
|
||||
cJSON_AddStringToObject(o, "tool_call_id", msg->tool_call_id);
|
||||
if (msg->tool_name[0])
|
||||
cJSON_AddStringToObject(o, "tool_name", msg->tool_name);
|
||||
if (msg->tool_calls_json && msg->tool_calls_json[0]) {
|
||||
cJSON *tc = cJSON_Parse(msg->tool_calls_json);
|
||||
if (cJSON_IsArray(tc))
|
||||
cJSON_AddItemToObject(o, "tool_calls", tc);
|
||||
else {
|
||||
cJSON_Delete(tc);
|
||||
cJSON_AddStringToObject(o, "tool_calls_json", msg->tool_calls_json);
|
||||
}
|
||||
}
|
||||
if (msg->meta_json && msg->meta_json[0]) {
|
||||
cJSON *mo = cJSON_Parse(msg->meta_json);
|
||||
if (cJSON_IsObject(mo))
|
||||
cJSON_AddItemToObject(o, "meta", mo);
|
||||
else
|
||||
cJSON_Delete(mo);
|
||||
}
|
||||
char *txt = cJSON_PrintUnformatted(o);
|
||||
cJSON_Delete(o);
|
||||
if (!txt)
|
||||
return -1;
|
||||
fputs(txt, fp);
|
||||
fputc('\n', fp);
|
||||
free(txt);
|
||||
fflush(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_session_append(cst_session_t *s, const cst_message_t *msg) {
|
||||
if (!s || !msg)
|
||||
return -1;
|
||||
if (session_push_mem(s, msg->role, msg->content, msg->tool_call_id,
|
||||
msg->tool_name, msg->tool_calls_json) != 0)
|
||||
return -1;
|
||||
|
||||
fprintf(s->fp, "{\"role\":");
|
||||
json_escape_write(s->fp, role_str(msg->role));
|
||||
fprintf(s->fp, ",\"content\":");
|
||||
json_escape_write(s->fp, msg->content ? msg->content : "");
|
||||
if (msg->tool_call_id[0]) {
|
||||
fprintf(s->fp, ",\"tool_call_id\":");
|
||||
json_escape_write(s->fp, msg->tool_call_id);
|
||||
}
|
||||
if (msg->tool_name[0]) {
|
||||
fprintf(s->fp, ",\"tool_name\":");
|
||||
json_escape_write(s->fp, msg->tool_name);
|
||||
}
|
||||
if (msg->tool_calls_json && msg->tool_calls_json[0]) {
|
||||
fprintf(s->fp, ",\"tool_calls_json\":");
|
||||
json_escape_write(s->fp, msg->tool_calls_json);
|
||||
}
|
||||
fprintf(s->fp, "}\n");
|
||||
fflush(s->fp);
|
||||
return 0;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
int rc = session_push_mem(s, msg);
|
||||
if (rc == 0 && s->fp)
|
||||
rc = write_line(s->fp, msg);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_session_count(const cst_session_t *s) {
|
||||
return s ? s->nmsgs : 0;
|
||||
}
|
||||
int cst_session_count(const cst_session_t *s) { return s ? s->nmsgs : 0; }
|
||||
|
||||
const cst_message_t *cst_session_get(const cst_session_t *s, int i) {
|
||||
if (!s || i < 0 || i >= s->nmsgs)
|
||||
@@ -303,9 +290,19 @@ const char *cst_session_path(const cst_session_t *s) {
|
||||
return s ? s->path : NULL;
|
||||
}
|
||||
|
||||
static int reopen_truncate(cst_session_t *s) {
|
||||
if (s->fp) {
|
||||
fclose(s->fp);
|
||||
s->fp = NULL;
|
||||
}
|
||||
s->fp = fopen(s->path, "w+");
|
||||
return s->fp ? 0 : -1;
|
||||
}
|
||||
|
||||
int cst_session_clear(cst_session_t *s, int keep_system) {
|
||||
if (!s)
|
||||
return -1;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
cst_message_t sys = {0};
|
||||
int have_sys = 0;
|
||||
if (keep_system) {
|
||||
@@ -313,51 +310,49 @@ int cst_session_clear(cst_session_t *s, int keep_system) {
|
||||
if (s->msgs[i].role == CST_ROLE_SYSTEM) {
|
||||
sys.role = CST_ROLE_SYSTEM;
|
||||
sys.content = xstrdup(s->msgs[i].content ? s->msgs[i].content : "");
|
||||
have_sys = 1;
|
||||
have_sys = sys.content != NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < s->nmsgs; i++) {
|
||||
free(s->msgs[i].content);
|
||||
free(s->msgs[i].tool_calls_json);
|
||||
free_all(s);
|
||||
int rc = reopen_truncate(s);
|
||||
if (rc == 0 && have_sys) {
|
||||
session_push_mem(s, &sys);
|
||||
write_line(s->fp, &sys);
|
||||
}
|
||||
s->nmsgs = 0;
|
||||
if (s->fp) {
|
||||
fclose(s->fp);
|
||||
s->fp = NULL;
|
||||
}
|
||||
s->fp = fopen(s->path, "w+");
|
||||
if (!s->fp) {
|
||||
if (have_sys)
|
||||
free(sys.content);
|
||||
return -1;
|
||||
}
|
||||
if (have_sys) {
|
||||
cst_session_append(s, &sys);
|
||||
free(sys.content);
|
||||
}
|
||||
return 0;
|
||||
free(sys.content);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_session_rewrite(cst_session_t *s, const cst_message_t *msgs, int n) {
|
||||
if (!s || n < 0)
|
||||
return -1;
|
||||
for (int i = 0; i < s->nmsgs; i++) {
|
||||
free(s->msgs[i].content);
|
||||
free(s->msgs[i].tool_calls_json);
|
||||
}
|
||||
s->nmsgs = 0;
|
||||
if (s->fp) {
|
||||
fclose(s->fp);
|
||||
s->fp = NULL;
|
||||
}
|
||||
s->fp = fopen(s->path, "w+");
|
||||
if (!s->fp)
|
||||
/* msgs may point into this session's own storage: copy before freeing. */
|
||||
cst_message_t *copy = calloc((size_t)(n ? n : 1), sizeof(*copy));
|
||||
if (!copy)
|
||||
return -1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (cst_session_append(s, &msgs[i]) != 0)
|
||||
return -1;
|
||||
copy[i] = msgs[i];
|
||||
copy[i].content = strdup(msgs[i].content ? msgs[i].content : "");
|
||||
copy[i].tool_calls_json =
|
||||
msgs[i].tool_calls_json ? strdup(msgs[i].tool_calls_json) : NULL;
|
||||
copy[i].meta_json = msgs[i].meta_json ? strdup(msgs[i].meta_json) : NULL;
|
||||
}
|
||||
return 0;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
free_all(s);
|
||||
int rc = reopen_truncate(s);
|
||||
for (int i = 0; rc == 0 && i < n; i++) {
|
||||
if (session_push_mem(s, ©[i]) != 0 || write_line(s->fp, ©[i]) != 0)
|
||||
rc = -1;
|
||||
}
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
for (int i = 0; i < n; i++) {
|
||||
free(copy[i].content);
|
||||
free(copy[i].tool_calls_json);
|
||||
free(copy[i].meta_json);
|
||||
}
|
||||
free(copy);
|
||||
return rc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user