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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! 预置 Prompt 模板库
//!
//! 提供常用场景的 Prompt 模板

use super::registry::PromptRegistry;
use super::template::{PromptTemplate, PromptVariable};

// ============================================================================
// 通用助手模板
// ============================================================================

/// 通用助手系统提示
pub fn general_assistant() -> PromptTemplate {
    PromptTemplate::new("general-assistant")
        .with_name("通用助手")
        .with_description("通用 AI 助手系统提示")
        .with_content(
            "你是一个乐于助人的 AI 助手。请以清晰、准确、专业的方式回答用户的问题。\n\n\
            回答时请注意:\n\
            1. 保持回答简洁明了\n\
            2. 如果不确定,请诚实说明\n\
            3. 必要时提供示例说明",
        )
        .with_tag("system")
        .with_tag("general")
}

/// 专业角色助手
pub fn role_assistant() -> PromptTemplate {
    PromptTemplate::new("role-assistant")
        .with_name("角色助手")
        .with_description("可自定义角色的助手模板")
        .with_content(
            "你是一个专业的{role}。你的专长是{expertise}。\n\n\
            在回答问题时,请:\n\
            1. 运用你的专业知识\n\
            2. 提供有见地的分析\n\
            3. 给出可操作的建议",
        )
        .with_variable(
            PromptVariable::new("role").with_description("角色名称,如:软件工程师、数据分析师"),
        )
        .with_variable(
            PromptVariable::new("expertise")
                .with_description("专业领域")
                .with_default("解决问题和提供帮助"),
        )
        .with_tag("system")
        .with_tag("role")
}

// ============================================================================
// 代码相关模板
// ============================================================================

/// 代码审查模板
pub fn code_review() -> PromptTemplate {
    PromptTemplate::new("code-review")
        .with_name("代码审查")
        .with_description("专业代码审查模板")
        .with_content(
            "请作为一个资深的{language}开发者,审查以下代码:\n\n\
            ```{language}\n{code}\n```\n\n\
            请从以下方面进行审查:\n\
            1. **代码质量**:可读性、命名规范、代码结构\n\
            2. **潜在问题**:Bug、边界情况、错误处理\n\
            3. **性能**:效率问题、优化建议\n\
            4. **安全性**:安全漏洞、敏感信息处理\n\
            5. **最佳实践**:设计模式、惯用写法\n\n\
            请提供具体的改进建议和示例代码。",
        )
        .with_variable(
            PromptVariable::new("language")
                .with_description("编程语言")
                .with_default("代码"),
        )
        .with_variable(PromptVariable::new("code").with_description("要审查的代码"))
        .with_tag("code")
        .with_tag("review")
}

/// 代码解释模板
pub fn code_explain() -> PromptTemplate {
    PromptTemplate::new("code-explain")
        .with_name("代码解释")
        .with_description("解释代码功能和原理")
        .with_content(
            "请详细解释以下{language}代码的功能和工作原理:\n\n\
            ```{language}\n{code}\n```\n\n\
            请包含:\n\
            1. **功能概述**:代码的主要用途\n\
            2. **逐行/逐块解释**:关键部分的详细说明\n\
            3. **使用示例**:如何调用或使用这段代码\n\
            4. **注意事项**:使用时需要注意的问题",
        )
        .with_variable(
            PromptVariable::new("language")
                .with_description("编程语言")
                .with_default("代码"),
        )
        .with_variable(PromptVariable::new("code").with_description("要解释的代码"))
        .with_tag("code")
        .with_tag("explain")
}

/// 代码生成模板
pub fn code_generate() -> PromptTemplate {
    PromptTemplate::new("code-generate")
        .with_name("代码生成")
        .with_description("根据需求生成代码")
        .with_content(
            "请使用 {language} 编写代码实现以下功能:\n\n\
            **需求描述**:\n{requirement}\n\n\
            **要求**:\n\
            1. 代码应该简洁、高效、可读性强\n\
            2. 包含必要的注释说明\n\
            3. 考虑边界情况和错误处理\n\
            4. 遵循 {language} 的最佳实践",
        )
        .with_variable(PromptVariable::new("language").with_description("编程语言"))
        .with_variable(PromptVariable::new("requirement").with_description("功能需求描述"))
        .with_tag("code")
        .with_tag("generate")
}

