静态缓存页面 · 查看动态版本 · 登录
智柴网 登录 | 注册
← 返回话题
✨步子哥 @steper · 2025-11-15 14:49

plan-act-api-service.ts 文件解读

一、文件作用

PlanActApiService 是计划(Plan)相关的 API 服务类,封装了计划模板的 CRUD、执行、版本管理和定时任务创建等操作。

二、核心功能模块

#### 1. 基础配置

``23:25:ui-vue3/src/api/plan-act-api-service.ts export class PlanActApiService { private static readonly PLAN_TEMPLATE_URL = '/api/plan-template' private static readonly CRON_TASK_URL = '/api/cron-tasks'

- 两个 API 端点:计划模板和定时任务
- 静态类设计,所有方法都是静态方法

#### 2. 计划执行模块

##### 方法:`executePlan` - 执行计划

28:61:ui-vue3/src/api/plan-act-api-service.ts // Execute generated plan using ManusController.executeByToolNameAsync public static async executePlan( planTemplateId: string, rawParam?: string, uploadedFiles?: string[], replacementParams?: Record, uploadKey?: string ): Promise { return LlmCheckService.withLlmCheck(async () => { console.log('[PlanActApiService] executePlan called with:', { planTemplateId, rawParam, uploadedFiles, replacementParams, uploadKey, })

// Add rawParam to replacementParams if provided (backend expects it in replacementParams) if (rawParam) { if (!replacementParams) { replacementParams = {} } replacementParams['userRequirement'] = rawParam console.log('[PlanActApiService] Added rawParam to replacementParams:', rawParam) }

// Use the unified DirectApiService method return await DirectApiService.executeByToolName( planTemplateId, replacementParams, uploadedFiles, uploadKey ) }) }

设计原理:
1. 参数统一化:将 `rawParam` 转换为 `replacementParams['userRequirement']`
   
typescript // 输入:rawParam = "分析日志" // 转换后:replacementParams = { userRequirement: "分析日志" }
2. 委托模式:委托给 `DirectApiService.executeByToolName`
   - 复用统一执行逻辑
   - 保持 API 层职责清晰
3. LLM 检查:通过 `withLlmCheck` 确保 LLM 已配置

参数说明:
- `planTemplateId`: 计划模板 ID(作为工具名)
- `rawParam`: 原始参数(转换为 `userRequirement`)
- `uploadedFiles`: 上传的文件列表
- `replacementParams`: 替换参数对象
- `uploadKey`: 上传键

#### 3. 计划模板管理模块

##### 方法:`savePlanTemplate` - 保存计划模板

64:72:ui-vue3/src/api/plan-act-api-service.ts // Save plan to server public static async savePlanTemplate(planId: string, planJson: string): Promise { const response = await fetch(
${this.PLAN_TEMPLATE_URL}/save, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ planId, planJson }), }) if (!response.ok) throw new Error(Failed to save plan: ${response.status}) return await response.json() }
作用:保存计划模板到服务器

##### 方法:`getAllPlanTemplates` - 获取所有计划模板

96:101:ui-vue3/src/api/plan-act-api-service.ts // Get all plan template list public static async getAllPlanTemplates(): Promise { const response = await fetch(
${this.PLAN_TEMPLATE_URL}/list) if (!response.ok) throw new Error(Failed to get plan template list: ${response.status}) return await response.json() }
使用场景:
- 侧边栏加载计划模板列表
- 显示所有可用的计划模板

##### 方法:`deletePlanTemplate` - 删除计划模板

103:112:ui-vue3/src/api/plan-act-api-service.ts // Delete plan template public static async deletePlanTemplate(planId: string): Promise { const response = await fetch(
${this.PLAN_TEMPLATE_URL}/delete, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ planId }), }) if (!response.ok) throw new Error(Failed to delete plan template: ${response.status}) return await response.json() }
#### 4. 版本管理模块

##### 方法:`getPlanVersions` - 获取所有版本

74:83:ui-vue3/src/api/plan-act-api-service.ts // Get all versions of plan public static async getPlanVersions(planId: string): Promise { const response = await fetch(
${this.PLAN_TEMPLATE_URL}/versions, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ planId }), }) if (!response.ok) throw new Error(Failed to get plan versions: ${response.status}) return await response.json() }
作用:获取计划模板的所有历史版本

##### 方法:`getVersionPlan` - 获取特定版本

85:94:ui-vue3/src/api/plan-act-api-service.ts // Get specific version of plan public static async getVersionPlan(planId: string, versionIndex: number): Promise { const response = await fetch(
${this.PLAN_TEMPLATE_URL}/get-version, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ planId, versionIndex: versionIndex.toString() }), }) if (!response.ok) throw new Error(Failed to get specific version plan: ${response.status}) return await response.json() }
设计细节:
- `versionIndex` 转换为字符串发送(后端要求)

#### 5. 定时任务模块

##### 方法:`createCronTask` - 创建定时任务

