forked from a2aproject/a2a-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.cs
More file actions
101 lines (90 loc) · 2.61 KB
/
Message.cs
File metadata and controls
101 lines (90 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Text.Json;
using System.Text.Json.Serialization;
namespace A2A;
/// <summary>
/// Message sender's role.
/// </summary>
[JsonConverter(typeof(KebabCaseLowerJsonStringEnumConverter<MessageRole>))]
public enum MessageRole
{
/// <summary>
/// User role.
/// </summary>
User,
/// <summary>
/// Agent role.
/// </summary>
Agent
}
/// <summary>
/// JSON converter for MessageRole enum.
/// </summary>
public class MessageRoleConverter : JsonConverter<MessageRole>
{
/// <inheritdoc />
public override MessageRole Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
return value switch
{
"user" => MessageRole.User,
"agent" => MessageRole.Agent,
_ => throw new JsonException($"Unknown message role: {value}")
};
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, MessageRole value, JsonSerializerOptions options)
{
var role = value switch
{
MessageRole.User => "user",
MessageRole.Agent => "agent",
_ => throw new JsonException($"Unknown message role: {value}")
};
writer.WriteStringValue(role);
}
}
/// <summary>
/// Represents a single message exchanged between user and agent.
/// </summary>
public class Message : A2AResponse
{
/// <summary>
/// Message sender's role.
/// </summary>
[JsonPropertyName("role")]
[JsonRequired]
public MessageRole Role { get; set; } = MessageRole.User;
/// <summary>
/// Message content.
/// </summary>
[JsonPropertyName("parts")]
[JsonRequired]
public List<Part> Parts { get; set; } = [];
/// <summary>
/// Extension metadata.
/// </summary>
[JsonPropertyName("metadata")]
public Dictionary<string, JsonElement>? Metadata { get; set; }
/// <summary>
/// List of tasks referenced as context by this message.
/// </summary>
[JsonPropertyName("referenceTaskIds")]
public List<string>? ReferenceTaskIds { get; set; }
/// <summary>
/// Identifier created by the message creator.
/// </summary>
[JsonPropertyName("messageId")]
[JsonRequired]
public string MessageId { get; set; } = string.Empty;
/// <summary>
/// Identifier of task the message is related to.
/// </summary>
[JsonPropertyName("taskId")]
public string? TaskId { get; set; }
/// <summary>
/// The context the message is associated with.
/// </summary>
[JsonPropertyName("contextId")]
public string? ContextId { get; set; }
}