refine element button

This commit is contained in:
Jicheng Lu 2024-04-02 14:09:42 -05:00
parent 7ae9e3ef60
commit 8168b22707
11 changed files with 62 additions and 51 deletions

View file

@ -22,4 +22,10 @@ public class StateChangeModel
[JsonPropertyName("after_active_rounds")]
public int? AfterActiveRounds { get; set; }
[JsonPropertyName("data_type")]
public string DataType { get; set; }
[JsonPropertyName("source")]
public string Source { get; set; }
}

View file

@ -1,20 +1,19 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using System.Text.Json;
using System.Reflection;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace BotSharp.Core.Messaging;
namespace BotSharp.Abstraction.Messaging;
public static class BotSharpMessageParser
{
public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options)
public static IRichMessage? ParseRichMessage(JsonElement root)
{
IRichMessage? res = null;
Type? targetType = null;
JsonElement element;
var jsonText = root.GetRawText();
@ -23,23 +22,23 @@ public static class BotSharpMessageParser
var richType = element.GetString();
if (richType == RichTypeEnum.ButtonTemplate)
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
targetType = typeof(ButtonTemplateMessage);
}
else if (richType == RichTypeEnum.MultiSelectTemplate)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
targetType = typeof(MultiSelectTemplateMessage);
}
else if (richType == RichTypeEnum.QuickReply)
{
res = JsonSerializer.Deserialize<QuickReplyMessage>(jsonText, options);
targetType = typeof(QuickReplyMessage);
}
else if (richType == RichTypeEnum.CouponTemplate)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
targetType = typeof(CouponTemplateMessage);
}
else if (richType == RichTypeEnum.Text)
{
res = JsonSerializer.Deserialize<TextMessage>(jsonText, options);
targetType = typeof(TextMessage);
}
else if (richType == RichTypeEnum.GenericTemplate)
{
@ -51,19 +50,24 @@ public static class BotSharpMessageParser
if (wrapperType != null && genericType != null)
{
var targetType = wrapperType.MakeGenericType(genericType);
res = JsonConvert.DeserializeObject(jsonText, targetType) as IRichMessage;
targetType = wrapperType.MakeGenericType(genericType);
}
}
}
}
if (targetType != null)
{
res = JsonConvert.DeserializeObject(jsonText, targetType) as IRichMessage;
}
return res;
}
public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options)
public static ITemplateMessage? ParseTemplateMessage(JsonElement root)
{
ITemplateMessage? res = null;
Type? targetType = null;
JsonElement element;
var jsonText = root.GetRawText();
@ -72,19 +76,19 @@ public static class BotSharpMessageParser
var templateType = element.GetString();
if (templateType == TemplateTypeEnum.Button)
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
targetType = typeof(ButtonTemplateMessage);
}
else if (templateType == TemplateTypeEnum.MultiSelect)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
targetType = typeof(MultiSelectTemplateMessage);
}
else if (templateType == TemplateTypeEnum.Coupon)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
targetType = typeof(CouponTemplateMessage);
}
else if (templateType == TemplateTypeEnum.Product)
{
res = JsonSerializer.Deserialize<ProductTemplateMessage>(jsonText, options);
targetType = typeof(ProductTemplateMessage);
}
else if (templateType == TemplateTypeEnum.Generic)
{
@ -96,13 +100,18 @@ public static class BotSharpMessageParser
if (wrapperType != null && genericType != null)
{
var targetType = wrapperType.MakeGenericType(genericType);
targetType = wrapperType.MakeGenericType(genericType);
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
}
}
}
}
if (targetType != null)
{
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
}
return res;
}
}

View file

@ -1,4 +1,3 @@
using BotSharp.Core.Messaging;
using System.Text.Json;
namespace BotSharp.Abstraction.Messaging.JsonConverters;
@ -9,7 +8,7 @@ public class RichContentJsonConverter : JsonConverter<IRichMessage>
{
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var root = jsonDoc.RootElement;
var res = BotSharpMessageParser.ParseRichMessage(root, options);
var res = BotSharpMessageParser.ParseRichMessage(root);
return res;
}

View file

