Entropic 2.11.1
Local-first agentic inference engine
Loading...
Searching...
No Matches
final_text.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
13#pragma once
14
15#include <nlohmann/json.hpp>
16
17#include <string>
18
19namespace facade_text {
20
55inline std::string extract_final_text(const char* result_json) {
56 if (result_json == nullptr) {
57 return {};
58 }
59 auto arr = nlohmann::json::parse(result_json, nullptr, false);
60 if (!arr.is_array()) {
61 return {};
62 }
63 for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
64 if (!it->is_object() || it->value("role", "") != "assistant") {
65 continue;
66 }
67 auto content = it->value("content", "");
68 if (!content.empty()) {
69 return content;
70 }
71 }
72 return {};
73}
74
93inline std::string no_response_reason(const char* result_json) {
94 auto arr = (result_json != nullptr)
95 ? nlohmann::json::parse(result_json, nullptr, false)
96 : nlohmann::json();
97 if (!arr.is_array()) {
98 return "(no response: the engine returned no readable conversation)";
99 }
100
101 bool saw_assistant = false;
102 for (const auto& msg : arr) {
103 if (msg.is_object() && msg.value("role", "") == "assistant") {
104 saw_assistant = true;
105 break;
106 }
107 }
108 if (!saw_assistant) {
109 return "(no response: the turn produced no assistant message at all)";
110 }
111 return "(no response: the turn ended with every assistant message empty — "
112 "the tier most likely stopped without calling entropic.complete)";
113}
114
122inline std::string final_text_or_reason(const char* result_json) {
123 auto text = extract_final_text(result_json);
124 return text.empty() ? no_response_reason(result_json) : text;
125}
126
127} // namespace facade_text
std::string no_response_reason(const char *result_json)
Explain why no answer text could be extracted (gh#130).
Definition final_text.h:93
std::string final_text_or_reason(const char *result_json)
Final answer text, or a diagnostic when there is none.
Definition final_text.h:122
std::string extract_final_text(const char *result_json)
Extract the answer text from a serialized conversation.
Definition final_text.h:55