Merge pull request #195 from ywang1110/add_richContent

Add and Default RichContent for UI Rendering Purpose
This commit is contained in:
Haiping 2023-10-28 21:18:59 -05:00 committed by GitHub
commit 2146f6e95e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 134 additions and 8 deletions

View file

@ -31,8 +31,4 @@
<PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Messaging\Models\" />
</ItemGroup>
</Project>

View file

@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Conversations;
/// </summary>
public interface IConversationStateService
{
string GetConversationId();
ConversationState Load(string conversationId);
string GetState(string name, string defaultValue = "");
bool ContainsState(string name);

View file

@ -32,6 +32,8 @@ public class RoleDialogModel : ITrackableMessage
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Data { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? RichContent { get; set; }
/// <summary>
/// Stop conversation completion

View file

@ -0,0 +1,15 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class QuickReplyElement
{
[JsonPropertyName("content_type")]
public string ContentType { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string? Payload { get; set; }
[JsonPropertyName("image_url")]
public string? ImageUrl { get; set; }
[JsonPropertyName("postback_url")]
public string? PostBackUrl { get; set; }
}
}

View file

@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class QuickReplyMessage : TextMessage
{
[JsonPropertyName("quick_replies")]
public List<QuickReplyElement> QuickReplies { get; set; } = new List<QuickReplyElement>();
}
}

View file

@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class Recipient
{
public string Id { get; set; } = string.Empty;
}
}

View file

@ -0,0 +1,13 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class RichContent<T>
{
public Recipient Recipient { get; set; } = new Recipient();
/// <summary>
/// RESPONSE
/// </summary>
[JsonPropertyName("messaging_type")]
public string MessagingType => "RESPONSE";
public T Message { get; set; }
}
}

View file

@ -0,0 +1,15 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class SenderActionMessage
{
/*
* Requests to display sender action should only include the sender_action parameter and the recipient object.
* All other Send API properties, such as text and templates, should be sent in a separate request.
*/
public Recipient Recipient { get; set; } = new Recipient();
[JsonPropertyName("sender_action")]
public string SenderAction { get; set; } = string.Empty;
}
}

View file

@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class AttachmentTemplate<T>
{
public string Type => "template";
public T Payload { get; set; }
}
}

View file

@ -0,0 +1,17 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class ButtonTemplate : TextMessage
{
[JsonPropertyName("template_type")]
public string TemplateType => "button";
public List<ButtonElement> Buttons { get; set; } = new List<ButtonElement>();
}
public class ButtonElement
{
public string Type { get; set; } = string.Empty;
public string? Url { get; set; }
public string Title { get; set; } = string.Empty;
}
}

View file

@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class CommonTemplate<T>
{
[JsonPropertyName("template_type")]
public string TemplateType { get; set; } = string.Empty;
public T Elements { get; set; }
}
}

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
{
public class TemplateMessage<T>
{
public T Attachment { get; set; }
}
}

View file

@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.Messaging.Models.RichContent
{
public class TextMessage
{
public string Text { get; set; } = string.Empty;
}
}

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
using System.Drawing;
@ -100,6 +101,14 @@ public partial class ConversationService
_logger.LogInformation(text);
#endif
// Only read content from RichContent for UI rendering. When richContent is null, create a basic text message for richContent.
var state = _services.GetRequiredService<IConversationStateService>();
message.RichContent = message.RichContent ?? new RichContent<TextMessage>
{
Recipient = new Recipient { Id = state.GetConversationId() },
Message = new TextMessage { Text = message.Content }
};
var hooks = _services.GetServices<IConversationHook>().ToList();
foreach (var hook in hooks)
{

View file

@ -28,6 +28,8 @@ public class ConversationStateService : IConversationStateService, IDisposable
_states = new ConversationState();
}
public string GetConversationId() => _conversationId;
public IConversationStateService SetState<T>(string name, T value)
{
if (value == null)

View file

@ -12,7 +12,7 @@ public class ConversationController : ControllerBase, IApiAdapter
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
public ConversationController(IServiceProvider services,
public ConversationController(IServiceProvider services,
IUserIdentity user)
{
_services = services;
@ -40,8 +40,8 @@ public class ConversationController : ControllerBase, IApiAdapter
}
[HttpPost("/conversation/{agentId}/{conversationId}")]
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
[FromRoute] string conversationId,
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
[FromRoute] string conversationId,
[FromBody] NewMessageModel input)
{
var conv = _services.GetRequiredService<IConversationService>();
@ -73,6 +73,7 @@ public class ConversationController : ControllerBase, IApiAdapter
response.Data = inputMsg.Data;
response.Function = inputMsg.FunctionName;
response.Instruction = inputMsg.Instruction;
response.RichContent = inputMsg.RichContent;
return response;
}

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
@ -11,4 +10,5 @@ public class MessageResponseModel : ITrackableMessage
public string Function { get; set; }
public object Data { get; set; }
public FunctionCallFromLlm Instruction { get; set; }
public object? RichContent { get; set; }
}