/// 代码重构模板
pub fn code_refactor() -> PromptTemplate {
    PromptTemplate::new("code-refactor")
        .with_name("代码重构")
        .with_description("重构和优化代码")
        .with_content(
            "请重构以下{language}代码,{goal}:\n\n\
            ```{language}\n{code}\n```\n\n\
            重构时请:\n\
            1. 保持功能不变\n\
            2. 提高代码质量\n\
            3. 解释你的改动及原因\n\
            4. 提供重构后的完整代码",
        )
        .with_variable(
            PromptVariable::new("language")
                .with_description("编程语言")
                .with_default("代码"),
        )
        .with_variable(PromptVariable::new("code").with_description("要重构的代码"))
        .with_variable(
            PromptVariable::new("goal")
                .with_description("重构目标")
                .with_default("使其更加清晰、高效"),
        )
        .with_tag("code")
        .with_tag("refactor")
}

/// 单元测试生成模板
pub fn code_test() -> PromptTemplate {
    PromptTemplate::new("code-test")
        .with_name("测试生成")
        .with_description("为代码生成单元测试")
        .with_content(
            "请为以下{language}代码编写单元测试:\n\n\
            ```{language}\n{code}\n```\n\n\
            测试要求:\n\
            1. 覆盖主要功能路径\n\
            2. 包含边界情况测试\n\
            3. 包含错误情况测试\n\
            4. 使用 {test_framework} 测试框架",
        )
        .with_variable(PromptVariable::new("language").with_description("编程语言"))
        .with_variable(PromptVariable::new("code").with_description("要测试的代码"))
        .with_variable(
            PromptVariable::new("test_framework")
                .with_description("测试框架")
                .with_default("标准"),
        )
        .with_tag("code")
        .with_tag("test")
}

// ============================================================================
// 写作和文档模板
// ============================================================================

/// 技术文档模板
pub fn tech_doc() -> PromptTemplate {
    PromptTemplate::new("tech-doc")
        .with_name("技术文档")
        .with_description("撰写技术文档")
        .with_content(
            "请为 {subject} 撰写技术文档。\n\n\
            文档应包含:\n\
            1. **概述**:简要介绍主题\n\
            2. **安装/配置**:如何开始使用\n\
            3. **使用指南**:基本用法和示例\n\
            4. **API 参考**:详细接口说明(如适用)\n\
            5. **常见问题**:FAQ 和故障排除\n\n\
            目标受众:{audience}",
        )
        .with_variable(PromptVariable::new("subject").with_description("文档主题"))
        .with_variable(
            PromptVariable::new("audience")
                .with_description("目标读者")
                .with_default("开发者"),
        )
        .with_tag("doc")
        .with_tag("writing")
}

/// 总结模板
pub fn summarize() -> PromptTemplate {
    PromptTemplate::new("summarize")
        .with_name("内容总结")
        .with_description("总结长文本内容")
        .with_content(
            "请总结以下内容:\n\n{content}\n\n\
            总结要求:\n\
            1. 长度约 {length}\n\
            2. 保留关键信息和主要观点\n\
            3. 使用清晰的结构组织\n\
            4. 语言简练准确",
        )
        .with_variable(PromptVariable::new("content").with_description("要总结的内容"))
        .with_variable(
            PromptVariable::new("length")
                .with_description("目标长度")
                .with_default("200-300字"),
        )
        .with_tag("writing")
        .with_tag("summary")
}

/// 翻译模板
pub fn translate() -> PromptTemplate {
    PromptTemplate::new("translate")
        .with_name("翻译")
        .with_description("翻译文本")
        .with_content(
            "请将以下内容从{source_lang}翻译成{target_lang}:\n\n\
            {content}\n\n\
            翻译要求:\n\
            1. 保持原文含义准确\n\
            2. 符合目标语言的表达习惯\n\
            3. 保持专业术语的准确性\n\
            4. 保持原文的语气和风格",
        )
        .with_variable(
            PromptVariable::new("source_lang")
                .with_description("源语言")
                .with_default("英文"),
        )
        .with_variable(
            PromptVariable::new("target_lang")
                .with_description("目标语言")
                .with_default("中文"),
        )
        .with_variable(PromptVariable::new("content").with_description("要翻译的内容"))
        .with_tag("writing")
        .with_tag("translation")
}

// ============================================================================
// 分析和推理模板
// ============================================================================