@ -1,4 +1,3 @@
using BotSharp.Core.Messaging;
using System.Text.Json;
namespace BotSharp.Abstraction.Messaging.JsonConverters;
@ -9,7 +8,7 @@ public class TemplateMessageJsonConverter : JsonConverter<ITemplateMessage>
{
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var root = jsonDoc.RootElement;
var res = BotSharpMessageParser.ParseTemplateMessage(root, options);
var res = BotSharpMessageParser.ParseTemplateMessage(root);
return res;
}

View file

@ -5,13 +5,19 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent;
/// </summary>
public class ElementButton
{
public string Type { get; set; }
public string Type { get; set; } = "web_url";
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Url { get; set; }
public string Title { get; set; }
public string Title { get; set; } = string.Empty;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Payload { get; set; }
[JsonPropertyName("is_primary")]
public bool IsPrimary { get; set; }
[JsonPropertyName("is_secondary")]
public bool IsSecondary { get; set; }
}

View file

@ -17,27 +17,8 @@ public class ButtonTemplateMessage : IRichMessage, ITemplateMessage
public string TemplateType => TemplateTypeEnum.Button;
[JsonPropertyName("buttons")]
public ButtonElement[] Buttons { get; set; } = new ButtonElement[0];
public ElementButton[] Buttons { get; set; } = new ElementButton[0];
[JsonPropertyName("is_horizontal")]
public bool IsHorizontal { get; set; }
}
public class ButtonElement
{
/// <summary>
/// web_url, postback, phone_number
/// </summary>
public string Type { get; set; } = "web_url";
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Url { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Payload { get; set; }
public string Title { get; set; } = string.Empty;
[JsonPropertyName("is_primary")]
public bool IsPrimary { get; set; }
}

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Models;
namespace BotSharp.Core.Conversations.Services;
@ -126,6 +127,6 @@ public partial class ConversationService : IConversationService
{
_conversationId = conversationId;
_state.Load(_conversationId);
states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds));
states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
}
}

View file

@ -70,7 +70,9 @@ public class ConversationStateService : IConversationStateService, IDisposable
BeforeValue = preValue,
BeforeActiveRounds = preActiveRounds,
AfterValue = currentValue,
AfterActiveRounds = curActiveRounds
AfterActiveRounds = curActiveRounds,
DataType = valueType,
Source = source
}).Wait();
}
}

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.OpenAPI.Controllers;
@ -166,11 +165,11 @@ public class ConversationController : ControllerBase
routing.Context.SetMessageId(conversationId, inputMsg.MessageId);
conv.SetConversationId(conversationId, input.States);
conv.States.SetState("channel", input.Channel)
.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("temperature", input.Temperature)
.SetState("sampling_factor", input.SamplingFactor);
conv.States.SetState("channel", input.Channel, source: StateSource.External)
.SetState("provider", input.Provider, source: StateSource.External)
.SetState("model", input.Model, source: StateSource.External)
.SetState("temperature", input.Temperature, source: StateSource.External)
.SetState("sampling_factor", input.SamplingFactor, source: StateSource.External);
var response = new ChatResponseModel();

View file

@ -436,6 +436,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
BeforeActiveRounds = stateChange.BeforeActiveRounds,
AfterValue = stateChange.AfterValue,
AfterActiveRounds = stateChange.AfterActiveRounds,
DataType = stateChange.DataType,
Source = stateChange.Source,
CreateTime = DateTime.UtcNow
};

View file

@ -35,6 +35,9 @@ public class StateValueMongoElement
public string? MessageId { get; set; }
public bool Active { get; set; }
public int ActiveRounds { get; set; }
public string DataType { get; set; }
public string Source { get; set; }
public DateTime UpdateTime { get; set; }
public static StateValueMongoElement ToMongoElement(StateValue element)
@ -45,6 +48,8 @@ public class StateValueMongoElement
MessageId = element.MessageId,
Active = element.Active,
ActiveRounds = element.ActiveRounds,
DataType = element.DataType,
Source = element.Source,
UpdateTime = element.UpdateTime
};
}
@ -57,6 +62,8 @@ public class StateValueMongoElement
MessageId = element.MessageId,
Active = element.Active,
ActiveRounds = element.ActiveRounds,
DataType= element.DataType,
Source = element.Source,
UpdateTime = element.UpdateTime
};
}