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:
2026-10-23 18:20:04 +02:00
parent 54068f1953
commit 3c4334cec9
1068 changed files with 100899 additions and 2 deletions
+102
View File
@@ -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
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include <functional>
#include <string>
#include <vector>
#include "ai/ai_transport.hpp"
namespace molo::ai {
struct ChatMessage {
std::string role;
std::string content;
};
struct AiClientOptions {
std::string base_url = "https://devplace.net/openai/v1";
std::string model = "molodetz";
int connect_timeout_sec = 30;
int session_cap_sec = 300;
};
// OpenAI-compatible chat client. Key comes from DEVPLACE_API_KEY env only,
// never a flag, never stored. Streaming responses parse as SSE.
class AiClient {
public:
AiClient(AiClientOptions opts, IAiTransport& transport);
// Calls on_delta per content delta; returns full assembled text.
// Returns empty and sets error on failure.
std::string chat_stream(const std::vector<ChatMessage>& messages,
const std::function<void(const std::string&)>& on_delta,
std::string& error) const;
const AiClientOptions& options() const { return opts_; }
static std::string request_json(const std::string& model,
const std::vector<ChatMessage>& messages, bool stream);
static std::string extract_delta(const std::string& data_json);
private:
AiClientOptions opts_;
IAiTransport& transport_;
};
} // namespace molo::ai
+58
View File
@@ -0,0 +1,58 @@
#include "ai/ai_session.hpp"
#include <fstream>
#include "caps.hpp"
namespace molo::ai {
AiSession::AiSession(std::string name)
: name_(std::move(name)), started_(std::chrono::steady_clock::now()) {}
bool AiSession::append(const std::string& role, const std::string& content, std::string& error) {
if (expired()) {
error = "session exceeded 300s cap";
return false;
}
if (transcript_bytes_ + role.size() + content.size() > caps::kAiTranscriptMaxBytes) {
error = "transcript exceeded 8 MB cap";
return false;
}
messages_.push_back(ChatMessage{role, content});
transcript_bytes_ += role.size() + content.size();
return true;
}
bool AiSession::expired() const {
auto elapsed = std::chrono::steady_clock::now() - started_;
return elapsed > std::chrono::seconds(caps::kAiSessionCapSec);
}
std::size_t AiSession::request_write(const std::string& path, const std::string& content) {
if (actions_ >= caps::kAiMaxActions) return static_cast<std::size_t>(-1);
++actions_;
pending_.push_back(PendingWrite{path, content});
return pending_.size() - 1;
}
bool AiSession::confirm_write(std::size_t index, std::string& error) {
if (index >= pending_.size()) {
error = "no such pending write";
return false;
}
const PendingWrite& write = pending_[index];
std::ofstream out(write.path, std::ios::binary | std::ios::trunc);
if (!out.good()) {
error = "cannot write " + write.path;
return false;
}
out << write.content;
pending_.erase(pending_.begin() + static_cast<long>(index));
return true;
}
void AiSession::reject_write(std::size_t index) {
if (index < pending_.size()) pending_.erase(pending_.begin() + static_cast<long>(index));
}
} // namespace molo::ai
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include <chrono>
#include <string>
#include <vector>
#include "ai/ai_client.hpp"
namespace molo::ai {
struct PendingWrite {
std::string path;
std::string content;
};
// Plan-then-apply session: the model proposes file writes, a human confirms
// each one, and only confirmation touches the filesystem. Caps: 8 MB
// transcript, 50 actions, 300 s session.
class AiSession {
public:
explicit AiSession(std::string name);
bool append(const std::string& role, const std::string& content, std::string& error);
std::size_t transcript_bytes() const { return transcript_bytes_; }
std::size_t action_count() const { return actions_; }
bool expired() const;
std::size_t request_write(const std::string& path, const std::string& content);
const std::vector<PendingWrite>& pending() const { return pending_; }
bool confirm_write(std::size_t index, std::string& error);
void reject_write(std::size_t index);
const std::string& name() const { return name_; }
const std::vector<ChatMessage>& messages() const { return messages_; }
private:
std::string name_;
std::vector<ChatMessage> messages_;
std::size_t transcript_bytes_ = 0;
std::size_t actions_ = 0;
std::vector<PendingWrite> pending_;
std::chrono::steady_clock::time_point started_;
};
} // namespace molo::ai
+23
View File
@@ -0,0 +1,23 @@
#include "ai/ai_transport.hpp"
namespace molo::ai {
StubAiTransport::StubAiTransport(std::vector<std::string> sse_bodies, int status)
: bodies_(std::move(sse_bodies)), status_(status) {}
AiHttpResponse StubAiTransport::post_stream(const AiHttpRequest& request) {
seen_.push_back(request);
AiHttpResponse out;
out.status = status_;
if (!bodies_.empty()) {
out.sse_body = bodies_.front();
bodies_.erase(bodies_.begin());
}
return out;
}
std::string TlsStub::unavailable_reason() {
return "https transport needs wolfSSL (not installed); plain http only in this build";
}
} // namespace molo::ai
+51
View File
@@ -0,0 +1,51 @@
#pragma once
#include <functional>
#include <string>
#include <vector>
namespace molo::ai {
struct AiHttpRequest {
std::string url;
std::string bearer_key;
std::string body_json;
int connect_timeout_sec = 30;
};
struct AiHttpResponse {
int status = 0;
std::string sse_body;
std::string error;
};
// Transport interface. In the full tree this is the 08-clients-openapi.md
// HTTP client; here a minimal socket transport plus a scripted stub.
class IAiTransport {
public:
virtual ~IAiTransport() = default;
virtual AiHttpResponse post_stream(const AiHttpRequest& request) = 0;
};
// Scripted transport for tests: returns canned SSE bodies.
class StubAiTransport : public IAiTransport {
public:
explicit StubAiTransport(std::vector<std::string> sse_bodies, int status = 200);
AiHttpResponse post_stream(const AiHttpRequest& request) override;
const std::vector<AiHttpRequest>& seen() const { return seen_; }
private:
std::vector<std::string> bodies_;
int status_;
std::vector<AiHttpRequest> seen_;
};
// MARKED STUB: HTTPS needs wolfSSL (sole dependency), which is not installed
// in this environment. Plain http:// works; https:// reports unavailable.
class TlsStub {
public:
static bool https_supported() { return false; }
static std::string unavailable_reason();
};
} // namespace molo::ai
+44
View File
@@ -0,0 +1,44 @@
#include "ai/prompt_pack.hpp"
namespace molo::ai {
std::string PromptPack::system_prompt() {
return "You are Molo, a C++23 web framework assistant. "
"Generate complete, compiling MoloApp code: routers, class-based views, "
"validation models, Jinja-like templates (extends, blocks, includes, if, for, "
"filters, auto-escaping), ORM models with Postgres/SQLite backends, auth, "
"sessions, forms, WebSockets, Socket.IO rooms, SSE streams, WebDAV handlers, "
"supervisor tasks, and integration tests mirroring URL paths. "
"Prefer small files, one class per file, namespaces mirroring directories. "
"Every knob gets a static cap. Never emit secrets; placeholders only. "
"Plan file writes first, then apply only after human confirmation.";
}
std::string PromptPack::feature_prompt(const std::string& topic) {
if (topic == "routing") {
return "Routing: FastAPI-style routers, path/query/body binding through the "
"validation framework, application chaining with automatic path merging.";
}
if (topic == "templates") {
return "Templates: inheritance with extends and blocks, includes, if/for, filters, "
"auto-escaping with explicit safe opt-out, i18n t() hooks, CSP nonces, "
"responsive mobile-first output.";
}
if (topic == "orm") {
return "ORM: dual Postgres/SQLite backends, auto-sync tables in dev and prod, "
"self-healing schema mismatches, parameterized queries only, pooling, "
"transactions, cache invalidation hooks.";
}
if (topic == "realtime") {
return "Realtime: WebSocket class views on routers, SSE streams, Socket.IO "
"namespaces/rooms/events with long-polling fallback, shared auth/session/"
"middleware/logging, client parity for every protocol.";
}
if (topic == "ops") {
return "Ops: /healthz /readyz /startupz probes, Prometheus 0.0.4 metrics with "
"series caps, OTLP bridge, CSP report endpoint, PO-based i18n.";
}
return "General MoloApp feature guidance: " + topic;
}
} // namespace molo::ai
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <string>
namespace molo::ai {
// Framework feature prompts for vibe-coding sessions.
class PromptPack {
public:
static std::string system_prompt();
static std::string feature_prompt(const std::string& topic);
};
} // namespace molo::ai
+79
View File
@@ -0,0 +1,79 @@
#include "ai/redactor.hpp"
#include <cctype>
namespace molo::ai {
bool Redactor::key_is_secret(const std::string& key) {
std::string upper;
for (char c : key) upper += static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
auto ends_with = [&](const std::string& suffix) {
return upper.size() >= suffix.size() &&
upper.compare(upper.size() - suffix.size(), suffix.size(), suffix) == 0;
};
return ends_with("_KEY") || ends_with("_SECRET") || ends_with("_TOKEN") ||
ends_with("APIKEY") || ends_with("API_KEY") || upper == "KEY" || upper == "SECRET" ||
upper == "TOKEN" || upper == "PASSWORD";
}
Redactor::Result Redactor::redact(const std::string& text) {
Result out;
out.text.reserve(text.size());
std::size_t i = 0;
while (i < text.size()) {
// Match a quoted or bare key followed by ':' or '='.
std::size_t key_start = i;
bool quoted = i < text.size() && text[i] == '"';
std::string key;
std::size_t j = i;
if (quoted) {
++j;
while (j < text.size() && text[j] != '"') {
key += text[j];
++j;
}
if (j < text.size()) ++j; // closing quote
} else {
while (j < text.size() &&
(std::isalnum(static_cast<unsigned char>(text[j])) || text[j] == '_')) {
key += text[j];
++j;
}
}
std::size_t k = j;
while (k < text.size() && (text[k] == ' ' || text[k] == '\t')) ++k;
if (!key.empty() && k < text.size() && (text[k] == ':' || text[k] == '=')) {
char sep = text[k];
std::size_t v = k + 1;
while (v < text.size() && (text[v] == ' ' || text[v] == '\t')) ++v;
if (key_is_secret(key)) {
out.text += text.substr(key_start, v - key_start);
if (v < text.size() && text[v] == '"') {
out.text += "\"***\"";
++v;
while (v < text.size() && text[v] != '"') {
if (text[v] == '\\') ++v;
++v;
}
if (v < text.size()) ++v;
} else {
out.text += "***";
while (v < text.size() && text[v] != '\n' && text[v] != ',' && text[v] != '}') ++v;
}
++out.redacted;
i = v;
if (i < text.size() && (text[i] == ',' || text[i] == '}')) {
out.text += text[i];
++i;
}
(void)sep;
continue;
}
}
out.text += text[i];
++i;
}
return out;
}
} // namespace molo::ai
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <string>
namespace molo::ai {
// Redacts secrets before prompt construction. Keys matching *_KEY,
// *_SECRET, *_TOKEN (case-insensitive) and .env.json-style assignments
// are masked. Redaction failures abort the request (caller checks ok()).
class Redactor {
public:
struct Result {
std::string text;
unsigned long redacted = 0;
bool ok = true;
};
static Result redact(const std::string& text);
static bool key_is_secret(const std::string& key);
};
} // namespace molo::ai
+127
View File
@@ -0,0 +1,127 @@
#include "ai/socket_transport.hpp"
#if !defined(_WIN32)
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <cstring>
namespace molo::ai {
SocketAiTransport::ParsedUrl SocketAiTransport::parse(const std::string& url) {
ParsedUrl out;
std::string rest = url;
if (rest.rfind("http://", 0) == 0) {
rest = rest.substr(7);
} else if (rest.rfind("https://", 0) == 0) {
out.https = true;
rest = rest.substr(8);
} else {
return out;
}
auto slash = rest.find('/');
std::string hostport = slash == std::string::npos ? rest : rest.substr(0, slash);
out.path = slash == std::string::npos ? "/" : rest.substr(slash);
auto colon = hostport.find(':');
if (colon != std::string::npos) {
out.host = hostport.substr(0, colon);
try {
out.port = std::stoi(hostport.substr(colon + 1));
} catch (...) {
return out;
}
} else {
out.host = hostport;
out.port = out.https ? 443 : 80;
}
if (!out.host.empty()) out.ok = true;
return out;
}
AiHttpResponse SocketAiTransport::post_stream(const AiHttpRequest& request) {
AiHttpResponse out;
ParsedUrl url = parse(request.url);
if (!url.ok) {
out.error = "bad URL: " + request.url;
return out;
}
if (url.https) {
out.error = TlsStub::unavailable_reason();
return out;
}
#if defined(_WIN32)
out.error = "socket transport is POSIX only";
return out;
#else
addrinfo hints{};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
addrinfo* list = nullptr;
if (getaddrinfo(url.host.c_str(), std::to_string(url.port).c_str(), &hints, &list) != 0) {
out.error = "dns failed for " + url.host;
return out;
}
int fd = -1;
for (addrinfo* ai = list; ai != nullptr; ai = ai->ai_next) {
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0) continue;
struct timeval tv;
tv.tv_sec = request.connect_timeout_sec;
tv.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) break;
close(fd);
fd = -1;
}
freeaddrinfo(list);
if (fd < 0) {
out.error = "connect failed to " + url.host;
return out;
}
std::string head = "POST " + url.path + " HTTP/1.1\r\nHost: " + url.host +
"\r\nContent-Type: application/json\r\nAccept: text/event-stream\r\n" +
"Authorization: Bearer " + request.bearer_key + "\r\nContent-Length: " +
std::to_string(request.body_json.size()) + "\r\nConnection: close\r\n\r\n";
std::string wire = head + request.body_json;
std::size_t sent = 0;
while (sent < wire.size()) {
ssize_t n = send(fd, wire.data() + sent, wire.size() - sent, 0);
if (n <= 0) {
close(fd);
out.error = "send failed";
return out;
}
sent += static_cast<std::size_t>(n);
}
std::string raw;
char buf[8192];
while (true) {
ssize_t n = recv(fd, buf, sizeof(buf), 0);
if (n <= 0) break;
raw.append(buf, static_cast<std::size_t>(n));
}
close(fd);
auto eol = raw.find("\r\n");
if (eol == std::string::npos) {
out.error = "bad response";
return out;
}
std::string status_line = raw.substr(0, eol);
auto sp = status_line.find(' ');
if (sp != std::string::npos) {
try {
out.status = std::stoi(status_line.substr(sp + 1));
} catch (...) {
out.status = 0;
}
}
auto hdr_end = raw.find("\r\n\r\n");
out.sse_body = hdr_end == std::string::npos ? "" : raw.substr(hdr_end + 4);
return out;
#endif
}
} // namespace molo::ai
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <string>
#include "ai/ai_transport.hpp"
namespace molo::ai {
// Minimal blocking HTTP transport over POSIX sockets. Plain http:// only;
// https:// URLs fail with the marked TLS stub reason.
class SocketAiTransport : public IAiTransport {
public:
AiHttpResponse post_stream(const AiHttpRequest& request) override;
private:
struct ParsedUrl {
std::string host;
int port = 80;
std::string path;
bool https = false;
bool ok = false;
};
static ParsedUrl parse(const std::string& url);
};
} // namespace molo::ai
+56
View File
@@ -0,0 +1,56 @@
#include "ai/sse_stream_parser.hpp"
namespace molo::ai {
void SseStreamParser::feed(const std::string& chunk) {
buffer_ += chunk;
std::size_t pos = 0;
while (true) {
std::size_t end = buffer_.find("\n\n", pos);
std::size_t sep_len = 2;
if (end == std::string::npos) {
end = buffer_.find("\r\n\r\n", pos);
sep_len = 4;
}
if (end == std::string::npos) break;
std::string block = buffer_.substr(pos, end - pos);
pos = end + sep_len;
std::string data;
std::size_t line_pos = 0;
while (line_pos < block.size()) {
std::size_t line_end = block.find('\n', line_pos);
std::string line = block.substr(line_pos, line_end == std::string::npos
? std::string::npos
: line_end - line_pos);
if (!line.empty() && line.back() == '\r') line.pop_back();
if (line.rfind("data:", 0) == 0) {
std::string payload = line.substr(5);
if (!payload.empty() && payload.front() == ' ') payload.erase(0, 1);
if (!data.empty()) data += "\n";
data += payload;
}
if (line_end == std::string::npos) break;
line_pos = line_end + 1;
}
if (data == "[DONE]") {
done_ = true;
} else if (!data.empty()) {
events_.push_back(data);
}
}
buffer_.erase(0, pos);
}
std::vector<std::string> SseStreamParser::take_events() {
std::vector<std::string> out = std::move(events_);
events_.clear();
return out;
}
void SseStreamParser::reset() {
buffer_.clear();
events_.clear();
done_ = false;
}
} // namespace molo::ai
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <string>
#include <vector>
namespace molo::ai {
// SSE parser for OpenAI-compatible chat streams. Shared with the 06-realtime
// SSE client contract: text/event-stream, data lines, blank-line framing,
// end on "data: [DONE]". This file owns the CLI copy; the realtime part
// reuses the same framing rules, never a duplicate parser.
class SseStreamParser {
public:
void feed(const std::string& chunk);
std::vector<std::string> take_events();
bool done() const { return done_; }
void reset();
private:
std::string buffer_;
std::vector<std::string> events_;
bool done_ = false;
};
} // namespace molo::ai