Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace BotSharp.Abstraction.Conversations;

/// <summary>
/// Puts an indication into the conversation's language before it is pushed. Indications skip
/// TranslationResponseHook -- they go out ahead of the function, where an LLM translation would
/// hold up the very line meant to say "working on it" -- so the host supplies fixed wording instead.
/// Optional: with none registered, indications go out as the function wrote them.
/// </summary>
public interface IIndicationLocalizer
{
/// <param name="language">The conversation's <c>StateConst.LANGUAGE</c>, e.g. <c>LanguageType.CHINESE</c>.</param>
/// <returns>The wording for <paramref name="language"/>, or <paramref name="indication"/> unchanged when there is none.</returns>
string Localize(string indication, string language);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ namespace BotSharp.Core.MessageHub;
public sealed class ConversationHub
{
private readonly MessageHub<HubObserveData<RoleDialogModel>> _hub;
private readonly Func<string, string>? _localize;

internal ConversationHub(MessageHub<HubObserveData<RoleDialogModel>> hub, string? conversationId)
internal ConversationHub(MessageHub<HubObserveData<RoleDialogModel>> hub, string? conversationId,
Func<string, string>? localize = null)
{
_hub = hub;
ConversationId = conversationId;
_localize = localize;
}

/// <summary>Where every push goes; null when taken outside a conversation.</summary>
Expand All @@ -30,7 +33,7 @@ public void PushIndication(RoleDialogModel message, string? indication)
return;
}

message.Indication = indication;
message.Indication = _localize?.Invoke(indication) ?? indication;

_hub.Push(new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Infrastructures.Enums;

namespace BotSharp.Core.MessageHub;

public static class MessageHubExtensions
Expand All @@ -9,6 +11,12 @@ public static ConversationHub GetHub(this IServiceProvider services)
{
var conv = services.GetRequiredService<IConversationService>();
var hub = services.GetRequiredService<MessageHub<HubObserveData<RoleDialogModel>>>();
return new ConversationHub(hub, conv.ConversationId);

// Resolved now, not at push time: a hub may be kept and pushed from a transport's own thread.
var localizer = services.GetService<IIndicationLocalizer>();
var language = conv.States.GetState(StateConst.LANGUAGE, LanguageType.ENGLISH);
Func<string, string>? localize = localizer == null ? null : text => localizer.Localize(text, language);

return new ConversationHub(hub, conv.ConversationId, localize);
}
}
Loading