Optimize startup, HTTP/SSE streaming, and fix macOS libedit bindings

This commit is contained in:
Luxferre
2026-10-14 15:30:12 +03:00
parent 105d283e01
commit a1d5f4bf48
10 changed files with 497 additions and 222 deletions
+15 -12
View File
@@ -180,12 +180,13 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
for (size_t tci = 0; tci < m.calls.size(); tci++) {
if (*interrupted) { rc = -1; break; }
ToolCall &tc = m.calls[tci];
std::string astr = filter_text(tc.arguments);
const std::string &astr = tc.arguments; /* already filtered by sanitize_arguments */
if (!m.calls_shown) {
cprint(sfmt("[tool call: %s(%s)]", tc.name.c_str(), astr.c_str()), std::array{33});
printf("\n");
}
std::string res; int sty = 2;
bool res_already_filtered = false;
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());
@@ -196,11 +197,11 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
if (fn == "bash" || fn == "shell_exec") {
std::string cmd = get_str(a, "command");
std::string workdir = get_str(a, "workdir");
std::string fcmd = filter_text(cmd);
std::string fwd = filter_text(workdir);
int to = cfg.shell_timeout;
if (a.contains("timeout")) { int n = get_int(a, "timeout"); if (n > 0) to = (n > 1000) ? n / 1000 : n; }
res = shell_with_workdir(fcmd, fwd, to, interrupted);
res = shell_with_workdir(cmd, fwd, to, interrupted);
res_already_filtered = true;
} else if (fn == "read") {
std::string p = tool_path_arg(a);
std::string fp = filter_text(p);
@@ -250,17 +251,17 @@ static int AL(Cfg &cfg, Messages &msgs, Usage &out, volatile int *interrupted, s
sty = 31;
}
}
std::string fres = filter_text(res);
res = offload_tool_result(fres, cfg.max_tool_res, tmps);
if (!res_already_filtered) res = filter_text(res);
res = offload_tool_result(res, cfg.max_tool_res, tmps);
{
std::string hdr = cstr("[tool result: ", std::array{32});
std::string fn2 = cstr(tc.name, std::array{32});
printf("%s%s]\n", hdr.c_str(), fn2.c_str());
std::string body = cstr(res, std::array{sty});
printf("%s\n\n", body.c_str());
cprint(res, std::array{sty});
printf("\n\n");
}
Message tm; tm.role = "tool"; tm.tool_call_id = tc.id; tm.content = res;
msgs.items.push_back(tm);
Message tm; tm.role = "tool"; tm.tool_call_id = tc.id; tm.content = std::move(res);
msgs.items.push_back(std::move(tm));
if (*interrupted) { rc = -1; break; }
}
if (rc != 0) break;
@@ -374,16 +375,15 @@ static void do_compact(Cfg &cfg, Messages &msgs) {
/* ----------------------------- main ----------------------------------- */
struct CurlGlobalGuard {
CurlGlobalGuard() { curl_global_init(CURL_GLOBAL_DEFAULT); }
~CurlGlobalGuard() { curl_global_cleanup(); }
~CurlGlobalGuard() { http_cleanup(); curl_global_cleanup(); }
};
int main(int argc, char **argv) {
CurlGlobalGuard curl_guard;
setup_signal_handler();
Cfg cfg = get_cfg(config_path());
cfg.context_window = fetch_context_window(cfg);
std::string sp = build_system_prompt(cfg);
g_col = color_enabled(cfg);
std::string sp = build_system_prompt(cfg);
Messages msgs;
{
Message sys; sys.role = "system"; sys.content = sp;
@@ -405,6 +405,7 @@ int main(int argc, char **argv) {
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; }
cfg.context_window = fetch_context_window(cfg);
Message um; um.role = "user"; um.content = u;
msgs.items.push_back(um);
Usage usg; std::string err;
@@ -443,6 +444,8 @@ int main(int argc, char **argv) {
{
cprint("Bantam Agent ready", std::array{1, 32}); printf(" ");
cprint(" (Enter submits; Ctrl+J or paste = new line)\n", std::array{2});
fflush(stdout);
cfg.context_window = fetch_context_window(cfg);
std::string info = sfmt("endpoint: %s model: %s temp: %g reasoning: %s context: %d\n",
cfg.endpoint.c_str(), cfg.model.c_str(), cfg.temperature, re.c_str(), cfg.context_window);
cprint(info, std::array{2});