NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
session.hpp
Go to the documentation of this file.
1// Copyright 2026 Aniket Kulkarni
2// SPDX-License-Identifier: Apache-2.0
3
6
7#pragma once
8
10#include "neuralplus/types.hpp"
11
12#include <any>
13#include <atomic>
14#include <functional>
15#include <mutex>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <unordered_map>
20#include <utility>
21#include <vector>
22
23namespace neuralplus {
24
25class AIClient;
26
30class NEURALPLUS_API SessionState final {
31public:
33 SessionState() = default;
34
36 template <typename T>
37 void set(std::string key, T value) {
38 std::lock_guard<std::mutex> lock(mutex_);
39 values_[std::move(key)] = std::move(value);
40 }
41
43 template <typename T>
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()) {
48 return std::nullopt;
49 }
50 const T* value = std::any_cast<T>(&iterator->second);
51 if (value == nullptr) {
52 throw std::bad_any_cast{};
53 }
54 return *value;
55 }
56
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{};
68 }
69 *current = std::invoke(std::forward<UpdateFunction>(update_function), *current);
70 return *current;
71 }
72
74 [[nodiscard]] bool contains(std::string_view key) const;
75
77 void erase(std::string_view key);
78
80 void clear();
81
82private:
83 SessionState(const SessionState&) = delete;
84 SessionState& operator=(const SessionState&) = delete;
85
86 mutable std::mutex mutex_;
87 std::unordered_map<std::string, std::any> values_;
88};
89
93 std::string id;
94
96 std::optional<std::string> system_message;
97
99 std::vector<Message> messages;
100};
101
106class NEURALPLUS_API Session final {
107public:
109 using Messages = std::vector<Message>;
110
112 explicit Session(SessionOptions options = {});
113 ~Session();
114
116 [[nodiscard]] const std::string& id() const noexcept;
117
121 void set_system(std::string message);
122
124 [[nodiscard]] std::optional<std::string> system() const;
125
127 void append(Message message);
128
130 [[nodiscard]] Messages messages() const;
131
133 void clear_messages();
134
136 [[nodiscard]] SessionState& state() noexcept;
137
139 [[nodiscard]] const SessionState& state() const noexcept;
140
141private:
142 friend class AIClient;
143
144 class RunLease final {
145 public:
146 RunLease(RunLease&& other) noexcept;
147 ~RunLease();
148
149 private:
150 friend class Session;
151 explicit RunLease(Session& session) noexcept;
152
153 RunLease(const RunLease&) = delete;
154 RunLease& operator=(const RunLease&) = delete;
155 RunLease& operator=(RunLease&&) = delete;
156
157 Session* session_;
158 };
159
160 [[nodiscard]] RunLease acquire();
161 void release() noexcept;
162 void append_from_client(Message message);
163
164 Session(const Session&) = delete;
165 Session& operator=(const Session&) = delete;
166
167 std::string id_;
168 mutable std::mutex messages_mutex_;
169 std::optional<std::string> system_message_;
170 Messages messages_;
171 SessionState state_;
172 std::atomic<bool> active_{false};
173};
174
175} // namespace neuralplus
Definition client.hpp:61
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