Files

76 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace AxCopilot.Services.Agent;
public static class TaskDecomposer
{
private static readonly Regex StepPattern = new Regex("(?:^|\\n)\\s*(?:(?:Step\\s*)?(\\d+)[.):\\-]\\s*)(.+?)(?=\\n|$)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static List<string> ExtractSteps(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return new List<string>();
}
MatchCollection matchCollection = StepPattern.Matches(text);
if (matchCollection.Count < 2)
{
return new List<string>();
}
List<string> list = new List<string>();
int num = 0;
foreach (Match item in matchCollection)
{
if (int.TryParse(item.Groups[1].Value, out var result) && (result == num + 1 || num == 0))
{
string input = item.Groups[2].Value.Trim();
input = Regex.Replace(input, "\\*\\*(.+?)\\*\\*", "$1");
input = Regex.Replace(input, "\\*(.+?)\\*", "$1");
input = Regex.Replace(input, "`(.+?)`", "$1");
input = Regex.Replace(input, "\\[(.+?)\\]\\(.+?\\)", "$1");
input = input.TrimEnd(':', ' ');
if (input.Length >= 3 && input.Length <= 300)
{
list.Add(input);
num = result;
}
}
}
return (list.Count >= 2) ? list.Take(20).ToList() : new List<string>();
}
public static int EstimateCurrentStep(List<string> steps, string toolName, string toolSummary, int lastStep)
{
if (steps.Count == 0)
{
return 0;
}
for (int i = lastStep; i < steps.Count; i++)
{
string text = steps[i].ToLowerInvariant();
string value = toolName.ToLowerInvariant();
string summary = toolSummary.ToLowerInvariant();
if (text.Contains(value) || ContainsKeywordOverlap(text, summary))
{
return i;
}
}
return Math.Min(lastStep + 1, steps.Count - 1);
}
private static bool ContainsKeywordOverlap(string stepText, string summary)
{
if (string.IsNullOrEmpty(summary))
{
return false;
}
IEnumerable<string> source = (from w in summary.Split(' ', '/', '\\', '.', ',', ':', ';', '(', ')')
where w.Length >= 3
select w).Take(5);
return source.Any((string w) => stepText.Contains(w));
}
}