Refactor scripted response decision helpers
This commit is contained in:
@@ -562,7 +562,7 @@ public sealed class JiboInteractionService(
|
|||||||
"robot_what_do_you_eat" => new JiboInteractionDecision(
|
"robot_what_do_you_eat" => new JiboInteractionDecision(
|
||||||
"robot_what_do_you_eat",
|
"robot_what_do_you_eat",
|
||||||
"The only thing I consume is electricity.",
|
"The only thing I consume is electricity.",
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates()),
|
ContextUpdates: ScriptedResponseDecisionBuilder.BuildScriptedResponseContextUpdates()),
|
||||||
"robot_where_do_you_live" => BuildScriptedPersonalityDecision(
|
"robot_where_do_you_live" => BuildScriptedPersonalityDecision(
|
||||||
catalog,
|
catalog,
|
||||||
"robot_where_do_you_live",
|
"robot_where_do_you_live",
|
||||||
@@ -912,7 +912,7 @@ public sealed class JiboInteractionService(
|
|||||||
"robot_what_are_you_made_of" => new JiboInteractionDecision(
|
"robot_what_are_you_made_of" => new JiboInteractionDecision(
|
||||||
"robot_what_are_you_made_of",
|
"robot_what_are_you_made_of",
|
||||||
"Let's see, I'm made of wires, motors, belts, gears, processors, cameras, and one baboon's heart in the middle of my body casing. I'm kidding about the baboon part, but everything else is true.",
|
"Let's see, I'm made of wires, motors, belts, gears, processors, cameras, and one baboon's heart in the middle of my body casing. I'm kidding about the baboon part, but everything else is true.",
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates()),
|
ContextUpdates: ScriptedResponseDecisionBuilder.BuildScriptedResponseContextUpdates()),
|
||||||
"good_morning" => BuildReactiveGreetingDecision(turn, "good_morning", referenceLocalTime),
|
"good_morning" => BuildReactiveGreetingDecision(turn, "good_morning", referenceLocalTime),
|
||||||
"good_afternoon" => BuildReactiveGreetingDecision(turn, "good_afternoon", referenceLocalTime),
|
"good_afternoon" => BuildReactiveGreetingDecision(turn, "good_afternoon", referenceLocalTime),
|
||||||
"good_evening" => BuildReactiveGreetingDecision(turn, "good_evening", referenceLocalTime),
|
"good_evening" => BuildReactiveGreetingDecision(turn, "good_evening", referenceLocalTime),
|
||||||
@@ -1507,7 +1507,7 @@ public sealed class JiboInteractionService(
|
|||||||
return new JiboInteractionDecision(
|
return new JiboInteractionDecision(
|
||||||
"current_location",
|
"current_location",
|
||||||
$"We're at {NormalizeLocationForSpeech(locationName)} if I'm not mistaken.",
|
$"We're at {NormalizeLocationForSpeech(locationName)} if I'm not mistaken.",
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
ContextUpdates: ScriptedResponseDecisionBuilder.BuildScriptedResponseContextUpdates());
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<JiboInteractionDecision> BuildWeatherReportDecisionAsync(
|
private async Task<JiboInteractionDecision> BuildWeatherReportDecisionAsync(
|
||||||
@@ -2826,10 +2826,11 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
return new JiboInteractionDecision(
|
return ScriptedResponseDecisionBuilder.BuildScriptedPersonalityDecision(
|
||||||
|
catalog,
|
||||||
|
randomizer,
|
||||||
intentName,
|
intentName,
|
||||||
SelectLegacyPersonalityReply(catalog, preferredSnippets),
|
preferredSnippets);
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiboInteractionDecision BuildScriptedFavoriteAnimalDecision(
|
private JiboInteractionDecision BuildScriptedFavoriteAnimalDecision(
|
||||||
@@ -2837,10 +2838,11 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
return new JiboInteractionDecision(
|
return ScriptedResponseDecisionBuilder.BuildScriptedFavoriteAnimalDecision(
|
||||||
|
catalog,
|
||||||
|
randomizer,
|
||||||
intentName,
|
intentName,
|
||||||
SelectLegacyReply(catalog.FavoriteAnimalReplies, preferredSnippets),
|
preferredSnippets);
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiboInteractionDecision BuildScriptedGreetingDecision(
|
private JiboInteractionDecision BuildScriptedGreetingDecision(
|
||||||
@@ -2848,10 +2850,11 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
return new JiboInteractionDecision(
|
return ScriptedResponseDecisionBuilder.BuildScriptedGreetingDecision(
|
||||||
|
catalog,
|
||||||
|
randomizer,
|
||||||
intentName,
|
intentName,
|
||||||
SelectLegacyGreetingReply(catalog, preferredSnippets),
|
preferredSnippets);
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiboInteractionDecision BuildScriptedHolidayDecision(
|
private JiboInteractionDecision BuildScriptedHolidayDecision(
|
||||||
@@ -2859,10 +2862,11 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
return new JiboInteractionDecision(
|
return ScriptedResponseDecisionBuilder.BuildScriptedHolidayDecision(
|
||||||
|
replies,
|
||||||
|
randomizer,
|
||||||
intentName,
|
intentName,
|
||||||
SelectLegacyReply(replies, preferredSnippets),
|
preferredSnippets);
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiboInteractionDecision BuildScriptedHolidayTrackerDecision(
|
private JiboInteractionDecision BuildScriptedHolidayTrackerDecision(
|
||||||
@@ -2870,10 +2874,11 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
return new JiboInteractionDecision(
|
return ScriptedResponseDecisionBuilder.BuildScriptedHolidayTrackerDecision(
|
||||||
|
catalog,
|
||||||
|
randomizer,
|
||||||
intentName,
|
intentName,
|
||||||
SelectLegacyReply(catalog.HolidayTrackerReplies, preferredSnippets),
|
preferredSnippets);
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiboInteractionDecision BuildScriptedHolidayGreetingDecision(
|
private JiboInteractionDecision BuildScriptedHolidayGreetingDecision(
|
||||||
@@ -2881,10 +2886,11 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
return new JiboInteractionDecision(
|
return ScriptedResponseDecisionBuilder.BuildScriptedHolidayGreetingDecision(
|
||||||
|
catalog,
|
||||||
|
randomizer,
|
||||||
intentName,
|
intentName,
|
||||||
SelectLegacyReply(catalog.HolidayGreetingReplies, preferredSnippets),
|
preferredSnippets);
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiboInteractionDecision BuildScriptedHolidayTemplateDecision(
|
private JiboInteractionDecision BuildScriptedHolidayTemplateDecision(
|
||||||
@@ -2894,63 +2900,29 @@ public sealed class JiboInteractionService(
|
|||||||
string intentName,
|
string intentName,
|
||||||
params string[] preferredSnippets)
|
params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
var selected = SelectLegacyReply(catalog.HolidayReplies, preferredSnippets);
|
var selected = ScriptedResponseDecisionBuilder.SelectLegacyReply(
|
||||||
|
catalog.HolidayReplies,
|
||||||
|
randomizer,
|
||||||
|
preferredSnippets);
|
||||||
return new JiboInteractionDecision(
|
return new JiboInteractionDecision(
|
||||||
intentName,
|
intentName,
|
||||||
RenderHolidayTemplate(selected, turn, presence),
|
RenderHolidayTemplate(selected, turn, presence),
|
||||||
ContextUpdates: BuildScriptedResponseContextUpdates());
|
ContextUpdates: ScriptedResponseDecisionBuilder.BuildScriptedResponseContextUpdates());
|
||||||
}
|
|
||||||
|
|
||||||
private static IDictionary<string, object?> BuildScriptedResponseContextUpdates()
|
|
||||||
{
|
|
||||||
return new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase)
|
|
||||||
{
|
|
||||||
[ChitchatStateMachine.StateMetadataKey] = "complete",
|
|
||||||
[ChitchatStateMachine.RouteMetadataKey] = "ScriptedResponse",
|
|
||||||
[ChitchatStateMachine.EmotionMetadataKey] = string.Empty
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string SelectLegacyPersonalityReply(JiboExperienceCatalog catalog, params string[] preferredSnippets)
|
private string SelectLegacyPersonalityReply(JiboExperienceCatalog catalog, params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
foreach (var snippet in preferredSnippets)
|
return ScriptedResponseDecisionBuilder.SelectLegacyPersonalityReply(catalog, randomizer, preferredSnippets);
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(snippet)) continue;
|
|
||||||
|
|
||||||
var match = catalog.PersonalityReplies.FirstOrDefault(reply =>
|
|
||||||
reply.Contains(snippet, StringComparison.OrdinalIgnoreCase));
|
|
||||||
if (!string.IsNullOrWhiteSpace(match)) return match;
|
|
||||||
}
|
|
||||||
|
|
||||||
return catalog.PersonalityReplies.Count == 0 ? string.Empty : randomizer.Choose(catalog.PersonalityReplies);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string SelectLegacyGreetingReply(JiboExperienceCatalog catalog, params string[] preferredSnippets)
|
private string SelectLegacyGreetingReply(JiboExperienceCatalog catalog, params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
foreach (var snippet in preferredSnippets)
|
return ScriptedResponseDecisionBuilder.SelectLegacyGreetingReply(catalog, randomizer, preferredSnippets);
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(snippet)) continue;
|
|
||||||
|
|
||||||
var match = catalog.GreetingReplies.FirstOrDefault(reply =>
|
|
||||||
reply.Contains(snippet, StringComparison.OrdinalIgnoreCase));
|
|
||||||
if (!string.IsNullOrWhiteSpace(match)) return match;
|
|
||||||
}
|
|
||||||
|
|
||||||
return catalog.GreetingReplies.Count == 0 ? string.Empty : randomizer.Choose(catalog.GreetingReplies);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string SelectLegacyReply(IReadOnlyList<string> replies, params string[] preferredSnippets)
|
private string SelectLegacyReply(IReadOnlyList<string> replies, params string[] preferredSnippets)
|
||||||
{
|
{
|
||||||
foreach (var snippet in preferredSnippets)
|
return ScriptedResponseDecisionBuilder.SelectLegacyReply(replies, randomizer, preferredSnippets);
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(snippet)) continue;
|
|
||||||
|
|
||||||
var match = replies.FirstOrDefault(reply =>
|
|
||||||
reply.Contains(snippet, StringComparison.OrdinalIgnoreCase));
|
|
||||||
if (!string.IsNullOrWhiteSpace(match)) return match;
|
|
||||||
}
|
|
||||||
|
|
||||||
return replies.Count == 0 ? string.Empty : randomizer.Choose(replies);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ResolveSemanticIntent(
|
private static string ResolveSemanticIntent(
|
||||||
|
|||||||
@@ -0,0 +1,140 @@
|
|||||||
|
using Jibo.Cloud.Application.Abstractions;
|
||||||
|
using Jibo.Runtime.Abstractions;
|
||||||
|
|
||||||
|
namespace Jibo.Cloud.Application.Services;
|
||||||
|
|
||||||
|
internal static class ScriptedResponseDecisionBuilder
|
||||||
|
{
|
||||||
|
internal static JiboInteractionDecision BuildScriptedPersonalityDecision(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
string intentName,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
return new JiboInteractionDecision(
|
||||||
|
intentName,
|
||||||
|
SelectLegacyPersonalityReply(catalog, randomizer, preferredSnippets),
|
||||||
|
ContextUpdates: BuildScriptedResponseContextUpdates());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static JiboInteractionDecision BuildScriptedFavoriteAnimalDecision(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
string intentName,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
return new JiboInteractionDecision(
|
||||||
|
intentName,
|
||||||
|
SelectLegacyReply(catalog.FavoriteAnimalReplies, randomizer, preferredSnippets),
|
||||||
|
ContextUpdates: BuildScriptedResponseContextUpdates());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static JiboInteractionDecision BuildScriptedGreetingDecision(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
string intentName,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
return new JiboInteractionDecision(
|
||||||
|
intentName,
|
||||||
|
SelectLegacyGreetingReply(catalog, randomizer, preferredSnippets),
|
||||||
|
ContextUpdates: BuildScriptedResponseContextUpdates());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static JiboInteractionDecision BuildScriptedHolidayDecision(
|
||||||
|
IReadOnlyList<string> replies,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
string intentName,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
return new JiboInteractionDecision(
|
||||||
|
intentName,
|
||||||
|
SelectLegacyReply(replies, randomizer, preferredSnippets),
|
||||||
|
ContextUpdates: BuildScriptedResponseContextUpdates());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static JiboInteractionDecision BuildScriptedHolidayTrackerDecision(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
string intentName,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
return new JiboInteractionDecision(
|
||||||
|
intentName,
|
||||||
|
SelectLegacyReply(catalog.HolidayTrackerReplies, randomizer, preferredSnippets),
|
||||||
|
ContextUpdates: BuildScriptedResponseContextUpdates());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static JiboInteractionDecision BuildScriptedHolidayGreetingDecision(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
string intentName,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
return new JiboInteractionDecision(
|
||||||
|
intentName,
|
||||||
|
SelectLegacyReply(catalog.HolidayGreetingReplies, randomizer, preferredSnippets),
|
||||||
|
ContextUpdates: BuildScriptedResponseContextUpdates());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static IDictionary<string, object?> BuildScriptedResponseContextUpdates()
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
[ChitchatStateMachine.StateMetadataKey] = "complete",
|
||||||
|
[ChitchatStateMachine.RouteMetadataKey] = "ScriptedResponse",
|
||||||
|
[ChitchatStateMachine.EmotionMetadataKey] = string.Empty
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string SelectLegacyPersonalityReply(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
foreach (var snippet in preferredSnippets)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(snippet)) continue;
|
||||||
|
|
||||||
|
var match = catalog.PersonalityReplies.FirstOrDefault(reply =>
|
||||||
|
reply.Contains(snippet, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (!string.IsNullOrWhiteSpace(match)) return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
return catalog.PersonalityReplies.Count == 0 ? string.Empty : randomizer.Choose(catalog.PersonalityReplies);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string SelectLegacyGreetingReply(
|
||||||
|
JiboExperienceCatalog catalog,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
foreach (var snippet in preferredSnippets)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(snippet)) continue;
|
||||||
|
|
||||||
|
var match = catalog.GreetingReplies.FirstOrDefault(reply =>
|
||||||
|
reply.Contains(snippet, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (!string.IsNullOrWhiteSpace(match)) return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
return catalog.GreetingReplies.Count == 0 ? string.Empty : randomizer.Choose(catalog.GreetingReplies);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string SelectLegacyReply(
|
||||||
|
IReadOnlyList<string> replies,
|
||||||
|
IJiboRandomizer randomizer,
|
||||||
|
params string[] preferredSnippets)
|
||||||
|
{
|
||||||
|
foreach (var snippet in preferredSnippets)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(snippet)) continue;
|
||||||
|
|
||||||
|
var match = replies.FirstOrDefault(reply =>
|
||||||
|
reply.Contains(snippet, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (!string.IsNullOrWhiteSpace(match)) return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
return replies.Count == 0 ? string.Empty : randomizer.Choose(replies);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user