microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI/Messages/UserMessage.cs
101lines · modecode
| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | |
| 4 | using Microsoft.Teams.Common; |
| 5 | |
| 6 | namespace Microsoft.Teams.AI.Messages; |
| 7 | |
| 8 | public class UserMessage(object? content) : UserMessage<object?>(content) |
| 9 | { |
| 10 | public static UserMessage<string> Text(string content) => new(content); |
| 11 | public static UserMessage<IEnumerable<IContent>> Text(IEnumerable<IContent> content) => new(content); |
| 12 | public static UserMessage<Stream> Media(Stream content) => new(content); |
| 13 | } |
| 14 | |
| 15 | public class UserMessage<T> : IMessage |
| 16 | { |
| 17 | [JsonPropertyName("role")] |
| 18 | [JsonPropertyOrder(0)] |
| 19 | public Role Role => Role.User; |
| 20 | |
| 21 | [JsonPropertyName("content")] |
| 22 | [JsonPropertyOrder(1)] |
| 23 | public T Content { get; set; } |
| 24 | |
| 25 | [JsonConstructor] |
| 26 | public UserMessage(T content) |
| 27 | { |
| 28 | Content = content; |
| 29 | } |
| 30 | |
| 31 | public string GetText() |
| 32 | { |
| 33 | if (Content is IEnumerable<IContent> asEnum) |
| 34 | { |
| 35 | return string.Join("\n", asEnum.Select(v => v.ToString())); |
| 36 | } |
| 37 | |
| 38 | if (Content is string asString) |
| 39 | { |
| 40 | return asString; |
| 41 | } |
| 42 | |
| 43 | return Content?.ToString() ?? throw new InvalidCastException(); |
| 44 | } |
| 45 | |
| 46 | public override string ToString() |
| 47 | { |
| 48 | return JsonSerializer.Serialize(this, new JsonSerializerOptions() |
| 49 | { |
| 50 | WriteIndented = true, |
| 51 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 52 | }); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | [JsonConverter(typeof(JsonConverter<ContentType>))] |
| 57 | public class ContentType(string value) : StringEnum(value) |
| 58 | { |
| 59 | public static readonly ContentType Text = new("text"); |
| 60 | public bool IsText => Text.Equals(Value); |
| 61 | |
| 62 | public static readonly ContentType ImageUrl = new("image_url"); |
| 63 | public bool IsImageUrl => ImageUrl.Equals(Value); |
| 64 | } |
| 65 | |
| 66 | /// <summary> |
| 67 | /// represents some message content |
| 68 | /// </summary> |
| 69 | public interface IContent |
| 70 | { |
| 71 | /// <summary> |
| 72 | /// the type of content |
| 73 | /// </summary> |
| 74 | public ContentType Type { get; } |
| 75 | } |
| 76 | |
| 77 | public class TextContent : IContent |
| 78 | { |
| 79 | [JsonPropertyName("type")] |
| 80 | [JsonPropertyOrder(0)] |
| 81 | public ContentType Type => ContentType.Text; |
| 82 | |
| 83 | [JsonPropertyName("text")] |
| 84 | [JsonPropertyOrder(1)] |
| 85 | public required string Text { get; set; } |
| 86 | |
| 87 | public override string ToString() => Text; |
| 88 | } |
| 89 | |
| 90 | public class ImageContent : IContent |
| 91 | { |
| 92 | [JsonPropertyName("type")] |
| 93 | [JsonPropertyOrder(0)] |
| 94 | public ContentType Type => ContentType.ImageUrl; |
| 95 | |
| 96 | [JsonPropertyName("image_url")] |
| 97 | [JsonPropertyOrder(1)] |
| 98 | public required string ImageUrl { get; set; } |
| 99 | |
| 100 | public override string ToString() => ImageUrl; |
| 101 | } |