mofa-foundation 0.1.1

MoFA Foundation - Core building blocks and utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! 默认类型定义
//!
//! 包含默认秘书实现使用的所有类型。

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// =============================================================================
// Todo 任务类型
// =============================================================================

/// Todo 任务状态
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TodoStatus {
    /// 待处理
    Pending,
    /// 需求澄清中
    Clarifying,
    /// 执行中
    InProgress,
    /// 等待反馈
    WaitingFeedback,
    /// 已完成
    Completed,
    /// 已取消
    Cancelled,
    /// 阻塞中(需要人类决策)
    Blocked(String),
}

/// Todo 任务优先级
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TodoPriority {
    Low = 0,
    Medium = 1,
    High = 2,
    Urgent = 3,
}

/// Todo 任务项
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TodoItem {
    /// 任务ID
    pub id: String,
    /// 原始想法/需求描述
    pub raw_idea: String,
    /// 澄清后的需求文档
    pub clarified_requirement: Option<ProjectRequirement>,
    /// 任务状态
    pub status: TodoStatus,
    /// 优先级
    pub priority: TodoPriority,
    /// 创建时间(Unix时间戳)
    pub created_at: u64,
    /// 更新时间
    pub updated_at: u64,
    /// 分配的执行Agent ID列表
    pub assigned_agents: Vec<String>,
    /// 执行结果
    pub execution_result: Option<ExecutionResult>,
    /// 元数据
    pub metadata: HashMap<String, String>,
}

impl TodoItem {
    pub fn new(id: &str, raw_idea: &str, priority: TodoPriority) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            id: id.to_string(),
            raw_idea: raw_idea.to_string(),
            clarified_requirement: None,
            status: TodoStatus::Pending,
            priority,
            created_at: now,
            updated_at: now,
            assigned_agents: Vec::new(),
            execution_result: None,
            metadata: HashMap::new(),
        }
    }

    /// 更新状态
    pub fn update_status(&mut self, status: TodoStatus) {
        self.status = status;
        self.updated_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
    }
}

// =============================================================================
// 项目需求类型
// =============================================================================

/// 项目需求文档(澄清后的需求)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectRequirement {
    /// 需求标题
    pub title: String,
    /// 详细描述
    pub description: String,
    /// 验收标准
    pub acceptance_criteria: Vec<String>,
    /// 子任务列表
    pub subtasks: Vec<Subtask>,
    /// 依赖关系
    pub dependencies: Vec<String>,
    /// 预估工作量(可选)
    pub estimated_effort: Option<String>,
    /// 相关资源
    pub resources: Vec<Resource>,
}

/// 子任务
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subtask {
    /// 子任务ID
    pub id: String,
    /// 子任务描述
    pub description: String,
    /// 所需能力(用于匹配执行Agent)
    pub required_capabilities: Vec<String>,
    /// 执行顺序(可并行的任务可以有相同的顺序号)
    pub order: u32,
    /// 依赖的其他子任务ID
    pub depends_on: Vec<String>,
}

/// 相关资源
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Resource {
    /// 资源名称
    pub name: String,
    /// 资源类型(file/url/api等)
    pub resource_type: String,
    /// 资源路径或URL
    pub path: String,
}

// =============================================================================
// 执行结果类型
// =============================================================================

/// 执行结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionResult {
    /// 是否成功
    pub success: bool,
    /// 结果摘要
    pub summary: String,
    /// 详细输出
    pub details: HashMap<String, String>,
    /// 产出物列表
    pub artifacts: Vec<Artifact>,
    /// 执行时间(毫秒)
    pub execution_time_ms: u64,
    /// 错误信息(如果失败)
    pub error: Option<String>,
}

/// 产出物
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Artifact {
    /// 产出物名称
    pub name: String,
    /// 产出物类型
    pub artifact_type: String,
    /// 产出物路径或内容
    pub content: String,
}

// =============================================================================
// 决策类型
// =============================================================================

/// 关键决策(需要推送给人类)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CriticalDecision {
    /// 决策ID
    pub id: String,
    /// 关联的Todo ID
    pub todo_id: String,
    /// 决策类型
    pub decision_type: DecisionType,
    /// 决策描述
    pub description: String,
    /// 可选方案
    pub options: Vec<DecisionOption>,
    /// 推荐方案(如果有)
    pub recommended_option: Option<usize>,
    /// 截止时间(Unix时间戳,可选)
    pub deadline: Option<u64>,
    /// 创建时间
    pub created_at: u64,
    /// 人类响应
    pub human_response: Option<HumanResponse>,
}

/// 决策类型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DecisionType {
    /// 需求澄清
    RequirementClarification,
    /// 技术选型
    TechnicalChoice,
    /// 优先级调整
    PriorityAdjustment,
    /// 异常处理
    ExceptionHandling,
    /// 资源申请
    ResourceRequest,
    /// 其他
    Other(String),
}

