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
+37 -4
View File
@@ -138,6 +138,7 @@ struct Cfg {
int force_stream = -1; /* -1 = auto, 0/1 = force (compaction) */
int context_window = 262144;
int max_tool_res = 65536;
bool context_window_set = false;
std::map<std::string, std::string> raw; /* raw key/value map */
};
@@ -180,17 +181,48 @@ inline std::string cstr(std::string_view text, const R& codes) {
s += "\033[";
for (size_t i = 0; i < codes.size(); i++) {
if (i) s += ';';
s += sfmt("%d", (int)codes[i]);
int v = (int)codes[i];
if (v <= 0) {
s += '0';
} else {
char rev[12];
int rn = 0;
while (v > 0 && rn < 12) { rev[rn++] = (char)('0' + (v % 10)); v /= 10; }
while (rn > 0) s += rev[--rn];
}
}
s += "m";
s += 'm';
s += text;
s += "\033[0m";
return s;
}
template<class R>
inline void cprint(std::string_view text, const R& codes) {
std::string w = cstr(text, codes);
std::fputs(w.c_str(), stdout);
if (text.empty()) return;
if (!g_col || codes.empty()) {
std::fwrite(text.data(), 1, text.size(), stdout);
return;
}
char seq[32];
size_t pos = 0;
seq[pos++] = '\033';
seq[pos++] = '[';
for (size_t i = 0; i < codes.size() && pos + 12 < sizeof(seq); i++) {
if (i) seq[pos++] = ';';
int v = (int)codes[i];
if (v <= 0) {
seq[pos++] = '0';
} else {
char rev[12];
int rn = 0;
while (v > 0 && rn < 12) { rev[rn++] = (char)('0' + (v % 10)); v /= 10; }
while (rn > 0) seq[pos++] = rev[--rn];
}
}
seq[pos++] = 'm';
std::fwrite(seq, 1, pos, stdout);
std::fwrite(text.data(), 1, text.size(), stdout);
std::fwrite("\033[0m", 1, 4, stdout);
}
int visible_len(std::string_view s);
int rune_width(int cp);
@@ -226,6 +258,7 @@ bool parse_nonstream_response(std::string_view body, Message &out, Usage &usage)
/* ------------------------------------------------------------------ */
/* llm.cpp — HTTP client (libcurl), headers, backoff, dispatch. */
/* ------------------------------------------------------------------ */
void http_cleanup();
int http_get(Cfg &cfg, std::string_view url, std::string &body, long &code,
std::string &errmsg);
int llm(Cfg &cfg, Messages &msgs, bool use_tools, Message &out, Usage &usage,