microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.0-preview.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.AI/Messages/UserMessage.cs

101lines · modecode

1using System.Text.Json;
2using System.Text.Json.Serialization;
3
4using Microsoft.Teams.Common;
5
6namespace Microsoft.Teams.AI.Messages;
7
8public 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
15public 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>))]
57public 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>
69public interface IContent
70{
71 /// <summary>
72 /// the type of content
73 /// </summary>
74 public ContentType Type { get; }
75}
76
77public 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
90public 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}