Skip to content

Commit

Permalink
feat: 初步实现 RemoteContext
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 27, 2025
1 parent cfd165a commit ec33716
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 19 deletions.
86 changes: 80 additions & 6 deletions source/MaaAgentServer/RemoteInstance/RemoteContext.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "RemoteContext.h"

#include "MaaAgent/Message.hpp"
#include "Utils/Codec.h"

MAA_AGENT_SERVER_NS_BEGIN

Expand All @@ -27,7 +28,18 @@ MaaTaskId RemoteContext::run_task(const std::string& entry, const json::object&

MaaRecoId RemoteContext::run_recognition(const std::string& entry, const json::object& pipeline_override, const cv::Mat& image)
{
return MaaRecoId();
ContextRunRecognitionReverseRequest req {
.context_id = context_id_,
.entry = entry,
.pipeline_override = pipeline_override,
.image = encode_image(image)
};

auto resp_opt = server_.send_and_recv<ContextRunRecognitionReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->reco_id;
}

MaaNodeId RemoteContext::run_action(
Expand All @@ -36,31 +48,93 @@ MaaNodeId RemoteContext::run_action(
const cv::Rect& box,
const std::string& reco_detail)
{
return MaaNodeId();
ContextRunActionReverseRequest req {
.context_id = context_id_,
.entry = entry,
.pipeline_override = pipeline_override,
.box = {box.x, box.y, box.width, box.height},
.reco_detail = reco_detail
};

auto resp_opt = server_.send_and_recv<ContextRunActionReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->node_id;
}

bool RemoteContext::override_pipeline(const json::object& pipeline_override)
{
return false;
ContextOverridePipelineReverseRequest req {
.context_id = context_id_,
.pipeline_override = pipeline_override,
};

auto resp_opt = server_.send_and_recv<ContextOverridePipelineReverseResponse>(req);
if (!resp_opt) {
return false;
}
return resp_opt->ret;
}

bool RemoteContext::override_next(const std::string& node_name, const std::vector<std::string>& next)
{
return false;
ContextOverrideNextReverseRequest req {
.context_id = context_id_,
.node_name = node_name,
.next = next,
};

auto resp_opt = server_.send_and_recv<ContextOverrideNextReverseResponse>(req);
if (!resp_opt) {
return false;
}
return resp_opt->ret;
}

MaaContext* RemoteContext::clone() const
{
ContextCloneReverseRequest req {
.context_id = context_id_,
};

auto resp_opt = server_.send_and_recv<ContextCloneReverseResponse>(req);
if (!resp_opt) {
return nullptr;
}
//TODO
//std::string cloned_id = resp_opt->cloned_context_id;

return nullptr;
}

MaaTaskId RemoteContext::task_id() const
{
return MaaTaskId();
ContextTaskIdReverseRequest req {
.context_id = context_id_,
};

auto resp_opt = server_.send_and_recv<ContextTaskIdReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}

return resp_opt->task_id;
}

MaaTasker* RemoteContext::tasker() const
{
{
ContextTaskerReverseRequest req {
.context_id = context_id_,
};

auto resp_opt = server_.send_and_recv<ContextTaskerReverseResponse>(req);
if (!resp_opt) {
return nullptr;
}
//TODO
//std::string tasker_id = resp_opt->tasker_id;

return nullptr;
}

Expand Down
156 changes: 143 additions & 13 deletions source/include/MaaAgent/Message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ MAA_AGENT_NS_BEGIN
// Request: client -> server
// ReverseRequest: server -> client

using MessageTypePlaceholder = int;

struct StartUpResponse
{
std::string version;
std::vector<std::string> actions;
std::vector<std::string> recognitions;

MEO_JSONIZATION(version, actions, recognitions);
MessageTypePlaceholder _StartUpResponse = 1;
MEO_JSONIZATION(version, actions, recognitions, _StartUpResponse);
};

struct ShutDownRequest
{
int shut_down_request_placeholder = 0;

MEO_JSONIZATION(shut_down_request_placeholder);
MessageTypePlaceholder _ShutDownRequest = 1;
MEO_JSONIZATION(_ShutDownRequest);
};

struct ShutDownResponse
{
int shut_down_response_placeholder = 0;

MEO_JSONIZATION(shut_down_response_placeholder);
MessageTypePlaceholder _ShutDownResponse = 1;
MEO_JSONIZATION(_ShutDownResponse);
};

struct CustomRecognitionRequest
Expand All @@ -46,7 +47,8 @@ struct CustomRecognitionRequest
std::string image;
std::array<int32_t, 4> roi {};

MEO_JSONIZATION(context_id, task_id, node_name, custom_recognition_name, custom_recognition_param, image, roi);
MessageTypePlaceholder _CustomRecognitionRequest = 1;
MEO_JSONIZATION(context_id, task_id, node_name, custom_recognition_name, custom_recognition_param, image, roi, _CustomRecognitionRequest);
};

struct CustomRecognitionResponse
Expand All @@ -55,7 +57,8 @@ struct CustomRecognitionResponse
std::array<int32_t, 4> out_box {};
std::string out_detail;

MEO_JSONIZATION(ret, out_box, out_detail);
MessageTypePlaceholder _CustomRecognitionResponse = 1;
MEO_JSONIZATION(ret, out_box, out_detail, _CustomRecognitionResponse);
};

