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

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
lines 204–204const auto start = turn_start(arr, turn_prompt);lines 205–205◇ for (size_t i = arr.size(); i > start && found.empty(); --i)lines 206–206found = substantive_assistant_text(arr[i - 1], tool_names);

Requirements

REQ-API-012

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

Show on the map →

Called by SCENARIO, WHEN, turn_outcome

Calls extract_final_text

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
lines 290–290std::string out = reason;lines 291–291const auto draft = extract_final_text(turn_json, nullptr, tool_names);lines 292–292◇ if (!draft.empty())lines 293–294out += "\n\nUnconfirmed text from this turn (the turn did not complete, " "so treat it as unverified):\n" + draft;lines 296–296return out;

Requirements

REQ-API-012

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

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

REQ-API-012

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

Show on the map →

Called by SCENARIO, final_text_or_reason

Calls none

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
lines 231–233auto arr = (result_json != nullptr) ? nlohmann::json::parse(result_json, nullptr, false) : nlohmann::json();lines 234–234◇ if (!arr.is_array())lines 235–235return "(no response: the engine returned no readable conversation)";lines 238–238bool saw_assistant = false;lines 239–245for (const auto& msg : arr) { if (msg.is_object() && msg.value("role", "") == "assistant") { saw_assistant = true; break; } } if (!saw_assistant) {lines 246–246return "(no response: the turn produced no assistant message at all)";

Requirements

REQ-API-012

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

Show on the map →

Called by extract_final_text

Calls is_completion_imitation, string_field

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
lines 145–145std::string text;lines 146–146◇ if (msg.is_object() && string_field(msg, "role") == "assistant")lines 147–147text = string_field(msg, "content");lines 149–149return is_completion_imitation(text, tool_names) ? std::stringlines 149–149:lines 149–149text;

Requirements

REQ-API-012

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

Show on the map →

Called by extract_final_text

Calls string_field

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;
}
Activity · 8 steps
lines 122–122size_t start = (turn_prompt == nullptr) ? 0 : arr.size();lines 123–123◇ for (size_t i = arr.size(); turn_prompt != nullptr && i > 0; --i)lines 124–124const autolines 124–124& msg = arr[i - 1];lines 125–126◇ if (msg.is_object() && string_field(msg, "role") == "user" && string_field(msg, "content") == turn_prompt)lines 127–127start = i;lines 128–128break;lines 131–131return start;

Requirements

REQ-API-012