/// 问题分析模板
pub fn analyze() -> PromptTemplate {
    PromptTemplate::new("analyze")
        .with_name("问题分析")
        .with_description("分析问题并给出解决方案")
        .with_content(
            "请分析以下问题并给出解决方案:\n\n\
            **问题描述**:\n{problem}\n\n\
            **上下文**:\n{context}\n\n\
            请提供:\n\
            1. **问题分析**:问题的根本原因\n\
            2. **解决方案**:可行的解决方法\n\
            3. **优劣分析**:各方案的优缺点\n\
            4. **推荐方案**:最佳建议及理由",
        )
        .with_variable(PromptVariable::new("problem").with_description("问题描述"))
        .with_variable(
            PromptVariable::new("context")
                .with_description("相关背景信息")
                .with_default("无额外上下文"),
        )
        .with_tag("analysis")
        .with_tag("problem-solving")
}

/// 对比分析模板
pub fn compare() -> PromptTemplate {
    PromptTemplate::new("compare")
        .with_name("对比分析")
        .with_description("对比多个选项")
        .with_content(
            "请对比分析以下选项:\n\n\
            {options}\n\n\
            对比维度:{dimensions}\n\n\
            请提供:\n\
            1. **详细对比表格**\n\
            2. **各选项优缺点**\n\
            3. **适用场景分析**\n\
            4. **推荐建议**",
        )
        .with_variable(PromptVariable::new("options").with_description("要对比的选项列表"))
        .with_variable(
            PromptVariable::new("dimensions")
                .with_description("对比维度")
                .with_default("功能、性能、易用性、成本"),
        )
        .with_tag("analysis")
        .with_tag("comparison")
}

// ============================================================================
// ReAct Agent 模板
// ============================================================================

/// ReAct 推理系统提示
pub fn react_system() -> PromptTemplate {
    PromptTemplate::new("react-system")
        .with_name("ReAct 系统提示")
        .with_description("ReAct Agent 的系统提示")
        .with_content(
            "你是一个使用 ReAct(Reasoning + Acting)方法解决问题的 AI Agent。\n\n\
            你可以使用以下工具:\n{tools}\n\n\
            解决问题时,请遵循以下步骤:\n\n\
            1. **Thought(思考)**:分析当前情况,决定下一步行动\n\
            2. **Action(行动)**:选择合适的工具并执行\n\
            3. **Observation(观察)**:分析工具返回的结果\n\
            4. 重复上述步骤直到得到答案\n\
            5. **Final Answer(最终答案)**:给出完整的回答\n\n\
            格式要求:\n\
            - Thought: 你的思考过程\n\
            - Action: 工具名称[参数]\n\
            - Observation: 工具返回的结果\n\
            - Final Answer: 你的最终回答",
        )
        .with_variable(PromptVariable::new("tools").with_description("可用工具列表"))
        .with_tag("react")
        .with_tag("agent")
}

/// ReAct 任务模板
pub fn react_task() -> PromptTemplate {
    PromptTemplate::new("react-task")
        .with_name("ReAct 任务")
        .with_description("ReAct Agent 的任务模板")
        .with_content("请完成以下任务:\n\n{task}\n\n开始你的推理和行动:")
        .with_variable(PromptVariable::new("task").with_description("任务描述"))
        .with_tag("react")
        .with_tag("agent")
}

// ============================================================================
// 多 Agent 协作模板
// ============================================================================

/// 辩论者模板
pub fn debater() -> PromptTemplate {
    PromptTemplate::new("debater")
        .with_name("辩论者")
        .with_description("辩论模式中的辩论者角色")
        .with_content(
            "你是辩论中的{position}方。\n\n\
            辩题:{topic}\n\n\
            前述观点:\n{previous}\n\n\
            请从你的立场出发,提供有力的论点。注意:\n\
            1. 基于事实和逻辑论证\n\
            2. 回应对方的观点\n\
            3. 保持专业和理性",
        )
        .with_variable(
            PromptVariable::new("position")
                .with_description("辩论立场")
                .with_enum(vec!["".to_string(), "".to_string()]),
        )
        .with_variable(PromptVariable::new("topic").with_description("辩论话题"))
        .with_variable(
            PromptVariable::new("previous")
                .with_description("之前的辩论内容")
                .with_default("这是辩论的开始"),
        )
        .with_tag("multi-agent")
        .with_tag("debate")
}

