Skip to main content

mofa_foundation/secretary/default/
types.rs

1//! 默认类型定义
2//!
3//! 包含默认秘书实现使用的所有类型。
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8// =============================================================================
9// Todo 任务类型
10// =============================================================================
11
12/// Todo 任务状态
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub enum TodoStatus {
15    /// 待处理
16    Pending,
17    /// 需求澄清中
18    Clarifying,
19    /// 执行中
20    InProgress,
21    /// 等待反馈
22    WaitingFeedback,
23    /// 已完成
24    Completed,
25    /// 已取消
26    Cancelled,
27    /// 阻塞中(需要人类决策)
28    Blocked(String),
29}
30
31/// Todo 任务优先级
32#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
33pub enum TodoPriority {
34    Low = 0,
35    Medium = 1,
36    High = 2,
37    Urgent = 3,
38}
39
40/// Todo 任务项
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct TodoItem {
43    /// 任务ID
44    pub id: String,
45    /// 原始想法/需求描述
46    pub raw_idea: String,
47    /// 澄清后的需求文档
48    pub clarified_requirement: Option<ProjectRequirement>,
49    /// 任务状态
50    pub status: TodoStatus,
51    /// 优先级
52    pub priority: TodoPriority,
53    /// 创建时间(Unix时间戳)
54    pub created_at: u64,
55    /// 更新时间
56    pub updated_at: u64,
57    /// 分配的执行Agent ID列表
58    pub assigned_agents: Vec<String>,
59    /// 执行结果
60    pub execution_result: Option<ExecutionResult>,
61    /// 元数据
62    pub metadata: HashMap<String, String>,
63}
64
65impl TodoItem {
66    pub fn new(id: &str, raw_idea: &str, priority: TodoPriority) -> Self {
67        let now = std::time::SystemTime::now()
68            .duration_since(std::time::UNIX_EPOCH)
69            .unwrap_or_default()
70            .as_secs();
71
72        Self {
73            id: id.to_string(),
74            raw_idea: raw_idea.to_string(),
75            clarified_requirement: None,
76            status: TodoStatus::Pending,
77            priority,
78            created_at: now,
79            updated_at: now,
80            assigned_agents: Vec::new(),
81            execution_result: None,
82            metadata: HashMap::new(),
83        }
84    }
85
86    /// 更新状态
87    pub fn update_status(&mut self, status: TodoStatus) {
88        self.status = status;
89        self.updated_at = std::time::SystemTime::now()
90            .duration_since(std::time::UNIX_EPOCH)
91            .unwrap_or_default()
92            .as_secs();
93    }
94}
95
96// =============================================================================
97// 项目需求类型
98// =============================================================================
99
100/// 项目需求文档(澄清后的需求)
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct ProjectRequirement {
103    /// 需求标题
104    pub title: String,
105    /// 详细描述
106    pub description: String,
107    /// 验收标准
108    pub acceptance_criteria: Vec<String>,
109    /// 子任务列表
110    pub subtasks: Vec<Subtask>,
111    /// 依赖关系
112    pub dependencies: Vec<String>,
113    /// 预估工作量(可选)
114    pub estimated_effort: Option<String>,
115    /// 相关资源
116    pub resources: Vec<Resource>,
117}
118
119/// 子任务
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct Subtask {
122    /// 子任务ID
123    pub id: String,
124    /// 子任务描述
125    pub description: String,
126    /// 所需能力(用于匹配执行Agent)
127    pub required_capabilities: Vec<String>,
128    /// 执行顺序(可并行的任务可以有相同的顺序号)
129    pub order: u32,
130    /// 依赖的其他子任务ID
131    pub depends_on: Vec<String>,
132}
133
134/// 相关资源
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct Resource {
137    /// 资源名称
138    pub name: String,
139    /// 资源类型(file/url/api等)
140    pub resource_type: String,
141    /// 资源路径或URL
142    pub path: String,
143}
144
145// =============================================================================
146// 执行结果类型
147// =============================================================================
148
149/// 执行结果
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct ExecutionResult {
152    /// 是否成功
153    pub success: bool,
154    /// 结果摘要
155    pub summary: String,
156    /// 详细输出
157    pub details: HashMap<String, String>,
158    /// 产出物列表
159    pub artifacts: Vec<Artifact>,
160    /// 执行时间(毫秒)
161    pub execution_time_ms: u64,
162    /// 错误信息(如果失败)
163    pub error: Option<String>,
164}
165
166/// 产出物
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct Artifact {
169    /// 产出物名称
170    pub name: String,
171    /// 产出物类型
172    pub artifact_type: String,
173    /// 产出物路径或内容
174    pub content: String,
175}
176
177// =============================================================================
178// 决策类型
179// =============================================================================
180
181/// 关键决策(需要推送给人类)
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct CriticalDecision {
184    /// 决策ID
185    pub id: String,
186    /// 关联的Todo ID
187    pub todo_id: String,
188    /// 决策类型
189    pub decision_type: DecisionType,
190    /// 决策描述
191    pub description: String,
192    /// 可选方案
193    pub options: Vec<DecisionOption>,
194    /// 推荐方案(如果有)
195    pub recommended_option: Option<usize>,
196    /// 截止时间(Unix时间戳,可选)
197    pub deadline: Option<u64>,
198    /// 创建时间
199    pub created_at: u64,
200    /// 人类响应
201    pub human_response: Option<HumanResponse>,
202}
203
204/// 决策类型
205#[derive(Debug, Clone, Serialize, Deserialize)]
206pub enum DecisionType {
207    /// 需求澄清
208    RequirementClarification,
209    /// 技术选型
210    TechnicalChoice,
211    /// 优先级调整
212    PriorityAdjustment,
213    /// 异常处理
214    ExceptionHandling,
215    /// 资源申请
216    ResourceRequest,
217    /// 其他
218    Other(String),
219}
220
221/// 决策选项
222#[derive(Debug, Clone, Serialize, Deserialize)]
223pub struct DecisionOption {
224    /// 选项标签
225    pub label: String,
226    /// 选项描述
227    pub description: String,
228    /// 预期影响
229    pub impact: String,
230}
231
232/// 人类响应
233#[derive(Debug, Clone, Serialize, Deserialize)]
234pub struct HumanResponse {
235    /// 选择的选项索引
236    pub selected_option: usize,
237    /// 附加说明
238    pub comment: Option<String>,
239    /// 响应时间
240    pub responded_at: u64,
241}
242
243// =============================================================================
244// 汇报类型
245// =============================================================================
246
247/// 汇报消息
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct Report {
250    /// 汇报ID
251    pub id: String,
252    /// 汇报类型
253    pub report_type: ReportType,
254    /// 关联的Todo ID列表
255    pub todo_ids: Vec<String>,
256    /// 汇报内容
257    pub content: String,
258    /// 附带的统计数据
259    pub statistics: HashMap<String, serde_json::Value>,
260    /// 创建时间
261    pub created_at: u64,
262}
263
264/// 汇报类型
265#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
266pub enum ReportType {
267    /// 任务完成汇报
268    TaskCompletion,
269    /// 进度汇报
270    Progress,
271    /// 异常汇报
272    Exception,
273    /// 每日总结
274    DailySummary,
275}
276
277// =============================================================================
278// A2A 消息类型
279// =============================================================================
280
281/// 秘书 Agent 与执行 Agent 之间的 A2A 消息
282#[derive(Debug, Clone, Serialize, Deserialize)]
283pub enum SecretaryMessage {
284    /// 分配任务
285    AssignTask {
286        task_id: String,
287        subtask: Subtask,
288        context: HashMap<String, String>,
289    },
290    /// 任务状态查询
291    QueryTaskStatus { task_id: String },
292    /// 任务取消
293    CancelTask { task_id: String, reason: String },
294    /// 任务状态报告(执行Agent发送)
295    TaskStatusReport {
296        task_id: String,
297        status: TaskExecutionStatus,
298        progress: u32,
299        message: Option<String>,
300    },
301    /// 任务完成报告(执行Agent发送)
302    TaskCompleteReport {
303        task_id: String,
304        result: ExecutionResult,
305    },
306    /// 请求决策(执行Agent发送)
307    RequestDecision {
308        task_id: String,
309        decision: CriticalDecision,
310    },
311}
312
313/// 任务执行状态
314#[derive(Debug, Clone, Serialize, Deserialize)]
315pub enum TaskExecutionStatus {
316    /// 已接收
317    Received,
318    /// 准备中
319    Preparing,
320    /// 执行中
321    Executing,
322    /// 等待外部响应
323    WaitingExternal,
324    /// 已完成
325    Completed,
326    /// 失败
327    Failed(String),
328}
329
330// =============================================================================
331// 默认输入输出类型
332// =============================================================================
333
334/// 默认用户输入
335#[derive(Debug, Clone, Serialize, Deserialize)]
336pub enum DefaultInput {
337    /// 新想法/需求
338    Idea {
339        content: String,
340        priority: Option<TodoPriority>,
341        metadata: Option<HashMap<String, String>>,
342    },
343    /// 决策响应
344    Decision {
345        decision_id: String,
346        selected_option: usize,
347        comment: Option<String>,
348    },
349    /// 查询请求
350    Query(QueryType),
351    /// 命令
352    Command(SecretaryCommand),
353}
354
355/// 查询类型
356#[derive(Debug, Clone, Serialize, Deserialize)]
357pub enum QueryType {
358    /// 获取 Todo 列表
359    ListTodos { filter: Option<TodoStatus> },
360    /// 获取 Todo 详情
361    GetTodo { todo_id: String },
362    /// 获取统计信息
363    Statistics,
364    /// 获取待决策列表
365    PendingDecisions,
366    /// 获取汇报历史
367    Reports { report_type: Option<ReportType> },
368}
369
370/// 秘书命令
371#[derive(Debug, Clone, Serialize, Deserialize)]
372pub enum SecretaryCommand {
373    /// 开始澄清某个 Todo
374    Clarify { todo_id: String },
375    /// 分配任务
376    Dispatch { todo_id: String },
377    /// 取消任务
378    Cancel { todo_id: String, reason: String },
379    /// 生成汇报
380    GenerateReport { report_type: ReportType },
381    /// 暂停秘书 Agent
382    Pause,
383    /// 恢复秘书 Agent
384    Resume,
385    /// 关闭秘书 Agent
386    Shutdown,
387}
388
389/// 默认秘书输出
390#[derive(Debug, Clone, Serialize, Deserialize)]
391pub enum DefaultOutput {
392    /// 确认消息
393    Acknowledgment { message: String },
394    /// 需要决策
395    DecisionRequired { decision: CriticalDecision },
396    /// 汇报
397    Report { report: Report },
398    /// 状态更新
399    StatusUpdate { todo_id: String, status: TodoStatus },
400    /// 任务完成通知
401    TaskCompleted {
402        todo_id: String,
403        result: ExecutionResult,
404    },
405    /// 错误消息
406    Error { message: String },
407    /// 通用消息(LLM生成)
408    Message { content: String },
409}
410
411/// 秘书 Agent 工作阶段
412#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
413pub enum WorkPhase {
414    /// 阶段1: 接收想法
415    ReceivingIdea,
416    /// 阶段2: 澄清需求
417    ClarifyingRequirement,
418    /// 阶段3: 调度分配
419    DispatchingTask,
420    /// 阶段4: 监控反馈
421    MonitoringExecution,
422    /// 阶段5: 验收汇报
423    ReportingCompletion,
424}