Skip to content

Commit 139b4ca

Browse files
authored
Merge pull request #277 from microsoft/users/tracyboehrer/conversationaccount-covnerter
Added ConversationAccountConverter
2 parents 7dab590 + 83f278b commit 139b4ca

File tree

7 files changed

+82
-6
lines changed

7 files changed

+82
-6
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Agents.Core.Models;
5+
using System;
6+
using System.Text.Json;
7+
8+
namespace Microsoft.Agents.Core.Serialization.Converters
9+
{
10+
internal class ConversationAccountConverter : ConnectorConverter<ConversationAccount>
11+
{
12+
protected override void ReadExtensionData(ref Utf8JsonReader reader, ConversationAccount value, string propertyName, JsonSerializerOptions options)
13+
{
14+
var extensionData = JsonSerializer.Deserialize<JsonElement>(ref reader, options);
15+
value.Properties.Add(propertyName, extensionData);
16+
}
17+
18+
protected override bool TryReadExtensionData(ref Utf8JsonReader reader, ConversationAccount value, string propertyName, JsonSerializerOptions options)
19+
{
20+
if (propertyName.Equals(nameof(value.Properties)))
21+
{
22+
var propertyValue = JsonSerializer.Deserialize<object>(ref reader, options);
23+
24+
foreach (var element in propertyValue.ToJsonElements())
25+
{
26+
value.Properties.Add(element.Key, element.Value);
27+
}
28+
29+
return true;
30+
}
31+
32+
return false;
33+
}
34+
35+
protected override bool TryWriteExtensionData(Utf8JsonWriter writer, ConversationAccount value, string propertyName)
36+
{
37+
if (propertyName.Equals(nameof(value.Properties)))
38+
{
39+
foreach (var extensionData in value.Properties)
40+
{
41+
writer.WritePropertyName(extensionData.Key);
42+
extensionData.Value.WriteTo(writer);
43+
}
44+
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
}
51+
}

src/libraries/Core/Microsoft.Agents.Core/Serialization/ProtocolJsonSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private static JsonSerializerOptions ApplyCoreOptions(this JsonSerializerOptions
8585
options.Converters.Add(new AudioCardConverter());
8686
options.Converters.Add(new CardActionConverter());
8787
options.Converters.Add(new ChannelAccountConverter());
88+
options.Converters.Add(new ConversationAccountConverter());
8889
options.Converters.Add(new EntityConverter());
8990
options.Converters.Add(new AIEntityConverter());
9091
options.Converters.Add(new TokenExchangeInvokeResponseConverter());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Agents.Core.Models;
5+
using Microsoft.Agents.Core.Serialization;
6+
using Xunit;
7+
8+
namespace Microsoft.Agents.Model.Tests
9+
{
10+
public class ConversationAccountTests
11+
{
12+
[Fact]
13+
public void ConversationAccount_RoundTrip()
14+
{
15+
var jsonIn = "{\"isGroup\":true,\"conversationType\":\"convType\",\"tenantId\":\"tenant_id\",\"id\":\"id\",\"name\":\"convName\",\"aadObjectId\":\"aadObject_id\",\"role\":\"convRole\",\"custom1\":\"custom1Value\",\"custom2\":\"custom2Value\"}";
16+
17+
var conversationAccountIn = ProtocolJsonSerializer.ToObject<ConversationAccount>(jsonIn);
18+
19+
Assert.Equal(2, conversationAccountIn.Properties.Count);
20+
21+
var jsonOut = ProtocolJsonSerializer.ToJson(conversationAccountIn);
22+
Assert.Equal(jsonIn, jsonOut);
23+
}
24+
}
25+
}

src/tests/Microsoft.Agents.Model.Tests/ConversationReferenceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void ConversationReferenceRoundTrip()
2626
var outJson = ProtocolJsonSerializer.ToJson(outReference);
2727

2828
// Specifically, should include: "bot": {} and not "agent": {}
29-
var outExpected = "{\"activityId\":\"id\",\"user\":{\"id\":\"user\"},\"bot\":{\"id\":\"agent\"},\"conversation\":{\"id\":\"conversation\",\"properties\":{}},\"channelId\":\"channelId\",\"serviceUrl\":\"serviceUrl\",\"locale\":\"locale\"}";
29+
var outExpected = "{\"activityId\":\"id\",\"user\":{\"id\":\"user\"},\"bot\":{\"id\":\"agent\"},\"conversation\":{\"id\":\"conversation\"},\"channelId\":\"channelId\",\"serviceUrl\":\"serviceUrl\",\"locale\":\"locale\"}";
3030

3131
Assert.Equal(outExpected, outJson);
3232

@@ -58,7 +58,7 @@ public void InActivityConversationReferenceSerialize()
5858
};
5959

6060
var outJson = ProtocolJsonSerializer.ToJson(activity);
61-
var outExpected = "{\"relatesTo\":{\"activityId\":\"id\",\"user\":{\"id\":\"user\"},\"bot\":{\"id\":\"agent\"},\"conversation\":{\"id\":\"conversation\",\"properties\":{}},\"channelId\":\"channelId\",\"serviceUrl\":\"serviceUrl\",\"locale\":\"locale\"}}";
61+
var outExpected = "{\"relatesTo\":{\"activityId\":\"id\",\"user\":{\"id\":\"user\"},\"bot\":{\"id\":\"agent\"},\"conversation\":{\"id\":\"conversation\"},\"channelId\":\"channelId\",\"serviceUrl\":\"serviceUrl\",\"locale\":\"locale\"}}";
6262
Assert.Equal(outExpected, outJson);
6363
}
6464
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"type":"event","id":"470d790f-dcfb-4e7e-9d38-fdae48fcdba0","timestamp":"2024-11-14T03:21:53.5114019\u002B00:00","channelId":"pva-published-engine-direct","from":{"id":"9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab","name":"crd17_getsCurrentWeather","role":"bot"},"conversation":{"id":"fe5203d5-03ce-44f1-8fb7-d26bf06ee40f","properties":{}},"recipient":{"id":"94219457-c13e-44ca-b91c-24078a4997c6","aadObjectId":"94219457-c13e-44ca-b91c-24078a4997c6","role":"user"},"channelData":{"streamType":"final","streamId":"e5084817-2e0d-4fca-bb27-3eca80ae49f8"},"replyToId":"406ffa1e-67a7-4ec9-9eef-f90cfbe0e389","valueType":"DynamicPlanStepBindUpdate","value":{"taskDialogId":"crd17_getsCurrentWeather.topic.MSNWeatherGetcurrentweather","arguments":{"units":"I","Location":"New York"}},"name":"DynamicPlanStepBindUpdate"}
1+
{"type":"event","id":"470d790f-dcfb-4e7e-9d38-fdae48fcdba0","timestamp":"2024-11-14T03:21:53.5114019\u002B00:00","channelId":"pva-published-engine-direct","from":{"id":"9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab","name":"crd17_getsCurrentWeather","role":"bot"},"conversation":{"id":"fe5203d5-03ce-44f1-8fb7-d26bf06ee40f"},"recipient":{"id":"94219457-c13e-44ca-b91c-24078a4997c6","aadObjectId":"94219457-c13e-44ca-b91c-24078a4997c6","role":"user"},"channelData":{"streamType":"final","streamId":"e5084817-2e0d-4fca-bb27-3eca80ae49f8"},"replyToId":"406ffa1e-67a7-4ec9-9eef-f90cfbe0e389","valueType":"DynamicPlanStepBindUpdate","value":{"taskDialogId":"crd17_getsCurrentWeather.topic.MSNWeatherGetcurrentweather","arguments":{"units":"I","Location":"New York"}},"name":"DynamicPlanStepBindUpdate"}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"type":"message","id":"8a82a3f3-cddf-444b-b607-58c79f3486b8","timestamp":"2024-11-14T03:18:57.5507994\u002B00:00","channelId":"pva-published-engine-direct","from":{"id":"9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab","name":"crd17_getsCurrentWeather","role":"bot"},"conversation":{"id":"fe5203d5-03ce-44f1-8fb7-d26bf06ee40f","properties":{}},"recipient":{"id":"94219457-c13e-44ca-b91c-24078a4997c6","aadObjectId":"94219457-c13e-44ca-b91c-24078a4997c6","role":"user"},"textFormat":"markdown","text":"Hello, I\u0027m Gets Current weather, a virtual assistant. I can answer general questions about the current weather as well as forecasts for today and tomorrow. Just so you are aware, I sometimes use AI to answer your questions.\n\n_**Note**: You can now customize this copilot\u0027s topics and knowledge for your own needs. For more information about this template and how to modify it, visit the template [documentation.](https://go.microsoft.com/fwlink/?linkid=2271069)_","speak":"Thanks for calling, how can I help?","inputHint":"acceptingInput","suggestedActions":{"actions":[{"type":"imBack","title":"What can I ask?","text":"What can I ask?","value":"What can I ask?"}]},"channelData":{"streamType":"final","streamId":"8a82a3f3-cddf-444b-b607-58c79f3486b8"},"replyToId":"b7b28891-a1a4-460d-ad37-11326f46fead"}
1+
{"type":"message","id":"8a82a3f3-cddf-444b-b607-58c79f3486b8","timestamp":"2024-11-14T03:18:57.5507994\u002B00:00","channelId":"pva-published-engine-direct","from":{"id":"9c73e213-d1a5-edcd-9eee-2db79aadb10f/806d3d4a-5c95-ef11-8a6a-6045bd03ceab","name":"crd17_getsCurrentWeather","role":"bot"},"conversation":{"id":"fe5203d5-03ce-44f1-8fb7-d26bf06ee40f"},"recipient":{"id":"94219457-c13e-44ca-b91c-24078a4997c6","aadObjectId":"94219457-c13e-44ca-b91c-24078a4997c6","role":"user"},"textFormat":"markdown","text":"Hello, I\u0027m Gets Current weather, a virtual assistant. I can answer general questions about the current weather as well as forecasts for today and tomorrow. Just so you are aware, I sometimes use AI to answer your questions.\n\n_**Note**: You can now customize this copilot\u0027s topics and knowledge for your own needs. For more information about this template and how to modify it, visit the template [documentation.](https://go.microsoft.com/fwlink/?linkid=2271069)_","speak":"Thanks for calling, how can I help?","inputHint":"acceptingInput","suggestedActions":{"actions":[{"type":"imBack","title":"What can I ask?","text":"What can I ask?","value":"What can I ask?"}]},"channelData":{"streamType":"final","streamId":"8a82a3f3-cddf-444b-b607-58c79f3486b8"},"replyToId":"b7b28891-a1a4-460d-ad37-11326f46fead"}

src/tests/Microsoft.Agents.Model.Tests/Resources/cps_suggestedactions_out.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"role": "bot"
1010
},
1111
"conversation": {
12-
"id": "fe5203d5-03ce-44f1-8fb7-d26bf06ee40f",
13-
"properties": {}
12+
"id": "fe5203d5-03ce-44f1-8fb7-d26bf06ee40f"
1413
},
1514
"recipient": {
1615
"id": "94219457-c13e-44ca-b91c-24078a4997c6",

0 commit comments

Comments
 (0)