diff --git a/examples/dotnet/dotnet-03-simple-chatbot/ConnectorExtensions.cs b/examples/dotnet/dotnet-03-simple-chatbot/ConnectorExtensions.cs new file mode 100644 index 00000000..882df91a --- /dev/null +++ b/examples/dotnet/dotnet-03-simple-chatbot/ConnectorExtensions.cs @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Text; +using Microsoft.SemanticKernel.ChatCompletion; +using Microsoft.SemanticWorkbench.Connector; + +namespace AgentExample; + +public static class ConnectorExtensions +{ + // TODO: the list of participants is incomplete, because agents see only participants being added + public static string GetParticipantName(this Conversation conversation, string id) + { + if (conversation.Participants.TryGetValue(id, out Participant? participant)) + { + return participant.Name; + } + + return "Unknown"; + } + + public static ChatHistory ToSemanticKernelChatHistory( + this Conversation conversation, + string assistantId, + string systemPrompt) + { + var result = new ChatHistory(systemPrompt); + + foreach (Message msg in conversation.Messages) + { + if (msg.Sender.Id == assistantId) + { + result.AddAssistantMessage(msg.Content!); + } + else + { + result.AddUserMessage( + $"[{conversation.GetParticipantName(msg.Sender.Id)}] {msg.Content}"); + } + } + + return result; + } + + public static string ToHtmlString( + this Conversation conversation, + string assistantId) + { + var result = new StringBuilder(); + result.AppendLine(""); + result.AppendLine("
");
+ if (msg.Sender.Id == assistantId)
+ {
+ result.AppendLine("Assistant
");
+ }
+ else
+ {
+ result
+ .Append("")
+ .Append(conversation.GetParticipantName(msg.Sender.Id))
+ .AppendLine("
");
+ }
+
+ result.AppendLine(msg.Content).AppendLine("
");
- if (msg.Sender.Id == assistantId)
- {
- result.AppendLine("Assistant
");
- }
- else
- {
- result
- .Append("")
- .Append(this.GetParticipantName(msg.Sender.Id))
- .AppendLine("
");
- }
-
- result.AppendLine(msg.Content).AppendLine("