#pragma once // Session: the one object every service shares. It owns the llm::ApiClient // (x-api-key plus anthropic-version, llm::AuthStyle::kHeader), the default // anthropic-beta set, the optional workspace, and a response tap that keeps // the headers of the last response so rate limit headers and retry-after are // visible on successes and on errors alike. #include #include #include #include "anthropic/core/beta.h" #include "anthropic/core/calls.h" #include "anthropic/core/util.h" #include "anthropic/core/errors.h" #include "anthropic/core/version.h" #include "clients/transport.h" #include "llm/api_client.h" namespace molo::anthropic::core { struct SessionOptions { ApiVersion version = kDefaultVersion; std::string base_url = "https://api.anthropic.com"; // no trailing slash std::string key_env = "ANTHROPIC_API_KEY"; std::string workspace_id; // anthropic-workspace-id, empty: not sent BetaSet betas; // sent on every call, merged with per-call betas }; // Wraps a transport and remembers the headers of the last response. class ResponseTap : public clients::Transport { public: struct State { std::mutex mutex; clients::HeaderMap headers; int status = 0; bool seen = false; }; ResponseTap(std::unique_ptr inner, std::shared_ptr state); const char* name() const override { return inner_->name(); } clients::Task send(const clients::ClientRequest& request) override; clients::Task send_stream(const clients::ClientRequest& request, clients::body::ChunkSink sink) override; void close() override { inner_->close(); } private: std::unique_ptr inner_; std::shared_ptr state_; }; class Session { public: // Real HTTPS transport; the key comes from llm::Config (environment first). static std::shared_ptr from_env(SessionOptions options = {}); // Any transport (tests use a scripted one for requests only); empty key: // not configured. static std::shared_ptr with_transport(std::unique_ptr transport, std::string key, SessionOptions options = {}); llm::ApiClient& api() const { return *api_; } std::shared_ptr api_ptr() const { return api_; } const SessionOptions& options() const { return options_; } bool configured() const { return api_->configured(); } ApiVersion version() const { return options_.version; } // Headers for one call: session betas plus `betas`, workspace (call value // wins over the session value), user profile id, and an optional version // override (anthropic-version). llm::Headers headers(const BetaSet& betas = {}, const std::string& workspace_id = "", const std::string& user_profile_id = "", std::optional version = std::nullopt) const; // Headers of the most recent response; empty RateLimits before the first. RateLimits last_response() const; void forget_response() const; // Runs `f`; every llm::ApiError leaves as AnthropicError carrying the // rate limit headers of the failed response. template auto guarded(F&& f) const -> decltype(f()) { forget_response(); try { return f(); } catch (const AnthropicError&) { throw; } catch (const llm::ApiError& e) { bool local = e.type() == "NOT_CONFIGURED" || e.type() == "INVALID_ARGUMENT"; throw AnthropicError(e, local ? RateLimits{} : last_response()); } } private: Session(std::shared_ptr api, std::shared_ptr tap, SessionOptions options); std::shared_ptr api_; std::shared_ptr tap_; SessionOptions options_; }; // Base for every service in this module. class CoreService : public llm::Service { public: explicit CoreService(std::shared_ptr session) : llm::Service(session->api_ptr()), session_(std::move(session)) {} Session& session() const { return *session_; } // Betas sent with every call of this service (e.g. skills-2025-10-02 for the // legacy Skills shapes, scope_id on files). CoreService& with_betas(const BetaSet& betas) { betas_ = betas; return *this; } const BetaSet& betas() const { return betas_; } // JSON call through the call table: path from `call` with ids, `query` // appended, `body` sent when non-null. Errors leave as AnthropicError. Json call_json(Call call, const std::string& id1 = "", const std::string& id2 = "", const std::string& query = "", const Json* body = nullptr) const; protected: BetaSet betas_; std::shared_ptr session_; }; } // namespace molo::anthropic::core