Replace nlohmann/json with native C++20 JSON engine, cut build RAM/time, fix 32-bit build, enable -O2 -march=native
This commit is contained in:
@@ -2,21 +2,18 @@
|
||||
#include "bantam.hpp"
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curl/curl.h>
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <span>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
/* ----------------------------- usage / pct ---------------------------- */
|
||||
static int ctx_tokens(Usage &u) {
|
||||
if (u.context_tokens > u.prompt_tokens) return u.context_tokens;
|
||||
@@ -59,12 +56,11 @@ static bool is_invalid_assistant_err(const char *err) {
|
||||
/* ----------------------------- sanitize args -------------------------- */
|
||||
static std::string sanitize_arguments(std::string_view args) {
|
||||
std::string f = filter_text(args);
|
||||
json o = nullptr;
|
||||
try { o = json::parse(f); } catch (...) { o = nullptr; }
|
||||
if (o.is_object()) return f;
|
||||
json w = json::object();
|
||||
Json o;
|
||||
if (Json::parse(f, o) && o.is_object()) return f;
|
||||
Json w = Json::object();
|
||||
w["invalid_raw"] = f;
|
||||
return w.dump(-1, ' ', false, json::error_handler_t::replace);
|
||||
return w.dump();
|
||||
}
|
||||
|
||||
/* ----------------------------- system prompt -------------------------- */
|
||||
@@ -106,20 +102,13 @@ static std::string build_system_prompt(Cfg &cfg) {
|
||||
}
|
||||
|
||||
/* ----------------------------- json arg helpers ----------------------- */
|
||||
static std::string get_str(const json &j, const char *k) {
|
||||
if (j.contains(k) && j[k].is_string()) return j[k].get<std::string>();
|
||||
return "";
|
||||
static std::string get_str(const Json &j, const char *k) {
|
||||
return j[k].str();
|
||||
}
|
||||
static int get_int(const json &j, const char *k, int def = 0) {
|
||||
if (j.contains(k) && j[k].is_number_integer()) {
|
||||
int64_t v = j[k].get<int64_t>();
|
||||
if (v > INT_MAX) return INT_MAX;
|
||||
if (v < INT_MIN) return INT_MIN;
|
||||
return (int)v;
|
||||
}
|
||||
return def;
|
||||
static int get_int(const Json &j, const char *k, int def = 0) {
|
||||
return j[k].as_int(def);
|
||||
}
|
||||
static std::string tool_path_arg(const json &a) {
|
||||
static std::string tool_path_arg(const Json &a) {
|
||||
std::string v = get_str(a, "filePath");
|
||||
if (v.empty()) v = get_str(a, "path");
|
||||
return v;
|
||||
@@ -197,12 +186,12 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
|
||||
printf("\n");
|
||||
}
|
||||
std::string res; int sty = 2;
|
||||
json a = nullptr;
|
||||
try { a = json::parse(astr); } catch (...) { a = nullptr; }
|
||||
if (!a.is_object()) {
|
||||
Json a_mut;
|
||||
if (!Json::parse(astr, a_mut) || !a_mut.is_object()) {
|
||||
res = sfmt("[tool error: invalid JSON args for %s: parse error. Raw: %s]", tc.name.c_str(), astr.c_str());
|
||||
sty = 31;
|
||||
} else {
|
||||
const Json &a = a_mut;
|
||||
std::string fn = tc.name;
|
||||
if (fn == "bash" || fn == "shell_exec") {
|
||||
std::string cmd = get_str(a, "command");
|
||||
@@ -234,9 +223,8 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
|
||||
auto sl = fp.find_last_of('/');
|
||||
if (sl != std::string::npos) mkdir_p(std::string_view(fp).substr(0, sl));
|
||||
else mkdir_p(".");
|
||||
std::ofstream of(fp, std::ios::binary | std::ios::trunc);
|
||||
if (!of) { res = sfmt("[tool error: write %s: %s]", fp.c_str(), strerror(errno)); sty = 31; }
|
||||
else { of.write(c.data(), (std::streamsize)c.size()); res = sfmt("Successfully wrote %zu bytes to %s", c.size(), fp.c_str()); }
|
||||
if (!write_file_str(fp, c)) { res = sfmt("[tool error: write %s: %s]", fp.c_str(), strerror(errno)); sty = 31; }
|
||||
else { res = sfmt("Successfully wrote %zu bytes to %s", c.size(), fp.c_str()); }
|
||||
} else {
|
||||
int werr = 0; std::string c = filter_text(cv);
|
||||
res = write_file(fp, offset, del, c, &werr);
|
||||
@@ -322,9 +310,9 @@ static std::string skill_prompt(std::string_view u, Cfg &cfg) {
|
||||
return sfmt("[cannot read skill \"%s\": %s]", name.c_str(), strerror(errno));
|
||||
if (S_ISDIR(st.st_mode))
|
||||
return sfmt("[cannot read skill \"%s\": Is a directory]", name.c_str());
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f) return sfmt("[cannot read skill \"%s\": %s]", name.c_str(), strerror(errno));
|
||||
std::string data((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
std::string data;
|
||||
if (!read_file_str(path, data))
|
||||
return sfmt("[cannot read skill \"%s\": %s]", name.c_str(), strerror(errno));
|
||||
while (!data.empty() && (data.back() == '\n' || data.back() == '\r')) data.pop_back();
|
||||
std::string out = data;
|
||||
if (!prompt.empty()) { out += "\n\n"; out += prompt; }
|
||||
@@ -394,12 +382,11 @@ int main(int argc, char **argv) {
|
||||
cprint("Error: file '", std::array{31}); cprint(p, std::array{31}); cprint("' not found.\n", std::array{31});
|
||||
return 1;
|
||||
}
|
||||
std::ifstream f(p, std::ios::binary);
|
||||
if (!f) {
|
||||
std::string u;
|
||||
if (!read_file_str(p, u)) {
|
||||
cprint("Error: file '", std::array{31}); cprint(p, std::array{31}); cprint("' not found.\n", std::array{31});
|
||||
return 1;
|
||||
}
|
||||
std::string u((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
size_t a = 0; while (a < u.size() && (u[a] == ' ' || u[a] == '\n' || u[a] == '\r' || u[a] == '\t')) a++;
|
||||
u = u.substr(a);
|
||||
if (!u.empty() && u[0] == '!') { run_direct_shell(u.substr(1), cfg.shell_timeout); return 0; }
|
||||
|
||||
Reference in New Issue
Block a user