Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
tool_result_classify.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
9
10#include <cctype>
11
12namespace entropic::mcp {
13
14namespace {
15
21inline bool starts_with_ci(std::string_view s, std::string_view prefix) {
22 if (s.size() < prefix.size()) { return false; }
23 for (size_t i = 0; i < prefix.size(); ++i) {
24 if (std::tolower(static_cast<unsigned char>(s[i]))
25 != std::tolower(static_cast<unsigned char>(prefix[i]))) {
26 return false;
27 }
28 }
29 return true;
30}
31
37inline std::string_view ltrim(std::string_view s) {
38 size_t i = 0;
39 while (i < s.size()
40 && std::isspace(static_cast<unsigned char>(s[i])) != 0) {
41 ++i;
42 }
43 return s.substr(i);
44}
45
46} // namespace
47
53bool is_effectively_empty(std::string_view s) {
54 for (unsigned char c : s) {
55 if (std::isspace(c) == 0) { return false; }
56 }
57 return true;
58}
59
65inline bool starts_with_json_error(std::string_view trimmed) {
66 if (trimmed.empty() || trimmed.front() != '{') { return false; }
67 auto pos = trimmed.find("\"error\"");
68 return pos != std::string_view::npos && pos < 32;
69}
70
76void truncate_to_cap(std::string& content, int cap) {
77 if (cap <= 0) { return; }
78 auto cap_sz = static_cast<size_t>(cap);
79 if (content.size() <= cap_sz) { return; }
80 size_t lost = content.size() - cap_sz;
81 std::string tail = "\n[... truncated, "
82 + std::to_string(lost) + " more bytes]";
83 size_t keep = (cap_sz > tail.size())
84 ? cap_sz - tail.size() : 0;
85 content.resize(keep);
86 content += tail;
87}
88
94bool looks_like_tool_error(std::string_view content) {
95 auto trimmed = ltrim(content);
96 return starts_with_ci(trimmed, "error")
97 || starts_with_ci(trimmed, "[error]")
98 || starts_with_json_error(trimmed);
99}
100
101} // namespace entropic::mcp
bool starts_with_json_error(std::string_view trimmed)
True if input begins with a JSON top-level "error" key.
Byte-level classifiers for tool-result content (#44, v2.1.0).
ENTROPIC_EXPORT bool looks_like_tool_error(std::string_view content)
Heuristic: the rendered tool-result text reads as an error.
ENTROPIC_EXPORT bool is_effectively_empty(std::string_view s)
True if s is empty or contains only ASCII whitespace.
ENTROPIC_EXPORT void truncate_to_cap(std::string &content, int cap)
Truncate tool-result content in place when it exceeds cap.