BotSharp/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

178 lines
5.5 KiB
C#
Raw Normal View History

2024-10-07 21:35:39 +00:00
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Functions.Models;
2023-11-24 23:26:33 +00:00
using BotSharp.Abstraction.Messaging;
2023-11-08 22:27:42 +00:00
using BotSharp.Abstraction.Messaging.Models.RichContent;
2024-12-05 17:11:16 +00:00
using System.Diagnostics;
2023-10-27 15:18:48 +00:00
2023-06-27 18:31:13 +00:00
namespace BotSharp.Abstraction.Conversations.Models;
2024-12-05 17:11:16 +00:00
[DebuggerStepThrough]
2023-10-27 15:18:48 +00:00
public class RoleDialogModel : ITrackableMessage
2023-06-27 18:31:13 +00:00
{
2023-10-30 16:48:18 +00:00
/// <summary>
/// If Role is Assistant, it is same as user's message id.
/// </summary>
2023-10-27 15:18:48 +00:00
public string MessageId { get; set; }
2024-10-07 21:35:39 +00:00
/// <summary>
/// The message type
/// </summary>
public string MessageType { get; set; } = MessageTypeName.Plain;
2023-06-27 18:31:13 +00:00
/// <summary>
/// user, system, assistant, function
2023-06-27 18:31:13 +00:00
/// </summary>
public string Role { get; set; }
2023-11-14 14:13:54 +00:00
/// <summary>
/// User id when Role is User
/// </summary>
public string SenderId { get; set; }
[JsonPropertyName("created_at")]
2023-08-07 20:17:54 +00:00
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
2023-11-14 14:13:54 +00:00
2023-07-21 20:15:09 +00:00
public string Content { get; set; }
2023-10-18 11:58:36 +00:00
2024-04-18 21:34:18 +00:00
public string? SecondaryContent { get; set; }
2024-05-07 16:55:44 +00:00
2024-05-07 22:16:07 +00:00
/// <summary>
2024-09-04 15:01:40 +00:00
/// Postback of previous round payload
2024-05-07 22:16:07 +00:00
/// </summary>
2024-05-07 16:55:44 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("payload")]
public string? Payload { get; set; }
2024-04-18 21:34:18 +00:00
2024-04-30 21:25:58 +00:00
/// <summary>
/// Indicator message used to provide UI feedback for function execution
/// </summary>
public string? Indication { get; set; }
2023-10-18 11:58:36 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2023-08-17 04:04:23 +00:00
public string CurrentAgentId { get; set; }
2023-06-27 18:31:13 +00:00
/// <summary>
/// Function name if LLM response function call
/// </summary>
2023-10-18 11:58:36 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FunctionName { get; set; }
2023-07-27 21:56:57 +00:00
2024-05-02 22:07:12 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ToolCallId { get; set; }
2024-04-06 23:33:49 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? PostbackFunctionName { get; set; }
2023-10-18 11:58:36 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2023-08-17 04:04:23 +00:00
public string? FunctionArgs { get; set; }
2025-04-20 23:24:13 +00:00
/// <summary>
/// Set this flag is in OnFunctionExecuting, if true, it won't be executed by InvokeFunction.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public bool Handled { get; set; } = false;
2023-08-20 20:20:16 +00:00
/// <summary>
/// Function execution structured data, this data won't pass to LLM.
/// It's ideal to render in rich content in UI.
/// </summary>
2023-10-18 11:58:36 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? Data { get; set; }
2024-03-08 13:19:13 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string ImageUrl { get; set; }
/// <summary>
/// Remember to set Message.Content as well
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2023-11-24 23:26:33 +00:00
public RichContent<IRichMessage>? RichContent { get; set; }
2023-08-20 20:20:16 +00:00
2024-08-29 20:00:28 +00:00
/// <summary>
/// Rich content for secondary language
/// </summary>
2024-04-18 21:34:18 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public RichContent<IRichMessage>? SecondaryRichContent { get; set; }
2023-08-31 02:09:38 +00:00
/// <summary>
/// Stop conversation completion
/// </summary>
2023-10-18 11:58:36 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
2023-08-31 02:09:38 +00:00
public bool StopCompletion { get; set; }
public FunctionCallFromLlm Instruction { get; set; }
2024-08-29 20:00:28 +00:00
/// <summary>
/// Files to be used in conversation
/// </summary>
2025-08-19 18:37:34 +00:00
public List<BotSharpFile>? Files { get; set; }
2024-05-03 19:57:44 +00:00
2024-07-01 16:03:42 +00:00
/// <summary>
/// The images generated by AI
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("generated_images")]
2025-08-19 18:37:34 +00:00
public List<ImageGeneration>? GeneratedImages { get; set; }
/// <summary>
/// The web annotations generated by AI
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("annotations")]
public List<ChatAnnotation>? Annotations { get; set; }
2024-08-29 20:00:28 +00:00
2025-03-05 23:22:46 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string RenderedInstruction { get; set; } = string.Empty;
2024-10-07 21:35:39 +00:00
2025-06-24 02:43:25 +00:00
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public bool IsStreaming { get; set; }
2025-07-29 18:55:53 +00:00
public RoleDialogModel()
2023-10-27 15:18:48 +00:00
{
}
public RoleDialogModel(string role, string text)
{
Role = role;
2023-07-21 20:15:09 +00:00
Content = text;
2023-10-27 15:18:48 +00:00
MessageId = Guid.NewGuid().ToString();
2024-10-07 21:35:39 +00:00
MessageType = MessageTypeName.Plain;
}
2023-06-27 18:31:13 +00:00
public override string ToString()
{
2023-08-18 04:27:07 +00:00
if (Role == AgentRole.Function)
{
return $"{Role}: {Content} <= {FunctionName}({FunctionArgs})";
2023-08-18 04:27:07 +00:00
}
else
{
return $"{Role}: {Content}";
}
2023-06-27 18:31:13 +00:00
}
2023-10-30 20:06:45 +00:00
public static RoleDialogModel From(RoleDialogModel source,
string? role = null,
string? content = null)
{
return new RoleDialogModel(role ?? source.Role, content ?? source.Content)
{
CurrentAgentId = source.CurrentAgentId,
MessageId = source.MessageId,
2024-10-07 21:35:39 +00:00
MessageType = source.MessageType,
2023-10-30 20:06:45 +00:00
FunctionArgs = source.FunctionArgs,
FunctionName = source.FunctionName,
2024-05-02 22:07:12 +00:00
ToolCallId = source.ToolCallId,
2025-01-09 22:30:25 +00:00
Indication = source.Indication,
2024-04-06 23:33:49 +00:00
PostbackFunctionName = source.PostbackFunctionName,
2023-10-30 20:06:45 +00:00
RichContent = source.RichContent,
2024-07-31 10:29:10 +00:00
Payload = source.Payload,
2023-10-30 20:06:45 +00:00
StopCompletion = source.StopCompletion,
Instruction = source.Instruction,
2025-06-24 02:43:25 +00:00
Data = source.Data,
2025-08-19 18:37:34 +00:00
IsStreaming = source.IsStreaming,
Annotations = source.Annotations
2023-10-30 20:06:45 +00:00
};
}
2023-06-27 18:31:13 +00:00
}