Entropic 2.11.1
Local-first agentic inference engine
Loading...
Searching...
No Matches
tool_call_history.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
10
11#include <nlohmann/json.hpp>
12
13#include <algorithm>
14
15static auto logger = entropic::log::get("mcp.history");
16
17namespace entropic {
18
31 : capacity_(capacity) {
32 buffer_.resize(capacity);
33 logger->info("ToolCallHistory initialized (capacity={})", capacity);
34}
35
50 std::unique_lock lock(mutex_);
51 buffer_[head_] = entry;
52 head_ = (head_ + 1) % capacity_;
53 if (count_ < capacity_) {
54 ++count_;
55 }
56 logger->info("Recorded: tool='{}', {}/{} slots",
57 entry.tool_name, count_, capacity_);
58}
59
72std::vector<ToolCallRecord> ToolCallHistory::recent(size_t count) const {
73 std::shared_lock lock(mutex_);
74 size_t n = std::min(count, count_);
75 std::vector<ToolCallRecord> result;
76 result.reserve(n);
77
78 for (size_t i = 0; i < n; ++i) {
79 size_t idx = (head_ + capacity_ - 1 - i) % capacity_;
80 result.push_back(buffer_[idx]);
81 }
82 return result;
83}
84
97std::vector<ToolCallRecord> ToolCallHistory::all() const {
98 std::shared_lock lock(mutex_);
99 std::vector<ToolCallRecord> result;
100 result.reserve(count_);
101
102 size_t start = (count_ < capacity_) ? 0
103 : head_;
104 for (size_t i = 0; i < count_; ++i) {
105 size_t idx = (start + i) % capacity_;
106 result.push_back(buffer_[idx]);
107 }
108 return result;
109}
110
120static nlohmann::json record_to_json(const ToolCallRecord& rec) {
121 nlohmann::json j;
122 j["sequence"] = rec.sequence;
123 j["tool_name"] = rec.tool_name;
124 j["params_summary"] = rec.params_summary;
125 j["status"] = rec.status;
126 j["result_summary"] = rec.result_summary;
127 j["elapsed_ms"] = rec.elapsed_ms;
128 j["iteration"] = rec.iteration;
129 if (!rec.error_detail.empty()) {
130 j["error_detail"] = rec.error_detail;
131 }
132 return j;
133}
134
144std::string ToolCallHistory::to_json(size_t count) const {
145 auto entries = (count == 0) ? all() : recent(count);
146 nlohmann::json arr = nlohmann::json::array();
147 for (const auto& rec : entries) {
148 arr.push_back(record_to_json(rec));
149 }
150 return arr.dump();
151}
152
160size_t ToolCallHistory::size() const {
161 std::shared_lock lock(mutex_);
162 return count_;
163}
164
176std::string summarize_params(const std::string& args_json) {
177 try {
178 auto j = nlohmann::json::parse(args_json);
179 if (!j.is_object()) {
180 return args_json;
181 }
182 std::string result;
183 for (auto it = j.begin(); it != j.end(); ++it) {
184 if (!result.empty()) {
185 result += ", ";
186 }
187 result += it.key();
188 }
189 return result;
190 } catch (...) {
191 return args_json;
192 }
193}
194
205std::string truncate_result(const std::string& text, size_t max_len) {
206 if (text.size() <= max_len) {
207 return text;
208 }
209 return text.substr(0, max_len) + "...";
210}
211
212} // namespace entropic
std::vector< ToolCallRecord > all() const
Get all stored entries (oldest first, insertion order).
ToolCallHistory(size_t capacity=100)
Construct with buffer capacity.
size_t size() const
Current number of stored entries.
void record(const ToolCallRecord &entry)
Record a completed tool call.
std::vector< ToolCallRecord > recent(size_t count) const
Get the N most recent entries (newest first).
std::string to_json(size_t count) const
Serialize recent entries to JSON array string.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Activate model on GPU (WARM → ACTIVE).
@ count
Sentinel — MUST remain last.
std::string truncate_result(const std::string &text, size_t max_len)
Truncate a string to max_len characters with "..." suffix.
std::string summarize_params(const std::string &args_json)
Extract top-level JSON keys as a comma-separated summary.
static nlohmann::json record_to_json(const ToolCallRecord &rec)
Serialize a single ToolCallRecord to JSON.
Lightweight record of a recent tool call for introspection.
size_t sequence
Monotonic sequence number.
std::string tool_name
Fully-qualified tool name.
int iteration
Loop iteration when called.
std::string error_detail
Error message if status != "success".
double elapsed_ms
Execution time in milliseconds.
std::string result_summary
First 200 chars of result.
std::string status
"success", "error", "timeout"
std::string params_summary
Top-level param keys only.
In-memory ring buffer of recent tool calls for introspection.