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.
74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
|
1 month ago
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using Microsoft.SemanticKernel.ChatCompletion;
|
||
|
|
|
||
|
|
namespace AI.Models.Store
|
||
|
|
{
|
||
|
|
public static class ConversationEntryMapper
|
||
|
|
{
|
||
|
|
public static List<ConversationEntryDto> ToDtoList(IEnumerable<ConversationEntry>? entries)
|
||
|
|
{
|
||
|
|
if (entries == null)
|
||
|
|
{
|
||
|
|
return new List<ConversationEntryDto>();
|
||
|
|
}
|
||
|
|
|
||
|
|
return entries.Select(e =>
|
||
|
|
{
|
||
|
|
if (e is TextConversationEntry t)
|
||
|
|
{
|
||
|
|
return new ConversationEntryDto
|
||
|
|
{
|
||
|
|
Kind = t.Kind,
|
||
|
|
Role = t.Role.ToString(),
|
||
|
|
Content = t.Content
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (e is SpecialConversationEntry s)
|
||
|
|
{
|
||
|
|
return new ConversationEntryDto
|
||
|
|
{
|
||
|
|
Kind = s.Kind,
|
||
|
|
Type = s.Type,
|
||
|
|
Payload = s.Payload
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return new ConversationEntryDto { Kind = e?.Kind ?? string.Empty };
|
||
|
|
}).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static List<ConversationEntry> ToEntryList(IEnumerable<ConversationEntryDto>? entries)
|
||
|
|
{
|
||
|
|
var list = new List<ConversationEntry>();
|
||
|
|
if (entries == null)
|
||
|
|
{
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (var e in entries)
|
||
|
|
{
|
||
|
|
if (e == null) continue;
|
||
|
|
|
||
|
|
if (string.Equals(e.Kind, "Text", StringComparison.OrdinalIgnoreCase))
|
||
|
|
{
|
||
|
|
var role = AuthorRole.User;
|
||
|
|
if (!string.IsNullOrEmpty(e.Role) && Enum.TryParse<AuthorRole>(e.Role, true, out var r))
|
||
|
|
{
|
||
|
|
role = r;
|
||
|
|
}
|
||
|
|
list.Add(new TextConversationEntry(role, e.Content ?? string.Empty));
|
||
|
|
}
|
||
|
|
else if (string.Equals(e.Kind, "Special", StringComparison.OrdinalIgnoreCase))
|
||
|
|
{
|
||
|
|
list.Add(new SpecialConversationEntry(e.Type ?? string.Empty, e.Payload ?? string.Empty));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|