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