You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/AI/Utils/SpecialMessageSerializer.cs

221 lines
8.6 KiB
C#

1 month ago
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
{
/// <summary>
/// 将 ISpecialMessage 序列化为 YAML 载荷字符串(仅载荷,不含 typetype 由 SpecialEntry 持有)。
/// </summary>
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<string, object>
{
["id"] = form.Id,
["formId"] = form.Definition.Id,
["title"] = form.Definition.Title,
["submitLabel"] = form.SubmitLabel,
["status"] = form.SubmitLabel
};
var fields = new List<Dictionary<string, object>>();
foreach (var entry in form.FieldsWithValues)
{
var f = new Dictionary<string, object>
{
["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<string, object>
{
["id"] = param.Id,
["title"] = param.Title,
["items"] = param.Items.Select(i => new Dictionary<string, object>
{
["name"] = i.Name,
["valueText"] = i.ValueText ?? string.Empty,
["description"] = i.Description ?? string.Empty,
["fieldType"] = i.FieldType.ToString(),
["options"] = i.Options ?? new List<string>()
}).ToList()
};
return ToYaml(dict);
}
private static string SerializeTable(TableDataMessage table)
{
var dict = new Dictionary<string, object>
{
["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<string, object>
{
["id"] = col.Id,
["title"] = col.Title,
["requiredColumns"] = col.RequiredColumns.ToList(),
["previewColumns"] = col.PreviewColumns.ToList(),
["mappings"] = col.Mappings.Select(m => new Dictionary<string, string>
{
["requiredColumn"] = m.RequiredColumn,
["matchedColumn"] = m.MatchedColumn
}).ToList<object>()
};
return ToYaml(dict);
}
private static string SerializeWorkflowStatus(WorkflowStatusMessage wf)
{
var dict = new Dictionary<string, object>
{
["id"] = wf.Id,
["title"] = wf.Title,
["steps"] = (wf.Steps ?? new System.Collections.ObjectModel.ObservableCollection<WorkflowStepModel>())
.Select(s => new Dictionary<string, object>
{
["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<string, object>
{
["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<string, object>
{
["id"] = xyz.Id,
["phase"] = (int)xyz.Phase,
["filePath"] = xyz.FilePath ?? string.Empty,
["matchButtonLabel"] = xyz.MatchButtonLabel ?? "确认匹配",
};
if (xyz.TablePreview != null)
{
var tableDict = new Dictionary<string, object>
{
["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<string, object>
{
["id"] = f.Id,
["label"] = f.Label,
["currentValue"] = f.CurrentValue ?? string.Empty,
["options"] = f.Options ?? new List<string>(),
}).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<string, object>
{
["id"] = gp.Id,
["phase"] = (int)gp.Phase,
["statusMessage"] = gp.StatusMessage ?? string.Empty,
["generateButtonLabel"] = gp.GenerateButtonLabel ?? "生成",
["items"] = gp.Items.Select(i => new Dictionary<string, object>
{
["name"] = i.Name,
["valueText"] = i.ValueText ?? string.Empty,
["description"] = i.Description ?? string.Empty,
["fieldType"] = i.FieldType.ToString(),
["options"] = i.Options ?? new List<string>()
}).ToList()
};
return ToYaml(dict);
}
private static string ToYaml(Dictionary<string, object> dict)
{
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
return serializer.Serialize(dict);
}
}
}