feat(clients): xai core: Responses, chat, tools, media, batch, models, ChatModel
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
// Bounded polling for deferred results (chat, image, video). Elapsed time is
|
||||
// the sum of the sleeps, so the bound is deterministic and testable with a
|
||||
// fake sleeper. Nothing here waits unbounded.
|
||||
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
#include "xai/core/caps.h"
|
||||
#include "xai/core/params.h"
|
||||
|
||||
namespace molo::xai::core {
|
||||
|
||||
struct WaitPolicy {
|
||||
int interval_ms = Caps::kDefaultPollIntervalMs;
|
||||
int timeout_ms = Caps::kDefaultWaitMs;
|
||||
|
||||
void validate() const {
|
||||
Check::range("poll interval ms", interval_ms, Caps::kMinPollIntervalMs, Caps::kMaxPollIntervalMs);
|
||||
Check::range("wait timeout ms", timeout_ms, Caps::kMinPollIntervalMs, Caps::kMaxWaitMs);
|
||||
}
|
||||
// Upper bound of step() calls: one immediately, then one per interval.
|
||||
int max_polls() const { return timeout_ms / interval_ms + 1; }
|
||||
};
|
||||
|
||||
using Sleeper = std::function<void(int ms)>;
|
||||
Sleeper real_sleeper(); // std::this_thread::sleep_for
|
||||
|
||||
// Calls `step` until it yields a value. Returns nullopt when the policy's
|
||||
// budget is spent (the caller reports the timeout loudly).
|
||||
template <typename T>
|
||||
std::optional<T> poll_until(const WaitPolicy& policy, const std::function<std::optional<T>()>& step,
|
||||
const Sleeper& sleeper) {
|
||||
policy.validate();
|
||||
const int polls = policy.max_polls();
|
||||
for (int i = 0; i < polls; ++i) {
|
||||
if (std::optional<T> v = step()) return v;
|
||||
if (i + 1 < polls) sleeper(policy.interval_ms);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace molo::xai::core
|
||||
Reference in New Issue
Block a user