feat(clients): llm vendor foundation: api client, SSE, errors, neutral chat types, ai.mk, example stubs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#include "llm/error.h"
|
||||
|
||||
#include "clients/body/json.h"
|
||||
|
||||
namespace molo::llm {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
ApiError::ApiError(std::string vendor, int http_status, std::string type,
|
||||
const std::string& message, std::string request_id)
|
||||
: std::runtime_error("molo::llm::" + vendor + ": " + type + ": " + message),
|
||||
vendor_(std::move(vendor)),
|
||||
http_status_(http_status),
|
||||
type_(std::move(type)),
|
||||
message_(message),
|
||||
request_id_(std::move(request_id)) {}
|
||||
|
||||
ApiError ApiError::invalid(const std::string& vendor, const std::string& message) {
|
||||
return ApiError(vendor, 400, "INVALID_ARGUMENT", message);
|
||||
}
|
||||
|
||||
NotConfiguredError::NotConfiguredError(const std::string& vendor,
|
||||
const std::string& env_var)
|
||||
: ApiError(vendor, 503, "NOT_CONFIGURED",
|
||||
"no credential configured (set " + env_var + ")") {}
|
||||
|
||||
namespace {
|
||||
std::string text_of(const Json* v) {
|
||||
if (!v) return "";
|
||||
if (v->is_string()) return v->as_string();
|
||||
if (v->is_number()) return std::to_string(v->as_int());
|
||||
return "";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ApiError error_from_response(const std::string& vendor,
|
||||
const clients::ClientResponse& response) {
|
||||
std::string type = "UNKNOWN";
|
||||
std::string message = "HTTP " + std::to_string(response.status());
|
||||
std::string request_id;
|
||||
for (const char* h : {"request-id", "x-request-id", "x-should-retry-request-id"})
|
||||
if (auto v = response.headers().get(h); v && !v->empty()) {
|
||||
request_id = *v;
|
||||
break;
|
||||
}
|
||||
try {
|
||||
Json doc = response.json();
|
||||
const Json* err = doc.is_object() ? doc.find("error") : nullptr;
|
||||
if (err && err->is_object()) {
|
||||
std::string t = text_of(err->find("type"));
|
||||
if (t.empty()) t = text_of(err->find("code"));
|
||||
if (!t.empty()) type = t;
|
||||
std::string m = text_of(err->find("message"));
|
||||
if (!m.empty()) message = m;
|
||||
} else if (err && err->is_string()) {
|
||||
message = err->as_string();
|
||||
} else if (doc.is_object()) {
|
||||
std::string m = text_of(doc.find("message"));
|
||||
if (!m.empty()) message = m;
|
||||
std::string t = text_of(doc.find("type"));
|
||||
if (t.empty()) t = text_of(doc.find("code"));
|
||||
if (!t.empty()) type = t;
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
// Non-JSON body: keep the generic type and message.
|
||||
}
|
||||
return ApiError(vendor, response.status(), type, message, request_id);
|
||||
}
|
||||
|
||||
} // namespace molo::llm
|
||||
Reference in New Issue
Block a user