struct CustomActionRequest
Expand All @@ -68,14 +71,16 @@ struct CustomActionRequest
int64_t reco_id = 0;
std::array<int32_t, 4> box {};

MEO_JSONIZATION(context_id, task_id, node_name, custom_action_name, custom_action_param, reco_id, box);
MessageTypePlaceholder _CustomActionRequest = 1;
MEO_JSONIZATION(context_id, task_id, node_name, custom_action_name, custom_action_param, reco_id, box, _CustomActionRequest);
};

struct CustomActionResponse
{
bool ret = false;

MEO_JSONIZATION(ret);
MessageTypePlaceholder _CustomActionResponse = 1;
MEO_JSONIZATION(ret, _CustomActionResponse);
};

struct ContextRunTaskReverseRequest
Expand All @@ -84,14 +89,139 @@ struct ContextRunTaskReverseRequest
std::string entry;
json::object pipeline_override;

MEO_JSONIZATION(context_id, entry, pipeline_override);
MessageTypePlaceholder _ContextRunTaskReverseRequest = 1;
MEO_JSONIZATION(context_id, entry, pipeline_override, _ContextRunTaskReverseRequest);
};

struct ContextRunTaskReverseResponse
{
int64_t task_id = 0;

MEO_JSONIZATION(task_id);

MessageTypePlaceholder _ContextRunTaskReverseResponse = 1;
MEO_JSONIZATION(task_id, _ContextRunTaskReverseResponse);
};

struct ContextRunRecognitionReverseRequest
{
std::string context_id;
std::string entry;
json::object pipeline_override;
std::string image;

MessageTypePlaceholder _ContextRunRecognitionReverseRequest = 1;
MEO_JSONIZATION(context_id, entry, pipeline_override, image, _ContextRunRecognitionReverseRequest);
};

struct ContextRunRecognitionReverseResponse
{
int64_t reco_id = 0;

MessageTypePlaceholder _ContextRunRecognitionReverseResponse = 1;
MEO_JSONIZATION(reco_id, _ContextRunRecognitionReverseResponse);
};

struct ContextRunActionReverseRequest
{
std::string context_id;
std::string entry;
json::object pipeline_override;
std::array<int, 4> box {};
std::string reco_detail;

MessageTypePlaceholder _ContextRunActionReverseRequest = 1;
MEO_JSONIZATION(context_id, entry, pipeline_override, box, reco_detail, _ContextRunActionReverseRequest);
};

struct ContextRunActionReverseResponse
{
int64_t node_id = 0;

MessageTypePlaceholder _ContextRunActionReverseResponse = 1;
MEO_JSONIZATION(node_id, _ContextRunActionReverseResponse);
};

struct ContextOverridePipelineReverseRequest
{
std::string context_id;
json::object pipeline_override;

MessageTypePlaceholder _ContextOverridePipelineReverseRequest = 1;
MEO_JSONIZATION(context_id, pipeline_override, _ContextOverridePipelineReverseRequest);
};

struct ContextOverridePipelineReverseResponse
{
bool ret = false;

MessageTypePlaceholder _ContextOverridePipelineReverseResponse = 1;
MEO_JSONIZATION(ret, _ContextOverridePipelineReverseResponse);
};

struct ContextOverrideNextReverseRequest
{
std::string context_id;
std::string node_name;
std::vector<std::string> next;

MessageTypePlaceholder _ContextOverrideNextReverseRequest = 1;
MEO_JSONIZATION(context_id, node_name, next, _ContextOverrideNextReverseRequest);
};

struct ContextOverrideNextReverseResponse
{
bool ret = false;

MessageTypePlaceholder _ContextOverrideNextReverseResponse = 1;
MEO_JSONIZATION(ret, _ContextOverrideNextReverseResponse);
};

struct ContextCloneReverseRequest
{
std::string context_id;

MessageTypePlaceholder _ContextClone = 1;
MEO_JSONIZATION(context_id, _ContextClone);
};

struct ContextCloneReverseResponse
{
std::string cloned_context_id;

MessageTypePlaceholder _ContextCloneReverseResponse = 1;
MEO_JSONIZATION(cloned_context_id, _ContextCloneReverseResponse);
};

struct ContextTaskIdReverseRequest
{
std::string context_id;

MessageTypePlaceholder _ContextTaskIdReverseRequest = 1;
MEO_JSONIZATION(context_id, _ContextTaskIdReverseRequest);
};

struct ContextTaskIdReverseResponse
{
int64_t task_id = 0;

MessageTypePlaceholder _ContextTaskIdReverseResponse = 1;
MEO_JSONIZATION(task_id, _ContextTaskIdReverseResponse);
};

struct ContextTaskerReverseRequest
{
std::string context_id;

MessageTypePlaceholder _ContextTaskerReverseRequest = 1;
MEO_JSONIZATION(context_id, _ContextTaskerReverseRequest);
};

struct ContextTaskerReverseResponse
{
std::string tasker_id;

MessageTypePlaceholder _ContextTaskerReverseResponse = 1;
MEO_JSONIZATION(tasker_id, _ContextTaskerReverseResponse);
};

MAA_AGENT_NS_END

0 comments on commit ec33716

Please sign in to comment.