/// 监督者模板
pub fn supervisor() -> PromptTemplate {
    PromptTemplate::new("supervisor")
        .with_name("监督者")
        .with_description("监督模式中的监督者角色")
        .with_content(
            "你是一个团队监督者,负责评估团队成员的工作成果。\n\n\
            **任务**:{task}\n\n\
            **团队成员的回答**:\n{responses}\n\n\
            请:\n\
            1. 评估每个回答的质量\n\
            2. 指出各自的优点和不足\n\
            3. 综合最佳内容给出最终答案\n\
            4. 给出改进建议",
        )
        .with_variable(PromptVariable::new("task").with_description("任务描述"))
        .with_variable(PromptVariable::new("responses").with_description("团队成员的回答"))
        .with_tag("multi-agent")
        .with_tag("supervisor")
}

/// 聚合者模板
pub fn aggregator() -> PromptTemplate {
    PromptTemplate::new("aggregator")
        .with_name("聚合者")
        .with_description("并行模式中的结果聚合角色")
        .with_content(
            "多个 Agent 已经分别处理了以下任务:\n\n\
            **原始任务**:{task}\n\n\
            **各 Agent 的结果**:\n{results}\n\n\
            请综合以上结果:\n\
            1. 找出共同点和差异点\n\
            2. 整合各方优势\n\
            3. 形成一个完整、准确的最终答案",
        )
        .with_variable(PromptVariable::new("task").with_description("原始任务"))
        .with_variable(PromptVariable::new("results").with_description("各 Agent 的结果"))
        .with_tag("multi-agent")
        .with_tag("aggregation")
}

// ============================================================================
// 注册表预加载
// ============================================================================

/// 创建包含所有预置模板的注册中心
pub fn create_preset_registry() -> PromptRegistry {
    let mut registry = PromptRegistry::new();

    // 通用助手
    registry.register(general_assistant());
    registry.register(role_assistant());

    // 代码相关
    registry.register(code_review());
    registry.register(code_explain());
    registry.register(code_generate());
    registry.register(code_refactor());
    registry.register(code_test());

    // 写作和文档
    registry.register(tech_doc());
    registry.register(summarize());
    registry.register(translate());

    // 分析和推理
    registry.register(analyze());
    registry.register(compare());

    // ReAct Agent
    registry.register(react_system());
    registry.register(react_task());

    // 多 Agent 协作
    registry.register(debater());
    registry.register(supervisor());
    registry.register(aggregator());

    registry
}

/// 将预置模板加载到现有注册中心
pub fn load_presets(registry: &mut PromptRegistry) {
    let presets = create_preset_registry();
    registry.merge(presets);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_preset_templates() {
        let registry = create_preset_registry();

        // 验证模板存在
        assert!(registry.contains("general-assistant"));
        assert!(registry.contains("code-review"));
        assert!(registry.contains("react-system"));
        assert!(registry.contains("supervisor"));

        // 验证标签
        let code_templates = registry.find_by_tag("code");
        assert!(code_templates.len() >= 4);

        let agent_templates = registry.find_by_tag("agent");
        assert!(agent_templates.len() >= 2);
    }

    #[test]
    fn test_code_review_template() {
        let template = code_review();

        let result = template
            .render(&[
                ("language", "rust"),
                ("code", "fn main() { info!(\"Hello\"); }"),
            ])
            .unwrap();

        assert!(result.contains("rust"));
        assert!(result.contains("fn main"));
    }

    #[test]
    fn test_role_assistant_template() {
        let template = role_assistant();

        // 使用默认值
        let result = template.render(&[("role", "数据分析师")]).unwrap();

        assert!(result.contains("数据分析师"));
        assert!(result.contains("解决问题和提供帮助")); // 默认值
    }

    #[test]
    fn test_react_system_template() {
        let template = react_system();

        let tools = "1. search: 搜索网页\n2. calculator: 计算器";
        let result = template.render(&[("tools", tools)]).unwrap();

        assert!(result.contains("ReAct"));
        assert!(result.contains("search"));
        assert!(result.contains("Thought"));
    }

    #[test]
    fn test_translate_template() {
        let template = translate();

        let result = template.render(&[("content", "Hello, World!")]).unwrap();

        assert!(result.contains("Hello, World!"));
        assert!(result.contains("英文")); // 默认值
        assert!(result.contains("中文")); // 默认值
    }

    #[test]
    fn test_load_presets() {
        let mut registry = PromptRegistry::new();
        assert!(registry.is_empty());

        load_presets(&mut registry);

        assert!(!registry.is_empty());
        assert!(registry.len() >= 15);
    }
}