-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRulePlanner.cs
More file actions
160 lines (137 loc) · 5.05 KB
/
RulePlanner.cs
File metadata and controls
160 lines (137 loc) · 5.05 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Path: Services/RulePlanner.cs
using System.Text.Json;
using System.Text.RegularExpressions;
using DevMiniOS.Interfaces;
using DevMiniOS.Models;
namespace DevMiniOS.Services;
/// <summary>
/// Implements a rule-based planner using regular expressions and intent scoring to map natural language input to commands,
/// with a catch-all fallback for unhandled input.
/// </summary>
public class RulePlanner : IPlanner
{
private List<Regex> _controllerRules = new();
public RulePlanner()
{
// Initialize rules with synonym expansion.
InitializeRules();
}
private void InitializeRules()
{
_controllerRules = new()
{
CreateRegexFromPattern("make a (.*?) controller with full crud"),
};
}
private Regex CreateRegexFromPattern(string pattern)
{
var tokens = pattern.Split(' ');
var expandedTokens = TextNormalizer.ExpandSynonyms(tokens).Distinct().ToArray();
var regexPattern = string.Join(" ", expandedTokens);
regexPattern = Regex.Replace(regexPattern, @"\b(create|read|update|delete)\b", "($1)");
regexPattern = Regex.Replace(regexPattern, @"\b(create|generate)\b", "($1)");
regexPattern = Regex.Replace(regexPattern, @"\b(register|inject)\b", "($1)");
regexPattern = Regex.Replace(regexPattern, @"\b(dependency injection)\b", "($1)");
return new Regex(regexPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
private class IntentRule
{
public string Op { get; set; } = "";
public Dictionary<string, int> Keywords { get; set; } = new();
public Regex? SlotRegex { get; set; }
}
private readonly List<IntentRule> _intentRules = new()
{
new IntentRule
{
Op = "make_model",
Keywords = new Dictionary<string, int> { { "model", 5 }, { "class", 3 }, { "entity", 3 }, { "create", 2 } },
SlotRegex = new Regex(@"\((.*?)\)", RegexOptions.Compiled | RegexOptions.IgnoreCase) // Extract fields inside parentheses
},
new IntentRule
{
Op = "wire_di",
Keywords = new Dictionary<string, int> { { "wire", 5 }, { "inject", 5 }, { "dependency", 3 }, { "register", 2 }, { "di", 4 } }
},
new IntentRule
{
Op = "add_tests",
Keywords = new Dictionary<string, int> { { "test", 5 }, { "tests", 5 }, { "unit", 3 }, { "integration", 3 } }
}
};
private Command? ClassifyIntent(string normalizedInput)
{
IntentRule? bestRule = null;
int bestScore = 0;
foreach (var rule in _intentRules)
{
int score = 0;
foreach (var keyword in rule.Keywords)
{
if (normalizedInput.Contains(keyword.Key))
{
score += keyword.Value;
}
}
if (score > bestScore)
{
bestScore = score;
bestRule = rule;
}
}
if (bestRule != null && bestScore > 5) // Threshold
{
string parameters = "{}";
if (bestRule.SlotRegex != null)
{
var match = bestRule.SlotRegex.Match(normalizedInput);
if (match.Success)
{
parameters = JsonSerializer.Serialize(new { slots = match.Groups[1].Value });
}
}
return new Command { Op = bestRule.Op, Name = "natural_language", Parameters = parameters };
}
return null;
}
/// <inheritdoc />
public async Task<Plan> CreatePlanAsync(string naturalLanguage)
{
// Normalize the input
string normalizedInput = TextNormalizer.Normalize(naturalLanguage);
foreach (var rule in _controllerRules)
{
var match = rule.Match(normalizedInput);
if (match.Success)
{
var controllerName = match.Groups[1].Value;
return new Plan
{
Commands = new List<Command>
{
new() { Op = "make_controller", Name = controllerName, Parameters = "{}" }
}
};
}
}
// If no regex matches, try intent classification
var intentCommand = ClassifyIntent(normalizedInput);
if (intentCommand != null)
{
return new Plan { Commands = new List<Command> { intentCommand } };
}
// Catch-all Fallback
return new Plan
{
Commands = new List<Command>
{
new Command
{
Op = "assist_request",
Name = "natural_language",
Parameters = JsonSerializer.Serialize(new { text = naturalLanguage, hints = "unclear intent" })
}
}
};
}
}