NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
simple_session.cpp

Build a credential-free client, session, stateful tool, and tracers.

// Copyright 2026 Aniket Kulkarni
// SPDX-License-Identifier: Apache-2.0
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
using namespace neuralplus;
namespace {
AIResponse demo_model(const AIRequest& request) {
std::string latest_tool_output;
for (const Message& message : request.messages) {
if (message.role() == Role::tool) {
latest_tool_output = message.text();
}
}
if (!latest_tool_output.empty()) {
AIResponse response{Message::assistant(
"The final tool output is " + latest_tool_output + ".")};
response.finish_reason = FinishReason::stop;
response.provider_model = "local-demo";
return response;
}
ToolCall first;
first.id = "increment-1";
first.name = "increment";
first.arguments = JsonValue{{"delta", 2}};
ToolCall second;
second.id = "increment-2";
second.name = "increment";
second.arguments = JsonValue{{"delta", 3}};
AIResponse response{
Message::assistant("I will update the counter.", {first, second})};
response.finish_reason = FinishReason::tool_calls;
response.provider_model = "local-demo";
return response;
}
} // namespace
int main() {
ToolSpec spec;
spec.name = "increment";
spec.description = "Atomically increment this session's counter.";
spec.input_schema = {
{"type", "object"},
{"properties", {{"delta", {{"type", "integer"}}}}},
{"required", {"delta"}},
};
auto increment = std::make_shared<FunctionTool>(
std::move(spec),
[](ToolContext& context, const JsonValue& arguments) {
const int delta = arguments.at("delta").get<int>();
const int value = context.state().update<int>(
"counter", 0,
[delta](int current) { return current + delta; });
return ToolOutput::json({{"counter", value}});
});
ClientOptions options;
options.tools = {increment};
options.tracers = {std::make_shared<ConsoleTracer>()};
model.provider = Provider::custom;
model.id = "local-demo";
model.display_name = "Local demo";
model.capabilities.tools = true;
FunctionAIClient client{std::move(model), demo_model, options};
Session session;
const AIResponse response =
client.generate(session, "Increment the counter by two and three.");
const int counter = session.state().get<int>("counter").value_or(-1);
std::cout << response.message.text() << '\n';
std::cout << "Session counter: " << counter << '\n';
std::cout << "Model rounds: " << response.model_rounds
<< ", tool calls: " << response.tool_calls << '\n';
return counter == 5 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Definition client.hpp:128
A valid conversation message with one normalized role.
Definition types.hpp:208
std::string text() const
Concatenates text parts without discarding non-text parts from the message.
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
Definition session.hpp:106
SessionState & state() noexcept
Returns the session-scoped generic cache.
Definition tool.hpp:28
SessionState & state() const noexcept
Returns the session-scoped state available to the tool.
Immutable snapshot passed to one provider round.
Definition types.hpp:372
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
FinishReason finish_reason
Normalized provider stop reason.
Definition types.hpp:401
std::size_t tool_calls
Tool invocations completed by the public generate call.
Definition types.hpp:416
Message message
Assistant message returned by the provider.
Definition types.hpp:398
std::size_t model_rounds
Provider rounds completed by the public generate call.
Definition types.hpp:413
Dependencies and behavior shared by all AIClient implementations.
Definition client.hpp:25
Tools tools
Immutable set of tools advertised to the configured model.
Definition client.hpp:27
bool tools
Supports function/tool declarations.
Definition types.hpp:308
bool parallel_tool_calls
May request more than one tool in a provider round.
Definition types.hpp:311
Data-driven model description; no C++ subclass is needed per model name.
Definition types.hpp:315
Provider provider
Provider protocol used by the configured client.
Definition types.hpp:317
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
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
JsonValue arguments
Parsed arguments. This is an empty object when parsing failed.
Definition types.hpp:176
Description and JSON Schema advertised to a model.
Definition types.hpp:189
JsonValue input_schema
JSON Schema whose root type must be object.
Definition types.hpp:197
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
nlohmann::json JsonValue
Definition types.hpp:26