Initial CST-Agent: portable Linux/Termux/Windows coding agent.
Vendored sashidown, OpenAI-compatible env (OPENAI_*), and bootstrap scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
#define _DEFAULT_SOURCE
|
||||
|
||||
#include "cst_agent.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#define OS_ACCESS _access
|
||||
#define OS_F_OK 0
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#define OS_ACCESS access
|
||||
#define OS_F_OK F_OK
|
||||
#endif
|
||||
|
||||
int cst_agent_store_dir(const char *workspace, char *out, size_t out_sz) {
|
||||
if (!workspace || !out || out_sz < 16)
|
||||
return -1;
|
||||
return os_path_join(out, out_sz, workspace, ".agent/sessions");
|
||||
}
|
||||
|
||||
int cst_agent_store_ensure(const char *workspace) {
|
||||
char base[OS_PATH_MAX];
|
||||
char sess[OS_PATH_MAX];
|
||||
if (cst_agent_store_dir(workspace, sess, sizeof(sess)) != 0)
|
||||
return -1;
|
||||
if (os_path_join(base, sizeof(base), workspace, ".agent") != 0)
|
||||
return -1;
|
||||
if (os_mkdir(base, 0755) != 0)
|
||||
return -1;
|
||||
if (os_mkdir(sess, 0755) != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void make_id(char *out, size_t n) {
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
os_localtime(&now, &tm);
|
||||
unsigned r = (unsigned)(os_getpid() ^ (unsigned)now);
|
||||
snprintf(out, n, "%04d%02d%02d-%02d%02d%02d-%04x", tm.tm_year + 1900,
|
||||
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
|
||||
r & 0xffff);
|
||||
}
|
||||
|
||||
int cst_agent_store_new_id(char *id_out, size_t id_sz) {
|
||||
if (!id_out || id_sz < 24)
|
||||
return -1;
|
||||
make_id(id_out, id_sz);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_agent_store_path_for_id(const char *workspace, const char *id,
|
||||
char *out, size_t out_sz) {
|
||||
if (!workspace || !id || !out)
|
||||
return -1;
|
||||
/* Accept bare id or full path. */
|
||||
if (strchr(id, '/') || strchr(id, '\\')) {
|
||||
snprintf(out, out_sz, "%s", id);
|
||||
return 0;
|
||||
}
|
||||
char dir[OS_PATH_MAX];
|
||||
char leaf[160];
|
||||
if (cst_agent_store_dir(workspace, dir, sizeof(dir)) != 0)
|
||||
return -1;
|
||||
snprintf(leaf, sizeof(leaf), "%s.jsonl", id);
|
||||
return os_path_join(out, out_sz, dir, leaf);
|
||||
}
|
||||
|
||||
int cst_agent_store_set_current(const char *workspace, const char *id) {
|
||||
if (cst_agent_store_ensure(workspace) != 0)
|
||||
return -1;
|
||||
char dir[OS_PATH_MAX];
|
||||
char path[OS_PATH_MAX];
|
||||
if (cst_agent_store_dir(workspace, dir, sizeof(dir)) != 0)
|
||||
return -1;
|
||||
if (os_path_join(path, sizeof(path), dir, "CURRENT") != 0)
|
||||
return -1;
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (!fp)
|
||||
return -1;
|
||||
fprintf(fp, "%s\n", id);
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_agent_store_get_current(const char *workspace, char *id_out,
|
||||
size_t id_sz) {
|
||||
char dir[OS_PATH_MAX];
|
||||
char path[OS_PATH_MAX];
|
||||
if (cst_agent_store_dir(workspace, dir, sizeof(dir)) != 0)
|
||||
return -1;
|
||||
if (os_path_join(path, sizeof(path), dir, "CURRENT") != 0)
|
||||
return -1;
|
||||
FILE *fp = fopen(path, "r");
|
||||
if (!fp)
|
||||
return -1;
|
||||
if (!fgets(id_out, (int)id_sz, fp)) {
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
size_t n = strlen(id_out);
|
||||
while (n > 0 && (id_out[n - 1] == '\n' || id_out[n - 1] == '\r'))
|
||||
id_out[--n] = 0;
|
||||
return id_out[0] ? 0 : -1;
|
||||
}
|
||||
|
||||
int cst_agent_store_resolve_default(const char *workspace, char *path_out,
|
||||
size_t path_sz, char *id_out,
|
||||
size_t id_sz) {
|
||||
if (cst_agent_store_ensure(workspace) != 0)
|
||||
return -1;
|
||||
char id[128];
|
||||
if (cst_agent_store_get_current(workspace, id, sizeof(id)) == 0) {
|
||||
char p[OS_PATH_MAX];
|
||||
if (cst_agent_store_path_for_id(workspace, id, p, sizeof(p)) == 0 &&
|
||||
OS_ACCESS(p, OS_F_OK) == 0) {
|
||||
snprintf(path_out, path_sz, "%s", p);
|
||||
if (id_out && id_sz)
|
||||
snprintf(id_out, id_sz, "%s", id);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (cst_agent_store_new_id(id, sizeof(id)) != 0)
|
||||
return -1;
|
||||
if (cst_agent_store_path_for_id(workspace, id, path_out, path_sz) != 0)
|
||||
return -1;
|
||||
cst_agent_store_set_current(workspace, id);
|
||||
if (id_out && id_sz)
|
||||
snprintf(id_out, id_sz, "%s", id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_agent_store_list(const char *workspace, char ids[][128], int max_ids) {
|
||||
if (!workspace || !ids || max_ids <= 0)
|
||||
return 0;
|
||||
char dir[OS_PATH_MAX];
|
||||
if (cst_agent_store_dir(workspace, dir, sizeof(dir)) != 0)
|
||||
return 0;
|
||||
DIR *d = opendir(dir);
|
||||
if (!d)
|
||||
return 0;
|
||||
typedef struct {
|
||||
char id[128];
|
||||
time_t mtime;
|
||||
} ent_t;
|
||||
ent_t *ents = calloc((size_t)max_ids * 4, sizeof(ent_t));
|
||||
int n = 0, cap = max_ids * 4;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
size_t len = strlen(de->d_name);
|
||||
if (len < 7 || strcmp(de->d_name + len - 6, ".jsonl") != 0)
|
||||
continue;
|
||||
if (n >= cap)
|
||||
break;
|
||||
snprintf(ents[n].id, sizeof(ents[n].id), "%.*s", (int)(len - 6),
|
||||
de->d_name);
|
||||
char full[OS_PATH_MAX];
|
||||
if (os_path_join(full, sizeof(full), dir, de->d_name) != 0)
|
||||
continue;
|
||||
struct stat st;
|
||||
ents[n].mtime = (stat(full, &st) == 0) ? st.st_mtime : 0;
|
||||
n++;
|
||||
}
|
||||
closedir(d);
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (ents[j].mtime > ents[i].mtime) {
|
||||
ent_t tmp = ents[i];
|
||||
ents[i] = ents[j];
|
||||
ents[j] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
int out_n = n < max_ids ? n : max_ids;
|
||||
for (int i = 0; i < out_n; i++)
|
||||
snprintf(ids[i], 128, "%s", ents[i].id);
|
||||
free(ents);
|
||||
return out_n;
|
||||
}
|
||||
Reference in New Issue
Block a user