NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
client.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
11#include "neuralplus/tool.hpp"
14#include "neuralplus/types.hpp"
15
16#include <functional>
17#include <memory>
18#include <mutex>
19#include <string>
20#include <unordered_map>
21
22namespace neuralplus {
23
28
31
33 std::shared_ptr<HttpTransport> transport;
34
36 std::size_t max_model_rounds{8};
37
40
43
46
48 bool stop_on_tool_error{false};
49
52};
53
61class NEURALPLUS_API AIClient {
62public:
63 virtual ~AIClient();
64
66 [[nodiscard]] const ModelDescriptor& model() const noexcept;
67
69 AIResponse generate(Session& session,
70 const Message& input,
71 const GenerateOptions& options = {});
72
75 const std::string& input,
76 const GenerateOptions& options = {});
77
80 const GenerateOptions& options = {});
81
83 AIResponse generate(const std::string& input,
84 const GenerateOptions& options = {});
85
86protected:
88 AIClient(ModelDescriptor model, const ClientOptions& options = {});
89
91 virtual AIResponse generate_once(const AIRequest& request) = 0;
92
94 void trace(const TraceEvent& event) const noexcept;
95
96private:
97 struct ToolExecution;
98 struct RegisteredTool {
99 std::shared_ptr<Tool> implementation;
100 ToolSpec spec;
101 };
102
103 [[nodiscard]] std::vector<ToolExecution>
104 execute_tools(Session& session,
105 const std::string& run_id,
106 const std::vector<ToolCall>& calls) const;
107
108 [[nodiscard]] ToolExecution
109 execute_tool(Session& session,
110 const std::string& run_id,
111 const ToolCall& call) const;
112
113 void validate_input(const Message& input) const;
114
115 AIClient(const AIClient&) = delete;
116 AIClient& operator=(const AIClient&) = delete;
117
118 ModelDescriptor model_;
119 ClientOptions options_;
120 std::unordered_map<std::string, RegisteredTool> tools_;
121 std::vector<ToolSpec> tool_specs_;
122};
123
128class NEURALPLUS_API FunctionAIClient final : public AIClient {
129public:
131 using Function = std::function<AIResponse(const AIRequest&)>;
132
135 Function function,
136 const ClientOptions& options = {});
137 ~FunctionAIClient() override;
138
139private:
140 FunctionAIClient(const FunctionAIClient&) = delete;
141 FunctionAIClient& operator=(const FunctionAIClient&) = delete;
142
143 AIResponse generate_once(const AIRequest& request) override;
144
145 std::mutex mutex_;
146 Function function_;
147};
148
149} // namespace neuralplus
Definition client.hpp:61
const ModelDescriptor & model() const noexcept
Returns the immutable model description used by this client.
AIResponse generate(Session &session, const std::string &input, const GenerateOptions &options={})
Convenience overload for text input.
virtual AIResponse generate_once(const AIRequest &request)=0
Performs exactly one provider request without mutating a Session.
AIClient(ModelDescriptor model, const ClientOptions &options={})
Creates a provider client for a model with shared tools, tracing, and transport.
void trace(const TraceEvent &event) const noexcept
Emits a custom event through this client's exception-isolated tracer fan-out.
AIResponse generate(const Message &input, const GenerateOptions &options={})
One-shot convenience overload that uses an internal Session.
AIResponse generate(const std::string &input, const GenerateOptions &options={})
One-shot convenience overload for text input.
Definition client.hpp:128
FunctionAIClient(ModelDescriptor model, Function function, const ClientOptions &options={})
Creates a client that delegates each model round to function.
std::function< AIResponse(const AIRequest &)> Function
Callback signature for one provider-independent model round.
Definition client.hpp:131
A valid conversation message with one normalized role.
Definition types.hpp:208
Definition session.hpp:106
Immutable snapshot passed to one provider round.
Definition types.hpp:372
Normalized response returned by both one provider round and the full tool loop.
Definition types.hpp:393
Dependencies and behavior shared by all AIClient implementations.
Definition client.hpp:25
std::shared_ptr< HttpTransport > transport
Injectable transport; null selects CurlHttpTransport.
Definition client.hpp:33
Tracers tracers
Zero or more tracing/logging destinations.
Definition client.hpp:30
std::size_t max_model_rounds
Hard limit for provider requests in one generation.
Definition client.hpp:36
Tools tools
Immutable set of tools advertised to the configured model.
Definition client.hpp:27
std::size_t max_parallel_tool_calls
Maximum number of tool callbacks executing concurrently.
Definition client.hpp:42
bool stop_on_tool_error
Returns after recording a failed tool batch instead of asking the model again.
Definition client.hpp:48
std::size_t max_tool_calls_per_generation
Maximum tool callbacks allowed across one generate call.
Definition client.hpp:45
bool capture_trace_payloads
Adds prompt/response/tool text to TraceEvent::payload when true.
Definition client.hpp:51
bool parallel_tool_calls
Executes calls from one provider round concurrently when true.
Definition client.hpp:39
Per-call generation settings shared by all providers.
Definition types.hpp:339
Data-driven model description; no C++ subclass is needed per model name.
Definition types.hpp:315
A normalized provider-requested tool call.
Definition types.hpp:168
Description and JSON Schema advertised to a model.
Definition types.hpp:189
One metadata-first trace record.
Definition tracing.hpp:42
std::vector< std::shared_ptr< Tool > > Tools
Shared tool collection accepted by ClientOptions.
Definition tool.hpp:130
std::vector< std::shared_ptr< Tracer > > Tracers
Shared tracer collection accepted by ClientOptions.
Definition tracing.hpp:215