19#include <unordered_map>
37 void set(std::string key, T value) {
38 std::lock_guard<std::mutex> lock(mutex_);
39 values_[std::move(key)] = std::move(value);
44 [[nodiscard]] std::optional<T>
get(std::string_view key)
const {
45 std::lock_guard<std::mutex> lock(mutex_);
46 const auto iterator = values_.find(std::string(key));
47 if (iterator == values_.end()) {
50 const T* value = std::any_cast<T>(&iterator->second);
51 if (value ==
nullptr) {
52 throw std::bad_any_cast{};
61 template <
typename T,
typename UpdateFunction>
62 T
update(std::string key, T initial_value, UpdateFunction&& update_function) {
63 std::lock_guard<std::mutex> lock(mutex_);
64 auto result = values_.try_emplace(std::move(key), std::move(initial_value));
65 T* current = std::any_cast<T>(&result.first->second);
66 if (current ==
nullptr) {
67 throw std::bad_any_cast{};
69 *current = std::invoke(std::forward<UpdateFunction>(update_function), *current);
74 [[nodiscard]]
bool contains(std::string_view key)
const;
77 void erase(std::string_view key);
86 mutable std::mutex mutex_;
87 std::unordered_map<std::string, std::any> values_;
116 [[nodiscard]]
const std::string&
id() const noexcept;
121 void set_system(std::
string message);
124 [[nodiscard]] std::optional<std::
string> system() const;
133 void clear_messages();
144 class RunLease final {
146 RunLease(RunLease&& other)
noexcept;
151 explicit RunLease(
Session& session)
noexcept;
153 RunLease(
const RunLease&) =
delete;
154 RunLease& operator=(
const RunLease&) =
delete;
155 RunLease& operator=(RunLease&&) =
delete;
160 [[nodiscard]] RunLease acquire();
161 void release() noexcept;
162 void append_from_client(
Message message);
168 mutable std::mutex messages_mutex_;
169 std::optional<std::
string> system_message_;
172 std::atomic<
bool> active_{
false};
A valid conversation message with one normalized role.
Definition types.hpp:208
Definition session.hpp:30
std::optional< T > get(std::string_view key) const
Returns a copy of a typed value, or no value when the key is absent.
Definition session.hpp:44
void set(std::string key, T value)
Stores or replaces a typed value.
Definition session.hpp:37
SessionState()=default
Creates an empty process-local cache.
void clear()
Removes all cached values.
bool contains(std::string_view key) const
Returns whether the cache contains a key.
void erase(std::string_view key)
Removes one cached value.
T update(std::string key, T initial_value, UpdateFunction &&update_function)
Definition session.hpp:62
Definition session.hpp:106
std::vector< Message > Messages
Ordered collection used for conversation snapshots.
Definition session.hpp:109
const std::string & id() const noexcept
Returns the stable session identifier.
Session(SessionOptions options={})
Creates or restores a session from options.
Options used when creating or restoring a Session.
Definition session.hpp:91
std::string id
Stable application-provided ID, or empty to generate one.
Definition session.hpp:93
std::vector< Message > messages
Restored non-system transcript.
Definition session.hpp:99
std::optional< std::string > system_message
Provider-independent system instruction.
Definition session.hpp:96