# 模块交互流程提示词

> 本文档描述RMDC系统各模块之间的交互流程，帮助大模型理解数据流和调用关系。

---

## 模块调用关系

```mermaid
graph LR
    FE[Vue3 Frontend] --> Core[rmdc-core]
    Core --> Jenkins[jenkins-branch-dac]
    Core --> Project[project-management]
    Core --> Audit[audit-log]
    Core --> UserAuth[user-auth]
    Core --> ExHub[exchange-hub]
    
    ExHub <--> MQTT[(MQTT)]
    MQTT <--> WD[watchdog]
    WD <--> Node[watchdog-node]
    WD <--> Agent[watchdog-agent]
```

---

## 核心交互流程

### 1. 用户请求处理

```mermaid
sequenceDiagram
    participant FE as 前端
    participant Core as rmdc-core
    participant Auth as AuthMiddleware
    participant Perm as PermMiddleware
    participant Biz as 业务模块
    
    FE->>Core: HTTP Request (JWT)
    Core->>Auth: 验证Token
    Auth->>Perm: 权限校验
    Perm->>Biz: 处理业务
    Biz-->>FE: HTTP Response
```

### 2. Jenkins构建触发

```mermaid
sequenceDiagram
    participant User as 用户
    participant Handler as BuildHandler
    participant Service as BuildService
    participant Jenkins as Jenkins API
    participant Tracker as BuildTracker
    
    User->>Handler: POST /builds/trigger
    Handler->>Service: TriggerBuild()
    Service->>Jenkins: 触发构建
    Service->>Tracker: TrackBuild()
    Service-->>User: 返回构建号
```

### 3. 项目注册授权

```mermaid
sequenceDiagram
    participant PM as project-management
    participant ExHub as exchange-hub
    participant MQTT as MQTT Broker
    participant WD as watchdog
    
    Note over PM: 创建项目，生成密钥
    WD->>MQTT: 发送注册请求(TOTP)
    MQTT->>ExHub: 转发
    ExHub->>PM: 验证项目
    PM-->>ExHub: 返回结果
    ExHub->>MQTT: 发送挑战码
    MQTT->>WD: 推送
    WD->>MQTT: 回复挑战
    ExHub->>ExHub: 验证成功，项目上线
```

### 4. K8S指令执行

```mermaid
sequenceDiagram
    participant User as 用户
    participant API as rmdc-core
    participant ExHub as exchange-hub
    participant MQTT as MQTT
    participant WD as watchdog
    participant K8S as K8S API
    
    User->>API: 发起K8S操作
    API->>ExHub: 发送指令
    ExHub->>MQTT: 发布command
    MQTT->>WD: 推送指令
    WD->>K8S: 执行操作
    K8S-->>WD: 返回结果
    WD->>MQTT: 发布result
    MQTT->>ExHub: 接收结果
    ExHub->>API: 返回结果
    API->>User: 展示结果
```

---

## 数据流向

### 下行数据流

```
用户操作 → rmdc-core → 业务模块 → exchange-hub → MQTT → watchdog → 执行目标
```

### 上行数据流

```
执行目标 → watchdog → MQTT → exchange-hub → 业务模块 → 持久化/展示
```

---

## 接口契约

### MQTT消息格式

```json
{
    "message_id": "uuid",
    "type": "command|message",
    "project_id": "namespace_xxx",
    "command_type": "k8s_exec|host_exec|...",
    "timestamp": 1704501234567,
    "payload": {...},
    "signature": "hmac-sha256"
}
```

### 执行结果格式

```json
{
    "command_id": "原指令ID",
    "status": "success|failure|timeout",
    "exit_code": 0,
    "output": "执行输出",
    "error": "",
    "duration": 1111
}
```
