final_text.h¶
src/facade/final_text.h
@file final_text.h @brief Operator-visible final-text extraction for the external bridge.
Owners: @tvanfossen
extract_final_text
std::string facade_text::extract_final_text(const char *result_json, const char *turn_prompt=nullptr, const ToolNames *tool_names=nullptr)
Extract the answer text from a serialized conversation.
src/facade/final_text.h:196
Called by SCENARIO, SCENARIO, failure_detail, final_answer, final_text_or_reason
Source · 15 lines
inline std::string extract_final_text(const char* result_json,
const char* turn_prompt = nullptr,
const ToolNames* tool_names = nullptr) {
auto arr = (result_json != nullptr)
? nlohmann::json::parse(result_json, nullptr, false)
: nlohmann::json();
std::string found;
if (arr.is_array()) {
const auto start = turn_start(arr, turn_prompt);
for (size_t i = arr.size(); i > start && found.empty(); --i) {
found = substantive_assistant_text(arr[i - 1], tool_names);
}
}
return found;
}Activity · 3 steps
Requirements
failure_detail
std::string facade_text::failure_detail(const std::string &reason, const char *turn_json, const ToolNames *tool_names)
A failed turn's reason, plus the turn's unconfirmed text if any.
src/facade/final_text.h:287
Source · 11 lines
inline std::string failure_detail(const std::string& reason,
const char* turn_json,
const ToolNames* tool_names) {
std::string out = reason;
const auto draft = extract_final_text(turn_json, nullptr, tool_names);
if (!draft.empty()) {
out += "\n\nUnconfirmed text from this turn (the turn did not complete, "
"so treat it as unverified):\n" + draft;
}
return out;
}Activity · 5 steps
Requirements
final_text_or_reason
std::string facade_text::final_text_or_reason(const char *result_json, const char *turn_prompt=nullptr, const ToolNames *tool_names=nullptr)
Final answer text, or a diagnostic when there is none.
src/facade/final_text.h:261
Called by SCENARIO, derive_async_final_state, render_turn
Source · 6 lines
inline std::string final_text_or_reason(const char* result_json,
const char* turn_prompt = nullptr,
const ToolNames* tool_names = nullptr) {
auto text = extract_final_text(result_json, turn_prompt, tool_names);
return text.empty() ? no_response_reason(result_json) : text;
}Requirements
no_response_reason
std::string facade_text::no_response_reason(const char *result_json)
Explain why no answer text could be extracted (gh#130).
src/facade/final_text.h:230
Source · 21 lines
inline std::string no_response_reason(const char* result_json) {
auto arr = (result_json != nullptr)
? nlohmann::json::parse(result_json, nullptr, false)
: nlohmann::json();
if (!arr.is_array()) {
return "(no response: the engine returned no readable conversation)";
}
bool saw_assistant = false;
for (const auto& msg : arr) {
if (msg.is_object() && msg.value("role", "") == "assistant") {
saw_assistant = true;
break;
}
}
if (!saw_assistant) {
return "(no response: the turn produced no assistant message at all)";
}
return "(no response: the turn ended with every assistant message empty — "
"the tier most likely stopped without calling entropic.complete)";
}Activity · 6 steps
Requirements
substantive_assistant_text
std::string facade_text::substantive_assistant_text(const nlohmann::json &msg, const ToolNames *tool_names)
An assistant message's text when it is an answer, else "".
src/facade/final_text.h:143
Source · 8 lines
inline std::string substantive_assistant_text(const nlohmann::json& msg,
const ToolNames* tool_names) {
std::string text;
if (msg.is_object() && string_field(msg, "role") == "assistant") {
text = string_field(msg, "content");
}
return is_completion_imitation(text, tool_names) ? std::string{} : text;
}Activity · 6 steps
Requirements
turn_start
size_t facade_text::turn_start(const nlohmann::json &arr, const char *turn_prompt)
Index just past THIS turn's user prompt; the turn's messages follow.
src/facade/final_text.h:121
Source · 12 lines
inline size_t turn_start(const nlohmann::json& arr, const char* turn_prompt) {
size_t start = (turn_prompt == nullptr) ? 0 : arr.size();
for (size_t i = arr.size(); turn_prompt != nullptr && i > 0; --i) {
const auto& msg = arr[i - 1];
if (msg.is_object() && string_field(msg, "role") == "user"
&& string_field(msg, "content") == turn_prompt) {
start = i;
break;
}
}
return start;
}