feat(clients): anthropic core: messages, streaming, tools, batches, models, files, skills, spec checker, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
#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 <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#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<clients::Transport> inner, std::shared_ptr<State> state);
|
||||
const char* name() const override { return inner_->name(); }
|
||||
clients::Task<clients::ClientResponse> send(const clients::ClientRequest& request) override;
|
||||
clients::Task<void> send_stream(const clients::ClientRequest& request,
|
||||
clients::body::ChunkSink sink) override;
|
||||
void close() override { inner_->close(); }
|
||||
|
||||
private:
|
||||
std::unique_ptr<clients::Transport> inner_;
|
||||
std::shared_ptr<State> state_;
|
||||
};
|
||||
|
||||
class Session {
|
||||
public:
|
||||
// Real HTTPS transport; the key comes from llm::Config (environment first).
|
||||
static std::shared_ptr<Session> from_env(SessionOptions options = {});
|
||||
// Any transport (tests use a scripted one for requests only); empty key:
|
||||
// not configured.
|
||||
static std::shared_ptr<Session> with_transport(std::unique_ptr<clients::Transport> transport,
|
||||
std::string key, SessionOptions options = {});
|
||||
|
||||
llm::ApiClient& api() const { return *api_; }
|
||||
std::shared_ptr<llm::ApiClient> 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<ApiVersion> 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 <typename F>
|
||||
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<llm::ApiClient> api, std::shared_ptr<ResponseTap::State> tap,
|
||||
SessionOptions options);
|
||||
|
||||
std::shared_ptr<llm::ApiClient> api_;
|
||||
std::shared_ptr<ResponseTap::State> tap_;
|
||||
SessionOptions options_;
|
||||
};
|
||||
|
||||
// Base for every service in this module.
|
||||
class CoreService : public llm::Service {
|
||||
public:
|
||||
explicit CoreService(std::shared_ptr<Session> 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> session_;
|
||||
};
|
||||
|
||||
} // namespace molo::anthropic::core
|
||||
Reference in New Issue
Block a user