MaaAssistantArknights / MaaAssistantArknights/maa-cli

RFC: `maa-cli` 前后端分离

Open
#322 23 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
Rust
Stars
117
Forks
39
PR merge metrics
No merged PRs in 30d

Description

## 现状

当前,maa-cli 执行任务主要包括以下步骤:

1. 解析 MaaCore 相关配置 (profile/*.toml);
2. 解析任务:对于自定义任务读取任务定义文件,对于预定义任务处理命令行参数;
3. 根据任务修改部分配置;
4. 加载 MaaCore 并根据配置进行初始化;
5. 向 MaaCore 添加解析任务,并启动;
6. 等待任务完成并推出程序。

## 问题

上述实现比较简单直接,但是存在以下几个问题:

- 每次执行任务都需要加载并配置 MaaCore,这需要耗费一定时间(约几秒)。这在执行比较长的任务可以忽略,但是对于执行简单的任务,尤其是通过命令行连续执行多个任务时,这个开销是不能忽略。
- 目前 maa-cli 启动后没有任何外部干预途径。MaaCore 部分选项允许运行时更改,而 maa-cli 当前实现方式无法做到。
- 未来 maa-cli 可以作为一个后端,给供其他前端调用。这样其他前端就可以避免相对复杂的 MaaCore FFI,同时也可以基于此开发 WebUI。

## 解决办法

为 maa-cli 引入一个 Server 模式,通过 `maa serve` 启动。启动后 `maa-cli` 将作为一个 RPC Server, 并监听某一个 Unix 或者 TCP Socket。`maa run`、`maa startup` 等命令将解析任务后向服务器发送请求。maa-cli 在运行任务时如果 server 未启动,将自行启动一个 server,并将其作为守护程序在后台活跃一段时间,然后通过相应的

## 具体实施

- RPC 框架:JSON-RPC (crate jsonrpsee)。简单,易于调试,浏览器原生支持,易于和已有代码集成。RPC 协议相关的结构体,通过一个单独的 Crate 实现。
- 传输:WebSocket or WS+TLS。WS 相比 HTTP 更高效,全双工,同时和 HTTP 一样受到浏览器原生支持。

## RPC API 列表

### 添加任务

**方法名称**: `AppendTask`

**请求**:

任务类型以及序列化为 JSON 的字符串。

```json
{
"taskType": "StartUp",
"taskParams": \"{}\",
}
```

**响应**:

```json
{
"taskId": "1"
}
```

其中 `0` 表示添加失败。

### 修改任务参数

**方法名称**: `SetParams`

**请求**:

```json
{
"taskId": "1",
"newParams": {
"client_type": "Txwy"
}
}
```

**响应**:

```json
{
"success": true
}
```

### 启动任务

**方法名称**: `Start`

**请求**:

```json
{}
```

**响应**:

```json
{
"success": true
}
```

### 停止任务

**方法名称**: `Stop`

**请求**:

```json
{}
```

**响应**:

```json
{
"success": true
}
```

### 检查任务是否正在进行

**方法名称**: `Running`

**请求**:

```json
{}
```

**响应**:

```json
{
"running": false
}
```

### 关闭 Server

**方法名称**: `Terminate`

**请求**:

```json
{}
```

**响应**:

```json
{
"success": true
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files, tests, or entry points are named. Start by tracing the current maa-cli command flow from task parsing through MaaCore initialization, then define the server and client boundaries around the listed AppendTask, SetParams, Start, Stop, Running, and Terminate methods; completion would require an agreed implementation scope and working serve-mode RPC behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api, backend-api-design, cli, networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.