Entropic 2.3.8
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
26 : capacity_(capacity) {
27 buffer_.resize(capacity);
28 logger->info("ToolCallHistory initialized (capacity={})", capacity);
29}
30
38 std::unique_lock lock(mutex_);
39 buffer_[head_] = entry;
40 head_ = (head_ + 1) % capacity_;
41 if (count_ < capacity_) {
42 ++count_;
43 }
44 logger->info("Recorded: tool='{}', {}/{} slots",
45 entry.tool_name, count_, capacity_);
46}
47
55std::vector<ToolCallRecord> ToolCallHistory::recent(size_t count) const {
56 std::shared_lock lock(mutex_);
57 size_t n = std::min(count, count_);
58 std::vector<ToolCallRecord> result;
59 result.reserve(n);
60
61 for (size_t i = 0; i < n; ++i) {
62 size_t idx = (head_ + capacity_ - 1 - i) % capacity_;
63 result.push_back(buffer_[idx]);
64 }
65 return result;
66}
67
74std::vector<ToolCallRecord> ToolCallHistory::all() const {
75 std::shared_lock lock(mutex_);
76 std::vector<ToolCallRecord> result;
77 result.reserve(count_);
78
79 size_t start = (count_ < capacity_) ? 0
80 : head_;
81 for (size_t i = 0; i < count_; ++i) {
82 size_t idx = (start + i) % capacity_;
83 result.push_back(buffer_[idx]);
84 }
85 return result;
86}
87
95static nlohmann::json record_to_json(const ToolCallRecord& rec) {
96 nlohmann::json j;
97 j["sequence"] = rec.sequence;
98 j["tool_name"] = rec.tool_name;
99 j["params_summary"] = rec.params_summary;
100 j["status"] = rec.status;
101 j["result_summary"] = rec.result_summary;
102 j["elapsed_ms"] = rec.elapsed_ms;
103 j["iteration"] = rec.iteration;
104 if (!rec.error_detail.empty()) {
105 j["error_detail"] = rec.error_detail;
106 }
107 return j;
108}
109
117std::string ToolCallHistory::to_json(size_t count) const {
118 auto entries = (count == 0) ? all() : recent(count);
119 nlohmann::json arr = nlohmann::json::array();
120 for (const auto& rec : entries) {
121 arr.push_back(record_to_json(rec));
122 }
123 return arr.dump();
124}
125
132size_t ToolCallHistory::size() const {
133 std::shared_lock lock(mutex_);
134 return count_;
135}
136
144std::string summarize_params(const std::string& args_json) {
145 try {
146 auto j = nlohmann::json::parse(args_json);
147 if (!j.is_object()) {
148 return args_json;
149 }
150 std::string result;
151 for (auto it = j.begin(); it != j.end(); ++it) {
152 if (!result.empty()) {
153 result += ", ";
154 }
155 result += it.key();
156 }
157 return result;
158 } catch (...) {
159 return args_json;
160 }
161}
162
171std::string truncate_result(const std::string& text, size_t max_len) {
172 if (text.size() <= max_len) {
173 return text;
174 }
175 return text.substr(0, max_len) + "...";
176}
177
178} // 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).
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.