feat(clients): meta: Meta Model API module, Muse Code protocol client, demo and tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
// Builds the ApiClient for the Meta Model API: bearer auth against
|
||||
// https://api.meta.ai/v1. The credential is META_API_KEY, or MODEL_API_KEY
|
||||
// (the name the docs' own examples use), looked up through llm::Config.
|
||||
// Also the retry policy the docs prescribe (exponential backoff with jitter on
|
||||
// 429 and transient 5xx, never on 504 or other 4xx).
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "llm/api_client.h"
|
||||
#include "llm/error.h"
|
||||
|
||||
namespace molo::meta {
|
||||
|
||||
struct MetaEndpoints {
|
||||
static constexpr const char* kVendor = "meta";
|
||||
static constexpr const char* kBaseUrl = "https://api.meta.ai/v1";
|
||||
static constexpr const char* kRealtimeUrl = "wss://api.meta.ai/v1/asr/realtime";
|
||||
static constexpr const char* kKeyEnv = "META_API_KEY";
|
||||
static constexpr const char* kAltKeyEnv = "MODEL_API_KEY";
|
||||
};
|
||||
|
||||
class MetaClient {
|
||||
public:
|
||||
// META_API_KEY first, then MODEL_API_KEY; empty when neither is set.
|
||||
static std::string key_from_env();
|
||||
static llm::ApiClientConfig config(const std::string& base_url = MetaEndpoints::kBaseUrl);
|
||||
// Real HTTPS transport, key from the environment (null key is fine: every
|
||||
// keyed call then throws NotConfiguredError before any traffic).
|
||||
static std::shared_ptr<llm::ApiClient> from_env();
|
||||
// Same, with an explicit key (empty = not configured).
|
||||
static std::shared_ptr<llm::ApiClient> with_key(const std::string& key);
|
||||
// Client for the one unauthenticated endpoint (GET /status). The docs say no
|
||||
// key is needed; a probe on 2026-10-24 showed a bogus bearer is ignored, so
|
||||
// a fixed placeholder is sent to satisfy ApiClient's non-empty key rule.
|
||||
static std::shared_ptr<llm::ApiClient> unauthenticated();
|
||||
static constexpr const char* kPlaceholderKey = "unauthenticated";
|
||||
};
|
||||
|
||||
struct RetryDecision {
|
||||
bool retry = false;
|
||||
int delay_ms = 0;
|
||||
std::string reason;
|
||||
};
|
||||
|
||||
class RetryPolicy {
|
||||
public:
|
||||
// 429 and 500/502/503 retry; 504 (gateway_timeout: the same request times
|
||||
// out again), 400/401/402/403/404/413 and everything else do not.
|
||||
static bool retryable(const llm::ApiError& error);
|
||||
// base * 2^attempt capped, plus deterministic jitter in [0, delay/2] derived
|
||||
// from `seed` so tests are exact. attempt counts from 0.
|
||||
static int delay_ms(int attempt, unsigned seed);
|
||||
static RetryDecision decide(const llm::ApiError& error, int attempt, unsigned seed);
|
||||
};
|
||||
|
||||
// Runs `f`, retrying per RetryPolicy up to MetaCaps::kMaxRetries times.
|
||||
// `sleep` is injectable so callers and tests control the waiting.
|
||||
template <typename F>
|
||||
auto call_with_retry(F&& f, unsigned seed = 1,
|
||||
const std::function<void(int)>& sleep = [](int ms) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
}) -> decltype(f()) {
|
||||
for (int attempt = 0;; ++attempt) {
|
||||
try {
|
||||
return f();
|
||||
} catch (const llm::ApiError& e) {
|
||||
RetryDecision d = RetryPolicy::decide(e, attempt, seed);
|
||||
if (!d.retry) throw;
|
||||
sleep(d.delay_ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace molo::meta
|
||||
Reference in New Issue
Block a user