using System.Collections.Generic; using System.Linq; using AI.Models; using AI.Models.Form; using AI.Models.SpecialMessages; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; namespace AI.Utils { /// /// 将 ISpecialMessage 序列化为 YAML 载荷字符串(仅载荷,不含 type;type 由 SpecialEntry 持有)。 /// public static class SpecialMessageSerializer { public static string Serialize(AI.Models.SpecialMessages.ISpecialMessage message) { if (message == null) { return string.Empty; } return message switch { FormRequestMessage form => SerializeForm(form), ParameterSetMessage param => SerializeParameterSet(param), TableDataMessage table => SerializeTable(table), ColumnMatchMessage col => SerializeColumnMatch(col), WorkflowStatusMessage wf => SerializeWorkflowStatus(wf), KnowledgeBaseMessage kb => SerializeKnowledgeBase(kb), AI.Models.SpecialMessages.XyzLoadCardMessage xyz => SerializeXyzLoadCard(xyz), AI.Models.SpecialMessages.GriddingParamCardMessage gp => SerializeGriddingParamCard(gp), _ => string.Empty }; } private static string SerializeForm(FormRequestMessage form) { var dict = new Dictionary { ["id"] = form.Id, ["formId"] = form.Definition.Id, ["title"] = form.Definition.Title, ["submitLabel"] = form.SubmitLabel, ["status"] = form.SubmitLabel }; var fields = new List>(); foreach (var entry in form.FieldsWithValues) { var f = new Dictionary { ["id"] = entry.Id, ["currentValue"] = entry.CurrentValue ?? string.Empty }; if (entry.Type == FormFieldType.MultiSelect && entry.SelectedValues.Count > 0) { f["selectedValues"] = entry.SelectedValues.ToList(); } fields.Add(f); } dict["fields"] = fields; return ToYaml(dict); } private static string SerializeParameterSet(ParameterSetMessage param) { var dict = new Dictionary { ["id"] = param.Id, ["title"] = param.Title, ["items"] = param.Items.Select(i => new Dictionary { ["name"] = i.Name, ["valueText"] = i.ValueText ?? string.Empty, ["description"] = i.Description ?? string.Empty, ["fieldType"] = i.FieldType.ToString(), ["options"] = i.Options ?? new List() }).ToList() }; return ToYaml(dict); } private static string SerializeTable(TableDataMessage table) { var dict = new Dictionary { ["id"] = table.Id, ["title"] = table.Title, ["columnNames"] = table.ColumnNames.ToList(), ["totalRowCount"] = table.TotalRowCount, ["maxPreviewRows"] = table.MaxPreviewRows, ["rows"] = table.Rows.Select(r => r.Cells.ToList()).ToList() }; return ToYaml(dict); } private static string SerializeColumnMatch(ColumnMatchMessage col) { var dict = new Dictionary { ["id"] = col.Id, ["title"] = col.Title, ["requiredColumns"] = col.RequiredColumns.ToList(), ["previewColumns"] = col.PreviewColumns.ToList(), ["mappings"] = col.Mappings.Select(m => new Dictionary { ["requiredColumn"] = m.RequiredColumn, ["matchedColumn"] = m.MatchedColumn }).ToList() }; return ToYaml(dict); } private static string SerializeWorkflowStatus(WorkflowStatusMessage wf) { var dict = new Dictionary { ["id"] = wf.Id, ["title"] = wf.Title, ["steps"] = (wf.Steps ?? new System.Collections.ObjectModel.ObservableCollection()) .Select(s => new Dictionary { ["id"] = s.Id, ["displayName"] = s.DisplayName ?? string.Empty, ["order"] = s.Order, ["status"] = s.Status.ToString(), ["outputResult"] = s.OutputResult?.ToString() ?? string.Empty, ["thought"] = s.Thought ?? string.Empty }).ToList() }; return ToYaml(dict); } private static string SerializeKnowledgeBase(KnowledgeBaseMessage kb) { var dict = new Dictionary { ["id"] = kb.Id, ["knowledgeId"] = kb.KnowledgeId, ["version"] = kb.Version ?? "1", ["topicKey"] = kb.TopicKey ?? string.Empty, ["query"] = kb.Query ?? string.Empty, ["rawContent"] = kb.RawContent ?? string.Empty }; return ToYaml(dict); } private static string SerializeXyzLoadCard(AI.Models.SpecialMessages.XyzLoadCardMessage xyz) { var dict = new Dictionary { ["id"] = xyz.Id, ["phase"] = (int)xyz.Phase, ["filePath"] = xyz.FilePath ?? string.Empty, ["matchButtonLabel"] = xyz.MatchButtonLabel ?? "确认匹配", }; if (xyz.TablePreview != null) { var tableDict = new Dictionary { ["title"] = xyz.TablePreview.Title, ["columnNames"] = xyz.TablePreview.ColumnNames.ToList(), ["totalRowCount"] = xyz.TablePreview.TotalRowCount, ["maxPreviewRows"] = xyz.TablePreview.MaxPreviewRows, ["rows"] = xyz.TablePreview.Rows.Select(r => r.Cells.ToList()).ToList(), }; dict["tablePreview"] = tableDict; } if (xyz.ColumnMatchFields.Count > 0) { dict["columnMatchFields"] = xyz.ColumnMatchFields.Select(f => new Dictionary { ["id"] = f.Id, ["label"] = f.Label, ["currentValue"] = f.CurrentValue ?? string.Empty, ["options"] = f.Options ?? new List(), }).ToList(); } if (xyz.ColumnMatchDefinition != null) { dict["columnMatchDefinitionTitle"] = xyz.ColumnMatchDefinition.Title ?? string.Empty; dict["columnMatchDefinitionSubmitTarget"] = xyz.ColumnMatchDefinition.SubmitTarget ?? string.Empty; } return ToYaml(dict); } private static string SerializeGriddingParamCard(AI.Models.SpecialMessages.GriddingParamCardMessage gp) { var dict = new Dictionary { ["id"] = gp.Id, ["phase"] = (int)gp.Phase, ["statusMessage"] = gp.StatusMessage ?? string.Empty, ["generateButtonLabel"] = gp.GenerateButtonLabel ?? "生成", ["items"] = gp.Items.Select(i => new Dictionary { ["name"] = i.Name, ["valueText"] = i.ValueText ?? string.Empty, ["description"] = i.Description ?? string.Empty, ["fieldType"] = i.FieldType.ToString(), ["options"] = i.Options ?? new List() }).ToList() }; return ToYaml(dict); } private static string ToYaml(Dictionary dict) { var serializer = new SerializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); return serializer.Serialize(dict); } } }