NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
types.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
11#include <nlohmann/json.hpp>
12
13#include <cstddef>
14#include <cstdint>
15#include <optional>
16#include <stdexcept>
17#include <string>
18#include <utility>
19#include <vector>
20
21namespace neuralplus {
22
26using JsonValue = nlohmann::json;
27
28// Object defaults below intentionally use `= JsonValue::object()`.
29// With nlohmann/json, `JsonValue{JsonValue::object()}` is a one-element array,
30// not an empty object: []-style initializer-list construction wins.
31
33enum class Provider {
34 openai,
35 anthropic,
36 gemini,
37 openai_compatible,
38 custom,
39};
40
42enum class Role {
43 system,
44 user,
45 assistant,
46 tool,
47};
48
50enum class FinishReason {
51 stop,
52 tool_calls,
53 length,
54 content_filter,
55 refusal,
56 error,
57 unknown,
58};
59
61enum class ContentType {
62 text,
63 image,
64 audio,
65 file,
66 extension,
67};
68
70enum class ContentSource {
71 value,
72 url,
73 bytes,
74 provider_data,
75};
76
78class NEURALPLUS_API Content final {
79public:
81 [[nodiscard]] static Content text(std::string value);
82
84 [[nodiscard]] static Content image_url(std::string url,
85 std::string media_type = {},
86 JsonValue options = JsonValue::object());
87
89 [[nodiscard]] static Content image_bytes(std::vector<std::uint8_t> bytes,
90 std::string media_type,
91 JsonValue options = JsonValue::object());
92
94 [[nodiscard]] static Content audio_url(std::string url,
95 std::string media_type = {},
96 JsonValue options = JsonValue::object());
97
99 [[nodiscard]] static Content audio_bytes(std::vector<std::uint8_t> bytes,
100 std::string media_type,
101 JsonValue options = JsonValue::object());
102
104 [[nodiscard]] static Content file_url(std::string url,
105 std::string media_type = {},
106 std::string filename = {},
107 JsonValue options = JsonValue::object());
108
110 [[nodiscard]] static Content file_bytes(std::vector<std::uint8_t> bytes,
111 std::string media_type,
112 std::string filename = {},
113 JsonValue options = JsonValue::object());
114
119 [[nodiscard]] static Content extension(std::string provider,
120 JsonValue provider_data);
121
123 [[nodiscard]] ContentType type() const noexcept;
124
126 [[nodiscard]] ContentSource source() const noexcept;
127
129 [[nodiscard]] const std::string& value() const noexcept;
130
132 [[nodiscard]] const std::string& url() const noexcept;
133
135 [[nodiscard]] const std::vector<std::uint8_t>& bytes() const noexcept;
136
138 [[nodiscard]] const std::string& media_type() const noexcept;
139
141 [[nodiscard]] const std::string& filename() const noexcept;
142
144 [[nodiscard]] const std::string& provider() const noexcept;
145
147 [[nodiscard]] const JsonValue& options() const noexcept;
148
150 [[nodiscard]] const JsonValue& provider_data() const noexcept;
151
152private:
153 Content(ContentType type, ContentSource source);
154
155 ContentType type_;
156 ContentSource source_;
157 std::string value_;
158 std::string url_;
159 std::vector<std::uint8_t> bytes_;
160 std::string media_type_;
161 std::string filename_;
162 std::string provider_;
163 JsonValue options_ = JsonValue::object();
164 JsonValue provider_data_ = JsonValue::object();
165};
166
168struct ToolCall {
170 std::string id;
171
173 std::string name;
174
176 JsonValue arguments = JsonValue::object();
177
179 std::string raw_arguments{"{}"};
180
182 bool arguments_valid{true};
183
185 JsonValue provider_metadata = JsonValue::object();
186};
187
189struct ToolSpec {
191 std::string name;
192
194 std::string description;
195
197 JsonValue input_schema =
198 JsonValue{{"type", "object"}, {"properties", JsonValue::object()}};
199
204 JsonValue provider_options = JsonValue::object();
205};
206
208class NEURALPLUS_API Message final {
209public:
211 using Contents = std::vector<Content>;
212
214 [[nodiscard]] static Message system(std::string text);
215
217 [[nodiscard]] static Message user(std::string text);
218
220 [[nodiscard]] static Message user(Contents contents);
221
223 [[nodiscard]] static Message assistant(std::string text,
224 std::vector<ToolCall> tool_calls = {});
225
227 [[nodiscard]] static Message assistant(Contents contents,
228 std::vector<ToolCall> tool_calls = {});
229
231 [[nodiscard]] static Message tool(std::string call_id,
232 std::string tool_name,
233 Contents contents,
234 bool is_error = false);
235
237 [[nodiscard]] static Message tool_json(std::string call_id,
238 std::string tool_name,
239 JsonValue value,
240 bool is_error = false);
241
243 [[nodiscard]] Role role() const noexcept;
244
246 [[nodiscard]] const Contents& contents() const noexcept;
247
249 [[nodiscard]] const std::vector<ToolCall>& tool_calls() const noexcept;
250
252 [[nodiscard]] const std::string& tool_call_id() const noexcept;
253
255 [[nodiscard]] const std::string& tool_name() const noexcept;
256
258 [[nodiscard]] bool is_tool_error() const noexcept;
259
261 [[nodiscard]] std::string text() const;
262
264 [[nodiscard]] const JsonValue& provider_metadata() const noexcept;
265
267 Message& set_provider_metadata(JsonValue metadata);
268
269private:
270 explicit Message(Role role);
271
272 Role role_;
273 Contents contents_;
274 std::vector<ToolCall> tool_calls_;
275 std::string tool_call_id_;
276 std::string tool_name_;
277 bool tool_error_{false};
278 JsonValue provider_metadata_ = JsonValue::object();
279};
280
284 bool text_input{true};
285
287 bool text_output{true};
288
290 bool image_input{false};
291
293 bool image_output{false};
294
296 bool audio_input{false};
297
299 bool audio_output{false};
300
302 bool file_input{false};
303
305 bool file_output{false};
306
308 bool tools{false};
309
311 bool parallel_tool_calls{false};
312};
313
317 Provider provider{Provider::custom};
318
320 std::string id;
321
323 std::string display_name;
324
327
329 std::size_t context_window{0};
330
332 std::optional<std::size_t> max_output_tokens;
333
335 JsonValue provider_options = JsonValue::object();
336};
337
341 std::optional<double> temperature;
342
344 std::optional<std::size_t> max_output_tokens;
345
347 JsonValue provider_options = JsonValue::object();
348};
349
353 std::optional<std::size_t> input_tokens;
354
356 std::optional<std::size_t> output_tokens;
357
359 std::optional<std::size_t> total_tokens;
360
362 std::optional<std::size_t> cached_input_tokens;
363
365 std::optional<std::size_t> cache_creation_input_tokens;
366
368 std::optional<std::size_t> reasoning_tokens;
369};
370
372struct AIRequest {
374 std::string session_id;
375
377 std::string run_id;
378
380 std::optional<std::string> system_message;
381
383 std::vector<Message> messages;
384
386 std::vector<ToolSpec> tools;
387
390};
391
393struct NEURALPLUS_API AIResponse {
395 explicit AIResponse(Message message);
396
399
401 FinishReason finish_reason{FinishReason::unknown};
402
405
408
410 std::string provider_model;
411
413 std::size_t model_rounds{1};
414
416 std::size_t tool_calls{0};
417
423 bool requires_continuation{false};
424
426 JsonValue provider_metadata = JsonValue::object();
427};
428
430class NEURALPLUS_API Error : public std::runtime_error {
431public:
432 using std::runtime_error::runtime_error;
433};
434
436class NEURALPLUS_API ConfigurationError final : public Error {
437public:
438 using Error::Error;
439};
440
442class NEURALPLUS_API SessionInUseError final : public Error {
443public:
444 using Error::Error;
445};
446
448class NEURALPLUS_API MaxRoundsError final : public Error {
449public:
450 using Error::Error;
451};
452
454class NEURALPLUS_API TransportError final : public Error {
455public:
456 using Error::Error;
457};
458
460class NEURALPLUS_API ProviderError final : public Error {
461public:
463 ProviderError(std::string message,
464 Provider provider,
465 int status,
466 std::string code = {},
467 std::string request_id = {});
468
470 [[nodiscard]] Provider provider() const noexcept;
471
473 [[nodiscard]] int status() const noexcept;
474
476 [[nodiscard]] const std::string& code() const noexcept;
477
479 [[nodiscard]] const std::string& request_id() const noexcept;
480
481private:
482 Provider provider_;
483 int status_;
484 std::string code_;
485 std::string request_id_;
486};
487
489[[nodiscard]] NEURALPLUS_API const char* to_string(Provider provider) noexcept;
490
492[[nodiscard]] NEURALPLUS_API const char* to_string(Role role) noexcept;
493
495[[nodiscard]] NEURALPLUS_API const char* to_string(FinishReason reason) noexcept;
496
497} // namespace neuralplus
Invalid or missing configuration.
Definition types.hpp:436
One text, image, audio, file, or provider-extension part.
Definition types.hpp:78
static Content audio_url(std::string url, std::string media_type={}, JsonValue options=JsonValue::object())
Creates audio referenced by URL.
static Content file_url(std::string url, std::string media_type={}, std::string filename={}, JsonValue options=JsonValue::object())
Creates a file referenced by URL.
static Content text(std::string value)
Creates a UTF-8 text part.
ContentType type() const noexcept
Returns the normalized kind of this content part.
static Content image_bytes(std::vector< std::uint8_t > bytes, std::string media_type, JsonValue options=JsonValue::object())
Creates an inline image. The provider adapter performs base64 encoding.
static Content image_url(std::string url, std::string media_type={}, JsonValue options=JsonValue::object())
Creates an image referenced by URL.
static Content extension(std::string provider, JsonValue provider_data)
static Content audio_bytes(std::vector< std::uint8_t > bytes, std::string media_type, JsonValue options=JsonValue::object())
Creates inline audio.
static Content file_bytes(std::vector< std::uint8_t > bytes, std::string media_type, std::string filename={}, JsonValue options=JsonValue::object())
Creates an inline file.
Base error for SDK failures.
Definition types.hpp:430
The configured number of provider requests was exhausted.
Definition types.hpp:448
A valid conversation message with one normalized role.
Definition types.hpp:208
static Message user(Contents contents)
Creates a multimodal user message.
static Message user(std::string text)
Creates a text user message.
static Message assistant(std::string text, std::vector< ToolCall > tool_calls={})
Creates a text assistant message, optionally containing tool calls.
Role role() const noexcept
Returns the normalized role of this message.
std::vector< Content > Contents
Ordered content-part collection used by multimodal messages.
Definition types.hpp:211
static Message system(std::string text)
Creates a system message. Sessions normally use Session::set_system.
static Message tool(std::string call_id, std::string tool_name, Contents contents, bool is_error=false)
Creates a tool result message.
static Message assistant(Contents contents, std::vector< ToolCall > tool_calls={})
Creates a multimodal assistant message.
static Message tool_json(std::string call_id, std::string tool_name, JsonValue value, bool is_error=false)
Creates a JSON tool result serialized as one text content part.
A provider returned an unsuccessful or malformed response.
Definition types.hpp:460
Provider provider() const noexcept
Returns the provider that produced the error.
ProviderError(std::string message, Provider provider, int status, std::string code={}, std::string request_id={})
Creates an error with normalized provider and HTTP correlation details.
A Session was already being used by another generation.
Definition types.hpp:442
An HTTP transport could not complete a request.
Definition types.hpp:454
Immutable snapshot passed to one provider round.
Definition types.hpp:372
std::string run_id
Identifier shared by all rounds of one generate call.
Definition types.hpp:377
GenerateOptions options
Normalized and provider-specific call options.
Definition types.hpp:389
std::vector< ToolSpec > tools
Tools available for this round.
Definition types.hpp:386
std::optional< std::string > system_message
Provider-independent system instruction.
Definition types.hpp:380
std::string session_id
Stable session identifier.
Definition types.hpp:374
std::vector< Message > messages
Complete normalized transcript snapshot.
Definition types.hpp:383
Normalized response returned by both one provider round and the full tool loop.
Definition types.hpp:393
std::string provider_request_id
Provider request/response correlation identifier.
Definition types.hpp:407
TokenUsage usage
Per-round usage from generate_once or cumulative usage from generate.
Definition types.hpp:404
std::string provider_model
Exact model identifier reported by the provider.
Definition types.hpp:410
AIResponse(Message message)
Creates a normalized response around one assistant message.
Message message
Assistant message returned by the provider.
Definition types.hpp:398
Per-call generation settings shared by all providers.
Definition types.hpp:339
std::optional< std::size_t > max_output_tokens
Maximum generated tokens for this call.
Definition types.hpp:344
std::optional< double > temperature
Sampling temperature, when the provider/model accepts it.
Definition types.hpp:341
Model features used for early validation and feature discovery.
Definition types.hpp:282
Data-driven model description; no C++ subclass is needed per model name.
Definition types.hpp:315
ModelCapabilities capabilities
Discoverable features used for early validation.
Definition types.hpp:326
std::string display_name
Optional user-interface label.
Definition types.hpp:323
std::string id
Provider model identifier.
Definition types.hpp:320
std::optional< std::size_t > max_output_tokens
Model-profile output limit applied when a call does not override it.
Definition types.hpp:332
Normalized token counts. Providers may leave unavailable values empty.
Definition types.hpp:351
std::optional< std::size_t > cache_creation_input_tokens
Input tokens written to a provider cache, when reported separately.
Definition types.hpp:365
std::optional< std::size_t > reasoning_tokens
Reasoning/thinking tokens, when reported.
Definition types.hpp:368
std::optional< std::size_t > input_tokens
Total tokens consumed from input, including provider cache reads/writes.
Definition types.hpp:353
std::optional< std::size_t > cached_input_tokens
Cached portion of input tokens, when reported.
Definition types.hpp:362
std::optional< std::size_t > total_tokens
Provider-reported or safely calculated total.
Definition types.hpp:359
std::optional< std::size_t > output_tokens
Tokens generated as output.
Definition types.hpp:356
A normalized provider-requested tool call.
Definition types.hpp:168
std::string id
Provider call identifier used to correlate the tool result.
Definition types.hpp:170
std::string name
Tool name requested by the provider.
Definition types.hpp:173
Description and JSON Schema advertised to a model.
Definition types.hpp:189
std::string name
Portable function name.
Definition types.hpp:191
std::string description
Human-readable instruction for deciding when to call the tool.
Definition types.hpp:194
ContentSource
Storage or reference form of a content part.
Definition types.hpp:70
ContentType
Kind of a multimodal content part.
Definition types.hpp:61
Role
Conversation role of a message.
Definition types.hpp:42
nlohmann::json JsonValue
Definition types.hpp:26
Provider
Provider family associated with a model descriptor.
Definition types.hpp:33
FinishReason
Normalized reason why generation stopped.
Definition types.hpp:50