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,145 @@
|
||||
#include "cst_agent.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Minimal JSON tool object: {"name":"...","arguments":{...}}
|
||||
* Buffer-until-valid baseline (NOT bounded CST). */
|
||||
|
||||
static const char *skip_ws(const char *p, const char *end) {
|
||||
while (p < end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t'))
|
||||
p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Find matching closing brace/bracket starting at open pos. Returns end ptr or
|
||||
* NULL if incomplete. Handles strings/escapes. */
|
||||
static const char *match_obj(const char *start, const char *end) {
|
||||
if (start >= end || *start != '{')
|
||||
return NULL;
|
||||
int depth = 0;
|
||||
int in_str = 0;
|
||||
for (const char *p = start; p < end; p++) {
|
||||
if (in_str) {
|
||||
if (*p == '\\' && p + 1 < end) {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
if (*p == '"')
|
||||
in_str = 0;
|
||||
continue;
|
||||
}
|
||||
if (*p == '"') {
|
||||
in_str = 1;
|
||||
continue;
|
||||
}
|
||||
if (*p == '{')
|
||||
depth++;
|
||||
else if (*p == '}') {
|
||||
depth--;
|
||||
if (depth == 0)
|
||||
return p + 1;
|
||||
}
|
||||
}
|
||||
return NULL; /* incomplete */
|
||||
}
|
||||
|
||||
static int extract_string(const char *obj, const char *end, const char *key,
|
||||
char *out, size_t out_sz) {
|
||||
char pat[128];
|
||||
snprintf(pat, sizeof(pat), "\"%s\"", key);
|
||||
const char *k = obj;
|
||||
while (k && k < end) {
|
||||
k = strstr(k, pat);
|
||||
if (!k || k >= end)
|
||||
return -1;
|
||||
/* ensure key boundary */
|
||||
const char *colon = k + strlen(pat);
|
||||
colon = skip_ws(colon, end);
|
||||
if (colon >= end || *colon != ':') {
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
colon++;
|
||||
colon = skip_ws(colon, end);
|
||||
if (colon >= end || *colon != '"')
|
||||
return -1;
|
||||
colon++;
|
||||
size_t o = 0;
|
||||
for (const char *p = colon; p < end && o + 1 < out_sz; p++) {
|
||||
if (*p == '\\' && p + 1 < end) {
|
||||
p++;
|
||||
if (*p == 'n')
|
||||
out[o++] = '\n';
|
||||
else
|
||||
out[o++] = *p;
|
||||
continue;
|
||||
}
|
||||
if (*p == '"') {
|
||||
out[o] = 0;
|
||||
return 0;
|
||||
}
|
||||
out[o++] = *p;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int cst_json_try_parse_tool(const char *buf, size_t len, cst_json_tool_t *out) {
|
||||
if (!buf || !out)
|
||||
return 0;
|
||||
memset(out, 0, sizeof(*out));
|
||||
const char *end = buf + len;
|
||||
const char *p = buf;
|
||||
while (p < end) {
|
||||
p = skip_ws(p, end);
|
||||
if (p >= end)
|
||||
break;
|
||||
if (*p != '{') {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
const char *obj_end = match_obj(p, end);
|
||||
if (!obj_end)
|
||||
return 0; /* incomplete JSON — starvation failure mode */
|
||||
if (extract_string(p, obj_end, "name", out->name, sizeof(out->name)) !=
|
||||
0) {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
/* arguments: object as raw substring */
|
||||
const char *akey = strstr(p, "\"arguments\"");
|
||||
if (!akey || akey >= obj_end) {
|
||||
p = obj_end;
|
||||
continue;
|
||||
}
|
||||
const char *colon = strchr(akey + 11, ':');
|
||||
if (!colon || colon >= obj_end)
|
||||
return 0;
|
||||
colon++;
|
||||
colon = skip_ws(colon, obj_end);
|
||||
if (colon >= obj_end)
|
||||
return 0;
|
||||
if (*colon == '{') {
|
||||
const char *ae = match_obj(colon, obj_end);
|
||||
if (!ae)
|
||||
return 0;
|
||||
size_t n = (size_t)(ae - colon);
|
||||
if (n >= sizeof(out->arguments_json))
|
||||
return 0;
|
||||
memcpy(out->arguments_json, colon, n);
|
||||
out->arguments_json[n] = 0;
|
||||
} else if (*colon == '"') {
|
||||
if (extract_string(akey, obj_end, "arguments", out->arguments_json,
|
||||
sizeof(out->arguments_json)) != 0)
|
||||
return 0;
|
||||
} else {
|
||||
p = obj_end;
|
||||
continue;
|
||||
}
|
||||
out->ok = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user