matrixcode-core 0.4.27

MatrixCode Agent Core - Pure logic, no UI
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
//! Workflow Create/Edit Tool
//!
//! 帮助用户创建、编辑、验证 workflow YAML 文件的工具
//!
//! 核心功能:
//! - 创建:从零开始创建 workflow
//! - 编辑:修改现有 workflow 的节点、边、参数
//! - 模板:提供预定义模板
//! - 验证:检查 workflow 结构合法性

use anyhow::{Result, bail};
use async_trait::async_trait;
use serde_json::{Value, json};

use crate::tools::{Tool, ToolDefinition};
use crate::workflow::{
    EdgeDef, InputDef, NodeDef, NodeType, OutputDef,
    parser::{parse_workflow, parse_workflow_from_file, to_yaml},
};
use std::fs;
use std::path::PathBuf;

/// 获取 workflow 保存目录
fn get_workflow_dir(location: &str) -> Result<PathBuf> {
    match location {
        "project" => {
            let cwd = std::env::current_dir()
                .map_err(|e| anyhow::anyhow!("Failed to get current directory: {}", e))?;
            Ok(cwd.join(".matrix").join("workflows"))
        }
        "user" => dirs::home_dir()
            .ok_or_else(|| anyhow::anyhow!("Failed to get home directory"))
            .map(|p| p.join(".matrix").join("workflows")),
        _ => bail!("Invalid location: {}. Use 'project' or 'user'", location),
    }
}

/// Workflow 创建工具
pub struct WorkflowCreateTool;

#[async_trait]
impl Tool for WorkflowCreateTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "workflow_create".to_string(),
            description: "创建和编辑 workflow YAML 文件,支持迭代修改。

【核心功能】
此工具是 workflow 制作的核心工具,支持完整的创建-编辑-验证循环。

【适用场景】
- 创建新的 workflow
- 修改现有 workflow(增删改节点、调整连接、修改参数)
- 获取预定义模板作为起点
- 验证 workflow 结构合法性
- **多次迭代调整**:用户可能反复修改直到满意

【功能模式】
- **create**: 创建新 workflow(首次创建)
- **edit**: 编辑现有 workflow(精确修改节点、边、参数)
- **template**: 获取模板(快速开始)
- **validate**: 验证结构(检查错误)
- **info**: 查看 workflow 详情(查看节点、边、参数)

【迭代修改流程】
典型使用流程:
1. template → 获取模板作为起点
2. create → 创建初始版本
3. edit → 多次调整(修改节点名称、添加节点、调整连接)
4. validate → 验证最终版本

【edit 模式操作类型】
- add_node: 添加节点
- remove_node: 删除节点
- update_node: 更新节点属性(name, task, params, on_failure)
- add_edge: 添加连接
- remove_edge: 删除连接
- add_input: 添加输入参数
- remove_input: 删除输入参数
- update_input: 更新输入参数
- add_output: 添加输出参数
- remove_output: 删除输出参数
- update_output: 更新输出参数
- update_metadata: 更新元数据(name, description, version)

【参数说明】
- mode: 操作模式 (create/edit/template/validate/info)
- workflow_id: 目标 workflow ID (edit/info 模式必需)
- workflow: workflow 定义 (create 模式必需)
- yaml_content: YAML 内容字符串 (可选,用于直接提供 YAML)
- edit_operation: 编辑操作类型 (edit 模式必需)
- edit_target: 编辑目标 (节点ID、边ID等)
- edit_value: 新值 (更新操作必需)
- location: 保存位置 (project/user,默认 project)
- template_type: 模板类型 (template 模式使用)

【使用示例】
创建 workflow:
{
  \"mode\": \"create\",
  \"workflow\": {\"id\": \"my-workflow\", \"name\": \"My Workflow\", ...}
}

添加节点:
{
  \"mode\": \"edit\",
  \"workflow_id\": \"my-workflow\",
  \"edit_operation\": \"add_node\",
  \"edit_value\": {\"id\": \"new_task\", \"type\": \"task\", \"name\": \"New Task\", \"task\": \"process\"}
}

修改节点名称:
{
  \"mode\": \"edit\",
  \"workflow_id\": \"my-workflow\",
  \"edit_operation\": \"update_node\",
  \"edit_target\": \"task1\",
  \"edit_value\": {\"name\": \"Updated Task Name\"}
}

