1
0
mirror of synced 2024-12-21 12:05:54 +01:00
ImHex/plugins/builtin/include/content/popups/popup_docs_question.hpp

149 lines
5.9 KiB
C++
Raw Normal View History

2023-11-10 20:47:08 +01:00
#pragma once
2023-04-10 01:42:53 +02:00
#include <hex/ui/popup.hpp>
#include <hex/ui/imgui_imhex_extensions.h>
#include <hex/api/localization_manager.hpp>
2023-04-10 01:42:53 +02:00
#include <hex/helpers/http_requests.hpp>
#include <functional>
#include <string>
namespace hex::plugin::builtin {
class PopupDocsQuestion : public Popup<PopupDocsQuestion> {
public:
2023-12-31 13:53:44 +01:00
PopupDocsQuestion(const std::string &input = "")
: hex::Popup<PopupDocsQuestion>("hex.builtin.popup.docs_question.title", true, true) {
if (!input.empty()) {
m_inputBuffer = input;
this->executeQuery();
}
}
2023-04-10 01:42:53 +02:00
enum class TextBlockType {
Text,
Code
};
void drawContent() override {
ImGui::PushItemWidth(600_scaled);
2023-12-19 13:10:25 +01:00
ImGui::BeginDisabled(m_requestTask.isRunning());
if (ImGui::InputText("##input", m_inputBuffer, ImGuiInputTextFlags_EnterReturnsTrue)) {
2023-04-10 01:42:53 +02:00
this->executeQuery();
}
ImGui::EndDisabled();
ImGui::PopItemWidth();
if (ImGui::BeginChild("##answer", scaled(ImVec2(600, 350)), true, ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
2023-12-19 13:10:25 +01:00
if (!m_requestTask.isRunning()) {
if (m_answer.empty()) {
if (m_noAnswer)
ImGuiExt::TextFormattedCentered("{}", "hex.builtin.popup.docs_question.no_answer"_lang);
2023-04-10 01:42:53 +02:00
else
ImGuiExt::TextFormattedCentered("{}", "hex.builtin.popup.docs_question.prompt"_lang);
2023-04-10 01:42:53 +02:00
} else {
int id = 1;
2023-12-19 13:10:25 +01:00
for (auto &[type, text] : m_answer) {
ImGui::PushID(id);
2023-04-10 01:42:53 +02:00
switch (type) {
case TextBlockType::Text:
ImGuiExt::TextFormattedWrapped("{}", text);
2023-04-10 01:42:53 +02:00
break;
case TextBlockType::Code:
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyle().Colors[ImGuiCol_WindowBg]);
auto textWidth = 400_scaled - ImGui::GetStyle().FramePadding.x * 4 - ImGui::GetStyle().ScrollbarSize;
auto textHeight = ImGui::CalcTextSize(text.c_str(), nullptr, false, textWidth).y + ImGui::GetStyle().FramePadding.y * 6;
if (ImGui::BeginChild("##code", ImVec2(textWidth, textHeight), true)) {
ImGuiExt::TextFormattedWrapped("{}", text);
2023-04-10 01:42:53 +02:00
ImGui::EndChild();
}
ImGui::PopStyleColor();
break;
}
ImGui::PopID();
id += 1;
2023-04-10 01:42:53 +02:00
}
}
} else {
ImGuiExt::TextFormattedCentered("{}", "hex.builtin.popup.docs_question.thinking"_lang);
2023-04-10 01:42:53 +02:00
}
}
ImGui::EndChild();
2024-04-12 22:56:10 +02:00
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
this->close();
2023-04-10 01:42:53 +02:00
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize;
}
private:
void executeQuery() {
2023-12-19 13:10:25 +01:00
m_requestTask = TaskManager::createBackgroundTask("Query Docs", [this, input = m_inputBuffer](Task &) {
m_noAnswer = false;
2023-04-10 01:42:53 +02:00
for (auto space : { "xj7sbzGbHH260vbpZOu1", "WZzDdGjxmgMSIE3xly6o" }) {
2023-12-19 13:10:25 +01:00
m_answer.clear();
2023-04-10 01:42:53 +02:00
auto request = HttpRequest("POST", hex::format("https://api.gitbook.com/v1/spaces/{}/search/ask", space));
// Documentation API often takes a long time to respond, so we set a timeout of 30 seconds
request.setTimeout(30'000);
const nlohmann::json body = { { "query", input } };
request.setBody(body.dump());
2023-04-10 01:42:53 +02:00
request.addHeader("Content-Type", "application/json");
auto response = request.execute().get();
if (!response.isSuccess())
continue;
try {
auto json = nlohmann::json::parse(response.getData());
2023-12-31 13:53:44 +01:00
2023-04-10 01:42:53 +02:00
if (!json.contains("answer"))
continue;
auto answer = json["answer"]["text"].get<std::string>();
if (answer.empty())
break;
auto blocks = wolv::util::splitString(answer, "```");
for (auto &block : blocks) {
if (block.empty())
continue;
block = wolv::util::trim(block);
if (block.starts_with("rust\n")) {
block = block.substr(5);
2023-12-19 13:10:25 +01:00
m_answer.emplace_back(TextBlockType::Code, block);
2023-04-10 01:42:53 +02:00
} else if (block.starts_with("cpp\n")) {
block = block.substr(4);
2023-12-19 13:10:25 +01:00
m_answer.emplace_back(TextBlockType::Code, block);
2023-04-10 01:42:53 +02:00
} else {
2023-12-19 13:10:25 +01:00
m_answer.emplace_back(TextBlockType::Text, block);
2023-04-10 01:42:53 +02:00
}
}
} catch(...) {
continue;
}
}
2023-12-31 13:53:44 +01:00
m_noAnswer = m_answer.empty();
2023-04-10 01:42:53 +02:00
});
}
private:
std::string m_inputBuffer;
std::vector<std::pair<TextBlockType, std::string>> m_answer;
TaskHolder m_requestTask;
bool m_noAnswer = false;
};
}