/// 决策选项
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecisionOption {
    /// 选项标签
    pub label: String,
    /// 选项描述
    pub description: String,
    /// 预期影响
    pub impact: String,
}

/// 人类响应
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HumanResponse {
    /// 选择的选项索引
    pub selected_option: usize,
    /// 附加说明
    pub comment: Option<String>,
    /// 响应时间
    pub responded_at: u64,
}

// =============================================================================
// 汇报类型
// =============================================================================

/// 汇报消息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Report {
    /// 汇报ID
    pub id: String,
    /// 汇报类型
    pub report_type: ReportType,
    /// 关联的Todo ID列表
    pub todo_ids: Vec<String>,
    /// 汇报内容
    pub content: String,
    /// 附带的统计数据
    pub statistics: HashMap<String, serde_json::Value>,
    /// 创建时间
    pub created_at: u64,
}

/// 汇报类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ReportType {
    /// 任务完成汇报
    TaskCompletion,
    /// 进度汇报
    Progress,
    /// 异常汇报
    Exception,
    /// 每日总结
    DailySummary,
}

// =============================================================================
// A2A 消息类型
// =============================================================================

/// 秘书 Agent 与执行 Agent 之间的 A2A 消息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SecretaryMessage {
    /// 分配任务
    AssignTask {
        task_id: String,
        subtask: Subtask,
        context: HashMap<String, String>,
    },
    /// 任务状态查询
    QueryTaskStatus { task_id: String },
    /// 任务取消
    CancelTask { task_id: String, reason: String },
    /// 任务状态报告(执行Agent发送)
    TaskStatusReport {
        task_id: String,
        status: TaskExecutionStatus,
        progress: u32,
        message: Option<String>,
    },
    /// 任务完成报告(执行Agent发送)
    TaskCompleteReport {
        task_id: String,
        result: ExecutionResult,
    },
    /// 请求决策(执行Agent发送)
    RequestDecision {
        task_id: String,
        decision: CriticalDecision,
    },
}

/// 任务执行状态
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TaskExecutionStatus {
    /// 已接收
    Received,
    /// 准备中
    Preparing,
    /// 执行中
    Executing,
    /// 等待外部响应
    WaitingExternal,
    /// 已完成
    Completed,
    /// 失败
    Failed(String),
}

// =============================================================================
// 默认输入输出类型
// =============================================================================

/// 默认用户输入
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DefaultInput {
    /// 新想法/需求
    Idea {
        content: String,
        priority: Option<TodoPriority>,
        metadata: Option<HashMap<String, String>>,
    },
    /// 决策响应
    Decision {
        decision_id: String,
        selected_option: usize,
        comment: Option<String>,
    },
    /// 查询请求
    Query(QueryType),
    /// 命令
    Command(SecretaryCommand),
}

/// 查询类型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryType {
    /// 获取 Todo 列表
    ListTodos { filter: Option<TodoStatus> },
    /// 获取 Todo 详情
    GetTodo { todo_id: String },
    /// 获取统计信息
    Statistics,
    /// 获取待决策列表
    PendingDecisions,
    /// 获取汇报历史
    Reports { report_type: Option<ReportType> },
}

/// 秘书命令
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SecretaryCommand {
    /// 开始澄清某个 Todo
    Clarify { todo_id: String },
    /// 分配任务
    Dispatch { todo_id: String },
    /// 取消任务
    Cancel { todo_id: String, reason: String },
    /// 生成汇报
    GenerateReport { report_type: ReportType },
    /// 暂停秘书 Agent
    Pause,
    /// 恢复秘书 Agent
    Resume,
    /// 关闭秘书 Agent
    Shutdown,
}

/// 默认秘书输出
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DefaultOutput {
    /// 确认消息
    Acknowledgment { message: String },
    /// 需要决策
    DecisionRequired { decision: CriticalDecision },
    /// 汇报
    Report { report: Report },
    /// 状态更新
    StatusUpdate { todo_id: String, status: TodoStatus },
    /// 任务完成通知
    TaskCompleted {
        todo_id: String,
        result: ExecutionResult,
    },
    /// 错误消息
    Error { message: String },
    /// 通用消息(LLM生成)
    Message { content: String },
}

/// 秘书 Agent 工作阶段
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WorkPhase {
    /// 阶段1: 接收想法
    ReceivingIdea,
    /// 阶段2: 澄清需求
    ClarifyingRequirement,
    /// 阶段3: 调度分配
    DispatchingTask,
    /// 阶段4: 监控反馈
    MonitoringExecution,
    /// 阶段5: 验收汇报
    ReportingCompletion,
}