添加连接:
{
  \"mode\": \"edit\",
  \"workflow_id\": \"my-workflow\",
  \"edit_operation\": \"add_edge\",
  \"edit_value\": {\"from\": \"task1\", \"to\": \"new_task\"}
}

查看详情:
{
  \"mode\": \"info\",
  \"workflow_id\": \"my-workflow\"
}".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "mode": {
                        "type": "string",
                        "enum": ["create", "edit", "template", "validate", "info"],
                        "description": "操作模式",
                        "default": "create"
                    },
                    "workflow_id": {
                        "type": "string",
                        "description": "目标 workflow ID (edit/info 模式必需)"
                    },
                    "workflow": {
                        "type": "object",
                        "description": "完整 workflow 定义 (create 模式)"
                    },
                    "yaml_content": {
                        "type": "string",
                        "description": "YAML 内容字符串 (可选)"
                    },
                    "edit_operation": {
                        "type": "string",
                        "enum": [
                            "add_node", "remove_node", "update_node",
                            "add_edge", "remove_edge",
                            "add_input", "remove_input", "update_input",
                            "add_output", "remove_output", "update_output",
                            "update_metadata"
                        ],
                        "description": "编辑操作类型 (edit 模式)"
                    },
                    "edit_target": {
                        "type": "string",
                        "description": "编辑目标:节点ID、边ID、输入参数名等"
                    },
                    "edit_value": {
                        "type": "object",
                        "description": "新值:节点定义、边定义、参数定义等"
                    },
                    "location": {
                        "type": "string",
                        "enum": ["project", "user"],
                        "description": "保存位置",
                        "default": "project"
                    },
                    "template_type": {
                        "type": "string",
                        "enum": ["simple", "parallel", "condition", "research", "batch"],
                        "description": "模板类型 (template 模式)"
                    },
                    "overwrite": {
                        "type": "boolean",
                        "description": "是否覆盖已存在文件",
                        "default": false
                    }
                },
                "required": ["mode"]
            }),
            is_priority: false,
        }
    }

    async fn execute(&self, params: Value) -> Result<String> {
        let mode = params["mode"].as_str().unwrap_or("create");

        match mode {
            "create" => self.handle_create(params).await,
            "edit" => self.handle_edit(params).await,
            "template" => self.handle_template(params).await,
            "validate" => self.handle_validate(params).await,
            "info" => self.handle_info(params).await,
            _ => bail!(
                "Invalid mode: {}. Supported modes: create, edit, template, validate, info",
                mode
            ),
        }
    }
}

impl WorkflowCreateTool {
    /// 处理创建 workflow
    async fn handle_create(&self, params: Value) -> Result<String> {
        // 获取 workflow 定义
        let workflow_def = if let Some(yaml_str) = params["yaml_content"].as_str() {
            // 从 YAML 解析
            parse_workflow(yaml_str).map_err(|e| anyhow::anyhow!("Failed to parse YAML: {}", e))?
        } else if let Some(workflow_json) = params.get("workflow") {
            // 从 JSON 解析
            serde_json::from_value(workflow_json.clone())
                .map_err(|e| anyhow::anyhow!("Failed to parse workflow JSON: {}", e))?
        } else {
            bail!(
                "Missing workflow definition. Provide either 'workflow' (JSON) or 'yaml_content' (YAML string)"
            );
        };

        // 验证 workflow
        workflow_def
            .validate()
            .map_err(|e| anyhow::anyhow!("Workflow validation failed: {}", e))?;

        // 获取保存位置
        let location = params["location"].as_str().unwrap_or("project");
        let overwrite = params["overwrite"].as_bool().unwrap_or(false);

        let save_dir = get_workflow_dir(location)?;

        // 创建目录(如果不存在)
        fs::create_dir_all(&save_dir)
            .map_err(|e| anyhow::anyhow!("Failed to create directory {:?}: {}", save_dir, e))?;

        // 检查文件是否存在
        let file_path = save_dir.join(format!("{}.yaml", workflow_def.id));
        if file_path.exists() && !overwrite {
            bail!(
                "Workflow file already exists: {:?}\nSet overwrite=true to replace it.",
                file_path
            );
        }

        // 转换为 YAML
        let yaml_content = to_yaml(&workflow_def)
            .map_err(|e| anyhow::anyhow!("Failed to convert to YAML: {}", e))?;

        // 保存文件
        fs::write(&file_path, &yaml_content)
            .map_err(|e| anyhow::anyhow!("Failed to write file {:?}: {}", file_path, e))?;

        Ok(format!(
            "✓ Workflow created successfully!\n\n📄 File: {:?}\n📁 ID: {}\n📝 Name: {}\n\n```yaml\n{}\n```\n\nUse 'workflow_run' to execute this workflow.",
            file_path, workflow_def.id, workflow_def.name, yaml_content
        ))
    }

