From 2ef78226aced4353660a74260c809550a9c26761 Mon Sep 17 00:00:00 2001 From: nekosu Date: Thu, 6 Mar 2025 12:05:26 +0800 Subject: [PATCH] feat(node): update --- .../NodeJS/release/maa-node/src/client.ts | 12 +++++- .../NodeJS/release/maa-node/src/maa.d.ts | 8 ++-- .../NodeJS/release/maa-node/src/server.ts | 4 +- source/binding/NodeJS/src/agent.cpp | 38 ++++++++++--------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/source/binding/NodeJS/release/maa-node/src/client.ts b/source/binding/NodeJS/release/maa-node/src/client.ts index d657b27f6..77f996eee 100644 --- a/source/binding/NodeJS/release/maa-node/src/client.ts +++ b/source/binding/NodeJS/release/maa-node/src/client.ts @@ -22,9 +22,17 @@ export class AgentClient { } } - start_child(exec: string, args: string[]) { - if (!maa.agent_client_start_child(this.handle, exec, args)) { + start_child(identifier: string | null) { + const rid = maa.agent_client_start_child(this.handle, identifier) + if (rid === null) { throw 'AgentClient start child failed' } + return rid + } + + connect() { + if (!maa.agent_client_connect(this.handle)) { + throw 'AgentClient connect failed' + } } } diff --git a/source/binding/NodeJS/release/maa-node/src/maa.d.ts b/source/binding/NodeJS/release/maa-node/src/maa.d.ts index 012e4973e..6f62af847 100644 --- a/source/binding/NodeJS/release/maa-node/src/maa.d.ts +++ b/source/binding/NodeJS/release/maa-node/src/maa.d.ts @@ -385,9 +385,9 @@ export declare function agent_client_bind_resource( ): boolean export declare function agent_client_start_child( handle: AgentClientHandle, - child_exec: string, - child_args: string[] -): boolean + identifier: string | null +): string | null +export declare function agent_client_connect(handle: AgentClientHandle): boolean // agent.cpp - server @@ -399,7 +399,7 @@ export declare function agent_server_register_custom_action( name: string, action: CustomActionCallback ): boolean -export declare function agent_server_start_up(args: string[]): boolean +export declare function agent_server_start_up(identifier: string): boolean export declare function agent_server_shut_down(): void // export declare function agent_server_join(): void // export declare function agent_server_detach(): void diff --git a/source/binding/NodeJS/release/maa-node/src/server.ts b/source/binding/NodeJS/release/maa-node/src/server.ts index 93f9b9054..2100b82ed 100644 --- a/source/binding/NodeJS/release/maa-node/src/server.ts +++ b/source/binding/NodeJS/release/maa-node/src/server.ts @@ -44,8 +44,8 @@ export const AgentServer = { ) }, - start_up(args: string[]) { - if (!maa.agent_server_start_up(args)) { + start_up(identifier: string) { + if (!maa.agent_server_start_up(identifier)) { throw 'AgentServer start up failed' } }, diff --git a/source/binding/NodeJS/src/agent.cpp b/source/binding/NodeJS/src/agent.cpp index 181c469d9..b2647b020 100644 --- a/source/binding/NodeJS/src/agent.cpp +++ b/source/binding/NodeJS/src/agent.cpp @@ -34,15 +34,9 @@ bool agent_server_register_custom_action(Napi::Env env, ExtContextInfo* context, } } -bool agent_server_start_up(std::vector args) +bool agent_server_start_up(std::string identifier) { - StringListBuffer buffer; - buffer.set_vector(args, [](auto str) { - StringBuffer buf; - buf.set(str); - return buf; - }); - return MaaAgentServerStartUp(buffer); + return MaaAgentServerStartUp(identifier.c_str()); } void agent_server_shut_down() @@ -93,16 +87,25 @@ bool agent_client_bind_resource(Napi::External info, Napi::Exte } } -bool agent_client_start_child(Napi::External info, std::string child_exec, std::vector child_args) +std::optional agent_client_create_socket(Napi::External info, std::optional identifier) { - StringListBuffer buffer; - buffer.set_vector(child_args, [](auto str) { - StringBuffer buf; - buf.set(str); - return buf; - }); - return MaaAgentClientStartChild(info.Data()->handle, child_exec.c_str(), buffer); + StringBuffer buf; + if (identifier) { + buf.set(identifier.value()); + } + if (MaaAgentClientCreateSocket(info.Data()->handle, buf.buffer)) { + return buf.str(); + } + else { + return std::nullopt; + } } + +bool agent_client_connect(Napi::External info) +{ + return MaaAgentClientConnect(info.Data()->handle); +} + #endif void load_agent(Napi::Env env, Napi::Object& exports, Napi::External context) @@ -120,7 +123,8 @@ void load_agent(Napi::Env env, Napi::Object& exports, Napi::External