83 lines
2.4 KiB
C++
83 lines
2.4 KiB
C++
#include "Net.hpp"
|
|||
|
|
|
||
|
|
#include "Caps.hpp"
|
||
|
|
|
||
|
|
namespace molo::example::fw {
|
||
|
|
|
||
|
|
RestClient::RestClient(RestTransport transport)
|
||
|
|
: transport_(std::move(transport)) {}
|
||
|
|
|
||
|
|
Response RestClient::get(const std::string& path) {
|
||
|
|
Request req;
|
||
|
|
req.method = Method::Get;
|
||
|
|
req.path = path;
|
||
|
|
return transport_(std::move(req));
|
||
|
|
}
|
||
|
|
|
||
|
|
Response RestClient::post_json(const std::string& path,
|
||
|
|
const std::string& json) {
|
||
|
|
Request req;
|
||
|
|
req.method = Method::Post;
|
||
|
|
req.path = path;
|
||
|
|
req.body = json;
|
||
|
|
req.headers.set("Content-Type", "application/json");
|
||
|
|
return transport_(std::move(req));
|
||
|
|
}
|
||
|
|
|
||
|
|
void OpenApi::add(const std::string& path, const std::string& method,
|
||
|
|
const std::string& summary) {
|
||
|
|
if (routes_.size() >= caps::kMaxOpenApiRoutes) return;
|
||
|
|
routes_.push_back({path, method, summary});
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string OpenApi::to_json() const {
|
||
|
|
std::string out =
|
||
|
|
"{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"living-example\","
|
||
|
|
"\"version\":\"0.1.0\"},\"paths\":{";
|
||
|
|
bool first = true;
|
||
|
|
for (const auto& r : routes_) {
|
||
|
|
if (!first) out += ",";
|
||
|
|
first = false;
|
||
|
|
out += "\"" + json_escape(r.path) + "\":{\"" + json_escape(r.method) +
|
||
|
|
"\":{\"summary\":\"" + json_escape(r.summary) + "\"}}";
|
||
|
|
}
|
||
|
|
return out + "}}";
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t OpenApi::size() const { return routes_.size(); }
|
||
|
|
|
||
|
|
std::string redact_secrets(const std::string& text) {
|
||
|
|
std::string out = text;
|
||
|
|
const char* keys[] = {"DEVPLACE_API_KEY", "API_KEY", "SECRET", "TOKEN",
|
||
|
|
"PASSWORD"};
|
||
|
|
for (const char* k : keys) {
|
||
|
|
std::string key(k);
|
||
|
|
std::size_t pos = 0;
|
||
|
|
while ((pos = out.find(key, pos)) != std::string::npos) {
|
||
|
|
std::size_t eol = out.find('\n', pos);
|
||
|
|
if (eol == std::string::npos) eol = out.size();
|
||
|
|
std::size_t eq = out.find('=', pos);
|
||
|
|
if (eq != std::string::npos && eq < eol) {
|
||
|
|
out.replace(eq + 1, eol - eq - 1, "[REDACTED]");
|
||
|
|
pos = eq + 1;
|
||
|
|
} else {
|
||
|
|
pos += key.size();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
AiSample ai_harness_sample(const std::string& prompt) {
|
||
|
|
AiSample s;
|
||
|
|
s.base_url = "https://devplace.net/openai/v1";
|
||
|
|
s.model = "molodetz";
|
||
|
|
s.redacted_prompt = redact_secrets(prompt);
|
||
|
|
s.stream_chunks = {"data: {\"delta\":\"plan: add route\"}\n\n",
|
||
|
|
"data: {\"delta\":\"apply: 1 file\"}\n\n",
|
||
|
|
"data: [DONE]\n\n"};
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::example::fw
|