    /// 处理模板请求
    async fn handle_template(&self, params: Value) -> Result<String> {
        let template_type = params["template_type"].as_str().unwrap_or("simple");

        let template = match template_type {
            "simple" => self.get_simple_template(),
            "parallel" => self.get_parallel_template(),
            "condition" => self.get_condition_template(),
            "research" => self.get_research_template(),
            "batch" => self.get_batch_template(),
            _ => bail!(
                "Unknown template type: {}. Available: simple, parallel, condition, research, batch",
                template_type
            ),
        };

        Ok(format!(
            "📋 Workflow Template: {}\n\n```yaml\n{}\n```\n\n💡 Tips:\n- Copy this template and modify as needed\n- Use 'workflow_create' with mode='create' to save it\n- Each workflow must have a start node and an end node",
            template_type, template
        ))
    }

    /// 验证 workflow 结构
    async fn handle_validate(&self, params: Value) -> Result<String> {
        let workflow_def = if let Some(yaml_str) = params["yaml_content"].as_str() {
            parse_workflow(yaml_str).map_err(|e| anyhow::anyhow!("YAML parsing failed: {}", e))?
        } else if let Some(workflow_json) = params.get("workflow") {
            serde_json::from_value(workflow_json.clone())
                .map_err(|e| anyhow::anyhow!("JSON parsing failed: {}", e))?
        } else {
            bail!(
                "Missing workflow definition. Provide either 'workflow' (JSON) or 'yaml_content' (YAML string)"
            );
        };

        // 执行验证
        match workflow_def.validate() {
            Ok(()) => Ok(format!(
                "✓ Workflow validation passed!\n\n📁 ID: {}\n📝 Name: {}\n📊 Nodes: {}\n🔗 Edges: {}\n📥 Inputs: {}\n📤 Outputs: {}",
                workflow_def.id,
                workflow_def.name,
                workflow_def.nodes.len(),
                workflow_def.edges.len(),
                workflow_def.inputs.len(),
                workflow_def.outputs.len()
            )),
            Err(e) => bail!("Workflow validation failed: {}", e),
        }
    }

