-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAnswerStepHandler.cs
More file actions
58 lines (47 loc) · 2.24 KB
/
AnswerStepHandler.cs
File metadata and controls
58 lines (47 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml.Wordprocessing;
using MaIN.Domain.Entities;
using MaIN.Services.Constants;
using MaIN.Services.Services.Abstract;
using MaIN.Services.Services.Models;
using MaIN.Services.Services.Models.Commands;
using MaIN.Services.Services.Steps.Commands;
namespace MaIN.Services.Services.Steps;
public class AnswerStepHandler(ICommandDispatcher commandDispatcher) : IStepHandler
{
public string StepName => "ANSWER";
public async Task<StepResult> Handle(StepContext context)
{
await context.NotifyProgress("true", context.Agent.Id, null, context.Agent.CurrentBehaviour, StepName);
var useMemory = context.Arguments.Contains("USE_MEMORY");
var useKnowledge = context.Arguments.Contains("USE_KNOWLEDGE");
var useKnowledgeAlways = context.Arguments.Contains("USE_KNOWLEDGE+ALWAYS");
var answerCommand = new AnswerCommand
{
Chat = StepHandlerExtensions.EnsureUserMessageReadiness(context.Chat),
KnowledgeUsage = useKnowledgeAlways ?
KnowledgeUsage.AlwaysUseKnowledge
: useKnowledge ? KnowledgeUsage.UseKnowledge
: useMemory ? KnowledgeUsage.UseMemory
: KnowledgeUsage.None,
Knowledge = context.Knowledge,
AgentId = context.Agent.Id,
Callback = context.Callback
};
var answerResponse = await commandDispatcher.DispatchAsync(answerCommand);
if (answerResponse == null)
throw new Exception("Answer command failed"); //TODO proper candidate for custom exception
var filterVal = GetFilter(answerResponse.Content);
if (!string.IsNullOrEmpty(filterVal))
context.Chat.Properties.TryAdd("data_filter", filterVal);
answerResponse.Time = DateTime.Now;
context.Chat.Messages.Add(answerResponse);
return new StepResult { Chat = context.Chat, RedirectMessage = answerResponse };
}
private static string? GetFilter(string? content)
{
var pattern = @"filter:?:?\{(.*?)\}";
var match = Regex.Match(content!, pattern);
return match.Success ? match.Groups[1].Value : null;
}
}