ForestHub SDK 0.1.0
C++14 LLM SDK for PC and embedded platforms
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (c) 2026 ForestHub. All rights reserved.
3// For commercial licensing, visit https://github.com/ForestHubAI/fh-sdk
4
5#ifndef FORESTHUB_CORE_INPUT_HPP
6#define FORESTHUB_CORE_INPUT_HPP
7
10
11#include <cstdint>
12#include <memory>
13#include <string>
14#include <utility>
15#include <vector>
16
18
19namespace foresthub {
20namespace core {
21
22using json = nlohmann::json;
23
25enum class InputType : uint8_t {
28};
29
31class Input {
32public:
33 virtual ~Input() = default;
34
36 virtual InputType GetInputType() const = 0;
37
40 virtual void ToJson(json& j) const = 0;
41};
42
49
51class InputItem {
52public:
53 virtual ~InputItem() = default;
54
56 virtual InputItemType GetItemType() const = 0;
57
59 virtual std::string ToString() const = 0;
60
63 virtual void ToJson(json& j) const = 0;
64};
65
67class InputString : public Input, public InputItem {
68public:
69 std::string text;
70
72 explicit InputString(std::string text) : text(std::move(text)) {}
73
75 std::string ToString() const override { return text; }
79 InputType GetInputType() const override { return InputType::kString; }
81 void ToJson(json& j) const override { j = json{{"value", text}}; }
82};
83
85class InputItems : public Input {
86public:
87 std::vector<std::shared_ptr<InputItem>> items;
88
90 InputItems() = default;
91
93 explicit InputItems(const std::shared_ptr<InputItem>& item) { items.push_back(item); }
94
96 void PushBack(const std::shared_ptr<InputItem>& item) { items.push_back(item); }
97
99 InputType GetInputType() const override { return InputType::kItems; }
100
102 void ToJson(json& j) const override {
103 j = json::array();
104 for (const auto& item : items) {
105 json item_j;
106 if (item) {
107 item->ToJson(item_j);
108 } else {
109 item_j = nullptr;
110 }
111 j.push_back(std::move(item_j));
112 }
113 }
114
116 using Iterator = std::vector<std::shared_ptr<InputItem>>::iterator;
118 using ConstIterator = std::vector<std::shared_ptr<InputItem>>::const_iterator;
119
121 Iterator Begin() { return items.begin(); }
123 Iterator End() { return items.end(); }
125 ConstIterator Begin() const { return items.begin(); }
127 ConstIterator End() const { return items.end(); }
128
135 template <typename... Args>
136 void Insert(Args&&... args) {
137 items.insert(std::forward<Args>(args)...);
138 }
139};
140
144inline std::shared_ptr<InputItems> AsInputItems(const std::shared_ptr<Input>& input) {
145 if (!input) {
146 return std::make_shared<InputItems>();
147 }
148
149 switch (input->GetInputType()) {
151 return std::static_pointer_cast<InputItems>(input);
152 case InputType::kString: {
153 auto new_list = std::make_shared<InputItems>();
154 new_list->PushBack(std::static_pointer_cast<InputString>(input));
155 return new_list;
156 }
157 }
158
159 return std::make_shared<InputItems>();
160}
161
162} // namespace core
163} // namespace foresthub
164
165#endif // FORESTHUB_CORE_INPUT_HPP
Abstract base for items inside an InputItems list.
Definition input.hpp:51
virtual std::string ToString() const =0
Human-readable text representation of this item.
virtual void ToJson(json &j) const =0
Serialize this item to JSON.
virtual InputItemType GetItemType() const =0
Returns the type discriminator for safe downcasting via static_pointer_cast.
void ToJson(json &j) const override
Serialize all items as a JSON array.
Definition input.hpp:102
void PushBack(const std::shared_ptr< InputItem > &item)
Append an item to the list.
Definition input.hpp:96
InputItems(const std::shared_ptr< InputItem > &item)
Construct from a single item.
Definition input.hpp:93
std::vector< std::shared_ptr< InputItem > >::const_iterator ConstIterator
Const iterator over items.
Definition input.hpp:118
InputType GetInputType() const override
Identifies this input as an item list.
Definition input.hpp:99
InputItems()=default
Construct an empty item list.
Iterator End()
Returns a mutable past-the-end iterator.
Definition input.hpp:123
ConstIterator End() const
Returns a const past-the-end iterator.
Definition input.hpp:127
void Insert(Args &&... args)
Forwards all arguments to the underlying vector's insert().
Definition input.hpp:136
std::vector< std::shared_ptr< InputItem > >::iterator Iterator
Mutable iterator over items.
Definition input.hpp:116
std::vector< std::shared_ptr< InputItem > > items
Ordered conversation items.
Definition input.hpp:87
ConstIterator Begin() const
Returns a const iterator to the first item.
Definition input.hpp:125
Iterator Begin()
Returns a mutable iterator to the first item.
Definition input.hpp:121
std::string ToString() const override
Returns the stored text directly.
Definition input.hpp:75
std::string text
The raw text content.
Definition input.hpp:69
InputString(std::string text)
Construct from a text string.
Definition input.hpp:72
InputItemType GetItemType() const override
Identifies this item as a plain text string.
Definition input.hpp:77
InputType GetInputType() const override
Identifies this input as a string.
Definition input.hpp:79
void ToJson(json &j) const override
Serialize as JSON object with "value" key.
Definition input.hpp:81
Abstract base for all input types.
Definition input.hpp:31
virtual InputType GetInputType() const =0
Returns the type discriminator for this input.
virtual void ToJson(json &j) const =0
Serialize this input to JSON.
Wrapper for nlohmann/json that works around abs macro conflicts.
Core abstractions: requests, responses, tools, input types, and provider interface.
std::shared_ptr< InputItems > AsInputItems(const std::shared_ptr< Input > &input)
Normalize input to InputItems.
Definition input.hpp:144
InputItemType
Type discriminator for InputItem subclasses.
Definition input.hpp:44
@ kString
Plain text (InputString).
Definition input.hpp:45
@ kToolResult
Tool execution result (ToolResult).
Definition input.hpp:47
@ kToolCall
Tool call request from the model (ToolCallRequest).
Definition input.hpp:46
InputType
Type discriminator for Input subclasses.
Definition input.hpp:25
@ kString
Single text string.
Definition input.hpp:26
@ kItems
Ordered list of conversation items.
Definition input.hpp:27
Top-level namespace for the ForestHub SDK.