feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include "ai/ai_client.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
|
||||
#include "ai/redactor.hpp"
|
||||
#include "ai/sse_stream_parser.hpp"
|
||||
#include "cli/json_writer.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
AiClient::AiClient(AiClientOptions opts, IAiTransport& transport)
|
||||
: opts_(std::move(opts)), transport_(transport) {}
|
||||
|
||||
std::string AiClient::request_json(const std::string& model,
|
||||
const std::vector<ChatMessage>& messages, bool stream) {
|
||||
std::vector<std::string> items;
|
||||
for (const auto& msg : messages) {
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["role"] = molo::cli::JsonWriter::quote(msg.role);
|
||||
fields["content"] = molo::cli::JsonWriter::quote(msg.content);
|
||||
items.push_back(molo::cli::JsonWriter::object(fields));
|
||||
}
|
||||
std::map<std::string, std::string> body;
|
||||
body["model"] = molo::cli::JsonWriter::quote(model);
|
||||
body["messages"] = molo::cli::JsonWriter::array(items);
|
||||
body["stream"] = stream ? "true" : "false";
|
||||
return molo::cli::JsonWriter::object(body);
|
||||
}
|
||||
|
||||
std::string AiClient::extract_delta(const std::string& data_json) {
|
||||
// Minimal pull of choices[0].delta.content without a JSON parser.
|
||||
auto pos = data_json.find("\"content\"");
|
||||
if (pos == std::string::npos) return "";
|
||||
pos = data_json.find(':', pos);
|
||||
if (pos == std::string::npos) return "";
|
||||
pos = data_json.find('"', pos);
|
||||
if (pos == std::string::npos) return "";
|
||||
std::string out;
|
||||
for (std::size_t i = pos + 1; i < data_json.size(); ++i) {
|
||||
char c = data_json[i];
|
||||
if (c == '\\' && i + 1 < data_json.size()) {
|
||||
char n = data_json[++i];
|
||||
if (n == 'n') {
|
||||
out += '\n';
|
||||
} else if (n == 't') {
|
||||
out += '\t';
|
||||
} else {
|
||||
out += n;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == '"') break;
|
||||
out += c;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string AiClient::chat_stream(const std::vector<ChatMessage>& messages,
|
||||
const std::function<void(const std::string&)>& on_delta,
|
||||
std::string& error) const {
|
||||
const char* key_raw = std::getenv("DEVPLACE_API_KEY");
|
||||
if (key_raw == nullptr || *key_raw == '\0') {
|
||||
error = "DEVPLACE_API_KEY is not set";
|
||||
return "";
|
||||
}
|
||||
if (opts_.base_url.rfind("https://", 0) == 0 && !TlsStub::https_supported()) {
|
||||
error = TlsStub::unavailable_reason();
|
||||
return "";
|
||||
}
|
||||
std::string body = request_json(opts_.model, messages, true);
|
||||
Redactor::Result redacted = Redactor::redact(body);
|
||||
if (!redacted.ok) {
|
||||
error = "redaction failed, request aborted";
|
||||
return "";
|
||||
}
|
||||
AiHttpRequest req;
|
||||
req.url = opts_.base_url + "/chat/completions";
|
||||
req.bearer_key = key_raw;
|
||||
req.body_json = redacted.text;
|
||||
req.connect_timeout_sec = opts_.connect_timeout_sec;
|
||||
AiHttpResponse resp = transport_.post_stream(req);
|
||||
if (resp.status != 200) {
|
||||
error = "AI endpoint status " + std::to_string(resp.status) + ": " + resp.error;
|
||||
return "";
|
||||
}
|
||||
SseStreamParser parser;
|
||||
parser.feed(resp.sse_body);
|
||||
std::string full;
|
||||
for (const auto& event : parser.take_events()) {
|
||||
std::string delta = extract_delta(event);
|
||||
full += delta;
|
||||
if (on_delta) on_delta(delta);
|
||||
}
|
||||
if (!parser.done()) {
|
||||
error = "stream ended without [DONE]";
|
||||
return "";
|
||||
}
|
||||
return full;
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
Reference in New Issue
Block a user