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