Entropic 2.11.1
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
61bool is_effectively_empty(std::string_view s) {
62 for (unsigned char c : s) {
63 if (std::isspace(c) == 0) { return false; }
64 }
65 return true;
66}
67
80inline bool starts_with_json_error(std::string_view trimmed) {
81 if (trimmed.empty() || trimmed.front() != '{') { return false; }
82 auto pos = trimmed.find("\"error\"");
83 return pos != std::string_view::npos && pos < 32;
84}
85
102void truncate_to_cap(std::string& content, int cap) {
103 if (cap <= 0) { return; }
104 auto cap_sz = static_cast<size_t>(cap);
105 if (content.size() <= cap_sz) { return; }
106 size_t lost = content.size() - cap_sz;
107 std::string tail = "\n[... truncated, "
108 + std::to_string(lost) + " more bytes]";
109 size_t keep = (cap_sz > tail.size())
110 ? cap_sz - tail.size() : 0;
111 content.resize(keep);
112 content += tail;
113}
114
127bool looks_like_tool_error(std::string_view content) {
128 auto trimmed = ltrim(content);
129 return starts_with_ci(trimmed, "error")
130 || starts_with_ci(trimmed, "[error]")
131 || starts_with_json_error(trimmed);
132}
133
134} // 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.