114:130:ui-vue3/src/api/plan-act-api-service.ts // Create cron task public static async createCronTask(cronConfig: CronConfig): Promise { const response = await fetch(this.CRON_TASK_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(cronConfig), }) if (!response.ok) { try { const errorData = await response.json() throw new Error(errorData.message ||
Failed to create cron task: ${response.status}) } catch { throw new Error(Failed to create cron task: ${response.status}) } } return await response.json() }
特点:
- 错误处理:尝试解析错误信息,失败时使用默认消息
- 类型安全:使用 `CronConfig` 类型

### 三、设计模式分析

#### 1. 适配器模式(Adapter Pattern)

`executePlan` 方法作为适配器,将不同的参数格式统一:

typescript // 前端调用方式 executePlan(planId, rawParam, files, params, uploadKey)

// 转换为统一格式 DirectApiService.executeByToolName(planId, { userRequirement: rawParam, ...params }, files, uploadKey)

#### 2. 委托模式(Delegation Pattern)

执行逻辑委托给 `DirectApiService`:

typescript // PlanActApiService 不直接调用后端 // 而是委托给 DirectApiService return await DirectApiService.executeByToolName(...)
优势:
- 代码复用
- 统一执行逻辑
- 易于维护

#### 3. 门面模式(Facade Pattern)

`PlanActApiService` 作为门面,简化复杂的计划操作:

typescript // 用户只需要调用简单的方法 PlanActApiService.executePlan(id, param) PlanActApiService.savePlanTemplate(id, json) PlanActApiService.getAllPlanTemplates()
### 四、数据流分析

#### 执行计划的数据流

用户操作 ↓ PlanActApiService.executePlan() ↓ 参数转换:rawParam → replacementParams['userRequirement'] ↓ LlmCheckService.withLlmCheck() [检查 LLM 配置] ↓ DirectApiService.executeByToolName() ↓ 构建请求体 ↓ POST /api/executor/executeByToolNameAsync ↓ 后端执行计划 ↓ 返回执行结果
#### 版本管理的数据流

用户查看版本历史 ↓ PlanActApiService.getPlanVersions(planId) ↓ POST /api/plan-template/versions ↓ 后端返回版本列表 ↓ 用户选择特定版本 ↓ PlanActApiService.getVersionPlan(planId, versionIndex) ↓ POST /api/plan-template/get-version ↓ 后端返回版本内容
### 五、API 端点总结

| 方法 | 端点 | 方法 | 说明 |
|------|------|------|------|
| `executePlan` | `/api/executor/executeByToolNameAsync` | POST | 执行计划(通过 DirectApiService) |
| `savePlanTemplate` | `/api/plan-template/save` | POST | 保存计划模板 |
| `getAllPlanTemplates` | `/api/plan-template/list` | GET | 获取所有计划模板 |
| `deletePlanTemplate` | `/api/plan-template/delete` | POST | 删除计划模板 |
| `getPlanVersions` | `/api/plan-template/versions` | POST | 获取所有版本 |
| `getVersionPlan` | `/api/plan-template/get-version` | POST | 获取特定版本 |
| `createCronTask` | `/api/cron-tasks` | POST | 创建定时任务 |

### 六、使用场景

#### 场景 1:侧边栏加载计划列表

typescript // stores/sidebar.ts const response = await PlanActApiService.getAllPlanTemplates() this.planTemplateList = response.templates
#### 场景 2:执行计划

typescript // 执行计划模板 await PlanActApiService.executePlan( templateId, userInput, // rawParam uploadedFiles, replacementParams, uploadKey )
#### 场景 3:保存计划模板

typescript // 保存编辑后的计划 await PlanActApiService.savePlanTemplate(planId, planJson)
#### 场景 4:版本管理

typescript // 获取所有版本 const versions = await PlanActApiService.getPlanVersions(planId)

// 获取特定版本 const version = await PlanActApiService.getVersionPlan(planId, index) `

七、设计优势

1. 职责清晰:计划相关操作集中管理 2. 代码复用:执行逻辑委托给 DirectApiService 3. 类型安全:使用 TypeScript 类型定义 4. 错误处理:统一的错误处理机制 5. 易于扩展:新增功能只需添加方法

八、潜在改进点

1. 统一错误处理:可以封装统一的错误处理函数 2. 类型定义:返回类型可以使用更具体的接口 3. 日志记录:可以统一日志格式

九、总结

PlanActApiService` 是计划管理的核心服务类,提供:

  • 计划执行:通过委托模式复用执行逻辑
  • 计划模板管理:CRUD 操作
  • 版本管理:历史版本查询
  • 定时任务:创建定时任务
设计特点:
  • 适配器模式:统一参数格式
  • 委托模式:复用执行逻辑
  • 门面模式:简化复杂操作
  • LLM 检查:自动检查配置
该服务是计划管理功能的前端 API 封装层,提供了完整的计划生命周期管理能力。

暂无表态