#include "intentionrecognition.h"

#include <jsoncpp/json/json.h>

#include <algorithm>
#include <cctype>
#include <ctime>
#include <future>
#include <list>
#include <map>
#include <regex>
#include <sstream>
#include <string>
#include <vector>

#include "assistant/error.h"
#include "assistant/message.h"
#include "assistant/session.h"
#include "config/prompttemplate.h"
#include "dialoguemessage.h"
#include "fileinference/fileinference.h"
#include "imagehandler/imagehandler.h"
#include "modelinference/modelinference.h"
#include "slotsnormalization.h"
#include "systemscenarioclass.h"
#include "utils/logger.h"
#include "utils/util.h"
#include "variabletypeconversion.h"
namespace {
// 系统助手/文档问答
constexpr int systemAssistantPromptId = int(config::PromptTemplate::PromptId::FileQuestionAnswering);
constexpr int meetingInformationExtractionPromptId =
    int(config::PromptTemplate::PromptId::MeetingInformationExtraction);
constexpr int todoListGenerationPromptId = int(config::PromptTemplate::PromptId::TodoListGeneration);
constexpr int weeklyReportGenerationPromptId = int(config::PromptTemplate::PromptId::WeeklyReportGeneration);
constexpr int defaultPromptId = int(config::PromptTemplate::PromptId::Default);
const std::string userNamePrefix = "user_dialogue_id_";  // 用户名
const bool is_end_model_for_intention = true;            // 使用端模型-阿里
}  // namespace

IntentionRecognition::IntentionRecognition() {
    loadIntentRecognitionModel();  // 加载意图识别模型
    loadIntentAndSlots();          // 加载意图和槽位
    loadPromptText();              // 加载意图识别的prompt
    loadJsonContent();             // 加载文档/图片类功能接口名称

    slotsNormalization_ = std::make_shared<SlotsNormalization>();

    // 加载可支持的系统接口名称
    supportedSystemInterfaces_ = TypeTrans::readTxtToList(DialogueConfig::supportedSystemInterfacesFile);
    // 获取无槽位标签
    getIntentionScenariosWithoutSlots();

    // 加载RAG的prompt
    LOGI("导入RAG system prompt. --> {}", DialogueConfig::ragPromptFileStr);

    loadPrompt(DialogueConfig::ragPromptFileStr);
    ragPrompt_ = getIntentPrompt();

    LOGI("ragPrompt_ = {}", ragPrompt_);

    missingSlotScript = loadJsonFile(DialogueConfig::missingSlotScriptFileStr);

    slotsValueNormalJson = loadJsonFile(DialogueConfig::slotsValueFileStr);

    // 生成会话id
    dialogueId = userNamePrefix + TypeTrans::getTimeStr();
    LOGI("此次会话的id --> {}", dialogueId);

    // 当前轮次数据
    pCurrentDialogue_ = std::make_unique<DialogueData::Dialogue>(currentDialogue);
}

IntentionRecognition::~IntentionRecognition() {}

void IntentionRecognition::getIntentionScenariosWithoutSlots() {
    // 获取无槽位意图标签
    for (const auto &intentLabel : intentSlotJsonData.getMemberNames()) {
        if (intentSlotJsonData.isObject() && intentSlotJsonData[intentLabel].isObject() &&
            intentLabel == "noSlotsLabel") {
            for (const auto &key : intentSlotJsonData[intentLabel].getMemberNames()) {
                size_t splitIndex = key.find('(');
                std::string noSlotName = key.substr(0, splitIndex);
                intentionScenariosWithoutSlots.push_back(noSlotName);
            }
        }
    }

    LOGI("无槽位标签数量 = {}", intentionScenariosWithoutSlots.size());
}

void IntentionRecognition::deleteCustomizeKnowledgeBase(const std::string &knowledgeBaseName) {
    if (fileInferenceInterface_ != nullptr) {
        return fileInferenceInterface_->deleteFileKnowledgeBase(knowledgeBaseName);
    }
}

void IntentionRecognition::setSystemOperationInterface(SystemOperationInterface *interface) {
    if (systemOperationInterface_ == nullptr) {
        systemOperationInterface_ = interface;
    }
}

void IntentionRecognition::setFileInferenceInterface(FileInferenceInterface *interface) {
    fileInferenceInterface_ = interface;
}

void IntentionRecognition::setTextEmbeddingInterface(TextEmbeddingInterface *interface) {
    stringEmbeddingInterface_ = interface;
    if (stringEmbeddingInterface_ == nullptr) {
        return;
    }

    // 设置回调函数
    stringEmbeddingInterface_->setTextEmbeddingCallback(
        [this](const DialogueCallback::TextEmbeddingResult &embeddingData) {
            if (embeddingData.type == DialogueCallback::Type::EmbeddingNormal) {
                // 获取向量化结果
                textVectorResult = embeddingData.embedding;
            } else if (embeddingData.type == DialogueCallback::Type::EmbeddingErrorOccurred) {
                LOGE("{}", embeddingData.errorMessage.c_str());
                callOutputCallback(embeddingData.type, assistant::message::createErrorMessageResult(
                                                           embeddingData.errorCode, embeddingData.errorMessage));
            }
        });
}

void IntentionRecognition::dealChatModelOutput(const DialogueCallback::ChatData &result,
                                               const std::set<DialogueData::InformationSource> &ragSources) {
    Json::Value resultText = util::stringToJson(result.text_);

    if (resultText.isMember("result")) {
        chatReplyCache_ += resultText["result"].asString();
    }

    if (!result.modelName_.empty()) {
        currentModelNameInUse = result.modelName_;
        LOGI("currentModelNameInUse = {}", currentModelNameInUse);
    }

    if (!result.feedbackUrl_.empty()) {
        feedbackUrlForCloudModel = result.feedbackUrl_;
        LOGI("feedbackUrlForCloudModel = {}", feedbackUrlForCloudModel);
    }

    // 处理is_end
    if (resultText.isMember("is_end")) {
        if (resultText["is_end"].asBool()) {
            chatGenCompleteText_ = chatReplyCache_;
            chatReplyCache_.clear();
        }
    }

    // 添加rag检索的文件源
    if (promptId_ == systemAssistantPromptId) {
        // 系统助手中才添加源
        std::set<assistant::session::InformationSource> sources;
        for (const auto &ragSource : ragSources) {
            sources.insert({ragSource.name, ragSource.url, ragSource.id});
        }

        Json::Value sourcesJson = assistant::message::createJsonInformationSources(sources);

        resultText["information_sources"] = sourcesJson;
    }
    std::string message =
        assistant::message::createTextMessageFromStreamChatResult(resultText, result.modelName_, result.feedbackUrl_);
    // LOGI("dealChatModelOutput: {}", message);

    if (promptId_ != meetingInformationExtractionPromptId && promptId_ != todoListGenerationPromptId) {
        callOutputCallback(result.type_, message);
    } else {
        // 新建日程、生成待办场景，需要输出思考过程
        if (resultText.isMember("reasoning_content") && !resultText["reasoning_content"].empty()) {
            callOutputCallback(result.type_, message);
        }
    }
}

void IntentionRecognition::setModelInferenceInterface(ModelInferenceInterface *interface) {
    modelInferenceInterface_ = interface;
    modelInferenceInterface_->setChatModelCallback([this](const DialogueCallback::ChatData &chatData) {
        if (chatData.type_ == DialogueCallback::ChatNormal) {
            // 获取生成的文本内容
            dealChatModelOutput(chatData, ragFileSources);
        } else if (chatData.type_ == DialogueCallback::ChatErrorOccurred) {
            LOGE("Error info: {} {}.", chatData.errorCode_, chatData.errorMessage_);
            callOutputCallback(chatData.type_, assistant::message::createErrorMessageResult(chatData.errorCode_,
                                                                                            chatData.errorMessage_));
            chatGenCompleteText_.clear();
            chatReplyCache_.clear();
        }
    });
}

void IntentionRecognition::setImageHandlerInterface(ImageHandlerInterface *interface) {
    imageHandlerInterface_ = interface;
    if (imageHandlerInterface_ == nullptr) {
        return;
    }
    imageHandlerInterface_->setTextToImageCallback([this](const DialogueCallback::TextToImageResult &result) {
        std::string message;
        if (result.type_ == DialogueCallback::ImageErrorOccurred) {
            message = assistant::message::createImageDataMessageWithErrorCode(result.modelName_, "image_generation",
                                                                              result.errorCode_, result.errorMessage_);
            callOutputCallback(DialogueCallback::ImageErrorOccurred, message);
            return;
        }
        assistant::message::ImageData imageData{};
        imageData.success = result.success;
        imageData.modelName = result.modelName_;
        imageData.errorCode = result.errorCode_;
        imageData.errorMessage = result.errorMessage_;
        if (result.type_ == DialogueCallback::ImageGenerated) {
            imageData.imageFormat = result.imageFormat;
            imageData.imageBase64Data = result.imageBase64Data_;
            imageData_ = message;
        }

        // 获取feedbackUrl
        std::string feedbackStr = imageHandlerInterface_->getFeedbackUrl(result.modelName_);
        message = assistant::message::createImageDataMessage(
            imageData, result.type_ == DialogueCallback::ImageGenerating, feedbackStr);
        callOutputCallback(result.type_, message);
    });
    imageHandlerInterface_->setImageMattingCallback([this](const DialogueCallback::ImageMattingResult &result) {
        if (result.type == DialogueCallback::ImageErrorOccurred) {
            callOutputCallback(DialogueCallback::ImageErrorOccurred,
                               assistant::message::createImageDataMessageWithErrorCode(
                                   result.modelName, "tool_call", result.errorCode, result.errorMessage));
            return;
        }

        assistant::message::ImageData imageData{};
        imageData.success = result.success;
        imageData.modelName = result.modelName;
        imageData.errorCode = result.errorCode;
        imageData.errorMessage = result.errorMessage;
        if (result.type == DialogueCallback::ImageGenerated) {
            imageData.imageFormat = result.imageFormat;
            imageData.imageBase64Data = result.imageBase64Data;
        }
        callOutputCallback(result.type, assistant::message::createImageDataMessage(
                                            imageData, result.type == DialogueCallback::ImageGenerating));
    });

    imageHandlerInterface_->setImageBinarizationCallback(
        [this](const DialogueCallback::ImageBinarizationResult &result) {
            if (result.type == DialogueCallback::ImageErrorOccurred) {
                callOutputCallback(DialogueCallback::ImageErrorOccurred,
                                   assistant::message::createImageDataMessageWithErrorCode(
                                       result.modelName, "tool_call", result.errorCode, result.errorMessage));
                return;
            }

            assistant::message::ImageData imageData{};
            imageData.success = result.success;
            imageData.modelName = result.modelName;
            imageData.errorCode = result.errorCode;
            imageData.errorMessage = result.errorMessage;
            if (result.type == DialogueCallback::ImageGenerated) {
                imageData.imageFormat = result.imageFormat;
                imageData.imageBase64Data = result.imageBase64Data;
            }
            callOutputCallback(result.type, assistant::message::createImageDataMessage(
                                                imageData, result.type == DialogueCallback::ImageGenerating));
        });
}

void IntentionRecognition::setOutputCallback(const DialogueCallback::OutputCallback &callback) {
    outputCallback_ = callback;
}

void IntentionRecognition::loadJsonContent() {
    functionInterfaceName_ = loadJsonFile(DialogueConfig::interfaceNameFileStr);
}

Json::Value IntentionRecognition::getfunctionInterfaceName() { return functionInterfaceName_; }

void IntentionRecognition::stopDialogue() {
    dialogueStop_ = true;
    if (imageHandlerInterface_) {
        imageHandlerInterface_->stopImageBinarization();
        imageHandlerInterface_->stopImageMatting();
    }
}

DialogueData::CallSuccessIdentifier IntentionRecognition::callSystemOperationInterface(
    const DialogueData::Intent &scenarioIntent) const {
    if (systemOperationInterface_ != nullptr) {
        return systemOperationInterface_->callSystemControlInterface(scenarioIntent);
    }
    return {};
}

// 新建日程
DialogueData::CallSuccessIdentifier IntentionRecognition::callSystemScheduleManagement(
    const std::string &scheduleText) const {
    if (systemOperationInterface_ != nullptr) {
        return systemOperationInterface_->callSystemScheduleManagementInterface(scheduleText);
    }
    return {};
}

// 生成待办
DialogueData::CallSuccessIdentifier IntentionRecognition::callSystemToDoList(const std::string &todoListText) const {
    if (systemOperationInterface_ != nullptr) {
        return systemOperationInterface_->callSystemToDoListInterface(todoListText);
    }
    return {};
}

bool IntentionRecognition::callSourceUrlTool(int toolId) {
    if (systemOperationInterface_ == nullptr) {
        return false;
    }
    return systemOperationInterface_->callSourceUrlTool(toolId);
}

std::future<DialogueData::FileSearchIdentifier> IntentionRecognition::callFileInferenceInterfaceSearchText(
    const std::string &knowledgeBaseName, const std::string &text, bool useDefault) const {
    if (fileInferenceInterface_ != nullptr) {
        return fileInferenceInterface_->ragSearchText(knowledgeBaseName, text, useDefault);
    }
    return {};
}

DialogueData::CallSuccessIdentifier IntentionRecognition::callFileInferenceInterfaceFileContent(
    const std::string &knowledgeBaseName, const std::string &input) const {
    if (fileInferenceInterface_ != nullptr) {
        return fileInferenceInterface_->fileContent(knowledgeBaseName, input);
    }
    return {};
}

std::future<bool> IntentionRecognition::callChatModelInferenceInterface(const std::string &modelInput) const {
    if (modelInferenceInterface_ != nullptr) {
        return modelInferenceInterface_->callChatModelInference(modelInput);
    }
    return {};
}

