|
| 1 | +using CrestApps.OrchardCore.AI.Models; |
| 2 | +using Microsoft.Extensions.Localization; |
| 3 | + |
| 4 | +namespace CrestApps.OrchardCore.AI; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Provides convenient extension methods for <see cref="IChatNotificationSender"/> |
| 8 | +/// to send common notification types without manually constructing <see cref="ChatNotification"/> objects. |
| 9 | +/// </summary> |
| 10 | +public static class ChatNotificationSenderExtensions |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Well-known notification IDs used by built-in notification types. |
| 14 | + /// </summary> |
| 15 | + public static class NotificationIds |
| 16 | + { |
| 17 | + public const string Typing = "typing"; |
| 18 | + public const string Transfer = "transfer"; |
| 19 | + public const string AgentConnected = "agent-connected"; |
| 20 | + public const string ConversationEnded = "conversation-ended"; |
| 21 | + public const string SessionEnded = "session-ended"; |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Well-known notification action names. |
| 26 | + /// </summary> |
| 27 | + public static class ActionNames |
| 28 | + { |
| 29 | + public const string CancelTransfer = "cancel-transfer"; |
| 30 | + public const string EndSession = "end-session"; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Shows a typing indicator bubble (e.g., "Mike is typing…"). |
| 35 | + /// </summary> |
| 36 | + /// <param name="sender">The notification sender.</param> |
| 37 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 38 | + /// <param name="chatType">The type of chat context.</param> |
| 39 | + /// <param name="T">The string localizer for translating user-facing messages.</param> |
| 40 | + /// <param name="agentName">Optional name of the agent who is typing.</param> |
| 41 | + public static Task ShowTypingAsync( |
| 42 | + this IChatNotificationSender sender, |
| 43 | + string sessionId, |
| 44 | + ChatContextType chatType, |
| 45 | + IStringLocalizer T, |
| 46 | + string agentName = null) |
| 47 | + { |
| 48 | + var content = string.IsNullOrEmpty(agentName) |
| 49 | + ? T["Agent is typing"].Value |
| 50 | + : T["{0} is typing", agentName].Value; |
| 51 | + |
| 52 | + return sender.SendAsync(sessionId, chatType, new ChatNotification |
| 53 | + { |
| 54 | + Id = NotificationIds.Typing, |
| 55 | + Type = "typing", |
| 56 | + Content = content, |
| 57 | + Icon = "fa-solid fa-ellipsis", |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Hides a previously shown typing indicator. |
| 63 | + /// </summary> |
| 64 | + /// <param name="sender">The notification sender.</param> |
| 65 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 66 | + /// <param name="chatType">The type of chat context.</param> |
| 67 | + public static Task HideTypingAsync( |
| 68 | + this IChatNotificationSender sender, |
| 69 | + string sessionId, |
| 70 | + ChatContextType chatType) |
| 71 | + { |
| 72 | + return sender.RemoveAsync(sessionId, chatType, NotificationIds.Typing); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Shows a transfer indicator bubble with optional wait time and cancel button. |
| 77 | + /// </summary> |
| 78 | + /// <param name="sender">The notification sender.</param> |
| 79 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 80 | + /// <param name="chatType">The type of chat context.</param> |
| 81 | + /// <param name="T">The string localizer for translating user-facing messages.</param> |
| 82 | + /// <param name="message">The transfer message. When <see langword="null"/>, a localized default is used.</param> |
| 83 | + /// <param name="estimatedWaitTime">Optional estimated wait time string (e.g., "2 minutes").</param> |
| 84 | + /// <param name="cancellable">Whether to show a cancel button. Defaults to <see langword="true"/>.</param> |
| 85 | + public static Task ShowTransferAsync( |
| 86 | + this IChatNotificationSender sender, |
| 87 | + string sessionId, |
| 88 | + ChatContextType chatType, |
| 89 | + IStringLocalizer T, |
| 90 | + string message = null, |
| 91 | + string estimatedWaitTime = null, |
| 92 | + bool cancellable = true) |
| 93 | + { |
| 94 | + var content = message ?? T["Transferring you to a live agent..."].Value; |
| 95 | + |
| 96 | + if (!string.IsNullOrEmpty(estimatedWaitTime)) |
| 97 | + { |
| 98 | + content += " " + T["Estimated wait: {0}.", estimatedWaitTime].Value; |
| 99 | + } |
| 100 | + |
| 101 | + var notification = new ChatNotification |
| 102 | + { |
| 103 | + Id = NotificationIds.Transfer, |
| 104 | + Type = "transfer", |
| 105 | + Content = content, |
| 106 | + Icon = "fa-solid fa-headset", |
| 107 | + }; |
| 108 | + |
| 109 | + if (cancellable) |
| 110 | + { |
| 111 | + notification.Actions = |
| 112 | + [ |
| 113 | + new ChatNotificationAction |
| 114 | + { |
| 115 | + Name = ActionNames.CancelTransfer, |
| 116 | + Label = T["Cancel Transfer"].Value, |
| 117 | + CssClass = "btn-outline-danger", |
| 118 | + Icon = "fa-solid fa-xmark", |
| 119 | + }, |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + if (!string.IsNullOrEmpty(estimatedWaitTime)) |
| 124 | + { |
| 125 | + notification.Metadata = new Dictionary<string, string> |
| 126 | + { |
| 127 | + ["estimatedWaitTime"] = estimatedWaitTime, |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + return sender.SendAsync(sessionId, chatType, notification); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Updates the transfer indicator with new information (e.g., updated wait time). |
| 136 | + /// </summary> |
| 137 | + /// <param name="sender">The notification sender.</param> |
| 138 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 139 | + /// <param name="chatType">The type of chat context.</param> |
| 140 | + /// <param name="localizer">The string localizer for translating user-facing messages.</param> |
| 141 | + /// <param name="message">The updated transfer message.</param> |
| 142 | + /// <param name="estimatedWaitTime">Optional updated estimated wait time.</param> |
| 143 | + /// <param name="cancellable">Whether to show a cancel button.</param> |
| 144 | + public static Task UpdateTransferAsync( |
| 145 | + this IChatNotificationSender sender, |
| 146 | + string sessionId, |
| 147 | + ChatContextType chatType, |
| 148 | + IStringLocalizer localizer, |
| 149 | + string message = null, |
| 150 | + string estimatedWaitTime = null, |
| 151 | + bool cancellable = true) |
| 152 | + { |
| 153 | + return ShowTransferAsync(sender, sessionId, chatType, localizer, message, estimatedWaitTime, cancellable); |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Hides a previously shown transfer indicator. |
| 158 | + /// </summary> |
| 159 | + /// <param name="sender">The notification sender.</param> |
| 160 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 161 | + /// <param name="chatType">The type of chat context.</param> |
| 162 | + public static Task HideTransferAsync( |
| 163 | + this IChatNotificationSender sender, |
| 164 | + string sessionId, |
| 165 | + ChatContextType chatType) |
| 166 | + { |
| 167 | + return sender.RemoveAsync(sessionId, chatType, NotificationIds.Transfer); |
| 168 | + } |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Shows an "agent connected" bubble indicating a live agent has joined the session. |
| 172 | + /// </summary> |
| 173 | + /// <param name="sender">The notification sender.</param> |
| 174 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 175 | + /// <param name="chatType">The type of chat context.</param> |
| 176 | + /// <param name="T">The string localizer for translating user-facing messages.</param> |
| 177 | + /// <param name="agentName">Optional name of the connected agent.</param> |
| 178 | + /// <param name="message">Optional custom message. When <see langword="null"/>, a localized default is used.</param> |
| 179 | + public static Task ShowAgentConnectedAsync( |
| 180 | + this IChatNotificationSender sender, |
| 181 | + string sessionId, |
| 182 | + ChatContextType chatType, |
| 183 | + IStringLocalizer T, |
| 184 | + string agentName = null, |
| 185 | + string message = null) |
| 186 | + { |
| 187 | + var content = message |
| 188 | + ?? (string.IsNullOrEmpty(agentName) |
| 189 | + ? T["You are now connected to a live agent."].Value |
| 190 | + : T["You are now connected to {0}.", agentName].Value); |
| 191 | + |
| 192 | + return sender.SendAsync(sessionId, chatType, new ChatNotification |
| 193 | + { |
| 194 | + Id = NotificationIds.AgentConnected, |
| 195 | + Type = "info", |
| 196 | + Content = content, |
| 197 | + Icon = "fa-solid fa-user-check", |
| 198 | + Dismissible = true, |
| 199 | + }); |
| 200 | + } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Hides a previously shown agent-connected notification. |
| 204 | + /// </summary> |
| 205 | + /// <param name="sender">The notification sender.</param> |
| 206 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 207 | + /// <param name="chatType">The type of chat context.</param> |
| 208 | + public static Task HideAgentConnectedAsync( |
| 209 | + this IChatNotificationSender sender, |
| 210 | + string sessionId, |
| 211 | + ChatContextType chatType) |
| 212 | + { |
| 213 | + return sender.RemoveAsync(sessionId, chatType, NotificationIds.AgentConnected); |
| 214 | + } |
| 215 | + |
| 216 | + /// <summary> |
| 217 | + /// Shows a "conversation ended" bubble. The user can still send messages |
| 218 | + /// via text or audio input, but this indicates the conversation mode has ended. |
| 219 | + /// </summary> |
| 220 | + /// <param name="sender">The notification sender.</param> |
| 221 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 222 | + /// <param name="chatType">The type of chat context.</param> |
| 223 | + /// <param name="T">The string localizer for translating user-facing messages.</param> |
| 224 | + /// <param name="message">The message to display. When <see langword="null"/>, a localized default is used.</param> |
| 225 | + public static Task ShowConversationEndedAsync( |
| 226 | + this IChatNotificationSender sender, |
| 227 | + string sessionId, |
| 228 | + ChatContextType chatType, |
| 229 | + IStringLocalizer T, |
| 230 | + string message = null) |
| 231 | + { |
| 232 | + return sender.SendAsync(sessionId, chatType, new ChatNotification |
| 233 | + { |
| 234 | + Id = NotificationIds.ConversationEnded, |
| 235 | + Type = "ended", |
| 236 | + Content = message ?? T["Conversation ended."].Value, |
| 237 | + Icon = "fa-solid fa-circle-check", |
| 238 | + Dismissible = true, |
| 239 | + }); |
| 240 | + } |
| 241 | + |
| 242 | + /// <summary> |
| 243 | + /// Shows a "session ended" bubble and optionally allows ending the session programmatically. |
| 244 | + /// </summary> |
| 245 | + /// <param name="sender">The notification sender.</param> |
| 246 | + /// <param name="sessionId">The session or interaction identifier.</param> |
| 247 | + /// <param name="chatType">The type of chat context.</param> |
| 248 | + /// <param name="T">The string localizer for translating user-facing messages.</param> |
| 249 | + /// <param name="message">The message to display. When <see langword="null"/>, a localized default is used.</param> |
| 250 | + public static Task ShowSessionEndedAsync( |
| 251 | + this IChatNotificationSender sender, |
| 252 | + string sessionId, |
| 253 | + ChatContextType chatType, |
| 254 | + IStringLocalizer T, |
| 255 | + string message = null) |
| 256 | + { |
| 257 | + return sender.SendAsync(sessionId, chatType, new ChatNotification |
| 258 | + { |
| 259 | + Id = NotificationIds.SessionEnded, |
| 260 | + Type = "ended", |
| 261 | + Content = message ?? T["This chat session has ended."].Value, |
| 262 | + Icon = "fa-solid fa-circle-check", |
| 263 | + Dismissible = true, |
| 264 | + }); |
| 265 | + } |
| 266 | +} |
0 commit comments