112 lines
4.2 KiB
C++
112 lines
4.2 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
// ApiClient: the one place a vendor module talks HTTP. Applies the vendor's
|
||
|
|
// auth style, sends JSON / multipart / raw bodies, streams SSE, maps every
|
||
|
|
// failure to ApiError. Runs over any clients::Transport (HttpTransport in
|
||
|
|
// production).
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <map>
|
||
|
|
#include <memory>
|
||
|
|
#include <optional>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
#include "clients/body/form.h"
|
||
|
|
#include "clients/body/json.h"
|
||
|
|
#include "clients/client.h"
|
||
|
|
#include "llm/error.h"
|
||
|
|
#include "llm/sse.h"
|
||
|
|
|
||
|
|
namespace molo::llm {
|
||
|
|
|
||
|
|
enum class AuthStyle { kBearer, kHeader };
|
||
|
|
|
||
|
|
struct ApiClientConfig {
|
||
|
|
std::string vendor; // "anthropic"
|
||
|
|
std::string base_url; // "https://api.anthropic.com", no trailing slash
|
||
|
|
std::string key_env; // "ANTHROPIC_API_KEY" (looked up via llm::Config)
|
||
|
|
AuthStyle auth = AuthStyle::kBearer;
|
||
|
|
std::string auth_header = "x-api-key"; // used for kHeader
|
||
|
|
std::map<std::string, std::string> default_headers; // e.g. anthropic-version
|
||
|
|
};
|
||
|
|
|
||
|
|
using Headers = std::map<std::string, std::string>;
|
||
|
|
|
||
|
|
struct RawResponse {
|
||
|
|
int status = 0;
|
||
|
|
std::string body;
|
||
|
|
std::string content_type;
|
||
|
|
clients::HeaderMap headers;
|
||
|
|
};
|
||
|
|
|
||
|
|
class ApiClient {
|
||
|
|
public:
|
||
|
|
// Empty key: not configured, every call throws NotConfiguredError before
|
||
|
|
// any network traffic.
|
||
|
|
ApiClient(std::unique_ptr<clients::Transport> transport, ApiClientConfig config,
|
||
|
|
std::string key);
|
||
|
|
|
||
|
|
// Real HTTPS transport, key from Config::get(config.key_env).
|
||
|
|
static std::shared_ptr<ApiClient> from_env(ApiClientConfig config);
|
||
|
|
|
||
|
|
bool configured() const { return !key_.empty(); }
|
||
|
|
const ApiClientConfig& config() const { return config_; }
|
||
|
|
clients::ClientStatsSnapshot stats() const { return client_.stats(); }
|
||
|
|
|
||
|
|
// JSON in, JSON out. body may be null (GET, DELETE). Empty 2xx body -> {}.
|
||
|
|
clients::body::Json request(clients::Method method, const std::string& path,
|
||
|
|
const clients::body::Json* body = nullptr,
|
||
|
|
const Headers& extra = {});
|
||
|
|
clients::body::Json get(const std::string& path, const Headers& extra = {});
|
||
|
|
clients::body::Json post(const std::string& path, const clients::body::Json& body,
|
||
|
|
const Headers& extra = {});
|
||
|
|
clients::body::Json patch(const std::string& path, const clients::body::Json& body,
|
||
|
|
const Headers& extra = {});
|
||
|
|
clients::body::Json del(const std::string& path, const Headers& extra = {});
|
||
|
|
|
||
|
|
// Bytes in/out for non-JSON payloads (file content, images, audio).
|
||
|
|
RawResponse request_raw(clients::Method method, const std::string& path,
|
||
|
|
const std::string* body = nullptr,
|
||
|
|
const std::string& content_type = "",
|
||
|
|
const Headers& extra = {});
|
||
|
|
clients::body::Json post_multipart(const std::string& path,
|
||
|
|
const clients::body::MultipartBody& form,
|
||
|
|
const Headers& extra = {});
|
||
|
|
|
||
|
|
// Streaming POST decoded as SSE; sink returns false to stop.
|
||
|
|
void stream(const std::string& path, const clients::body::Json& body,
|
||
|
|
const SseDecoder::Sink& sink, const Headers& extra = {});
|
||
|
|
|
||
|
|
private:
|
||
|
|
clients::ClientRequest build(clients::Method method, const std::string& path,
|
||
|
|
const Headers& extra) const;
|
||
|
|
void guard() const;
|
||
|
|
RawResponse finish(const clients::ClientResponse& response) const;
|
||
|
|
|
||
|
|
ApiClientConfig config_;
|
||
|
|
std::string key_;
|
||
|
|
clients::Client client_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Base for vendor service classes.
|
||
|
|
class Service {
|
||
|
|
public:
|
||
|
|
explicit Service(std::shared_ptr<ApiClient> client) : client_(std::move(client)) {}
|
||
|
|
virtual ~Service() = default;
|
||
|
|
bool configured() const { return client_->configured(); }
|
||
|
|
ApiClient& client() const { return *client_; }
|
||
|
|
|
||
|
|
// True for a single URL path segment: 1..256 of [A-Za-z0-9._:-] and not
|
||
|
|
// "." or "..". Use before putting any id in a path.
|
||
|
|
static bool valid_id(const std::string& id);
|
||
|
|
// Throws ApiError::invalid when the id is not a valid segment.
|
||
|
|
void check_id(const std::string& what, const std::string& id) const;
|
||
|
|
static std::string string_at(const clients::body::Json& doc, const std::string& key,
|
||
|
|
const std::string& fallback = "");
|
||
|
|
|
||
|
|
protected:
|
||
|
|
std::shared_ptr<ApiClient> client_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace molo::llm
|