DialogueCallback::TextEmbeddingResult IntentionRecognition::callTextEmbeddingInterface(
    const std::string &queryInput) const {
    if (stringEmbeddingInterface_ != nullptr) {
        return stringEmbeddingInterface_->textVectorization(queryInput);
    }
    return {};
}

DialogueData::CallSuccessIdentifier IntentionRecognition::callIntentModelInferenceInterface(
    const std::string &modelInput) const {
    if (modelInferenceInterface_ != nullptr) {
        return modelInferenceInterface_->callIntentionModelInference(modelInput);
    }
    return {};
}

std::string IntentionRecognition::intentModelName() const {
    if (modelInferenceInterface_ == nullptr) {
        return "";
    }
    return modelInferenceInterface_->intentModelName();
}

std::future<std::string> IntentionRecognition::createFileKnowledgeBase() {
    // 创建向量数据库
    if (fileInferenceInterface_ != nullptr) {
        return fileInferenceInterface_->createFileKnowledgeBase();
    }
    return {};
}

DialogueData::FileSearchIdentifier IntentionRecognition::updateFileKnowledgeBase(const std::string &knowledgeBaseName,
                                                                                 const std::string &docFilePath) {
    // 更新已有向量数据库
    if (fileInferenceInterface_ == nullptr) {
        LOGE("文档处理类接口为空！");
        return {-1, "文档处理类接口为空！"};
    }
    return fileInferenceInterface_->updateFileKnowledgeBase(knowledgeBaseName, docFilePath);
}

DialogueData::CallSuccessIdentifier IntentionRecognition::callImageHandlerInterface(const std::string &prompt) const {
    if (imageHandlerInterface_ != nullptr) {
        return imageHandlerInterface_->textToImage(prompt);
    }
    return {};
}

DialogueData::CallSuccessIdentifier IntentionRecognition::callImageMattingInterface(
    const std::string &imagePath) const {
    if (imageHandlerInterface_ != nullptr) {
        return imageHandlerInterface_->imageMatting(imagePath);
    }
    return {};
}

DialogueData::CallSuccessIdentifier IntentionRecognition::callImageBinarizationInterface(
    const std::string &imagePath) const {
    if (imageHandlerInterface_ != nullptr) {
        return imageHandlerInterface_->imageBinarization(imagePath);
    }
    return {};
}

void IntentionRecognition::callOutputCallback(DialogueCallback::Type type, const std::string &message) const {
    if (outputCallback_ != nullptr) {
        outputCallback_(type, message);
    }
}

void IntentionRecognition::loadIntentAndSlots() {
    intentSlotJsonData = loadJsonFile(DialogueConfig::intentSlotFile);

    for (const auto &intentLabel : intentSlotJsonData.getMemberNames()) {
        if (intentSlotJsonData.isObject() && intentSlotJsonData[intentLabel].isObject()) {
            for (const auto &key : intentSlotJsonData[intentLabel].getMemberNames()) {
                systemScenarioIntentionsTotal.push_back(key);
            }

            for (const auto &key : intentSlotJsonData[intentLabel].getMemberNames()) {
                if (!replaceTextForIntentPrompt_.empty()) {
                    replaceTextForIntentPrompt_ += ";";
                }
                replaceTextForIntentPrompt_ += key;
            }
        }
    }

    LOGI("全场景槽位标签数量 = {}", systemScenarioIntentionsTotal.size());
}

void IntentionRecognition::loadPromptText() {
    LOGI("导入意图识别system prompt. --> {}", DialogueConfig::intentPromptFileStr);
    getpromptReplaceText(replaceTextForIntentPrompt_);

    if (is_end_model_for_intention) {
        loadPrompt(DialogueConfig::intentPromptFileStr);
    } else {
        loadPrompt(DialogueConfig::intentPromptFileCloudStr);
    }

    intentPrompt_ = getIntentPrompt();
}

void IntentionRecognition::loadIntentRecognitionModel() {
    LOGI("加载意图识别模型成功. --> {}", DialogueConfig::intentModelPathStr);
}

void IntentionRecognition::setScenarioInstructionPrompt(int promptId, const std::string &promptText,
                                                        const std::string &instructionName) {
    instructionPrompt_ = promptText;
    promptId_ = promptId;
    instructionName_ = instructionName;
    promptPassed = true;  // 传入成功标识
    LOGD("prompt text = {}", promptText);
    LOGD("promptId_ = {}, instructionName_ = {}", promptId_, instructionName_);

    if (promptId == 0) {
        defaultPromptContent = promptText;
    }
}

void IntentionRecognition::setRagFilePath(const std::string &userFilePath) {
    documentFilePath_ = userFilePath;
    filePassed = true;  // 传入成功标识
}

void IntentionRecognition::clearDialogueData() {
    if (!currentDialogue.dialogue.empty()) {
        currentDialogue = DialogueData::Dialogue();
    }
    if (!documentFilePath_.empty()) {
        documentFilePath_.clear();
    }
    if (!imageFilePath_.empty()) {
        imageFilePath_.clear();
    }
    if (resetForPromptId != 0) {
        resetForPromptId = 0;
    }
}

void IntentionRecognition::clearHistoryDialogue() {
    if (pCurrentDialogue_ != nullptr) {
        pCurrentDialogue_->dialogue.clear();
    }

    clearDialogueData();

    // 删除指定的向量数据库
    deleteCustomizeKnowledgeBase(customizeKBName);

    // 清除数据库名称
    customizeKBName.clear();
}