    /// 处理编辑 workflow
    async fn handle_edit(&self, params: Value) -> Result<String> {
        // 获取 workflow ID
        let workflow_id = params["workflow_id"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing 'workflow_id' parameter"))?;

        // 获取保存位置
        let location = params["location"].as_str().unwrap_or("project");
        let save_dir = get_workflow_dir(location)?;
        let file_path = save_dir.join(format!("{}.yaml", workflow_id));

        // 直接从文件加载 workflow
        if !file_path.exists() {
            bail!("Workflow '{}' not found at {:?}", workflow_id, file_path);
        }

        let mut workflow = parse_workflow_from_file(&file_path)
            .map_err(|e| anyhow::anyhow!("Failed to load workflow: {}", e))?;

        // 获取编辑操作类型
        let edit_operation = params["edit_operation"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing 'edit_operation' parameter"))?;

        let edit_target = params["edit_target"].as_str();
        let edit_value = params.get("edit_value").cloned();

        // 执行编辑操作
        match edit_operation {
            // 节点操作
            "add_node" => {
                let node_json = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for add_node"))?;
                let new_node: NodeDef = serde_json::from_value(node_json)
                    .map_err(|e| anyhow::anyhow!("Invalid node definition: {}", e))?;

                // 检查节点ID是否已存在
                if workflow.nodes.iter().any(|n| n.id == new_node.id) {
                    bail!("Node '{}' already exists", new_node.id);
                }

                workflow.nodes.push(new_node);
            }

            "remove_node" => {
                let node_id = edit_target
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_target' for remove_node"))?;
                let idx = workflow
                    .nodes
                    .iter()
                    .position(|n| n.id == node_id)
                    .ok_or_else(|| anyhow::anyhow!("Node '{}' not found", node_id))?;

                // 不能删除 start/end 节点
                let node = &workflow.nodes[idx];
                if node.node_type == NodeType::Start || node.node_type == NodeType::End {
                    bail!("Cannot remove start/end nodes");
                }

                // 删除相关边
                workflow
                    .edges
                    .retain(|e| e.from != node_id && e.to != node_id);
                workflow.nodes.remove(idx);
            }

            "update_node" => {
                let node_id = edit_target
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_target' for update_node"))?;
                let node = workflow
                    .nodes
                    .iter_mut()
                    .find(|n| n.id == node_id)
                    .ok_or_else(|| anyhow::anyhow!("Node '{}' not found", node_id))?;

                let updates = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for update_node"))?;

                // 更新节点属性
                if let Some(name) = updates.get("name").and_then(|v| v.as_str()) {
                    node.name = name.to_string();
                }
                if let Some(task) = updates.get("task").and_then(|v| v.as_str()) {
                    node.task = Some(task.to_string());
                }
                if let Some(params_val) = updates.get("params") {
                    node.params = serde_json::from_value(params_val.clone())
                        .map_err(|e| anyhow::anyhow!("Invalid params: {}", e))?;
                }
                if let Some(timeout) = updates.get("timeout_ms").and_then(|v| v.as_u64()) {
                    node.timeout_ms = Some(timeout);
                }
            }

            // 边操作
            "add_edge" => {
                let edge_json = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for add_edge"))?;
                let new_edge: EdgeDef = serde_json::from_value(edge_json)
                    .map_err(|e| anyhow::anyhow!("Invalid edge definition: {}", e))?;

                // 检查源和目标节点是否存在
                if !workflow.nodes.iter().any(|n| n.id == new_edge.from) {
                    bail!("Source node '{}' not found", new_edge.from);
                }
                if !workflow.nodes.iter().any(|n| n.id == new_edge.to) {
                    bail!("Target node '{}' not found", new_edge.to);
                }

                workflow.edges.push(new_edge);
            }

            "remove_edge" => {
                let from = edit_target.ok_or_else(|| {
                    anyhow::anyhow!("Missing 'edit_target' (from node) for remove_edge")
                })?;
                let to = params["to"]
                    .as_str()
                    .ok_or_else(|| anyhow::anyhow!("Missing 'to' parameter for remove_edge"))?;

                workflow.edges.retain(|e| e.from != from || e.to != to);
            }

            // 输入参数操作
            "add_input" => {
                let input_json = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for add_input"))?;
                let new_input: InputDef = serde_json::from_value(input_json)
                    .map_err(|e| anyhow::anyhow!("Invalid input definition: {}", e))?;

                workflow.inputs.push(new_input);
            }

            "remove_input" => {
                let input_name = edit_target
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_target' for remove_input"))?;
                workflow.inputs.retain(|i| i.name != input_name);
            }

            "update_input" => {
                let input_name = edit_target
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_target' for update_input"))?;
                let input = workflow
                    .inputs
                    .iter_mut()
                    .find(|i| i.name == input_name)
                    .ok_or_else(|| anyhow::anyhow!("Input '{}' not found", input_name))?;

                let updates = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for update_input"))?;

                if let Some(desc) = updates.get("description").and_then(|v| v.as_str()) {
                    input.description = Some(desc.to_string());
                }
                if let Some(required) = updates.get("required").and_then(|v| v.as_bool()) {
                    input.required = required;
                }
                if let Some(default) = updates.get("default") {
                    input.default = Some(default.clone());
                }
            }

            // 输出参数操作
            "add_output" => {
                let output_json = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for add_output"))?;
                let new_output: OutputDef = serde_json::from_value(output_json)
                    .map_err(|e| anyhow::anyhow!("Invalid output definition: {}", e))?;

                workflow.outputs.push(new_output);
            }

            "remove_output" => {
                let output_name = edit_target
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_target' for remove_output"))?;
                workflow.outputs.retain(|o| o.name != output_name);
            }

            "update_output" => {
                let output_name = edit_target
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_target' for update_output"))?;
                let output = workflow
                    .outputs
                    .iter_mut()
                    .find(|o| o.name == output_name)
                    .ok_or_else(|| anyhow::anyhow!("Output '{}' not found", output_name))?;

                let updates = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for update_output"))?;

                if let Some(value) = updates.get("value").and_then(|v| v.as_str()) {
                    output.value = value.to_string();
                }
                if let Some(desc) = updates.get("description").and_then(|v| v.as_str()) {
                    output.description = Some(desc.to_string());
                }
            }

            // 元数据操作
            "update_metadata" => {
                let updates = edit_value
                    .ok_or_else(|| anyhow::anyhow!("Missing 'edit_value' for update_metadata"))?;

                if let Some(name) = updates.get("name").and_then(|v| v.as_str()) {
                    workflow.name = name.to_string();
                }
                if let Some(desc) = updates.get("description").and_then(|v| v.as_str()) {
                    workflow.description = Some(desc.to_string());
                }
                if let Some(version) = updates.get("version").and_then(|v| v.as_str()) {
                    workflow.version = version.to_string();
                }
            }

            _ => bail!("Unknown edit operation: {}", edit_operation),
        }

        // 验证修改后的 workflow
        workflow
            .validate()
            .map_err(|e| anyhow::anyhow!("Workflow validation failed after edit: {}", e))?;

        // 保存文件
        let yaml_content =
            to_yaml(&workflow).map_err(|e| anyhow::anyhow!("Failed to convert to YAML: {}", e))?;
        fs::write(&file_path, &yaml_content)
            .map_err(|e| anyhow::anyhow!("Failed to write file {:?}: {}", file_path, e))?;

        Ok(format!(
            "✓ Workflow '{}' updated successfully!\n\nOperation: {}\n📄 File: {:?}\n\n```yaml\n{}\n```\n\nUse 'info' mode to see detailed structure.",
            workflow_id, edit_operation, file_path, yaml_content
        ))
    }

    /// 处理查看 workflow 信息
    async fn handle_info(&self, params: Value) -> Result<String> {
        let workflow_id = params["workflow_id"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing 'workflow_id' parameter"))?;

        // 获取保存位置
        let location = params["location"].as_str().unwrap_or("project");
        let save_dir = get_workflow_dir(location)?;
        let file_path = save_dir.join(format!("{}.yaml", workflow_id));

        // 直接从文件加载
        if !file_path.exists() {
            bail!("Workflow '{}' not found at {:?}", workflow_id, file_path);
        }

        let workflow = parse_workflow_from_file(&file_path)
            .map_err(|e| anyhow::anyhow!("Failed to load workflow: {}", e))?;

        let mut info = format!(
            "📋 Workflow: {}\n\n📁 ID: {}\n📝 Name: {}\n📌 Version: {}\n",
            workflow_id, workflow.id, workflow.name, workflow.version
        );

        if let Some(ref desc) = workflow.description {
            info.push_str(&format!("📖 Description: {}\n", desc));
        }

        // 节点信息
        info.push_str("\n📊 Nodes:\n");
        for node in &workflow.nodes {
            let node_type = match node.node_type {
                NodeType::Start => "start",
                NodeType::End => "end",
                NodeType::Task => "task",
                NodeType::Condition => "condition",
                NodeType::Parallel => "parallel",
                NodeType::Wait => "wait",
                NodeType::Approval => "approval",
                NodeType::SubWorkflow => "subworkflow",
            };
            info.push_str(&format!("  - {} [{}] {}\n", node.id, node_type, node.name));

            if let Some(ref task) = node.task {
                info.push_str(&format!("    Task: {}\n", task));
            }
        }

        // 边信息
        info.push_str("\n🔗 Edges:\n");
        for edge in &workflow.edges {
            let label = edge
                .label
                .as_ref()
                .map(|l| format!(" ({})", l))
                .unwrap_or_default();
            info.push_str(&format!("  {}{}{}\n", edge.from, edge.to, label));
        }

        // 输入参数
        if !workflow.inputs.is_empty() {
            info.push_str("\n📥 Inputs:\n");
            for input in &workflow.inputs {
                let required = if input.required { " (required)" } else { "" };
                info.push_str(&format!(
                    "  - {} [{}]{}\n",
                    input.name, input.input_type, required
                ));
                if let Some(ref desc) = input.description {
                    info.push_str(&format!("    {}\n", desc));
                }
            }
        }

        // 输出参数
        if !workflow.outputs.is_empty() {
            info.push_str("\n📤 Outputs:\n");
            for output in &workflow.outputs {
                info.push_str(&format!("  - {}: {}\n", output.name, output.value));
            }
        }

        Ok(info)
    }

    /// 简单工作流模板
    fn get_simple_template(&self) -> String {
        r#"id: my-workflow
name: My Workflow
version: "1.0.0"
description: A simple workflow example

inputs:
  - name: param1
    type: string
    required: true
    description: First parameter

outputs:
  - name: result
    value: "{{nodes.end.output}}"

nodes:
  - id: start
    type: start
    name: Start

  - id: task1
    type: task
    name: First Task
    task: example_task
    params:
      input: "{{inputs.param1}}"
    on_failure:
      type: abort

  - id: end
    type: end
    name: End

edges:
  - from: start
    to: task1
  - from: task1
    to: end
"#
        .to_string()
    }

    /// 并行工作流模板
    fn get_parallel_template(&self) -> String {
        r#"id: parallel-workflow
name: Parallel Workflow
version: "1.0.0"
description: A workflow with parallel execution

inputs:
  - name: data
    type: string
    required: true
    description: Input data to process

nodes:
  - id: start
    type: start
    name: Start

  - id: parallel1
    type: parallel
    name: Parallel Processing
    parallel_branches:
      - name: Branch A
        nodes:
          - id: task_a
            type: task
            name: Task A
            task: process_a
            params:
              data: "{{inputs.data}}"
      - name: Branch B
        nodes:
          - id: task_b
            type: task
            name: Task B
            task: process_b
            params:
              data: "{{inputs.data}}"

  - id: merge
    type: task
    name: Merge Results
    task: merge_results
    params:
      result_a: "{{nodes.task_a.output}}"
      result_b: "{{nodes.task_b.output}}"

  - id: end
    type: end
    name: End

edges:
  - from: start
    to: parallel1
  - from: parallel1
    to: merge
  - from: merge
    to: end
"#
        .to_string()
    }

    /// 条件工作流模板
    fn get_condition_template(&self) -> String {
        r#"id: condition-workflow
name: Conditional Workflow
version: "1.0.0"
description: A workflow with conditional branches

inputs:
  - name: value
    type: number
    required: true
    description: Value to check

nodes:
  - id: start
    type: start
    name: Start

  - id: check_value
    type: condition
    name: Check Value
    branches:
      - name: Positive
        condition: "{{inputs.value}} > 0"
        target: positive_task
      - name: Negative
        condition: "{{inputs.value}} < 0"
        target: negative_task
      - name: Zero
        condition: "{{inputs.value}} == 0"
        target: zero_task

  - id: positive_task
    type: task
    name: Handle Positive
    task: handle_positive

  - id: negative_task
    type: task
    name: Handle Negative
    task: handle_negative

  - id: zero_task
    type: task
    name: Handle Zero
    task: handle_zero

  - id: end
    type: end
    name: End

edges:
  - from: start
    to: check_value
  - from: positive_task
    to: end
  - from: negative_task
    to: end
  - from: zero_task
    to: end
"#
        .to_string()
    }

    /// 研究工作流模板
    fn get_research_template(&self) -> String {
        r#"id: research-workflow
name: Research Workflow
version: "1.0.0"
description: Automated research and report generation

inputs:
  - name: topic
    type: string
    required: true
    description: Research topic
  - name: depth
    type: string
    required: false
    default: "medium"
    description: Research depth (shallow/medium/deep)

outputs:
  - name: report
    value: "{{nodes.generate_report.output}}"

nodes:
  - id: start
    type: start
    name: Start

  - id: search_web
    type: task
    name: Search Web
    task: websearch
    params:
      query: "{{inputs.topic}}"
      max_results: 10
    on_failure:
      type: retry
      max_attempts: 3

  - id: analyze_results
    type: task
    name: Analyze Results
    task: analyze
    params:
      data: "{{nodes.search_web.output}}"
      depth: "{{inputs.depth}}"

  - id: generate_report
    type: task
    name: Generate Report
    task: content_generation
    params:
      topic: "{{inputs.topic}}"
      research_data: "{{nodes.analyze_results.output}}"
      style: "professional"

  - id: end
    type: end
    name: End

edges:
  - from: start
    to: search_web
  - from: search_web
    to: analyze_results
  - from: analyze_results
    to: generate_report
  - from: generate_report
    to: end
"#
        .to_string()
    }

    /// 批量操作工作流模板
    fn get_batch_template(&self) -> String {
        r#"id: batch-workflow
name: Batch Processing Workflow
version: "1.0.0"
description: Process multiple items in batch

inputs:
  - name: items
    type: array
    required: true
    description: Array of items to process
  - name: operation
    type: string
    required: true
    description: Operation to perform on each item

nodes:
  - id: start
    type: start
    name: Start

  - id: prepare_batch
    type: task
    name: Prepare Batch
    task: prepare_batch
    params:
      items: "{{inputs.items}}"

  - id: process_items
    type: parallel
    name: Process Items
    parallel_branches:
      - name: Process Chunk 1
        nodes:
          - id: chunk1
            type: task
            name: Process Chunk 1
            task: "{{inputs.operation}}"
            params:
              items: "{{nodes.prepare_batch.chunk1}}"
      - name: Process Chunk 2
        nodes:
          - id: chunk2
            type: task
            name: Process Chunk 2
            task: "{{inputs.operation}}"
            params:
              items: "{{nodes.prepare_batch.chunk2}}"

  - id: aggregate_results
    type: task
    name: Aggregate Results
    task: aggregate
    params:
      results:
        - "{{nodes.chunk1.output}}"
        - "{{nodes.chunk2.output}}"

  - id: end
    type: end
    name: End

edges:
  - from: start
    to: prepare_batch
  - from: prepare_batch
    to: process_items
  - from: process_items
    to: aggregate_results
  - from: aggregate_results
    to: end
"#
        .to_string()
    }
}

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

    #[tokio::test]
    async fn test_template_simple() {
        let tool = WorkflowCreateTool;
        let result = tool
            .execute(json!({
                "mode": "template",
                "template_type": "simple"
            }))
            .await
            .unwrap();

        assert!(result.contains("simple"));
        assert!(result.contains("id:"));
        assert!(result.contains("nodes:"));
        assert!(result.contains("edges:"));
    }

    #[tokio::test]
    async fn test_template_research() {
        let tool = WorkflowCreateTool;
        let result = tool
            .execute(json!({
                "mode": "template",
                "template_type": "research"
            }))
            .await
            .unwrap();

        assert!(result.contains("research"));
        assert!(result.contains("websearch"));
        assert!(result.contains("generate_report"));
    }

    #[tokio::test]
    async fn test_validate_valid_workflow() {
        let tool = WorkflowCreateTool;
        let result = tool.execute(json!({
            "mode": "validate",
            "yaml_content": "id: test\nname: Test\nnodes:\n  - id: start\n    type: start\n    name: Start\n  - id: end\n    type: end\n    name: End\nedges:\n  - from: start\n    to: end"
        })).await.unwrap();

        assert!(result.contains("validation passed"));
        assert!(result.contains("Nodes: 2"));
        assert!(result.contains("Edges: 1"));
    }

    #[tokio::test]
    async fn test_validate_invalid_workflow() {
        let tool = WorkflowCreateTool;
        let result = tool.execute(json!({
            "mode": "validate",
            "yaml_content": "id: test\nname: Test\nnodes:\n  - id: task1\n    type: task\n    name: Task"
        })).await;

        // Should fail because no start/end nodes
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("validation failed")
        );
    }

    #[tokio::test]
    async fn test_info_mode() {
        let tool = WorkflowCreateTool;

        // 先创建一个 workflow
        let create_result = tool.execute(json!({
            "mode": "create",
            "workflow": {
                "id": "test-info-workflow",
                "name": "Test Info Workflow",
                "version": "1.0.0",
                "description": "A test workflow for info mode",
                "nodes": [
                    {"id": "start", "type": "start", "name": "Start"},
                    {"id": "task1", "type": "task", "name": "Task 1", "task": "process"},
                    {"id": "end", "type": "end", "name": "End"}
                ],
                "edges": [
                    {"from": "start", "to": "task1"},
                    {"from": "task1", "to": "end"}
                ],
                "inputs": [
                    {"name": "data", "type": "string", "required": true, "description": "Input data"}
                ],
                "outputs": [
                    {"name": "result", "value": "{{nodes.task1.output}}"}
                ]
            },
            "location": "project",
            "overwrite": true
        })).await.unwrap();

        assert!(create_result.contains("created successfully"));

        // 测试 info 模式
        let info_result = tool
            .execute(json!({
                "mode": "info",
                "workflow_id": "test-info-workflow"
            }))
            .await
            .unwrap();

        assert!(info_result.contains("test-info-workflow"));
        assert!(info_result.contains("Test Info Workflow"));
        assert!(info_result.contains("Task 1"));
        assert!(info_result.contains("start → task1"));
        assert!(info_result.contains("data [string] (required)"));
    }

    #[tokio::test]
    async fn test_edit_update_node() {
        let tool = WorkflowCreateTool;

        // 先创建一个 workflow
        tool.execute(json!({
            "mode": "create",
            "workflow": {
                "id": "test-edit-workflow",
                "name": "Test Edit Workflow",
                "version": "1.0.0",
                "nodes": [
                    {"id": "start", "type": "start", "name": "Start"},
                    {"id": "task1", "type": "task", "name": "Old Name", "task": "old_task"},
                    {"id": "end", "type": "end", "name": "End"}
                ],
                "edges": [
                    {"from": "start", "to": "task1"},
                    {"from": "task1", "to": "end"}
                ]
            },
            "location": "project",
            "overwrite": true
        }))
        .await
        .unwrap();

        // 测试 update_node
        let edit_result = tool
            .execute(json!({
                "mode": "edit",
                "workflow_id": "test-edit-workflow",
                "edit_operation": "update_node",
                "edit_target": "task1",
                "edit_value": {
                    "name": "New Name",
                    "task": "new_task"
                }
            }))
            .await
            .unwrap();

        assert!(edit_result.contains("updated successfully"));
        assert!(edit_result.contains("update_node"));

        // 验证更新
        let info_result = tool
            .execute(json!({
                "mode": "info",
                "workflow_id": "test-edit-workflow"
            }))
            .await
            .unwrap();

        assert!(info_result.contains("New Name"));
        assert!(info_result.contains("new_task"));
        assert!(!info_result.contains("Old Name"));
    }

    #[tokio::test]
    async fn test_edit_add_node() {
        let tool = WorkflowCreateTool;

        // 先创建一个 workflow
        tool.execute(json!({
            "mode": "create",
            "workflow": {
                "id": "test-add-node-workflow",
                "name": "Test Add Node",
                "version": "1.0.0",
                "nodes": [
                    {"id": "start", "type": "start", "name": "Start"},
                    {"id": "end", "type": "end", "name": "End"}
                ],
                "edges": [
                    {"from": "start", "to": "end"}
                ]
            },
            "location": "project",
            "overwrite": true
        }))
        .await
        .unwrap();

        // 测试 add_node
        let edit_result = tool
            .execute(json!({
                "mode": "edit",
                "workflow_id": "test-add-node-workflow",
                "edit_operation": "add_node",
                "edit_value": {
                    "id": "new_task",
                    "type": "task",
                    "name": "New Task",
                    "task": "process"
                }
            }))
            .await
            .unwrap();

        assert!(edit_result.contains("updated successfully"));

        // 测试 add_edge
        let edge_result = tool
            .execute(json!({
                "mode": "edit",
                "workflow_id": "test-add-node-workflow",
                "edit_operation": "add_edge",
                "edit_value": {
                    "from": "start",
                    "to": "new_task"
                }
            }))
            .await
            .unwrap();

        assert!(edge_result.contains("updated successfully"));

        // 再添加一条边
        tool.execute(json!({
            "mode": "edit",
            "workflow_id": "test-add-node-workflow",
            "edit_operation": "add_edge",
            "edit_value": {
                "from": "new_task",
                "to": "end"
            }
        }))
        .await
        .unwrap();

        // 验证节点和边
        let info_result = tool
            .execute(json!({
                "mode": "info",
                "workflow_id": "test-add-node-workflow"
            }))
            .await
            .unwrap();

        assert!(info_result.contains("new_task"));
        assert!(info_result.contains("New Task"));
        assert!(info_result.contains("start → new_task"));
        assert!(info_result.contains("new_task → end"));
    }

    #[tokio::test]
    async fn test_invalid_mode() {
        let tool = WorkflowCreateTool;
        let result = tool
            .execute(json!({
                "mode": "invalid"
            }))
            .await;

        assert!(result.is_err());
    }

    #[test]
    fn test_definition() {
        let tool = WorkflowCreateTool;
        let def = tool.definition();

        assert_eq!(def.name, "workflow_create");
        assert!(def.description.contains("创建并保存"));
        assert!(def.parameters["properties"]["mode"].is_object());
    }
}