void IntentionRecognition::printDialogueContent(const DialogueData::Dialogue &currentDialogue) {
    std::string log;
    log += fmt::format("~~~~~~~~~~~~~~~~~~~~~打印currentDialogue的内容~~~~~~~~~~~~~~~");
    for (size_t i = 0; i < currentDialogue.dialogue.size(); ++i) {
        log += fmt::format("dialogue_id = {}\n", currentDialogue.dialogue_id);
        log += fmt::format("turn_num = {}\n", currentDialogue.dialogue[i].turn_num);
        log += fmt::format("first_turn_flag = {}\n", currentDialogue.dialogue[i].first_turn_flag);
        log += fmt::format("role = {}\n", currentDialogue.dialogue[i].role);
        log += fmt::format("user_input = {}\n", currentDialogue.dialogue[i].user_input);
        log += fmt::format("query_token_number = {}\n", currentDialogue.dialogue[i].query_token_number);
        log += fmt::format("intent_category = {}\n", currentDialogue.dialogue[i].intent_category);
        log += fmt::format("system_response = {}\n", currentDialogue.dialogue[i].system_response);
        log += fmt::format("response_token_number = {}\n", currentDialogue.dialogue[i].response_token_number);

        log += fmt::format("turn_keywords = {}\n", fmt::join(currentDialogue.dialogue[i].turn_keywords, "\t"));

        log += fmt::format("dialogue_state.slots = \n");
        for (const auto &element : currentDialogue.dialogue[i].dialogue_state.slots) {
            log += fmt::format("\tname = {}\n", element.name);
            log += fmt::format("\tisRequired = {}\n", element.isRequired);
            log += fmt::format("\tvalue = {}\n", fmt::join(element.values, " "));
        }

        log += fmt::format("dialogue_state.system_acts = {}\n",
                           fmt::join(currentDialogue.dialogue[i].dialogue_state.system_acts, "\t"));
        log += fmt::format("dialogue_state.user_acts = {}\n",
                           fmt::join(currentDialogue.dialogue[i].dialogue_state.user_acts, "\t"));
    }

    log += fmt::format("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    LOGI("{}", log);
}

Json::Value IntentionRecognition::roundOneMultiModalInputRecognition(const std::string &userInput,
                                                                     std::vector<DialogueData::Slot> &currentSlot,
                                                                     DialogueData::DialogueState &currentState) {
    // TODO 长度检查
    std::string fileTypeValue = userInput.substr(userInput.length() - 6, -1);
    LOGI("fileTypeValue = {}", fileTypeValue);

    DialogueData::CallSuccessIdentifier tempRes;

    // 填充槽位
    // TODO 建议按照以下方式优化
    //   currentSlot.push_back(DialogueData::Slot{"附件类型", true,
    //   {fileTypeValue}});
    //   currentSlot.push_back(DialogueData::Slot{"API接口名称", false, {}});
    currentSlot.emplace_back();
    currentSlot.back().name = "附件类型";
    currentSlot.back().isRequired = true;
    currentSlot.back().values = {fileTypeValue};

    currentSlot.emplace_back();
    currentSlot.back().name = "API接口名称";
    currentSlot.back().isRequired = false;
    currentSlot.back().values = {};

    // 添加其他字段的值
    currentTurn.intent_category = "多模态输入识别";
    currentTurn.turn_keywords = {fileTypeValue};

    currentState.slots = {currentSlot};
    currentState.system_acts = {"指令场景回复"};  // 引导
    currentState.user_acts = {"请求"};

    // 输出相关API接口及引导话术
    std::string resScript = "收到您的图片啦~需要我对这张图片做什么呢？";

    LOGI("提供给用户【{}】类API接口及引导话术", fileTypeValue);
    std::list<std::string> listResult = TypeTrans::jsonArrayToStdList(functionInterfaceName_[fileTypeValue]);

    LOGI("{}", fmt::join(listResult, "\t"));

    std::string concatValue = "\n";
    for (const auto &element : listResult) {
        concatValue += element;
        concatValue += "\t\n";
    }

    currentTurn.system_response = resScript + concatValue;

    tempRes.successRes = true;
    tempRes.responseContent = resScript;
    tempRes.additionalContent = functionInterfaceName_[fileTypeValue];

    currentTurn.dialogue_state = currentState;
    currentDialogue.dialogue.push_back(currentTurn);

    // 引导类callback
    DialogueCallback::Type dataType = DialogueCallback::ChatGuidedNormal;
    guideCallback(fileTypeValue, resScript, listResult, "", dataType);

    return TypeTrans::callSuccessIdentifierToJson(tempRes);
}

Json::Value IntentionRecognition::roundTwoMultiModalInputRecognition(std::string &userInput,
                                                                     std::string &beforeTurnImage,
                                                                     DialogueData::APINameSearchResult &searchResult,
                                                                     std::vector<DialogueData::Slot> &currentSlot,
                                                                     DialogueData::DialogueState &currentState) {
    LOGI("用户输入的是功能指令名称: {}", userInput);

    std::string fileType = searchResult.category.c_str();
    // 填充槽位
    currentSlot.emplace_back();
    currentSlot.back().name = "附件类型";
    currentSlot.back().isRequired = true;
    currentSlot.back().values = {fileType};

    currentSlot.emplace_back();
    currentSlot.back().name = "API接口名称";
    currentSlot.back().isRequired = false;
    currentSlot.back().values = {userInput};

    // 添加其他字段的值
    currentTurn.intent_category = "多模态输入识别";
    currentTurn.turn_keywords = {userInput};

    currentState.slots = {currentSlot};
    currentState.system_acts = {"对话生成"};
    currentState.user_acts = {"请求"};

    // // 调用对应的功能接口，进行处理
    DialogueData::CallSuccessIdentifier apiResult;

    if (fileType == "文档") {
        LOGI("调用指令类接口：allChatModelInference()");

        // 读取指令场景的prompt
        bool success = callChatModelInferenceInterface(instructModelInput).get();

        // 追加callback中生成的对话内容
        if (success) {
            apiResult.responseContent = chatGenCompleteText_;
        }

    } else {
        // 图像接口
        // 调用图像处理的接口, 目前仅支持智能抠图、黑白处理（二值化）
        // userInput为指令名称,beforeTurnImage为图片路径，此处需要替换为图像处理
        // 的接口，该接口根据用户输入的指令，选择智能抠图，或者其他图像类指令接口

        if (userInput == "智能抠图") {
            apiResult = callImageMattingInterface(beforeTurnImage);

        } else if (userInput == "扫描黑白件") {
            apiResult = callImageBinarizationInterface(beforeTurnImage);
        }

        // 追加callback中生成的图片内容
        apiResult.responseContent = "恭喜您！图片处理已完毕，请记得查看哟~";
        apiResult.additionalContent = imageData_;
        apiResult.successRes = true;
    }

    currentTurn.system_response = apiResult.responseContent;

    currentTurn.dialogue_state = currentState;
    currentDialogue.dialogue.push_back(currentTurn);

    // 异常码判断
    if (apiResult.errorCode != 0 && apiResult.errorCode != -1) {
        // 接口出错
        apiResult.responseContent = "哎呀，不好意思，多模态输入识别场景图片处理时出现了一些小状况。";

        bool isError = true;
        std::string errorMessage;
        if (fileType == "文档") {
            errorMessage = "ChatModel interface call failed.";

        } else {
            errorMessage = "ImageHandler interface call failed.";
        }

        DialogueCallback::Type dataType = DialogueCallback::SystemOperationErrorOccurred;
    } else {
        // 调用图像类callback
        std::string modelName = "多模态输入识别";
        bool isError = false;
        std::string errorMessage;
        enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
    }

    return TypeTrans::callSuccessIdentifierToJson(apiResult);  // string
}

Json::Value IntentionRecognition::ragRetrieveGeneration(const std::string &responseContent,
                                                        const std::string &userInput, bool isFirstRound) {
    Json::Value dialogueStatus;
    DialogueData::CallSuccessIdentifier callResult;
    DialogueData::DialogueState currentState;
    std::vector<DialogueData::Slot> currentSlot;

    // 生成模型输入
    bool isRagInstruct = true;
    std::string instructName;
    if (isUseCustomizeKB) {
        instructName = "文档问答";
    } else {
        instructName = "系统问答";
        // "rag检索生成";
    }

    std::string ragUserInput = responseContent + "\nquestion: " + userInput + "\n";

    ragPromptCopy_ = ragPrompt_;

    getInstructionCompleteSession(isFirstRound, ragUserInput, instructName, isRagInstruct, true);

    LOGI("RAG's modelInput = {}", instructModelInput);

    bool success = callChatModelInferenceInterface(instructModelInput).get();

    // 追加callback中生成的对话内容
    if (success) {
        callResult.responseContent = chatGenCompleteText_;
    }

    if (!callResult.responseContent.empty() && success) {
        // rag检索生成成功
        LOGI("~~rag检索成功~~ {}", callResult.responseContent);

        // // 更新会话状态
        // 填充槽位
        currentSlot.emplace_back();
        currentSlot.back().name = "附件类型";
        currentSlot.back().isRequired = true;
        currentSlot.back().values = {"文档"};

        currentSlot.emplace_back();
        currentSlot.back().name = "API接口名称";
        currentSlot.back().isRequired = false;
        currentSlot.back().values = {"RAG文档问答"};

        // 添加其他字段的值
        currentTurn.intent_category = instructName;  // 多模态输入识别
        currentTurn.turn_keywords = {instructName};

        currentState.slots = {currentSlot};
        currentState.system_acts = {"rag检索生成"};
        currentState.user_acts = {"请求"};

        currentTurn.system_response = callResult.responseContent;
        DialogueData::TokenCount responseTokenNumber = calculateTokenNumber(callResult.responseContent);
        currentTurn.response_token_number = responseTokenNumber.totalTokens;
        currentTurn.dialogue_state = currentState;
        currentDialogue.dialogue.push_back(currentTurn);

        dialogueStatus = TypeTrans::callSuccessIdentifierToJson(callResult);

    } else {
        // chatModel接口调用出错
        std::string responseStr = "哎呀，系统【RAG检索生成】接口好像出了点小状况哦~ ";
        callResult.responseContent = responseStr;
        dialogueStatus = TypeTrans::callSuccessIdentifierToJson(callResult);

        // 纯文本类callback
        bool isError = true;
        std::string errorMessage = "RAG retrieval generation interface call failed.";
    }
    return dialogueStatus;
}

Json::Value IntentionRecognition::ragProcessingScenario(const std::string &userInput, const std::string &inputFilePath,
                                                        bool isFirstRound, bool updateSuccessTag) {
    Json::Value dialogueStatus;

    LOGI("customizeKBName = {}", customizeKBName);
    if (customizeKBName.empty() && documentFilePath_.empty()) {
        currentTurn.knowledge_base_name = defaultKBName;
    }

    // 系统助手
    DialogueData::FileSearchIdentifier ragSearchRes;
    std::future<DialogueData::FileSearchIdentifier> result;

    if ((updateSuccessTag || !customizeKBName.empty()) && isUseCustomizeKB) {
        // 调用RAG-检索接口 -- 传入文档
        result = callFileInferenceInterfaceSearchText(customizeKBName, userInput, false);
    } else {
        // 调用RAG-检索接口
        result = callFileInferenceInterfaceSearchText(defaultKBName, userInput, true);
    }

    if (result.wait_for(std::chrono::seconds(300)) == std::future_status::ready) {
        try {
            ragSearchRes = result.get();
            ragFileSources = ragSearchRes.sources;
        } catch (const std::exception &e) {
            LOGE("Vectorization retrieval failed: {}", e.what());
        }
    } else {
        // 超时处理
        LOGI("Vectorization retrieval timeout, unable to obtain results.");
    }

    if (ragSearchRes.errorCode == 0 && !ragSearchRes.responseContent.empty()) {
        LOGI("~~~~~~~~~~~~~~~~~~~~进入rag检索生成场景~~~~~~~~~~~~~~~~~~~~");
        dialogueStatus = ragRetrieveGeneration(ragSearchRes.responseContent, userInput, isFirstRound);
        return dialogueStatus;
    } else {
        // 检索结果为空，回复话术
        // 纯文本类callback
        std::string responseStr =
            "当前未检索到相关参考资料。建议您补充更多细节信息，或上传相关文档后"
            "重试。";
        bool isError = false;
        std::string errorMessage;
        enum DialogueCallback::Type dataType = DialogueCallback::IntentNormal;
        textCallback(responseStr, "", isError, 0, errorMessage, dataType, "");
        // 重置feedback
        feedbackUrlForCloudModel = "";
        return dialogueStatus;
    }
}

void IntentionRecognition::getCompleteSessionFromMemory(bool isFirstRound, const std::string &userInput,
                                                        bool isEndModel) {
    // 从currentDialogue中获取

    Json::Value historyToUse;
    // 创建"messages"数组
    Json::Value messagesArray(Json::arrayValue);

    // 添加提示词
    Json::Value systemMessage;
    systemMessage["content"] = intentPrompt_;
    LOGD("{}", intentPrompt_);
    systemMessage["role"] = "system";
    messagesArray.append(systemMessage);

    // // 直接截取最近N轮的对话内容，并只针对有槽位类的意图，进行历史会话追加
    if (!isFirstRound) {
        int startIndex =
            std::max(0, static_cast<int>(currentDialogue.dialogue.size()) - DialogueConfig::maxFilteredRounds);

        LOGI("截取第【 \" {} \" 】轮以后的历史对话", startIndex);

        for (size_t i = startIndex; i < currentDialogue.dialogue.size(); ++i) {
            std::list<std::string> tempList = currentDialogue.dialogue[i].dialogue_state.system_acts;
            std::string intentName_ = currentDialogue.dialogue[i].intent_category;

            std::string systemResponseStr = currentDialogue.dialogue[i].system_response;
            std::string intentResultStr = currentDialogue.dialogue[i].intent_model_result;

            auto it = std::find(tempList.begin(), tempList.end(), "进一步询问");
            auto it_ = std::find(tempList.begin(), tempList.end(), "对话生成");
            auto ragFind_ = std::find(tempList.begin(), tempList.end(), "rag检索生成");
            auto instructFind_ = std::find(tempList.begin(), tempList.end(), "指令场景回复");
            auto onlyUseIntentFind_ = std::find(DialogueConfig::onlyUseIntendFieldsScene.begin(),
                                                DialogueConfig::onlyUseIntendFieldsScene.end(), intentName_);

            if (it == tempList.end() && it_ == tempList.end() && ragFind_ == tempList.end() &&
                instructFind_ == tempList.end() &&
                onlyUseIntentFind_ == DialogueConfig::onlyUseIntendFieldsScene.end()) {
                // 不属于无槽位、仅打开页面、意图不明确、对话生成、指令场景、rag检索生成等情况
                if (!isEndModel && !systemResponseStr.empty()) {
                    Json::Value userQuery;
                    userQuery["content"] = currentDialogue.dialogue[i].user_input;
                    userQuery["role"] = "user";
                    messagesArray.append(userQuery);

                    Json::Value sysResponse;
                    sysResponse["content"] = systemResponseStr;
                    sysResponse["role"] = "assistant";

                    messagesArray.append(sysResponse);
                } else if (isEndModel && !intentResultStr.empty()) {
                    Json::Value userQuery;
                    userQuery["content"] = currentDialogue.dialogue[i].user_input;
                    userQuery["role"] = "user";
                    messagesArray.append(userQuery);

                    Json::Value sysResponse;
                    sysResponse["content"] = intentResultStr;
                    sysResponse["role"] = "assistant";
                    messagesArray.append(sysResponse);
                }
            }
        }
    }

    // 当前轮次的用户输入
    Json::Value userMessage;
    userMessage["content"] = userInput;
    userMessage["role"] = "user";

    messagesArray.append(userMessage);

    // 将"messages"数组添加到根对象
    historyToUse["messages"] = messagesArray;

    // 将JSON对象格式化为字符串并输出
    Json::StreamWriterBuilder jswBuilder;
    jswBuilder["emitUTF8"] = true;
    std::unique_ptr<Json::StreamWriter> jsWriter(jswBuilder.newStreamWriter());

    std::ostringstream os;
    jsWriter->write(historyToUse, &os);
    modelInput = os.str();

    LOGI("Intention Model's modelInput = {}", modelInput);
}

void IntentionRecognition::getInstructionCompleteSession(bool isFirstRound, const std::string &userInput,
                                                         std::string &instructName, bool isRagInstruct,
                                                         bool hasCustomizeKBName) {
    // 获取指令类的上下文
    // 从currentDialogue中获取

    Json::Value historyToUse;
    // 创建"messages"数组
    Json::Value messagesArray(Json::arrayValue);
    Json::Value systemMessage;

    // 添加提示词
    if (isRagInstruct) {  // 使用rag prompt
        LOGI("ragPromptCopy_ = {}", ragPromptCopy_);
        systemMessage["content"] = ragPromptCopy_;
    } else {
        systemMessage["content"] = instructionPrompt_;
    }

    systemMessage["role"] = "system";
    messagesArray.append(systemMessage);

    LOGD(" instructionPrompt_ {}", instructionPrompt_);

    // 获取当前模型最大token数
    if (modelInferenceInterface_ != nullptr) {
        int tempMaxTokens = modelInferenceInterface_->getCurrentModelMaxTokens();
        if (tempMaxTokens > 0) {
            maxContextTokens = tempMaxTokens;
        }
    }

    LOGI("当前RAG场景模型最大Token数 = {}", maxContextTokens);

    if (!isFirstRound) {
        int startIndex = 0;
        bool isMultiRound{false};
        // 计算userInput的总token数
        DialogueData::TokenCount responseTokenNumber = calculateTokenNumber(userInput);
        int tempMaxContextTokens = maxContextTokens - responseTokenNumber.totalTokens;
        LOGI("userInput.totalTokens = {}, tempMaxContextTokens = {}", responseTokenNumber.totalTokens,
             tempMaxContextTokens);

        auto it =
            std::find(DialogueConfig::nameForNeedContent.begin(), DialogueConfig::nameForNeedContent.end(), promptId_);
        int ragTokenTotalNum = 0;
        for (int i = currentDialogue.dialogue.size() - 1; i >= 0; --i) {
            LOGI("当前轮 I = {}", i);
            DialogueData::DialogueTurn perTurnDialogue = currentDialogue.dialogue[i];
            if (((!isRagInstruct && it != DialogueConfig::nameForNeedContent.end()) ||
                 (isRagInstruct && hasCustomizeKBName)) &&
                perTurnDialogue.intent_category == instructName && startIndex < DialogueConfig::maxFilteredRounds &&
                ragTokenTotalNum < tempMaxContextTokens && !perTurnDialogue.system_response.empty()) {
                // 指令场景仅扩写、润色、翻译三个场景需要上下文
                // 闲聊也需要上下文
                // rag场景需要上下文,不包含检索失败的上下文
                ragTokenTotalNum += perTurnDialogue.response_token_number;
                LOGI("ragTokenTotalNum = {}", ragTokenTotalNum);
                ++startIndex;
            }

            if (startIndex == DialogueConfig::maxFilteredRounds && ragTokenTotalNum < tempMaxContextTokens) {
                startIndex = i;
                isMultiRound = true;
                break;
            } else if (ragTokenTotalNum >= tempMaxContextTokens) {
                LOGI("超出最大上下文限制, ragTokenTotalNum = {}", ragTokenTotalNum);
                startIndex = i + 1;
                isMultiRound = true;
                break;
            }
        }

        if (!isMultiRound) {
            // 小于等于1轮
            startIndex = 0;
        }

        LOGI("截取第【 \" {} \" 】轮以后的历史对话", startIndex);

        for (size_t i = startIndex; i < currentDialogue.dialogue.size(); ++i) {
            DialogueData::DialogueTurn perTurnDialogue = currentDialogue.dialogue[i];
            if (((isRagInstruct && hasCustomizeKBName) ||
                 (!isRagInstruct && it != DialogueConfig::nameForNeedContent.end())) &&
                perTurnDialogue.intent_category == instructName && !perTurnDialogue.system_response.empty()) {
                Json::Value userQuery;
                userQuery["content"] = perTurnDialogue.user_input;
                userQuery["role"] = "user";
                messagesArray.append(userQuery);

                Json::Value sysResponse;
                sysResponse["content"] = perTurnDialogue.system_response;
                sysResponse["role"] = "assistant";

                messagesArray.append(sysResponse);
            }
        }
    }

    // 当前轮次的用户输入
    Json::Value userMessage;
    userMessage["content"] = userInput;
    userMessage["role"] = "user";

    messagesArray.append(userMessage);

    // 将"messages"数组添加到根对象
    historyToUse["messages"] = messagesArray;

    // 将JSON对象格式化为字符串并输出
    Json::StreamWriterBuilder jswBuilder;
    jswBuilder["emitUTF8"] = true;
    std::unique_ptr<Json::StreamWriter> jsWriter(jswBuilder.newStreamWriter());

    std::ostringstream os;
    jsWriter->write(historyToUse, &os);
    instructModelInput = os.str();

    LOGI("Instruction Model's modelInput = {}", instructModelInput);
}

Json::Value IntentionRecognition::unclearIntentionsProcessing(std::string &dialogueId,
                                                              DialogueData::DialogueState &currentState,
                                                              const bool &parseSuccessFlag, std::string &curModelIntent,
                                                              const bool &includeIntentionKeyWords,
                                                              const std::string &modelName) {
    std::string responseStr;

    if (!parseSuccessFlag && !includeIntentionKeyWords && !curModelIntent.empty()) {
        responseStr = curModelIntent;
    } else if (!parseSuccessFlag && includeIntentionKeyWords) {
        // 意图识别结果格式有问题
        responseStr =
            "非常抱歉，当前意图识别结果的格式出现了问题，导致无法顺利解析。还请"
            "您再尝试一次，给您造成困扰，深表歉意";
    } else {
        responseStr = "很抱歉，我不太理解您的意思呢。您可以详细描述一下哦";
    }

    LOGI("response = {}", responseStr);
    DialogueData::CallSuccessIdentifier tempResult;

    // // 更新对话状态
    if (!parseSuccessFlag) {
        currentState.system_acts = {"对话生成"};  // 系统动作
    } else {
        currentState.system_acts = {"进一步询问"};  // 系统动作
    }
    currentState.user_acts = {"请求"};  // 用户动作

    currentTurn.system_response = responseStr;  // 系统回复

    currentTurn.dialogue_state = currentState;
    currentDialogue.dialogue.push_back(currentTurn);

    // 输出
    tempResult.successRes = true;
    tempResult.responseContent = responseStr;

    // 纯文本类callback
    bool isError = false;
    std::string errorMessage;
    enum DialogueCallback::Type dataType = DialogueCallback::IntentNormal;
    textCallback(responseStr, modelName, isError, 0, errorMessage, dataType, "");
    // 重置feedback
    feedbackUrlForCloudModel = "";
    return TypeTrans::callSuccessIdentifierToJson(tempResult);
}

void IntentionRecognition::guideCallback(std::string &fileTypeValue, std::string &resScript,
                                         std::list<std::string> &listResult, const std::string &modelName,
                                         enum DialogueCallback::Type &dataType) {  // 引导类callback格式

    // 结果转换成助手的输出格式
    Json::Value assistantText;

    assistantText["message_type"] = "tool_call";

    if (fileTypeValue == "文档") {
        assistantText["file_type"] = "document";
    } else {
        assistantText["file_type"] = "image";
    }

    // 创建 "content" 数组节点，添加元素
    Json::Value contentArray;

    Json::Value contentElement;
    contentElement["type"] = "text";
    contentElement["text"] = resScript;
    contentArray.append(contentElement);

    for (uint32_t i = 0; i < listResult.size(); ++i) {
        auto it = std::next(listResult.begin(), i);

        Json::Value contentElement_;
        contentElement_["tool_id"] = uint32_t(i + 1);  // guide_func_id
        contentElement_["tool_name"] = *it;
        if (*it == "扫描黑白件") {
            contentElement_["tool_description"] = "进行扫描黑白处理";
        } else if (*it == "智能抠图") {
            contentElement_["tool_description"] = "实现智能识别，一键抠图";
        }
        contentArray.append(contentElement_);
    }

    assistantText["content"] = contentArray;

    assistantText["error_code"] = 0;
    assistantText["error_message"] = "";
    assistantText["model"] = modelName;

    std::string assistantTextStr = util::jsonToString(assistantText);

    // 调用助手的回调函数
    DialogueCallback::ChatData tempDialogueData;
    tempDialogueData.type_ = DialogueCallback::ChatGuidedNormal;
    tempDialogueData.text_ = assistantTextStr;
    callOutputCallback(tempDialogueData.type_, tempDialogueData.text_);
}

void IntentionRecognition::textCallback(std::string &responseStr, const std::string &modelName, const bool &isError,
                                        int errorCode, std::string &errorMessage, enum DialogueCallback::Type &dataType,
                                        const std::string &feedbackUrl) {  // 纯文本类的callback格式

    // 结果转换成助手的输出格式
    Json::Value assistantText;

    assistantText["message_type"] = "chat";

    // 创建 "content" 数组节点，添加元素
    Json::Value contentArray;

    Json::Value contentElement;
    contentElement["type"] = "text";
    contentElement["text"]["sentence_id"] = 1;
    contentElement["text"]["is_end"] = true;
    contentElement["text"]["result"] = responseStr;
    contentArray.append(contentElement);

    assistantText["content"] = contentArray;

    if (!isError) {
        assistantText["error_code"] = 0;
        assistantText["error_message"] = "";

        if (modelInferenceInterface_ != nullptr) {
            if (modelInferenceInterface_->isCloudModel() && !feedbackUrl.empty()) {
                assistantText["feedback_url"] = feedbackUrl;
            }
        }
    } else {
        // TODO
        assistantText["error_code"] = 1;
        assistantText["error_message"] = errorMessage;
    }

    assistantText["model"] = modelName;

    Json::StreamWriterBuilder jswBuilder;
    jswBuilder["emitUTF8"] = true;
    std::unique_ptr<Json::StreamWriter> jsWriter(jswBuilder.newStreamWriter());

    std::ostringstream os;
    jsWriter->write(assistantText, &os);
    std::string assistantTextStr = os.str();
    LOGI("assistantText = {}", assistantTextStr);

    // 调用助手的回调函数
    DialogueCallback::ChatData tempDialogueData;
    tempDialogueData.text_ = assistantTextStr;

    if (!isError) {
        tempDialogueData.type_ = dataType;
        callOutputCallback(tempDialogueData.type_, tempDialogueData.text_);

    } else {
        tempDialogueData.type_ = dataType;
        tempDialogueData.errorCode_ = errorCode;
        tempDialogueData.errorMessage_ = errorMessage;

        callOutputCallback(tempDialogueData.type_, assistant::message::createErrorMessageResult(
                                                       tempDialogueData.errorCode_, tempDialogueData.errorMessage_));
    }
}

Json::Value IntentionRecognition::intentRecognitionModelProcessingScenarios(const std::string &userInput,
                                                                            bool isFirstRound) {
    LOGI("~~~~~~~~~~~~~~~~~~~~进入意图识别场景~~~~~~~~~~~~~~~~~~");
    // // 使用意图识别模型推理的场景
    DialogueData::Intent curIntent;  // 意图
    Json::Value dialogueStatus;
    DialogueData::CallSuccessIdentifier processResult;
    DialogueData::DialogueState currentState;
    std::vector<DialogueData::Slot> currentSlot;

    // // 获取当前轮次完整会话
    getCompleteSessionFromMemory(isFirstRound, userInput,
                                 is_end_model_for_intention);  // 从currentDialogue中

    // // 意图识别模型进行推理
    processResult = callIntentModelInferenceInterface(modelInput);

    // 异常码判断
    LOGI("processResult.errorcode = {}", processResult.errorCode);

    if (!processResult.successRes) {
        callOutputCallback(
            DialogueCallback::IntentErrorOccurred,
            assistant::message::createErrorMessageResult(processResult.errorCode, processResult.errorMessage));
        // 重置feedback
        feedbackUrlForCloudModel = "";
        return {};
    }

    if (dialogueStop_) {
        dialogueStop_ = false;
        // 传回助手一个结束标识符
        std::string responseStr;
        std::string errorMessage;
        std::string modelName = intentModelName();
        enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
        textCallback(responseStr, modelName, false, 0, errorMessage, dataType, "");
        return {};
    }

    // // 解析意图结果
    std::string curModelIntent = processResult.responseContent;
    LOGD("curModelIntent = {}", curModelIntent);

    Json::Value jsonData;
    Json::Reader reader;

    bool parseSuccessFlag = reader.parse(curModelIntent, jsonData);
    LOGI("parseSuccessFlag = {}", parseSuccessFlag);
    bool includeIntentKeyWords = false;
    if (!parseSuccessFlag) {
        includeIntentKeyWords = TypeTrans::containsElement(curModelIntent, intentKeyWords);
        if (includeIntentKeyWords) {
            curModelIntent = std::move(TypeTrans::replaceChinesePunctuation(curModelIntent));
            LOGI(
                "String contains Chinese punctuation. The replaced string "
                "is: "
                "{}",
                curModelIntent);

            parseSuccessFlag = reader.parse(curModelIntent, jsonData);
            if (parseSuccessFlag) {
                LOGI("curModelIntent 解析正确");
            } else {
                LOGI("curModelIntent 解析错误");
            }
        }
    }

    if (parseSuccessFlag) {
        LOGI("解析意图识别结果: {}", curModelIntent);

        Json::StreamWriterBuilder jswBuilder;
        jswBuilder["emitUTF8"] = true;
        std::unique_ptr<Json::StreamWriter> jsWriter(jswBuilder.newStreamWriter());

        std::ostringstream os;
        jsWriter->write(jsonData, &os);
        std::string strJson = os.str();

        LOGI("意图识别输出的jsonData.size() = {}", jsonData.size());

        if (jsonData.size() == 1 && jsonData.isArray()) {
            // 单意图

            // 获取数组的第一个元素
            const Json::Value &firstElement = jsonData[0];

            // 将意图识别结果合并到curIntent
            if (!firstElement["intent_category"].empty()) {
                curIntent.name = firstElement["intent_category"].asString();
                LOGI("Intent Name: {}", curIntent.name);
            }

            if (!firstElement["inquiry"].empty()) {
                curIntent.inquiry = firstElement["inquiry"].asString();
                LOGI("inquiry: {}", curIntent.inquiry);
            }

            const Json::Value &slotsJson = firstElement["slots"];
            LOGI("Slots:");

            if (!firstElement.isNull() && slotsJson.isArray()) {
                for (const auto &slotJson : slotsJson) {
                    DialogueData::Slot slot;
                    slot.name = slotJson["name"].asString();
                    slot.values.push_back(slotJson["value"].asString());

                    LOGI("  Slot Name: {}, Slot Value: ", slot.name, slotJson["value"].asString());

                    curIntent.slots.push_back(slot);
                }
            }
        }
    }

    currentTurn.intent_model_result = curModelIntent;
    LOGI("parseSuccessFlag = {}", parseSuccessFlag);

    // // 根据意图、或者解析结果parseSuccessFlag进行判断
    if (curIntent.name.empty() || !parseSuccessFlag || curModelIntent.empty()) {
        std::string verbalTricksStr =
            "由于意图不够清晰明确，抑或模型未能生成指定格式的输出，系统自动"
            "进行"
            "了对话回复的生成。";

        LOGI("{}", verbalTricksStr);

        currentTurn.intent_category = "不明确意图";  // 意图
        currentTurn.turn_keywords = {};              // 当前轮次关键词
        currentState.slots = {};                     // 更新对话状态

        dialogueStatus = unclearIntentionsProcessing(currentDialogue.dialogue_id, currentState, parseSuccessFlag,
                                                     curModelIntent, includeIntentKeyWords, intentModelName());

        printDialogueContent(currentDialogue);

        return dialogueStatus;
    }

    // 意图明确
    LOGD("意图明确");
    LOGD("curIntent.name = {}", curIntent.name);
    currentTurn.intent_model_result = curModelIntent;

    auto mapFindIt = intentMap_.find(curIntent.name);
    std::string curIntentName = (mapFindIt != intentMap_.end()) ? mapFindIt->second : curIntent.name;

    std::string srcIntentName = curIntent.name;
    curIntent.name = curIntentName;

    std::list<std::string> eleSearchRes = TypeTrans::findElementContainingString(
        systemScenarioIntentionsTotal,  // 系统操作、多模态、应用控制等系统控制类场景
        curIntent.name);                // 判断是否为当前体系中的意图

    // 修改成规范的意图名称
    if (!eleSearchRes.empty()) {
        LOGI("eleSearchRes = {}", fmt::join(eleSearchRes, "\t"));
        std::string firstElement = eleSearchRes.front();
        curIntent.name = firstElement;
        LOGI("After conversion, curIntent.name = {}", curIntent.name);
    }

    auto findIt = std::find(DialogueConfig::idleChatName.begin(), DialogueConfig::idleChatName.end(), curIntent.name);

    if (eleSearchRes.empty() || (findIt != DialogueConfig::idleChatName.end())) {
        // 其他/闲聊、知识问答等场景

        std::string verbalTricksStr = "进入闲聊/知识问答场景.";
        LOGD("{}", verbalTricksStr);

        // 生成模型输入
        bool isRagInstruct = false;
        std::string instructName = "默认";
        getInstructionCompleteSession(isFirstRound, userInput, instructName, isRagInstruct, false);

        LOGI("ModelInput for chatting and qa = {}", instructModelInput);

        // 调用端模型（默认模型）进行处理并返回结果,
        // 使用默认提示词，不用传入
        bool success = callChatModelInferenceInterface(instructModelInput).get();

        // 追加callback中生成的对话内容
        if (success) {
            processResult.responseContent = chatGenCompleteText_;
        }

        // // 更新对话状态
        size_t splitIndex = curIntent.name.find('(');
        currentTurn.intent_category = curIntent.name.substr(0, splitIndex);
        currentTurn.turn_keywords = {curIntent.name};  // 当前轮次关键词
        currentState.slots = {};                       // 更新对话状态

        currentState.system_acts = {"对话生成"};                      // 系统动作
        currentState.user_acts = {"请求"};                            // 用户动作
        currentTurn.system_response = processResult.responseContent;  // 系统响应

        currentTurn.dialogue_state = currentState;
        currentDialogue.dialogue.push_back(currentTurn);

        dialogueStatus = TypeTrans::callSuccessIdentifierToJson(processResult);

        return dialogueStatus;
    }

    // // 用户意图在当前体系中存在
    else if (!eleSearchRes.empty() && eleSearchRes.size() == 1 && findIt == DialogueConfig::idleChatName.end()) {
        auto supportedFindIt =
            std::find(supportedSystemInterfaces_.begin(), supportedSystemInterfaces_.end(), curIntent.name);

        if (supportedFindIt != supportedSystemInterfaces_.end()) {
            std::string verbalTricksStr = "可支持的系统控制场景";
            LOGI("{}", verbalTricksStr);

            DialogueData::Intent curIntentCopy = curIntent;

            // 单意图
            // 使用front函数获取第一个元素
            std::string firstElement = eleSearchRes.front();

            LOGI("firstElement = {}", firstElement);

            bool slotIdentifierRes{false};
            auto openPageFindIt = std::find(DialogueConfig::slotsAvailableOnlyPageOpeningScene.begin(),
                                            DialogueConfig::slotsAvailableOnlyPageOpeningScene.end(), curIntent.name);

            if (firstElement.rfind("()") == firstElement.size() - 2 ||
                openPageFindIt != DialogueConfig::slotsAvailableOnlyPageOpeningScene.end()) {
                slotIdentifierRes = true;  // 无槽位判断
            }

            // // 拆分意图和槽位
            size_t splitIndex = firstElement.find('(');
            curIntent.name = firstElement.substr(0, splitIndex);  // 意图

            Json::Value slotList;  // 槽位列表

            currentTurn.intent_category = curIntent.name;  // 当前轮次的意图
            currentTurn.turn_keywords = {curIntent.name};  // 当前轮次关键词

            if (!slotIdentifierRes) {
                // 有槽位的意图场景
                std::string verbalTricksStr = "~~~~有槽位的意图场景，规范化槽位信息~~~~";

                LOGI("{}", verbalTricksStr);
                std::vector<DialogueData::Slot> currentSlotCopy = currentSlot;

                slotList = intentSlotJsonData["hasSlotsLabel"][firstElement];
                std::string slotName_;

                // 需要遍历所有的槽位
                for (const auto &slotName : slotList) {
                    slotName_ = slotName.asString();
                    LOGI("slotName = {}", slotName_);

                    currentSlotCopy.emplace_back();

                    size_t foundPos = slotName_.find('*');
                    if (foundPos != std::string::npos) {
                        // 存在，必填槽
                        LOGI("\t存在，必填槽");
                        std::regex pattern("\\*");
                        slotName_ = std::regex_replace(slotName_, pattern, "");

                        currentSlotCopy.back().name = slotName_;
                        currentSlotCopy.back().isRequired = true;
                    } else {
                        currentSlotCopy.back().name = slotName_;
                        currentSlotCopy.back().isRequired = false;
                    }

                    currentSlotCopy.back().values = {};
                    LOGI("isRequired = {}", currentSlotCopy.back().isRequired);
                }

                // 填充当前轮次意图的所有槽位
                for (auto &eleSlot : currentSlotCopy) {
                    LOGI("eleSlot.name = {}", eleSlot.name);

                    for (auto &curSlot : curIntent.slots) {
                        LOGI("\tcurSlot.name = {}", curSlot.name);

                        if ((eleSlot.name == curSlot.name ||
                             (curSlot.name == "类型" && curIntent.name == "多模态输入识别_多模态输入识别" &&
                              eleSlot.name == "API接口名称")) &&
                            !curSlot.values.empty()) {
                            for (auto &valueEle : curSlot.values) {
                                LOGI("\t\tvalueEle = {}", valueEle);
                                if (valueEle == "大" && curIntent.name == "系统操作_音量控制") {
                                    valueEle = "增大";
                                } else if (valueEle == "小" && curIntent.name == "系统操作_音量控制") {
                                    valueEle = "减小";
                                } else if (valueEle == "高" && curIntent.name == "系统操作_音量控制") {
                                    valueEle = "调高";
                                } else if (valueEle == "低" && curIntent.name == "系统操作_音量控制") {
                                    valueEle = "调低";
                                } else if (valueEle == "快进" && curIntent.name ==
                                                                     "第三方应用控制_多媒体-"
                                                                     "播放器控制") {
                                    valueEle = "下一首";
                                } else if (valueEle == "快退" && curIntent.name ==
                                                                     "第三方应用控制_多媒体-"
                                                                     "播放器控制") {
                                    valueEle = "上一首";
                                }
                                eleSlot.values.push_back(valueEle);
                            }
                        }
                    }
                }

                currentState.slots = currentSlotCopy;  // 更新对话状态

                curIntent.slots = currentSlotCopy;  // 重新更新curIntent
                curIntentCopy.slots = currentSlotCopy;

                // 槽位取值规范化处理
                curIntentCopy = slotNormalizationProcess(curIntentCopy, slotsValueNormalJson, modelDimension);

                curIntent.slots = curIntentCopy.slots;  // 重新更新curIntent
            }

            LOGI("~~~~~~~~~~~~打印当前轮次意图槽位信息~~~~~~~~~~~~~~");
            LOGI("curIntent.name = {}", curIntent.name);

            for (const auto &ele : curIntent.slots) {
                LOGI("\tcurSlot.name = {}", ele.name);
                LOGI("\tcurSlot.isRequired = {}", ele.isRequired);
                for (const auto &slotValue : ele.values) {
                    LOGI("\t\tValue = {}", slotValue);
                }
            }

            // // 进入系统控制流程
            processResult = systemApplicationControlMain(curIntent, userInput, slotIdentifierRes);

            // 更新对话状态
            if (processResult.slotMissing) {
                currentState.system_acts = {"询问"};  // 系统动作

            } else if (processResult.isMultiModalDoc || slotIdentifierRes) {
                currentState.system_acts = {"对话生成"};  // 系统动作
            } else {
                currentState.system_acts = {"回复"};  // 系统动作
            }

            // // 更新对话状态
            currentState.slots = curIntent.slots;
            currentState.user_acts = {"请求"};                            // 用户动作
            currentTurn.system_response = processResult.responseContent;  // 系统响应

            currentTurn.dialogue_state = currentState;
            currentDialogue.dialogue.push_back(currentTurn);

            printDialogueContent(currentDialogue);

            dialogueStatus = TypeTrans::callSuccessIdentifierToJson(processResult);

            return dialogueStatus;
        } else {
            // 目前尚未开放此系统控制场景
            size_t splitIndex = curIntent.name.find('(');
            std::string secondLabelName = curIntent.name.substr(0, splitIndex);

            size_t splitIdx = secondLabelName.find('_');
            std::string interfaceName = secondLabelName.substr(splitIdx + 1);
            LOGI("interfaceName = {}", interfaceName);
            std::string verbalTricksStr =
                "系统控制场景【" + interfaceName + "】现阶段尚未开放，敬请期待后续更新与启用。";

            LOGI("verbalTricksStr = {}", verbalTricksStr);
            processResult.responseContent = verbalTricksStr;
            dialogueStatus = TypeTrans::callSuccessIdentifierToJson(processResult);

            // 文本类callback
            std::string modelName = intentModelName();
            bool isError = false;
            std::string errorMessage;
            enum DialogueCallback::Type dataType = DialogueCallback::IntentNormal;
            textCallback(processResult.responseContent, modelName, isError, 0, errorMessage, dataType, "");
            // 重置feedback
            feedbackUrlForCloudModel = "";
            return dialogueStatus;
        }
    } else {
        // // 多意图的处理
        std::string verbalTricksStr =
            "AI助手在多意图处理与识别场景方面的功能尚未开启，暂无"
            "法为您提供相应服务。我们正不懈努力，致力于在后续的升级中完"
            "善此功能，以期给您带来更为卓越、智能的使用体验";

        LOGD("{}", verbalTricksStr);

        processResult.responseContent = verbalTricksStr;
        dialogueStatus = TypeTrans::callSuccessIdentifierToJson(processResult);

        // 文本类callback
        std::string modelName = intentModelName();
        bool isError = false;
        std::string errorMessage;
        enum DialogueCallback::Type dataType = DialogueCallback::IntentNormal;
        textCallback(processResult.responseContent, modelName, isError, 0, errorMessage, dataType, "");
        // 重置feedback
        feedbackUrlForCloudModel = "";
        return dialogueStatus;
    }
}

void IntentionRecognition::imageInstructionCallback(const std::string &responseStr, const std::string &modelName,
                                                    bool isError, int errorCode, const std::string &errorMessage,
                                                    enum DialogueCallback::Type &dataType, const std::string &fileType,
                                                    int toolId, const std::string &toolDescription,
                                                    const std::string &toolData,
                                                    const std::string &feedbackUrl) {  // 图像指令类的callback格式

    // // 结果转换成助手的输出格式
    Json::Value assistantText;

    assistantText["message_type"] = "tool_call";
    if (fileType == "文档") {
        assistantText["file_type"] = "document";
    } else if (fileType == "图片") {
        assistantText["file_type"] = "image";
    } else {
        assistantText["file_type"] = "schedule";  // 日历
    }

    // 创建 "content" 数组节点，添加元素
    Json::Value contentArray;

    Json::Value contentElement;
    contentElement["type"] = "text";
    contentElement["text"] = responseStr;
    contentArray.append(contentElement);

    Json::Value contentElementLink;
    contentElementLink["tool_id"] = toolId;
    contentElementLink["tool_name"] = fileType;
    contentElementLink["tool_description"]["description"] = toolDescription;
    contentElementLink["tool_description"]["parameter"] = toolData;
    contentArray.append(contentElementLink);

    assistantText["content"] = contentArray;

    if (!isError) {
        assistantText["error_code"] = 0;
        assistantText["error_message"] = "";
        if (modelInferenceInterface_ != nullptr) {
            if (modelInferenceInterface_->isCloudModel() && !feedbackUrl.empty()) {
                assistantText["feedback_url"] = feedbackUrl;
            }
        }
    } else {
        assistantText["error_code"] = 1;
        assistantText["error_message"] = errorMessage;
    }

    assistantText["model"] = modelName;

    Json::StreamWriterBuilder jswBuilder;
    jswBuilder["emitUTF8"] = true;
    std::unique_ptr<Json::StreamWriter> jsWriter(jswBuilder.newStreamWriter());

    std::ostringstream os;
    jsWriter->write(assistantText, &os);
    std::string assistantTextStr = os.str();
    LOGI("assistantText = {}", assistantTextStr);

    // 调用助手的回调函数
    DialogueCallback::ChatData tempDialogueData;
    tempDialogueData.text_ = assistantTextStr;

    if (!isError) {
        tempDialogueData.type_ = dataType;
        callOutputCallback(tempDialogueData.type_, tempDialogueData.text_);

    } else {
        tempDialogueData.type_ = dataType;
        tempDialogueData.errorCode_ = errorCode;
        tempDialogueData.errorMessage_ = errorMessage;

        callOutputCallback(tempDialogueData.type_, assistant::message::createErrorMessageResult(
                                                       tempDialogueData.errorCode_, tempDialogueData.errorMessage_));
    }
}

bool IntentionRecognition::isTextToImageCommand(const std::string &prompt) {
    bool isConditionMet = false;
    for (const std::string &keyword : DialogueConfig::wordJudgment) {
        size_t index = prompt.find(keyword);
        if (index != std::string::npos) {
            isConditionMet = true;
        }
    }
    if (not isConditionMet) {
        return false;
    }

    isConditionMet = false;

    for (const std::string &keyword : DialogueConfig::mainFont) {
        size_t index = prompt.find(keyword);
        if (index != std::string::npos) {
            isConditionMet = true;
        }
    }
    if (not isConditionMet) {
        return false;
    }

    return true;
}

DialogueData::CallSuccessIdentifier IntentionRecognition::multiModalInputRecognition(
    DialogueData::Intent &curTurnIntent, const std::string &userInput) {
    std::map<std::string, std::list<std::string>> tempSlot;
    DialogueData::CallSuccessIdentifier callResult;

    for (const auto &slot : curTurnIntent.slots) {
        tempSlot[slot.name] = slot.values;
    }

    auto it = tempSlot.find("附件类型");
    if (it != tempSlot.end() && !tempSlot["附件类型"].empty()) {
        auto it_ = tempSlot.find("API接口名称");

        if (it_ != tempSlot.end() && !tempSlot["API接口名称"].front().empty()) {
            LOGI("tempSlot[\"API接口名称\"].front() = {}", tempSlot["API接口名称"].front());
        }

        std::string multiCheckRes = TypeTrans::checkList(tempSlot["附件类型"]);

        if (multiCheckRes == "图片") {
            // 图像场景是有槽位的

            if (tempSlot["API接口名称"].size() == 1 && tempSlot["API接口名称"].front().empty()) {
                callResult.responseContent = "您想生成一张什么主题风格的图片呢？赶快告诉我吧.";
                callResult.successRes = true;
                callResult.slotMissing = true;

                // 纯文本类callback
                std::string modelName = intentModelName();
                bool isError = false;
                std::string errorMessage;
                DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                textCallback(callResult.responseContent, modelName, isError, 0, errorMessage, dataType, "");
            } else if (tempSlot["API接口名称"].front() != "文生图") {
                if (TypeTrans::findAPIName(tempSlot["API接口名称"].front(), functionInterfaceName_, "图片").found ||
                    tempSlot["API接口名称"].front() == "图片处理") {
                    callResult.responseContent =
                        "请先输入一张图片，之后按照助手给出的引导信息输入就行啦"
                        "~";
                    callResult.successRes = true;

                    // 纯文本类callback
                    std::string modelName = intentModelName();
                    bool isError = false;
                    std::string errorMessage;
                    DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                    textCallback(callResult.responseContent, modelName, isError, 0, errorMessage, dataType, "");
                } else {
                    // 不支持的图像类接口
                    callResult.responseContent =
                        "很抱歉，这个功能暂时不支持使用呢，敬请期待后续更新与启"
                        "用吧~";
                    callResult.successRes = true;

                    // 纯文本类callback
                    std::string modelName = intentModelName();
                    bool isError = false;
                    std::string errorMessage;
                    DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                    textCallback(callResult.responseContent, modelName, isError, 0, errorMessage, dataType, "");
                }

            } else {
                LOGD("callImageHandlerInterface");
                // 进入该逻辑的，只能是文生图
                callResult = callImageHandlerInterface(userInput);

                std::string modelName = intentModelName();

                if (callResult.errorCode != 0 && callResult.errorCode != -1) {
                    // 接口出错
                    callResult.responseContent = "哎呀，不好意思，多模态输入识别场景文生图出现了小状况~";
                    bool isError = true;
                    std::string errorMessage = "ImageHandler-TextToImage interface call failed.";

                    DialogueCallback::Type dataType = DialogueCallback::SystemOperationErrorOccurred;
                } else {
                    // 调用文生图callback
                    // 追加callback中生成的图片内容
                    callResult.responseContent = "以下是精心为您创作生成的图片，敬请查收与欣赏。 ";
                    callResult.additionalContent = imageData_;
                    callResult.successRes = true;
                    bool isError = false;
                    std::string errorMessage;
                    enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                }
            }
        } else {
            callResult.isMultiModalDoc = true;
            // 生成模型输入
            bool isRagInstruct = false;
            std::string instructName = "默认";
            getInstructionCompleteSession(false, userInput, instructName, isRagInstruct, false);

            LOGI("ModelInput for Multimodal document class = {}", instructModelInput);

            // 调用端模型（默认模型）进行处理并返回结果,
            // 使用默认提示词，不用传入
            bool success = callChatModelInferenceInterface(instructModelInput).get();

            // 追加callback中生成的对话内容
            if (success) {
                callResult.responseContent = chatGenCompleteText_;
            }
        }
    } else {
        // 附件类型为空
        callResult.responseContent =
            "实在不好意思，我好像没完全理解您的需求。您能否麻烦再详细说说您想处"
            "理的文件类型，以及打算对文件执行怎样的操作呢？这样我就能更准确地回"
            "应您，为您提供更贴心的服务啦。 ";
        callResult.successRes = true;
        callResult.slotMissing = true;

        // 纯文本类callback
        std::string modelName = intentModelName();
        bool isError = false;
        std::string errorMessage;
        DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
        textCallback(callResult.responseContent, modelName, isError, 0, errorMessage, dataType, "");
    }
    // 重置feedback
    feedbackUrlForCloudModel = "";
    return callResult;
}

DialogueData::CallSuccessIdentifier IntentionRecognition::systemApplicationControlMain(
    DialogueData::Intent &curTurnIntent, const std::string &userInput, bool slotIdentifier) {
    LOGI("userInput = {}", userInput);

    LOGI("正在处理的场景 = systemApplicationControlMain -- {}", curTurnIntent.name);

    size_t splitIndex = curTurnIntent.name.find('_');
    std::string interfaceName = curTurnIntent.name.substr(splitIndex + 1);
    LOGI("interfaceName = {}", interfaceName);

    DialogueData::CallSuccessIdentifier callIdentify;

    // 多模态输入识别模块--用户输入除指令类外的文本,经过意图识别模型推理的处理逻辑
    if (curTurnIntent.name == "多模态输入识别_多模态输入识别") {
        callIdentify = multiModalInputRecognition(curTurnIntent, userInput);
        return callIdentify;
    }

    if (TypeTrans::isStringInList(DialogueConfig::applicationCategoriesIntent,
                                  curTurnIntent.name.substr(0, curTurnIntent.name.find('_')))) {
        if (slotIdentifier) {
            // 无槽位场景
            callIdentify = callSystemOperationInterface(curTurnIntent);  // 调用系统命令接口

            // 异常码判断
            if (!callIdentify.successRes) {
                callIdentify.responseContent =
                    "很抱歉这次未能成功为您弹出【 " + interfaceName + " 】页面，或许您可以尝试换一种与我对话的方式";

            } else {
                callIdentify.responseContent = "您请求的【 " + interfaceName + " 】页面已成功弹出";
            }

        } else {
            // 有槽位场景，命令控制类,直接判断必填槽是否缺失即可
            // 判断必填槽是否存在，且不为空

            std::list<std::string> emptySlotName;
            std::list<std::string> noEleList = {""};
            for (auto &slot : curTurnIntent.slots) {
                LOGI("hasSlot -- slot.isRequired = {}", slot.isRequired);
                if (slot.isRequired == true && (slot.values.empty() || slot.values == noEleList)) {
                    LOGI("hasSlot -- slot.name = {} 槽位为空", slot.name);

                    // requiredSlotNotEmpty = false; // 必填槽为空
                    emptySlotName.push_back(slot.name);  // 必填槽为空的槽位名称
                } else if (slot.isRequired == false && (slot.values.empty() || slot.values == noEleList) &&
                           curTurnIntent.name == "系统操作_蓝牙操作") {
                    slot.values = {"打开"};
                }
            }

            LOGI("emptySlotName.ele = ", fmt::join(emptySlotName, "\t"));

            if (emptySlotName.empty()) {
                // 无必填槽或者必填槽不为空,
                callIdentify = callSystemOperationInterface(curTurnIntent);  // 调用系统命令接口

                // 异常码判断
                if (!callIdentify.successRes ||
                    (callIdentify.errorCode != 0 && callIdentify.errorCode != -1)) {  // 接口出错
                    if (curTurnIntent.name == "系统操作_设置显示器屏幕缩放") {
                        callIdentify.responseContent = "很遗憾，这次【 " + interfaceName +
                                                       " 】调整不太顺利，"
                                                       "可能是缩放比例有误，请您输入100%-150%"
                                                       "之间的数值";
                    } else if (std::find(DialogueConfig::intentIndividualScene.begin(),
                                         DialogueConfig::intentIndividualScene.end(),
                                         curTurnIntent.name) != DialogueConfig::intentIndividualScene.end()) {
                        callIdentify.responseContent = "很抱歉这次未能成功为您弹出【 " + interfaceName +
                                                       " 】页面，或许您可以尝试换一种与我对话的方式";
                    } else {
                        callIdentify.responseContent = "很遗憾，这次【 " + interfaceName +
                                                       " 】调整未能成功，可能是有些细节信息有所遗漏"
                                                       "或不太符合要求，烦请您再描述得详细些";
                    }
                } else {
                    bool displayScaling;
                    if (curTurnIntent.name == "系统操作_设置显示器屏幕缩放") {
                        displayScaling = TypeTrans::checkAndSetScaling(curTurnIntent.slots);
                        LOGI("displayScaling = {}", (displayScaling ? "true" : "false"));
                    }

                    if (!displayScaling && curTurnIntent.name == "系统操作_设置显示器屏幕缩放") {
                        callIdentify.responseContent = "很遗憾，这次【 " + interfaceName +
                                                       " 】调整不太顺利, "
                                                       "可能是缩放比例有误，请您输入100%-150%之间的数值";

                    } else {
                        if (curTurnIntent.name == "系统操作_设置显示器屏幕缩放") {
                            callIdentify.responseContent = "恭喜您！【 " + interfaceName +
                                                           " 】调整成功，接下来只需您重启电脑，就能即刻生"
                                                           "效";
                        } else if (std::find(DialogueConfig::intentIndividualScene.begin(),
                                             DialogueConfig::intentIndividualScene.end(),
                                             curTurnIntent.name) != DialogueConfig::intentIndividualScene.end()) {
                            callIdentify.responseContent = "您请求的【 " + interfaceName + "】页面已成功弹出";
                        } else if (curTurnIntent.name == "第三方应用控制_应用控制") {
                            callIdentify.responseContent = "恭喜您！应用页面已打开";
                        } else if (curTurnIntent.name == "第三方应用控制_多媒体-播放器控制") {
                            callIdentify.responseContent = "您请求的【 " + interfaceName + " 】调整成功";
                        } else {
                            callIdentify.responseContent =
                                "恭喜您！【 " + interfaceName + " 】调整成功，烦请您移步至设置中查看";
                        }
                    }
                }
            } else {
                // 必填槽为空
                // 若必填槽为空，则询问，进行补槽

                if (!curTurnIntent.inquiry.empty() && curTurnIntent.name != "系统操作_设置系统字体") {
                    callIdentify.responseContent = curTurnIntent.inquiry;
                } else {
                    std::string missingScript = missingSlotScript[curTurnIntent.name][emptySlotName.front()].asString();
                    LOGI("~~~~~EmptySlot = {}\tScript = {}", emptySlotName.front(), missingScript);

                    callIdentify.responseContent = missingScript;
                }

                callIdentify.successRes = true;
                callIdentify.slotMissing = true;
            }
        }

        // 纯文本类callback
        std::string modelName = intentModelName();
        if (!callIdentify.successRes || (callIdentify.errorCode != 0 && callIdentify.errorCode != -1)) {
            bool isError = true;
            std::string errorMessage = "System operation interface call failed.";
            DialogueCallback::Type dataType = DialogueCallback::SystemOperationErrorOccurred;
            int errorCode = int(assistant::ErrorCode::SystemOperationFailed);
            textCallback(callIdentify.responseContent, modelName, isError, errorCode, errorMessage, dataType, "");
        } else {
            bool isError = false;
            std::string errorMessage;
            DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
            textCallback(callIdentify.responseContent, modelName, isError, 0, errorMessage, dataType, "");
        }
        // 重置feedback
        feedbackUrlForCloudModel = "";
        return callIdentify;
    }

    return callIdentify;
}

Json::Value IntentionRecognition::textInstructionScenario(const std::string &userInput, bool isFirstRound,
                                                          const std::string &userFile) {
    // 文本类指令场景
    DialogueData::CallSuccessIdentifier callResult;
    DialogueData::DialogueState currentState;
    std::vector<DialogueData::Slot> currentSlot;

    currentTurn.intent_category = instructionName_;
    currentTurn.turn_keywords = {instructionName_};

    currentState.slots = {currentSlot};
    currentState.system_acts = {"指令场景回复"};
    currentState.user_acts = {"请求"};

    // // 调用对应的功能接口，进行处理
    LOGI("调用指令类接口：callChatModelInference()");

    // 生成模型输入
    bool isRagInstruct = false;
    std::string documentContent;
    std::string instructUserInput;

    if (!userFile.empty() && (promptId_ == meetingInformationExtractionPromptId ||
                              promptId_ == todoListGenerationPromptId || promptId_ == weeklyReportGenerationPromptId)) {
        // 指令场景（除系统助手）不需要知识库
        documentContent =
            callFileInferenceInterfaceFileContent(defaultKBName, userFile).responseContent;  // 获取文件内容
        instructUserInput = documentContent + "\nquestion: " + userInput + "\n";

    } else {
        instructUserInput = userInput;
    }

    getInstructionCompleteSession(isFirstRound, instructUserInput, instructionName_, isRagInstruct, false);
    LOGI("Instruction Model's modelInput = {}", instructModelInput);

    bool success = callChatModelInferenceInterface(instructModelInput).get();

    // 追加callback中生成的对话内容
    if (success) {
        callResult.responseContent = chatGenCompleteText_;
        if (!dialogueStop_) {
            // 处理日程和待办场景
            if (promptId_ == meetingInformationExtractionPromptId) {
                LOGI("chatGenCompleteText_ = {}", chatGenCompleteText_);
                DialogueData::CallSuccessIdentifier callSuccess = callSystemScheduleManagement(chatGenCompleteText_);

                if (callSuccess.successRes == true) {
                    // 使用图像指令类callback
                    std::string responseStr =
                        "您的日程已新建成功！您可以在 日历 "
                        "插件中查看、修改或删除此日程。";
                    std::string modelName = currentModelNameInUse;
                    std::string errorMessage;
                    std::string descriptionStr = "在日历插件中查看、修改或删除此日程";
                    std::string scheduleDate = callSuccess.scheduleDate;
                    enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                    imageInstructionCallback("", modelName, false, 0, errorMessage, dataType, "日程", 21,
                                             descriptionStr, scheduleDate, feedbackUrlForCloudModel);
                    // 重置feedback
                    feedbackUrlForCloudModel = "";
                } else {
                    // 纯文本类callback
                    std::string responseStr =
                        "非常抱歉，我无法为您新建日程，因为您没有提供开始时间等"
                        "信息"
                        "，或者您提供的信息我无法识别。为了成功新建日程，请确保"
                        "您提"
                        "供了清晰、准确的日程信息。";
                    std::string modelName = currentModelNameInUse;
                    int errorCode = int(assistant::ErrorCode::CreateScheduleFailed);
                    std::string errorMessage = responseStr;
                    enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationErrorOccurred;
                    textCallback(responseStr, modelName, true, errorCode, errorMessage, dataType, "");
                }
            } else if (promptId_ == todoListGenerationPromptId) {
                DialogueData::CallSuccessIdentifier callSuccess = callSystemToDoList(chatGenCompleteText_);

                if (callSuccess.successRes == true) {
                    // 使用图像指令类callback
                    std::string todoListHeadStr = "以下是为您生成的待办:\n";
                    std::string responseStr = todoListHeadStr + chatGenCompleteText_;
                    std::string modelName = currentModelNameInUse;
                    std::string errorMessage;
                    std::string descriptionStr = "在日历插件中查看、修改或删除此日程";
                    std::string scheduleDate = callSuccess.scheduleDate;
                    enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                    imageInstructionCallback(responseStr, modelName, false, 0, errorMessage, dataType, "待办", 22,
                                             descriptionStr, scheduleDate, feedbackUrlForCloudModel);
                    // 重置feedback
                    feedbackUrlForCloudModel = "";
                } else {
                    // 纯文本类callback
                    std::string responseStr =
                        "非常抱歉，我无法为您生成待办，因为您没有提供开始时间等"
                        "信息"
                        "，或者您提供的信息我无法识别。为了成功生成待办，请确保"
                        "您提"
                        "供了清晰、准确的日程信息。";
                    std::string modelName = currentModelNameInUse;
                    int errorCode = int(assistant::ErrorCode::CreateToDoListFailed);
                    std::string errorMessage = responseStr;
                    enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationErrorOccurred;
                    textCallback(responseStr, modelName, true, errorCode, errorMessage, dataType, "");
                }
            }
        } else {
            if (promptId_ == todoListGenerationPromptId || promptId_ == meetingInformationExtractionPromptId) {
                dialogueStop_ = false;
                // 传回助手一个结束标识符
                std::string responseStr;
                std::string errorMessage;
                std::string modelName = currentModelNameInUse;
                enum DialogueCallback::Type dataType = DialogueCallback::SystemOperationNormal;
                textCallback(responseStr, modelName, false, 0, errorMessage, dataType, feedbackUrlForCloudModel);
            }
        }
    } else {
        callResult.responseContent = "哎呀，系统【对话】接口好像出了点小状况哦~ ";
    }

    currentTurn.dialogue_state = currentState;
    currentTurn.system_response = callResult.responseContent;
    DialogueData::TokenCount responseTokenNumber = calculateTokenNumber(callResult.responseContent);
    currentTurn.response_token_number = responseTokenNumber.totalTokens;
    currentDialogue.dialogue.push_back(currentTurn);

    Json::Value dialogueStatus = TypeTrans::callSuccessIdentifierToJson(callResult);

    // 重置，目前设定的选一次指令，默认一直为该指令场景,以下三种场景是单次的
    if (promptId_ == meetingInformationExtractionPromptId || promptId_ == todoListGenerationPromptId ||
        promptId_ == weeklyReportGenerationPromptId) {
        promptPassed = false;
        promptId_ = defaultPromptId;
        instructionName_ = "默认";
        instructionPrompt_ = defaultPromptContent;
    }

    printDialogueContent(currentDialogue);
    // 重置feedback
    feedbackUrlForCloudModel = "";
    return dialogueStatus;
}

std::pair<std::string, std::string> IntentionRecognition::processSlotValue(Json::Value &slotsList,
                                                                           const std::string &slotStr,
                                                                           const std::string &intentName,
                                                                           const std::string &slotName, int &dim) {
    if (!TypeTrans::isStringInJsonArray(slotsList, slotStr)) {
        LOGI("TypeTrans::isPureUTF8ChineseText(slotStr) = {}, slotStr = {}", TypeTrans::isPureUTF8ChineseText(slotStr),
             slotStr);
        if (TypeTrans::isPureUTF8ChineseText(slotStr)) {
            // 文本类槽位，且不在槽位取值范围内，进行规划范处理
            // 计算相似度，选取top1作为规范化结果
            LOGI("处理文本型槽位");
            Json::Value vectorSearchResult;
            std::pair<std::string, std::string> replacedSlot = {"", "未对槽位进行规范化转换"};

            // 调用文本向量化接口
            DialogueCallback::TextEmbeddingResult embeddingResult = callTextEmbeddingInterface(slotStr);
            LOGI("embeddingResult.embedding.size = {}", embeddingResult.embedding.size());

            if (!textVectorResult.empty() && textVectorResult.size() == modelDimension) {
                // 调用向量数据库检索接口
                slotsNormalization_ = std::make_shared<SlotsNormalization>();
                vectorSearchResult = slotsNormalization_->Search(textVectorResult, slotStr, slotName, intentName);
                // 打印向量化检索结果
                LOGI("vectorSearchResult : {}", util::jsonToString(vectorSearchResult));

                if (!vectorSearchResult["Top_K_Label"].empty() && vectorSearchResult["Top_K_Label"].isArray()) {
                    replacedSlot = {vectorSearchResult["Top_K_Label"][0].asString(), "已成功规范化槽位"};
                    return replacedSlot;
                }
            }
            return {slotStr, "向量化服务异常，未做规范化转换"};
        } else if (TypeTrans::isInteger(slotStr) || TypeTrans::isInteger(TypeTrans::removeSuffix(slotStr))) {
            // 如果是整数，判断是否在取值范围内，不超出则不用进一步处理；对于超出取值范围的进行就近处理
            LOGI("处理数值型槽位 -- 整数");

            auto result = TypeTrans::isSlotInRange(slotStr, intentName, DialogueConfig::integerScenarios);
            if (!result.first.empty()) {
                LOGI("\t最终处理后的值为: {}", result.first);
            }
            if (!result.second.empty()) {
                LOGI("\t提示信息: {}", result.second);
            }

            return result;
        } else if (TypeTrans::isDecimal(slotStr) || TypeTrans::isDecimal(TypeTrans::removeSuffix(slotStr))) {
            // 如果是小数，判断是否在取值范围内，不超出则不用进一步处理；对于超出取值范围的进行就近处理
            LOGI("处理数值型槽位 -- 小数");

            auto decimalResult = TypeTrans::isSlotInDecimalRange(slotStr, intentName, DialogueConfig::decimalScenarios,
                                                                 DialogueConfig::integerScenarios);
            if (!decimalResult.first.empty()) {
                LOGI("\t最终处理后的值为: {}", decimalResult.first);
            }
            if (!decimalResult.second.empty()) {
                LOGI("\t提示信息: {}", decimalResult.second);
            }

            return decimalResult;
        } else {
            // 槽位类型不支持的场景，将槽位置为空, 并返回引导的话术
            return {"", "不合理的槽位类型，请按提示重新输入"};
        }
    } else {
        // 本身是规范化槽位，不用进一步处理
        return {slotStr, "规范化槽位"};
    }
}

DialogueData::Intent IntentionRecognition::slotNormalizationProcess(DialogueData::Intent &curIntentSlots,
                                                                    Json::Value &slotsValueJson, int &dim) {
    LOGI("~~~~文本向量化进行槽位规范化处理~~~~");
    auto findSlot = std::find(DialogueConfig::slotNormalizationFilterList.begin(),
                              DialogueConfig::slotNormalizationFilterList.end(), curIntentSlots.name);

    if (findSlot == DialogueConfig::slotNormalizationFilterList.end()) {
        LOGI("curIntentSlots.name = {}", curIntentSlots.name);

        for (auto &ele : curIntentSlots.slots) {
            if (slotsValueJson.isMember(curIntentSlots.name) && !slotsValueJson.isNull() && slotsValueJson.isObject()) {
                Json::Value slotsValue = slotsValueJson[curIntentSlots.name];
                if (!slotsValue.isNull() && slotsValue.isMember(ele.name) && slotsValue.isObject()) {
                    // 查找 "屏幕亮度值" 节点
                    Json::Value slotsValueList = slotsValue[ele.name];

                    // 打印
                    LOGI("\tslotsValue = {}", util::jsonToString(slotsValueList));

                    if (slotsValueList.isArray() && !slotsValueList.empty()) {
                        for (auto &slot_ : ele.values) {
                            LOGI("\t\tValue = {}", slot_);
                            LOGI("\t\t字符串 \"{}\" 是否在slotsValueList中: {}", slot_,
                                 (TypeTrans::isStringInJsonArray(slotsValueList, slot_) ? "是" : "否"));

                            std::pair<std::string, std::string> replaceSlotResult =
                                processSlotValue(slotsValueList, slot_, curIntentSlots.name, ele.name, dim);

                            // 替换槽位值
                            if (!replaceSlotResult.first.empty()) {
                                LOGI("槽位规范化处理后的值为: {}\t{}", replaceSlotResult.first,
                                     replaceSlotResult.second);
                                slot_ = replaceSlotResult.first;
                            } else if (!replaceSlotResult.second.empty()) {
                                LOGI("存在无法识别或不满足条件的槽位: {}", replaceSlotResult.second);
                                if (ele.name != "桌面应用名称") {
                                    slot_ = replaceSlotResult.first;
                                }
                                curIntentSlots.abnormalSlotScript = replaceSlotResult.second;
                            }
                        }
                    } else {
                        LOGI(
                            "slotsValueList "
                            "不是数组或者为空，无法进行规范化处理");
                    }
                } else {
                    LOGI(
                        "槽位 -- {} "
                        "不在规范化的槽位列表中，不需要做规范化处理",
                        ele.name);
                }
            } else {
                LOGI("意图 -- {} 不在槽位规范化的意图列表中，不需要做规范化处理", curIntentSlots.name);
            }
        }
    } else {
        LOGI("意图 -- {} 在过滤列表中，不需要做规范化处理", curIntentSlots.name);
    }

    return curIntentSlots;
}

Json::Value IntentionRecognition::createCustomizeDataBase(const std::string &userFile, bool &updateSuccess) {
    std::future<std::string> createResult;
    Json::Value dialogueResponse;
    DialogueData::CallSuccessIdentifier processResult;

    // 创建或新增向量数据库
    if (isFirstAfferentFile) {
        // 初始会话或者清除上下文恢复到初始状态
        createResult = createFileKnowledgeBase();
        if (createResult.wait_for(std::chrono::seconds(300)) == std::future_status::ready) {
            customizeKBName = createResult.get();
            if (!customizeKBName.empty()) {
                DialogueData::FileSearchIdentifier createKBResult =
                    updateFileKnowledgeBase(customizeKBName, documentFilePath_);
                if (createKBResult.errorCode) {
                    std::string responseMessage =
                        "向量数据库创建失败，请检查向量数据库服务是否正常"
                        "。";
                    enum DialogueCallback::Type dataType = DialogueCallback::IntentErrorOccurred;
                    int errorCode = int(assistant::ErrorCode::UnknownError);
                    textCallback(responseMessage, "", true, createKBResult.errorCode, createKBResult.errorMessage,
                                 dataType, "");
                    return dialogueResponse;
                }
                LOGI("创建知识库成功，{}", customizeKBName);
                updateSuccess = true;

            } else {
                LOGE("创建知识库失败,自定义用户数据库名称为空");
                // 文本类callback
                std::string responseStr =
                    R"(创建知识库失败,自定义用户数据库名称为空。请检查向量数据库接口服务是否正常。)";
                // std::string modelName = intentModelName();
                bool isError = true;
                std::string errorMessage = "Create knowledge base failed.";
                int errorCode = int(assistant::ErrorCode::UnknownError);
                enum DialogueCallback::Type dataType = DialogueCallback::RagInterfaceErrorOccurred;
                textCallback(responseStr, "", isError, errorCode, errorMessage, dataType, "");
                processResult.responseContent = responseStr;
                dialogueResponse = TypeTrans::callSuccessIdentifierToJson(processResult);
                return dialogueResponse;
            }

        } else {
            // 超时处理
            // 文本类callback
            std::string responseStr = R"(创建知识库失败,接口服务请求超时，请检查向量数据库接口服务是否正常。)";
            LOGI("responseStr: {}", responseStr);

            bool isError = true;
            std::string errorMessage =
                "Create vector database timeout, unable to obtain "
                "results.";
            int errorCode = int(assistant::ErrorCode::UnknownError);
            enum DialogueCallback::Type dataType = DialogueCallback::RagInterfaceErrorOccurred;
            textCallback(responseStr, "", isError, errorCode, errorMessage, dataType, "");
            processResult.responseContent = responseStr;
            dialogueResponse = TypeTrans::callSuccessIdentifierToJson(processResult);
            return dialogueResponse;
        }
        currentTurn.knowledge_base_name = customizeKBName;

        // 重置
        isFirstAfferentFile = false;

    } else if (!isFirstAfferentFile && !userFile.empty() && !customizeKBName.empty()) {
        // 更新已有的向量数据库
        LOGI("update vector database. customizeKBName = {} ", customizeKBName);
        DialogueData::FileSearchIdentifier updateKBResult = updateFileKnowledgeBase(customizeKBName, documentFilePath_);
        if (updateKBResult.errorCode) {
            std::string responseMessage =
                "向量数据库创建失败，请检查向量数据库服务是否正常"
                "。";
            enum DialogueCallback::Type dataType = DialogueCallback::IntentErrorOccurred;
            textCallback(responseMessage, "", true, updateKBResult.errorCode, updateKBResult.errorMessage, dataType,
                         "");
            return dialogueResponse;
        }
        LOGI("创建知识库成功，{}", customizeKBName);
        updateSuccess = true;

        currentTurn.knowledge_base_name = customizeKBName;
    }

    return dialogueResponse;
}

DialogueData::TokenCount IntentionRecognition::calculateTokenNumber(const std::string &userInput) {
    // // 计算总字符数
    int userInputLength = static_cast<int>(userInput.size());
    LOGI("字符总数: {}", userInputLength);

    // // 计算英文字符数
    int englishCharactersNumber = TypeTrans::countEnglishChars(userInput);
    LOGI("英文字符总数 = {}", englishCharactersNumber);

    int enTokens = static_cast<int>(std::ceil(static_cast<float>(englishCharactersNumber) / DialogueConfig::enBytes /
                                              DialogueConfig::enCorrespondence));
    LOGI("英文字符Token总数 = {}", enTokens);

    // 中文字符数转token
    int chineseCharactersNumber = userInputLength - englishCharactersNumber;
    LOGI("中文字符总数 = {}", chineseCharactersNumber);
    int zhTokens = static_cast<int>(std::ceil(static_cast<float>(chineseCharactersNumber) / DialogueConfig::zhBytes /
                                              DialogueConfig::zhCorrespondence));
    LOGI("中文字符Token总数 = {}", zhTokens);

    int totalTokenNum = enTokens + zhTokens;
    LOGI("Token总数 = {}", totalTokenNum);

    return {userInputLength, enTokens, zhTokens, totalTokenNum};
}

bool IntentionRecognition::isScheduleToolCall(const Json::Value &jsonMessage) {
    return jsonMessage.isMember("file_type") && jsonMessage["file_type"].asString() == "schedule";
}

Json::Value IntentionRecognition::dialogueControlMain(bool isFirstRound, const std::string &userInput) {
    std::string userText;
    std::string userFilePath;   // 文档路径
    std::string imageFilePath;  // 图片路径
    dialogueStop_ = false;

    // TODO 需要打开注释
    Json::Value jsonUserInput = util::stringToJson(userInput);
    if (not jsonUserInput.isMember("content")) {
        callOutputCallback(
            DialogueCallback::ChatErrorOccurred,
            assistant::message::createErrorMessageResult(int(assistant::ErrorCode::InputTextInvaild), "输入参数异常"));
    }
    int toolId{-1};
    for (const auto &item : jsonUserInput["content"]) {
        if (item.isMember("text")) {
            userText = item["text"].asString();
        }
        if (item.isMember("document_url")) {
            userFilePath = item["document_url"].asString();
        }
        if (item.isMember("image_url")) {
            imageFilePath = item["image_url"].asString();
        }
        if (item.isMember("tool_id")) {
            toolId = item["tool_id"].asInt();
        }
    }

    // 系统助手工具调用
    if (assistant::message::isSourceUrlToolCall(jsonUserInput)) {
        int theToolId = assistant::message::toolCallIdFromMessage(jsonUserInput);
        callSourceUrlTool(theToolId);
        return {};
    } else if (isScheduleToolCall(jsonUserInput)) {
        // 解析日期信息
        int toolId_{0};
        std::string scheduleDate;
        if (jsonUserInput.isMember("content") && jsonUserInput["content"].isArray() &&
            jsonUserInput["content"].size() > 0) {
            for (unsigned int i = 0; i < jsonUserInput["content"].size(); i++) {
                if (jsonUserInput["content"][i].isMember("type") &&
                    jsonUserInput["content"][i]["type"].asString() == "tool_id" &&
                    jsonUserInput["content"][i].isMember("tool_id") && jsonUserInput["content"][i]["tool_id"].isInt()) {
                    toolId_ = jsonUserInput["content"][i]["tool_id"].asInt();
                } else if (jsonUserInput["content"][i].isMember("type") &&
                           jsonUserInput["content"][i]["type"].asString() == "schedule_date" &&
                           jsonUserInput["content"][i].isMember("schedule_date") &&
                           jsonUserInput["content"][i]["schedule_date"].isString()) {
                    scheduleDate = jsonUserInput["content"][i]["schedule_date"].asString();
                }
            }
        }

        if (toolId_ == 21 || toolId_ == 22) {
            if (!scheduleDate.empty()) {
                // 调用系统打开日历超链接的接口
                systemOperationInterface_->callSystemOpenScheduleInterface(scheduleDate);
                return {};
            }
            return {};
        }
    }

    std::string userInputTemp;
    userInputTemp += fmt::format("用户输入文本：{}\n", userText);
    userInputTemp += fmt::format("用户文件路径：{}\n", userFilePath);
    userInputTemp += fmt::format("用户图片路径：{}", imageFilePath);
    LOGI("{}", userInputTemp);

    // 当前轮次槽位、系统动作、用户动作
    DialogueData::DialogueState currentState;
    // 当前轮次槽位信息
    std::vector<DialogueData::Slot> currentSlot;
    // 会话id
    currentDialogue.dialogue_id = dialogueId;

    Json::Value dialogueResponse;
    DialogueData::CallSuccessIdentifier processResult;

    LOGI("currentDialogue.dialogue_id = {}", currentDialogue.dialogue_id);

    if (isFirstRound) {
        // 初始化当前轮次对话变量
        currentTurn.turn_num = currentDialogue.dialogue.size();  // 当前会话轮数，和索引保持一致
        currentTurn.first_turn_flag = isFirstRound;
        currentTurn.role = "user/sys";
        currentTurn.user_input = userText;

        LOGI("第一轮: \n{}", currentTurn.toString());

    } else {
        // 加载 currentDialogue.dialogue_id的历史会话数据
        // 当前会话轮数，和索引保持一致
        currentTurn.turn_num = currentDialogue.dialogue.size();
        currentTurn.first_turn_flag = isFirstRound;
        currentTurn.role = "user";
        currentTurn.user_input = userText;

        LOGI("非第一轮: \n{}", currentTurn.toString());
    }

    // 多模态第一轮处理逻辑，拖入图片文件
    if (!imageFilePath.empty() && toolId == -1) {
        // 传入图片
        LOGD("roundOneMultiModalInputRecognition");
        userText = "用户输入了一张图片";
        currentTurn.image_file_path = imageFilePath;
        imageFilePath_ = imageFilePath;
        dialogueResponse = roundOneMultiModalInputRecognition(userText, currentSlot, currentState);

        return dialogueResponse;
    }

    // 用户输入为文本或者图像指令名称的场景--多模态输入识别的第二轮逻辑
    // TODO toolMap应该从配置文件读取
    std::map<int, std::string> toolMap{{1, "扫描黑白件"}, {2, "智能抠图"}};
    if (toolMap.find(toolId) != toolMap.end()) {
        userText = toolMap[toolId];
    }
    DialogueData::APINameSearchResult imageSearchResult =
        TypeTrans::findAPIName(userText, functionInterfaceName_, "图片");

    if (currentTurn.turn_num > 0 && imageSearchResult.found) {
        // 存在图片类指令名称的输入
        LOGI("imageSearchResult.found = {}", imageSearchResult.found);
        if (!imageFilePath.empty()) {
            LOGI("多模态输入识别第二轮");
            LOGI("beforeTurnImagePath = {}", imageFilePath);

            dialogueResponse = roundTwoMultiModalInputRecognition(userText, imageFilePath, imageSearchResult,
                                                                  currentSlot, currentState);

            return dialogueResponse;
        }
    }

    if (isTextToImageCommand(userText)) {
        // 包含文生图关键词，进入文生图
        processResult = callImageHandlerInterface(userText);

        std::string modelName = intentModelName();

        if (!processResult.successRes) {
            // 接口出错
            processResult.responseContent = "哎呀，不好意思，多模态输入识别场景文生图出现了小状况~";
        } else {
            // 调用文生图callback
            // 追加callback中生成的图片内容
            processResult.responseContent = "以下是精心为您创作生成的图片，敬请查收与欣赏。 ";
            processResult.additionalContent = imageData_;
        }

        dialogueResponse = TypeTrans::callSuccessIdentifierToJson(processResult);

        return dialogueResponse;
    }

    currentTurn.user_file_path = userFilePath;

    // 计算token数量
    DialogueData::TokenCount userTextLength = calculateTokenNumber(userText);
    currentTurn.query_token_number = userTextLength.totalTokens;

    // 判断使用系统知识库还是自定义知识库
    LOGI(
        "resetForPromptId = {}, instructionName_ = {}, "
        "promptId_ = {}",
        resetForPromptId, instructionName_, promptId_);

    if (promptId_ == systemAssistantPromptId) {
        // 用户选择系统问答指令的情况
        if (!userFilePath.empty()) {
            isUseCustomizeKB = true;
            LOGI("isUseCustomizeKB = {}", isUseCustomizeKB);

            // 置文档类问答的promptId_为0
            resetForPromptId = systemAssistantPromptId;
            promptId_ = defaultPromptId;
        } else {
            isUseCustomizeKB = false;
            LOGI("isUseCustomizeKB = {}", isUseCustomizeKB);
            if (resetForPromptId != 0) {
                resetForPromptId = 0;
            }
        }
    } else if (promptId_ != systemAssistantPromptId && promptId_ != defaultPromptId && resetForPromptId != 0) {
        resetForPromptId = 0;
    }

    if (!userFilePath.empty() && promptId_ != meetingInformationExtractionPromptId &&
        promptId_ != todoListGenerationPromptId && promptId_ != weeklyReportGenerationPromptId) {  // 文档问答

        if (documentFilePath_.empty()) {
            // 初次传入文件时，置为true
            isFirstAfferentFile = true;
        }

        documentFilePath_ = userFilePath;

        // 用户未优先选择系统问答指令，仅先传入文件的情况
        if (promptId_ != systemAssistantPromptId) {
            // 不选择系统问答指令，仅传入文件
            isUseCustomizeKB = true;
            LOGI("isUseCustomizeKB = {}", isUseCustomizeKB);

        } else {
            // 选择了系统问答指令
            if (resetFlagForPromptId) {
                isUseCustomizeKB = true;
                LOGI("isUseCustomizeKB = {}", isUseCustomizeKB);

            } else {
                isUseCustomizeKB = false;
                LOGI("isUseCustomizeKB = {}", isUseCustomizeKB);
                if (resetForPromptId != 0) {
                    resetForPromptId = 0;
                }
            }
        }

        if (promptId_ != systemAssistantPromptId) {
            // 置promptId_为系统问答
            resetForPromptId = systemAssistantPromptId;
            promptId_ = defaultPromptId;
            instructionName_ = "系统问答";
            LOGI(
                "resetForPromptId = {}, instructionName_ = {}, "
                "promptId_ = {}",
                resetForPromptId, instructionName_, promptId_);
            resetFlagForPromptId = true;
        } else {
            resetForPromptId = systemAssistantPromptId;
            promptId_ = defaultPromptId;
        }
    }

    LOGI("documentFilePath_ = {}", documentFilePath_);
    LOGI("isUseCustomizeKB = {}", isUseCustomizeKB);
    LOGI(
        "resetForPromptId = {}, instructionName_ = {}, "
        "promptId_ = {}",
        resetForPromptId, instructionName_, promptId_);

    bool updateSuccess{false};
    if (userText.empty() && userFilePath.empty()) {
        // 用户输入为空
        // 纯文本类callback
        // std::string modelName = intentModelName();
        bool isError = true;
        std::string errorMessage = "user input can not be empty.";
        std::string responseMessage = "用户输入不能为空哟~ ";
        enum DialogueCallback::Type dataType = DialogueCallback::ImageErrorOccurred;

        int errorCode = int(assistant::ErrorCode::InputTextInvaild);

        textCallback(responseMessage, "", isError, errorCode, errorMessage, dataType, "");
        processResult.responseContent = responseMessage;
        dialogueResponse = TypeTrans::callSuccessIdentifierToJson(processResult);
        return dialogueResponse;
    } else if (userText.empty() && !userFilePath.empty() &&
               (promptId_ == systemAssistantPromptId || resetForPromptId == systemAssistantPromptId) &&
               promptId_ != meetingInformationExtractionPromptId && promptId_ != todoListGenerationPromptId &&
               promptId_ != weeklyReportGenerationPromptId) {
        std::string modelName = intentModelName();
        bool isError = false;
        int errorCode = 0;
        std::string errorMessage;
        std::string responseMessage = "收到您的文档啦~需要我对这份文档做什么呢?";
        enum DialogueCallback::Type dataType = DialogueCallback::IntentNormal;

        // // 更新对话状态
        currentTurn.intent_category = modelName;
        currentTurn.turn_keywords = {modelName};  // 当前轮次关键词
        currentState.slots = {};                  // 更新对话状态

        currentState.system_acts = {"对话生成"};        // 系统动作
        currentState.user_acts = {"请求"};              // 用户动作
        currentTurn.system_response = responseMessage;  // 系统响应

        currentTurn.dialogue_state = currentState;
        currentDialogue.dialogue.push_back(currentTurn);

        // 创建自定义数据库
        createCustomizeDataBase(userFilePath, updateSuccess);
        if (updateSuccess) {
            // callback
            textCallback(responseMessage, "", isError, errorCode, errorMessage, dataType, "");
        }
        processResult.responseContent = responseMessage;
        dialogueResponse = TypeTrans::callSuccessIdentifierToJson(processResult);
        return dialogueResponse;
    } else if (!userText.empty() && !documentFilePath_.empty() &&
               (promptId_ == systemAssistantPromptId || resetForPromptId == systemAssistantPromptId) &&
               promptId_ != meetingInformationExtractionPromptId && promptId_ != todoListGenerationPromptId &&
               promptId_ != weeklyReportGenerationPromptId) {
        // 创建自定义数据库
        createCustomizeDataBase(userFilePath, updateSuccess);
    }

    LOGD("{}", instructionName_);
    if (promptPassed and promptId_ != systemAssistantPromptId and promptId_ != defaultPromptId and
        promptId_ != int(config::PromptTemplate::PromptId::IntentionRecognition)) {
        LOGI("文本类指令场景");

        // // 文本类指令场景
        dialogueResponse = textInstructionScenario(userText, isFirstRound, userFilePath);
        return dialogueResponse;
    }

    // 系统控制场景（意图识别）
    if (promptId_ == int(config::PromptTemplate::PromptId::IntentionRecognition)) {
        dialogueResponse = intentRecognitionModelProcessingScenarios(userText, isFirstRound);
        return dialogueResponse;
    }

    // RAG场景（系统助手、文档问答）
    if ((promptId_ == systemAssistantPromptId || resetForPromptId == systemAssistantPromptId) &&
        promptId_ != meetingInformationExtractionPromptId && promptId_ != todoListGenerationPromptId &&
        promptId_ != weeklyReportGenerationPromptId) {
        LOGI("RAG场景（系统助手、文档问答）");
        dialogueResponse = ragProcessingScenario(userText, userFilePath, isFirstRound, updateSuccess);
        printDialogueContent(currentDialogue);
        return dialogueResponse;
    }

    // 未选择任何指令场景，直接进入闲聊模式
    std::string verbalTricksStr = "进入闲聊/知识问答场景.";
    LOGD("{}", verbalTricksStr);

    // 生成模型输入
    bool isRagInstruct = false;
    std::string instructName = "其他_闲聊";
    getInstructionCompleteSession(isFirstRound, userText, instructName, isRagInstruct, false);

    LOGI("ModelInput for chatting and qa = {}", instructModelInput);

    // 调用端模型（默认模型）进行处理并返回结果,
    // 使用默认提示词，不用传入
    bool success = callChatModelInferenceInterface(instructModelInput).get();

    // 追加callback中生成的对话内容
    if (success) {
        processResult.responseContent = chatGenCompleteText_;
    }

    // // 更新对话状态
    currentTurn.intent_category = "其他_闲聊";
    currentTurn.turn_keywords = {"其他_闲聊"};  // 当前轮次关键词
    currentState.slots = {};                    // 更新对话状态

    currentState.system_acts = {"对话生成"};                      // 系统动作
    currentState.user_acts = {"请求"};                            // 用户动作
    currentTurn.system_response = processResult.responseContent;  // 系统响应
    DialogueData::TokenCount responseTokenNumber = calculateTokenNumber(processResult.responseContent);
    currentTurn.response_token_number = responseTokenNumber.totalTokens;
    currentTurn.dialogue_state = currentState;
    currentDialogue.dialogue.push_back(currentTurn);

    dialogueResponse = TypeTrans::callSuccessIdentifierToJson(processResult);

    return dialogueResponse;
}
